cyg_flag 系列函数

本文詳細介紹了ECOS作業系統中的旗標API,包括初始化、銷毀旗標,設置與清除條件,以及如何等待旗標狀態變化等核心功能。這些API為線程同步提供了強大的支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cyg_flag_init
Name:cyg_flag_init ( ) - initialize a flag for use
Synopsis:
void cyg_flag_init
(
  cyg_flag_t *flag /* flag to initialize */
)
Description:This initializes a flag for use. Flags are synchronization mechanism that allows threads to wait on a condition or a set of conditions. Each condition is represented as a bit. Bits are user defined.
Include:#include <cyg/kernel/kapi.h>
Returns:nothing
See Also:cyg_flag_destroy

 cyg_flag_destroy
Name:cyg_flag_destroy ( ) - destroy (invalidate) a flag
Synopsis:
void cyg_flag_destroy
(
  cyg_flag_t *flag /* flag to destroy (invalidate) */
)
Description:This destroys or invalidates a flag. Be certain that no threads are waiting on or otherwise using a flag when you call this function or you may deadlock the system.
Include:#include <cyg/kernel/kapi.h>
Returns:nothing
See Also:cyg_flag_init

 cyg_flag_setbits
Name:cyg_flag_setbits ( ) - set bits (conditions) in a flag
Synopsis:
void cyg_flag_setbits
(
  cyg_flag_t       *flag, /* flag to modify */
  cyg_flag_value_t value  /* bits to set    */
)
Description:This sets bits (conditions) to true in a flag. Any bit in "value" that is set to true (1) will set the equivalent bit in the flag. This may wake threads waiting on this flag as a result.
Include:#include <cyg/kernel/kapi.h>
Returns:nothing
See Also:cyg_flag_maskbits, cyg_flag_wait, cyg_flag_timed_wait, cyg_flag_poll, cyg_flag_peek, cyg_flag_waiting

 cyg_flag_maskbits
Name:cyg_flag_maskbits ( ) - clear conditions (bits) in a flag
Synopsis:
void cyg_flag_maskbits
(
  cyg_flag_t       *flag, /* flag to modify */
  cyg_flag_value_t value  /* bits to clear  */
)
Description:This clears bits (conditions) in a flag. Any bit that is set to false (0) in "value" will be subsequently cleared in the flag. If "value" is set to 0, all conditions will be cleared, if "value" is set to all ones, no conditions will be cleared. Since this just clears conditions, no thread will run as a result of a call to this function.
Include:#include <cyg/kernel/kapi.h>
Returns:nothing
See Also:cyg_flag_setbits, cyg_flag_wait, cyg_flag_timed_wait, cyg_flag_poll, cyg_flag_peek, cyg_flag_waiting

 cyg_flag_wait
Name:cyg_flag_wait ( ) - wait forever on a flag
Synopsis:
cyg_flag_value_t cyg_flag_wait
(
  cyg_flag_t       *flag,   /* flag to wait on     */
  cyg_flag_value_t pattern, /* pattern to wait for */
  cyg_flag_mode_t  mode     /* mode of waiting     */
)
Description:This causes the calling thread to wait on a set of bits (conditions) to be set in a given flag. The "mode" indicates how the pattern will be interpreted:

CYG_FLAG_WAITMODE_AND - return match if all conditions in the pattern are set in the flag

CYG_FLAG_WAITMODE_OR - return match if any of the conditions in the pattern are set in the flag.

CYG_FLAG_WAITMODE_CLR - automatically clear the conditions that caused the calling thread to return a match, IF there was a match.

CYG_FLAG_WAITMODE_CLR can be combined with CYG_FLAG_WAITMODE_AND or CYG_FLAG_WAITMODE_OR to clear the bits that caused the condition to be met by oring the bitfields together.

If the conditions are met, the pattern that caused the pattern match is returned. A value of 0 will be returned if the thread was awakened for another reason other than a pattern match or a bad value was specified as the mode.

