当前位置:   article > 正文

【STM32标准库】移植FreeRTOS_stm32移植freertos

stm32移植freertos

1、在工程中新建一个FreeRTOS文件夹,并把FreeRTOSv9.0.0\FreeRTOS\Source的文件全部复制过来。

 2、在portable文件夹中,只需要保留以下三个文件夹,其他的全部删除。

 3、在keil工程中新建FreeRTOS_CODE和FreeRTOS_PORTTABLE分组。

 在FreeRTOS_CODE分组中增加以下6个文件。

 在FreeRTOS_PORTTABLE分组中增加以下两个文件。

 

 添加完的效果如下。

 4、在keil工程中加入以下两个路径。

 5、从FreeRTOSv9.0.0\FreeRTOS\Demo\CORTEX_STM32F103_Keil中复制FreeRTOSConfig.h文件到WarGodCode(FreeRTOS)\FreeRTOS\include。

 6、将stm32f10x_it.c 文件中下面3个函数注释掉。

 7、将FreeRTOSConfig.h文件替换成以下代码。

  1. /*
  2. FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
  3. All rights reserved
  4. VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. This file is part of the FreeRTOS distribution.
  6. FreeRTOS is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License (version 2) as published by the
  8. Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
  9. ***************************************************************************
  10. >>! NOTE: The modification to the GPL is included to allow you to !<<
  11. >>! distribute a combined work that includes FreeRTOS without being !<<
  12. >>! obliged to provide the source code for proprietary components !<<
  13. >>! outside of the FreeRTOS kernel. !<<
  14. ***************************************************************************
  15. FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
  16. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. FOR A PARTICULAR PURPOSE. Full license text is available on the following
  18. link: http://www.freertos.org/a00114.html
  19. ***************************************************************************
  20. * *
  21. * FreeRTOS provides completely free yet professionally developed, *
  22. * robust, strictly quality controlled, supported, and cross *
  23. * platform software that is more than just the market leader, it *
  24. * is the industry's de facto standard. *
  25. * *
  26. * Help yourself get started quickly while simultaneously helping *
  27. * to support the FreeRTOS project by purchasing a FreeRTOS *
  28. * tutorial book, reference manual, or both: *
  29. * http://www.FreeRTOS.org/Documentation *
  30. * *
  31. ***************************************************************************
  32. http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
  33. the FAQ page "My application does not run, what could be wrong?". Have you
  34. defined configASSERT()?
  35. http://www.FreeRTOS.org/support - In return for receiving this top quality
  36. embedded software for free we request you assist our global community by
  37. participating in the support forum.
  38. http://www.FreeRTOS.org/training - Investing in training allows your team to
  39. be as productive as possible as early as possible. Now you can receive
  40. FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
  41. Ltd, and the world's leading authority on the world's leading RTOS.
  42. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  43. including FreeRTOS+Trace - an indispensable productivity tool, a DOS
  44. compatible FAT file system, and our tiny thread aware UDP/IP stack.
  45. http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
  46. Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
  47. http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
  48. Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
  49. licenses offer ticketed support, indemnification and commercial middleware.
  50. http://www.SafeRTOS.com - High Integrity Systems also provide a safety
  51. engineered and independently SIL3 certified version for use in safety and
  52. mission critical applications that require provable dependability.
  53. 1 tab == 4 spaces!
  54. */
  55. #ifndef FREERTOS_CONFIG_H
  56. #define FREERTOS_CONFIG_H
  57. /*-----------------------------------------------------------
  58. * Application specific definitions.
  59. *
  60. * These definitions should be adjusted for your particular hardware and
  61. * application requirements.
  62. *
  63. * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
  64. * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
  65. *
  66. * See http://www.freertos.org/a00110.html.
  67. *----------------------------------------------------------*/
  68. //针对不同的编译器调用不同的stdint.h文件
  69. #if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
  70. #include <stdint.h>
  71. extern uint32_t SystemCoreClock;
  72. #endif
  73. /***************************************************************************************************************/
  74. /* FreeRTOS基础配置配置选项 */
  75. /***************************************************************************************************************/
  76. #define configUSE_PREEMPTION 1 //1使用抢占式内核,0使用协程
  77. #define configUSE_TIME_SLICING 1 //1使能时间片调度(默认是使能的)
  78. //1启用特殊方法来选择下一个要运行的任务,一般是硬件计算前导零指令,如果所使用的MCU没有这些硬件指令的话此宏应该设置为0!
  79. #define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
  80. #define configUSE_TICKLESS_IDLE 0 //1启用低功耗tickless模式
  81. #define configUSE_QUEUE_SETS 1 //为1时启用队列
  82. #define configCPU_CLOCK_HZ (SystemCoreClock) //CPU频率
  83. #define configTICK_RATE_HZ (1000) //时钟节拍频率,这里设置为1000,周期就是1ms
  84. #define configMAX_PRIORITIES (5) //可使用的最大优先级
  85. #define configMINIMAL_STACK_SIZE ((unsigned short)64) //空闲任务使用的堆栈大小
  86. #define configMAX_TASK_NAME_LEN (16) //任务名字字符串长度
  87. #define configUSE_16_BIT_TICKS 0 //系统节拍计数器变量数据类型,1表示为16位无符号整形,0表示为32位无符号整形
  88. #define configIDLE_SHOULD_YIELD 1 //为1时空闲任务放弃CPU使用权给其他同优先级的用户任务
  89. #define configUSE_TASK_NOTIFICATIONS 1 //为1时开启任务通知功能,默认开启
  90. #define configUSE_MUTEXES 1 //为1时使用互斥信号量
  91. #define configQUEUE_REGISTRY_SIZE 8 //不为0时表示启用队列记录,具体的值是可以记录的队列和信号量最大数目。
  92. //大于0时启用堆栈溢出检测功能,如果使用此功能用户必须提供一个栈溢出钩子函数,如果使用的话
  93. //此值可以为1或者2,因为有两种栈溢出检测方法。
  94. #define configCHECK_FOR_STACK_OVERFLOW 0
  95. #define configUSE_RECURSIVE_MUTEXES 1 //为1时使用递归互斥信号量
  96. #define configUSE_MALLOC_FAILED_HOOK 0 //1使用内存申请失败钩子函数
  97. #define configUSE_APPLICATION_TASK_TAG 0
  98. #define configUSE_COUNTING_SEMAPHORES 1 //为1时使用计数信号量
  99. /***************************************************************************************************************/
  100. /* FreeRTOS与内存申请有关配置选项 */
  101. /***************************************************************************************************************/
  102. #define configSUPPORT_DYNAMIC_ALLOCATION 1 //支持动态内存申请
  103. #define configTOTAL_HEAP_SIZE ((size_t)(5 * 1024)) //系统所有总的堆大小
  104. /***************************************************************************************************************/
  105. /* FreeRTOS与钩子函数有关的配置选项 */
  106. /***************************************************************************************************************/
  107. #define configUSE_IDLE_HOOK 0 //1,使用空闲钩子;0,不使用
  108. #define configUSE_TICK_HOOK 0 //1,使用空闲钩子;0,不使用
  109. /***************************************************************************************************************/
  110. /* FreeRTOS与运行时间和任务状态收集有关的配置选项 */
  111. /***************************************************************************************************************/
  112. #define configGENERATE_RUN_TIME_STATS 0 //为1时启用运行时间统计功能
  113. #define configUSE_TRACE_FACILITY 1 //为1启用可视化跟踪调试
  114. #define configUSE_STATS_FORMATTING_FUNCTIONS 1 //与宏configUSE_TRACE_FACILITY同时为1时会编译下面3个函数
  115. //prvWriteNameToBuffer(),vTaskList(),vTaskGetRunTimeStats()
  116. /***************************************************************************************************************/
  117. /* FreeRTOS与协程有关的配置选项 */
  118. /***************************************************************************************************************/
  119. #define configUSE_CO_ROUTINES 0 //为1时启用协程,启用协程以后必须添加文件croutine.c
  120. #define configMAX_CO_ROUTINE_PRIORITIES (2) //协程的有效优先级数目
  121. /***************************************************************************************************************/
  122. /* FreeRTOS与软件定时器有关的配置选项 */
  123. /***************************************************************************************************************/
  124. #define configUSE_TIMERS 1 //为1时启用软件定时器
  125. #define configTIMER_TASK_PRIORITY (2) //软件定时器优先级
  126. #define configTIMER_QUEUE_LENGTH 10 //软件定时器队列长度
  127. #define configTIMER_TASK_STACK_DEPTH (128) //软件定时器任务堆栈大小
  128. /***************************************************************************************************************/
  129. /* FreeRTOS可选函数配置选项 */
  130. /***************************************************************************************************************/
  131. #define INCLUDE_xTaskGetSchedulerState 1
  132. #define INCLUDE_vTaskPrioritySet 1
  133. #define INCLUDE_uxTaskPriorityGet 1
  134. #define INCLUDE_vTaskDelete 1
  135. #define INCLUDE_vTaskCleanUpResources 1
  136. #define INCLUDE_vTaskSuspend 1
  137. #define INCLUDE_vTaskDelayUntil 1
  138. #define INCLUDE_vTaskDelay 1
  139. #define INCLUDE_eTaskGetState 1
  140. #define INCLUDE_xTimerPendFunctionCall 1
  141. /***************************************************************************************************************/
  142. /* FreeRTOS与中断有关的配置选项 */
  143. /***************************************************************************************************************/
  144. #ifdef __NVIC_PRIO_BITS
  145. #define configPRIO_BITS __NVIC_PRIO_BITS
  146. #else
  147. #define configPRIO_BITS 4
  148. #endif
  149. #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 15 //中断最低优先级
  150. #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5 //系统可管理的最高中断优先级
  151. #define configKERNEL_INTERRUPT_PRIORITY (configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))
  152. #define configMAX_SYSCALL_INTERRUPT_PRIORITY (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))
  153. /***************************************************************************************************************/
  154. /* FreeRTOS与中断服务函数有关的配置选项 */
  155. /***************************************************************************************************************/
  156. #define vPortSVCHandler SVC_Handler
  157. #define xPortPendSVHandler PendSV_Handler
  158. #define xPortSysTickHandler SysTick_Handler
  159. #endif /* FREERTOS_CONFIG_H */

