Skip to content

Commit ae85d5b

Browse files
committed
[bsp][mm32f526x] 1.Move the configuration peripheral pin to mm32_msp.c;
2.Break out of loop if read ADC value exceeds 5ms;
1 parent 0eea85c commit ae85d5b

File tree

6 files changed

+138
-103
lines changed

6 files changed

+138
-103
lines changed

bsp/mm32f526x/board/SConscript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ cwd = GetCurrentDir()
88

99
# add the general drivers.
1010
src = ['board.c']
11+
src += ['mm32_msp.c']
1112

1213
CPPPATH = [cwd]
1314

bsp/mm32f526x/board/board.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <rthw.h>
1616
#include "mm32_device.h"
1717
#include <hal_common.h>
18+
#include "mm32_msp.h"
1819

1920
#define SRAM_SIZE 0x1C000
2021

bsp/mm32f526x/board/mm32_msp.c

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

bsp/mm32f526x/board/mm32_msp.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef __MM32_MSP_H__
2+
#define __MM32_MSP_H__
3+
4+
void mm32_msp_uart_init(void *instance);
5+
void mm32_msp_adc_init(void *instance);
6+
7+
#endif /* __MM32_MSP_H__ */

bsp/mm32f526x/drivers/drv_adc.c

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717

1818
#if defined(BSP_USING_ADC)
1919

