PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

2330 lines
88KB

  1. /*-------------------------------------------------------------------------
  2. Arduino library to control a wide variety of WS2811- and WS2812-based RGB
  3. LED devices such as Adafruit FLORA RGB Smart Pixels and NeoPixel strips.
  4. Currently handles 400 and 800 KHz bitstreams on 8, 12 and 16 MHz ATmega
  5. MCUs, with LEDs wired for various color orders. Handles most output pins
  6. (possible exception with upper PORT registers on the Arduino Mega).
  7. Written by Phil Burgess / Paint Your Dragon for Adafruit Industries,
  8. contributions by PJRC, Michael Miller and other members of the open
  9. source community.
  10. Adafruit invests time and resources providing this open source code,
  11. please support Adafruit and open-source hardware by purchasing products
  12. from Adafruit!
  13. -------------------------------------------------------------------------
  14. This file is part of the Adafruit NeoPixel library.
  15. NeoPixel is free software: you can redistribute it and/or modify
  16. it under the terms of the GNU Lesser General Public License as
  17. published by the Free Software Foundation, either version 3 of
  18. the License, or (at your option) any later version.
  19. NeoPixel is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. GNU Lesser General Public License for more details.
  23. You should have received a copy of the GNU Lesser General Public
  24. License along with NeoPixel. If not, see
  25. <http://www.gnu.org/licenses/>.
  26. -------------------------------------------------------------------------*/
  27. #include "Adafruit_NeoPixel.h"
  28. #if defined(NRF52) || defined(NRF52_SERIES)
  29. #include "nrf.h"
  30. // Interrupt is only disabled if there is no PWM device available
  31. // Note: Adafruit Bluefruit nrf52 does not use this option
  32. //#define NRF52_DISABLE_INT
  33. #endif
  34. // Constructor when length, pin and type are known at compile-time:
  35. Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t n, uint8_t p, neoPixelType t) :
  36. begun(false), brightness(0), pixels(NULL), endTime(0)
  37. {
  38. updateType(t);
  39. updateLength(n);
  40. setPin(p);
  41. }
  42. // via Michael Vogt/neophob: empty constructor is used when strand length
  43. // isn't known at compile-time; situations where program config might be
  44. // read from internal flash memory or an SD card, or arrive via serial
  45. // command. If using this constructor, MUST follow up with updateType(),
  46. // updateLength(), etc. to establish the strand type, length and pin number!
  47. Adafruit_NeoPixel::Adafruit_NeoPixel() :
  48. #ifdef NEO_KHZ400
  49. is800KHz(true),
  50. #endif
  51. begun(false), numLEDs(0), numBytes(0), pin(-1), brightness(0), pixels(NULL),
  52. rOffset(1), gOffset(0), bOffset(2), wOffset(1), endTime(0)
  53. {
  54. }
  55. Adafruit_NeoPixel::~Adafruit_NeoPixel() {
  56. if(pixels) free(pixels);
  57. if(pin >= 0) pinMode(pin, INPUT);
  58. }
  59. void Adafruit_NeoPixel::begin(void) {
  60. if(pin >= 0) {
  61. pinMode(pin, OUTPUT);
  62. digitalWrite(pin, LOW);
  63. }
  64. begun = true;
  65. }
  66. void Adafruit_NeoPixel::updateLength(uint16_t n) {
  67. if(pixels) free(pixels); // Free existing data (if any)
  68. // Allocate new data -- note: ALL PIXELS ARE CLEARED
  69. numBytes = n * ((wOffset == rOffset) ? 3 : 4);
  70. if((pixels = (uint8_t *)malloc(numBytes))) {
  71. memset(pixels, 0, numBytes);
  72. numLEDs = n;
  73. } else {
  74. numLEDs = numBytes = 0;
  75. }
  76. }
  77. void Adafruit_NeoPixel::updateType(neoPixelType t) {
  78. boolean oldThreeBytesPerPixel = (wOffset == rOffset); // false if RGBW
  79. wOffset = (t >> 6) & 0b11; // See notes in header file
  80. rOffset = (t >> 4) & 0b11; // regarding R/G/B/W offsets
  81. gOffset = (t >> 2) & 0b11;
  82. bOffset = t & 0b11;
  83. #ifdef NEO_KHZ400
  84. is800KHz = (t < 256); // 400 KHz flag is 1<<8
  85. #endif
  86. // If bytes-per-pixel has changed (and pixel data was previously
  87. // allocated), re-allocate to new size. Will clear any data.
  88. if(pixels) {
  89. boolean newThreeBytesPerPixel = (wOffset == rOffset);
  90. if(newThreeBytesPerPixel != oldThreeBytesPerPixel) updateLength(numLEDs);
  91. }
  92. }
  93. #if defined(ESP8266)
  94. // ESP8266 show() is external to enforce ICACHE_RAM_ATTR execution
  95. extern "C" void ICACHE_RAM_ATTR espShow(
  96. uint8_t pin, uint8_t *pixels, uint32_t numBytes, uint8_t type);
  97. #elif defined(ESP32)
  98. extern "C" void espShow(
  99. uint8_t pin, uint8_t *pixels, uint32_t numBytes, uint8_t type);
  100. #endif // ESP8266
  101. void Adafruit_NeoPixel::show(void) {
  102. if(!pixels) return;
  103. // Data latch = 300+ microsecond pause in the output stream. Rather than
  104. // put a delay at the end of the function, the ending time is noted and
  105. // the function will simply hold off (if needed) on issuing the
  106. // subsequent round of data until the latch time has elapsed. This
  107. // allows the mainline code to start generating the next frame of data
  108. // rather than stalling for the latch.
  109. while(!canShow());
  110. // endTime is a private member (rather than global var) so that multiple
  111. // instances on different pins can be quickly issued in succession (each
  112. // instance doesn't delay the next).
  113. // In order to make this code runtime-configurable to work with any pin,
  114. // SBI/CBI instructions are eschewed in favor of full PORT writes via the
  115. // OUT or ST instructions. It relies on two facts: that peripheral
  116. // functions (such as PWM) take precedence on output pins, so our PORT-
  117. // wide writes won't interfere, and that interrupts are globally disabled
  118. // while data is being issued to the LEDs, so no other code will be
  119. // accessing the PORT. The code takes an initial 'snapshot' of the PORT
  120. // state, computes 'pin high' and 'pin low' values, and writes these back
  121. // to the PORT register as needed.
  122. // NRF52 may use PWM + DMA (if available), may not need to disable interrupt
  123. #if !( defined(NRF52) || defined(NRF52_SERIES) )
  124. noInterrupts(); // Need 100% focus on instruction timing
  125. #endif
  126. #ifdef __AVR__
  127. // AVR MCUs -- ATmega & ATtiny (no XMEGA) ---------------------------------
  128. volatile uint16_t
  129. i = numBytes; // Loop counter
  130. volatile uint8_t
  131. *ptr = pixels, // Pointer to next byte
  132. b = *ptr++, // Current byte value
  133. hi, // PORT w/output bit set high
  134. lo; // PORT w/output bit set low
  135. // Hand-tuned assembly code issues data to the LED drivers at a specific
  136. // rate. There's separate code for different CPU speeds (8, 12, 16 MHz)
  137. // for both the WS2811 (400 KHz) and WS2812 (800 KHz) drivers. The
  138. // datastream timing for the LED drivers allows a little wiggle room each
  139. // way (listed in the datasheets), so the conditions for compiling each
  140. // case are set up for a range of frequencies rather than just the exact
  141. // 8, 12 or 16 MHz values, permitting use with some close-but-not-spot-on
  142. // devices (e.g. 16.5 MHz DigiSpark). The ranges were arrived at based
  143. // on the datasheet figures and have not been extensively tested outside
  144. // the canonical 8/12/16 MHz speeds; there's no guarantee these will work
  145. // close to the extremes (or possibly they could be pushed further).
  146. // Keep in mind only one CPU speed case actually gets compiled; the
  147. // resulting program isn't as massive as it might look from source here.
  148. // 8 MHz(ish) AVR ---------------------------------------------------------
  149. #if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL)
  150. #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
  151. if(is800KHz) {
  152. #endif
  153. volatile uint8_t n1, n2 = 0; // First, next bits out
  154. // Squeezing an 800 KHz stream out of an 8 MHz chip requires code
  155. // specific to each PORT register.
  156. // 10 instruction clocks per bit: HHxxxxxLLL
  157. // OUT instructions: ^ ^ ^ (T=0,2,7)
  158. // PORTD OUTPUT ----------------------------------------------------
  159. #if defined(PORTD)
  160. #if defined(PORTB) || defined(PORTC) || defined(PORTF)
  161. if(port == &PORTD) {
  162. #endif // defined(PORTB/C/F)
  163. hi = PORTD | pinMask;
  164. lo = PORTD & ~pinMask;
  165. n1 = lo;
  166. if(b & 0x80) n1 = hi;
  167. // Dirty trick: RJMPs proceeding to the next instruction are used
  168. // to delay two clock cycles in one instruction word (rather than
  169. // using two NOPs). This was necessary in order to squeeze the
  170. // loop down to exactly 64 words -- the maximum possible for a
  171. // relative branch.
  172. asm volatile(
  173. "headD:" "\n\t" // Clk Pseudocode
  174. // Bit 7:
  175. "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
  176. "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo
  177. "out %[port] , %[n1]" "\n\t" // 1 PORT = n1
  178. "rjmp .+0" "\n\t" // 2 nop nop
  179. "sbrc %[byte] , 6" "\n\t" // 1-2 if(b & 0x40)
  180. "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi
  181. "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
  182. "rjmp .+0" "\n\t" // 2 nop nop
  183. // Bit 6:
  184. "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
  185. "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo
  186. "out %[port] , %[n2]" "\n\t" // 1 PORT = n2
  187. "rjmp .+0" "\n\t" // 2 nop nop
  188. "sbrc %[byte] , 5" "\n\t" // 1-2 if(b & 0x20)
  189. "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi
  190. "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
  191. "rjmp .+0" "\n\t" // 2 nop nop
  192. // Bit 5:
  193. "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
  194. "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo
  195. "out %[port] , %[n1]" "\n\t" // 1 PORT = n1
  196. "rjmp .+0" "\n\t" // 2 nop nop
  197. "sbrc %[byte] , 4" "\n\t" // 1-2 if(b & 0x10)
  198. "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi
  199. "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
  200. "rjmp .+0" "\n\t" // 2 nop nop
  201. // Bit 4:
  202. "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
  203. "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo
  204. "out %[port] , %[n2]" "\n\t" // 1 PORT = n2
  205. "rjmp .+0" "\n\t" // 2 nop nop
  206. "sbrc %[byte] , 3" "\n\t" // 1-2 if(b & 0x08)
  207. "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi
  208. "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
  209. "rjmp .+0" "\n\t" // 2 nop nop
  210. // Bit 3:
  211. "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
  212. "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo
  213. "out %[port] , %[n1]" "\n\t" // 1 PORT = n1
  214. "rjmp .+0" "\n\t" // 2 nop nop
  215. "sbrc %[byte] , 2" "\n\t" // 1-2 if(b & 0x04)
  216. "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi
  217. "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
  218. "rjmp .+0" "\n\t" // 2 nop nop
  219. // Bit 2:
  220. "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
  221. "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo
  222. "out %[port] , %[n2]" "\n\t" // 1 PORT = n2
  223. "rjmp .+0" "\n\t" // 2 nop nop
  224. "sbrc %[byte] , 1" "\n\t" // 1-2 if(b & 0x02)
  225. "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi
  226. "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
  227. "rjmp .+0" "\n\t" // 2 nop nop
  228. // Bit 1:
  229. "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
  230. "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo
  231. "out %[port] , %[n1]" "\n\t" // 1 PORT = n1
  232. "rjmp .+0" "\n\t" // 2 nop nop
  233. "sbrc %[byte] , 0" "\n\t" // 1-2 if(b & 0x01)
  234. "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi
  235. "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
  236. "sbiw %[count], 1" "\n\t" // 2 i-- (don't act on Z flag yet)
  237. // Bit 0:
  238. "out %[port] , %[hi]" "\n\t" // 1 PORT = hi
  239. "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo
  240. "out %[port] , %[n2]" "\n\t" // 1 PORT = n2
  241. "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++
  242. "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 0x80)
  243. "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi
  244. "out %[port] , %[lo]" "\n\t" // 1 PORT = lo
  245. "brne headD" "\n" // 2 while(i) (Z flag set above)
  246. : [byte] "+r" (b),
  247. [n1] "+r" (n1),
  248. [n2] "+r" (n2),
  249. [count] "+w" (i)
  250. : [port] "I" (_SFR_IO_ADDR(PORTD)),
  251. [ptr] "e" (ptr),
  252. [hi] "r" (hi),
  253. [lo] "r" (lo));
  254. #if defined(PORTB) || defined(PORTC) || defined(PORTF)
  255. } else // other PORT(s)
  256. #endif // defined(PORTB/C/F)
  257. #endif // defined(PORTD)
  258. // PORTB OUTPUT ----------------------------------------------------
  259. #if defined(PORTB)
  260. #if defined(PORTD) || defined(PORTC) || defined(PORTF)
  261. if(port == &PORTB) {
  262. #endif // defined(PORTD/C/F)
  263. // Same as above, just switched to PORTB and stripped of comments.
  264. hi = PORTB | pinMask;
  265. lo = PORTB & ~pinMask;
  266. n1 = lo;
  267. if(b & 0x80) n1 = hi;
  268. asm volatile(
  269. "headB:" "\n\t"
  270. "out %[port] , %[hi]" "\n\t"
  271. "mov %[n2] , %[lo]" "\n\t"
  272. "out %[port] , %[n1]" "\n\t"
  273. "rjmp .+0" "\n\t"
  274. "sbrc %[byte] , 6" "\n\t"
  275. "mov %[n2] , %[hi]" "\n\t"
  276. "out %[port] , %[lo]" "\n\t"
  277. "rjmp .+0" "\n\t"
  278. "out %[port] , %[hi]" "\n\t"
  279. "mov %[n1] , %[lo]" "\n\t"
  280. "out %[port] , %[n2]" "\n\t"
  281. "rjmp .+0" "\n\t"
  282. "sbrc %[byte] , 5" "\n\t"
  283. "mov %[n1] , %[hi]" "\n\t"
  284. "out %[port] , %[lo]" "\n\t"
  285. "rjmp .+0" "\n\t"
  286. "out %[port] , %[hi]" "\n\t"
  287. "mov %[n2] , %[lo]" "\n\t"
  288. "out %[port] , %[n1]" "\n\t"
  289. "rjmp .+0" "\n\t"
  290. "sbrc %[byte] , 4" "\n\t"
  291. "mov %[n2] , %[hi]" "\n\t"
  292. "out %[port] , %[lo]" "\n\t"
  293. "rjmp .+0" "\n\t"
  294. "out %[port] , %[hi]" "\n\t"
  295. "mov %[n1] , %[lo]" "\n\t"
  296. "out %[port] , %[n2]" "\n\t"
  297. "rjmp .+0" "\n\t"
  298. "sbrc %[byte] , 3" "\n\t"
  299. "mov %[n1] , %[hi]" "\n\t"
  300. "out %[port] , %[lo]" "\n\t"
  301. "rjmp .+0" "\n\t"
  302. "out %[port] , %[hi]" "\n\t"
  303. "mov %[n2] , %[lo]" "\n\t"
  304. "out %[port] , %[n1]" "\n\t"
  305. "rjmp .+0" "\n\t"
  306. "sbrc %[byte] , 2" "\n\t"
  307. "mov %[n2] , %[hi]" "\n\t"
  308. "out %[port] , %[lo]" "\n\t"
  309. "rjmp .+0" "\n\t"
  310. "out %[port] , %[hi]" "\n\t"
  311. "mov %[n1] , %[lo]" "\n\t"
  312. "out %[port] , %[n2]" "\n\t"
  313. "rjmp .+0" "\n\t"
  314. "sbrc %[byte] , 1" "\n\t"
  315. "mov %[n1] , %[hi]" "\n\t"
  316. "out %[port] , %[lo]" "\n\t"
  317. "rjmp .+0" "\n\t"
  318. "out %[port] , %[hi]" "\n\t"
  319. "mov %[n2] , %[lo]" "\n\t"
  320. "out %[port] , %[n1]" "\n\t"
  321. "rjmp .+0" "\n\t"
  322. "sbrc %[byte] , 0" "\n\t"
  323. "mov %[n2] , %[hi]" "\n\t"
  324. "out %[port] , %[lo]" "\n\t"
  325. "sbiw %[count], 1" "\n\t"
  326. "out %[port] , %[hi]" "\n\t"
  327. "mov %[n1] , %[lo]" "\n\t"
  328. "out %[port] , %[n2]" "\n\t"
  329. "ld %[byte] , %a[ptr]+" "\n\t"
  330. "sbrc %[byte] , 7" "\n\t"
  331. "mov %[n1] , %[hi]" "\n\t"
  332. "out %[port] , %[lo]" "\n\t"
  333. "brne headB" "\n"
  334. : [byte] "+r" (b), [n1] "+r" (n1), [n2] "+r" (n2), [count] "+w" (i)
  335. : [port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi),
  336. [lo] "r" (lo));
  337. #if defined(PORTD) || defined(PORTC) || defined(PORTF)
  338. }
  339. #endif
  340. #if defined(PORTC) || defined(PORTF)
  341. else
  342. #endif // defined(PORTC/F)
  343. #endif // defined(PORTB)
  344. // PORTC OUTPUT ----------------------------------------------------
  345. #if defined(PORTC)
  346. #if defined(PORTD) || defined(PORTB) || defined(PORTF)
  347. if(port == &PORTC) {
  348. #endif // defined(PORTD/B/F)
  349. // Same as above, just switched to PORTC and stripped of comments.
  350. hi = PORTC | pinMask;
  351. lo = PORTC & ~pinMask;
  352. n1 = lo;
  353. if(b & 0x80) n1 = hi;
  354. asm volatile(
  355. "headC:" "\n\t"
  356. "out %[port] , %[hi]" "\n\t"
  357. "mov %[n2] , %[lo]" "\n\t"
  358. "out %[port] , %[n1]" "\n\t"
  359. "rjmp .+0" "\n\t"
  360. "sbrc %[byte] , 6" "\n\t"
  361. "mov %[n2] , %[hi]" "\n\t"
  362. "out %[port] , %[lo]" "\n\t"
  363. "rjmp .+0" "\n\t"
  364. "out %[port] , %[hi]" "\n\t"
  365. "mov %[n1] , %[lo]" "\n\t"
  366. "out %[port] , %[n2]" "\n\t"
  367. "rjmp .+0" "\n\t"
  368. "sbrc %[byte] , 5" "\n\t"
  369. "mov %[n1] , %[hi]" "\n\t"
  370. "out %[port] , %[lo]" "\n\t"
  371. "rjmp .+0" "\n\t"
  372. "out %[port] , %[hi]" "\n\t"
  373. "mov %[n2] , %[lo]" "\n\t"
  374. "out %[port] , %[n1]" "\n\t"
  375. "rjmp .+0" "\n\t"
  376. "sbrc %[byte] , 4" "\n\t"
  377. "mov %[n2] , %[hi]" "\n\t"
  378. "out %[port] , %[lo]" "\n\t"
  379. "rjmp .+0" "\n\t"
  380. "out %[port] , %[hi]" "\n\t"
  381. "mov %[n1] , %[lo]" "\n\t"
  382. "out %[port] , %[n2]" "\n\t"
  383. "rjmp .+0" "\n\t"
  384. "sbrc %[byte] , 3" "\n\t"
  385. "mov %[n1] , %[hi]" "\n\t"
  386. "out %[port] , %[lo]" "\n\t"
  387. "rjmp .+0" "\n\t"
  388. "out %[port] , %[hi]" "\n\t"
  389. "mov %[n2] , %[lo]" "\n\t"
  390. "out %[port] , %[n1]" "\n\t"
  391. "rjmp .+0" "\n\t"
  392. "sbrc %[byte] , 2" "\n\t"
  393. "mov %[n2] , %[hi]" "\n\t"
  394. "out %[port] , %[lo]" "\n\t"
  395. "rjmp .+0" "\n\t"
  396. "out %[port] , %[hi]" "\n\t"
  397. "mov %[n1] , %[lo]" "\n\t"
  398. "out %[port] , %[n2]" "\n\t"
  399. "rjmp .+0" "\n\t"
  400. "sbrc %[byte] , 1" "\n\t"
  401. "mov %[n1] , %[hi]" "\n\t"
  402. "out %[port] , %[lo]" "\n\t"
  403. "rjmp .+0" "\n\t"
  404. "out %[port] , %[hi]" "\n\t"
  405. "mov %[n2] , %[lo]" "\n\t"
  406. "out %[port] , %[n1]" "\n\t"
  407. "rjmp .+0" "\n\t"
  408. "sbrc %[byte] , 0" "\n\t"
  409. "mov %[n2] , %[hi]" "\n\t"
  410. "out %[port] , %[lo]" "\n\t"
  411. "sbiw %[count], 1" "\n\t"
  412. "out %[port] , %[hi]" "\n\t"
  413. "mov %[n1] , %[lo]" "\n\t"
  414. "out %[port] , %[n2]" "\n\t"
  415. "ld %[byte] , %a[ptr]+" "\n\t"
  416. "sbrc %[byte] , 7" "\n\t"
  417. "mov %[n1] , %[hi]" "\n\t"
  418. "out %[port] , %[lo]" "\n\t"
  419. "brne headC" "\n"
  420. : [byte] "+r" (b), [n1] "+r" (n1), [n2] "+r" (n2), [count] "+w" (i)
  421. : [port] "I" (_SFR_IO_ADDR(PORTC)), [ptr] "e" (ptr), [hi] "r" (hi),
  422. [lo] "r" (lo));
  423. #if defined(PORTD) || defined(PORTB) || defined(PORTF)
  424. }
  425. #endif // defined(PORTD/B/F)
  426. #if defined(PORTF)
  427. else
  428. #endif
  429. #endif // defined(PORTC)
  430. // PORTF OUTPUT ----------------------------------------------------
  431. #if defined(PORTF)
  432. #if defined(PORTD) || defined(PORTB) || defined(PORTC)
  433. if(port == &PORTF) {
  434. #endif // defined(PORTD/B/C)
  435. hi = PORTF | pinMask;
  436. lo = PORTF & ~pinMask;
  437. n1 = lo;
  438. if(b & 0x80) n1 = hi;
  439. asm volatile(
  440. "headF:" "\n\t"
  441. "out %[port] , %[hi]" "\n\t"
  442. "mov %[n2] , %[lo]" "\n\t"
  443. "out %[port] , %[n1]" "\n\t"
  444. "rjmp .+0" "\n\t"
  445. "sbrc %[byte] , 6" "\n\t"
  446. "mov %[n2] , %[hi]" "\n\t"
  447. "out %[port] , %[lo]" "\n\t"
  448. "rjmp .+0" "\n\t"
  449. "out %[port] , %[hi]" "\n\t"
  450. "mov %[n1] , %[lo]" "\n\t"
  451. "out %[port] , %[n2]" "\n\t"
  452. "rjmp .+0" "\n\t"
  453. "sbrc %[byte] , 5" "\n\t"
  454. "mov %[n1] , %[hi]" "\n\t"
  455. "out %[port] , %[lo]" "\n\t"
  456. "rjmp .+0" "\n\t"
  457. "out %[port] , %[hi]" "\n\t"
  458. "mov %[n2] , %[lo]" "\n\t"
  459. "out %[port] , %[n1]" "\n\t"
  460. "rjmp .+0" "\n\t"
  461. "sbrc %[byte] , 4" "\n\t"
  462. "mov %[n2] , %[hi]" "\n\t"
  463. "out %[port] , %[lo]" "\n\t"
  464. "rjmp .+0" "\n\t"
  465. "out %[port] , %[hi]" "\n\t"
  466. "mov %[n1] , %[lo]" "\n\t"
  467. "out %[port] , %[n2]" "\n\t"
  468. "rjmp .+0" "\n\t"
  469. "sbrc %[byte] , 3" "\n\t"
  470. "mov %[n1] , %[hi]" "\n\t"
  471. "out %[port] , %[lo]" "\n\t"
  472. "rjmp .+0" "\n\t"
  473. "out %[port] , %[hi]" "\n\t"
  474. "mov %[n2] , %[lo]" "\n\t"
  475. "out %[port] , %[n1]" "\n\t"
  476. "rjmp .+0" "\n\t"
  477. "sbrc %[byte] , 2" "\n\t"
  478. "mov %[n2] , %[hi]" "\n\t"
  479. "out %[port] , %[lo]" "\n\t"
  480. "rjmp .+0" "\n\t"
  481. "out %[port] , %[hi]" "\n\t"
  482. "mov %[n1] , %[lo]" "\n\t"
  483. "out %[port] , %[n2]" "\n\t"
  484. "rjmp .+0" "\n\t"
  485. "sbrc %[byte] , 1" "\n\t"
  486. "mov %[n1] , %[hi]" "\n\t"
  487. "out %[port] , %[lo]" "\n\t"
  488. "rjmp .+0" "\n\t"
  489. "out %[port] , %[hi]" "\n\t"
  490. "mov %[n2] , %[lo]" "\n\t"
  491. "out %[port] , %[n1]" "\n\t"
  492. "rjmp .+0" "\n\t"
  493. "sbrc %[byte] , 0" "\n\t"
  494. "mov %[n2] , %[hi]" "\n\t"
  495. "out %[port] , %[lo]" "\n\t"
  496. "sbiw %[count], 1" "\n\t"
  497. "out %[port] , %[hi]" "\n\t"
  498. "mov %[n1] , %[lo]" "\n\t"
  499. "out %[port] , %[n2]" "\n\t"
  500. "ld %[byte] , %a[ptr]+" "\n\t"
  501. "sbrc %[byte] , 7" "\n\t"
  502. "mov %[n1] , %[hi]" "\n\t"
  503. "out %[port] , %[lo]" "\n\t"
  504. "brne headF" "\n"
  505. : [byte] "+r" (b), [n1] "+r" (n1), [n2] "+r" (n2), [count] "+w" (i)
  506. : [port] "I" (_SFR_IO_ADDR(PORTF)), [ptr] "e" (ptr), [hi] "r" (hi),
  507. [lo] "r" (lo));
  508. #if defined(PORTD) || defined(PORTB) || defined(PORTC)
  509. }
  510. #endif // defined(PORTD/B/C)
  511. #endif // defined(PORTF)
  512. #ifdef NEO_KHZ400
  513. } else { // end 800 KHz, do 400 KHz
  514. // Timing is more relaxed; unrolling the inner loop for each bit is
  515. // not necessary. Still using the peculiar RJMPs as 2X NOPs, not out
  516. // of need but just to trim the code size down a little.
  517. // This 400-KHz-datastream-on-8-MHz-CPU code is not quite identical
  518. // to the 800-on-16 code later -- the hi/lo timing between WS2811 and
  519. // WS2812 is not simply a 2:1 scale!
  520. // 20 inst. clocks per bit: HHHHxxxxxxLLLLLLLLLL
  521. // ST instructions: ^ ^ ^ (T=0,4,10)
  522. volatile uint8_t next, bit;
  523. hi = *port | pinMask;
  524. lo = *port & ~pinMask;
  525. next = lo;
  526. bit = 8;
  527. asm volatile(
  528. "head20:" "\n\t" // Clk Pseudocode (T = 0)
  529. "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2)
  530. "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128)
  531. "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4)
  532. "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 6)
  533. "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7)
  534. "dec %[bit]" "\n\t" // 1 bit-- (T = 8)
  535. "breq nextbyte20" "\n\t" // 1-2 if(bit == 0)
  536. "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 10)
  537. "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 12)
  538. "rjmp .+0" "\n\t" // 2 nop nop (T = 14)
  539. "rjmp .+0" "\n\t" // 2 nop nop (T = 16)
  540. "rjmp .+0" "\n\t" // 2 nop nop (T = 18)
  541. "rjmp head20" "\n\t" // 2 -> head20 (next bit out)
  542. "nextbyte20:" "\n\t" // (T = 10)
  543. "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 12)
  544. "nop" "\n\t" // 1 nop (T = 13)
  545. "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 14)
  546. "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 16)
  547. "sbiw %[count], 1" "\n\t" // 2 i-- (T = 18)
  548. "brne head20" "\n" // 2 if(i != 0) -> (next byte)
  549. : [port] "+e" (port),
  550. [byte] "+r" (b),
  551. [bit] "+r" (bit),
  552. [next] "+r" (next),
  553. [count] "+w" (i)
  554. : [hi] "r" (hi),
  555. [lo] "r" (lo),
  556. [ptr] "e" (ptr));
  557. }
  558. #endif // NEO_KHZ400
  559. // 12 MHz(ish) AVR --------------------------------------------------------
  560. #elif (F_CPU >= 11100000UL) && (F_CPU <= 14300000UL)
  561. #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
  562. if(is800KHz) {
  563. #endif
  564. // In the 12 MHz case, an optimized 800 KHz datastream (no dead time
  565. // between bytes) requires a PORT-specific loop similar to the 8 MHz
  566. // code (but a little more relaxed in this case).
  567. // 15 instruction clocks per bit: HHHHxxxxxxLLLLL
  568. // OUT instructions: ^ ^ ^ (T=0,4,10)
  569. volatile uint8_t next;
  570. // PORTD OUTPUT ----------------------------------------------------
  571. #if defined(PORTD)
  572. #if defined(PORTB) || defined(PORTC) || defined(PORTF)
  573. if(port == &PORTD) {
  574. #endif // defined(PORTB/C/F)
  575. hi = PORTD | pinMask;
  576. lo = PORTD & ~pinMask;
  577. next = lo;
  578. if(b & 0x80) next = hi;
  579. // Don't "optimize" the OUT calls into the bitTime subroutine;
  580. // we're exploiting the RCALL and RET as 3- and 4-cycle NOPs!
  581. asm volatile(
  582. "headD:" "\n\t" // (T = 0)
  583. "out %[port], %[hi]" "\n\t" // (T = 1)
  584. "rcall bitTimeD" "\n\t" // Bit 7 (T = 15)
  585. "out %[port], %[hi]" "\n\t"
  586. "rcall bitTimeD" "\n\t" // Bit 6
  587. "out %[port], %[hi]" "\n\t"
  588. "rcall bitTimeD" "\n\t" // Bit 5
  589. "out %[port], %[hi]" "\n\t"
  590. "rcall bitTimeD" "\n\t" // Bit 4
  591. "out %[port], %[hi]" "\n\t"
  592. "rcall bitTimeD" "\n\t" // Bit 3
  593. "out %[port], %[hi]" "\n\t"
  594. "rcall bitTimeD" "\n\t" // Bit 2
  595. "out %[port], %[hi]" "\n\t"
  596. "rcall bitTimeD" "\n\t" // Bit 1
  597. // Bit 0:
  598. "out %[port] , %[hi]" "\n\t" // 1 PORT = hi (T = 1)
  599. "rjmp .+0" "\n\t" // 2 nop nop (T = 3)
  600. "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 5)
  601. "out %[port] , %[next]" "\n\t" // 1 PORT = next (T = 6)
  602. "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7)
  603. "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 0x80) (T = 8)
  604. "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 9)
  605. "nop" "\n\t" // 1 (T = 10)
  606. "out %[port] , %[lo]" "\n\t" // 1 PORT = lo (T = 11)
  607. "sbiw %[count], 1" "\n\t" // 2 i-- (T = 13)
  608. "brne headD" "\n\t" // 2 if(i != 0) -> (next byte)
  609. "rjmp doneD" "\n\t"
  610. "bitTimeD:" "\n\t" // nop nop nop (T = 4)
  611. "out %[port], %[next]" "\n\t" // 1 PORT = next (T = 5)
  612. "mov %[next], %[lo]" "\n\t" // 1 next = lo (T = 6)
  613. "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 7)
  614. "sbrc %[byte], 7" "\n\t" // 1-2 if(b & 0x80) (T = 8)
  615. "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 9)
  616. "nop" "\n\t" // 1 (T = 10)
  617. "out %[port], %[lo]" "\n\t" // 1 PORT = lo (T = 11)
  618. "ret" "\n\t" // 4 nop nop nop nop (T = 15)
  619. "doneD:" "\n"
  620. : [byte] "+r" (b),
  621. [next] "+r" (next),
  622. [count] "+w" (i)
  623. : [port] "I" (_SFR_IO_ADDR(PORTD)),
  624. [ptr] "e" (ptr),
  625. [hi] "r" (hi),
  626. [lo] "r" (lo));
  627. #if defined(PORTB) || defined(PORTC) || defined(PORTF)
  628. } else // other PORT(s)
  629. #endif // defined(PORTB/C/F)
  630. #endif // defined(PORTD)
  631. // PORTB OUTPUT ----------------------------------------------------
  632. #if defined(PORTB)
  633. #if defined(PORTD) || defined(PORTC) || defined(PORTF)
  634. if(port == &PORTB) {
  635. #endif // defined(PORTD/C/F)
  636. hi = PORTB | pinMask;
  637. lo = PORTB & ~pinMask;
  638. next = lo;
  639. if(b & 0x80) next = hi;
  640. // Same as above, just set for PORTB & stripped of comments
  641. asm volatile(
  642. "headB:" "\n\t"
  643. "out %[port], %[hi]" "\n\t"
  644. "rcall bitTimeB" "\n\t"
  645. "out %[port], %[hi]" "\n\t"
  646. "rcall bitTimeB" "\n\t"
  647. "out %[port], %[hi]" "\n\t"
  648. "rcall bitTimeB" "\n\t"
  649. "out %[port], %[hi]" "\n\t"
  650. "rcall bitTimeB" "\n\t"
  651. "out %[port], %[hi]" "\n\t"
  652. "rcall bitTimeB" "\n\t"
  653. "out %[port], %[hi]" "\n\t"
  654. "rcall bitTimeB" "\n\t"
  655. "out %[port], %[hi]" "\n\t"
  656. "rcall bitTimeB" "\n\t"
  657. "out %[port] , %[hi]" "\n\t"
  658. "rjmp .+0" "\n\t"
  659. "ld %[byte] , %a[ptr]+" "\n\t"
  660. "out %[port] , %[next]" "\n\t"
  661. "mov %[next] , %[lo]" "\n\t"
  662. "sbrc %[byte] , 7" "\n\t"
  663. "mov %[next] , %[hi]" "\n\t"
  664. "nop" "\n\t"
  665. "out %[port] , %[lo]" "\n\t"
  666. "sbiw %[count], 1" "\n\t"
  667. "brne headB" "\n\t"
  668. "rjmp doneB" "\n\t"
  669. "bitTimeB:" "\n\t"
  670. "out %[port], %[next]" "\n\t"
  671. "mov %[next], %[lo]" "\n\t"
  672. "rol %[byte]" "\n\t"
  673. "sbrc %[byte], 7" "\n\t"
  674. "mov %[next], %[hi]" "\n\t"
  675. "nop" "\n\t"
  676. "out %[port], %[lo]" "\n\t"
  677. "ret" "\n\t"
  678. "doneB:" "\n"
  679. : [byte] "+r" (b), [next] "+r" (next), [count] "+w" (i)
  680. : [port] "I" (_SFR_IO_ADDR(PORTB)), [ptr] "e" (ptr), [hi] "r" (hi),
  681. [lo] "r" (lo));
  682. #if defined(PORTD) || defined(PORTC) || defined(PORTF)
  683. }
  684. #endif
  685. #if defined(PORTC) || defined(PORTF)
  686. else
  687. #endif // defined(PORTC/F)
  688. #endif // defined(PORTB)
  689. // PORTC OUTPUT ----------------------------------------------------
  690. #if defined(PORTC)
  691. #if defined(PORTD) || defined(PORTB) || defined(PORTF)
  692. if(port == &PORTC) {
  693. #endif // defined(PORTD/B/F)
  694. hi = PORTC | pinMask;
  695. lo = PORTC & ~pinMask;
  696. next = lo;
  697. if(b & 0x80) next = hi;
  698. // Same as above, just set for PORTC & stripped of comments
  699. asm volatile(
  700. "headC:" "\n\t"
  701. "out %[port], %[hi]" "\n\t"
  702. "rcall bitTimeC" "\n\t"
  703. "out %[port], %[hi]" "\n\t"
  704. "rcall bitTimeC" "\n\t"
  705. "out %[port], %[hi]" "\n\t"
  706. "rcall bitTimeC" "\n\t"
  707. "out %[port], %[hi]" "\n\t"
  708. "rcall bitTimeC" "\n\t"
  709. "out %[port], %[hi]" "\n\t"
  710. "rcall bitTimeC" "\n\t"
  711. "out %[port], %[hi]" "\n\t"
  712. "rcall bitTimeC" "\n\t"
  713. "out %[port], %[hi]" "\n\t"
  714. "rcall bitTimeC" "\n\t"
  715. "out %[port] , %[hi]" "\n\t"
  716. "rjmp .+0" "\n\t"
  717. "ld %[byte] , %a[ptr]+" "\n\t"
  718. "out %[port] , %[next]" "\n\t"
  719. "mov %[next] , %[lo]" "\n\t"
  720. "sbrc %[byte] , 7" "\n\t"
  721. "mov %[next] , %[hi]" "\n\t"
  722. "nop" "\n\t"
  723. "out %[port] , %[lo]" "\n\t"
  724. "sbiw %[count], 1" "\n\t"
  725. "brne headC" "\n\t"
  726. "rjmp doneC" "\n\t"
  727. "bitTimeC:" "\n\t"
  728. "out %[port], %[next]" "\n\t"
  729. "mov %[next], %[lo]" "\n\t"
  730. "rol %[byte]" "\n\t"
  731. "sbrc %[byte], 7" "\n\t"
  732. "mov %[next], %[hi]" "\n\t"
  733. "nop" "\n\t"
  734. "out %[port], %[lo]" "\n\t"
  735. "ret" "\n\t"
  736. "doneC:" "\n"
  737. : [byte] "+r" (b), [next] "+r" (next), [count] "+w" (i)
  738. : [port] "I" (_SFR_IO_ADDR(PORTC)), [ptr] "e" (ptr), [hi] "r" (hi),
  739. [lo] "r" (lo));
  740. #if defined(PORTD) || defined(PORTB) || defined(PORTF)
  741. }
  742. #endif // defined(PORTD/B/F)
  743. #if defined(PORTF)
  744. else
  745. #endif
  746. #endif // defined(PORTC)
  747. // PORTF OUTPUT ----------------------------------------------------
  748. #if defined(PORTF)
  749. #if defined(PORTD) || defined(PORTB) || defined(PORTC)
  750. if(port == &PORTF) {
  751. #endif // defined(PORTD/B/C)
  752. hi = PORTF | pinMask;
  753. lo = PORTF & ~pinMask;
  754. next = lo;
  755. if(b & 0x80) next = hi;
  756. // Same as above, just set for PORTF & stripped of comments
  757. asm volatile(
  758. "headF:" "\n\t"
  759. "out %[port], %[hi]" "\n\t"
  760. "rcall bitTimeC" "\n\t"
  761. "out %[port], %[hi]" "\n\t"
  762. "rcall bitTimeC" "\n\t"
  763. "out %[port], %[hi]" "\n\t"
  764. "rcall bitTimeC" "\n\t"
  765. "out %[port], %[hi]" "\n\t"
  766. "rcall bitTimeC" "\n\t"
  767. "out %[port], %[hi]" "\n\t"
  768. "rcall bitTimeC" "\n\t"
  769. "out %[port], %[hi]" "\n\t"
  770. "rcall bitTimeC" "\n\t"
  771. "out %[port], %[hi]" "\n\t"
  772. "rcall bitTimeC" "\n\t"
  773. "out %[port] , %[hi]" "\n\t"
  774. "rjmp .+0" "\n\t"
  775. "ld %[byte] , %a[ptr]+" "\n\t"
  776. "out %[port] , %[next]" "\n\t"
  777. "mov %[next] , %[lo]" "\n\t"
  778. "sbrc %[byte] , 7" "\n\t"
  779. "mov %[next] , %[hi]" "\n\t"
  780. "nop" "\n\t"
  781. "out %[port] , %[lo]" "\n\t"
  782. "sbiw %[count], 1" "\n\t"
  783. "brne headF" "\n\t"
  784. "rjmp doneC" "\n\t"
  785. "bitTimeC:" "\n\t"
  786. "out %[port], %[next]" "\n\t"
  787. "mov %[next], %[lo]" "\n\t"
  788. "rol %[byte]" "\n\t"
  789. "sbrc %[byte], 7" "\n\t"
  790. "mov %[next], %[hi]" "\n\t"
  791. "nop" "\n\t"
  792. "out %[port], %[lo]" "\n\t"
  793. "ret" "\n\t"
  794. "doneC:" "\n"
  795. : [byte] "+r" (b), [next] "+r" (next), [count] "+w" (i)
  796. : [port] "I" (_SFR_IO_ADDR(PORTF)), [ptr] "e" (ptr), [hi] "r" (hi),
  797. [lo] "r" (lo));
  798. #if defined(PORTD) || defined(PORTB) || defined(PORTC)
  799. }
  800. #endif // defined(PORTD/B/C)
  801. #endif // defined(PORTF)
  802. #ifdef NEO_KHZ400
  803. } else { // 400 KHz
  804. // 30 instruction clocks per bit: HHHHHHxxxxxxxxxLLLLLLLLLLLLLLL
  805. // ST instructions: ^ ^ ^ (T=0,6,15)
  806. volatile uint8_t next, bit;
  807. hi = *port | pinMask;
  808. lo = *port & ~pinMask;
  809. next = lo;
  810. bit = 8;
  811. asm volatile(
  812. "head30:" "\n\t" // Clk Pseudocode (T = 0)
  813. "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2)
  814. "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128)
  815. "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4)
  816. "rjmp .+0" "\n\t" // 2 nop nop (T = 6)
  817. "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 8)
  818. "rjmp .+0" "\n\t" // 2 nop nop (T = 10)
  819. "rjmp .+0" "\n\t" // 2 nop nop (T = 12)
  820. "rjmp .+0" "\n\t" // 2 nop nop (T = 14)
  821. "nop" "\n\t" // 1 nop (T = 15)
  822. "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 17)
  823. "rjmp .+0" "\n\t" // 2 nop nop (T = 19)
  824. "dec %[bit]" "\n\t" // 1 bit-- (T = 20)
  825. "breq nextbyte30" "\n\t" // 1-2 if(bit == 0)
  826. "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 22)
  827. "rjmp .+0" "\n\t" // 2 nop nop (T = 24)
  828. "rjmp .+0" "\n\t" // 2 nop nop (T = 26)
  829. "rjmp .+0" "\n\t" // 2 nop nop (T = 28)
  830. "rjmp head30" "\n\t" // 2 -> head30 (next bit out)
  831. "nextbyte30:" "\n\t" // (T = 22)
  832. "nop" "\n\t" // 1 nop (T = 23)
  833. "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 24)
  834. "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 26)
  835. "sbiw %[count], 1" "\n\t" // 2 i-- (T = 28)
  836. "brne head30" "\n" // 1-2 if(i != 0) -> (next byte)
  837. : [port] "+e" (port),
  838. [byte] "+r" (b),
  839. [bit] "+r" (bit),
  840. [next] "+r" (next),
  841. [count] "+w" (i)
  842. : [hi] "r" (hi),
  843. [lo] "r" (lo),
  844. [ptr] "e" (ptr));
  845. }
  846. #endif // NEO_KHZ400
  847. // 16 MHz(ish) AVR --------------------------------------------------------
  848. #elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L)
  849. #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
  850. if(is800KHz) {
  851. #endif
  852. // WS2811 and WS2812 have different hi/lo duty cycles; this is
  853. // similar but NOT an exact copy of the prior 400-on-8 code.
  854. // 20 inst. clocks per bit: HHHHHxxxxxxxxLLLLLLL
  855. // ST instructions: ^ ^ ^ (T=0,5,13)
  856. volatile uint8_t next, bit;
  857. hi = *port | pinMask;
  858. lo = *port & ~pinMask;
  859. next = lo;
  860. bit = 8;
  861. asm volatile(
  862. "head20:" "\n\t" // Clk Pseudocode (T = 0)
  863. "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2)
  864. "sbrc %[byte], 7" "\n\t" // 1-2 if(b & 128)
  865. "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4)
  866. "dec %[bit]" "\n\t" // 1 bit-- (T = 5)
  867. "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 7)
  868. "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 8)
  869. "breq nextbyte20" "\n\t" // 1-2 if(bit == 0) (from dec above)
  870. "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 10)
  871. "rjmp .+0" "\n\t" // 2 nop nop (T = 12)
  872. "nop" "\n\t" // 1 nop (T = 13)
  873. "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 15)
  874. "nop" "\n\t" // 1 nop (T = 16)
  875. "rjmp .+0" "\n\t" // 2 nop nop (T = 18)
  876. "rjmp head20" "\n\t" // 2 -> head20 (next bit out)
  877. "nextbyte20:" "\n\t" // (T = 10)
  878. "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 11)
  879. "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 13)
  880. "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 15)
  881. "nop" "\n\t" // 1 nop (T = 16)
  882. "sbiw %[count], 1" "\n\t" // 2 i-- (T = 18)
  883. "brne head20" "\n" // 2 if(i != 0) -> (next byte)
  884. : [port] "+e" (port),
  885. [byte] "+r" (b),
  886. [bit] "+r" (bit),
  887. [next] "+r" (next),
  888. [count] "+w" (i)
  889. : [ptr] "e" (ptr),
  890. [hi] "r" (hi),
  891. [lo] "r" (lo));
  892. #ifdef NEO_KHZ400
  893. } else { // 400 KHz
  894. // The 400 KHz clock on 16 MHz MCU is the most 'relaxed' version.
  895. // 40 inst. clocks per bit: HHHHHHHHxxxxxxxxxxxxLLLLLLLLLLLLLLLLLLLL
  896. // ST instructions: ^ ^ ^ (T=0,8,20)
  897. volatile uint8_t next, bit;
  898. hi = *port | pinMask;
  899. lo = *port & ~pinMask;
  900. next = lo;
  901. bit = 8;
  902. asm volatile(
  903. "head40:" "\n\t" // Clk Pseudocode (T = 0)
  904. "st %a[port], %[hi]" "\n\t" // 2 PORT = hi (T = 2)
  905. "sbrc %[byte] , 7" "\n\t" // 1-2 if(b & 128)
  906. "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 4)
  907. "rjmp .+0" "\n\t" // 2 nop nop (T = 6)
  908. "rjmp .+0" "\n\t" // 2 nop nop (T = 8)
  909. "st %a[port], %[next]" "\n\t" // 2 PORT = next (T = 10)
  910. "rjmp .+0" "\n\t" // 2 nop nop (T = 12)
  911. "rjmp .+0" "\n\t" // 2 nop nop (T = 14)
  912. "rjmp .+0" "\n\t" // 2 nop nop (T = 16)
  913. "rjmp .+0" "\n\t" // 2 nop nop (T = 18)
  914. "rjmp .+0" "\n\t" // 2 nop nop (T = 20)
  915. "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 22)
  916. "nop" "\n\t" // 1 nop (T = 23)
  917. "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 24)
  918. "dec %[bit]" "\n\t" // 1 bit-- (T = 25)
  919. "breq nextbyte40" "\n\t" // 1-2 if(bit == 0)
  920. "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 27)
  921. "nop" "\n\t" // 1 nop (T = 28)
  922. "rjmp .+0" "\n\t" // 2 nop nop (T = 30)
  923. "rjmp .+0" "\n\t" // 2 nop nop (T = 32)
  924. "rjmp .+0" "\n\t" // 2 nop nop (T = 34)
  925. "rjmp .+0" "\n\t" // 2 nop nop (T = 36)
  926. "rjmp .+0" "\n\t" // 2 nop nop (T = 38)
  927. "rjmp head40" "\n\t" // 2 -> head40 (next bit out)
  928. "nextbyte40:" "\n\t" // (T = 27)
  929. "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 28)
  930. "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 30)
  931. "rjmp .+0" "\n\t" // 2 nop nop (T = 32)
  932. "st %a[port], %[lo]" "\n\t" // 2 PORT = lo (T = 34)
  933. "rjmp .+0" "\n\t" // 2 nop nop (T = 36)
  934. "sbiw %[count], 1" "\n\t" // 2 i-- (T = 38)
  935. "brne head40" "\n" // 1-2 if(i != 0) -> (next byte)
  936. : [port] "+e" (port),
  937. [byte] "+r" (b),
  938. [bit] "+r" (bit),
  939. [next] "+r" (next),
  940. [count] "+w" (i)
  941. : [ptr] "e" (ptr),
  942. [hi] "r" (hi),
  943. [lo] "r" (lo));
  944. }
  945. #endif // NEO_KHZ400
  946. #else
  947. #error "CPU SPEED NOT SUPPORTED"
  948. #endif // end F_CPU ifdefs on __AVR__
  949. // END AVR ----------------------------------------------------------------
  950. #elif defined(__arm__)
  951. // ARM MCUs -- Teensy 3.0, 3.1, LC, Arduino Due ---------------------------
  952. #if defined(TEENSYDUINO) && defined(KINETISK) // Teensy 3.0, 3.1, 3.2, 3.5, 3.6
  953. #define CYCLES_800_T0H (F_CPU / 4000000)
  954. #define CYCLES_800_T1H (F_CPU / 1250000)
  955. #define CYCLES_800 (F_CPU / 800000)
  956. #define CYCLES_400_T0H (F_CPU / 2000000)
  957. #define CYCLES_400_T1H (F_CPU / 833333)
  958. #define CYCLES_400 (F_CPU / 400000)
  959. uint8_t *p = pixels,
  960. *end = p + numBytes, pix, mask;
  961. volatile uint8_t *set = portSetRegister(pin),
  962. *clr = portClearRegister(pin);
  963. uint32_t cyc;
  964. ARM_DEMCR |= ARM_DEMCR_TRCENA;
  965. ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
  966. #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
  967. if(is800KHz) {
  968. #endif
  969. cyc = ARM_DWT_CYCCNT + CYCLES_800;
  970. while(p < end) {
  971. pix = *p++;
  972. for(mask = 0x80; mask; mask >>= 1) {
  973. while(ARM_DWT_CYCCNT - cyc < CYCLES_800);
  974. cyc = ARM_DWT_CYCCNT;
  975. *set = 1;
  976. if(pix & mask) {
  977. while(ARM_DWT_CYCCNT - cyc < CYCLES_800_T1H);
  978. } else {
  979. while(ARM_DWT_CYCCNT - cyc < CYCLES_800_T0H);
  980. }
  981. *clr = 1;
  982. }
  983. }
  984. while(ARM_DWT_CYCCNT - cyc < CYCLES_800);
  985. #ifdef NEO_KHZ400
  986. } else { // 400 kHz bitstream
  987. cyc = ARM_DWT_CYCCNT + CYCLES_400;
  988. while(p < end) {
  989. pix = *p++;
  990. for(mask = 0x80; mask; mask >>= 1) {
  991. while(ARM_DWT_CYCCNT - cyc < CYCLES_400);
  992. cyc = ARM_DWT_CYCCNT;
  993. *set = 1;
  994. if(pix & mask) {
  995. while(ARM_DWT_CYCCNT - cyc < CYCLES_400_T1H);
  996. } else {
  997. while(ARM_DWT_CYCCNT - cyc < CYCLES_400_T0H);
  998. }
  999. *clr = 1;
  1000. }
  1001. }
  1002. while(ARM_DWT_CYCCNT - cyc < CYCLES_400);
  1003. }
  1004. #endif // NEO_KHZ400
  1005. #elif defined(TEENSYDUINO) && (defined(__IMXRT1052__) || defined(__IMXRT1062__))
  1006. #define CYCLES_800_T0H (F_CPU_ACTUAL / 4000000)
  1007. #define CYCLES_800_T1H (F_CPU_ACTUAL / 1250000)
  1008. #define CYCLES_800 (F_CPU_ACTUAL / 800000)
  1009. #define CYCLES_400_T0H (F_CPU_ACTUAL / 2000000)
  1010. #define CYCLES_400_T1H (F_CPU_ACTUAL / 833333)
  1011. #define CYCLES_400 (F_CPU_ACTUAL / 400000)
  1012. uint8_t *p = pixels,
  1013. *end = p + numBytes, pix, mask;
  1014. volatile uint32_t *set = portSetRegister(pin),
  1015. *clr = portClearRegister(pin);
  1016. uint32_t cyc,
  1017. msk = digitalPinToBitMask(pin);
  1018. ARM_DEMCR |= ARM_DEMCR_TRCENA;
  1019. ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
  1020. #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
  1021. if(is800KHz) {
  1022. #endif
  1023. cyc = ARM_DWT_CYCCNT + CYCLES_800;
  1024. while(p < end) {
  1025. pix = *p++;
  1026. for(mask = 0x80; mask; mask >>= 1) {
  1027. while(ARM_DWT_CYCCNT - cyc < CYCLES_800);
  1028. cyc = ARM_DWT_CYCCNT;
  1029. *set = msk;
  1030. if(pix & mask) {
  1031. while(ARM_DWT_CYCCNT - cyc < CYCLES_800_T1H);
  1032. } else {
  1033. while(ARM_DWT_CYCCNT - cyc < CYCLES_800_T0H);
  1034. }
  1035. *clr = msk;
  1036. }
  1037. }
  1038. while(ARM_DWT_CYCCNT - cyc < CYCLES_800);
  1039. #ifdef NEO_KHZ400
  1040. } else { // 400 kHz bitstream
  1041. cyc = ARM_DWT_CYCCNT + CYCLES_400;
  1042. while(p < end) {
  1043. pix = *p++;
  1044. for(mask = 0x80; mask; mask >>= 1) {
  1045. while(ARM_DWT_CYCCNT - cyc < CYCLES_400);
  1046. cyc = ARM_DWT_CYCCNT;
  1047. *set = msk;
  1048. if(pix & mask) {
  1049. while(ARM_DWT_CYCCNT - cyc < CYCLES_400_T1H);
  1050. } else {
  1051. while(ARM_DWT_CYCCNT - cyc < CYCLES_400_T0H);
  1052. }
  1053. *clr = msk;
  1054. }
  1055. }
  1056. while(ARM_DWT_CYCCNT - cyc < CYCLES_400);
  1057. }
  1058. #endif // NEO_KHZ400
  1059. #elif defined(TEENSYDUINO) && defined(__MKL26Z64__) // Teensy-LC
  1060. #if F_CPU == 48000000
  1061. uint8_t *p = pixels,
  1062. pix, count, dly,
  1063. bitmask = digitalPinToBitMask(pin);
  1064. volatile uint8_t *reg = portSetRegister(pin);
  1065. uint32_t num = numBytes;
  1066. asm volatile(
  1067. "L%=_begin:" "\n\t"
  1068. "ldrb %[pix], [%[p], #0]" "\n\t"
  1069. "lsl %[pix], #24" "\n\t"
  1070. "movs %[count], #7" "\n\t"
  1071. "L%=_loop:" "\n\t"
  1072. "lsl %[pix], #1" "\n\t"
  1073. "bcs L%=_loop_one" "\n\t"
  1074. "L%=_loop_zero:"
  1075. "strb %[bitmask], [%[reg], #0]" "\n\t"
  1076. "movs %[dly], #4" "\n\t"
  1077. "L%=_loop_delay_T0H:" "\n\t"
  1078. "sub %[dly], #1" "\n\t"
  1079. "bne L%=_loop_delay_T0H" "\n\t"
  1080. "strb %[bitmask], [%[reg], #4]" "\n\t"
  1081. "movs %[dly], #13" "\n\t"
  1082. "L%=_loop_delay_T0L:" "\n\t"
  1083. "sub %[dly], #1" "\n\t"
  1084. "bne L%=_loop_delay_T0L" "\n\t"
  1085. "b L%=_next" "\n\t"
  1086. "L%=_loop_one:"
  1087. "strb %[bitmask], [%[reg], #0]" "\n\t"
  1088. "movs %[dly], #13" "\n\t"
  1089. "L%=_loop_delay_T1H:" "\n\t"
  1090. "sub %[dly], #1" "\n\t"
  1091. "bne L%=_loop_delay_T1H" "\n\t"
  1092. "strb %[bitmask], [%[reg], #4]" "\n\t"
  1093. "movs %[dly], #4" "\n\t"
  1094. "L%=_loop_delay_T1L:" "\n\t"
  1095. "sub %[dly], #1" "\n\t"
  1096. "bne L%=_loop_delay_T1L" "\n\t"
  1097. "nop" "\n\t"
  1098. "L%=_next:" "\n\t"
  1099. "sub %[count], #1" "\n\t"
  1100. "bne L%=_loop" "\n\t"
  1101. "lsl %[pix], #1" "\n\t"
  1102. "bcs L%=_last_one" "\n\t"
  1103. "L%=_last_zero:"
  1104. "strb %[bitmask], [%[reg], #0]" "\n\t"
  1105. "movs %[dly], #4" "\n\t"
  1106. "L%=_last_delay_T0H:" "\n\t"
  1107. "sub %[dly], #1" "\n\t"
  1108. "bne L%=_last_delay_T0H" "\n\t"
  1109. "strb %[bitmask], [%[reg], #4]" "\n\t"
  1110. "movs %[dly], #10" "\n\t"
  1111. "L%=_last_delay_T0L:" "\n\t"
  1112. "sub %[dly], #1" "\n\t"
  1113. "bne L%=_last_delay_T0L" "\n\t"
  1114. "b L%=_repeat" "\n\t"
  1115. "L%=_last_one:"
  1116. "strb %[bitmask], [%[reg], #0]" "\n\t"
  1117. "movs %[dly], #13" "\n\t"
  1118. "L%=_last_delay_T1H:" "\n\t"
  1119. "sub %[dly], #1" "\n\t"
  1120. "bne L%=_last_delay_T1H" "\n\t"
  1121. "strb %[bitmask], [%[reg], #4]" "\n\t"
  1122. "movs %[dly], #1" "\n\t"
  1123. "L%=_last_delay_T1L:" "\n\t"
  1124. "sub %[dly], #1" "\n\t"
  1125. "bne L%=_last_delay_T1L" "\n\t"
  1126. "nop" "\n\t"
  1127. "L%=_repeat:" "\n\t"
  1128. "add %[p], #1" "\n\t"
  1129. "sub %[num], #1" "\n\t"
  1130. "bne L%=_begin" "\n\t"
  1131. "L%=_done:" "\n\t"
  1132. : [p] "+r" (p),
  1133. [pix] "=&r" (pix),
  1134. [count] "=&r" (count),
  1135. [dly] "=&r" (dly),
  1136. [num] "+r" (num)
  1137. : [bitmask] "r" (bitmask),
  1138. [reg] "r" (reg)
  1139. );
  1140. #else
  1141. #error "Sorry, only 48 MHz is supported, please set Tools > CPU Speed to 48 MHz"
  1142. #endif // F_CPU == 48000000
  1143. // Begin of support for nRF52 based boards -------------------------
  1144. #elif defined(NRF52) || defined(NRF52_SERIES)
  1145. // [[[Begin of the Neopixel NRF52 EasyDMA implementation
  1146. // by the Hackerspace San Salvador]]]
  1147. // This technique uses the PWM peripheral on the NRF52. The PWM uses the
  1148. // EasyDMA feature included on the chip. This technique loads the duty
  1149. // cycle configuration for each cycle when the PWM is enabled. For this
  1150. // to work we need to store a 16 bit configuration for each bit of the
  1151. // RGB(W) values in the pixel buffer.
  1152. // Comparator values for the PWM were hand picked and are guaranteed to
  1153. // be 100% organic to preserve freshness and high accuracy. Current
  1154. // parameters are:
  1155. // * PWM Clock: 16Mhz
  1156. // * Minimum step time: 62.5ns
  1157. // * Time for zero in high (T0H): 0.31ms
  1158. // * Time for one in high (T1H): 0.75ms
  1159. // * Cycle time: 1.25us
  1160. // * Frequency: 800Khz
  1161. // For 400Khz we just double the calculated times.
  1162. // ---------- BEGIN Constants for the EasyDMA implementation -----------
  1163. // The PWM starts the duty cycle in LOW. To start with HIGH we
  1164. // need to set the 15th bit on each register.
  1165. // WS2812 (rev A) timing is 0.35 and 0.7us
  1166. //#define MAGIC_T0H 5UL | (0x8000) // 0.3125us
  1167. //#define MAGIC_T1H 12UL | (0x8000) // 0.75us
  1168. // WS2812B (rev B) timing is 0.4 and 0.8 us
  1169. #define MAGIC_T0H 6UL | (0x8000) // 0.375us
  1170. #define MAGIC_T1H 13UL | (0x8000) // 0.8125us
  1171. // WS2811 (400 khz) timing is 0.5 and 1.2
  1172. #define MAGIC_T0H_400KHz 8UL | (0x8000) // 0.5us
  1173. #define MAGIC_T1H_400KHz 19UL | (0x8000) // 1.1875us
  1174. // For 400Khz, we double value of CTOPVAL
  1175. #define CTOPVAL 20UL // 1.25us
  1176. #define CTOPVAL_400KHz 40UL // 2.5us
  1177. // ---------- END Constants for the EasyDMA implementation -------------
  1178. //
  1179. // If there is no device available an alternative cycle-counter
  1180. // implementation is tried.
  1181. // The nRF52 runs with a fixed clock of 64Mhz. The alternative
  1182. // implementation is the same as the one used for the Teensy 3.0/1/2 but
  1183. // with the Nordic SDK HAL & registers syntax.
  1184. // The number of cycles was hand picked and is guaranteed to be 100%
  1185. // organic to preserve freshness and high accuracy.
  1186. // ---------- BEGIN Constants for cycle counter implementation ---------
  1187. #define CYCLES_800_T0H 18 // ~0.36 uS
  1188. #define CYCLES_800_T1H 41 // ~0.76 uS
  1189. #define CYCLES_800 71 // ~1.25 uS
  1190. #define CYCLES_400_T0H 26 // ~0.50 uS
  1191. #define CYCLES_400_T1H 70 // ~1.26 uS
  1192. #define CYCLES_400 156 // ~2.50 uS
  1193. // ---------- END of Constants for cycle counter implementation --------
  1194. // To support both the SoftDevice + Neopixels we use the EasyDMA
  1195. // feature from the NRF25. However this technique implies to
  1196. // generate a pattern and store it on the memory. The actual
  1197. // memory used in bytes corresponds to the following formula:
  1198. // totalMem = numBytes*8*2+(2*2)
  1199. // The two additional bytes at the end are needed to reset the
  1200. // sequence.
  1201. //
  1202. // If there is not enough memory, we will fall back to cycle counter
  1203. // using DWT
  1204. uint32_t pattern_size = numBytes*8*sizeof(uint16_t)+2*sizeof(uint16_t);
  1205. uint16_t* pixels_pattern = NULL;
  1206. NRF_PWM_Type* pwm = NULL;
  1207. // Try to find a free PWM device, which is not enabled
  1208. // and has no connected pins
  1209. NRF_PWM_Type* PWM[] = {
  1210. NRF_PWM0, NRF_PWM1, NRF_PWM2
  1211. #ifdef NRF_PWM3
  1212. ,NRF_PWM3
  1213. #endif
  1214. };
  1215. for(int device = 0; device < (sizeof(PWM)/sizeof(PWM[0])); device++) {
  1216. if( (PWM[device]->ENABLE == 0) &&
  1217. (PWM[device]->PSEL.OUT[0] & PWM_PSEL_OUT_CONNECT_Msk) &&
  1218. (PWM[device]->PSEL.OUT[1] & PWM_PSEL_OUT_CONNECT_Msk) &&
  1219. (PWM[device]->PSEL.OUT[2] & PWM_PSEL_OUT_CONNECT_Msk) &&
  1220. (PWM[device]->PSEL.OUT[3] & PWM_PSEL_OUT_CONNECT_Msk)
  1221. ) {
  1222. pwm = PWM[device];
  1223. break;
  1224. }
  1225. }
  1226. // only malloc if there is PWM device available
  1227. if ( pwm != NULL ) {
  1228. #ifdef ARDUINO_NRF52_ADAFRUIT // use thread-safe malloc
  1229. pixels_pattern = (uint16_t *) rtos_malloc(pattern_size);
  1230. #else
  1231. pixels_pattern = (uint16_t *) malloc(pattern_size);
  1232. #endif
  1233. }
  1234. // Use the identified device to choose the implementation
  1235. // If a PWM device is available use DMA
  1236. if( (pixels_pattern != NULL) && (pwm != NULL) ) {
  1237. uint16_t pos = 0; // bit position
  1238. for(uint16_t n=0; n<numBytes; n++) {
  1239. uint8_t pix = pixels[n];
  1240. for(uint8_t mask=0x80, i=0; mask>0; mask >>= 1, i++) {
  1241. #ifdef NEO_KHZ400
  1242. if( !is800KHz ) {
  1243. pixels_pattern[pos] = (pix & mask) ? MAGIC_T1H_400KHz : MAGIC_T0H_400KHz;
  1244. }else
  1245. #endif
  1246. {
  1247. pixels_pattern[pos] = (pix & mask) ? MAGIC_T1H : MAGIC_T0H;
  1248. }
  1249. pos++;
  1250. }
  1251. }
  1252. // Zero padding to indicate the end of que sequence
  1253. pixels_pattern[++pos] = 0 | (0x8000); // Seq end
  1254. pixels_pattern[++pos] = 0 | (0x8000); // Seq end
  1255. // Set the wave mode to count UP
  1256. pwm->MODE = (PWM_MODE_UPDOWN_Up << PWM_MODE_UPDOWN_Pos);
  1257. // Set the PWM to use the 16MHz clock
  1258. pwm->PRESCALER = (PWM_PRESCALER_PRESCALER_DIV_1 << PWM_PRESCALER_PRESCALER_Pos);
  1259. // Setting of the maximum count
  1260. // but keeping it on 16Mhz allows for more granularity just
  1261. // in case someone wants to do more fine-tuning of the timing.
  1262. #ifdef NEO_KHZ400
  1263. if( !is800KHz ) {
  1264. pwm->COUNTERTOP = (CTOPVAL_400KHz << PWM_COUNTERTOP_COUNTERTOP_Pos);
  1265. }else
  1266. #endif
  1267. {
  1268. pwm->COUNTERTOP = (CTOPVAL << PWM_COUNTERTOP_COUNTERTOP_Pos);
  1269. }
  1270. // Disable loops, we want the sequence to repeat only once
  1271. pwm->LOOP = (PWM_LOOP_CNT_Disabled << PWM_LOOP_CNT_Pos);
  1272. // On the "Common" setting the PWM uses the same pattern for the
  1273. // for supported sequences. The pattern is stored on half-word
  1274. // of 16bits
  1275. pwm->DECODER = (PWM_DECODER_LOAD_Common << PWM_DECODER_LOAD_Pos) |
  1276. (PWM_DECODER_MODE_RefreshCount << PWM_DECODER_MODE_Pos);
  1277. // Pointer to the memory storing the patter
  1278. pwm->SEQ[0].PTR = (uint32_t)(pixels_pattern) << PWM_SEQ_PTR_PTR_Pos;
  1279. // Calculation of the number of steps loaded from memory.
  1280. pwm->SEQ[0].CNT = (pattern_size/sizeof(uint16_t)) << PWM_SEQ_CNT_CNT_Pos;
  1281. // The following settings are ignored with the current config.
  1282. pwm->SEQ[0].REFRESH = 0;
  1283. pwm->SEQ[0].ENDDELAY = 0;
  1284. // The Neopixel implementation is a blocking algorithm. DMA
  1285. // allows for non-blocking operation. To "simulate" a blocking
  1286. // operation we enable the interruption for the end of sequence
  1287. // and block the execution thread until the event flag is set by
  1288. // the peripheral.
  1289. // pwm->INTEN |= (PWM_INTEN_SEQEND0_Enabled<<PWM_INTEN_SEQEND0_Pos);
  1290. // PSEL must be configured before enabling PWM
  1291. pwm->PSEL.OUT[0] = g_ADigitalPinMap[pin];
  1292. // Enable the PWM
  1293. pwm->ENABLE = 1;
  1294. // After all of this and many hours of reading the documentation
  1295. // we are ready to start the sequence...
  1296. pwm->EVENTS_SEQEND[0] = 0;
  1297. pwm->TASKS_SEQSTART[0] = 1;
  1298. // But we have to wait for the flag to be set.
  1299. while(!pwm->EVENTS_SEQEND[0])
  1300. {
  1301. #ifdef ARDUINO_NRF52_ADAFRUIT
  1302. yield();
  1303. #endif
  1304. }
  1305. // Before leave we clear the flag for the event.
  1306. pwm->EVENTS_SEQEND[0] = 0;
  1307. // We need to disable the device and disconnect
  1308. // all the outputs before leave or the device will not
  1309. // be selected on the next call.
  1310. // TODO: Check if disabling the device causes performance issues.
  1311. pwm->ENABLE = 0;
  1312. pwm->PSEL.OUT[0] = 0xFFFFFFFFUL;
  1313. #ifdef ARDUINO_NRF52_ADAFRUIT // use thread-safe free
  1314. rtos_free(pixels_pattern);
  1315. #else
  1316. free(pixels_pattern);
  1317. #endif
  1318. }// End of DMA implementation
  1319. // ---------------------------------------------------------------------
  1320. else{
  1321. // Fall back to DWT
  1322. #ifdef ARDUINO_NRF52_ADAFRUIT
  1323. // Bluefruit Feather 52 uses freeRTOS
  1324. // Critical Section is used since it does not block SoftDevice execution
  1325. taskENTER_CRITICAL();
  1326. #elif defined(NRF52_DISABLE_INT)
  1327. // If you are using the Bluetooth SoftDevice we advise you to not disable
  1328. // the interrupts. Disabling the interrupts even for short periods of time
  1329. // causes the SoftDevice to stop working.
  1330. // Disable the interrupts only in cases where you need high performance for
  1331. // the LEDs and if you are not using the EasyDMA feature.
  1332. __disable_irq();
  1333. #endif
  1334. NRF_GPIO_Type* nrf_port = (NRF_GPIO_Type*) digitalPinToPort(pin);
  1335. uint32_t pinMask = digitalPinToBitMask(pin);
  1336. uint32_t CYCLES_X00 = CYCLES_800;
  1337. uint32_t CYCLES_X00_T1H = CYCLES_800_T1H;
  1338. uint32_t CYCLES_X00_T0H = CYCLES_800_T0H;
  1339. #ifdef NEO_KHZ400
  1340. if( !is800KHz )
  1341. {
  1342. CYCLES_X00 = CYCLES_400;
  1343. CYCLES_X00_T1H = CYCLES_400_T1H;
  1344. CYCLES_X00_T0H = CYCLES_400_T0H;
  1345. }
  1346. #endif
  1347. // Enable DWT in debug core
  1348. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  1349. DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
  1350. // Tries to re-send the frame if is interrupted by the SoftDevice.
  1351. while(1) {
  1352. uint8_t *p = pixels;
  1353. uint32_t cycStart = DWT->CYCCNT;
  1354. uint32_t cyc = 0;
  1355. for(uint16_t n=0; n<numBytes; n++) {
  1356. uint8_t pix = *p++;
  1357. for(uint8_t mask = 0x80; mask; mask >>= 1) {
  1358. while(DWT->CYCCNT - cyc < CYCLES_X00);
  1359. cyc = DWT->CYCCNT;
  1360. nrf_port->OUTSET |= pinMask;
  1361. if(pix & mask) {
  1362. while(DWT->CYCCNT - cyc < CYCLES_X00_T1H);
  1363. } else {
  1364. while(DWT->CYCCNT - cyc < CYCLES_X00_T0H);
  1365. }
  1366. nrf_port->OUTCLR |= pinMask;
  1367. }
  1368. }
  1369. while(DWT->CYCCNT - cyc < CYCLES_X00);
  1370. // If total time longer than 25%, resend the whole data.
  1371. // Since we are likely to be interrupted by SoftDevice
  1372. if ( (DWT->CYCCNT - cycStart) < ( 8*numBytes*((CYCLES_X00*5)/4) ) ) {
  1373. break;
  1374. }
  1375. // re-send need 300us delay
  1376. delayMicroseconds(300);
  1377. }
  1378. // Enable interrupts again
  1379. #ifdef ARDUINO_NRF52_ADAFRUIT
  1380. taskEXIT_CRITICAL();
  1381. #elif defined(NRF52_DISABLE_INT)
  1382. __enable_irq();
  1383. #endif
  1384. }
  1385. // END of NRF52 implementation
  1386. #elif defined (__SAMD21E17A__) || defined(__SAMD21G18A__) || defined(__SAMD21E18A__) || defined(__SAMD21J18A__) // Arduino Zero, Gemma/Trinket M0, SODAQ Autonomo and others
  1387. // Tried this with a timer/counter, couldn't quite get adequate
  1388. // resolution. So yay, you get a load of goofball NOPs...
  1389. uint8_t *ptr, *end, p, bitMask, portNum;
  1390. uint32_t pinMask;
  1391. portNum = g_APinDescription[pin].ulPort;
  1392. pinMask = 1ul << g_APinDescription[pin].ulPin;
  1393. ptr = pixels;
  1394. end = ptr + numBytes;
  1395. p = *ptr++;
  1396. bitMask = 0x80;
  1397. volatile uint32_t *set = &(PORT->Group[portNum].OUTSET.reg),
  1398. *clr = &(PORT->Group[portNum].OUTCLR.reg);
  1399. #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
  1400. if(is800KHz) {
  1401. #endif
  1402. for(;;) {
  1403. *set = pinMask;
  1404. asm("nop; nop; nop; nop; nop; nop; nop; nop;");
  1405. if(p & bitMask) {
  1406. asm("nop; nop; nop; nop; nop; nop; nop; nop;"
  1407. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1408. "nop; nop; nop; nop;");
  1409. *clr = pinMask;
  1410. } else {
  1411. *clr = pinMask;
  1412. asm("nop; nop; nop; nop; nop; nop; nop; nop;"
  1413. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1414. "nop; nop; nop; nop;");
  1415. }
  1416. if(bitMask >>= 1) {
  1417. asm("nop; nop; nop; nop; nop; nop; nop; nop; nop;");
  1418. } else {
  1419. if(ptr >= end) break;
  1420. p = *ptr++;
  1421. bitMask = 0x80;
  1422. }
  1423. }
  1424. #ifdef NEO_KHZ400
  1425. } else { // 400 KHz bitstream
  1426. for(;;) {
  1427. *set = pinMask;
  1428. asm("nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop;");
  1429. if(p & bitMask) {
  1430. asm("nop; nop; nop; nop; nop; nop; nop; nop;"
  1431. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1432. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1433. "nop; nop; nop;");
  1434. *clr = pinMask;
  1435. } else {
  1436. *clr = pinMask;
  1437. asm("nop; nop; nop; nop; nop; nop; nop; nop;"
  1438. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1439. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1440. "nop; nop; nop;");
  1441. }
  1442. asm("nop; nop; nop; nop; nop; nop; nop; nop;"
  1443. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1444. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1445. "nop; nop; nop; nop; nop; nop; nop; nop;");
  1446. if(bitMask >>= 1) {
  1447. asm("nop; nop; nop; nop; nop; nop; nop;");
  1448. } else {
  1449. if(ptr >= end) break;
  1450. p = *ptr++;
  1451. bitMask = 0x80;
  1452. }
  1453. }
  1454. }
  1455. #endif
  1456. #elif defined (__SAMD51__) // M4 @ 120mhz
  1457. // Tried this with a timer/counter, couldn't quite get adequate
  1458. // resolution. So yay, you get a load of goofball NOPs...
  1459. uint8_t *ptr, *end, p, bitMask, portNum;
  1460. uint32_t pinMask;
  1461. portNum = g_APinDescription[pin].ulPort;
  1462. pinMask = 1ul << g_APinDescription[pin].ulPin;
  1463. ptr = pixels;
  1464. end = ptr + numBytes;
  1465. p = *ptr++;
  1466. bitMask = 0x80;
  1467. volatile uint32_t *set = &(PORT->Group[portNum].OUTSET.reg),
  1468. *clr = &(PORT->Group[portNum].OUTCLR.reg);
  1469. #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
  1470. if(is800KHz) {
  1471. #endif
  1472. for(;;) {
  1473. if(p & bitMask) { // ONE
  1474. // High 800ns
  1475. *set = pinMask;
  1476. asm("nop; nop; nop; nop; nop; nop; nop; nop;"
  1477. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1478. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1479. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1480. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1481. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1482. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1483. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1484. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1485. "nop; nop; nop; nop; nop; nop; nop; nop;");
  1486. // Low 450ns
  1487. *clr = pinMask;
  1488. asm("nop; nop; nop; nop; nop; nop; nop; nop;"
  1489. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1490. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1491. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1492. "nop;");
  1493. } else { // ZERO
  1494. // High 400ns
  1495. *set = pinMask;
  1496. asm("nop; nop; nop; nop; nop; nop; nop; nop;"
  1497. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1498. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1499. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1500. "nop;");
  1501. // Low 850ns
  1502. *clr = pinMask;
  1503. asm("nop; nop; nop; nop; nop; nop; nop; nop;"
  1504. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1505. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1506. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1507. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1508. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1509. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1510. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1511. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1512. "nop; nop; nop; nop; nop; nop; nop; nop;");
  1513. }
  1514. if(bitMask >>= 1) {
  1515. // Move on to the next pixel
  1516. asm("nop;");
  1517. } else {
  1518. if(ptr >= end) break;
  1519. p = *ptr++;
  1520. bitMask = 0x80;
  1521. }
  1522. }
  1523. #ifdef NEO_KHZ400
  1524. } else { // 400 KHz bitstream
  1525. // ToDo!
  1526. }
  1527. #endif
  1528. #elif defined (ARDUINO_STM32_FEATHER) // FEATHER WICED (120MHz)
  1529. // Tried this with a timer/counter, couldn't quite get adequate
  1530. // resolution. So yay, you get a load of goofball NOPs...
  1531. uint8_t *ptr, *end, p, bitMask;
  1532. uint32_t pinMask;
  1533. pinMask = BIT(PIN_MAP[pin].gpio_bit);
  1534. ptr = pixels;
  1535. end = ptr + numBytes;
  1536. p = *ptr++;
  1537. bitMask = 0x80;
  1538. volatile uint16_t *set = &(PIN_MAP[pin].gpio_device->regs->BSRRL);
  1539. volatile uint16_t *clr = &(PIN_MAP[pin].gpio_device->regs->BSRRH);
  1540. #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
  1541. if(is800KHz) {
  1542. #endif
  1543. for(;;) {
  1544. if(p & bitMask) { // ONE
  1545. // High 800ns
  1546. *set = pinMask;
  1547. asm("nop; nop; nop; nop; nop; nop; nop; nop;"
  1548. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1549. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1550. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1551. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1552. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1553. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1554. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1555. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1556. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1557. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1558. "nop; nop; nop; nop; nop; nop;");
  1559. // Low 450ns
  1560. *clr = pinMask;
  1561. asm("nop; nop; nop; nop; nop; nop; nop; nop;"
  1562. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1563. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1564. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1565. "nop; nop; nop; nop; nop; nop;");
  1566. } else { // ZERO
  1567. // High 400ns
  1568. *set = pinMask;
  1569. asm("nop; nop; nop; nop; nop; nop; nop; nop;"
  1570. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1571. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1572. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1573. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1574. "nop;");
  1575. // Low 850ns
  1576. *clr = pinMask;
  1577. asm("nop; nop; nop; nop; nop; nop; nop; nop;"
  1578. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1579. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1580. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1581. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1582. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1583. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1584. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1585. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1586. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1587. "nop; nop; nop; nop; nop; nop; nop; nop;"
  1588. "nop; nop; nop; nop;");
  1589. }
  1590. if(bitMask >>= 1) {
  1591. // Move on to the next pixel
  1592. asm("nop;");
  1593. } else {
  1594. if(ptr >= end) break;
  1595. p = *ptr++;
  1596. bitMask = 0x80;
  1597. }
  1598. }
  1599. #ifdef NEO_KHZ400
  1600. } else { // 400 KHz bitstream
  1601. // ToDo!
  1602. }
  1603. #endif
  1604. #elif defined (NRF51)
  1605. uint8_t *p = pixels,
  1606. pix, count, mask;
  1607. int32_t num = numBytes;
  1608. unsigned int bitmask = ( 1 << g_ADigitalPinMap[pin] );
  1609. // https://github.com/sandeepmistry/arduino-nRF5/blob/dc53980c8bac27898fca90d8ecb268e11111edc1/variants/BBCmicrobit/variant.cpp
  1610. volatile unsigned int *reg = (unsigned int *) (0x50000000UL + 0x508);
  1611. // https://github.com/sandeepmistry/arduino-nRF5/blob/dc53980c8bac27898fca90d8ecb268e11111edc1/cores/nRF5/SDK/components/device/nrf51.h
  1612. // http://www.iot-programmer.com/index.php/books/27-micro-bit-iot-in-c/chapters-micro-bit-iot-in-c/47-micro-bit-iot-in-c-fast-memory-mapped-gpio?showall=1
  1613. // https://github.com/Microsoft/pxt-neopixel/blob/master/sendbuffer.asm
  1614. asm volatile(
  1615. // "cpsid i" ; disable irq
  1616. // b .start
  1617. "b L%=_start" "\n\t"
  1618. // .nextbit: ; C0
  1619. "L%=_nextbit:" "\n\t" //; C0
  1620. // str r1, [r3, #0] ; pin := hi C2
  1621. "strb %[bitmask], [%[reg], #0]" "\n\t" //; pin := hi C2
  1622. // tst r6, r0 ; C3
  1623. "tst %[mask], %[pix]" "\n\t"// ; C3
  1624. // bne .islate ; C4
  1625. "bne L%=_islate" "\n\t" //; C4
  1626. // str r1, [r2, #0] ; pin := lo C6
  1627. "strb %[bitmask], [%[reg], #4]" "\n\t" //; pin := lo C6
  1628. // .islate:
  1629. "L%=_islate:" "\n\t"
  1630. // lsrs r6, r6, #1 ; r6 >>= 1 C7
  1631. "lsr %[mask], %[mask], #1" "\n\t" //; r6 >>= 1 C7
  1632. // bne .justbit ; C8
  1633. "bne L%=_justbit" "\n\t" //; C8
  1634. // ; not just a bit - need new byte
  1635. // adds r4, #1 ; r4++ C9
  1636. "add %[p], #1" "\n\t" //; r4++ C9
  1637. // subs r5, #1 ; r5-- C10
  1638. "sub %[num], #1" "\n\t" //; r5-- C10
  1639. // bcc .stop ; if (r5<0) goto .stop C11
  1640. "bcc L%=_stop" "\n\t" //; if (r5<0) goto .stop C11
  1641. // .start:
  1642. "L%=_start:"
  1643. // movs r6, #0x80 ; reset mask C12
  1644. "movs %[mask], #0x80" "\n\t" //; reset mask C12
  1645. // nop ; C13
  1646. "nop" "\n\t" //; C13
  1647. // .common: ; C13
  1648. "L%=_common:" "\n\t" //; C13
  1649. // str r1, [r2, #0] ; pin := lo C15
  1650. "strb %[bitmask], [%[reg], #4]" "\n\t" //; pin := lo C15
  1651. // ; always re-load byte - it just fits with the cycles better this way
  1652. // ldrb r0, [r4, #0] ; r0 := *r4 C17
  1653. "ldrb %[pix], [%[p], #0]" "\n\t" //; r0 := *r4 C17
  1654. // b .nextbit ; C20
  1655. "b L%=_nextbit" "\n\t" //; C20
  1656. // .justbit: ; C10
  1657. "L%=_justbit:" "\n\t" //; C10
  1658. // ; no nops, branch taken is already 3 cycles
  1659. // b .common ; C13
  1660. "b L%=_common" "\n\t" //; C13
  1661. // .stop:
  1662. "L%=_stop:" "\n\t"
  1663. // str r1, [r2, #0] ; pin := lo
  1664. "strb %[bitmask], [%[reg], #4]" "\n\t" //; pin := lo
  1665. // cpsie i ; enable irq
  1666. : [p] "+r" (p),
  1667. [pix] "=&r" (pix),
  1668. [count] "=&r" (count),
  1669. [mask] "=&r" (mask),
  1670. [num] "+r" (num)
  1671. : [bitmask] "r" (bitmask),
  1672. [reg] "r" (reg)
  1673. );
  1674. #elif defined(__SAM3X8E__) // Arduino Due
  1675. #define SCALE VARIANT_MCK / 2UL / 1000000UL
  1676. #define INST (2UL * F_CPU / VARIANT_MCK)
  1677. #define TIME_800_0 ((int)(0.40 * SCALE + 0.5) - (5 * INST))
  1678. #define TIME_800_1 ((int)(0.80 * SCALE + 0.5) - (5 * INST))
  1679. #define PERIOD_800 ((int)(1.25 * SCALE + 0.5) - (5 * INST))
  1680. #define TIME_400_0 ((int)(0.50 * SCALE + 0.5) - (5 * INST))
  1681. #define TIME_400_1 ((int)(1.20 * SCALE + 0.5) - (5 * INST))
  1682. #define PERIOD_400 ((int)(2.50 * SCALE + 0.5) - (5 * INST))
  1683. int pinMask, time0, time1, period, t;
  1684. Pio *port;
  1685. volatile WoReg *portSet, *portClear, *timeValue, *timeReset;
  1686. uint8_t *p, *end, pix, mask;
  1687. pmc_set_writeprotect(false);
  1688. pmc_enable_periph_clk((uint32_t)TC3_IRQn);
  1689. TC_Configure(TC1, 0,
  1690. TC_CMR_WAVE | TC_CMR_WAVSEL_UP | TC_CMR_TCCLKS_TIMER_CLOCK1);
  1691. TC_Start(TC1, 0);
  1692. pinMask = g_APinDescription[pin].ulPin; // Don't 'optimize' these into
  1693. port = g_APinDescription[pin].pPort; // declarations above. Want to
  1694. portSet = &(port->PIO_SODR); // burn a few cycles after
  1695. portClear = &(port->PIO_CODR); // starting timer to minimize
  1696. timeValue = &(TC1->TC_CHANNEL[0].TC_CV); // the initial 'while'.
  1697. timeReset = &(TC1->TC_CHANNEL[0].TC_CCR);
  1698. p = pixels;
  1699. end = p + numBytes;
  1700. pix = *p++;
  1701. mask = 0x80;
  1702. #ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
  1703. if(is800KHz) {
  1704. #endif
  1705. time0 = TIME_800_0;
  1706. time1 = TIME_800_1;
  1707. period = PERIOD_800;
  1708. #ifdef NEO_KHZ400
  1709. } else { // 400 KHz bitstream
  1710. time0 = TIME_400_0;
  1711. time1 = TIME_400_1;
  1712. period = PERIOD_400;
  1713. }
  1714. #endif
  1715. for(t = time0;; t = time0) {
  1716. if(pix & mask) t = time1;
  1717. while(*timeValue < period);
  1718. *portSet = pinMask;
  1719. *timeReset = TC_CCR_CLKEN | TC_CCR_SWTRG;
  1720. while(*timeValue < t);
  1721. *portClear = pinMask;
  1722. if(!(mask >>= 1)) { // This 'inside-out' loop logic utilizes
  1723. if(p >= end) break; // idle time to minimize inter-byte delays.
  1724. pix = *p++;
  1725. mask = 0x80;
  1726. }
  1727. }
  1728. while(*timeValue < period); // Wait for last bit
  1729. TC_Stop(TC1, 0);
  1730. #endif // end Due
  1731. // END ARM ----------------------------------------------------------------
  1732. #elif defined(ESP8266) || defined(ESP32)
  1733. // ESP8266 ----------------------------------------------------------------
  1734. // ESP8266 show() is external to enforce ICACHE_RAM_ATTR execution
  1735. espShow(pin, pixels, numBytes, is800KHz);
  1736. #elif defined(__ARDUINO_ARC__)
  1737. // Arduino 101 -----------------------------------------------------------
  1738. #define NOPx7 { __builtin_arc_nop(); \
  1739. __builtin_arc_nop(); __builtin_arc_nop(); \
  1740. __builtin_arc_nop(); __builtin_arc_nop(); \
  1741. __builtin_arc_nop(); __builtin_arc_nop(); }
  1742. PinDescription *pindesc = &g_APinDescription[pin];
  1743. register uint32_t loop = 8 * numBytes; // one loop to handle all bytes and all bits
  1744. register uint8_t *p = pixels;
  1745. register uint32_t currByte = (uint32_t) (*p);
  1746. register uint32_t currBit = 0x80 & currByte;
  1747. register uint32_t bitCounter = 0;
  1748. register uint32_t first = 1;
  1749. // The loop is unusual. Very first iteration puts all the way LOW to the wire -
  1750. // constant LOW does not affect NEOPIXEL, so there is no visible effect displayed.
  1751. // During that very first iteration CPU caches instructions in the loop.
  1752. // Because of the caching process, "CPU slows down". NEOPIXEL pulse is very time sensitive
  1753. // that's why we let the CPU cache first and we start regular pulse from 2nd iteration
  1754. if (pindesc->ulGPIOType == SS_GPIO) {
  1755. register uint32_t reg = pindesc->ulGPIOBase + SS_GPIO_SWPORTA_DR;
  1756. uint32_t reg_val = __builtin_arc_lr((volatile uint32_t)reg);
  1757. register uint32_t reg_bit_high = reg_val | (1 << pindesc->ulGPIOId);
  1758. register uint32_t reg_bit_low = reg_val & ~(1 << pindesc->ulGPIOId);
  1759. loop += 1; // include first, special iteration
  1760. while(loop--) {
  1761. if(!first) {
  1762. currByte <<= 1;
  1763. bitCounter++;
  1764. }
  1765. // 1 is >550ns high and >450ns low; 0 is 200..500ns high and >450ns low
  1766. __builtin_arc_sr(first ? reg_bit_low : reg_bit_high, (volatile uint32_t)reg);
  1767. if(currBit) { // ~400ns HIGH (740ns overall)
  1768. NOPx7
  1769. NOPx7
  1770. }
  1771. // ~340ns HIGH
  1772. NOPx7
  1773. __builtin_arc_nop();
  1774. // 820ns LOW; per spec, max allowed low here is 5000ns */
  1775. __builtin_arc_sr(reg_bit_low, (volatile uint32_t)reg);
  1776. NOPx7
  1777. NOPx7
  1778. if(bitCounter >= 8) {
  1779. bitCounter = 0;
  1780. currByte = (uint32_t) (*++p);
  1781. }
  1782. currBit = 0x80 & currByte;
  1783. first = 0;
  1784. }
  1785. } else if(pindesc->ulGPIOType == SOC_GPIO) {
  1786. register uint32_t reg = pindesc->ulGPIOBase + SOC_GPIO_SWPORTA_DR;
  1787. uint32_t reg_val = MMIO_REG_VAL(reg);
  1788. register uint32_t reg_bit_high = reg_val | (1 << pindesc->ulGPIOId);
  1789. register uint32_t reg_bit_low = reg_val & ~(1 << pindesc->ulGPIOId);
  1790. loop += 1; // include first, special iteration
  1791. while(loop--) {
  1792. if(!first) {
  1793. currByte <<= 1;
  1794. bitCounter++;
  1795. }
  1796. MMIO_REG_VAL(reg) = first ? reg_bit_low : reg_bit_high;
  1797. if(currBit) { // ~430ns HIGH (740ns overall)
  1798. NOPx7
  1799. NOPx7
  1800. __builtin_arc_nop();
  1801. }
  1802. // ~310ns HIGH
  1803. NOPx7
  1804. // 850ns LOW; per spec, max allowed low here is 5000ns */
  1805. MMIO_REG_VAL(reg) = reg_bit_low;
  1806. NOPx7
  1807. NOPx7
  1808. if(bitCounter >= 8) {
  1809. bitCounter = 0;
  1810. currByte = (uint32_t) (*++p);
  1811. }
  1812. currBit = 0x80 & currByte;
  1813. first = 0;
  1814. }
  1815. }
  1816. #else
  1817. #error Architecture not supported
  1818. #endif
  1819. // END ARCHITECTURE SELECT ------------------------------------------------
  1820. #if !( defined(NRF52) || defined(NRF52_SERIES) )
  1821. interrupts();
  1822. #endif
  1823. endTime = micros(); // Save EOD time for latch on next call
  1824. }
  1825. // Set the output pin number
  1826. void Adafruit_NeoPixel::setPin(uint8_t p) {
  1827. if(begun && (pin >= 0)) pinMode(pin, INPUT);
  1828. pin = p;
  1829. if(begun) {
  1830. pinMode(p, OUTPUT);
  1831. digitalWrite(p, LOW);
  1832. }
  1833. #ifdef __AVR__
  1834. port = portOutputRegister(digitalPinToPort(p));
  1835. pinMask = digitalPinToBitMask(p);
  1836. #endif
  1837. }
  1838. // Set pixel color from separate R,G,B components:
  1839. void Adafruit_NeoPixel::setPixelColor(
  1840. uint16_t n, uint8_t r, uint8_t g, uint8_t b) {
  1841. if(n < numLEDs) {
  1842. if(brightness) { // See notes in setBrightness()
  1843. r = (r * brightness) >> 8;
  1844. g = (g * brightness) >> 8;
  1845. b = (b * brightness) >> 8;
  1846. }
  1847. uint8_t *p;
  1848. if(wOffset == rOffset) { // Is an RGB-type strip
  1849. p = &pixels[n * 3]; // 3 bytes per pixel
  1850. } else { // Is a WRGB-type strip
  1851. p = &pixels[n * 4]; // 4 bytes per pixel
  1852. p[wOffset] = 0; // But only R,G,B passed -- set W to 0
  1853. }
  1854. p[rOffset] = r; // R,G,B always stored
  1855. p[gOffset] = g;
  1856. p[bOffset] = b;
  1857. }
  1858. }
  1859. void Adafruit_NeoPixel::setPixelColor(
  1860. uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
  1861. if(n < numLEDs) {
  1862. if(brightness) { // See notes in setBrightness()
  1863. r = (r * brightness) >> 8;
  1864. g = (g * brightness) >> 8;
  1865. b = (b * brightness) >> 8;
  1866. w = (w * brightness) >> 8;
  1867. }
  1868. uint8_t *p;
  1869. if(wOffset == rOffset) { // Is an RGB-type strip
  1870. p = &pixels[n * 3]; // 3 bytes per pixel (ignore W)
  1871. } else { // Is a WRGB-type strip
  1872. p = &pixels[n * 4]; // 4 bytes per pixel
  1873. p[wOffset] = w; // Store W
  1874. }
  1875. p[rOffset] = r; // Store R,G,B
  1876. p[gOffset] = g;
  1877. p[bOffset] = b;
  1878. }
  1879. }
  1880. // Set pixel color from 'packed' 32-bit RGB color:
  1881. void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c) {
  1882. if(n < numLEDs) {
  1883. uint8_t *p,
  1884. r = (uint8_t)(c >> 16),
  1885. g = (uint8_t)(c >> 8),
  1886. b = (uint8_t)c;
  1887. if(brightness) { // See notes in setBrightness()
  1888. r = (r * brightness) >> 8;
  1889. g = (g * brightness) >> 8;
  1890. b = (b * brightness) >> 8;
  1891. }
  1892. if(wOffset == rOffset) {
  1893. p = &pixels[n * 3];
  1894. } else {
  1895. p = &pixels[n * 4];
  1896. uint8_t w = (uint8_t)(c >> 24);
  1897. p[wOffset] = brightness ? ((w * brightness) >> 8) : w;
  1898. }
  1899. p[rOffset] = r;
  1900. p[gOffset] = g;
  1901. p[bOffset] = b;
  1902. }
  1903. }
  1904. // Fills all or a given start+length of strip. Arguments:
  1905. // Packed RGB color (0 if unspecified, effectively a strip clear operation).
  1906. // Index if first pixel (0 if unspecified - beginning of strip).
  1907. // Pixel count (if unspecified, fills to end of strip).
  1908. void Adafruit_NeoPixel::fill(uint32_t c, uint16_t first, uint16_t count) {
  1909. uint16_t i, end;
  1910. if(first >= numLEDs) {
  1911. return; // If first LED is past end of strip, nothing to do
  1912. }
  1913. // Calculate the index ONE AFTER the last pixel to fill
  1914. if(count == 0) {
  1915. // Fill to end of strip
  1916. end = numLEDs;
  1917. } else {
  1918. // Ensure that the loop won't go past the last pixel
  1919. end = first + count;
  1920. if(end > numLEDs) end = numLEDs;
  1921. }
  1922. for(i = first; i < end; i++) {
  1923. this->setPixelColor(i, c);
  1924. }
  1925. }
  1926. // Convert separate R,G,B into packed 32-bit RGB color.
  1927. // Packed format is always RGB, regardless of LED strand color order.
  1928. uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b) {
  1929. return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
  1930. }
  1931. // Convert separate R,G,B,W into packed 32-bit WRGB color.
  1932. // Packed format is always WRGB, regardless of LED strand color order.
  1933. uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
  1934. return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
  1935. }
  1936. // Query color from previously-set pixel (returns packed 32-bit RGB value)
  1937. uint32_t Adafruit_NeoPixel::getPixelColor(uint16_t n) const {
  1938. if(n >= numLEDs) return 0; // Out of bounds, return no color.
  1939. uint8_t *p;
  1940. if(wOffset == rOffset) { // Is RGB-type device
  1941. p = &pixels[n * 3];
  1942. if(brightness) {
  1943. // Stored color was decimated by setBrightness(). Returned value
  1944. // attempts to scale back to an approximation of the original 24-bit
  1945. // value used when setting the pixel color, but there will always be
  1946. // some error -- those bits are simply gone. Issue is most
  1947. // pronounced at low brightness levels.
  1948. return (((uint32_t)(p[rOffset] << 8) / brightness) << 16) |
  1949. (((uint32_t)(p[gOffset] << 8) / brightness) << 8) |
  1950. ( (uint32_t)(p[bOffset] << 8) / brightness );
  1951. } else {
  1952. // No brightness adjustment has been made -- return 'raw' color
  1953. return ((uint32_t)p[rOffset] << 16) |
  1954. ((uint32_t)p[gOffset] << 8) |
  1955. (uint32_t)p[bOffset];
  1956. }
  1957. } else { // Is RGBW-type device
  1958. p = &pixels[n * 4];
  1959. if(brightness) { // Return scaled color
  1960. return (((uint32_t)(p[wOffset] << 8) / brightness) << 24) |
  1961. (((uint32_t)(p[rOffset] << 8) / brightness) << 16) |
  1962. (((uint32_t)(p[gOffset] << 8) / brightness) << 8) |
  1963. ( (uint32_t)(p[bOffset] << 8) / brightness );
  1964. } else { // Return raw color
  1965. return ((uint32_t)p[wOffset] << 24) |
  1966. ((uint32_t)p[rOffset] << 16) |
  1967. ((uint32_t)p[gOffset] << 8) |
  1968. (uint32_t)p[bOffset];
  1969. }
  1970. }
  1971. }
  1972. // Returns pointer to pixels[] array. Pixel data is stored in device-
  1973. // native format and is not translated here. Application will need to be
  1974. // aware of specific pixel data format and handle colors appropriately.
  1975. uint8_t *Adafruit_NeoPixel::getPixels(void) const {
  1976. return pixels;
  1977. }
  1978. uint16_t Adafruit_NeoPixel::numPixels(void) const {
  1979. return numLEDs;
  1980. }
  1981. // Adjust output brightness; 0=darkest (off), 255=brightest. This does
  1982. // NOT immediately affect what's currently displayed on the LEDs. The
  1983. // next call to show() will refresh the LEDs at this level. However,
  1984. // this process is potentially "lossy," especially when increasing
  1985. // brightness. The tight timing in the WS2811/WS2812 code means there
  1986. // aren't enough free cycles to perform this scaling on the fly as data
  1987. // is issued. So we make a pass through the existing color data in RAM
  1988. // and scale it (subsequent graphics commands also work at this
  1989. // brightness level). If there's a significant step up in brightness,
  1990. // the limited number of steps (quantization) in the old data will be
  1991. // quite visible in the re-scaled version. For a non-destructive
  1992. // change, you'll need to re-render the full strip data. C'est la vie.
  1993. void Adafruit_NeoPixel::setBrightness(uint8_t b) {
  1994. // Stored brightness value is different than what's passed.
  1995. // This simplifies the actual scaling math later, allowing a fast
  1996. // 8x8-bit multiply and taking the MSB. 'brightness' is a uint8_t,
  1997. // adding 1 here may (intentionally) roll over...so 0 = max brightness
  1998. // (color values are interpreted literally; no scaling), 1 = min
  1999. // brightness (off), 255 = just below max brightness.
  2000. uint8_t newBrightness = b + 1;
  2001. if(newBrightness != brightness) { // Compare against prior value
  2002. // Brightness has changed -- re-scale existing data in RAM
  2003. uint8_t c,
  2004. *ptr = pixels,
  2005. oldBrightness = brightness - 1; // De-wrap old brightness value
  2006. uint16_t scale;
  2007. if(oldBrightness == 0) scale = 0; // Avoid /0
  2008. else if(b == 255) scale = 65535 / oldBrightness;
  2009. else scale = (((uint16_t)newBrightness << 8) - 1) / oldBrightness;
  2010. for(uint16_t i=0; i<numBytes; i++) {
  2011. c = *ptr;
  2012. *ptr++ = (c * scale) >> 8;
  2013. }
  2014. brightness = newBrightness;
  2015. }
  2016. }
  2017. //Return the brightness value
  2018. uint8_t Adafruit_NeoPixel::getBrightness(void) const {
  2019. return brightness - 1;
  2020. }
  2021. void Adafruit_NeoPixel::clear() {
  2022. memset(pixels, 0, numBytes);
  2023. }
  2024. /* A PROGMEM (flash mem) table containing 8-bit unsigned sine wave (0-255).
  2025. Copy & paste this snippet into a Python REPL to regenerate:
  2026. import math
  2027. for x in range(256):
  2028. print("{:3},".format(int((math.sin(x/128.0*math.pi)+1.0)*127.5+0.5))),
  2029. if x&15 == 15: print
  2030. */
  2031. static const uint8_t PROGMEM _sineTable[256] = {
  2032. 128,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173,
  2033. 176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215,
  2034. 218,220,222,224,226,228,230,232,234,235,237,238,240,241,243,244,
  2035. 245,246,248,249,250,250,251,252,253,253,254,254,254,255,255,255,
  2036. 255,255,255,255,254,254,254,253,253,252,251,250,250,249,248,246,
  2037. 245,244,243,241,240,238,237,235,234,232,230,228,226,224,222,220,
  2038. 218,215,213,211,208,206,203,201,198,196,193,190,188,185,182,179,
  2039. 176,173,170,167,165,162,158,155,152,149,146,143,140,137,134,131,
  2040. 128,124,121,118,115,112,109,106,103,100, 97, 93, 90, 88, 85, 82,
  2041. 79, 76, 73, 70, 67, 65, 62, 59, 57, 54, 52, 49, 47, 44, 42, 40,
  2042. 37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11,
  2043. 10, 9, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0,
  2044. 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9,
  2045. 10, 11, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35,
  2046. 37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76,
  2047. 79, 82, 85, 88, 90, 93, 97,100,103,106,109,112,115,118,121,124};
  2048. /* Similar to above, but for an 8-bit gamma-correction table.
  2049. Copy & paste this snippet into a Python REPL to regenerate:
  2050. import math
  2051. gamma=2.6
  2052. for x in range(256):
  2053. print("{:3},".format(int(math.pow((x)/255.0,gamma)*255.0+0.5))),
  2054. if x&15 == 15: print
  2055. */
  2056. static const uint8_t PROGMEM _gammaTable[256] = {
  2057. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  2058. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  2059. 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
  2060. 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7,
  2061. 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12,
  2062. 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20,
  2063. 20, 21, 21, 22, 22, 23, 24, 24, 25, 25, 26, 27, 27, 28, 29, 29,
  2064. 30, 31, 31, 32, 33, 34, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42,
  2065. 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
  2066. 58, 59, 60, 61, 62, 63, 64, 65, 66, 68, 69, 70, 71, 72, 73, 75,
  2067. 76, 77, 78, 80, 81, 82, 84, 85, 86, 88, 89, 90, 92, 93, 94, 96,
  2068. 97, 99,100,102,103,105,106,108,109,111,112,114,115,117,119,120,
  2069. 122,124,125,127,129,130,132,134,136,137,139,141,143,145,146,148,
  2070. 150,152,154,156,158,160,162,164,166,168,170,172,174,176,178,180,
  2071. 182,184,186,188,191,193,195,197,199,202,204,206,209,211,213,215,
  2072. 218,220,223,225,227,230,232,235,237,240,242,245,247,250,252,255};
  2073. uint8_t Adafruit_NeoPixel::sine8(uint8_t x) const {
  2074. return pgm_read_byte(&_sineTable[x]); // 0-255 in, 0-255 out
  2075. }
  2076. uint8_t Adafruit_NeoPixel::gamma8(uint8_t x) const {
  2077. return pgm_read_byte(&_gammaTable[x]); // 0-255 in, 0-255 out
  2078. }