| [3] | 1 | /* IRLibSAMD21.h |
|---|
| 2 | * Part of IRLib Library for Arduino receiving, decoding, and sending |
|---|
| 3 | * infrared signals. See COPYRIGHT.txt and LICENSE.txt for more information. |
|---|
| 4 | * |
|---|
| 5 | * This type of content is normally stored in IRLibHardware.h but we have |
|---|
| 6 | * moved at her because the SAMD21 support is significantly different than the |
|---|
| 7 | * AVR 8-bit hardware support. Separating it out into a separate file |
|---|
| 8 | * will make it easier to include comments and to maintain the code. |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | /* This file provides hardware support for the SAMD21G18A processor or as we |
|---|
| 12 | * will refer to it simply as SAMD 21. This processor is used in the Arduino Zero, |
|---|
| 13 | * Arduino M0, Arduino M0 Pro, and the Adafruit Feather M0 series boards as well as |
|---|
| 14 | * the upcoming Adafruit Circuit Playground Express. |
|---|
| 15 | * Most of the code has been adapted from messages on the Arduino.cc support forum |
|---|
| 16 | * and from the code at: |
|---|
| 17 | * https://github.com/manitou48/ZERO/tree/master/IRtest |
|---|
| 18 | * Receiving is supported through all three receiver classes. As you would expect |
|---|
| 19 | * the IRrecvLoop class can be used with any available input pin. The IRrecvPCI and |
|---|
| 20 | * IRfrequency classes can be used on any pin that supports "attachInterrupt()". |
|---|
| 21 | * Specifically that means everything except pin 4 on the Arduino Zero. And everything |
|---|
| 22 | * except pin 2 on the boards from Arduino.org such as the Arduino M0, M0 Pro, and |
|---|
| 23 | * Zero Pro. For details on supported pins see these links: |
|---|
| 24 | * https://www.arduino.cc/en/Reference/AttachInterrupt |
|---|
| 25 | * http://www.arduino.org/learning/reference/AttachInterrupt |
|---|
| 26 | * Note that although we support Arduino.org hardware, you should only use the |
|---|
| 27 | * Arduino.cc IDE for compiling your sketches. |
|---|
| 28 | * Both sending and receiving use GCLK0 even though GCLK0-GCLK3 are generally reserved |
|---|
| 29 | * the Arduino infrastructure. However we are using the default 48 MHz clock source |
|---|
| 30 | * and a divisor of "1" so we aren't changing any of the GCLK0 set up. It's therefore |
|---|
| 31 | * safe to use. |
|---|
| 32 | * The IRrecv receiver class using a 50 microsecond timer interrupt is also supported. |
|---|
| 33 | * It defaults to using hardware timer TC3 however TC4 and TC5 can also be used in case |
|---|
| 34 | * of conflicts with other libraries. The IRrecv and IRrecvLoop classes should be able to use |
|---|
| 35 | * any available input pin. Note that all of our example sketches used pin 2 for |
|---|
| 36 | * receiver and pin 3 for frequency measurement. However pin 2 is not available for PCI |
|---|
| 37 | * interrupts on the Arduino.org platforms and neither 2 nor 3 are available on the |
|---|
| 38 | * Adafruit Feather M0 platforms. We are recommending using 5 and 6 for receiving and |
|---|
| 39 | * frequency measurement respectively. |
|---|
| 40 | * For sending, you can use any pin that supports PWM on your particular |
|---|
| 41 | * platform. On Arduino Zero that means pins 3, 4, 5, 6, 8, 9, 10, 11, 12, or 13. |
|---|
| 42 | * Additionally Arduino M0 Pro adds pins 2 and 7 to that list. In all cases the default |
|---|
| 43 | * output pin is 9. The code automatically selects TCC0 or TCC1 based on pin number |
|---|
| 44 | * as needed. It has been tested at frequencies of 36, 38, 39, 40 and 57 which |
|---|
| 45 | * are the frequencies of our supported protocols. |
|---|
| 46 | * As of this release the code has been tested on Arduino Zero, Arduino M0 Pro, and |
|---|
| 47 | * Adafruit Feather M0 BLE version. |
|---|
| 48 | */ |
|---|
| 49 | #ifndef IRLibSAMD21_h |
|---|
| 50 | #define IRLibSAMD21_h |
|---|
| 51 | /* |
|---|
| 52 | * This section contains user changeable values. You can probably stick with the defaults |
|---|
| 53 | * but if there are hardware conflicts with other libraries, you can change these values. |
|---|
| 54 | */ |
|---|
| 55 | //Choose PWM output pin number from among the following |
|---|
| 56 | //Un-comment only one of these. |
|---|
| 57 | //#define IR_SEND_PWM_PIN 2 |
|---|
| 58 | //#define IR_SEND_PWM_PIN 3 |
|---|
| 59 | //#define IR_SEND_PWM_PIN 4 |
|---|
| 60 | //#define IR_SEND_PWM_PIN 5 |
|---|
| 61 | //#define IR_SEND_PWM_PIN 6 |
|---|
| 62 | //#define IR_SEND_PWM_PIN 7 |
|---|
| 63 | //#define IR_SEND_PWM_PIN 8 |
|---|
| 64 | //#define IR_SEND_PWM_PIN 9 |
|---|
| 65 | //#define IR_SEND_PWM_PIN 10 |
|---|
| 66 | //#define IR_SEND_PWM_PIN 11 |
|---|
| 67 | #define IR_SEND_PWM_PIN 12 |
|---|
| 68 | //#define IR_SEND_PWM_PIN 13 |
|---|
| 69 | //Override default for Adafruit Circuit Playground Express |
|---|
| 70 | #ifdef ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS |
|---|
| 71 | #define IR_SEND_PWM_PIN 25 |
|---|
| 72 | #endif |
|---|
| 73 | //Choose which timer counter to use for the 50 microsecond interrupt |
|---|
| 74 | //Un-comment only one of these. |
|---|
| 75 | #define IR_TCn 3 |
|---|
| 76 | //#define IR_TCn 4 |
|---|
| 77 | //#define IR_TCn 5 |
|---|
| 78 | |
|---|
| 79 | /* |
|---|
| 80 | * Everything below this point should not be changed. It computes needed defines |
|---|
| 81 | * based on the user set values above. |
|---|
| 82 | */ |
|---|
| 83 | |
|---|
| 84 | // Saves us a lot of typing when synchronizing |
|---|
| 85 | #define syncTC while (IR_TCx->COUNT16.STATUS.bit.SYNCBUSY) |
|---|
| 86 | #define syncGCLK while (GCLK->STATUS.bit.SYNCBUSY) |
|---|
| 87 | |
|---|
| 88 | #if defined(ARDUINO_SAMD_FEATHER_M0) |
|---|
| 89 | #if ( (IR_SEND_PWM_PIN<5) || (IR_SEND_PWM_PIN==7) || (IR_SEND_PWM_PIN==8) ) |
|---|
| 90 | #error "Pin unsupported on Adafruit Feather M0" |
|---|
| 91 | #endif |
|---|
| 92 | #endif |
|---|
| 93 | |
|---|
| 94 | //Pins 8 and 9 use TCC1 and all others use TCC0 |
|---|
| 95 | #if ( (IR_SEND_PWM_PIN==8) || (IR_SEND_PWM_PIN==9) ) |
|---|
| 96 | #define IR_TCCx TCC1 |
|---|
| 97 | #else |
|---|
| 98 | #define IR_TCCx TCC0 |
|---|
| 99 | #endif |
|---|
| 100 | |
|---|
| 101 | //Although there are 8 WO there are only 4 CC |
|---|
| 102 | #define CC4 CC0 |
|---|
| 103 | #define CC5 CC1 |
|---|
| 104 | #define CC6 CC2 |
|---|
| 105 | #define CC7 CC3 |
|---|
| 106 | |
|---|
| 107 | //Set other values based on pin number |
|---|
| 108 | #if (IR_SEND_PWM_PIN==2) |
|---|
| 109 | //NOTE: Arduino M0 Pro only |
|---|
| 110 | #if defined(ARDUINO_SAM_ZERO) |
|---|
| 111 | //PA08 E:TCC0-WO[0] F:TCC1-WO[2] |
|---|
| 112 | #define IR_MUX_EF PORT_PMUX_PMUXE_E |
|---|
| 113 | #define IR_CCx CC0 |
|---|
| 114 | #define IR_CCn 0 |
|---|
| 115 | #else |
|---|
| 116 | #error "Pin 2 only available on Arduino M0 Pro" |
|---|
| 117 | #endif |
|---|
| 118 | #elif (IR_SEND_PWM_PIN==3) |
|---|
| 119 | //PA09 E:TCC0-WO[1] F:TCC1-WO[3] |
|---|
| 120 | #define IR_MUX_EF PORT_PMUX_PMUXO_E |
|---|
| 121 | #define IR_CCx CC1 |
|---|
| 122 | #define IR_CCn 1 |
|---|
| 123 | #elif (IR_SEND_PWM_PIN==4) |
|---|
| 124 | //Arduino M0 Pro swaps pins 2 and 4 |
|---|
| 125 | #if(ARDUINO_SAM_ZERO) |
|---|
| 126 | //Arduino M0 Pro |
|---|
| 127 | //PA09 E:TC3-WO[0] F:TCC0-WO[4] |
|---|
| 128 | #define IR_MUX_EF PORT_PMUX_PMUXE_F |
|---|
| 129 | #define IR_CCx CC4 |
|---|
| 130 | #define IR_CCn 4 |
|---|
| 131 | #else |
|---|
| 132 | //Arduino Zero |
|---|
| 133 | //PA08 E:TCC0-WO[0] F:TCC1-WO[2] |
|---|
| 134 | #define IR_MUX_EF PORT_PMUX_PMUXE_E |
|---|
| 135 | #define IR_CCx CC0 |
|---|
| 136 | #define IR_CCn 0 |
|---|
| 137 | #endif |
|---|
| 138 | #elif (IR_SEND_PWM_PIN==5) |
|---|
| 139 | //PA15 E:TC3-WO[1] F:TCC0-WO[5] |
|---|
| 140 | #define IR_MUX_EF PORT_PMUX_PMUXO_F |
|---|
| 141 | #define IR_CCx CC5 |
|---|
| 142 | #define IR_CCn 5 |
|---|
| 143 | #elif (IR_SEND_PWM_PIN==6) |
|---|
| 144 | //PA20 E:TC7-WO[0] F:TCC0-WO[6] |
|---|
| 145 | #define IR_MUX_EF PORT_PMUX_PMUXE_F |
|---|
| 146 | #define IR_CCx CC6 |
|---|
| 147 | #define IR_CCn 6 |
|---|
| 148 | #elif (IR_SEND_PWM_PIN==7) |
|---|
| 149 | //NOTE: Arduino M0 Pro only |
|---|
| 150 | #if defined(ARDUINO_SAM_ZERO) |
|---|
| 151 | //PA08 E:TC7-WO[1] F:TCC0-WO[7] |
|---|
| 152 | #define IR_MUX_EF PORT_PMUX_PMUXO_F |
|---|
| 153 | #define IR_CCx CC7 |
|---|
| 154 | #define IR_CCn 7 |
|---|
| 155 | #else |
|---|
| 156 | #error "Pin 7 only available on Arduino M0 Pro" |
|---|
| 157 | #endif |
|---|
| 158 | #elif (IR_SEND_PWM_PIN==8) |
|---|
| 159 | //PA06 E:TCC1-WO[0] F:-- |
|---|
| 160 | #define IR_MUX_EF PORT_PMUX_PMUXE_E |
|---|
| 161 | #define IR_CCx CC0 |
|---|
| 162 | #define IR_CCn 0 |
|---|
| 163 | #elif (IR_SEND_PWM_PIN==9) |
|---|
| 164 | //PA07 E:TCC1-WO[1] F:-- |
|---|
| 165 | #define IR_MUX_EF PORT_PMUX_PMUXO_E |
|---|
| 166 | #define IR_CCx CC1 |
|---|
| 167 | #define IR_CCn 1 |
|---|
| 168 | #elif (IR_SEND_PWM_PIN==10) |
|---|
| 169 | //PA18 E:TC3-WO[0] F:TCC0-WO[2] |
|---|
| 170 | #define IR_MUX_EF PORT_PMUX_PMUXE_F |
|---|
| 171 | #define IR_CCx CC2 |
|---|
| 172 | #define IR_CCn 2 |
|---|
| 173 | #elif (IR_SEND_PWM_PIN==11) |
|---|
| 174 | //PA16 E:TCC2-WO[0] F:TCC0-WO[6] |
|---|
| 175 | #define IR_MUX_EF PORT_PMUX_PMUXE_F |
|---|
| 176 | #define IR_CCx CC6 |
|---|
| 177 | #define IR_CCn 6 |
|---|
| 178 | #elif (IR_SEND_PWM_PIN==12) |
|---|
| 179 | //PA19 E:TC3-WO[1] F:TCC0-WO[3] |
|---|
| 180 | #define IR_MUX_EF PORT_PMUX_PMUXO_F |
|---|
| 181 | #define IR_CCx CC3 |
|---|
| 182 | #define IR_CCn 3 |
|---|
| 183 | #elif (IR_SEND_PWM_PIN==13) |
|---|
| 184 | //PA17 E:TCC2-WO[1] F:TCC0-WO[7] |
|---|
| 185 | #define IR_MUX_EF PORT_PMUX_PMUXO_F |
|---|
| 186 | #define IR_CCx CC7 |
|---|
| 187 | #define IR_CCn 7 |
|---|
| 188 | #elif (IR_SEND_PWM_PIN==25)//Adafruit Circuit Playground Express only |
|---|
| 189 | //PA23 E:TC4-WO[1] F:TCC0-WO[5] |
|---|
| 190 | #define IR_MUX_EF PORT_PMUX_PMUXO_F |
|---|
| 191 | #define IR_CCx CC5 |
|---|
| 192 | #define IR_CCn 5 |
|---|
| 193 | #else |
|---|
| 194 | #error "Invalid SAMD21 PWM pin" |
|---|
| 195 | #endif |
|---|
| 196 | |
|---|
| 197 | // Turns PWM on and off after already set up |
|---|
| 198 | #define IR_SEND_PWM_START {IR_TCCx->CTRLA.reg |= TCC_CTRLA_ENABLE;\ |
|---|
| 199 | while (IR_TCCx->SYNCBUSY.bit.ENABLE);} |
|---|
| 200 | #define IR_SEND_MARK_TIME(time) IRLibDelayUSecs(time) |
|---|
| 201 | #define IR_SEND_PWM_STOP {IR_TCCx->CTRLA.reg &= ~TCC_CTRLA_ENABLE;\ |
|---|
| 202 | while (IR_TCCx->SYNCBUSY.bit.ENABLE);} |
|---|
| 203 | #define IR_SEND_CONFIG_KHZ(val) initializeSAMD21PWM(val); |
|---|
| 204 | |
|---|
| 205 | /* These are the definitions for setting up the 50 microsecond |
|---|
| 206 | * timer interrupt for the IRrecv class. |
|---|
| 207 | */ |
|---|
| 208 | #if (IR_TCn==3) |
|---|
| 209 | #define IR_TCx TC3 |
|---|
| 210 | #define IR_GCM GCM_TCC2_TC3 |
|---|
| 211 | #define IR_RECV_INTR_NAME void TC3_Handler() |
|---|
| 212 | #define IR_IRQ TC3_IRQn |
|---|
| 213 | #elif (IR_TCn==4) |
|---|
| 214 | #define IR_TCx TC4 |
|---|
| 215 | #define IR_GCM GCM_TC4_TC5 |
|---|
| 216 | #define IR_RECV_INTR_NAME void TC4_Handler() |
|---|
| 217 | #define IR_IRQ TC4_IRQn |
|---|
| 218 | #elif (IR_TCn==5) |
|---|
| 219 | #define IR_TCx TC5 |
|---|
| 220 | #define IR_GCM GCM_TC4_TC5 |
|---|
| 221 | #define IR_RECV_INTR_NAME void TC5_Handler() |
|---|
| 222 | #define IR_IRQ TC5_IRQn |
|---|
| 223 | #else |
|---|
| 224 | #error "Invalid IR_TCn value" |
|---|
| 225 | #endif |
|---|
| 226 | |
|---|
| 227 | #define IR_RECV_ENABLE_INTR ({NVIC_EnableIRQ(IR_IRQ);\ |
|---|
| 228 | IR_TCx->COUNT16.INTENSET.reg = TC_INTENSET_OVF;\ |
|---|
| 229 | IR_TCx->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE; syncTC;}) |
|---|
| 230 | #define IR_RECV_DISABLE_INTR IR_TCx->COUNT16.INTENCLR.reg = TC_INTENCLR_OVF; |
|---|
| 231 | #define IR_RECV_CONFIG_TICKS() initializeSAMD21timerInterrupt() |
|---|
| 232 | |
|---|
| 233 | //Clear interrupt |
|---|
| 234 | #define IR_CLEAR_INTERRUPT IR_TCx->COUNT16.INTFLAG.bit.MC0 = 1; |
|---|
| 235 | |
|---|
| 236 | //prototypes |
|---|
| 237 | void initializeSAMD21PWM(uint16_t khz); |
|---|
| 238 | void initializeSAMD21timerInterrupt(void); |
|---|
| 239 | |
|---|
| 240 | #endif //IRLibSAMD21_h |
|---|