FreeRtos和ucos的区别?先从那个入手好?

本人有的资料是ucos的,考试也考,但freertos好像是开源免费的,对于后期做出产品,版权方便。我应该先学习哪个?
关注者
53
被浏览
175,518

12 个回答

FreeRTOS一直在演进一直在发展,且周边的组件丰富,另外就是mcu厂家基本上标配,因为开源,所以他的生命力足够强也很包容,目前作者已经加入亚马逊IoT事业部,相信在物联网时代会大放光彩。

ucos算是比较中规中矩的代表,靠版权挣钱,另外ucosii过了足够多的安全认证,但是外围组件都需要额外授权费,比较吃力。好处是国内中文资料足够多,源码分析的也多,所以比较适合学习,但是说到用仅仅是个kernel在今天能干嘛呢。

我了解到去年ucos的母公司micrium已经被siliconlab收购,归到IoT相关部门,所以未来怎么干还得观望。

我个人推荐你用FreeRTOS,足够好用也易用,第一步是用起来,后面才是感悟体会和深入其中。

> 有关FreeRTOS中的函数后面都会讲到,前面章节部分主要是说一下原理,看的时候比较乏味,了解即可,没必要较真

1-简介

FreeRTOS流和缓冲区是任务和任务之间、中断和任务之间的通信原语。和其他多数FreeRTOS通信原语不同,他们针对读和写的单一性进行了方案的优化工作。例如将数据从中断服务例程传递到任务,或在双核cpu上从一个微控制器内核传递到另一个微控制器内核。数据通过拷贝传递——发送端将数据复制到缓冲区,读取端将数据复制出缓冲区。 流缓冲区传递连续的字节流。消息缓冲区传递大小可变但离散的消息。消息缓冲区使用流缓冲区进行数据传输。 在FreeRTOS对象是惟一的,流缓冲区的实现(也是消息缓冲区的实现,因为消息缓冲区是建立在流缓冲区之上的)假设只有一个任务或中断将写入缓冲区(写入器),并且仅有一个任务或中断将从缓冲区读取(读取器)。对不同的任务或者中断写入或者读取是安全的,但是有多个不同的写入或者读取是不安全的。若有多个不同的写入器,那么程序写入必须每次对写入的API函数(例如xStreamBufferSend())的调用都放在临界区中,并将发送阻塞时间设置为0。同样,如果有多个不同的读取器,那么应用程序编写器必须将每次对读取API函数(如xStreamBufferReceive())的调用都放在临界区中,并使用0的接收块时间。

2-中断服务到任务流

2.1 介绍

流缓冲区允许一个字节流从中断服务程序传递到一个任务,或者从一个任务传递到另一个任务。字节流可以是任意长度的,而且不一定有开头或结尾。一次可以写入任意数量的字节,一次可以读取任意数量的字节。数据通过复制传递——发送端将数据复制到缓冲区,读取端将数据复制出缓冲区。 与大多数其他FreeRTOS通信原语不同,流缓冲区是针对单读单写场景进行优化的,例如将数据从中断服务程序传递到任务,或在双核CPU上从一个微控制器内核传递到另一个微控制器内核。 在FreeRTOS/source/stream_buffer.c源文件来启用流缓冲区功能。可自行查阅。 流缓冲区的实现使用了任务的通知。因此,调用将调用任务置于阻塞状态的流缓冲区API函数可以更改调用任务的通知状态和值。

2.2 快速入门

在reeRTOS/Demo/Common/Minimal/StreamBufferInterrupt.c源文件 提供了一个示例,说明从中断服务程序到任务使用流缓冲区传递数据的例程。在这里我给大家复制过来了。

