Adafruit_NeoPixel.cpp 117 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*!
 * @file Adafruit_NeoPixel.cpp
 *
 * @mainpage Arduino Library for driving Adafruit NeoPixel addressable LEDs,
 * FLORA RGB Smart Pixels and compatible devicess -- WS2811, WS2812, WS2812B,
 * SK6812, etc.
 *
 * @section intro_sec Introduction
 *
 * This is the documentation for Adafruit's NeoPixel library for the
 * Arduino platform, allowing a broad range of microcontroller boards
 * (most AVR boards, many ARM devices, ESP8266 and ESP32, among others)
 * to control Adafruit NeoPixels, FLORA RGB Smart Pixels and compatible
 * devices -- WS2811, WS2812, WS2812B, SK6812, etc.
 *
 * Adafruit invests time and resources providing this open source code,
 * please support Adafruit and open-source hardware by purchasing products
 * from Adafruit!
 *
 * @section author Author
 *
 * Written by Phil "Paint Your Dragon" Burgess for Adafruit Industries,
 * with contributions by PJRC, Michael Miller and other members of the
 * open source community.
 *
 * @section license License
 *
 * This file is part of the Adafruit_NeoPixel library.
 *
 * Adafruit_NeoPixel is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Adafruit_NeoPixel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with NeoPixel. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include "Adafruit_NeoPixel.h"

#if defined(TARGET_LPC1768)
Eric Duminil's avatar
Eric Duminil committed
49
#include <time.h>
50
51
52
53
54
55
56
57
58
59
#endif

#if defined(NRF52) || defined(NRF52_SERIES)
#include "nrf.h"

// Interrupt is only disabled if there is no PWM device available
// Note: Adafruit Bluefruit nrf52 does not use this option
//#define NRF52_DISABLE_INT
#endif

Eric Duminil's avatar
Eric Duminil committed
60
61
62
63
64
65
66
67
#if defined(ARDUINO_ARCH_NRF52840)
#if defined __has_include
#if __has_include(<pinDefinitions.h>)
#include <pinDefinitions.h>
#endif
#endif
#endif

68
69
70
71
72
73
74
75
76
77
78
79
/*!
  @brief   NeoPixel constructor when length, pin and pixel type are known
           at compile-time.
  @param   n  Number of NeoPixels in strand.
  @param   p  Arduino pin number which will drive the NeoPixel data in.
  @param   t  Pixel type -- add together NEO_* constants defined in
              Adafruit_NeoPixel.h, for example NEO_GRB+NEO_KHZ800 for
              NeoPixels expecting an 800 KHz (vs 400 KHz) data stream
              with color bytes expressed in green, red, blue order per
              pixel.
  @return  Adafruit_NeoPixel object. Call the begin() function before use.
*/
Eric Duminil's avatar
Eric Duminil committed
80
81
Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t n, int16_t p, neoPixelType t)
    : begun(false), brightness(0), pixels(NULL), endTime(0) {
82
83
84
  updateType(t);
  updateLength(n);
  setPin(p);
Eric Duminil's avatar
Eric Duminil committed
85
86
87
88
89
90
91
92
93
94
#if defined(ARDUINO_ARCH_RP2040)
  // Find a free SM on one of the PIO's
  sm = pio_claim_unused_sm(pio, false); // don't panic
  // Try pio1 if SM not found
  if (sm < 0) {
    pio = pio1;
    sm = pio_claim_unused_sm(pio, true); // panic if no SM is free
  }
  init = true;
#endif
95
96
97
98
99
100
101
102
103
104
105
106
}

/*!
  @brief   "Empty" NeoPixel constructor when length, pin and/or pixel type
           are not known at compile-time, and must be initialized later with
           updateType(), updateLength() and setPin().
  @return  Adafruit_NeoPixel object. Call the begin() function before use.
  @note    This function is deprecated, here only for old projects that
           may still be calling it. New projects should instead use the
           'new' keyword with the first constructor syntax (length, pin,
           type).
*/
Eric Duminil's avatar
Eric Duminil committed
107
108
Adafruit_NeoPixel::Adafruit_NeoPixel()
    :
109
#if defined(NEO_KHZ400)
Eric Duminil's avatar
Eric Duminil committed
110
      is800KHz(true),
111
#endif
Eric Duminil's avatar
Eric Duminil committed
112
113
      begun(false), numLEDs(0), numBytes(0), pin(-1), brightness(0),
      pixels(NULL), rOffset(1), gOffset(0), bOffset(2), wOffset(1), endTime(0) {
114
115
116
117
118
119
120
}

/*!
  @brief   Deallocate Adafruit_NeoPixel object, set data pin back to INPUT.
*/
Adafruit_NeoPixel::~Adafruit_NeoPixel() {
  free(pixels);
Eric Duminil's avatar
Eric Duminil committed
121
122
  if (pin >= 0)
    pinMode(pin, INPUT);
123
124
125
126
127
128
}

/*!
  @brief   Configure NeoPixel pin for output.
*/
void Adafruit_NeoPixel::begin(void) {
Eric Duminil's avatar
Eric Duminil committed
129
  if (pin >= 0) {
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    pinMode(pin, OUTPUT);
    digitalWrite(pin, LOW);
  }
  begun = true;
}

/*!
  @brief   Change the length of a previously-declared Adafruit_NeoPixel
           strip object. Old data is deallocated and new data is cleared.
           Pin number and pixel format are unchanged.
  @param   n  New length of strip, in pixels.
  @note    This function is deprecated, here only for old projects that
           may still be calling it. New projects should instead use the
           'new' keyword with the first constructor syntax (length, pin,
           type).
*/
void Adafruit_NeoPixel::updateLength(uint16_t n) {
  free(pixels); // Free existing data (if any)

  // Allocate new data -- note: ALL PIXELS ARE CLEARED
  numBytes = n * ((wOffset == rOffset) ? 3 : 4);
Eric Duminil's avatar
Eric Duminil committed
151
  if ((pixels = (uint8_t *)malloc(numBytes))) {
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    memset(pixels, 0, numBytes);
    numLEDs = n;
  } else {
    numLEDs = numBytes = 0;
  }
}

/*!
  @brief   Change the pixel format of a previously-declared
           Adafruit_NeoPixel strip object. If format changes from one of
           the RGB variants to an RGBW variant (or RGBW to RGB), the old
           data will be deallocated and new data is cleared. Otherwise,
           the old data will remain in RAM and is not reordered to the
           new format, so it's advisable to follow up with clear().
  @param   t  Pixel type -- add together NEO_* constants defined in
              Adafruit_NeoPixel.h, for example NEO_GRB+NEO_KHZ800 for
              NeoPixels expecting an 800 KHz (vs 400 KHz) data stream
              with color bytes expressed in green, red, blue order per
              pixel.
  @note    This function is deprecated, here only for old projects that
           may still be calling it. New projects should instead use the
           'new' keyword with the first constructor syntax
           (length, pin, type).
*/
void Adafruit_NeoPixel::updateType(neoPixelType t) {
  bool oldThreeBytesPerPixel = (wOffset == rOffset); // false if RGBW

  wOffset = (t >> 6) & 0b11; // See notes in header file
  rOffset = (t >> 4) & 0b11; // regarding R/G/B/W offsets
  gOffset = (t >> 2) & 0b11;
Eric Duminil's avatar
Eric Duminil committed
182
  bOffset = t & 0b11;
183
#if defined(NEO_KHZ400)
Eric Duminil's avatar
Eric Duminil committed
184
  is800KHz = (t < 256); // 400 KHz flag is 1<<8
185
186
187
188
#endif

  // If bytes-per-pixel has changed (and pixel data was previously
  // allocated), re-allocate to new size. Will clear any data.
Eric Duminil's avatar
Eric Duminil committed
189
  if (pixels) {
190
    bool newThreeBytesPerPixel = (wOffset == rOffset);
Eric Duminil's avatar
Eric Duminil committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    if (newThreeBytesPerPixel != oldThreeBytesPerPixel)
      updateLength(numLEDs);
  }
}

// RP2040 specific driver
#if defined(ARDUINO_ARCH_RP2040)
void Adafruit_NeoPixel::rp2040Init(uint8_t pin, bool is800KHz)
{
  uint offset = pio_add_program(pio, &ws2812_program);

  if (is800KHz)
  {
    // 800kHz, 8 bit transfers
    ws2812_program_init(pio, sm, offset, pin, 800000, 8);
  }
  else
  {
    // 400kHz, 8 bit transfers
    ws2812_program_init(pio, sm, offset, pin, 400000, 8);
211
212
  }
}
Eric Duminil's avatar
Eric Duminil committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Not a user API
void  Adafruit_NeoPixel::rp2040Show(uint8_t pin, uint8_t *pixels, uint32_t numBytes, bool is800KHz)
{
  if (this->init)
  {
    // On first pass through initialise the PIO
    rp2040Init(pin, is800KHz);
    this->init = false;
  }

  while(numBytes--)
    // Bits for transmission must be shifted to top 8 bits
    pio_sm_put_blocking(pio, sm, ((uint32_t)*pixels++)<< 24);
}

#endif
229
230
231

#if defined(ESP8266)
// ESP8266 show() is external to enforce ICACHE_RAM_ATTR execution
Eric Duminil's avatar
Eric Duminil committed
232
233
extern "C" IRAM_ATTR void espShow(uint16_t pin, uint8_t *pixels,
                                  uint32_t numBytes, uint8_t type);
234
#elif defined(ESP32)
Eric Duminil's avatar
Eric Duminil committed
235
236
extern "C" void espShow(uint16_t pin, uint8_t *pixels, uint32_t numBytes,
                        uint8_t type);
237
238
#endif // ESP8266

Eric Duminil's avatar
Eric Duminil committed
239
#if defined(K210)
240
241
242
243
#define KENDRYTE_K210 1
#endif

#if defined(KENDRYTE_K210)
Eric Duminil's avatar
Eric Duminil committed
244
245
246
extern "C" void k210Show(uint8_t pin, uint8_t *pixels, uint32_t numBytes,
                         boolean is800KHz);
#endif // KENDRYTE_K210
247
248
249
250
251
252
253
254
255
256
257
258
259
/*!
  @brief   Transmit pixel data in RAM to NeoPixels.
  @note    On most architectures, interrupts are temporarily disabled in
           order to achieve the correct NeoPixel signal timing. This means
           that the Arduino millis() and micros() functions, which require
           interrupts, will lose small intervals of time whenever this
           function is called (about 30 microseconds per RGB pixel, 40 for
           RGBW pixels). There's no easy fix for this, but a few
           specialized alternative or companion libraries exist that use
           very device-specific peripherals to work around it.
*/
void Adafruit_NeoPixel::show(void) {

Eric Duminil's avatar
Eric Duminil committed
260
261
  if (!pixels)
    return;
262
263
264
265
266
267
268

  // Data latch = 300+ microsecond pause in the output stream. Rather than
  // put a delay at the end of the function, the ending time is noted and
  // the function will simply hold off (if needed) on issuing the
  // subsequent round of data until the latch time has elapsed. This
  // allows the mainline code to start generating the next frame of data
  // rather than stalling for the latch.
Eric Duminil's avatar
Eric Duminil committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
  while (!canShow())
    ;
    // endTime is a private member (rather than global var) so that multiple
    // instances on different pins can be quickly issued in succession (each
    // instance doesn't delay the next).

    // In order to make this code runtime-configurable to work with any pin,
    // SBI/CBI instructions are eschewed in favor of full PORT writes via the
    // OUT or ST instructions. It relies on two facts: that peripheral
    // functions (such as PWM) take precedence on output pins, so our PORT-
    // wide writes won't interfere, and that interrupts are globally disabled
    // while data is being issued to the LEDs, so no other code will be
    // accessing the PORT. The code takes an initial 'snapshot' of the PORT
    // state, computes 'pin high' and 'pin low' values, and writes these back
    // to the PORT register as needed.

    // NRF52 may use PWM + DMA (if available), may not need to disable interrupt
#if !(defined(NRF52) || defined(NRF52_SERIES))
287
288
289
290
  noInterrupts(); // Need 100% focus on instruction timing
#endif

#if defined(__AVR__)
Eric Duminil's avatar
Eric Duminil committed
291
  // AVR MCUs -- ATmega & ATtiny (no XMEGA) ---------------------------------
292

Eric Duminil's avatar
Eric Duminil committed
293
294
295
296
297
  volatile uint16_t i = numBytes; // Loop counter
  volatile uint8_t *ptr = pixels, // Pointer to next byte
      b = *ptr++,                 // Current byte value
      hi,                         // PORT w/output bit set high
      lo;                         // PORT w/output bit set low
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316

  // Hand-tuned assembly code issues data to the LED drivers at a specific
  // rate. There's separate code for different CPU speeds (8, 12, 16 MHz)
  // for both the WS2811 (400 KHz) and WS2812 (800 KHz) drivers. The
  // datastream timing for the LED drivers allows a little wiggle room each
  // way (listed in the datasheets), so the conditions for compiling each
  // case are set up for a range of frequencies rather than just the exact
  // 8, 12 or 16 MHz values, permitting use with some close-but-not-spot-on
  // devices (e.g. 16.5 MHz DigiSpark). The ranges were arrived at based
  // on the datasheet figures and have not been extensively tested outside
  // the canonical 8/12/16 MHz speeds; there's no guarantee these will work
  // close to the extremes (or possibly they could be pushed further).
  // Keep in mind only one CPU speed case actually gets compiled; the
  // resulting program isn't as massive as it might look from source here.

// 8 MHz(ish) AVR ---------------------------------------------------------
#if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL)

#if defined(NEO_KHZ400) // 800 KHz check needed only if 400 KHz support enabled
Eric Duminil's avatar
Eric Duminil committed
317
  if (is800KHz) {
318
319
#endif

Eric Duminil's avatar
Eric Duminil committed
320
    volatile uint8_t n1, n2 = 0; // First, next bits out
321
322
323
324
325
326
327
328
329
330

    // Squeezing an 800 KHz stream out of an 8 MHz chip requires code
    // specific to each PORT register.

    // 10 instruction clocks per bit: HHxxxxxLLL
    // OUT instructions:              ^ ^    ^   (T=0,2,7)

    // PORTD OUTPUT ----------------------------------------------------

#if defined(PORTD)
Eric Duminil's avatar
Eric Duminil committed
331
332
333
#if defined(PORTB) || defined(PORTC) || defined(PORTF)
    if (port == &PORTD) {
#endif // defined(PORTB/C/F)
334

Eric Duminil's avatar
Eric Duminil committed
335
      hi = PORTD | pinMask;
336
337
      lo = PORTD & ~pinMask;
      n1 = lo;
Eric Duminil's avatar
Eric Duminil committed
338
339
      if (b & 0x80)
        n1 = hi;
340
341
342
343
344
345
346
347

      // Dirty trick: RJMPs proceeding to the next instruction are used
      // to delay two clock cycles in one instruction word (rather than
      // using two NOPs). This was necessary in order to squeeze the
      // loop down to exactly 64 words -- the maximum possible for a
      // relative branch.

      asm volatile(
Eric Duminil's avatar
Eric Duminil committed
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
          "headD:"
          "\n\t" // Clk  Pseudocode
                 // Bit 7:
          "out  %[port] , %[hi]"
          "\n\t" // 1    PORT = hi
          "mov  %[n2]   , %[lo]"
          "\n\t" // 1    n2   = lo
          "out  %[port] , %[n1]"
          "\n\t" // 1    PORT = n1
          "rjmp .+0"
          "\n\t" // 2    nop nop
          "sbrc %[byte] , 6"
          "\n\t" // 1-2  if(b & 0x40)
          "mov %[n2]   , %[hi]"
          "\n\t" // 0-1   n2 = hi
          "out  %[port] , %[lo]"
          "\n\t" // 1    PORT = lo
          "rjmp .+0"
          "\n\t" // 2    nop nop
          // Bit 6:
          "out  %[port] , %[hi]"
          "\n\t" // 1    PORT = hi
          "mov  %[n1]   , %[lo]"
          "\n\t" // 1    n1   = lo
          "out  %[port] , %[n2]"
          "\n\t" // 1    PORT = n2
          "rjmp .+0"
          "\n\t" // 2    nop nop
          "sbrc %[byte] , 5"
          "\n\t" // 1-2  if(b & 0x20)
          "mov %[n1]   , %[hi]"
          "\n\t" // 0-1   n1 = hi
          "out  %[port] , %[lo]"
          "\n\t" // 1    PORT = lo
          "rjmp .+0"
          "\n\t" // 2    nop nop
          // Bit 5:
          "out  %[port] , %[hi]"
          "\n\t" // 1    PORT = hi
          "mov  %[n2]   , %[lo]"
          "\n\t" // 1    n2   = lo
          "out  %[port] , %[n1]"
          "\n\t" // 1    PORT = n1
          "rjmp .+0"
          "\n\t" // 2    nop nop
          "sbrc %[byte] , 4"
          "\n\t" // 1-2  if(b & 0x10)
          "mov %[n2]   , %[hi]"
          "\n\t" // 0-1   n2 = hi
          "out  %[port] , %[lo]"
          "\n\t" // 1    PORT = lo
          "rjmp .+0"
          "\n\t" // 2    nop nop
          // Bit 4:
          "out  %[port] , %[hi]"
          "\n\t" // 1    PORT = hi
          "mov  %[n1]   , %[lo]"
          "\n\t" // 1    n1   = lo
          "out  %[port] , %[n2]"
          "\n\t" // 1    PORT = n2
          "rjmp .+0"
          "\n\t" // 2    nop nop
          "sbrc %[byte] , 3"
          "\n\t" // 1-2  if(b & 0x08)
          "mov %[n1]   , %[hi]"
          "\n\t" // 0-1   n1 = hi
          "out  %[port] , %[lo]"
          "\n\t" // 1    PORT = lo
          "rjmp .+0"
          "\n\t" // 2    nop nop
          // Bit 3:
          "out  %[port] , %[hi]"
          "\n\t" // 1    PORT = hi
          "mov  %[n2]   , %[lo]"
          "\n\t" // 1    n2   = lo
          "out  %[port] , %[n1]"
          "\n\t" // 1    PORT = n1
          "rjmp .+0"
          "\n\t" // 2    nop nop
          "sbrc %[byte] , 2"
          "\n\t" // 1-2  if(b & 0x04)
          "mov %[n2]   , %[hi]"
          "\n\t" // 0-1   n2 = hi
          "out  %[port] , %[lo]"
          "\n\t" // 1    PORT = lo
          "rjmp .+0"
          "\n\t" // 2    nop nop
          // Bit 2:
          "out  %[port] , %[hi]"
          "\n\t" // 1    PORT = hi
          "mov  %[n1]   , %[lo]"
          "\n\t" // 1    n1   = lo
          "out  %[port] , %[n2]"
          "\n\t" // 1    PORT = n2
          "rjmp .+0"
          "\n\t" // 2    nop nop
          "sbrc %[byte] , 1"
          "\n\t" // 1-2  if(b & 0x02)
          "mov %[n1]   , %[hi]"
          "\n\t" // 0-1   n1 = hi
          "out  %[port] , %[lo]"
          "\n\t" // 1    PORT = lo
          "rjmp .+0"
          "\n\t" // 2    nop nop
          // Bit 1:
          "out  %[port] , %[hi]"
          "\n\t" // 1    PORT = hi
          "mov  %[n2]   , %[lo]"
          "\n\t" // 1    n2   = lo
          "out  %[port] , %[n1]"
          "\n\t" // 1    PORT = n1
          "rjmp .+0"
          "\n\t" // 2    nop nop
          "sbrc %[byte] , 0"
          "\n\t" // 1-2  if(b & 0x01)
          "mov %[n2]   , %[hi]"
          "\n\t" // 0-1   n2 = hi
          "out  %[port] , %[lo]"
          "\n\t" // 1    PORT = lo
          "sbiw %[count], 1"
          "\n\t" // 2    i-- (don't act on Z flag yet)
          // Bit 0:
          "out  %[port] , %[hi]"
          "\n\t" // 1    PORT = hi
          "mov  %[n1]   , %[lo]"
          "\n\t" // 1    n1   = lo
          "out  %[port] , %[n2]"
          "\n\t" // 1    PORT = n2
          "ld   %[byte] , %a[ptr]+"
          "\n\t" // 2    b = *ptr++
          "sbrc %[byte] , 7"
          "\n\t" // 1-2  if(b & 0x80)
          "mov %[n1]   , %[hi]"
          "\n\t" // 0-1   n1 = hi
          "out  %[port] , %[lo]"
          "\n\t" // 1    PORT = lo
          "brne headD"
          "\n" // 2    while(i) (Z flag set above)
          : [byte] "+r"(b), [n1] "+r"(n1), [n2] "+r"(n2), [count] "+w"(i)
          : [port] "I"(_SFR_IO_ADDR(PORTD)), [ptr] "e"(ptr), [hi] "r"(hi),
            [lo] "r"(lo));

#if defined(PORTB) || defined(PORTC) || defined(PORTF)
491
    } else // other PORT(s)
Eric Duminil's avatar
Eric Duminil committed
492
#endif // defined(PORTB/C/F)
493
494
495
496
497
#endif // defined(PORTD)

    // PORTB OUTPUT ----------------------------------------------------

#if defined(PORTB)
Eric Duminil's avatar
Eric Duminil committed
498
499
500
#if defined(PORTD) || defined(PORTC) || defined(PORTF)
        if (port == &PORTB) {
#endif // defined(PORTD/C/F)
501
502

      // Same as above, just switched to PORTB and stripped of comments.
Eric Duminil's avatar
Eric Duminil committed
503
      hi = PORTB | pinMask;
504
505
      lo = PORTB & ~pinMask;
      n1 = lo;
Eric Duminil's avatar
Eric Duminil committed
506
507
      if (b & 0x80)
        n1 = hi;
508
509

      asm volatile(
Eric Duminil's avatar
Eric Duminil committed
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
          "headB:"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 6"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 5"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 4"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 3"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 2"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 1"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 0"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "sbiw %[count], 1"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "ld   %[byte] , %a[ptr]+"
          "\n\t"
          "sbrc %[byte] , 7"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "brne headB"
          "\n"
          : [byte] "+r"(b), [n1] "+r"(n1), [n2] "+r"(n2), [count] "+w"(i)
          : [port] "I"(_SFR_IO_ADDR(PORTB)), [ptr] "e"(ptr), [hi] "r"(hi),
            [lo] "r"(lo));

#if defined(PORTD) || defined(PORTC) || defined(PORTF)
645
    }
Eric Duminil's avatar
Eric Duminil committed
646
647
#endif
#if defined(PORTC) || defined(PORTF)
648
    else
Eric Duminil's avatar
Eric Duminil committed
649
#endif // defined(PORTC/F)
650
651
652
653
654
#endif // defined(PORTB)

    // PORTC OUTPUT ----------------------------------------------------

#if defined(PORTC)
Eric Duminil's avatar
Eric Duminil committed
655
656
657
#if defined(PORTD) || defined(PORTB) || defined(PORTF)
        if (port == &PORTC) {
#endif // defined(PORTD/B/F)
658
659

      // Same as above, just switched to PORTC and stripped of comments.
Eric Duminil's avatar
Eric Duminil committed
660
      hi = PORTC | pinMask;
661
662
      lo = PORTC & ~pinMask;
      n1 = lo;
Eric Duminil's avatar
Eric Duminil committed
663
664
      if (b & 0x80)
        n1 = hi;
665
666

      asm volatile(
Eric Duminil's avatar
Eric Duminil committed
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
          "headC:"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 6"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 5"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 4"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 3"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 2"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 1"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 0"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "sbiw %[count], 1"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "ld   %[byte] , %a[ptr]+"
          "\n\t"
          "sbrc %[byte] , 7"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "brne headC"
          "\n"
          : [byte] "+r"(b), [n1] "+r"(n1), [n2] "+r"(n2), [count] "+w"(i)
          : [port] "I"(_SFR_IO_ADDR(PORTC)), [ptr] "e"(ptr), [hi] "r"(hi),
            [lo] "r"(lo));

#if defined(PORTD) || defined(PORTB) || defined(PORTF)
802
    }
Eric Duminil's avatar
Eric Duminil committed
803
804
#endif // defined(PORTD/B/F)
#if defined(PORTF)
805
    else
Eric Duminil's avatar
Eric Duminil committed
806
#endif
807
808
809
810
811
#endif // defined(PORTC)

    // PORTF OUTPUT ----------------------------------------------------

#if defined(PORTF)
Eric Duminil's avatar
Eric Duminil committed
812
813
814
#if defined(PORTD) || defined(PORTB) || defined(PORTC)
        if (port == &PORTF) {
#endif // defined(PORTD/B/C)
815

Eric Duminil's avatar
Eric Duminil committed
816
      hi = PORTF | pinMask;
817
818
      lo = PORTF & ~pinMask;
      n1 = lo;
Eric Duminil's avatar
Eric Duminil committed
819
820
      if (b & 0x80)
        n1 = hi;
821
822

      asm volatile(
Eric Duminil's avatar
Eric Duminil committed
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
          "headF:"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 6"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 5"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 4"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 3"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 2"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 1"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n2]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n1]"
          "\n\t"
          "rjmp .+0"
          "\n\t"
          "sbrc %[byte] , 0"
          "\n\t"
          "mov %[n2]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "sbiw %[count], 1"
          "\n\t"
          "out  %[port] , %[hi]"
          "\n\t"
          "mov  %[n1]   , %[lo]"
          "\n\t"
          "out  %[port] , %[n2]"
          "\n\t"
          "ld   %[byte] , %a[ptr]+"
          "\n\t"
          "sbrc %[byte] , 7"
          "\n\t"
          "mov %[n1]   , %[hi]"
          "\n\t"
          "out  %[port] , %[lo]"
          "\n\t"
          "brne headF"
          "\n"
          : [byte] "+r"(b), [n1] "+r"(n1), [n2] "+r"(n2), [count] "+w"(i)
          : [port] "I"(_SFR_IO_ADDR(PORTF)), [ptr] "e"(ptr), [hi] "r"(hi),
            [lo] "r"(lo));

#if defined(PORTD) || defined(PORTB) || defined(PORTC)
958
    }
Eric Duminil's avatar
Eric Duminil committed
959
#endif // defined(PORTD/B/C)
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
#endif // defined(PORTF)

#if defined(NEO_KHZ400)
  } else { // end 800 KHz, do 400 KHz

    // Timing is more relaxed; unrolling the inner loop for each bit is
    // not necessary. Still using the peculiar RJMPs as 2X NOPs, not out
    // of need but just to trim the code size down a little.
    // This 400-KHz-datastream-on-8-MHz-CPU code is not quite identical
    // to the 800-on-16 code later -- the hi/lo timing between WS2811 and
    // WS2812 is not simply a 2:1 scale!

    // 20 inst. clocks per bit: HHHHxxxxxxLLLLLLLLLL
    // ST instructions:         ^   ^     ^          (T=0,4,10)

    volatile uint8_t next, bit;

Eric Duminil's avatar
Eric Duminil committed
977
978
    hi = *port | pinMask;
    lo = *port & ~pinMask;
979
    next = lo;
Eric Duminil's avatar
Eric Duminil committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
    bit = 8;

    asm volatile("head20:"
                 "\n\t" // Clk  Pseudocode    (T =  0)
                 "st   %a[port], %[hi]"
                 "\n\t" // 2    PORT = hi     (T =  2)
                 "sbrc %[byte] , 7"
                 "\n\t" // 1-2  if(b & 128)
                 "mov  %[next], %[hi]"
                 "\n\t" // 0-1   next = hi    (T =  4)
                 "st   %a[port], %[next]"
                 "\n\t" // 2    PORT = next   (T =  6)
                 "mov  %[next] , %[lo]"
                 "\n\t" // 1    next = lo     (T =  7)
                 "dec  %[bit]"
                 "\n\t" // 1    bit--         (T =  8)
                 "breq nextbyte20"
                 "\n\t" // 1-2  if(bit == 0)
                 "rol  %[byte]"
                 "\n\t" // 1    b <<= 1       (T = 10)
                 "st   %a[port], %[lo]"
                 "\n\t" // 2    PORT = lo     (T = 12)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 14)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 16)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 18)
                 "rjmp head20"
                 "\n\t" // 2    -> head20 (next bit out)
                 "nextbyte20:"
                 "\n\t" //                    (T = 10)
                 "st   %a[port], %[lo]"
                 "\n\t" // 2    PORT = lo     (T = 12)
                 "nop"
                 "\n\t" // 1    nop           (T = 13)
                 "ldi  %[bit]  , 8"
                 "\n\t" // 1    bit = 8       (T = 14)
                 "ld   %[byte] , %a[ptr]+"
                 "\n\t" // 2    b = *ptr++    (T = 16)
                 "sbiw %[count], 1"
                 "\n\t" // 2    i--           (T = 18)
                 "brne head20"
                 "\n" // 2    if(i != 0) -> (next byte)
                 : [port] "+e"(port), [byte] "+r"(b), [bit] "+r"(bit),
                   [next] "+r"(next), [count] "+w"(i)
                 : [hi] "r"(hi), [lo] "r"(lo), [ptr] "e"(ptr));
1027
1028
1029
1030
1031
1032
1033
  }
#endif // NEO_KHZ400

// 12 MHz(ish) AVR --------------------------------------------------------
#elif (F_CPU >= 11100000UL) && (F_CPU <= 14300000UL)

#if defined(NEO_KHZ400) // 800 KHz check needed only if 400 KHz support enabled
Eric Duminil's avatar
Eric Duminil committed
1034
  if (is800KHz) {
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
#endif

    // In the 12 MHz case, an optimized 800 KHz datastream (no dead time
    // between bytes) requires a PORT-specific loop similar to the 8 MHz
    // code (but a little more relaxed in this case).

    // 15 instruction clocks per bit: HHHHxxxxxxLLLLL
    // OUT instructions:              ^   ^     ^     (T=0,4,10)

    volatile uint8_t next;

    // PORTD OUTPUT ----------------------------------------------------

#if defined(PORTD)
Eric Duminil's avatar
Eric Duminil committed
1049
1050
1051
#if defined(PORTB) || defined(PORTC) || defined(PORTF)
    if (port == &PORTD) {
#endif // defined(PORTB/C/F)
1052

Eric Duminil's avatar
Eric Duminil committed
1053
1054
      hi = PORTD | pinMask;
      lo = PORTD & ~pinMask;
1055
      next = lo;
Eric Duminil's avatar
Eric Duminil committed
1056
1057
      if (b & 0x80)
        next = hi;
1058
1059
1060

      // Don't "optimize" the OUT calls into the bitTime subroutine;
      // we're exploiting the RCALL and RET as 3- and 4-cycle NOPs!
Eric Duminil's avatar
Eric Duminil committed
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
      asm volatile("headD:"
                   "\n\t" //        (T =  0)
                   "out   %[port], %[hi]"
                   "\n\t" //        (T =  1)
                   "rcall bitTimeD"
                   "\n\t" // Bit 7  (T = 15)
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeD"
                   "\n\t" // Bit 6
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeD"
                   "\n\t" // Bit 5
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeD"
                   "\n\t" // Bit 4
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeD"
                   "\n\t" // Bit 3
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeD"
                   "\n\t" // Bit 2
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeD"
                   "\n\t" // Bit 1
                   // Bit 0:
                   "out  %[port] , %[hi]"
                   "\n\t" // 1    PORT = hi    (T =  1)
                   "rjmp .+0"
                   "\n\t" // 2    nop nop      (T =  3)
                   "ld   %[byte] , %a[ptr]+"
                   "\n\t" // 2    b = *ptr++   (T =  5)
                   "out  %[port] , %[next]"
                   "\n\t" // 1    PORT = next  (T =  6)
                   "mov  %[next] , %[lo]"
                   "\n\t" // 1    next = lo    (T =  7)
                   "sbrc %[byte] , 7"
                   "\n\t" // 1-2  if(b & 0x80) (T =  8)
                   "mov %[next] , %[hi]"
                   "\n\t" // 0-1    next = hi  (T =  9)
                   "nop"
                   "\n\t" // 1                 (T = 10)
                   "out  %[port] , %[lo]"
                   "\n\t" // 1    PORT = lo    (T = 11)
                   "sbiw %[count], 1"
                   "\n\t" // 2    i--          (T = 13)
                   "brne headD"
                   "\n\t" // 2    if(i != 0) -> (next byte)
                   "rjmp doneD"
                   "\n\t"
                   "bitTimeD:"
                   "\n\t" //      nop nop nop     (T =  4)
                   "out  %[port], %[next]"
                   "\n\t" // 1    PORT = next     (T =  5)
                   "mov  %[next], %[lo]"
                   "\n\t" // 1    next = lo       (T =  6)
                   "rol  %[byte]"
                   "\n\t" // 1    b <<= 1         (T =  7)
                   "sbrc %[byte], 7"
                   "\n\t" // 1-2  if(b & 0x80)    (T =  8)
                   "mov %[next], %[hi]"
                   "\n\t" // 0-1   next = hi      (T =  9)
                   "nop"
                   "\n\t" // 1                    (T = 10)
                   "out  %[port], %[lo]"
                   "\n\t" // 1    PORT = lo       (T = 11)
                   "ret"
                   "\n\t" // 4    nop nop nop nop (T = 15)
                   "doneD:"
                   "\n"
                   : [byte] "+r"(b), [next] "+r"(next), [count] "+w"(i)
                   : [port] "I"(_SFR_IO_ADDR(PORTD)), [ptr] "e"(ptr),
                     [hi] "r"(hi), [lo] "r"(lo));

#if defined(PORTB) || defined(PORTC) || defined(PORTF)
1141
    } else // other PORT(s)
Eric Duminil's avatar
Eric Duminil committed
1142
#endif // defined(PORTB/C/F)
1143
1144
1145
1146
1147
#endif // defined(PORTD)

    // PORTB OUTPUT ----------------------------------------------------

#if defined(PORTB)
Eric Duminil's avatar
Eric Duminil committed
1148
1149
1150
#if defined(PORTD) || defined(PORTC) || defined(PORTF)
        if (port == &PORTB) {
#endif // defined(PORTD/C/F)
1151

Eric Duminil's avatar
Eric Duminil committed
1152
1153
      hi = PORTB | pinMask;
      lo = PORTB & ~pinMask;
1154
      next = lo;
Eric Duminil's avatar
Eric Duminil committed
1155
1156
      if (b & 0x80)
        next = hi;
1157
1158

      // Same as above, just set for PORTB & stripped of comments
Eric Duminil's avatar
Eric Duminil committed
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
      asm volatile("headB:"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeB"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeB"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeB"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeB"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeB"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeB"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeB"
                   "\n\t"
                   "out  %[port] , %[hi]"
                   "\n\t"
                   "rjmp .+0"
                   "\n\t"
                   "ld   %[byte] , %a[ptr]+"
                   "\n\t"
                   "out  %[port] , %[next]"
                   "\n\t"
                   "mov  %[next] , %[lo]"
                   "\n\t"
                   "sbrc %[byte] , 7"
                   "\n\t"
                   "mov %[next] , %[hi]"
                   "\n\t"
                   "nop"
                   "\n\t"
                   "out  %[port] , %[lo]"
                   "\n\t"
                   "sbiw %[count], 1"
                   "\n\t"
                   "brne headB"
                   "\n\t"
                   "rjmp doneB"
                   "\n\t"
                   "bitTimeB:"
                   "\n\t"
                   "out  %[port], %[next]"
                   "\n\t"
                   "mov  %[next], %[lo]"
                   "\n\t"
                   "rol  %[byte]"
                   "\n\t"
                   "sbrc %[byte], 7"
                   "\n\t"
                   "mov %[next], %[hi]"
                   "\n\t"
                   "nop"
                   "\n\t"
                   "out  %[port], %[lo]"
                   "\n\t"
                   "ret"
                   "\n\t"
                   "doneB:"
                   "\n"
                   : [byte] "+r"(b), [next] "+r"(next), [count] "+w"(i)
                   : [port] "I"(_SFR_IO_ADDR(PORTB)), [ptr] "e"(ptr),
                     [hi] "r"(hi), [lo] "r"(lo));

#if defined(PORTD) || defined(PORTC) || defined(PORTF)
1238
    }
Eric Duminil's avatar
Eric Duminil committed
1239
1240
#endif
#if defined(PORTC) || defined(PORTF)
1241
    else
Eric Duminil's avatar
Eric Duminil committed
1242
#endif // defined(PORTC/F)
1243
1244
1245
1246
1247
#endif // defined(PORTB)

    // PORTC OUTPUT ----------------------------------------------------

#if defined(PORTC)
Eric Duminil's avatar
Eric Duminil committed
1248
1249
1250
#if defined(PORTD) || defined(PORTB) || defined(PORTF)
        if (port == &PORTC) {
#endif // defined(PORTD/B/F)
1251

Eric Duminil's avatar
Eric Duminil committed
1252
1253
      hi = PORTC | pinMask;
      lo = PORTC & ~pinMask;
1254
      next = lo;
Eric Duminil's avatar
Eric Duminil committed
1255
1256
      if (b & 0x80)
        next = hi;
1257
1258

      // Same as above, just set for PORTC & stripped of comments
Eric Duminil's avatar
Eric Duminil committed
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
      asm volatile("headC:"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out  %[port] , %[hi]"
                   "\n\t"
                   "rjmp .+0"
                   "\n\t"
                   "ld   %[byte] , %a[ptr]+"
                   "\n\t"
                   "out  %[port] , %[next]"
                   "\n\t"
                   "mov  %[next] , %[lo]"
                   "\n\t"
                   "sbrc %[byte] , 7"
                   "\n\t"
                   "mov %[next] , %[hi]"
                   "\n\t"
                   "nop"
                   "\n\t"
                   "out  %[port] , %[lo]"
                   "\n\t"
                   "sbiw %[count], 1"
                   "\n\t"
                   "brne headC"
                   "\n\t"
                   "rjmp doneC"
                   "\n\t"
                   "bitTimeC:"
                   "\n\t"
                   "out  %[port], %[next]"
                   "\n\t"
                   "mov  %[next], %[lo]"
                   "\n\t"
                   "rol  %[byte]"
                   "\n\t"
                   "sbrc %[byte], 7"
                   "\n\t"
                   "mov %[next], %[hi]"
                   "\n\t"
                   "nop"
                   "\n\t"
                   "out  %[port], %[lo]"
                   "\n\t"
                   "ret"
                   "\n\t"
                   "doneC:"
                   "\n"
                   : [byte] "+r"(b), [next] "+r"(next), [count] "+w"(i)
                   : [port] "I"(_SFR_IO_ADDR(PORTC)), [ptr] "e"(ptr),
                     [hi] "r"(hi), [lo] "r"(lo));

#if defined(PORTD) || defined(PORTB) || defined(PORTF)
1338
    }
Eric Duminil's avatar
Eric Duminil committed
1339
1340
#endif // defined(PORTD/B/F)
#if defined(PORTF)
1341
    else
Eric Duminil's avatar
Eric Duminil committed
1342
#endif
1343
1344
1345
1346
1347
#endif // defined(PORTC)

    // PORTF OUTPUT ----------------------------------------------------

#if defined(PORTF)
Eric Duminil's avatar
Eric Duminil committed
1348
1349
1350
#if defined(PORTD) || defined(PORTB) || defined(PORTC)
        if (port == &PORTF) {
#endif // defined(PORTD/B/C)
1351

Eric Duminil's avatar
Eric Duminil committed
1352
1353
      hi = PORTF | pinMask;
      lo = PORTF & ~pinMask;
1354
      next = lo;
Eric Duminil's avatar
Eric Duminil committed
1355
1356
      if (b & 0x80)
        next = hi;
1357
1358

      // Same as above, just set for PORTF & stripped of comments
Eric Duminil's avatar
Eric Duminil committed
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
      asm volatile("headF:"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out   %[port], %[hi]"
                   "\n\t"
                   "rcall bitTimeC"
                   "\n\t"
                   "out  %[port] , %[hi]"
                   "\n\t"
                   "rjmp .+0"
                   "\n\t"
                   "ld   %[byte] , %a[ptr]+"
                   "\n\t"
                   "out  %[port] , %[next]"
                   "\n\t"
                   "mov  %[next] , %[lo]"
                   "\n\t"
                   "sbrc %[byte] , 7"
                   "\n\t"
                   "mov %[next] , %[hi]"
                   "\n\t"
                   "nop"
                   "\n\t"
                   "out  %[port] , %[lo]"
                   "\n\t"
                   "sbiw %[count], 1"
                   "\n\t"
                   "brne headF"
                   "\n\t"
                   "rjmp doneC"
                   "\n\t"
                   "bitTimeC:"
                   "\n\t"
                   "out  %[port], %[next]"
                   "\n\t"
                   "mov  %[next], %[lo]"
                   "\n\t"
                   "rol  %[byte]"
                   "\n\t"
                   "sbrc %[byte], 7"
                   "\n\t"
                   "mov %[next], %[hi]"
                   "\n\t"
                   "nop"
                   "\n\t"
                   "out  %[port], %[lo]"
                   "\n\t"
                   "ret"
                   "\n\t"
                   "doneC:"
                   "\n"
                   : [byte] "+r"(b), [next] "+r"(next), [count] "+w"(i)
                   : [port] "I"(_SFR_IO_ADDR(PORTF)), [ptr] "e"(ptr),
                     [hi] "r"(hi), [lo] "r"(lo));

#if defined(PORTD) || defined(PORTB) || defined(PORTC)
1438
    }
Eric Duminil's avatar
Eric Duminil committed
1439
#endif // defined(PORTD/B/C)
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
#endif // defined(PORTF)

#if defined(NEO_KHZ400)
  } else { // 400 KHz

    // 30 instruction clocks per bit: HHHHHHxxxxxxxxxLLLLLLLLLLLLLLL
    // ST instructions:               ^     ^        ^    (T=0,6,15)

    volatile uint8_t next, bit;

Eric Duminil's avatar
Eric Duminil committed
1450
1451
    hi = *port | pinMask;
    lo = *port & ~pinMask;
1452
    next = lo;
Eric Duminil's avatar
Eric Duminil committed
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
    bit = 8;

    asm volatile("head30:"
                 "\n\t" // Clk  Pseudocode    (T =  0)
                 "st   %a[port], %[hi]"
                 "\n\t" // 2    PORT = hi     (T =  2)
                 "sbrc %[byte] , 7"
                 "\n\t" // 1-2  if(b & 128)
                 "mov  %[next], %[hi]"
                 "\n\t" // 0-1   next = hi    (T =  4)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T =  6)
                 "st   %a[port], %[next]"
                 "\n\t" // 2    PORT = next   (T =  8)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 10)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 12)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 14)
                 "nop"
                 "\n\t" // 1    nop           (T = 15)
                 "st   %a[port], %[lo]"
                 "\n\t" // 2    PORT = lo     (T = 17)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 19)
                 "dec  %[bit]"
                 "\n\t" // 1    bit--         (T = 20)
                 "breq nextbyte30"
                 "\n\t" // 1-2  if(bit == 0)
                 "rol  %[byte]"
                 "\n\t" // 1    b <<= 1       (T = 22)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 24)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 26)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 28)
                 "rjmp head30"
                 "\n\t" // 2    -> head30 (next bit out)
                 "nextbyte30:"
                 "\n\t" //                    (T = 22)
                 "nop"
                 "\n\t" // 1    nop           (T = 23)
                 "ldi  %[bit]  , 8"
                 "\n\t" // 1    bit = 8       (T = 24)
                 "ld   %[byte] , %a[ptr]+"
                 "\n\t" // 2    b = *ptr++    (T = 26)
                 "sbiw %[count], 1"
                 "\n\t" // 2    i--           (T = 28)
                 "brne head30"
                 "\n" // 1-2  if(i != 0) -> (next byte)
                 : [port] "+e"(port), [byte] "+r"(b), [bit] "+r"(bit),
                   [next] "+r"(next), [count] "+w"(i)
                 : [hi] "r"(hi), [lo] "r"(lo), [ptr] "e"(ptr));
1508
1509
1510
1511
1512
1513
1514
  }
#endif // NEO_KHZ400

// 16 MHz(ish) AVR --------------------------------------------------------
#elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L)

#if defined(NEO_KHZ400) // 800 KHz check needed only if 400 KHz support enabled
Eric Duminil's avatar
Eric Duminil committed
1515
  if (is800KHz) {
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
#endif

    // WS2811 and WS2812 have different hi/lo duty cycles; this is
    // similar but NOT an exact copy of the prior 400-on-8 code.

    // 20 inst. clocks per bit: HHHHHxxxxxxxxLLLLLLL
    // ST instructions:         ^   ^        ^       (T=0,5,13)

    volatile uint8_t next, bit;

Eric Duminil's avatar
Eric Duminil committed
1526
1527
    hi = *port | pinMask;
    lo = *port & ~pinMask;
1528
    next = lo;
Eric Duminil's avatar
Eric Duminil committed
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
    bit = 8;

    asm volatile("head20:"
                 "\n\t" // Clk  Pseudocode    (T =  0)
                 "st   %a[port],  %[hi]"
                 "\n\t" // 2    PORT = hi     (T =  2)
                 "sbrc %[byte],  7"
                 "\n\t" // 1-2  if(b & 128)
                 "mov  %[next], %[hi]"
                 "\n\t" // 0-1   next = hi    (T =  4)
                 "dec  %[bit]"
                 "\n\t" // 1    bit--         (T =  5)
                 "st   %a[port],  %[next]"
                 "\n\t" // 2    PORT = next   (T =  7)
                 "mov  %[next] ,  %[lo]"
                 "\n\t" // 1    next = lo     (T =  8)
                 "breq nextbyte20"
                 "\n\t" // 1-2  if(bit == 0) (from dec above)
                 "rol  %[byte]"
                 "\n\t" // 1    b <<= 1       (T = 10)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 12)
                 "nop"
                 "\n\t" // 1    nop           (T = 13)
                 "st   %a[port],  %[lo]"
                 "\n\t" // 2    PORT = lo     (T = 15)
                 "nop"
                 "\n\t" // 1    nop           (T = 16)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 18)
                 "rjmp head20"
                 "\n\t" // 2    -> head20 (next bit out)
                 "nextbyte20:"
                 "\n\t" //                    (T = 10)
                 "ldi  %[bit]  ,  8"
                 "\n\t" // 1    bit = 8       (T = 11)
                 "ld   %[byte] ,  %a[ptr]+"
                 "\n\t" // 2    b = *ptr++    (T = 13)
                 "st   %a[port], %[lo]"
                 "\n\t" // 2    PORT = lo     (T = 15)
                 "nop"
                 "\n\t" // 1    nop           (T = 16)
                 "sbiw %[count], 1"
                 "\n\t" // 2    i--           (T = 18)
                 "brne head20"
                 "\n" // 2    if(i != 0) -> (next byte)
                 : [port] "+e"(port), [byte] "+r"(b), [bit] "+r"(bit),
                   [next] "+r"(next), [count] "+w"(i)
                 : [ptr] "e"(ptr), [hi] "r"(hi), [lo] "r"(lo));
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588

#if defined(NEO_KHZ400)
  } else { // 400 KHz

    // The 400 KHz clock on 16 MHz MCU is the most 'relaxed' version.

    // 40 inst. clocks per bit: HHHHHHHHxxxxxxxxxxxxLLLLLLLLLLLLLLLLLLLL
    // ST instructions:         ^       ^           ^         (T=0,8,20)

    volatile uint8_t next, bit;

Eric Duminil's avatar
Eric Duminil committed
1589
1590
    hi = *port | pinMask;
    lo = *port & ~pinMask;
1591
    next = lo;
Eric Duminil's avatar
Eric Duminil committed
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
    bit = 8;

    asm volatile("head40:"
                 "\n\t" // Clk  Pseudocode    (T =  0)
                 "st   %a[port], %[hi]"
                 "\n\t" // 2    PORT = hi     (T =  2)
                 "sbrc %[byte] , 7"
                 "\n\t" // 1-2  if(b & 128)
                 "mov  %[next] , %[hi]"
                 "\n\t" // 0-1   next = hi    (T =  4)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T =  6)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T =  8)
                 "st   %a[port], %[next]"
                 "\n\t" // 2    PORT = next   (T = 10)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 12)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 14)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 16)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 18)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 20)
                 "st   %a[port], %[lo]"
                 "\n\t" // 2    PORT = lo     (T = 22)
                 "nop"
                 "\n\t" // 1    nop           (T = 23)
                 "mov  %[next] , %[lo]"
                 "\n\t" // 1    next = lo     (T = 24)
                 "dec  %[bit]"
                 "\n\t" // 1    bit--         (T = 25)
                 "breq nextbyte40"
                 "\n\t" // 1-2  if(bit == 0)
                 "rol  %[byte]"
                 "\n\t" // 1    b <<= 1       (T = 27)
                 "nop"
                 "\n\t" // 1    nop           (T = 28)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 30)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 32)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 34)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 36)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 38)
                 "rjmp head40"
                 "\n\t" // 2    -> head40 (next bit out)
                 "nextbyte40:"
                 "\n\t" //                    (T = 27)
                 "ldi  %[bit]  , 8"
                 "\n\t" // 1    bit = 8       (T = 28)
                 "ld   %[byte] , %a[ptr]+"
                 "\n\t" // 2    b = *ptr++    (T = 30)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 32)
                 "st   %a[port], %[lo]"
                 "\n\t" // 2    PORT = lo     (T = 34)
                 "rjmp .+0"
                 "\n\t" // 2    nop nop       (T = 36)
                 "sbiw %[count], 1"
                 "\n\t" // 2    i--           (T = 38)
                 "brne head40"
                 "\n" // 1-2  if(i != 0) -> (next byte)
                 : [port] "+e"(port), [byte] "+r"(b), [bit] "+r"(bit),
                   [next] "+r"(next), [count] "+w"(i)
                 : [ptr] "e"(ptr), [hi] "r"(hi), [lo] "r"(lo));
1663
1664
1665
1666
  }
