当UART接收的数据为不定长度时,那么该如何判断一帧数据的结束呢?我们可以使用IDLE LINE功能来判断。
同事Ji Cheng在其博客http://blog.chinaaet.com/detail/40378介绍了如何在KL26上使用这个功能,本博客在Freescale Cortex-M4核芯片K22上实现IDLE LINE功能。我这里完成的功能是:串口1使用中断方式接收数据,在接收中断服务函数中将接收到的数据再发送出去,当接收完一帧数据后,进入IDLE 中断并打印信息,以表示一帧数据的结束。
测试平台: FRDM_K22F + MQX4.2
BSP中改动的地方有:
1). user_config.h中,
打开UART1的中断功能
#define BSPCFG_ENABLE_ITTYB 0 改为: #define BSPCFG_ENABLE_ITTYB 1
2). serial.h中,添加
#define IO_IOCTL_SERIAL_ENABLE_IDLE _IO(IO_TYPE_SERIAL,0x1F)
3). serl_pol_kuart.c中,_kuart_polled_ioctl()函数中添加:
case IO_IOCTL_SERIAL_ENABLE_IDLE:
if( *(bool *)param_ptr == TRUE )
{
/* enable idleline */
sci_ptr->C2 |= UART_C2_ILIE_MASK; sci_ptr->C1 |= UART_C1_ILT_MASK;
}
else
{
/* disable idleline */
sci_ptr->C2 &= ~UART_C2_ILIE_MASK;
}
break;
本例程直接在hello.c文件中修改:
#include <mqx.h>
#include <bsp.h>
#include <fio.h>
#if ! BSPCFG_ENABLE_IO_SUBSYSTEM
#error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined non-zero in user_config.h. Please recompile BSP with this option.
#endif
#ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED
#error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in user_config.h and recompile BSP with this option.
#endif
/* Task IDs */
#define HELLO_TASK 5
extern void hello_task(uint32_t);
const TASK_TEMPLATE_STRUCT MQX_template_list[] =
{
/* Task Index, Function, Stack, Priority, Name, Attributes, Param, Time Slice */
{ HELLO_TASK, hello_task, 1500, 8, "hello", MQX_AUTO_START_TASK, 0, 0 },
{ 0 }
};
void UART1_RX_ISR(void);
/*TASK*-----------------------------------------------------
*
* Task Name : hello_task
* Comments :
* This task prints " Hello World "
*
*END*-----------------------------------------------------*/
void hello_task
(
uint32_t initial_data
)
{
(void)initial_data; /* disable 'unused variable' warning */
uint32_t result;
MQX_FILE_PTR uart1_dev = NULL;
bool enable_idleline = TRUE;
printf("Hello World\n");
uart1_dev=fopen( "ittyb:", NULL);
if( uart1_dev == NULL )
{
/* device could not be opened */
_task_block();
}
/* wait for transfer complete flag */
result = ioctl( uart1_dev, IO_IOCTL_SERIAL_ENABLE_IDLE, &enable_idleline );
if( result == IO_ERROR_INVALID_IOCTL_CMD )
{
/* ioctl not supported, use newer MQX version */
_task_block();
}
_int_install_isr(INT_UART1_RX_TX, UART1_RX_ISR,NULL);
_task_block();
}
void UART1_RX_ISR(void)
{
uint8_t Receive_data;
/* Rx */
if((UART1_BASE_PTR->S1 & UART_S1_RDRF_MASK))
{
Receive_data = UART1_BASE_PTR->D;
while(!(UART1_BASE_PTR->S1 & UART_S1_TDRE_MASK));
UART1_BASE_PTR->D = Receive_data;
}
/* IDLE interrupt */
if(UART1_S1&UART_S1_IDLE_MASK)// if IDLE Line interrupt occured
{
// clear interrupt flag; To clear IDLE, read UART status S1 with IDLE set and then read D
UART1_S1 |= UART_S1_IDLE_MASK;
Receive_data=(uint8_t)(UART1_BASE_PTR->D);
printf("\r\nGo to Idle line\r\n");
}
}
/* EOF */
最终实验现象为: