free rtos 几个重要函数说明

//-----------------------------------创建计数型信号量-------------------------------
SemaphoreHandle_t CountSemaphore;//计数型信号量    

CountSemaphore=xSemaphoreCreateCounting(255,0);//创建计数型信号量

BaseType_t err;
u8 semavalue;
if(CountSemaphore!=NULL)      //计数型信号量创建成功
{
    err=xSemaphoreGive(CountSemaphore);//释放计数型信号量
    if(err==pdFALSE)
    {
        printf("信号量释放失败!!!\r\n");
    }
    
    xSemaphoreTake(CountSemaphore,portMAX_DELAY);     //等待数值信号量释放
    semavalue=uxSemaphoreGetCount(CountSemaphore);    //获取计数型信号量值
    
}
//-----------------------------------二值信号量-------------------------------

SemaphoreHandle_t BinarySemaphore;    //二值信号量句柄

BinarySemaphore=xSemaphoreCreateBinary();//创建二值信号量
if(BinarySemaphore!=NULL)xSemaphoreGive(BinarySemaphore);//二值信号量创建成功以后要先释放一下
BaseType_t err=pdFALSE;
    if(BinarySemaphore!=NULL)
    {
        err=xSemaphoreTake(BinarySemaphore,portMAX_DELAY);    //获取信号量
        if(err==pdTRUE)                                        //获取信号量成功
        {}
    }
xSemaphoreGive(BinarySemaphore);                //释放信号量    
extern SemaphoreHandle_t BinarySemaphore;    //二值信号量句柄
BaseType_t xHigherPriorityTaskWoken;
if(BinarySemaphore!=NULL)    
xSemaphoreGiveFromISR(BinarySemaphore,&xHigherPriorityTaskWoken);    //释放二值信号量
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);//如果需要的话进行一次任务切换
//-----------------------------------互斥信号量------------------------------

SemaphoreHandle_t MutexSemaphore;    //声明互斥信号量句柄
    
MutexSemaphore=xSemaphoreCreateMutex();//创建互斥信号量

xSemaphoreTake(MutexSemaphore,portMAX_DELAY);    //获取互斥信号量
xSemaphoreGive(MutexSemaphore);                    //释放信号量

//-----------------------------------软件定时器------------------------------
TimerHandle_t     AutoReloadTimer_Handle;            //周期定时器句柄
TimerHandle_t    OneShotTimer_Handle;            //单次定时器句柄

void AutoReloadCallback(TimerHandle_t xTimer);     //周期定时器回调函数
void OneShotCallback(TimerHandle_t xTimer);        //单次定时器回调函数

AutoReloadTimer_Handle=xTimerCreate((const char*        )"AutoReloadTimer",
                                    (TickType_t            )1000,
                                    (UBaseType_t        )pdTRUE,
                                    (void*                )1,
                                    (TimerCallbackFunction_t)AutoReloadCallback); //周期定时器,周期1s(1000个时钟节拍),周期模式
//创建单次定时器
OneShotTimer_Handle=xTimerCreate((const char*            )"OneShotTimer",
                                 (TickType_t            )2000,
                                 (UBaseType_t            )pdFALSE,
                                 (void*                    )2,
                                 (TimerCallbackFunction_t)OneShotCallback); //单次定时器,周期2s(2000个时钟节拍),单次模式
                                                      
    xTimerStop(AutoReloadTimer_Handle,0);     //关闭周期定时器
    xTimerStop(OneShotTimer_Handle,0);         //关闭单次定时器
    xTimerStart(OneShotTimer_Handle,0);        //开启单次定时器
    xTimerStart(AutoReloadTimer_Handle,0);    //开启周期定时器
    
//周期定时器的回调函数
void AutoReloadCallback(TimerHandle_t xTimer)
{
    static u8 tmr1_num=0;
    tmr1_num++;                                    //周期定时器执行次数加1
}

//单次定时器的回调函数
void OneShotCallback(TimerHandle_t xTimer)
{
    static u8 tmr2_num = 0;
    tmr2_num++;        //周期定时器执行次数加1
    LED1=!LED1;
    printf("定时器2运行结束\r\n");
}
//-----------------------------------事件标志组(位)------------------------------
//个人理解为标志位的集合


EventGroupHandle_t EventGroupHandler;    //事件标志组句柄

#define EVENTBIT_0    (1<<0)                //事件位
#define EVENTBIT_1    (1<<1)
#define EVENTBIT_2    (1<<2)
#define EVENTBIT_ALL    (EVENTBIT_0|EVENTBIT_1|EVENTBIT_2)

EventGroupHandler=xEventGroupCreate();     //创建事件标志组

if(EventGroupHandler!=NULL)
{
    xEventGroupSetBits(EventGroupHandler,EVENTBIT_1);
    xEventGroupSetBits(EventGroupHandler,EVENTBIT_2);    
}
EventBits_t EventValue;
if(EventGroupHandler!=NULL)
{
//等待事件组中的相应事件位
EventValue=xEventGroupWaitBits((EventGroupHandle_t    )EventGroupHandler,        
                               (EventBits_t            )EVENTBIT_ALL,//指定要置位的
                               (BaseType_t            )pdTRUE,    //清除            
                               (BaseType_t            )pdTRUE,//全部置位
                               (TickType_t            )portMAX_DELAY);//死等
}
EventBits_t NewValue;

NewValue=xEventGroupGetBits(EventGroupHandler);    //获取当前标志位被置位的与值
//-----------------------------------队列------------------------------
#include "queue.h"

#define MESSAGE_Q_NUM   4       //发送数据的消息队列的数量 
QueueHandle_t Message_Queue;    //信息队列句柄

Message_Queue=xQueueCreate(MESSAGE_Q_NUM,200); //创建消息Message_Queue,队列项长度是消息长度


BaseType_t xHigherPriorityTaskWoken;
BaseType_t err=xQueueSend(Message_Queue,USART_RX_BUF,10);
xQueueReceive(Message_Queue,&key,portMAX_DELAY)
 //就向队列发送接收到的数据
if(Message_Queue!=NULL)
{
    xQueueSendFromISR(Message_Queue,USART_RX_BUF,&xHigherPriorityTaskWoken);//向队列中发送数据可以
    
    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);//如果需要的话进行一次任务切换
}

//-----------------------------------内存------------------------------

u8 *buffer;
buffer=pvPortMalloc(30);//申请内存,30个字节

if(buffer!=NULL)vPortFree(buffer);    //释放内存
buffer=NULL;

u32 freemem;
freemem=xPortGetFreeHeapSize();        //获取剩余内存大小,单位字节

//每个任务的堆栈有限,大量使用内存,动态申请最靠谱


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值