#endif // NEO_KHZ400

#else
Eric Duminil's avatar
Eric Duminil committed
1667
#error "CPU SPEED NOT SUPPORTED"
1668
1669
#endif // end F_CPU ifdefs on __AVR__

Eric Duminil's avatar
Eric Duminil committed
1670
  // END AVR ----------------------------------------------------------------
1671
1672
1673

#elif defined(__arm__)

Eric Duminil's avatar
Eric Duminil committed
1674
1675
1676
1677
1678
    // ARM MCUs -- Teensy 3.0, 3.1, LC, Arduino Due, RP2040 -------------------

#if defined(ARDUINO_ARCH_RP2040)
  // Use PIO
  rp2040Show(pin, pixels, numBytes, is800KHz);
1679

Eric Duminil's avatar
Eric Duminil committed
1680
1681
1682
1683
1684
1685
1686
1687
#elif defined(TEENSYDUINO) &&                                                  \
    defined(KINETISK) // Teensy 3.0, 3.1, 3.2, 3.5, 3.6
#define CYCLES_800_T0H (F_CPU / 4000000)
#define CYCLES_800_T1H (F_CPU / 1250000)
#define CYCLES_800 (F_CPU / 800000)
#define CYCLES_400_T0H (F_CPU / 2000000)
#define CYCLES_400_T1H (F_CPU / 833333)
#define CYCLES_400 (F_CPU / 400000)
1688

Eric Duminil's avatar
Eric Duminil committed
1689
1690
1691
  uint8_t *p = pixels, *end = p + numBytes, pix, mask;
  volatile uint8_t *set = portSetRegister(pin), *clr = portClearRegister(pin);
  uint32_t cyc;
1692

Eric Duminil's avatar
Eric Duminil committed
1693
  ARM_DEMCR |= ARM_DEMCR_TRCENA;
1694
1695
1696
  ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;

#if defined(NEO_KHZ400) // 800 KHz check needed only if 400 KHz support enabled
Eric Duminil's avatar
Eric Duminil committed
1697
  if (is800KHz) {
1698
1699
#endif
    cyc = ARM_DWT_CYCCNT + CYCLES_800;
Eric Duminil's avatar
Eric Duminil committed
1700
    while (p < end) {
1701
      pix = *p++;
Eric Duminil's avatar
Eric Duminil committed
1702
1703
1704
1705
      for (mask = 0x80; mask; mask >>= 1) {
        while (ARM_DWT_CYCCNT - cyc < CYCLES_800)
          ;
        cyc = ARM_DWT_CYCCNT;
1706
        *set = 1;
Eric Duminil's avatar
Eric Duminil committed
1707
1708
1709
        if (pix & mask) {
          while (ARM_DWT_CYCCNT - cyc < CYCLES_800_T1H)
            ;
1710
        } else {
Eric Duminil's avatar
Eric Duminil committed
1711
1712
          while (ARM_DWT_CYCCNT - cyc < CYCLES_800_T0H)
            ;
1713
1714
1715
1716
        }
        *clr = 1;
      }
    }
Eric Duminil's avatar
Eric Duminil committed
1717
1718
    while (ARM_DWT_CYCCNT - cyc < CYCLES_800)
      ;
1719
1720
1721
#if defined(NEO_KHZ400)
  } else { // 400 kHz bitstream
    cyc = ARM_DWT_CYCCNT + CYCLES_400;
Eric Duminil's avatar
Eric Duminil committed
1722
    while (p < end) {
1723
      pix = *p++;
Eric Duminil's avatar
Eric Duminil committed
1724
1725
1726
1727
      for (mask = 0x80; mask; mask >>= 1) {
        while (ARM_DWT_CYCCNT - cyc < CYCLES_400)
          ;
        cyc = ARM_DWT_CYCCNT;
1728
        *set = 1;
Eric Duminil's avatar
Eric Duminil committed
1729
1730
1731
        if (pix & mask) {
          while (ARM_DWT_CYCCNT - cyc < CYCLES_400_T1H)
            ;
1732
        } else {
Eric Duminil's avatar
Eric Duminil committed
1733
1734
          while (ARM_DWT_CYCCNT - cyc < CYCLES_400_T0H)
            ;
1735
1736
1737
1738
        }
        *clr = 1;
      }
    }
Eric Duminil's avatar
Eric Duminil committed
1739
1740
    while (ARM_DWT_CYCCNT - cyc < CYCLES_400)
      ;
1741
1742
1743
1744
  }
#endif // NEO_KHZ400

#elif defined(TEENSYDUINO) && (defined(__IMXRT1052__) || defined(__IMXRT1062__))
Eric Duminil's avatar
Eric Duminil committed
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
#define CYCLES_800_T0H (F_CPU_ACTUAL / 4000000)
#define CYCLES_800_T1H (F_CPU_ACTUAL / 1250000)
#define CYCLES_800 (F_CPU_ACTUAL / 800000)
#define CYCLES_400_T0H (F_CPU_ACTUAL / 2000000)
#define CYCLES_400_T1H (F_CPU_ACTUAL / 833333)
#define CYCLES_400 (F_CPU_ACTUAL / 400000)

  uint8_t *p = pixels, *end = p + numBytes, pix, mask;
  volatile uint32_t *set = portSetRegister(pin), *clr = portClearRegister(pin);
  uint32_t cyc, msk = digitalPinToBitMask(pin);

  ARM_DEMCR |= ARM_DEMCR_TRCENA;
1757
1758
1759
  ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;

#if defined(NEO_KHZ400) // 800 KHz check needed only if 400 KHz support enabled
Eric Duminil's avatar
Eric Duminil committed
1760
  if (is800KHz) {
1761
1762
#endif
    cyc = ARM_DWT_CYCCNT + CYCLES_800;
Eric Duminil's avatar
Eric Duminil committed
1763
    while (p < end) {
1764
      pix = *p++;
Eric Duminil's avatar
Eric Duminil committed
1765
1766
1767
1768
      for (mask = 0x80; mask; mask >>= 1) {
        while (ARM_DWT_CYCCNT - cyc < CYCLES_800)
          ;
        cyc = ARM_DWT_CYCCNT;
1769
        *set = msk;
Eric Duminil's avatar
Eric Duminil committed
1770
1771
1772
        if (pix & mask) {
          while (ARM_DWT_CYCCNT - cyc < CYCLES_800_T1H)
            ;
1773
        } else {
Eric Duminil's avatar
Eric Duminil committed
1774
1775
          while (ARM_DWT_CYCCNT - cyc < CYCLES_800_T0H)
            ;
1776
1777
1778
1779
        }
        *clr = msk;
      }
    }
Eric Duminil's avatar
Eric Duminil committed
1780
1781
    while (ARM_DWT_CYCCNT - cyc < CYCLES_800)
      ;
1782
1783
1784
#if defined(NEO_KHZ400)
  } else { // 400 kHz bitstream
    cyc = ARM_DWT_CYCCNT + CYCLES_400;
Eric Duminil's avatar
Eric Duminil committed
1785
    while (p < end) {
1786
      pix = *p++;
Eric Duminil's avatar
Eric Duminil committed
1787
1788
1789
1790
      for (mask = 0x80; mask; mask >>= 1) {
        while (ARM_DWT_CYCCNT - cyc < CYCLES_400)
          ;
        cyc = ARM_DWT_CYCCNT;
1791
        *set = msk;
Eric Duminil's avatar
Eric Duminil committed
1792
1793
1794
        if (pix & mask) {
          while (ARM_DWT_CYCCNT - cyc < CYCLES_400_T1H)
            ;
1795
        } else {
Eric Duminil's avatar
Eric Duminil committed
1796
1797
          while (ARM_DWT_CYCCNT - cyc < CYCLES_400_T0H)
            ;
1798
1799
1800
1801
        }
        *clr = msk;
      }
    }
Eric Duminil's avatar
Eric Duminil committed
1802
1803
    while (ARM_DWT_CYCCNT - cyc < CYCLES_400)
      ;
1804
1805
1806
1807
1808
1809
  }
#endif // NEO_KHZ400

#elif defined(TEENSYDUINO) && defined(__MKL26Z64__) // Teensy-LC

#if F_CPU == 48000000
Eric Duminil's avatar
Eric Duminil committed
1810
  uint8_t *p = pixels, pix, count, dly, bitmask = digitalPinToBitMask(pin);
1811
  volatile uint8_t *reg = portSetRegister(pin);
Eric Duminil's avatar
Eric Duminil committed
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
  uint32_t num = numBytes;
  asm volatile("L%=_begin:"
               "\n\t"
               "ldrb  %[pix], [%[p], #0]"
               "\n\t"
               "lsl   %[pix], #24"
               "\n\t"
               "movs  %[count], #7"
               "\n\t"
               "L%=_loop:"
               "\n\t"
               "lsl   %[pix], #1"
               "\n\t"
               "bcs   L%=_loop_one"
               "\n\t"
               "L%=_loop_zero:"
               "\n\t"
               "strb  %[bitmask], [%[reg], #0]"
               "\n\t"
               "movs  %[dly], #4"
               "\n\t"
               "L%=_loop_delay_T0H:"
               "\n\t"
               "sub   %[dly], #1"
               "\n\t"
               "bne   L%=_loop_delay_T0H"
               "\n\t"
               "strb  %[bitmask], [%[reg], #4]"
               "\n\t"
               "movs  %[dly], #13"
               "\n\t"
               "L%=_loop_delay_T0L:"
               "\n\t"
               "sub   %[dly], #1"
               "\n\t"
               "bne   L%=_loop_delay_T0L"
               "\n\t"
               "b     L%=_next"
               "\n\t"
               "L%=_loop_one:"
               "\n\t"
               "strb  %[bitmask], [%[reg], #0]"
               "\n\t"
               "movs  %[dly], #13"
               "\n\t"
               "L%=_loop_delay_T1H:"
               "\n\t"
               "sub   %[dly], #1"
               "\n\t"
               "bne   L%=_loop_delay_T1H"
               "\n\t"
               "strb  %[bitmask], [%[reg], #4]"
               "\n\t"
               "movs  %[dly], #4"
               "\n\t"
               "L%=_loop_delay_T1L:"
               "\n\t"
               "sub   %[dly], #1"
               "\n\t"
               "bne   L%=_loop_delay_T1L"
               "\n\t"
               "nop"
               "\n\t"
               "L%=_next:"
               "\n\t"
               "sub   %[count], #1"
               "\n\t"
               "bne   L%=_loop"
               "\n\t"
               "lsl   %[pix], #1"
               "\n\t"
               "bcs   L%=_last_one"
               "\n\t"
               "L%=_last_zero:"
               "\n\t"
               "strb  %[bitmask], [%[reg], #0]"
               "\n\t"
               "movs  %[dly], #4"
               "\n\t"
               "L%=_last_delay_T0H:"
               "\n\t"
               "sub   %[dly], #1"
               "\n\t"
               "bne   L%=_last_delay_T0H"
               "\n\t"
               "strb  %[bitmask], [%[reg], #4]"
               "\n\t"
               "movs  %[dly], #10"
               "\n\t"
               "L%=_last_delay_T0L:"
               "\n\t"
               "sub   %[dly], #1"
               "\n\t"
               "bne   L%=_last_delay_T0L"
               "\n\t"
               "b     L%=_repeat"
               "\n\t"
               "L%=_last_one:"
               "\n\t"
               "strb  %[bitmask], [%[reg], #0]"
               "\n\t"
               "movs  %[dly], #13"
               "\n\t"
               "L%=_last_delay_T1H:"
               "\n\t"
               "sub   %[dly], #1"
               "\n\t"
               "bne   L%=_last_delay_T1H"
               "\n\t"
               "strb  %[bitmask], [%[reg], #4]"
               "\n\t"
               "movs  %[dly], #1"
               "\n\t"
               "L%=_last_delay_T1L:"
               "\n\t"
               "sub   %[dly], #1"
               "\n\t"
               "bne   L%=_last_delay_T1L"
               "\n\t"
               "nop"
               "\n\t"
               "L%=_repeat:"
               "\n\t"
               "add   %[p], #1"
               "\n\t"
               "sub   %[num], #1"
               "\n\t"
               "bne   L%=_begin"
               "\n\t"
               "L%=_done:"
               "\n\t"
               : [p] "+r"(p), [pix] "=&r"(pix), [count] "=&r"(count),
                 [dly] "=&r"(dly), [num] "+r"(num)
               : [bitmask] "r"(bitmask), [reg] "r"(reg));
1946
1947
1948
1949
#else
#error "Sorry, only 48 MHz is supported, please set Tools > CPU Speed to 48 MHz"
#endif // F_CPU == 48000000

Eric Duminil's avatar
Eric Duminil committed
1950
  // Begin of support for nRF52 based boards  -------------------------
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978

#elif defined(NRF52) || defined(NRF52_SERIES)
// [[[Begin of the Neopixel NRF52 EasyDMA implementation
//                                    by the Hackerspace San Salvador]]]
// This technique uses the PWM peripheral on the NRF52. The PWM uses the
// EasyDMA feature included on the chip. This technique loads the duty
// cycle configuration for each cycle when the PWM is enabled. For this
// to work we need to store a 16 bit configuration for each bit of the
// RGB(W) values in the pixel buffer.
// Comparator values for the PWM were hand picked and are guaranteed to
// be 100% organic to preserve freshness and high accuracy. Current
// parameters are:
//   * PWM Clock: 16Mhz
//   * Minimum step time: 62.5ns
//   * Time for zero in high (T0H): 0.31ms
//   * Time for one in high (T1H): 0.75ms
//   * Cycle time:  1.25us
//   * Frequency: 800Khz
// For 400Khz we just double the calculated times.
// ---------- BEGIN Constants for the EasyDMA implementation -----------
// The PWM starts the duty cycle in LOW. To start with HIGH we
// need to set the 15th bit on each register.

// WS2812 (rev A) timing is 0.35 and 0.7us
//#define MAGIC_T0H               5UL | (0x8000) // 0.3125us
//#define MAGIC_T1H              12UL | (0x8000) // 0.75us

// WS2812B (rev B) timing is 0.4 and 0.8 us
Eric Duminil's avatar
Eric Duminil committed
1979
1980
#define MAGIC_T0H 6UL | (0x8000) // 0.375us
#define MAGIC_T1H 13UL | (0x8000) // 0.8125us
1981
1982

// WS2811 (400 khz) timing is 0.5 and 1.2
Eric Duminil's avatar
Eric Duminil committed
1983
1984
#define MAGIC_T0H_400KHz 8UL | (0x8000) // 0.5us
#define MAGIC_T1H_400KHz 19UL | (0x8000) // 1.1875us
1985
1986

// For 400Khz, we double value of CTOPVAL
Eric Duminil's avatar
Eric Duminil committed
1987
1988
#define CTOPVAL 20UL // 1.25us
#define CTOPVAL_400KHz 40UL // 2.5us
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999

// ---------- END Constants for the EasyDMA implementation -------------
//
// If there is no device available an alternative cycle-counter
// implementation is tried.
// The nRF52 runs with a fixed clock of 64Mhz. The alternative
// implementation is the same as the one used for the Teensy 3.0/1/2 but
// with the Nordic SDK HAL & registers syntax.
// The number of cycles was hand picked and is guaranteed to be 100%
// organic to preserve freshness and high accuracy.
// ---------- BEGIN Constants for cycle counter implementation ---------
Eric Duminil's avatar
Eric Duminil committed
2000
2001
2002
#define CYCLES_800_T0H 18 // ~0.36 uS
#define CYCLES_800_T1H 41 // ~0.76 uS
#define CYCLES_800 71 // ~1.25 uS
2003

Eric Duminil's avatar
Eric Duminil committed
2004
2005
2006
2007
#define CYCLES_400_T0H 26 // ~0.50 uS
#define CYCLES_400_T1H 70 // ~1.26 uS
#define CYCLES_400 156 // ~2.50 uS
  // ---------- END of Constants for cycle counter implementation --------
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018

  // To support both the SoftDevice + Neopixels we use the EasyDMA
  // feature from the NRF25. However this technique implies to
  // generate a pattern and store it on the memory. The actual
  // memory used in bytes corresponds to the following formula:
  //              totalMem = numBytes*8*2+(2*2)
  // The two additional bytes at the end are needed to reset the
  // sequence.
  //
  // If there is not enough memory, we will fall back to cycle counter
  // using DWT
