没有合适的资源?快使用搜索试试~ 我知道了~
首页Freertos多线程实战:降低嵌入式系统编程难度
Freertos多线程实战:降低嵌入式系统编程难度
需积分: 48 27 下载量 179 浏览量
更新于2024-07-21
1
收藏 1.74MB PDF 举报
"Freertos多线程操作系统是一本实用指南,由Richard Barry撰写,专注于介绍在小型嵌入式系统中应用FreeRTOS实时操作系统的实践方法。该系统设计初衷是为微控制器而构建,旨在简化多任务处理的开发过程,特别是通过使用RAM系列来降低编程复杂度。 在第一章中,作者首先介绍了多任务的基本概念和在小型嵌入式环境中的应用。他强调了术语的一致性,并明确了本书的范围,主要关注FreeRTOS的核心功能,如任务创建、调度和优先级管理。 1.1章节深入讨论了任务的创建,通过`xTaskCreate()` API函数的使用示例,展示了如何定义和启动新任务。两个实例分别展示了如何编写任务代码和利用任务参数传递数据。这有助于读者理解如何将实际工作负载分配给不同的任务实例。 1.5部分着重于任务优先级的概念,作者提供了一个实验性的例子,指导读者如何调整任务优先级以实现更有效的任务调度。通过实践,开发者可以理解如何平衡不同任务的执行顺序,确保关键任务能在有限的时间内得到响应。 此外,1.6章节探讨了'NotRunning'(阻塞)状态的扩展,即当一个任务因为等待某个条件或资源而暂停时的状态。这里讨论了任务阻塞机制以及如何设计合理的阻塞策略,以避免系统资源的浪费。 这本指南不仅提供了技术细节,还提供了丰富的实战案例,使读者能够快速掌握Freertos在嵌入式多线程环境中的应用,降低开发难度,提高系统的实时性和效率。对于任何希望通过FreeRTOS进行微控制器多任务开发的工程师来说,这本书是不可或缺的参考资料。"
资源详情
资源推荐
http://www.FreeRTOS.org
FreeRTOS
Designed For Microcontrollers;
3
© 2009 Richard Barry. Distribution or publication in any form is strictly prohibited.
Scope
This chapter aims to give readers a good understanding of:
• How FreeRTOS allocates processing time to each task within an application.
• How FreeRTOS chooses which task should execute at any given time.
• How the relative priority of each task affects system behavior.
• The states that a task can exist in.
In addition readers will hopefully gain a good understanding of:
• How to implement tasks.
• How to create one or more instances of a task.
• How to use the task parameter.
• How to change the priority of a task that has already been created.
• How to delete a task.
• How to implement periodic processing.
• When the idle task will execute and how it can be used.
The concepts presented in this chapter are fundamental to understanding how to use FreeRTOS and
how FreeRTOS applications behave – this is therefore the most detailed chapter in the book.
http://www.FreeRTOS.org
FreeRTOS
Designed For Microcontrollers;
4
© 2009 Richard Barry. Distribution or publication in any form is strictly prohibited.
1.2 T
ASK
F
UNCTIONS
Tasks are implemented as C functions. The only thing special about them is their prototype, which
must return void and take a void pointer parameter. The prototype is demonstrated by Listing 1.
void ATaskFunction( void *pvParameters );
Listing 1 The task function prototype
Each task is a small program in its own right. It has an entry point, will normally run forever within an
infinite loop, and will not exit. The structure of a typical task is shown in Listing 2.
FreeRTOS tasks must not be allowed to return from their implementing function in any way – they
must not contain a ‘return’ statement and must not be allowed to execute past the end of the function.
If a task is no longer required it should instead be explicitly deleted. This is also demonstrated in
Listing 2.
A single task function definition can be used to create any number of tasks – each created task being
a separate execution instance with its own stack and its own copy of any automatic (stack) variables
defined within the task itself.
void ATaskFunction( void *pvParameters )
{
/* Variables can be declared just as per a normal function. Each instance
of a task created using this function will have its own copy of the
iVariableExample variable. This would not be true if the variable was
declared static – in which case only one copy of the variable would exist
and this copy would be shared by each created instance of the task. */
int iVariableExample = 0;
/* A task will normally be implemented as in infinite loop. */
for( ;; )
{
/* The code to implement the task functionality will go here. */
}
/* Should the task implementation ever break out of the above loop
then the task must be deleted before reaching the end of this function.
The NULL parameter passed to the vTaskDelete() function indicates that
the task to be deleted is the calling (this) task. */
vTaskDelete( NULL );
}
Listing 2 The structure of a typical task function
http://www.FreeRTOS.org
FreeRTOS
Designed For Microcontrollers;
5
© 2009 Richard Barry. Distribution or publication in any form is strictly prohibited.
1.3 T
OP
L
EVEL
T
ASK
S
TATES
An application can consist of many tasks. If the microcontroller running the application only contains a
single core then only one task can actually be executing at any given time. This implies that a task
can exist in one of two states, Running and Not Running. We will consider this simplistic model first -
but keep in mind that this is an over simplification as later we will see the Not Running state actually
contains a number of sub-states.
When a task is in the Running state the processor is actually executing its code. When a task is in the
Not Running state the task is dormant, its status having been saved ready for it to resume execution
the next time the scheduler decides it should enter the Running state. When a task resumes
execution it does so from exactly the instruction it was about to execute before it last left the Running
state.
Figure 1 Top level task states and transitions.
A task transitioned from the Not Running to the Running state is said to have been “switched in” or
“swapped in”. Conversely, a task transitioned from the Running state to the Not Running state is said
to have been “switched out” or “swapped out”. The FreeRTOS scheduler is the only entity that can
switch a task in and out.
http://www.FreeRTOS.org
FreeRTOS
Designed For Microcontrollers;
6
© 2009 Richard Barry. Distribution or publication in any form is strictly prohibited.
1.4 C
REATING
T
ASKS
xTaskCreate() API Function
Tasks are created using the FreeRTOS xTaskCreate() API function. This is probably the most
complex of all the API functions so it is unfortunate that it is the first encountered, but tasks must be
mastered first as they are the most fundamental component of a multitasking system. All the
examples that accompany this book make use of the xTaskCreate() function so there are plenty of
examples to reference.
APPENDIX 5: describes the data types and naming conventions used.
portBASE_TYPE xTaskCreate( pdTASK_CODE pvTaskCode,
const signed portCHAR * const pcName,
unsigned portSHORT usStackDepth,
void *pvParameters,
unsigned portBASE_TYPE uxPriority,
xTaskHandle *pxCreatedTask
);
Listing 3 The xTaskCreate() API function prototype
Table 1 xTaskCreate() parameters and return value
Parameter
Name/Returned
Value
Description
pvTaskCode Tasks are just C functions that never exit, and as such are normally implemented
as an infinite loop. The pvTaskCode parameter is simply a pointer to the function
(in effect just the function name) that implements the task.
pcName A descriptive name for the task. This is not used by FreeRTOS in any way. It is
included purely as a debugging aid. Identifying a task by a human readable name
is much simpler than attempting to do the same from its handle.
The application defined constant configMAX_TASK_NAME_LEN defines the
maximum length a task name can take – including the NULL terminator.
Supplying a string longer than this maximum will simply result in the string being
silently truncated.
http://www.FreeRTOS.org
FreeRTOS
Designed For Microcontrollers;
7
© 2009 Richard Barry. Distribution or publication in any form is strictly prohibited.
Table 1 xTaskCreate() parameters and return value
Parameter
Name/Returned
Value
Description
usStackDepth Each task has its own unique state that is allocated by the kernel to the task when
the task is created. The usStackDepth value tells the kernel how big to make the
stack.
The value specifies the number of words the stack can hold, not the number of
bytes. For example, if the stack is 32 bits wide and usStackDepth is passed in as
100, then 400 bytes of stack space will be allocated (100 * 4bytes). The stack
depth multiplied by the stack width must not exceed the maximum value that can
be contained in a variable of type size_t.
The size of the stack used by the idle task is defined by the application defined
constant configMINIMAL_STACK_SIZE . The value assigned to this constant in
the FreeRTOS demo application for the microcontroller architecture being used is
the minimum recommended for any task. If your task uses a lot of stack space
then you will need to assign a larger value.
There is no easy way of determining the stack space required by a task. It is
possible to calculate, but most users will simply assign what they think is a
reasonable value, then use the features provided by FreeRTOS to ensure both
that the space allocated is indeed adequate, and that RAM is not being
unnecessarily wasted. CHAPTER 6 contains information on how to query the
stack space being used by a task.
pvParameters Task functions accept a parameter of type pointer to void ( void* ). The value
assigned to pvParameters will be the value passed into the task. Some examples
within this document demonstrate how the parameter can be used.
uxPriority Defines the priority at which the task will execute. Priorities can be assigned from
0, which is the lowest priority, to (configMAX_PRIORITIES – 1), which is the
highest priority.
configMAX_PRIORITIES is a user defined constant. There is no upper limit on the
number of priorities that can be available (other than the limit of the data types
used and the RAM available in your microcontroller), but you should use the
lowest number of priorities actually required in order to avoid wasting RAM.
Passing a uxPriority value above (configMAX_PRIORITIES – 1) will result in the
actual priority assigned to the task being silently capped to the maximum
legitimate value.
剩余162页未读,继续阅读
qq_26302861
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 计算机人脸表情动画技术发展综述
- 关系数据库的关键字搜索技术综述:模型、架构与未来趋势
- 迭代自适应逆滤波在语音情感识别中的应用
- 概念知识树在旅游领域智能分析中的应用
- 构建is-a层次与OWL本体集成:理论与算法
- 基于语义元的相似度计算方法研究:改进与有效性验证
- 网格梯度多密度聚类算法:去噪与高效聚类
- 网格服务工作流动态调度算法PGSWA研究
- 突发事件连锁反应网络模型与应急预警分析
- BA网络上的病毒营销与网站推广仿真研究
- 离散HSMM故障预测模型:有效提升系统状态预测
- 煤矿安全评价:信息融合与可拓理论的应用
- 多维度Petri网工作流模型MD_WFN:统一建模与应用研究
- 面向过程追踪的知识安全描述方法
- 基于收益的软件过程资源调度优化策略
- 多核环境下基于数据流Java的Web服务器优化实现提升性能
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功