FastLED 3.10.10
Loading...
Searching...
No Matches
fastpin_base.h
Go to the documentation of this file.
1
6
7#pragma once
8
9#include "platforms/is_platform.h"
11#include "fl/stl/noexcept.h"
13
14// LPC845 / LPC804 have a native pin backend (platforms/arm/lpc/pin_lpc.hpp).
15// The other LPC families still resolve fl::pinMode / digitalWrite to
16// platforms/shared/pin_noop.hpp, so forced FastPin = NoPinMapPin would
17// silently do nothing (#5073, #5189). The guard lives here so every fastpin
18// route, including a direct fl/system/fastpin.h include, sees it.
19#if defined(FL_IS_ARM_LPC) && defined(FASTLED_FORCE_SOFTWARE_PINS) && \
20 !(defined(FL_IS_ARM_LPC_845) || defined(FL_IS_ARM_LPC_804))
21#error "FASTLED_FORCE_SOFTWARE_PINS is not supported on LPC11xx / LPC11Uxx / LPC15xx: these families have no pin backend yet (fl::pinMode/digitalWrite are no-ops; pending #5108 / #5110), so software pins could not drive anything. Remove the define to use the native GPIO pins."
22#endif
23
24#ifdef FASTLED_FORCE_SOFTWARE_PINS
25#include "fl/system/pin.h" // IWYU pragma: keep
26#include "fl/system/fastpin_no_pinmap.h" // IWYU pragma: keep
27#endif
28
32
34FL_DISABLE_WARNING(ignored-qualifiers)
35#ifdef FL_IS_ESP32
36// Get rid of the endless volatile warnings in ESP32
37FL_DISABLE_WARNING(pragmas)
39#endif
40
41namespace fl {
42
44//
45// Pin access class - needs to tune for various platforms (naive fallback solution?)
46//
48
53struct ValidPinBase {
56 static constexpr bool validpin() FL_NO_EXCEPT { return true; }
57};
58
60class Selectable {
61public:
62 #ifndef FL_IS_AVR
63 virtual ~Selectable() FL_NO_EXCEPT {}
64 #endif
65 virtual void select() FL_NO_EXCEPT = 0;
66 virtual void release() FL_NO_EXCEPT = 0;
67 virtual bool isSelected() FL_NO_EXCEPT = 0;
68};
69
84// ESP32 and ESP8266 use SDK pin APIs rather than Arduino's register-port
85// mapping macros. LPC defines FASTLED_NO_PINMAP.
86#if defined(FASTLED_FORCE_SOFTWARE_PINS) && \
87 (defined(FASTLED_NO_PINMAP) || defined(FL_IS_ESP32) || defined(FL_IS_ESP8266))
88#define FL_FASTPIN_HAS_NO_PINMAP
90#if defined(FL_IS_ESP32) || defined(FL_IS_ESP8266) || \
91 defined(FL_IS_ARM_LPC_845) || defined(FL_IS_ARM_LPC_804)
92// The ESP and LPC8xx headers supply checked specializations for all supported
93// pins. An unspecified pin must not inherit the permissive portless fallback.
94template<fl::u8 PIN> class FastPin : public NoPinMapPin<PIN> {
95public:
96 static constexpr bool validpin() FL_NO_EXCEPT { return false; }
97 static void setOutput() FL_NO_EXCEPT {
98 FL_STATIC_ASSERT(PIN != PIN, "No FastPin specialization for this pin");
99 }
100};
101#else
102template<fl::u8 PIN> class FastPin : public NoPinMapPin<PIN> {};
103#endif
104#elif defined(FASTLED_FORCE_SOFTWARE_PINS)
105#define FL_FASTPIN_HAS_RUNTIME_PORT
106template<fl::u8 PIN> class FastPin {
107 static RwReg sPinMask;
108 static volatile RwReg *sPort;
109 static volatile RoReg *sInPort;
110 static void _init() {
111 sPinMask = digitalPinToBitMask(PIN);
112 sPort = portOutputRegister(digitalPinToPort(PIN));
113 sInPort = portInputRegister(digitalPinToPort(PIN));
114 }
115
116public:
117 typedef volatile RwReg * port_ptr_t;
118 typedef RwReg port_t;
119
121 inline static void setOutput() { _init(); (fl::pinMode)(PIN, PinMode::Output); }
123 inline static void setInput() { _init(); (fl::pinMode)(PIN, PinMode::Input); }
124
126 inline static void hi() __attribute__ ((always_inline)) { *sPort |= sPinMask; }
128 inline static void lo() __attribute__ ((always_inline)) { *sPort &= ~sPinMask; }
129
131 inline static void strobe() __attribute__ ((always_inline)) { toggle(); toggle(); }
132
134 inline static void toggle() __attribute__ ((always_inline)) {
135#ifdef FL_IS_AVR
136 *sInPort = sPinMask; // AVR PIN registers toggle output bits when written with ones.
137#else
138 *sPort ^= sPinMask;
139#endif
140 }
141
143 inline static void hi(FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { *port |= sPinMask; }
145 inline static void lo(FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { *port &= ~sPinMask; }
147 inline static void set(FASTLED_REGISTER port_t val) __attribute__ ((always_inline)) { *sPort = val; }
148
150 inline static void fastset(FASTLED_REGISTER port_ptr_t port, FASTLED_REGISTER port_t val) __attribute__ ((always_inline)) { *port = val; }
151
153 static port_t hival() __attribute__ ((always_inline)) { return *sPort | sPinMask; }
155 static port_t loval() __attribute__ ((always_inline)) { return *sPort & ~sPinMask; }
157 static port_ptr_t port() __attribute__ ((always_inline)) { return sPort; }
159 static port_t mask() __attribute__ ((always_inline)) { return sPinMask; }
160};
161
162template<fl::u8 PIN> RwReg FastPin<PIN>::sPinMask;
163template<fl::u8 PIN> volatile RwReg *FastPin<PIN>::sPort;
164template<fl::u8 PIN> volatile RoReg *FastPin<PIN>::sInPort;
165
166#else
167
168template<fl::u8 PIN> class FastPin {
169 // This is a default implementation. If you are hitting this then FastPin<> is either:
170 // 1) Not defined -or-
171 // 2) Not part of the set of defined FastPin<> specializations for your platform
172 // You need to define a FastPin<> specialization
173 // or change what get's included for your particular build target.
174 // Keep in mind that these messages are cryptic, so it's best to define an invalid in type.
175#ifdef FASTLED_ALL_PINS_VALID
176 constexpr static bool validpin() FL_NO_EXCEPT { return true; }
177#else
178 constexpr static bool validpin() { return false; }
179#endif
180 constexpr static bool LowSpeedOnlyRecommended() FL_NO_EXCEPT { // Some implementations assume this exists.
181 // Caller must always determine if high speed use if allowed on a given pin,
182 // because it depends on more than just the chip packaging ... it depends on entire board (and even system) design.
183 return false; // choosing default to be FALSE, to allow users to ATTEMPT to use high-speed on pins where support is not known
184 }
185
186 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).");
187
188 static void _init() FL_NO_EXCEPT { }
189
190public:
191 typedef volatile RwReg * port_ptr_t;
192 typedef RwReg port_t;
193
195 inline static void setOutput() FL_NO_EXCEPT { }
197 inline static void setInput() FL_NO_EXCEPT { }
198
200 inline static void hi() FL_NO_EXCEPT __attribute__ ((always_inline)) { }
202 inline static void lo() FL_NO_EXCEPT __attribute__ ((always_inline)) { }
203
205 inline static void strobe() FL_NO_EXCEPT __attribute__ ((always_inline)) { }
206
208 inline static void toggle() FL_NO_EXCEPT __attribute__ ((always_inline)) { }
209
211 inline static void hi(FASTLED_REGISTER port_ptr_t port) FL_NO_EXCEPT __attribute__ ((always_inline)) {
212 FASTLED_UNUSED(port);
213 }
215 inline static void lo(FASTLED_REGISTER port_ptr_t port) FL_NO_EXCEPT __attribute__ ((always_inline)) {
216 FASTLED_UNUSED(port);
217 }
219 inline static void set(FASTLED_REGISTER port_t val) FL_NO_EXCEPT __attribute__ ((always_inline)) {
220 FASTLED_UNUSED(val);
221 }
222
224 inline static void fastset(FASTLED_REGISTER port_ptr_t port, FASTLED_REGISTER port_t val) FL_NO_EXCEPT __attribute__ ((always_inline)) {
225 FASTLED_UNUSED(port);
226 FASTLED_UNUSED(val);
227 }
228
230 static port_t hival() FL_NO_EXCEPT __attribute__ ((always_inline)) { return 0; }
232 static port_t loval() FL_NO_EXCEPT __attribute__ ((always_inline)) { return 0;}
234 static port_ptr_t port() FL_NO_EXCEPT __attribute__ ((always_inline)) { return nullptr; }
236 static port_t mask() FL_NO_EXCEPT __attribute__ ((always_inline)) { return 0; }
237};
238
239#endif
240
244template<fl::u8 PIN> class FastPinBB : public FastPin<PIN> {};
245
246typedef volatile fl::u32 & reg32_t;
247typedef volatile fl::u32 * ptr_reg32_t;
248
251template<fl::u8 port> struct __FL_PORT_INFO {
253 static bool hasPort() FL_NO_EXCEPT { return 0; }
255 static const char *portName() FL_NO_EXCEPT { return "--"; }
257 static const void *portAddr() FL_NO_EXCEPT { return nullptr; }
258};
259
260
267#define _FL_DEFINE_PORT(L, BASE) template<> struct __FL_PORT_INFO<L> { \
268 typedef BASE __t_baseType; \
269 static bool hasPort() { return 1; } \
270 static const char *portName() { return #L; } \
271 static const void *portAddr() { return (void*)&__t_baseType::r(); } };
272
283#define _FL_DEFINE_PORT3(L, LC, BASE) template<> struct __FL_PORT_INFO<LC> { \
284 typedef BASE __t_baseType; \
285 static bool hasPort() { return 1; } \
286 static const char *portName() { return #L; } \
287 static const void *portAddr() { return (void*)&__t_baseType::r(); } };
288
289} // namespace fl
290
292
bool toggle
Definition Blur.ino:12
#define PIN
Definition PinMode.ino:7
fl::FastPinBB< PIN > FastPinBB
Definition fastpin.h:20
fl::FastPin< PIN > FastPin
Definition fastpin.h:19
Forced software pin for Arduino cores with no register pin map.
void pinMode(int pin, PinMode mode) FL_NO_EXCEPT
Set pin mode (input, output, pull-up, pull-down)
Definition pin.cpp.hpp:398
Base definition for an LED controller.
#define FL_DISABLE_WARNING(warning)
#define FASTLED_REGISTER
#define FL_STATIC_ASSERT(...)
#define FL_DISABLE_WARNING_VOLATILE
#define FL_DISABLE_WARNING_PUSH
#define FL_DISABLE_WARNING_POP
#define FASTLED_UNUSED(x)
#define FL_DISABLE_WARNING_DEPRECATED_REGISTER
#define FL_NO_EXCEPT
Portable compile-time assertion wrapper.