你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【STM32N6570-DK评测】移植FreeRTOS系统

[复制链接]
TLLED 发布时间:2025-2-8 10:10
一、下载FreeRTOS源码

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

002.png



二、添加文件

2.1、复制文件到项目下的middleware
004.png

2.2、在工程中添加FreeRTOS源码
003.png


三、程序代码


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 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 configENABLE_MVE                                                   0

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

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

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

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

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

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

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

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

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

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

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

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

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

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


3.2、屏蔽函数


在stm32n6xx_it.c
005.png

006.png



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

  2. void SystemClock_Config(void);
  3. void PeriphCommonClock_Config(void);

  4. int main(void)
  5. {
  6.   HAL_Init();
  7.   SystemClock_Config();
  8.   PeriphCommonClock_Config();
  9.         usart_init(115200);
  10.         init_led();
  11.        
  12.         task_create();

  13.   while (1)
  14.   {

  15.   }
  16. }





  17. /**
  18.   * @brief System Clock Configuration
  19.   * @retval None
  20.   */
  21. void SystemClock_Config(void)
  22. {
  23.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  24.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  25.   /** Configure the System Power Supply
  26.   */
  27.   if (HAL_PWREx_ConfigSupply(PWR_EXTERNAL_SOURCE_SUPPLY) != HAL_OK)
  28.   {
  29.     Error_Handler();
  30.   }

  31.   /* Enable HSI */
  32.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  33.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  34.   RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
  35.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  36.   RCC_OscInitStruct.PLL1.PLLState = RCC_PLL_NONE;
  37.   RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_NONE;
  38.   RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;
  39.   RCC_OscInitStruct.PLL4.PLLState = RCC_PLL_NONE;
  40.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  41.   {
  42.     Error_Handler();
  43.   }

  44.   /** Get current CPU/System buses clocks configuration and if necessary switch
  45. to intermediate HSI clock to ensure target clock can be set
  46.   */
  47.   HAL_RCC_GetClockConfig(&RCC_ClkInitStruct);
  48.   if ((RCC_ClkInitStruct.CPUCLKSource == RCC_CPUCLKSOURCE_IC1) ||
  49.      (RCC_ClkInitStruct.SYSCLKSource == RCC_SYSCLKSOURCE_IC2_IC6_IC11))
  50.   {
  51.     RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_CPUCLK | RCC_CLOCKTYPE_SYSCLK);
  52.     RCC_ClkInitStruct.CPUCLKSource = RCC_CPUCLKSOURCE_HSI;
  53.     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  54.     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct) != HAL_OK)
  55.     {
  56.       /* Initialization Error */
  57.       Error_Handler();
  58.     }
  59.   }

  60.   /** Initializes the RCC Oscillators according to the specified parameters
  61.   * in the RCC_OscInitTypeDef structure.
  62.   */
  63.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  64.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  65.   RCC_OscInitStruct.PLL1.PLLState = RCC_PLL_ON;
  66.   RCC_OscInitStruct.PLL1.PLLSource = RCC_PLLSOURCE_HSE;
  67.   RCC_OscInitStruct.PLL1.PLLM = 3;
  68.   RCC_OscInitStruct.PLL1.PLLN = 100;
  69.   RCC_OscInitStruct.PLL1.PLLFractional = 0;
  70.   RCC_OscInitStruct.PLL1.PLLP1 = 2;
  71.   RCC_OscInitStruct.PLL1.PLLP2 = 1;
  72.   RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_NONE;
  73.   RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;
  74.   RCC_OscInitStruct.PLL4.PLLState = RCC_PLL_NONE;
  75.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  76.   {
  77.     Error_Handler();
  78.   }

  79.   /** Initializes the CPU, AHB and APB buses clocks
  80.   */
  81.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_CPUCLK|RCC_CLOCKTYPE_HCLK
  82.                               |RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
  83.                               |RCC_CLOCKTYPE_PCLK2|RCC_CLOCKTYPE_PCLK5
  84.                               |RCC_CLOCKTYPE_PCLK4;
  85.   RCC_ClkInitStruct.CPUCLKSource = RCC_CPUCLKSOURCE_IC1;
  86.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_IC2_IC6_IC11;
  87.   RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  88.   RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
  89.   RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
  90.   RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;
  91.   RCC_ClkInitStruct.APB5CLKDivider = RCC_APB5_DIV1;
  92.   RCC_ClkInitStruct.IC1Selection.ClockSelection = RCC_ICCLKSOURCE_PLL1;
  93.   RCC_ClkInitStruct.IC1Selection.ClockDivider = 1;
  94.   RCC_ClkInitStruct.IC2Selection.ClockSelection = RCC_ICCLKSOURCE_PLL1;
  95.   RCC_ClkInitStruct.IC2Selection.ClockDivider = 2;
  96.   RCC_ClkInitStruct.IC6Selection.ClockSelection = RCC_ICCLKSOURCE_PLL1;
  97.   RCC_ClkInitStruct.IC6Selection.ClockDivider = 4;
  98.   RCC_ClkInitStruct.IC11Selection.ClockSelection = RCC_ICCLKSOURCE_PLL1;
  99.   RCC_ClkInitStruct.IC11Selection.ClockDivider = 8;

  100.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct) != HAL_OK)
  101.   {
  102.     Error_Handler();
  103.   }
  104. }

  105. /**
  106.   * @brief Peripherals Common Clock Configuration
  107.   * @retval None
  108.   */
  109. void PeriphCommonClock_Config(void)
  110. {
  111.   RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

  112.   /** Initializes the peripherals clock
  113.   */
  114.   PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_TIM;
  115.   PeriphClkInitStruct.TIMPresSelection = RCC_TIMPRES_DIV1;
  116.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  117.   {
  118.     Error_Handler();
  119.   }
  120. }

  121. /**
  122.   * @brief ICACHE Initialization Function
  123.   * @param None
  124.   * @retval None
  125.   */
  126. static void MX_ICACHE_Init(void)
  127. {

  128.   /* USER CODE BEGIN ICACHE_Init 0 */

  129.   /* USER CODE END ICACHE_Init 0 */

  130.   /* USER CODE BEGIN ICACHE_Init 1 */

  131.   /* USER CODE END ICACHE_Init 1 */

  132.   /** Enable instruction cache in 1-way (direct mapped cache)
  133.   */
  134.   if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK)
  135.   {
  136.     Error_Handler();
  137.   }
  138.   if (HAL_ICACHE_Enable() != HAL_OK)
  139.   {
  140.     Error_Handler();
  141.   }
  142.   /* USER CODE BEGIN ICACHE_Init 2 */

  143.   /* USER CODE END ICACHE_Init 2 */

  144. }
  145. /* USER CODE BEGIN 4 */

  146. /* USER CODE END 4 */

  147. /**
  148.   * @brief  This function is executed in case of error occurrence.
  149.   * @retval None
  150.   */
  151. void Error_Handler(void)
  152. {
  153.   /* USER CODE BEGIN Error_Handler_Debug */
  154.   /* User can add his own implementation to report the HAL error return state */
  155.   __disable_irq();
  156.   while (1)
  157.   {
  158.   }
  159.   /* USER CODE END Error_Handler_Debug */
  160. }

  161. #ifdef  USE_FULL_ASSERT
  162. /**
  163.   * @brief  Reports the name of the source file and the source line number
  164.   *         where the assert_param error has occurred.
  165.   * @param  file: pointer to the source file name
  166.   * @param  line: assert_param error line source number
  167.   * @retval None
  168.   */
  169. void assert_failed(uint8_t *file, uint32_t line)
  170. {
  171.   /* USER CODE BEGIN 6 */
  172.   /* User can add his own implementation to report the file name and line number,
  173.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  174.   /* USER CODE END 6 */
  175. }
  176. #endif /* USE_FULL_ASSERT */