20-
#define ADC_CONFIG_GPIORCC RCC_AHBPeriph_GPIOA
21-
#define ADC_CONFIG_GPIOX GPIOA
22-
#define ADC_CONFIG_IOX GPIO_Pin_0 | GPIO_Pin_1
23-
2420
struct mm32_adc
2521
{
2622
struct rt_adc_device mm32_adc_device;
@@ -56,37 +52,22 @@ static rt_err_t mm32_adc_init(struct rt_adc_device *device, rt_int8_t channel, r
5652
adc_x = device->parent.user_data;
5753

5854
if (enabled) {
59-
#if defined(BSP_USING_ADC1)
60-
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //Enable ADC1 clock
61-
#endif /* BSP_USING_ADC1 */
62-
63-
#if defined(BSP_USING_ADC2)
64-
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); //Enable ADC2 clock
65-
#endif /* BSP_USING_ADC2 */
55+
mm32_msp_adc_init((void *)adc_x);
6656

6757
ADC_CalibrationConfig(adc_x, 0x1FE);
6858

6959
ADC_StructInit(&ADC_InitStruct);
7060
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
7161
ADC_InitStruct.ADC_Prescaler = ADC_Prescaler_16; //ADC prescale factor
72-
ADC_InitStruct.ADC_Mode = ADC_Mode_Scan; //Set ADC mode to continuous conversion mode
62+
ADC_InitStruct.ADC_Mode = ADC_Mode_Scan; //Set ADC mode to scan conversion mode
7363
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; //AD data right-justified
7464
ADC_Init(adc_x, &ADC_InitStruct);
7565

7666
ADC_SampleTimeConfig(adc_x, channel, ADC_SampleTime_240_5);
77-
ADC_ChannelCmd(adc_x, channel, ENABLE);
78-
79-
ADC_DifferentialConversionConfig(adc_x, ADC_Differential_Conversion_4_5);
8067

81-
//config gpio
82-
GPIO_InitTypeDef GPIO_InitStruct;
83-
GPIO_StructInit(&GPIO_InitStruct);
68+
ADC_ChannelCmd(adc_x, channel, ENABLE);
8469

85-
RCC_AHBPeriphClockCmd(ADC_CONFIG_GPIORCC, ENABLE);
86-
GPIO_InitStruct.GPIO_Pin = ADC_CONFIG_IOX;
87-
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
88-
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
89-
GPIO_Init(ADC_CONFIG_GPIOX, &GPIO_InitStruct);
70+
ADC_DifferentialConversionConfig(adc_x, ADC_Pseudo_Differential_Conversion_4_5);
9071

9172
ADC_Cmd(adc_x, ENABLE);
9273
} else {
@@ -112,10 +93,15 @@ static rt_err_t mm32_get_adc_value(struct rt_adc_device *device, rt_int8_t chann
11293
adc_x = device->parent.user_data;
11394

11495
ADC_SoftwareStartConvCmd(adc_x, ENABLE);
115-
while(ADC_GetFlagStatus(adc_x, ADC_IT_EOS) == 0)
96+
97+
rt_uint32_t cnt = 0;
98+
while(ADC_GetFlagStatus(adc_x, ADC_FLAG_EOS) == 0) {
11699
rt_thread_mdelay(1);
100+
if (cnt++ > 5)
101+
break;
102+
}
117103

118-
ADC_ClearFlag(adc_x, ADC_IT_EOS);
104+
ADC_ClearFlag(adc_x, ADC_FLAG_EOS);
119105

120106
*value = ADC_GetChannelConvertedValue(adc_x, channel);
121107
return RT_EOK;

bsp/mm32f526x/drivers/drv_uart.c

Lines changed: 7 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Date Author Notes
88
* 2021-08-05 mazhiyuan first version
99
*/
10+
#include "board.h"
1011
#include <mm32_device.h>
1112
#include <rtdevice.h>
1213
#include "drv_uart.h"
@@ -177,84 +178,13 @@ void UART3_IRQHandler(void)
177178
}
178179
#endif /* BSP_USING_UART3 */
179180

180-
#ifdef BSP_USING_UART1
181-
static void UART1PINconfigStepA(void)
182-
{
183-
/* Enable UART clock */
184-
RCC_APB2PeriphClockCmd(RCC_APB2Periph_UART1, ENABLE);
185-
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
186-
}
187-
188-
static void UART1PINconfigStepB(void)
189-
{
190-
GPIO_InitTypeDef GPIO_InitStructure;
191-
/* Configure USART Rx/tx PIN */
192-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
193-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_High;
194-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
195-
GPIO_Init(GPIOA, &GPIO_InitStructure);
196-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
197-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
198-
GPIO_Init(GPIOA, &GPIO_InitStructure);
199-
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
200-
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
201-
}
202-
#endif
203-
204-
#ifdef BSP_USING_UART2
205-
static void UART2PINconfigStepA(void)
206-
{
207-
/* Enable UART clock */
208-
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART2, ENABLE);
209-
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
210-
}
211-
212-
static void UART2PINconfigStepB(void)
213-
{
214-
GPIO_InitTypeDef GPIO_InitStructure;
215-
/* Configure USART Rx/tx PIN */
216-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
217-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_High;
218-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
219-
GPIO_Init(GPIOA, &GPIO_InitStructure);
220-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
221-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
222-
GPIO_Init(GPIOA, &GPIO_InitStructure);
223-
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
224-
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);
225-
}
226-
#endif
227-
228-
#ifdef BSP_USING_UART3
229-
static void UART3PINconfigStepA(void)
230-
{
231-
/* Enable UART clock */
232-
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART3, ENABLE);
233-
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
234-
}
235-
236-
static void UART3PINconfigStepB(void)
237-
{
238-
GPIO_InitTypeDef GPIO_InitStructure;
239-
/* Configure USART Rx/tx PIN */
240-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
241-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_High;
242-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
243-
GPIO_Init(GPIOC, &GPIO_InitStructure);
244-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
245-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
246-
GPIO_Init(GPIOC, &GPIO_InitStructure);
247-
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_7);
248-
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_7);
249-
}
250-
#endif
251-
252181
int rt_hw_uart_init(void)
253182
{
254183
struct mm32_uart *uart;
255184
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
256185
#ifdef BSP_USING_UART1
257-
UART1PINconfigStepA();
186+
mm32_msp_uart_init((void *)UART1);
187+
258188
uart = &uart1;
259189
uart->uart = UART1;
260190
uart->irq = UART1_IRQn;
@@ -265,11 +195,11 @@ int rt_hw_uart_init(void)
265195
rt_hw_serial_register(&serial1, "uart1",
266196
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
267197
uart);
268-
UART1PINconfigStepB();
269198
#endif /* BSP_USING_UART1 */
270199

271200
#ifdef BSP_USING_UART2
272-
UART2PINconfigStepA();
201+
mm32_msp_uart_init((void *)UART2);
202+
273203
uart = &uart2;
274204
uart->uart = UART2;
275205
uart->irq = UART2_IRQn;
@@ -280,11 +210,11 @@ int rt_hw_uart_init(void)
280210
rt_hw_serial_register(&serial2, "uart2",
281211
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
282212
uart);
283-
UART2PINconfigStepB();
284213
#endif /* BSP_USING_UART2 */
285214

286215
#ifdef BSP_USING_UART3
287-
UART3PINconfigStepA();
216+
mm32_msp_uart_init((void *)UART3);
217+
288218
uart = &uart3;
289219
uart->uart = UART3;
290220
uart->irq = UART3_IRQn;
@@ -295,7 +225,6 @@ int rt_hw_uart_init(void)
295225
rt_hw_serial_register(&serial3, "uart3",
296226
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
297227
uart);
298-
UART3PINconfigStepB();
299228
#endif /* BSP_USING_UART3 */
300229

301230
return 0;

0 commit comments

Comments
 (0)