FastLED 3.6.0
Loading...
Searching...
No Matches
fastpin.h
Go to the documentation of this file.
1#ifndef __INC_FASTPIN_H
2#define __INC_FASTPIN_H
3
4#include "FastLED.h"
5
6#include "led_sysdefs.h"
7#include <stddef.h>
8
9#pragma GCC diagnostic push
10#pragma GCC diagnostic ignored "-Wignored-qualifiers"
11
14
15FASTLED_NAMESPACE_BEGIN
16
19#define NO_PIN 255
20
22//
23// Pin access class - needs to tune for various platforms (naive fallback solution?)
24//
26
29public:
30 virtual void select() = 0;
31 virtual void release() = 0;
32 virtual bool isSelected() = 0;
33};
34
35#if !defined(FASTLED_NO_PINMAP)
36
38class Pin : public Selectable {
39 volatile RwReg *mPort;
40 volatile RoReg *mInPort;
41 RwReg mPinMask;
42 uint8_t mPin;
43
46 void _init() {
47 mPinMask = digitalPinToBitMask(mPin);
48 mPort = (volatile RwReg*)portOutputRegister(digitalPinToPort(mPin));
49 mInPort = (volatile RoReg*)portInputRegister(digitalPinToPort(mPin));
50 }
51
52public:
55 Pin(int pin) : mPin(pin) { _init(); }
56
57 typedef volatile RwReg * port_ptr_t;
58 typedef RwReg port_t;
59
61 inline void setOutput() { pinMode(mPin, OUTPUT); }
62
64 inline void setInput() { pinMode(mPin, INPUT); }
65
67 inline void hi() __attribute__ ((always_inline)) { *mPort |= mPinMask; }
69 inline void lo() __attribute__ ((always_inline)) { *mPort &= ~mPinMask; }
70
72 inline void strobe() __attribute__ ((always_inline)) { toggle(); toggle(); }
75 inline void toggle() __attribute__ ((always_inline)) { *mInPort = mPinMask; }
76
79 inline void hi(FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { *port |= mPinMask; }
82 inline void lo(FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { *port &= ~mPinMask; }
86 inline void set(FASTLED_REGISTER port_t val) __attribute__ ((always_inline)) { *mPort = val; }
87
91 inline void fastset(FASTLED_REGISTER port_ptr_t port, FASTLED_REGISTER port_t val) __attribute__ ((always_inline)) { *port = val; }
92
94 port_t hival() __attribute__ ((always_inline)) { return *mPort | mPinMask; }
96 port_t loval() __attribute__ ((always_inline)) { return *mPort & ~mPinMask; }
98 port_ptr_t port() __attribute__ ((always_inline)) { return mPort; }
100 port_t mask() __attribute__ ((always_inline)) { return mPinMask; }
101
103 virtual void select() { hi(); }
105 virtual void release() { lo(); }
107 virtual bool isSelected() { return (*mPort & mPinMask) == mPinMask; }
108};
109
111class OutputPin : public Pin {
112public:
114 OutputPin(int pin) : Pin(pin) { setOutput(); }
115};
116
118class InputPin : public Pin {
119public:
121 InputPin(int pin) : Pin(pin) { setInput(); }
122};
123
124#else
125// This is the empty code version of the raw pin class, method bodies should be filled in to Do The Right Thing[tm] when making this
126// available on a new platform
127class Pin : public Selectable {
128 volatile RwReg *mPort;
129 volatile RoReg *mInPort;
130 RwReg mPinMask;
131 uint8_t mPin;
132
133 void _init() {
134 // TODO: fill in init on a new platform
135 mPinMask = 0;
136 mPort = NULL;
137 mInPort = NULL;
138 }
139
140public:
141 Pin(int pin) : mPin(pin) { _init(); }
142
143 void setPin(int pin) { mPin = pin; _init(); }
144
145 typedef volatile RwReg * port_ptr_t;
146 typedef RwReg port_t;
147
148 inline void setOutput() { /* TODO: Set pin output */ }
149 inline void setInput() { /* TODO: Set pin input */ }
150
151 inline void hi() __attribute__ ((always_inline)) { *mPort |= mPinMask; }
152 inline void lo() __attribute__ ((always_inline)) { *mPort &= ~mPinMask; }
153
154 inline void strobe() __attribute__ ((always_inline)) { toggle(); toggle(); }
155 inline void toggle() __attribute__ ((always_inline)) { *mInPort = mPinMask; }
156
157 inline void hi(FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { *port |= mPinMask; }
158 inline void lo(FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { *port &= ~mPinMask; }
159 inline void set(FASTLED_REGISTER port_t val) __attribute__ ((always_inline)) { *mPort = val; }
160
161 inline void fastset(FASTLED_REGISTER port_ptr_t port, FASTLED_REGISTER port_t val) __attribute__ ((always_inline)) { *port = val; }
162
163 port_t hival() __attribute__ ((always_inline)) { return *mPort | mPinMask; }
164 port_t loval() __attribute__ ((always_inline)) { return *mPort & ~mPinMask; }
165 port_ptr_t port() __attribute__ ((always_inline)) { return mPort; }
166 port_t mask() __attribute__ ((always_inline)) { return mPinMask; }
167
168 virtual void select() { hi(); }
169 virtual void release() { lo(); }
170 virtual bool isSelected() { return (*mPort & mPinMask) == mPinMask; }
171};
172
173class OutputPin : public Pin {
174public:
175 OutputPin(int pin) : Pin(pin) { setOutput(); }
176};
177
178class InputPin : public Pin {
179public:
180 InputPin(int pin) : Pin(pin) { setInput(); }
181};
182
183#endif
184
199#ifdef FASTLED_FORCE_SOFTWARE_PINS
200template<uint8_t PIN> class FastPin {
201 static RwReg sPinMask;
202 static volatile RwReg *sPort;
203 static volatile RoReg *sInPort;
204 static void _init() {
205#if !defined(FASTLED_NO_PINMAP)
206 sPinMask = digitalPinToBitMask(PIN);
207 sPort = portOutputRegister(digitalPinToPort(PIN));
208 sInPort = portInputRegister(digitalPinToPort(PIN));
209#endif
210 }
211
212public:
213 typedef volatile RwReg * port_ptr_t;
214 typedef RwReg port_t;
215
217 inline static void setOutput() { _init(); pinMode(PIN, OUTPUT); }
219 inline static void setInput() { _init(); pinMode(PIN, INPUT); }
220
222 inline static void hi() __attribute__ ((always_inline)) { *sPort |= sPinMask; }
224 inline static void lo() __attribute__ ((always_inline)) { *sPort &= ~sPinMask; }
225
227 inline static void strobe() __attribute__ ((always_inline)) { toggle(); toggle(); }
228
230 inline static void toggle() __attribute__ ((always_inline)) { *sInPort = sPinMask; }
231
233 inline static void hi(FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { *port |= sPinMask; }
235 inline static void lo(FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { *port &= ~sPinMask; }
237 inline static void set(FASTLED_REGISTER port_t val) __attribute__ ((always_inline)) { *sPort = val; }
238
240 inline static void fastset(FASTLED_REGISTER port_ptr_t port, FASTLED_REGISTER port_t val) __attribute__ ((always_inline)) { *port = val; }
241
243 static port_t hival() __attribute__ ((always_inline)) { return *sPort | sPinMask; }
245 static port_t loval() __attribute__ ((always_inline)) { return *sPort & ~sPinMask; }
247 static port_ptr_t port() __attribute__ ((always_inline)) { return sPort; }
249 static port_t mask() __attribute__ ((always_inline)) { return sPinMask; }
250};
251
252template<uint8_t PIN> RwReg FastPin<PIN>::sPinMask;
253template<uint8_t PIN> volatile RwReg *FastPin<PIN>::sPort;
254template<uint8_t PIN> volatile RoReg *FastPin<PIN>::sInPort;
255
256#else
257
258template<uint8_t PIN> class FastPin {
259 constexpr static bool validpin() { return false; }
260
261 static_assert(validpin(), "Invalid pin specified");
262
263 static void _init() { }
264
265public:
266 typedef volatile RwReg * port_ptr_t;
267 typedef RwReg port_t;
268
270 inline static void setOutput() { }
272 inline static void setInput() { }
273
275 inline static void hi() __attribute__ ((always_inline)) { }
277 inline static void lo() __attribute__ ((always_inline)) { }
278
280 inline static void strobe() __attribute__ ((always_inline)) { }
281
283 inline static void toggle() __attribute__ ((always_inline)) { }
284
286 inline static void hi(FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { }
288 inline static void lo(FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { }
290 inline static void set(FASTLED_REGISTER port_t val) __attribute__ ((always_inline)) { }
291
293 inline static void fastset(FASTLED_REGISTER port_ptr_t port, FASTLED_REGISTER port_t val) __attribute__ ((always_inline)) { }
294
296 static port_t hival() __attribute__ ((always_inline)) { return 0; }
298 static port_t loval() __attribute__ ((always_inline)) { return 0;}
300 static port_ptr_t port() __attribute__ ((always_inline)) { return NULL; }
302 static port_t mask() __attribute__ ((always_inline)) { return 0; }
303};
304
305#endif
306
310template<uint8_t PIN> class FastPinBB : public FastPin<PIN> {};
311
312typedef volatile uint32_t & reg32_t;
313typedef volatile uint32_t * ptr_reg32_t;
314
317template<uint8_t port> struct __FL_PORT_INFO {
319 static bool hasPort() { return 0; }
321 static const char *portName() { return "--"; }
323 static const void *portAddr() { return NULL; }
324};
325
326
333#define _FL_DEFINE_PORT(L, BASE) template<> struct __FL_PORT_INFO<L> { \
334 static bool hasPort() { return 1; } \
335 static const char *portName() { return #L; } \
336 typedef BASE __t_baseType; \
337 static const void *portAddr() { return (void*)&__t_baseType::r(); } };
338
349#define _FL_DEFINE_PORT3(L, LC, BASE) template<> struct __FL_PORT_INFO<LC> { \
350 static bool hasPort() { return 1; } \
351 static const char *portName() { return #L; } \
352 typedef BASE __t_baseType; \
353 static const void *portAddr() { return (void*)&__t_baseType::r(); } };
354
355FASTLED_NAMESPACE_END
356
357#pragma GCC diagnostic pop
358
359#endif // __INC_FASTPIN_H
central include file for FastLED, defines the CFastLED class/object
FastPin implementation for bit-banded access.
Definition fastpin.h:310
The simplest level of Pin class.
Definition fastpin.h:200
static port_t hival()
Gets the state of the port with this pin HIGH
Definition fastpin.h:243
RwReg port_t
type for a pin read/write register, non-volatile
Definition fastpin.h:214
static void toggle()
Toggle the pin.
Definition fastpin.h:230
static void hi(FASTLED_REGISTER port_ptr_t port)
Set the same pin on another port to HIGH
Definition fastpin.h:233
static port_t loval()
Gets the state of the port with this pin LOW
Definition fastpin.h:245
static void lo(FASTLED_REGISTER port_ptr_t port)
Set the same pin on another port to LOW
Definition fastpin.h:235
static void set(FASTLED_REGISTER port_t val)
Set the state of the output register.
Definition fastpin.h:237
static port_t mask()
Get the pin mask.
Definition fastpin.h:249
volatile RwReg * port_ptr_t
type for a pin read/write register, volatile
Definition fastpin.h:213
static void lo()
Set the pin state to LOW
Definition fastpin.h:224
static port_ptr_t port()
Get the output state of the port.
Definition fastpin.h:247
static void setOutput()
Set the pin mode as OUTPUT
Definition fastpin.h:217
static void strobe()
Toggle the pin twice to create a short pulse.
Definition fastpin.h:227
static void fastset(FASTLED_REGISTER port_ptr_t port, FASTLED_REGISTER port_t val)
Set the state of a port.
Definition fastpin.h:240
static void hi()
Set the pin state to HIGH
Definition fastpin.h:222
static void setInput()
Set the pin mode as INPUT
Definition fastpin.h:219
I/O pin initially set to INPUT.
Definition fastpin.h:118
InputPin(int pin)
Constructor.
Definition fastpin.h:121
I/O pin initially set to OUTPUT.
Definition fastpin.h:111
OutputPin(int pin)
Constructor.
Definition fastpin.h:114
Naive fallback solution for low level pin access.
Definition fastpin.h:38
void fastset(FASTLED_REGISTER port_ptr_t port, FASTLED_REGISTER port_t val)
Set the state of a port.
Definition fastpin.h:91
void lo(FASTLED_REGISTER port_ptr_t port)
Set the same pin on another port to LOW
Definition fastpin.h:82
virtual void release()
Set the pin state to LOW
Definition fastpin.h:105
void setOutput()
Set the pin mode as OUTPUT
Definition fastpin.h:61
void set(FASTLED_REGISTER port_t val)
Set the state of the output register.
Definition fastpin.h:86
port_t loval()
Gets the state of the port with this pin LOW
Definition fastpin.h:96
void setInput()
Set the pin mode as INPUT
Definition fastpin.h:64
void hi()
Set the pin state to HIGH
Definition fastpin.h:67
void toggle()
Toggle the pin.
Definition fastpin.h:75
void hi(FASTLED_REGISTER port_ptr_t port)
Set the same pin on another port to HIGH
Definition fastpin.h:79
void lo()
Set the pin state to LOW
Definition fastpin.h:69
virtual void select()
Set the pin state to HIGH
Definition fastpin.h:103
volatile RwReg * port_ptr_t
type for a pin read/write register, volatile
Definition fastpin.h:57
virtual bool isSelected()
Checks if the pin is currently HIGH
Definition fastpin.h:107
RwReg port_t
type for a pin read/write register, non-volatile
Definition fastpin.h:58
port_t mask()
Get the pin mask.
Definition fastpin.h:100
port_ptr_t port()
Get the output state of the port.
Definition fastpin.h:98
void strobe()
Toggle the pin twice to create a short pulse.
Definition fastpin.h:72
Pin(int pin)
Constructor.
Definition fastpin.h:55
port_t hival()
Gets the state of the port with this pin HIGH
Definition fastpin.h:94
Abstract class for "selectable" things.
Definition fastpin.h:28
virtual void release()=0
Release this object.
virtual void select()=0
Select this object.
virtual bool isSelected()=0
Check if this object is currently selected.
volatile uint32_t * ptr_reg32_t
Pointer to a 32-bit register, volatile.
Definition fastpin.h:313
volatile uint32_t & reg32_t
Reference to a 32-bit register, volatile.
Definition fastpin.h:312
Determines which platform system definitions to include.
Utility template for tracking down information about pins and ports.
Definition fastpin.h:317
static const void * portAddr()
Gets the raw address of the port.
Definition fastpin.h:323
static bool hasPort()
Checks whether a port exists.
Definition fastpin.h:319
static const char * portName()
Gets the name of the port, as a C-string.
Definition fastpin.h:321