Eric Duminil's avatar
Eric Duminil committed
2019
2020
2021
  uint32_t pattern_size =
      numBytes * 8 * sizeof(uint16_t) + 2 * sizeof(uint16_t);
  uint16_t *pixels_pattern = NULL;
2022

Eric Duminil's avatar
Eric Duminil committed
2023
  NRF_PWM_Type *pwm = NULL;
2024
2025
2026

  // Try to find a free PWM device, which is not enabled
  // and has no connected pins
Eric Duminil's avatar
Eric Duminil committed
2027
2028
2029
2030
  NRF_PWM_Type *PWM[] = {
    NRF_PWM0,
    NRF_PWM1,
    NRF_PWM2
2031
#if defined(NRF_PWM3)
Eric Duminil's avatar
Eric Duminil committed
2032
2033
    ,
    NRF_PWM3
2034
2035
2036
#endif
  };

Eric Duminil's avatar
Eric Duminil committed
2037
2038
2039
  for (unsigned int device = 0; device < (sizeof(PWM) / sizeof(PWM[0]));
       device++) {
    if ((PWM[device]->ENABLE == 0) &&
2040
2041
2042
        (PWM[device]->PSEL.OUT[0] & PWM_PSEL_OUT_CONNECT_Msk) &&
        (PWM[device]->PSEL.OUT[1] & PWM_PSEL_OUT_CONNECT_Msk) &&
        (PWM[device]->PSEL.OUT[2] & PWM_PSEL_OUT_CONNECT_Msk) &&
Eric Duminil's avatar
Eric Duminil committed
2043
        (PWM[device]->PSEL.OUT[3] & PWM_PSEL_OUT_CONNECT_Msk)) {
2044
2045
2046
2047
2048
2049
      pwm = PWM[device];
      break;
    }
  }

  // only malloc if there is PWM device available
Eric Duminil's avatar
Eric Duminil committed
2050
2051
2052
2053
2054
2055
  if (pwm != NULL) {
#if defined(ARDUINO_NRF52_ADAFRUIT) // use thread-safe malloc
    pixels_pattern = (uint16_t *)rtos_malloc(pattern_size);
#else
    pixels_pattern = (uint16_t *)malloc(pattern_size);
#endif
2056
2057
2058
2059
  }

  // Use the identified device to choose the implementation
  // If a PWM device is available use DMA
Eric Duminil's avatar
Eric Duminil committed
2060
  if ((pixels_pattern != NULL) && (pwm != NULL)) {
2061
2062
    uint16_t pos = 0; // bit position

Eric Duminil's avatar
Eric Duminil committed
2063
    for (uint16_t n = 0; n < numBytes; n++) {
2064
2065
      uint8_t pix = pixels[n];

Eric Duminil's avatar
Eric Duminil committed
2066
2067
2068
2069
2070
2071
2072
      for (uint8_t mask = 0x80; mask > 0; mask >>= 1) {
#if defined(NEO_KHZ400)
        if (!is800KHz) {
          pixels_pattern[pos] =
              (pix & mask) ? MAGIC_T1H_400KHz : MAGIC_T0H_400KHz;
        } else
#endif
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
        {
          pixels_pattern[pos] = (pix & mask) ? MAGIC_T1H : MAGIC_T0H;
        }

        pos++;
      }
    }

    // Zero padding to indicate the end of que sequence
    pixels_pattern[pos++] = 0 | (0x8000); // Seq end
    pixels_pattern[pos++] = 0 | (0x8000); // Seq end

    // Set the wave mode to count UP
    pwm->MODE = (PWM_MODE_UPDOWN_Up << PWM_MODE_UPDOWN_Pos);

    // Set the PWM to use the 16MHz clock
Eric Duminil's avatar
Eric Duminil committed
2089
2090
    pwm->PRESCALER =
        (PWM_PRESCALER_PRESCALER_DIV_1 << PWM_PRESCALER_PRESCALER_Pos);
2091
2092
2093
2094
2095

    // Setting of the maximum count
    // but keeping it on 16Mhz allows for more granularity just
    // in case someone wants to do more fine-tuning of the timing.
#if defined(NEO_KHZ400)
Eric Duminil's avatar
Eric Duminil committed
2096
    if (!is800KHz) {
2097
      pwm->COUNTERTOP = (CTOPVAL_400KHz << PWM_COUNTERTOP_COUNTERTOP_Pos);
Eric Duminil's avatar
Eric Duminil committed
2098
    } else
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
#endif
    {
      pwm->COUNTERTOP = (CTOPVAL << PWM_COUNTERTOP_COUNTERTOP_Pos);
    }

    // Disable loops, we want the sequence to repeat only once
    pwm->LOOP = (PWM_LOOP_CNT_Disabled << PWM_LOOP_CNT_Pos);

    // On the "Common" setting the PWM uses the same pattern for the
    // for supported sequences. The pattern is stored on half-word
    // of 16bits
    pwm->DECODER = (PWM_DECODER_LOAD_Common << PWM_DECODER_LOAD_Pos) |
                   (PWM_DECODER_MODE_RefreshCount << PWM_DECODER_MODE_Pos);

    // Pointer to the memory storing the patter
    pwm->SEQ[0].PTR = (uint32_t)(pixels_pattern) << PWM_SEQ_PTR_PTR_Pos;

    // Calculation of the number of steps loaded from memory.
Eric Duminil's avatar
Eric Duminil committed
2117
    pwm->SEQ[0].CNT = (pattern_size / sizeof(uint16_t)) << PWM_SEQ_CNT_CNT_Pos;
2118
2119

    // The following settings are ignored with the current config.
Eric Duminil's avatar
Eric Duminil committed
2120
    pwm->SEQ[0].REFRESH = 0;
2121
2122
2123
2124
2125
2126
2127
    pwm->SEQ[0].ENDDELAY = 0;

    // The Neopixel implementation is a blocking algorithm. DMA
    // allows for non-blocking operation. To "simulate" a blocking
    // operation we enable the interruption for the end of sequence
    // and block the execution thread until the event flag is set by
    // the peripheral.
Eric Duminil's avatar
Eric Duminil committed
2128
    //    pwm->INTEN |= (PWM_INTEN_SEQEND0_Enabled<<PWM_INTEN_SEQEND0_Pos);
2129

Eric Duminil's avatar
Eric Duminil committed
2130
2131
// PSEL must be configured before enabling PWM
#if defined(ARDUINO_ARCH_NRF52840)
2132
    pwm->PSEL.OUT[0] = g_APinDescription[pin].name;
Eric Duminil's avatar
Eric Duminil committed
2133
#else
2134
    pwm->PSEL.OUT[0] = g_ADigitalPinMap[pin];
Eric Duminil's avatar
Eric Duminil committed
2135
#endif
2136
2137
2138
2139
2140
2141

    // Enable the PWM
    pwm->ENABLE = 1;

    // After all of this and many hours of reading the documentation
    // we are ready to start the sequence...
Eric Duminil's avatar
Eric Duminil committed
2142
    pwm->EVENTS_SEQEND[0] = 0;
2143
2144
2145
    pwm->TASKS_SEQSTART[0] = 1;

    // But we have to wait for the flag to be set.
Eric Duminil's avatar
Eric Duminil committed
2146
2147
    while (!pwm->EVENTS_SEQEND[0]) {
#if defined(ARDUINO_NRF52_ADAFRUIT) || defined(ARDUINO_ARCH_NRF52840)
2148
      yield();
Eric Duminil's avatar
Eric Duminil committed
2149
#endif
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
    }

    // Before leave we clear the flag for the event.
    pwm->EVENTS_SEQEND[0] = 0;

    // We need to disable the device and disconnect
    // all the outputs before leave or the device will not
    // be selected on the next call.
    // TODO: Check if disabling the device causes performance issues.
    pwm->ENABLE = 0;

    pwm->PSEL.OUT[0] = 0xFFFFFFFFUL;

Eric Duminil's avatar
Eric Duminil committed
2163
2164
2165
2166
2167
2168
#if defined(ARDUINO_NRF52_ADAFRUIT) // use thread-safe free
    rtos_free(pixels_pattern);
#else
    free(pixels_pattern);
#endif
  } // End of DMA implementation
2169
  // ---------------------------------------------------------------------
Eric Duminil's avatar
Eric Duminil committed
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
  else {
#ifndef ARDUINO_ARCH_NRF52840
// Fall back to DWT
#if defined(ARDUINO_NRF52_ADAFRUIT)
    // Bluefruit Feather 52 uses freeRTOS
    // Critical Section is used since it does not block SoftDevice execution
    taskENTER_CRITICAL();
#elif defined(NRF52_DISABLE_INT)
    // If you are using the Bluetooth SoftDevice we advise you to not disable
    // the interrupts. Disabling the interrupts even for short periods of time
    // causes the SoftDevice to stop working.
    // Disable the interrupts only in cases where you need high performance for
    // the LEDs and if you are not using the EasyDMA feature.
    __disable_irq();
#endif

    NRF_GPIO_Type *nrf_port = (NRF_GPIO_Type *)digitalPinToPort(pin);
2187
2188
    uint32_t pinMask = digitalPinToBitMask(pin);

Eric Duminil's avatar
Eric Duminil committed
2189
    uint32_t CYCLES_X00 = CYCLES_800;
2190
2191
2192
2193
    uint32_t CYCLES_X00_T1H = CYCLES_800_T1H;
    uint32_t CYCLES_X00_T0H = CYCLES_800_T0H;

#if defined(NEO_KHZ400)
Eric Duminil's avatar
Eric Duminil committed
2194
2195
    if (!is800KHz) {
      CYCLES_X00 = CYCLES_400;
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
      CYCLES_X00_T1H = CYCLES_400_T1H;
      CYCLES_X00_T0H = CYCLES_400_T0H;
    }
#endif

    // Enable DWT in debug core
    CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
    DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;

    // Tries to re-send the frame if is interrupted by the SoftDevice.
Eric Duminil's avatar
Eric Duminil committed
2206
    while (1) {
2207
2208
2209
2210
2211
      uint8_t *p = pixels;

      uint32_t cycStart = DWT->CYCCNT;
      uint32_t cyc = 0;

Eric Duminil's avatar
Eric Duminil committed
2212
      for (uint16_t n = 0; n < numBytes; n++) {
2213
2214
        uint8_t pix = *p++;

Eric Duminil's avatar
Eric Duminil committed
2215
2216
2217
2218
        for (uint8_t mask = 0x80; mask; mask >>= 1) {
          while (DWT->CYCCNT - cyc < CYCLES_X00)
            ;
          cyc = DWT->CYCCNT;
2219
2220
2221

          nrf_port->OUTSET |= pinMask;

Eric Duminil's avatar
Eric Duminil committed
2222
2223
2224
          if (pix & mask) {
            while (DWT->CYCCNT - cyc < CYCLES_X00_T1H)
              ;
2225
          } else {
Eric Duminil's avatar
Eric Duminil committed
2226
2227
            while (DWT->CYCCNT - cyc < CYCLES_X00_T0H)
              ;
2228
2229
2230
2231
2232
          }

          nrf_port->OUTCLR |= pinMask;
        }
      }
Eric Duminil's avatar
Eric Duminil committed
2233
2234
      while (DWT->CYCCNT - cyc < CYCLES_X00)
        ;
2235
2236
2237

      // If total time longer than 25%, resend the whole data.
      // Since we are likely to be interrupted by SoftDevice
Eric Duminil's avatar
Eric Duminil committed
2238
      if ((DWT->CYCCNT - cycStart) < (8 * numBytes * ((CYCLES_X00 * 5) / 4))) {
2239
2240
2241
2242
2243
2244
2245
        break;
      }

      // re-send need 300us delay
      delayMicroseconds(300);
    }

Eric Duminil's avatar
Eric Duminil committed
2246
2247
2248
2249
2250
2251
// Enable interrupts again
#if defined(ARDUINO_NRF52_ADAFRUIT)
    taskEXIT_CRITICAL();
#elif defined(NRF52_DISABLE_INT)
    __enable_irq();
#endif
2252
2253
#endif
  }
Eric Duminil's avatar
Eric Duminil committed
2254
  // END of NRF52 implementation
2255

Eric Duminil's avatar
Eric Duminil committed
2256
2257
2258
2259
#elif defined(__SAMD21E17A__) || defined(__SAMD21G18A__) ||                    \
    defined(__SAMD21E18A__) ||                                                 \
    defined(__SAMD21J18A__) // Arduino Zero, Gemma/Trinket M0, SODAQ Autonomo
                            // and others
2260
2261
2262
  // Tried this with a timer/counter, couldn't quite get adequate
  // resolution. So yay, you get a load of goofball NOPs...

Eric Duminil's avatar
Eric Duminil committed
2263
2264
  uint8_t *ptr, *end, p, bitMask, portNum;
  uint32_t pinMask;
2265

Eric Duminil's avatar
Eric Duminil committed
2266
2267
2268
2269
2270
2271
  portNum = g_APinDescription[pin].ulPort;
  pinMask = 1ul << g_APinDescription[pin].ulPin;
  ptr = pixels;
  end = ptr + numBytes;
  p = *ptr++;
  bitMask = 0x80;
2272
2273
2274
2275
2276

  volatile uint32_t *set = &(PORT->Group[portNum].OUTSET.reg),
                    *clr = &(PORT->Group[portNum].OUTCLR.reg);

#if defined(NEO_KHZ400) // 800 KHz check needed only if 400 KHz support enabled
Eric Duminil's avatar
Eric Duminil committed
2277
  if (is800KHz) {
2278
#endif
Eric Duminil's avatar
Eric Duminil committed
2279
    for (;;) {
2280
2281
      *set = pinMask;
      asm("nop; nop; nop; nop; nop; nop; nop; nop;");
Eric Duminil's avatar
Eric Duminil committed
2282
      if (p & bitMask) {
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
        asm("nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop;");
        *clr = pinMask;
      } else {
        *clr = pinMask;
        asm("nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop;");
      }
Eric Duminil's avatar
Eric Duminil committed
2293
      if (bitMask >>= 1) {
2294
2295
        asm("nop; nop; nop; nop; nop; nop; nop; nop; nop;");
      } else {
Eric Duminil's avatar
Eric Duminil committed
2296
2297
2298
        if (ptr >= end)
          break;
        p = *ptr++;
2299
2300
2301
2302
2303
        bitMask = 0x80;
      }
    }
#if defined(NEO_KHZ400)
  } else { // 400 KHz bitstream
Eric Duminil's avatar
Eric Duminil committed
2304
    for (;;) {
2305
2306
      *set = pinMask;
      asm("nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop;");
Eric Duminil's avatar
Eric Duminil committed
2307
      if (p & bitMask) {
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
        asm("nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop;");
        *clr = pinMask;
      } else {
        *clr = pinMask;
        asm("nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop;");
      }
      asm("nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;");
Eric Duminil's avatar
Eric Duminil committed
2324
      if (bitMask >>= 1) {
2325
2326
        asm("nop; nop; nop; nop; nop; nop; nop;");
      } else {
Eric Duminil's avatar
Eric Duminil committed
2327
2328
2329
        if (ptr >= end)
          break;
        p = *ptr++;
2330
2331
2332
2333
2334
2335
        bitMask = 0x80;
      }
    }
  }
#endif

Eric Duminil's avatar
Eric Duminil committed
2336
2337
2338
2339
2340
2341
//----
#elif defined(XMC1100_XMC2GO) || defined(XMC1100_H_BRIDGE2GO) || defined(XMC1100_Boot_Kit)  || defined(XMC1300_Boot_Kit)

  // XMC1100/1200/1300 with ARM Cortex M0 are running with 32MHz, XMC1400 runs with 48MHz so may not work
  // Tried this with a timer/counter, couldn't quite get adequate
  // resolution.  So yay, you get a load of goofball NOPs...
2342

Eric Duminil's avatar
Eric Duminil committed
2343
  uint8_t  *ptr, *end, p, bitMask, portNum;
2344
2345
2346
2347
2348
2349
2350
  uint32_t  pinMask;

  ptr     =  pixels;
  end     =  ptr + numBytes;
  p       = *ptr++;
  bitMask =  0x80;

Eric Duminil's avatar
Eric Duminil committed
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
  XMC_GPIO_PORT_t*  XMC_port = mapping_port_pin[ pin ].port;
  uint8_t  XMC_pin           = mapping_port_pin[ pin ].pin;

	uint32_t omrhigh = (uint32_t)XMC_GPIO_OUTPUT_LEVEL_HIGH << XMC_pin;
	uint32_t omrlow  = (uint32_t)XMC_GPIO_OUTPUT_LEVEL_LOW << XMC_pin;

#ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
  if(is800KHz) {
#endif
    for(;;) {
			XMC_port->OMR = omrhigh;
      asm("nop; nop; nop; nop;");
      if(p & bitMask) {
        asm("nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop;");
			  XMC_port->OMR = omrlow;
      } else {
				XMC_port->OMR = omrlow;
        asm("nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop;");
      }
      if(bitMask >>= 1) {
        asm("nop; nop; nop; nop; nop;");
      } else {
        if(ptr >= end) break;
        p       = *ptr++;
        bitMask = 0x80;
      }
    }
#ifdef NEO_KHZ400 // untested code
  } else { // 400 KHz bitstream
    for(;;) {
      XMC_port->OMR = omrhigh;
      asm("nop; nop; nop; nop; nop;");
      if(p & bitMask) {
        asm("nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop;");
        XMC_port->OMR = omrlow;
      } else {
        XMC_port->OMR = omrlow;
        asm("nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop;");
      }
      asm("nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;");
      if(bitMask >>= 1) {
        asm("nop; nop; nop;");
      } else {
        if(ptr >= end) break;
        p       = *ptr++;
        bitMask = 0x80;
      }
    }
  }

#endif
//----

//----
#elif defined(XMC4700_Relax_Kit) || defined(XMC4800_Relax_Kit)

// XMC4700 and XMC4800 with ARM Cortex M4 are running with 144MHz
// Tried this with a timer/counter, couldn't quite get adequate
// resolution.  So yay, you get a load of goofball NOPs...

uint8_t  *ptr, *end, p, bitMask, portNum;
uint32_t  pinMask;

ptr     =  pixels;
end     =  ptr + numBytes;
p       = *ptr++;
bitMask =  0x80;

XMC_GPIO_PORT_t*   XMC_port = mapping_port_pin[ pin ].port;
uint8_t            XMC_pin  = mapping_port_pin[ pin ].pin;

uint32_t omrhigh = (uint32_t)XMC_GPIO_OUTPUT_LEVEL_HIGH << XMC_pin;
uint32_t omrlow  = (uint32_t)XMC_GPIO_OUTPUT_LEVEL_LOW << XMC_pin;

#ifdef NEO_KHZ400 // 800 KHz check needed only if 400 KHz support enabled
if(is800KHz) {
#endif

  for(;;) {
    XMC_port->OMR = omrhigh;
    asm("nop; nop; nop; nop; nop; nop; nop; nop;"
        "nop; nop; nop; nop; nop; nop; nop; nop;"
        "nop; nop; nop; nop; nop; nop; nop; nop;"
        "nop; nop; nop; nop; nop; nop; nop; nop;"
        "nop; nop; nop; nop; nop; nop; nop; nop;"
        "nop; nop; nop; nop; nop; nop; nop; nop;"
        "nop; nop; nop; nop;");
    if(p & bitMask) {
      asm("nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;");
      XMC_port->OMR = omrlow;
    } else {
      XMC_port->OMR = omrlow;
      asm("nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;");
    }
    if(bitMask >>= 1) {
      asm("nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;"
          "nop; nop; nop; nop; nop; nop; nop; nop;");
    } else {
      if(ptr >= end) break;
      p       = *ptr++;
      bitMask = 0x80;
    }
  }


#ifdef NEO_KHZ400
  } else { // 400 KHz bitstream
    // ToDo!
  }
#endif
//----

#elif defined(__SAMD51__) // M4

  uint8_t *ptr, *end, p, bitMask, portNum, bit;
  uint32_t pinMask;

  portNum = g_APinDescription[pin].ulPort;
  pinMask = 1ul << g_APinDescription[pin].ulPin;
  ptr = pixels;
  end = ptr + numBytes;
  p = *ptr++;
  bitMask = 0x80;

2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
  volatile uint32_t *set = &(PORT->Group[portNum].OUTSET.reg),
                    *clr = &(PORT->Group[portNum].OUTCLR.reg);

  // SAMD51 overclock-compatible timing is only a mild abomination.
  // It uses SysTick for a consistent clock reference regardless of
  // optimization / cache settings.  That's the good news.  The bad news,
  // since SysTick->VAL is a volatile type it's slow to access...and then,
  // with the SysTick interval that Arduino sets up (1 ms), this would
  // require a subtract and MOD operation for gauging elapsed time, and
  // all taken in combination that lacks adequate temporal resolution
  // for NeoPixel timing.  So a kind of horrible thing is done here...
  // since interrupts are turned off anyway and it's generally accepted
  // by now that we're gonna lose track of time in the NeoPixel lib,
  // the SysTick timer is reconfigured for a period matching the NeoPixel
  // bit timing (either 800 or 400 KHz) and we watch SysTick->VAL very
  // closely (just a threshold, no subtract or MOD or anything) and that
  // seems to work just well enough.  When finished, the SysTick
  // peripheral is set back to its original state.

Eric Duminil's avatar
Eric Duminil committed
2532
  uint32_t t0, t1, top, ticks, saveLoad = SysTick->LOAD, saveVal = SysTick->VAL;
2533
2534

#if defined(NEO_KHZ400) // 800 KHz check needed only if 400 KHz support enabled
Eric Duminil's avatar
Eric Duminil committed
2535
  if (is800KHz) {
2536
#endif
Eric Duminil's avatar
Eric Duminil committed
2537
2538
2539
    top = (uint32_t)(F_CPU * 0.00000125);      // Bit hi + lo = 1.25 uS
    t0 = top - (uint32_t)(F_CPU * 0.00000040); // 0 = 0.4 uS hi
    t1 = top - (uint32_t)(F_CPU * 0.00000080); // 1 = 0.8 uS hi
2540
#if defined(NEO_KHZ400)
Eric Duminil's avatar
Eric Duminil committed
2541
2542
2543
2544
  } else {                                     // 400 KHz bitstream
    top = (uint32_t)(F_CPU * 0.00000250);      // Bit hi + lo = 2.5 uS
    t0 = top - (uint32_t)(F_CPU * 0.00000050); // 0 = 0.5 uS hi
    t1 = top - (uint32_t)(F_CPU * 0.00000120); // 1 = 1.2 uS hi
2545
2546
2547
  }
#endif

Eric Duminil's avatar
Eric Duminil committed
2548
2549
2550
  SysTick->LOAD = top; // Config SysTick for NeoPixel bit freq
  SysTick->VAL = top;  // Set to start value (counts down)
  (void)SysTick->VAL;  // Dummy read helps sync up 1st bit
2551

Eric Duminil's avatar
Eric Duminil committed
2552
2553
  for (;;) {
    *set = pinMask;                  // Set output high
2554
    ticks = (p & bitMask) ? t1 : t0; // SysTick threshold,
Eric Duminil's avatar
Eric Duminil committed
2555
2556
2557
2558
2559
2560
2561
2562
    while (SysTick->VAL > ticks)
      ;                     // wait for it
    *clr = pinMask;         // Set output low
    if (!(bitMask >>= 1)) { // Next bit for this byte...done?
      if (ptr >= end)
        break;        // If last byte sent, exit loop
      p = *ptr++;     // Fetch next byte
      bitMask = 0x80; // Reset bitmask
2563
    }
Eric Duminil's avatar
Eric Duminil committed
2564
2565
    while (SysTick->VAL <= ticks)
      ; // Wait for rollover to 'top'
2566
2567
  }

Eric Duminil's avatar
Eric Duminil committed
2568
2569
  SysTick->LOAD = saveLoad; // Restore SysTick rollover to 1 ms
  SysTick->VAL = saveVal;   // Restore SysTick value
2570

Eric Duminil's avatar
Eric Duminil committed
2571
#elif defined(ARDUINO_STM32_FEATHER) // FEATHER WICED (120MHz)
2572
2573
2574
2575

  // Tried this with a timer/counter, couldn't quite get adequate
  // resolution. So yay, you get a load of goofball NOPs...

Eric Duminil's avatar
Eric Duminil committed
2576
2577
  uint8_t *ptr, *end, p, bitMask;
  uint32_t pinMask;
2578

Eric Duminil's avatar
Eric Duminil committed
2579
2580
2581
2582
2583
  pinMask = BIT(PIN_MAP[pin].gpio_bit);
  ptr = pixels;
  end = ptr + numBytes;
  p = *ptr++;
  bitMask = 0x80;
2584
2585
2586
2587
2588

  volatile uint16_t *set = &(PIN_MAP[pin].gpio_device->regs->BSRRL);
  volatile uint16_t *clr = &(PIN_MAP[pin].gpio_device->regs->BSRRH);

#if defined(NEO_KHZ400) // 800 KHz check needed only if 400 KHz support enabled
Eric Duminil's avatar
Eric Duminil committed
2589
  if (is800KHz) {
2590
#endif
Eric Duminil's avatar
Eric Duminil committed
2591
2592
    for (;;) {
      if (p & bitMask) { // ONE
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
        // High 800ns
        *set = pinMask;
        asm("nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop;");
        // Low 450ns
        *clr = pinMask;
        asm("nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop;");
      } else { // ZERO
        // High 400ns
        *set = pinMask;
        asm("nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop;");
        // Low 850ns
        *clr = pinMask;
        asm("nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop; nop; nop; nop; nop;"
            "nop; nop; nop; nop;");
      }
Eric Duminil's avatar
Eric Duminil committed
2638
      if (bitMask >>= 1) {
2639
2640
2641
        // Move on to the next pixel
        asm("nop;");
      } else {
Eric Duminil's avatar
Eric Duminil committed
2642
2643
2644
        if (ptr >= end)
          break;
        p = *ptr++;
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
        bitMask = 0x80;
      }
    }
#if defined(NEO_KHZ400)
  } else { // 400 KHz bitstream
    // ToDo!
  }
#endif

#elif defined(TARGET_LPC1768)
Eric Duminil's avatar
Eric Duminil committed
2655
2656
2657
2658
2659
  uint8_t *ptr, *end, p, bitMask;
  ptr = pixels;
  end = ptr + numBytes;
  p = *ptr++;
  bitMask = 0x80;
2660
2661

#if defined(NEO_KHZ400) // 800 KHz check needed only if 400 KHz support enabled
Eric Duminil's avatar
Eric Duminil committed
2662
  if (is800KHz) {
2663
#endif
Eric Duminil's avatar
Eric Duminil committed
2664
2665
    for (;;) {
      if (p & bitMask) {
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
        // data ONE high
        // min: 550 typ: 700 max: 5,500
        gpio_set(pin);
        time::delay_ns(550);
        // min: 450 typ: 600 max: 5,000
        gpio_clear(pin);
        time::delay_ns(450);
      } else {
        // data ZERO high
        // min: 200  typ: 350 max: 500
        gpio_set(pin);
        time::delay_ns(200);
        // data low
        // min: 450 typ: 600 max: 5,000
        gpio_clear(pin);
        time::delay_ns(450);
      }
Eric Duminil's avatar
Eric Duminil committed
2683
      if (bitMask >>= 1) {
2684
2685
2686
        // Move on to the next pixel
        asm("nop;");
      } else {
Eric Duminil's avatar
Eric Duminil committed
2687
2688
2689
        if (ptr >= end)
          break;
        p = *ptr++;
2690
2691
2692
2693
2694
2695
2696
2697
2698
        bitMask = 0x80;
      }
    }
#if defined(NEO_KHZ400)
  } else { // 400 KHz bitstream
    // ToDo!
  }
#endif
#elif defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_ARDUINO_CORE_STM32)
Eric Duminil's avatar
Eric Duminil committed
2699
2700
  uint8_t *p = pixels, *end = p + numBytes, pix = *p++, mask = 0x80;
  uint32_t cyc;
2701
2702
  uint32_t saveLoad = SysTick->LOAD, saveVal = SysTick->VAL;
#if defined(NEO_KHZ400) // 800 KHz check needed only if 400 KHz support enabled
Eric Duminil's avatar
Eric Duminil committed
2703
  if (is800KHz) {
2704
#endif
Eric Duminil's avatar
Eric Duminil committed
2705
2706
2707
    uint32_t top = (F_CPU / 800000);       // 1.25µs
    uint32_t t0 = top - (F_CPU / 2500000); // 0.4µs
    uint32_t t1 = top - (F_CPU / 1250000); // 0.8µs
2708
    SysTick->LOAD = top - 1; // Config SysTick for NeoPixel bit freq
Eric Duminil's avatar
Eric Duminil committed
2709
2710
    SysTick->VAL = 0;        // Set to start value
    for (;;) {
2711
2712
      LL_GPIO_SetOutputPin(gpioPort, gpioPin);
      cyc = (pix & mask) ? t1 : t0;
Eric Duminil's avatar
Eric Duminil committed
2713
2714
      while (SysTick->VAL > cyc)
        ;
2715
      LL_GPIO_ResetOutputPin(gpioPort, gpioPin);
Eric Duminil's avatar
Eric Duminil committed
2716
2717
2718
2719
      if (!(mask >>= 1)) {
        if (p >= end)
          break;
        pix = *p++;
2720
2721
        mask = 0x80;
      }
Eric Duminil's avatar
Eric Duminil committed
2722
2723
      while (SysTick->VAL <= cyc)
        ;
2724
2725
    }
#if defined(NEO_KHZ400)
Eric Duminil's avatar
Eric Duminil committed
2726
2727
2728
2729
  } else {                                 // 400 kHz bitstream
    uint32_t top = (F_CPU / 400000);       // 2.5µs
    uint32_t t0 = top - (F_CPU / 2000000); // 0.5µs
    uint32_t t1 = top - (F_CPU / 833333);  // 1.2µs
2730
    SysTick->LOAD = top - 1; // Config SysTick for NeoPixel bit freq
Eric Duminil's avatar
Eric Duminil committed
2731
2732
    SysTick->VAL = 0;        // Set to start value
    for (;;) {
2733
2734
      LL_GPIO_SetOutputPin(gpioPort, gpioPin);
      cyc = (pix & mask) ? t1 : t0;
Eric Duminil's avatar
Eric Duminil committed
2735
2736
      while (SysTick->VAL > cyc)
        ;
2737
      LL_GPIO_ResetOutputPin(gpioPort, gpioPin);
Eric Duminil's avatar
Eric Duminil committed
2738
2739
2740
2741
      if (!(mask >>= 1)) {
        if (p >= end)
          break;
        pix = *p++;
2742
2743
        mask = 0x80;
      }
Eric Duminil's avatar
Eric Duminil committed
2744
2745
      while (SysTick->VAL <= cyc)
        ;
2746
2747
2748
    }
  }
#endif // NEO_KHZ400
Eric Duminil's avatar
Eric Duminil committed
2749
2750
2751
2752
2753
2754
2755
  SysTick->LOAD = saveLoad; // Restore SysTick rollover to 1 ms
  SysTick->VAL = saveVal;   // Restore SysTick value
#elif defined(NRF51)
  uint8_t *p = pixels, pix, count, mask;
  int32_t num = numBytes;
  unsigned int bitmask = (1 << g_ADigitalPinMap[pin]);
  // https://github.com/sandeepmistry/arduino-nRF5/blob/dc53980c8bac27898fca90d8ecb268e11111edc1/variants/BBCmicrobit/variant.cpp
2756

Eric Duminil's avatar
Eric Duminil committed
2757
  volatile unsigned int *reg = (unsigned int *)(0x50000000UL + 0x508);
2758

Eric Duminil's avatar
Eric Duminil committed
2759
2760
2761
  // https://github.com/sandeepmistry/arduino-nRF5/blob/dc53980c8bac27898fca90d8ecb268e11111edc1/cores/nRF5/SDK/components/device/nrf51.h
  // 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
  // https://github.com/Microsoft/pxt-neopixel/blob/master/sendbuffer.asm
