[AT32L021] 【AT-START-L021测评】+ 运行FreeRTOS

[复制链接]
1508|0
 楼主| tlled 发表于 2024-12-5 00:03 | 显示全部楼层 |阅读模式
移植FreeRTOS系统到开发板。

一、下载FreeRTOS软件

下载地址:https://www.freertos.org/
001.png

二、添加文件到工程

将下载的源码添加到工程中
002.png

三、程序代码部分

根据芯片的硬件资源来配置FreeRTOS。
3.1、FreeRTOSConfig.h
  1. /*
  2. * FreeRTOS V202212.01
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * https://www.FreeRTOS.org
  23. * https://github.com/FreeRTOS
  24. *
  25. */

  26. #ifndef FREERTOS_CONFIG_H
  27. #define FREERTOS_CONFIG_H

  28. /*-----------------------------------------------------------
  29. * Application specific definitions.
  30. *
  31. * These definitions should be adjusted for your particular hardware and
  32. * application requirements.
  33. *
  34. * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
  35. * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
  36. *
  37. * See http://www.freertos.org/a00110.html.
  38. *----------------------------------------------------------*/

  39. /* Ensure definitions are only used by the compiler, and not by the assembler. */
  40. #if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
  41.   #include <stdint.h>
  42.   extern uint32_t system_core_clock;//SystemCoreClock;
  43. #endif


  44. #define configUSE_PREEMPTION                                        1
  45. #define configSUPPORT_STATIC_ALLOCATION                        0//1
  46. #define configSUPPORT_DYNAMIC_ALLOCATION                1
  47. #define configUSE_IDLE_HOOK                                                0
  48. #define configUSE_TICK_HOOK                                                0
  49. #define configCPU_CLOCK_HZ                                                (system_core_clock)//80000000//( SystemCoreClock )
  50. #define configTICK_RATE_HZ                                                ( ( TickType_t ) 1000 )
  51. #define configMAX_PRIORITIES                                        ( 56 )
  52. #define configMINIMAL_STACK_SIZE                                ( ( uint16_t ) 512 )
  53. #define configTOTAL_HEAP_SIZE                                        ( ( size_t ) 5 * 1024 )
  54. #define configMAX_TASK_NAME_LEN                                        ( 16 )
  55. #define configUSE_TRACE_FACILITY                                1
  56. #define configUSE_16_BIT_TICKS                                        0
  57. #define configUSE_MUTEXES                                                1
  58. #define configQUEUE_REGISTRY_SIZE                                8
  59. #define configUSE_RECURSIVE_MUTEXES                                1
  60. #define configUSE_COUNTING_SEMAPHORES                        1
  61. #define configUSE_PORT_OPTIMISED_TASK_SELECTION        0
  62. #define configUSE_MALLOC_FAILED_HOOK                        0//1
  63. #define configCHECK_FOR_STACK_OVERFLOW                        0//2

  64. /* Defaults to size_t for backward compatibility, but can be changed
  65. * if lengths will always be less than the number of bytes in a size_t. */
  66. #define configMESSAGE_BUFFER_LENGTH_TYPE                size_t
  67. /* USER CODE END MESSAGE_BUFFER_LENGTH_TYPE */

  68. /* Software timer definitions. */
  69. #define configUSE_TIMERS                                                1
  70. #define configTIMER_TASK_PRIORITY                                ( 2 )
  71. #define configTIMER_QUEUE_LENGTH                                10
  72. #define configTIMER_TASK_STACK_DEPTH                        256

  73. /* Set the following definitions to 1 to include the API function, or zero
  74. * to exclude the API function. */
  75. #define INCLUDE_vTaskPrioritySet                                1
  76. #define INCLUDE_uxTaskPriorityGet                                1
  77. #define INCLUDE_vTaskDelete                                                1
  78. #define INCLUDE_vTaskCleanUpResources                        0
  79. #define INCLUDE_vTaskSuspend                                        1
  80. #define INCLUDE_vTaskDelayUntil                                        1
  81. #define INCLUDE_vTaskDelay                                                1
  82. #define INCLUDE_xTaskGetSchedulerState                        1
  83. #define INCLUDE_xTimerPendFunctionCall                        0
  84. #define INCLUDE_xQueueGetMutexHolder                        1
  85. #define INCLUDE_uxTaskGetStackHighWaterMark                1
  86. #define INCLUDE_eTaskGetState                                        1

  87. /* Cortex-M specific definitions. */
  88. #ifdef __NVIC_PRIO_BITS
  89.         /* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
  90.         #define configPRIO_BITS                                                __NVIC_PRIO_BITS
  91. #else
  92.         #define configPRIO_BITS                                                4
  93. #endif

  94. /* The lowest interrupt priority that can be used in a call to a "set priority"
  95. * function. */
  96. #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY        15

  97. /* The highest interrupt priority that can be used by any interrupt service
  98. * routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT
  99. * CALL INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A
  100. * HIGHER PRIORITY THAN THIS! (higher priorities are lower numeric values. */
  101. #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5

  102. /* Interrupt priorities used by the kernel port layer itself.  These are generic
  103. * to all Cortex-M ports, and do not rely on any particular library functions. */
  104. #define configKERNEL_INTERRUPT_PRIORITY                                ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << ( 8 - configPRIO_BITS ) )
  105. /* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
  106. See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
  107. #define configMAX_SYSCALL_INTERRUPT_PRIORITY                ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << ( 8 - configPRIO_BITS ) )

  108. /* Normal assert() semantics without relying on the provision of an assert.h
  109. * header file. */
  110. #define configASSERT( x )                                                        if ( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }

  111. /* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
  112. * standard names. */
  113. #define vPortSVCHandler                                                                SVC_Handler
  114. #define xPortPendSVHandler                                                        PendSV_Handler
  115. //#define xPortSysTickHandler                                                        SysTick_Handler

  116. /* Allow system call from within FreeRTOS kernel only. */
  117. #define configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY        1

  118. /* STM32H743 has 16 MPU regions and therefore it is necessary to configure
  119. * configTOTAL_MPU_REGIONS correctly. */
  120. #define configTOTAL_MPU_REGIONS                                                16

  121. /* The default TEX,S,C,B setting marks the SRAM as shareable and as a result,
  122. * disables cache. Do not mark the SRAM as shareable because caching is being
  123. * used. TEX=0, S=0, C=1, B=1. */
  124. #define configTEX_S_C_B_SRAM                                                ( 0x03UL )

  125. #endif /* FREERTOS_CONFIG_H */


