stm32&freertos&kfifo(巧夺天工)

所长 所长     2022-08-22     110

关键词:

/**********************************************************
  *
  *  文件名:       kfifo.c
  *
  *  文件描述:     该文件包含的kfifo的处理函数
  *
  *  创建人:       GXP
  *
  *   创建日期:     2016年8月9日16:13:06
  *
  *  版本号:      1.0
  *
  *  修改记录:    无
  *   
  *   本文参考博客地址: http://blog.csdn.net/linyt/article/details/5764312
  *                   http://blog.csdn.net/chen19870707/article/details/39899743
  *
***********************************************************/


#include "kfifo.h"

#include "FreeRTOS.h"
#include "task.h"


#define min(a, b)                (((a) < (b)) ? (a) : (b))



//找出最接近 最大2的指数次幂
unsigned int roundup_pow_of_two(unsigned int date_roundup_pow_of_two )
{            
    /* 这里采用 STM32 硬件提供的计算前导零指令 CLZ
     * 举个例子,假如变量date_roundup_pow_of_two 0x09
     *(二进制为:0000 0000 0000 0000 0000 0000 0000 1001), 即bit3和bit0为1
     * 则__clz( (date_roundup_pow_of_two)的值为28,即最高位1 前面有28个0,32-28 =3 代表最高位1 的 位置
     * 31UL 表示 无符号 int 数字 31,否则默认为 有符号 int 数字 31
     * 这里参考  FreeRTOS 的 寻找高级优先级任务 的写法,详细解释到朱工博客
     * 博客地址: http://blog.csdn.net/zhzht19861011/article/details/51418383
     */

    return ( 1UL << ( 32UL - ( unsigned int ) __clz( (date_roundup_pow_of_two) ) ) );

}


/* 
 * 每次调用这个函数都会产生 两个内存块,一个内存块指向struct KFIFO,一个指向 KFIFO.buff
 * 因此 如果这两个内存块不在使用请释放掉!GXP,2016年8月17日12:55:54
 */

struct KFIFO *kfifo_alloc(unsigned int size) 
{   
    unsigned char *buffer;
    
    struct KFIFO *ret;
    
        ret=(struct KFIFO *) pvPortMalloc(sizeof (struct KFIFO));

    /*  
     * round up to the next power of 2, since our ‘let the indices  
     * wrap‘ tachnique works only in this case.  
         * 如果size 是2的 次幂圆整,则 size & (size - 1)  =0
     */
  
    if (size & (size - 1)) 
        {   
            //        BUG_ON(size > 0x80000000);  
            
            //如果你要申请的buffer 不是 2的 次幂圆整,就要把 size 变成 2的次幂圆整 ,方便下面计算
        size = roundup_pow_of_two(size);
    }
        
        //这里使用 FreeRTOS的 分配内存的 API
    buffer = (unsigned char*) pvPortMalloc(size);
        
    if (!buffer)   //如果返回的值为NULL,这说明分配内存失败
        return 0UL;
  
        //    ret = kfifo_init(buffer, size, lock);   
        
        ret->buffer=buffer;
        ret->size  =size;
        ret->in  = 0;
        ret->out = 0;
        
    if (!ret) //如果ret的值为NULL,这说明分配内存失败
        vPortFree(buffer); //释放之前分配的 内存空间
  
    return ret;
        
}


unsigned int __kfifo_put(struct KFIFO *fifo, unsigned char *buffer, unsigned int len)   
{
    unsigned int L;
    
        //环形缓冲区的剩余容量为fifo->size - fifo->in + fifo->out,让写入的长度取len和剩余容量中较小的,避免写越界;
    len = min( len , fifo->size - fifo->in + fifo->out );
  
    /*  
     * Ensure that we sample the fifo->out index -before- we  
     * start putting bytes into the kfifo.  
     */   
                //多处理器 处理内存 的 屏障,STM32不需要这个
                //    smp_mb(); 
  
    /* first put the data starting from fifo->in to buffer end */
                /* 首先将数据从fifo.in 所在的位置开始写,写之前,首先要看一下fifo->in到 buffer 末尾的大小 是不是 比 len 大*/
    
                /*
                 * 前面讲到fifo->size已经2的次幂圆整,主要是方便这里计算,提升效率
             * 在对10进行求余的时候,我们发现,余数总是整数中的个位上的数字,而不用管其他位是什么;
                 * 所以,kfifo->in % kfifo->size 可以转化为 kfifo->in & (kfifo->size – 1),效率会提升
                 * 所以fifo->size - (fifo->in & (fifo->size - L)) 即位 fifo->in 到 buffer末尾所剩余的长度,
                 * L取len和剩余长度的最小值,即为需要拷贝L 字节到fifo->buffer + fifo->in的位置上。
                 */ 
    L = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
    
    memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, L);   
  
    /* then put the rest (if any) at the beginning of the buffer */ 

    memcpy(fifo->buffer, buffer + L, len - L);
  
    /*  
     * Ensure that we add the bytes to the kfifo -before-  
     * we update the fifo->in index.  
     */   
  
      // smp_wmb();   //多处理器 处理内存 的 屏障,STM32不需要这个    

            /* 
             * 注意这里 只是用了 fifo->in +=  len而未取模,
             * 这就是kfifo的设计精妙之处,这里用到了unsigned int的溢出性质,
             * 当in 持续增加到溢出时又会被置为0,这样就节省了每次in向前增加都要取模的性能,
             * 锱铢必较,精益求精,让人不得不佩服。
             */
  
    fifo->in += len; 
        
      /*返回值 代表  写入数据的个数 ,这样 就可以根据返回值 判断缓冲区是否写满*/
    return len;   
}  
  