Include:#include <cyg/kernel/kapi.h>
Returns:the pattern that caused a match or 0 if an error.
See Also:cyg_flag_setbits, cyg_flag_maskbits, cyg_flag_timed_wait, cyg_flag_poll, cyg_flag_peek, cyg_flag_waiting

 cyg_flag_timed_wait
Name:cyg_flag_timed_wait ( ) - wait on a flag until timeout
Synopsis:
cyg_flag_value_t cyg_flag_timed_wait
(
  cyg_flag_t       *flag,   /* flag to wait on        */
  cyg_flag_value_t pattern, /* pattern to wait for    */
  cyg_flag_mode_t  mode     /* mode of waiting        */
  cyg_tick_count_t abstime  /* absolute timeout value */
)
Description:This causes the calling thread to wait on a set of bits (conditions) to be set in a given flag. If the system clock goes beyond "abstime" the wait will timeout and an error will be returned. The "mode" indicates how the pattern will be interpreted:

CYG_FLAG_WAITMODE_AND - return match if all conditions in the pattern are set in the flag

CYG_FLAG_WAITMODE_OR - return match if any of the conditions in the pattern are set in the flag.

CYG_FLAG_WAITMODE_CLR - automatically clear the conditions that caused the calling thread to return a match, IF there was a match.

CYG_FLAG_WAITMODE_CLR can be combined with CYG_FLAG_WAITMODE_AND or CYG_FLAG_WAITMODE_OR to clear the bits that caused the condition to be met by oring the bitfields together.

If the conditions are met, the pattern that caused the pattern match is returned. A value of 0 will be returned if the thread timed out, was awakened for another reason other than a pattern match or a bad value was specified as the mode.

Include:#include <cyg/kernel/kapi.h>
Returns:the pattern that caused a match or 0 if an error or timeout.
See Also:cyg_flag_setbits, cyg_flag_maskbits, cyg_flag_wait, cyg_flag_poll, cyg_flag_peek, cyg_flag_waiting

 cyg_flag_poll
Name:cyg_flag_poll ( ) - test for pattern match but do not block
Synopsis:
cyg_flag_value_t cyg_flag_poll
(
  cyg_flag_t       *flag,   /* flag to wait on     */
  cyg_flag_value_t pattern, /* pattern to wait for */
  cyg_flag_mode_t  mode     /* mode of waiting     */
)
Description:This causes the calling thread to check if a set of bits (conditions) have been set in a given flag. The "mode" indicates how the pattern will be interpreted:

CYG_FLAG_WAITMODE_AND - return match if all conditions in the pattern are set in the flag

CYG_FLAG_WAITMODE_OR - return match if any of the conditions in the pattern are set in the flag.

CYG_FLAG_WAITMODE_CLR - automatically clear the conditions that caused the calling thread to return a match, IF there was a match.

CYG_FLAG_WAITMODE_CLR can be combined with CYG_FLAG_WAITMODE_AND or CYG_FLAG_WAITMODE_OR to clear the bits that caused the condition to be met by oring the bitfields together.

If the conditions are met, the pattern that caused the pattern match is returned. A value of 0 will be returned if the thread timed out, was awakened for another reason other than a pattern match or a bad value was specified as the mode.

Include:#include <cyg/kernel/kapi.h>
Returns:the pattern that caused a match or 0 if there was no match.
See Also:cyg_flag_setbits, cyg_flag_maskbits, cyg_flag_wait, cyg_flag_timed_wait, cyg_flag_peek, cyg_flag_waiting

 cyg_flag_peek
Name:cyg_flag_peek ( ) - returns bits (conditions) currently set in a flag
Synopsis:
cyg_flag_value_t cyg_flag_peek
(
  cyg_flag_t *flag /* flag to peek at */
)
Description:This returns the current bits (conditions) that are set in a given flag.
Include:#include <cyg/kernel/kapi.h>
Returns:the bits (conditions) as a bitmask that have been set in the flag.
See Also:cyg_flag_setbits, cyg_flag_maskbits, cyg_flag_wait, cyg_flag_timed_wait, cyg_flag_poll, cyg_flag_waiting

 

cyg_flag_waiting

Name:

cyg_flag_waiting ( ) - check to see if threads wait on a given flag

