学习一下freertos stream buffer
最近在自己从0到1写rtos,主要是为了学习rtos,并没有其他卵用,光学会调API是不够的,看源码又恼火,所以学习的最好办法还是根据自己的理解自己动手撸代码,虽然大部分思路都是抄的freertos或者rt thread(主要是抄思路,代码还是自己按理解写的),抄完调通的时候rtos也就基本掌握了。
- 废话不多说,直接分析代码吧,先从StreamBuffer_t这个控制块或者说handle
说起。
typedef struct StreamBufferDef_t /*lint !e9058 Style convention uses tag. */
{
volatile size_t xTail; /* 相当于队列尾部,从这取数据. */
volatile size_t xHead; /* 队列头,从这个地址存数据. */
size_t xLength; /* The length of the buffer pointed to by pucBuffer. */
size_t xTriggerLevelBytes; /* 存多少个bytes数据后通知消费者来取. */
volatile TaskHandle_t xTaskWaitingToReceive; /* 因为接收失败而挂起的任务,由于是一对一的方式,所以不需要链表 */
volatile TaskHandle_t xTaskWaitingToSend; /*同上 */
uint8_t *pucBuffer; /* 存放数据的内存*/
uint8_t ucFlags;
} StreamBuffer_t;
- 接下来就是喜闻乐见的分析xStreamBufferGenericCreateStatic这个函数了,因为我通常用静态内存分配,所以是这个函数。
StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
size_t xTriggerLevelBytes,
BaseType_t xIsMessageBuffer,
uint8_t * const pucStreamBufferStorageArea,
StaticStreamBuffer_t * const pxStaticStreamBuffer )
{
StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer; /*lint !e740 !e9087 Safe cast as StaticStreamBuffer_t is opaque Streambuffer_t. */
StreamBufferHandle_t xReturn;
uint8_t ucFlags;
- StaticStreamBuffer_t StreamBuffer_t 其实是一样的(自己跳转看定义),StreamBufferHandle_t是上面两个的指针,不知道freertos搞这么多定义干什么,跳来跳去,一点也不干净。
if( xTriggerLevelBytes == ( size_t ) 0 )
{
xTriggerLevelBytes = (</

本文详细解析了FreeRTOS中的StreamBuffer_t控制块,探讨了StreamBufferGenericCreateStatic函数,重点讲解了静态内存分配和StreamBuffer的初始化、重置、触发级别设置。通过实例展示了如何使用StreamBuffer实现一对一的任务同步与数据通信。
1300

被折叠的 条评论
为什么被折叠?



