[MM32软件] 【灵动微电子MM32F5330测评】FreeRTOS移植和任务创建

[复制链接]
 楼主| xiaoqi976633690 发表于 2024-6-24 11:42 | 显示全部楼层 |阅读模式
<
本帖最后由 xiaoqi976633690 于 2024-6-24 11:47 编辑

#技术资源# #申请原创#
一、实验目的
在MM32F5330MCU上移植FreeRTOS,并运行2个task,打印信息。
二、实验代码
1.FreeRTOS移植
    a.下载源码 freertos
    https://www.freertos.org/zh-cn-cmn-s/    00.png

    b.整理文件
    核心文件在目录   源码\FreeRTOS\Source  下的.c和.h文件
10.png   
    .h文件在目录 源码\FreeRTOS\Source\include  下
include.png
     接口文件在   源码\\FreeRTOS\Source\portable\GCC\ARM_CM33_NTZ\non_secure 下                       源码\FreeRTOS\Source\portable\MemMang\heap_4.c
11.png heap4.png
     将以上文件copy出来,放入自己的工程内

    keil目录拓扑
    02.png
   增加include路径
09.png

    c.FREERTOSCONFIG.H文件修改
     将freertos文件加入工程后就剩最后的freertos配置文件了。
     主要注意以下几个配置
FREERTOSCONFIG.H文件定义
#include "platform.h"
extern uint32_t SystemCoreClock;

//configENABLE_TRUSTZONE 配置为0

#define configENABLE_MPU                                0//MPU支持
#define configENABLE_FPU                                1 //要启用 FPU 支持,请构建 FreeRTOS,并将 configENABLE_FPU 设置为一
#define configENABLE_TRUSTZONE                          0 //TrustZone 支持

