Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "InterruptManager.h"
00003 #include "cmsis.h"
00004 #include "test_env.h"
00005 
00006 #if defined(TARGET_LPC1768) || defined(TARGET_LPC4088)
00007 #define TIMER_IRQ       TIMER3_IRQn
00008 #elif defined(TARGET_LPC11U24) || defined(TARGET_LPC1114)
00009 #define TIMER_IRQ       TIMER_32_1_IRQn
00010 #elif defined(TARGET_KL25Z)
00011 #define TIMER_IRQ       LPTimer_IRQn
00012 #elif defined(TARGET_LPC2368) || defined(TARGET_LPC2460)
00013 #define TIMER_IRQ       TIMER3_IRQn
00014 #elif defined(TARGET_SAMR21G18A) || defined(TARGET_SAMD21J18A) || defined(TARGET_SAMD21G18A)
00015 #define TIMER_IRQ       TC4_IRQn
00016 #elif defined(TARGET_SAML21J18A)
00017 #define TIMER_IRQ       TC0_IRQn
00018 #else
00019 #error [NOT_SUPPORTED] This test can't run on this target.
00020 #endif
00021 
00022 Serial pc(USBTX, USBRX);
00023 
00024 Ticker flipper_1;
00025 DigitalOut led1(LED1);
00026 int led1_state = 0;
00027 void flip_1() {
00028     if (led1_state) {
00029         led1 = 0; led1_state = 0;
00030     } else {
00031         led1 = 1; led1_state = 1;
00032     }
00033 }
00034 
00035 class Sender {
00036 public:
00037     Sender(Serial&s, char c): _s(s), _c(c) {}
00038     void send() { _s.putc(_c); }
00039 private:
00040     Serial& _s;
00041     char _c;
00042 };
00043 Ticker flipper_2;
00044 Sender s1(pc, '1');
00045 Sender s2(pc, '2');
00046 
00047 #if defined(TARGET_LPC1768) || defined(TARGET_LPC11U24) || defined(TARGET_LPC4088) || defined(TARGET_LPC2368) || defined(TARGET_LPC1114) || defined(TARGET_LPC2460)
00048 #   define LED_NAME LED2
00049 #elif defined(TARGET_KL05Z)
00050 #   define LED_NAME LED2
00051 #elif defined(TARGET_SAMR21G18A) || defined(TARGET_SAMD21J18A) || defined(TARGET_SAMD21G18A) || defined(TARGET_SAML21J18A) /*to avoid build errors*/
00052 #   define LED_NAME LED2  /*Only 1 LED available*/
00053 #else
00054 #   define LED_NAME PTE31
00055 #endif
00056 
00057 DigitalOut led2(LED_NAME);
00058 int led2_state = 0;
00059 void flip_2() {
00060     if (led2_state) {
00061         led2 = 0; led2_state = 0;
00062     } else {
00063         led2 = 1; led2_state = 1;
00064     }
00065 }
00066 
00067 void testme(void) {
00068     pc.putc('!');
00069 }
00070 
00071 class Counter {
00072 public:
00073     void inc(void) {
00074         count ++;
00075     }
00076     int get_count(void) const {
00077         return count;
00078     }
00079 private:
00080     static int count;
00081 };
00082 
00083 int Counter::count = 0;
00084 
00085 int main() {
00086     led1 = 0;
00087     led2 = 0;
00088     uint32_t initial_handler, final_handler;
00089     Counter c;
00090 
00091     // Test chaining inside Serial class
00092     flipper_1.attach(&flip_1, 1.0); // the address of the function to be attached (flip) and the interval (1 second)
00093     flipper_2.attach(&flip_2, 2.0); // the address of the function to be attached (flip) and the interval (2 seconds)
00094 
00095     // Test global chaining (InterruptManager)
00096     printf("Handler initially: %08X\n", initial_handler = NVIC_GetVector(TIMER_IRQ));
00097     InterruptManager *pManager = InterruptManager::get();
00098     pFunctionPointer_t ptm = pManager->add_handler(testme, TIMER_IRQ);
00099     pFunctionPointer_t pinc = pManager->add_handler_front(&c, &Counter::inc, TIMER_IRQ);
00100     printf("Handler after calling InterruptManager: %08X\n", NVIC_GetVector(TIMER_IRQ));
00101 
00102     wait(4.0);
00103 
00104     if (!pManager->remove_handler(ptm, TIMER_IRQ) || !pManager->remove_handler(pinc, TIMER_IRQ)) {
00105         printf ("remove handler failed.\n");
00106         notify_completion(false);
00107     }
00108     printf("Interrupt handler calls: %d\n", c.get_count());
00109     printf("Handler after removing previously added functions: %08X\n", final_handler = NVIC_GetVector(TIMER_IRQ));
00110 
00111     if (initial_handler != final_handler) {
00112         printf( "InteruptManager test failed.\n");
00113         notify_completion(false);
00114     }
00115 
00116     while(1);
00117 }