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  * 2023-10-18     shelton      first version
9  */
10 
11 #include <rtthread.h>
12 #include "at32f402_405.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_pins = GPIO_PINS_10;
36         gpio_init(GPIOA, &gpio_init_struct);
37 
38         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE9, GPIO_MUX_7);
39         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE10, GPIO_MUX_7);
40     }
41 #endif
42 #ifdef BSP_USING_UART2
43     if(USART2 == usart_x)
44     {
45         crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, TRUE);
46         crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
47 
48         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
49         gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
50         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
51         gpio_init_struct.gpio_pins = GPIO_PINS_2;
52         gpio_init(GPIOA, &gpio_init_struct);
53 
54         gpio_init_struct.gpio_pins = GPIO_PINS_3;
55         gpio_init(GPIOA, &gpio_init_struct);
56 
57         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE2, GPIO_MUX_7);
58         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE3, GPIO_MUX_7);
59     }
60 #endif
61 #ifdef BSP_USING_UART3
62     if(USART3 == usart_x)
63     {
64         crm_periph_clock_enable(CRM_USART3_PERIPH_CLOCK, TRUE);
65         crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
66 
67         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
68         gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
69         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
70         gpio_init_struct.gpio_pins = GPIO_PINS_10;
71         gpio_init(GPIOB, &gpio_init_struct);
72 
73         gpio_init_struct.gpio_pins = GPIO_PINS_11;
74         gpio_init(GPIOB, &gpio_init_struct);
75 
76         gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE10, GPIO_MUX_7);
77         gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE11, GPIO_MUX_7);
78     }
79 #endif
80     /* add others */
81 }
82 #endif /* BSP_USING_SERIAL */
83 
84 #ifdef BSP_USING_SPI
at32_msp_spi_init(void * instance)85 void at32_msp_spi_init(void *instance)
86 {
87     gpio_init_type gpio_init_struct;
88     spi_type *spi_x = (spi_type *)instance;
89 
90     gpio_default_para_init(&gpio_init_struct);
91     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
92 #ifdef BSP_USING_SPI1
93     if(SPI1 == spi_x)
94     {
95         crm_periph_clock_enable(CRM_SPI1_PERIPH_CLOCK, TRUE);
96         crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
97 
98         gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
99         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
100         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
101         gpio_init_struct.gpio_pins = GPIO_PINS_5 | GPIO_PINS_6 | GPIO_PINS_7;
102         gpio_init(GPIOA, &gpio_init_struct);
103 
104         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE5, GPIO_MUX_5);
105         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE6, GPIO_MUX_5);
106         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE7, GPIO_MUX_5);
107     }
108 #endif
109 #ifdef BSP_USING_SPI2
110     if(SPI2 == spi_x)
111     {
112         crm_periph_clock_enable(CRM_SPI2_PERIPH_CLOCK, TRUE);
113         crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
114 
115         gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
116         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
117         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
118         gpio_init_struct.gpio_pins = GPIO_PINS_2 | GPIO_PINS_3 | GPIO_PINS_7;
119         gpio_init(GPIOC, &gpio_init_struct);
120 
121         gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE2, GPIO_MUX_5);
122         gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE3, GPIO_MUX_5);
123         gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE7, GPIO_MUX_5);
124     }
125 #endif
126     /* add others */
127 }
128 #endif /* BSP_USING_SPI */
129 
130 #ifdef BSP_USING_HARD_I2C
at32_msp_i2c_init(void * instance)131 void at32_msp_i2c_init(void *instance)
132 {
133     gpio_init_type gpio_init_struct;
134     i2c_type *i2c_x = (i2c_type *)instance;
135 
136     gpio_default_para_init(&gpio_init_struct);
137     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
138 #ifdef BSP_USING_HARD_I2C1
139     if(I2C1 == i2c_x)
140     {
141         crm_periph_clock_enable(CRM_I2C1_PERIPH_CLOCK, TRUE);
142         crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
143 
144         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
145         gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
146         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
147         gpio_init_struct.gpio_pins = GPIO_PINS_6 | GPIO_PINS_7;
148         gpio_init(GPIOB, &gpio_init_struct);
149 
150         gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE6, GPIO_MUX_4);
151         gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE7, GPIO_MUX_4);
152     }
153 #endif
154 #ifdef BSP_USING_HARD_I2C2
155     if(I2C2 == i2c_x)
156     {
157         crm_periph_clock_enable(CRM_I2C2_PERIPH_CLOCK, TRUE);
158         crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
159 
160         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
161         gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
162         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
163         gpio_init_struct.gpio_pins = GPIO_PINS_3 | GPIO_PINS_10;
164         gpio_init(GPIOB, &gpio_init_struct);
165 
166         gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE3, GPIO_MUX_4);
167         gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE10, GPIO_MUX_4);
168     }
169 #endif
170 #ifdef BSP_USING_HARD_I2C3
171     if(I2C3 == i2c_x)
172     {
173         crm_periph_clock_enable(CRM_I2C3_PERIPH_CLOCK, TRUE);
174         crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
175         crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
176 
177         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
178         gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
179         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
180         gpio_init_struct.gpio_pins = GPIO_PINS_8;
181         gpio_init(GPIOA, &gpio_init_struct);
182         gpio_init_struct.gpio_pins = GPIO_PINS_9;
183         gpio_init(GPIOC, &gpio_init_struct);
184 
185         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE8, GPIO_MUX_4);
186         gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE9, GPIO_MUX_4);
187     }
188 #endif
189     /* add others */
190 }
191 #endif /* BSP_USING_HARD_I2C */
192 
193 #ifdef BSP_USING_PWM
at32_msp_tmr_init(void * instance)194 void at32_msp_tmr_init(void *instance)
195 {
196     gpio_init_type gpio_init_struct;
197     tmr_type *tmr_x = (tmr_type *)instance;
198 
199     gpio_default_para_init(&gpio_init_struct);
200     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
201     if(tmr_x == TMR1)
202     {
203         /* tmr1 clock enable */
204         crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE);
205         /* gpioa clock enable */
206         crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
207 
208         /* gpioa configuration: tmr1 channel1 and channel4 as alternate function push-pull */
209         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
210         gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
211         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
212         gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_11;
213         gpio_init(GPIOA, &gpio_init_struct);
214 
215         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE8, GPIO_MUX_1);
216         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE11, GPIO_MUX_1);
217     }
218 
219     if(tmr_x == TMR2)
220     {
221         /* tmr2 clock enable */
222         crm_periph_clock_enable(CRM_TMR2_PERIPH_CLOCK, TRUE);
223         /* gpioa clock enable */
224         crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
225 
226         /* gpioa configuration: tmr1 channel1 and channel2 as alternate function push-pull */
227         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
228         gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
229         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
230         gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1;
231         gpio_init(GPIOA, &gpio_init_struct);
232 
233         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE0, GPIO_MUX_1);
234         gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE1, GPIO_MUX_1);
235     }
236     /* add others */
237 }
238 #endif /* BSP_USING_PWM */
239 
240 #ifdef BSP_USING_ADC
at32_msp_adc_init(void * instance)241 void at32_msp_adc_init(void *instance)
242 {
243     gpio_init_type gpio_init_struct;
244     adc_type *adc_x = (adc_type *)instance;
245 
246     gpio_default_para_init(&gpio_init_struct);
247     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
248 #ifdef BSP_USING_ADC1
249     if(adc_x == ADC1)
250     {
251         /* adc1 & gpio clock enable */
252         crm_periph_clock_enable(CRM_ADC1_PERIPH_CLOCK, TRUE);
253         crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
254 
255         /* configure adc channel as analog input */
256         gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_2 | GPIO_PINS_3 | GPIO_PINS_4 | GPIO_PINS_5;
257         gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG;
258         gpio_init(GPIOC, &gpio_init_struct);
259     }
260 #endif
261 }
262 #endif /* BSP_USING_ADC */
263 
264 #ifdef BSP_USING_HWTIMER
at32_msp_hwtmr_init(void * instance)265 void at32_msp_hwtmr_init(void *instance)
266 {
267     tmr_type *tmr_x = (tmr_type *)instance;
268 
269 #ifdef BSP_USING_HWTMR3
270     if(tmr_x == TMR3)
271     {
272         /* tmr3 clock enable */
273         crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE);
274     }
275 #endif
276 }
277 #endif
278 
279 #ifdef BSP_USING_CAN
at32_msp_can_init(void * instance)280 void at32_msp_can_init(void *instance)
281 {
282     gpio_init_type gpio_init_struct;
283     can_type *can_x = (can_type *)instance;
284 
285     gpio_default_para_init(&gpio_init_struct);
286     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
287 #ifdef BSP_USING_CAN1
288     if(can_x == CAN1)
289     {
290         crm_periph_clock_enable(CRM_CAN1_PERIPH_CLOCK, TRUE);
291         crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
292 
293         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
294         gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
295         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
296         gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_9;
297         gpio_init(GPIOB, &gpio_init_struct);
298 
299         gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE8, GPIO_MUX_9);
300         gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE9, GPIO_MUX_9);
301     }
302 #endif
303 }
304 #endif /* BSP_USING_CAN */
305 
306 #ifdef BSP_USING_QSPI
at32_msp_qspi_init(void * instance)307 void at32_msp_qspi_init(void *instance)
308 {
309     gpio_init_type gpio_init_struct;
310     qspi_type *qspi_x = (qspi_type *)instance;
311 
312     gpio_default_para_init(&gpio_init_struct);
313     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
314 #ifdef BSP_USING_QSPI1
315     if(qspi_x == QSPI1)
316     {
317         crm_periph_clock_enable(CRM_QSPI1_PERIPH_CLOCK, TRUE);
318         crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
319         crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
320 
321         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
322         gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
323         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
324         gpio_init_struct.gpio_pins = GPIO_PINS_2 | GPIO_PINS_7;
325         gpio_init(GPIOB, &gpio_init_struct);
326 
327         gpio_init_struct.gpio_pins = GPIO_PINS_5 | GPIO_PINS_8 | GPIO_PINS_9 | GPIO_PINS_11;
328         gpio_init(GPIOC, &gpio_init_struct);
329 
330         gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE2, GPIO_MUX_11);
331         gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE7, GPIO_MUX_11);
332         gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE5, GPIO_MUX_11);
333         gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE8, GPIO_MUX_11);
334         gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE9, GPIO_MUX_11);
335         gpio_pin_mux_config(GPIOC, GPIO_PINS_SOURCE11, GPIO_MUX_11);
336     }
337 #endif
338 }
339 #endif /* BSP_USING_QSPI */
340 
341 #ifdef BSP_USING_USBOTG
at32_msp_usb_init(void * instance)342 void at32_msp_usb_init(void *instance)
343 {
344     /* defalut usb clock from hext */
345     usb_clk48_s clk_s = USB_CLK_HEXT;
346 
347 #if defined (BSP_USING_HOST_USBOTG1) || defined (BSP_USING_DEVICE_USBOTG1)
348     crm_periph_clock_enable(CRM_OTGFS1_PERIPH_CLOCK, TRUE);
349 #endif
350 
351 #if defined (BSP_USING_HOST_USBOTG2) || defined (BSP_USING_DEVICE_USBOTG2)
352     crm_periph_clock_enable(CRM_OTGHS_PERIPH_CLOCK, TRUE);
353 #endif
354 
355     if(clk_s == USB_CLK_HICK)
356     {
357         crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_HICK);
358 
359         /* enable the acc calibration ready interrupt */
360         crm_periph_clock_enable(CRM_ACC_PERIPH_CLOCK, TRUE);
361 
362         /* update the c1\c2\c3 value */
363         acc_write_c1(7980);
364         acc_write_c2(8000);
365         acc_write_c3(8020);
366 
367         /* open acc calibration */
368         acc_calibration_mode_enable(ACC_CAL_HICKTRIM, TRUE);
369     }
370     else
371     {
372         /* attention: pllu divider is set at board.c */
373         /* enable pllu clock output */
374         crm_pllu_output_set(TRUE);
375         /* wait till pllu is ready */
376         while(crm_flag_get(CRM_PLLU_STABLE_FLAG) == RESET)
377         {
378         }
379         crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_PLLU);
380     }
381 }
382 #endif /* BSP_USING_USBOTG */
383