test

Dependents:   Telemetria_RX_SD_GPS_copy Telemetria_RX_SD_GPS Telemetria_TX Telemetria_TX ... more

Committer:
Helmut Tschemernjak
Date:
Fri Aug 11 15:11:08 2017 +0200
Revision:
84:3428e25c7157
Parent:
80:62f0b027efff
Child:
88:d120930aeb13
Modified InitSerial() to check if the USB Serial is active,
turn off USB if no SerialUSB is being in use to save power

Who changed what in which revision?

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