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-12-28 shelton first version
9 */
10
11 #include <rtthread.h>
12 #include "at32f45x.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(usart_x == USART1)
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 | GPIO_PINS_10;
33 gpio_init(GPIOA, &gpio_init_struct);
34
35 gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE9, GPIO_MUX_7);
36 gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE10, GPIO_MUX_7);
37 }
38 #endif
39 #ifdef BSP_USING_UART2
40 if(usart_x == USART2)
41 {
42 crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, TRUE);
43 crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
44
45 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
46 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
47 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
48 gpio_init_struct.gpio_pins = GPIO_PINS_5 | GPIO_PINS_6;
49 gpio_init(GPIOD, &gpio_init_struct);
50
51 gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE5, GPIO_MUX_7);
52 gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE6, GPIO_MUX_7);
53 }
54 #endif
55 #ifdef BSP_USING_UART3
56 if(usart_x == USART3)
57 {
58 crm_periph_clock_enable(CRM_USART3_PERIPH_CLOCK, TRUE);
59 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
60
61 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
62 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
63 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
64 gpio_init_struct.gpio_pins = GPIO_PINS_10 | GPIO_PINS_11;
65 gpio_init(GPIOB, &gpio_init_struct);
66
67 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE10, GPIO_MUX_7);
68 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE11, GPIO_MUX_7);
69 }
70 #endif
71 /* add others */
72 }
73 #endif /* BSP_USING_SERIAL */
74
75 #ifdef BSP_USING_SPI
at32_msp_spi_init(void * instance)76 void at32_msp_spi_init(void *instance)
77 {
78 gpio_init_type gpio_init_struct;
79 spi_type *spi_x = (spi_type *)instance;
80
81 gpio_default_para_init(&gpio_init_struct);
82 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
83 #ifdef BSP_USING_SPI1
84 if(spi_x == SPI1)
85 {
86 crm_periph_clock_enable(CRM_SPI1_PERIPH_CLOCK, TRUE);
87 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
88
89 gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
90 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
91 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
92 gpio_init_struct.gpio_pins = GPIO_PINS_4;
93 gpio_init(GPIOA, &gpio_init_struct);
94
95 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
96 gpio_init_struct.gpio_pins = GPIO_PINS_5 | GPIO_PINS_6 | GPIO_PINS_7;
97 gpio_init(GPIOA, &gpio_init_struct);
98
99 gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE5, GPIO_MUX_5);
100 gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE6, GPIO_MUX_5);
101 gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE7, GPIO_MUX_5);
102 }
103 #endif
104 #ifdef BSP_USING_SPI2
105 if(spi_x == SPI2)
106 {
107 crm_periph_clock_enable(CRM_SPI2_PERIPH_CLOCK, TRUE);
108 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
109
110 gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
111 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
112 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
113 gpio_init_struct.gpio_pins = GPIO_PINS_12;
114 gpio_init(GPIOB, &gpio_init_struct);
115
116 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
117 gpio_init_struct.gpio_pins = GPIO_PINS_13 | GPIO_PINS_14 | GPIO_PINS_15;
118 gpio_init(GPIOB, &gpio_init_struct);
119
120 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE13, GPIO_MUX_5);
121 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE14, GPIO_MUX_5);
122 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE15, GPIO_MUX_5);
123 }
124 #endif
125 /* add others */
126 }
127 #endif /* BSP_USING_SPI */
128
129 #ifdef BSP_USING_HARD_I2C
at32_msp_i2c_init(void * instance)130 void at32_msp_i2c_init(void *instance)
131 {
132 gpio_init_type gpio_init_struct;
133 i2c_type *i2c_x = (i2c_type *)instance;
134
135 gpio_default_para_init(&gpio_init_struct);
136 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
137 #ifdef BSP_USING_HARD_I2C1
138 if(I2C1 == i2c_x)
139 {
140 crm_periph_clock_enable(CRM_I2C1_PERIPH_CLOCK, TRUE);
141 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
142
143 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
144 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
145 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
146 gpio_init_struct.gpio_pins = GPIO_PINS_6 | GPIO_PINS_7;
147 gpio_init(GPIOB, &gpio_init_struct);
148
149 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE6, GPIO_MUX_4);
150 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE7, GPIO_MUX_4);
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 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE10, GPIO_MUX_4);
166 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE11, GPIO_MUX_4);
167 }
168 #endif
169 #ifdef BSP_USING_HARD_I2C3
170 if(I2C3 == i2c_x)
171 {
172 crm_periph_clock_enable(CRM_I2C3_PERIPH_CLOCK, TRUE);
173 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
174 crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
175
176 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
177 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
178 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
179 gpio_init_struct.gpio_pins = GPIO_PINS_8;
180 gpio_init(GPIOA, &gpio_init_struct);
181 gpio_init_struct.gpio_pins = GPIO_PINS_9;
182 gpio_init(GPIOC, &gpio_init_struct);
183
184 gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE8, GPIO_MUX_4);
185 gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE9, GPIO_MUX_4);
186 }
187 #endif
188 /* add others */
189 }
190 #endif /* BSP_USING_HARD_I2C */
191
192 #ifdef BSP_USING_SDIO
at32_msp_sdio_init(void * instance)193 void at32_msp_sdio_init(void *instance)
194 {
195 gpio_init_type gpio_init_struct;
196 sdio_type *sdio_x = (sdio_type *)instance;
197
198 gpio_default_para_init(&gpio_init_struct);
199 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
200 if(sdio_x == SDIO1)
201 {
202 /* if used dma ... */
203 crm_periph_clock_enable(CRM_DMA2_PERIPH_CLOCK, TRUE);
204 crm_periph_clock_enable(CRM_SDIO1_PERIPH_CLOCK, TRUE);
205 crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
206 crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
207
208 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
209 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
210 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
211 gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_9 | GPIO_PINS_10 | GPIO_PINS_11 | GPIO_PINS_12;
212 gpio_init(GPIOC, &gpio_init_struct);
213
214 gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE8, GPIO_MUX_12);
215 gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE9, GPIO_MUX_12);
216 gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE10, GPIO_MUX_12);
217 gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE11, GPIO_MUX_12);
218 gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE12, GPIO_MUX_12);
219
220 gpio_init_struct.gpio_pins = GPIO_PINS_2;
221 gpio_init(GPIOD, &gpio_init_struct);
222
223 gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE2, GPIO_MUX_12);
224 }
225 }
226 #endif /* BSP_USING_SDIO */
227
228 #ifdef BSP_USING_PWM
at32_msp_tmr_init(void * instance)229 void at32_msp_tmr_init(void *instance)
230 {
231 gpio_init_type gpio_init_struct;
232 tmr_type *tmr_x = (tmr_type *)instance;
233
234 gpio_default_para_init(&gpio_init_struct);
235 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
236 if(tmr_x == TMR1)
237 {
238 /* tmr1 clock enable */
239 crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE);
240 /* gpioa clock enable */
241 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
242
243 /* gpioa configuration: tmr1 channel1 and channel4 as alternate function push-pull */
244 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
245 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
246 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
247 gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_11;
248 gpio_init(GPIOA, &gpio_init_struct);
249
250 gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE8, GPIO_MUX_1);
251 gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE11, GPIO_MUX_1);
252 }
253
254 if(tmr_x == TMR2)
255 {
256 /* tmr2 clock enable */
257 crm_periph_clock_enable(CRM_TMR2_PERIPH_CLOCK, TRUE);
258 /* gpioa clock enable */
259 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
260
261 /* gpioa configuration: tmr1 channel1 and channel2 as alternate function push-pull */
262 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
263 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
264 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
265 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1;
266 gpio_init(GPIOA, &gpio_init_struct);
267
268 gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE0, GPIO_MUX_1);
269 gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE1, GPIO_MUX_1);
270 }
271 /* add others */
272 }
273 #endif /* BSP_USING_PWM */
274
275 #ifdef BSP_USING_ADC
at32_msp_adc_init(void * instance)276 void at32_msp_adc_init(void *instance)
277 {
278 gpio_init_type gpio_init_struct;
279 adc_type *adc_x = (adc_type *)instance;
280
281 gpio_default_para_init(&gpio_init_struct);
282 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
283 #ifdef BSP_USING_ADC1
284 if(adc_x == ADC1)
285 {
286 /* adc1 & gpio clock enable */
287 crm_periph_clock_enable(CRM_ADC1_PERIPH_CLOCK, TRUE);
288 crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
289
290 /* configure adc channel as analog input */
291 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_2 | GPIO_PINS_3 | GPIO_PINS_4 | GPIO_PINS_5;
292 gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
293 gpio_init(GPIOC, &gpio_init_struct);
294 }
295 #endif
296
297 #ifdef BSP_USING_ADC2
298 if(adc_x == ADC2)
299 {
300 /* adc2 & gpio clock enable */
301 crm_periph_clock_enable(CRM_ADC2_PERIPH_CLOCK, TRUE);
302 crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
303
304 /* configure adc channel as analog input */
305 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_2 | GPIO_PINS_3 | GPIO_PINS_4 | GPIO_PINS_5;
306 gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
307 gpio_init(GPIOC, &gpio_init_struct);
308 }
309 #endif
310 }
311 #endif /* BSP_USING_ADC */
312
313 #ifdef BSP_USING_HWTIMER
at32_msp_hwtmr_init(void * instance)314 void at32_msp_hwtmr_init(void *instance)
315 {
316 tmr_type *tmr_x = (tmr_type *)instance;
317
318 #ifdef BSP_USING_HWTMR3
319 if(tmr_x == TMR3)
320 {
321 /* tmr3 clock enable */
322 crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE);
323 }
324 #endif
325
326 #ifdef BSP_USING_HWTMR4
327 if(tmr_x == TMR4)
328 {
329 /* tmr4 clock enable */
330 crm_periph_clock_enable(CRM_TMR4_PERIPH_CLOCK, TRUE);
331 }
332 #endif
333
334 #ifdef BSP_USING_HWTMR5
335 if(tmr_x == TMR5)
336 {
337 /* tmr5 clock enable */
338 crm_periph_clock_enable(CRM_TMR5_PERIPH_CLOCK, TRUE);
339 }
340 #endif
341 }
342 #endif
343
344 #ifdef BSP_USING_CAN_V2
at32_msp_can_init(void * instance)345 void at32_msp_can_init(void *instance)
346 {
347 gpio_init_type gpio_init_struct;
348 can_type *can_x = (can_type *)instance;
349
350 gpio_default_para_init(&gpio_init_struct);
351 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
352 #ifdef BSP_USING_CAN1
353 if(can_x == CAN1)
354 {
355 crm_periph_clock_enable(CRM_CAN1_PERIPH_CLOCK, TRUE);
356 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
357 crm_can_clock_select(CRM_CAN1, CRM_CAN_CLOCK_SOURCE_PCLK);
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_7 | GPIO_PINS_8 | GPIO_PINS_9;
363 gpio_init(GPIOB, &gpio_init_struct);
364
365 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE7, GPIO_MUX_11);
366 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE8, GPIO_MUX_9);
367 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE9, GPIO_MUX_9);
368 }
369 #endif
370 #ifdef BSP_USING_CAN2
371 if(can_x == CAN2)
372 {
373 crm_periph_clock_enable(CRM_CAN2_PERIPH_CLOCK, TRUE);
374 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
375 crm_can_clock_select(CRM_CAN2, CRM_CAN_CLOCK_SOURCE_PCLK);
376
377 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
378 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
379 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
380 gpio_init_struct.gpio_pins = GPIO_PINS_5 | GPIO_PINS_6 | GPIO_PINS_11;
381 gpio_init(GPIOB, &gpio_init_struct);
382
383 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE5, GPIO_MUX_9);
384 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE6, GPIO_MUX_9);
385 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE11, GPIO_MUX_13);
386 }
387 #endif
388 }
389 #endif /* BSP_USING_CAN */
390
391 #ifdef BSP_USING_QSPI
at32_msp_qspi_init(void * instance)392 void at32_msp_qspi_init(void *instance)
393 {
394 gpio_init_type gpio_init_struct;
395 qspi_type *qspi_x = (qspi_type *)instance;
396
397 gpio_default_para_init(&gpio_init_struct);
398 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
399 #ifdef BSP_USING_QSPI1
400 if(qspi_x == QSPI1)
401 {
402 crm_periph_clock_enable(CRM_QSPI1_PERIPH_CLOCK, TRUE);
403 crm_periph_clock_enable(CRM_GPIOF_PERIPH_CLOCK, TRUE);
404 crm_periph_clock_enable(CRM_GPIOG_PERIPH_CLOCK, TRUE);
405
406 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
407 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
408 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
409 gpio_init_struct.gpio_pins = GPIO_PINS_6 | GPIO_PINS_7 | GPIO_PINS_8 | GPIO_PINS_9 | GPIO_PINS_10;
410 gpio_init(GPIOF, &gpio_init_struct);
411
412 gpio_init_struct.gpio_pins = GPIO_PINS_6;
413 gpio_init(GPIOG, &gpio_init_struct);
414
415 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE6, GPIO_MUX_9);
416 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE7, GPIO_MUX_9);
417 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE8, GPIO_MUX_10);
418 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE9, GPIO_MUX_10);
419 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE10, GPIO_MUX_9);
420 gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE6, GPIO_MUX_10);
421 }
422 #endif
423 }
424 #endif /* BSP_USING_QSPI */
425
426 #ifdef BSP_USING_SDRAM
at32_msp_sdram_init(void * instance)427 void at32_msp_sdram_init(void *instance)
428 {
429 gpio_init_type gpio_init_struct;
430
431 gpio_default_para_init(&gpio_init_struct);
432
433 /* enable the gpio clock */
434 crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
435 crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
436 crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
437 crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
438 crm_periph_clock_enable(CRM_GPIOF_PERIPH_CLOCK, TRUE);
439 crm_periph_clock_enable(CRM_GPIOG_PERIPH_CLOCK, TRUE);
440
441 /* enable the xmc clock */
442 crm_periph_clock_enable(CRM_XMC_PERIPH_CLOCK, TRUE);
443
444 /* address lines configuration */
445 gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
446 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
447 gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
448 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
449
450 gpio_init_struct.gpio_pins = GPIO_PINS_5 | GPIO_PINS_6;
451 gpio_init(GPIOB, &gpio_init_struct);
452
453 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_2 | GPIO_PINS_3;
454 gpio_init(GPIOC, &gpio_init_struct);
455
456 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_8 | GPIO_PINS_9 | GPIO_PINS_10 | GPIO_PINS_14 | GPIO_PINS_15;
457 gpio_init(GPIOD, &gpio_init_struct);
458
459 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_7 | GPIO_PINS_8 | GPIO_PINS_10 |
460 GPIO_PINS_9 | GPIO_PINS_11 | GPIO_PINS_11 | GPIO_PINS_12 |
461 GPIO_PINS_13 | GPIO_PINS_14 | GPIO_PINS_15;
462 gpio_init(GPIOE, &gpio_init_struct);
463
464 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_2 | GPIO_PINS_3 |
465 GPIO_PINS_4 | GPIO_PINS_5 | GPIO_PINS_11 | GPIO_PINS_12 |
466 GPIO_PINS_13 | GPIO_PINS_14 | GPIO_PINS_15;
467 gpio_init(GPIOF, &gpio_init_struct);
468
469 gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_2 | GPIO_PINS_4 |
470 GPIO_PINS_5 | GPIO_PINS_8 | GPIO_PINS_15;
471 gpio_init(GPIOG, &gpio_init_struct);
472
473 /* gpio mux configuration */
474 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE5, GPIO_MUX_12);
475 gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE6, GPIO_MUX_12);
476
477 gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE0, GPIO_MUX_12);
478 gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE2, GPIO_MUX_12);
479 gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE3, GPIO_MUX_12);
480
481 gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE0, GPIO_MUX_12);
482 gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE1, GPIO_MUX_12);
483 gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE8, GPIO_MUX_12);
484 gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE9, GPIO_MUX_12);
485 gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE10, GPIO_MUX_12);
486 gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE14, GPIO_MUX_12);
487 gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE15, GPIO_MUX_12);
488
489 gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE0, GPIO_MUX_12);
490 gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE1, GPIO_MUX_12);
491 gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE7, GPIO_MUX_12);
492 gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE8, GPIO_MUX_12);
493 gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE9, GPIO_MUX_12);
494 gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE10, GPIO_MUX_12);
495 gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE11, GPIO_MUX_12);
496 gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE12, GPIO_MUX_12);
497 gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE13, GPIO_MUX_12);
498 gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE14, GPIO_MUX_12);
499 gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE15, GPIO_MUX_12);
500
501 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE0, GPIO_MUX_12);
502 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE1, GPIO_MUX_12);
503 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE2, GPIO_MUX_12);
504 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE3, GPIO_MUX_12);
505 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE4, GPIO_MUX_12);
506 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE5, GPIO_MUX_12);
507 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE11, GPIO_MUX_12);
508 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE12, GPIO_MUX_12);
509 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE13, GPIO_MUX_12);
510 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE14, GPIO_MUX_12);
511 gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE15, GPIO_MUX_12);
512
513 gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE0, GPIO_MUX_12);
514 gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE1, GPIO_MUX_12);
515 gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE2, GPIO_MUX_12);
516 gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE4, GPIO_MUX_12);
517 gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE5, GPIO_MUX_12);
518 gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE8, GPIO_MUX_12);
519 gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE15, GPIO_MUX_12);
520 }
521
522 #endif /* BSP_USING_SDRAM */
523
524 #ifdef BSP_USING_USBOTG
at32_msp_usb_init(void * instance)525 void at32_msp_usb_init(void *instance)
526 {
527 /* defalut usb clock from hext */
528 usb_clk48_s clk_s = USB_CLK_HEXT;
529
530 #if defined (BSP_USING_HOST_USBOTG1) || defined (BSP_USING_DEVICE_USBOTG1)
531 crm_periph_clock_enable(CRM_OTGFS1_PERIPH_CLOCK, TRUE);
532 #endif
533
534 if(clk_s == USB_CLK_HICK)
535 {
536 crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_HICK);
537
538 /* enable the acc calibration ready interrupt */
539 crm_periph_clock_enable(CRM_ACC_PERIPH_CLOCK, TRUE);
540
541 /* update the c1\c2\c3 value */
542 acc_write_c1(7980);
543 acc_write_c2(8000);
544 acc_write_c3(8020);
545
546 /* open acc calibration */
547 acc_calibration_mode_enable(ACC_CAL_HICKTRIM, TRUE);
548 }
549 else
550 {
551 /* attention: pllu divider is set at board.c */
552 /* enable pllu clock output */
553 crm_pllu_output_set(TRUE);
554 /* wait till pllu is ready */
555 while(crm_flag_get(CRM_PLLU_STABLE_FLAG) == RESET)
556 {
557 }
558 crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_PLLU);
559 }
560 }
561 #endif /* BSP_USING_USBOTG */
562
563 #ifdef BSP_USING_DAC
at32_msp_dac_init(void * instance)564 void at32_msp_dac_init(void *instance)
565 {
566 gpio_init_type gpio_init_struct;
567 dac_type *dac_x = (dac_type *)instance;
568
569 gpio_default_para_init(&gpio_init_struct);
570 gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
571 #ifdef BSP_USING_DAC1
572 if(dac_x == DAC)
573 {
574 /* dac & gpio clock enable */
575 crm_periph_clock_enable(CRM_DAC_PERIPH_CLOCK, TRUE);
576 crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
577
578 /* configure adc channel as analog output */
579 gpio_init_struct.gpio_pins = GPIO_PINS_4 | GPIO_PINS_5;
580 gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
581 gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
582 gpio_init(GPIOA, &gpio_init_struct);
583 }
584 #endif
585 }
586 #endif /* BSP_USING_DAC */
587