1 /*
2 * Copyright (c) 2022-2025, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2025-01-22 chasel first version
9 */
10
11 #include <rtthread.h>
12 #include "mm32_device.h"
13 #include "mm32_msp.h"
14
mm32_msp_uart_init(void * instance)15 void mm32_msp_uart_init(void *instance)
16 {
17 GPIO_InitTypeDef GPIO_InitStructure;
18 UART_TypeDef *uart_x = (UART_TypeDef *)instance;
19
20 #ifdef BSP_USING_UART1
21 if(UART1 == uart_x)
22 {
23 /* Enable UART clock */
24 RCC_APB2PeriphClockCmd(RCC_APB2Periph_UART1, ENABLE);
25 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
26
27 /* Configure USART Rx/tx PIN */
28 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
29 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_High;
30 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
31 GPIO_Init(GPIOA, &GPIO_InitStructure);
32 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
33 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
34 GPIO_Init(GPIOA, &GPIO_InitStructure);
35
36 GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
37 GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
38 }
39 #endif
40 #ifdef BSP_USING_UART2
41 if(UART2 == uart_x)
42 {
43 /* Enable UART clock */
44 RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART2, ENABLE);
45 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
46
47 /* Configure USART Rx/tx PIN */
48 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
49 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_High;
50 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
51 GPIO_Init(GPIOA, &GPIO_InitStructure);
52 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
53 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
54 GPIO_Init(GPIOA, &GPIO_InitStructure);
55
56 GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
57 GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);
58 }
59 #endif
60 #ifdef BSP_USING_UART3
61 if(UART3 == uart_x)
62 {
63 /* Enable UART clock */
64 RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART3, ENABLE);
65 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
66
67 GPIO_InitTypeDef GPIO_InitStructure;
68 /* Configure USART Rx/tx PIN */
69 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
70 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_High;
71 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
72 GPIO_Init(GPIOC, &GPIO_InitStructure);
73 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
74 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
75 GPIO_Init(GPIOC, &GPIO_InitStructure);
76
77 GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_7);
78 GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_7);
79 }
80 #endif
81 /* add others */
82 }
83
84 #ifdef BSP_USING_ADC
mm32_msp_adc_init(void * instance)85 void mm32_msp_adc_init(void *instance)
86 {
87 GPIO_InitTypeDef GPIO_InitStruct;
88 ADC_TypeDef *adc_x = (ADC_TypeDef *)instance;
89
90 #ifdef BSP_USING_ADC1
91 if(adc_x == ADC1)
92 {
93 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //Enable ADC1 clock
94 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
95
96 /* configure adc channel as analog input */
97 GPIO_StructInit(&GPIO_InitStruct);
98 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
99 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
100 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
101 GPIO_Init(GPIOA, &GPIO_InitStruct);
102 }
103 #endif
104
105 #ifdef BSP_USING_ADC2
106 if(adc_x == ADC2)
107 {
108 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); //Enable ADC2 clock
109 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
110
111 /* configure adc channel as analog input */
112 GPIO_StructInit(&GPIO_InitStruct);
113 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
114 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
115 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
116 GPIO_Init(GPIOA, &GPIO_InitStruct);
117 }
118 #endif
119 }
120 #endif /* BSP_USING_ADC */
121
122