#define configMINIMAL_STACK_SIZE     ( ( uint16_t ) 256 )/ /小了任务里面printf打印会有问题
#define configTOTAL_HEAP_SIZE        ( ( size_t ) ( 10 * 1024 ) ) //大了编译器报错
#define configCPU_CLOCK_HZ                              SystemCoreClock//系统时钟180000000
//注意mm32f5330_it.c要注释掉一下几个中断
#define SVC_Handler                                     SVCall_Handler
#define xPortPendSVHandler                              PendSV_Handler
#define xPortSysTickHandler                             SysTick_Handler
完整配置文件
  1. #ifndef FREERTOS_CONFIG_H
  2. #define FREERTOS_CONFIG_H

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

  15. /* Star-MC1 port configuration. */
  16. #define configENABLE_MPU                                0
  17. #define configENABLE_FPU                                1
  18. #define configENABLE_TRUSTZONE                          0

  19. /* Constants related to the behaviour or the scheduler. */
  20. #define configUSE_PORT_OPTIMISED_TASK_SELECTION         0
  21. #define configUSE_PREEMPTION                            1
  22. #define configUSE_TIME_SLICING                          1
  23. #define configMAX_PRIORITIES                            ( 5 )
  24. #define configIDLE_SHOULD_YIELD                         1
  25. #define configUSE_16_BIT_TICKS                          0 /* Only for 8 and 16-bit hardware. */

  26. /* Constants that describe the hardware and memory usage. */
  27. #define configCPU_CLOCK_HZ                              SystemCoreClock//系统时钟
  28. #define configMINIMAL_STACK_SIZE                        ( ( uint16_t ) 512 )
  29. #define configMAX_TASK_NAME_LEN                         ( 12 )
  30. #define configTOTAL_HEAP_SIZE                           ( ( size_t ) ( 10 * 1024 ) )//大了会报错
  31. #define configTICK_RATE_HZ                          ( ( TickType_t ) 1000 )
  32. /* Constants that build features in or out. */
  33. #define configUSE_MUTEXES                               1
  34. #define configUSE_TICKLESS_IDLE                         1
  35. #define configUSE_APPLICATION_TASK_TAG                  0
  36. #define configUSE_NEWLIB_REENTRANT                      0
  37. #define configUSE_CO_ROUTINES                           0
  38. #define configUSE_COUNTING_SEMAPHORES                   1
  39. #define configUSE_RECURSIVE_MUTEXES                     1
  40. #define configUSE_QUEUE_SETS                            0
  41. #define configUSE_TASK_NOTIFICATIONS                    1
  42. #define configUSE_TRACE_FACILITY                        1

  43. /* Constants that define which hook (callback) functions should be used. */
  44. #define configUSE_IDLE_HOOK                             0
  45. #define configUSE_TICK_HOOK                             0
  46. #define configUSE_MALLOC_FAILED_HOOK                    0

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

  51. /* Software timer definitions. */
  52. #define configUSE_TIMERS                                1
  53. #define configTIMER_TASK_PRIORITY                       ( 3 )
  54. #define configTIMER_QUEUE_LENGTH                        5
  55. #define configTIMER_TASK_STACK_DEPTH                    ( configMINIMAL_STACK_SIZE  )

  56. /* Set the following definitions to 1 to include the API function, or zero
  57. * to exclude the API function.  NOTE:  Setting an INCLUDE_ parameter to 0 is
  58. * only necessary if the linker does not automatically remove functions that are
  59. * not referenced anyway. */
  60. #define INCLUDE_vTaskPrioritySet                        1
  61. #define INCLUDE_uxTaskPriorityGet                       1
  62. #define INCLUDE_vTaskDelete                             1
  63. #define INCLUDE_vTaskCleanUpResources                   0
  64. #define INCLUDE_vTaskSuspend                            1
  65. #define INCLUDE_vTaskDelayUntil                         1
  66. #define INCLUDE_vTaskDelay                              1
  67. #define INCLUDE_uxTaskGetStackHighWaterMark             0
  68. #define INCLUDE_xTaskGetIdleTaskHandle                  0
  69. #define INCLUDE_eTaskGetState                           1
  70. #define INCLUDE_xTaskResumeFromISR                      0
  71. #define INCLUDE_xTaskGetCurrentTaskHandle               1
  72. #define INCLUDE_xTaskGetSchedulerState                  0
  73. #define INCLUDE_xSemaphoreGetMutexHolder                0
  74. #define INCLUDE_xTimerPendFunctionCall                  1

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

  80. /* Dimensions a buffer that can be used by the FreeRTOS+CLI command interpreter.
  81. * See the FreeRTOS+CLI documentation for more information:
  82. * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_CLI/ */
  83. #define configCOMMAND_INT_MAX_OUTPUT_SIZE               2048

  84. /* Interrupt priority configuration follows...................... */

  85. /* Use the system definition, if there is one. */
  86. #ifdef __NVIC_PRIO_BITS
  87.     #define configPRIO_BITS                             __NVIC_PRIO_BITS
  88. #else
  89.     #define configPRIO_BITS                             3   /* 8 priority levels. */
  90. #endif

  91. /* The lowest interrupt priority that can be used in a call to a "set priority"
  92. * function. */
  93. #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY         0x07

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

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

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

  105. /* The #ifdef guards against the file being included from IAR assembly files. */
  106. #ifndef __IASMARM__

  107.     /* Constants related to the generation of run time stats. */
  108.     #define configGENERATE_RUN_TIME_STATS               0
  109.     #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()
  110.     #define portGET_RUN_TIME_COUNTER_VALUE()            0
  111.   //  #define configTICK_RATE_HZ                          ( ( TickType_t ) 1000 )

  112. #endif /* __IASMARM__ */

  113. /* Enable static allocation. */
  114. #define configSUPPORT_STATIC_ALLOCATION                 0

  115. /* System Handler */
  116. #define SVC_Handler                                     SVCall_Handler
  117. #define xPortPendSVHandler                              PendSV_Handler
  118. #define xPortSysTickHandler                             SysTick_Handler

  119. #endif /* FREERTOS_CONFIG_H */


     d.中断函数屏蔽
     中断里面要屏蔽模板自带的几个中断入口函数,因为freertos已经帮我们实现了,为了防止编译报重定义所以需要屏蔽。      屏蔽中断入口.png
    e. 移植打印tinyprintf
     任务里面不能使用microlib自带的 pintf函数 不然卡死,这个坑了我好几天,移植都没啥问题就是进入调度后会卡死。
     找了个小资源的tinyprintf 移植上来可以打印了,xprintf测试也可以


  1. int uart1_sent(int ch)
  2. {      
  3.     UART_SendData(UART1, (uint8_t)ch);

  4.     while (RESET == UART_GetFlagStatus(UART1, UART_FLAG_TXC))
  5.     {
  6.     }

  7.     return (ch);
  8. }

  9. void tiny_sent ( void* p, char ch)
  10. {      
  11.     UART_SendData(UART1, (uint8_t)ch);

  12.     while (RESET == UART_GetFlagStatus(UART1, UART_FLAG_TXC))
  13.     {
  14.     }

  15. }
     
  1.   xdev_out(uart1_sent);//xprintf 接口
  2.                 init_printf(NULL,tiny_sent );//tinyprintf 接口


    d.编译
       13编译.png


