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存储过程,需求是收集一个复杂查询的内容(涉及到多张表),然后把符合条件的记录插入到目标表中.当中原表之中的一个的日期字段 ...
随机推荐
- multiprocessing 方法解析:
以上是关于进程池的使用,截下来开始介绍如何使用多进程,由于multiprocessing 实现比concurrent.futures 实现更加底层这里还是推荐大家使用concurrent.future ...
- HTML-入门
HTML——超文本标记语言(Hyper Text Markup Language)html——W3C制定的标准超文本——除了文本,还有图片.视频.音频等丰富的元素标记(标签)——用来展示内容表型:标签 ...
- 题解【洛谷P2679】[NOIP2015]子串
题面 看到求方案数,还要对 \(1000000007\ (1e9+7)\) 取模,一般这样的问题都要考虑 动态规划. 我们设 \(dp_{i,j,k,0/1}\) 表示 \(A_{1\dots i}\ ...
- python之爬虫(爬取.ts文件并将其合并为.MP4文件——以及一些异常的注意事项)
//20200115 最近在看“咱们裸熊——we bears”第一季和第三季都看完了,单单就第二季死活找不到,只有腾讯有资源,但是要vip……而且还是国语版……所以就瞄上了一个视频网站——可以在线观看 ...
- centos8 apache+mysql+php
apache安装 dnf install httpd httpd-tools 开机启动 systemctl enable httpd 立即启动 systemctl start httpd 查看状态 s ...
- Pytest学习7-参数化
在测试过程中,参数化是必不可少的功能,本文就讨论下pytest的几种参数化方法 @pytest.mark.parametrize:参数化测试函数 1.内置的pytest.mark.parametriz ...
- [HAOI2011] Problem b - 莫比乌斯反演
复习一下莫比乌斯反演 首先很显然用一下容斥把它转化成求 \(ans=\sum_{i=1}^a \sum_{j=1}^b [{gcd(i,j)=d}]\) 我们可以定义 f(d) 和 F(d) 如下: ...
- Oracle登录报错-ORA-00119
报错 如果配置监听没有问题了,但是连接时又出现ORA-00119问题: ORA-00119: invalid specification for system parameter LOCAL_LIST ...
- C语言strcat()函数:字符串连接(拼接)
C语言strcat()函数:字符串连接(拼接) C语言 strcat() 函数用来将两个字符串连接(拼接)起来. 头文件:string.h 语法/原型: char*strcat(char* str ...
- 关于wget安装mysql的过程
鄙人才疏学浅,写此文章是为了帮助我那些刚入门的朋友一点心意 第一步 安装yum源 安装mysql的yum源:wget https://dev.mysql.com/get/mysql80-commun ...