/*
 * FreeRTOS V202112.00
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * http://www.FreeRTOS.org
 * http://aws.amazon.com/freertos
 * 1 tab == 4 spaces!
 * A simple example that shows a stream buffer being used to pass data from an
 * interrupt to a task.
 * There are two strings, pcStringToSend and pcStringToReceive, where
 * pcStringToReceive is a substring of pcStringToSend.  The interrupt sends
 * a few bytes of pcStringToSend to a stream buffer ever few times that it
 * executes.  A task reads the bytes from the stream buffer, looking for the
 * substring, and flagging an error if the received data is invalid.
/* Standard includes. */
#include "stdio.h"
#include "string.h"
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "stream_buffer.h"
/* Demo app includes. */
#include "StreamBufferInterrupt.h"
#define sbiSTREAM_BUFFER_LENGTH_BYTES        ( ( size_t ) 100 )
#define sbiSTREAM_BUFFER_TRIGGER_LEVEL_10    ( ( BaseType_t ) 10 )
/*-----------------------------------------------------------*/
/* Implements the task that receives a stream of bytes from the interrupt. */
static void prvReceivingTask( void * pvParameters );
/*-----------------------------------------------------------*/
/* The stream buffer that is used to send data from an interrupt to the task. */
static StreamBufferHandle_t xStreamBuffer = NULL;
/* The string that is sent from the interrupt to the task four bytes at a
 * time.  Must be multiple of 4 bytes long as the ISR sends 4 bytes at a time*/
static const char * pcStringToSend = "_____Hello FreeRTOS_____";
/* The string to task is looking for, which must be a substring of
 * pcStringToSend. */
static const char * pcStringToReceive = "Hello FreeRTOS";
/* Set to pdFAIL if anything unexpected happens. */
static BaseType_t xDemoStatus = pdPASS;
/* Incremented each time pcStringToReceive is correctly received, provided no
 * errors have occurred.  Used so the check task can check this task is still
 * running as expected. */
static uint32_t ulCycleCount = 0;
/*-----------------------------------------------------------*/
void vStartStreamBufferInterruptDemo( void )
    /* Create the stream buffer that sends data from the interrupt to the
     * task, and create the task. */
    xStreamBuffer = xStreamBufferCreate( /* The buffer length in bytes. */
        sbiSTREAM_BUFFER_LENGTH_BYTES,
        /* The stream buffer's trigger level. */
        sbiSTREAM_BUFFER_TRIGGER_LEVEL_10 );
    xTaskCreate( prvReceivingTask,         /* The function that implements the task. */
                 "StrIntRx",               /* Human readable name for the task. */
                 configMINIMAL_STACK_SIZE, /* Stack size (in words!). */
                 NULL,                     /* Task parameter is not used. */
                 tskIDLE_PRIORITY + 2,     /* The priority at which the task is created. */
                 NULL );                   /* No use for the task handle. */
/*-----------------------------------------------------------*/
static void prvReceivingTask( void * pvParameters )
    char cRxBuffer[ 20 ];
    BaseType_t xNextByte = 0;
    /* Remove warning about unused parameters. */
    ( void ) pvParameters;
    /* Make sure the string will fit in the Rx buffer, including the NULL
     * terminator. */
    configASSERT( sizeof( cRxBuffer ) > strlen( pcStringToReceive ) );
    /* Make sure the stream buffer has been created. */
    configASSERT( xStreamBuffer != NULL );
    /* Start with the Rx buffer in a known state. */
    memset( cRxBuffer, 0x00, sizeof( cRxBuffer ) );
    for( ; ; )
        /* Keep receiving characters until the end of the string is received.
         * Note:  An infinite block time is used to simplify the example.  Infinite
         * block times are not recommended in production code as they do not allow
         * for error recovery. */
        xStreamBufferReceive( /* The stream buffer data is being received from. */
            xStreamBuffer,
            /* Where to place received data. */
            ( void * ) &( cRxBuffer[ xNextByte ] ),
            /* The number of bytes to receive. */
            sizeof( char ),
            /* The time to wait for the next data if the buffer
             * is empty. */
            portMAX_DELAY );
        /* If xNextByte is 0 then this task is looking for the start of the
         * string, which is 'H'. */
        if( xNextByte == 0 )
            if( cRxBuffer[ xNextByte ] == 'H' )
                /* The start of the string has been found.  Now receive
                 * characters until the end of the string is found. */
                xNextByte++;
            /* Receiving characters while looking for the end of the string,
             * which is an 'S'. */
            if( cRxBuffer[ xNextByte ] == 'S' )
                /* The string has now been received.  Check its validity. */
                if( strcmp( cRxBuffer, pcStringToReceive ) != 0 )
                    xDemoStatus = pdFAIL;
                /* Return to start looking for the beginning of the string
                 * again. */
                memset( cRxBuffer, 0x00, sizeof( cRxBuffer ) );
                xNextByte = 0;
                /* Increment the cycle count as an indication to the check task
                 * that this demo is still running. */
                if( xDemoStatus == pdPASS )
                    ulCycleCount++;
                /* Receive the next character the next time around, while
                 * continuing to look for the end of the string. */
                xNextByte++;
                configASSERT( ( size_t ) xNextByte < sizeof( cRxBuffer ) );
