基本实例

  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优先级比较高的时候。

05-11 13:55