[MCU framework][OS layer] FreeRTOS middleware public functions

Publisher:塞上老马Latest update time:2022-09-19 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Repackage the freertos function to adapt to RTX4 /RTX5 / FREERTOS_NRF_RTC.


FreeRTOS is a class of RTOS designed to be small enough to run on microcontrollers—although its use is not limited to microcontroller applications.


A microcontroller is a small, resource-constrained processor that integrates the processor itself, read-only memory (ROM or flash memory) to hold the program to be executed, and random access memory (RAM) required for the program to execute on a single chip. Typically, the program is executed directly from the read-only memory.


Microcontrollers are used in deeply embedded applications (those where you never actually see the processor itself or the software it runs), which often have very specific and specialized jobs to do. Size constraints and the specialized end-application nature rarely warrant the use of a full RTOS implementation - or indeed make it possible. Therefore, FreeRTOS only provides core real-time scheduling functionality, inter-task communication, timing, and synchronization primitives. This means it is more accurately described as a real-time kernel or real-time executive. Additional functionality, such as a command console interface or a network stack, can be included in add-ons.


Why FreeRTOS?

Trusted Kernel

With its proven robustness, tiny footprint, and broad device support, the FreeRTOS kernel is trusted by the world's leading companies as the de facto standard for microcontrollers and small microprocessors.


Accelerate time to market

With detailed preconfigured demos and Internet of Things (IoT) reference integrations, there's no need to figure out how to set up a project. Download, compile quickly, and get to market faster.


Broad ecosystem support

Our partner ecosystem offers a broad range of choices, including community contributions, professional support, and integrated IDEs and productivity tools.


Predictability of long-term support

FreeRTOS provides functional stability through long-term support (LTS) releases. FreeRTOS LTS libraries provide two years of security updates and critical bug fixes. They are maintained by AWS for the benefit of the FreeRTOS community.


feature

Small and power-efficient core

Scalable in size, with usable program memory footprint as low as 9KB. Some architectures include tickless power saving modes


Supports more than 40 architectures

One code base for more than 40 MCU architectures and more than 15 toolchains, including the latest RISC-V and ARMv8-M (Arm Cortex-M33) microcontrollers


Modular library

A growing library of add-on libraries for all industry sectors, including secure on-premises or cloud connections


AWS Reference Integration

Take advantage of tested samples that include all the necessary libraries to connect securely to the cloud


MIT license, with options

FreeRTOS is available for any purpose under its MIT license. Commercial licenses and security certifications are also available from our strategic partners.


Official website address: https://www.freertos.org/

FreeRTOS Kernel Quick Start Guide: https://www.freertos.org/FreeRTOS-quick-start-guide.html


/********************************************************************************

* @file    os_api.c

* @author jianqiang.xue

* @Version V1.0.0

* @Date    2021-04-03

* @brief   

********************************************************************************/


#include

#include

#include

#include "cmsis_os2.h"

#include "os_api.h"


#include "FreeRTOS.h"

#include "task.h"

#include "queue.h"


#define IS_IRQ_MASKED()           (__get_PRIMASK() != 0U)

#define IS_IRQ_MODE()             (__get_IPSR() != 0U)

#define IS_IRQ()                  (IS_IRQ_MODE() || (IS_IRQ_MASKED() && (KernelState == osKernelRunning)))

#define MAX_BITS_TASK_NOTIFY      31U

#define THREAD_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_TASK_NOTIFY)  - 1U))

/* Kernel initialization state */

static osKernelState_t KernelState = osKernelInactive;

/**************************************OS_KERNEL************ ************************/

os_status os_kernel_initialize(void)

{

    return (os_status)osKernelInitialize();

}


os_status os_kernel_start(void)

{

    return (os_status)osKernelStart();

}


os_status os_kernel_lock(void)

{

    return (os_status)osKernelLock();

}


os_status os_kernel_unlock(void)

{

    return (os_status)osKernelUnlock();

}


os_status os_delay(uint32_t ms)

{

    return (os_status)osDelay(ms);

}


uint32_t os_get_tick(void)

{

    return osKernelGetTickCount();

}


/************************************OS_THREAD************************************/

os_thread_id os_thread_create(const os_thread_def_t *thread_def, void *arg)

{

    if (thread_def == NULL)

    {

        return NULL;

    }

    osThreadAttr_t attr = {0};

    attr.name = arg;

    attr.priority = osPriorityLow;

    if (thread_def->tpriority == OS_PRIORITY_LOW)

    {

        attr.priority = osPriorityLow;

    }

    else if (thread_def->tpriority == OS_PRIORITY_NORMAL)

    {

        attr.priority = osPriorityNormal;

    }

    else if (thread_def->tpriority == OS_PRIORITY_ABOVENORMAL)

    {

        attr.priority = osPriorityAboveNormal;

    }

    else if (thread_def->tpriority == OS_PRIORITY_HIGH)

    {

        attr.priority = osPriorityHigh;

    }

    else if (thread_def->tpriority == OS_PRIORITY_REALTIME)

    {

        attr.priority = osPriorityRealtime;

    }

    else

    {

        attr.priority = osPriorityLow;

    }

    attr.stack_size = thread_def->stacksize;

    attr.attr_bits = osThreadDetached;

    return (os_thread_id)osThreadNew((osThreadFunc_t)thread_def->pthread, arg, &attr);

}


