[MM32硬件] 【灵动微电子MM32F0121测评】8、移植RTOS到MM32F0121系列MCU

[复制链接]
 楼主| sujingliang 发表于 2025-6-16 11:41 | 显示全部楼层 |阅读模式
本文目的:移植RTOS到MM32F0121系列MCU

首先要准备一个MM32F0121基本模板,可以以GPIO_LED_Toggle为基础。

在LibSamples_MM32F0120_V1.13.4中建立了middlewares\freertos目录,复制一份freeRTOS库文件此目录

将freeRTOS下各路径下7个文件添加到工程中
8.png

添加include paths
..\..\..\..\..\middlewares\freertos\source\include
..\..\..\..\..\middlewares\freertos\source\portable\rvds\ARM_CM0


在freeRTOS中找到FreeRTOSConfig.h、portmacro.h,复制到工程根目录下




FreeRTOSConfig.h中修改

  1. #define configUSE_PREEMPTION                        1
  2. #define configUSE_IDLE_HOOK                                0
  3. #define configUSE_TICK_HOOK                                0
  4. #define configCPU_CLOCK_HZ                                ( 72000000 )
  5. #define configTICK_RATE_HZ                                ( ( TickType_t ) 1000 )
  6. #define configMAX_PRIORITIES                        ( 5 )
  7. #define configMINIMAL_STACK_SIZE                ( ( unsigned short ) 128 )
  8. #define configTOTAL_HEAP_SIZE                        ( ( size_t ) ( 3*1024 ) )
  9. #define configMAX_TASK_NAME_LEN                        ( 5 )
  10. #define configUSE_TRACE_FACILITY                1
  11. #define configUSE_16_BIT_TICKS                        0
  12. #define configIDLE_SHOULD_YIELD                        1
  13. #define configUSE_MUTEXES                                1
  14. #define configQUEUE_REGISTRY_SIZE                8
  15. #define configCHECK_FOR_STACK_OVERFLOW        0
  16. #define configUSE_RECURSIVE_MUTEXES                1
  17. #define configUSE_MALLOC_FAILED_HOOK        0
  18. #define configUSE_APPLICATION_TASK_TAG        0
  19. #define configUSE_COUNTING_SEMAPHORES        1
  20. #define configGENERATE_RUN_TIME_STATS        0

  21. /* Co-routine definitions. */
  22. #define configUSE_CO_ROUTINES                         0
  23. #define configMAX_CO_ROUTINE_PRIORITIES ( 2 )

  24. /* Software timer definitions. */
  25. #define configUSE_TIMERS                                0
  26. #define configTIMER_TASK_PRIORITY                ( 2 )
  27. #define configTIMER_QUEUE_LENGTH                5
  28. #define configTIMER_TASK_STACK_DEPTH        ( 80 )

  29. /* Set the following definitions to 1 to include the API function, or zero
  30. to exclude the API function. */
  31. #define INCLUDE_vTaskPrioritySet                1
  32. #define INCLUDE_uxTaskPriorityGet                1
  33. #define INCLUDE_vTaskDelete                                1
  34. #define INCLUDE_vTaskCleanUpResources        1
  35. #define INCLUDE_vTaskSuspend                        1
  36. #define INCLUDE_vTaskDelayUntil                        1
  37. #define INCLUDE_vTaskDelay                                1

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

  41. /* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
  42. standard names - or at least those used in the unmodified vector table. */
  43. //#define vPortSVCHandler SVC_Handler

  44. #define xPortPendSVHandler PendSV_Handler
  45. #define xPortSysTickHandler SysTick_Handler

  46. #endif /* FREERTOS_CONFIG_H */


注意其中:
#define configCPU_CLOCK_HZ                                ( 72000000 )  //主频72MHZ
#define configMINIMAL_STACK_SIZE                ( ( unsigned short ) 128 )  //stack根据情况设置,LED task可以设小点,printf需要设大一点

