0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

FreeRTOS:一个迷你的实时操作系统内核

科技绿洲 来源:面包板社区 作者:面包板社区 2023-06-29 17:15 次阅读

** 1、FreeRTOS**

FreeRTOS是一个迷你的实时操作系统内核。作为一个轻量级的操作系统,功能包括:任务管理、时间管理、信号量、消息队列、内存管理、记录功能、软件定时器、协程等,可基本满足较小系统的需要。由于RTOS需占用一定的系统资源(尤其是RAM资源),只有μC/OS-II、embOS、salvo、FreeRTOS等少数实时操作系统能在小RAM单片机上运行。相对μC/OS-II、embOS等商业操作系统,FreeRTOS操作系统是完全免费的操作系统,具有源码公开、可移植、可裁减、调度策略灵活的特点,可以方便地移植到各种单片机上运行。

2、移植

(1)第一步:创建工程

修改时钟

图片

(2)第二步:LED配置

图片

(3)第三步:FreeRTOS配置

FreeRTOS公共部分配置

图片

LED线程配置

图片

(4)第四部:任务入口函数编写

#include "led_task.h"
#include "led.h"
                /* led entry function */
                /* pvParameters contains TaskHandle_t */
                void led_task_entry(void * pvParameters)
{
                    FSP_PARAMETER_NOT_USED(pvParameters);


                    /* TODO: add your own code here */
                    while(1)
                    {
                                                led_on();
                        vTaskDelay(750);
                                                led_off();
                                                vTaskDelay(750);
                    }
                }

(5)第五步:主函数处理

