Mixin base class that provides the default validpin() implementation for explicitly declared pins. All platform-specific pin classes (_ARMPIN, _ESPPIN, etc.) should inherit from this to indicate that explicitly defined pins are valid by default. Undefined pins use the default FastPin<> template which returns false.
All explicitly declared pins are valid by default Platforms can override this in specific cases to mark pins as invalid (e.g., ground pins, UART pins)
Abstract class for "selectable" things
< Select this object
< Release this object
< Check if this object is currently selected
The simplest level of Pin class. This relies on runtime functions during initialization to get the port/pin mask for the pin. Most of the accesses involve references to these static globals that get set up. This won't be the fastest set of pin operations, but it will provide pin level access on pretty much all Arduino environments. In addition, it includes some methods to help optimize access in various ways. Namely, the versions of hi(), lo(), and fastset() that take the port register as a passed in register variable (saving a global dereference), since these functions are aggressively inlined, that can help collapse out a lot of extraneous memory loads/dereferences.
In addition, if, while writing a bunch of data to a pin, you know no other pins will be getting written to, you can get/cache a value of the pin's port register and use that to do a full set to the register. This results in one being able to simply do a store to the register, vs. the load, and/or, and store that would be done normally.
There are platform specific instantiations of this class that provide direct i/o register access to pins for much higher speed pin twiddling.
Note that these classes are all static functions. So the proper usage is Pin<13>::hi(); or such. Instantiating objects is not recommended, as passing Pin objects around will likely -not- have the effect you're expecting.
<
<
FastPin implementation for bit-banded access. Only for MCUs that support bitbanding.
- Note
- This bitband class is optional!
< Reference to a 32-bit register, volatile
< Pointer to a 32-bit register, volatile
Utility template for tracking down information about pins and ports
- Template Parameters
-
| port | the port to check information for |
Checks whether a port exists
Gets the name of the port, as a C-string
Gets the raw address of the port
Macro to create the instantiations for defined ports. We're going to abuse this later for auto discovery of pin/port mappings for new variants. Use this for ports that are numeric in nature, e.g. GPIO0, GPIO1, etc.
- Parameters
-
| L | the number of the port |
| BASE | the data type for the register |
Macro to create the instantiations for defined ports. We're going to abuse this later for auto discovery of pin/port mappings for new variants. Use this for ports that are letters. The first parameter will be the letter, the second parameter will be an integer/counter of some kind. This is because attempts to turn macro parameters into character constants break in some compilers.
- Parameters
-
| L | the letter of the port |
| LC | an integer counter |
| BASE | the data type for the register |
Definition at line 19 of file fastpin_base.h.
26 {
27
29
30
31
33
38struct ValidPinBase {
41 static constexpr bool validpin()
FL_NOEXCEPT {
return true; }
42};
43
45class Selectable {
46public:
47 #ifndef FL_IS_AVR
49 #endif
53};
54
69#ifdef FASTLED_FORCE_SOFTWARE_PINS
70template<fl::u8 PIN>
class FastPin {
71 static RwReg sPinMask;
72 static volatile RwReg *sPort;
73 static volatile RoReg *sInPort;
74 static void _init() {
75#if !defined(FASTLED_NO_PINMAP)
76 sPinMask = digitalPinToBitMask(
PIN);
77 sPort = portOutputRegister(digitalPinToPort(
PIN));
78 sInPort = portInputRegister(digitalPinToPort(
PIN));
79#endif
80 }
81
82public:
83 typedef volatile RwReg * port_ptr_t;
84 typedef RwReg port_t;
85
87 inline static void setOutput() { _init();
pinMode(
PIN, PinMode::Output); }
89 inline static void setInput() { _init();
pinMode(
PIN, PinMode::Input); }
90
92 inline static void hi() __attribute__ ((always_inline)) { *sPort |= sPinMask; }
94 inline static void lo() __attribute__ ((always_inline)) { *sPort &= ~sPinMask; }
95
97 inline static void strobe() __attribute__ ((always_inline)) {
toggle();
toggle(); }
98
100 inline static void toggle() __attribute__ ((always_inline)) { *sInPort = sPinMask; }
101
103 inline static void hi(
FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { *port |= sPinMask; }
105 inline static void lo(
FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { *port &= ~sPinMask; }
107 inline static void set(
FASTLED_REGISTER port_t val) __attribute__ ((always_inline)) { *sPort = val; }
108
111
113 static port_t hival() __attribute__ ((always_inline)) { return *sPort | sPinMask; }
115 static port_t loval() __attribute__ ((always_inline)) { return *sPort & ~sPinMask; }
117 static port_ptr_t port() __attribute__ ((always_inline)) { return sPort; }
119 static port_t mask() __attribute__ ((always_inline)) { return sPinMask; }
120};
121
122template<fl::u8 PIN> RwReg FastPin<PIN>::sPinMask;
123template<fl::u8 PIN> volatile RwReg *FastPin<PIN>::sPort;
124template<fl::u8 PIN> volatile RoReg *FastPin<PIN>::sInPort;
125
126#else
127
128template<fl::u8 PIN>
class FastPin {
129
130
131
132
133
134
135#ifdef FASTLED_ALL_PINS_VALID
136 constexpr static bool validpin()
FL_NOEXCEPT {
return true; }
137#else
138 constexpr static bool validpin() { return false; }
139#endif
140 constexpr static bool LowSpeedOnlyRecommended()
FL_NOEXCEPT {
141
142
143 return false;
144 }
145
146 FL_STATIC_ASSERT(validpin(),
"This pin has been marked as an invalid pin, common reasons includes it being a ground pin, read only, or too noisy (e.g. hooked up to the uart).");
147
149
150public:
151 typedef volatile RwReg * port_ptr_t;
152 typedef RwReg port_t;
153
158
160 inline static void hi()
FL_NOEXCEPT __attribute__ ((always_inline)) { }
162 inline static void lo()
FL_NOEXCEPT __attribute__ ((always_inline)) { }
163
165 inline static void strobe()
FL_NOEXCEPT __attribute__ ((always_inline)) { }
166
169
173 }
177 }
181 }
182
187 }
188
190 static port_t hival()
FL_NOEXCEPT __attribute__ ((always_inline)) {
return 0; }
192 static port_t loval()
FL_NOEXCEPT __attribute__ ((always_inline)) {
return 0;}
194 static port_ptr_t port()
FL_NOEXCEPT __attribute__ ((always_inline)) {
return nullptr; }
196 static port_t mask()
FL_NOEXCEPT __attribute__ ((always_inline)) {
return 0; }
197};
198
199#endif
200
205
206typedef volatile fl::u32 & reg32_t;
207typedef volatile fl::u32 * ptr_reg32_t;
208
211template<fl::u8 port> struct __FL_PORT_INFO {
215 static const char *portName()
FL_NOEXCEPT {
return "--"; }
217 static const void *portAddr()
FL_NOEXCEPT {
return nullptr; }
218};
219
220
227#define _FL_DEFINE_PORT(L, BASE) template<> struct __FL_PORT_INFO<L> { \
228 typedef BASE __t_baseType; \
229 static bool hasPort() { return 1; } \
230 static const char *portName() { return #L; } \
231 static const void *portAddr() { return (void*)&__t_baseType::r(); } };
232
243#define _FL_DEFINE_PORT3(L, LC, BASE) template<> struct __FL_PORT_INFO<LC> { \
244 typedef BASE __t_baseType; \
245 static bool hasPort() { return 1; } \
246 static const char *portName() { return #L; } \
247 static const void *portAddr() { return (void*)&__t_baseType::r(); } };
248
249}
fl::FastPinBB< PIN > FastPinBB
fl::FastPin< PIN > FastPin
void pinMode(int pin, PinMode mode)
Set pin mode (input, output, pull-up, pull-down)
#define FL_STATIC_ASSERT(...)
#define FASTLED_UNUSED(x)
References FASTLED_REGISTER, FASTLED_UNUSED, FL_DISABLE_WARNING_VOLATILE, FL_NOEXCEPT, FL_STATIC_ASSERT, PIN, and toggle.