• stm32f103中freertos的tasks基本使用案例及备忘


    基本实例

      freetos的在stm32中使用踩了一些坑,事情做完了,就 做个备忘,希望能给后面的人一些借鉴。
    先给出一个实际的例子吧。

    • 启动代码
    void task_create(void)
    {
        xTaskCreate(vButtonCheckTask,"Button",configMINIMAL_STACK_SIZE,NULL,configMAX_PRIORITIES-1,NULL);
        xTaskCreate(vButtonLEDsTask,"ButtonLeds",configMINIMAL_STACK_SIZE,NULL,configMAX_PRIORITIES-1,NULL);
    }
    
    
    • 回调函数
    static void vButtonCheckTask( void *pvParameters )
    {
      //for debounce
      static uint8_t count;
      portTickType xLastWakeTime;
      const portTickType xFrequency = 20;
      const portTickType yDelay = 20 / portTICK_RATE_MS;
      xLastWakeTime=xTaskGetTickCount();
      //create semaphores for each button
      vSemaphoreCreateBinary(xButtonWakeupSemaphore);
      vSemaphoreCreateBinary(xButtonUser1Semaphore);
      vSemaphoreCreateBinary(xButtonUser2Semaphore);
    //check if semaphores were created successfully
      if((xButtonWakeupSemaphore!=NULL)&&(xButtonUser1Semaphore!=NULL)&&(xButtonUser2Semaphore!=NULL))
      {
        //successfully created
        //resets initial semaphores to 0
        xSemaphoreTake(xButtonWakeupSemaphore, (portTickType)0);
        xSemaphoreTake(xButtonUser1Semaphore, (portTickType)0);
        xSemaphoreTake(xButtonUser2Semaphore, (portTickType)0);
      }  else {
          //send error of failure
      }
    
      for (;;)
        {
          if (ButtonRead(BWAKEUPPORT, BWAKEUP)==pdTRUE)
            {
              vTaskDelay(yDelay);
              if(ButtonRead(BWAKEUPPORT, BWAKEUP)==pdTRUE)
              {
                  while(ButtonRead(BWAKEUPPORT, BWAKEUP)==pdTRUE);
                  xSemaphoreGive(xButtonWakeupSemaphore);
                  //LEDToggle(1);
                  usart1_puts(" key1 pressed 
    ");
                  //printf(" led 1 on
    ");
              }
            }
          if (ButtonRead(BUSER1PORT, BUSER1)==pdTRUE)
            {
              vTaskDelay(yDelay);
              if(ButtonRead(BUSER1PORT, BUSER1)==pdTRUE)
              {
                  while(ButtonRead(BUSER1PORT, BUSER1)==pdTRUE);
                  //LEDToggle(1);
                  //printf(" use1 presssed
    
    ");
                  usart1_puts("key2 presssed
    
    ");
                  xSemaphoreGive(xButtonUser1Semaphore);
              }
            }
          if (ButtonRead(BUSER2PORT, BUSER2)==pdTRUE)
            {
              vTaskDelay(yDelay);
              if(ButtonRead(BUSER2PORT, BUSER2)==pdTRUE)
              {
                  while(ButtonRead(BUSER2PORT, BUSER2)==pdTRUE);
                  usart1_puts("key3 presssed 
    
    ");
                  //xSemaphoreGive(xButtonUser2Semaphore);
              }
            }
        }
    }
    
    void vButtonLEDsTask( void *pvParameters )
    {
      const portTickType xDelay = 50 / portTICK_RATE_MS;
    
      for( ;; )
      {
          if((xButtonWakeupSemaphore!=NULL)&&(xButtonUser1Semaphore!=NULL)&&(xButtonUser2Semaphore!=NULL))
          {
             if (xSemaphoreTake(xButtonWakeupSemaphore, (portTickType)10)==pdTRUE)
                 {
                   //LEDOn(1);
                   usart1_puts("led1 on 
    
    ");
                   LEDToggle(1);
                   //give semaphore back
                   //xSemaphoreGive(xButtonWakeupSemaphore);
                 }
             if (xSemaphoreTake(xButtonUser1Semaphore, (portTickType)10)==pdTRUE)
                 {
                   usart1_puts("led2 on 
    
    ");
                   LEDToggle(2);
                   //LEDOn(2);
                   //xSemaphoreGive(xButtonUser1Semaphore);
                 }
             if (xSemaphoreTake(xButtonUser2Semaphore, (portTickType)10)==pdTRUE)
                 {
                   usart1_puts("led3 on 
    
    ");
                   //LEDToggle(3);
                   //LEDOn(2);
                   //xSemaphoreGive(xButtonUser2Semaphore);
                 }
          }
    
        //usart1_puts("task running 
    
    ");
        vTaskDelay(xDelay);
        //vTaskDelayUntil(&xLastWakeTime,xFrequency);
      }
    }
    
    

    重要备忘

      freetos的task和里面的函数尽量在一个文件中。
    对于某些stm32 的平台,回调函数和task不在一个文件下,会出现一些异常。

      freetos的task的回调函数尽量使用静态函数:

      freetos的task中的循环中一定不能丢了
    vTaskDelay(xDelay);不然会出现一直循环,被调度不到的情况,特别是你的task优先级比较高的时候。

  • 相关阅读:
    基于jquery的提示框JavaScript 插件,类Bootstrap
    css重点知识和bug解决方法
    小白初识js
    动态的将数据生成“表格”(京东商品),并具备搜索筛选工能
    原装js轮播图,鼠标移入停止轮播,移出继续轮播
    用js写九九乘法表格,附带背景颜色
    用js写直角三角形,等腰三角形,菱形
    关于两数组,求出两数组中相同的的对象
    好的js书写习惯
    前端之道
  • 原文地址:https://www.cnblogs.com/dylancao/p/12340433.html
Copyright © 2020-2023  润新知