查看: 1415|回复: 1

[分享] 【MCX先手尝鲜】移植FreeRTOS

[复制链接]
  • TA的每日心情
    开心
    5 小时前
  • 签到天数: 1481 天

    连续签到: 1 天

    [LV.10]以坛为家III

    152

    主题

    3146

    帖子

    31

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    8652
    最后登录
    2025-7-27
    发表于 2024-1-23 14:59:10 | 显示全部楼层 |阅读模式
    学习移植FreeRTOS系统到开发板。


    一、下载源码

    下载地址:https://www.freertos.org/zh-cn-cmn-s/a00104.html
    001.png


    二、添加文件到项目


    2.1、添加文件到项目
    002.png

    2.2、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 SystemCoreClock;
    43. #endif

    44. #ifndef configENABLE_FPU
    45.   #define configENABLE_FPU                        1
    46. #endif
    47. #ifndef configENABLE_MPU
    48.   #define configENABLE_MPU                        0
    49. #endif
    50. #ifndef configENABLE_TRUSTZONE
    51.   #define configENABLE_TRUSTZONE                  0
    52. #endif
    53. #ifndef configRUN_FREERTOS_SECURE_ONLY
    54.   #define configRUN_FREERTOS_SECURE_ONLY          1
    55. #endif

    56. #define configUSE_PREEMPTION                                        1
    57. #define configSUPPORT_STATIC_ALLOCATION                        0//1
    58. #define configSUPPORT_DYNAMIC_ALLOCATION                1
    59. #define configUSE_IDLE_HOOK                                                0
    60. #define configUSE_TICK_HOOK                                                0
    61. #define configCPU_CLOCK_HZ                                                ( SystemCoreClock )
    62. #define configTICK_RATE_HZ                                                ( ( TickType_t ) 1000 )
    63. #define configMAX_PRIORITIES                                        ( 56 )
    64. #define configMINIMAL_STACK_SIZE                                ( ( uint16_t ) 512 )
    65. #define configTOTAL_HEAP_SIZE                                        ( ( size_t ) 15 * 1024 )
    66. #define configMAX_TASK_NAME_LEN                                        ( 16 )
    67. #define configUSE_TRACE_FACILITY                                1
    68. #define configUSE_16_BIT_TICKS                                        0
    69. #define configUSE_MUTEXES                                                1
    70. #define configQUEUE_REGISTRY_SIZE                                8
    71. #define configUSE_RECURSIVE_MUTEXES                                1
    72. #define configUSE_COUNTING_SEMAPHORES                        1
    73. #define configUSE_PORT_OPTIMISED_TASK_SELECTION        0
    74. #define configUSE_MALLOC_FAILED_HOOK                        0//1
    75. #define configCHECK_FOR_STACK_OVERFLOW                        0//2

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

    80. /* Software timer definitions. */
    81. #define configUSE_TIMERS                                                1
    82. #define configTIMER_TASK_PRIORITY                                ( 2 )
    83. #define configTIMER_QUEUE_LENGTH                                10
    84. #define configTIMER_TASK_STACK_DEPTH                        256

    85. /* Set the following definitions to 1 to include the API function, or zero
    86. * to exclude the API function. */
    87. #define INCLUDE_vTaskPrioritySet                                1
    88. #define INCLUDE_uxTaskPriorityGet                                1
    89. #define INCLUDE_vTaskDelete                                                1
    90. #define INCLUDE_vTaskCleanUpResources                        0
    91. #define INCLUDE_vTaskSuspend                                        1
    92. #define INCLUDE_vTaskDelayUntil                                        1
    93. #define INCLUDE_vTaskDelay                                                1
    94. #define INCLUDE_xTaskGetSchedulerState                        1
    95. #define INCLUDE_xTimerPendFunctionCall                        1
    96. #define INCLUDE_xQueueGetMutexHolder                        1
    97. #define INCLUDE_uxTaskGetStackHighWaterMark                1
    98. #define INCLUDE_eTaskGetState                                        1

    99. /* Cortex-M specific definitions. */
    100. #ifdef __NVIC_PRIO_BITS
    101.         /* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
    102.         #define configPRIO_BITS                                                __NVIC_PRIO_BITS
    103. #else
    104.         #define configPRIO_BITS                                                4
    105. #endif

    106. /* The lowest interrupt priority that can be used in a call to a "set priority"
    107. * function. */
    108. #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY        15

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

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

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

    123. /* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
    124. * standard names. */
    125. #define vPortSVCHandler                                                                SVC_Handler
    126. #define xPortPendSVHandler                                                        PendSV_Handler
    127. #define xPortSysTickHandler                                                        SysTick_Handler

    128. /* Allow system call from within FreeRTOS kernel only. */
    129. #define configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY        1

    130. /* STM32H743 has 16 MPU regions and therefore it is necessary to configure
    131. * configTOTAL_MPU_REGIONS correctly. */
    132. #define configTOTAL_MPU_REGIONS                                                16

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

    137. #endif /* FREERTOS_CONFIG_H */
    复制代码


    2.3、task.c
    1. #include "main.h"

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

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


    12. void start_task(void *pvParameters);
    13. void gui_task(void *pvParameters);

    14. void task1(void *pvParameters);  
    15. void task2(void *pvParameters);


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

    26.         vTaskStartScheduler();
    27. }

    28. void start_task(void *pvParameters)
    29. {
    30.         taskENTER_CRITICAL();
    31.         //task1
    32.         xTaskCreate((TaskFunction_t )task1,                  
    33.                                                         (const char*    )"task1",               
    34.                                                         (uint16_t       )TASK1_STK_SIZE,         
    35.                                                         (void*          )NULL,                  
    36.                                                         (UBaseType_t    )TASK1_PRIO,            
    37.                                                         (TaskHandle_t*  )&Task1Task_Handler);   
    38.         //task2
    39.         xTaskCreate((TaskFunction_t )task2,                  
    40.                                                         (const char*    )"task2",               
    41.                                                         (uint16_t       )TASK2_STK_SIZE,        
    42.                                                         (void*          )NULL,                  
    43.                                                         (UBaseType_t    )TASK2_PRIO,            
    44.                                                         (TaskHandle_t*  )&Task2Task_Handler);
    45.         taskEXIT_CRITICAL();
    46.         vTaskDelete(StartTask_Handler);                                               
    47. }


    48. //task1
    49. void task1(void *pvParameters)
    50. {
    51.     while (1)
    52.     {
    53.         printf("task1 run ...\r\n");
    54.         vTaskDelay(200);
    55.     }
    56. }

    57. //task2
    58. void task2(void *pvParameters)
    59. {
    60.     while (1)
    61.     {       
    62.         printf("task2 run ...\r\n");
    63.         vTaskDelay(100);
    64.     }
    65. }
    复制代码


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

    2. int main(void)
    3. {

    4.         CLOCK_SetClkDiv(kCLOCK_DivFlexcom4Clk, 1u);
    5.         CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);

    6.         BOARD_InitPins();
    7.         BOARD_PowerMode_OD();
    8.         BOARD_InitBootClocks();
    9.         BOARD_InitDebugConsole();

    10.         init_led();

    11.         task_create();

    12.         while (1)
    13.         {

    14.         }
    15. }
    复制代码


    三、程序运行

    程序运行后,串口输出
    003.png


    哎...今天够累的,签到来了~
    回复

    使用道具 举报

  • TA的每日心情
    开心
    5 小时前
  • 签到天数: 528 天

    连续签到: 23 天

    [LV.9]以坛为家II

    21

    主题

    854

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    2317
    最后登录
    2025-7-27
    发表于 2024-1-23 19:45:34 | 显示全部楼层
    大佬,历害了!
    哎...今天够累的,签到来了~
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-7-27 12:24 , Processed in 0.083983 second(s), 21 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表