模仿cdc

This commit is contained in:
dgjames 2024-12-21 19:25:52 +08:00
parent f27d46c2bd
commit 509665eb13

View File

@ -25,6 +25,7 @@ rt_thread_t Snake_Thread = RT_NULL;
rt_thread_t Infrared_Thread = RT_NULL;
rt_thread_t Test_Thread = RT_NULL;
rt_thread_t CDC_Thread = RT_NULL;
rt_thread_t Serial_Thread = RT_NULL;
char DEMO_PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char DEMO_DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0};
@ -404,33 +405,47 @@ void cdc_entry(void *parameter)
}
}
/* 用于接收消息的信号量 */
static struct rt_semaphore rx_sem;
/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
/* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
rt_sem_release(&rx_sem);
// /* 用于接收消息的信号量 */
// static struct rt_semaphore rx_sem;
// /* 接收数据回调函数 */
// static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
// {
// /* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
// rt_sem_release(&rx_sem);
return RT_EOK;
}
// return RT_EOK;
// }
static void serial_thread_entry(void *parameter)
{
char ch[105];
rt_device_t dev = RT_NULL;
char buf[] = "hello rt-thread!\r\n";
dev = rt_device_find("uart3");
if (dev)
rt_device_open(dev, RT_DEVICE_FLAG_RDWR);
else
return;
while (1)
{
/* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
int Size;
while ((Size = rt_device_read(serial, -1, &ch, 8)) != 1)
{
/* 阻塞等待接收信号量,等到信号量后再次读取数据 */
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
}
rt_kprintf("%s", ch);
rt_device_write(dev, 0, buf, rt_strlen(buf));
rt_thread_mdelay(500);
}
// char ch[105];
// while (1)
// {
// /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
// int Size;
// while ((Size = rt_device_read(serial, -1, &ch, 8)) != 1)
// {
// /* 阻塞等待接收信号量,等到信号量后再次读取数据 */
// rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
// }
// rt_kprintf("%s", ch);
// }
}
void ath_init(void)
@ -445,38 +460,39 @@ void ath_init(void)
}
void serial_init(void)
{
// 初始化设备
serial = rt_device_find("uart3");
if (Dev == RT_NULL)
{
rt_kprintf("uart3 not find. Fail");
return;
}
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 初始化配置参数 */
// // 初始化设备
// serial = rt_device_find("uart3");
// if (Dev == RT_NULL)
// {
// rt_kprintf("uart3 not find. Fail");
// return;
// }
// struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 初始化配置参数 */
config.baud_rate = BAUD_RATE_9600; // 修改波特率为 9600
config.data_bits = DATA_BITS_8; // 数据位 8
config.stop_bits = STOP_BITS_1; // 停止位 1
config.bufsz = 128; // 修改缓冲区 buff size 为 128
config.parity = PARITY_NONE; // 无奇偶校验位
// config.baud_rate = BAUD_RATE_9600; // 修改波特率为 9600
// config.data_bits = DATA_BITS_8; // 数据位 8
// config.stop_bits = STOP_BITS_1; // 停止位 1
// config.bufsz = 128; // 修改缓冲区 buff size 为 128
// config.parity = PARITY_NONE; // 无奇偶校验位
/* step3控制串口设备。通过控制接口传入命令控制字与控制参数 */
rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
/* 初始化信号量 */
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
/* 以 中断 接收及轮询发送方式打开串口设备 */
rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
// /* 设置接收回调函数 */
rt_device_set_rx_indicate(serial, uart_input);
const char *str = "hello RTT\r\n";
/* 发送字符串 */
rt_device_write(serial, 0, str, (sizeof(str) - 1));
/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
// /* step3控制串口设备。通过控制接口传入命令控制字与控制参数 */
// rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
// /* 初始化信号量 */
// rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
// /* 以 中断 接收及轮询发送方式打开串口设备 */
// rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
// // /* 设置接收回调函数 */
// rt_device_set_rx_indicate(serial, uart_input);
// const char *str = "hello RTT\r\n";
// /* 发送字符串 */
// rt_device_write(serial, 0, str, (sizeof(str) - 1));
// /* 创建 serial 线程 */
// rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
Serial_Thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
if (Serial_Thread != RT_NULL)
{
rt_thread_startup(thread);
rt_thread_startup(Serial_Thread);
}
else
{