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

[复制链接]
 楼主| 比神乐 发表于 2024-12-9 16:46 | 显示全部楼层 |阅读模式
代码:
  1. #include "at32l021_board.h"
  2. #include "at32l021_clock.h"
  3. #include "FreeRTOS.h"
  4. #include "task.h"
  5. #include "queue.h"
  6. #include "semphr.h"
  7. #include "stdio.h"
  8. /** @addtogroup UTILITIES_examples
  9.   * @{
  10.   */
  11. int cnt=0;
  12. SemaphoreHandle_t MutexSemaphore;
  13. /** @addtogroup FreeRTOS_demo
  14.   * @{
  15.   */

  16. //unsigned char const mainSENDER_1=30;
  17. //unsigned char const mainSENDER_2=50;
  18. button_type at32_button_press(void);
  19. void task1(void *pvParameters)
  20. {
  21.     static uint32_t times;
  22.     while(1)
  23.     {
  24.                                 xSemaphoreTake(MutexSemaphore,portMAX_DELAY);
  25.                                 printf("low task Running!\r\n");
  26.                                 for(times=0;times<2000000;times++)
  27.                                 {
  28.                                         delay_us(2);
  29.                                         //taskYIELD();
  30.                                 }
  31.                                 xSemaphoreGive(MutexSemaphore);
  32.         vTaskDelay(1000);                                               /* 延时1000ticks */
  33.     }
  34. }

  35. /**
  36. * [url=home.php?mod=space&uid=247401]@brief[/url]       task2
  37. * @param       pvParameters : 传入参数(未用到)
  38. * @retval      无
  39. */

  40. void Keyprocess_task(void *pvParameters)
  41. {
  42.     while(1)
  43.     {
  44.                        
  45.                         printf("middle task Running!\r\n");
  46.                         vTaskDelay(1000);
  47.                                
  48.     }
  49. }

  50. void task3(void *pvParameters)
  51. {
  52.                
  53.     while(1)
  54.     {
  55.                         printf("high task Pend Sem!\r\n");
  56.                         xSemaphoreTake(MutexSemaphore,portMAX_DELAY);
  57.                         printf("high task Running!\r\n");
  58.                         xSemaphoreGive(MutexSemaphore);
  59.                         vTaskDelay(1000);
  60.                                
  61.     }
  62. }
  63. /**
  64.   * @brief  main function.
  65.   * @param  none
  66.   * @retval none
  67.   */
  68. int main(void)
  69. {
  70.   system_clock_config();
  71.         delay_init();
  72.   /* init led2 and led3 */
  73.   at32_led_init(LED2);
  74.   at32_led_init(LED3);
  75.         at32_button_init();
  76.   /* init usart1 */
  77.   uart_print_init(115200);

  78.   
  79.         MutexSemaphore=xSemaphoreCreateMutex();
  80.                 if(MutexSemaphore==NULL)
  81.                 {
  82.                         printf("Sema Create Failed!\r\n");
  83.                 }
  84.        
  85.                 /* create led2 task */
  86.                 xTaskCreate(task1,
  87.                  "vTask1",
  88.                  128,
  89.                 (void *)30,
  90.                  
  91.                                                                  2,
  92.                 NULL);
  93.                
  94.                 xTaskCreate(Keyprocess_task,
  95.                  "vTask2",
  96.                  128,
  97.                  NULL,
  98.                  3,
  99.                  NULL);
  100.                 xTaskCreate(task3,
  101.                  "vTask3",
  102.                  128,
  103.                  NULL,
  104.                  4,
  105.                  NULL);

  106.                 /* start scheduler */
  107.                 vTaskStartScheduler();
  108.        
  109. }

运行效果图:
1.jpg
电竞孔乙己 发表于 2024-12-18 14:19 | 显示全部楼层
这看不出来互斥啊,可以稍微讲解一下不。三个任务的执行过程
 楼主| 比神乐 发表于 2024-12-19 15:58 | 显示全部楼层
互斥信号量是为了解决优先级反转的问题,不是一两句话就能解释清楚的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3535

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部

470

主题

3535

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部