一、起始
一天心血来潮,看到freeRtos\usos\rt-thread这些优秀系统,自己也想造个轮子。但是这些rtos实现困难无比,尤其这对于我这个知识储备可怜至极的人。真rtos太难,现在把目光聚焦到ble、zigbee协议中的osal系统,基于轮询的一个系统,可以说完美符合我的心态。
二、目标
不要求低开销,不要求多功能,甚至不要求能用(其实是懒得debug,如果有人觉得有用,我会继续写下去)。
三、代码
- ses.c
/*
* @Author: LC ??????@qq.com
* @Date: 2022-10-10 23:01:11
* @LastEditors: LC ??????@qq.com
* @LastEditTime: 2022-10-23 03:13:38
* @FilePath: \easy sample System\ses.c
* @Description:
* ses: (easy sample System)目标是越简单越好,什么抢占、恢复、多线程都是邪教
* Copyright (c) 2022 by LC ??????@qq.com, All Rights Reserved.
*/
#include "ses.h"
/**
* @fn :
* @brief : 系统启动,不返回
* @return {*}
* @attention :
* @author : LC
* @param {SES} *self
*/
static void SES_systemRum(SES *self)
{
while(1){
#if(_BIND_PHYSICAL_TIME == 0) //是否将心跳绑定物理时间,否则心跳只是代表轮询次数
#if _OCCUPY_SENSOR//心跳快慢在一些简单应用中会特别快,或者在无事发生,系统占用率特别低时,故这里是必须的,但在一些对性能要求特别高的应用,这里的执行是不必要的
delay_us(100 - self->_ses_occupy%100);
#else
delay_us(100);
#endif
#endif
for(uint8_t i=0; i<sizeof(self->_manager); i++){
if(self->_manager[i]->_event != 0 && self->_manager[i]->_state == _ready){
self->_manager[i]->_state = _run;
self->_manager[i]->_event = self->_manager[i]->_p(self, self->_manager[i]->_event);//没处理完的事件在下次会重复触发
self->_manager[i]->_state = _sleepLow;//退回休眠态,等待定时器唤醒
}
}
self->_idleControl._event = self->_idleControl._p(self, self->_idleControl._event);//空闲线程独立于管理数组,默认存在,不准删除
}
}
/**
* @fn :
* @brief : 构造函数
* @return {*}-
* @attention :
* @author : LC
* @param {SES_functionManager_t} *_manager
* @param {uint8_t} _ses_threadNum
*/
SES systemCreate(SES_functionManager_t *_manager[], uint8_t _ses_threadNum)
{
SES ses;
ses.systemRum = SES_systemRum;
ses.getTick = SES_getTick;
ses.increaseTick = SES_increaseTick;
ses.createIdlehook = SES_createIdlehook;
ses.deleteIdlehook = SES_deleteIdlehook;
ses.wake = SES_wake;
ses.sleep = SES_sleep;
ses.clock_wakeOne = SES_clock_wakeOne;
ses.clock_wakeCycle = SES_clock_wakeCycle;
#if _MESSAGE_QUEUE
ses.queue_create = SES_queue_create;
ses.queue_delete = SES_queue_delete;
ses.queue_Send = SES_queue_Send;
ses.queue_receive = SES_queue_receive;
#endif
ses._ses_registration = _ses_registration;
ses.idle = SES_idle;
ses._idleControl._event =0, ses._idleControl._p = ses.idle, ses._idleControl._state = _ready;//定义空闲线程的线程控制块
ses._ses_cycleCount = 0;
ses._ses_timerList_Tick[0][0] = 0;
ses._hookList[0] = NULL;
ses._manager = _manager;
ses._ses_threadNum = _ses_threadNum;
#if _MESSAGE_QUEUE
ses._ses_queue[0] = NULL;
#endif
#if _OCCUPY_SENSOR
ses._ses_occupy = 0;
#endif
return ses;
}
/**
* @fn :
* @brief : 不提供与物理时间强相关的心跳,而是提供轮询的轮询次数,也叫Tick
* @return {*}
* @attention :
* @author : LC
* @param {SES} *self
*/
static uint32_t SES_getTick(SES *self)
{
return self->_ses_cycleCount;
}
/**
* @fn :
* @brief : 登记定时器,定时器数量自由设置,每个定时器带来6字节全局开销
* @return {*}
* @attention :
* @author : LC
* @param {SES} *self
* @param {uint8_t} index
* @param {uint16_t} _Tick
* @param {uint8_t} keep
*/
error_t _ses_registration(SES *self, uint8_t index, uint16_t _Tick, uint8_t keep)//登记定时器
{
error_t result = 0;
if(self->_ses_timerList_Tick[_MAX_TIMER_NUM-1][0] != 0){
result = _no_location;//定时器无位置,可以通过设置_MAX_TIMER_NUM设置定时器数量,每个定时器带来6字节全局开销,默认16个定时器
return result;
}
for(int i=0; i<_MAX_TIMER_NUM; i++){
if(_Tick < self->_ses_timerList_Tick[i][1]){//发现升序数组的插入点
for(int j=_MAX_TIMER_NUM-1; j>i; j++){
self->_ses_timerList_Tick[j][0] = self->_ses_timerList_Tick[j-1][0];
self->_ses_timerList_Tick[j][1] = self->_ses_timerList_Tick[j-1][1];
self->_ses_timerList_index[j][0] = self->_ses_timerList_index[j-1][0];
self->_ses_timerList_index[j][1] = self->_ses_timerList_index[j-1][1];
}
self->_ses_timerList_Tick[i][0] = _Tick;
self->_ses_timerList_Tick[i][1] = _Tick;
self->_ses_timerList_index[i][0] = index;
self->_ses_timerList_index[i][1] = keep;
}
}
return result;
}
/**
* @fn :
* @brief : 创建空闲线程的回调钩子,最大数量为:_IDLE_HOOK_NUMBER个
* @return {*}
* @attention :
* @author : LC
* @param {SES} *self
* @param {_functionVoid_t} p
*/
static error_t SES_createIdlehook(SES *self, _functionVoid_t p)
{
error_t result = 0;
for(uint8_t i=0; i<_IDLE_HOOK_NUMBER; i++){
if(self->_hookList[i] == NULL){
self->_hookList[i] = p;
break;
}
if(i == _IDLE_HOOK_NUMBER - 1){//若在最后,钩子仍不为空,返回_no_location错误
result = _no_location;
}
}
return result;
}
/**
* @fn :
* @brief : 删除空闲线程的回调钩子
* @return {*}
* @attention : 会自动整理钩子
* @author : LC
* @param {SES} *self
* @param {_functionVoid_t} p
*/
static error_t SES_deleteIdlehook(SES *self, _functionVoid_t p)
{
error_t result = 0;
for(uint8_t i=0; i<_IDLE_HOOK_NUMBER; i++){
if(self->_hookList[i] == p){
self->_hookList[i] = NULL;
for(; i<_IDLE_HOOK_NUMBER-1; i++){
if(self->_hookList[i+1] != NULL){
self->_hookList[i] = self->_hookList[i+1];
}
self->_hookList[i+1] = NULL;
}
break;
}
if(i == _IDLE_HOOK_NUMBER - 1){//若在最后,钩子仍不为空,返回_no_define错误
result = _no_define;
}
}
return result;
}
/**
* @fn :
* @brief : 唤醒休眠的线程,如果轮转优先级在自己之上则将会在下一周期唤醒,否则当前循环。
* @return {*}
* @attention : 无法唤醒以及休眠自己,等价于:SES_clock_wakeOne(&a,b,0);
* @author : LC
* @param {SES} *self
* @param {SES_functionManager_t} *_controlBlock
* @param {uint16_t} _event
*/
static error_t SES_wake(SES *self, SES_functionManager_t *_controlBlock, uint16_t _event)
{
error_t result = 0;
for(uint8_t i=0; i<self->_ses_threadNum; i++){
if(self->_manager[i]->_p == _controlBlock){
_controlBlock->_state = _ready;
_controlBlock->_event |= _event;
result = 0;//找到了线程控制器
break;
}
result = _no_define;
}
return result;
}
/**
* @fn :
* @brief : 线程挂起函数,当使用后从下次循环开始将对事件不再响应,直到SES_wake()唤醒,注意事件唤醒后仍然生效
* @return {*}
* @attention : 挂起线程且仅能被“SES_wake()”函数唤醒,不能休眠空闲线程
* @author : LC
* @param {SES} *self
* @param {SES_functionManager_t} *_controlBlock
*/
static error_t SES_sleep(SES *self, SES_functionManager_t *_controlBlock)
{
error_t result = 0;
for(uint8_t i=0; i<self->_ses_threadNum; i++){
if(self->_manager[i]->_p == _controlBlock){
_controlBlock->_state = _sleepHigh;
result = 0;//找到了线程控制器
break;
}
result = _no_define;
}
return result;
}
/**
* @fn :
* @brief : 在设定的循环周期之后启动一次,只会暂时占据定时器一个位置,且_Tick>0。若为深度休眠,调用该函数无效, 会暂时占据定时器位置
* @return {*}
* @attention : 一般来说,这个方法提供给其他线程以获得受限的对其他线程的控制权
* @author : LC
* @param {SES} *self
* @param {SES_functionManager_t} *_controlBlock
* @param {uint16_t} _event
* @param {uint16_t} _Tick
*/
static error_t SES_clock_wakeOne(SES *self, SES_functionManager_t *_controlBlock, uint16_t _event, uint16_t _Tick)
{
error_t result = 0;
uint8_t i;
for(i=0; i<self->_ses_threadNum; i++){//休眠线程
if(self->_manager[i]->_p == _controlBlock){
_controlBlock->_state = _sleepLow;
_controlBlock->_event |= _event;
result = 0;//找到了线程控制器
break;
}
result = _no_define;
}
if(result != 0){
return result;
}
result = self->_ses_registration(self, i, _Tick, 0);
return result;
}
/**
* @fn :
* @brief : 循环在设定的循环周期之后启动,一经调用永远占据定时器的一个位置,每个线程应当避免循环调用该函数的情况
* @return {*}
* @attention : 一般来说,这个方法提供给线程自身以获得完全的对自身线程的控制权,但需要谨慎使用,尤其注意的是:循环进入的线程可能循环调用该函数撑爆定时器数量最大值
* @author : LC
* @param {SES} *self
* @param {SES_functionManager_t} *_controlBlock
* @param {uint16_t} _event
* @param {uint16_t} _Tick
*/
static error_t SES_clock_wakeCycle(SES *self, SES_functionManager_t *_controlBlock, uint16_t _event, uint16_t _Tick)
{
error_t result = 0;
uint8_t i;
for(i=0; i<self->_ses_threadNum; i++){//休眠线程
if(self->_manager[i]->_p == _controlBlock){
_controlBlock->_state = _sleepLow;
_controlBlock->_event |= _event;
result = 0;//找到了线程控制器
break;
}
result = _no_define;
}
if(result != 0){
return result;
}
result = self->_ses_registration(self, i, _Tick, 1);
return result;
}
#if _MESSAGE_QUEUE
/**
* @fn :
* @brief : 创建一个队列,可创建的最大值通过设置宏:_MAX_QUEUE_NUM,typeSize <= 256-4
* @return {*}
* @attention :
* @author : LC
* @param {SES} *self
* @param {uint8_t} lenth
* @param {uint8_t} typeSize
*/
static void *SES_queue_create(SES *self, uint8_t lenth, uint8_t typeSize)
{
uint8_t *_queue = NULL;
for(uint8_t i=0; i<_MAX_QUEUE_NUM; i++){
if(self->_ses_queue[i] == NULL){
_queue = SES_malloc(lenth*(typeSize+sizeof(SES_queue_t))+2);
if(i != _MAX_QUEUE_NUM-1){//为了标记队列结尾
self->_ses_queue[i+1] = NULL;
}
}
}
if(_queue == NULL){
return (void *)_queue;
}
for(uint16_t i=0; i<lenth*(typeSize+sizeof(SES_queue_t)); i++){//初始化
_queue[i] = 0;
}
_queue[0] = lenth;
_queue[1] = typeSize;
return (void *)_queue;
}
/**
* @fn :
* @brief : 删除整个队列,在队列管理数组中是有整理的
* @return {*}
* @attention :
* @author : LC
* @param {SES} *self
* @param {void} **queue
*/
static error_t SES_queue_delete(SES *self, void **queue)
{
error_t result = 0;
if(*queue == NULL){
result = _void_point;
}else{
for(uint8_t i=0; i<_MAX_QUEUE_NUM; i++){
if(self->_ses_queue[i] == *queue){
self->_ses_queue[i] = NULL;
for(uint8_t j=i; j<_MAX_QUEUE_NUM - 1; j++){
self->_ses_queue[i] = self->_ses_queue[i+1];//覆盖
if(self->_ses_queue[i] == NULL){//到结尾了
break;
}
}
}
}
SES_free(*queue);
*queue = NULL;
}
return result;
}
/**
* @fn :
* @brief :
* @return {*}
* @attention : 不会整理队列,只要有空位就插入,值得注意消息的发送并不会让休眠的线程被唤醒
* @author : LC
* @param {SES} *self
* @param {SES_functionManager_t} *_controlBlock
* @param {void} *queue
* @param {void} *point
*/
static error_t SES_queue_Send(SES *self, SES_functionManager_t *_controlBlock, void *queue, void *point)
{
error_t result = 0;
void *get;
if(queue == NULL || point == NULL){
result = _void_point;
return result;
}
for(uint8_t i=0; i<self->_ses_threadNum; i++){//控制块是否在线程管理数组有注册
if(self->_manager[i] == _controlBlock){
result = 0;
break;
}
result = _no_define;
}
if(result != 0){
return result;
}
for(uint8_t i=0; i<((uint8_t *)queue)[0]; i++){
get = &(((uint8_t *)queue)[2 + i*(((uint8_t *)queue)[1] + sizeof(SES_queue_t))]);
if(((SES_queue_t *)get)->control_block == NULL){//找到一个空位置
((SES_queue_t *)get)->control_block = _controlBlock;
get = (SES_queue_t *)get + 1;
for(int8_t j=0; j<((uint8_t *)queue)[1]; j++){
((uint8_t *)get)[j] = ((uint8_t *)point)[j];
}
result = 0;
break;
}
result = _no_location;
}
return result;
}
/**
* @fn :
* @brief : 接收一个消息
* @return {*}
* @attention :
* @author : LC
* @param {SES} *self
* @param {SES_functionManager_t} *_controlBlock
* @param {void} *queue
* @param {void} *point
*/
static error_t SES_queue_receive(SES *self, SES_functionManager_t *_controlBlock, void *queue, void *point)
{
error_t result = 0;
void *get;
if(queue == NULL || point == NULL){
result = _void_point;
return result;
}
for(uint8_t i=0; i<self->_ses_threadNum; i++){//控制块是否在线程管理数组有注册
if(self->_manager[i] == _controlBlock){
result = 0;
break;
}
result = _no_define;
}
if(result != 0){
return result;
}
for(uint8_t i=0; i<((uint8_t *)queue)[0]; i++){
get = &(((uint8_t *)queue)[2 + i*(((uint8_t *)queue)[1] + sizeof(SES_queue_t))]);
if(((SES_queue_t *)get)->control_block == _controlBlock){//找到队列
get = (SES_queue_t *)get + 1;
((SES_queue_t *)get)->control_block = NULL;//删除队列
for(int8_t j=0; j<((uint8_t *)queue)[1]; j++){//复制
((uint8_t *)point)[j] = ((uint8_t *)get)[j];
}
result = 0;
break;
}
result = _no_define;
}
return result;
}
#endif
/**
* @fn :
* @brief :
* @return {*}
* @attention :
* @author : LC
* @param {SES} *self
*/
static error_t SES_increaseTick(SES *self)
{
self->_ses_cycleCount++;
}
/**
* @fn :
* @brief : 空闲函数,处理休眠唤醒、内核占用计算等功能
* @return {*}
* @attention :
* @author : LC
* @param {SES} *self
* @param {uint16_t} _event
*/
void SES_idle(SES *self, uint16_t _event)
{
static uint32_t _hisTick = 0;
/*空闲函数必须有的功能,在汇编阶段用宏定义开启*/
#if _OCCUPY_SENSOR
#if _BIND_PHYSICAL_TIME//通过空闲线程与用户定义的线程的运行时间做比较得出占用率
static uint32_t firstTick = 0, afterTick = 0;//当绑定了物理时间,心跳即运行时间
uint32_t _idle_runTime = (afterTick - firstTick);
firstTick = self->getTick(self);
uint32_t _thread_runTime = (firstTick - afterTick);
self->_ses_occupy = 100 - _idle_runTime*100 / (_idle_runTime + _thread_runTime);
#else
__nop();
#endif
#endif
#if _LOW_POWER_DISSIPATION//低功耗(通过器件休眠与唤醒, 与具体单片机相关,这里不实现)
__nop();
#endif
#if _MESSAGE_QUEUE
for(uint8_t i=0; i<_MAX_QUEUE_NUM; i++){
if(self->_ses_queue[i] != NULL){//队列非空
for(uint8_t j=0; j<((uint8_t *)(self->_ses_queue[i]))[0]; j++){
void *get = &(((uint8_t *)(self->_ses_queue[i]))[2 + j*(((uint8_t *)(self->_ses_queue[i]))[1] + sizeof(SES_queue_t))]);
if(((SES_queue_t *)get)->control_block != NULL){//控制块指针不为空
for(uint8_t k=0; k<self->_ses_threadNum; k++){//试着查找控制块
if(((SES_queue_t *)get)->control_block == self->_manager[k]){//找到了
self->_manager[k]->_event |= (1<<16);//最高位默认为消息通知事件
}else if(k == self->_ses_threadNum - 1){//在最后都没有找到
exit(EXIT_FAILURE);//发生了严重错误
}
}
}
}
}else{
break;
}
}
#endif
#if _BIND_PHYSICAL_TIME //是否将心跳绑定物理时间,否则心跳只是代表轮询次数
/*需要实现单片机滴答定时器,并在中断溢出函数中每毫秒调用函数:self->increaseTick(self);*/
#else
/*当前循环次数,自增*/
self->increaseTick(self);
#endif
for(int16_t i=0; i<_MAX_TIMER_NUM; i++){//清除单次唤醒完成的定时器
if(self->_ses_timerList_Tick[i][0] == 0){
break;//_Tick遇到零就是无定时器
}
if(self->_ses_timerList_Tick[i][1] == 0){//如果没有重装载
for(int j=_MAX_TIMER_NUM-1; j>i; j++){//覆盖
self->_ses_timerList_Tick[j][0] = self->_ses_timerList_Tick[j+1][0];
self->_ses_timerList_Tick[j][1] = self->_ses_timerList_Tick[j+1][1];
self->_ses_timerList_index[j][0] = self->_ses_timerList_index[j+1][0];
self->_ses_timerList_index[j][1] = self->_ses_timerList_index[j+1][1];
}
self->_ses_timerList_Tick[_MAX_TIMER_NUM-1][0] = 0;//删除定时器
self->_ses_timerList_Tick[_MAX_TIMER_NUM-1][1] = 0;
self->_ses_timerList_index[_MAX_TIMER_NUM-1][0] = 0;
self->_ses_timerList_index[_MAX_TIMER_NUM-1][1] = 0;
}
}
for(int16_t i=0; i<_MAX_TIMER_NUM; i++){//唤醒到点的线程
if(self->_ses_timerList_Tick[i][0] != 0){//定时器非空
if(self->_ses_timerList_Tick[i][1] != 0){
self->_ses_timerList_Tick[i][1] -= (self->getTick(self) - _hisTick);
self->_manager[self->_ses_timerList_index[i][0]]->_state = _sleepLow;//进入休眠
}else{
if(self->_ses_timerList_index[i][1] == 1){//重装载
self->_ses_timerList_Tick[i][1] == self->_ses_timerList_Tick[i][0];
}else{//不重装载,在下一循环会删除定时器
__nop();
}
if(self->_manager[self->_ses_timerList_index[i][0]]->_state != _sleepHigh){
self->_manager[self->_ses_timerList_index[i][0]]->_state = _ready;//就绪
}
}
}
}
/*调用钩子回调,默认开启,会在NULL处停止,故要求_hookList要整理hook*/
for(int i=0; i<_IDLE_HOOK_NUMBER; i++){
if(self->_hookList[i] != NULL){
(self->_hookList[i])();
}else{
break;
}
}
#if _OCCUPY_SENSOR
#if _BIND_PHYSICAL_TIME
afterTick = self->getTick(self);
#else//通过被唤醒的线程数量判定占用率
uint8_t count = 0;
for(uint8_t i=0; i<sizeof(self->_manager); i++){
if(self->_manager[i]->_event != 0 && self->_manager[i]->_state == _ready){
count++;
}
}
self->_ses_occupy = 100*count/self->_ses_threadNum;
#endif
#endif
if(self->getTick(self) > 0xffffff00){
self->_ses_cycleCount = 0;
}
_hisTick = self->getTick(self);
}
- ses.h
/*
* @Author: LC ???????@qq.com
* @Date: 2022-10-10 23:02:03
* @LastEditors: LC ????????@qq.com
* @LastEditTime: 2022-10-23 02:48:28
* @FilePath: \easy sample System\ses.h
* @Description:
* 命名规则:属于ses系统的函数以“ SES_ ”开头,结构体还需要后加“ _t ”
* 属于ses系统的全局变量以“ _ ”开头后跟小写
* 属于ses系统的宏定义以“ _ ”开头后跟大写,枚举元素不论大小写但不准用驼峰法命名
* 全局:_manager --- 该数组在注册线程必须用到
*
* Copyright (c) 2022 by LC ??????@qq.com, All Rights Reserved.
*/
#ifndef __SES_H__
#define __SES_H__
#include "stdint.h"
#include "stddef.h"
#include "stdlib.h"
/* ----基本类型------ */
#define uint32_t unsigned long int
#define uint16_t unsigned int
#define uint8_t unsigned char
/* ----------------- */
/* ----特殊类型------ */
typedef enum{//自定义全局错误,系统占用头8种(默认0是无错误)
//--------------------------------------------------------------
_no_define = 1, //无定义,一般是找不到控制块、队列等
_no_location, //没位置,一般是队列满、钩子管理器与定时管理器满
_void_point, //空指针,
_reserve_4,
_reserve_5,
_reserve_6,
_reserve_7,
_reserve_8,
//--------------------------------------------------------------
}error_t;
typedef enum{
_ready = 1, //就绪态,等待其他线程出让cpu控制权
_sleepLow, //休眠态,会被定时器唤醒
_sleepHigh, //休眠态,直到唤醒不会参与运行
_run, //运行态,退出运行会进入就绪态
}state_t;
/* ----------------- */
/* ---系统重要定义--- */
#define SES_malloc(x) malloc(x)
#define SES_free(x) free(x)
/* ---系统重要定义--- */
/* ------系统的静态元素数量-------- */
#define _IDLE_HOOK_NUMBER 8 //空闲钩子的数量
#define _MAX_TIMER_NUM 16 //设置的最大定时器数量
#define _MAX_QUEUE_NUM 16 //同一时间的最大队列数量,队列的大小不限制
/* ------------------------------ */
/* ---想要开启的功能--- */
#define _LOW_POWER_DISSIPATION 0//通过控制休眠以及唤醒来实现低功耗模式
#define _OCCUPY_SENSOR 1//占用传感器:感知内核占用情况
#define _MESSAGE_QUEUE 1//开启消息队列
#define _BIND_PHYSICAL_TIME 0//绑定物理时间,这个需要定时器,
/* -------------------- */
typedef void (*_functionVoid_t)();
typedef uint16_t (*functionThread_t)(void *, uint16_t);
typedef struct
{
uint16_t _event; //一个最大16个事件的线程,_event != 0意味着事件发生
functionThread_t _p;//事件处理函数,参数应当传入当前线程的事件变量_event
state_t _state; //指示当前线程的状态
}SES_functionManager_t;
typedef struct
{
/* data */
void *control_block;
}SES_queue_t;
typedef struct
{
/* data */
SES_functionManager_t **_manager;
uint8_t _ses_threadNum;
#if _MESSAGE_QUEUE
void *_ses_queue[_MAX_QUEUE_NUM];
#endif
#if _OCCUPY_SENSOR
uint8_t _ses_occupy;
#endif
/* -----系统重要变量----- */
uint32_t _ses_cycleCount;//系统的循环次数
uint8_t _ses_timerList_index[_MAX_TIMER_NUM][2]; //0:控制块注册数组的指定线程的下标, 1: 持续性(0是不保持,1是保持)
uint16_t _ses_timerList_Tick[_MAX_TIMER_NUM][2]; //2:控制块注册数组的指定线程希望延时触发的心跳, 3: 还需要等待的次数
/* ----------钩子相关成员-------------- */
_functionVoid_t _hookList[_IDLE_HOOK_NUMBER]; //空闲线程的钩子函数列表
SES_functionManager_t _idleControl;
/* 成员 */
error_t (* systemRum)(void *self);
error_t (* getTick)(void *self);
void (* increaseTick)(void *self);
error_t (* createIdlehook)(void *self, _functionVoid_t p);
error_t (* deleteIdlehook)(void *self, _functionVoid_t p);
error_t (* wake)(void *self, SES_functionManager_t *_controlBlock, uint16_t _event);//无法唤醒自己
error_t (* sleep)(void *self, SES_functionManager_t *_controlBlock);//挂起线程且仅能被“SES_wake()”函数唤醒
error_t (* clock_wakeOne)(void *self, SES_functionManager_t *_controlBlock, uint16_t _event, uint16_t _Tick);
error_t (* clock_wakeCycle)(void *self, SES_functionManager_t *_controlBlock, uint16_t _event, uint16_t _Tick);
#if _MESSAGE_QUEUE
void *(* queue_create)(void *self, uint8_t lenth, uint8_t typeSize);
error_t (* queue_delete)(void *self, void **queue);
error_t (* queue_Send)(void *self, SES_functionManager_t *_controlBlock, void *queue, void *point);
error_t (* queue_receive)(void *self, SES_functionManager_t *_controlBlock, void *queue, void *point);
#endif
error_t (* _ses_registration)(void *self, uint8_t index, uint16_t _Tick, uint8_t keep);//内部成员,不该被调用
error_t (* idle)(void *self, uint16_t _event);//内部成员,不该被调用
}SES;
SES systemCreate(SES_functionManager_t *_manager[], uint8_t _ses_threadNum);
static void SES_systemRum(SES *self);
static uint32_t SES_getTick(SES *self);
static void SES_increaseTick(SES *self);
static error_t SES_createIdlehook(SES *self, _functionVoid_t p);
static error_t SES_deleteIdlehook(SES *self, _functionVoid_t p);
static error_t SES_wake(SES *self, SES_functionManager_t *_controlBlock, uint16_t _event);//无法唤醒自己
static error_t SES_sleep(SES *self, SES_functionManager_t *_controlBlock);//挂起线程且仅能被“SES_wake()”函数唤醒
static error_t SES_clock_wakeOne(SES *self, SES_functionManager_t *_controlBlock, uint16_t _event, uint16_t _Tick);
static error_t SES_clock_wakeCycle(SES *self, SES_functionManager_t *_controlBlock, uint16_t _event, uint16_t _Tick);
#if _MESSAGE_QUEUE
#define _MESSAGE_EVENT (1<<15) //消息事件默认占用最高比特位
static void *SES_queue_create(SES *self, uint8_t lenth, uint8_t typeSize);
static error_t SES_queue_delete(SES *self, void **queue);
static error_t SES_queue_Send(SES *self, SES_functionManager_t *_controlBlock, void *queue, void *point);
static error_t SES_queue_receive(SES *self, SES_functionManager_t *_controlBlock, void *queue, void *point);
#endif
/*----系统内部函数-----*/
static error_t _ses_registration(SES *self, uint8_t index, uint16_t _Tick, uint8_t keep);//登记定时器
static error_t SES_idle(SES *self, uint16_t _event);
#endif
- main.c
/*
* @Author: LC ??????????@qq.com
* @Date: 2022-10-16 21:50:59
* @LastEditors: LC ??????????@qq.com
* @LastEditTime: 2022-10-18 00:54:10
* @FilePath: \easy sample System\main.c
* @Description:
*
* Copyright (c) 2022 by LC ???????@qq.com, All Rights Reserved.
*/
#include "ses.h"
#define START_EVENT (1<<0)
uint16_t hardwareInit_Thread(SES *self, uint16_t _event);
SES_functionManager_t hardwareInitControl = {._event=0, ._p=(functionThread_t)hardwareInit_Thread, ._state=_ready};
/* ------------控制块注册数组,注册线程只支持在汇编前写道该数组中,不允许动态方法---------- */
SES_functionManager_t *_manager[] = {//指针(的)数组
&hardwareInitControl,//线程控制器指针, 最开始调用的线程
};
int main(void){
SES ses = systemCreate(_manager, sizeof(_manager)/sizeof(SES_functionManager_t *));
/* ---硬件初始化调用--- */
/* ---系统启动--- */
ses.wake(&ses, &hardwareInitControl, START_EVENT);
ses.systemRum(&ses);
}
uint16_t hardwareInit_Thread(SES *self, uint16_t _event)
{
if(_event & START_EVENT){
return (_event ^ START_EVENT);//返回未处理的
}
}