This is code is part of a Technion course project in advanced IoT, implementing a device to receive and present sensors data from a Formula racing car built by students at Technion - Israel Institute of Technology.
Fork of DISCO-L072CZ-LRWAN1_LoRa_PingPong by
This is code is part of a Technion course project in advanced IoT, implementing a device to receive sensors data from another L072CZ-LRWAN1 installed on a Formula racing car (built by students at Technion - Israel Institute of Technology), and sends it to a GUI presenting the data (GUI project: github.com/ward-mattar/TechnionFormulaGUI).
How to install
- Create an account on Mbed: https://os.mbed.com/account/signup/
- Import project into Compiler
- In the Program Workspace select "Formula_Nucleo_Receiver"
- Select a Platform like so:
- Click button at top-left
- Add Board
- Search "NUCLEO F103RB" and then "Add to your Mbed Compiler"
- Finally click "Compile", if the build was successful, the binary would download automatically
- To install it on device simply plug it in to a PC, open device drive and drag then drop binary file in it
SX1276GenericLib/Arduino-mbed-APIs/arduino-mbed.cpp@12:046346a16ff4, 2018-05-19 (annotated)
- Committer:
- wardm
- Date:
- Sat May 19 15:42:38 2018 +0000
- Revision:
- 12:046346a16ff4
V1.0.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wardm | 12:046346a16ff4 | 1 | /* |
wardm | 12:046346a16ff4 | 2 | * The file is Licensed under the Apache License, Version 2.0 |
wardm | 12:046346a16ff4 | 3 | * (c) 2017 Helmut Tschemernjak |
wardm | 12:046346a16ff4 | 4 | * 30826 Garbsen (Hannover) Germany |
wardm | 12:046346a16ff4 | 5 | */ |
wardm | 12:046346a16ff4 | 6 | |
wardm | 12:046346a16ff4 | 7 | #ifdef ARDUINO |
wardm | 12:046346a16ff4 | 8 | |
wardm | 12:046346a16ff4 | 9 | using namespace std; |
wardm | 12:046346a16ff4 | 10 | |
wardm | 12:046346a16ff4 | 11 | #include "arduino-mbed.h" |
wardm | 12:046346a16ff4 | 12 | #include "arduino-util.h" |
wardm | 12:046346a16ff4 | 13 | |
wardm | 12:046346a16ff4 | 14 | Stream *ser; |
wardm | 12:046346a16ff4 | 15 | bool SerialUSB_active = false; |
wardm | 12:046346a16ff4 | 16 | |
wardm | 12:046346a16ff4 | 17 | void InitSerial(Stream *serial, int timeout_ms) { |
wardm | 12:046346a16ff4 | 18 | ser = serial; |
wardm | 12:046346a16ff4 | 19 | if (serial == (Stream *)&SerialUSB) { |
wardm | 12:046346a16ff4 | 20 | uint32_t start = ms_getTicker(); |
wardm | 12:046346a16ff4 | 21 | |
wardm | 12:046346a16ff4 | 22 | SerialUSB_active = true; |
wardm | 12:046346a16ff4 | 23 | while(!SerialUSB) { |
wardm | 12:046346a16ff4 | 24 | if (ms_getTicker() > start + timeout_ms) { |
wardm | 12:046346a16ff4 | 25 | SerialUSB_active = false; |
wardm | 12:046346a16ff4 | 26 | break; |
wardm | 12:046346a16ff4 | 27 | } |
wardm | 12:046346a16ff4 | 28 | } |
wardm | 12:046346a16ff4 | 29 | if (!SerialUSB_active) { |
wardm | 12:046346a16ff4 | 30 | USB->DEVICE.CTRLA.bit.SWRST = 1; // disconnect the USB Port |
wardm | 12:046346a16ff4 | 31 | while (USB->DEVICE.CTRLA.bit.SWRST == 1); |
wardm | 12:046346a16ff4 | 32 | } |
wardm | 12:046346a16ff4 | 33 | } |
wardm | 12:046346a16ff4 | 34 | } |
wardm | 12:046346a16ff4 | 35 | |
wardm | 12:046346a16ff4 | 36 | static void pinInt00(void); |
wardm | 12:046346a16ff4 | 37 | static void pinInt01(void); |
wardm | 12:046346a16ff4 | 38 | static void pinInt02(void); |
wardm | 12:046346a16ff4 | 39 | static void pinInt03(void); |
wardm | 12:046346a16ff4 | 40 | static void pinInt04(void); |
wardm | 12:046346a16ff4 | 41 | static void pinInt05(void); |
wardm | 12:046346a16ff4 | 42 | static void pinInt06(void); |
wardm | 12:046346a16ff4 | 43 | static void pinInt07(void); |
wardm | 12:046346a16ff4 | 44 | static void pinInt08(void); |
wardm | 12:046346a16ff4 | 45 | static void pinInt09(void); |
wardm | 12:046346a16ff4 | 46 | static void pinInt10(void); |
wardm | 12:046346a16ff4 | 47 | static void pinInt11(void); |
wardm | 12:046346a16ff4 | 48 | static void pinInt12(void); |
wardm | 12:046346a16ff4 | 49 | static void pinInt13(void); |
wardm | 12:046346a16ff4 | 50 | static void pinInt14(void); |
wardm | 12:046346a16ff4 | 51 | static void pinInt15(void); |
wardm | 12:046346a16ff4 | 52 | static void pinInt16(void); |
wardm | 12:046346a16ff4 | 53 | static void pinInt17(void); |
wardm | 12:046346a16ff4 | 54 | static void pinInt18(void); |
wardm | 12:046346a16ff4 | 55 | static void pinInt19(void); |
wardm | 12:046346a16ff4 | 56 | static void pinInt20(void); |
wardm | 12:046346a16ff4 | 57 | static void pinInt21(void); |
wardm | 12:046346a16ff4 | 58 | static void pinInt22(void); |
wardm | 12:046346a16ff4 | 59 | static void pinInt23(void); |
wardm | 12:046346a16ff4 | 60 | static void pinInt24(void); |
wardm | 12:046346a16ff4 | 61 | static void pinInt25(void); |
wardm | 12:046346a16ff4 | 62 | static void pinInt26(void); |
wardm | 12:046346a16ff4 | 63 | static void pinInt27(void); |
wardm | 12:046346a16ff4 | 64 | static void pinInt28(void); |
wardm | 12:046346a16ff4 | 65 | static void pinInt29(void); |
wardm | 12:046346a16ff4 | 66 | static void pinInt30(void); |
wardm | 12:046346a16ff4 | 67 | static void pinInt31(void); |
wardm | 12:046346a16ff4 | 68 | static void pinInt32(void); |
wardm | 12:046346a16ff4 | 69 | static void pinInt33(void); |
wardm | 12:046346a16ff4 | 70 | static void pinInt34(void); |
wardm | 12:046346a16ff4 | 71 | static void pinInt35(void); |
wardm | 12:046346a16ff4 | 72 | static void pinInt36(void); |
wardm | 12:046346a16ff4 | 73 | static void pinInt37(void); |
wardm | 12:046346a16ff4 | 74 | static void pinInt38(void); |
wardm | 12:046346a16ff4 | 75 | static void pinInt39(void); |
wardm | 12:046346a16ff4 | 76 | static void pinInt40(void); |
wardm | 12:046346a16ff4 | 77 | static void pinInt41(void); |
wardm | 12:046346a16ff4 | 78 | static void pinInt42(void); |
wardm | 12:046346a16ff4 | 79 | static void pinInt43(void); |
wardm | 12:046346a16ff4 | 80 | static void pinInt44(void); |
wardm | 12:046346a16ff4 | 81 | static void pinInt45(void); |
wardm | 12:046346a16ff4 | 82 | static void pinInt46(void); |
wardm | 12:046346a16ff4 | 83 | static void pinInt47(void); |
wardm | 12:046346a16ff4 | 84 | |
wardm | 12:046346a16ff4 | 85 | |
wardm | 12:046346a16ff4 | 86 | |
wardm | 12:046346a16ff4 | 87 | #define MAX_MCU_PINS 48 |
wardm | 12:046346a16ff4 | 88 | class InterruptIn; |
wardm | 12:046346a16ff4 | 89 | struct intPtrTable { |
wardm | 12:046346a16ff4 | 90 | void (*func)(void); |
wardm | 12:046346a16ff4 | 91 | InterruptIn *context; |
wardm | 12:046346a16ff4 | 92 | } intPtrTable[MAX_MCU_PINS] = { |
wardm | 12:046346a16ff4 | 93 | { pinInt00, NULL }, |
wardm | 12:046346a16ff4 | 94 | { pinInt01, NULL }, |
wardm | 12:046346a16ff4 | 95 | { pinInt02, NULL }, |
wardm | 12:046346a16ff4 | 96 | { pinInt03, NULL }, |
wardm | 12:046346a16ff4 | 97 | { pinInt04, NULL }, |
wardm | 12:046346a16ff4 | 98 | { pinInt05, NULL }, |
wardm | 12:046346a16ff4 | 99 | { pinInt06, NULL }, |
wardm | 12:046346a16ff4 | 100 | { pinInt07, NULL }, |
wardm | 12:046346a16ff4 | 101 | { pinInt08, NULL }, |
wardm | 12:046346a16ff4 | 102 | { pinInt09, NULL }, |
wardm | 12:046346a16ff4 | 103 | { pinInt10, NULL }, |
wardm | 12:046346a16ff4 | 104 | { pinInt11, NULL }, |
wardm | 12:046346a16ff4 | 105 | { pinInt12, NULL }, |
wardm | 12:046346a16ff4 | 106 | { pinInt13, NULL }, |
wardm | 12:046346a16ff4 | 107 | { pinInt14, NULL }, |
wardm | 12:046346a16ff4 | 108 | { pinInt15, NULL }, |
wardm | 12:046346a16ff4 | 109 | { pinInt16, NULL }, |
wardm | 12:046346a16ff4 | 110 | { pinInt17, NULL }, |
wardm | 12:046346a16ff4 | 111 | { pinInt18, NULL }, |
wardm | 12:046346a16ff4 | 112 | { pinInt19, NULL }, |
wardm | 12:046346a16ff4 | 113 | { pinInt20, NULL }, |
wardm | 12:046346a16ff4 | 114 | { pinInt21, NULL }, |
wardm | 12:046346a16ff4 | 115 | { pinInt22, NULL }, |
wardm | 12:046346a16ff4 | 116 | { pinInt23, NULL }, |
wardm | 12:046346a16ff4 | 117 | { pinInt24, NULL }, |
wardm | 12:046346a16ff4 | 118 | { pinInt25, NULL }, |
wardm | 12:046346a16ff4 | 119 | { pinInt26, NULL }, |
wardm | 12:046346a16ff4 | 120 | { pinInt27, NULL }, |
wardm | 12:046346a16ff4 | 121 | { pinInt28, NULL }, |
wardm | 12:046346a16ff4 | 122 | { pinInt29, NULL }, |
wardm | 12:046346a16ff4 | 123 | { pinInt30, NULL }, |
wardm | 12:046346a16ff4 | 124 | { pinInt31, NULL }, |
wardm | 12:046346a16ff4 | 125 | { pinInt32, NULL }, |
wardm | 12:046346a16ff4 | 126 | { pinInt33, NULL }, |
wardm | 12:046346a16ff4 | 127 | { pinInt34, NULL }, |
wardm | 12:046346a16ff4 | 128 | { pinInt35, NULL }, |
wardm | 12:046346a16ff4 | 129 | { pinInt36, NULL }, |
wardm | 12:046346a16ff4 | 130 | { pinInt37, NULL }, |
wardm | 12:046346a16ff4 | 131 | { pinInt38, NULL }, |
wardm | 12:046346a16ff4 | 132 | { pinInt39, NULL }, |
wardm | 12:046346a16ff4 | 133 | { pinInt40, NULL }, |
wardm | 12:046346a16ff4 | 134 | { pinInt41, NULL }, |
wardm | 12:046346a16ff4 | 135 | { pinInt42, NULL }, |
wardm | 12:046346a16ff4 | 136 | { pinInt43, NULL }, |
wardm | 12:046346a16ff4 | 137 | { pinInt44, NULL }, |
wardm | 12:046346a16ff4 | 138 | { pinInt45, NULL }, |
wardm | 12:046346a16ff4 | 139 | { pinInt46, NULL }, |
wardm | 12:046346a16ff4 | 140 | { pinInt47, NULL } |
wardm | 12:046346a16ff4 | 141 | }; // our max MCUs pins |
wardm | 12:046346a16ff4 | 142 | |
wardm | 12:046346a16ff4 | 143 | |
wardm | 12:046346a16ff4 | 144 | |
wardm | 12:046346a16ff4 | 145 | static void pinInt00(void) { InterruptIn::_irq_handler(intPtrTable[0].context); } |
wardm | 12:046346a16ff4 | 146 | static void pinInt01(void) { InterruptIn::_irq_handler(intPtrTable[1].context); } |
wardm | 12:046346a16ff4 | 147 | static void pinInt02(void) { InterruptIn::_irq_handler(intPtrTable[2].context); } |
wardm | 12:046346a16ff4 | 148 | static void pinInt03(void) { InterruptIn::_irq_handler(intPtrTable[3].context); } |
wardm | 12:046346a16ff4 | 149 | static void pinInt04(void) { InterruptIn::_irq_handler(intPtrTable[4].context); } |
wardm | 12:046346a16ff4 | 150 | static void pinInt05(void) { InterruptIn::_irq_handler(intPtrTable[5].context); } |
wardm | 12:046346a16ff4 | 151 | static void pinInt06(void) { InterruptIn::_irq_handler(intPtrTable[6].context); } |
wardm | 12:046346a16ff4 | 152 | static void pinInt07(void) { InterruptIn::_irq_handler(intPtrTable[7].context); } |
wardm | 12:046346a16ff4 | 153 | static void pinInt08(void) { InterruptIn::_irq_handler(intPtrTable[8].context); } |
wardm | 12:046346a16ff4 | 154 | static void pinInt09(void) { InterruptIn::_irq_handler(intPtrTable[9].context); } |
wardm | 12:046346a16ff4 | 155 | static void pinInt10(void) { InterruptIn::_irq_handler(intPtrTable[10].context); } |
wardm | 12:046346a16ff4 | 156 | static void pinInt11(void) { InterruptIn::_irq_handler(intPtrTable[11].context); } |
wardm | 12:046346a16ff4 | 157 | static void pinInt12(void) { InterruptIn::_irq_handler(intPtrTable[12].context); } |
wardm | 12:046346a16ff4 | 158 | static void pinInt13(void) { InterruptIn::_irq_handler(intPtrTable[13].context); } |
wardm | 12:046346a16ff4 | 159 | static void pinInt14(void) { InterruptIn::_irq_handler(intPtrTable[14].context); } |
wardm | 12:046346a16ff4 | 160 | static void pinInt15(void) { InterruptIn::_irq_handler(intPtrTable[15].context); } |
wardm | 12:046346a16ff4 | 161 | static void pinInt16(void) { InterruptIn::_irq_handler(intPtrTable[16].context); } |
wardm | 12:046346a16ff4 | 162 | static void pinInt17(void) { InterruptIn::_irq_handler(intPtrTable[17].context); } |
wardm | 12:046346a16ff4 | 163 | static void pinInt18(void) { InterruptIn::_irq_handler(intPtrTable[18].context); } |
wardm | 12:046346a16ff4 | 164 | static void pinInt19(void) { InterruptIn::_irq_handler(intPtrTable[19].context); } |
wardm | 12:046346a16ff4 | 165 | static void pinInt20(void) { InterruptIn::_irq_handler(intPtrTable[20].context); } |
wardm | 12:046346a16ff4 | 166 | static void pinInt21(void) { InterruptIn::_irq_handler(intPtrTable[21].context); } |
wardm | 12:046346a16ff4 | 167 | static void pinInt22(void) { InterruptIn::_irq_handler(intPtrTable[22].context); } |
wardm | 12:046346a16ff4 | 168 | static void pinInt23(void) { InterruptIn::_irq_handler(intPtrTable[23].context); } |
wardm | 12:046346a16ff4 | 169 | static void pinInt24(void) { InterruptIn::_irq_handler(intPtrTable[24].context); } |
wardm | 12:046346a16ff4 | 170 | static void pinInt25(void) { InterruptIn::_irq_handler(intPtrTable[25].context); } |
wardm | 12:046346a16ff4 | 171 | static void pinInt26(void) { InterruptIn::_irq_handler(intPtrTable[26].context); } |
wardm | 12:046346a16ff4 | 172 | static void pinInt27(void) { InterruptIn::_irq_handler(intPtrTable[27].context); } |
wardm | 12:046346a16ff4 | 173 | static void pinInt28(void) { InterruptIn::_irq_handler(intPtrTable[28].context); } |
wardm | 12:046346a16ff4 | 174 | static void pinInt29(void) { InterruptIn::_irq_handler(intPtrTable[29].context); } |
wardm | 12:046346a16ff4 | 175 | static void pinInt30(void) { InterruptIn::_irq_handler(intPtrTable[30].context); } |
wardm | 12:046346a16ff4 | 176 | static void pinInt31(void) { InterruptIn::_irq_handler(intPtrTable[31].context); } |
wardm | 12:046346a16ff4 | 177 | static void pinInt32(void) { InterruptIn::_irq_handler(intPtrTable[32].context); } |
wardm | 12:046346a16ff4 | 178 | static void pinInt33(void) { InterruptIn::_irq_handler(intPtrTable[33].context); } |
wardm | 12:046346a16ff4 | 179 | static void pinInt34(void) { InterruptIn::_irq_handler(intPtrTable[34].context); } |
wardm | 12:046346a16ff4 | 180 | static void pinInt35(void) { InterruptIn::_irq_handler(intPtrTable[35].context); } |
wardm | 12:046346a16ff4 | 181 | static void pinInt36(void) { InterruptIn::_irq_handler(intPtrTable[36].context); } |
wardm | 12:046346a16ff4 | 182 | static void pinInt37(void) { InterruptIn::_irq_handler(intPtrTable[37].context); } |
wardm | 12:046346a16ff4 | 183 | static void pinInt38(void) { InterruptIn::_irq_handler(intPtrTable[38].context); } |
wardm | 12:046346a16ff4 | 184 | static void pinInt39(void) { InterruptIn::_irq_handler(intPtrTable[39].context); } |
wardm | 12:046346a16ff4 | 185 | static void pinInt40(void) { InterruptIn::_irq_handler(intPtrTable[40].context); } |
wardm | 12:046346a16ff4 | 186 | static void pinInt41(void) { InterruptIn::_irq_handler(intPtrTable[41].context); } |
wardm | 12:046346a16ff4 | 187 | static void pinInt42(void) { InterruptIn::_irq_handler(intPtrTable[42].context); } |
wardm | 12:046346a16ff4 | 188 | static void pinInt43(void) { InterruptIn::_irq_handler(intPtrTable[43].context); } |
wardm | 12:046346a16ff4 | 189 | static void pinInt44(void) { InterruptIn::_irq_handler(intPtrTable[44].context); } |
wardm | 12:046346a16ff4 | 190 | static void pinInt45(void) { InterruptIn::_irq_handler(intPtrTable[45].context); } |
wardm | 12:046346a16ff4 | 191 | static void pinInt46(void) { InterruptIn::_irq_handler(intPtrTable[46].context); } |
wardm | 12:046346a16ff4 | 192 | static void pinInt47(void) { InterruptIn::_irq_handler(intPtrTable[47].context); } |
wardm | 12:046346a16ff4 | 193 | |
wardm | 12:046346a16ff4 | 194 | |
wardm | 12:046346a16ff4 | 195 | void wait_ms(uint32_t ms) |
wardm | 12:046346a16ff4 | 196 | { |
wardm | 12:046346a16ff4 | 197 | uint32_t start = ms_getTicker(); |
wardm | 12:046346a16ff4 | 198 | |
wardm | 12:046346a16ff4 | 199 | while (true) { |
wardm | 12:046346a16ff4 | 200 | uint32_t t = ms_getTicker(); |
wardm | 12:046346a16ff4 | 201 | if (t < start) // warp. |
wardm | 12:046346a16ff4 | 202 | start = 0; |
wardm | 12:046346a16ff4 | 203 | if (t > (start + ms)) |
wardm | 12:046346a16ff4 | 204 | break; |
wardm | 12:046346a16ff4 | 205 | } |
wardm | 12:046346a16ff4 | 206 | } |
wardm | 12:046346a16ff4 | 207 | |
wardm | 12:046346a16ff4 | 208 | struct TimeoutVector TimeOuts[MAX_TIMEOUTS]; |
wardm | 12:046346a16ff4 | 209 | |
wardm | 12:046346a16ff4 | 210 | void |
wardm | 12:046346a16ff4 | 211 | InterruptIn::rise(Callback<void()> func) { |
wardm | 12:046346a16ff4 | 212 | if (_gpioPin >= MAX_MCU_PINS-1) |
wardm | 12:046346a16ff4 | 213 | return; |
wardm | 12:046346a16ff4 | 214 | if (func) { |
wardm | 12:046346a16ff4 | 215 | _func = func; |
wardm | 12:046346a16ff4 | 216 | intPtrTable[_gpioPin].context = this; |
wardm | 12:046346a16ff4 | 217 | attachInterrupt(MYdigitalPinToInterrupt(_gpioPin), intPtrTable[_gpioPin].func, RISING); |
wardm | 12:046346a16ff4 | 218 | } else { |
wardm | 12:046346a16ff4 | 219 | _func = InterruptIn::donothing; |
wardm | 12:046346a16ff4 | 220 | intPtrTable[_gpioPin].context = NULL; |
wardm | 12:046346a16ff4 | 221 | detachInterrupt(_gpioPin); |
wardm | 12:046346a16ff4 | 222 | } |
wardm | 12:046346a16ff4 | 223 | }; |
wardm | 12:046346a16ff4 | 224 | |
wardm | 12:046346a16ff4 | 225 | void |
wardm | 12:046346a16ff4 | 226 | InterruptIn::fall(Callback<void()> func) { |
wardm | 12:046346a16ff4 | 227 | if (func) { |
wardm | 12:046346a16ff4 | 228 | _func = func; |
wardm | 12:046346a16ff4 | 229 | intPtrTable[_gpioPin].context = this; |
wardm | 12:046346a16ff4 | 230 | attachInterrupt(MYdigitalPinToInterrupt(_gpioPin), intPtrTable[_gpioPin].func, FALLING); |
wardm | 12:046346a16ff4 | 231 | } else { |
wardm | 12:046346a16ff4 | 232 | _func = InterruptIn::donothing; |
wardm | 12:046346a16ff4 | 233 | intPtrTable[_gpioPin].context = NULL; |
wardm | 12:046346a16ff4 | 234 | detachInterrupt(_gpioPin); |
wardm | 12:046346a16ff4 | 235 | } |
wardm | 12:046346a16ff4 | 236 | } |
wardm | 12:046346a16ff4 | 237 | |
wardm | 12:046346a16ff4 | 238 | |
wardm | 12:046346a16ff4 | 239 | uint32_t s_getTicker(void) |
wardm | 12:046346a16ff4 | 240 | { |
wardm | 12:046346a16ff4 | 241 | long long ns = ns_getTicker(); |
wardm | 12:046346a16ff4 | 242 | ns /= (long long)1000000000; // to secs |
wardm | 12:046346a16ff4 | 243 | |
wardm | 12:046346a16ff4 | 244 | int secs = ns; |
wardm | 12:046346a16ff4 | 245 | return secs; |
wardm | 12:046346a16ff4 | 246 | } |
wardm | 12:046346a16ff4 | 247 | |
wardm | 12:046346a16ff4 | 248 | |
wardm | 12:046346a16ff4 | 249 | uint32_t ms_getTicker(void) |
wardm | 12:046346a16ff4 | 250 | { |
wardm | 12:046346a16ff4 | 251 | uint32_t us = us_getTicker(); |
wardm | 12:046346a16ff4 | 252 | |
wardm | 12:046346a16ff4 | 253 | us /= 1000; // to ms |
wardm | 12:046346a16ff4 | 254 | return us; |
wardm | 12:046346a16ff4 | 255 | } |
wardm | 12:046346a16ff4 | 256 | |
wardm | 12:046346a16ff4 | 257 | uint32_t us_getTicker(void) |
wardm | 12:046346a16ff4 | 258 | { |
wardm | 12:046346a16ff4 | 259 | long long ns = ns_getTicker(); |
wardm | 12:046346a16ff4 | 260 | |
wardm | 12:046346a16ff4 | 261 | ns /= (long long)1000; // to us |
wardm | 12:046346a16ff4 | 262 | uint32_t us = ns & 0xffffffff; |
wardm | 12:046346a16ff4 | 263 | |
wardm | 12:046346a16ff4 | 264 | return us; |
wardm | 12:046346a16ff4 | 265 | } |
wardm | 12:046346a16ff4 | 266 | |
wardm | 12:046346a16ff4 | 267 | |
wardm | 12:046346a16ff4 | 268 | void |
wardm | 12:046346a16ff4 | 269 | Timeout::insert(void) |
wardm | 12:046346a16ff4 | 270 | { |
wardm | 12:046346a16ff4 | 271 | noInterrupts(); |
wardm | 12:046346a16ff4 | 272 | for (int i = 0; i < MAX_TIMEOUTS-1; i++) { |
wardm | 12:046346a16ff4 | 273 | struct TimeoutVector *tvp = &TimeOuts[i]; |
wardm | 12:046346a16ff4 | 274 | if (tvp->timer == this) // already here, timer has been restartet. |
wardm | 12:046346a16ff4 | 275 | break; |
wardm | 12:046346a16ff4 | 276 | if (tvp->timer == NULL) { |
wardm | 12:046346a16ff4 | 277 | tvp->timer = this; |
wardm | 12:046346a16ff4 | 278 | break; |
wardm | 12:046346a16ff4 | 279 | } |
wardm | 12:046346a16ff4 | 280 | } |
wardm | 12:046346a16ff4 | 281 | interrupts(); |
wardm | 12:046346a16ff4 | 282 | } |
wardm | 12:046346a16ff4 | 283 | |
wardm | 12:046346a16ff4 | 284 | void |
wardm | 12:046346a16ff4 | 285 | Timeout::remove(void) |
wardm | 12:046346a16ff4 | 286 | { |
wardm | 12:046346a16ff4 | 287 | noInterrupts(); |
wardm | 12:046346a16ff4 | 288 | for (int i = 0; i < MAX_TIMEOUTS-1; i++) { |
wardm | 12:046346a16ff4 | 289 | struct TimeoutVector *tvp = &TimeOuts[i]; |
wardm | 12:046346a16ff4 | 290 | if (tvp->timer == this) { |
wardm | 12:046346a16ff4 | 291 | tvp->timer = NULL; |
wardm | 12:046346a16ff4 | 292 | break; |
wardm | 12:046346a16ff4 | 293 | } |
wardm | 12:046346a16ff4 | 294 | } |
wardm | 12:046346a16ff4 | 295 | interrupts(); |
wardm | 12:046346a16ff4 | 296 | } |
wardm | 12:046346a16ff4 | 297 | |
wardm | 12:046346a16ff4 | 298 | |
wardm | 12:046346a16ff4 | 299 | void |
wardm | 12:046346a16ff4 | 300 | Timeout::restart() |
wardm | 12:046346a16ff4 | 301 | { |
wardm | 12:046346a16ff4 | 302 | Tcc *t = getTimeout_tcc(); |
wardm | 12:046346a16ff4 | 303 | uint64_t timeout = ~0; |
wardm | 12:046346a16ff4 | 304 | |
wardm | 12:046346a16ff4 | 305 | /* |
wardm | 12:046346a16ff4 | 306 | * find the lowest timeout value which is our the next timeout |
wardm | 12:046346a16ff4 | 307 | * zero means stop the timer. |
wardm | 12:046346a16ff4 | 308 | */ |
wardm | 12:046346a16ff4 | 309 | noInterrupts(); |
wardm | 12:046346a16ff4 | 310 | for (int i = 0; i < MAX_TIMEOUTS-1; i++) { |
wardm | 12:046346a16ff4 | 311 | struct TimeoutVector *tvp = &TimeOuts[i]; |
wardm | 12:046346a16ff4 | 312 | if (tvp->timer) { |
wardm | 12:046346a16ff4 | 313 | if (tvp->timer->_timeout < timeout) { |
wardm | 12:046346a16ff4 | 314 | timeout = tvp->timer->_timeout; |
wardm | 12:046346a16ff4 | 315 | } |
wardm | 12:046346a16ff4 | 316 | } |
wardm | 12:046346a16ff4 | 317 | } |
wardm | 12:046346a16ff4 | 318 | interrupts(); |
wardm | 12:046346a16ff4 | 319 | |
wardm | 12:046346a16ff4 | 320 | if (timeout == (uint64_t)~0) { |
wardm | 12:046346a16ff4 | 321 | stopTimer(t); |
wardm | 12:046346a16ff4 | 322 | return; |
wardm | 12:046346a16ff4 | 323 | } |
wardm | 12:046346a16ff4 | 324 | |
wardm | 12:046346a16ff4 | 325 | uint64_t nsecs = ns_getTicker(); |
wardm | 12:046346a16ff4 | 326 | |
wardm | 12:046346a16ff4 | 327 | if (timeout > nsecs) { |
wardm | 12:046346a16ff4 | 328 | startTimer(t, (uint64_t)timeout - (uint64_t)nsecs); |
wardm | 12:046346a16ff4 | 329 | return; |
wardm | 12:046346a16ff4 | 330 | } else { |
wardm | 12:046346a16ff4 | 331 | startTimer(t, (uint64_t)1); // just one nsec to trigger interrrupt |
wardm | 12:046346a16ff4 | 332 | } |
wardm | 12:046346a16ff4 | 333 | } |
wardm | 12:046346a16ff4 | 334 | |
wardm | 12:046346a16ff4 | 335 | #endif // ARDUINO |