test
Dependents: Telemetria_RX_SD_GPS_copy Telemetria_RX_SD_GPS Telemetria_TX Telemetria_TX ... more
Arduino-mbed-APIs/arduino-mbed.cpp@71:7067e67902a8, 2017-07-23 (annotated)
- Committer:
- Helmut Tschemernjak
- Date:
- Sun Jul 23 16:09:55 2017 +0200
- Revision:
- 71:7067e67902a8
- Parent:
- sx1276/arduino-mbed.cpp@70:1d496aae2819
- Child:
- 75:7330dd86cdea
Renamed Arduino files to Arduino-mbed-APIs
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 | 69:d440a5b04708 | 182 | attachInterrupt(MYdigitalPinToInterrupt(_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 | 69:d440a5b04708 | 195 | attachInterrupt(MYdigitalPinToInterrupt(_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 | } TimeOuts[MAX_TIMEOUTS]; |
Helmut Tschemernjak | 65:b2d98328fcba | 209 | |
Helmut Tschemernjak | 65:b2d98328fcba | 210 | |
Helmut Tschemernjak | 65:b2d98328fcba | 211 | #if defined(__SAMD21G18A__) || defined(__SAMD21J18A__) |
Helmut Tschemernjak | 65:b2d98328fcba | 212 | /* |
Helmut Tschemernjak | 65:b2d98328fcba | 213 | * __SAMD21J18A__ is the SamD21 Explained Board |
Helmut Tschemernjak | 65:b2d98328fcba | 214 | * __SAMD21G18A__ is Genuino Zero-Board (compatible with the LoRa board) |
Helmut Tschemernjak | 65:b2d98328fcba | 215 | */ |
Helmut Tschemernjak | 65:b2d98328fcba | 216 | |
Helmut Tschemernjak | 65:b2d98328fcba | 217 | /* |
Helmut Tschemernjak | 67:d3afd803f40d | 218 | * see tcc.h is automatically included from: |
Helmut Tschemernjak | 65:b2d98328fcba | 219 | * Arduino15/packages/arduino/tools/CMSIS-Atmel/1.1.0/CMSIS/ |
Helmut Tschemernjak | 65:b2d98328fcba | 220 | * Device/ATMEL/samd21/include/component/tcc.h |
Helmut Tschemernjak | 66:fbb2da34bd9a | 221 | * See also tcc.c (ASF/mbed, e.g. Tcc_get_count_value) |
Helmut Tschemernjak | 65:b2d98328fcba | 222 | */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 223 | static void initTimer(Tcc *t); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 224 | static uint32_t getTimerCount(Tcc *t); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 225 | |
Helmut Tschemernjak | 67:d3afd803f40d | 226 | /* |
Helmut Tschemernjak | 67:d3afd803f40d | 227 | * The Atmel D21 has three TCC timer, other models have more. |
Helmut Tschemernjak | 67:d3afd803f40d | 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 | 67:d3afd803f40d | 239 | |
Helmut Tschemernjak | 67:d3afd803f40d | 240 | /* |
Helmut Tschemernjak | 67:d3afd803f40d | 241 | * We preferably use the TCC timers because it supports 24-bit counters |
Helmut Tschemernjak | 67:d3afd803f40d | 242 | * versus TC Timer which supports only 8 or 16 bit counters |
Helmut Tschemernjak | 67:d3afd803f40d | 243 | * TCC0/1/2 timer work on the D21 using Arduino. |
Helmut Tschemernjak | 67:d3afd803f40d | 244 | */ |
Helmut Tschemernjak | 67:d3afd803f40d | 245 | #define USE_TCC_TIMEOUT 0 // 0=TCC0, 1=TTC1, 2=TTC2 (see TCC_data) |
Helmut Tschemernjak | 66:fbb2da34bd9a | 246 | #define USE_TCC_TICKER 1 |
Helmut Tschemernjak | 66:fbb2da34bd9a | 247 | |
Helmut Tschemernjak | 67:d3afd803f40d | 248 | |
Helmut Tschemernjak | 67:d3afd803f40d | 249 | /* |
Helmut Tschemernjak | 67:d3afd803f40d | 250 | * every 21333 ns equals one tick (1/(48000000/1024)) // prescaler 1024, 48 MHz |
Helmut Tschemernjak | 67:d3afd803f40d | 251 | * every 61035 ns equals one tick (1/(32768/2)) // prescaler 2, 32 kHz |
Helmut Tschemernjak | 67:d3afd803f40d | 252 | * COUNT*DIVIDER*SECS until interrupt |
Helmut Tschemernjak | 67:d3afd803f40d | 253 | * CPU 48 MHz = (65536*1024)/1.398636s |
Helmut Tschemernjak | 67:d3afd803f40d | 254 | * RTC 32 kHz = (65536*2)/4.0s |
Helmut Tschemernjak | 67:d3afd803f40d | 255 | */ |
Helmut Tschemernjak | 67:d3afd803f40d | 256 | #define NS_PER_CLOCK_CPU 21333 // ns secs per clock |
Helmut Tschemernjak | 67:d3afd803f40d | 257 | #define NS_PER_CLOCK_RTC 61035 // ns secs per clock |
Helmut Tschemernjak | 67:d3afd803f40d | 258 | |
Helmut Tschemernjak | 67:d3afd803f40d | 259 | #define NS_PER_CLOCK NS_PER_CLOCK_RTC |
Helmut Tschemernjak | 66:fbb2da34bd9a | 260 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 261 | /* ----------------- TICKER TIMER CODE ----------------------*/ |
Helmut Tschemernjak | 67:d3afd803f40d | 262 | |
Helmut Tschemernjak | 67:d3afd803f40d | 263 | /* |
Helmut Tschemernjak | 67:d3afd803f40d | 264 | * The global ns_counter contains the time in ns from the last time |
Helmut Tschemernjak | 67:d3afd803f40d | 265 | * the counter has been wrapped. It cannot be used directly because the |
Helmut Tschemernjak | 67:d3afd803f40d | 266 | * current counter has to be added fore using it. Use instead |
Helmut Tschemernjak | 67:d3afd803f40d | 267 | * ns_getTicker(), us_ ns_getTicker(), ms_getTicker() |
Helmut Tschemernjak | 67:d3afd803f40d | 268 | */ |
Helmut Tschemernjak | 67:d3afd803f40d | 269 | |
Helmut Tschemernjak | 67:d3afd803f40d | 270 | uint64_t ticker_ns; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 271 | static bool initTickerDone = false; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 272 | |
Helmut Tschemernjak | 70:1d496aae2819 | 273 | uint32_t s_getTicker(void) |
Helmut Tschemernjak | 70:1d496aae2819 | 274 | { |
Helmut Tschemernjak | 70:1d496aae2819 | 275 | long long ns = ns_getTicker(); |
Helmut Tschemernjak | 70:1d496aae2819 | 276 | ns /= (long long)1000000000; // to secs |
Helmut Tschemernjak | 70:1d496aae2819 | 277 | |
Helmut Tschemernjak | 70:1d496aae2819 | 278 | int secs = ns; |
Helmut Tschemernjak | 70:1d496aae2819 | 279 | return secs; |
Helmut Tschemernjak | 70:1d496aae2819 | 280 | } |
Helmut Tschemernjak | 70:1d496aae2819 | 281 | |
Helmut Tschemernjak | 70:1d496aae2819 | 282 | |
Helmut Tschemernjak | 67:d3afd803f40d | 283 | uint32_t ms_getTicker(void) |
Helmut Tschemernjak | 67:d3afd803f40d | 284 | { |
Helmut Tschemernjak | 67:d3afd803f40d | 285 | uint32_t us = us_getTicker(); |
Helmut Tschemernjak | 67:d3afd803f40d | 286 | |
Helmut Tschemernjak | 67:d3afd803f40d | 287 | us /= 1000; // to ms |
Helmut Tschemernjak | 67:d3afd803f40d | 288 | return us; |
Helmut Tschemernjak | 67:d3afd803f40d | 289 | } |
Helmut Tschemernjak | 67:d3afd803f40d | 290 | |
Helmut Tschemernjak | 67:d3afd803f40d | 291 | uint32_t us_getTicker(void) |
Helmut Tschemernjak | 67:d3afd803f40d | 292 | { |
Helmut Tschemernjak | 67:d3afd803f40d | 293 | long long ns = ns_getTicker(); |
Helmut Tschemernjak | 67:d3afd803f40d | 294 | |
Helmut Tschemernjak | 67:d3afd803f40d | 295 | ns /= (long long)1000; // to us |
Helmut Tschemernjak | 67:d3afd803f40d | 296 | uint32_t us = ns & 0xffffffff; |
Helmut Tschemernjak | 67:d3afd803f40d | 297 | |
Helmut Tschemernjak | 67:d3afd803f40d | 298 | return us; |
Helmut Tschemernjak | 67:d3afd803f40d | 299 | } |
Helmut Tschemernjak | 67:d3afd803f40d | 300 | |
Helmut Tschemernjak | 67:d3afd803f40d | 301 | |
Helmut Tschemernjak | 67:d3afd803f40d | 302 | uint64_t ns_getTicker(void) |
Helmut Tschemernjak | 66:fbb2da34bd9a | 303 | { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 304 | Tcc *t = TCC_data[USE_TCC_TICKER].tcc_ptr; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 305 | if (!initTickerDone) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 306 | initTimer(t); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 307 | initTickerDone = true; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 308 | |
Helmut Tschemernjak | 67:d3afd803f40d | 309 | // set counter top to max 16 bit for testing |
Helmut Tschemernjak | 67:d3afd803f40d | 310 | // t->PER.bit.PER = 0xffff; |
Helmut Tschemernjak | 67:d3afd803f40d | 311 | // while (t->SYNCBUSY.bit.PER == 1); // wait for sync |
Helmut Tschemernjak | 66:fbb2da34bd9a | 312 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 313 | t->CTRLA.reg |= TCC_CTRLA_ENABLE ; // Enable TC |
Helmut Tschemernjak | 66:fbb2da34bd9a | 314 | while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync |
Helmut Tschemernjak | 67:d3afd803f40d | 315 | } |
Helmut Tschemernjak | 67:d3afd803f40d | 316 | |
Helmut Tschemernjak | 67:d3afd803f40d | 317 | /* |
Helmut Tschemernjak | 67:d3afd803f40d | 318 | * if we are called from the interrupt level, the counter contains |
Helmut Tschemernjak | 67:d3afd803f40d | 319 | * somehow wrong data, therfore we needs to read it twice. |
Helmut Tschemernjak | 67:d3afd803f40d | 320 | * Another option was to add a little wait (loop 500x) |
Helmut Tschemernjak | 67:d3afd803f40d | 321 | * in the TCC_TIMEOUT interrupt handler. |
Helmut Tschemernjak | 67:d3afd803f40d | 322 | */ |
Helmut Tschemernjak | 67:d3afd803f40d | 323 | if (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) // check if we are in the interrupt |
Helmut Tschemernjak | 67:d3afd803f40d | 324 | getTimerCount(t); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 325 | |
Helmut Tschemernjak | 67:d3afd803f40d | 326 | uint64_t counter_us = (uint64_t)NS_PER_CLOCK * (uint64_t)getTimerCount(t); |
Helmut Tschemernjak | 67:d3afd803f40d | 327 | uint64_t ns = ticker_ns + counter_us; |
Helmut Tschemernjak | 67:d3afd803f40d | 328 | |
Helmut Tschemernjak | 67:d3afd803f40d | 329 | return ns; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 330 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 331 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 332 | #if USE_TCC_TICKER == 0 |
Helmut Tschemernjak | 66:fbb2da34bd9a | 333 | void TCC0_Handler() |
Helmut Tschemernjak | 66:fbb2da34bd9a | 334 | #elif USE_TCC_TICKER == 1 |
Helmut Tschemernjak | 66:fbb2da34bd9a | 335 | void TCC1_Handler() |
Helmut Tschemernjak | 66:fbb2da34bd9a | 336 | #elif USE_TCC_TICKER == 2 |
Helmut Tschemernjak | 66:fbb2da34bd9a | 337 | void TCC2_Handler() |
Helmut Tschemernjak | 66:fbb2da34bd9a | 338 | #endif |
Helmut Tschemernjak | 66:fbb2da34bd9a | 339 | { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 340 | Tcc *t = TCC_data[USE_TCC_TICKER].tcc_ptr; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 341 | /* |
Helmut Tschemernjak | 67:d3afd803f40d | 342 | * Overflow means the timer top exeeded |
Helmut Tschemernjak | 66:fbb2da34bd9a | 343 | */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 344 | if (t->INTFLAG.bit.OVF == 1) { // A overflow caused the interrupt |
Helmut Tschemernjak | 66:fbb2da34bd9a | 345 | t->INTFLAG.bit.OVF = 1; // writing a one clears the flag ovf flag |
Helmut Tschemernjak | 67:d3afd803f40d | 346 | // Serial.println("T_OVF"); |
Helmut Tschemernjak | 67:d3afd803f40d | 347 | |
Helmut Tschemernjak | 67:d3afd803f40d | 348 | /* |
Helmut Tschemernjak | 67:d3afd803f40d | 349 | * reading the count once is needed, otherwise |
Helmut Tschemernjak | 67:d3afd803f40d | 350 | * it will not wrap correct. |
Helmut Tschemernjak | 67:d3afd803f40d | 351 | */ |
Helmut Tschemernjak | 67:d3afd803f40d | 352 | getTimerCount(t); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 353 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 354 | int bits = TCC_data[USE_TCC_TICKER].nbits; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 355 | int maxCounts = (uint32_t)(1<<bits); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 356 | |
Helmut Tschemernjak | 67:d3afd803f40d | 357 | ticker_ns += (uint64_t)NS_PER_CLOCK * (uint64_t)maxCounts; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 358 | } |
Helmut Tschemernjak | 66:fbb2da34bd9a | 359 | if (t->INTFLAG.bit.MC0 == 1) { // A compare to cc0 caused the interrupt |
Helmut Tschemernjak | 66:fbb2da34bd9a | 360 | t->INTFLAG.bit.MC0 = 1; // writing a one clears the MCO (match capture) flag |
Helmut Tschemernjak | 67:d3afd803f40d | 361 | // Serial.println("T_MC0"); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 362 | } |
Helmut Tschemernjak | 66:fbb2da34bd9a | 363 | } |
Helmut Tschemernjak | 66:fbb2da34bd9a | 364 | |
Helmut Tschemernjak | 67:d3afd803f40d | 365 | /* ----------------- SUPPORT CODE FOR TCC TIMERS----------------------*/ |
Helmut Tschemernjak | 65:b2d98328fcba | 366 | |
Helmut Tschemernjak | 65:b2d98328fcba | 367 | static bool initTimerDone = false; |
Helmut Tschemernjak | 65:b2d98328fcba | 368 | |
Helmut Tschemernjak | 67:d3afd803f40d | 369 | static void initTimer(Tcc *t) |
Helmut Tschemernjak | 67:d3afd803f40d | 370 | { |
Helmut Tschemernjak | 65:b2d98328fcba | 371 | |
Helmut Tschemernjak | 67:d3afd803f40d | 372 | /* |
Helmut Tschemernjak | 67:d3afd803f40d | 373 | * enable clock for TCC, see gclk.h |
Helmut Tschemernjak | 67:d3afd803f40d | 374 | * GCLK_CLKCTRL_GEN_GCLK0 for 48 Mhz CPU |
Helmut Tschemernjak | 67:d3afd803f40d | 375 | * GCLK_CLKCTRL_GEN_GCLK1 for 32k extern crystal XOSC32K (ifdef CRYSTALLESS) |
Helmut Tschemernjak | 67:d3afd803f40d | 376 | * GCLK_CLKCTRL_GEN_GCLK1 for 32k internal OSC32K |
Helmut Tschemernjak | 67:d3afd803f40d | 377 | * see Arduino: arduino/hardware/samd/1.6.15/cores/arduino/startup.c |
Helmut Tschemernjak | 67:d3afd803f40d | 378 | * Use TCC_CTRLA_PRESCALER_DIV1024 for for 48 Mhz clock |
Helmut Tschemernjak | 67:d3afd803f40d | 379 | * Use TCC_CTRLA_PRESCALER_DIV2 for 32k clock |
Helmut Tschemernjak | 67:d3afd803f40d | 380 | */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 381 | if (t == TCC0 || t == TCC1) { |
Helmut Tschemernjak | 67:d3afd803f40d | 382 | REG_GCLK_CLKCTRL = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK1 | GCLK_CLKCTRL_ID_TCC0_TCC1); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 383 | } else if (t == TCC2) { |
Helmut Tschemernjak | 67:d3afd803f40d | 384 | REG_GCLK_CLKCTRL = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK1 | GCLK_CLKCTRL_ID_TCC2_TC3_Val); |
Helmut Tschemernjak | 46:e78a1d0391ac | 385 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 386 | while (GCLK->STATUS.bit.SYNCBUSY == 1); // wait for sync |
Helmut Tschemernjak | 65:b2d98328fcba | 387 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 388 | t->CTRLA.reg &= ~TCC_CTRLA_ENABLE; // Disable TCC |
Helmut Tschemernjak | 66:fbb2da34bd9a | 389 | while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync |
Helmut Tschemernjak | 65:b2d98328fcba | 390 | |
Helmut Tschemernjak | 67:d3afd803f40d | 391 | t->CTRLA.reg |= (TCC_CTRLA_PRESCALER_DIV2 | TCC_CTRLA_RUNSTDBY); // Set perscaler |
Helmut Tschemernjak | 65:b2d98328fcba | 392 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 393 | t->WAVE.reg |= TCC_WAVE_WAVEGEN_NFRQ; // Set wave form configuration |
Helmut Tschemernjak | 66:fbb2da34bd9a | 394 | while (t->SYNCBUSY.bit.WAVE == 1); // wait for sync |
Helmut Tschemernjak | 65:b2d98328fcba | 395 | |
Helmut Tschemernjak | 67:d3afd803f40d | 396 | t->PER.bit.PER = 0xffffff; // set counter top to max 24 bit |
Helmut Tschemernjak | 66:fbb2da34bd9a | 397 | while (t->SYNCBUSY.bit.PER == 1); // wait for sync |
Helmut Tschemernjak | 65:b2d98328fcba | 398 | |
Helmut Tschemernjak | 65:b2d98328fcba | 399 | // the compare counter TC->CC[0].reg will be set in the startTimer |
Helmut Tschemernjak | 65:b2d98328fcba | 400 | // after the timeout calculation is known. |
Helmut Tschemernjak | 65:b2d98328fcba | 401 | |
Helmut Tschemernjak | 65:b2d98328fcba | 402 | // Interrupts |
Helmut Tschemernjak | 66:fbb2da34bd9a | 403 | t->INTENSET.reg = 0; // disable all interrupts |
Helmut Tschemernjak | 66:fbb2da34bd9a | 404 | t->INTENSET.bit.OVF = 1; // enable overfollow |
Helmut Tschemernjak | 66:fbb2da34bd9a | 405 | t->INTENSET.bit.MC0 = 1; // enable compare match to CC0 |
Helmut Tschemernjak | 65:b2d98328fcba | 406 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 407 | const struct TCC_config *cp = &TCC_data[0]; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 408 | while (cp->tcc_ptr) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 409 | if (cp->tcc_ptr == t) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 410 | NVIC_EnableIRQ(cp->tcc_irq); // Enable InterruptVector |
Helmut Tschemernjak | 66:fbb2da34bd9a | 411 | break; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 412 | } |
Helmut Tschemernjak | 66:fbb2da34bd9a | 413 | cp++; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 414 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 415 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 416 | |
Helmut Tschemernjak | 67:d3afd803f40d | 417 | #if 0 |
Helmut Tschemernjak | 67:d3afd803f40d | 418 | // Atmel ASF Code |
Helmut Tschemernjak | 67:d3afd803f40d | 419 | static uint32_t getTimerCount(Tcc *t) |
Helmut Tschemernjak | 67:d3afd803f40d | 420 | { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 421 | uint32_t last_cmd; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 422 | /* Wait last command done */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 423 | do { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 424 | while (t->SYNCBUSY.bit.CTRLB); /* Wait for sync */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 425 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 426 | last_cmd = t->CTRLBSET.reg & TCC_CTRLBSET_CMD_Msk; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 427 | if (TCC_CTRLBSET_CMD_NONE == last_cmd) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 428 | /* Issue read command and break */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 429 | t->CTRLBSET.bit.CMD = TCC_CTRLBSET_CMD_READSYNC_Val; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 430 | break; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 431 | } else if (TCC_CTRLBSET_CMD_READSYNC == last_cmd) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 432 | /* Command have been issued */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 433 | break; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 434 | } |
Helmut Tschemernjak | 66:fbb2da34bd9a | 435 | } while (1); |
Helmut Tschemernjak | 65:b2d98328fcba | 436 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 437 | while (t->SYNCBUSY.bit.COUNT); /* Wait for sync */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 438 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 439 | return t->COUNT.reg; |
Helmut Tschemernjak | 46:e78a1d0391ac | 440 | } |
Helmut Tschemernjak | 67:d3afd803f40d | 441 | #endif |
Helmut Tschemernjak | 67:d3afd803f40d | 442 | |
Helmut Tschemernjak | 67:d3afd803f40d | 443 | |
Helmut Tschemernjak | 67:d3afd803f40d | 444 | static uint32_t getTimerCount(Tcc *t) |
Helmut Tschemernjak | 67:d3afd803f40d | 445 | { |
Helmut Tschemernjak | 67:d3afd803f40d | 446 | |
Helmut Tschemernjak | 67:d3afd803f40d | 447 | noInterrupts(); |
Helmut Tschemernjak | 67:d3afd803f40d | 448 | |
Helmut Tschemernjak | 67:d3afd803f40d | 449 | while (t->SYNCBUSY.bit.CTRLB); /* Wait for sync */ |
Helmut Tschemernjak | 67:d3afd803f40d | 450 | |
Helmut Tschemernjak | 67:d3afd803f40d | 451 | t->CTRLBSET.bit.CMD = TCC_CTRLBSET_CMD_READSYNC_Val; /* Issue read command and break */ |
Helmut Tschemernjak | 67:d3afd803f40d | 452 | |
Helmut Tschemernjak | 67:d3afd803f40d | 453 | while (t->SYNCBUSY.bit.COUNT); /* Wait for sync */ |
Helmut Tschemernjak | 67:d3afd803f40d | 454 | |
Helmut Tschemernjak | 67:d3afd803f40d | 455 | uint32_t count = t->COUNT.reg; |
Helmut Tschemernjak | 67:d3afd803f40d | 456 | |
Helmut Tschemernjak | 67:d3afd803f40d | 457 | interrupts(); |
Helmut Tschemernjak | 67:d3afd803f40d | 458 | |
Helmut Tschemernjak | 67:d3afd803f40d | 459 | return count; |
Helmut Tschemernjak | 67:d3afd803f40d | 460 | } |
Helmut Tschemernjak | 67:d3afd803f40d | 461 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 462 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 463 | static void stopTimer(Tcc *t) |
Helmut Tschemernjak | 46:e78a1d0391ac | 464 | { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 465 | t->CTRLA.reg &= ~TCC_CTRLA_ENABLE; // Disable TC |
Helmut Tschemernjak | 66:fbb2da34bd9a | 466 | while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync |
Helmut Tschemernjak | 66:fbb2da34bd9a | 467 | } |
Helmut Tschemernjak | 66:fbb2da34bd9a | 468 | |
Helmut Tschemernjak | 67:d3afd803f40d | 469 | |
Helmut Tschemernjak | 67:d3afd803f40d | 470 | /* ----------------- TIMEOUT TIMER CODE ----------------------*/ |
Helmut Tschemernjak | 67:d3afd803f40d | 471 | |
Helmut Tschemernjak | 67:d3afd803f40d | 472 | static void startTimer(Tcc *t, uint64_t delay_ns) |
Helmut Tschemernjak | 66:fbb2da34bd9a | 473 | { |
Helmut Tschemernjak | 67:d3afd803f40d | 474 | if (!initTimerDone) { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 475 | initTimer(t); // initial setup with stopped timer |
Helmut Tschemernjak | 67:d3afd803f40d | 476 | initTimerDone = true; |
Helmut Tschemernjak | 67:d3afd803f40d | 477 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 478 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 479 | stopTimer(t); // avoid timer interrupts while calculating |
Helmut Tschemernjak | 46:e78a1d0391ac | 480 | |
Helmut Tschemernjak | 65:b2d98328fcba | 481 | /* |
Helmut Tschemernjak | 65:b2d98328fcba | 482 | * every 21333 ns equals one tick (1/(48000000/1024)) |
Helmut Tschemernjak | 65:b2d98328fcba | 483 | * COUNT*DIVIDER*SECS until interrupt |
Helmut Tschemernjak | 65:b2d98328fcba | 484 | * 48 Mhz = (65536*1024)/1.398636s |
Helmut Tschemernjak | 65:b2d98328fcba | 485 | */ |
Helmut Tschemernjak | 67:d3afd803f40d | 486 | uint64_t nclocks = (uint64_t)delay_ns; |
Helmut Tschemernjak | 67:d3afd803f40d | 487 | nclocks /= (uint64_t)NS_PER_CLOCK; |
Helmut Tschemernjak | 65:b2d98328fcba | 488 | int nCounts = nclocks; |
Helmut Tschemernjak | 65:b2d98328fcba | 489 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 490 | int bits = TCC_data[USE_TCC_TIMEOUT].nbits; |
Helmut Tschemernjak | 65:b2d98328fcba | 491 | int maxCounts = (uint32_t)(1<<bits)-1; |
Helmut Tschemernjak | 46:e78a1d0391ac | 492 | |
Helmut Tschemernjak | 65:b2d98328fcba | 493 | if (nCounts > maxCounts) // if count exceeds timer capacity |
Helmut Tschemernjak | 65:b2d98328fcba | 494 | nCounts = maxCounts; // set the largest posible count. |
Helmut Tschemernjak | 66:fbb2da34bd9a | 495 | if (nCounts <= 0) |
Helmut Tschemernjak | 65:b2d98328fcba | 496 | nCounts = 1; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 497 | t->CC[0].bit.CC = nCounts; |
Helmut Tschemernjak | 66:fbb2da34bd9a | 498 | while (t->SYNCBUSY.bit.CC0 == 1); // wait for sync |
Helmut Tschemernjak | 46:e78a1d0391ac | 499 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 500 | t->CTRLA.reg |= TCC_CTRLA_ENABLE ; // Enable TC |
Helmut Tschemernjak | 66:fbb2da34bd9a | 501 | while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync |
Helmut Tschemernjak | 67:d3afd803f40d | 502 | #if 0 |
Helmut Tschemernjak | 67:d3afd803f40d | 503 | Serial.print(ms_getTicker(), DEC); |
Helmut Tschemernjak | 65:b2d98328fcba | 504 | Serial.print(" startTimer: nCounts="); |
Helmut Tschemernjak | 65:b2d98328fcba | 505 | Serial.println(nCounts, DEC); |
Helmut Tschemernjak | 65:b2d98328fcba | 506 | #endif |
Helmut Tschemernjak | 46:e78a1d0391ac | 507 | } |
Helmut Tschemernjak | 46:e78a1d0391ac | 508 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 509 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 510 | #if USE_TCC_TIMEOUT == 0 |
Helmut Tschemernjak | 65:b2d98328fcba | 511 | void TCC0_Handler() |
Helmut Tschemernjak | 66:fbb2da34bd9a | 512 | #elif USE_TCC_TIMEOUT == 1 |
Helmut Tschemernjak | 65:b2d98328fcba | 513 | void TCC1_Handler() |
Helmut Tschemernjak | 66:fbb2da34bd9a | 514 | #elif USE_TCC_TIMEOUT == 2 |
Helmut Tschemernjak | 65:b2d98328fcba | 515 | void TCC2_Handler() |
Helmut Tschemernjak | 65:b2d98328fcba | 516 | #endif |
Helmut Tschemernjak | 65:b2d98328fcba | 517 | { |
Helmut Tschemernjak | 66:fbb2da34bd9a | 518 | Tcc *t = TCC_data[USE_TCC_TIMEOUT].tcc_ptr; |
Helmut Tschemernjak | 67:d3afd803f40d | 519 | uint64_t nsecs = ns_getTicker(); |
Helmut Tschemernjak | 65:b2d98328fcba | 520 | |
Helmut Tschemernjak | 65:b2d98328fcba | 521 | /* |
Helmut Tschemernjak | 65:b2d98328fcba | 522 | * Overflow means the max timer exeeded, we need restart the timer |
Helmut Tschemernjak | 65:b2d98328fcba | 523 | * Interrupts and |
Helmut Tschemernjak | 65:b2d98328fcba | 524 | */ |
Helmut Tschemernjak | 66:fbb2da34bd9a | 525 | if (t->INTFLAG.bit.OVF == 1) { // A overflow caused the interrupt |
Helmut Tschemernjak | 66:fbb2da34bd9a | 526 | t->INTFLAG.bit.OVF = 1; // writing a one clears the flag ovf flag |
Helmut Tschemernjak | 65:b2d98328fcba | 527 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 528 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 529 | if (t->INTFLAG.bit.MC0 == 1) { // A compare to cc0 caused the interrupt |
Helmut Tschemernjak | 65:b2d98328fcba | 530 | //Serial.print("MC0\r\n"); |
Helmut Tschemernjak | 66:fbb2da34bd9a | 531 | t->INTFLAG.bit.MC0 = 1; // writing a one clears the MCO (match capture) flag |
Helmut Tschemernjak | 65:b2d98328fcba | 532 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 533 | |
Helmut Tschemernjak | 66:fbb2da34bd9a | 534 | t->CTRLA.reg &= ~TCC_CTRLA_ENABLE; // Disable TC |
Helmut Tschemernjak | 66:fbb2da34bd9a | 535 | while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync |
Helmut Tschemernjak | 65:b2d98328fcba | 536 | |
Helmut Tschemernjak | 65:b2d98328fcba | 537 | for (int i = 0; i < MAX_TIMEOUTS-1; i++) { |
Helmut Tschemernjak | 65:b2d98328fcba | 538 | struct TimeoutVector *tvp = &TimeOuts[i]; |
Helmut Tschemernjak | 67:d3afd803f40d | 539 | if (tvp->timer && nsecs >= tvp->timer->_timeout) { |
Helmut Tschemernjak | 65:b2d98328fcba | 540 | Timeout *saveTimer = tvp->timer; |
Helmut Tschemernjak | 65:b2d98328fcba | 541 | tvp->timer = NULL; |
Helmut Tschemernjak | 65:b2d98328fcba | 542 | Timeout::_irq_handler(saveTimer); |
Helmut Tschemernjak | 65:b2d98328fcba | 543 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 544 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 545 | /* |
Helmut Tschemernjak | 65:b2d98328fcba | 546 | * we need to restart the timer for remaining interrupts |
Helmut Tschemernjak | 67:d3afd803f40d | 547 | * Another reason is that we stopped this counter, in case there are |
Helmut Tschemernjak | 67:d3afd803f40d | 548 | * remaining counts, we need to re-schedule the counter. |
Helmut Tschemernjak | 65:b2d98328fcba | 549 | */ |
Helmut Tschemernjak | 67:d3afd803f40d | 550 | Timeout::restart(); |
Helmut Tschemernjak | 65:b2d98328fcba | 551 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 552 | |
Helmut Tschemernjak | 67:d3afd803f40d | 553 | |
Helmut Tschemernjak | 65:b2d98328fcba | 554 | #endif // D21 TCC Timer |
Helmut Tschemernjak | 65:b2d98328fcba | 555 | |
Helmut Tschemernjak | 65:b2d98328fcba | 556 | void |
Helmut Tschemernjak | 65:b2d98328fcba | 557 | Timeout::insert(void) |
Helmut Tschemernjak | 65:b2d98328fcba | 558 | { |
Helmut Tschemernjak | 65:b2d98328fcba | 559 | noInterrupts(); |
Helmut Tschemernjak | 65:b2d98328fcba | 560 | for (int i = 0; i < MAX_TIMEOUTS-1; i++) { |
Helmut Tschemernjak | 65:b2d98328fcba | 561 | struct TimeoutVector *tvp = &TimeOuts[i]; |
Helmut Tschemernjak | 65:b2d98328fcba | 562 | if (tvp->timer == NULL) { |
Helmut Tschemernjak | 65:b2d98328fcba | 563 | tvp->timer = this; |
Helmut Tschemernjak | 65:b2d98328fcba | 564 | break; |
Helmut Tschemernjak | 65:b2d98328fcba | 565 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 566 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 567 | interrupts(); |
Helmut Tschemernjak | 65:b2d98328fcba | 568 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 569 | |
Helmut Tschemernjak | 65:b2d98328fcba | 570 | void |
Helmut Tschemernjak | 65:b2d98328fcba | 571 | Timeout::remove(void) |
Helmut Tschemernjak | 65:b2d98328fcba | 572 | { |
Helmut Tschemernjak | 65:b2d98328fcba | 573 | noInterrupts(); |
Helmut Tschemernjak | 65:b2d98328fcba | 574 | for (int i = 0; i < MAX_TIMEOUTS-1; i++) { |
Helmut Tschemernjak | 65:b2d98328fcba | 575 | struct TimeoutVector *tvp = &TimeOuts[i]; |
Helmut Tschemernjak | 65:b2d98328fcba | 576 | if (tvp->timer == this) { |
Helmut Tschemernjak | 65:b2d98328fcba | 577 | tvp->timer = NULL; |
Helmut Tschemernjak | 65:b2d98328fcba | 578 | break; |
Helmut Tschemernjak | 65:b2d98328fcba | 579 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 580 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 581 | interrupts(); |
Helmut Tschemernjak | 65:b2d98328fcba | 582 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 583 | |
Helmut Tschemernjak | 65:b2d98328fcba | 584 | |
Helmut Tschemernjak | 65:b2d98328fcba | 585 | void |
Helmut Tschemernjak | 67:d3afd803f40d | 586 | Timeout::restart() |
Helmut Tschemernjak | 65:b2d98328fcba | 587 | { |
Helmut Tschemernjak | 67:d3afd803f40d | 588 | Tcc *t = TCC_data[USE_TCC_TIMEOUT].tcc_ptr; |
Helmut Tschemernjak | 67:d3afd803f40d | 589 | uint64_t timeout = ~0; |
Helmut Tschemernjak | 65:b2d98328fcba | 590 | |
Helmut Tschemernjak | 65:b2d98328fcba | 591 | /* |
Helmut Tschemernjak | 65:b2d98328fcba | 592 | * find the lowest timeout value which is our the next timeout |
Helmut Tschemernjak | 65:b2d98328fcba | 593 | * zero means stop the timer. |
Helmut Tschemernjak | 65:b2d98328fcba | 594 | */ |
Helmut Tschemernjak | 65:b2d98328fcba | 595 | noInterrupts(); |
Helmut Tschemernjak | 65:b2d98328fcba | 596 | for (int i = 0; i < MAX_TIMEOUTS-1; i++) { |
Helmut Tschemernjak | 65:b2d98328fcba | 597 | struct TimeoutVector *tvp = &TimeOuts[i]; |
Helmut Tschemernjak | 67:d3afd803f40d | 598 | if (tvp->timer) { |
Helmut Tschemernjak | 67:d3afd803f40d | 599 | if (tvp->timer->_timeout < timeout) { |
Helmut Tschemernjak | 67:d3afd803f40d | 600 | timeout = tvp->timer->_timeout; |
Helmut Tschemernjak | 65:b2d98328fcba | 601 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 602 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 603 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 604 | interrupts(); |
Helmut Tschemernjak | 65:b2d98328fcba | 605 | |
Helmut Tschemernjak | 67:d3afd803f40d | 606 | if (timeout == (uint64_t)~0) { |
Helmut Tschemernjak | 67:d3afd803f40d | 607 | stopTimer(t); |
Helmut Tschemernjak | 65:b2d98328fcba | 608 | return; |
Helmut Tschemernjak | 65:b2d98328fcba | 609 | } |
Helmut Tschemernjak | 67:d3afd803f40d | 610 | |
Helmut Tschemernjak | 67:d3afd803f40d | 611 | uint64_t nsecs = ns_getTicker(); |
Helmut Tschemernjak | 65:b2d98328fcba | 612 | |
Helmut Tschemernjak | 67:d3afd803f40d | 613 | if (timeout > nsecs) { |
Helmut Tschemernjak | 67:d3afd803f40d | 614 | startTimer(t, (uint64_t)timeout - (uint64_t)nsecs); |
Helmut Tschemernjak | 65:b2d98328fcba | 615 | return; |
Helmut Tschemernjak | 65:b2d98328fcba | 616 | } else { |
Helmut Tschemernjak | 67:d3afd803f40d | 617 | startTimer(t, (uint64_t)1); // just one nsec to trigger interrrupt |
Helmut Tschemernjak | 65:b2d98328fcba | 618 | } |
Helmut Tschemernjak | 65:b2d98328fcba | 619 | } |
Helmut Tschemernjak | 67:d3afd803f40d | 620 | |
Helmut Tschemernjak | 67:d3afd803f40d | 621 | /* ----------------- D21 sleep() and deepsleep() code ----------------------*/ |
Helmut Tschemernjak | 67:d3afd803f40d | 622 | |
Helmut Tschemernjak | 67:d3afd803f40d | 623 | void sleep(void) |
Helmut Tschemernjak | 67:d3afd803f40d | 624 | { |
Helmut Tschemernjak | 67:d3afd803f40d | 625 | #if 1 // (SAMD20 || SAMD21) |
Helmut Tschemernjak | 67:d3afd803f40d | 626 | /* Errata: Make sure that the Flash does not power all the way down |
Helmut Tschemernjak | 67:d3afd803f40d | 627 | * when in sleep mode. */ |
Helmut Tschemernjak | 67:d3afd803f40d | 628 | NVMCTRL->CTRLB.bit.SLEEPPRM = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val; |
Helmut Tschemernjak | 67:d3afd803f40d | 629 | #endif |
Helmut Tschemernjak | 70:1d496aae2819 | 630 | uint32_t saved_ms = ms_getTicker(); |
Helmut Tschemernjak | 70:1d496aae2819 | 631 | SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; // disbale SysTick |
Helmut Tschemernjak | 70:1d496aae2819 | 632 | |
Helmut Tschemernjak | 67:d3afd803f40d | 633 | SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk; // clear deep sleep |
Helmut Tschemernjak | 67:d3afd803f40d | 634 | PM->SLEEP.reg = 2; // SYSTEM_SLEEPMODE_IDLE_2 IDLE 2 sleep mode. |
Helmut Tschemernjak | 67:d3afd803f40d | 635 | |
Helmut Tschemernjak | 67:d3afd803f40d | 636 | __DSB(); // ensures the completion of memory accesses |
Helmut Tschemernjak | 67:d3afd803f40d | 637 | __WFI(); // wait for interrupt |
Helmut Tschemernjak | 70:1d496aae2819 | 638 | |
Helmut Tschemernjak | 70:1d496aae2819 | 639 | int count = ms_getTicker() - saved_ms; |
Helmut Tschemernjak | 70:1d496aae2819 | 640 | if (count > 0) { // update the Arduino Systicks |
Helmut Tschemernjak | 70:1d496aae2819 | 641 | for (int i = 0; i < count; i++) { |
Helmut Tschemernjak | 70:1d496aae2819 | 642 | SysTick_Handler(); |
Helmut Tschemernjak | 70:1d496aae2819 | 643 | } |
Helmut Tschemernjak | 70:1d496aae2819 | 644 | } |
Helmut Tschemernjak | 70:1d496aae2819 | 645 | SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // enable SysTick |
Helmut Tschemernjak | 67:d3afd803f40d | 646 | } |
Helmut Tschemernjak | 67:d3afd803f40d | 647 | |
Helmut Tschemernjak | 67:d3afd803f40d | 648 | void deepsleep(void) |
Helmut Tschemernjak | 67:d3afd803f40d | 649 | { |
Helmut Tschemernjak | 67:d3afd803f40d | 650 | #if 1 // (SAMD20 || SAMD21) |
Helmut Tschemernjak | 67:d3afd803f40d | 651 | /* Errata: Make sure that the Flash does not power all the way down |
Helmut Tschemernjak | 67:d3afd803f40d | 652 | * when in sleep mode. */ |
Helmut Tschemernjak | 67:d3afd803f40d | 653 | NVMCTRL->CTRLB.bit.SLEEPPRM = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val; |
Helmut Tschemernjak | 67:d3afd803f40d | 654 | #endif |
Helmut Tschemernjak | 67:d3afd803f40d | 655 | |
Helmut Tschemernjak | 70:1d496aae2819 | 656 | SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; // disbale SysTick |
Helmut Tschemernjak | 70:1d496aae2819 | 657 | |
Helmut Tschemernjak | 67:d3afd803f40d | 658 | SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; // standby mode |
Helmut Tschemernjak | 70:1d496aae2819 | 659 | |
Helmut Tschemernjak | 67:d3afd803f40d | 660 | __DSB(); // ensures the completion of memory accesses |
Helmut Tschemernjak | 67:d3afd803f40d | 661 | __WFI(); // wait for interrupt |
Helmut Tschemernjak | 70:1d496aae2819 | 662 | |
Helmut Tschemernjak | 70:1d496aae2819 | 663 | SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // enable SysTick |
Helmut Tschemernjak | 67:d3afd803f40d | 664 | } |
Helmut Tschemernjak | 67:d3afd803f40d | 665 | |
Helmut Tschemernjak | 67:d3afd803f40d | 666 | |
Helmut Tschemernjak | 46:e78a1d0391ac | 667 | #endif // ARDUINO |