test
Dependents: Telemetria_RX_SD_GPS_copy Telemetria_RX_SD_GPS Telemetria_TX Telemetria_TX ... more
sx1276/arduino-mbed.cpp@66:fbb2da34bd9a, 2017-07-12 (annotated)
- Committer:
- Helmut Tschemernjak
- Date:
- Wed Jul 12 15:11:30 2017 +0200
- Revision:
- 66:fbb2da34bd9a
- Parent:
- 65:b2d98328fcba
- Child:
- 67:d3afd803f40d
Added support for ticker timer
Made the timer functions more generic
Who changed what in which revision?
User | Revision | Line number | New 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 | 65:b2d98328fcba | 12 | |
Helmut Tschemernjak | 65:b2d98328fcba | 13 | |
Helmut Tschemernjak | 65:b2d98328fcba | 14 | static void pinInt00(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 15 | static void pinInt01(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 16 | static void pinInt02(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 17 | static void pinInt03(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 18 | static void pinInt04(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 19 | static void pinInt05(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 20 | static void pinInt06(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 21 | static void pinInt07(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 22 | static void pinInt08(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 23 | static void pinInt09(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 24 | static void pinInt10(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 25 | static void pinInt11(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 26 | static void pinInt12(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 27 | static void pinInt13(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 28 | static void pinInt14(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 29 | static void pinInt15(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 30 | static void pinInt16(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 31 | static void pinInt17(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 32 | static void pinInt18(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 33 | static void pinInt19(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 34 | static void pinInt20(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 35 | static void pinInt21(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 36 | static void pinInt22(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 37 | static void pinInt23(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 38 | static void pinInt24(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 39 | static void pinInt25(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 40 | static void pinInt26(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 41 | static void pinInt27(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 42 | static void pinInt28(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 43 | static void pinInt29(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 44 | static void pinInt30(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 45 | static void pinInt31(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 46 | static void pinInt32(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 47 | static void pinInt33(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 48 | static void pinInt34(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 49 | static void pinInt35(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 50 | static void pinInt36(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 51 | static void pinInt37(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 52 | static void pinInt38(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 53 | static void pinInt39(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 54 | static void pinInt40(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 55 | static void pinInt41(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 56 | static void pinInt42(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 57 | static void pinInt43(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 58 | static void pinInt44(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 59 | static void pinInt45(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 60 | static void pinInt46(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 61 | static void pinInt47(void); |
Helmut Tschemernjak | 46:e78a1d0391ac | 62 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 63 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 64 | |
Helmut Tschemernjak | 65:b2d98328fcba | 65 | #define MAX_MCU_PINS 48 |
Helmut Tschemernjak | 65:b2d98328fcba | 66 | class InterruptIn; |
Helmut Tschemernjak | 65:b2d98328fcba | 67 | struct intPtrTable { |
Helmut Tschemernjak | 65:b2d98328fcba | 68 | void (*func)(void); |
Helmut Tschemernjak | 65:b2d98328fcba | 69 | InterruptIn *context; |
Helmut Tschemernjak | 65:b2d98328fcba | 70 | } intPtrTable[MAX_MCU_PINS] = { |
Helmut Tschemernjak | 65:b2d98328fcba | 71 | { pinInt00, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 72 | { pinInt01, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 73 | { pinInt02, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 74 | { pinInt03, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 75 | { pinInt04, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 76 | { pinInt05, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 77 | { pinInt06, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 78 | { pinInt07, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 79 | { pinInt08, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 80 | { pinInt09, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 81 | { pinInt10, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 82 | { pinInt11, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 83 | { pinInt12, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 84 | { pinInt13, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 85 | { pinInt14, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 86 | { pinInt15, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 87 | { pinInt16, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 88 | { pinInt17, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 89 | { pinInt18, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 90 | { pinInt19, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 91 | { pinInt20, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 92 | { pinInt21, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 93 | { pinInt22, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 94 | { pinInt23, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 95 | { pinInt24, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 96 | { pinInt25, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 97 | { pinInt26, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 98 | { pinInt27, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 99 | { pinInt28, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 100 | { pinInt29, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 101 | { pinInt30, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 102 | { pinInt31, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 103 | { pinInt32, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 104 | { pinInt33, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 105 | { pinInt34, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 106 | { pinInt35, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 107 | { pinInt36, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 108 | { pinInt37, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 109 | { pinInt38, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 110 | { pinInt39, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 111 | { pinInt40, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 112 | { pinInt41, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 113 | { pinInt42, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 114 | { pinInt43, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 115 | { pinInt44, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 116 | { pinInt45, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 117 | { pinInt46, NULL }, |
Helmut Tschemernjak | 65:b2d98328fcba | 118 | { pinInt47, NULL } |
Helmut Tschemernjak | 65:b2d98328fcba | 119 | }; // our max MCUs pins |
Helmut Tschemernjak | 46:e78a1d0391ac | 120 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 121 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 122 | |
Helmut Tschemernjak | 65:b2d98328fcba | 123 | static void pinInt00(void) { InterruptIn::_irq_handler(intPtrTable[0].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 124 | static void pinInt01(void) { InterruptIn::_irq_handler(intPtrTable[1].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 125 | static void pinInt02(void) { InterruptIn::_irq_handler(intPtrTable[2].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 126 | static void pinInt03(void) { InterruptIn::_irq_handler(intPtrTable[3].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 127 | static void pinInt04(void) { InterruptIn::_irq_handler(intPtrTable[4].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 128 | static void pinInt05(void) { InterruptIn::_irq_handler(intPtrTable[5].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 129 | static void pinInt06(void) { InterruptIn::_irq_handler(intPtrTable[6].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 130 | static void pinInt07(void) { InterruptIn::_irq_handler(intPtrTable[7].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 131 | static void pinInt08(void) { InterruptIn::_irq_handler(intPtrTable[8].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 132 | static void pinInt09(void) { InterruptIn::_irq_handler(intPtrTable[9].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 133 | static void pinInt10(void) { InterruptIn::_irq_handler(intPtrTable[10].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 134 | static void pinInt11(void) { InterruptIn::_irq_handler(intPtrTable[11].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 135 | static void pinInt12(void) { InterruptIn::_irq_handler(intPtrTable[12].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 136 | static void pinInt13(void) { InterruptIn::_irq_handler(intPtrTable[13].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 137 | static void pinInt14(void) { InterruptIn::_irq_handler(intPtrTable[14].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 138 | static void pinInt15(void) { InterruptIn::_irq_handler(intPtrTable[15].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 139 | static void pinInt16(void) { InterruptIn::_irq_handler(intPtrTable[16].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 140 | static void pinInt17(void) { InterruptIn::_irq_handler(intPtrTable[17].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 141 | static void pinInt18(void) { InterruptIn::_irq_handler(intPtrTable[18].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 142 | static void pinInt19(void) { InterruptIn::_irq_handler(intPtrTable[19].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 143 | static void pinInt20(void) { InterruptIn::_irq_handler(intPtrTable[20].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 144 | static void pinInt21(void) { InterruptIn::_irq_handler(intPtrTable[21].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 145 | static void pinInt22(void) { InterruptIn::_irq_handler(intPtrTable[22].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 146 | static void pinInt23(void) { InterruptIn::_irq_handler(intPtrTable[23].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 147 | static void pinInt24(void) { InterruptIn::_irq_handler(intPtrTable[24].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 148 | static void pinInt25(void) { InterruptIn::_irq_handler(intPtrTable[25].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 149 | static void pinInt26(void) { InterruptIn::_irq_handler(intPtrTable[26].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 150 | static void pinInt27(void) { InterruptIn::_irq_handler(intPtrTable[27].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 151 | static void pinInt28(void) { InterruptIn::_irq_handler(intPtrTable[28].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 152 | static void pinInt29(void) { InterruptIn::_irq_handler(intPtrTable[29].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 153 | static void pinInt30(void) { InterruptIn::_irq_handler(intPtrTable[30].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 154 | static void pinInt31(void) { InterruptIn::_irq_handler(intPtrTable[31].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 155 | static void pinInt32(void) { InterruptIn::_irq_handler(intPtrTable[32].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 156 | static void pinInt33(void) { InterruptIn::_irq_handler(intPtrTable[33].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 157 | static void pinInt34(void) { InterruptIn::_irq_handler(intPtrTable[34].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 158 | static void pinInt35(void) { InterruptIn::_irq_handler(intPtrTable[35].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 159 | static void pinInt36(void) { InterruptIn::_irq_handler(intPtrTable[36].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 160 | static void pinInt37(void) { InterruptIn::_irq_handler(intPtrTable[37].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 161 | static void pinInt38(void) { InterruptIn::_irq_handler(intPtrTable[38].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 162 | static void pinInt39(void) { InterruptIn::_irq_handler(intPtrTable[39].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 163 | static void pinInt40(void) { InterruptIn::_irq_handler(intPtrTable[40].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 164 | static void pinInt41(void) { InterruptIn::_irq_handler(intPtrTable[41].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 165 | static void pinInt42(void) { InterruptIn::_irq_handler(intPtrTable[42].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 166 | static void pinInt43(void) { InterruptIn::_irq_handler(intPtrTable[43].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 167 | static void pinInt44(void) { InterruptIn::_irq_handler(intPtrTable[44].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 168 | static void pinInt45(void) { InterruptIn::_irq_handler(intPtrTable[45].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 169 | static void pinInt46(void) { InterruptIn::_irq_handler(intPtrTable[46].context); } |
Helmut Tschemernjak | 65:b2d98328fcba | 170 | static void pinInt47(void) { InterruptIn::_irq_handler(intPtrTable[47].context); } |
Helmut Tschemernjak | 46:e78a1d0391ac | 171 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 172 | |
Helmut Tschemernjak | 65:b2d98328fcba | 173 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 174 | |
Helmut Tschemernjak | 65:b2d98328fcba | 175 | void |
Helmut Tschemernjak | 65:b2d98328fcba | 176 | InterruptIn::rise(Callback<void()> func) { |
Helmut Tschemernjak | 65:b2d98328fcba | 177 | if (_gpioPin >= MAX_MCU_PINS-1) |
Helmut Tschemernjak | 65:b2d98328fcba | 178 | return; |
Helmut Tschemernjak | 65:b2d98328fcba | 179 | if (func) { |
Helmut Tschemernjak | 65:b2d98328fcba | 180 | _func = func; |
Helmut Tschemernjak | 65:b2d98328fcba | 181 | intPtrTable[_gpioPin].context = this; |
Helmut Tschemernjak | 65:b2d98328fcba | 182 | attachInterrupt(digitalPinToInterrupt(_gpioPin), intPtrTable[_gpioPin].func, RISING); |
Helmut Tschemernjak | 65:b2d98328fcba | 183 | } else { |
Helmut Tschemernjak | 65:b2d98328fcba | 184 | _func = InterruptIn::donothing; |
Helmut Tschemernjak | 65:b2d98328fcba | 185 | intPtrTable[_gpioPin].context = NULL; |
Helmut Tschemernjak | 65:b2d98328fcba | 186 | detachInterrupt(_gpioPin); |
Helmut Tschemernjak | 46:e78a1d0391ac | 187 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 188 | }; |
Helmut Tschemernjak | 46:e78a1d0391ac | 189 | |
Helmut Tschemernjak | 65:b2d98328fcba | 190 | void |
Helmut Tschemernjak | 65:b2d98328fcba | 191 | InterruptIn::fall(Callback<void()> func) { |
Helmut Tschemernjak | 65:b2d98328fcba | 192 | if (func) { |
Helmut Tschemernjak | 65:b2d98328fcba | 193 | _func = func; |
Helmut Tschemernjak | 65:b2d98328fcba | 194 | intPtrTable[_gpioPin].context = this; |
Helmut Tschemernjak | 65:b2d98328fcba | 195 | attachInterrupt(digitalPinToInterrupt(_gpioPin), intPtrTable[_gpioPin].func, FALLING); |
Helmut Tschemernjak | 65:b2d98328fcba | 196 | } else { |
Helmut Tschemernjak | 65:b2d98328fcba | 197 | _func = InterruptIn::donothing; |
Helmut Tschemernjak | 65:b2d98328fcba | 198 | intPtrTable[_gpioPin].context = NULL; |
Helmut Tschemernjak | 65:b2d98328fcba | 199 | detachInterrupt(_gpioPin); |
Helmut Tschemernjak | 46:e78a1d0391ac | 200 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 201 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 202 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 203 | |
Helmut Tschemernjak | 65:b2d98328fcba | 204 | #define MAX_TIMEOUTS 10 |
Helmut Tschemernjak | 65:b2d98328fcba | 205 | class Timeout; |
Helmut Tschemernjak | 65:b2d98328fcba | 206 | struct TimeoutVector { |
Helmut Tschemernjak | 65:b2d98328fcba | 207 | Timeout *timer; |
Helmut Tschemernjak | 65:b2d98328fcba | 208 | volatile uint32_t timeout; // us |
Helmut Tschemernjak | 65:b2d98328fcba | 209 | } TimeOuts[MAX_TIMEOUTS]; |
Helmut Tschemernjak | 65:b2d98328fcba | 210 | |
Helmut Tschemernjak | 65:b2d98328fcba | 211 | |
Helmut Tschemernjak | 65:b2d98328fcba | 212 | #if defined(__SAMD21G18A__) || defined(__SAMD21J18A__) |
Helmut Tschemernjak | 65:b2d98328fcba | 213 | /* |
Helmut Tschemernjak | 65:b2d98328fcba | 214 | * __SAMD21J18A__ is the SamD21 Explained Board |
Helmut Tschemernjak | 65:b2d98328fcba | 215 | * __SAMD21G18A__ is Genuino Zero-Board (compatible with the LoRa board) |
Helmut Tschemernjak | 65:b2d98328fcba | 216 | */ |
Helmut Tschemernjak | 65:b2d98328fcba | 217 | |
Helmut Tschemernjak | 65:b2d98328fcba | 218 | /* |
Helmut Tschemernjak | 65:b2d98328fcba | 219 | * see tcc.h included from |
Helmut Tschemernjak | 65:b2d98328fcba | 220 | * Arduino15/packages/arduino/tools/CMSIS-Atmel/1.1.0/CMSIS/ |
Helmut Tschemernjak | 65:b2d98328fcba | 221 | * Device/ATMEL/samd21/include/component/tcc.h |
Helmut Tschemernjak | 66:fbb2da34bd9a | 222 | * See also tcc.c (ASF/mbed, e.g. Tcc_get_count_value) |
Helmut Tschemernjak | 66:fbb2da34bd9a | 223 | * TODO connect the clock source to OSCULP32K. |
Helmut Tschemernjak | 65:b2d98328fcba | 224 | */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 225 | static void initTimer(Tcc *t); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 226 | static uint32_t getTimerCount(Tcc *t); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 227 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 228 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 229 | static const struct TCC_config { |
Helmut Tschemernjak | 65:b2d98328fcba | 230 | Tcc *tcc_ptr; |
Helmut Tschemernjak | 65:b2d98328fcba | 231 | IRQn_Type tcc_irq; |
Helmut Tschemernjak | 65:b2d98328fcba | 232 | uint8_t nbits; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 233 | } TCC_data[] { |
Helmut Tschemernjak | 65:b2d98328fcba | 234 | { TCC0, TCC0_IRQn, 24 }, |
Helmut Tschemernjak | 65:b2d98328fcba | 235 | { TCC1, TCC1_IRQn, 24 }, |
Helmut Tschemernjak | 65:b2d98328fcba | 236 | { TCC2, TCC2_IRQn, 16 }, |
Helmut Tschemernjak | 66:fbb2da34bd9a | 237 | { NULL, (IRQn_Type)NULL, 0 } |
Helmut Tschemernjak | 65:b2d98328fcba | 238 | }; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 239 | #define USE_TCC_TIMEOUT 0 // TCC0, TTC1, TTC2 are working using the Arduino D21 |
Helmut Tschemernjak | 66:fbb2da34bd9a | 240 | #define USE_TCC_TICKER 1 |
Helmut Tschemernjak | 66:fbb2da34bd9a | 241 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 242 | #define NS_PER_CLOCK 21333 // ns secs per clock |
Helmut Tschemernjak | 66:fbb2da34bd9a | 243 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 244 | /* ----------------- TICKER TIMER CODE ----------------------*/ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 245 | long long ticker_ns = 0; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 246 | static bool initTickerDone = false; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 247 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 248 | /*long long*/ uint32_t us_getTicker(void) |
Helmut Tschemernjak | 66:fbb2da34bd9a | 249 | { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 250 | Tcc *t = TCC_data[USE_TCC_TICKER].tcc_ptr; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 251 | if (!initTickerDone) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 252 | initTimer(t); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 253 | initTickerDone = true; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 254 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 255 | int bits = TCC_data[USE_TCC_TIMEOUT].nbits; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 256 | int maxCounts = (uint32_t)(1<<bits)-1; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 257 | t->CC[0].bit.CC = 0xfff; // maxCounts; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 258 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 259 | t->CTRLA.reg |= TCC_CTRLA_ENABLE ; // Enable TC |
Helmut Tschemernjak | 66:fbb2da34bd9a | 260 | while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync |
Helmut Tschemernjak | 66:fbb2da34bd9a | 261 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 262 | } |
Helmut Tschemernjak | 66:fbb2da34bd9a | 263 | long long tmp_ns = ticker_ns; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 264 | tmp_ns += (NS_PER_CLOCK * getTimerCount(t)); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 265 | uint32_t t32 = (long long)tmp_ns / (long long)1000 / (long long)1000; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 266 | return t32; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 267 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 268 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 269 | #if USE_TCC_TICKER == 0 |
Helmut Tschemernjak | 66:fbb2da34bd9a | 270 | void TCC0_Handler() |
Helmut Tschemernjak | 66:fbb2da34bd9a | 271 | #elif USE_TCC_TICKER == 1 |
Helmut Tschemernjak | 66:fbb2da34bd9a | 272 | void TCC1_Handler() |
Helmut Tschemernjak | 66:fbb2da34bd9a | 273 | #elif USE_TCC_TICKER == 2 |
Helmut Tschemernjak | 66:fbb2da34bd9a | 274 | void TCC2_Handler() |
Helmut Tschemernjak | 66:fbb2da34bd9a | 275 | #endif |
Helmut Tschemernjak | 66:fbb2da34bd9a | 276 | { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 277 | Tcc *t = TCC_data[USE_TCC_TICKER].tcc_ptr; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 278 | /* |
Helmut Tschemernjak | 66:fbb2da34bd9a | 279 | * Overflow means the max timer exeeded |
Helmut Tschemernjak | 66:fbb2da34bd9a | 280 | */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 281 | if (t->INTFLAG.bit.OVF == 1) { // A overflow caused the interrupt |
Helmut Tschemernjak | 66:fbb2da34bd9a | 282 | Serial.print("Ticker_OVF\r\n"); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 283 | t->INTFLAG.bit.OVF = 1; // writing a one clears the flag ovf flag |
Helmut Tschemernjak | 66:fbb2da34bd9a | 284 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 285 | int bits = TCC_data[USE_TCC_TICKER].nbits; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 286 | int maxCounts = (uint32_t)(1<<bits); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 287 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 288 | ticker_ns += (NS_PER_CLOCK * maxCounts); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 289 | } |
Helmut Tschemernjak | 66:fbb2da34bd9a | 290 | if (t->INTFLAG.bit.MC0 == 1) { // A compare to cc0 caused the interrupt |
Helmut Tschemernjak | 66:fbb2da34bd9a | 291 | Serial.print("MC0\r\n"); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 292 | t->INTFLAG.bit.MC0 = 1; // writing a one clears the MCO (match capture) flag |
Helmut Tschemernjak | 66:fbb2da34bd9a | 293 | } |
Helmut Tschemernjak | 66:fbb2da34bd9a | 294 | Serial.println("TICKER_INTR"); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 295 | #if 1 |
Helmut Tschemernjak | 66:fbb2da34bd9a | 296 | t->CTRLA.reg &= ~TCC_CTRLA_ENABLE; // Disable TC |
Helmut Tschemernjak | 66:fbb2da34bd9a | 297 | while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync |
Helmut Tschemernjak | 66:fbb2da34bd9a | 298 | t->CTRLA.reg |= TCC_CTRLA_ENABLE; // Disable TC |
Helmut Tschemernjak | 66:fbb2da34bd9a | 299 | while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync |
Helmut Tschemernjak | 66:fbb2da34bd9a | 300 | #endif |
Helmut Tschemernjak | 66:fbb2da34bd9a | 301 | } |
Helmut Tschemernjak | 66:fbb2da34bd9a | 302 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 303 | /* ----------------- TIMEOUT TIMER CODE ----------------------*/ |
Helmut Tschemernjak | 65:b2d98328fcba | 304 | |
Helmut Tschemernjak | 65:b2d98328fcba | 305 | static bool initTimerDone = false; |
Helmut Tschemernjak | 65:b2d98328fcba | 306 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 307 | static void initTimer(Tcc *t) { |
Helmut Tschemernjak | 65:b2d98328fcba | 308 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 309 | // Enable clock for TCC, see gclk.h |
Helmut Tschemernjak | 66:fbb2da34bd9a | 310 | if (t == TCC0 || t == TCC1) { |
Helmut Tschemernjak | 65:b2d98328fcba | 311 | REG_GCLK_CLKCTRL = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID_TCC0_TCC1); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 312 | } else if (t == TCC2) { |
Helmut Tschemernjak | 65:b2d98328fcba | 313 | REG_GCLK_CLKCTRL = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID_TCC2_TC3_Val); |
Helmut Tschemernjak | 46:e78a1d0391ac | 314 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 315 | while (GCLK->STATUS.bit.SYNCBUSY == 1); // wait for sync |
Helmut Tschemernjak | 65:b2d98328fcba | 316 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 317 | t->CTRLA.reg &= ~TCC_CTRLA_ENABLE; // Disable TCC |
Helmut Tschemernjak | 66:fbb2da34bd9a | 318 | while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync |
Helmut Tschemernjak | 65:b2d98328fcba | 319 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 320 | t->CTRLA.reg |= (TCC_CTRLA_PRESCALER_DIV1024 | TCC_CTRLA_RUNSTDBY); // Set perscaler |
Helmut Tschemernjak | 65:b2d98328fcba | 321 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 322 | t->WAVE.reg |= TCC_WAVE_WAVEGEN_NFRQ; // Set wave form configuration |
Helmut Tschemernjak | 66:fbb2da34bd9a | 323 | while (t->SYNCBUSY.bit.WAVE == 1); // wait for sync |
Helmut Tschemernjak | 65:b2d98328fcba | 324 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 325 | t->PER.bit.PER = 0xFFFFFF; // set counter top to max 24 bit |
Helmut Tschemernjak | 66:fbb2da34bd9a | 326 | while (t->SYNCBUSY.bit.PER == 1); // wait for sync |
Helmut Tschemernjak | 65:b2d98328fcba | 327 | |
Helmut Tschemernjak | 65:b2d98328fcba | 328 | // the compare counter TC->CC[0].reg will be set in the startTimer |
Helmut Tschemernjak | 65:b2d98328fcba | 329 | // after the timeout calculation is known. |
Helmut Tschemernjak | 65:b2d98328fcba | 330 | |
Helmut Tschemernjak | 65:b2d98328fcba | 331 | // Interrupts |
Helmut Tschemernjak | 66:fbb2da34bd9a | 332 | t->INTENSET.reg = 0; // disable all interrupts |
Helmut Tschemernjak | 66:fbb2da34bd9a | 333 | t->INTENSET.bit.OVF = 1; // enable overfollow |
Helmut Tschemernjak | 66:fbb2da34bd9a | 334 | t->INTENSET.bit.MC0 = 1; // enable compare match to CC0 |
Helmut Tschemernjak | 65:b2d98328fcba | 335 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 336 | const struct TCC_config *cp = &TCC_data[0]; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 337 | while (cp->tcc_ptr) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 338 | if (cp->tcc_ptr == t) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 339 | NVIC_EnableIRQ(cp->tcc_irq); // Enable InterruptVector |
Helmut Tschemernjak | 66:fbb2da34bd9a | 340 | break; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 341 | } |
Helmut Tschemernjak | 66:fbb2da34bd9a | 342 | cp++; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 343 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 344 | initTimerDone = true; |
Helmut Tschemernjak | 46:e78a1d0391ac | 345 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 346 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 347 | static uint32_t getTimerCount(Tcc *t) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 348 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 349 | uint32_t last_cmd; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 350 | /* Wait last command done */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 351 | do { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 352 | while (t->SYNCBUSY.bit.CTRLB); /* Wait for sync */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 353 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 354 | last_cmd = t->CTRLBSET.reg & TCC_CTRLBSET_CMD_Msk; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 355 | if (TCC_CTRLBSET_CMD_NONE == last_cmd) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 356 | /* Issue read command and break */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 357 | t->CTRLBSET.bit.CMD = TCC_CTRLBSET_CMD_READSYNC_Val; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 358 | break; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 359 | } else if (TCC_CTRLBSET_CMD_READSYNC == last_cmd) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 360 | /* Command have been issued */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 361 | break; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 362 | } |
Helmut Tschemernjak | 66:fbb2da34bd9a | 363 | } while (1); |
Helmut Tschemernjak | 65:b2d98328fcba | 364 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 365 | while (t->SYNCBUSY.bit.COUNT); /* Wait for sync */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 366 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 367 | return t->COUNT.reg; |
Helmut Tschemernjak | 46:e78a1d0391ac | 368 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 369 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 370 | static void stopTimer(Tcc *t) |
Helmut Tschemernjak | 46:e78a1d0391ac | 371 | { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 372 | t->CTRLA.reg &= ~TCC_CTRLA_ENABLE; // Disable TC |
Helmut Tschemernjak | 66:fbb2da34bd9a | 373 | while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync |
Helmut Tschemernjak | 66:fbb2da34bd9a | 374 | } |
Helmut Tschemernjak | 66:fbb2da34bd9a | 375 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 376 | static void startTimer(Tcc *t, uint32_t delay_us) |
Helmut Tschemernjak | 66:fbb2da34bd9a | 377 | { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 378 | if (!initTimerDone) |
Helmut Tschemernjak | 66:fbb2da34bd9a | 379 | initTimer(t); // initial setup with stopped timer |
Helmut Tschemernjak | 65:b2d98328fcba | 380 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 381 | stopTimer(t); // avoid timer interrupts while calculating |
Helmut Tschemernjak | 46:e78a1d0391ac | 382 | |
Helmut Tschemernjak | 65:b2d98328fcba | 383 | /* |
Helmut Tschemernjak | 65:b2d98328fcba | 384 | * every 21333 ns equals one tick (1/(48000000/1024)) |
Helmut Tschemernjak | 65:b2d98328fcba | 385 | * COUNT*DIVIDER*SECS until interrupt |
Helmut Tschemernjak | 65:b2d98328fcba | 386 | * 48 Mhz = (65536*1024)/1.398636s |
Helmut Tschemernjak | 65:b2d98328fcba | 387 | */ |
Helmut Tschemernjak | 65:b2d98328fcba | 388 | long long nclocks = delay_us * 1000; // ns; |
Helmut Tschemernjak | 65:b2d98328fcba | 389 | nclocks = nclocks / 21333; |
Helmut Tschemernjak | 65:b2d98328fcba | 390 | int nCounts = nclocks; |
Helmut Tschemernjak | 65:b2d98328fcba | 391 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 392 | int bits = TCC_data[USE_TCC_TIMEOUT].nbits; |
Helmut Tschemernjak | 65:b2d98328fcba | 393 | int maxCounts = (uint32_t)(1<<bits)-1; |
Helmut Tschemernjak | 46:e78a1d0391ac | 394 | |
Helmut Tschemernjak | 65:b2d98328fcba | 395 | if (nCounts > maxCounts) // if count exceeds timer capacity |
Helmut Tschemernjak | 65:b2d98328fcba | 396 | nCounts = maxCounts; // set the largest posible count. |
Helmut Tschemernjak | 66:fbb2da34bd9a | 397 | if (nCounts <= 0) |
Helmut Tschemernjak | 65:b2d98328fcba | 398 | nCounts = 1; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 399 | t->CC[0].bit.CC = nCounts; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 400 | while (t->SYNCBUSY.bit.CC0 == 1); // wait for sync |
Helmut Tschemernjak | 46:e78a1d0391ac | 401 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 402 | t->CTRLA.reg |= TCC_CTRLA_ENABLE ; // Enable TC |
Helmut Tschemernjak | 66:fbb2da34bd9a | 403 | while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync |
Helmut Tschemernjak | 65:b2d98328fcba | 404 | #if 1 |
Helmut Tschemernjak | 65:b2d98328fcba | 405 | Serial.print(millis(), DEC); |
Helmut Tschemernjak | 65:b2d98328fcba | 406 | Serial.print(" startTimer: nCounts="); |
Helmut Tschemernjak | 65:b2d98328fcba | 407 | Serial.println(nCounts, DEC); |
Helmut Tschemernjak | 65:b2d98328fcba | 408 | #endif |
Helmut Tschemernjak | 46:e78a1d0391ac | 409 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 410 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 411 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 412 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 413 | #if USE_TCC_TIMEOUT == 0 |
Helmut Tschemernjak | 65:b2d98328fcba | 414 | void TCC0_Handler() |
Helmut Tschemernjak | 66:fbb2da34bd9a | 415 | #elif USE_TCC_TIMEOUT == 1 |
Helmut Tschemernjak | 65:b2d98328fcba | 416 | void TCC1_Handler() |
Helmut Tschemernjak | 66:fbb2da34bd9a | 417 | #elif USE_TCC_TIMEOUT == 2 |
Helmut Tschemernjak | 65:b2d98328fcba | 418 | void TCC2_Handler() |
Helmut Tschemernjak | 65:b2d98328fcba | 419 | #endif |
Helmut Tschemernjak | 65:b2d98328fcba | 420 | { |
Helmut Tschemernjak | 65:b2d98328fcba | 421 | static uint32_t last_usecs = 0; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 422 | Tcc *t = TCC_data[USE_TCC_TIMEOUT].tcc_ptr; |
Helmut Tschemernjak | 65:b2d98328fcba | 423 | uint32_t usecs = micros(); |
Helmut Tschemernjak | 65:b2d98328fcba | 424 | uint32_t u_offset = 0; |
Helmut Tschemernjak | 65:b2d98328fcba | 425 | |
Helmut Tschemernjak | 65:b2d98328fcba | 426 | if (last_usecs && last_usecs < usecs) { |
Helmut Tschemernjak | 65:b2d98328fcba | 427 | /* |
Helmut Tschemernjak | 65:b2d98328fcba | 428 | * Problem is that the micros sometimes gives smaller values |
Helmut Tschemernjak | 65:b2d98328fcba | 429 | * compared to previuos micros. As a workaround we all 1ms. |
Helmut Tschemernjak | 65:b2d98328fcba | 430 | */ |
Helmut Tschemernjak | 65:b2d98328fcba | 431 | u_offset = 1000; |
Helmut Tschemernjak | 65:b2d98328fcba | 432 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 433 | last_usecs = usecs; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 434 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 435 | // Serial.print(getTimerCount(t), DEC); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 436 | // Serial.println(" TimerCount"); |
Helmut Tschemernjak | 46:e78a1d0391ac | 437 | |
Helmut Tschemernjak | 65:b2d98328fcba | 438 | /* |
Helmut Tschemernjak | 65:b2d98328fcba | 439 | * Overflow means the max timer exeeded, we need restart the timer |
Helmut Tschemernjak | 65:b2d98328fcba | 440 | * Interrupts and |
Helmut Tschemernjak | 65:b2d98328fcba | 441 | */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 442 | if (t->INTFLAG.bit.OVF == 1) { // A overflow caused the interrupt |
Helmut Tschemernjak | 66:fbb2da34bd9a | 443 | Serial.print("Timer_OVF\r\n"); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 444 | t->INTFLAG.bit.OVF = 1; // writing a one clears the flag ovf flag |
Helmut Tschemernjak | 65:b2d98328fcba | 445 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 446 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 447 | if (t->INTFLAG.bit.MC0 == 1) { // A compare to cc0 caused the interrupt |
Helmut Tschemernjak | 65:b2d98328fcba | 448 | //Serial.print("MC0\r\n"); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 449 | t->INTFLAG.bit.MC0 = 1; // writing a one clears the MCO (match capture) flag |
Helmut Tschemernjak | 65:b2d98328fcba | 450 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 451 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 452 | t->CTRLA.reg &= ~TCC_CTRLA_ENABLE; // Disable TC |
Helmut Tschemernjak | 66:fbb2da34bd9a | 453 | while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync |
Helmut Tschemernjak | 65:b2d98328fcba | 454 | |
Helmut Tschemernjak | 65:b2d98328fcba | 455 | for (int i = 0; i < MAX_TIMEOUTS-1; i++) { |
Helmut Tschemernjak | 65:b2d98328fcba | 456 | struct TimeoutVector *tvp = &TimeOuts[i]; |
Helmut Tschemernjak | 65:b2d98328fcba | 457 | if (tvp->timer && tvp->timeout && usecs + u_offset >= tvp->timeout) { |
Helmut Tschemernjak | 65:b2d98328fcba | 458 | Timeout *saveTimer = tvp->timer; |
Helmut Tschemernjak | 65:b2d98328fcba | 459 | tvp->timer = NULL; |
Helmut Tschemernjak | 65:b2d98328fcba | 460 | tvp->timeout = 0; |
Helmut Tschemernjak | 65:b2d98328fcba | 461 | Timeout::_irq_handler(saveTimer); |
Helmut Tschemernjak | 65:b2d98328fcba | 462 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 463 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 464 | /* |
Helmut Tschemernjak | 65:b2d98328fcba | 465 | * we need to restart the timer for remaining interrupts |
Helmut Tschemernjak | 65:b2d98328fcba | 466 | * we provide the interrupt entry time in usecs which means |
Helmut Tschemernjak | 65:b2d98328fcba | 467 | * we don't count the irq_hander duration or debug prints |
Helmut Tschemernjak | 65:b2d98328fcba | 468 | */ |
Helmut Tschemernjak | 65:b2d98328fcba | 469 | Timeout::restart(usecs); |
Helmut Tschemernjak | 65:b2d98328fcba | 470 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 471 | |
Helmut Tschemernjak | 65:b2d98328fcba | 472 | #endif // D21 TCC Timer |
Helmut Tschemernjak | 65:b2d98328fcba | 473 | |
Helmut Tschemernjak | 65:b2d98328fcba | 474 | void |
Helmut Tschemernjak | 65:b2d98328fcba | 475 | Timeout::insert(void) |
Helmut Tschemernjak | 65:b2d98328fcba | 476 | { |
Helmut Tschemernjak | 65:b2d98328fcba | 477 | noInterrupts(); |
Helmut Tschemernjak | 65:b2d98328fcba | 478 | for (int i = 0; i < MAX_TIMEOUTS-1; i++) { |
Helmut Tschemernjak | 65:b2d98328fcba | 479 | struct TimeoutVector *tvp = &TimeOuts[i]; |
Helmut Tschemernjak | 65:b2d98328fcba | 480 | if (tvp->timer == NULL) { |
Helmut Tschemernjak | 65:b2d98328fcba | 481 | tvp->timeout = _timeout; |
Helmut Tschemernjak | 65:b2d98328fcba | 482 | tvp->timer = this; |
Helmut Tschemernjak | 65:b2d98328fcba | 483 | break; |
Helmut Tschemernjak | 65:b2d98328fcba | 484 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 485 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 486 | interrupts(); |
Helmut Tschemernjak | 65:b2d98328fcba | 487 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 488 | |
Helmut Tschemernjak | 65:b2d98328fcba | 489 | void |
Helmut Tschemernjak | 65:b2d98328fcba | 490 | Timeout::remove(void) |
Helmut Tschemernjak | 65:b2d98328fcba | 491 | { |
Helmut Tschemernjak | 65:b2d98328fcba | 492 | noInterrupts(); |
Helmut Tschemernjak | 65:b2d98328fcba | 493 | for (int i = 0; i < MAX_TIMEOUTS-1; i++) { |
Helmut Tschemernjak | 65:b2d98328fcba | 494 | struct TimeoutVector *tvp = &TimeOuts[i]; |
Helmut Tschemernjak | 65:b2d98328fcba | 495 | if (tvp->timer == this) { |
Helmut Tschemernjak | 65:b2d98328fcba | 496 | tvp->timer = NULL; |
Helmut Tschemernjak | 65:b2d98328fcba | 497 | tvp->timeout = 0; |
Helmut Tschemernjak | 65:b2d98328fcba | 498 | break; |
Helmut Tschemernjak | 65:b2d98328fcba | 499 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 500 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 501 | interrupts(); |
Helmut Tschemernjak | 65:b2d98328fcba | 502 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 503 | |
Helmut Tschemernjak | 65:b2d98328fcba | 504 | |
Helmut Tschemernjak | 65:b2d98328fcba | 505 | void |
Helmut Tschemernjak | 65:b2d98328fcba | 506 | Timeout::restart(uint32_t usecs) |
Helmut Tschemernjak | 65:b2d98328fcba | 507 | { |
Helmut Tschemernjak | 65:b2d98328fcba | 508 | uint32_t timeout = ~0; |
Helmut Tschemernjak | 65:b2d98328fcba | 509 | |
Helmut Tschemernjak | 65:b2d98328fcba | 510 | /* |
Helmut Tschemernjak | 65:b2d98328fcba | 511 | * find the lowest timeout value which is our the next timeout |
Helmut Tschemernjak | 65:b2d98328fcba | 512 | * zero means stop the timer. |
Helmut Tschemernjak | 65:b2d98328fcba | 513 | */ |
Helmut Tschemernjak | 65:b2d98328fcba | 514 | noInterrupts(); |
Helmut Tschemernjak | 65:b2d98328fcba | 515 | for (int i = 0; i < MAX_TIMEOUTS-1; i++) { |
Helmut Tschemernjak | 65:b2d98328fcba | 516 | struct TimeoutVector *tvp = &TimeOuts[i]; |
Helmut Tschemernjak | 65:b2d98328fcba | 517 | if (tvp->timer && tvp->timeout > 0) { |
Helmut Tschemernjak | 65:b2d98328fcba | 518 | if (tvp->timeout < timeout) { |
Helmut Tschemernjak | 65:b2d98328fcba | 519 | timeout = tvp->timeout; |
Helmut Tschemernjak | 65:b2d98328fcba | 520 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 521 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 522 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 523 | interrupts(); |
Helmut Tschemernjak | 65:b2d98328fcba | 524 | |
Helmut Tschemernjak | 65:b2d98328fcba | 525 | if (timeout == (uint32_t)~0) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 526 | stopTimer(TCC_data[USE_TCC_TIMEOUT].tcc_ptr); |
Helmut Tschemernjak | 65:b2d98328fcba | 527 | return; |
Helmut Tschemernjak | 65:b2d98328fcba | 528 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 529 | if (!usecs) |
Helmut Tschemernjak | 65:b2d98328fcba | 530 | usecs = micros(); |
Helmut Tschemernjak | 65:b2d98328fcba | 531 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 532 | Tcc *t = TCC_data[USE_TCC_TIMEOUT].tcc_ptr; |
Helmut Tschemernjak | 65:b2d98328fcba | 533 | if (timeout > usecs) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 534 | startTimer(t, timeout - usecs); |
Helmut Tschemernjak | 65:b2d98328fcba | 535 | return; |
Helmut Tschemernjak | 65:b2d98328fcba | 536 | } else { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 537 | startTimer(t, 1); // just one usec to trigger interrrupt |
Helmut Tschemernjak | 65:b2d98328fcba | 538 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 539 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 540 | #endif // ARDUINO |