Q群:
电话:
邮箱:
地址:
接口 | 说明 |
---|---|
os_hw_interrupt_disable | 关闭全局中断 |
os_hw_interrupt_enable | 恢复之前的中断状态,必须传入关闭中断时的返回值,否则系统运行将变得不确定 |
os_find_first_bit_set | 32位整型数中,从低位向高位,获取第1个置1的bit |
os_board_auto_init | 板级自动初始化接口,需要BSP在板级初始化程序中调用 |
该函数用于关闭全局中断,函数原型如下:
os_base_t os_hw_interrupt_disable(void);
参数 | 说明 |
---|---|
无 | 无 |
返回 | 说明 |
os_base_t | 返回os_hw_interrupt_disable函数运行前的中断状态 |
该函数用于恢复调用 os_hw_interrupt_disable()函数前的中断状态。需要注意的是:如果调用 os_hw_interrupt_disable()函数前是关中断状态,那么调用os_hw_interrupt_enable()后依然是关中断状态。恢复中断往往是和关闭中断成对使用的。函数原型如下:
void os_hw_interrupt_enable(os_base_t level);
参数 | 说明 |
---|---|
level | 需要传入上一次os_hw_interrupt_disable 返回的中断状态,否则系统运行将可能变得不确定 |
返回 | 说明 |
无 | 无 |
该函数用于在32位整形数中,从低位到高位获取第一个值为1的bit,函数原型如下:
os_int32_t os_find_first_bit_set(os_int32_t value);
参数 | 说明 |
---|---|
value | 任意一个32位整形数 |
返回 | 说明 |
os_int32_t | 从低位到高位中第一个值为1的bit。返回为0,表示未找到;1~32表示第一个为1的bit所在的位置 |
该函数用于板级自动初始化,需要BSP在板级初始化程序中调用,函数原型如下:
void os_board_auto_init(void);
本例中创建了2个任务,都会去访问临界资源(全局变量count),访问时用关全局中断来保护
#include <oneos_config.h>
#include <os_dbg.h>
#include <os_task.h>
#include <shell.h>
#include <os_hw.h>
#define TEST_TAG "TEST"
#define TASK_STACK_SIZE 1024
#define TASK1_PRIORITY 15
#define TASK2_PRIORITY 16
#define TASK_TIMESLICE 10
static os_uint32_t count = 0;
void task1_entry(void *para)
{
os_uint32_t readcount;
os_uint32_t level;
while (1)
{
level = os_hw_interrupt_disable();
count++;
readcount = count;
os_hw_interrupt_enable(level);
LOG_E(TEST_TAG, "task1_entry count:%d", readcount);
os_task_delay(100);
}
}
void task2_entry(void *para)
{
os_uint32_t readcount;
os_uint32_t level;
while (1)
{
level = os_hw_interrupt_disable();
count += 2;
readcount = count;
os_hw_interrupt_enable(level);
LOG_E(TEST_TAG, "task2_entry count:%d", readcount);
os_task_delay(100);
}
}
void interrupt_sample(void)
{
os_task_t *task1 = OS_NULL;
os_task_t *task2 = OS_NULL;
task1 = os_task_create("task1",
task1_entry,
OS_NULL,
TASK_STACK_SIZE,
TASK1_PRIORITY,
TASK_TIMESLICE);
if (task1)
{
os_task_startup(task1);
}
task2 = os_task_create("task2",
task2_entry,
OS_NULL,
TASK_STACK_SIZE,
TASK2_PRIORITY,
TASK_TIMESLICE);
if (task2)
{
os_task_startup(task2);
}
}
SH_CMD_EXPORT(interrupt_sample, interrupt_sample, "an sample of enable/disable interrupt");
运行结果如下:
sh />interrupt_sample
E/TEST: task1_entry count:1
E/TEST: task2_entry count:3
E/TEST: task1_entry count:4
E/TEST: task2_entry count:6
E/TEST: task1_entry count:7
E/TEST: task2_entry count:9
E/TEST: task1_entry count:10
E/TEST: task2_entry count:12
E/TEST: task1_entry count:13
E/TEST: task2_entry count:15
E/TEST: task1_entry count:16
......