3.2、fun_task.c
  1. #include "main.h"

  2. #define START_TASK_PRO                1                               
  3. #define START_STK_SIZE                64
  4. TaskHandle_t StartTask_Handler;

  5. #define TASK1_PRIO      2                  
  6. #define TASK1_STK_SIZE  64                 
  7. static TaskHandle_t            Task1Task_Handler = NULL;  
  8.            
  9. #define TASK2_PRIO      3                  
  10. #define TASK2_STK_SIZE  64                 
  11. static TaskHandle_t            Task2Task_Handler = NULL;  

  12. #define TASK3_PRIO      4                  
  13. #define TASK3_STK_SIZE  64                 
  14. static TaskHandle_t            Task3Task_Handler = NULL;  


  15. void start_task(void *pvParameters);

  16. void task1(void *pvParameters);  
  17. void task2(void *pvParameters);
  18. void task3(void *pvParameters);

  19. void task_create(void)
  20. {
  21.        
  22.         //start_task
  23.         xTaskCreate((TaskFunction_t )start_task,                  
  24.                                                         (const char*    )"start_task",               
  25.                                                         (uint16_t       )START_STK_SIZE,         
  26.                                                         (void*          )NULL,                  
  27.                                                         (UBaseType_t    )START_TASK_PRO,            
  28.                                                         (TaskHandle_t*  )&StartTask_Handler);   

  29.         vTaskStartScheduler();
  30. }

  31. void start_task(void *pvParameters)
  32. {
  33.         taskENTER_CRITICAL();
  34.         //task1
  35.         xTaskCreate((TaskFunction_t )task1,                  
  36.                                                         (const char*    )"task1",               
  37.                                                         (uint16_t       )TASK1_STK_SIZE,         
  38.                                                         (void*          )NULL,                  
  39.                                                         (UBaseType_t    )TASK1_PRIO,            
  40.                                                         (TaskHandle_t*  )&Task1Task_Handler);   
  41.         //task2
  42.         xTaskCreate((TaskFunction_t )task2,                  
  43.                                                         (const char*    )"task2",               
  44.                                                         (uint16_t       )TASK2_STK_SIZE,        
  45.                                                         (void*          )NULL,                  
  46.                                                         (UBaseType_t    )TASK2_PRIO,            
  47.                                                         (TaskHandle_t*  )&Task2Task_Handler);
  48.                                                        
  49.         //task3
  50.         xTaskCreate((TaskFunction_t )task3,                  
  51.                                                         (const char*    )"task3",               
  52.                                                         (uint16_t       )TASK3_STK_SIZE,        
  53.                                                         (void*          )NULL,                  
  54.                                                         (UBaseType_t    )TASK3_PRIO,            
  55.                                                         (TaskHandle_t*  )&Task3Task_Handler);                                                
  56.         taskEXIT_CRITICAL();
  57.         vTaskDelete(StartTask_Handler);                                               
  58. }


  59. //task1
  60. void task1(void *pvParameters)
  61. {
  62.     while (1)
  63.     {
  64.         printf("task1 run ...\r\n");
  65.         vTaskDelay(100);
  66.     }
  67. }

  68. //task2
  69. void task2(void *pvParameters)
  70. {
  71.     while (1)
  72.     {       
  73.         printf("task2 run ...\r\n");
  74.         vTaskDelay(200);
  75.     }
  76. }

  77. //task3
  78. void task3(void *pvParameters)
  79. {
  80.     while (1)
  81.     {       
  82.         printf("task3 run ...\r\n");
  83.         vTaskDelay(300);
  84.     }
  85. }



3.3、main.c
  1. #include "main.h"

  2. int main(void)
  3. {
  4.   system_clock_config();
  5.         init_usart(115200);
  6.         delay_init();
  7.         init_led();
  8.         task_create();
  9.   while(1)
  10.   {
  11.   }
  12. }


四、程序运行

下载程序后,串口输出
003.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

701

帖子

7

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