基本实例

  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基本使用案例及备忘的更多相关文章

  1. JAVA中获得一个月最大天数的方法(备忘)

    Calendar 类是一个抽象类,为日历字段之间的转换提供了一些方法.其中有一个重要方法 getActualMaximum ,该方法用于返回指定日历字段实际的最大值. 利用这个方法(Calendar. ...

  2. 从3dmax中导入模型到UDK Editor(供个人备忘)

    笔记从3dmax中导入模型到UDK Editor 1)      在3dmax中导出 2)      选择FBX格式,保存 3)      在UDK中打开content browser,自己选个pac ...

  3. c++中的dictionary对象:map的使用备忘

    #include <map> #include <iostream> using namespace std; void main(){ map <string, int ...

  4. Java基础知识强化之IO流笔记45:IO流练习之 把集合中的数据存储到文本文件案例

    1. 把集合中的数据存储到文本文件案例:    需求:把ArrayList集合中的字符串数据存储到文本文件 ? (1)分析:通过题目的意思我们可以知道如下的一些内容,ArrayList集合里存储的是字 ...

  5. Laravel 中使用 swoole 项目实战开发案例二 (后端主动分场景给界面推送消息)

    推荐阅读:Laravel 中使用 swoole 项目实战开发案例一 (建立 swoole 和前端通信)​ 需求分析 我们假设有一个需求,我在后端点击按钮 1,首页弹出 “后端触发了按钮 1”.后端点了 ...

  6. arduino中SCoop库的简单应用案例

    转载:https://www.csdn.net/gather_27/MtTaggzsMDExMS1ibG9n.html arduino中SCoop库的简单应用案例首先这篇文章来在视频https://v ...

  7. 【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 ...

  8. ECMAScript 5(ES5)中bind方法简介备忘

    一直以来对和this有关的东西模糊不清,譬如call.apply等等.这次看到一个和bind有关的笔试题,故记此文以备忘. bind和call以及apply一样,都是可以改变上下文的this指向的.不 ...

  9. 项目中oracle存储过程记录——经常使用语法备忘

    项目中oracle存储过程记录--经常使用语法备忘 项目中须要写一个oracle存储过程,需求是收集一个复杂查询的内容(涉及到多张表),然后把符合条件的记录插入到目标表中.当中原表之中的一个的日期字段 ...

随机推荐

  1. hyper-v虚拟机不能访问外网的解决方案

    直接说解决方案,将虚拟机的一张网卡改为旧版网络适配器即可.具体原因还不可知. 延申一下,一般应该使用的交换机,是“外部”类型即可.

  2. 849. Dijkstra求最短路 I(模板)

    给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为正值. 请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1. 输入格式 第一行包含整数n和m. 接下来m行每行包 ...

  3. SVN的使用01

    关于svn的使用以及TortoiseSVN常见操作 一.关于svn介绍 在介绍之前提一下,MyEclipse项目组的建立,以及源文件夹的创建. 新建的那一栏点击other 在搜索栏中搜索Java Wo ...

  4. python接口自动化-requests-toolbelt处理multipart/form-data

    1.requests-toolbelt官方文档:https://pypi.org/project/requests-toolbelt/ 2.环境安装 pip install requests-tool ...

  5. [HAOI2011] Problem b - 莫比乌斯反演

    复习一下莫比乌斯反演 首先很显然用一下容斥把它转化成求 \(ans=\sum_{i=1}^a \sum_{j=1}^b [{gcd(i,j)=d}]\) 我们可以定义 f(d) 和 F(d) 如下: ...

  6. oracle sql 数据库之间导入数据

    1.导入别的表 insert into EMPI_IDENTIFY select id,empiid, name||':' ||idcardno,'accidcardno','' from empi_ ...

  7. PP: Imaging time-series to improve classification and imputation

    From: University of Maryland encode time series as different types of images. reformulate features o ...

  8. nice-validator判断表单是否验证通过

    $("#formSurvery").isValid(function(is){ if(is){ alert("通过!") } } 如果is为false则表示不通 ...

  9. python面试的100题(21)

    正则表达式 94.请写出一段代码用正则匹配出ip? ip地址的生成规则. IP地址,是由32位数字二进制转为四个十进制的字符串组成. 怎么转化?下面讲解: 二进制:111111111111111111 ...

  10. mysql行级锁 select for update

    mysql行级锁 select for update 1.属于行级锁 2.where条件后需要写出明确的索引条件(如果有多个条件,可以建立联合索引) 3.如果其所在的事务提交或者回滚后,或者更新该条数 ...