FastArduino - Arduino LEONARDO Support v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
leonardo.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
22#ifndef BOARDS_LEONARDO_HH
23#define BOARDS_LEONARDO_HH
24
25#include "io.h"
26#include "../defines.h"
27#include <avr/sleep.h>
28
30/* This board is based on ATmega32u4 */
31#define BOARD_ATMEGA32U4
33
39namespace board
40{
46 inline static void init()
47 {
48 // Disable USB controller by default
49 *((volatile uint8_t*) USBCON) = 0;
50 *((volatile uint8_t*) UDCON) = 0;
51 // Disable all interrupts
52 *((volatile uint8_t*) UDINT) = 0;
53 *((volatile uint8_t*) UDIEN) = 0;
54 }
55
59 enum class Port: uint8_t
60 {
62 PORT_B = 0,
64 PORT_C,
66 PORT_D,
68 PORT_E,
70 PORT_F,
71 // FastArduino internal: DO NOT USE
72 NONE = UINT8_MAX
73 };
74
82 enum class DigitalPin: uint8_t
83 {
85 D0_PD2 = 0,
87 D0 = D0_PD2,
89 D1_PD3,
91 D1 = D1_PD3,
93 D2_PD1,
95 D2 = D2_PD1,
97 D3_PD0,
99 D3 = D3_PD0,
101 D4_PD4,
103 D4 = D4_PD4,
105 D5_PC6,
107 D5 = D5_PC6,
109 D6_PD7,
111 D6 = D6_PD7,
113 D7_PE6,
115 D7 = D7_PE6,
117 D8_PB4,
119 D8 = D8_PB4,
121 D9_PB5,
123 D9 = D9_PB5,
125 D10_PB6,
127 D10 = D10_PB6,
129 D11_PB7,
131 D11 = D11_PB7,
133 D12_PD6,
135 D12 = D12_PD6,
137 D13_PC7,
139 D13 = D13_PC7,
140
142 A0_PF7,
144 A0 = A0_PF7,
146 A1_PF6,
148 A1 = A1_PF6,
150 A2_PF5,
152 A2 = A2_PF5,
154 A3_PF4,
156 A3 = A3_PF4,
158 A4_PF1,
160 A4 = A4_PF1,
162 A5_PF0,
164 A5 = A5_PF0,
165
167 SCK_PB1,
169 MOSI_PB2,
171 MISO_PB3,
172
174 RXLED_PB0,
176 TXLED_PD5,
177
179 LED = D13_PC7,
180 // FastArduino internal: DO NOT USE
181 NONE = UINT8_MAX
182 };
183
187 enum class AnalogClock: uint8_t
188 {
189 MAX_FREQ_50KHz = 0,
190 MAX_FREQ_100KHz,
191 MAX_FREQ_200KHz,
192 MAX_FREQ_500KHz,
193 MAX_FREQ_1MHz
194 };
195
199 enum class AnalogReference: uint8_t
200 {
202 AREF = 0,
204 AVCC,
208 };
209
216 enum class AnalogPin: uint8_t
217 {
219 A0_ADC7 = 0,
221 A0 = A0_ADC7,
223 A1_ADC6,
225 A1 = A1_ADC6,
227 A2_ADC5,
229 A2 = A2_ADC5,
231 A3_ADC4,
233 A3 = A3_ADC4,
235 A4_ADC1,
237 A4 = A4_ADC1,
239 A5_ADC0,
241 A5 = A5_ADC0,
242
246 D4 = A6_D4_ADC8,
250 D6 = A7_D6_ADC10,
254 D8 = A8_D8_ADC11,
258 D9 = A9_D9_ADC12,
267
269 TEMP,
271 BANDGAP,
272 // FastArduino internal: DO NOT USE
273 NONE = UINT8_MAX
274 };
275
294 enum class PWMPin : uint8_t
295 {
296 D11_PB7_OC0A = 0,
297 D3_PD0_OC0B,
298 D9_PB5_OC1A,
299 D10_PB6_OC1B,
300 D11_PB7_OC1C,
301 D5_PC6_OC3A,
302 //TODO High-speed timer (Timer4) related PWM pins are not listed here because Timer4 is not supported yet
303
304 D3 = D3_PD0_OC0B,
305 D9 = D9_PB5_OC1A,
306 D10 = D10_PB6_OC1B,
307 D5 = D5_PC6_OC3A,
308 // FastArduino internal: DO NOT USE
309 NONE = UINT8_MAX
310 };
311
320 enum class ExternalInterruptPin : uint8_t
321 {
322 D3_PD0_EXT0 = 0,
323 D2_PD1_EXT1,
324 D0_PD2_EXT2,
325 D1_PD3_EXT3,
326 D7_PE6_EXT6,
327
328 D3 = D3_PD0_EXT0,
329 D2 = D2_PD1_EXT1,
330 D0 = D0_PD2_EXT2,
331 D1 = D1_PD3_EXT3,
332 D7 = D7_PE6_EXT6
333 };
334
344 enum class InterruptPin : uint8_t
345 {
346 RXLED_PB0_PCI0 = uint8_t(DigitalPin::RXLED_PB0),
347 SCK_PB1_PCI0 = uint8_t(DigitalPin::SCK_PB1),
348 MOSI_PB2_PCI0 = uint8_t(DigitalPin::MOSI_PB2),
349 MISO_PB3_PCI0 = uint8_t(DigitalPin::MISO_PB3),
350 D8_PB4_PCI0 = uint8_t(DigitalPin::D8_PB4),
351 D9_PB5_PCI0 = uint8_t(DigitalPin::D9_PB5),
352 D10_PB6_PCI0 = uint8_t(DigitalPin::D10_PB6),
353 D11_PB7_PCI0 = uint8_t(DigitalPin::D11_PB7),
354
355 D8 = D8_PB4_PCI0,
356 D9 = D9_PB5_PCI0,
357 D10 = D10_PB6_PCI0,
358 D11 = D11_PB7_PCI0
359 };
360
366 enum class USART: uint8_t
367 {
368 USART1 = 1
369 };
370
375 enum class Timer: uint8_t
376 {
378 TIMER0 = 0,
380 TIMER1 = 1,
382 TIMER3 = 3
383 };
384
385 //TODO Add specific enumeration for High-speed timer? (or specific traits?)
386
390 enum class SleepMode: uint8_t
391 {
397 IDLE = SLEEP_MODE_IDLE,
402 ADC_NOISE_REDUCTION = SLEEP_MODE_ADC,
413 POWER_DOWN = SLEEP_MODE_PWR_DOWN,
417 POWER_SAVE = SLEEP_MODE_PWR_SAVE,
422 STANDBY = SLEEP_MODE_STANDBY,
426 EXTENDED_STANDBY = SLEEP_MODE_EXT_STANDBY
427 };
428};
429
431// Forward declare interrupt service routines to allow them as friends.
432extern "C" {
433 void ADC_vect(void) SIGNAL_HANDLER;
434 void ANALOG_COMP_vect(void) SIGNAL_HANDLER;
435 void INT0_vect(void) SIGNAL_HANDLER;
436 void INT1_vect(void) SIGNAL_HANDLER;
437 void INT2_vect(void) SIGNAL_HANDLER;
438 void INT3_vect(void) SIGNAL_HANDLER;
439 void INT6_vect(void) SIGNAL_HANDLER;
440 void PCINT0_vect(void) SIGNAL_HANDLER;
441 void SPI_STC_vect(void) SIGNAL_HANDLER;
442 void TIMER0_COMPA_vect(void) SIGNAL_HANDLER;
443 void TIMER0_COMPB_vect(void) SIGNAL_HANDLER;
444 void TIMER0_OVF_vect(void) SIGNAL_HANDLER;
445 void TIMER1_CAPT_vect(void) SIGNAL_HANDLER;
446 void TIMER1_COMPA_vect(void) SIGNAL_HANDLER;
447 void TIMER1_COMPB_vect(void) SIGNAL_HANDLER;
448 void TIMER1_COMPC_vect(void) SIGNAL_HANDLER;
449 void TIMER1_OVF_vect(void) SIGNAL_HANDLER;
450 void TIMER3_CAPT_vect(void) SIGNAL_HANDLER;
451 void TIMER3_COMPA_vect(void) SIGNAL_HANDLER;
452 void TIMER3_COMPB_vect(void) SIGNAL_HANDLER;
453 void TIMER3_COMPC_vect(void) SIGNAL_HANDLER;
454 void TIMER3_OVF_vect(void) SIGNAL_HANDLER;
455 void TIMER4_COMPA_vect(void) SIGNAL_HANDLER;
456 void TIMER4_COMPB_vect(void) SIGNAL_HANDLER;
457 void TIMER4_COMPD_vect(void) SIGNAL_HANDLER;
458 void TIMER4_OVF_vect(void) SIGNAL_HANDLER;
459 void TWI_vect(void) SIGNAL_HANDLER;
460 void WDT_vect(void) SIGNAL_HANDLER;
461 void USART1_RX_vect(void) SIGNAL_HANDLER;
462 void USART1_TX_vect(void) SIGNAL_HANDLER;
463 void USART1_UDRE_vect(void) SIGNAL_HANDLER;
464 void EE_READY_vect(void) SIGNAL_HANDLER;
465}
467
468#endif /* BOARDS_LEONARDO_HH */
Defines all types and constants specific to support Arduino LEONARDO board (ATmega32u4 MCU target).
Definition: leonardo.h:40
PWMPin
Defines all digital output pins of ATmega32u4, capable of PWM output.
Definition: leonardo.h:295
Port
Defines all available ports of ATmega32u4.
Definition: leonardo.h:60
@ PORT_F
Port F (6 IO)
@ PORT_D
Port D (8 IO, only 7 available on LEONARDO)
@ PORT_C
Port C (2 IO)
@ PORT_E
Port E (2 IO, only 1 available on LEONARDO)
@ PORT_B
Port B (8 IO, only 7 available on LEONARDO)
static void init()
Performs special initialization for ATmega32u4, actually disabling USB related interrupts,...
Definition: leonardo.h:46
DigitalPin
Defines all available digital input/output pins of ATmega32u4, with reference to Arduino LEONARDO pin...
Definition: leonardo.h:83
@ A1_PF6
Pin PF6 (A1 on Arduino LEONARDO)
@ D6_PD7
Pin PD7 (D6 on Arduino LEONARDO)
@ A4
Pin PF1 (A4 on Arduino LEONARDO)
@ A0_PF7
Pin PF7 (A0 on Arduino LEONARDO)
@ D0
Pin PD2 (D0 on Arduino LEONARDO)
@ D2_PD1
Pin PD1 (D2 on Arduino LEONARDO)
@ A4_PF1
Pin PF1 (A4 on Arduino LEONARDO)
@ D4
Pin PD4 (D4 on Arduino LEONARDO)
@ A1
Pin PF6 (A1 on Arduino LEONARDO)
@ MISO_PB3
Pin PB3 (MISO (SPI) on Arduino LEONARDO)
@ D5_PC6
Pin PC6 (D5 on Arduino LEONARDO)
@ D10_PB6
Pin PB6 (D10 on Arduino LEONARDO)
@ D0_PD2
Pin PD2 (D0 on Arduino LEONARDO)
@ D1
Pin PD3 (D1 on Arduino LEONARDO)
@ RXLED_PB0
Pin PB0 (RXLED on Arduino LEONARDO, no pin)
@ D1_PD3
Pin PD3 (D1 on Arduino LEONARDO)
@ LED
Shortcut for LED pin on Arduino.
@ A3
Pin PF4 (A3 on Arduino LEONARDO)
@ D3_PD0
Pin PD0 (D3 on Arduino LEONARDO)
@ MOSI_PB2
Pin PB2 (MOSI (SPI) on Arduino LEONARDO)
@ D12
Pin PD6 (D12 on Arduino LEONARDO)
@ D10
Pin PB6 (D10 on Arduino LEONARDO)
@ D7_PE6
Pin PE6 (D7 on Arduino LEONARDO)
@ D4_PD4
Pin PD4 (D4 on Arduino LEONARDO)
@ D9_PB5
Pin PB5 (D9 on Arduino LEONARDO)
@ SCK_PB1
Pin PB1 (SCK (SPI) on Arduino LEONARDO)
@ D7
Pin PE6 (D7 on Arduino LEONARDO)
@ D8_PB4
Pin PB4 (D8 on Arduino LEONARDO)
@ D3
Pin PD0 (D3 on Arduino LEONARDO)
@ D9
Pin PB5 (D9 on Arduino LEONARDO)
@ D11_PB7
Pin PB7 (D11 on Arduino LEONARDO)
@ D8
Pin PB4 (D8 on Arduino LEONARDO)
@ D13
Pin PC7 (D13 on Arduino LEONARDO)
@ D13_PC7
Pin PC7 (D13 on Arduino LEONARDO)
@ A3_PF4
Pin PF4 (A3 on Arduino LEONARDO)
@ D11
Pin PB7 (D11 on Arduino LEONARDO)
@ D2
Pin PD1 (D2 on Arduino LEONARDO)
@ A2
Pin PF5 (A2 on Arduino LEONARDO)
@ A5
Pin PF0 (A5 on Arduino LEONARDO)
@ D12_PD6
Pin PD6 (D12 on Arduino LEONARDO)
@ TXLED_PD5
Pin PD5 (TXLED on Arduino LEONARDO, no pin)
@ A5_PF0
Pin PF0 (A5 on Arduino LEONARDO)
@ A0
Pin PF7 (A0 on Arduino LEONARDO)
@ D6
Pin PD7 (D6 on Arduino LEONARDO)
@ D5
Pin PC6 (D5 on Arduino LEONARDO)
@ A2_PF5
Pin PF5 (A2 on Arduino LEONARDO)
AnalogClock
Defines available clocks of ATmega32u4, used for analog input.
Definition: leonardo.h:188
AnalogPin
Defines all available analog input pins of ATmega32u4, with reference to Arduino LEONARDO pins.
Definition: leonardo.h:217
@ A8_D8_ADC11
Pin ADC11 (D8 on Arduino LEONARDO)
@ A0_ADC7
Pin ADC7 (A0 on Arduino LEONARDO)
@ A11_D12_ADC9
Pin ADC9 (D12 on Arduino LEONARDO)
@ A3_ADC4
Pin ADC4 (A3 on Arduino LEONARDO)
@ A10_D10_ADC13
Pin ADC13 (D10 on Arduino LEONARDO)
@ A4_ADC1
Pin ADC1 (A4 on Arduino LEONARDO)
@ A9_D9_ADC12
Pin ADC12 (D9 on Arduino LEONARDO)
@ A5_ADC0
Pin ADC0 (A5 on Arduino LEONARDO)
@ TEMP
Temperature sensor.
@ A6_D4_ADC8
Pin ADC8 (D4 on Arduino LEONARDO)
@ A2_ADC5
Pin ADC5 (A2 on Arduino LEONARDO)
@ A1_ADC6
Pin ADC6 (A1 on Arduino LEONARDO)
@ A7_D6_ADC10
Pin ADC10 (D6 on Arduino LEONARDO)
@ BANDGAP
Bandgap reference.
InterruptPin
Defines all digital output pins of ATmega32u4, usable as pin change interrupt (PCI) pins.
Definition: leonardo.h:345
USART
Defines all USART modules of ATmega32u4, actually only one.
Definition: leonardo.h:367
Timer
Defines all "standard" timers available for ATmega32u4.
Definition: leonardo.h:376
@ TIMER3
Timer3 (16 bits)
@ TIMER1
Timer1 (16 bits)
@ TIMER0
Timer0 (8 bits)
ExternalInterruptPin
Defines all digital output pins of ATmega32u4, usable as direct external interrupt pins.
Definition: leonardo.h:321
SleepMode
Defines all available sleep modes for ATmega32u4.
Definition: leonardo.h:391
@ EXTENDED_STANDBY
This mode is exactly the same as STANDBY.
@ ADC_NOISE_REDUCTION
In this mode, CPU is stopped but other peripherals and interrupts work normally, except IO.
@ POWER_DOWN
In this mode, everything is stopped (including oscillator) but external interrupts,...
@ IDLE
In this mode, CPU is stopped but all other peripherals and interrupts work normally.
@ STANDBY
This mode is similar to POWER_DOWN, except the oscillator is kept running, hence waking up from this ...
@ POWER_SAVE
This mode is exactly the same as POWER_DOWN.
AnalogReference
Defines available voltage references of ATmega32u4, used for analog input.
Definition: leonardo.h:200
@ INTERNAL_2_56V
Voltage reference is internal 2.56V reference, generated from the internal bandgap reference.
@ AREF
Voltage reference is given by the AREF pin.
@ AVCC
Voltage reference is given by the AVcc pin.