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