unsigned int __kfifo_get(struct KFIFO *fifo, unsigned char *buffer, unsigned int len)   
{
    unsigned int L;   
  
    len = min(len, fifo->in - fifo->out);   
  
    /*  
     * Ensure that we sample the fifo->in index -before- we  
     * start removing bytes from the kfifo.  
     */   
  
    //smp_rmb();    //多处理器 处理内存 的 屏障,STM32不需要这个
  
    /* first get the data from fifo->out until the end of the buffer */   
    L = min(len, fifo->size - (fifo->out & (fifo->size - 1)));   
    memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)), L);   
  
    /* then get the rest (if any) from the beginning of the buffer */   
    memcpy(buffer + L, fifo->buffer, len - L);   
  
    /*  
     * Ensure that we remove the bytes from the kfifo -before-  
     * we update the fifo->out index.  
     */   
  
    //smp_mb();   //多处理器 处理内存 的 屏障,STM32不需要这个
            
            /*
           * 注意这里 只是用了 fifo->out +=  len 也未取模运算,
             * 同样unsigned int的溢出性质,当out 持续增加到溢出时又会被置为0,
             * 如果in先溢出,出现 in  < out 的情况,那么 in – out 为负数(又将溢出),
             * in – out 的值还是为buffer中数据的长度。
             */

    fifo->out += len;
  
    return len;  
}

 

 

kfifo.h

 

 

#ifndef _KFIFO_H_
#define _KFIFO_H


//声明 一个 结构体 kfifo

struct KFIFO
{   
    unsigned char *buffer;    /* the buffer holding the data */   
    unsigned int size;            /* the size of the allocated buffer */   
    unsigned int in;                /* data is added at offset (in % size) */   
    unsigned int out;                /* data is extracted from off. (out % size) */   
        /*STM32 只有一个核心,同一时刻只能写或者读,因此不需要*/
        //    volatile unsigned int *lock; /* protects concurrent modifications */  
};

unsigned int roundup_pow_of_two( unsigned int date_roundup_pow_of_two );

struct KFIFO *kfifo_alloc(unsigned int size);

unsigned int __kfifo_put(struct KFIFO *fifo, unsigned char *buffer, unsigned int len);

unsigned int __kfifo_get(struct KFIFO *fifo, unsigned char *buffer, unsigned int len);

#endif

 

1.freertos的简介&移植freertos到stm32平台(代码片段)

I.说明作者:WXP(翱翔云端的鸟)联系方式:328452854@qq.com||13100610853(联系请注明CSDN)申明:个人原创,转载请先经过本人同意!要说的话:个人水平有限,不足之处,还请指正!有疑问欢迎大家联系我交流探讨!=... 查看详情

freertos消息队列&esp32使用(代码片段)

FreeRTOS消息队列&ESP32实战FreeRTOS消息队列FreeRTOS的消息队列和操作系统课中讲的消息队列大差不差,都是为了有序的、安全的在多任务间在发送信息。下面是其一些特性。多任务访问  队列不是属于某个特别指定的任务的... 查看详情

CMSIS & STM32,如何开始? [关闭]

】CMSIS&STM32,如何开始?[关闭]【英文标题】:CMSIS&STM32,Howtobegin?[closed]【发布时间】:2021-09-1212:28:07【问题描述】:我想在STM32上使用CMSIS启动项目。网上冲浪,没找到具体教程。一些使用SPL(ST已停止)启动项目。我想要... 查看详情

stm32使用freertos过程记录

STM32使用FreeRTOS过程记录 查看详情

STM32 FreeRTOS 与 LibOpenCM3