2762
2763

  asm volatile(
Eric Duminil's avatar
Eric Duminil committed
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
      // "cpsid i" ; disable irq

      //    b .start
      "b  L%=_start"
      "\n\t"
      // .nextbit:               ;            C0
      "L%=_nextbit:"
      "\n\t" //;            C0
      //    str r1, [r3, #0]    ; pin := hi  C2
      "strb %[bitmask], [%[reg], #0]"
      "\n\t" //; pin := hi  C2
      //    tst r6, r0          ;            C3
      "tst %[mask], %[pix]"
      "\n\t" //          ;            C3
      //    bne .islate         ;            C4
      "bne L%=_islate"
      "\n\t" //;            C4
      //    str r1, [r2, #0]    ; pin := lo  C6
      "strb %[bitmask], [%[reg], #4]"
      "\n\t" //; pin := lo  C6
      // .islate:
      "L%=_islate:"
      "\n\t"
      //    lsrs r6, r6, #1     ; r6 >>= 1   C7
      "lsr %[mask], %[mask], #1"
      "\n\t" //; r6 >>= 1   C7
      //    bne .justbit        ;            C8
      "bne L%=_justbit"
      "\n\t" //;            C8

      //    ; not just a bit - need new byte
      //    adds r4, #1         ; r4++       C9
      "add %[p], #1"
      "\n\t" //; r4++       C9
      //    subs r5, #1         ; r5--       C10
      "sub %[num], #1"
      "\n\t" //; r5--       C10
      //    bcc .stop           ; if (r5<0) goto .stop  C11
      "bcc L%=_stop"
      "\n\t" //; if (r5<0) goto .stop  C11
      // .start:
      "L%=_start:"
      //    movs r6, #0x80      ; reset mask C12
      "movs %[mask], #0x80"
      "\n\t" //; reset mask C12
      //    nop                 ;            C13
      "nop"
      "\n\t" //;            C13

      // .common:               ;             C13
      "L%=_common:"
      "\n\t" //;            C13
      //    str r1, [r2, #0]   ; pin := lo   C15
      "strb %[bitmask], [%[reg], #4]"
      "\n\t" //; pin := lo  C15
      //    ; always re-load byte - it just fits with the cycles better this way
      //    ldrb r0, [r4, #0]  ; r0 := *r4   C17
      "ldrb  %[pix], [%[p], #0]"
      "\n\t" //; r0 := *r4   C17
      //    b .nextbit         ;             C20
      "b L%=_nextbit"
      "\n\t" //;             C20

      // .justbit: ; C10
      "L%=_justbit:"
      "\n\t" //; C10
      //    ; no nops, branch taken is already 3 cycles
      //    b .common ; C13
      "b L%=_common"
      "\n\t" //; C13

      // .stop:
      "L%=_stop:"
      "\n\t"
      //    str r1, [r2, #0]   ; pin := lo
      "strb %[bitmask], [%[reg], #4]"
      "\n\t" //; pin := lo
      //    cpsie i            ; enable irq

      : [p] "+r"(p), [pix] "=&r"(pix), [count] "=&r"(count), [mask] "=&r"(mask),
        [num] "+r"(num)
      : [bitmask] "r"(bitmask), [reg] "r"(reg));
2846
2847
2848

#elif defined(__SAM3X8E__) // Arduino Due

Eric Duminil's avatar
Eric Duminil committed
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
#define SCALE VARIANT_MCK / 2UL / 1000000UL
#define INST (2UL * F_CPU / VARIANT_MCK)
#define TIME_800_0 ((int)(0.40 * SCALE + 0.5) - (5 * INST))
#define TIME_800_1 ((int)(0.80 * SCALE + 0.5) - (5 * INST))
#define PERIOD_800 ((int)(1.25 * SCALE + 0.5) - (5 * INST))
#define TIME_400_0 ((int)(0.50 * SCALE + 0.5) - (5 * INST))
#define TIME_400_1 ((int)(1.20 * SCALE + 0.5) - (5 * INST))
#define PERIOD_400 ((int)(2.50 * SCALE + 0.5) - (5 * INST))

  int pinMask, time0, time1, period, t;
  Pio *port;
2860
  volatile WoReg *portSet, *portClear, *timeValue, *timeReset;
Eric Duminil's avatar
Eric Duminil committed
2861
  uint8_t *p, *end, pix, mask;
2862
2863
2864
2865

  pmc_set_writeprotect(false);
  pmc_enable_periph_clk((uint32_t)TC3_IRQn);
  TC_Configure(TC1, 0,
Eric Duminil's avatar
Eric Duminil committed
2866
               TC_CMR_WAVE | TC_CMR_WAVSEL_UP | TC_CMR_TCCLKS_TIMER_CLOCK1);
2867
2868
  TC_Start(TC1, 0);

Eric Duminil's avatar
Eric Duminil committed
2869
2870
2871
2872
2873
  pinMask = g_APinDescription[pin].ulPin;  // Don't 'optimize' these into
  port = g_APinDescription[pin].pPort;     // declarations above. Want to
  portSet = &(port->PIO_SODR);             // burn a few cycles after
  portClear = &(port->PIO_CODR);           // starting timer to minimize
  timeValue = &(TC1->TC_CHANNEL[0].TC_CV); // the initial 'while'.
2874
  timeReset = &(TC1->TC_CHANNEL[0].TC_CCR);
Eric Duminil's avatar
Eric Duminil committed
2875
2876
2877
2878
  p = pixels;
  end = p + numBytes;
  pix = *p++;
  mask = 0x80;
2879
2880

#if defined(NEO_KHZ400) // 800 KHz check needed only if 400 KHz support enabled
Eric Duminil's avatar
Eric Duminil committed
2881
  if (is800KHz) {
2882
#endif
Eric Duminil's avatar
Eric Duminil committed
2883
2884
    time0 = TIME_800_0;
    time1 = TIME_800_1;
2885
2886
2887
    period = PERIOD_800;
#if defined(NEO_KHZ400)
  } else { // 400 KHz bitstream
Eric Duminil's avatar
Eric Duminil committed
2888
2889
    time0 = TIME_400_0;
    time1 = TIME_400_1;
2890
2891
2892
2893
    period = PERIOD_400;
  }
#endif

Eric Duminil's avatar
Eric Duminil committed
2894
2895
2896
2897
2898
2899
  for (t = time0;; t = time0) {
    if (pix & mask)
      t = time1;
    while (*timeValue < (unsigned)period)
      ;
    *portSet = pinMask;
2900
    *timeReset = TC_CCR_CLKEN | TC_CCR_SWTRG;
Eric Duminil's avatar
Eric Duminil committed
2901
2902
    while (*timeValue < (unsigned)t)
      ;
2903
    *portClear = pinMask;
Eric Duminil's avatar
Eric Duminil committed
2904
2905
2906
    if (!(mask >>= 1)) { // This 'inside-out' loop logic utilizes
      if (p >= end)
        break; // idle time to minimize inter-byte delays.
2907
2908
2909
2910
      pix = *p++;
      mask = 0x80;
    }
  }
Eric Duminil's avatar
Eric Duminil committed
2911
2912
  while (*timeValue < (unsigned)period)
    ; // Wait for last bit
2913
2914
2915
2916
  TC_Stop(TC1, 0);

#endif // end Due

Eric Duminil's avatar
Eric Duminil committed
2917
  // END ARM ----------------------------------------------------------------
2918
2919
2920

#elif defined(ESP8266) || defined(ESP32)

Eric Duminil's avatar
Eric Duminil committed
2921
  // ESP8266 ----------------------------------------------------------------
2922
2923
2924
2925
2926
2927
2928
2929

  // ESP8266 show() is external to enforce ICACHE_RAM_ATTR execution
  espShow(pin, pixels, numBytes, is800KHz);

#elif defined(KENDRYTE_K210)

  k210Show(pin, pixels, numBytes, is800KHz);

Eric Duminil's avatar
Eric Duminil committed
2930
#elif defined(__ARDUINO_ARC__)
2931

Eric Duminil's avatar
Eric Duminil committed
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
    // Arduino 101  -----------------------------------------------------------

#define NOPx7                                                                  \
  {                                                                            \
    __builtin_arc_nop();                                                       \
    __builtin_arc_nop();                                                       \
    __builtin_arc_nop();                                                       \
    __builtin_arc_nop();                                                       \
    __builtin_arc_nop();                                                       \
    __builtin_arc_nop();                                                       \
    __builtin_arc_nop();                                                       \
  }
2944
2945

  PinDescription *pindesc = &g_APinDescription[pin];
Eric Duminil's avatar
Eric Duminil committed
2946
2947
  register uint32_t loop =
      8 * numBytes; // one loop to handle all bytes and all bits
2948
  register uint8_t *p = pixels;
Eric Duminil's avatar
Eric Duminil committed
2949
  register uint32_t currByte = (uint32_t)(*p);
2950
2951
2952
2953
  register uint32_t currBit = 0x80 & currByte;
  register uint32_t bitCounter = 0;
  register uint32_t first = 1;

Eric Duminil's avatar
Eric Duminil committed
2954
2955
2956
2957
2958
2959
  // The loop is unusual. Very first iteration puts all the way LOW to the wire
  // - constant LOW does not affect NEOPIXEL, so there is no visible effect
  // displayed. During that very first iteration CPU caches instructions in the
  // loop. Because of the caching process, "CPU slows down". NEOPIXEL pulse is
  // very time sensitive that's why we let the CPU cache first and we start
  // regular pulse from 2nd iteration
2960
2961
2962
2963
  if (pindesc->ulGPIOType == SS_GPIO) {
    register uint32_t reg = pindesc->ulGPIOBase + SS_GPIO_SWPORTA_DR;
    uint32_t reg_val = __builtin_arc_lr((volatile uint32_t)reg);
    register uint32_t reg_bit_high = reg_val | (1 << pindesc->ulGPIOId);
Eric Duminil's avatar
Eric Duminil committed
2964
    register uint32_t reg_bit_low = reg_val & ~(1 << pindesc->ulGPIOId);
2965
2966

    loop += 1; // include first, special iteration
Eric Duminil's avatar
Eric Duminil committed
2967
2968
    while (loop--) {
      if (!first) {
2969
2970
2971
2972
2973
        currByte <<= 1;
        bitCounter++;
      }

      // 1 is >550ns high and >450ns low; 0 is 200..500ns high and >450ns low
Eric Duminil's avatar
Eric Duminil committed
2974
2975
2976
2977
      __builtin_arc_sr(first ? reg_bit_low : reg_bit_high,
                       (volatile uint32_t)reg);
      if (currBit) { // ~400ns HIGH (740ns overall)
        NOPx7 NOPx7
2978
2979
      }
      // ~340ns HIGH
Eric Duminil's avatar
Eric Duminil committed
2980
      NOPx7 __builtin_arc_nop();
2981
2982
2983

      // 820ns LOW; per spec, max allowed low here is 5000ns */
      __builtin_arc_sr(reg_bit_low, (volatile uint32_t)reg);
Eric Duminil's avatar
Eric Duminil committed
2984
      NOPx7 NOPx7
2985

Eric Duminil's avatar
Eric Duminil committed
2986
          if (bitCounter >= 8) {
2987
        bitCounter = 0;
Eric Duminil's avatar
Eric Duminil committed
2988
        currByte = (uint32_t)(*++p);
2989
2990
2991
2992
2993
      }

      currBit = 0x80 & currByte;
      first = 0;
    }
Eric Duminil's avatar
Eric Duminil committed
2994
  } else if (pindesc->ulGPIOType == SOC_GPIO) {
2995
2996
2997
    register uint32_t reg = pindesc->ulGPIOBase + SOC_GPIO_SWPORTA_DR;
    uint32_t reg_val = MMIO_REG_VAL(reg);
    register uint32_t reg_bit_high = reg_val | (1 << pindesc->ulGPIOId);
Eric Duminil's avatar
Eric Duminil committed
2998
    register uint32_t reg_bit_low = reg_val & ~(1 << pindesc->ulGPIOId);
2999
3000

    loop += 1; // include first, special iteration
Eric Duminil's avatar
Eric Duminil committed
3001
3002
    while (loop--) {
      if (!first) {
3003
3004
3005
3006
        currByte <<= 1;
        bitCounter++;
      }
      MMIO_REG_VAL(reg) = first ? reg_bit_low : reg_bit_high;
Eric Duminil's avatar
Eric Duminil committed
3007
3008
      if (currBit) { // ~430ns HIGH (740ns overall)
        NOPx7 NOPx7 __builtin_arc_nop();
3009
3010
3011
3012
      }
      // ~310ns HIGH
      NOPx7

Eric Duminil's avatar
Eric Duminil committed
3013
3014
3015
          // 850ns LOW; per spec, max allowed low here is 5000ns */
          MMIO_REG_VAL(reg) = reg_bit_low;
      NOPx7 NOPx7
3016

Eric Duminil's avatar
Eric Duminil committed
3017
          if (bitCounter >= 8) {
3018
        bitCounter = 0;
Eric Duminil's avatar
Eric Duminil committed
3019
        currByte = (uint32_t)(*++p);
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
      }

      currBit = 0x80 & currByte;
      first = 0;
    }
  }

#else
#error Architecture not supported
#endif

Eric Duminil's avatar
Eric Duminil committed
3031
  // END ARCHITECTURE SELECT ------------------------------------------------
3032

Eric Duminil's avatar
Eric Duminil committed
3033
#if !(defined(NRF52) || defined(NRF52_SERIES))
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
  interrupts();
#endif

  endTime = micros(); // Save EOD time for latch on next call
}

/*!
  @brief   Set/change the NeoPixel output pin number. Previous pin,
           if any, is set to INPUT and the new pin is set to OUTPUT.
  @param   p  Arduino pin number (-1 = no pin).
*/
Eric Duminil's avatar
Eric Duminil committed
3045
3046
3047
void Adafruit_NeoPixel::setPin(int16_t p) {
  if (begun && (pin >= 0))
    pinMode(pin, INPUT); // Disable existing out pin
3048
  pin = p;
Eric Duminil's avatar
Eric Duminil committed
3049
  if (begun) {
3050
3051
3052
3053
    pinMode(p, OUTPUT);
    digitalWrite(p, LOW);
  }
#if defined(__AVR__)
Eric Duminil's avatar
Eric Duminil committed
3054
  port = portOutputRegister(digitalPinToPort(p));
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
  pinMask = digitalPinToBitMask(p);
#endif
#if defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_ARDUINO_CORE_STM32)
  gpioPort = digitalPinToPort(p);
  gpioPin = STM_LL_GPIO_PIN(digitalPinToPinName(p));
#endif
}

/*!
  @brief   Set a pixel's color using separate red, green and blue
           components. If using RGBW pixels, white will be set to 0.
  @param   n  Pixel index, starting from 0.
  @param   r  Red brightness, 0 = minimum (off), 255 = maximum.
  @param   g  Green brightness, 0 = minimum (off), 255 = maximum.
  @param   b  Blue brightness, 0 = minimum (off), 255 = maximum.
*/
Eric Duminil's avatar
Eric Duminil committed
3071
3072
void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint8_t r, uint8_t g,
                                      uint8_t b) {
3073

Eric Duminil's avatar
Eric Duminil committed
3074
3075
  if (n < numLEDs) {
    if (brightness) { // See notes in setBrightness()
3076
3077
3078
3079
3080
      r = (r * brightness) >> 8;
      g = (g * brightness) >> 8;
      b = (b * brightness) >> 8;
    }
    uint8_t *p;
Eric Duminil's avatar
Eric Duminil committed
3081
3082
3083
3084
3085
    if (wOffset == rOffset) { // Is an RGB-type strip
      p = &pixels[n * 3];     // 3 bytes per pixel
    } else {                  // Is a WRGB-type strip
      p = &pixels[n * 4];     // 4 bytes per pixel
      p[wOffset] = 0;         // But only R,G,B passed -- set W to 0
3086
    }
Eric Duminil's avatar
Eric Duminil committed
3087
    p[rOffset] = r; // R,G,B always stored
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
    p[gOffset] = g;
    p[bOffset] = b;
  }
}

/*!
  @brief   Set a pixel's color using separate red, green, blue and white
           components (for RGBW NeoPixels only).
  @param   n  Pixel index, starting from 0.
  @param   r  Red brightness, 0 = minimum (off), 255 = maximum.
  @param   g  Green brightness, 0 = minimum (off), 255 = maximum.
  @param   b  Blue brightness, 0 = minimum (off), 255 = maximum.
  @param   w  White brightness, 0 = minimum (off), 255 = maximum, ignored
              if using RGB pixels.
*/
Eric Duminil's avatar
Eric Duminil committed
3103
3104
void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint8_t r, uint8_t g,
                                      uint8_t b, uint8_t w) {
3105

Eric Duminil's avatar
Eric Duminil committed
3106
3107
  if (n < numLEDs) {
    if (brightness) { // See notes in setBrightness()
3108
3109
3110
3111
3112
3113
      r = (r * brightness) >> 8;
      g = (g * brightness) >> 8;
      b = (b * brightness) >> 8;
      w = (w * brightness) >> 8;
    }
    uint8_t *p;
Eric Duminil's avatar
Eric Duminil committed
3114
3115
3116
3117
3118
    if (wOffset == rOffset) { // Is an RGB-type strip
      p = &pixels[n * 3];     // 3 bytes per pixel (ignore W)
    } else {                  // Is a WRGB-type strip
      p = &pixels[n * 4];     // 4 bytes per pixel
      p[wOffset] = w;         // Store W
3119
    }
Eric Duminil's avatar
Eric Duminil committed
3120
    p[rOffset] = r; // Store R,G,B
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
    p[gOffset] = g;
    p[bOffset] = b;
  }
}

