比神乐 发表于 2024-12-9 16:46

【AT-START-L021测评】FREERTOS之互斥信号量

代码:
#include "at32l021_board.h"
#include "at32l021_clock.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "stdio.h"
/** @addtogroup UTILITIES_examples
* @{
*/
int cnt=0;
SemaphoreHandle_t MutexSemaphore;
/** @addtogroup FreeRTOS_demo
* @{
*/

//unsigned char const mainSENDER_1=30;
//unsigned char const mainSENDER_2=50;
button_type at32_button_press(void);
void task1(void *pvParameters)
{
    static uint32_t times;
    while(1)
    {
                                xSemaphoreTake(MutexSemaphore,portMAX_DELAY);
                                printf("low task Running!\r\n");
                                for(times=0;times<2000000;times++)
                                {
                                        delay_us(2);
                                        //taskYIELD();
                                }
                                xSemaphoreGive(MutexSemaphore);
      vTaskDelay(1000);                                             /* 延时1000ticks */
    }
}

/**
* @brief       task2
* @param       pvParameters : 传入参数(未用到)
* @retval      无
*/

void Keyprocess_task(void *pvParameters)
{
    while(1)
    {
                       
                        printf("middle task Running!\r\n");
                        vTaskDelay(1000);
                               
    }
}

void task3(void *pvParameters)
{
               
    while(1)
    {
                        printf("high task Pend Sem!\r\n");
                        xSemaphoreTake(MutexSemaphore,portMAX_DELAY);
                        printf("high task Running!\r\n");
                        xSemaphoreGive(MutexSemaphore);
                        vTaskDelay(1000);
                               
    }
}
/**
* @briefmain function.
* @paramnone
* @retval none
*/
int main(void)
{
system_clock_config();
        delay_init();
/* init led2 and led3 */
at32_led_init(LED2);
at32_led_init(LED3);
        at32_button_init();
/* init usart1 */
uart_print_init(115200);


        MutexSemaphore=xSemaphoreCreateMutex();
                if(MutexSemaphore==NULL)
                {
                        printf("Sema Create Failed!\r\n");
                }
       
                /* create led2 task */
                xTaskCreate(task1,
               "vTask1",
               128,
                (void *)30,
               
                                                               2,
                NULL);
               
                xTaskCreate(Keyprocess_task,
               "vTask2",
               128,
               NULL,
               3,
               NULL);
                xTaskCreate(task3,
               "vTask3",
               128,
               NULL,
               4,
               NULL);

                /* start scheduler */
                vTaskStartScheduler();
       
}

运行效果图:

电竞孔乙己 发表于 2024-12-18 14:19

这看不出来互斥啊,可以稍微讲解一下不。三个任务的执行过程

比神乐 发表于 2024-12-19 15:58

互斥信号量是为了解决优先级反转的问题,不是一两句话就能解释清楚的
页: [1]
查看完整版本: 【AT-START-L021测评】FREERTOS之互斥信号量