】STM32FreeRTOS与LibOpenCM3【英文标题】:STM32FreeRTOSwithLibOpenCM3【发布时间】:2019-02-2619:30:39【问题描述】:我有一个STM32BluePillBoard(STM32F103C8with8MhzQuarz)并尝试上传一个用LibOpenCM3和FreeRTOS制作的小闪烁程序。但由于某种原因,FreeRTO... 查看详情

STM32 & TLV5628 SPI 通信

】STM32&TLV5628SPI通信【英文标题】:STM32&TLV5628SPICommunication【发布时间】:2019-06-1408:38:36【问题描述】:大家好,感谢您的宝贵时间。我一直致力于将STM32f446RENucleo板与TLV56288位八进制串行DAC连接起来。我遇到了多个问题,但... 查看详情

移植freertos到stm32

移植FreeRTOS到STM32单片机上引言介绍什么是RTOS?为什么嵌入式设备往往使用RTOS?FreeRTOS具体步骤总结引言本文详细介绍如何移植FreeRTOS到STM32单片机上。移植操作系统是嵌入式开发的入门基础,单片机和嵌入式在物理上... 查看详情

移植freertos到stm32

移植FreeRTOS到STM32单片机上引言介绍什么是RTOS?为什么嵌入式设备往往使用RTOS?FreeRTOS具体步骤总结引言本文详细介绍如何移植FreeRTOS到STM32单片机上。移植操作系统是嵌入式开发的入门基础,单片机和嵌入式在物理上... 查看详情

stm32移植freertos(图文实操)

目录一、Freertos1、前后台系统2、可剥夺型内核3、Freertos系统简介二、FreeRTOS移植1.在基础工程种新建Freertos文件夹,并且将源码添加到这个文件夹中。2.在poertable文件夹中保留keil、MemMang、和RVDS,其他删除3.在基础工程中新建分... 查看详情

freertos移植stm32f407(代码片段)

FREERTOS移植STM32F407获取FREERTOS源码从github上下载:FREERTOS源码创建基础工程1.利用Keil5创建一个STM32基础工程,建议直接使用任何一个STM32库模板。在基础工程中创建一个文件夹FREERTOS,用来存放FreeRTOS源码,该源码存... 查看详情

CubeMX STM32 HAL + FreeRTOS

】CubeMXSTM32HAL+FreeRTOS【英文标题】:【发布时间】:2017-04-0719:33:46【问题描述】:我想检查一下STM32Workbench是否正常工作。所以,我为我的F7Discovery创建了新项目。我正在使用HAL库并且还启用了FreeRTOS。由于FreeRTOS需要一个独立的... 查看详情

FreeRTOS STM32 集成

】FreeRTOSSTM32集成【英文标题】:FreeRTOSSTM32Integrating【发布时间】:2018-07-1417:46:27【问题描述】:我正在尝试在现有的STM32固件项目中启动FreeRTOS。我复制了所有文件,将中断优先级更正为最低。但是,处理器卡在xTaskIncrementTickfor... 查看详情

stm32移植freertos出现的问题及解决办法

STM32移植FreeRTOS出现的问题及解决办法引言问题:\\FreeRTOS\\queue.c(2761):error:#268:declarationmaynotappearafterexecutablestatementinblock解决方法引言我们在使用STM32移植FreeRTOS时,可能会出现很多问题,本文主要来归纳总结一下(... 查看详情

stm32为啥要用freertos

参考技术A可以不用啊,RTOS只是一个操作系统,多任务并行时建议使用RTOS 查看详情

随机硬故障 - STM32F4 - FreeRTOS

】随机硬故障-STM32F4-FreeRTOS【英文标题】:RandomHardFaults-STM32F4-FreeRTOS【发布时间】:2018-05-2716:38:24【问题描述】:我有一块运行FreeRTOS的STM32F4板(上面有3个任务),我每使用15-50分钟就会出现一次硬故障。我的硬件:3个编码器、... 查看详情

stm32堆和栈(stm32heap&stack)

...片机下的堆和栈是什么样的分布呢?以下是网摘:刚接手STM32时,你只编写一个intmain(){while(1);}BUILD://ProgramSize:Code=340RO-data=252RW-data=0ZI-data=1632 编译后,就会发现这么个程序已用了1600多的RAM,要是 查看详情

禁用中断让freeRTOS在stm32上运行

】禁用中断让freeRTOS在stm32上运行【英文标题】:DisableinterrupttoletfreeRTOSrunonstm32【发布时间】:2021-04-0111:13:14【问题描述】:我正在开展一个项目,我正在通过STM32f4上的DMA连续获取数字样本。在我做一些DSP的每个样本之后,DMA都... 查看详情

stm3232-bitarmcortexmcus&&mpus

一、STM3232-bitArmCortexMCUs二、STM32ArmCortexMPUs 查看详情