[开发板] 基于CW32L031 开发板的FreeRTOS另类移植

[复制链接]
1134|4
 楼主| zeshoufx 发表于 2023-6-7 21:36 | 显示全部楼层 |阅读模式
本帖最后由 zeshoufx 于 2023-6-7 21:36 编辑

一、添加相关文件

移植配置选项

移植配置选项


二、配置FreeRTOS
主要修改内容为:
(1)#define configTOTAL_HEAP_SIZE ((size_t)4096)//4096
(2)#define configMINIMAL_STACK_SIZE ((uint16_t)128)(3)#define configMAX_PRIORITIES 8
(4)#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 0x3
  1. #ifndef FREERTOS_CONFIG_H
  2. #define FREERTOS_CONFIG_H

  3. #include "bitband.h"

  4. /*-----------------------------------------------------------
  5. * Application specific definitions.
  6. *
  7. * These definitions should be adjusted for your particular hardware and
  8. * application requirements.
  9. *
  10. * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
  11. * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
  12. *
  13. * See http://www.freertos.org/a00110.html
  14. *----------------------------------------------------------*/

  15. #if (defined(__ARMCC_VERSION) || defined(__GNUC__) || defined(__ICCARM__))
  16. #include <stdint.h>

  17. extern uint32_t SystemCoreClock;
  18. #endif

  19. /* Constants that describe the hardware and memory usage. */
  20. #define configCPU_CLOCK_HZ                    (SystemCoreClock)
  21. #define configTICK_RATE_HZ                    ((TickType_t)1000)
  22. #define configTOTAL_HEAP_SIZE                 ((size_t)4096)//4096
  23. #define configMINIMAL_STACK_SIZE              ((uint16_t)128)
  24. #define configSUPPORT_DYNAMIC_ALLOCATION      1
  25. #define configSUPPORT_STATIC_ALLOCATION       0

  26. /* Constants related to the behaviour or the scheduler. */
  27. #define configMAX_PRIORITIES                  8
  28. #define configUSE_PREEMPTION                  1
  29. #define configUSE_TIME_SLICING                1
  30. #define configIDLE_SHOULD_YIELD               1
  31. #define configMAX_TASK_NAME_LEN               (16)
  32. #define configUSE_16_BIT_TICKS                0

  33. /* Software timer definitions. */
  34. #define configUSE_TIMERS                      1
  35. #define configTIMER_TASK_PRIORITY             2
  36. #define configTIMER_QUEUE_LENGTH              5
  37. #define configTIMER_TASK_STACK_DEPTH          (configMINIMAL_STACK_SIZE * 2)

  38. /* Constants that build features in or out. */
  39. #define configUSE_MUTEXES                     1
  40. #define configUSE_RECURSIVE_MUTEXES           1
  41. #define configUSE_COUNTING_SEMAPHORES         1
  42. #define configUSE_QUEUE_SETS                  1
  43. #define configUSE_TASK_NOTIFICATIONS          1
  44. #define configUSE_TRACE_FACILITY              1
  45. #define configUSE_TICKLESS_IDLE               1
  46. #define configUSE_APPLICATION_TASK_TAG        0
  47. #define configUSE_NEWLIB_REENTRANT            0
  48. #define configUSE_CO_ROUTINES                 0

  49. /* Constants provided for debugging and optimisation assistance. */
  50. #define configCHECK_FOR_STACK_OVERFLOW        0
  51. #define configQUEUE_REGISTRY_SIZE             0
  52. #define configASSERT( x )                     if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }

  53. /* Constants that define which hook (callback) functions should be used. */
  54. #define configUSE_IDLE_HOOK                   0
  55. #define configUSE_TICK_HOOK                   0
  56. #define configUSE_DAEMON_TASK_STARTUP_HOOK    0
  57. #define configUSE_MALLOC_FAILED_HOOK          0

  58. /* Port specific configuration. */
  59. #define configENABLE_MPU                      0
  60. #define configENABLE_FPU                      0
  61. #define configENABLE_TRUSTZONE                0
  62. #define configMINIMAL_SECURE_STACK_SIZE       ((uint32_t)1024)
  63. #define configRUN_FREERTOS_SECURE_ONLY        0

  64. /* Cortex-M specific definitions. */
  65. #ifdef __NVIC_PRIO_BITS
  66.   /* __NVIC_PRIO_BITS will be specified when CMSIS is being used. */
  67.   #define configPRIO_BITS                     __NVIC_PRIO_BITS
  68. #else
  69.   /* 7 priority levels */
  70.   #define configPRIO_BITS                     4//3
  71. #endif

  72. /* The lowest interrupt priority that can be used in a call to a "set priority" function. */
  73. #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY       0x3

  74. /* The highest interrupt priority that can be used by any interrupt service
  75. * routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT
  76. * CALL INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A
  77. * HIGHER PRIORITY THAN THIS! (higher priorities are lower numeric values). */
  78. #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY  3

  79. /* Interrupt priorities used by the kernel port layer itself.  These are generic
  80. * to all Cortex-M ports, and do not rely on any particular library functions. */
  81. #define configKERNEL_INTERRUPT_PRIORITY               (configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))

  82. /* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
  83. * See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
  84. #define configMAX_SYSCALL_INTERRUPT_PRIORITY          (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))

  85. /* Set the following definitions to 1 to include the API function, or zero
  86. * to exclude the API function.  NOTE:  Setting an INCLUDE_ parameter to 0 is
  87. * only necessary if the linker does not automatically remove functions that are
  88. * not referenced anyway. */
  89. #define INCLUDE_vTaskPrioritySet              1
  90. #define INCLUDE_uxTaskPriorityGet             1
  91. #define INCLUDE_vTaskDelete                   1
  92. #define INCLUDE_vTaskSuspend                  1
  93. #define INCLUDE_vTaskDelayUntil               1
  94. #define INCLUDE_vTaskDelay                    1
  95. #define INCLUDE_xTaskGetIdleTaskHandle        1
  96. #define INCLUDE_xTaskAbortDelay               1
  97. #define INCLUDE_xQueueGetMutexHolder          1
  98. #define INCLUDE_xSemaphoreGetMutexHolder      1
  99. #define INCLUDE_xTaskGetHandle                1
  100. #define INCLUDE_uxTaskGetStackHighWaterMark   1
  101. #define INCLUDE_uxTaskGetStackHighWaterMark2  1
  102. #define INCLUDE_eTaskGetState                 1
  103. #define INCLUDE_xTaskResumeFromISR            1
  104. #define INCLUDE_xTimerPendFunctionCall        1
  105. #define INCLUDE_xTaskGetSchedulerState        1
  106. #define INCLUDE_xTaskGetCurrentTaskHandle     1

  107. /* Map the FreeRTOS port interrupt handlers to their CMSIS standard names. */
  108. #define xPortPendSVHandler                    PendSV_Handler
  109. #define vPortSVCHandler                       SVC_Handler
  110. #define xPortSysTickHandler                   SysTick_Handler

  111. #if (defined(__ARMCC_VERSION) || defined(__GNUC__) || defined(__ICCARM__))
  112. /* Include debug event definitions */
  113. #include "freertos_evr.h"
  114. #endif

  115. #endif /* FREERTOS_CONFIG_H */