/*!
  @brief   Set a pixel's color using a 32-bit 'packed' RGB or RGBW value.
  @param   n  Pixel index, starting from 0.
  @param   c  32-bit color value. Most significant byte is white (for RGBW
              pixels) or ignored (for RGB pixels), next is red, then green,
              and least significant byte is blue.
*/
void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c) {
Eric Duminil's avatar
Eric Duminil committed
3134
3135
3136
  if (n < numLEDs) {
    uint8_t *p, r = (uint8_t)(c >> 16), g = (uint8_t)(c >> 8), b = (uint8_t)c;
    if (brightness) { // See notes in setBrightness()
3137
3138
3139
3140
      r = (r * brightness) >> 8;
      g = (g * brightness) >> 8;
      b = (b * brightness) >> 8;
    }
Eric Duminil's avatar
Eric Duminil committed
3141
    if (wOffset == rOffset) {
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
      p = &pixels[n * 3];
    } else {
      p = &pixels[n * 4];
      uint8_t w = (uint8_t)(c >> 24);
      p[wOffset] = brightness ? ((w * brightness) >> 8) : w;
    }
    p[rOffset] = r;
    p[gOffset] = g;
    p[bOffset] = b;
  }
}

/*!
  @brief   Fill all or part of the NeoPixel strip with a color.
  @param   c      32-bit color value. Most significant byte is white (for
                  RGBW pixels) or ignored (for RGB pixels), next is red,
                  then green, and least significant byte is blue. If all
                  arguments are unspecified, this will be 0 (off).
  @param   first  Index of first pixel to fill, starting from 0. Must be
                  in-bounds, no clipping is performed. 0 if unspecified.
  @param   count  Number of pixels to fill, as a positive value. Passing
                  0 or leaving unspecified will fill to end of strip.
*/
void Adafruit_NeoPixel::fill(uint32_t c, uint16_t first, uint16_t count) {
  uint16_t i, end;

Eric Duminil's avatar
Eric Duminil committed
3168
  if (first >= numLEDs) {
3169
3170
3171
3172
    return; // If first LED is past end of strip, nothing to do
  }

  // Calculate the index ONE AFTER the last pixel to fill
Eric Duminil's avatar
Eric Duminil committed
3173
  if (count == 0) {
3174
3175
3176
3177
3178
    // Fill to end of strip
    end = numLEDs;
  } else {
    // Ensure that the loop won't go past the last pixel
    end = first + count;
Eric Duminil's avatar
Eric Duminil committed
3179
3180
    if (end > numLEDs)
      end = numLEDs;
3181
3182
  }

Eric Duminil's avatar
Eric Duminil committed
3183
  for (i = first; i < end; i++) {
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
    this->setPixelColor(i, c);
  }
}

/*!
  @brief   Convert hue, saturation and value into a packed 32-bit RGB color
           that can be passed to setPixelColor() or other RGB-compatible
           functions.
  @param   hue  An unsigned 16-bit value, 0 to 65535, representing one full
                loop of the color wheel, which allows 16-bit hues to "roll
                over" while still doing the expected thing (and allowing
                more precision than the wheel() function that was common to
                prior NeoPixel examples).
  @param   sat  Saturation, 8-bit value, 0 (min or pure grayscale) to 255
                (max or pure hue). Default of 255 if unspecified.
  @param   val  Value (brightness), 8-bit value, 0 (min / black / off) to
                255 (max or full brightness). Default of 255 if unspecified.
  @return  Packed 32-bit RGB with the most significant byte set to 0 -- the
           white element of WRGB pixels is NOT utilized. Result is linearly
           but not perceptually correct, so you may want to pass the result
           through the gamma32() function (or your own gamma-correction
           operation) else colors may appear washed out. This is not done
           automatically by this function because coders may desire a more
           refined gamma-correction function than the simplified
           one-size-fits-all operation of gamma32(). Diffusing the LEDs also
           really seems to help when using low-saturation colors.
*/
uint32_t Adafruit_NeoPixel::ColorHSV(uint16_t hue, uint8_t sat, uint8_t val) {

  uint8_t r, g, b;

  // Remap 0-65535 to 0-1529. Pure red is CENTERED on the 64K rollover;
  // 0 is not the start of pure red, but the midpoint...a few values above
  // zero and a few below 65536 all yield pure red (similarly, 32768 is the
  // midpoint, not start, of pure cyan). The 8-bit RGB hexcone (256 values
  // each for red, green, blue) really only allows for 1530 distinct hues
  // (not 1536, more on that below), but the full unsigned 16-bit type was
  // chosen for hue so that one's code can easily handle a contiguous color
  // wheel by allowing hue to roll over in either direction.
  hue = (hue * 1530L + 32768) / 65536;
  // Because red is centered on the rollover point (the +32768 above,
  // essentially a fixed-point +0.5), the above actually yields 0 to 1530,
  // where 0 and 1530 would yield the same thing. Rather than apply a
  // costly modulo operator, 1530 is handled as a special case below.

  // So you'd think that the color "hexcone" (the thing that ramps from
  // pure red, to pure yellow, to pure green and so forth back to red,
  // yielding six slices), and with each color component having 256
  // possible values (0-255), might have 1536 possible items (6*256),
  // but in reality there's 1530. This is because the last element in
  // each 256-element slice is equal to the first element of the next
  // slice, and keeping those in there this would create small
  // discontinuities in the color wheel. So the last element of each
  // slice is dropped...we regard only elements 0-254, with item 255
  // being picked up as element 0 of the next slice. Like this:
  // Red to not-quite-pure-yellow is:        255,   0, 0 to 255, 254,   0
  // Pure yellow to not-quite-pure-green is: 255, 255, 0 to   1, 255,   0
  // Pure green to not-quite-pure-cyan is:     0, 255, 0 to   0, 255, 254
  // and so forth. Hence, 1530 distinct hues (0 to 1529), and hence why
  // the constants below are not the multiples of 256 you might expect.

  // Convert hue to R,G,B (nested ifs faster than divide+mod+switch):
Eric Duminil's avatar
Eric Duminil committed
3246
  if (hue < 510) { // Red to Green-1
3247
    b = 0;
Eric Duminil's avatar
Eric Duminil committed
3248
    if (hue < 255) { //   Red to Yellow-1
3249
      r = 255;
Eric Duminil's avatar
Eric Duminil committed
3250
3251
3252
      g = hue;       //     g = 0 to 254
    } else {         //   Yellow to Green-1
      r = 510 - hue; //     r = 255 to 1
3253
3254
      g = 255;
    }
Eric Duminil's avatar
Eric Duminil committed
3255
  } else if (hue < 1020) { // Green to Blue-1
3256
    r = 0;
Eric Duminil's avatar
Eric Duminil committed
3257
    if (hue < 765) { //   Green to Cyan-1
3258
      g = 255;
Eric Duminil's avatar
Eric Duminil committed
3259
3260
3261
      b = hue - 510;  //     b = 0 to 254
    } else {          //   Cyan to Blue-1
      g = 1020 - hue; //     g = 255 to 1
3262
3263
      b = 255;
    }
Eric Duminil's avatar
Eric Duminil committed
3264
  } else if (hue < 1530) { // Blue to Red-1
3265
    g = 0;
Eric Duminil's avatar
Eric Duminil committed
3266
3267
    if (hue < 1275) { //   Blue to Magenta-1
      r = hue - 1020; //     r = 0 to 254
3268
      b = 255;
Eric Duminil's avatar
Eric Duminil committed
3269
    } else { //   Magenta to Red-1
3270
      r = 255;
Eric Duminil's avatar
Eric Duminil committed
3271
      b = 1530 - hue; //     b = 255 to 1
3272
    }
Eric Duminil's avatar
Eric Duminil committed
3273
  } else { // Last 0.5 Red (quicker than % operator)
3274
3275
3276
3277
3278
    r = 255;
    g = b = 0;
  }

  // Apply saturation and value to R,G,B, pack into 32-bit result:
Eric Duminil's avatar
Eric Duminil committed
3279
3280
3281
  uint32_t v1 = 1 + val;  // 1 to 256; allows >>8 instead of /255
  uint16_t s1 = 1 + sat;  // 1 to 256; same reason
  uint8_t s2 = 255 - sat; // 255 to 0
3282
  return ((((((r * s1) >> 8) + s2) * v1) & 0xff00) << 8) |
Eric Duminil's avatar
Eric Duminil committed
3283
3284
         (((((g * s1) >> 8) + s2) * v1) & 0xff00) |
         (((((b * s1) >> 8) + s2) * v1) >> 8);
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
}

/*!
  @brief   Query the color of a previously-set pixel.
  @param   n  Index of pixel to read (0 = first).
  @return  'Packed' 32-bit RGB or WRGB value. Most significant byte is white
           (for RGBW pixels) or 0 (for RGB pixels), next is red, then green,
           and least significant byte is blue.
  @note    If the strip brightness has been changed from the default value
           of 255, the color read from a pixel may not exactly match what
           was previously written with one of the setPixelColor() functions.
           This gets more pronounced at lower brightness levels.
*/
uint32_t Adafruit_NeoPixel::getPixelColor(uint16_t n) const {
Eric Duminil's avatar
Eric Duminil committed
3299
3300
  if (n >= numLEDs)
    return 0; // Out of bounds, return no color.
3301
3302
3303

  uint8_t *p;

Eric Duminil's avatar
Eric Duminil committed
3304
  if (wOffset == rOffset) { // Is RGB-type device
3305
    p = &pixels[n * 3];
Eric Duminil's avatar
Eric Duminil committed
3306
    if (brightness) {
3307
3308
3309
3310
3311
3312
      // Stored color was decimated by setBrightness(). Returned value
      // attempts to scale back to an approximation of the original 24-bit
      // value used when setting the pixel color, but there will always be
      // some error -- those bits are simply gone. Issue is most
      // pronounced at low brightness levels.
      return (((uint32_t)(p[rOffset] << 8) / brightness) << 16) |
Eric Duminil's avatar
Eric Duminil committed
3313
3314
             (((uint32_t)(p[gOffset] << 8) / brightness) << 8) |
             ((uint32_t)(p[bOffset] << 8) / brightness);
3315
3316
    } else {
      // No brightness adjustment has been made -- return 'raw' color
Eric Duminil's avatar
Eric Duminil committed
3317
3318
      return ((uint32_t)p[rOffset] << 16) | ((uint32_t)p[gOffset] << 8) |
             (uint32_t)p[bOffset];
3319
    }
Eric Duminil's avatar
Eric Duminil committed
3320
  } else { // Is RGBW-type device
3321
    p = &pixels[n * 4];
Eric Duminil's avatar
Eric Duminil committed
3322
    if (brightness) { // Return scaled color
3323
3324
      return (((uint32_t)(p[wOffset] << 8) / brightness) << 24) |
             (((uint32_t)(p[rOffset] << 8) / brightness) << 16) |
Eric Duminil's avatar
Eric Duminil committed
3325
3326
             (((uint32_t)(p[gOffset] << 8) / brightness) << 8) |
             ((uint32_t)(p[bOffset] << 8) / brightness);
3327
    } else { // Return raw color
Eric Duminil's avatar
Eric Duminil committed
3328
3329
      return ((uint32_t)p[wOffset] << 24) | ((uint32_t)p[rOffset] << 16) |
             ((uint32_t)p[gOffset] << 8) | (uint32_t)p[bOffset];
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
    }
  }
}

/*!
  @brief   Adjust output brightness. Does not immediately affect what's
           currently displayed on the LEDs. The next call to show() will
           refresh the LEDs at this level.
  @param   b  Brightness setting, 0=minimum (off), 255=brightest.
  @note    This was intended for one-time use in one's setup() function,
           not as an animation effect in itself. Because of the way this
           library "pre-multiplies" LED colors in RAM, changing the
           brightness is often a "lossy" operation -- what you write to
           pixels isn't necessary the same as what you'll read back.
           Repeated brightness changes using this function exacerbate the
           problem. Smart programs therefore treat the strip as a
           write-only resource, maintaining their own state to render each
           frame of an animation, not relying on read-modify-write.
*/
void Adafruit_NeoPixel::setBrightness(uint8_t b) {
  // Stored brightness value is different than what's passed.
  // This simplifies the actual scaling math later, allowing a fast
  // 8x8-bit multiply and taking the MSB. 'brightness' is a uint8_t,
  // adding 1 here may (intentionally) roll over...so 0 = max brightness
  // (color values are interpreted literally; no scaling), 1 = min
  // brightness (off), 255 = just below max brightness.
  uint8_t newBrightness = b + 1;
Eric Duminil's avatar
Eric Duminil committed
3357
  if (newBrightness != brightness) { // Compare against prior value
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
    // Brightness has changed -- re-scale existing data in RAM,
    // This process is potentially "lossy," especially when increasing
    // brightness. The tight timing in the WS2811/WS2812 code means there
    // aren't enough free cycles to perform this scaling on the fly as data
    // is issued. So we make a pass through the existing color data in RAM
    // and scale it (subsequent graphics commands also work at this
    // brightness level). If there's a significant step up in brightness,
    // the limited number of steps (quantization) in the old data will be
    // quite visible in the re-scaled version. For a non-destructive
    // change, you'll need to re-render the full strip data. C'est la vie.
Eric Duminil's avatar
Eric Duminil committed
3368
3369
    uint8_t c, *ptr = pixels,
               oldBrightness = brightness - 1; // De-wrap old brightness value
3370
    uint16_t scale;
Eric Duminil's avatar
Eric Duminil committed
3371
3372
3373
3374
3375
3376
3377
3378
    if (oldBrightness == 0)
      scale = 0; // Avoid /0
    else if (b == 255)
      scale = 65535 / oldBrightness;
    else
      scale = (((uint16_t)newBrightness << 8) - 1) / oldBrightness;
    for (uint16_t i = 0; i < numBytes; i++) {
      c = *ptr;
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
      *ptr++ = (c * scale) >> 8;
    }
    brightness = newBrightness;
  }
}

/*!
  @brief   Retrieve the last-set brightness value for the strip.
  @return  Brightness value: 0 = minimum (off), 255 = maximum.
*/
Eric Duminil's avatar
Eric Duminil committed
3389
uint8_t Adafruit_NeoPixel::getBrightness(void) const { return brightness - 1; }
3390
3391
3392
3393

/*!
  @brief   Fill the whole NeoPixel strip with 0 / black / off.
*/
Eric Duminil's avatar
Eric Duminil committed
3394
void Adafruit_NeoPixel::clear(void) { memset(pixels, 0, numBytes); }
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407

// A 32-bit variant of gamma8() that applies the same function
// to all components of a packed RGB or WRGB value.
uint32_t Adafruit_NeoPixel::gamma32(uint32_t x) {
  uint8_t *y = (uint8_t *)&x;
  // All four bytes of a 32-bit value are filtered even if RGB (not WRGB),
  // to avoid a bunch of shifting and masking that would be necessary for
  // properly handling different endianisms (and each byte is a fairly
  // trivial operation, so it might not even be wasting cycles vs a check
  // and branch for the RGB case). In theory this might cause trouble *if*
  // someone's storing information in the unused most significant byte
  // of an RGB value, but this seems exceedingly rare and if it's
  // encountered in reality they can mask values going in or coming out.
Eric Duminil's avatar
Eric Duminil committed
3408
3409
  for (uint8_t i = 0; i < 4; i++)
    y[i] = gamma8(y[i]);
3410
3411
  return x; // Packed 32-bit return
}
Eric Duminil's avatar
Eric Duminil committed
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439

/*!
  @brief   Fill NeoPixel strip with one or more cycles of hues.
           Everyone loves the rainbow swirl so much, now it's canon!
  @param   first_hue   Hue of first pixel, 0-65535, representing one full
                       cycle of the color wheel. Each subsequent pixel will
                       be offset to complete one or more cycles over the
                       length of the strip.
  @param   reps        Number of cycles of the color wheel over the length
                       of the strip. Default is 1. Negative values can be
                       used to reverse the hue order.
  @param   saturation  Saturation (optional), 0-255 = gray to pure hue,
                       default = 255.
  @param   brightness  Brightness/value (optional), 0-255 = off to max,
                       default = 255. This is distinct and in combination
                       with any configured global strip brightness.
  @param   gammify     If true (default), apply gamma correction to colors
                       for better appearance.
*/
void Adafruit_NeoPixel::rainbow(uint16_t first_hue, int8_t reps,
  uint8_t saturation, uint8_t brightness, bool gammify) {
  for (uint16_t i=0; i<numLEDs; i++) {
    uint16_t hue = first_hue + (i * reps * 65536) / numLEDs;
    uint32_t color = ColorHSV(hue, saturation, brightness);
    if (gammify) color = gamma32(color);
    setPixelColor(i, color);
  }
}