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-11     shelton      first version
9  */
10 
11 #include <rtthread.h>
12 #include "at32f415.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 #endif /* BSP_USING_ADC */
262 
263 #ifdef BSP_USING_HWTIMER
at32_msp_hwtmr_init(void * instance)264 void at32_msp_hwtmr_init(void *instance)
265 {
266     tmr_type *tmr_x = (tmr_type *)instance;
267 
268 #ifdef BSP_USING_HWTMR3
269     if(tmr_x == TMR3)
270     {
271         /* tmr3 clock enable */
272         crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE);
273     }
274 #endif
275 
276 #ifdef BSP_USING_HWTMR4
277     if(tmr_x == TMR4)
278     {
279         /* tmr4 clock enable */
280         crm_periph_clock_enable(CRM_TMR4_PERIPH_CLOCK, TRUE);
281     }
282 #endif
283 
284 #ifdef BSP_USING_HWTMR5
285     if(tmr_x == TMR5)
286     {
287         /* tmr5 clock enable */
288         crm_periph_clock_enable(CRM_TMR5_PERIPH_CLOCK, TRUE);
289     }
290 #endif
291 }
292 #endif
293 
294 #ifdef BSP_USING_CAN
at32_msp_can_init(void * instance)295 void at32_msp_can_init(void *instance)
296 {
297     gpio_init_type gpio_init_struct;
298     can_type *can_x = (can_type *)instance;
299 
300     gpio_default_para_init(&gpio_init_struct);
301     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
302 #ifdef BSP_USING_CAN1
303     if(CAN1 == can_x)
304     {
305         crm_periph_clock_enable(CRM_CAN1_PERIPH_CLOCK, TRUE);
306         crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
307         crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
308 
309         gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
310         gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
311         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
312         gpio_init_struct.gpio_pins = GPIO_PINS_9;
313         gpio_init(GPIOB, &gpio_init_struct);
314         gpio_pin_remap_config(CAN1_GMUX_0010, TRUE);
315 
316         gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
317         gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
318         gpio_init_struct.gpio_pins = GPIO_PINS_8;
319         gpio_init(GPIOB, &gpio_init_struct);
320     }
321 #endif
322 }
323 #endif /* BSP_USING_CAN */
324 
325 #ifdef BSP_USING_USBOTG
at32_msp_usb_init(void * instance)326 void at32_msp_usb_init(void *instance)
327 {
328     /* enable usb clock */
329     crm_periph_clock_enable(CRM_OTGFS1_PERIPH_CLOCK, TRUE);
330 
331     switch(system_core_clock)
332     {
333         /* 48MHz */
334         case 48000000:
335             crm_usb_clock_div_set(CRM_USB_DIV_1);
336             break;
337 
338         /* 72MHz */
339         case 72000000:
340             crm_usb_clock_div_set(CRM_USB_DIV_1_5);
341             break;
342 
343         /* 96MHz */
344         case 96000000:
345             crm_usb_clock_div_set(CRM_USB_DIV_2);
346             break;
347 
348         /* 120MHz */
349         case 120000000:
350             crm_usb_clock_div_set(CRM_USB_DIV_2_5);
351             break;
352 
353         /* 144MHz */
354         case 144000000:
355             crm_usb_clock_div_set(CRM_USB_DIV_3);
356             break;
357 
358         default:
359             break;
360     }
361 }
362 
363 #endif /* BSP_USING_USBOTG */
364