之前做过DMA加串口空闲中断的方式接收不定长数据,想移植过来,但是那样的话就用不上rtthread自带的信号量这个内容了,于是基于信号量做了一个不定长接收的功能,基本思路是用两个字节间的时间间隔来做区分两帧的依据,用信号量好处不用另外开定时器,代码如下:
#define SAMPLE_UART4_NAME "uart4" /* 需要操作的设备 /
static struct rt_semaphore rx_sem4; / 信号量 /
static rt_device_t serial4; / 设备句柄 */
/* 接收数据回调函数 /
static rt_err_t uart4_rx_ind(rt_device_t dev, rt_size_t size)
{
/ 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
if (size > 0)
{
rt_sem_release(&rx_sem4);
}
return RT_EOK;
}
static char uart_ZIGBEE_get_char(void)
{
char ch;
static uint8_t rv_flag = 1;
while (rt_device_read(serial4, 0, &ch, 1) == 0)
{
rt_sem_control(&rx_sem4, RT_IPC_CMD_RESET, RT_NULL);
if(rv_flag)
rt_sem_take(&rx_sem4, RT_WAITING_FOREVER);
else if(-RT_ETIMEOUT == rt_sem_take(&rx_sem4, 100))
{
rv_flag = 1;
ch = '\n';
return ch;
}
}
rv_flag = 0;
return ch;
}
/* 数据解析线程 */
static void ZIGBEE_data_parsing(void)
{
char rx_temp = 0, rx_len = 0;
char data_buf[30] = {0};
while (1)
{
rx_temp = uart_ZIGBEE_get_char();
if(rx_temp != '\n')
{
data_buf[(rx_len++) % 30] = rx_temp;
}
else
{
rx_len = 0;
rt_kprintf("uart4 received data is %s\n", data_buf);
memset(data_buf, 0, sizeof(data_buf));
}
}
}
static int uart4_ZIGBEE(void)
{
rt_err_t ret = RT_EOK;
/* 查找系统中的串口设备 /
serial4 = rt_device_find(SAMPLE_UART4_NAME);
if (!serial4)
{
rt_kprintf("find %s failed!\n", SAMPLE_UART4_NAME);
return RT_ERROR;
}
/ 初始化信号量 /
rt_sem_init(&rx_sem4, "rx_sem4", 0, RT_IPC_FLAG_FIFO);
/ 以中断接收及轮询发送模式打开串口设备 /
rt_device_open(serial4, RT_DEVICE_FLAG_INT_RX);
/ 设置接收回调函数 */
rt_device_set_rx_indicate(serial4, uart4_rx_ind);
rt_thread_t thread = rt_thread_create("ZIG_th", (void (*)(void *parameter))ZIGBEE_data_parsing,
RT_NULL, 512, RT_THREAD_PRIORITY_MAX-10, 10);
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
ret = RT_ERROR;
}
return ret;
}
INIT_APP_EXPORT(uart4_ZIGBEE);
最终效果如下:
原作者:Sky_Lannister