You are on page 1of 12

ARM PROGRAMMING

Bi Quc Bo

Example of a data logger

BI QUC BO 1

Beginning of the code


No RTOS main() { Inialize(); while (1) { // no code yet } } Using FreeRTOS main() { Inialize(); while (1) { // no code yet } }

Add serial port functionality


MAIN MENU --------1. Read A/D 2. Write GPIO outputs 3. Read GPIO inputs 4. Set LEDs 5. Write string to LCD display

BI QUC BO 2

Add serial port functionality


void ProcessSerialPort() { char s[80]; while (1) { char c = getchar(); switch c { case 1: // Read and display ADC value break; case 2: // Prompt for value and write to GPIO outputs printf(Enter GPIO value: ); fgets(s, 80, stdin); // will block until user hits <enter> break;

Add serial port functionality


case 3: // Read and display GPIO input values break; case 4: // Prompt for value and write to LEDs printf(Enter LED value: ); fgets(s, 80, stdin); // will block until user hits <enter> break; case 5: // Prompt for value and write to LCD display printf(Enter display string: ); fgets(s, 80, stdin); // will block until user hits <enter> break; default: // print error message } } }

BI QUC BO 3

Non-RTOS
void ProcessSerialPort() { // serial port code goes here }

main() { while (1) { if ( SerialCharAvail() ) ProcessSerialPort(); } }

Using FreeRTOS
void ProcessSerialPort(void *pvParameters ) { // serial port code goes here } void Main(void) { xTaskCreate(ProcessSerialPort, serial Task", 1000, NULL, serialPri, NULL ); while (1) { // no code yet } }

BI QUC BO 4

Compare
Khi khng dng RTOS, trong vng lp chnh phi c lnh kim tra serial port. Chng trnh s b block khi ch ngi dng nhp d liu t serial port. Vi RTOS, ta khng cn thm code kim tra serial port. Tc v ProcessSerialPort s b block khi ch ngi dng nhp d liu, nhng chng trnh chnh s khng b block

Add Keypad Functionality


Keypad trong trng hp ny l 1 ma trn phm. Khi phm c nhn, 1 ngt c to ra. Khi khng dng RTOS, 1 flag s c set trong ISR. Vi FreeRTOS, ISR c dng give semaphore cho keypad

BI QUC BO 5

Non-RTOS
void ProcessKeypad() { char key = ScanKeypad(); // code to process key goes here }

Free-RTOS
void ProcessKeypad(void *pvParameters ) { while (1) { xSemaphoreTake( xKeyPadSemaphore, portMAX_DELAY ); char key = ScanKeypad(); // code to process key goes here } }

BI QUC BO 6

Non-FreeRTOS
main() { while (1) { if ( SerialCharAvail() ) ProcessSerialPort(); if ( Keypressed ) ProcessKeypad(); } }

Void main(void) { xTaskCreate(ProcessSerialPort, serial Task", 1000, NULL, serialPri, NULL ); xTaskCreate(ProcessKeypad, keypad Task", 1000, NULL, keyPadPri, NULL ); while (1) { // no code yet } }

BI QUC BO 7

Khi khng dng RTOS, nu chng trnh ang ch serial port, cc phm nhn trn keypad s khng c x l Nu dng ISR cho keypad x l phm, ISR s chy trong thi gian di. iu ny lm gim tc p ng ca h thng.

Vi FreeRTOS, nu u tin ca ProcessKeypad cao hn ProcessSerialPort, chng trnh s chuyn sang ProcessKeypad ngay khi c phm nhn, x l v quay tr li

ProcessSerialPort.

BI QUC BO 8

Adding ADC function


Khi ADC c d liu, mt ngt s c to ra. ISR s c gi tr ADC vo 1 bin ton cc. Khi khng dng RTOS, ISR s set 1 c. Khi dng vi freeRTOS, ISR s give mt semaphore. Sau khi gi tr ADC c c, n s c x l v lu vo 1 mng.

Non-RTOS
void ProcessADC() { if ( SampleReady ) // code to read and store ADC value }

BI QUC BO 9

Non-FreeRTOS
main() { while (1) { if ( SerialCharAvail() ) ProcessSerialPort(); if ( Keypressed ) ProcessKeypad(); if ( SampleReady ) ProcessADC(); } }

D liu c th b mt nu chng trnh cha gi ProcessADC() trc khi c d liu tip theo (v phi ch serialport, ). D liu c th sai nu chng trnh chnh v ngt cng truy cp bin ton cc lu gi tr ca ADC cng lc.

BI QUC BO 10

With FreeRTOS
void ProcessADC(void *pvParameters) { while (1) { xSemaphoreTake( xADCSemaphore, portMAX_DELAY ); // code to read and store ADC value } }

With FreeRTOS
Void main(void) { xTaskCreate(ProcessSerialPort, serial Task", 1000, NULL, serialPri, NULL ); xTaskCreate(ProcessKeypad, keypad Task", 1000, NULL, keyPadPri, NULL ); xTaskCreate(ProcessADC, ADC Task", 1000, NULL, ADCPri, NULL ); while (1) { // no code yet } }

BI QUC BO 11

m bo ADC lun c x l trc, u tin ca tc v ADC c set cao hn cc tc v cn li. Thay v ghi gi tr vo 1 bin, ISR c th ghi gi tr ADC vo 1 queue.

Conclusion
Nu khng dng RTOS, programmer phi thit k mt scheduler qun l cc s kin. Vic dng FreeRTOS thit k chng trnh s lm cho vic thit k d dng hn. FreeRTOS c debug v m bo bug-free, do s dng FreeRTOS s lm cho chng trnh n nh hn

BI QUC BO 12

You might also like