[STM32H7] 【STM32H745I-DISCO试用】移植FreeRTOS系统

[复制链接]
1513|1
 楼主| tlled 发表于 2025-3-4 23:47 | 显示全部楼层 |阅读模式
本帖最后由 tlled 于 2025-3-4 23:46 编辑

学习在STM32H745I-DISCO开发板上移植freertos系统。


一、下载FreeRTOS源码


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




二、添加源码到工程


2.1、复制源码到工程文件夹下
002.png

2.2、添加文件到项目工程

003.png

2.3、修改文件
修改stm32h7xx_it.c文件,屏蔽掉下面函数

void SVC_Handler(void)
004.png

void PendSV_Handler(void)
005.png

void SysTick_Handler(void)
006.png

2.4、修改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. #if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
  29.         #include <stdint.h>
  30.         extern uint32_t SystemCoreClock;
  31. #endif

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

  43. #define configUSE_PREEMPTION                                        1
  44. #define configUSE_PORT_OPTIMISED_TASK_SELECTION        0
  45. //#define configUSE_QUEUE_SETS                                        1
  46. #define configUSE_IDLE_HOOK                                                0
  47. #define configUSE_TICK_HOOK                                                0//1
  48. #define configCPU_CLOCK_HZ                                                ( SystemCoreClock )
  49. #define configTICK_RATE_HZ                                                ( ( TickType_t )1000 )
  50. #define configMAX_PRIORITIES                                        ( 5 )
  51. #define configMINIMAL_STACK_SIZE                                ( ( unsigned short ) 130 )
  52. #define configTOTAL_HEAP_SIZE                                        ( ( size_t ) ( 20 * 1024 ) )
  53. #define configMAX_TASK_NAME_LEN                                        ( 10 )
  54. #define configUSE_TRACE_FACILITY                                1
  55. #define configUSE_16_BIT_TICKS                                        0
  56. #define configIDLE_SHOULD_YIELD                                        1
  57. #define configUSE_MUTEXES                                                1
  58. #define configQUEUE_REGISTRY_SIZE                                8
  59. #define configCHECK_FOR_STACK_OVERFLOW                        0//2
  60. #define configUSE_RECURSIVE_MUTEXES                                1
  61. #define configUSE_MALLOC_FAILED_HOOK                        0//1
  62. #define configUSE_APPLICATION_TASK_TAG                        0
  63. #define configUSE_COUNTING_SEMAPHORES                        1

  64. /* The full demo always has tasks to run so the tick will never be turned off.
  65. The blinky demo will use the default tickless idle implementation to turn the
  66. tick off. */
  67. #define configUSE_TICKLESS_IDLE                                        0

  68. /* Run time stats gathering definitions. */
  69. #define configGENERATE_RUN_TIME_STATS        0

  70. /* This demo makes use of one or more example stats formatting functions.  These
  71. format the raw data provided by the uxTaskGetSystemState() function in to human
  72. readable ASCII form.  See the notes in the implementation of vTaskList() within
  73. FreeRTOS/Source/tasks.c for limitations. */
  74. #define configUSE_STATS_FORMATTING_FUNCTIONS        1


  75. /* Software timer definitions. */
  76. #define configUSE_TIMERS                                1
  77. #define configTIMER_TASK_PRIORITY                2//( configMAX_PRIORITIES - 1 )
  78. #define configTIMER_QUEUE_LENGTH                10//5
  79. #define configTIMER_TASK_STACK_DEPTH        ( configMINIMAL_STACK_SIZE * 2 )

  80. /* Set the following definitions to 1 to include the API function, or zero
  81. to exclude the API function. */
  82. #define INCLUDE_vTaskPrioritySet                1
  83. #define INCLUDE_uxTaskPriorityGet                1
  84. #define INCLUDE_vTaskDelete                                1
  85. #define INCLUDE_vTaskCleanUpResources        1
  86. #define INCLUDE_vTaskSuspend                        1
  87. #define INCLUDE_vTaskDelayUntil                        1
  88. #define INCLUDE_vTaskDelay                                1
  89. #define INCLUDE_eTaskGetState                        1
  90. #define INCLUDE_xTimerPendFunctionCall        1

  91. /* Cortex-M specific definitions. */
  92. #ifdef __NVIC_PRIO_BITS
  93.         /* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
  94.         #define configPRIO_BITS                       __NVIC_PRIO_BITS
  95. #else
  96.         #define configPRIO_BITS                       4        /* 15 priority levels */
  97. #endif

  98. /* The lowest interrupt priority that can be used in a call to a "set priority"
  99. function. */
  100. #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY                        15

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

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

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

  117. /* Prevent the inclusion of items the assembler will not understand in assembly
  118. files. */
  119. //#ifndef __IAR_SYSTEMS_ASM__

  120. //        /* Library includes. */
  121. //        #include "stm32f7xx_hal.h"

  122. //        extern uint32_t SystemCoreClock;

  123. //        /* Normal assert() semantics without relying on the provision of an assert.h
  124. //        header file. */
  125. //        extern void vAssertCalled( uint32_t ulLine, const char *pcFile );
  126. //        #define configASSERT( x ) if( ( x ) == 0 ) vAssertCalled( __LINE__, __FILE__ )

  127. //#endif /* __IAR_SYSTEMS_ASM__ */

  128. #endif /* FREERTOS_CONFIG_H */



三、程序部分


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

  2. #define START_TASK_PRO                tskIDLE_PRIORITY + 1                               
  3. #define START_STK_SIZE                256
  4. TaskHandle_t StartTask_Handler;

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

  12. #define TASK3_PRIO              tskIDLE_PRIORITY + 4                  
  13. #define TASK3_STK_SIZE          256               
  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-lvlg
  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*  )&Task2Task_Handler);
  56.         taskEXIT_CRITICAL();
  57.                                                        
  58.                                                        
  59.         vTaskDelete(StartTask_Handler);                                               
  60. }

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

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

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


3.2、main.c
  1. #include "main.h"
  2. #include "usart/usart.h"
  3. #include "led/led.h"

  4. #ifndef HSEM_ID_0
  5. #define HSEM_ID_0 (0U) /* HW semaphore 0*/
  6. #endif

  7. void SystemClock_Config(void);

  8. static void CPU_CACHE_Enable(void);
  9. static void MPU_Config(void);


  10. int main(void)
  11. {
  12.         MPU_Config();
  13.         CPU_CACHE_Enable();
  14.   HAL_Init();
  15.         SystemClock_Config();
  16.   __HAL_RCC_HSEM_CLK_ENABLE();
  17.   
  18.         HAL_HSEM_FastTake(HSEM_ID_0);   
  19.         HAL_HSEM_Release(HSEM_ID_0,0);

  20.         init_led();
  21.         init_uart3(115200);
  22.         
  23.         task_create();

  24.   while (1)
  25.   {
  26.   }
  27. }


四、程序运行

下载程序后,串口输出
100.png

yangjiaxu 发表于 2025-4-9 14:21 | 显示全部楼层
这个确实可以学习学习,主要是这种比较细致的教程确实很有意义
您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

701

帖子

7

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