Soldered SD Card Arduino Library 1.0.0
Easily read and write files to and form the SD card breakout! A fork of the original SDFat library by Bill Greiman.
Loading...
Searching...
No Matches
DigitalPin.h
Go to the documentation of this file.
1/* Arduino DigitalIO Library
2 * Copyright (C) 2013 by William Greiman
3 *
4 * This file is part of the Arduino DigitalIO Library
5 *
6 * This Library is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with the Arduino DigitalIO Library. If not, see
18 * <http://www.gnu.org/licenses/>.
19 */
28#ifndef DigitalPin_h
29#define DigitalPin_h
30#if defined(__AVR__) || defined(DOXYGEN)
31#include <avr/io.h>
34{
35 volatile uint8_t *pin;
36 volatile uint8_t *ddr;
37 volatile uint8_t *port;
38 uint8_t mask;
39};
40
42#define GPIO_PIN(reg, bit) \
43 { \
44 &PIN##reg, &DDR##reg, &PORT##reg, 1 << bit \
45 }
46
47// Include pin map for current board.
48#include "boards/GpioPinMap.h"
49//------------------------------------------------------------------------------
51void badPinNumber(void) __attribute__((error("Pin number is too large or not a constant")));
52//------------------------------------------------------------------------------
56static inline __attribute__((always_inline)) void badPinCheck(uint8_t pin)
57{
58 if (!__builtin_constant_p(pin) || pin >= NUM_DIGITAL_PINS)
59 {
61 }
62}
63//------------------------------------------------------------------------------
68static inline __attribute__((always_inline)) volatile uint8_t *ddrReg(uint8_t pin)
69{
70 badPinCheck(pin);
71 return GpioPinMap[pin].ddr;
72}
73//------------------------------------------------------------------------------
78static inline __attribute__((always_inline)) uint8_t pinMask(uint8_t pin)
79{
80 badPinCheck(pin);
81 return GpioPinMap[pin].mask;
82}
83//------------------------------------------------------------------------------
88static inline __attribute__((always_inline)) volatile uint8_t *pinReg(uint8_t pin)
89{
90 badPinCheck(pin);
91 return GpioPinMap[pin].pin;
92}
93//------------------------------------------------------------------------------
98static inline __attribute__((always_inline)) volatile uint8_t *portReg(uint8_t pin)
99{
100 badPinCheck(pin);
101 return GpioPinMap[pin].port;
102}
103//------------------------------------------------------------------------------
109static inline __attribute__((always_inline)) void fastBitWriteSafe(volatile uint8_t *address, uint8_t mask, bool level)
110{
111 uint8_t s;
112 if (address > reinterpret_cast<uint8_t *>(0X3F))
113 {
114 s = SREG;
115 cli();
116 }
117 if (level)
118 {
119 *address |= mask;
120 }
121 else
122 {
123 *address &= ~mask;
124 }
125 if (address > reinterpret_cast<uint8_t *>(0X3F))
126 {
127 SREG = s;
128 }
129}
130//------------------------------------------------------------------------------
135static inline __attribute__((always_inline)) bool fastDigitalRead(uint8_t pin)
136{
137 return *pinReg(pin) & pinMask(pin);
138}
139//------------------------------------------------------------------------------
146static inline __attribute__((always_inline)) void fastDigitalToggle(uint8_t pin)
147{
148 if (pinReg(pin) > reinterpret_cast<uint8_t *>(0X3F))
149 {
150 // must write bit to high address port
151 *pinReg(pin) = pinMask(pin);
152 }
153 else
154 {
155 // will compile to sbi and PIN register will not be read.
156 *pinReg(pin) |= pinMask(pin);
157 }
158}
159//------------------------------------------------------------------------------
164static inline __attribute__((always_inline)) void fastDigitalWrite(uint8_t pin, bool level)
165{
166 fastBitWriteSafe(portReg(pin), pinMask(pin), level);
167}
168//------------------------------------------------------------------------------
173static inline __attribute__((always_inline)) void fastDdrWrite(uint8_t pin, bool level)
174{
175 fastBitWriteSafe(ddrReg(pin), pinMask(pin), level);
176}
177//------------------------------------------------------------------------------
185static inline __attribute__((always_inline)) void fastPinMode(uint8_t pin, uint8_t mode)
186{
187 fastDdrWrite(pin, mode == OUTPUT);
188 if (mode != OUTPUT)
189 {
190 fastDigitalWrite(pin, mode == INPUT_PULLUP);
191 }
192}
193#else // defined(__AVR__)
194#if defined(CORE_TEENSY)
195//------------------------------------------------------------------------------
200static inline __attribute__((always_inline)) bool fastDigitalRead(uint8_t pin)
201{
202 return *portInputRegister(pin);
203}
204//------------------------------------------------------------------------------
209static inline __attribute__((always_inline)) void fastDigitalWrite(uint8_t pin, bool value)
210{
211 if (value)
212 {
213 *portSetRegister(pin) = 1;
214 }
215 else
216 {
217 *portClearRegister(pin) = 1;
218 }
219}
220#elif defined(__SAM3X8E__) || defined(__SAM3X8H__)
221//------------------------------------------------------------------------------
226static inline __attribute__((always_inline)) bool fastDigitalRead(uint8_t pin)
227{
228 return g_APinDescription[pin].pPort->PIO_PDSR & g_APinDescription[pin].ulPin;
229}
230//------------------------------------------------------------------------------
235static inline __attribute__((always_inline)) void fastDigitalWrite(uint8_t pin, bool value)
236{
237 if (value)
238 {
239 g_APinDescription[pin].pPort->PIO_SODR = g_APinDescription[pin].ulPin;
240 }
241 else
242 {
243 g_APinDescription[pin].pPort->PIO_CODR = g_APinDescription[pin].ulPin;
244 }
245}
246#elif defined(ESP8266)
247//------------------------------------------------------------------------------
252static inline __attribute__((always_inline)) void fastDigitalWrite(uint8_t pin, uint8_t val)
253{
254 if (pin < 16)
255 {
256 if (val)
257 {
258 GPOS = (1 << pin);
259 }
260 else
261 {
262 GPOC = (1 << pin);
263 }
264 }
265 else if (pin == 16)
266 {
267 if (val)
268 {
269 GP16O |= 1;
270 }
271 else
272 {
273 GP16O &= ~1;
274 }
275 }
276}
277//------------------------------------------------------------------------------
282static inline __attribute__((always_inline)) bool fastDigitalRead(uint8_t pin)
283{
284 if (pin < 16)
285 {
286 return GPIP(pin);
287 }
288 else if (pin == 16)
289 {
290 return GP16I & 0x01;
291 }
292 return 0;
293}
294#else // CORE_TEENSY
295//------------------------------------------------------------------------------
296inline void fastDigitalWrite(uint8_t pin, bool value)
297{
298 digitalWrite(pin, value);
299}
300//------------------------------------------------------------------------------
301inline bool fastDigitalRead(uint8_t pin)
302{
303 return digitalRead(pin);
304}
305#endif // CORE_TEENSY
306//------------------------------------------------------------------------------
307inline void fastDigitalToggle(uint8_t pin)
308{
309 fastDigitalWrite(pin, !fastDigitalRead(pin));
310}
311//------------------------------------------------------------------------------
312inline void fastPinMode(uint8_t pin, uint8_t mode)
313{
314 pinMode(pin, mode);
315}
316#endif // __AVR__
317//------------------------------------------------------------------------------
324#define fastPinConfig(pin, mode, level) \
325 { \
326 fastPinMode(pin, mode); \
327 fastDigitalWrite(pin, level); \
328 }
329//==============================================================================
334template <uint8_t PinNumber> class DigitalPin
335{
336 public:
337 //----------------------------------------------------------------------------
339 DigitalPin()
340 {
341 }
342 //----------------------------------------------------------------------------
349 inline DigitalPin &operator=(bool value) __attribute__((always_inline))
350 {
351 write(value);
352 return *this;
353 }
354 //----------------------------------------------------------------------------
358 inline operator bool() const __attribute__((always_inline))
359 {
360 return read();
361 }
362 //----------------------------------------------------------------------------
368 inline __attribute__((always_inline)) void config(uint8_t mode, bool level)
369 {
370 fastPinConfig(PinNumber, mode, level);
371 }
372 //----------------------------------------------------------------------------
376 inline __attribute__((always_inline)) void high()
377 {
378 write(true);
379 }
380 //----------------------------------------------------------------------------
384 inline __attribute__((always_inline)) void low()
385 {
386 write(false);
387 }
388 //----------------------------------------------------------------------------
396 inline __attribute__((always_inline)) void mode(uint8_t mode)
397 {
398 fastPinMode(PinNumber, mode);
399 }
400 //----------------------------------------------------------------------------
402 inline __attribute__((always_inline)) bool read() const
403 {
404 return fastDigitalRead(PinNumber);
405 }
406 //----------------------------------------------------------------------------
412 inline __attribute__((always_inline)) void toggle()
413 {
414 fastDigitalToggle(PinNumber);
415 }
416 //----------------------------------------------------------------------------
421 inline __attribute__((always_inline)) void write(bool value)
422 {
423 fastDigitalWrite(PinNumber, value);
424 }
425};
426#endif // DigitalPin_h
static const GpioPinMap_t GpioPinMap[]
Definition AvrDevelopersGpioPinMap.h:3
union csd_t __attribute__
static bool value
Definition DigitalPin.h:210
static uint8_t mode
Definition DigitalPin.h:186
void badPinNumber(void) __attribute__((error("Pin number is too large or not a const ant")))
static uint8_t bool level
Definition DigitalPin.h:110
static uint8_t mask
Definition DigitalPin.h:109
Definition DigitalPin.h:34
volatile uint8_t * ddr
Definition DigitalPin.h:36
volatile uint8_t * port
Definition DigitalPin.h:37
volatile uint8_t * pin
Definition DigitalPin.h:35
uint8_t mask
Definition DigitalPin.h:38