查看: 1663|回复: 0

[分享] 【MCX-N947-BRK板卡试用】运行FreeRTOS

[复制链接]
  • TA的每日心情
    奋斗
    14 分钟前
  • 签到天数: 2458 天

    连续签到: 83 天

    [LV.Master]伴坛终老

    25

    主题

    7665

    帖子

    21

    金牌会员

    Rank: 6Rank: 6

    积分
    14757
    最后登录
    2025-7-27
    发表于 2024-5-5 23:58:26 | 显示全部楼层 |阅读模式
    本帖最后由 yinwuqing 于 2024-5-5 23:58 编辑

         随着物联网,人工智能,AI等大型项目的市场需求,原本的裸机前后台系统已经很难满足项目的功能需求,在裸机系统中,系统的主体就是main函数里面顺序执行的无限循环,这个无限循环里面CPU按照顺序完成各种事情。在多任务系统中,根据功能的不同,把整个系统分割成一个个独立的且无法返回的函数,这个函数我们称为任务。系统中的每一任务都有多种运行状态。系统初始化完成后,创建的任务就可以在系统中竞争一定的资源,由内核进行调度。     今天笔者主要介绍FreeRTOS的应用。FreeRTOS 是开源免费的,可从官网https://www.freertos.org/下载源码和说明手册,当然也是可以通过github网址https://github.com/FreeRTOS/FreeRTOS获取的。用户可以获取到最新版的源码后,移植到自己项目源码中,然后做一些适配修改,即可实现多任务多线程编程。官方的“SDK_2_14_0_FRDM-MCXN947”文件包中有提供\rtos模块,主推freertos应用,因此这里我们进入到“SDK_2_14_0_FRDM-MCXN947\boards\frdmmcxn947\freertos_examples”目录下,可预览官方提供支持FreeRTOS的参考例程。笔者以“freertos_hello”参考例程为基础,实现创建三个独立线程,每个线程闪灯的频率不同。      部分参考代码如下:
    1. #include "fsl_common.h"
    2. #include "fsl_port.h"
    3. #include "pin_mux.h"

    4. /* FUNCTION ************************************************************************************************************
    5. *
    6. * Function Name : BOARD_InitBootPins
    7. * Description   : Calls initialization functions.
    8. *
    9. * END ****************************************************************************************************************/
    10. void BOARD_InitBootPins(void)
    11. {
    12.     BOARD_InitPins();
    13. }

    14. void BOARD_InitPins(void)
    15. {
    16.     /* Enables the clock for PORT1: Enables clock */
    17.     CLOCK_EnableClock(kCLOCK_Port1);
    18.                 CLOCK_EnableClock(kCLOCK_Port3);

    19.          const port_pin_config_t port3_2_pinD15_config = {/* Internal pull-up/down resistor is disabled */
    20.                                                       kPORT_PullDisable,
    21.                                                       /* Low internal pull resistor value is selected. */
    22.                                                       kPORT_LowPullResistor,
    23.                                                       /* Fast slew rate is configured */
    24.                                                       kPORT_FastSlewRate,
    25.                                                       /* Passive input filter is disabled */
    26.                                                       kPORT_PassiveFilterDisable,
    27.                                                       /* Open drain output is disabled */
    28.                                                       kPORT_OpenDrainDisable,
    29.                                                       /* Low drive strength is configured */
    30.                                                       kPORT_LowDriveStrength,
    31.                                                       /* Pin is configured as PIO3_2 */
    32.                                                       kPORT_MuxAlt0,
    33.                                                       /* Digital input enabled */
    34.                                                       kPORT_InputBufferEnable,
    35.                                                       /* Digital input is not inverted */
    36.                                                       kPORT_InputNormal,
    37.                                                       /* Pin Control Register fields [15:0] are not locked */
    38.                                                       kPORT_UnlockRegister};
    39.     /* PORT3_2 (pin D15) is configured as PIO3_2 */
    40.     PORT_SetPinConfig(PORT3, 2U, &port3_2_pinD15_config);
    41.           const port_pin_config_t port3_3_pinD16_config = {/* Internal pull-up/down resistor is disabled */
    42.                                                       kPORT_PullDisable,
    43.                                                       /* Low internal pull resistor value is selected. */
    44.                                                       kPORT_LowPullResistor,
    45.                                                       /* Fast slew rate is configured */
    46.                                                       kPORT_FastSlewRate,
    47.                                                       /* Passive input filter is disabled */
    48.                                                       kPORT_PassiveFilterDisable,
    49.                                                       /* Open drain output is disabled */
    50.                                                       kPORT_OpenDrainDisable,
    51.                                                       /* Low drive strength is configured */
    52.                                                       kPORT_LowDriveStrength,
    53.                                                       /* Pin is configured as PIO3_3 */
    54.                                                       kPORT_MuxAlt0,
    55.                                                       /* Digital input enabled */
    56.                                                       kPORT_InputBufferEnable,
    57.                                                       /* Digital input is not inverted */
    58.                                                       kPORT_InputNormal,
    59.                                                       /* Pin Control Register fields [15:0] are not locked */
    60.                                                       kPORT_UnlockRegister};
    61.     /* PORT3_3 (pin D16) is configured as PIO3_3 */
    62.     PORT_SetPinConfig(PORT3, 3U, &port3_3_pinD16_config);
    63.     const port_pin_config_t port3_4_pinF14_config = {/* Internal pull-up/down resistor is disabled */
    64.                                                       kPORT_PullDisable,
    65.                                                       /* Low internal pull resistor value is selected. */
    66.                                                       kPORT_LowPullResistor,
    67.                                                       /* Fast slew rate is configured */
    68.                                                       kPORT_FastSlewRate,
    69.                                                       /* Passive input filter is disabled */
    70.                                                       kPORT_PassiveFilterDisable,
    71.                                                       /* Open drain output is disabled */
    72.                                                       kPORT_OpenDrainDisable,
    73.                                                       /* Low drive strength is configured */
    74.                                                       kPORT_LowDriveStrength,
    75.                                                       /* Pin is configured as PIO3_4 */
    76.                                                       kPORT_MuxAlt0,
    77.                                                       /* Digital input enabled */
    78.                                                       kPORT_InputBufferEnable,
    79.                                                       /* Digital input is not inverted */
    80.                                                       kPORT_InputNormal,
    81.                                                       /* Pin Control Register fields [15:0] are not locked */
    82.                                                       kPORT_UnlockRegister};
    83.     /* PORT3_4 (pin F14) is configured as PIO3_4 */
    84.     PORT_SetPinConfig(PORT3, 4U, &port3_4_pinF14_config);
    85.     const port_pin_config_t port1_8_pinA1_config = {/* Internal pull-up/down resistor is disabled */
    86.                                                     kPORT_PullDisable,
    87.                                                     /* Low internal pull resistor value is selected. */
    88.                                                     kPORT_LowPullResistor,
    89.                                                     /* Fast slew rate is configured */
    90.                                                     kPORT_FastSlewRate,
    91.                                                     /* Passive input filter is disabled */
    92.                                                     kPORT_PassiveFilterDisable,
    93.                                                     /* Open drain output is disabled */
    94.                                                     kPORT_OpenDrainDisable,
    95.                                                     /* Low drive strength is configured */
    96.                                                     kPORT_LowDriveStrength,
    97.                                                     /* Pin is configured as FC4_P0 */
    98.                                                     kPORT_MuxAlt2,
    99.                                                     /* Digital input enabled */
    100.                                                     kPORT_InputBufferEnable,
    101.                                                     /* Digital input is not inverted */
    102.                                                     kPORT_InputNormal,
    103.                                                     /* Pin Control Register fields [15:0] are not locked */
    104.                                                     kPORT_UnlockRegister};
    105.     /* PORT1_8 (pin A1) is configured as FC4_P0 */
    106.     PORT_SetPinConfig(PORT1, 8U, &port1_8_pinA1_config);

    107.     const port_pin_config_t port1_9_pinB1_config = {/* Internal pull-up/down resistor is disabled */
    108.                                                     kPORT_PullDisable,
    109.                                                     /* Low internal pull resistor value is selected. */
    110.                                                     kPORT_LowPullResistor,
    111.                                                     /* Fast slew rate is configured */
    112.                                                     kPORT_FastSlewRate,
    113.                                                     /* Passive input filter is disabled */
    114.                                                     kPORT_PassiveFilterDisable,
    115.                                                     /* Open drain output is disabled */
    116.                                                     kPORT_OpenDrainDisable,
    117.                                                     /* Low drive strength is configured */
    118.                                                     kPORT_LowDriveStrength,
    119.                                                     /* Pin is configured as FC4_P1 */
    120.                                                     kPORT_MuxAlt2,
    121.                                                     /* Digital input enabled */
    122.                                                     kPORT_InputBufferEnable,
    123.                                                     /* Digital input is not inverted */
    124.                                                     kPORT_InputNormal,
    125.                                                     /* Pin Control Register fields [15:0] are not locked */
    126.                                                     kPORT_UnlockRegister};
    127.     /* PORT1_9 (pin B1) is configured as FC4_P1 */
    128.     PORT_SetPinConfig(PORT1, 9U, &port1_9_pinB1_config);
    129. }
    复制代码
    1. /* FreeRTOS kernel includes. */
    2. #include "FreeRTOS.h"
    3. #include "task.h"
    4. #include "queue.h"
    5. #include "timers.h"

    6. /* Freescale includes. */
    7. #include "fsl_device_registers.h"
    8. #include "fsl_debug_console.h"
    9. #include "pin_mux.h"
    10. #include "clock_config.h"
    11. #include "board.h"

    12. #include "fsl_clock.h"
    13. /*******************************************************************************
    14. * Definitions
    15. ******************************************************************************/

    16. /* Task priorities. */
    17. #define led1_task_PRIORITY (configMAX_PRIORITIES - 1)
    18. #define led2_task_PRIORITY (configMAX_PRIORITIES - 2)
    19. #define led3_task_PRIORITY (configMAX_PRIORITIES - 3)
    20. /*******************************************************************************
    21. * Prototypes
    22. ******************************************************************************/
    23. static void led1_task(void *pvParameters);
    24. static void led2_task(void *pvParameters);
    25. static void led3_task(void *pvParameters);

    26. /*!
    27. * @brief Application entry point.
    28. */
    29. int main(void)
    30. {
    31.     /* Init board hardware. */
    32.     /* attach FRO 12M to FLEXCOMM4 (debug console) */
    33.     CLOCK_SetClkDiv(kCLOCK_DivFlexcom4Clk, 1u);
    34.     CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
    35.                 CLOCK_EnableClock(kCLOCK_Gpio3);
    36.        
    37.     BOARD_InitPins();
    38.     BOARD_InitBootClocks();
    39.     BOARD_InitDebugConsole();
    40.           LED_GREEN_INIT(LOGIC_LED_OFF);
    41.                 LED_BLUE_INIT(LOGIC_LED_OFF);
    42.                 LED_RED_INIT(LOGIC_LED_OFF);
    43.     if (xTaskCreate(led1_task, "Led1_task", configMINIMAL_STACK_SIZE + 100, NULL, led1_task_PRIORITY, NULL) != pdPASS)
    44.     {
    45.         PRINTF("Task1 creation failed!.\r\n");
    46.         while (1);
    47.     }
    48.                 if (xTaskCreate(led2_task, "Led2_task", configMINIMAL_STACK_SIZE + 200, NULL, led2_task_PRIORITY, NULL) != pdPASS)
    49.     {
    50.         PRINTF("Task2 creation failed!.\r\n");
    51.         while (1);
    52.     }
    53.                 if (xTaskCreate(led3_task, "Led3_task", configMINIMAL_STACK_SIZE + 300, NULL, led3_task_PRIORITY, NULL) != pdPASS)
    54.     {
    55.         PRINTF("Task3 creation failed!.\r\n");
    56.         while (1);
    57.     }
    58.     vTaskStartScheduler();
    59.     for (;;);
    60. }

    61. static void led1_task(void *pvParameters)
    62. {
    63.     for (;;)
    64.     {
    65.                           LED_RED_TOGGLE();
    66.                           vTaskDelay(100);
    67.         PRINTF("LED1 run.\r\n");
    68.     }
    69. }

    70. static void led2_task(void *pvParameters)
    71. {
    72.     for (;;)
    73.     {
    74.                           LED_GREEN_TOGGLE();
    75.                           vTaskDelay(500);
    76.         PRINTF("LED2 run.\r\n");
    77.     }
    78. }

    79. static void led3_task(void *pvParameters)
    80. {
    81.     for (;;)
    82.     {
    83.                           LED_BLUE_TOGGLE();
    84.                           vTaskDelay(1000);
    85.         PRINTF("LED3 run.\r\n");
    86.       
    87.     }
    88. }
    复制代码
        实物连线如下图所示:
    实物连线.jpg
        工程编译、下载后,则板上的RGB灯会以一定规律变化各个颜色,与此同时,串口调试助手输出如下信息:
    串口输出.gif
          可见三个任务的实时执行情况与理论相同,视频见如下附件。
    运行状态.zip (8.39 MB, 下载次数: 1)
    哎...今天够累的,签到来了~
    回复

    使用道具 举报

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

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-7-27 12:24 , Processed in 0.083691 second(s), 20 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表