Simon Ford
/
Priority
Example to show how priority can be used to ensure timing critical task can be run
main.cpp@0:2b952ccae54d, 2010-08-17 (annotated)
- Committer:
- simon
- Date:
- Tue Aug 17 10:14:44 2010 +0000
- Revision:
- 0:2b952ccae54d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:2b952ccae54d | 1 | // Example running the mbed Tickers at a lower priority |
simon | 0:2b952ccae54d | 2 | |
simon | 0:2b952ccae54d | 3 | #include "mbed.h" |
simon | 0:2b952ccae54d | 4 | |
simon | 0:2b952ccae54d | 5 | volatile int counter = 0; |
simon | 0:2b952ccae54d | 6 | void timing_critical() { |
simon | 0:2b952ccae54d | 7 | counter++; |
simon | 0:2b952ccae54d | 8 | } |
simon | 0:2b952ccae54d | 9 | |
simon | 0:2b952ccae54d | 10 | void long_event() { |
simon | 0:2b952ccae54d | 11 | wait_ms(50); |
simon | 0:2b952ccae54d | 12 | } |
simon | 0:2b952ccae54d | 13 | |
simon | 0:2b952ccae54d | 14 | PwmOut out(p25); |
simon | 0:2b952ccae54d | 15 | InterruptIn in(p26); |
simon | 0:2b952ccae54d | 16 | Ticker tick; |
simon | 0:2b952ccae54d | 17 | |
simon | 0:2b952ccae54d | 18 | int main() { |
simon | 0:2b952ccae54d | 19 | out.period_ms(10); |
simon | 0:2b952ccae54d | 20 | out.pulsewidth_ms(5); |
simon | 0:2b952ccae54d | 21 | in.rise(&timing_critical); |
simon | 0:2b952ccae54d | 22 | |
simon | 0:2b952ccae54d | 23 | printf("1) InterruptIn only...\n"); |
simon | 0:2b952ccae54d | 24 | for(int i=0; i<5; i++) { |
simon | 0:2b952ccae54d | 25 | counter = 0; |
simon | 0:2b952ccae54d | 26 | wait(1); |
simon | 0:2b952ccae54d | 27 | printf("counts/sec = %d\n", counter); |
simon | 0:2b952ccae54d | 28 | } |
simon | 0:2b952ccae54d | 29 | |
simon | 0:2b952ccae54d | 30 | tick.attach(&long_event, 0.1); |
simon | 0:2b952ccae54d | 31 | |
simon | 0:2b952ccae54d | 32 | printf("2) InterruptIn plus long running occasional ticker event...\n"); |
simon | 0:2b952ccae54d | 33 | for(int i=0; i<5; i++) { |
simon | 0:2b952ccae54d | 34 | counter = 0; |
simon | 0:2b952ccae54d | 35 | wait(1); |
simon | 0:2b952ccae54d | 36 | printf("count/sec = %d\n", counter); |
simon | 0:2b952ccae54d | 37 | } |
simon | 0:2b952ccae54d | 38 | |
simon | 0:2b952ccae54d | 39 | printf("3) InterruptIn plus long running occasional ticker event at lower priority...\n"); |
simon | 0:2b952ccae54d | 40 | NVIC_SetPriority(TIMER3_IRQn, 255); // set mbed tickers to lower priority than other things |
simon | 0:2b952ccae54d | 41 | for(int i=0; i<5; i++) { |
simon | 0:2b952ccae54d | 42 | counter = 0; |
simon | 0:2b952ccae54d | 43 | wait(1); |
simon | 0:2b952ccae54d | 44 | printf("counter = %d\n", counter); |
simon | 0:2b952ccae54d | 45 | } |
simon | 0:2b952ccae54d | 46 | } |