STM32下使用Keil自帶的RTL
Keil ARTX(Advanced Real-Time eXecutive)是Keil為ARM 系列所提供的一個(gè)小型實(shí)時(shí)操作系統(tǒng),整合于其UV3開(kāi)發(fā)環(huán)境之中.在下載并安裝了RealView MDK個(gè)人學(xué)習(xí)版后,ARTX同時(shí)安裝到了C:KeilARMRLRTX目錄下,在C:KeilARMBoardsKeilMCBSTM32STLIB_RTX_Blinky目錄下有STM32的例子程序。經(jīng)過(guò)簡(jiǎn)單修改,運(yùn)行在萬(wàn)利開(kāi)發(fā)板上。
#include
#include
OS_TID t_phaseA; //分配任務(wù)ID號(hào)
OS_TID t_phaseB;
OS_TID t_phaseC;
OS_TID t_phaseD;
#define LED_A GPIO_Pin_4
#define LED_B GPIO_Pin_5
#define LED_C GPIO_Pin_6
#define LED_D GPIO_Pin_7
#define LED_On(led) GPIO_SetBits(GPIOC, led)
#define LED_Off(led) GPIO_ResetBits(GPIOC, led)
extern void SetupClock (void);
extern void SetupLED (void);
void phaseA (void) __task {
for (;;) {
LED_On (LED_A);
os_dly_wait (100);
LED_Off(LED_A);
os_dly_wait (100);
}
}
void phaseB (void) __task {
for (;;) {
LED_On (LED_B);
os_dly_wait (100);
LED_Off(LED_B);
os_dly_wait (100);
}
}
void phaseC (void) __task {
for (;;) {
LED_On (LED_C);
os_dly_wait (100);
LED_Off(LED_C);
os_dly_wait (100);
}
}
void phaseD (void) __task {
for (;;) {
LED_On (LED_D);
os_dly_wait (100);
LED_Off(LED_D);
os_dly_wait (100);
}
}
void init (void) __task {
t_phaseA = os_tsk_create (phaseA, 0);
os_dly_wait (50);
t_phaseB = os_tsk_create (phaseB, 0);
os_dly_wait (50);
t_phaseC = os_tsk_create (phaseC, 0);
os_dly_wait (50);
t_phaseD = os_tsk_create (phaseD, 0);
os_tsk_delete_self ();
}
int main (void) {
SetupClock();
SetupLED ();
os_sys_init (init);
}
經(jīng)過(guò)初試化后,os_sys_init (init)啟動(dòng)了初試化任務(wù)。在初試化任務(wù)中,又先后啟動(dòng)了4個(gè)任務(wù),啟動(dòng)后,
刪除初試化任務(wù)。這4個(gè)任務(wù)各自控制一個(gè)LED的開(kāi)關(guān)。
============================================
個(gè)人理解:
int main(void)
{
}
問(wèn)1:為什么在TaskMain里面創(chuàng)建很多小任務(wù)不在下面繼續(xù)調(diào)用os_sys_init_user( ); 這個(gè)API呢?原因是因?yàn)槌跏蓟疶askMain之后不再返回了,所以他也執(zhí)行不到下面while
問(wèn)2:TaskMain 里面創(chuàng)建的小任務(wù),而小任務(wù)里面也都是有while(1)死循環(huán),這就是多線程的概念。除了while(1)其他也都是只執(zhí)行一次,包括主函數(shù)和小任務(wù)里面的變量,只有while(1)是時(shí)間片和優(yōu)先級(jí)運(yùn)行,實(shí)現(xiàn)多線程執(zhí)行。
評(píng)論