mbed-os
Fork of mbed-os by
features/unsupported/tests/mbed/interruptin/main.cpp@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | #include "test_env.h" |
elessair | 0:f269e3021894 | 2 | |
elessair | 0:f269e3021894 | 3 | #if !DEVICE_INTERRUPTIN |
elessair | 0:f269e3021894 | 4 | #error [NOT_SUPPORTED] InterruptIn is not supported |
elessair | 0:f269e3021894 | 5 | #endif |
elessair | 0:f269e3021894 | 6 | |
elessair | 0:f269e3021894 | 7 | DigitalOut myled(LED1); |
elessair | 0:f269e3021894 | 8 | DigitalOut led2(LED2); |
elessair | 0:f269e3021894 | 9 | |
elessair | 0:f269e3021894 | 10 | volatile int checks = 0; |
elessair | 0:f269e3021894 | 11 | void in_handler() { |
elessair | 0:f269e3021894 | 12 | checks++; |
elessair | 0:f269e3021894 | 13 | led2 = !led2; |
elessair | 0:f269e3021894 | 14 | } |
elessair | 0:f269e3021894 | 15 | |
elessair | 0:f269e3021894 | 16 | #if defined(TARGET_KL25Z) |
elessair | 0:f269e3021894 | 17 | #define PIN_OUT PTC6 |
elessair | 0:f269e3021894 | 18 | #define PIN_IN PTA5 |
elessair | 0:f269e3021894 | 19 | |
elessair | 0:f269e3021894 | 20 | #elif defined(TARGET_KL05Z) |
elessair | 0:f269e3021894 | 21 | #define PIN_OUT PTB11 |
elessair | 0:f269e3021894 | 22 | #define PIN_IN PTB1 |
elessair | 0:f269e3021894 | 23 | |
elessair | 0:f269e3021894 | 24 | #elif defined(TARGET_LPC812) |
elessair | 0:f269e3021894 | 25 | #define PIN_OUT D10 |
elessair | 0:f269e3021894 | 26 | #define PIN_IN D11 |
elessair | 0:f269e3021894 | 27 | |
elessair | 0:f269e3021894 | 28 | #elif defined(TARGET_LPC1114) |
elessair | 0:f269e3021894 | 29 | #define PIN_OUT dp1 |
elessair | 0:f269e3021894 | 30 | #define PIN_IN dp2 |
elessair | 0:f269e3021894 | 31 | |
elessair | 0:f269e3021894 | 32 | #elif defined(TARGET_LPC1549) |
elessair | 0:f269e3021894 | 33 | // TARGET_FF_ARDUINO cannot be used, because D0 is used as USBRX (USB serial |
elessair | 0:f269e3021894 | 34 | // port pin), D1 is used as USBTX |
elessair | 0:f269e3021894 | 35 | #define PIN_OUT D2 |
elessair | 0:f269e3021894 | 36 | #define PIN_IN D7 |
elessair | 0:f269e3021894 | 37 | |
elessair | 0:f269e3021894 | 38 | #elif defined(TARGET_LPC4088) |
elessair | 0:f269e3021894 | 39 | #define PIN_IN (p11) |
elessair | 0:f269e3021894 | 40 | #define PIN_OUT (p12) |
elessair | 0:f269e3021894 | 41 | |
elessair | 0:f269e3021894 | 42 | #elif defined(TARGET_STM) && defined(TARGET_FF_ARDUINO) |
elessair | 0:f269e3021894 | 43 | // TARGET_FF_ARDUINO cannot be used |
elessair | 0:f269e3021894 | 44 | // D0 is used as USBRX for some NUCLEO64 |
elessair | 0:f269e3021894 | 45 | // D7 is not used for some NUCLEO32 |
elessair | 0:f269e3021894 | 46 | #define PIN_OUT D2 |
elessair | 0:f269e3021894 | 47 | #define PIN_IN D9 |
elessair | 0:f269e3021894 | 48 | |
elessair | 0:f269e3021894 | 49 | #elif defined(TARGET_DISCO_L053C8) || \ |
elessair | 0:f269e3021894 | 50 | defined(TARGET_DISCO_F334C8) |
elessair | 0:f269e3021894 | 51 | #define PIN_OUT PA_15 |
elessair | 0:f269e3021894 | 52 | #define PIN_IN PA_8 |
elessair | 0:f269e3021894 | 53 | |
elessair | 0:f269e3021894 | 54 | #elif defined(TARGET_DISCO_L476VG) |
elessair | 0:f269e3021894 | 55 | #define PIN_OUT PA_1 |
elessair | 0:f269e3021894 | 56 | #define PIN_IN PA_2 |
elessair | 0:f269e3021894 | 57 | |
elessair | 0:f269e3021894 | 58 | #elif defined(TARGET_ARCH_MAX) || \ |
elessair | 0:f269e3021894 | 59 | defined(TARGET_DISCO_F407VG) || \ |
elessair | 0:f269e3021894 | 60 | defined(TARGET_DISCO_F429ZI)|| \ |
elessair | 0:f269e3021894 | 61 | defined(TARGET_DISCO_F401VC) |
elessair | 0:f269e3021894 | 62 | #define PIN_OUT PC_12 |
elessair | 0:f269e3021894 | 63 | #define PIN_IN PD_0 |
elessair | 0:f269e3021894 | 64 | |
elessair | 0:f269e3021894 | 65 | #elif defined(TARGET_RZ_A1H) |
elessair | 0:f269e3021894 | 66 | #define PIN_OUT D1 |
elessair | 0:f269e3021894 | 67 | #define PIN_IN D5 |
elessair | 0:f269e3021894 | 68 | |
elessair | 0:f269e3021894 | 69 | #elif defined(TARGET_VK_RZ_A1H) |
elessair | 0:f269e3021894 | 70 | #define PIN_OUT P3_2 |
elessair | 0:f269e3021894 | 71 | #define PIN_IN P5_6 |
elessair | 0:f269e3021894 | 72 | |
elessair | 0:f269e3021894 | 73 | #elif defined(TARGET_FF_ARDUINO) |
elessair | 0:f269e3021894 | 74 | #define PIN_OUT D0 |
elessair | 0:f269e3021894 | 75 | #define PIN_IN D7 |
elessair | 0:f269e3021894 | 76 | |
elessair | 0:f269e3021894 | 77 | #elif defined(TARGET_MAXWSNENV) |
elessair | 0:f269e3021894 | 78 | #define PIN_OUT P0_0 |
elessair | 0:f269e3021894 | 79 | #define PIN_IN P0_1 |
elessair | 0:f269e3021894 | 80 | |
elessair | 0:f269e3021894 | 81 | #elif defined(TARGET_MAX32600MBED) |
elessair | 0:f269e3021894 | 82 | #define PIN_OUT P1_0 |
elessair | 0:f269e3021894 | 83 | #define PIN_IN P4_7 |
elessair | 0:f269e3021894 | 84 | |
elessair | 0:f269e3021894 | 85 | #elif defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32GG_STK3700) || defined(TARGET_EFM32WG_STK3800) |
elessair | 0:f269e3021894 | 86 | #define PIN_OUT PD0 |
elessair | 0:f269e3021894 | 87 | #define PIN_IN PC3 |
elessair | 0:f269e3021894 | 88 | |
elessair | 0:f269e3021894 | 89 | #elif defined(TARGET_EFM32ZG_STK3200) |
elessair | 0:f269e3021894 | 90 | #define PIN_OUT PD7 |
elessair | 0:f269e3021894 | 91 | #define PIN_IN PC1 |
elessair | 0:f269e3021894 | 92 | |
elessair | 0:f269e3021894 | 93 | #elif defined(TARGET_EFM32HG_STK3400) |
elessair | 0:f269e3021894 | 94 | #define PIN_OUT PE10 |
elessair | 0:f269e3021894 | 95 | #define PIN_IN PC1 |
elessair | 0:f269e3021894 | 96 | |
elessair | 0:f269e3021894 | 97 | #elif defined(TARGET_EFM32PG_STK3401) |
elessair | 0:f269e3021894 | 98 | #define PIN_OUT PC6 |
elessair | 0:f269e3021894 | 99 | #define PIN_IN PA3 |
elessair | 0:f269e3021894 | 100 | |
elessair | 0:f269e3021894 | 101 | #elif defined(TARGET_SAMR21G18A) || defined(TARGET_SAMD21J18A) || defined(TARGET_SAMD21G18A) |
elessair | 0:f269e3021894 | 102 | #define PIN_OUT PB02 |
elessair | 0:f269e3021894 | 103 | #define PIN_IN PB03 |
elessair | 0:f269e3021894 | 104 | |
elessair | 0:f269e3021894 | 105 | #elif defined(TARGET_SAML21J18A) |
elessair | 0:f269e3021894 | 106 | #define PIN_OUT PA02 |
elessair | 0:f269e3021894 | 107 | #define PIN_IN PA03 |
elessair | 0:f269e3021894 | 108 | |
elessair | 0:f269e3021894 | 109 | #elif defined(TARGET_SAMG55J19) |
elessair | 0:f269e3021894 | 110 | #define PIN_OUT PA13 |
elessair | 0:f269e3021894 | 111 | #define PIN_IN PB13 |
elessair | 0:f269e3021894 | 112 | |
elessair | 0:f269e3021894 | 113 | #else |
elessair | 0:f269e3021894 | 114 | #define PIN_IN (p5) |
elessair | 0:f269e3021894 | 115 | #define PIN_OUT (p25) |
elessair | 0:f269e3021894 | 116 | |
elessair | 0:f269e3021894 | 117 | #endif |
elessair | 0:f269e3021894 | 118 | |
elessair | 0:f269e3021894 | 119 | DigitalOut out(PIN_OUT); |
elessair | 0:f269e3021894 | 120 | InterruptIn in(PIN_IN); |
elessair | 0:f269e3021894 | 121 | |
elessair | 0:f269e3021894 | 122 | #define IN_OUT_SET out = 1; myled = 1; |
elessair | 0:f269e3021894 | 123 | #define IN_OUT_CLEAR out = 0; myled = 0; |
elessair | 0:f269e3021894 | 124 | |
elessair | 0:f269e3021894 | 125 | void flipper() { |
elessair | 0:f269e3021894 | 126 | for (int i = 0; i < 5; i++) { |
elessair | 0:f269e3021894 | 127 | IN_OUT_SET; |
elessair | 0:f269e3021894 | 128 | wait(0.2); |
elessair | 0:f269e3021894 | 129 | IN_OUT_CLEAR; |
elessair | 0:f269e3021894 | 130 | wait(0.2); |
elessair | 0:f269e3021894 | 131 | } |
elessair | 0:f269e3021894 | 132 | } |
elessair | 0:f269e3021894 | 133 | |
elessair | 0:f269e3021894 | 134 | int main() { |
elessair | 0:f269e3021894 | 135 | MBED_HOSTTEST_TIMEOUT(15); |
elessair | 0:f269e3021894 | 136 | MBED_HOSTTEST_SELECT(default_auto); |
elessair | 0:f269e3021894 | 137 | MBED_HOSTTEST_DESCRIPTION(InterruptIn); |
elessair | 0:f269e3021894 | 138 | MBED_HOSTTEST_START("MBED_A7"); |
elessair | 0:f269e3021894 | 139 | |
elessair | 0:f269e3021894 | 140 | IN_OUT_CLEAR; |
elessair | 0:f269e3021894 | 141 | //Test falling edges first |
elessair | 0:f269e3021894 | 142 | in.rise(NULL); |
elessair | 0:f269e3021894 | 143 | in.fall(in_handler); |
elessair | 0:f269e3021894 | 144 | flipper(); |
elessair | 0:f269e3021894 | 145 | |
elessair | 0:f269e3021894 | 146 | if(checks != 5) { |
elessair | 0:f269e3021894 | 147 | printf("MBED: falling edges test failed: %d\r\n",checks); |
elessair | 0:f269e3021894 | 148 | MBED_HOSTTEST_RESULT(false); |
elessair | 0:f269e3021894 | 149 | } |
elessair | 0:f269e3021894 | 150 | |
elessair | 0:f269e3021894 | 151 | //Now test rising edges |
elessair | 0:f269e3021894 | 152 | in.rise(in_handler); |
elessair | 0:f269e3021894 | 153 | in.fall(NULL); |
elessair | 0:f269e3021894 | 154 | flipper(); |
elessair | 0:f269e3021894 | 155 | |
elessair | 0:f269e3021894 | 156 | if (checks != 10) { |
elessair | 0:f269e3021894 | 157 | printf("MBED: raising edges test failed: %d\r\n", checks); |
elessair | 0:f269e3021894 | 158 | MBED_HOSTTEST_RESULT(false); |
elessair | 0:f269e3021894 | 159 | } |
elessair | 0:f269e3021894 | 160 | |
elessair | 0:f269e3021894 | 161 | //Now test switch off edge detection |
elessair | 0:f269e3021894 | 162 | in.rise(NULL); |
elessair | 0:f269e3021894 | 163 | in.fall(NULL); |
elessair | 0:f269e3021894 | 164 | flipper(); |
elessair | 0:f269e3021894 | 165 | |
elessair | 0:f269e3021894 | 166 | if (checks != 10) { |
elessair | 0:f269e3021894 | 167 | printf("MBED: edge detection switch off test failed: %d\r\n", checks); |
elessair | 0:f269e3021894 | 168 | MBED_HOSTTEST_RESULT(false); |
elessair | 0:f269e3021894 | 169 | } |
elessair | 0:f269e3021894 | 170 | |
elessair | 0:f269e3021894 | 171 | //Finally test both |
elessair | 0:f269e3021894 | 172 | in.rise(in_handler); |
elessair | 0:f269e3021894 | 173 | in.fall(in_handler); |
elessair | 0:f269e3021894 | 174 | flipper(); |
elessair | 0:f269e3021894 | 175 | |
elessair | 0:f269e3021894 | 176 | if (checks != 20) { |
elessair | 0:f269e3021894 | 177 | printf("MBED: Simultaneous rising and falling edges failed: %d\r\n", checks); |
elessair | 0:f269e3021894 | 178 | MBED_HOSTTEST_RESULT(false); |
elessair | 0:f269e3021894 | 179 | } |
elessair | 0:f269e3021894 | 180 | |
elessair | 0:f269e3021894 | 181 | MBED_HOSTTEST_RESULT(true); |
elessair | 0:f269e3021894 | 182 | } |