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 \r\n");
              //printf(" led 1 on\r\n");
          }
        }
      if (ButtonRead(BUSER1PORT, BUSER1)==pdTRUE)
        {
          vTaskDelay(yDelay);
          if(ButtonRead(BUSER1PORT, BUSER1)==pdTRUE)
          {
              while(ButtonRead(BUSER1PORT, BUSER1)==pdTRUE);
              //LEDToggle(1);
              //printf(" use1 presssed\n\r");
              usart1_puts("key2 presssed\n\r");
              xSemaphoreGive(xButtonUser1Semaphore);
          }
        }
      if (ButtonRead(BUSER2PORT, BUSER2)==pdTRUE)
        {
          vTaskDelay(yDelay);
          if(ButtonRead(BUSER2PORT, BUSER2)==pdTRUE)
          {
              while(ButtonRead(BUSER2PORT, BUSER2)==pdTRUE);
              usart1_puts("key3 presssed \n\r");
              //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 \n\r");
               LEDToggle(1);
               //give semaphore back
               //xSemaphoreGive(xButtonWakeupSemaphore);
             }
         if (xSemaphoreTake(xButtonUser1Semaphore, (portTickType)10)==pdTRUE)
             {
               usart1_puts("led2 on \n\r");
               LEDToggle(2);
               //LEDOn(2);
               //xSemaphoreGive(xButtonUser1Semaphore);
             }
         if (xSemaphoreTake(xButtonUser2Semaphore, (portTickType)10)==pdTRUE)
             {
               usart1_puts("led3 on \n\r");
               //LEDToggle(3);
               //LEDOn(2);
               //xSemaphoreGive(xButtonUser2Semaphore);
             }
      }

    //usart1_puts("task running \n\r");
    vTaskDelay(xDelay);
    //vTaskDelayUntil(&xLastWakeTime,xFrequency);
  }
}

重要備忘

  freetos的task和里面的函數盡量在一個文件中。
對於某些stm32 的平台,回調函數和task不在一個文件下,會出現一些異常。

  freetos的task的回調函數盡量使用靜態函數:

  freetos的task中的循環中一定不能丟了
vTaskDelay(xDelay);不然會出現一直循環,被調度不到的情況,特別是你的task優先級比較高的時候。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM