以下部分都是在main.c里面
使用了静态信号量,用于触发中断时释放信号量,十分的方便,线程和信号量都需要进行定义。
定义:
static rt_thread_t usart1_thread = RT_NULL;
static void usart1_thread_entry(void* parameter);
static rt_err_t uart1_input(rt_device_t dev, rt_size_t size);
static rt_device_t serialuart1;
char str[] = "hello RT-Thread111!\r\n";
static struct rt_semaphore rx_sem1;
主函数
int main(void)
{ rt_sem_init(&rx_sem1, "rx_sem1", 0, RT_IPC_FLAG_FIFO);
usart1_thread =
rt_thread_create( "usart1",
usart1_thread_entry,
RT_NULL,
512,
5,
20);
if (usart1_thread != RT_NULL)
rt_thread_startup(usart1_thread);
else
return -1;
}
线程函数
static void usart1_thread_entry(void* parameter)
{
serialuart1 = rt_device_find("uart1");
rt_device_open(serialuart1 , RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX );
rt_device_set_rx_indicate(serialuart1 , uart1_input);
rt_device_write(serialuart1 , 0, str, (sizeof(str) - 1));
char ch;
while (1)
{
while (rt_device_read(serialuart1 , -1, &ch, 1) != 1)
{
rt_sem_take(&rx_sem1, RT_WAITING_FOREVER);
}
rt_device_write(serialuart1 , 0, &ch, 1);
}
}
回调函数
static rt_err_t uart1_input(rt_device_t dev, rt_size_t size)
{
rt_sem_release(&rx_sem1);
return RT_EOK;
}