在线时间1115 小时
UID1694538
注册时间2018-5-21
NXP金币5077
TA的每日心情 | 奋斗 14 分钟前 |
---|
签到天数: 2458 天 连续签到: 83 天 [LV.Master]伴坛终老
金牌会员
 
- 积分
- 14757
- 最后登录
- 2025-7-27
|
本帖最后由 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”参考例程为基础,实现创建三个独立线程,每个线程闪灯的频率不同。 部分参考代码如下:
- #include "fsl_common.h"
- #include "fsl_port.h"
- #include "pin_mux.h"
- /* FUNCTION ************************************************************************************************************
- *
- * Function Name : BOARD_InitBootPins
- * Description : Calls initialization functions.
- *
- * END ****************************************************************************************************************/
- void BOARD_InitBootPins(void)
- {
- BOARD_InitPins();
- }
- void BOARD_InitPins(void)
- {
- /* Enables the clock for PORT1: Enables clock */
- CLOCK_EnableClock(kCLOCK_Port1);
- CLOCK_EnableClock(kCLOCK_Port3);
- const port_pin_config_t port3_2_pinD15_config = {/* Internal pull-up/down resistor is disabled */
- kPORT_PullDisable,
- /* Low internal pull resistor value is selected. */
- kPORT_LowPullResistor,
- /* Fast slew rate is configured */
- kPORT_FastSlewRate,
- /* Passive input filter is disabled */
- kPORT_PassiveFilterDisable,
- /* Open drain output is disabled */
- kPORT_OpenDrainDisable,
- /* Low drive strength is configured */
- kPORT_LowDriveStrength,
- /* Pin is configured as PIO3_2 */
- kPORT_MuxAlt0,
- /* Digital input enabled */
- kPORT_InputBufferEnable,
- /* Digital input is not inverted */
- kPORT_InputNormal,
- /* Pin Control Register fields [15:0] are not locked */
- kPORT_UnlockRegister};
- /* PORT3_2 (pin D15) is configured as PIO3_2 */
- PORT_SetPinConfig(PORT3, 2U, &port3_2_pinD15_config);
- const port_pin_config_t port3_3_pinD16_config = {/* Internal pull-up/down resistor is disabled */
- kPORT_PullDisable,
- /* Low internal pull resistor value is selected. */
- kPORT_LowPullResistor,
- /* Fast slew rate is configured */
- kPORT_FastSlewRate,
- /* Passive input filter is disabled */
- kPORT_PassiveFilterDisable,
- /* Open drain output is disabled */
- kPORT_OpenDrainDisable,
- /* Low drive strength is configured */
- kPORT_LowDriveStrength,
- /* Pin is configured as PIO3_3 */
- kPORT_MuxAlt0,
- /* Digital input enabled */
- kPORT_InputBufferEnable,
- /* Digital input is not inverted */
- kPORT_InputNormal,
- /* Pin Control Register fields [15:0] are not locked */
- kPORT_UnlockRegister};
- /* PORT3_3 (pin D16) is configured as PIO3_3 */
- PORT_SetPinConfig(PORT3, 3U, &port3_3_pinD16_config);
- const port_pin_config_t port3_4_pinF14_config = {/* Internal pull-up/down resistor is disabled */
- kPORT_PullDisable,
- /* Low internal pull resistor value is selected. */
- kPORT_LowPullResistor,
- /* Fast slew rate is configured */
- kPORT_FastSlewRate,
- /* Passive input filter is disabled */
- kPORT_PassiveFilterDisable,
- /* Open drain output is disabled */
- kPORT_OpenDrainDisable,
- /* Low drive strength is configured */
- kPORT_LowDriveStrength,
- /* Pin is configured as PIO3_4 */
- kPORT_MuxAlt0,
- /* Digital input enabled */
- kPORT_InputBufferEnable,
- /* Digital input is not inverted */
- kPORT_InputNormal,
- /* Pin Control Register fields [15:0] are not locked */
- kPORT_UnlockRegister};
- /* PORT3_4 (pin F14) is configured as PIO3_4 */
- PORT_SetPinConfig(PORT3, 4U, &port3_4_pinF14_config);
- const port_pin_config_t port1_8_pinA1_config = {/* Internal pull-up/down resistor is disabled */
- kPORT_PullDisable,
- /* Low internal pull resistor value is selected. */
- kPORT_LowPullResistor,
- /* Fast slew rate is configured */
- kPORT_FastSlewRate,
- /* Passive input filter is disabled */
- kPORT_PassiveFilterDisable,
- /* Open drain output is disabled */
- kPORT_OpenDrainDisable,
- /* Low drive strength is configured */
- kPORT_LowDriveStrength,
- /* Pin is configured as FC4_P0 */
- kPORT_MuxAlt2,
- /* Digital input enabled */
- kPORT_InputBufferEnable,
- /* Digital input is not inverted */
- kPORT_InputNormal,
- /* Pin Control Register fields [15:0] are not locked */
- kPORT_UnlockRegister};
- /* PORT1_8 (pin A1) is configured as FC4_P0 */
- PORT_SetPinConfig(PORT1, 8U, &port1_8_pinA1_config);
- const port_pin_config_t port1_9_pinB1_config = {/* Internal pull-up/down resistor is disabled */
- kPORT_PullDisable,
- /* Low internal pull resistor value is selected. */
- kPORT_LowPullResistor,
- /* Fast slew rate is configured */
- kPORT_FastSlewRate,
- /* Passive input filter is disabled */
- kPORT_PassiveFilterDisable,
- /* Open drain output is disabled */
- kPORT_OpenDrainDisable,
- /* Low drive strength is configured */
- kPORT_LowDriveStrength,
- /* Pin is configured as FC4_P1 */
- kPORT_MuxAlt2,
- /* Digital input enabled */
- kPORT_InputBufferEnable,
- /* Digital input is not inverted */
- kPORT_InputNormal,
- /* Pin Control Register fields [15:0] are not locked */
- kPORT_UnlockRegister};
- /* PORT1_9 (pin B1) is configured as FC4_P1 */
- PORT_SetPinConfig(PORT1, 9U, &port1_9_pinB1_config);
- }
复制代码- /* FreeRTOS kernel includes. */
- #include "FreeRTOS.h"
- #include "task.h"
- #include "queue.h"
- #include "timers.h"
- /* Freescale includes. */
- #include "fsl_device_registers.h"
- #include "fsl_debug_console.h"
- #include "pin_mux.h"
- #include "clock_config.h"
- #include "board.h"
- #include "fsl_clock.h"
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- /* Task priorities. */
- #define led1_task_PRIORITY (configMAX_PRIORITIES - 1)
- #define led2_task_PRIORITY (configMAX_PRIORITIES - 2)
- #define led3_task_PRIORITY (configMAX_PRIORITIES - 3)
- /*******************************************************************************
- * Prototypes
- ******************************************************************************/
- static void led1_task(void *pvParameters);
- static void led2_task(void *pvParameters);
- static void led3_task(void *pvParameters);
- /*!
- * @brief Application entry point.
- */
- int main(void)
- {
- /* Init board hardware. */
- /* attach FRO 12M to FLEXCOMM4 (debug console) */
- CLOCK_SetClkDiv(kCLOCK_DivFlexcom4Clk, 1u);
- CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
- CLOCK_EnableClock(kCLOCK_Gpio3);
-
- BOARD_InitPins();
- BOARD_InitBootClocks();
- BOARD_InitDebugConsole();
- LED_GREEN_INIT(LOGIC_LED_OFF);
- LED_BLUE_INIT(LOGIC_LED_OFF);
- LED_RED_INIT(LOGIC_LED_OFF);
- if (xTaskCreate(led1_task, "Led1_task", configMINIMAL_STACK_SIZE + 100, NULL, led1_task_PRIORITY, NULL) != pdPASS)
- {
- PRINTF("Task1 creation failed!.\r\n");
- while (1);
- }
- if (xTaskCreate(led2_task, "Led2_task", configMINIMAL_STACK_SIZE + 200, NULL, led2_task_PRIORITY, NULL) != pdPASS)
- {
- PRINTF("Task2 creation failed!.\r\n");
- while (1);
- }
- if (xTaskCreate(led3_task, "Led3_task", configMINIMAL_STACK_SIZE + 300, NULL, led3_task_PRIORITY, NULL) != pdPASS)
- {
- PRINTF("Task3 creation failed!.\r\n");
- while (1);
- }
- vTaskStartScheduler();
- for (;;);
- }
- static void led1_task(void *pvParameters)
- {
- for (;;)
- {
- LED_RED_TOGGLE();
- vTaskDelay(100);
- PRINTF("LED1 run.\r\n");
- }
- }
- static void led2_task(void *pvParameters)
- {
- for (;;)
- {
- LED_GREEN_TOGGLE();
- vTaskDelay(500);
- PRINTF("LED2 run.\r\n");
- }
- }
- static void led3_task(void *pvParameters)
- {
- for (;;)
- {
- LED_BLUE_TOGGLE();
- vTaskDelay(1000);
- PRINTF("LED3 run.\r\n");
-
- }
- }
复制代码 实物连线如下图所示:
工程编译、下载后,则板上的RGB灯会以一定规律变化各个颜色,与此同时,串口调试助手输出如下信息:
可见三个任务的实时执行情况与理论相同,视频见如下附件。
运行状态.zip
(8.39 MB, 下载次数: 1)
|
|