K22中使用UART的IDLE Line功能

当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 */

最终实验现象为:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值