跳转到主要内容

使用MM32F3270基于Azure RTOS事件标志组的应用

<strong><font color="#4e5e9e"> 简 介 </font> </strong>

Azure RTOS ThreadX 是 Microsoft 提供的高级工业级实时操作系统 (RTOS)。它是专门为深度嵌入式实时 IoT 应用程序设计的。Azure RTOS ThreadX 提供高级计划、通信、同步、计时器、内存管理和中断管理功能。此外,Azure RTOS ThreadX 具有许多高级功能,包括 picokernel™ 体系结构、preemption-threshold™ 计划、event-chaining™、执行分析、性能指标和系统事件跟踪。Azure RTOS ThreadX 非常易于使用,适用于要求极其苛刻的嵌入式应用程序。Azure RTOS ThreadX 在各种产品(包括消费者设备、医疗电子设备和工业控制设备)上的部署次数已达数十亿次。

具体的介绍和用户指南可以参考:

https://docs.microsoft.com/zh-cn/azure/rtos/threadx/

在前文描述移植基本内核的基础上,本文描述了如何基于MM32F3270系列MCU结合Azure RTOS ThreadX应用事件标志组的使用,引导用户理解Azure RTOS ThreadX事件标志组的功能。

表 1 适用系列型号
<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259903-1.png&…; alt=“表 1 适用系列型号" /></center>

<strong><font color="#4e5e9e">1、移植应用的准备</font> </strong>

<strong>1.1 硬件开发板的准备</strong>

该移植过程中应用的开发板为MM32的EVB-F3270,板载MM32F3273G9P。

<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259904-2.png&…; alt=“使用MM32F3270基于Azure RTOS事件标志组的应用" /></center>

EVB-F3270 (MM32F3273G9P)的简要参数:

● Arm Cortex-M3 内核

● 板载 MM32F3273G9P(LQFP144)

● USB Host / Device、SPI、I2C

● 4 x Key、4 x LED

● I2S Speaker

● TF-Card

● Ethernet PHY

<strong>1.2 软件的准备</strong>

<strong><font color="red">库函数和例程(Lib Samples)</font> </strong>

该移植过程中应用的 Firmware 分别为 MM32F3270 库函数和例程,下载地址:

<a href="https://www.mindmotion.com.cn/products/mm32mcu/mm32f/mm32f_mainstream/m…;

<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259905-3.png&…; alt=“使用MM32F3270基于Azure RTOS事件标志组的应用" /></center>

<strong><font color="red">Azure RTOS ThreadX(源码)</font> </strong>

ThreadX 的源代码已经开放,我们可以从 ThreadX 公共源代码存储库获取 Azure RTOS ThreadX,网址为:https://github.com/azure-rtos/threadx/

具体的商用使用条件参考Azure的许可证说明:

<a href="https://www.microsoft.com/en-us/legal/intellectualproperty/tech-licensi…;

Microsoft publishes the Azure RTOS source code to GitHub. No license is required to install and use the software for internal development, testing, and evaluation purposes. A license is required to distribute or sell components and devices unless using Azure RTOS licensed hardware.

Azure RTOS 何时需要许可证?

Microsoft 将 Azure RTOS 源代码发布到 GitHub。安装和使用该软件进行内部开发、测试和评估无需许可证。分发或销售组件和设备需要许可证,除非使用 Azure RTOS 许可的硬件。

ThreadX 安装

可以通过将 GitHub 存储库克隆到本地计算机来安装 ThreadX。下面是用于在 PC 上创建 ThreadX 存储库的克隆的典型语法。

shell复制

git clone https://github.com/azure-rtos/threadx

或者,也可以使用 GitHub 主页上的“下载”按钮来下载存储库的副本。

下载后的仓库代码目录列表如下:

<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259906-4.png&…; alt=“使用MM32F3270基于Azure RTOS事件标志组的应用" /></center>

<strong><font color="red">Azure RTOS ThreadX(源码)支持的开发环境</font> </strong>

ThreadX 内核提供好了各种主流硬件平台和软件平台的移植文件,以Cortex_M3为例,可以支持以下六种开发环境:

<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259907-5.png&…; alt=“使用MM32F3270基于Azure RTOS事件标志组的应用" /></center>

本次移植过程使用Azure RTOS原有的sample_threadx.c Samples为例子,稍作修改,演示了事件标志组的功能与应用。

<strong><font color="#4e5e9e">2、Threadx 事件标志组的应用</font> </strong>

