You are on page 1of 12

ARM PROGRAMMING

Bi Qu c B o

Example of a data logger

BI QU C B O 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 QU C B O 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 QU C B O 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 QU C B O 4

Compare
Khi khng dng RTOS, trong vng l p chnh ph i c l nh ki m tra serial port. Chng trnh s b block khi ch ng i dng nh p d li u t serial port. V i RTOS, ta khng c n thm code ki m tra serial port. Tc v ProcessSerialPort s b block khi ch ng i dng nh p d li u, nhng chng trnh chnh s khng b block

Add Keypad Functionality


Keypad trong tr ng h p ny l 1 ma tr n phm. Khi phm c nh n, 1 ng t c t o ra. Khi khng dng RTOS, 1 flag s c set trong ISR. V i FreeRTOS, ISR c dng give semaphore cho keypad

BI QU C B O 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 QU C B O 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 QU C B O 7

Khi khng dng RTOS, n u chng trnh ang ch serial port, cc phm nh n trn keypad s khng c x l N u dng ISR cho keypad x l phm, ISR s ch y trong th i gian di. i u ny lm gi m t c p ng c a h th ng.

V i FreeRTOS, n u u tin c a ProcessKeypad cao hn ProcessSerialPort, chng trnh s chuy n sang ProcessKeypad ngay khi c phm nh n, x l v quay tr l i

ProcessSerialPort.

BI QU C B O 8

Adding ADC function


Khi ADC c d li u, m t ng t s c t o ra. ISR s c gi tr ADC vo 1 bi n ton c c. Khi khng dng RTOS, ISR s set 1 c . Khi dng v i freeRTOS, ISR s give m t semaphore. Sau khi gi tr ADC c c, n s c x l v lu vo 1 m ng.

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

BI QU C B O 9

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

D li u c th b m t n u chng trnh cha g i ProcessADC() tr c khi c d li u ti p theo (v ph i ch serialport, ). D li u c th sai n u chng trnh chnh v ng t cng truy c p bi n ton c c lu gi tr c a ADC cng lc.

BI QU C B O 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 QU C B O 11

m b o ADC lun c x l tr c, u tin c a tc v ADC c set cao hn cc tc v cn l i. Thay v ghi gi tr vo 1 bi n, ISR c th ghi gi tr ADC vo 1 queue.

Conclusion
N u khng dng RTOS, programmer ph i thi t k m t scheduler qu n l cc s ki n. Vi c dng FreeRTOS thi t k chng trnh s lm cho vi c thi t k d dng hn. FreeRTOS c debug v m b o bug-free, do s d ng FreeRTOS s lm cho chng trnh n nh hn

BI QU C B O 12

You might also like