/* generated main source file - do not edit */
#include "bsp_api.h"
                #include "FreeRTOS.h"
                #include "task.h"
                #include "semphr.h"
                extern void prt_task_create(void);
                extern TaskHandle_t prt_task;
                                extern void led_task_create(void);
                extern TaskHandle_t led_task;
                uint32_t g_fsp_common_thread_count;
                bool g_fsp_common_initialized;
                SemaphoreHandle_t g_fsp_common_initialized_semaphore;
                #if configSUPPORT_STAT IC _ALLOCATION
                StaticSemaphore_t g_fsp_common_initialized_semaphore_memory;
                #endif
                void g_hal_init(void);
                /** Weak reference for tx_err_callback */
                #if defined(__ICCARM__)
                #define rtos_startup_err_callback_WEAK_ATTRIBUTE
                #pragma weak rtos_startup_err_callback = rtos_startup_err_callback_internal
                #elif defined(__GNUC__)
                #define rtos_startup_err_callback_WEAK_ATTRIBUTE __attribute__ ((weak, alias("rtos_startup_err_callback_internal")))
                #endif
                void rtos_startup_err_callback_internal(void * p_instance, void * p_data);
                void rtos_startup_err_callback(void * p_instance, void * p_data) rtos_startup_err_callback_WEAK_ATTRIBUTE;
                /*********************************************************************************************************************
                * @brief This is a weak example initialization error function. It should be overridden by defining a user function
                * with the prototype below.
                * - void rtos_startup_err_callback(void * p_instance, void * p_data)
                *
                * @param[in] p_instance arguments used to identify which instance caused the error and p_data Callback arguments used to identify what error caused the callback.
                **********************************************************************************************************************/
                void rtos_startup_err_callback_internal(void * p_instance, void * p_data);
                void rtos_startup_err_callback_internal(void * p_instance, void * p_data)
{
                    /** Suppress compiler warning for not using parameters. */
                    FSP_PARAMETER_NOT_USED(p_instance);
                    FSP_PARAMETER_NOT_USED(p_data);


                    /** An error has occurred. Please check function arguments for more information. */
                    BSP_CFG_HANDLE_UNRECOVERABLE_ERROR(0);
                }


                void rtos_startup_common_init(void);
                void rtos_startup_common_init(void)
{
                    /* First thread will take care of common initialization. */
                    BaseType_t err;
                    err = xSemaphoreTake(g_fsp_common_initialized_semaphore, portMAX_DELAY);
                    if (pdPASS != err)
                    {
                        /* Check err, problem occurred. */
                        rtos_startup_err_callback(g_fsp_common_initialized_semaphore, 0);
                    }


                    /* Only perform common initialization if this is the first thread to execute. */
                    if (false == g_fsp_common_initialized)
                    {
                        /* Later threads will not run this code. */
                        g_fsp_common_initialized = true;


                        /* Perform common module initialization. */
                        g_hal_init();


                        /* Now that common initialization is done, let other threads through. */
                        /* First decrement by 1 since 1 thread has already come through. */
                        g_fsp_common_thread_count--;
                        while (g_fsp_common_thread_count > 0)
                        {
                            err = xSemaphoreGive(g_fsp_common_initialized_semaphore);
                            if (pdPASS != err)
                            {
                                /* Check err, problem occurred. */
                                rtos_startup_err_callback(g_fsp_common_initialized_semaphore, 0);
                            }
                            g_fsp_common_thread_count--;
                        }
                    }
                }


                int main(void)
{
                    g_fsp_common_thread_count = 0;
                    g_fsp_common_initialized = false;


                    /* Create semaphore to make sure common init is done before threads start running. */
                    g_fsp_common_initialized_semaphore =
                    #if configSUPPORT_STATIC_ALLOCATION
                    xSemaphoreCreateCountingStatic(
                    #else
                    xSemaphoreCreateCounting(
                    #endif
                        256,
                        1
                        #if configSUPPORT_STATIC_ALLOCATION
                        , &g_fsp_common_initialized_semaphore_memory
                        #endif
                    );


                    if (NULL == g_fsp_common_initialized_semaphore) {
                        rtos_startup_err_callback(g_fsp_common_initialized_semaphore, 0);
                    }


                    /* Init RTOS tasks. */
//                    prt_task_create();
led_task_create();


                    /* Start the scheduler. */
                    vTaskStartScheduler();
                    return 0;
                }


                #if configSUPPORT_STATIC_ALLOCATION
                void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer,
                uint32_t *pulIdleTaskStackSize) BSP_WEAK_REFERENCE;
                void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer,
                uint32_t *pulTimerTaskStackSize) BSP_WEAK_REFERENCE;


                /* If configSUPPORT_STATIC_ALLOCATION is set to 1, the application must provide an
                * implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
                * used by the Idle task. */
                void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
                                                    StackType_t **  ppxIdleTaskStackBuffer,
                                                    uint32_t * pulIdleTaskStackSize )
                {
                    /* If the buffers to be provided to the Idle task are declared inside this
                    * function then they must be declared static - otherwise they will be allocated on
                    * the stack and so not exists after this function exits. */
                    static StaticTask_t xIdleTaskTCB;
                    static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];


                    /* Pass out a pointer to the StaticTask_t structure in which the Idle
                    * task's state will be stored. */
                    *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;


                    /* Pass out the array that will be used as the Idle task's stack. */
                    *ppxIdleTaskStackBuffer = uxIdleTaskStack;


                    /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
                    * Note that, as the array is necessarily of type StackType_t,
                    * configMINIMAL_STACK_SIZE is specified in words, not bytes. */
                    *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
                }


                /* If configSUPPORT_STATIC_ALLOCATION is set to 1, the application must provide an
                * implementation of vApplicationGetTimerTaskMemory() to provide the memory that is
                * used by the RTOS daemon/time task. */
                void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
                                                     StackType_t **  ppxTimerTaskStackBuffer,
                                                     uint32_t *      pulTimerTaskStackSize )
                {
                    /* If the buffers to be provided to the Timer task are declared inside this
                    * function then they must be declared static - otherwise they will be allocated on
                    * the stack and so not exists after this function exits. */
                    static StaticTask_t xTimerTaskTCB;
                    static StackType_t uxTimerTaskStack[ configMINIMAL_STACK_SIZE ];


                    /* Pass out a pointer to the StaticTask_t structure in which the Idle
                    * task's state will be stored. */
                    *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;


                    /* Pass out the array that will be used as the Timer task's stack. */
                    *ppxTimerTaskStackBuffer = uxTimerTaskStack;


                    /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
                    * Note that, as the array is necessarily of type StackType_t,
                    * configMINIMAL_STACK_SIZE is specified in words, not bytes. */
                    *pulTimerTaskStackSize = configMINIMAL_STACK_SIZE;
                }
                #endif