该章节介绍了事件标志组应用实现的过程和注意事项,该演示程序可在MM32F3273G9P的EVB-F3270上运行。

此示例在文件 main_event_flags_demo.c 中定义,旨在说明如何在嵌入式多线程环境中使用事件标志组,实现任务之间的数据信号传递与中断或任务同步的相互关系。

<strong>2.1 Azure Threadx event简介</strong>

事件标志是Threadx线程同步的一个强大工具。事件标志可以被任何线程设置或清除,也可以被任何线程检查。线程可以在等待设置某些事件标志组时挂起。

每个事件标志用1 bit表示,事件标志以32个为一组排列,组成一个事件标志组。之后的所有操作都是以组为单位。

<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259908-6.png&…; alt=“使用MM32F3270基于Azure RTOS事件标志组的应用" /></center>

线程可以同时对一个组中的所有32个事件标志进行操作。

要设置或清除事件标志组,可以使用tx_event_flags_set服务。

使用tx_event_flags_get服务可以“获取”事件标志组。

获取事件标志时,线程可以等待一个事件标志(一段时间或永久等待),直到该事件标志被另一个线程或中断设置为1(set状态)。

对于嵌入式应用来说,就有点像对一个个的寄存器位进行“&”和“|”操作。

使用事件标志组可以有效的解决中断服务程序和任务之间的同步问题。

<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259909-7.png&…; alt=“使用MM32F3270基于Azure RTOS事件标志组的应用" /></center>

<strong>2.2 Azure Threadx event的结构体</strong>

Event的结构体

<pre style="overflow-x:auto; background-color:#e9e9e9;">typedef struct TX_EVENT_FLAGS_GROUP_STRUCT
{
/* Define the event flags group ID used for error checking. */
ULONG tx_event_flags_group_id;
/* Define the event flags group's name. */
CHAR *tx_event_flags_group_name;
/* Define the actual current event flags in this group. A zero in a
particular bit indicates the event flag is not set. */
ULONG tx_event_flags_group_current;
/* Define the reset search flag that is set when an ISR sets flags during
the search of the suspended threads list. */
UINT tx_event_flags_group_reset_search;
/* Define the event flags group suspension list head along with a count of
how many threads are suspended. */
struct TX_THREAD_STRUCT
*tx_event_flags_group_suspension_list;
UINT tx_event_flags_group_suspended_count;
/* Define the created list next and previous pointers. */
struct TX_EVENT_FLAGS_GROUP_STRUCT
*tx_event_flags_group_created_next,
*tx_event_flags_group_created_previous;
/* Define the delayed clearing event flags. */
ULONG tx_event_flags_group_delayed_clear;
#ifdef TX_EVENT_FLAGS_ENABLE_PERFORMANCE_INFO
/* Define the number of event flag sets. */
ULONG tx_event_flags_group_performance_set_count;
/* Define the number of event flag gets. */
ULONG tx_event_flags_group__performance_get_count;
/* Define the number of event flag suspensions. */
ULONG tx_event_flags_group___performance_suspension_count;
/* Define the number of event flag timeouts. */
ULONG tx_event_flags_group____performance_timeout_count;
#endif
#ifndef TX_DISABLE_NOTIFY_CALLBACKS
/* Define the application callback routine used to notify the application when
an event flag is set. */
VOID (*tx_event_flags_group_set_notify)(struct TX_EVENT_FLAGS_GROUP_STRUCT *group_ptr);
#endif
/* Define the port extension in the event flags group control block. This
is typically defined to whitespace in tx_port.h. */
TX_EVENT_FLAGS_GROUP_EXTENSION
} TX_EVENT_FLAGS_GROUP;</pre>

Event flags包含成员的含义:

<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259910-8.png&…; alt=“使用MM32F3270基于Azure RTOS事件标志组的应用" /></center>

<strong>2.3 Azure Threadx event的主要函数与功能</strong>

事件标志组的主要函数

<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259911-9.png&…; alt=“使用MM32F3270基于Azure RTOS事件标志组的应用" /></center>

具体函数的中文说明可以参考:

https://docs.microsoft.com/zh-cn/azure/rtos/threadx/chapter4

具体函数的英文说明可以参考:

https://docs.microsoft.com/en-us/azure/rtos/threadx/threadx-smp/chapter…

<strong>2.4 事件标志组的应用演示</strong>

<strong><font color="red">2.4.1 工程目录的建立</font> </strong>

打开目标工程文件夹“MM32F3270Project”:

