source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/utility/IRLibSAMD21.cpp @ 8

Last change on this file since 8 was 3, checked in by markus, 7 years ago

Setup project structure

File size: 3.1 KB
Line 
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/* See IRLibSAMD21.h for details about this implementation.
12 */
13#if defined (__SAMD21G18A__)
14#include "IRLibHardware.h"
15
16//Save some typing
17#define IR_APD g_APinDescription[IR_SEND_PWM_PIN]
18
19void initializeSAMD21PWM(uint16_t khz) {
20  // Enable the port multiplexer for the PWM channel on pin   
21  PORT->Group[IR_APD.ulPort].PINCFG[IR_APD.ulPin].bit.PMUXEN = 1;
22  // Connect the TCC timer to the port outputs - port pins are paired odd PMUXO and even PMUXE
23  // F & E peripherals specify the timers: TCC0, TCC1 and TCC2
24  PORT->Group[IR_APD.ulPort].PMUX[IR_APD.ulPin >> 1].reg |= IR_MUX_EF; 
25
26  // Feed GCLK0 to TCC0 and TCC1
27  REG_GCLK_CLKCTRL = GCLK_CLKCTRL_CLKEN |       // Enable GCLK0 to TCC0 and TCC1
28                     GCLK_CLKCTRL_GEN_GCLK0 |   // Select GCLK0
29                     GCLK_CLKCTRL_ID_TCC0_TCC1; // Feed GCLK0 to TCC0 and TCC1
30  syncGCLK;                                     // Wait for synchronization
31
32  // Normal (single slope) PWM operation: timers countinuously count up to PER
33  // register value and then is reset to 0
34  IR_TCCx->WAVE.reg |= TCC_WAVE_WAVEGEN_NPWM;   // Setup single slope PWM on TCCx
35  while (IR_TCCx->SYNCBUSY.bit.WAVE);           // Wait for synchronization
36
37  // Each timer counts up to a maximum or TOP value set by the PER register,
38  // this determines the frequency of the PWM operation.
39  uint32_t cc = F_CPU/(khz*1000) - 1;
40  IR_TCCx->PER.reg = cc;      // Set the frequency of the PWM on IR_TCCx
41  while(IR_TCCx->SYNCBUSY.bit.PER);
42
43  // The CCx register value corresponds to the pulsewidth in microseconds (us)
44  IR_TCCx->CC[(IR_CCn & 0x03)].reg = cc/3;      // Set the duty cycle of the PWM on TCC0 to 33%
45  while(IR_TCCx->SYNCBUSY.bit.IR_CCx);
46   
47  // Enable IR_TCCx timer but do not turn on PWM yet. Will turn it on later.
48  IR_TCCx->CTRLA.reg |= TCC_CTRLA_PRESCALER_DIV1;     // Divide GCLK0 by 1
49  while (IR_TCCx->SYNCBUSY.bit.ENABLE);
50  IR_TCCx->CTRLA.reg &= ~TCC_CTRLA_ENABLE;            //initially off will turn on later
51  while (IR_TCCx->SYNCBUSY.bit.ENABLE);
52}
53/*
54 * Setup the 50 microsecond timer hardware interrupt for the IRrecv class.
55 */
56void initializeSAMD21timerInterrupt(void) {
57  GCLK->CLKCTRL.reg = (uint16_t)(GCLK_CLKCTRL_CLKEN | 
58                      GCLK_CLKCTRL_GEN_GCLK0 | 
59                      GCLK_CLKCTRL_ID(IR_GCM));
60  syncGCLK;
61  IR_TCx->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE;
62  syncTC;
63  IR_TCx->COUNT16.CTRLA.reg = TC_CTRLA_SWRST;
64  while (IR_TCx->COUNT16.CTRLA.bit.SWRST);
65  IR_TCx->COUNT16.CTRLA.reg |= TC_CTRLA_MODE_COUNT16;
66  IR_TCx->COUNT16.CTRLA.reg |= TC_CTRLA_WAVEGEN_MFRQ;
67  IR_TCx->COUNT16.CC[0].reg = F_CPU/(1000000/USEC_PER_TICK) - 1;
68  syncTC;
69}
70#endif
Note: See TracBrowser for help on using the repository browser.