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 * 2024-04-12 shelton first version
9 */
10
11 #include <rtthread.h>
12 #include "at32a403a.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 #ifdef BSP_USING_HARD_I2C3
167 if(I2C3 == i2c_x)
168 {
169 crm_periph_clock_enable(CRM_I2C3_PERIPH_CLOCK, TRUE);
170 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
171 crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
172
173 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
174 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
175 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
176 gpio_init_struct.gpio_pins = GPIO_PINS_8;
177 gpio_init(GPIOA, &gpio_init_struct);
178 gpio_init_struct.gpio_pins = GPIO_PINS_9;
179 gpio_init(GPIOC, &gpio_init_struct);
180 }
181 #endif
182 /* add others */
183 }
184 #endif /* BSP_USING_HARD_I2C */
185
186 #ifdef BSP_USING_SDIO
at32_msp_sdio_init(void * instance)187 void at32_msp_sdio_init(void *instance)
188 {
189 gpio_init_type gpio_init_struct;
190 sdio_type *sdio_x = (sdio_type *)instance;
191
192 gpio_default_para_init(&gpio_init_struct);
193 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
194 if(sdio_x == SDIO1)
195 {
196 /* if used dma ... */
197 crm_periph_clock_enable(CRM_DMA2_PERIPH_CLOCK, TRUE);
198 crm_periph_clock_enable(CRM_SDIO1_PERIPH_CLOCK, TRUE);
199 crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
200 crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
201
202 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
203 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
204 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
205 gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_9 | GPIO_PINS_10 | GPIO_PINS_11 | GPIO_PINS_12;
206 gpio_init(GPIOC, &gpio_init_struct);
207
208 gpio_init_struct.gpio_pins = GPIO_PINS_2;
209 gpio_init(GPIOD, &gpio_init_struct);
210 }
211 }
212 #endif /* BSP_USING_SDIO */
213
214 #ifdef BSP_USING_PWM
at32_msp_tmr_init(void * instance)215 void at32_msp_tmr_init(void *instance)
216 {
217 gpio_init_type gpio_init_struct;
218 tmr_type *tmr_x = (tmr_type *)instance;
219
220 gpio_default_para_init(&gpio_init_struct);
221 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
222 if(tmr_x == TMR1)
223 {
224 /* tmr1 clock enable */
225 crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE);
226 /* gpioa clock enable */
227 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
228
229 /* gpioa configuration: tmr1 channel1 and channel4 as alternate function push-pull */
230 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
231 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
232 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
233 gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_11;
234 gpio_init(GPIOA, &gpio_init_struct);
235 }
236
237 if(tmr_x == TMR2)
238 {
239 /* tmr2 clock enable */
240 crm_periph_clock_enable(CRM_TMR2_PERIPH_CLOCK, TRUE);
241 /* gpioa clock enable */
242 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
243
244 /* gpioa configuration: tmr1 channel1 and channel2 as alternate function push-pull */
245 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
246 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
247 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
248 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1;
249 gpio_init(GPIOA, &gpio_init_struct);
250 }
251 /* add others */
252 }
253 #endif /* BSP_USING_PWM */
254
255 #ifdef BSP_USING_ADC
at32_msp_adc_init(void * instance)256 void at32_msp_adc_init(void *instance)
257 {
258 gpio_init_type gpio_init_struct;
259 adc_type *adc_x = (adc_type *)instance;
260
261 gpio_default_para_init(&gpio_init_struct);
262 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
263 #ifdef BSP_USING_ADC1
264 if(adc_x == ADC1)
265 {
266 /* adc1 & gpio clock enable */
267 crm_periph_clock_enable(CRM_ADC1_PERIPH_CLOCK, TRUE);
268 crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
269
270 /* configure adc channel as analog input */
271 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_2 | GPIO_PINS_3 | GPIO_PINS_4 | GPIO_PINS_5;
272 gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
273 gpio_init(GPIOC, &gpio_init_struct);
274 }
275 #endif
276
277 #ifdef BSP_USING_ADC2
278 if(adc_x == ADC2)
279 {
280 /* adc2 & gpio clock enable */
281 crm_periph_clock_enable(CRM_ADC2_PERIPH_CLOCK, TRUE);
282 crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
283
284 /* configure adc channel as analog input */
285 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_2 | GPIO_PINS_3 | GPIO_PINS_4 | GPIO_PINS_5;
286 gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
287 gpio_init(GPIOC, &gpio_init_struct);
288 }
289 #endif
290 }
291 #endif /* BSP_USING_ADC */
292
293 #ifdef BSP_USING_HWTIMER
at32_msp_hwtmr_init(void * instance)294 void at32_msp_hwtmr_init(void *instance)
295 {
296 tmr_type *tmr_x = (tmr_type *)instance;
297
298 #ifdef BSP_USING_HWTMR3
299 if(tmr_x == TMR3)
300 {
301 /* tmr3 clock enable */
302 crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE);
303 }
304 #endif
305
306 #ifdef BSP_USING_HWTMR4
307 if(tmr_x == TMR4)
308 {
309 /* tmr4 clock enable */
310 crm_periph_clock_enable(CRM_TMR4_PERIPH_CLOCK, TRUE);
311 }
312 #endif
313
314 #ifdef BSP_USING_HWTMR5
315 if(tmr_x == TMR5)
316 {
317 /* tmr5 clock enable */
318 crm_periph_clock_enable(CRM_TMR5_PERIPH_CLOCK, TRUE);
319 }
320 #endif
321 }
322 #endif
323
324 #ifdef BSP_USING_CAN
at32_msp_can_init(void * instance)325 void at32_msp_can_init(void *instance)
326 {
327 gpio_init_type gpio_init_struct;
328 can_type *can_x = (can_type *)instance;
329
330 gpio_default_para_init(&gpio_init_struct);
331 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
332 #ifdef BSP_USING_CAN1
333 if(CAN1 == can_x)
334 {
335 crm_periph_clock_enable(CRM_CAN1_PERIPH_CLOCK, TRUE);
336 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
337 crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
338
339 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
340 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
341 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
342 gpio_init_struct.gpio_pins = GPIO_PINS_9;
343 gpio_init(GPIOB, &gpio_init_struct);
344 gpio_pin_remap_config(CAN1_GMUX_0010, TRUE);
345
346 gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
347 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
348 gpio_init_struct.gpio_pins = GPIO_PINS_8;
349 gpio_init(GPIOB, &gpio_init_struct);
350 }
351 #endif
352 #ifdef BSP_USING_CAN2
353 if(CAN2 == can_x)
354 {
355 crm_periph_clock_enable(CRM_CAN2_PERIPH_CLOCK, TRUE);
356 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
357 crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
358
359 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
360 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
361 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
362 gpio_init_struct.gpio_pins = GPIO_PINS_6;
363 gpio_init(GPIOB, &gpio_init_struct);
364 gpio_pin_remap_config(CAN2_GMUX_0001, TRUE);
365
366 gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
367 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
368 gpio_init_struct.gpio_pins = GPIO_PINS_5;
369 gpio_init(GPIOB, &gpio_init_struct);
370 }
371 #endif
372 }
373 #endif /* BSP_USING_CAN */
374
375 #ifdef BSP_USING_USBD
at32_msp_usb_init(void * instance)376 void at32_msp_usb_init(void *instance)
377 {
378 usb_clk48_s clk_s;
379
380 /* default usb clock source from hick */
381 clk_s = USB_CLK_HICK;
382
383 /* enable usb clock */
384 crm_periph_clock_enable(CRM_USB_PERIPH_CLOCK, TRUE);
385
386 if(clk_s == USB_CLK_HICK)
387 {
388 crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_HICK);
389
390 /* enable the acc calibration ready interrupt */
391 crm_periph_clock_enable(CRM_ACC_PERIPH_CLOCK, TRUE);
392
393 /* update the c1\c2\c3 value */
394 acc_write_c1(7980);
395 acc_write_c2(8000);
396 acc_write_c3(8020);
397
398 /* open acc calibration */
399 acc_calibration_mode_enable(ACC_CAL_HICKTRIM, TRUE);
400 }
401 else
402 {
403 switch(system_core_clock)
404 {
405 /* 48MHz */
406 case 48000000:
407 crm_usb_clock_div_set(CRM_USB_DIV_1);
408 break;
409 /* 72MHz */
410 case 72000000:
411 crm_usb_clock_div_set(CRM_USB_DIV_1_5);
412 break;
413 /* 96MHz */
414 case 96000000:
415 crm_usb_clock_div_set(CRM_USB_DIV_2);
416 break;
417 /* 120MHz */
418 case 120000000:
419 crm_usb_clock_div_set(CRM_USB_DIV_2_5);
420 break;
421 /* 144MHz */
422 case 144000000:
423 crm_usb_clock_div_set(CRM_USB_DIV_3);
424 break;
425 /* 168MHz */
426 case 168000000:
427 crm_usb_clock_div_set(CRM_USB_DIV_3_5);
428 break;
429 /* 192MHz */
430 case 192000000:
431 crm_usb_clock_div_set(CRM_USB_DIV_4);
432 break;
433 default:
434 break;
435 }
436 }
437 }
438 #endif /* BSP_USING_USBD */
439
440 #ifdef BSP_USING_DAC
at32_msp_dac_init(void * instance)441 void at32_msp_dac_init(void *instance)
442 {
443 gpio_init_type gpio_init_struct;
444 dac_type *dac_x = (dac_type *)instance;
445
446 gpio_default_para_init(&gpio_init_struct);
447 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
448 #ifdef BSP_USING_DAC1
449 if(dac_x == DAC)
450 {
451 /* dac & gpio clock enable */
452 crm_periph_clock_enable(CRM_DAC_PERIPH_CLOCK, TRUE);
453 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
454
455 /* configure adc channel as analog output */
456 gpio_init_struct.gpio_pins = GPIO_PINS_4 | GPIO_PINS_5;
457 gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
458 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
459 gpio_init(GPIOA, &gpio_init_struct);
460 }
461 #endif
462 }
463 #endif /* BSP_USING_DAC */
464