]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/tests/mbed/interrupt_chaining/main.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / tests / mbed / interrupt_chaining / main.cpp
1 #include "mbed.h"
2 #include "InterruptManager.h"
3 #include "cmsis.h"
4 #include "test_env.h"
5
6 #if defined(TARGET_LPC1768) || defined(TARGET_LPC4088)
7 #define TIMER_IRQ       TIMER3_IRQn
8 #elif defined(TARGET_LPC11U24) || defined(TARGET_LPC1114)
9 #define TIMER_IRQ       TIMER_32_1_IRQn
10 #elif defined(TARGET_KL25Z)
11 #define TIMER_IRQ       LPTimer_IRQn
12 #elif defined(TARGET_LPC2368)
13 #define TIMER_IRQ       TIMER3_IRQn
14 #else
15 #error This test can't run on this target.
16 #endif
17
18 Serial pc(USBTX, USBRX);
19
20 Ticker flipper_1;
21 DigitalOut led1(LED1);
22 int led1_state = 0;
23 void flip_1() {
24     if (led1_state) {
25         led1 = 0; led1_state = 0;
26     } else {
27         led1 = 1; led1_state = 1;
28     }
29 }
30
31 class Sender {
32 public:
33     Sender(Serial&s, char c): _s(s), _c(c) {}
34     void send() { _s.putc(_c); }
35 private:
36     Serial& _s;
37     char _c;
38 };
39 Ticker flipper_2;
40 Sender s1(pc, '1');
41 Sender s2(pc, '2');
42
43 #if defined(TARGET_LPC1768) || defined(TARGET_LPC11U24) || defined(TARGET_LPC4088) || defined(TARGET_LPC2368) || defined(TARGET_LPC1114)
44 #   define LED_NAME LED2
45 #elif defined(TARGET_KL05Z)
46 #   define LED_NAME LED2
47 #else
48 #   define LED_NAME PTE31
49 #endif
50
51 DigitalOut led2(LED_NAME);
52 int led2_state = 0;
53 void flip_2() {
54     if (led2_state) {
55         led2 = 0; led2_state = 0;
56     } else {
57         led2 = 1; led2_state = 1;
58     }
59 }
60
61 void testme(void) {
62     pc.putc('!');
63 }
64
65 class Counter {
66 public:
67     void inc(void) {
68         count ++;
69     }
70     int get_count(void) const {
71         return count;
72     }
73 private:
74     static int count;
75 };
76
77 int Counter::count = 0;
78
79 int main() {
80     led1 = 0;
81     led2 = 0;
82     uint32_t initial_handler, final_handler;
83     Counter c;
84
85     // Test chaining inside Serial class
86     flipper_1.attach(&flip_1, 1.0); // the address of the function to be attached (flip) and the interval (1 second)
87     flipper_2.attach(&flip_2, 2.0); // the address of the function to be attached (flip) and the interval (2 seconds)
88
89     // Test global chaining (InterruptManager)
90     printf("Handler initially: %08X\n", initial_handler = NVIC_GetVector(TIMER_IRQ));
91     InterruptManager *pManager = InterruptManager::get();
92     pFunctionPointer_t ptm = pManager->add_handler(testme, TIMER_IRQ);
93     pFunctionPointer_t pinc = pManager->add_handler_front(&c, &Counter::inc, TIMER_IRQ);
94     printf("Handler after calling InterruptManager: %08X\n", NVIC_GetVector(TIMER_IRQ));
95
96     wait(4.0);
97
98     if (!pManager->remove_handler(ptm, TIMER_IRQ) || !pManager->remove_handler(pinc, TIMER_IRQ)) {
99         printf ("remove handler failed.\n");
100         notify_completion(false);
101     }
102     printf("Interrupt handler calls: %d\n", c.get_count());
103     printf("Handler after removing previously added functions: %08X\n", final_handler = NVIC_GetVector(TIMER_IRQ));
104
105     if (initial_handler != final_handler) {
106         printf( "InteruptManager test failed.\n");
107         notify_completion(false);
108     }
109
110     while(1);
111 }