Skip to content

Commit 12b1e74

Browse files
author
mazhiyuan
committed
修复STM32串口mask问题
1 parent 634e534 commit 12b1e74

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

bsp/stm32/libraries/HAL_Drivers/drv_usart.c

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,53 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar
251251
return RT_EOK;
252252
}
253253

254+
rt_uint32_t stm32_uart_get_mask(rt_uint32_t word_length, rt_uint32_t parity)
255+
{
256+
rt_uint32_t mask;
257+
if (word_length == UART_WORDLENGTH_8B)
258+
{
259+
if (parity == UART_PARITY_NONE)
260+
{
261+
mask = 0x00FFU ;
262+
}
263+
else
264+
{
265+
mask = 0x007FU ;
266+
}
267+
}
268+
#ifdef UART_WORDLENGTH_9B
269+
else if (word_length == UART_WORDLENGTH_9B)
270+
{
271+
if (parity == UART_PARITY_NONE)
272+
{
273+
mask = 0x01FFU ;
274+
}
275+
else
276+
{
277+
mask = 0x00FFU ;
278+
}
279+
}
280+
#endif
281+
#ifdef UART_WORDLENGTH_7B
282+
else if (word_length == UART_WORDLENGTH_7B)
283+
{
284+
if (parity == UART_PARITY_NONE)
285+
{
286+
mask = 0x007FU ;
287+
}
288+
else
289+
{
290+
mask = 0x003FU ;
291+
}
292+
}
293+
else
294+
{
295+
mask = 0x0000U;
296+
}
297+
#endif
298+
return mask;
299+
}
300+
254301
static int stm32_putc(struct rt_serial_device *serial, char c)
255302
{
256303
struct stm32_uart *uart;
@@ -282,9 +329,9 @@ static int stm32_getc(struct rt_serial_device *serial)
282329
#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WL) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32F0) \
283330
|| defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7) \
284331
|| defined(SOC_SERIES_STM32G4) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32WB)|| defined(SOC_SERIES_STM32F3)
285-
ch = uart->handle.Instance->RDR & 0xff;
332+
ch = uart->handle.Instance->RDR & stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity);
286333
#else
287-
ch = uart->handle.Instance->DR & 0xff;
334+
ch = uart->handle.Instance->DR & stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity);
288335
#endif
289336
}
290337
return ch;

bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.c

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,53 @@ static int stm32_putc(struct rt_serial_device *serial, char c)
302302
return 1;
303303
}
304304

305+
rt_uint32_t stm32_uart_get_mask(rt_uint32_t word_length, rt_uint32_t parity)
306+
{
307+
rt_uint32_t mask;
308+
if (word_length == UART_WORDLENGTH_8B)
309+
{
310+
if (parity == UART_PARITY_NONE)
311+
{
312+
mask = 0x00FFU ;
313+
}
314+
else
315+
{
316+
mask = 0x007FU ;
317+
}
318+
}
319+
#ifdef UART_WORDLENGTH_9B
320+
else if (word_length == UART_WORDLENGTH_9B)
321+
{
322+
if (parity == UART_PARITY_NONE)
323+
{
324+
mask = 0x01FFU ;
325+
}
326+
else
327+
{
328+
mask = 0x00FFU ;
329+
}
330+
}
331+
#endif
332+
#ifdef UART_WORDLENGTH_7B
333+
else if (word_length == UART_WORDLENGTH_7B)
334+
{
335+
if (parity == UART_PARITY_NONE)
336+
{
337+
mask = 0x007FU ;
338+
}
339+
else
340+
{
341+
mask = 0x003FU ;
342+
}
343+
}
344+
else
345+
{
346+
mask = 0x0000U;
347+
}
348+
#endif
349+
return mask;
350+
}
351+
305352
static int stm32_getc(struct rt_serial_device *serial)
306353
{
307354
int ch;
@@ -311,7 +358,7 @@ static int stm32_getc(struct rt_serial_device *serial)
311358

312359
ch = -1;
313360
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET)
314-
ch = UART_GET_RDR(&uart->handle);
361+
ch = UART_GET_RDR(&uart->handle, stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity));
315362
return ch;
316363
}
317364

@@ -403,7 +450,7 @@ static void uart_isr(struct rt_serial_device *serial)
403450
rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
404451
RT_ASSERT(rx_fifo != RT_NULL);
405452

406-
rt_ringbuffer_putchar(&(rx_fifo->rb), UART_GET_RDR(&uart->handle));
453+
rt_ringbuffer_putchar(&(rx_fifo->rb), UART_GET_RDR(&uart->handle, stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity)));
407454

408455
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
409456
}

bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ int rt_hw_usart_init(void);
2424
|| defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7) \
2525
|| defined(SOC_SERIES_STM32G4)
2626
#define UART_SET_TDR(__HANDLE__, __DATA__) ((__HANDLE__)->Instance->TDR = (__DATA__))
27-
#define UART_GET_RDR(__HANDLE__) ((__HANDLE__)->Instance->RDR & 0xFF)
27+
#define UART_GET_RDR(__HANDLE__, MASK) ((__HANDLE__)->Instance->RDR & MASK)
2828

2929
#else
3030
#define UART_SET_TDR(__HANDLE__, __DATA__) ((__HANDLE__)->Instance->DR = (__DATA__))
31-
#define UART_GET_RDR(__HANDLE__) ((__HANDLE__)->Instance->DR & 0xFF)
31+
#define UART_GET_RDR(__HANDLE__, MASK) ((__HANDLE__)->Instance->DR & MASK)
3232
#endif
3333

3434

0 commit comments

Comments
 (0)