复制代码


3.4、fun_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              2                  
  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. #define TASK3_PRIO              4                  
  13. #define TASK3_STK_SIZE          128                 
  14. static TaskHandle_t                    Task3Task_Handler = NULL;  


  15. void start_task(void *pvParameters);
  16. void gui_task(void *pvParameters);

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



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

  30.         vTaskStartScheduler();
  31. }

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

  62. //task1
  63. void task1(void *pvParameters)
  64. {
  65.                 __IO uint32_t index = 0;
  66.                 __IO uint32_t flash_id_index = 0;
  67.        
  68.     while (1)
  69.     {               
  70.                                 led1_tog();
  71.         printf("task1 run ...\r\n");
  72.         vTaskDelay(200);
  73.     }
  74. }

  75. //task2
  76. void task2(void *pvParameters)
  77. {
  78.     while (1)
  79.     {       
  80.                                 led2_tog();
  81.         printf("task2 run ...\r\n");
  82.         vTaskDelay(100);
  83.     }
  84. }

  85. //task3
  86. void task3(void *pvParameters)
  87. {
  88.     while (1)
  89.     {       
  90.         printf("task3 run ...\r\n");
  91.         vTaskDelay(100);
  92.     }
  93. }
复制代码


四、运行结果

4.1、仿真运行后,串口输出


001.png



4.2、LED灯运行

rtos-led.gif










收藏 评论1 发布时间:2025-2-8 10:10

举报

1个回答
STMCU-管管 回答时间:2025-4-11 10:08:19

感谢分享

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版