stm32f103中freertos的tasks基本使用案例及备忘
基本实例
freetos的在stm32中使用踩了一些坑,事情做完了,就 做个备忘,希望能给后面的人一些借鉴。
先给出一个实际的例子吧。
- 启动代码
void task_create(void)
{
xTaskCreate(vButtonCheckTask,"Button",configMINIMAL_STACK_SIZE,NULL,configMAX_PRIORITIES-1,NULL);
xTaskCreate(vButtonLEDsTask,"ButtonLeds",configMINIMAL_STACK_SIZE,NULL,configMAX_PRIORITIES-1,NULL);
}
- 回调函数
static void vButtonCheckTask( void *pvParameters )
{
//for debounce
static uint8_t count;
portTickType xLastWakeTime;
const portTickType xFrequency = 20;
const portTickType yDelay = 20 / portTICK_RATE_MS;
xLastWakeTime=xTaskGetTickCount();
//create semaphores for each button
vSemaphoreCreateBinary(xButtonWakeupSemaphore);
vSemaphoreCreateBinary(xButtonUser1Semaphore);
vSemaphoreCreateBinary(xButtonUser2Semaphore);
//check if semaphores were created successfully
if((xButtonWakeupSemaphore!=NULL)&&(xButtonUser1Semaphore!=NULL)&&(xButtonUser2Semaphore!=NULL))
{
//successfully created
//resets initial semaphores to 0
xSemaphoreTake(xButtonWakeupSemaphore, (portTickType)0);
xSemaphoreTake(xButtonUser1Semaphore, (portTickType)0);
xSemaphoreTake(xButtonUser2Semaphore, (portTickType)0);
} else {
//send error of failure
}
for (;;)
{
if (ButtonRead(BWAKEUPPORT, BWAKEUP)==pdTRUE)
{
vTaskDelay(yDelay);
if(ButtonRead(BWAKEUPPORT, BWAKEUP)==pdTRUE)
{
while(ButtonRead(BWAKEUPPORT, BWAKEUP)==pdTRUE);
xSemaphoreGive(xButtonWakeupSemaphore);
//LEDToggle(1);
usart1_puts(" key1 pressed \r\n");
//printf(" led 1 on\r\n");
}
}
if (ButtonRead(BUSER1PORT, BUSER1)==pdTRUE)
{
vTaskDelay(yDelay);
if(ButtonRead(BUSER1PORT, BUSER1)==pdTRUE)
{
while(ButtonRead(BUSER1PORT, BUSER1)==pdTRUE);
//LEDToggle(1);
//printf(" use1 presssed\n\r");
usart1_puts("key2 presssed\n\r");
xSemaphoreGive(xButtonUser1Semaphore);
}
}
if (ButtonRead(BUSER2PORT, BUSER2)==pdTRUE)
{
vTaskDelay(yDelay);
if(ButtonRead(BUSER2PORT, BUSER2)==pdTRUE)
{
while(ButtonRead(BUSER2PORT, BUSER2)==pdTRUE);
usart1_puts("key3 presssed \n\r");
//xSemaphoreGive(xButtonUser2Semaphore);
}
}
}
}
void vButtonLEDsTask( void *pvParameters )
{
const portTickType xDelay = 50 / portTICK_RATE_MS;
for( ;; )
{
if((xButtonWakeupSemaphore!=NULL)&&(xButtonUser1Semaphore!=NULL)&&(xButtonUser2Semaphore!=NULL))
{
if (xSemaphoreTake(xButtonWakeupSemaphore, (portTickType)10)==pdTRUE)
{
//LEDOn(1);
usart1_puts("led1 on \n\r");
LEDToggle(1);
//give semaphore back
//xSemaphoreGive(xButtonWakeupSemaphore);
}
if (xSemaphoreTake(xButtonUser1Semaphore, (portTickType)10)==pdTRUE)
{
usart1_puts("led2 on \n\r");
LEDToggle(2);
//LEDOn(2);
//xSemaphoreGive(xButtonUser1Semaphore);
}
if (xSemaphoreTake(xButtonUser2Semaphore, (portTickType)10)==pdTRUE)
{
usart1_puts("led3 on \n\r");
//LEDToggle(3);
//LEDOn(2);
//xSemaphoreGive(xButtonUser2Semaphore);
}
}
//usart1_puts("task running \n\r");
vTaskDelay(xDelay);
//vTaskDelayUntil(&xLastWakeTime,xFrequency);
}
}
重要备忘
freetos的task和里面的函数尽量在一个文件中。
对于某些stm32 的平台,回调函数和task不在一个文件下,会出现一些异常。
freetos的task的回调函数尽量使用静态函数:
freetos的task中的循环中一定不能丢了
vTaskDelay(xDelay);不然会出现一直循环,被调度不到的情况,特别是你的task优先级比较高的时候。
stm32f103中freertos的tasks基本使用案例及备忘的更多相关文章
- JAVA中获得一个月最大天数的方法(备忘)
Calendar 类是一个抽象类,为日历字段之间的转换提供了一些方法.其中有一个重要方法 getActualMaximum ,该方法用于返回指定日历字段实际的最大值. 利用这个方法(Calendar. ...
- 从3dmax中导入模型到UDK Editor(供个人备忘)
笔记从3dmax中导入模型到UDK Editor 1) 在3dmax中导出 2) 选择FBX格式,保存 3) 在UDK中打开content browser,自己选个pac ...
- c++中的dictionary对象:map的使用备忘
#include <map> #include <iostream> using namespace std; void main(){ map <string, int ...
- Java基础知识强化之IO流笔记45:IO流练习之 把集合中的数据存储到文本文件案例
1. 把集合中的数据存储到文本文件案例: 需求:把ArrayList集合中的字符串数据存储到文本文件 ? (1)分析:通过题目的意思我们可以知道如下的一些内容,ArrayList集合里存储的是字 ...
- Laravel 中使用 swoole 项目实战开发案例二 (后端主动分场景给界面推送消息)
推荐阅读:Laravel 中使用 swoole 项目实战开发案例一 (建立 swoole 和前端通信) 需求分析 我们假设有一个需求,我在后端点击按钮 1,首页弹出 “后端触发了按钮 1”.后端点了 ...
- arduino中SCoop库的简单应用案例
转载:https://www.csdn.net/gather_27/MtTaggzsMDExMS1ibG9n.html arduino中SCoop库的简单应用案例首先这篇文章来在视频https://v ...
- 【C#】无损转换Image为Icon 【C#】组件发布:MessageTip,轻快型消息提示窗 【C#】给无窗口的进程发送消息 【手记】WebBrowser响应页面中的blank开新窗口及window.close关闭本窗体 【手记】调用Process.EnterDebugMode引发异常:并非所有引用的特权或组都分配给呼叫方 【C#】DataRowState演变备忘
[C#]无损转换Image为Icon 如题,市面上常见的方法是: var handle = bmp.GetHicon(); //得到图标句柄 return Icon.FromHandle(handle ...
- ECMAScript 5(ES5)中bind方法简介备忘
一直以来对和this有关的东西模糊不清,譬如call.apply等等.这次看到一个和bind有关的笔试题,故记此文以备忘. bind和call以及apply一样,都是可以改变上下文的this指向的.不 ...
- 项目中oracle存储过程记录——经常使用语法备忘
项目中oracle存储过程记录--经常使用语法备忘 项目中须要写一个oracle存储过程,需求是收集一个复杂查询的内容(涉及到多张表),然后把符合条件的记录插入到目标表中.当中原表之中的一个的日期字段 ...
随机推荐
- ffmpeg rtp rtmp udp 推流命令
推组播 组播地址指的范围是224.0.0.0—239.255.255.255 ffmpeg -re -i chunwan.h264 -vcodec mpeg2video -f mpeg2video u ...
- 1级搭建类106-Oracle 19c 单实例 FS(华为云)公开
项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列除特定项目目前不对外发布,仅作为博客记录,其他公开.如学员在 ...
- jQuery---美女相册案例
美女相册案例 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UT ...
- 统计redis大key信息(前topN)
相关包下载链接 https://github.com/sripathikrishnan/redis-rdb-tools/releaseshttps://pypi.org/project/python- ...
- 莫凡_linux
1.安装软件 2.基本命令ls和cd cd 指令 第一个要知道的指令就是怎么样去到你想去的地方. cd (Change Directory) 就是干这个的. 找到 Linux 的 terminal 窗 ...
- SSH、telnet配置以及它们之间区别
命令: SSH ip domain-name www.baidu.com --配置主机名(用来远程访问) user privilege secret --配置账户名和密码 line vty --配置端 ...
- Alice and Hairdresser
Alice's hair is growing by leaps and bounds. Maybe the cause of it is the excess of vitamins, or may ...
- centos软连接的增删
软连接操作 增加 ln-s 源文件 软连接名 修改 ln –snf 源文件 软连接 删除 只删除软连接 rm -rf 软连接名 只删除源文件 rm -rf 源文件 -r循环 -f强制
- day02_1spring3
面向切面编程.AOP手动代理和spring编写代理 一.什么是AOP 1.AOP简介: 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方 ...
- PLSQL无法连接(不存在或找不到oci.dll)
问题说明:新系统安装plsql后,连接不上Oracle,连接时出现过两种报错 1.找不到OCI.dll文件 2.不能初始化OCI.dll文件,即OCI.dll文件错误 解决方案 plsql连接Orac ...