基本实例
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优先级比较高的时候。
作者:虚生 出处:https://www.cnblogs.com/dylancao/ 以音频和传感器算法为核心的智能可穿戴产品解决方案提供商 ,提供可穿戴智能软硬件解决方案的设计,开发和咨询服务。 勾搭热线:邮箱:1173496664@qq.com weixin:18019245820 市场技术对接群:347609188 |
![]() |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2019-02-21 用pyinstaller打包python程序,解决打包时的错误:Cannot find existing PyQt5 plugin directories
2019-02-21 win10下 anaconda 环境下python2和python3版本转换
2019-02-21 zsh: command not found: conda的一种解决方法