#define xPortPendSVHandler PendSV_Handler  //
#define xPortSysTickHandler SysTick_Handler  //这两个中断需要被freeRTOS接管


mm32f0120_it.c中将这两个中断处理函数注释掉,避免编译冲突
  1. /*
  2. void PendSV_Handler(void)
  3. {
  4. }
  5. */
  1. /*
  2. void SysTick_Handler(void)
  3. {

  4.     if (0 != PLATFORM_DelayTick)
  5.     {
  6.         PLATFORM_DelayTick--;
  7.     }

  8.                
  9. }

  10. */
main()修改,执行freeRTOS_Sample()
  1. int main(void)
  2. {
  3.     PLATFORM_Init();

  4.                 freeRTOS_Sample();

  5.     while (1)
  6.     {
  7.     }
  8. }


PLATFORM_Init中注释掉PLATFORM_InitDelay()
  1. void PLATFORM_Init(void)
  2. {
  3.    // PLATFORM_InitDelay();

  4.     PLATFORM_InitConsole(115200);

  5.     PLATFORM_InitLED();

  6.     PLATFORM_PrintInfo();
  7. }


新建freeRTOS_demo.c存放以下内容:

freeRTOS_Sample()
建立3个Task

#define PRINT_TASK_STACK_SIZE  (256)  // 256 words = 1024 bytes
#define LED_TASK_STACK_SIZE    (configMINIMAL_STACK_SIZE)  // 128 words = 512 bytes

  1. void freeRTOS_Sample(void)
  2. {
  3.         printf("【灵动微电子MM32F0121测评】8、移植RTOS到MM32F0121系列MCU\r\n");
  4.         SysTick_Init();
  5.        

  6.         xTaskCreate( vLED1Task, "LED1", LED_TASK_STACK_SIZE, NULL,  tskIDLE_PRIORITY + 6 , NULL );       
  7.         xTaskCreate( vLED2Task, "LED2", LED_TASK_STACK_SIZE, NULL,  tskIDLE_PRIORITY + 7 , NULL );
  8.         xTaskCreate( printTask, "print", PRINT_TASK_STACK_SIZE, NULL,  tskIDLE_PRIORITY + 8 , NULL );

  9.         vTaskStartScheduler();
  10.        
  11.         while(1); // 正常情况下不会执行到这里
  12. }


SysTick_Init()
  1. static void SysTick_Init(void)
  2. {
  3.         if (SysTick_Config(SystemCoreClock / configTICK_RATE_HZ)) {
  4.     printf("SysTick config failed\r\n");
  5.                 while (1);  // 初始化失败
  6.         }
  7. }


三个TASK

  1. void vLED1Task( void *pvParameters )
  2. {
  3.        
  4.     for( ;; )
  5.         {
  6.                 PLATFORM_LED_Toggle(LED1);
  7.                 //printf("1 Free Heap: %d\n", xPortGetFreeHeapSize());
  8.     vTaskDelay(pdMS_TO_TICKS(100));
  9.         }
  10. }


  11. void vLED2Task( void *pvParameters )
  12. {
  13.         for( ;; )
  14.         {
  15.                 PLATFORM_LED_Toggle(LED2);
  16.                 //printf("2 Free Heap: %d\n", xPortGetFreeHeapSize());
  17.                 vTaskDelay(pdMS_TO_TICKS(500));
  18.         }
  19. }


  20. void printTask( void *pvParameters )
  21. {
  22.   for( ;; )
  23.         {
  24.                 printf("Hello 21ic, Hello MM32F0121\r\n");
  25.     vTaskDelay(pdMS_TO_TICKS(2000));

  26.         }
  27. }


运行情况:

tutieshi_640x357_6s (1).gif


AdaMaYun 发表于 2025-7-31 17:58 | 显示全部楼层
移植RTOS很不错的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

84

主题

146

帖子

3

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