1 /*
2 * Copyright (c) 2006-2018, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2022-05-10 shelton first version
9 */
10
11 #include <rtthread.h>
12 #include "at32f413.h"
13 #include "at32_msp.h"
14
15 #ifdef BSP_USING_UART
at32_msp_usart_init(void * instance)16 void at32_msp_usart_init(void *instance)
17 {
18 gpio_init_type gpio_init_struct;
19 usart_type *usart_x = (usart_type *)instance;
20
21 gpio_default_para_init(&gpio_init_struct);
22 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
23 #ifdef BSP_USING_UART1
24 if(USART1 == usart_x)
25 {
26 crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
27 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
28
29 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
30 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
31 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
32 gpio_init_struct.gpio_pins = GPIO_PINS_9;
33 gpio_init(GPIOA, &gpio_init_struct);
34
35 gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
36 gpio_init_struct.gpio_pins = GPIO_PINS_10;
37 gpio_init(GPIOA, &gpio_init_struct);
38 }
39 #endif
40 #ifdef BSP_USING_UART2
41 if(USART2 == usart_x)
42 {
43 crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, TRUE);
44 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
45
46 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
47 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
48 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
49 gpio_init_struct.gpio_pins = GPIO_PINS_2;
50 gpio_init(GPIOA, &gpio_init_struct);
51
52 gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
53 gpio_init_struct.gpio_pins = GPIO_PINS_3;
54 gpio_init(GPIOA, &gpio_init_struct);
55 }
56 #endif
57 #ifdef BSP_USING_UART3
58 if(USART3 == usart_x)
59 {
60 crm_periph_clock_enable(CRM_USART3_PERIPH_CLOCK, TRUE);
61 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
62
63 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
64 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
65 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
66 gpio_init_struct.gpio_pins = GPIO_PINS_10;
67 gpio_init(GPIOB, &gpio_init_struct);
68
69 gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
70 gpio_init_struct.gpio_pins = GPIO_PINS_11;
71 gpio_init(GPIOB, &gpio_init_struct);
72 }
73 #endif
74 /* add others */
75 }
76 #endif /* BSP_USING_SERIAL */
77
78 #ifdef BSP_USING_SPI
at32_msp_spi_init(void * instance)79 void at32_msp_spi_init(void *instance)
80 {
81 gpio_init_type gpio_init_struct;
82 spi_type *spi_x = (spi_type *)instance;
83
84 gpio_default_para_init(&gpio_init_struct);
85 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
86 #ifdef BSP_USING_SPI1
87 if(SPI1 == spi_x)
88 {
89 crm_periph_clock_enable(CRM_SPI1_PERIPH_CLOCK, TRUE);
90 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
91
92 gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
93 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
94 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
95 gpio_init_struct.gpio_pins = GPIO_PINS_4;
96 gpio_init(GPIOA, &gpio_init_struct);
97
98 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
99 gpio_init_struct.gpio_pins = GPIO_PINS_5 | GPIO_PINS_7;
100 gpio_init(GPIOA, &gpio_init_struct);
101
102 gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
103 gpio_init_struct.gpio_pins = GPIO_PINS_6;
104 gpio_init(GPIOA, &gpio_init_struct);
105 }
106 #endif
107 #ifdef BSP_USING_SPI2
108 if(SPI2 == spi_x)
109 {
110 crm_periph_clock_enable(CRM_SPI2_PERIPH_CLOCK, TRUE);
111 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
112
113 gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
114 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
115 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
116 gpio_init_struct.gpio_pins = GPIO_PINS_12;
117 gpio_init(GPIOB, &gpio_init_struct);
118
119 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
120 gpio_init_struct.gpio_pins = GPIO_PINS_13 | GPIO_PINS_15;
121 gpio_init(GPIOB, &gpio_init_struct);
122
123 gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
124 gpio_init_struct.gpio_pins = GPIO_PINS_14;
125 gpio_init(GPIOB, &gpio_init_struct);
126 }
127 #endif
128 /* add others */
129 }
130 #endif /* BSP_USING_SPI */
131
132 #ifdef BSP_USING_HARD_I2C
at32_msp_i2c_init(void * instance)133 void at32_msp_i2c_init(void *instance)
134 {
135 gpio_init_type gpio_init_struct;
136 i2c_type *i2c_x = (i2c_type *)instance;
137
138 gpio_default_para_init(&gpio_init_struct);
139 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
140 #ifdef BSP_USING_HARD_I2C1
141 if(I2C1 == i2c_x)
142 {
143 crm_periph_clock_enable(CRM_I2C1_PERIPH_CLOCK, TRUE);
144 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
145
146 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
147 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
148 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
149 gpio_init_struct.gpio_pins = GPIO_PINS_6 | GPIO_PINS_7;
150 gpio_init(GPIOB, &gpio_init_struct);
151 }
152 #endif
153 #ifdef BSP_USING_HARD_I2C2
154 if(I2C2 == i2c_x)
155 {
156 crm_periph_clock_enable(CRM_I2C2_PERIPH_CLOCK, TRUE);
157 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
158
159 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
160 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
161 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
162 gpio_init_struct.gpio_pins = GPIO_PINS_10 | GPIO_PINS_11;
163 gpio_init(GPIOB, &gpio_init_struct);
164 }
165 #endif
166 /* add others */
167 }
168 #endif /* BSP_USING_HARD_I2C */
169
170 #ifdef BSP_USING_SDIO
at32_msp_sdio_init(void * instance)171 void at32_msp_sdio_init(void *instance)
172 {
173 gpio_init_type gpio_init_struct;
174 sdio_type *sdio_x = (sdio_type *)instance;
175
176 gpio_default_para_init(&gpio_init_struct);
177 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
178 if(sdio_x == SDIO1)
179 {
180 /* if used dma ... */
181 crm_periph_clock_enable(CRM_DMA2_PERIPH_CLOCK, TRUE);
182 crm_periph_clock_enable(CRM_SDIO1_PERIPH_CLOCK, TRUE);
183 crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
184 crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
185
186 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
187 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
188 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
189 gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_9 | GPIO_PINS_10 | GPIO_PINS_11 | GPIO_PINS_12;
190 gpio_init(GPIOC, &gpio_init_struct);
191
192 gpio_init_struct.gpio_pins = GPIO_PINS_2;
193 gpio_init(GPIOD, &gpio_init_struct);
194 }
195 }
196 #endif /* BSP_USING_SDIO */
197
198 #ifdef BSP_USING_PWM
at32_msp_tmr_init(void * instance)199 void at32_msp_tmr_init(void *instance)
200 {
201 gpio_init_type gpio_init_struct;
202 tmr_type *tmr_x = (tmr_type *)instance;
203
204 gpio_default_para_init(&gpio_init_struct);
205 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
206 if(tmr_x == TMR1)
207 {
208 /* tmr1 clock enable */
209 crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE);
210 /* gpioa clock enable */
211 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
212
213 /* gpioa configuration: tmr1 channel1 and channel4 as alternate function push-pull */
214 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
215 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
216 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
217 gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_11;
218 gpio_init(GPIOA, &gpio_init_struct);
219 }
220
221 if(tmr_x == TMR2)
222 {
223 /* tmr2 clock enable */
224 crm_periph_clock_enable(CRM_TMR2_PERIPH_CLOCK, TRUE);
225 /* gpioa clock enable */
226 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
227
228 /* gpioa configuration: tmr1 channel1 and channel2 as alternate function push-pull */
229 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
230 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
231 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
232 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1;
233 gpio_init(GPIOA, &gpio_init_struct);
234 }
235 /* add others */
236 }
237 #endif /* BSP_USING_PWM */
238
239 #ifdef BSP_USING_ADC
at32_msp_adc_init(void * instance)240 void at32_msp_adc_init(void *instance)
241 {
242 gpio_init_type gpio_init_struct;
243 adc_type *adc_x = (adc_type *)instance;
244
245 gpio_default_para_init(&gpio_init_struct);
246 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
247 #ifdef BSP_USING_ADC1
248 if(adc_x == ADC1)
249 {
250 /* adc1 & gpio clock enable */
251 crm_periph_clock_enable(CRM_ADC1_PERIPH_CLOCK, TRUE);
252 crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
253
254 /* configure adc channel as analog input */
255 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_2 | GPIO_PINS_3 | GPIO_PINS_4 | GPIO_PINS_5;
256 gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
257 gpio_init(GPIOC, &gpio_init_struct);
258 }
259 #endif
260
261 #ifdef BSP_USING_ADC2
262 if(adc_x == ADC2)
263 {
264 /* adc2 & gpio clock enable */
265 crm_periph_clock_enable(CRM_ADC2_PERIPH_CLOCK, TRUE);
266 crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
267
268 /* configure adc channel as analog input */
269 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_2 | GPIO_PINS_3 | GPIO_PINS_4 | GPIO_PINS_5;
270 gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
271 gpio_init(GPIOC, &gpio_init_struct);
272 }
273 #endif
274 }
275 #endif /* BSP_USING_ADC */
276
277 #ifdef BSP_USING_HWTIMER
at32_msp_hwtmr_init(void * instance)278 void at32_msp_hwtmr_init(void *instance)
279 {
280 tmr_type *tmr_x = (tmr_type *)instance;
281
282 #ifdef BSP_USING_HWTMR3
283 if(tmr_x == TMR3)
284 {
285 /* tmr3 clock enable */
286 crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE);
287 }
288 #endif
289
290 #ifdef BSP_USING_HWTMR4
291 if(tmr_x == TMR4)
292 {
293 /* tmr4 clock enable */
294 crm_periph_clock_enable(CRM_TMR4_PERIPH_CLOCK, TRUE);
295 }
296 #endif
297
298 #ifdef BSP_USING_HWTMR5
299 if(tmr_x == TMR5)
300 {
301 /* tmr5 clock enable */
302 crm_periph_clock_enable(CRM_TMR5_PERIPH_CLOCK, TRUE);
303 }
304 #endif
305 }
306 #endif
307
308 #ifdef BSP_USING_CAN
at32_msp_can_init(void * instance)309 void at32_msp_can_init(void *instance)
310 {
311 gpio_init_type gpio_init_struct;
312 can_type *can_x = (can_type *)instance;
313
314 gpio_default_para_init(&gpio_init_struct);
315 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
316 #ifdef BSP_USING_CAN1
317 if(CAN1 == can_x)
318 {
319 crm_periph_clock_enable(CRM_CAN1_PERIPH_CLOCK, TRUE);
320 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
321 crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
322
323 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
324 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
325 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
326 gpio_init_struct.gpio_pins = GPIO_PINS_9;
327 gpio_init(GPIOB, &gpio_init_struct);
328 gpio_pin_remap_config(CAN1_GMUX_0010, TRUE);
329
330 gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
331 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
332 gpio_init_struct.gpio_pins = GPIO_PINS_8;
333 gpio_init(GPIOB, &gpio_init_struct);
334 }
335 #endif
336 #ifdef BSP_USING_CAN2
337 if(CAN2 == can_x)
338 {
339 crm_periph_clock_enable(CRM_CAN2_PERIPH_CLOCK, TRUE);
340 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
341 crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
342
343 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
344 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
345 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
346 gpio_init_struct.gpio_pins = GPIO_PINS_6;
347 gpio_init(GPIOB, &gpio_init_struct);
348 gpio_pin_remap_config(CAN2_GMUX_0001, TRUE);
349
350 gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
351 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
352 gpio_init_struct.gpio_pins = GPIO_PINS_5;
353 gpio_init(GPIOB, &gpio_init_struct);
354 }
355 #endif
356 }
357 #endif /* BSP_USING_CAN */
358
359 #ifdef BSP_USING_USBD
at32_msp_usb_init(void * instance)360 void at32_msp_usb_init(void *instance)
361 {
362 usb_clk48_s clk_s;
363
364 /* default usb clock source from hick */
365 clk_s = USB_CLK_HICK;
366
367 /* enable usb clock */
368 crm_periph_clock_enable(CRM_USB_PERIPH_CLOCK, TRUE);
369
370 if(clk_s == USB_CLK_HICK)
371 {
372 crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_HICK);
373
374 /* enable the acc calibration ready interrupt */
375 crm_periph_clock_enable(CRM_ACC_PERIPH_CLOCK, TRUE);
376
377 /* update the c1\c2\c3 value */
378 acc_write_c1(7980);
379 acc_write_c2(8000);
380 acc_write_c3(8020);
381
382 /* open acc calibration */
383 acc_calibration_mode_enable(ACC_CAL_HICKTRIM, TRUE);
384 }
385 else
386 {
387 switch(system_core_clock)
388 {
389 /* 48MHz */
390 case 48000000:
391 crm_usb_clock_div_set(CRM_USB_DIV_1);
392 break;
393 /* 72MHz */
394 case 72000000:
395 crm_usb_clock_div_set(CRM_USB_DIV_1_5);
396 break;
397 /* 96MHz */
398 case 96000000:
399 crm_usb_clock_div_set(CRM_USB_DIV_2);
400 break;
401 /* 120MHz */
402 case 120000000:
403 crm_usb_clock_div_set(CRM_USB_DIV_2_5);
404 break;
405 /* 144MHz */
406 case 144000000:
407 crm_usb_clock_div_set(CRM_USB_DIV_3);
408 break;
409 /* 168MHz */
410 case 168000000:
411 crm_usb_clock_div_set(CRM_USB_DIV_3_5);
412 break;
413 /* 192MHz */
414 case 192000000:
415 crm_usb_clock_div_set(CRM_USB_DIV_4);
416 break;
417 default:
418 break;
419 }
420 }
421 }
422
423 #endif /* BSP_USING_USBD */
424