Skip to content

Commit 072cb3a

Browse files
wdfk-progRbb666
authored andcommitted
feat[STM32][USART]: add uart error callback support
add a control command to register a per-device uart error callback invoke the callback from the HAL uart error hook and clear it before close return -RT_ENOSYS when dma mode is requested without dma support propagate the set-int fallback result in optmode setup to keep control return values explicit
1 parent 96c0ce2 commit 072cb3a

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

bsp/stm32/libraries/HAL_Drivers/drivers/drv_usart_v2.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,25 +280,39 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar
280280
{
281281
#ifdef RT_SERIAL_USING_DMA
282282
stm32_dma_config(serial, ctrl_arg);
283+
#else
284+
return -RT_ENOSYS;
283285
#endif
284286
}
285287
else
286-
stm32_control(serial, RT_DEVICE_CTRL_SET_INT, (void *)ctrl_arg);
288+
{
289+
return stm32_control(serial, RT_DEVICE_CTRL_SET_INT, (void *)ctrl_arg);
290+
}
287291
break;
288292

289-
case RT_DEVICE_CHECK_OPTMODE: {
293+
case RT_DEVICE_CHECK_OPTMODE:
294+
{
290295
if (ctrl_arg & RT_DEVICE_FLAG_DMA_TX)
291296
return RT_SERIAL_TX_BLOCKING_NO_BUFFER;
292297
else
293298
return RT_SERIAL_TX_BLOCKING_BUFFER;
294299
}
295300
case RT_DEVICE_CTRL_CLOSE:
301+
{
302+
uart->error_indicate = RT_NULL;
296303
if (HAL_UART_DeInit(&(uart->handle)) != HAL_OK)
297304
{
298305
RT_ASSERT(0)
299306
}
300307
break;
301308
}
309+
case UART_CTRL_SET_ERROR_CALLBACK:
310+
{
311+
uart->error_indicate = (rt_err_t (*)(rt_device_t dev, rt_uint32_t error_code))arg;
312+
break;
313+
}
314+
}
315+
302316
return RT_EOK;
303317
}
304318

@@ -1242,6 +1256,10 @@ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
12421256
struct stm32_uart *uart = (struct stm32_uart *)huart;
12431257
LOG_D("%s: %s %d\n", __FUNCTION__, uart->config->name, huart->ErrorCode);
12441258
UNUSED(uart);
1259+
if(uart->error_indicate != RT_NULL)
1260+
{
1261+
uart->error_indicate(&uart->serial.parent, huart->ErrorCode);
1262+
}
12451263
}
12461264

12471265
/**
@@ -1304,12 +1322,13 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
13041322
#endif /* RT_SERIAL_USING_DMA */
13051323

13061324
static const struct rt_uart_ops stm32_uart_ops =
1307-
{
1308-
.configure = stm32_configure,
1309-
.control = stm32_control,
1310-
.putc = stm32_putc,
1311-
.getc = stm32_getc,
1312-
.transmit = stm32_transmit};
1325+
{
1326+
.configure = stm32_configure,
1327+
.control = stm32_control,
1328+
.putc = stm32_putc,
1329+
.getc = stm32_getc,
1330+
.transmit = stm32_transmit
1331+
};
13131332

13141333
int rt_hw_usart_init(void)
13151334
{

bsp/stm32/libraries/HAL_Drivers/drivers/drv_usart_v2.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ int rt_hw_usart_init(void);
4747
#define UART_RX_DMA_IT_HT_FLAG 0x01
4848
#define UART_RX_DMA_IT_TC_FLAG 0x02
4949

50+
#define UART_CTRL_SET_ERROR_CALLBACK 0x100
5051

5152
/* stm32 config class */
5253
struct stm32_uart_config
@@ -66,7 +67,8 @@ struct stm32_uart
6667
{
6768
UART_HandleTypeDef handle;
6869
struct stm32_uart_config *config;
69-
70+
/* device call back */
71+
rt_err_t (*error_indicate)(rt_device_t dev, rt_uint32_t error_code);
7072
#ifdef RT_SERIAL_USING_DMA
7173
struct
7274
{

0 commit comments

Comments
 (0)