eCos读UART数据实例

#include "oem_portmisc.h"

static cyg_io_handle_t ser_hndl;

static char stackUART_READ_TASK_STACK_SZ;

static cyg_thread thread_data;

static cyg_handle_t thread_handle;

static char pf_stackPARSE_TASK_STACK_SZ;

static cyg_thread pf_thread_data;

static cyg_handle_t pf_thread_handle;

/* Mailbox的结构是一个FIFO类型的循环队列,存储的是指针 */

/* Search "cyg_mbox_create fifo" in baidu for more info */

static cyg_handle_t uart_mbox_h;

static cyg_mbox uart_mbox;

static cyg_mutex_t uart_mutex;

static u8 ring_bufMSG_SZ \* NR_MSG;

static u32 rb_write_seq = 0;

static u32 rb_read_seq = 0;

#define rb_lock() do { \

cyg_mutex_lock(&uart_mutex); \

} while (0)

#define rb_unlock() do { \

cyg_mutex_unlock(&uart_mutex); \

} while (0)

static bool snd_data(const u8 *data)

{

if (uart_mbox_h && cyg_mbox_tryput(uart_mbox_h, (void *)data)) {

return true;

}

return false;

}

void *uart_get_data(void)

{

if (uart_mbox_h) {

return cyg_mbox_get(uart_mbox_h);

}

return NULL;

}

static void push_msg(u8 *msg)

{

u32 offset;

if (!msg) {

return;

}

rb_lock();

offset = (rb_write_seq & (NR_MSG - 1)) * MSG_SZ;

memcpy(&(ring_bufoffset), msg, MSG_SZ);

rb_write_seq++;

if ((rb_write_seq - rb_read_seq) > NR_MSG) {

rb_read_seq = rb_write_seq - NR_MSG;

}

rb_unlock();

}

static bool pull_msg(u8 *msg)

{

u32 offset;

if (!msg) {

return false;

}

rb_lock();

if (rb_read_seq == rb_write_seq) {

rb_unlock();

return false;

}

offset = (rb_read_seq & (NR_MSG - 1)) * MSG_SZ;

memcpy(msg, &(ring_bufoffset), MSG_SZ);

rb_read_seq++;

rb_unlock();

return true;

}

static void uart_read_task(cyg_addrword_t data)

{

u8 frameMSG_SZ;

u8 i;

u32 nread;

u32 ret = 0;

diag_printf("OEM%s enter uart_read_task\n", func);

while (1) {

rx_2bytes_hdr:

i = 0;

while (i < 1) {

nread = 1;

ret = cyg_io_read(ser_hndl, &framei, &nread);

if (ENOERR != ret) {

continue;

}

if (framei != 0x55) {

continue;

}

i++;

break;

}

while (i < 2) {

nread = 1;

ret = cyg_io_read(ser_hndl, &framei, &nread);

if (ENOERR != ret) {

continue;

}

if (framei != 0x5A) {

goto rx_2bytes_hdr;

}

i++;

break;

}

while (i < MSG_SZ) {

nread = MSG_SZ - i;

ret = cyg_io_read(ser_hndl, &framei, &nread);

if (ENOERR != ret) {

continue;

}

i += nread;

}

push_msg(frame);

}

diag_printf("OEM%s finished\n", func);

}

static void parse_frame_task(cyg_addrword_t data)

{

u8 frameMSG_SZ;

u8 i;

while (1) {

if (pull_msg(frame)) {

#if defined(DEBUG)

for (i = 0; i < MSG_SZ; i++) {

diag_printf("%02x ", framei);

}

diag_printf("\n");

#endif

cyg_thread_delay(100);

}

}

}

void uart_init(void)

{

int ret;

// fuelgauge uart config

cyg_serial_info_t serial_info =

CYG_SERIAL_INFO_INIT(CYGNUM_SERIAL_BAUD_4800,

CYGNUM_SERIAL_STOP_1,

CYGNUM_SERIAL_PARITY_EVEN,

CYGNUM_SERIAL_WORD_LENGTH_8,

0 );

cyg_uint32 size = sizeof(serial_info);

cyg_mbox_create(&uart_mbox_h, &uart_mbox);

cyg_mutex_init(&uart_mutex);

ret = cyg_io_lookup(FG_UART, &ser_hndl);

if (ret != ENOERR) {

diag_printf("OEM failed to opening \"%s\"\n", FG_UART);

return 0;

}

ret = cyg_io_set_config(ser_hndl, CYG_IO_SET_CONFIG_SERIAL_INFO, &serial_info, &size);

if (ret != ENOERR)

diag_printf("OEM set_config flow_control returned an error\n");

diag_printf("OEM succeeded in opening \"%s\"\n", FG_UART);

cyg_thread_create(UART_READ_TASK_PRIORITY, // Priority - just a number

uart_read_task, // entry

0, // entry parameter

"uart_read_task", // Name

&stack0, // Stack

UART_READ_TASK_STACK_SZ, // Size

&thread_handle, // Handle

&thread_data // Thread data structure

);

cyg_thread_resume(thread_handle); // Start it

#if 1

// Decode frame thread

cyg_thread_create(PARSE_TASK_PRIORITY, // Priority - just a number

parse_frame_task, // entry

0, // entry parameter

"parse_frame_task", // Name

&pf_stack0, // Stack

PARSE_TASK_STACK_SZ, // Size

&pf_thread_handle, // Handle

&pf_thread_data // Thread data structure

);

cyg_thread_resume(pf_thread_handle); // Start it

#endif

}

module_init(uart_init);

相关推荐
ScilogyHunter1 个月前
Zephyr串口驱动开发及构建完全指南
驱动开发·uart·zephyr
记帖1 个月前
STM32C542开发(3)----配置串口打印
uart·stm32cubeide·stm32cubemx2·stm32c542cct6
ALINX技术博客1 个月前
【黑金云课堂】FPGA技术教程FPGA基础:FIFO与Uart通信
fpga开发·uart·fpga·fifo
山木嵌入式2 个月前
STM32串口不定长数据接收:超时解析法+DMA+空闲中断法(附完整代码)
stm32·单片机·串口·uart
Strugglingler2 个月前
【Linux 用户态操作 UART】
linux·uart
Strugglingler2 个月前
【Linux PL011驱动支持RS485】
linux·uart·rs485·pl011
山木嵌入式2 个月前
同步通信与异步通信(UART/USART):定义、原理、场景全解析
串口·嵌入式·uart·通信
山木嵌入式2 个月前
STM32 UART串口通信协议与3种底层驱动实现(寄存器/标准库/HAL库)
stm32·单片机·串口·uart
小懒懒️2 个月前
嵌入式常见通信协议——RS232,RS485
uart·rs232·rs485