/*-----------------------------------------------------------*/
void vBasicStreamBufferSendFromISR( void )
    static size_t xNextByteToSend = 0;
    const BaseType_t xCallsBetweenSends = 100, xBytesToSend = 4;
    static BaseType_t xCallCount = 0;
    /* Is it time to write to the stream buffer again? */
    xCallCount++;
    if( xCallCount > xCallsBetweenSends )
        xCallCount = 0;
        /* Send the next four bytes to the stream buffer. */
        xStreamBufferSendFromISR( xStreamBuffer,
                                  ( const void * ) ( pcStringToSend + xNextByteToSend ),
                                  xBytesToSend,
                                  NULL );
        /* Send the next four bytes the next time around, wrapping to the start
         * of the string if necessary. */
        xNextByteToSend += xBytesToSend;
        if( xNextByteToSend >= strlen( pcStringToSend ) )
            xNextByteToSend = 0;
/*-----------------------------------------------------------*/
BaseType_t xIsInterruptStreamBufferDemoStillRunning( void )
    uint32_t ulLastCycleCount = 0;
    /* Check the demo is still running. */
    if( ulLastCycleCount == ulCycleCount )
        xDemoStatus = pdFAIL;
        ulLastCycleCount = ulCycleCount;
    return xDemoStatus;
}

2.2 阻塞读取和触发电平

xStreamBufferReceive()用于读取来自 RTOS 任务的流缓冲区的数据。xStreamBufferReceiveFromISR())是 用于从中断服务例程 (ISR) 从流缓冲区中读取数据。 下面是这两个函数的用法举例:

//1-xStreamBufferReceive()
void vAFunction( StreamBuffer_t xStreamBuffer )
uint8_t ucRxData[ 20 ];
size_t xReceivedBytes;
const TickType_t xBlockTime = pdMS_TO_TICKS( 20 );
    /* Receive up to another sizeof( ucRxData ) bytes from the stream buffer.
    Wait in the Blocked state (so not using any CPU processing time) for a
    maximum of 100ms for the full sizeof( ucRxData ) number of bytes to be
    available. */
    xReceivedBytes = xStreamBufferReceive( xStreamBuffer,
                                           ( void * ) ucRxData,
                                           sizeof( ucRxData ),
                                           xBlockTime );
    if( xReceivedBytes > 0 )
        /* A ucRxData contains another xRecievedBytes bytes of data, which can
        be processed here.... */
2--xStreamBufferReceiveFromISR()
/* A stream buffer that has already been created. */
StreamBuffer_t xStreamBuffer;
void vAnInterruptServiceRoutine( void )
uint8_t ucRxData[ 20 ];
size_t xReceivedBytes;
BaseType_t xHigherPriorityTaskWoken = pdFALSE;  /* Initialised to pdFALSE. */
    /* Receive the next stream from the stream buffer. */
    xReceivedBytes = xStreamBufferReceiveFromISR( xStreamBuffer,
                                                  ( void * ) ucRxData,
                                                  sizeof( ucRxData ),
                                                  &xHigherPriorityTaskWoken );
    if( xReceivedBytes > 0 )
        /* ucRxData contains xReceivedBytes read from the stream buffer.
        Process the stream here.... */
    /* If xHigherPriorityTaskWoken was set to pdTRUE inside
    xStreamBufferReceiveFromISR() then a task that has a priority above the
    priority of the currently executing task was unblocked and a context
    switch should be performed to ensure the ISR returns to the unblocked