使用Platformio平台的libopencm3开发框架来开发STM32G0,下面为使用FreeRTOS系统的队列Queue。
1 新建项目
在PIO主页新建项目,框架选择libopencm3,开发板选择 MonkeyPi_STM32_G070RB;
新建完成后在src目录新建主程序文件main.c;
然后更改项目文件platformio.ini的烧写和调试方式:
1upload_protocol = cmsis-dap
2debug_tool = cmsis-dap
2 添加FreeRTOS库
将上一节工程中的FreeRTOS目录直接拷贝到当前工程的lib目录下即可,添加完成后重新打开项目,以便VSCode获取代码索引;
3 在FreeRTOS中使用Queue
这里将串口接收的数据存放到队列中,然后在其他任务中可以取出来进行打印显示;
创建队列
1xQueueHandle uart_queue;
2
3//create queue
4 uart_queue = xQueueCreate(16, sizeof(char));
使用 xQueueCreate 接口创建队列,上面为创建16个单位的char类型存储队列;
在串口中断中发送数据到队列中
1/**
2 * @brief uart1 isr function
3 *
4 */
5void usart1_isr(void)
6{
7 //receive interrupt
8 if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) &&
9 ((USART_ISR(USART1) & USART_ISR_RXNE) != 0)) {
10
11 char c = usart_recv(USART1);
12
13 xQueueSendFromISR(uart_queue, &c, NULL);
14 }
15}
因为是在中断中,因此使用 xQueueSendFromISR 向队列发送数据,如果是不在中断中则直接使用 xQueueSend 接口;
在其他任务中取出数据
1static void app_uart_task(void *param)
2{
3 char str[32] = {0};
4 char i=0;
5
6 printf(" app uart task ");
7
8 while(1){
9 char c;
10 if( xQueueReceive(uart_queue, &c, 5) == pdPASS) {
11
12 //printf("recv:%c ", c);
13
14 if(c == ' '){
15 printf(" recv_str: %s ", str);
16 memset(str,0,32);
17 i=0;
18 }else{
19 if(i<32){
20 str[i++] = c;
21 }else{
22 i = 0;
23 }
24 }
25 }
26 }
27}
使用接口函数 xQueueReceive 从队列中取数据。
相关文章