8、在main.c中编写代码验证功能,这里创建了两个LED任务,优先级相同,每隔500ms轮流执行,烧录代码后可以看到两个LED都在闪烁。

  1. /**
  2. ******************************************************************************
  3. * @file Project/STM32F10x_StdPeriph_Template/main.c
  4. * @author MCD Application Team
  5. * @version V3.6.0
  6. * @date 20-September-2021
  7. * @brief Main program body
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * Copyright (c) 2011 STMicroelectronics.
  12. * All rights reserved.
  13. *
  14. * This software is licensed under terms that can be found in the LICENSE file
  15. * in the root directory of this software component.
  16. * If no LICENSE file comes with this software, it is provided AS-IS.
  17. *
  18. ******************************************************************************
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f10x.h"
  22. #include <stdio.h>
  23. #include "led.h"
  24. #include "FreeRTOS.h"
  25. #include "task.h"
  26. //任务优先级
  27. #define START_TASK_PRIO 1
  28. //任务堆栈大小
  29. #define START_STK_SIZE 128
  30. //任务句柄
  31. TaskHandle_t StartTask_Handler;
  32. //任务函数
  33. void Start_Task(void *pvParameters);
  34. //任务优先级
  35. #define LED0_TASK_PRIO 1
  36. //任务堆栈大小
  37. #define LED0_STK_SIZE 128
  38. //任务句柄
  39. TaskHandle_t Led0Task_Handler;
  40. //任务函数
  41. void Led0_Task(void *pvParameters);
  42. //任务优先级
  43. #define LED1_TASK_PRIO 1
  44. //任务堆栈大小
  45. #define LED1_STK_SIZE 128
  46. //任务句柄
  47. TaskHandle_t Led1Task_Handler;
  48. //任务函数
  49. void Led1_Task(void *pvParameters);
  50. /**
  51. * @brief Main program.
  52. * @param None
  53. * @retval None
  54. */
  55. int main(void)
  56. {
  57. /* Add your application code here
  58. */
  59. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
  60. LED_Init();
  61. //创建开始任务
  62. xTaskCreate((TaskFunction_t )Start_Task, //任务函数
  63. (const char* )"Start_Task", //任务名称
  64. (uint16_t )START_STK_SIZE, //任务堆栈大小
  65. (void* )NULL, //传递给任务函数的参数
  66. (UBaseType_t )START_TASK_PRIO, //任务优先级
  67. (TaskHandle_t* )&StartTask_Handler); //任务句柄
  68. vTaskStartScheduler(); //开启任务调度
  69. }
  70. //开始任务任务函数
  71. void Start_Task(void *pvParameters)
  72. {
  73. taskENTER_CRITICAL();//进入临界区
  74. //创建LED0任务
  75. xTaskCreate((TaskFunction_t )Led0_Task,
  76. (const char* )"LED0_Task",
  77. (uint16_t )LED0_STK_SIZE,
  78. (void* )NULL,
  79. (UBaseType_t )LED0_TASK_PRIO,
  80. (TaskHandle_t* )&Led0Task_Handler);
  81. //创建LED1任务
  82. xTaskCreate((TaskFunction_t )Led1_Task,
  83. (const char* )"LED1_Task",
  84. (uint16_t )LED1_STK_SIZE,
  85. (void* )NULL,
  86. (UBaseType_t )LED1_TASK_PRIO,
  87. (TaskHandle_t* )&Led1Task_Handler);
  88. vTaskDelete(StartTask_Handler); //删除开始任务
  89. taskEXIT_CRITICAL(); //退出临界区
  90. }
  91. //LED0任务函数
  92. void Led0_Task(void *pvParameters)
  93. {
  94. while(1)
  95. {
  96. Led0_On();
  97. vTaskDelay(500);
  98. Led0_Off();
  99. vTaskDelay(500);
  100. }
  101. }
  102. //LED1任务函数
  103. void Led1_Task(void *pvParameters)
  104. {
  105. while(1)
  106. {
  107. Led1_On();
  108. vTaskDelay(500);
  109. Led1_Off();
  110. vTaskDelay(500);
  111. }
  112. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/730394
推荐阅读
相关标签
  

闽ICP备14008679号