2.任务创建


  1. //任务优先级
  2. #define START_TASK_PRIO                1
  3. //任务堆栈大小        
  4. #define START_STK_SIZE                 128  
  5. //任务句柄
  6. TaskHandle_t StartTask_Handler;
  7. //任务函数
  8. void start_task(void *pvParameters);

  9. //任务优先级
  10. #define LED1_TASK_PRIO                2
  11. //任务堆栈大小        
  12. #define LED1_STK_SIZE                 50  
  13. //任务句柄
  14. TaskHandle_t LED1Task_Handler;
  15. //任务函数
  16. void led1_task(void *pvParameters);

  17. //任务优先级
  18. #define LED2_TASK_PRIO                3
  19. //任务堆栈大小        
  20. #define LED2_STK_SIZE                 50  
  21. //任务句柄
  22. TaskHandle_t LED2Task_Handler;
  23. //任务函数
  24. void led2_task(void *pvParameters);

  25. int uart1_sent(int ch)
  26. {      
  27.     UART_SendData(UART1, (uint8_t)ch);

  28.     while (RESET == UART_GetFlagStatus(UART1, UART_FLAG_TXC))
  29.     {
  30.     }

  31.     return (ch);
  32. }

  33. void tiny_sent ( void* p, char ch)
  34. {      
  35.     UART_SendData(UART1, (uint8_t)ch);

  36.     while (RESET == UART_GetFlagStatus(UART1, UART_FLAG_TXC))
  37.     {
  38.     }

  39. }


  40. int main(void)
  41. {
  42.                 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  43.     PLATFORM_InitConsole(115200);
  44.     PLATFORM_InitLED();
  45.                 xdev_out(uart1_sent);//xprintf 接口
  46.                 init_printf(NULL,tiny_sent );//tinyprintf 接口
  47.                 //创建开始任务
  48.     xTaskCreate(  start_task  ,      //任务函数
  49.                 (const char*    )"start_task",          //任务名称
  50.                 (uint16_t       )START_STK_SIZE,        //任务堆栈大小
  51.                 (void*          )NULL,                  //传递给任务函数的参数
  52.                 (UBaseType_t    )START_TASK_PRIO,       //任务优先级
  53.                 (TaskHandle_t*  )&StartTask_Handler);   //任务句柄   
  54.                
  55.     vTaskStartScheduler();          //开启任务调度
  56.         
  57.                 while (1)
  58.                 {

  59.                 }        
  60. }



  61. //开始任务任务函数
  62. void start_task(void *pvParameters)
  63. {

  64.     taskENTER_CRITICAL();           //进入临界区
  65.     //创建LED0任务
  66.     xTaskCreate((TaskFunction_t )led1_task,            
  67.                 (const char*    )"led1_task",           
  68.                 (uint16_t       )LED1_STK_SIZE,
  69.                 (void*          )NULL,                                
  70.                 (UBaseType_t    )LED1_TASK_PRIO,        
  71.                 (TaskHandle_t*  )&LED1Task_Handler);   
  72.     //创建LED1任务
  73.     xTaskCreate((TaskFunction_t )led2_task,     
  74.                 (const char*    )"led2_task",   
  75.                 (uint16_t       )LED2_STK_SIZE,
  76.                 (void*          )NULL,
  77.                 (UBaseType_t    )LED2_TASK_PRIO,
  78.                 (TaskHandle_t*  )&LED2Task_Handler);         
  79.     vTaskDelete(StartTask_Handler); //删除开始任务
  80.     taskEXIT_CRITICAL();            //退出临界区
  81. }

  82. //LED0任务函数
  83. void led1_task(void *pvParameters)
  84. {
  85.     while(1)
  86.     {
  87.                                 //xprintf("task1 led1111111111111111111111111\r");
  88.                                 tfp_printf("task1 led1111111111111111111111111\r");
  89.         GPIO_WriteBit(GPIOB, GPIO_Pin_11, 0);
  90.         vTaskDelay(500);
  91.                                 GPIO_WriteBit(GPIOB, GPIO_Pin_11, 1);
  92.                                 vTaskDelay(500);

  93.     }
  94. }   

  95. //LED1任务函数
  96. void led2_task(void *pvParameters)
  97. {
  98.     while(1)
  99.     {
  100.                                 xprintf("task2 led222222\r");
  101.         GPIO_WriteBit(GPIOB, GPIO_Pin_10, 0);
  102.         vTaskDelay(200);
  103.         GPIO_WriteBit(GPIOB, GPIO_Pin_10, 1);
  104.         vTaskDelay(200);
  105.     }
  106. }



三、实验结果
14串口打印任务信息.png


四、实验心得

移植过程还是有很多需要注意的地方,由于没有M33内核太多参考很多报错和异常都需要自己去搜索引擎上查找相关信息。
但是通过对异常的解决可以加深自己对freertos的了解。
希望本帖能帮助大家减少开发过程中的异常。








 楼主| xiaoqi976633690 发表于 2024-6-24 11:44 | 显示全部楼层
附件在此

GPIO_KEY_Input FREERTOS.zip

819.54 KB, 下载次数: 8

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

本版积分规则

35

主题

204

帖子

2

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