📄 os.c
字号:
/**************************************************
文件名:os.c
功能:微操作系统c语言实现
说明:时钟:7.3728MHz
*****************************************************/
#include "os.h"
typedef struct SchedUnit{
void (*tp)(void);
} OSSchedUnit;
enum {
OSH_MAX_TASKS = 8,
OSH_TASK_BITMASK = OSH_MAX_TASKS - 1
};
OSSchedUnit OSH_queue[OSH_MAX_TASKS];
volatile uint8_t OSH_sched_full;
volatile uint8_t OSH_sched_free;
result_t MainHardwareInit(void)
{
return SUCCESS; //doing nothing
}
/*************************************************************************
*功能描述:任务调度队列初始化
*参数说明:
*返回值:
**************************************************************************/
void OSSchedInit(void )
{
OSH_sched_free = 0;
OSH_sched_full = 0;
}
void OSH_uwait(int u_sec)
{
while (u_sec > 0) {
__asm volatile ("nop");
__asm volatile ("nop");
__asm volatile ("nop");
__asm volatile ("nop");
__asm volatile ("nop");
__asm volatile ("nop");
__asm volatile ("nop");
__asm volatile ("nop");
u_sec--;
}
}
result_t MainStdControlInit(void)
{
return SUCCESS; //doing nothing
}
result_t MainStdControlStart(void)
{
return SUCCESS; //doing nothing
}
void EnableInterrupt(void)
{
__asm volatile ("sei");//开全局中断
}
void OSH_wait(void)
{
__asm volatile ("nop");//空指令
__asm volatile ("nop");
}
void OSH_sleep(void)
{
* (volatile unsigned char *)(0x35 + 0x20) |= 1 << 5;
__asm volatile ("sleep");
}
/*************************************************************************
*功能描述:原子操作的结束操作
*参数说明:
*返回值:
**************************************************************************/
void AtomicEnd(uint8_t oldSreg)
{
* (volatile unsigned char *)(0x3F + 0x20) = oldSreg;
}
/*************************************************************************
*功能描述:原子操作的起始操作
*参数说明:
*返回值:
**************************************************************************/
uint8_t AtomicStart(void )
{
uint8_t result = * (volatile unsigned char *)(0x3F + 0x20);//读取和保存状态寄存器
__asm volatile ("cli");
return result;
}
/*************************************************************************
*功能描述:提取任务队列中的任务
*参数说明:
*返回值:
**************************************************************************/
bool OSHRunNextTask(void)
{
uint8_t fInterruptFlags;
uint8_t old_full;
void (*func)(void );
if (OSH_sched_full == OSH_sched_free) {
return 0;
}
else {
fInterruptFlags = AtomicStart();
old_full = OSH_sched_full;
OSH_sched_full++;
OSH_sched_full &= OSH_TASK_BITMASK;
func = OSH_queue[(int )old_full].tp;
OSH_queue[(int )old_full].tp = 0;
AtomicEnd(fInterruptFlags);
func();
return 1;
}
}
void OSHRunTask(void)
{
while (OSHRunNextTask())
;
OSH_sleep();
OSH_wait();
}
/*************************************************************************
*功能描述:任务函数入任务对列
*参数说明:函数指针
*返回值:
**************************************************************************/
bool OSPostTask(void (*tp)(void))
{
uint8_t fInterruptFlags;
uint8_t tmp;
fInterruptFlags = AtomicStart();
tmp = OSH_sched_free;
OSH_sched_free++;
OSH_sched_free &= OSH_TASK_BITMASK;
if (OSH_sched_free != OSH_sched_full) {
AtomicEnd(fInterruptFlags);
OSH_queue[tmp].tp = tp;
return TRUE;
}
else {
OSH_sched_free = tmp;
AtomicEnd(fInterruptFlags);
return FALSE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -