Infrared4Arduino 1.2.3
Loading...
Searching...
No Matches
Due.h
Go to the documentation of this file.
1/*
2Copyright (C) 2020 Bengt Martensson.
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 3 of the License, or (at
7your option) any later version.
8
9This program is distributed in the hope that it will be useful, but
10WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program. If not, see http://www.gnu.org/licenses/.
16 */
17
18// This is basically copied from
19// https://github.com/enternoescape/Arduino-IRremote-Due
20// (License: LGPL 2.1)
21// with minor adjustments.
22
23#pragma once
24
25#define CURRENT_CLASS Due
26
27#define HAS_FLASH_READ 1
28#define HAS_HARDWARE_PWM 1
29#define HAS_SAMPLING 1
30#define HAS_INPUT_CAPTURE 0
31
32#define STRCPY_PF_CAST(x) static_cast<const char*>(x)
33
34class Due : public Board {
35public:
36 Due() {
37 };
38
39private:
40
41#if defined(IR_USE_PWM0) // pin 34 ////////////////////////////////////////////
42#define PWM_PIN 34
43#define IR_USE_PWM_PORT PIOC
44#define IR_USE_PWM_PERIPH PIO_PERIPH_B
45#define IR_USE_PWM_PINMASK PIO_PC2
46#define IR_USE_PWM_CH 0
47#elif defined(IR_USE_PWM1) // pin 36 //////////////////////////////////////////
48#define PWM_PIN 36
49#define IR_USE_PWM_PORT PIOC
50#define IR_USE_PWM_PERIPH PIO_PERIPH_B
51#define IR_USE_PWM_PINMASK PIO_PC4
52#define IR_USE_PWM_CH 1
53#elif defined(IR_USE_PWM2) // pin 38 //////////////////////////////////////////
54#define PWM_PIN 38
55#define IR_USE_PWM_PORT PIOC
56#define IR_USE_PWM_PERIPH PIO_PERIPH_B
57#define IR_USE_PWM_PINMASK PIO_PC6
58#define IR_USE_PWM_CH 2
59#elif defined(IR_USE_PWM3) // pin 40 //////////////////////////////////////////
60#define PWM_PIN 40
61#define IR_USE_PWM_PORT PIOC
62#define IR_USE_PWM_PERIPH PIO_PERIPH_B
63#define IR_USE_PWM_PINMASK PIO_PC8
64#define IR_USE_PWM_CH 3
65#elif defined(IR_USE_PWM4) // pin 9 ///////////////////////////////////////////
66#define PWM_PIN 9
67#define IR_USE_PWM_PORT PIOC
68#define IR_USE_PWM_PERIPH PIO_PERIPH_B
69#define IR_USE_PWM_PINMASK PIO_PC21
70#define IR_USE_PWM_CH 4
71#elif defined(IR_USE_PWM5) // pin 8 ///////////////////////////////////////////
72#define PWM_PIN 8
73#define IR_USE_PWM_PORT PIOC
74#define IR_USE_PWM_PERIPH PIO_PERIPH_B
75#define IR_USE_PWM_PINMASK PIO_PC22
76#define IR_USE_PWM_CH 5
77#elif defined(IR_USE_PWM6) // pin 7 ///////////////////////////////////////////
78#define PWM_PIN 7
79#define IR_USE_PWM_PORT PIOC
80#define IR_USE_PWM_PERIPH PIO_PERIPH_B
81#define IR_USE_PWM_PINMASK PIO_PC23
82#define IR_USE_PWM_CH 6
83#elif defined(IR_USE_PWM7) // pin 6 ///////////////////////////////////////////
84#define PWM_PIN 6
85#define IR_USE_PWM_PORT PIOC
86#define IR_USE_PWM_PERIPH PIO_PERIPH_B
87#define IR_USE_PWM_PINMASK PIO_PC24
88#define IR_USE_PWM_CH 7
89#endif
90
91#if defined(IR_USE_TC3) // Timer clock 3 //////////////////////////////////////
92#define IR_USE_TC_IRQ TC3_IRQn
93#define IR_USE_TC TC1
94#define IR_USE_CH 0
95#define TIMER_INTR_NAME TC3_Handler
96#elif defined(IR_USE_TC4) // Timer clock 4 ////////////////////////////////////
97#define IR_USE_TC_IRQ TC4_IRQn
98#define IR_USE_TC TC1
99#define IR_USE_CH 1
100#define TIMER_INTR_NAME TC4_Handler
101#elif defined(IR_USE_TC5) // Timer clock 5 ////////////////////////////////////
102#define IR_USE_TC_IRQ TC5_IRQn
103#define IR_USE_TC TC1
104#define IR_USE_CH 2
105#define TIMER_INTR_NAME TC5_Handler
106#endif
107
108 //Clears the interrupt.
109 void timerReset() {
110 IR_USE_TC->TC_CHANNEL[IR_USE_CH].TC_SR;
111 }
112
113 void timerEnablePwm() {
114 PWMC_EnableChannel(PWM_INTERFACE, IR_USE_PWM_CH);
115 };
116
117 void timerDisablePwm() {
118 PWMC_DisableChannel(PWM_INTERFACE, IR_USE_PWM_CH);
119 writeLow();
120 };
121
122 void timerEnableIntr() {
123 NVIC_EnableIRQ(IR_USE_TC_IRQ);
124 };
125
126 void timerDisableIntr() {
127 NVIC_DisableIRQ(IR_USE_TC_IRQ);
128 };
129
130 static const unsigned multiplicator = 20U; // was 2U;
131
132 void timerConfigHz(frequency_t frequency, dutycycle_t dutyCycle) {
133 pmc_enable_periph_clk(PWM_INTERFACE_ID);
134 const uint32_t pwmval = frequency * multiplicator;
135 PWMC_ConfigureClocks(PWM_FREQUENCY * PWM_MAX_DUTY_CYCLE, pwmval, F_CPU);
136 PIO_Configure(IR_USE_PWM_PORT, IR_USE_PWM_PERIPH, IR_USE_PWM_PINMASK, PIO_DEFAULT);
137 PWMC_ConfigureChannel(PWM_INTERFACE, IR_USE_PWM_CH, PWM_CMR_CPRE_CLKB, 0U, 0U);
138 PWMC_SetPeriod(PWM_INTERFACE, IR_USE_PWM_CH, multiplicator);
139 PWMC_SetDutyCycle(PWM_INTERFACE, IR_USE_PWM_CH, static_cast<uint16_t>((multiplicator * dutyCycle + 50U) / 100U));
140 };
141
142 void timerConfigNormal() {
143 pmc_enable_periph_clk(static_cast<uint32_t>(IR_USE_TC_IRQ));
144 TC_Configure(IR_USE_TC, IR_USE_CH, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK1);
145 const uint32_t rc = F_CPU / 2U * microsPerTick / 1000000U;
146 TC_SetRA(IR_USE_TC, IR_USE_CH, rc / 2U);
147 TC_SetRC(IR_USE_TC, IR_USE_CH, rc);
148 TC_Start(IR_USE_TC, IR_USE_CH);
149 IR_USE_TC->TC_CHANNEL[IR_USE_CH].TC_IER = TC_IER_CPCS;
150 IR_USE_TC->TC_CHANNEL[IR_USE_CH].TC_IDR = ~TC_IER_CPCS;
151 };
152};
int8_t dutycycle_t
Type for duty cycle in percent.
Definition: InfraredTypes.h:36
uint32_t frequency_t
Type for modulation frequency in Hz.
Definition: InfraredTypes.h:31
This class serves as an HAL (Hardware Abstraction Layer).
Definition: Board.h:33
static const unsigned long microsPerTick
Definition: Board.h:57
virtual void writeLow()
Definition: Board.h:298
Definition: Due.h:34
Due()
Definition: Due.h:36