0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

FreeRTOS:一個迷你的實時操作系統(tǒng)內(nèi)核

科技綠洲 ? 來源:面包板社區(qū) ? 作者:面包板社區(qū) ? 2023-06-29 17:15 ? 次閱讀

** 1、FreeRTOS**

FreeRTOS是一個迷你的實時操作系統(tǒng)內(nèi)核。作為一個輕量級的操作系統(tǒng),功能包括:任務(wù)管理、時間管理、信號量、消息隊列、內(nèi)存管理、記錄功能、軟件定時器、協(xié)程等,可基本滿足較小系統(tǒng)的需要。由于RTOS需占用一定的系統(tǒng)資源(尤其是RAM資源),只有μC/OS-II、embOS、salvo、FreeRTOS等少數(shù)實時操作系統(tǒng)能在小RAM單片機上運行。相對μC/OS-II、embOS等商業(yè)操作系統(tǒng),F(xiàn)reeRTOS操作系統(tǒng)是完全免費的操作系統(tǒng),具有源碼公開、可移植、可裁減、調(diào)度策略靈活的特點,可以方便地移植到各種單片機上運行。

2、移植

(1)第一步:創(chuàng)建工程

修改時鐘

圖片

(2)第二步:LED配置

圖片

(3)第三步:FreeRTOS配置

FreeRTOS公共部分配置

圖片

LED線程配置

圖片

(4)第四部:任務(wù)入口函數(shù)編寫

#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)第五步:主函數(shù)處理

/* 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)第六步:觀察結(jié)果

圖片

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 內(nèi)存
    +關(guān)注

    關(guān)注

    8

    文章

    2902

    瀏覽量

    73534
  • 操作系統(tǒng)
    +關(guān)注

    關(guān)注

    37

    文章

    6544

    瀏覽量

    122728
  • FreeRTOS
    +關(guān)注

    關(guān)注

    12

    文章

    483

    瀏覽量

    61721
收藏 人收藏

    評論

    相關(guān)推薦

    轉(zhuǎn):開始打怪——FreeRTOS

    FreeRTOS迷你實時操作系統(tǒng)內(nèi)核。作為
    發(fā)表于 08-11 09:49

    十大物聯(lián)網(wǎng)操作系統(tǒng)介紹

    FreeRTOS:是迷你實時操作系統(tǒng)內(nèi)核。作為
    發(fā)表于 01-16 22:50

    在STM32下完成FreeRTOS的多任務(wù)程序開發(fā)

    ---------FreeRTOS迷你實時操作系統(tǒng)內(nèi)
    發(fā)表于 08-09 07:27

    FreeRTOS的相關(guān)資料推薦

    1、FreeRTOS簡介FreeRTOS迷你實時操作
    發(fā)表于 01-07 08:12

    如何在STM32下完成基于FreeRTOS的多任務(wù)程序

    ).FreeRTOS迷你實時操作系統(tǒng)內(nèi)核。作
    發(fā)表于 01-17 07:10

    嵌入式實時操作系統(tǒng)的相關(guān)資料分享

    基礎(chǔ)知識在嵌入式領(lǐng)域中,采用嵌入式實時操作系統(tǒng)(RTOS)可以更合理、更有效地利用CPU的資源,簡化應(yīng)用軟件的設(shè)計,縮短系統(tǒng)開發(fā)的時間,更好地保證系統(tǒng)
    發(fā)表于 01-24 06:44

    FreeRTOS是什么?有何功能呢

    配置6.配置FreeRTOS7.配置STM32CubeMX生成工程文件 8.點擊GENERATE CODE生成工程文件四、KEIL程序、前言FreeRTOS
    發(fā)表于 02-14 07:54

    什么是FreeRTOS?怎樣去移植FreeRTOS

    FreeRTOSFreeRTOS迷你實時操作系統(tǒng)
    發(fā)表于 02-23 07:12

    FreeRTOSV8.2.0

    FreeRTOS迷你實時操作系統(tǒng)內(nèi)核。作為
    發(fā)表于 11-09 18:10 ?15次下載

    freertos中文手冊(概念_功能和特點_原理實現(xiàn))

    FreeRTOS迷你實時操作系統(tǒng)內(nèi)核。作為
    發(fā)表于 04-16 08:51 ?3.3w次閱讀
    <b class='flag-5'>freertos</b>中文手冊(概念_功能和特點_原理實現(xiàn))

    實時操作系統(tǒng)Free RTOS的詳細介紹

    實時操作系統(tǒng)Free RTOS 簡介 FreeRTOS迷你
    發(fā)表于 06-21 14:30 ?5976次閱讀

    STM32下完成基于FreeTROS的多程序任務(wù)

    STM32下完成基于FreeTROS的多程序任務(wù)、簡介二、創(chuàng)建多任務(wù)三、燒錄及運行四、運行結(jié)果五、參考資料、簡介FreeRTOS
    發(fā)表于 12-07 09:21 ?1次下載
    STM32下完成基于FreeTROS的多程序任務(wù)

    STM32L051使用HAL庫操作實例(12)- FreeRTOS系統(tǒng)點亮LED實例

    配置6.配置FreeRTOS7.配置STM32CubeMX生成工程文件 8.點擊GENERATE CODE生成工程文件四、KEIL程序、前言FreeRTOS
    發(fā)表于 12-09 16:51 ?7次下載
    STM32L051使用HAL庫<b class='flag-5'>操作</b>實例(12)- <b class='flag-5'>FreeRTOS</b><b class='flag-5'>系統(tǒng)</b>點亮LED實例

    FreeRTOS學習(1)——FreeRTOS移植

    FreeRTOSFreeRTOS迷你實時操作系統(tǒng)
    發(fā)表于 12-29 19:47 ?9次下載
    <b class='flag-5'>FreeRTOS</b>學習(1)——<b class='flag-5'>FreeRTOS</b>移植

    FreeRTOS入門學

    ).FreeRTOS迷你實時操作系統(tǒng)內(nèi)核。作
    發(fā)表于 01-17 11:12 ?19次下載
    <b class='flag-5'>FreeRTOS</b>入門學