Adafruit_NeoPixel.cpp 99 KB

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