2016-02-13 116 views
0

我正在学习freertos。我怎样才能让任务在运行2秒后进入睡眠状态?我尝试使用vTaskSuspend(),但立即停止任务 ,我也试图把v taskDelay(2000)之前,它没有多大的不同。 我想在被调用2秒后让快速闪烁的任务进入休眠状态,并运行正常的闪烁任务。freertos暂停任务

void resourse(const TickType_t xFrequency) 
{ 
    TickType_t xLastWakeTime; 
    xLastWakeTime = xTaskGetTickCount(); 
    while(1) 
    { 
    HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin); 
    vTaskDelayUntil(&xLastWakeTime, xFrequency); 
    } 
} 
xSemaphoreHandle botton_one = 0; 
void botton(void* r) 
{ 

    while(1) 
     { 
     if(xSemaphoreTake(botton_one, 1)) 
     { 
     HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin); 
     xSemaphoreGive(botton_one); 
     } 
     vTaskDelay(1); 

     } 
} 
void normal_blinking(void* r) 
{ 
    while(1) 
     { 
     if(xSemaphoreTake(botton_one, 1)) 
     { 
      resourse(500); 
      xSemaphoreGive(botton_one); 
     } 

     } 
} 
void fast_blinking(void* s) 
{ 
    while(1){ 
     if((HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin))== 0) 
     { 
     xSemaphoreTake(botton_one, 1); 
     resourse(50); 
     xSemaphoreGive(botton_one); 
     } 
     vTaskDelay(2000); 
     vTaskSuspend(NULL); 
    } 
} 

int main(void) 
{ 
    TaskHandle_t xHandle; 
    botton_one = xSemaphoreCreateMutex(); 
    HAL_Init(); 
    SystemClock_Config(); 
    MX_GPIO_Init(); 

    xTaskCreate(botton, (const char*)"task_1", 1024, 0, 3, 0); 
    xTaskCreate(normal_blinking, (const char*)"task_2", 1024, 0, 2,0); 
    xTaskCreate(fast_blinking, (const char*)"task_3", 1024, 0, 1,0); 

    vTaskStartScheduler(); 

    while (1){ 
    } 
} 

回答

0

目前还不清楚你想要做什么,或者什么不按你想要的工作。您建议同时使用vTaskSuspend()vTaskDelay(),但它们用于不同的事情。

如果您调用vTaskDelay(),则任务会在您指定的任何时间段内进入阻塞状态(停止作为可以实际执行的任务),然后在该时间段后自动离开阻塞状态。

如果调用vTaskSuspend(),则任务将进入挂起状态,并且除非另一个任务或中断调用vTaskResume(),否则将永不再运行。

你只是想让任务运行两秒钟,然后再也不会呢?在这种情况下,你可以做一些简单的事情,如:

void mytask(void *pv) 
{ 
TickType_t xTimeOnEntering = xTaskGetTickCount(); 

    while(xTaskGetTickCount() - xTimeOnEntering < pdMS_TO_TICKS(2000)) 
    { 
     /* Run your code here. */ 
    } 

    /* Two seconds of execution is up, delete the task. */ 
    <a href="http://www.freertos.org/a00126.html">vTaskDelete</a>(NULL); 
} 
+0

我需要任务运行两秒钟,然后进入睡眠状态,并在被调用时重新开始。这是通过按下一个按钮来完成的。 –