/************************************OS_TIMER************ ************************/

os_timer_id os_timer_create(const os_timer_def_t *timer_def, os_timer_t type, void *arg)

{

    return osTimerNew((osTimerFunc_t)timer_def->ptimer, (osTimerType_t)type, arg, NULL);

}


os_status os_timer_start(os_timer_id timer_id, uint32_t millisec)

{

    return (os_status)osTimerStart(timer_id, millisec);

}


os_status os_timer_stop(os_timer_id timer_id)

{

    return (os_status)osTimerStop(timer_id);

}


/************************************OS_MAIL************ ************************/

os_mail_qid os_mail_create(const os_mailq_def_t *queue_def, os_thread_id thread_id)

{

    return (os_mail_qid)osMessageQueueNew(queue_def->queue_sz, queue_def->item_sz, NULL);

}


void *os_mail_alloc(os_mail_qid queue_id, uint32_t millisec)

{

    return NULL;

}


void *os_mail_clean_and_alloc(os_mail_qid queue_id, uint32_t millisec)

{

    return os_mail_alloc(queue_id, millisec);

}


os_status os_mail_put(os_mail_qid queue_id, void *mail)

{

    return (os_status)osMessageQueuePut((osMessageQueueId_t)queue_id, mail, NULL, NULL);

}


os_event os_mail_get(os_mail_qid queue_id, uint32_t millisec, void *arg)

{

    osStatus_t status;

    os_event event_t;

    status = osMessageQueueGet((osMessageQueueId_t)queue_id, arg, NULL, millisec);

    event_t.status = (os_status)status;

    event_t.def.message_id = (os_message_qid)queue_id;

    event_t.value.p = arg;

    return event_t;

}


os_status os_mail_free(os_mail_qid queue_id, void *mail)

{

    return (os_status)0;

}


/************************************OS_POOL************ ************************/

os_pool_id os_pool_create(const os_pool_def_t *pool_def)

{

    return (os_pool_id)osMemoryPoolNew(pool_def->pool_sz, pool_def->item_sz, NULL);

}


void *os_pool_alloc(os_pool_id pool_id)

{

    return osMemoryPoolAlloc((osMemoryPoolId_t)pool_id, 0);

}


void *os_pool_calloc(os_pool_id pool_id)

{

   return os_pool_alloc(pool_id);

}


os_status os_pool_free(os_pool_id pool_id, void *block)

{

    return (os_status)osMemoryPoolFree((osMemoryPoolId_t)pool_id, block);

}

/**************************************OS_MSG_QUEUE************ ************************/

os_message_qid os_message_create(const os_messageq_def_t *queue_def, os_thread_id thread_id)

{

    return (os_message_qid)osMessageQueueNew(queue_def->queue_sz, 4, &(queue_def->attr));

}


os_status os_message_put(os_message_qid queue_id, uint32_t info, uint32_t millisec)

{

    return (os_status)osMessageQueuePut((osMessageQueueId_t)queue_id, (const void *)&info, 0, millisec); // Send Message

}


os_event os_message_get(os_message_qid queue_id, uint32_t millisec)

{

    QueueHandle_t hQueue = (QueueHandle_t)queue_id;

    BaseType_t yield;

    uint32_t *msg_ptr;

    os_event event;


    event.status = OS_OK;

    if (IS_IRQ())

    {

        if ((hQueue == NULL) || (millisec != 0U))

        {

            event.status = OS_ERROR_PARAMETER;

        }

        else

        {

            yield = false;

            if (xQueueReceiveFromISR(hQueue, &msg_ptr, &yield) != true)

            {

                event.status = OS_ERROR_RESOURCE;

            }

            else

            {

                event.status = OS_EVENT_MESSAGE;

                event.value.p = (void *)msg_ptr;

                portYIELD_FROM_ISR(yield);

            }

        }

    }

    else

    {

        if (hQueue == NULL)

        {

            event.status = OS_ERROR_PARAMETER;

        }

        else

        {

            if (xQueueReceive(hQueue, &msg_ptr, (TickType_t)millisec) != true)

            {

                if (millisec != 0U)

                {

                    event.status = OS_EVENT_TIMEOUT;

                }

                else

                {

                    event.status = OS_ERROR_RESOURCE;

                }

            }

            else

            {

                event.status = OS_EVENT_MESSAGE;

                event.value.p = (void *)msg_ptr;

                //LOG_D("os_msg_get:0x%x|0x%x", queue_id, msg_ptr);

            }

        }

    }


    return event;

}


uint8_t os_message_get_space(os_message_qid queue_id)

{

    return (uint8_t)osMessageQueueGetSpace(queue_id);

}


uint8_t os_message_get_count(os_message_qid queue_id)

{

    return (uint8_t)osMessageQueueGetCount(queue_id);

}


/**************************************OS_SIGNAL************ ************************/

int32_t isr_signal_set(os_thread_id thread_id, int32_t signals)

{

    return osThreadFlagsSet((osThreadId_t)thread_id, signals);

}


int32_t os_signal_set(os_thread_id thread_id, int32_t signals)

{

    return osThreadFlagsSet((osThreadId_t)thread_id, signals);

[1] [2] [3]
Reference address:[MCU framework][OS layer] FreeRTOS middleware public functions

Previous article:[MCU framework][OS layer] freertos_rtc middleware public function
Next article:[MCU framework][OS layer] RTX5 middleware public functions

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号