【Avnet | NXP FRDM-MCXN947试用活动】+串口中断发送接收测试
串口是常用的外设,用于通信或者打印数据信息。本次就来试试使用中断方式来发送接收串口数据。首先初始化串口,使用中断方式需要设置中断回调函数。
```
lpuart_handle_t s_lpuart;
lpuart_transfer_t lpuart_recv;
uint8_t lpuart_buff;
volatile bool uart_send_ok = false;
volatile bool uart_recv_ok = false;
void lpuart_isr_cb(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData)
{
if(base == LPUART4)
{
switch (status)
{
case kStatus_LPUART_RxIdle:
uart_recv_ok = true;
LPUART_TransferReceiveNonBlocking(LPUART4, &s_lpuart,&lpuart_recv,NULL);
break;
case kStatus_LPUART_TxIdle:
uart_send_ok = true;
break;
case kStatus_LPUART_RxRingBufferOverrun://
break;
default:
break;
}
}
}
void uart_init(uint32_t bps)
{
lpuart_config_t usrtConfig;
/* Board pin, clock, debug console init */
/* attach FRO 12M to FLEXCOMM4 (debug console) */
CLOCK_SetClkDiv(kCLOCK_DivFlexcom4Clk, 1u);
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM4);
LPUART_GetDefaultConfig(&usrtConfig);
usrtConfig.baudRate_Bps = bps;
usrtConfig.enableSingleWire = 0;
usrtConfig.enableRx = 1U;
usrtConfig.enableTx = 1U;
LPUART_Init(LPUART4, &usrtConfig, 12000000U);
LPUART_TransferCreateHandle(LPUART4, &s_lpuart, lpuart_isr_cb, NULL);
}
```
使用如下API创建中断函数句柄和传递回调函数参数。
!(https://www.eefocus.com/forum/data/attachment/forum/202411/17/212022b4u1ze1w9f1zz914.png)
如下为设置中断函数处理。
!(https://www.eefocus.com/forum/data/attachment/forum/202411/17/212200kddpqkrqbdzd2qbe.png)
下面是FLEXCOMM通用的调用中断处理。
!(https://www.eefocus.com/forum/data/attachment/forum/202411/17/212347ybkagy2k2aki4kgf.png)
下面是串口具体的中断处理函数了。
!(https://www.eefocus.com/forum/data/attachment/forum/202411/17/212546goxxjx6pyv60x5y6.png)
具体的串口中断函数就会根据状态调用我们初始设置的中断回调函数。
!(https://www.eefocus.com/forum/data/attachment/forum/202411/17/212828m5c7a5bdsvb5dz6e.png)
中断回调函数原型:
```
/*! @brief LPUART transfer callback function. */
typedef void (*lpuart_transfer_callback_t)(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData);
```
下面我们来写具体的回调处理,发送接收串口数据。
!(https://www.eefocus.com/forum/data/attachment/forum/202411/17/213247u5ep9e139e45xp3i.png)
!(https://www.eefocus.com/forum/data/attachment/forum/202411/17/213419trhicmfgvrhh1kr3.png)
编译烧写之后可以通过串口助手来测试一下发送接收数据。
!(https://www.eefocus.com/forum/data/attachment/forum/202411/17/213524acy6om2ooooycmtt.png)
可以看到发送接收相符。
页:
[1]