Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "test_env.h" 00002 00003 #if !DEVICE_INTERRUPTIN 00004 #error [NOT_SUPPORTED] InterruptIn is not supported 00005 #endif 00006 00007 DigitalOut myled(LED1); 00008 DigitalOut led2(LED2); 00009 00010 volatile int checks = 0; 00011 void in_handler() { 00012 checks++; 00013 led2 = !led2; 00014 } 00015 00016 #if defined(TARGET_KL25Z) 00017 #define PIN_OUT PTC6 00018 #define PIN_IN PTA5 00019 00020 #elif defined(TARGET_KL05Z) 00021 #define PIN_OUT PTB11 00022 #define PIN_IN PTB1 00023 00024 #elif defined(TARGET_LPC812) 00025 #define PIN_OUT D10 00026 #define PIN_IN D11 00027 00028 #elif defined(TARGET_LPC1114) 00029 #define PIN_OUT dp1 00030 #define PIN_IN dp2 00031 00032 #elif defined(TARGET_LPC1549) 00033 // TARGET_FF_ARDUINO cannot be used, because D0 is used as USBRX (USB serial 00034 // port pin), D1 is used as USBTX 00035 #define PIN_OUT D2 00036 #define PIN_IN D7 00037 00038 #elif defined(TARGET_LPC4088) 00039 #define PIN_IN (p11) 00040 #define PIN_OUT (p12) 00041 00042 #elif defined(TARGET_STM) && defined(TARGET_FF_ARDUINO) 00043 // TARGET_FF_ARDUINO cannot be used 00044 // D0 is used as USBRX for some NUCLEO64 00045 // D7 is not used for some NUCLEO32 00046 #define PIN_OUT D2 00047 #define PIN_IN D3 00048 00049 #elif defined(TARGET_DISCO_L053C8) || \ 00050 defined(TARGET_DISCO_F334C8) 00051 #define PIN_OUT PA_15 00052 #define PIN_IN PA_8 00053 00054 #elif defined(TARGET_DISCO_L476VG) 00055 #define PIN_OUT PA_1 00056 #define PIN_IN PA_2 00057 00058 #elif defined(TARGET_ARCH_MAX) || \ 00059 defined(TARGET_DISCO_F407VG) || \ 00060 defined(TARGET_DISCO_F429ZI)|| \ 00061 defined(TARGET_DISCO_F401VC) 00062 #define PIN_OUT PC_12 00063 #define PIN_IN PD_0 00064 00065 #elif defined(TARGET_RZ_A1H) 00066 #define PIN_OUT D1 00067 #define PIN_IN D5 00068 00069 #elif defined(TARGET_VK_RZ_A1H) 00070 #define PIN_OUT P3_2 00071 #define PIN_IN P5_6 00072 00073 #elif defined(TARGET_FF_ARDUINO) 00074 #define PIN_OUT D0 00075 #define PIN_IN D7 00076 00077 #elif defined(TARGET_MAXWSNENV) 00078 #define PIN_OUT P0_0 00079 #define PIN_IN P0_1 00080 00081 #elif defined(TARGET_MAX32600MBED) 00082 #define PIN_OUT P1_0 00083 #define PIN_IN P4_7 00084 00085 #elif defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32GG_STK3700) || defined(TARGET_EFM32WG_STK3800) 00086 #define PIN_OUT PD0 00087 #define PIN_IN PC3 00088 00089 #elif defined(TARGET_EFM32ZG_STK3200) 00090 #define PIN_OUT PD7 00091 #define PIN_IN PC1 00092 00093 #elif defined(TARGET_EFM32HG_STK3400) 00094 #define PIN_OUT PE10 00095 #define PIN_IN PC1 00096 00097 #elif defined(TARGET_EFM32PG_STK3401) 00098 #define PIN_OUT PC6 00099 #define PIN_IN PA3 00100 00101 #elif defined(TARGET_SAMR21G18A) || defined(TARGET_SAMD21J18A) || defined(TARGET_SAMD21G18A) 00102 #define PIN_OUT PB02 00103 #define PIN_IN PB03 00104 00105 #elif defined(TARGET_SAML21J18A) 00106 #define PIN_OUT PA02 00107 #define PIN_IN PA03 00108 00109 #elif defined(TARGET_SAMG55J19) 00110 #define PIN_OUT PA13 00111 #define PIN_IN PB13 00112 00113 #else 00114 #define PIN_IN (p5) 00115 #define PIN_OUT (p25) 00116 00117 #endif 00118 00119 DigitalOut out(PIN_OUT); 00120 InterruptIn in(PIN_IN); 00121 00122 #define IN_OUT_SET out = 1; myled = 1; 00123 #define IN_OUT_CLEAR out = 0; myled = 0; 00124 00125 void flipper() { 00126 for (int i = 0; i < 5; i++) { 00127 IN_OUT_SET; 00128 wait(0.2); 00129 IN_OUT_CLEAR; 00130 wait(0.2); 00131 } 00132 } 00133 00134 int main() { 00135 MBED_HOSTTEST_TIMEOUT(15); 00136 MBED_HOSTTEST_SELECT(default_auto); 00137 MBED_HOSTTEST_DESCRIPTION(InterruptIn); 00138 MBED_HOSTTEST_START("MBED_A7"); 00139 00140 IN_OUT_CLEAR; 00141 //Test falling edges first 00142 in.rise(NULL); 00143 in.fall(in_handler); 00144 flipper(); 00145 00146 if(checks != 5) { 00147 printf("MBED: falling edges test failed: %d\r\n",checks); 00148 MBED_HOSTTEST_RESULT(false); 00149 } 00150 00151 //Now test rising edges 00152 in.rise(in_handler); 00153 in.fall(NULL); 00154 flipper(); 00155 00156 if (checks != 10) { 00157 printf("MBED: raising edges test failed: %d\r\n", checks); 00158 MBED_HOSTTEST_RESULT(false); 00159 } 00160 00161 //Now test switch off edge detection 00162 in.rise(NULL); 00163 in.fall(NULL); 00164 flipper(); 00165 00166 if (checks != 10) { 00167 printf("MBED: edge detection switch off test failed: %d\r\n", checks); 00168 MBED_HOSTTEST_RESULT(false); 00169 } 00170 00171 //Finally test both 00172 in.rise(in_handler); 00173 in.fall(in_handler); 00174 flipper(); 00175 00176 if (checks != 20) { 00177 printf("MBED: Simultaneous rising and falling edges failed: %d\r\n", checks); 00178 MBED_HOSTTEST_RESULT(false); 00179 } 00180 00181 MBED_HOSTTEST_RESULT(true); 00182 }
Generated on Sun Jul 17 2022 08:25:26 by
1.7.2