?狀態機在嵌入式軟件中隨處可見,可能你會說狀態機有什么難的,不就是?switch 嗎? switch僅僅是最基礎的一個點,關于狀態機的更多操作,或許你都沒有見過,下面分享幾種實現方法。
1. 狀態機基本術語
現態:是指當前所處的狀態。
條件:又稱為“事件”,當一個條件被滿足,將會觸發一個動作,或者執行一次狀態的遷移。
動作:條件滿足后執行的動作。動作執行完畢后,可以遷移到新的狀態,也可以仍舊保持原狀態。動作不是必需的,當條件滿足后,也可以不執行任何動作,直接遷移到新狀態。
次態:條件滿足后要遷往的新狀態。“次態”是相對于“現態”而言的,“次態”一旦被激活,就轉變成新的“現態”了。
2. 傳統有限狀態機FSM
如下圖所示,這是一個定時計數器,計數器存在兩種狀態,一種為設置狀態,一種為計時狀態。
設置狀態:
“+” “-” 按鍵對初始倒計時進行設置
當計數值設置完成,點擊確認鍵啟動計時 ,即切換到計時狀態
計時狀態:
按下“+” “-” 會進行密碼的輸入“+”表示1 ,“-”表示輸入0 ,密碼共有4位
確認鍵:只有輸入的密碼等于默認密碼,按確認鍵才能停止計時,否則計時直接到零,并執行相關操作
3. 嵌套switch
?
/*************************************** 1.列出所有的狀態 ***************************************/ typedef?enum{ ??SETTING, ??TIMING }STATE_TYPE; /*************************************** 2.列出所有的事件 ***************************************/ typedef?enum{ ????UP_EVT, ??DOWN_EVT, ??ARM_EVT, ??TICK_EVT }EVENT_TYPE; /*************************************** 3.定義和狀態機相關結構 ***************************************/ struct??bomb { ??uint8_t?state; ??uint8_t?timeout; ??uint8_t?code; ??uint8_t?defuse_code; }bomb1; /*************************************** 4.初始化狀態機 ***************************************/ void?bomb1_init(void) { ??bomb1.state?=?SETTING; ??bomb1.defuse_code?=?6;????//0110? } /*************************************** 5.?狀態機事件派發 ***************************************/ void?bomb1_fsm_dispatch(EVENT_TYPE?evt?,void*?param) { ??switch(bomb1.state) ??{ ??????case?SETTING: ??????{ ??????????switch(evt) ??????????{ ??????????????case?UP_EVT:????//?"+"???按鍵按下事件 ????????????????if(bomb1.timeout60)??++bomb1.timeout; ??????????????????bsp_display(bomb1.timeout); ??????????????break; ??????????????case?DOWN_EVT:??//?"-"???按鍵按下事件 ??????????????????if(bomb1.timeout?>?0)??--bomb1.timeout; ??????????????????bsp_display(bomb1.timeout); ??????????????break; ??????????????case?ARM_EVT:???//?"確認"?按鍵按下事件 ??????????????????bomb1.state?=?TIMING; ??????????????????bomb1.code??=?0; ??????????????break; ??????????} ??????}?break;? ??????case?TIMING: ??????{ ??????????switch(evt) ??????????{ ??????????????case?UP_EVT:????//?"+"???按鍵按下事件 ?????????????????bomb1.code?=?(bomb1.code?<<1)?|0x01; ??????????????break; ??????????????case?DOWN_EVT:??//?"-"???按鍵按下事件 ??????????????????bomb1.code?=?(bomb1.code?<<1);? ??????????????break; ??????????????case?ARM_EVT:???//?"確認"?按鍵按下事件 ??????????????????if(bomb1.code?==?bomb1.defuse_code){ ??????????????????????bomb1.state?=?SETTING; ??????????????????} ??????????????????else{ ????????????????????bsp_display("bomb!") ??????????????????} ??????????????break; ??????????????case?TICK_EVT: ??????????????????if(bomb1.timeout) ??????????????????{ ??????????????????????--bomb1.timeout; ??????????????????????bsp_display(bomb1.timeout); ??????????????????} ??????????????????if(bomb1.timeout?==?0) ??????????????????{ ??????????????????????bsp_display("bomb!") ??????????????????} ??????????????break; ??????????}??? ??????}break; ??} }
?
優點:簡單,代碼閱讀連貫,容易理解
缺點:
當狀態或事件增多時,代碼狀態函數需要經常改動,狀態事件處理函數會代碼量會不斷增加
狀態機沒有進行封裝,移植性差。
沒有實現狀態的進入和退出的操作。進入和退出在狀態機中尤為重要。進入事件:只會在剛進入時觸發一次,主要作用是對狀態進行必要的初始化。退出事件:只會在狀態切換時觸發一次 ,主要的作用是清除狀態產生的中間參數,為下次進入提供干凈環境
4. 狀態表
二維狀態轉換表
狀態機可以分為狀態和事件 ,狀態的躍遷都是受事件驅動的,因此可以通過一個二維表格來表示狀態的躍遷。
僅當(code == defuse_code) 時才發生到setting 的轉換。
?
/*1.列出所有的狀態*/ enum { ??SETTING, ??TIMING, ??MAX_STATE }; /*2.列出所有的事件*/ enum { ??UP_EVT, ??DOWN_EVT, ??ARM_EVT, ??TICK_EVT, ??MAX_EVT }; /*3.定義狀態表*/ typedef?void?(*fp_state)(EVT_TYPE?evt?,?void*?param); static??const?fp_state??bomb2_table[MAX_STATE][MAX_EVENT]?= { ??{setting_UP?,?setting_DOWN?,?setting_ARM?,?null}, ??{setting_UP?,?setting_DOWN?,?setting_ARM?,?timing_TICK} }; struct?bomb_t { ??const?fp_state?const?*state_table;?/*?the?State-Table?*/ ??uint8_t?state;?/*?the?current?active?state?*/ ?? ??uint8_t?timeout; ??uint8_t?code; ??uint8_t?defuse_code; }; struct?bomb?bomb2= { ??.state_table?=?bomb2_table; } void?bomb2_init(void) { ??bomb2.defuse_code?=?6;?//?0110 ??bomb2.state?=?SETTING; } void?bomb2_dispatch(EVT_TYPE?evt?,?void*?param) { ??fp_state??s?=?NULL; ??if(evt?>?MAX_EVT) ??{ ??????LOG("EVT?type?error!"); ??????return; ??} ??s?=?bomb2.state_table[bomb2.state?*?MAX_EVT?+?evt]; ??if(s?!=?NULL) ??{ ??????s(evt?,?param); ??} } /*列出所有的狀態對應的事件處理函數*/ void?setting_UP(EVT_TYPE?evt,?void*?param) { ??if(bomb1.timeout60)??++bomb1.timeout; ??bsp_display(bomb1.timeout); }
?
缺點:函數粒度太小是最明顯的一個缺點,一個狀態和一個事件就會產生一個函數,當狀態和事件較多時,處理函數將增加很快,在閱讀代碼時,邏輯分散。沒有實現進入退出動作。
一維狀態轉換表
實現原理:
?
?typedef?void?(*fp_action)(EVT_TYPE?evt,void*?param); ???? ????/*轉換表基礎結構*/ ????struct?tran_evt_t ????{ ???????EVT_TYPE?evt; ????????uint8_t?next_state; ????}; ????/*狀態的描述*/ ????struct??fsm_state_t ????{ ????????fp_action??enter_action;??????//進入動作 ????????fp_action??exit_action;???//退出動作 ????????fp_action??action;??????????? ???????? ????????tran_evt_t*?tran;????//轉換表 ????????uint8_t?????tran_nb;?//轉換表的大小 ????????const?char*?name; ????} ????/*狀態表本體*/ ????#define??ARRAY(x)???x,sizeof(x)/sizeof(x[0]) ????const?struct??fsm_state_t??state_table[]= ????{ ????????{setting_enter?,?setting_exit?,?setting_action?,?ARRAY(set_tran_evt),"setting"?}, ????????{timing_enter?,?timing_exit?,?timing_action?,?ARRAY(time_tran_evt),"timing"?} ????}; ???? ????/*構建一個狀態機*/ ????struct?fsm ????{ ????????const?struct?state_t?*?state_table;?/*?the?State-Table?*/ ????????uint8_t?cur_state;??????????????????????/*?the?current?active?state?*/ ???????? ????????uint8_t?timeout; ????????uint8_t?code; ????????uint8_t?defuse_code; ????}bomb3; ???? ????/*初始化狀態機*/ ????void??bomb3_init(void) ????{ ????????bomb3.state_table?=?state_table;??//指向狀態表 ????????bomb3.cur_state?=?setting; ????????bomb3.defuse_code?=?8;?//1000 ????} ????/*狀態機事件派發*/ ????void??fsm_dispatch(EVT_TYPE?evt?,?void*?param) ????{ ????????tran_evt_t*?p_tran?=?NULL; ???????? ????????/*獲取當前狀態的轉換表*/ ????????p_tran?=?bomb3.state_table[bomb3.cur_state]->tran; ???????? ????????/*判斷所有可能的轉換是否與當前觸發的事件匹配*/ ????????for(uint8_t?i=0;ievt?==?evt)//事件會觸發轉換 ????????????{ ????????????????if(NULL?!=?bomb3.state_table[bomb3.cur_state].exit_action){ ??????????????bomb3.state_table[bomb3.cur_state].exit_action(NULL);??//執行退出動作 ?????????????} ????????????????if(bomb3.state_table[_tran[i]->next_state].enter_action){ ???????????????????bomb3.state_table[_tran[i]->next_state].enter_action(NULL);//執行進入動作 ????????????????} ????????????????/*更新當前狀態*/ ????????????????bomb3.cur_state?=?p_tran[i]->next_state; ????????????} ????????????else ????????????{ ?????????????????bomb3.state_table[bomb3.cur_state].action(evt,param); ????????????} ????????} ????} ????/************************************************************************* ????setting狀態相關 ????************************************************************************/ ????void?setting_enter(EVT_TYPE?evt?,?void*?param) ????{ ???????? ????} ????void?setting_exit(EVT_TYPE?evt?,?void*?param) ????{ ???????? ????} ????void?setting_action(EVT_TYPE?evt?,?void*?param) ????{ ???????? ????} ????tran_evt_t?set_tran_evt[]= ????{ ????????{ARM?,?timing}, ????} ????/*timing?狀態相關*/ ;i++)>
?
優點:
各個狀態面向用戶相對獨立,增加事件和狀態不需要去修改先前已存在的狀態事件函數。
實現了狀態的進入和退出
容易根據狀態躍遷圖來設計 (狀態躍遷圖列出了每個狀態的躍遷可能,也就是這里的轉換表)
實現靈活,可實現復雜邏輯,如上一次狀態,增加監護條件來減少事件的數量。可實現非完全事件驅動
缺點:
函數粒度較小(比二維小且增長慢),可以看到,每一個狀態需要至少3個函數,還需要列出所有的轉換關系。
5. QP嵌入式實時框架
事件驅動型編程
好萊塢原則:和傳統的順序式編程方法例如“超級循環”,或傳統的RTOS 的任務不同。絕大多數的現代事件驅動型系統根據好萊塢原則被構造(Don’t call me; I’ll call you.)
面向對象
類和單一繼承:
工具
QM :一個通過UML類圖來描述狀態機的軟件,并且可以自動生成C代碼
QS軟件追蹤工具:
6. QEP實現有限狀態機Fsm
/*?qevent.h?----------------------------------------------------------------*/ ??typedef?struct?QEventTag? ??{?? ????QSignal?sig;????? ????uint8_t?dynamic_;?? ??}?QEvent; ??/*?qep.h?-------------------------------------------------------------------*/ ??typedef?uint8_t?QState;?/*?status?returned?from?a?state-handler?function?*/ ??typedef?QState?(*QStateHandler)?(void?*me,?QEvent?const?*e);?/*?argument?list?*/ ??typedef?struct?QFsmTag???/*?Finite?State?Machine?*/ ??{? ????QStateHandler?state;?????/*?current?active?state?*/ ??}QFsm; ?? ??#define?QFsm_ctor(me_,?initial_)?((me_)->state?=?(initial_)) ??void?QFsm_init?(QFsm?*me,?QEvent?const?*e); ??void?QFsm_dispatch(QFsm?*me,?QEvent?const?*e); ?? ??#define?Q_RET_HANDLED?((QState)0) ??#define?Q_RET_IGNORED?((QState)1) ??#define?Q_RET_TRAN?((QState)2) ??#define?Q_HANDLED()?(Q_RET_HANDLED) ??#define?Q_IGNORED()?(Q_RET_IGNORED) ?? ???#define?Q_TRAN(target_)?(((QFsm?*)me)->state?=?(QStateHandler)???(target_),Q_RET_TRAN) ?? ??enum?QReservedSignals ??{ ??????Q_ENTRY_SIG?=?1,? ????Q_EXIT_SIG,? ????Q_INIT_SIG,? ????Q_USER_SIG? ??}; ?? ??/*?file?qfsm_ini.c?---------------------------------------------------------*/ ??#include?"qep_port.h"?/*?the?port?of?the?QEP?event?processor?*/ ??#include?"qassert.h"?/*?embedded?systems-friendly?assertions?*/ ??void?QFsm_init(QFsm?*me,?QEvent?const?*e)? ??{ ??????(*me->state)(me,?e);?/*?execute?the?top-most?initial?transition?*/ ????/*?enter?the?target?*/ ????(void)(*me->state)(me?,?&QEP_reservedEvt_[Q_ENTRY_SIG]); ??} ??/*?file?qfsm_dis.c?---------------------------------------------------------*/ ??void?QFsm_dispatch(QFsm?*me,?QEvent?const?*e) ??{ ??????QStateHandler?s?=?me->state;?/*?save?the?current?state?*/ ????QState?r?=?(*s)(me,?e);?/*?call?the?event?handler?*/ ????if?(r?==?Q_RET_TRAN)??/*?transition?taken??*/ ??????{ ????????????(void)(*s)(me,?&QEP_reservedEvt_[Q_EXIT_SIG]);?/*?exit?the?source?*/ ????????????(void)(*me->state)(me,?&QEP_reservedEvt_[Q_ENTRY_SIG]);/*enter?target*/ ????} ??} 實現上面定時器例子 ??#include?"qep_port.h"?/*?the?port?of?the?QEP?event?processor?*/ ??#include?"bsp.h"?/*?board?support?package?*/ ?? ??enum?BombSignals?/*?all?signals?for?the?Bomb?FSM?*/ ??{? ??????UP_SIG?=?Q_USER_SIG, ??????DOWN_SIG, ??????ARM_SIG, ??????TICK_SIG ??}; ??typedef?struct?TickEvtTag? ??{ ????QEvent?super;??????/*?derive?from?the?QEvent?structure?*/ ????uint8_t?fine_time;?/*?the?fine?1/10?s?counter?*/ ??}TickEvt; ?? ??typedef?struct?Bomb4Tag? ??{ ????QFsm?super;???/*?derive?from?QFsm?*/ ????uint8_t?timeout;?/*?number?of?seconds?till?explosion?*/ ????????uint8_t?code;????/*?currently?entered?code?to?disarm?the?bomb?*/ ????????uint8_t?defuse;??/*?secret?defuse?code?to?disarm?the?bomb?*/ ??}?Bomb4; ?? ??void?Bomb4_ctor?(Bomb4?*me,?uint8_t?defuse); ??QState?Bomb4_initial(Bomb4?*me,?QEvent?const?*e); ??QState?Bomb4_setting(Bomb4?*me,?QEvent?const?*e); ??QState?Bomb4_timing?(Bomb4?*me,?QEvent?const?*e); ??/*--------------------------------------------------------------------------*/ ??/*?the?initial?value?of?the?timeout?*/ ??#define?INIT_TIMEOUT?10 ??/*..........................................................................*/ ??void?Bomb4_ctor(Bomb4?*me,?uint8_t?defuse)?{ ????QFsm_ctor_(&me->super,?(QStateHandler)&Bomb4_initial); ????me->defuse?=?defuse;?/*?the?defuse?code?is?assigned?at?instantiation?*/ ??} ??/*..........................................................................*/ ??QState?Bomb4_initial(Bomb4?*me,?QEvent?const?*e)?{ ????(void)e; ????me->timeout?=?INIT_TIMEOUT; ????return?Q_TRAN(&Bomb4_setting); ??} ??/*..........................................................................*/ ??QState?Bomb4_setting(Bomb4?*me,?QEvent?const?*e)?{ ????switch?(e->sig){ ????????case?UP_SIG:{ ????????????if?(me->timeout?60)?{ ????????????????++me->timeout; ????????????????BSP_display(me->timeout); ????????????} ??????????????return?Q_HANDLED(); ????????} ????????case?DOWN_SIG:?{ ????????????if?(me->timeout?>?1)?{ ????????????????--me->timeout; ????????????????BSP_display(me->timeout); ????????????} ????????????return?Q_HANDLED(); ????????} ????????case?ARM_SIG:?{ ????????????return?Q_TRAN(&Bomb4_timing);?/*?transition?to?"timing"?*/ ????????} ????} ????return?Q_IGNORED(); ??} ??/*..........................................................................*/ ??void?Bomb4_timing(Bomb4?*me,?QEvent?const?*e)?{ ????switch?(e->sig)?{ ????????case?Q_ENTRY_SIG:?{ ????????????me->code?=?0;?/*?clear?the?defuse?code?*/ ????????????return?Q_HANDLED(); ??????????} ????????case?UP_SIG:?{ ????????????me->code?<<=?1; ????????????me->code?|=?1; ????????????return?Q_HANDLED(); ??????????} ????????case?DOWN_SIG:?{ ????????????me->code?<<=?1; ????????????return?Q_HANDLED(); ????????} ????????case?ARM_SIG:?{ ????????????if?(me->code?==?me->defuse)?{ ????????????????return?Q_TRAN(&Bomb4_setting); ????????????} ????????????return?Q_HANDLED(); ????????} ????????case?TICK_SIG:?{ ????????????if?(((TickEvt?const?*)e)->fine_time?==?0)?{ ????????????????--me->timeout; ????????????????BSP_display(me->timeout); ????????????????if?(me->timeout?==?0)?{ ????????????????BSP_boom();?/*?destroy?the?bomb?*/ ????????????????} ????????????} ????????????return?Q_HANDLED(); ????????} ????} ????return?Q_IGNORED(); ??}
?
優點:
采用面向對象的設計方法,很好的移植性
實現了進入退出動作
合適的粒度,且事件的粒度可控
狀態切換時通過改變指針,效率高
可擴展成為層次狀態機
缺點:
對事件的定義以及事件粒度的控制是設計的最大難點,如串口接收到一幀數據,這些變量的更新單獨作為某個事件,還是串口收到數據作為一個事件。再或者顯示屏,如果使用此種編程方式,如何設計事件。
7. QP實現層次狀態機
初始化層次狀態機的實現:在初始化時,用戶所選取的狀態永遠是最底層的狀態,如上圖,我們在計算器開機后,應該進入的是開始狀態,這就涉及到一個問題,由最初top(頂狀態)到begin 是有一條狀態切換路徑的,當我們設置狀態為begin如何搜索這條路徑成為關鍵(知道了路徑才能正確的進入begin,要執行路徑中過渡狀態的進入和退出事件)。
?
void?QHsm_init(QHsm?*me,?QEvent?const?*e)? ????{ ?????Q_ALLEGE((*me->state)(me,?e)?==?Q_RET_TRAN); ????????t?=?(QStateHandler)&QHsm_top;?/*?HSM?starts?in?the?top?state?*/ ??????do?{?/*?drill?into?the?target...?*/ ??????QStateHandler?path[QEP_MAX_NEST_DEPTH_]; ???????int8_t?ip?=?(int8_t)0;?/*?transition?entry?path?index?*/ ???????path[0]?=?me->state;?/*?這里的狀態為begin?*/ ???????????? ????????????/*通過執行空信號,從底層狀態找到頂狀態的路徑*/ ????????(void)QEP_TRIG_(me->state,?QEP_EMPTY_SIG_); ????????while?(me->state?!=?t)?{ ?????????path[++ip]?=?me->state; ???????(void)QEP_TRIG_(me->state,?QEP_EMPTY_SIG_); ??????} ????????????/*切換為begin*/ ???????me->state?=?path[0];?/*?restore?the?target?of?the?initial?tran.?*/ ??????/*?鉆到最底層的狀態,執行路徑中的所有進入事件?*/ ????????Q_ASSERT(ip?(int8_t)QEP_MAX_NEST_DEPTH_); ??????do?{?/*?retrace?the?entry?path?in?reverse?(desired)?order...?*/ ??????????QEP_ENTER_(path[ip]);?/*?enter?path[ip]?*/ ???????}?while?((--ip)?>=?(int8_t)0); ???????????? ????????t?=?path[0];?/*?current?state?becomes?the?new?source?*/ ???????}?while?(QEP_TRIG_(t,?Q_INIT_SIG)?==?Q_RET_TRAN); ??????me->state?=?t; ????}
?
狀態切換:
/*.................................................................*/ QState?result(Calc?*me,?QEvent?const?*e)? { ????switch?(e->sig)? ????{you ????????case?ENTER_SIG:{ ????????????break; ????????} ????????case?EXIT_SIG:{ ????????????break; ????????} ????????case?C_SIG:? ????????{ ????????????printf("clear");???? ????????????return?Q_HANDLED(); ????????} ????????case?B_SIG: ????????{?? ????????????return?Q_TRAN(&begin); ????????} ????} ????return?Q_SUPER(&reday); } /*.ready為result和begin的超狀態................................................*/ QState?ready(Calc?*me,?QEvent?const?*e)? { ????switch?(e->sig)? ????{ ????????case?ENTER_SIG:{ ????????????break; ????????} ????????case?EXIT_SIG:{ ????????????break; ????????} ????????case?OPER_SIG: ????????{?? ????????????return?Q_TRAN(&opEntered); ????????} ????} ????return?Q_SUPER(&on); } void?QHsm_dispatch(QHsm?*me,?QEvent?const?*e)? { ????QStateHandler?path[QEP_MAX_NEST_DEPTH_]; ????QStateHandler?s; ????QStateHandler?t; ????QState?r; ????t?=?me->state;?????/*?save?the?current?state?*/ ????do?{???????/*?process?the?event?hierarchically...?*/ ????????s?=?me->state; ????????r?=?(*s)(me,?e);???/*?invoke?state?handler?s?*/ ????}?while?(r?==?Q_RET_SUPER);?//當前狀態不能處理事件?,直到找到能處理事件的狀態 ???? ????if?(r?==?Q_RET_TRAN)?{?????/*?transition?taken??*/ ????????int8_t?ip?=?(int8_t)(-1);???/*?transition?entry?path?index?*/ ????????int8_t?iq;???????/*?helper?transition?entry?path?index?*/ ????????path[0]?=?me->state;????/*?save?the?target?of?the?transition?*/ ????????path[1]?=?t; ????????while?(t?!=?s)?{???/*?exit?current?state?to?transition?source?s...?*/ ????????????if?(QEP_TRIG_(t,?Q_EXIT_SIG)?==?Q_RET_HANDLED)?{/*exit?handled??*/ ????????????????(void)QEP_TRIG_(t,?QEP_EMPTY_SIG_);?/*?find?superstate?of?t?*/ ????????????} ????????????t?=?me->state;???/*?me->state?holds?the?superstate?*/ ????????} ?????.?.?. ????} ????me->state?=?t;?????/*?set?new?state?or?restore?the?current?state?*/ }
t?=?path[0];?/*?target?of?the?transition?*/ ????if?(s?==?t)?{?/*?(a)?check?source==target?(transition?to?self)?*/ ?????????QEP_EXIT_(s)?/*?exit?the?source?*/ ?????????ip?=?(int8_t)0;?/*?enter?the?target?*/ ?????} ?????else?{ ?????????(void)QEP_TRIG_(t,?QEP_EMPTY_SIG_);?/*?superstate?of?target?*/ ?????????t?=?me->state; ?????????if?(s?==?t)?{?/*?(b)?check?source==target->super?*/ ??????????????ip?=?(int8_t)0;?/*?enter?the?target?*/ ??????????} ?????????else?{ ?????????????(void)QEP_TRIG_(s,?QEP_EMPTY_SIG_);?/*?superstate?of?src?*/ ?????????????/*?(c)?check?source->super==target->super?*/ ?????????????if(me->state?==?t)?{ ?????????????????QEP_EXIT_(s)?/*?exit?the?source?*/ ?????????????????ip?=?(int8_t)0;?/*?enter?the?target?*/ ??????????????} ??????????????else?{ ???????????????????/*?(d)?check?source->super==target?*/ ???????????????????if?(me->state?==?path[0])?{ ??????????????????????QEP_EXIT_(s)?/*?exit?the?source?*/ ???????????????????} ???????????????????else?{?/*?(e)?check?rest?of?source==target->super->super.. ???????????????????????*?and?store?the?entry?path?along?the?way?*/ ????????????????????....
?
8. QP實時框架的組成
內存管理
使用內存池,對于低性能mcu,內存極為有限,引入內存管理主要是整個架構中,是以事件作為主要的任務通信手段,且事件是帶參數的,可能相同類型的事件會多次觸發,而事件處理完成后,需要清除事件,無法使用靜態的事件,因此是有必要為不同事件創建內存池的。對于不同塊大小的內存池,需要考慮的是每個塊的起始地址對齊問題。在進行內存池初始化時,我們是根據blocksize+header大小來進行劃分內存池的。假設一個2字節的結構,如果以2來進行劃分,假設mcu 4字節對齊,那么將有一半的結構起始地址無法對齊,這時需要為每個塊預留空間,保證每個塊的對齊。
事件隊列
每一個活動對象維護一個事件隊列,事件都是由基礎事件派生的,不同類型的事件只需要將其基礎事件成員添加到活動對象的隊列中即可,最終在取出的時候通過一個強制轉換便能獲得附加的參數。
事件派發
直接事件發送QActive_postLIFO()
發行訂閱事件發送豎軸表示信號(為事件的基類)活動對象支持64個優先級,每一個活動對象要求擁有唯一優先級通過優先級的bit位來表示某個事件被哪些活動對象訂閱,并在事件觸發后根據優先級為活動對象派發事件。
代碼風格
評論
查看更多