Synopsis:

cyg_bool_t cyg_flag_waiting( cyg_flag_t *flag /* flag to check */)

Description:

This reports whether any threads are currently being blocked waiting for bits (conditions) to be set in the given flag.

Include:

#include <cyg/kernel/kapi.h>

Returns:

"true" if threads are being blocked, "false" otherwise.

See Also:

cyg_flag_setbits, cyg_flag_maskbits, cyg_flag_wait, cyg_flag_timed_wait, cyg_flag_poll, cyg_flag_peek

 

原文见:http://www.navosha.com/ecos/c/cyg_flag.html

在 eCos 操作系统中,`cyg_alarm_create` 是用于创建一个 **软件报警(software alarm)对象** 的函数,它是使用定时器功能的第一步。报警对象通常与一个硬件时钟(clock)相关联,并在特定时间点触发一个回调函数。 --- ## 一、函数原型 ```c void cyg_alarm_create(cyg_clock_t* clock, cyg_alarm_t* alarm, cyg_handle_t* handle, cyg_alarm_fn* action, cyg_addrword_t data); ``` 或者更完整的原型(取决于 eCos 版本): ```c void cyg_alarm_create(cyg_clock_t* clock, cyg_alarm_t* alarm, cyg_handle_t* handle, cyg_alarm_fn* action, cyg_addrword_t data, cyg_tick_count_t trigger, cyg_tick_count_t period, cyg_bool_t enabled); ``` --- ## 二、参数说明 | 参数 | 说明 | |------|------| | `clock` | 与报警对象关联的时钟指针(通常为 `cyg_real_time_clock()`) | | `alarm` | 预分配的 `cyg_alarm_t` 结构体对象,用于存储报警信息 | | `handle` | 返回的报警句柄,后续操作使用 | | `action` | 报警触发时调用的回调函数 | | `data` | 回调函数的参数(用户自定义) | | `trigger` | 初始触发时间(可选) | | `period` | 周期间隔(0 表示只触发一次) | | `enabled` | 是否立即启用报警(`true` 或 `false`) | --- ## 三、使用流程 通常使用 `cyg_alarm_create` 的流程如下: 1. 获取系统时钟(`cyg_real_time_clock()`) 2. 定义并初始化一个 `cyg_alarm_t` 对象 3. 调用 `cyg_alarm_create` 创建报警对象 4. 调用 `cyg_alarm_initialize` 或直接设置触发时间 5. 触发后执行回调函数 --- ## 四、完整示例代码 ```c #include <cyg/kernel/kapi.h> #include <cyg/alarm/alarm.h> #include <cyg/io/io.h> // diag_printf // 回调函数 void my_alarm_callback(cyg_handle_t alarm, cyg_addrword_t data) { diag_printf("Alarm triggered! Data: %d\n", (int)data); } int main(void) { cyg_clock_t* sys_clock = cyg_real_time_clock(); // 获取系统时钟 cyg_alarm_t my_alarm; // 报警对象 cyg_handle_t alarm_handle; // 报警句柄 // 创建报警对象 cyg_alarm_create(sys_clock, &my_alarm, &alarm_handle, my_alarm_callback, (cyg_addrword_t)0x1234); // 初始化报警:100 ticks 后触发,之后每 50 ticks 触发一次 cyg_alarm_initialize(alarm_handle, cyg_current_time() + 100, 50); diag_printf("Alarm scheduled. Waiting for triggers...\n"); // 保持主线程运行,等待报警触发 cyg_thread_delay(500); // 禁用报警 cyg_alarm_disable(alarm_handle); diag_printf("Alarm disabled.\n"); return 0; } ``` --- ## 五、解释 - `cyg_real_time_clock()` 获取系统主时钟。 - `cyg_alarm_create()` 将报警对象与系统时钟绑定,并设置回调函数。 - `cyg_alarm_initialize()` 设置报警的触发时间和周期。 - 报警回调函数在中断上下文中执行,因此不能执行阻塞操作或调用非中断安全函数。 - `cyg_alarm_disable()` 可以用来停止报警的触发。 --- ## 六、相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值