<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259912-10.png…; alt=“使用MM32F3270基于Azure RTOS事件标志组的应用" /></center>

移除原有样例.c 文件sample_threadx.c:

<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259913-11.png…; alt=“使用MM32F3270基于Azure RTOS事件标志组的应用" /></center>

参考sample_threadx.c建立main_event_flags_demo.c文件,并添加hardware目录中的led.c到工程项目中。

<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259914-12.png…; alt=“使用MM32F3270基于Azure RTOS事件标志组的应用" /></center>

<strong><font color="red">2.4.2 创建事件标志组tx_event_flags_create</font> </strong>

声明需要使用的事件标志组变量。

调用tx_event_flags_create, 建立一个事件标志组。

创建一个待使用的事件标志组

<pre style="overflow-x:auto; background-color:#e9e9e9;">#include "tx_api.h"

#define DEMO_STACK_SIZE 1024

/* Define the ThreadX object control blocks... */
TX_THREAD thread_0;
TX_THREAD thread_5;

TX_EVENT_FLAGS_GROUP event_flags_0;

/* Define what the initial system looks like. */
void tx_application_define(void *first_unused_memory)
{
/* Create the main thread. */
tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
thread_0_stack, DEMO_STACK_SIZE,
1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
/* Create thread 5. This thread simply pends on an event flag which will be set
by thread_0. */
tx_thread_create(&thread_5, "thread 5", thread_5_entry, 5,
thread_5_stack, DEMO_STACK_SIZE,
4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
/* Create the event flags group used by threads 0 and 5. */
tx_event_flags_create(&event_flags_0, "event flags 0");
}</pre>

函数原型:

tx_event_flags_create函数原型

<pre style="overflow-x:auto; background-color:#e9e9e9;">UINT _tx_event_flags_create(TX_EVENT_FLAGS_GROUP *group_ptr, CHAR *name_ptr)</pre>

参数:

● group_ptr:指向事件标志组控制块的指针。

● name_ptr:指向事件标志组名称的指针。

返回值:

● TX_SUCCESS:(0X00) 成功创建事件组。

● TX_GROUP_ERROR:(0x06) 事件组指针无效。指针为 NULL 或事件组已创建。

● NX_CALLER_ERROR:(0x13) 此服务的调用方无效。

应用中该实现函数:

tx_event_flags_create(&event_flags_0, "event flags 0");

A. 第1个参数是事件标志组引用的指针,引用声明的事件标注“event_flags_0”指向的地址。

B. 第2个参数是事件标志组的名字,字符串值为" event flags 0"。

<strong><font color="red">2.4.3 创建一个任务用于发送事件标志组</font> </strong>

在此建立一个定时发送一个事件标志组的简单任务,发送完成,发送任务计数值加一:

创建一个发送事件标志组的任务

<pre style="overflow-x:auto; background-color:#e9e9e9;">/* Define the test threads. */
void thread_0_entry(ULONG thread_input)
{
UINT status;
/* This thread simply sits in while-forever-sleep loop. */
while(1)
{
/* Increment the thread counter. */
thread_0_counter++;
/* Sleep for 10 ticks. */
tx_thread_sleep(10);
/* Set event flag 0 to wakeup thread 5. */
status = tx_event_flags_set(&event_flags_0, 0x1, TX_OR);
/* Check status. */
if (status != TX_SUCCESS)
break;
}
}</pre>

函数原型:

_tx_event_flags_set函数原型

<pre style="overflow-x:auto; background-color:#e9e9e9;">UINT _tx_event_flags_set(TX_EVENT_FLAGS_GROUP *group_ptr, ULONG flags_to_set, UINT set_option)</pre>

参数:

● group_ptr:指向以前创建的事件标志组控制块的指针。

● flags_to_set:根据所选的 set-option,指定要设置或清除的事件标志。

● set_option:指定将所指定的事件标志与该组的当前事件标志进行“AND”或“OR”运算。以下是有效的选择:

- TX_AND (0x02)

- TX_OR (0x00)

如果选择 TX_AND,则会指定将所指定的事件标志与该组的当前事件标志进行“AND”运算。此选项通常用于清除组中的事件标志。否则,如果指定了 TX_OR,则对所指定的事件标志与该组的当前事件标志进行“OR”运算。

返回值:

● TX_SUCCESS:(0X00) 成功设置事件标志。

● TX_GROUP_ERROR:(0x06) 指向事件标志组的指针无效。

● TX_OPTION_ERROR:(0x08) 指定的 set-option 无效。

应用中该实现函数:

status = tx_event_flags_set(&event_flags_0, 0x1, TX_OR);

A. 第1个参数是事件标志组引用的指针,引用声明的事件标注“event_flags_0”指向的地址。

B. 第2个参数是事件标志组的名字,字符串值为" event flags 0"。

返回值

C. 第3个参数是TX_OR,则对所指定的事件标志与该组的当前事件标志进行“OR”运算。

<strong><font color="red">2.4.4 创建一个任务接收事件标志组</font> </strong>

在此建立一个任务,实现接收收事件标志组,接收事件设置值成功,接收任务计数值加一:

创建一个接收事件标志组任务

<pre style="overflow-x:auto; background-color:#e9e9e9;">void thread_5_entry(ULONG thread_input)
{
UINT status;
ULONG actual_flags;
/* This thread simply waits for an event in a forever loop. */
while(1)
{
/* Increment the thread counter. */
thread_5_counter++;
/* Wait for event flag 0. */
status = tx_event_flags_get(&event_flags_0, 0x1, TX_OR_CLEAR,
&actual_flags, TX_WAIT_FOREVER);
/* Check status. */
if ((status != TX_SUCCESS) || (actual_flags != 0x1))
break;
}
}</pre>

函数原型:

_tx_event_flags_set函数原型

<pre style="overflow-x:auto; background-color:#e9e9e9;">UINT _tx_event_flags_get(TX_EVENT_FLAGS_GROUP *group_ptr, ULONG requested_flags,
UINT get_option, ULONG *actual_flags_ptr, ULONG wait_option)</pre>

参数:

● group_ptr:指向以前创建的事件标志组的指针。

● requested_flags:32 位无符号变量,表示请求的事件标志。

● get_option:指定是否需要所有或任何请求的事件标志。以下是有效的选择:

- TX_AND (0x02)

- TX_AND_CLEAR (0x03)

- TX_OR (0x00)

- TX_OR_CLEAR (0x01)

如果选择 TX_AND 或 TX_AND_CLEAR,则会指定所有事件标志在组中都必须存在。如果选择 TX_OR 或 TX_OR_CLEAR,则会指定任何事件标志都符合要求。如果指定 TX_AND_CLEAR 或 TX_OR_CLEAR,则会清除满足请求的事件标志(设置为零)。

● actual_flags_ptr:指向放置检索到的事件标志的位置这一目标的指针。注意,实际获得的标志可能包含没有请求的标志。

● wait_option:定义未设置所选事件标志时服务的行为方式。

等待选项的定义如下:

- TX_NO_WAIT (0x00000000) - 如果选择 TX_NO_WAIT,则无论此服务是否成功,都会导致立即从此服务返回。如果从非线程(例如初始化、计时器或 ISR)调用服务,则这是唯一有效的选项。

- TX_WAIT_FOREVER 超时值 (0xFFFFFFFF) - 选择 TX_WAIT_FOREVER 会导致发出调用的线程无限期挂起,直到事件标志可用为止。

- 超时值(0x00000001 至 0xFFFFFFFE)- 如果选择一个数值(1 到 0xFFFFFFFE),则会指定在等待事件标志时发出调用的线程保持挂起的最大计时器时钟周期数。

返回值:

● TX_SUCCESS:(0X00) 成功获取事件标志。

● TX_DELETED:(0x01) 线程挂起时删除了事件标志组。

● TX_NO_EVENTS:(0X07) 服务无法在指定的等待时间内获取指定的事件。

● TX_WAIT_ABORTED:(0x1A) 挂起状态由其他线程、计时器或 ISR 中止。

● TX_GROUP_ERROR:(0x06) 事件标志组指针无效。

● TX_PTR_ERROR:(0x03) 指向实际事件标志的指针无效。

● TX_WAIT_ERROR:(0x04) 从非线程调用时指定了除 TX_NO_WAIT 以外的等待选项。

● TX_OPTION_ERROR:(0x08) 指定的 get-option 无效。

应用中该实现函数:

status = tx_event_flags_get(&event_flags_0, 0x1, TX_OR_CLEAR, &actual_flags, TX_WAIT_FOREVER);

A. 第1个参数是事件标志组引用的指针,引用声明的事件标注“event_flags_0”指向的地址。

B. 第2个参数是根据所选的 get-option,指定要设置或清除的事件标志等于0x01。

C. 第3个参数是TX_OR_CLEAR,则会判断指定任何事件标志都符合要求,符合后并清除。

<strong><font color="#4e5e9e">3、Threadx 的事件标志组应用实现与调试</font> </strong>

<strong>3.1 代码实现</strong>

下载调试默认会运行到main()函数,如下为全部实现的代码。

Demo演示代码

<pre style="overflow-x:auto; background-color:#e9e9e9;">/* This is a small demo of the high-performance ThreadX kernel. It includes examples of six
threads of different priorities, using a message an event flags group. */

#include "tx_api.h"

#define DEMO_STACK_SIZE 1024

/* Define the ThreadX object control blocks... */
TX_THREAD thread_0;
TX_THREAD thread_5;

TX_EVENT_FLAGS_GROUP event_flags_0;

/* Define the counters used in the demo application... */
ULONG thread_0_counter;
ULONG thread_5_counter;

/* Define the thread stacks. */
UCHAR thread_0_stack[DEMO_STACK_SIZE];
UCHAR thread_5_stack[DEMO_STACK_SIZE];

/* Define thread prototypes. */
void thread_0_entry(ULONG thread_input);
void thread_5_entry(ULONG thread_input);

/* Define main entry point. */
int main()
{
/* Enter the ThreadX kernel. */
tx_kernel_enter();
}

/* Define what the initial system looks like. */
void tx_application_define(void *first_unused_memory)
{
/* Create the main thread. */
tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
thread_0_stack, DEMO_STACK_SIZE,
1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
/* Create thread 5. This thread simply pends on an event flag which will be set
by thread_0. */
tx_thread_create(&thread_5, "thread 5", thread_5_entry, 5,
thread_5_stack, DEMO_STACK_SIZE,
4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
/* Create the event flags group used by threads 0 and 5. */
tx_event_flags_create(&event_flags_0, "event flags 0");
}

/* Define the test threads. */
void thread_0_entry(ULONG thread_input)
{
UINT status;
/* This thread simply sits in while-forever-sleep loop. */
while(1)
{
/* Increment the thread counter. */
thread_0_counter++;
/* Sleep for 10 ticks. */
tx_thread_sleep(10);
/* Set event flag 0 to wakeup thread 5. */
status = tx_event_flags_set(&event_flags_0, 0x1, TX_OR);
/* Check status. */
if (status != TX_SUCCESS)
break;
}
}

void thread_5_entry(ULONG thread_input)
{
UINT status;
ULONG actual_flags;
/* This thread simply waits for an event in a forever loop. */
while(1)
{
/* Increment the thread counter. */
thread_5_counter++;
/* Wait for event flag 0. */
status = tx_event_flags_get(&event_flags_0, 0x1, TX_OR_CLEAR,
&actual_flags, TX_WAIT_FOREVER);
/* Check status. */
if ((status != TX_SUCCESS) || (actual_flags != 0x1))
break;
}
}</pre>

<strong>3.2 下载与调试</strong>

进入调试模式,可以看到接收事件标志组成功。

<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259915-13.png…; alt=“使用MM32F3270基于Azure RTOS事件标志组的应用" /></center>

该demo示例,建立了2个线程,程序执行一个发送事件标志,另外一个线程接收事件标志,实现执行Task Counter与发送&接收的Message Counter的累加。

全速运行后,可以看到相关的数值在变化:

<center><img src="http://mcu.eetrend.com/files/2022-07/wen_zhang_/100561762-259916-14.png…; alt=“使用MM32F3270基于Azure RTOS事件标志组的应用" /></center>

<strong><font color="#4e5e9e">4、小结</font> </strong>

这个演示程序中,使用了tx_thread_sleep()函数做了延时,刚好使得发送事件标志组与接收事件标志组一一配对,计数值累加后都是相等的,说明同步成功。

Azure RTOS ThreadX 的使用事件标志组可以方便实现任务对任务或任务对中断的同步,结合MM32F3270的强大性能,可以实现Azure RTOS的各种特色功能。

点击下方链接进行下载:

<a href="https://mindmotion.com.cn/download/products/MM32F3270_Event_Flags.zip">…;

来源:<a href="https://mp.weixin.qq.com/s/xXrrwAIwwxKLa7DjEELN8w">灵动MM32MCU</a&gt;
免责声明:本文为转载文章,转载此文目的在于传递更多信息,版权归原作者所有。本文所用视频、图片、文字如涉及作品版权问题,请联系小编进行处理(联系邮箱:cathy@eetrend.com)。