三、LED初始化
  1. #include "led.h"


  2. void led_init(void)
  3. {
  4.         GPIO_InitTypeDef gpio_structure;
  5.         
  6.         RCC_AHBPeriphClk_Enable(RCC_AHB_PERIPH_GPIOC,ENABLE);
  7.         RCC_AHBPeriphClk_Enable(RCC_AHB_PERIPH_GPIOA,ENABLE);
  8.         
  9.         gpio_structure.IT=GPIO_IT_NONE;
  10.         gpio_structure.Mode=GPIO_MODE_OUTPUT_PP;
  11.         gpio_structure.Pins=GPIO_PIN_13;
  12.         
  13.         GPIO_Init(CW_GPIOC,&gpio_structure);
  14.         
  15.         gpio_structure.IT=GPIO_IT_NONE;
  16.         gpio_structure.Mode=GPIO_MODE_OUTPUT_PP;
  17.         gpio_structure.Pins=GPIO_PIN_7|GPIO_PIN_8;
  18.         
  19.         GPIO_Init(CW_GPIOA,&gpio_structure);
  20.         
  21.         GPIO_WritePin(CW_GPIOC,GPIO_PIN_13,GPIO_Pin_SET);
  22.         GPIO_WritePin(CW_GPIOA,GPIO_PIN_7,GPIO_Pin_SET);
  23.         GPIO_WritePin(CW_GPIOA,GPIO_PIN_8,GPIO_Pin_SET);

  24. }

  25. void led_on(led_type led)
  26. {
  27.         switch(led)
  28.         {
  29.                 case led_1:GPIO_WritePin(CW_GPIOC,GPIO_PIN_13,GPIO_Pin_RESET);
  30.                         break;
  31.                 case led_2:GPIO_WritePin(CW_GPIOA,GPIO_PIN_7,GPIO_Pin_RESET);
  32.                         break;
  33.                 case led_3:GPIO_WritePin(CW_GPIOA,GPIO_PIN_8,GPIO_Pin_RESET);
  34.                         break;
  35.                 case led_all:
  36.                         GPIO_WritePin(CW_GPIOC,GPIO_PIN_13,GPIO_Pin_RESET);
  37.                         GPIO_WritePin(CW_GPIOA,GPIO_PIN_7,GPIO_Pin_RESET);
  38.                         GPIO_WritePin(CW_GPIOA,GPIO_PIN_8,GPIO_Pin_RESET);
  39.                         break;
  40.                 default:
  41.                         break;
  42.         }
  43. }


  44. void led_off(led_type led)
  45. {
  46.         switch(led)
  47.         {
  48.                 case led_1:GPIO_WritePin(CW_GPIOC,GPIO_PIN_13,GPIO_Pin_SET);
  49.                         break;
  50.                 case led_2:GPIO_WritePin(CW_GPIOA,GPIO_PIN_7,GPIO_Pin_SET);
  51.                         break;
  52.                 case led_3:GPIO_WritePin(CW_GPIOA,GPIO_PIN_8,GPIO_Pin_SET);
  53.                         break;
  54.                 case led_all:
  55.                         GPIO_WritePin(CW_GPIOC,GPIO_PIN_13,GPIO_Pin_SET);
  56.                         GPIO_WritePin(CW_GPIOA,GPIO_PIN_7,GPIO_Pin_SET);
  57.                         GPIO_WritePin(CW_GPIOA,GPIO_PIN_8,GPIO_Pin_SET);
  58.                         break;
  59.                 default:
  60.                         break;
  61.         }
  62. }

  63. void led_toggle(led_type led)
  64. {
  65.         switch(led)
  66.         {
  67.                 case led_1:GPIO_TogglePin(CW_GPIOC,GPIO_PIN_13);
  68.                         break;
  69.                 case led_2:GPIO_TogglePin(CW_GPIOA,GPIO_PIN_7);
  70.                         break;
  71.                 case led_3:GPIO_TogglePin(CW_GPIOA,GPIO_PIN_8);
  72.                         break;
  73.                 case led_all:
  74.                         GPIO_TogglePin(CW_GPIOC,GPIO_PIN_13);
  75.                         GPIO_TogglePin(CW_GPIOA,GPIO_PIN_7);
  76.                         GPIO_TogglePin(CW_GPIOA,GPIO_PIN_8);
  77.                         break;
  78.                 default:
  79.                         break;
  80.         }
  81. }
  1. #ifndef _led_H
  2. #define _led_H

  3. #include "cw32l031.h"
  4. #include "cw32l031_gpio.h"
  5. #include "cw32l031_rcc.h"

  6. typedef enum
  7. {
  8.         led_1=1,
  9.         led_2,
  10.         led_3,
  11.         led_all
  12. }led_type;


  13. void led_init(void);
  14. void led_on(led_type led);
  15. void led_off(led_type led);
  16. void led_toggle(led_type led);


  17. #endif

