test

Dependents:   Telemetria_RX_SD_GPS_copy Telemetria_RX_SD_GPS Telemetria_TX Telemetria_TX ... more

Committer:
Helmut Tschemernjak
Date:
Fri Jul 21 17:09:05 2017 +0200
Revision:
69:d440a5b04708
Parent:
67:d3afd803f40d
Child:
70:1d496aae2819
Added more support for Arduino D21
Added mode (push/pull) for InterruptIn
Added proper SPI support for Arduino

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Helmut Tschemernjak 46:e78a1d0391ac 1 /*
Helmut Tschemernjak 65:b2d98328fcba 2 * The file is Licensed under the Apache License, Version 2.0
Helmut Tschemernjak 46:e78a1d0391ac 3 * (c) 2017 Helmut Tschemernjak
Helmut Tschemernjak 46:e78a1d0391ac 4 * 30826 Garbsen (Hannover) Germany
Helmut Tschemernjak 46:e78a1d0391ac 5 */
Helmut Tschemernjak 46:e78a1d0391ac 6
Helmut Tschemernjak 65:b2d98328fcba 7 #ifdef ARDUINO
Helmut Tschemernjak 65:b2d98328fcba 8
Helmut Tschemernjak 65:b2d98328fcba 9 using namespace std;
Helmut Tschemernjak 65:b2d98328fcba 10
Helmut Tschemernjak 65:b2d98328fcba 11 #include "arduino-mbed.h"
Helmut Tschemernjak 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 67:d3afd803f40d 273 uint32_t ms_getTicker(void)
Helmut Tschemernjak 67:d3afd803f40d 274 {
Helmut Tschemernjak 67:d3afd803f40d 275 uint32_t us = us_getTicker();
Helmut Tschemernjak 67:d3afd803f40d 276
Helmut Tschemernjak 67:d3afd803f40d 277 us /= 1000; // to ms
Helmut Tschemernjak 67:d3afd803f40d 278 return us;
Helmut Tschemernjak 67:d3afd803f40d 279 }
Helmut Tschemernjak 67:d3afd803f40d 280
Helmut Tschemernjak 67:d3afd803f40d 281 uint32_t us_getTicker(void)
Helmut Tschemernjak 67:d3afd803f40d 282 {
Helmut Tschemernjak 67:d3afd803f40d 283 long long ns = ns_getTicker();
Helmut Tschemernjak 67:d3afd803f40d 284
Helmut Tschemernjak 67:d3afd803f40d 285 ns /= (long long)1000; // to us
Helmut Tschemernjak 67:d3afd803f40d 286 uint32_t us = ns & 0xffffffff;
Helmut Tschemernjak 67:d3afd803f40d 287
Helmut Tschemernjak 67:d3afd803f40d 288 return us;
Helmut Tschemernjak 67:d3afd803f40d 289 }
Helmut Tschemernjak 67:d3afd803f40d 290
Helmut Tschemernjak 67:d3afd803f40d 291
Helmut Tschemernjak 67:d3afd803f40d 292 uint64_t ns_getTicker(void)
Helmut Tschemernjak 66:fbb2da34bd9a 293 {
Helmut Tschemernjak 66:fbb2da34bd9a 294 Tcc *t = TCC_data[USE_TCC_TICKER].tcc_ptr;
Helmut Tschemernjak 66:fbb2da34bd9a 295 if (!initTickerDone) {
Helmut Tschemernjak 66:fbb2da34bd9a 296 initTimer(t);
Helmut Tschemernjak 66:fbb2da34bd9a 297 initTickerDone = true;
Helmut Tschemernjak 66:fbb2da34bd9a 298
Helmut Tschemernjak 67:d3afd803f40d 299 // set counter top to max 16 bit for testing
Helmut Tschemernjak 67:d3afd803f40d 300 // t->PER.bit.PER = 0xffff;
Helmut Tschemernjak 67:d3afd803f40d 301 // while (t->SYNCBUSY.bit.PER == 1); // wait for sync
Helmut Tschemernjak 66:fbb2da34bd9a 302
Helmut Tschemernjak 66:fbb2da34bd9a 303 t->CTRLA.reg |= TCC_CTRLA_ENABLE ; // Enable TC
Helmut Tschemernjak 66:fbb2da34bd9a 304 while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync
Helmut Tschemernjak 67:d3afd803f40d 305 }
Helmut Tschemernjak 67:d3afd803f40d 306
Helmut Tschemernjak 67:d3afd803f40d 307 /*
Helmut Tschemernjak 67:d3afd803f40d 308 * if we are called from the interrupt level, the counter contains
Helmut Tschemernjak 67:d3afd803f40d 309 * somehow wrong data, therfore we needs to read it twice.
Helmut Tschemernjak 67:d3afd803f40d 310 * Another option was to add a little wait (loop 500x)
Helmut Tschemernjak 67:d3afd803f40d 311 * in the TCC_TIMEOUT interrupt handler.
Helmut Tschemernjak 67:d3afd803f40d 312 */
Helmut Tschemernjak 67:d3afd803f40d 313 if (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) // check if we are in the interrupt
Helmut Tschemernjak 67:d3afd803f40d 314 getTimerCount(t);
Helmut Tschemernjak 66:fbb2da34bd9a 315
Helmut Tschemernjak 67:d3afd803f40d 316 uint64_t counter_us = (uint64_t)NS_PER_CLOCK * (uint64_t)getTimerCount(t);
Helmut Tschemernjak 67:d3afd803f40d 317 uint64_t ns = ticker_ns + counter_us;
Helmut Tschemernjak 67:d3afd803f40d 318
Helmut Tschemernjak 67:d3afd803f40d 319 return ns;
Helmut Tschemernjak 66:fbb2da34bd9a 320 }
Helmut Tschemernjak 65:b2d98328fcba 321
Helmut Tschemernjak 66:fbb2da34bd9a 322 #if USE_TCC_TICKER == 0
Helmut Tschemernjak 66:fbb2da34bd9a 323 void TCC0_Handler()
Helmut Tschemernjak 66:fbb2da34bd9a 324 #elif USE_TCC_TICKER == 1
Helmut Tschemernjak 66:fbb2da34bd9a 325 void TCC1_Handler()
Helmut Tschemernjak 66:fbb2da34bd9a 326 #elif USE_TCC_TICKER == 2
Helmut Tschemernjak 66:fbb2da34bd9a 327 void TCC2_Handler()
Helmut Tschemernjak 66:fbb2da34bd9a 328 #endif
Helmut Tschemernjak 66:fbb2da34bd9a 329 {
Helmut Tschemernjak 66:fbb2da34bd9a 330 Tcc *t = TCC_data[USE_TCC_TICKER].tcc_ptr;
Helmut Tschemernjak 66:fbb2da34bd9a 331 /*
Helmut Tschemernjak 67:d3afd803f40d 332 * Overflow means the timer top exeeded
Helmut Tschemernjak 66:fbb2da34bd9a 333 */
Helmut Tschemernjak 66:fbb2da34bd9a 334 if (t->INTFLAG.bit.OVF == 1) { // A overflow caused the interrupt
Helmut Tschemernjak 66:fbb2da34bd9a 335 t->INTFLAG.bit.OVF = 1; // writing a one clears the flag ovf flag
Helmut Tschemernjak 67:d3afd803f40d 336 // Serial.println("T_OVF");
Helmut Tschemernjak 67:d3afd803f40d 337
Helmut Tschemernjak 67:d3afd803f40d 338 /*
Helmut Tschemernjak 67:d3afd803f40d 339 * reading the count once is needed, otherwise
Helmut Tschemernjak 67:d3afd803f40d 340 * it will not wrap correct.
Helmut Tschemernjak 67:d3afd803f40d 341 */
Helmut Tschemernjak 67:d3afd803f40d 342 getTimerCount(t);
Helmut Tschemernjak 66:fbb2da34bd9a 343
Helmut Tschemernjak 66:fbb2da34bd9a 344 int bits = TCC_data[USE_TCC_TICKER].nbits;
Helmut Tschemernjak 66:fbb2da34bd9a 345 int maxCounts = (uint32_t)(1<<bits);
Helmut Tschemernjak 66:fbb2da34bd9a 346
Helmut Tschemernjak 67:d3afd803f40d 347 ticker_ns += (uint64_t)NS_PER_CLOCK * (uint64_t)maxCounts;
Helmut Tschemernjak 66:fbb2da34bd9a 348 }
Helmut Tschemernjak 66:fbb2da34bd9a 349 if (t->INTFLAG.bit.MC0 == 1) { // A compare to cc0 caused the interrupt
Helmut Tschemernjak 66:fbb2da34bd9a 350 t->INTFLAG.bit.MC0 = 1; // writing a one clears the MCO (match capture) flag
Helmut Tschemernjak 67:d3afd803f40d 351 // Serial.println("T_MC0");
Helmut Tschemernjak 66:fbb2da34bd9a 352 }
Helmut Tschemernjak 66:fbb2da34bd9a 353 }
Helmut Tschemernjak 66:fbb2da34bd9a 354
Helmut Tschemernjak 67:d3afd803f40d 355 /* ----------------- SUPPORT CODE FOR TCC TIMERS----------------------*/
Helmut Tschemernjak 65:b2d98328fcba 356
Helmut Tschemernjak 65:b2d98328fcba 357 static bool initTimerDone = false;
Helmut Tschemernjak 65:b2d98328fcba 358
Helmut Tschemernjak 67:d3afd803f40d 359 static void initTimer(Tcc *t)
Helmut Tschemernjak 67:d3afd803f40d 360 {
Helmut Tschemernjak 65:b2d98328fcba 361
Helmut Tschemernjak 67:d3afd803f40d 362 /*
Helmut Tschemernjak 67:d3afd803f40d 363 * enable clock for TCC, see gclk.h
Helmut Tschemernjak 67:d3afd803f40d 364 * GCLK_CLKCTRL_GEN_GCLK0 for 48 Mhz CPU
Helmut Tschemernjak 67:d3afd803f40d 365 * GCLK_CLKCTRL_GEN_GCLK1 for 32k extern crystal XOSC32K (ifdef CRYSTALLESS)
Helmut Tschemernjak 67:d3afd803f40d 366 * GCLK_CLKCTRL_GEN_GCLK1 for 32k internal OSC32K
Helmut Tschemernjak 67:d3afd803f40d 367 * see Arduino: arduino/hardware/samd/1.6.15/cores/arduino/startup.c
Helmut Tschemernjak 67:d3afd803f40d 368 * Use TCC_CTRLA_PRESCALER_DIV1024 for for 48 Mhz clock
Helmut Tschemernjak 67:d3afd803f40d 369 * Use TCC_CTRLA_PRESCALER_DIV2 for 32k clock
Helmut Tschemernjak 67:d3afd803f40d 370 */
Helmut Tschemernjak 66:fbb2da34bd9a 371 if (t == TCC0 || t == TCC1) {
Helmut Tschemernjak 67:d3afd803f40d 372 REG_GCLK_CLKCTRL = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK1 | GCLK_CLKCTRL_ID_TCC0_TCC1);
Helmut Tschemernjak 66:fbb2da34bd9a 373 } else if (t == TCC2) {
Helmut Tschemernjak 67:d3afd803f40d 374 REG_GCLK_CLKCTRL = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK1 | GCLK_CLKCTRL_ID_TCC2_TC3_Val);
Helmut Tschemernjak 46:e78a1d0391ac 375 }
Helmut Tschemernjak 65:b2d98328fcba 376 while (GCLK->STATUS.bit.SYNCBUSY == 1); // wait for sync
Helmut Tschemernjak 65:b2d98328fcba 377
Helmut Tschemernjak 66:fbb2da34bd9a 378 t->CTRLA.reg &= ~TCC_CTRLA_ENABLE; // Disable TCC
Helmut Tschemernjak 66:fbb2da34bd9a 379 while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync
Helmut Tschemernjak 65:b2d98328fcba 380
Helmut Tschemernjak 67:d3afd803f40d 381 t->CTRLA.reg |= (TCC_CTRLA_PRESCALER_DIV2 | TCC_CTRLA_RUNSTDBY); // Set perscaler
Helmut Tschemernjak 65:b2d98328fcba 382
Helmut Tschemernjak 66:fbb2da34bd9a 383 t->WAVE.reg |= TCC_WAVE_WAVEGEN_NFRQ; // Set wave form configuration
Helmut Tschemernjak 66:fbb2da34bd9a 384 while (t->SYNCBUSY.bit.WAVE == 1); // wait for sync
Helmut Tschemernjak 65:b2d98328fcba 385
Helmut Tschemernjak 67:d3afd803f40d 386 t->PER.bit.PER = 0xffffff; // set counter top to max 24 bit
Helmut Tschemernjak 66:fbb2da34bd9a 387 while (t->SYNCBUSY.bit.PER == 1); // wait for sync
Helmut Tschemernjak 65:b2d98328fcba 388
Helmut Tschemernjak 65:b2d98328fcba 389 // the compare counter TC->CC[0].reg will be set in the startTimer
Helmut Tschemernjak 65:b2d98328fcba 390 // after the timeout calculation is known.
Helmut Tschemernjak 65:b2d98328fcba 391
Helmut Tschemernjak 65:b2d98328fcba 392 // Interrupts
Helmut Tschemernjak 66:fbb2da34bd9a 393 t->INTENSET.reg = 0; // disable all interrupts
Helmut Tschemernjak 66:fbb2da34bd9a 394 t->INTENSET.bit.OVF = 1; // enable overfollow
Helmut Tschemernjak 66:fbb2da34bd9a 395 t->INTENSET.bit.MC0 = 1; // enable compare match to CC0
Helmut Tschemernjak 65:b2d98328fcba 396
Helmut Tschemernjak 66:fbb2da34bd9a 397 const struct TCC_config *cp = &TCC_data[0];
Helmut Tschemernjak 66:fbb2da34bd9a 398 while (cp->tcc_ptr) {
Helmut Tschemernjak 66:fbb2da34bd9a 399 if (cp->tcc_ptr == t) {
Helmut Tschemernjak 66:fbb2da34bd9a 400 NVIC_EnableIRQ(cp->tcc_irq); // Enable InterruptVector
Helmut Tschemernjak 66:fbb2da34bd9a 401 break;
Helmut Tschemernjak 66:fbb2da34bd9a 402 }
Helmut Tschemernjak 66:fbb2da34bd9a 403 cp++;
Helmut Tschemernjak 66:fbb2da34bd9a 404 }
Helmut Tschemernjak 46:e78a1d0391ac 405 }
Helmut Tschemernjak 46:e78a1d0391ac 406
Helmut Tschemernjak 67:d3afd803f40d 407 #if 0
Helmut Tschemernjak 67:d3afd803f40d 408 // Atmel ASF Code
Helmut Tschemernjak 67:d3afd803f40d 409 static uint32_t getTimerCount(Tcc *t)
Helmut Tschemernjak 67:d3afd803f40d 410 {
Helmut Tschemernjak 66:fbb2da34bd9a 411 uint32_t last_cmd;
Helmut Tschemernjak 66:fbb2da34bd9a 412 /* Wait last command done */
Helmut Tschemernjak 66:fbb2da34bd9a 413 do {
Helmut Tschemernjak 66:fbb2da34bd9a 414 while (t->SYNCBUSY.bit.CTRLB); /* Wait for sync */
Helmut Tschemernjak 66:fbb2da34bd9a 415
Helmut Tschemernjak 66:fbb2da34bd9a 416 last_cmd = t->CTRLBSET.reg & TCC_CTRLBSET_CMD_Msk;
Helmut Tschemernjak 66:fbb2da34bd9a 417 if (TCC_CTRLBSET_CMD_NONE == last_cmd) {
Helmut Tschemernjak 66:fbb2da34bd9a 418 /* Issue read command and break */
Helmut Tschemernjak 66:fbb2da34bd9a 419 t->CTRLBSET.bit.CMD = TCC_CTRLBSET_CMD_READSYNC_Val;
Helmut Tschemernjak 66:fbb2da34bd9a 420 break;
Helmut Tschemernjak 66:fbb2da34bd9a 421 } else if (TCC_CTRLBSET_CMD_READSYNC == last_cmd) {
Helmut Tschemernjak 66:fbb2da34bd9a 422 /* Command have been issued */
Helmut Tschemernjak 66:fbb2da34bd9a 423 break;
Helmut Tschemernjak 66:fbb2da34bd9a 424 }
Helmut Tschemernjak 66:fbb2da34bd9a 425 } while (1);
Helmut Tschemernjak 65:b2d98328fcba 426
Helmut Tschemernjak 66:fbb2da34bd9a 427 while (t->SYNCBUSY.bit.COUNT); /* Wait for sync */
Helmut Tschemernjak 66:fbb2da34bd9a 428
Helmut Tschemernjak 66:fbb2da34bd9a 429 return t->COUNT.reg;
Helmut Tschemernjak 46:e78a1d0391ac 430 }
Helmut Tschemernjak 67:d3afd803f40d 431 #endif
Helmut Tschemernjak 67:d3afd803f40d 432
Helmut Tschemernjak 67:d3afd803f40d 433
Helmut Tschemernjak 67:d3afd803f40d 434 static uint32_t getTimerCount(Tcc *t)
Helmut Tschemernjak 67:d3afd803f40d 435 {
Helmut Tschemernjak 67:d3afd803f40d 436
Helmut Tschemernjak 67:d3afd803f40d 437 noInterrupts();
Helmut Tschemernjak 67:d3afd803f40d 438
Helmut Tschemernjak 67:d3afd803f40d 439 while (t->SYNCBUSY.bit.CTRLB); /* Wait for sync */
Helmut Tschemernjak 67:d3afd803f40d 440
Helmut Tschemernjak 67:d3afd803f40d 441 t->CTRLBSET.bit.CMD = TCC_CTRLBSET_CMD_READSYNC_Val; /* Issue read command and break */
Helmut Tschemernjak 67:d3afd803f40d 442
Helmut Tschemernjak 67:d3afd803f40d 443 while (t->SYNCBUSY.bit.COUNT); /* Wait for sync */
Helmut Tschemernjak 67:d3afd803f40d 444
Helmut Tschemernjak 67:d3afd803f40d 445 uint32_t count = t->COUNT.reg;
Helmut Tschemernjak 67:d3afd803f40d 446
Helmut Tschemernjak 67:d3afd803f40d 447 interrupts();
Helmut Tschemernjak 67:d3afd803f40d 448
Helmut Tschemernjak 67:d3afd803f40d 449 return count;
Helmut Tschemernjak 67:d3afd803f40d 450 }
Helmut Tschemernjak 67:d3afd803f40d 451
Helmut Tschemernjak 46:e78a1d0391ac 452
Helmut Tschemernjak 66:fbb2da34bd9a 453 static void stopTimer(Tcc *t)
Helmut Tschemernjak 46:e78a1d0391ac 454 {
Helmut Tschemernjak 66:fbb2da34bd9a 455 t->CTRLA.reg &= ~TCC_CTRLA_ENABLE; // Disable TC
Helmut Tschemernjak 66:fbb2da34bd9a 456 while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync
Helmut Tschemernjak 66:fbb2da34bd9a 457 }
Helmut Tschemernjak 66:fbb2da34bd9a 458
Helmut Tschemernjak 67:d3afd803f40d 459
Helmut Tschemernjak 67:d3afd803f40d 460 /* ----------------- TIMEOUT TIMER CODE ----------------------*/
Helmut Tschemernjak 67:d3afd803f40d 461
Helmut Tschemernjak 67:d3afd803f40d 462 static void startTimer(Tcc *t, uint64_t delay_ns)
Helmut Tschemernjak 66:fbb2da34bd9a 463 {
Helmut Tschemernjak 67:d3afd803f40d 464 if (!initTimerDone) {
Helmut Tschemernjak 66:fbb2da34bd9a 465 initTimer(t); // initial setup with stopped timer
Helmut Tschemernjak 67:d3afd803f40d 466 initTimerDone = true;
Helmut Tschemernjak 67:d3afd803f40d 467 }
Helmut Tschemernjak 65:b2d98328fcba 468
Helmut Tschemernjak 66:fbb2da34bd9a 469 stopTimer(t); // avoid timer interrupts while calculating
Helmut Tschemernjak 46:e78a1d0391ac 470
Helmut Tschemernjak 65:b2d98328fcba 471 /*
Helmut Tschemernjak 65:b2d98328fcba 472 * every 21333 ns equals one tick (1/(48000000/1024))
Helmut Tschemernjak 65:b2d98328fcba 473 * COUNT*DIVIDER*SECS until interrupt
Helmut Tschemernjak 65:b2d98328fcba 474 * 48 Mhz = (65536*1024)/1.398636s
Helmut Tschemernjak 65:b2d98328fcba 475 */
Helmut Tschemernjak 67:d3afd803f40d 476 uint64_t nclocks = (uint64_t)delay_ns;
Helmut Tschemernjak 67:d3afd803f40d 477 nclocks /= (uint64_t)NS_PER_CLOCK;
Helmut Tschemernjak 65:b2d98328fcba 478 int nCounts = nclocks;
Helmut Tschemernjak 65:b2d98328fcba 479
Helmut Tschemernjak 66:fbb2da34bd9a 480 int bits = TCC_data[USE_TCC_TIMEOUT].nbits;
Helmut Tschemernjak 65:b2d98328fcba 481 int maxCounts = (uint32_t)(1<<bits)-1;
Helmut Tschemernjak 46:e78a1d0391ac 482
Helmut Tschemernjak 65:b2d98328fcba 483 if (nCounts > maxCounts) // if count exceeds timer capacity
Helmut Tschemernjak 65:b2d98328fcba 484 nCounts = maxCounts; // set the largest posible count.
Helmut Tschemernjak 66:fbb2da34bd9a 485 if (nCounts <= 0)
Helmut Tschemernjak 65:b2d98328fcba 486 nCounts = 1;
Helmut Tschemernjak 66:fbb2da34bd9a 487 t->CC[0].bit.CC = nCounts;
Helmut Tschemernjak 66:fbb2da34bd9a 488 while (t->SYNCBUSY.bit.CC0 == 1); // wait for sync
Helmut Tschemernjak 46:e78a1d0391ac 489
Helmut Tschemernjak 66:fbb2da34bd9a 490 t->CTRLA.reg |= TCC_CTRLA_ENABLE ; // Enable TC
Helmut Tschemernjak 66:fbb2da34bd9a 491 while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync
Helmut Tschemernjak 67:d3afd803f40d 492 #if 0
Helmut Tschemernjak 67:d3afd803f40d 493 Serial.print(ms_getTicker(), DEC);
Helmut Tschemernjak 65:b2d98328fcba 494 Serial.print(" startTimer: nCounts=");
Helmut Tschemernjak 65:b2d98328fcba 495 Serial.println(nCounts, DEC);
Helmut Tschemernjak 65:b2d98328fcba 496 #endif
Helmut Tschemernjak 46:e78a1d0391ac 497 }
Helmut Tschemernjak 46:e78a1d0391ac 498
Helmut Tschemernjak 46:e78a1d0391ac 499
Helmut Tschemernjak 66:fbb2da34bd9a 500 #if USE_TCC_TIMEOUT == 0
Helmut Tschemernjak 65:b2d98328fcba 501 void TCC0_Handler()
Helmut Tschemernjak 66:fbb2da34bd9a 502 #elif USE_TCC_TIMEOUT == 1
Helmut Tschemernjak 65:b2d98328fcba 503 void TCC1_Handler()
Helmut Tschemernjak 66:fbb2da34bd9a 504 #elif USE_TCC_TIMEOUT == 2
Helmut Tschemernjak 65:b2d98328fcba 505 void TCC2_Handler()
Helmut Tschemernjak 65:b2d98328fcba 506 #endif
Helmut Tschemernjak 65:b2d98328fcba 507 {
Helmut Tschemernjak 66:fbb2da34bd9a 508 Tcc *t = TCC_data[USE_TCC_TIMEOUT].tcc_ptr;
Helmut Tschemernjak 67:d3afd803f40d 509 uint64_t nsecs = ns_getTicker();
Helmut Tschemernjak 65:b2d98328fcba 510
Helmut Tschemernjak 65:b2d98328fcba 511 /*
Helmut Tschemernjak 65:b2d98328fcba 512 * Overflow means the max timer exeeded, we need restart the timer
Helmut Tschemernjak 65:b2d98328fcba 513 * Interrupts and
Helmut Tschemernjak 65:b2d98328fcba 514 */
Helmut Tschemernjak 66:fbb2da34bd9a 515 if (t->INTFLAG.bit.OVF == 1) { // A overflow caused the interrupt
Helmut Tschemernjak 66:fbb2da34bd9a 516 t->INTFLAG.bit.OVF = 1; // writing a one clears the flag ovf flag
Helmut Tschemernjak 65:b2d98328fcba 517 }
Helmut Tschemernjak 65:b2d98328fcba 518
Helmut Tschemernjak 66:fbb2da34bd9a 519 if (t->INTFLAG.bit.MC0 == 1) { // A compare to cc0 caused the interrupt
Helmut Tschemernjak 65:b2d98328fcba 520 //Serial.print("MC0\r\n");
Helmut Tschemernjak 66:fbb2da34bd9a 521 t->INTFLAG.bit.MC0 = 1; // writing a one clears the MCO (match capture) flag
Helmut Tschemernjak 65:b2d98328fcba 522 }
Helmut Tschemernjak 65:b2d98328fcba 523
Helmut Tschemernjak 66:fbb2da34bd9a 524 t->CTRLA.reg &= ~TCC_CTRLA_ENABLE; // Disable TC
Helmut Tschemernjak 66:fbb2da34bd9a 525 while (t->SYNCBUSY.bit.ENABLE == 1); // wait for sync
Helmut Tschemernjak 65:b2d98328fcba 526
Helmut Tschemernjak 65:b2d98328fcba 527 for (int i = 0; i < MAX_TIMEOUTS-1; i++) {
Helmut Tschemernjak 65:b2d98328fcba 528 struct TimeoutVector *tvp = &TimeOuts[i];
Helmut Tschemernjak 67:d3afd803f40d 529 if (tvp->timer && nsecs >= tvp->timer->_timeout) {
Helmut Tschemernjak 65:b2d98328fcba 530 Timeout *saveTimer = tvp->timer;
Helmut Tschemernjak 65:b2d98328fcba 531 tvp->timer = NULL;
Helmut Tschemernjak 65:b2d98328fcba 532 Timeout::_irq_handler(saveTimer);
Helmut Tschemernjak 65:b2d98328fcba 533 }
Helmut Tschemernjak 65:b2d98328fcba 534 }
Helmut Tschemernjak 65:b2d98328fcba 535 /*
Helmut Tschemernjak 65:b2d98328fcba 536 * we need to restart the timer for remaining interrupts
Helmut Tschemernjak 67:d3afd803f40d 537 * Another reason is that we stopped this counter, in case there are
Helmut Tschemernjak 67:d3afd803f40d 538 * remaining counts, we need to re-schedule the counter.
Helmut Tschemernjak 65:b2d98328fcba 539 */
Helmut Tschemernjak 67:d3afd803f40d 540 Timeout::restart();
Helmut Tschemernjak 65:b2d98328fcba 541 }
Helmut Tschemernjak 65:b2d98328fcba 542
Helmut Tschemernjak 67:d3afd803f40d 543
Helmut Tschemernjak 65:b2d98328fcba 544 #endif // D21 TCC Timer
Helmut Tschemernjak 65:b2d98328fcba 545
Helmut Tschemernjak 65:b2d98328fcba 546 void
Helmut Tschemernjak 65:b2d98328fcba 547 Timeout::insert(void)
Helmut Tschemernjak 65:b2d98328fcba 548 {
Helmut Tschemernjak 65:b2d98328fcba 549 noInterrupts();
Helmut Tschemernjak 65:b2d98328fcba 550 for (int i = 0; i < MAX_TIMEOUTS-1; i++) {
Helmut Tschemernjak 65:b2d98328fcba 551 struct TimeoutVector *tvp = &TimeOuts[i];
Helmut Tschemernjak 65:b2d98328fcba 552 if (tvp->timer == NULL) {
Helmut Tschemernjak 65:b2d98328fcba 553 tvp->timer = this;
Helmut Tschemernjak 65:b2d98328fcba 554 break;
Helmut Tschemernjak 65:b2d98328fcba 555 }
Helmut Tschemernjak 65:b2d98328fcba 556 }
Helmut Tschemernjak 65:b2d98328fcba 557 interrupts();
Helmut Tschemernjak 65:b2d98328fcba 558 }
Helmut Tschemernjak 65:b2d98328fcba 559
Helmut Tschemernjak 65:b2d98328fcba 560 void
Helmut Tschemernjak 65:b2d98328fcba 561 Timeout::remove(void)
Helmut Tschemernjak 65:b2d98328fcba 562 {
Helmut Tschemernjak 65:b2d98328fcba 563 noInterrupts();
Helmut Tschemernjak 65:b2d98328fcba 564 for (int i = 0; i < MAX_TIMEOUTS-1; i++) {
Helmut Tschemernjak 65:b2d98328fcba 565 struct TimeoutVector *tvp = &TimeOuts[i];
Helmut Tschemernjak 65:b2d98328fcba 566 if (tvp->timer == this) {
Helmut Tschemernjak 65:b2d98328fcba 567 tvp->timer = NULL;
Helmut Tschemernjak 65:b2d98328fcba 568 break;
Helmut Tschemernjak 65:b2d98328fcba 569 }
Helmut Tschemernjak 65:b2d98328fcba 570 }
Helmut Tschemernjak 65:b2d98328fcba 571 interrupts();
Helmut Tschemernjak 65:b2d98328fcba 572 }
Helmut Tschemernjak 65:b2d98328fcba 573
Helmut Tschemernjak 65:b2d98328fcba 574
Helmut Tschemernjak 65:b2d98328fcba 575 void
Helmut Tschemernjak 67:d3afd803f40d 576 Timeout::restart()
Helmut Tschemernjak 65:b2d98328fcba 577 {
Helmut Tschemernjak 67:d3afd803f40d 578 Tcc *t = TCC_data[USE_TCC_TIMEOUT].tcc_ptr;
Helmut Tschemernjak 67:d3afd803f40d 579 uint64_t timeout = ~0;
Helmut Tschemernjak 65:b2d98328fcba 580
Helmut Tschemernjak 65:b2d98328fcba 581 /*
Helmut Tschemernjak 65:b2d98328fcba 582 * find the lowest timeout value which is our the next timeout
Helmut Tschemernjak 65:b2d98328fcba 583 * zero means stop the timer.
Helmut Tschemernjak 65:b2d98328fcba 584 */
Helmut Tschemernjak 65:b2d98328fcba 585 noInterrupts();
Helmut Tschemernjak 65:b2d98328fcba 586 for (int i = 0; i < MAX_TIMEOUTS-1; i++) {
Helmut Tschemernjak 65:b2d98328fcba 587 struct TimeoutVector *tvp = &TimeOuts[i];
Helmut Tschemernjak 67:d3afd803f40d 588 if (tvp->timer) {
Helmut Tschemernjak 67:d3afd803f40d 589 if (tvp->timer->_timeout < timeout) {
Helmut Tschemernjak 67:d3afd803f40d 590 timeout = tvp->timer->_timeout;
Helmut Tschemernjak 65:b2d98328fcba 591 }
Helmut Tschemernjak 65:b2d98328fcba 592 }
Helmut Tschemernjak 65:b2d98328fcba 593 }
Helmut Tschemernjak 65:b2d98328fcba 594 interrupts();
Helmut Tschemernjak 65:b2d98328fcba 595
Helmut Tschemernjak 67:d3afd803f40d 596 if (timeout == (uint64_t)~0) {
Helmut Tschemernjak 67:d3afd803f40d 597 stopTimer(t);
Helmut Tschemernjak 65:b2d98328fcba 598 return;
Helmut Tschemernjak 65:b2d98328fcba 599 }
Helmut Tschemernjak 67:d3afd803f40d 600
Helmut Tschemernjak 67:d3afd803f40d 601 uint64_t nsecs = ns_getTicker();
Helmut Tschemernjak 65:b2d98328fcba 602
Helmut Tschemernjak 67:d3afd803f40d 603 if (timeout > nsecs) {
Helmut Tschemernjak 67:d3afd803f40d 604 startTimer(t, (uint64_t)timeout - (uint64_t)nsecs);
Helmut Tschemernjak 65:b2d98328fcba 605 return;
Helmut Tschemernjak 65:b2d98328fcba 606 } else {
Helmut Tschemernjak 67:d3afd803f40d 607 startTimer(t, (uint64_t)1); // just one nsec to trigger interrrupt
Helmut Tschemernjak 65:b2d98328fcba 608 }
Helmut Tschemernjak 65:b2d98328fcba 609 }
Helmut Tschemernjak 67:d3afd803f40d 610
Helmut Tschemernjak 67:d3afd803f40d 611 /* ----------------- D21 sleep() and deepsleep() code ----------------------*/
Helmut Tschemernjak 67:d3afd803f40d 612
Helmut Tschemernjak 67:d3afd803f40d 613 void sleep(void)
Helmut Tschemernjak 67:d3afd803f40d 614 {
Helmut Tschemernjak 67:d3afd803f40d 615 #if 1 // (SAMD20 || SAMD21)
Helmut Tschemernjak 67:d3afd803f40d 616 /* Errata: Make sure that the Flash does not power all the way down
Helmut Tschemernjak 67:d3afd803f40d 617 * when in sleep mode. */
Helmut Tschemernjak 67:d3afd803f40d 618 NVMCTRL->CTRLB.bit.SLEEPPRM = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val;
Helmut Tschemernjak 67:d3afd803f40d 619 #endif
Helmut Tschemernjak 67:d3afd803f40d 620
Helmut Tschemernjak 67:d3afd803f40d 621 SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk; // clear deep sleep
Helmut Tschemernjak 67:d3afd803f40d 622 PM->SLEEP.reg = 2; // SYSTEM_SLEEPMODE_IDLE_2 IDLE 2 sleep mode.
Helmut Tschemernjak 67:d3afd803f40d 623
Helmut Tschemernjak 67:d3afd803f40d 624 __DSB(); // ensures the completion of memory accesses
Helmut Tschemernjak 67:d3afd803f40d 625 __WFI(); // wait for interrupt
Helmut Tschemernjak 67:d3afd803f40d 626 }
Helmut Tschemernjak 67:d3afd803f40d 627
Helmut Tschemernjak 67:d3afd803f40d 628 void deepsleep(void)
Helmut Tschemernjak 67:d3afd803f40d 629 {
Helmut Tschemernjak 67:d3afd803f40d 630 #if 1 // (SAMD20 || SAMD21)
Helmut Tschemernjak 67:d3afd803f40d 631 /* Errata: Make sure that the Flash does not power all the way down
Helmut Tschemernjak 67:d3afd803f40d 632 * when in sleep mode. */
Helmut Tschemernjak 67:d3afd803f40d 633 NVMCTRL->CTRLB.bit.SLEEPPRM = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val;
Helmut Tschemernjak 67:d3afd803f40d 634 #endif
Helmut Tschemernjak 67:d3afd803f40d 635
Helmut Tschemernjak 67:d3afd803f40d 636 SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; // standby mode
Helmut Tschemernjak 67:d3afd803f40d 637
Helmut Tschemernjak 67:d3afd803f40d 638 __DSB(); // ensures the completion of memory accesses
Helmut Tschemernjak 67:d3afd803f40d 639 __WFI(); // wait for interrupt
Helmut Tschemernjak 67:d3afd803f40d 640 }
Helmut Tschemernjak 67:d3afd803f40d 641
Helmut Tschemernjak 67:d3afd803f40d 642
Helmut Tschemernjak 46:e78a1d0391ac 643 #endif // ARDUINO