以下部分都是在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;
}