四、主函数
  1. #include "main.h"

  2. /******************************************************************************/

  3. static TaskHandle_t led1_task;
  4. static TaskHandle_t led2_task;
  5. static TaskHandle_t led3_task;
  6. static TaskHandle_t app_task;


  7. static void led1_entry(void *arg);
  8. static void led2_entry(void *arg);
  9. static void led3_entry(void *arg);
  10. static void app_entry(void* arg);
  11. /**
  12. ******************************************************************************
  13. ** \brief  Main function of project
  14. **
  15. ** \return uint32_t return value, if needed
  16. **
  17. ** This sample toggle GPIOA.00
  18. **
  19. ******************************************************************************/
  20. int32_t main(void)
  21. {
  22.         BaseType_t xreturn=pdPASS;
  23.         
  24.         led_init();
  25. //        systick_set(8);
  26.         SysTick_Config(SystemCoreClock);
  27.         beep_init();

  28.     xreturn=xTaskCreate(app_entry,"app",64,NULL,1,&app_task);
  29.         if(pdPASS==xreturn)
  30.         {
  31.                 vTaskStartScheduler();
  32.         }
  33.         else
  34.         {
  35.                 return -1;
  36.         }

  37. }


  38. static void app_entry(void* arg)
  39. {
  40. //        BaseType_t xreturn=pdPASS;
  41.         taskENTER_CRITICAL();
  42.         
  43.         xTaskCreate(led1_entry,"led1",64,NULL,2,&led1_task);
  44.         
  45.         
  46.         xTaskCreate(led2_entry,"led2",64,NULL,3,&led2_task);
  47.         
  48.         
  49.         xTaskCreate(led3_entry,"led3",64,NULL,4,&led3_task);
  50.         
  51.         
  52.         vTaskDelete(app_task);
  53.         taskEXIT_CRITICAL();
  54. }


  55. static void led1_entry(void *arg)
  56. {
  57.         while(1)
  58.         {
  59.                 led_toggle(led_1);
  60.                 vTaskDelay(350);
  61.         }
  62. }


  63. static void led2_entry(void *arg)
  64. {
  65.         while(1)
  66.         {
  67.                 led_toggle(led_2);
  68.                 vTaskDelay(450);
  69.         }
  70. }


  71. static void led3_entry(void *arg)
  72. {
  73.         while(1)
  74.         {
  75.                 led_toggle(led_3);
  76.                 vTaskDelay(550);
  77.         }
  78.         
  79. }


  80. /******************************************************************************
  81. * EOF (not truncated)
  82. ******************************************************************************/
  83. #ifdef  USE_FULL_ASSERT
  84. /**
  85.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Reports the name of the source file and the source line number
  86.   *         where the assert_param error has occurred.
  87.   * @param  file: pointer to the source file name
  88.   * @param  line: assert_param error line source number
  89.   * @retval None
  90.   */
  91. void assert_failed(uint8_t *file, uint32_t line)
  92. {
  93.     /* USER CODE BEGIN 6 */
  94.     /* User can add his own implementation to report the file name and line number,
  95.        tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  96.     /* USER CODE END 6 */
  97. }
  98. #endif /* USE_FULL_ASSERT */

五、结果

移植结果展示

移植结果展示



LOVEEVER 发表于 2023-10-19 17:04 | 显示全部楼层
RTOS了解学习一下,很实用
小小蚂蚁举千斤 发表于 2023-10-23 14:48 | 显示全部楼层
楼主这个另类移植很好,很值得深入学习
星辰大海不退缩 发表于 2023-10-25 13:28 | 显示全部楼层
这个RTOS确实非常实用
中国龙芯CDX 发表于 2023-11-14 09:54 | 显示全部楼层
常用的 RTOS 有国外的 FreeRTOS、μC/OS、 RTX 和国内的 FreeRTOS、 Huawei LiteOS 和 AliOS-Things
您需要登录后才可以回帖 登录 | 注册

本版积分规则

67

主题

1991

帖子

15

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