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_EMAC
at32_msp_emac_init(void * instance)392 void at32_msp_emac_init(void *instance)
393 {
394     gpio_init_type gpio_init_struct;
395 
396     crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
397     crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
398     crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
399     crm_periph_clock_enable(CRM_GPIOG_PERIPH_CLOCK, TRUE);
400 
401     gpio_default_para_init(&gpio_init_struct);
402     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
403     gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
404     gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
405     gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
406     gpio_init_struct.gpio_pins = GPIO_PINS_1 | GPIO_PINS_2;
407     gpio_init(GPIOA, &gpio_init_struct);
408 
409     gpio_init_struct.gpio_pins = GPIO_PINS_1;
410     gpio_init(GPIOC, &gpio_init_struct);
411 
412     gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_9 | GPIO_PINS_10;
413     gpio_init(GPIOD, &gpio_init_struct);
414 
415     gpio_init_struct.gpio_pins = GPIO_PINS_11 | GPIO_PINS_13 | GPIO_PINS_14;
416     gpio_init(GPIOG, &gpio_init_struct);
417 
418     gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE1, GPIO_MUX_11);
419     gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE2, GPIO_MUX_11);
420     gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE1, GPIO_MUX_11);
421     gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE8, GPIO_MUX_11);
422     gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE9, GPIO_MUX_11);
423     gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE10, GPIO_MUX_11);
424     gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE11, GPIO_MUX_11);
425     gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE13, GPIO_MUX_11);
426     gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE14, GPIO_MUX_11);
427 }
428 #endif /* BSP_USING_EMAC */
429 
430 #ifdef BSP_USING_QSPI
at32_msp_qspi_init(void * instance)431 void at32_msp_qspi_init(void *instance)
432 {
433     gpio_init_type gpio_init_struct;
434     qspi_type *qspi_x = (qspi_type *)instance;
435 
436     gpio_default_para_init(&gpio_init_struct);
437     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
438 #ifdef BSP_USING_QSPI1
439     if(qspi_x == QSPI1)
440     {
441         crm_periph_clock_enable(CRM_QSPI1_PERIPH_CLOCK, TRUE);
442         crm_periph_clock_enable(CRM_GPIOF_PERIPH_CLOCK, TRUE);
443         crm_periph_clock_enable(CRM_GPIOG_PERIPH_CLOCK, TRUE);
444 
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_pins = GPIO_PINS_6 | GPIO_PINS_7 | GPIO_PINS_8 | GPIO_PINS_9 | GPIO_PINS_10;
449         gpio_init(GPIOF, &gpio_init_struct);
450 
451         gpio_init_struct.gpio_pins = GPIO_PINS_6;
452         gpio_init(GPIOG, &gpio_init_struct);
453 
454         gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE6, GPIO_MUX_9);
455         gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE7, GPIO_MUX_9);
456         gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE8, GPIO_MUX_10);
457         gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE9, GPIO_MUX_10);
458         gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE10, GPIO_MUX_9);
459         gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE6, GPIO_MUX_10);
460     }
461 #endif
462 }
463 #endif /* BSP_USING_QSPI */
464 
465 #ifdef BSP_USING_SDRAM
at32_msp_sdram_init(void * instance)466 void at32_msp_sdram_init(void *instance)
467 {
468     gpio_init_type gpio_init_struct;
469 
470     gpio_default_para_init(&gpio_init_struct);
471 
472     /* enable the gpio clock */
473     crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
474     crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
475     crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
476     crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
477     crm_periph_clock_enable(CRM_GPIOF_PERIPH_CLOCK, TRUE);
478     crm_periph_clock_enable(CRM_GPIOG_PERIPH_CLOCK, TRUE);
479 
480     /* enable the xmc clock */
481     crm_periph_clock_enable(CRM_XMC_PERIPH_CLOCK, TRUE);
482 
483     /* address lines configuration */
484     gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
485     gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
486     gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
487     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
488 
489     gpio_init_struct.gpio_pins = GPIO_PINS_5 | GPIO_PINS_6;
490     gpio_init(GPIOB, &gpio_init_struct);
491 
492     gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_2 | GPIO_PINS_3;
493     gpio_init(GPIOC, &gpio_init_struct);
494 
495     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;
496     gpio_init(GPIOD, &gpio_init_struct);
497 
498     gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_7 | GPIO_PINS_8 | GPIO_PINS_10 |
499                                  GPIO_PINS_9 | GPIO_PINS_11 | GPIO_PINS_11 | GPIO_PINS_12 |
500                                  GPIO_PINS_13 | GPIO_PINS_14 | GPIO_PINS_15;
501     gpio_init(GPIOE, &gpio_init_struct);
502 
503     gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_2 | GPIO_PINS_3 |
504                                  GPIO_PINS_4 | GPIO_PINS_5 | GPIO_PINS_11 | GPIO_PINS_12 |
505                                  GPIO_PINS_13 | GPIO_PINS_14 | GPIO_PINS_15;
506     gpio_init(GPIOF, &gpio_init_struct);
507 
508     gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_2 | GPIO_PINS_4 |
509                                  GPIO_PINS_5 | GPIO_PINS_8 | GPIO_PINS_15;
510     gpio_init(GPIOG, &gpio_init_struct);
511 
512     /* gpio mux configuration */
513     gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE5, GPIO_MUX_12);
514     gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE6, GPIO_MUX_12);
515 
516     gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE0, GPIO_MUX_12);
517     gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE2, GPIO_MUX_12);
518     gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE3, GPIO_MUX_12);
519 
520     gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE0, GPIO_MUX_12);
521     gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE1, GPIO_MUX_12);
522     gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE8, GPIO_MUX_12);
523     gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE9, GPIO_MUX_12);
524     gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE10, GPIO_MUX_12);
525     gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE14, GPIO_MUX_12);
526     gpio_pin_mux_config(GPIOD, GPIO_PINS_SOURCE15, GPIO_MUX_12);
527 
528     gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE0, GPIO_MUX_12);
529     gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE1, GPIO_MUX_12);
530     gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE7, GPIO_MUX_12);
531     gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE8, GPIO_MUX_12);
532     gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE9, GPIO_MUX_12);
533     gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE10, GPIO_MUX_12);
534     gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE11, GPIO_MUX_12);
535     gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE12, GPIO_MUX_12);
536     gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE13, GPIO_MUX_12);
537     gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE14, GPIO_MUX_12);
538     gpio_pin_mux_config(GPIOE, GPIO_PINS_SOURCE15, GPIO_MUX_12);
539 
540     gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE0, GPIO_MUX_12);
541     gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE1, GPIO_MUX_12);
542     gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE2, GPIO_MUX_12);
543     gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE3, GPIO_MUX_12);
544     gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE4, GPIO_MUX_12);
545     gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE5, GPIO_MUX_12);
546     gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE11, GPIO_MUX_12);
547     gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE12, GPIO_MUX_12);
548     gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE13, GPIO_MUX_12);
549     gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE14, GPIO_MUX_12);
550     gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE15, GPIO_MUX_12);
551 
552     gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE0, GPIO_MUX_12);
553     gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE1, GPIO_MUX_12);
554     gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE2, GPIO_MUX_12);
555     gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE4, GPIO_MUX_12);
556     gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE5, GPIO_MUX_12);
557     gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE8, GPIO_MUX_12);
558     gpio_pin_mux_config(GPIOG, GPIO_PINS_SOURCE15, GPIO_MUX_12);
559 }
560 
561 #endif /* BSP_USING_SDRAM */
562 
563 #ifdef BSP_USING_USBOTG
at32_msp_usb_init(void * instance)564 void at32_msp_usb_init(void *instance)
565 {
566     /* defalut usb clock from hext */
567     usb_clk48_s clk_s = USB_CLK_HEXT;
568 
569 #if defined (BSP_USING_HOST_USBOTG1) || defined (BSP_USING_DEVICE_USBOTG1)
570     crm_periph_clock_enable(CRM_OTGFS1_PERIPH_CLOCK, TRUE);
571 #endif
572 
573     if(clk_s == USB_CLK_HICK)
574     {
575         crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_HICK);
576 
577         /* enable the acc calibration ready interrupt */
578         crm_periph_clock_enable(CRM_ACC_PERIPH_CLOCK, TRUE);
579 
580         /* update the c1\c2\c3 value */
581         acc_write_c1(7980);
582         acc_write_c2(8000);
583         acc_write_c3(8020);
584 
585         /* open acc calibration */
586         acc_calibration_mode_enable(ACC_CAL_HICKTRIM, TRUE);
587     }
588     else
589     {
590         /* attention: pllu divider is set at board.c */
591         /* enable pllu clock output */
592         crm_pllu_output_set(TRUE);
593         /* wait till pllu is ready */
594         while(crm_flag_get(CRM_PLLU_STABLE_FLAG) == RESET)
595         {
596         }
597         crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_PLLU);
598     }
599 }
600 #endif /* BSP_USING_USBOTG */
601 
602 #ifdef BSP_USING_DAC
at32_msp_dac_init(void * instance)603 void at32_msp_dac_init(void *instance)
604 {
605     gpio_init_type gpio_init_struct;
606     dac_type *dac_x = (dac_type *)instance;
607 
608     gpio_default_para_init(&gpio_init_struct);
609     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
610 #ifdef BSP_USING_DAC1
611     if(dac_x == DAC)
612     {
613         /* dac & gpio clock enable */
614         crm_periph_clock_enable(CRM_DAC_PERIPH_CLOCK, TRUE);
615         crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
616 
617         /* configure adc channel as analog output */
618         gpio_init_struct.gpio_pins = GPIO_PINS_4 | GPIO_PINS_5;
619         gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
620         gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
621         gpio_init(GPIOA, &gpio_init_struct);
622     }
623 #endif
624 }
625 #endif /* BSP_USING_DAC */
626