(6)第六步:观察结果

图片

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 内存
    +关注

    关注

    8

    文章

    2996

    浏览量

    73862
  • 操作系统
    +关注

    关注

    37

    文章

    6722

    浏览量

    123177
  • FreeRTOS
    +关注

    关注

    12

    文章

    483

    浏览量

    61984
收藏 人收藏

    评论

    相关推荐

    转:开始打怪——FreeRTOS

    FreeRTOS迷你实时操作系统内核。作为
    发表于 08-11 09:49

    十大物联网操作系统介绍

    FreeRTOS:是迷你实时操作系统内核。作为
    发表于 01-16 22:50

    在STM32下完成FreeRTOS的多任务程序开发

    ---------FreeRTOS迷你实时操作系统
    发表于 08-09 07:27

    FreeRTOS的相关资料推荐

    1、FreeRTOS简介FreeRTOS迷你实时操作
    发表于 01-07 08:12

    如何在STM32下完成基于FreeRTOS的多任务程序

    ).FreeRTOS迷你实时操作系统内核。作
    发表于 01-17 07:10

    嵌入式实时操作系统的相关资料分享

    基础知识在嵌入式领域中,采用嵌入式实时操作系统(RTOS)可以更合理、更有效地利用CPU的资源,简化应用软件的设计,缩短系统开发的时间,更好地保证系统
    发表于 01-24 06:44

    FreeRTOS是什么?有何功能呢

    配置6.配置FreeRTOS7.配置STM32CubeMX生成工程文件 8.点击GENERATE CODE生成工程文件四、KEIL程序、前言FreeRTOS
    发表于 02-14 07:54

    什么是FreeRTOS?怎样去移植FreeRTOS

    FreeRTOSFreeRTOS迷你实时操作系统
    发表于 02-23 07:12

    FreeRTOSV8.2.0

    FreeRTOS迷你实时操作系统内核。作为
    发表于 11-09 18:10 15次下载

    freertos中文手册(概念_功能和特点_原理实现)

    FreeRTOS迷你实时操作系统内核。作为
    发表于 04-16 08:51 3.3w次阅读
    <b class='flag-5'>freertos</b>中文手册(概念_功能和特点_原理实现)

    实时操作系统Free RTOS的详细介绍

    实时操作系统Free RTOS 简介 FreeRTOS迷你
    发表于 06-21 14:30 6156次阅读

    STM32下完成基于FreeTROS的多程序任务

    STM32下完成基于FreeTROS的多程序任务、简介二、创建多任务三、烧录及运行四、运行结果五、参考资料、简介FreeRTOS
    发表于 12-07 09:21 1次下载
    STM32下完成基于FreeTROS的多程序任务

    STM32L051使用HAL库操作实例(12)- FreeRTOS系统点亮LED实例

    配置6.配置FreeRTOS7.配置STM32CubeMX生成工程文件 8.点击GENERATE CODE生成工程文件四、KEIL程序、前言FreeRTOS
    发表于 12-09 16:51 7次下载
    STM32L051使用HAL库<b class='flag-5'>操作</b>实例(12)- <b class='flag-5'>FreeRTOS</b><b class='flag-5'>系统</b>点亮LED实例

    FreeRTOS学习(1)——FreeRTOS移植

    FreeRTOSFreeRTOS迷你实时操作系统
    发表于 12-29 19:47 9次下载
    <b class='flag-5'>FreeRTOS</b>学习(1)——<b class='flag-5'>FreeRTOS</b>移植

    FreeRTOS入门学

    ).FreeRTOS迷你实时操作系统内核。作
    发表于 01-17 11:12 19次下载
    <b class='flag-5'>FreeRTOS</b>入门学