比神乐 发表于 2024-12-9 10:59

【AT-START-L021测评】+FREERTOS二值信号量

代码:
#include "at32l021_board.h"
#include "at32l021_clock.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
/** @addtogroup UTILITIES_examples
* @{
*/
QueueHandle_t xBinarySemaphore;
/** @addtogroup FreeRTOS_demo
* @{
*/
static void vPeriodicTassk(void *pvParamters)
{
      button_type key;
    while(1)
    {      
                              key=at32_button_press();
                        if(key==0)
                        {
                                        if(xBinarySemaphore!=NULL)
                                        {
                                                xSemaphoreGive(xBinarySemaphore);
                                        }
                              
      
                              //LED0_TOGGLE();
      //vTaskDelay(10);
                              }/* 延时1000ticks */
    }
}
static void vHandlerTask(void *pvParamters)
{
      for(;;)
      {
                xSemaphoreTake(xBinarySemaphore,portMAX_DELAY);
                //printf("Handler task - Processing event.\r\n");
                if(xBinarySemaphore!=NULL)
                {
                        at32_led_toggle(LED2);
                }
                //vTaskDelay(500);
      }
}
/**
* @briefmain function.
* @paramnone
* @retval none
*/
int main(void)
{
system_clock_config();
      at32_board_init();
/* init led2 and led3 */
at32_led_init(LED2);
at32_led_init(LED3);

/* init usart1 */
uart_print_init(115200);
      
/* enter critical */
taskENTER_CRITICAL();
      vSemaphoreCreateBinary(xBinarySemaphore);
/* create led2 task */
xTaskCreate((TaskFunction_t )vPeriodicTassk,
               (const char*    )"vTask1",
               (uint16_t       )128,
               (void*          )NULL,
               (UBaseType_t    )1,
               (TaskHandle_t*)NULL);

/* create led3 task */
xTaskCreate((TaskFunction_t )vHandlerTask,
               (const char*    )"vTask2",
               (uint16_t       )128,
               (void*          )NULL,
               (UBaseType_t    )2,
               (TaskHandle_t*)NULL) ;


/* exit critical */
taskEXIT_CRITICAL();

/* start scheduler */
vTaskStartScheduler();
}

效果图

页: [1]
查看完整版本: 【AT-START-L021测评】+FREERTOS二值信号量