Adafruit_SPITFT.cpp 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534
  1. /*!
  2. * @file Adafruit_SPITFT.cpp
  3. *
  4. * @mainpage Adafruit SPI TFT Displays (and some others)
  5. *
  6. * @section intro_sec Introduction
  7. *
  8. * Part of Adafruit's GFX graphics library. Originally this class was
  9. * written to handle a range of color TFT displays connected via SPI,
  10. * but over time this library and some display-specific subclasses have
  11. * mutated to include some color OLEDs as well as parallel-interfaced
  12. * displays. The name's been kept for the sake of older code.
  13. *
  14. * Adafruit invests time and resources providing this open source code,
  15. * please support Adafruit and open-source hardware by purchasing
  16. * products from Adafruit!
  17. * @section dependencies Dependencies
  18. *
  19. * This library depends on <a href="https://github.com/adafruit/Adafruit_GFX">
  20. * Adafruit_GFX</a> being present on your system. Please make sure you have
  21. * installed the latest version before using this library.
  22. *
  23. * @section author Author
  24. *
  25. * Written by Limor "ladyada" Fried for Adafruit Industries,
  26. * with contributions from the open source community.
  27. *
  28. * @section license License
  29. *
  30. * BSD license, all text here must be included in any redistribution.
  31. */
  32. #if !defined(__AVR_ATtiny85__) // Not for ATtiny, at all
  33. #include "Adafruit_SPITFT.h"
  34. #if defined(__AVR__)
  35. #if defined(__AVR_XMEGA__) // only tested with __AVR_ATmega4809__
  36. #define AVR_WRITESPI(x) \
  37. for (SPI0_DATA = (x); (!(SPI0_INTFLAGS & _BV(SPI_IF_bp)));)
  38. #else
  39. #define AVR_WRITESPI(x) for (SPDR = (x); (!(SPSR & _BV(SPIF)));)
  40. #endif
  41. #endif
  42. #if defined(PORT_IOBUS)
  43. // On SAMD21, redefine digitalPinToPort() to use the slightly-faster
  44. // PORT_IOBUS rather than PORT (not needed on SAMD51).
  45. #undef digitalPinToPort
  46. #define digitalPinToPort(P) (&(PORT_IOBUS->Group[g_APinDescription[P].ulPort]))
  47. #endif // end PORT_IOBUS
  48. #if defined(USE_SPI_DMA) && (defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO))
  49. // #pragma message ("GFX DMA IS ENABLED. HIGHLY EXPERIMENTAL.")
  50. #include "wiring_private.h" // pinPeripheral() function
  51. #include <Adafruit_ZeroDMA.h>
  52. #include <malloc.h> // memalign() function
  53. #define tcNum 2 // Timer/Counter for parallel write strobe PWM
  54. #define wrPeripheral PIO_CCL // Use CCL to invert write strobe
  55. // DMA transfer-in-progress indicator and callback
  56. static volatile bool dma_busy = false;
  57. static void dma_callback(Adafruit_ZeroDMA *dma) { dma_busy = false; }
  58. #if defined(__SAMD51__)
  59. // Timer/counter info by index #
  60. static const struct {
  61. Tc *tc; // -> Timer/Counter base address
  62. int gclk; // GCLK ID
  63. int evu; // EVSYS user ID
  64. } tcList[] = {{TC0, TC0_GCLK_ID, EVSYS_ID_USER_TC0_EVU},
  65. {TC1, TC1_GCLK_ID, EVSYS_ID_USER_TC1_EVU},
  66. {TC2, TC2_GCLK_ID, EVSYS_ID_USER_TC2_EVU},
  67. {TC3, TC3_GCLK_ID, EVSYS_ID_USER_TC3_EVU},
  68. #if defined(TC4)
  69. {TC4, TC4_GCLK_ID, EVSYS_ID_USER_TC4_EVU},
  70. #endif
  71. #if defined(TC5)
  72. {TC5, TC5_GCLK_ID, EVSYS_ID_USER_TC5_EVU},
  73. #endif
  74. #if defined(TC6)
  75. {TC6, TC6_GCLK_ID, EVSYS_ID_USER_TC6_EVU},
  76. #endif
  77. #if defined(TC7)
  78. {TC7, TC7_GCLK_ID, EVSYS_ID_USER_TC7_EVU}
  79. #endif
  80. };
  81. #define NUM_TIMERS (sizeof tcList / sizeof tcList[0]) ///< # timer/counters
  82. #endif // end __SAMD51__
  83. #endif // end USE_SPI_DMA
  84. // Possible values for Adafruit_SPITFT.connection:
  85. #define TFT_HARD_SPI 0 ///< Display interface = hardware SPI
  86. #define TFT_SOFT_SPI 1 ///< Display interface = software SPI
  87. #define TFT_PARALLEL 2 ///< Display interface = 8- or 16-bit parallel
  88. // CONSTRUCTORS ------------------------------------------------------------
  89. /*!
  90. @brief Adafruit_SPITFT constructor for software (bitbang) SPI.
  91. @param w Display width in pixels at default rotation setting (0).
  92. @param h Display height in pixels at default rotation setting (0).
  93. @param cs Arduino pin # for chip-select (-1 if unused, tie CS low).
  94. @param dc Arduino pin # for data/command select (required).
  95. @param mosi Arduino pin # for bitbang SPI MOSI signal (required).
  96. @param sck Arduino pin # for bitbang SPI SCK signal (required).
  97. @param rst Arduino pin # for display reset (optional, display reset
  98. can be tied to MCU reset, default of -1 means unused).
  99. @param miso Arduino pin # for bitbang SPI MISO signal (optional,
  100. -1 default, many displays don't support SPI read).
  101. @note Output pins are not initialized; application typically will
  102. need to call subclass' begin() function, which in turn calls
  103. this library's initSPI() function to initialize pins.
  104. */
  105. Adafruit_SPITFT::Adafruit_SPITFT(uint16_t w, uint16_t h, int8_t cs, int8_t dc,
  106. int8_t mosi, int8_t sck, int8_t rst,
  107. int8_t miso)
  108. : Adafruit_GFX(w, h), connection(TFT_SOFT_SPI), _rst(rst), _cs(cs),
  109. _dc(dc) {
  110. swspi._sck = sck;
  111. swspi._mosi = mosi;
  112. swspi._miso = miso;
  113. #if defined(USE_FAST_PINIO)
  114. #if defined(HAS_PORT_SET_CLR)
  115. #if defined(CORE_TEENSY)
  116. #if !defined(KINETISK)
  117. dcPinMask = digitalPinToBitMask(dc);
  118. swspi.sckPinMask = digitalPinToBitMask(sck);
  119. swspi.mosiPinMask = digitalPinToBitMask(mosi);
  120. #endif
  121. dcPortSet = portSetRegister(dc);
  122. dcPortClr = portClearRegister(dc);
  123. swspi.sckPortSet = portSetRegister(sck);
  124. swspi.sckPortClr = portClearRegister(sck);
  125. swspi.mosiPortSet = portSetRegister(mosi);
  126. swspi.mosiPortClr = portClearRegister(mosi);
  127. if (cs >= 0) {
  128. #if !defined(KINETISK)
  129. csPinMask = digitalPinToBitMask(cs);
  130. #endif
  131. csPortSet = portSetRegister(cs);
  132. csPortClr = portClearRegister(cs);
  133. } else {
  134. #if !defined(KINETISK)
  135. csPinMask = 0;
  136. #endif
  137. csPortSet = dcPortSet;
  138. csPortClr = dcPortClr;
  139. }
  140. if (miso >= 0) {
  141. swspi.misoPort = portInputRegister(miso);
  142. #if !defined(KINETISK)
  143. swspi.misoPinMask = digitalPinToBitMask(miso);
  144. #endif
  145. } else {
  146. swspi.misoPort = portInputRegister(dc);
  147. }
  148. #else // !CORE_TEENSY
  149. dcPinMask = digitalPinToBitMask(dc);
  150. swspi.sckPinMask = digitalPinToBitMask(sck);
  151. swspi.mosiPinMask = digitalPinToBitMask(mosi);
  152. dcPortSet = &(PORT->Group[g_APinDescription[dc].ulPort].OUTSET.reg);
  153. dcPortClr = &(PORT->Group[g_APinDescription[dc].ulPort].OUTCLR.reg);
  154. swspi.sckPortSet = &(PORT->Group[g_APinDescription[sck].ulPort].OUTSET.reg);
  155. swspi.sckPortClr = &(PORT->Group[g_APinDescription[sck].ulPort].OUTCLR.reg);
  156. swspi.mosiPortSet = &(PORT->Group[g_APinDescription[mosi].ulPort].OUTSET.reg);
  157. swspi.mosiPortClr = &(PORT->Group[g_APinDescription[mosi].ulPort].OUTCLR.reg);
  158. if (cs >= 0) {
  159. csPinMask = digitalPinToBitMask(cs);
  160. csPortSet = &(PORT->Group[g_APinDescription[cs].ulPort].OUTSET.reg);
  161. csPortClr = &(PORT->Group[g_APinDescription[cs].ulPort].OUTCLR.reg);
  162. } else {
  163. // No chip-select line defined; might be permanently tied to GND.
  164. // Assign a valid GPIO register (though not used for CS), and an
  165. // empty pin bitmask...the nonsense bit-twiddling might be faster
  166. // than checking _cs and possibly branching.
  167. csPortSet = dcPortSet;
  168. csPortClr = dcPortClr;
  169. csPinMask = 0;
  170. }
  171. if (miso >= 0) {
  172. swspi.misoPinMask = digitalPinToBitMask(miso);
  173. swspi.misoPort = (PORTreg_t)portInputRegister(digitalPinToPort(miso));
  174. } else {
  175. swspi.misoPinMask = 0;
  176. swspi.misoPort = (PORTreg_t)portInputRegister(digitalPinToPort(dc));
  177. }
  178. #endif // end !CORE_TEENSY
  179. #else // !HAS_PORT_SET_CLR
  180. dcPort = (PORTreg_t)portOutputRegister(digitalPinToPort(dc));
  181. dcPinMaskSet = digitalPinToBitMask(dc);
  182. swspi.sckPort = (PORTreg_t)portOutputRegister(digitalPinToPort(sck));
  183. swspi.sckPinMaskSet = digitalPinToBitMask(sck);
  184. swspi.mosiPort = (PORTreg_t)portOutputRegister(digitalPinToPort(mosi));
  185. swspi.mosiPinMaskSet = digitalPinToBitMask(mosi);
  186. if (cs >= 0) {
  187. csPort = (PORTreg_t)portOutputRegister(digitalPinToPort(cs));
  188. csPinMaskSet = digitalPinToBitMask(cs);
  189. } else {
  190. // No chip-select line defined; might be permanently tied to GND.
  191. // Assign a valid GPIO register (though not used for CS), and an
  192. // empty pin bitmask...the nonsense bit-twiddling might be faster
  193. // than checking _cs and possibly branching.
  194. csPort = dcPort;
  195. csPinMaskSet = 0;
  196. }
  197. if (miso >= 0) {
  198. swspi.misoPort = (PORTreg_t)portInputRegister(digitalPinToPort(miso));
  199. swspi.misoPinMask = digitalPinToBitMask(miso);
  200. } else {
  201. swspi.misoPort = (PORTreg_t)portInputRegister(digitalPinToPort(dc));
  202. swspi.misoPinMask = 0;
  203. }
  204. csPinMaskClr = ~csPinMaskSet;
  205. dcPinMaskClr = ~dcPinMaskSet;
  206. swspi.sckPinMaskClr = ~swspi.sckPinMaskSet;
  207. swspi.mosiPinMaskClr = ~swspi.mosiPinMaskSet;
  208. #endif // !end HAS_PORT_SET_CLR
  209. #endif // end USE_FAST_PINIO
  210. }
  211. /*!
  212. @brief Adafruit_SPITFT constructor for hardware SPI using the board's
  213. default SPI peripheral.
  214. @param w Display width in pixels at default rotation setting (0).
  215. @param h Display height in pixels at default rotation setting (0).
  216. @param cs Arduino pin # for chip-select (-1 if unused, tie CS low).
  217. @param dc Arduino pin # for data/command select (required).
  218. @param rst Arduino pin # for display reset (optional, display reset
  219. can be tied to MCU reset, default of -1 means unused).
  220. @note Output pins are not initialized; application typically will
  221. need to call subclass' begin() function, which in turn calls
  222. this library's initSPI() function to initialize pins.
  223. */
  224. #if defined(ESP8266) // See notes below
  225. Adafruit_SPITFT::Adafruit_SPITFT(uint16_t w, uint16_t h, int8_t cs, int8_t dc,
  226. int8_t rst)
  227. : Adafruit_GFX(w, h), connection(TFT_HARD_SPI), _rst(rst), _cs(cs),
  228. _dc(dc) {
  229. hwspi._spi = &SPI;
  230. }
  231. #else // !ESP8266
  232. Adafruit_SPITFT::Adafruit_SPITFT(uint16_t w, uint16_t h, int8_t cs, int8_t dc,
  233. int8_t rst)
  234. : Adafruit_SPITFT(w, h, &SPI, cs, dc, rst) {
  235. // This just invokes the hardware SPI constructor below,
  236. // passing the default SPI device (&SPI).
  237. }
  238. #endif // end !ESP8266
  239. #if !defined(ESP8266)
  240. // ESP8266 compiler freaks out at this constructor -- it can't disambiguate
  241. // beteween the SPIClass pointer (argument #3) and a regular integer.
  242. // Solution here it to just not offer this variant on the ESP8266. You can
  243. // use the default hardware SPI peripheral, or you can use software SPI,
  244. // but if there's any library out there that creates a 'virtual' SPIClass
  245. // peripheral and drives it with software bitbanging, that's not supported.
  246. /*!
  247. @brief Adafruit_SPITFT constructor for hardware SPI using a specific
  248. SPI peripheral.
  249. @param w Display width in pixels at default rotation (0).
  250. @param h Display height in pixels at default rotation (0).
  251. @param spiClass Pointer to SPIClass type (e.g. &SPI or &SPI1).
  252. @param cs Arduino pin # for chip-select (-1 if unused, tie CS low).
  253. @param dc Arduino pin # for data/command select (required).
  254. @param rst Arduino pin # for display reset (optional, display reset
  255. can be tied to MCU reset, default of -1 means unused).
  256. @note Output pins are not initialized in constructor; application
  257. typically will need to call subclass' begin() function, which
  258. in turn calls this library's initSPI() function to initialize
  259. pins. EXCEPT...if you have built your own SERCOM SPI peripheral
  260. (calling the SPIClass constructor) rather than one of the
  261. built-in SPI devices (e.g. &SPI, &SPI1 and so forth), you will
  262. need to call the begin() function for your object as well as
  263. pinPeripheral() for the MOSI, MISO and SCK pins to configure
  264. GPIO manually. Do this BEFORE calling the display-specific
  265. begin or init function. Unfortunate but unavoidable.
  266. */
  267. Adafruit_SPITFT::Adafruit_SPITFT(uint16_t w, uint16_t h, SPIClass *spiClass,
  268. int8_t cs, int8_t dc, int8_t rst)
  269. : Adafruit_GFX(w, h), connection(TFT_HARD_SPI), _rst(rst), _cs(cs),
  270. _dc(dc) {
  271. hwspi._spi = spiClass;
  272. #if defined(USE_FAST_PINIO)
  273. #if defined(HAS_PORT_SET_CLR)
  274. #if defined(CORE_TEENSY)
  275. #if !defined(KINETISK)
  276. dcPinMask = digitalPinToBitMask(dc);
  277. #endif
  278. dcPortSet = portSetRegister(dc);
  279. dcPortClr = portClearRegister(dc);
  280. if (cs >= 0) {
  281. #if !defined(KINETISK)
  282. csPinMask = digitalPinToBitMask(cs);
  283. #endif
  284. csPortSet = portSetRegister(cs);
  285. csPortClr = portClearRegister(cs);
  286. } else { // see comments below
  287. #if !defined(KINETISK)
  288. csPinMask = 0;
  289. #endif
  290. csPortSet = dcPortSet;
  291. csPortClr = dcPortClr;
  292. }
  293. #else // !CORE_TEENSY
  294. dcPinMask = digitalPinToBitMask(dc);
  295. dcPortSet = &(PORT->Group[g_APinDescription[dc].ulPort].OUTSET.reg);
  296. dcPortClr = &(PORT->Group[g_APinDescription[dc].ulPort].OUTCLR.reg);
  297. if (cs >= 0) {
  298. csPinMask = digitalPinToBitMask(cs);
  299. csPortSet = &(PORT->Group[g_APinDescription[cs].ulPort].OUTSET.reg);
  300. csPortClr = &(PORT->Group[g_APinDescription[cs].ulPort].OUTCLR.reg);
  301. } else {
  302. // No chip-select line defined; might be permanently tied to GND.
  303. // Assign a valid GPIO register (though not used for CS), and an
  304. // empty pin bitmask...the nonsense bit-twiddling might be faster
  305. // than checking _cs and possibly branching.
  306. csPortSet = dcPortSet;
  307. csPortClr = dcPortClr;
  308. csPinMask = 0;
  309. }
  310. #endif // end !CORE_TEENSY
  311. #else // !HAS_PORT_SET_CLR
  312. dcPort = (PORTreg_t)portOutputRegister(digitalPinToPort(dc));
  313. dcPinMaskSet = digitalPinToBitMask(dc);
  314. if (cs >= 0) {
  315. csPort = (PORTreg_t)portOutputRegister(digitalPinToPort(cs));
  316. csPinMaskSet = digitalPinToBitMask(cs);
  317. } else {
  318. // No chip-select line defined; might be permanently tied to GND.
  319. // Assign a valid GPIO register (though not used for CS), and an
  320. // empty pin bitmask...the nonsense bit-twiddling might be faster
  321. // than checking _cs and possibly branching.
  322. csPort = dcPort;
  323. csPinMaskSet = 0;
  324. }
  325. csPinMaskClr = ~csPinMaskSet;
  326. dcPinMaskClr = ~dcPinMaskSet;
  327. #endif // end !HAS_PORT_SET_CLR
  328. #endif // end USE_FAST_PINIO
  329. }
  330. #endif // end !ESP8266
  331. /*!
  332. @brief Adafruit_SPITFT constructor for parallel display connection.
  333. @param w Display width in pixels at default rotation (0).
  334. @param h Display height in pixels at default rotation (0).
  335. @param busWidth If tft16 (enumeration in header file), is a 16-bit
  336. parallel connection, else 8-bit.
  337. 16-bit isn't fully implemented or tested yet so
  338. applications should pass "tft8bitbus" for now...needed to
  339. stick a required enum argument in there to
  340. disambiguate this constructor from the soft-SPI case.
  341. Argument is ignored on 8-bit architectures (no 'wide'
  342. support there since PORTs are 8 bits anyway).
  343. @param d0 Arduino pin # for data bit 0 (1+ are extrapolated).
  344. The 8 (or 16) data bits MUST be contiguous and byte-
  345. aligned (or word-aligned for wide interface) within
  346. the same PORT register (might not correspond to
  347. Arduino pin sequence).
  348. @param wr Arduino pin # for write strobe (required).
  349. @param dc Arduino pin # for data/command select (required).
  350. @param cs Arduino pin # for chip-select (optional, -1 if unused,
  351. tie CS low).
  352. @param rst Arduino pin # for display reset (optional, display reset
  353. can be tied to MCU reset, default of -1 means unused).
  354. @param rd Arduino pin # for read strobe (optional, -1 if unused).
  355. @note Output pins are not initialized; application typically will need
  356. to call subclass' begin() function, which in turn calls this
  357. library's initSPI() function to initialize pins.
  358. Yes, the name is a misnomer...this library originally handled
  359. only SPI displays, parallel being a recent addition (but not
  360. wanting to break existing code).
  361. */
  362. Adafruit_SPITFT::Adafruit_SPITFT(uint16_t w, uint16_t h, tftBusWidth busWidth,
  363. int8_t d0, int8_t wr, int8_t dc, int8_t cs,
  364. int8_t rst, int8_t rd)
  365. : Adafruit_GFX(w, h), connection(TFT_PARALLEL), _rst(rst), _cs(cs),
  366. _dc(dc) {
  367. tft8._d0 = d0;
  368. tft8._wr = wr;
  369. tft8._rd = rd;
  370. tft8.wide = (busWidth == tft16bitbus);
  371. #if defined(USE_FAST_PINIO)
  372. #if defined(HAS_PORT_SET_CLR)
  373. #if defined(CORE_TEENSY)
  374. tft8.wrPortSet = portSetRegister(wr);
  375. tft8.wrPortClr = portClearRegister(wr);
  376. #if !defined(KINETISK)
  377. dcPinMask = digitalPinToBitMask(dc);
  378. #endif
  379. dcPortSet = portSetRegister(dc);
  380. dcPortClr = portClearRegister(dc);
  381. if (cs >= 0) {
  382. #if !defined(KINETISK)
  383. csPinMask = digitalPinToBitMask(cs);
  384. #endif
  385. csPortSet = portSetRegister(cs);
  386. csPortClr = portClearRegister(cs);
  387. } else { // see comments below
  388. #if !defined(KINETISK)
  389. csPinMask = 0;
  390. #endif
  391. csPortSet = dcPortSet;
  392. csPortClr = dcPortClr;
  393. }
  394. if (rd >= 0) { // if read-strobe pin specified...
  395. #if defined(KINETISK)
  396. tft8.rdPinMask = 1;
  397. #else // !KINETISK
  398. tft8.rdPinMask = digitalPinToBitMask(rd);
  399. #endif
  400. tft8.rdPortSet = portSetRegister(rd);
  401. tft8.rdPortClr = portClearRegister(rd);
  402. } else {
  403. tft8.rdPinMask = 0;
  404. tft8.rdPortSet = dcPortSet;
  405. tft8.rdPortClr = dcPortClr;
  406. }
  407. // These are all uint8_t* pointers -- elsewhere they're recast
  408. // as necessary if a 'wide' 16-bit interface is in use.
  409. tft8.writePort = portOutputRegister(d0);
  410. tft8.readPort = portInputRegister(d0);
  411. tft8.dirSet = portModeRegister(d0);
  412. tft8.dirClr = portModeRegister(d0);
  413. #else // !CORE_TEENSY
  414. tft8.wrPinMask = digitalPinToBitMask(wr);
  415. tft8.wrPortSet = &(PORT->Group[g_APinDescription[wr].ulPort].OUTSET.reg);
  416. tft8.wrPortClr = &(PORT->Group[g_APinDescription[wr].ulPort].OUTCLR.reg);
  417. dcPinMask = digitalPinToBitMask(dc);
  418. dcPortSet = &(PORT->Group[g_APinDescription[dc].ulPort].OUTSET.reg);
  419. dcPortClr = &(PORT->Group[g_APinDescription[dc].ulPort].OUTCLR.reg);
  420. if (cs >= 0) {
  421. csPinMask = digitalPinToBitMask(cs);
  422. csPortSet = &(PORT->Group[g_APinDescription[cs].ulPort].OUTSET.reg);
  423. csPortClr = &(PORT->Group[g_APinDescription[cs].ulPort].OUTCLR.reg);
  424. } else {
  425. // No chip-select line defined; might be permanently tied to GND.
  426. // Assign a valid GPIO register (though not used for CS), and an
  427. // empty pin bitmask...the nonsense bit-twiddling might be faster
  428. // than checking _cs and possibly branching.
  429. csPortSet = dcPortSet;
  430. csPortClr = dcPortClr;
  431. csPinMask = 0;
  432. }
  433. if (rd >= 0) { // if read-strobe pin specified...
  434. tft8.rdPinMask = digitalPinToBitMask(rd);
  435. tft8.rdPortSet = &(PORT->Group[g_APinDescription[rd].ulPort].OUTSET.reg);
  436. tft8.rdPortClr = &(PORT->Group[g_APinDescription[rd].ulPort].OUTCLR.reg);
  437. } else {
  438. tft8.rdPinMask = 0;
  439. tft8.rdPortSet = dcPortSet;
  440. tft8.rdPortClr = dcPortClr;
  441. }
  442. // Get pointers to PORT write/read/dir bytes within 32-bit PORT
  443. uint8_t dBit = g_APinDescription[d0].ulPin; // d0 bit # in PORT
  444. PortGroup *p = (&(PORT->Group[g_APinDescription[d0].ulPort]));
  445. uint8_t offset = dBit / 8; // d[7:0] byte # within PORT
  446. if (tft8.wide)
  447. offset &= ~1; // d[15:8] byte # within PORT
  448. // These are all uint8_t* pointers -- elsewhere they're recast
  449. // as necessary if a 'wide' 16-bit interface is in use.
  450. tft8.writePort = (volatile uint8_t *)&(p->OUT.reg) + offset;
  451. tft8.readPort = (volatile uint8_t *)&(p->IN.reg) + offset;
  452. tft8.dirSet = (volatile uint8_t *)&(p->DIRSET.reg) + offset;
  453. tft8.dirClr = (volatile uint8_t *)&(p->DIRCLR.reg) + offset;
  454. #endif // end !CORE_TEENSY
  455. #else // !HAS_PORT_SET_CLR
  456. tft8.wrPort = (PORTreg_t)portOutputRegister(digitalPinToPort(wr));
  457. tft8.wrPinMaskSet = digitalPinToBitMask(wr);
  458. dcPort = (PORTreg_t)portOutputRegister(digitalPinToPort(dc));
  459. dcPinMaskSet = digitalPinToBitMask(dc);
  460. if (cs >= 0) {
  461. csPort = (PORTreg_t)portOutputRegister(digitalPinToPort(cs));
  462. csPinMaskSet = digitalPinToBitMask(cs);
  463. } else {
  464. // No chip-select line defined; might be permanently tied to GND.
  465. // Assign a valid GPIO register (though not used for CS), and an
  466. // empty pin bitmask...the nonsense bit-twiddling might be faster
  467. // than checking _cs and possibly branching.
  468. csPort = dcPort;
  469. csPinMaskSet = 0;
  470. }
  471. if (rd >= 0) { // if read-strobe pin specified...
  472. tft8.rdPort = (PORTreg_t)portOutputRegister(digitalPinToPort(rd));
  473. tft8.rdPinMaskSet = digitalPinToBitMask(rd);
  474. } else {
  475. tft8.rdPort = dcPort;
  476. tft8.rdPinMaskSet = 0;
  477. }
  478. csPinMaskClr = ~csPinMaskSet;
  479. dcPinMaskClr = ~dcPinMaskSet;
  480. tft8.wrPinMaskClr = ~tft8.wrPinMaskSet;
  481. tft8.rdPinMaskClr = ~tft8.rdPinMaskSet;
  482. tft8.writePort = (PORTreg_t)portOutputRegister(digitalPinToPort(d0));
  483. tft8.readPort = (PORTreg_t)portInputRegister(digitalPinToPort(d0));
  484. tft8.portDir = (PORTreg_t)portModeRegister(digitalPinToPort(d0));
  485. #endif // end !HAS_PORT_SET_CLR
  486. #endif // end USE_FAST_PINIO
  487. }
  488. // end constructors -------
  489. // CLASS MEMBER FUNCTIONS --------------------------------------------------
  490. // begin() and setAddrWindow() MUST be declared by any subclass.
  491. /*!
  492. @brief Configure microcontroller pins for TFT interfacing. Typically
  493. called by a subclass' begin() function.
  494. @param freq SPI frequency when using hardware SPI. If default (0)
  495. is passed, will fall back on a device-specific value.
  496. Value is ignored when using software SPI or parallel
  497. connection.
  498. @param spiMode SPI mode when using hardware SPI. MUST be one of the
  499. values SPI_MODE0, SPI_MODE1, SPI_MODE2 or SPI_MODE3
  500. defined in SPI.h. Do NOT attempt to pass '0' for
  501. SPI_MODE0 and so forth...the values are NOT the same!
  502. Use ONLY the defines! (Pity it's not an enum.)
  503. @note Another anachronistically-named function; this is called even
  504. when the display connection is parallel (not SPI). Also, this
  505. could probably be made private...quite a few class functions
  506. were generously put in the public section.
  507. */
  508. void Adafruit_SPITFT::initSPI(uint32_t freq, uint8_t spiMode) {
  509. if (!freq)
  510. freq = DEFAULT_SPI_FREQ; // If no freq specified, use default
  511. // Init basic control pins common to all connection types
  512. if (_cs >= 0) {
  513. pinMode(_cs, OUTPUT);
  514. digitalWrite(_cs, HIGH); // Deselect
  515. }
  516. pinMode(_dc, OUTPUT);
  517. digitalWrite(_dc, HIGH); // Data mode
  518. if (connection == TFT_HARD_SPI) {
  519. #if defined(SPI_HAS_TRANSACTION)
  520. hwspi.settings = SPISettings(freq, MSBFIRST, spiMode);
  521. #else
  522. hwspi._freq = freq; // Save freq value for later
  523. #endif
  524. hwspi._mode = spiMode; // Save spiMode value for later
  525. // Call hwspi._spi->begin() ONLY if this is among the 'established'
  526. // SPI interfaces in variant.h. For DIY roll-your-own SERCOM SPIs,
  527. // begin() and pinPeripheral() calls MUST be made in one's calling
  528. // code, BEFORE the screen-specific begin/init function is called.
  529. // Reason for this is that SPI::begin() makes its own calls to
  530. // pinPeripheral() based on g_APinDescription[n].ulPinType, which
  531. // on non-established SPI interface pins will always be PIO_DIGITAL
  532. // or similar, while we need PIO_SERCOM or PIO_SERCOM_ALT...it's
  533. // highly unique between devices and variants for each pin or
  534. // SERCOM so we can't make those calls ourselves here. And the SPI
  535. // device needs to be set up before calling this because it's
  536. // immediately followed with initialization commands. Blargh.
  537. if (
  538. #if !defined(SPI_INTERFACES_COUNT)
  539. 1
  540. #else
  541. #if SPI_INTERFACES_COUNT > 0
  542. (hwspi._spi == &SPI)
  543. #endif
  544. #if SPI_INTERFACES_COUNT > 1
  545. || (hwspi._spi == &SPI1)
  546. #endif
  547. #if SPI_INTERFACES_COUNT > 2
  548. || (hwspi._spi == &SPI2)
  549. #endif
  550. #if SPI_INTERFACES_COUNT > 3
  551. || (hwspi._spi == &SPI3)
  552. #endif
  553. #if SPI_INTERFACES_COUNT > 4
  554. || (hwspi._spi == &SPI4)
  555. #endif
  556. #if SPI_INTERFACES_COUNT > 5
  557. || (hwspi._spi == &SPI5)
  558. #endif
  559. #endif // end SPI_INTERFACES_COUNT
  560. ) {
  561. hwspi._spi->begin();
  562. }
  563. } else if (connection == TFT_SOFT_SPI) {
  564. pinMode(swspi._mosi, OUTPUT);
  565. digitalWrite(swspi._mosi, LOW);
  566. pinMode(swspi._sck, OUTPUT);
  567. digitalWrite(swspi._sck, LOW);
  568. if (swspi._miso >= 0) {
  569. pinMode(swspi._miso, INPUT);
  570. }
  571. } else { // TFT_PARALLEL
  572. // Initialize data pins. We were only passed d0, so scan
  573. // the pin description list looking for the other pins.
  574. // They'll be on the same PORT, and within the next 7 (or 15) bits
  575. // (because we need to write to a contiguous PORT byte or word).
  576. #if defined(__AVR__)
  577. // PORT registers are 8 bits wide, so just need a register match...
  578. for (uint8_t i = 0; i < NUM_DIGITAL_PINS; i++) {
  579. if ((PORTreg_t)portOutputRegister(digitalPinToPort(i)) ==
  580. tft8.writePort) {
  581. pinMode(i, OUTPUT);
  582. digitalWrite(i, LOW);
  583. }
  584. }
  585. #elif defined(USE_FAST_PINIO)
  586. #if defined(CORE_TEENSY)
  587. if (!tft8.wide) {
  588. *tft8.dirSet = 0xFF; // Set port to output
  589. *tft8.writePort = 0x00; // Write all 0s
  590. } else {
  591. *(volatile uint16_t *)tft8.dirSet = 0xFFFF;
  592. *(volatile uint16_t *)tft8.writePort = 0x0000;
  593. }
  594. #else // !CORE_TEENSY
  595. uint8_t portNum = g_APinDescription[tft8._d0].ulPort, // d0 PORT #
  596. dBit = g_APinDescription[tft8._d0].ulPin, // d0 bit in PORT
  597. lastBit = dBit + (tft8.wide ? 15 : 7);
  598. for (uint8_t i = 0; i < PINS_COUNT; i++) {
  599. if ((g_APinDescription[i].ulPort == portNum) &&
  600. (g_APinDescription[i].ulPin >= dBit) &&
  601. (g_APinDescription[i].ulPin <= (uint32_t)lastBit)) {
  602. pinMode(i, OUTPUT);
  603. digitalWrite(i, LOW);
  604. }
  605. }
  606. #endif // end !CORE_TEENSY
  607. #endif
  608. pinMode(tft8._wr, OUTPUT);
  609. digitalWrite(tft8._wr, HIGH);
  610. if (tft8._rd >= 0) {
  611. pinMode(tft8._rd, OUTPUT);
  612. digitalWrite(tft8._rd, HIGH);
  613. }
  614. }
  615. if (_rst >= 0) {
  616. // Toggle _rst low to reset
  617. pinMode(_rst, OUTPUT);
  618. digitalWrite(_rst, HIGH);
  619. delay(100);
  620. digitalWrite(_rst, LOW);
  621. delay(100);
  622. digitalWrite(_rst, HIGH);
  623. delay(200);
  624. }
  625. #if defined(USE_SPI_DMA) && (defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO))
  626. if (((connection == TFT_HARD_SPI) || (connection == TFT_PARALLEL)) &&
  627. (dma.allocate() == DMA_STATUS_OK)) { // Allocate channel
  628. // The DMA library needs to alloc at least one valid descriptor,
  629. // so we do that here. It's not used in the usual sense though,
  630. // just before a transfer we copy descriptor[0] to this address.
  631. if (dptr = dma.addDescriptor(NULL, NULL, 42, DMA_BEAT_SIZE_BYTE, false,
  632. false)) {
  633. // Alloc 2 scanlines worth of pixels on display's major axis,
  634. // whichever that is, rounding each up to 2-pixel boundary.
  635. int major = (WIDTH > HEIGHT) ? WIDTH : HEIGHT;
  636. major += (major & 1); // -> next 2-pixel bound, if needed.
  637. maxFillLen = major * 2; // 2 scanlines
  638. // Note to future self: if you decide to make the pixel buffer
  639. // much larger, remember that DMA transfer descriptors can't
  640. // exceed 65,535 bytes (not 65,536), meaning 32,767 pixels max.
  641. // Not that we have that kind of RAM to throw around right now.
  642. if ((pixelBuf[0] = (uint16_t *)malloc(maxFillLen * sizeof(uint16_t)))) {
  643. // Alloc OK. Get pointer to start of second scanline.
  644. pixelBuf[1] = &pixelBuf[0][major];
  645. // Determine number of DMA descriptors needed to cover
  646. // entire screen when entire 2-line pixelBuf is used
  647. // (round up for fractional last descriptor).
  648. int numDescriptors = (WIDTH * HEIGHT + (maxFillLen - 1)) / maxFillLen;
  649. // DMA descriptors MUST be 128-bit (16 byte) aligned.
  650. // memalign() is considered obsolete but it's replacements
  651. // (aligned_alloc() or posix_memalign()) are not currently
  652. // available in the version of ARM GCC in use, but this
  653. // is, so here we are.
  654. if ((descriptor = (DmacDescriptor *)memalign(
  655. 16, numDescriptors * sizeof(DmacDescriptor)))) {
  656. int dmac_id;
  657. volatile uint32_t *data_reg;
  658. if (connection == TFT_HARD_SPI) {
  659. // THIS IS AN AFFRONT TO NATURE, but I don't know
  660. // any "clean" way to get the sercom number from the
  661. // the SPIClass pointer (e.g. &SPI or &SPI1), which
  662. // is all we have to work with. SPIClass does contain
  663. // a SERCOM pointer but it is a PRIVATE member!
  664. // Doing an UNSPEAKABLY HORRIBLE THING here, directly
  665. // accessing the first 32-bit value in the SPIClass
  666. // structure, knowing that's (currently) where the
  667. // SERCOM pointer lives, but this ENTIRELY DEPENDS on
  668. // that structure not changing nor the compiler
  669. // rearranging things. Oh the humanity!
  670. if (*(SERCOM **)hwspi._spi == &sercom0) {
  671. dmac_id = SERCOM0_DMAC_ID_TX;
  672. data_reg = &SERCOM0->SPI.DATA.reg;
  673. #if defined SERCOM1
  674. } else if (*(SERCOM **)hwspi._spi == &sercom1) {
  675. dmac_id = SERCOM1_DMAC_ID_TX;
  676. data_reg = &SERCOM1->SPI.DATA.reg;
  677. #endif
  678. #if defined SERCOM2
  679. } else if (*(SERCOM **)hwspi._spi == &sercom2) {
  680. dmac_id = SERCOM2_DMAC_ID_TX;
  681. data_reg = &SERCOM2->SPI.DATA.reg;
  682. #endif
  683. #if defined SERCOM3
  684. } else if (*(SERCOM **)hwspi._spi == &sercom3) {
  685. dmac_id = SERCOM3_DMAC_ID_TX;
  686. data_reg = &SERCOM3->SPI.DATA.reg;
  687. #endif
  688. #if defined SERCOM4
  689. } else if (*(SERCOM **)hwspi._spi == &sercom4) {
  690. dmac_id = SERCOM4_DMAC_ID_TX;
  691. data_reg = &SERCOM4->SPI.DATA.reg;
  692. #endif
  693. #if defined SERCOM5
  694. } else if (*(SERCOM **)hwspi._spi == &sercom5) {
  695. dmac_id = SERCOM5_DMAC_ID_TX;
  696. data_reg = &SERCOM5->SPI.DATA.reg;
  697. #endif
  698. #if defined SERCOM6
  699. } else if (*(SERCOM **)hwspi._spi == &sercom6) {
  700. dmac_id = SERCOM6_DMAC_ID_TX;
  701. data_reg = &SERCOM6->SPI.DATA.reg;
  702. #endif
  703. #if defined SERCOM7
  704. } else if (*(SERCOM **)hwspi._spi == &sercom7) {
  705. dmac_id = SERCOM7_DMAC_ID_TX;
  706. data_reg = &SERCOM7->SPI.DATA.reg;
  707. #endif
  708. }
  709. dma.setPriority(DMA_PRIORITY_3);
  710. dma.setTrigger(dmac_id);
  711. dma.setAction(DMA_TRIGGER_ACTON_BEAT);
  712. // Initialize descriptor list.
  713. for (int d = 0; d < numDescriptors; d++) {
  714. // No need to set SRCADDR, DESCADDR or BTCNT --
  715. // those are done in the pixel-writing functions.
  716. descriptor[d].BTCTRL.bit.VALID = true;
  717. descriptor[d].BTCTRL.bit.EVOSEL = DMA_EVENT_OUTPUT_DISABLE;
  718. descriptor[d].BTCTRL.bit.BLOCKACT = DMA_BLOCK_ACTION_NOACT;
  719. descriptor[d].BTCTRL.bit.BEATSIZE = DMA_BEAT_SIZE_BYTE;
  720. descriptor[d].BTCTRL.bit.DSTINC = 0;
  721. descriptor[d].BTCTRL.bit.STEPSEL = DMA_STEPSEL_SRC;
  722. descriptor[d].BTCTRL.bit.STEPSIZE =
  723. DMA_ADDRESS_INCREMENT_STEP_SIZE_1;
  724. descriptor[d].DSTADDR.reg = (uint32_t)data_reg;
  725. }
  726. } else { // Parallel connection
  727. #if defined(__SAMD51__)
  728. int dmaChannel = dma.getChannel();
  729. // Enable event output, use EVOSEL output
  730. DMAC->Channel[dmaChannel].CHEVCTRL.bit.EVOE = 1;
  731. DMAC->Channel[dmaChannel].CHEVCTRL.bit.EVOMODE = 0;
  732. // CONFIGURE TIMER/COUNTER (for write strobe)
  733. Tc *timer = tcList[tcNum].tc; // -> Timer struct
  734. int id = tcList[tcNum].gclk; // Timer GCLK ID
  735. GCLK_PCHCTRL_Type pchctrl;
  736. // Set up timer clock source from GCLK
  737. GCLK->PCHCTRL[id].bit.CHEN = 0; // Stop timer
  738. while (GCLK->PCHCTRL[id].bit.CHEN)
  739. ; // Wait for it
  740. pchctrl.bit.GEN = GCLK_PCHCTRL_GEN_GCLK0_Val;
  741. pchctrl.bit.CHEN = 1; // Enable
  742. GCLK->PCHCTRL[id].reg = pchctrl.reg;
  743. while (!GCLK->PCHCTRL[id].bit.CHEN)
  744. ; // Wait for it
  745. // Disable timer/counter before configuring it
  746. timer->COUNT8.CTRLA.bit.ENABLE = 0;
  747. while (timer->COUNT8.SYNCBUSY.bit.STATUS)
  748. ;
  749. timer->COUNT8.WAVE.bit.WAVEGEN = 2; // NPWM
  750. timer->COUNT8.CTRLA.bit.MODE = 1; // 8-bit
  751. timer->COUNT8.CTRLA.bit.PRESCALER = 0; // 1:1
  752. while (timer->COUNT8.SYNCBUSY.bit.STATUS)
  753. ;
  754. timer->COUNT8.CTRLBCLR.bit.DIR = 1; // Count UP
  755. while (timer->COUNT8.SYNCBUSY.bit.CTRLB)
  756. ;
  757. timer->COUNT8.CTRLBSET.bit.ONESHOT = 1; // One-shot
  758. while (timer->COUNT8.SYNCBUSY.bit.CTRLB)
  759. ;
  760. timer->COUNT8.PER.reg = 6; // PWM top
  761. while (timer->COUNT8.SYNCBUSY.bit.PER)
  762. ;
  763. timer->COUNT8.CC[0].reg = 2; // Compare
  764. while (timer->COUNT8.SYNCBUSY.bit.CC0)
  765. ;
  766. // Enable async input events,
  767. // event action = restart.
  768. timer->COUNT8.EVCTRL.bit.TCEI = 1;
  769. timer->COUNT8.EVCTRL.bit.EVACT = 1;
  770. // Enable timer
  771. timer->COUNT8.CTRLA.reg |= TC_CTRLA_ENABLE;
  772. while (timer->COUNT8.SYNCBUSY.bit.STATUS)
  773. ;
  774. #if (wrPeripheral == PIO_CCL)
  775. // CONFIGURE CCL (inverts timer/counter output)
  776. MCLK->APBCMASK.bit.CCL_ = 1; // Enable CCL clock
  777. CCL->CTRL.bit.ENABLE = 0; // Disable to config
  778. CCL->CTRL.bit.SWRST = 1; // Reset CCL registers
  779. CCL->LUTCTRL[tcNum].bit.ENABLE = 0; // Disable LUT
  780. CCL->LUTCTRL[tcNum].bit.FILTSEL = 0; // No filter
  781. CCL->LUTCTRL[tcNum].bit.INSEL0 = 6; // TC input
  782. CCL->LUTCTRL[tcNum].bit.INSEL1 = 0; // MASK
  783. CCL->LUTCTRL[tcNum].bit.INSEL2 = 0; // MASK
  784. CCL->LUTCTRL[tcNum].bit.TRUTH = 1; // Invert in 0
  785. CCL->LUTCTRL[tcNum].bit.ENABLE = 1; // Enable LUT
  786. CCL->CTRL.bit.ENABLE = 1; // Enable CCL
  787. #endif
  788. // CONFIGURE EVENT SYSTEM
  789. // Set up event system clock source from GCLK...
  790. // Disable EVSYS, wait for disable
  791. GCLK->PCHCTRL[EVSYS_GCLK_ID_0].bit.CHEN = 0;
  792. while (GCLK->PCHCTRL[EVSYS_GCLK_ID_0].bit.CHEN)
  793. ;
  794. pchctrl.bit.GEN = GCLK_PCHCTRL_GEN_GCLK0_Val;
  795. pchctrl.bit.CHEN = 1; // Re-enable
  796. GCLK->PCHCTRL[EVSYS_GCLK_ID_0].reg = pchctrl.reg;
  797. // Wait for it, then enable EVSYS clock
  798. while (!GCLK->PCHCTRL[EVSYS_GCLK_ID_0].bit.CHEN)
  799. ;
  800. MCLK->APBBMASK.bit.EVSYS_ = 1;
  801. // Connect Timer EVU to ch 0
  802. EVSYS->USER[tcList[tcNum].evu].reg = 1;
  803. // Datasheet recommends single write operation;
  804. // reg instead of bit. Also datasheet: PATH bits
  805. // must be zero when using async!
  806. EVSYS_CHANNEL_Type ev;
  807. ev.reg = 0;
  808. ev.bit.PATH = 2; // Asynchronous
  809. ev.bit.EVGEN = 0x22 + dmaChannel; // DMA channel 0+
  810. EVSYS->Channel[0].CHANNEL.reg = ev.reg;
  811. // Initialize descriptor list.
  812. for (int d = 0; d < numDescriptors; d++) {
  813. // No need to set SRCADDR, DESCADDR or BTCNT --
  814. // those are done in the pixel-writing functions.
  815. descriptor[d].BTCTRL.bit.VALID = true;
  816. // Event strobe on beat xfer:
  817. descriptor[d].BTCTRL.bit.EVOSEL = 0x3;
  818. descriptor[d].BTCTRL.bit.BLOCKACT = DMA_BLOCK_ACTION_NOACT;
  819. descriptor[d].BTCTRL.bit.BEATSIZE =
  820. tft8.wide ? DMA_BEAT_SIZE_HWORD : DMA_BEAT_SIZE_BYTE;
  821. descriptor[d].BTCTRL.bit.SRCINC = 1;
  822. descriptor[d].BTCTRL.bit.DSTINC = 0;
  823. descriptor[d].BTCTRL.bit.STEPSEL = DMA_STEPSEL_SRC;
  824. descriptor[d].BTCTRL.bit.STEPSIZE =
  825. DMA_ADDRESS_INCREMENT_STEP_SIZE_1;
  826. descriptor[d].DSTADDR.reg = (uint32_t)tft8.writePort;
  827. }
  828. #endif // __SAMD51
  829. } // end parallel-specific DMA setup
  830. lastFillColor = 0x0000;
  831. lastFillLen = 0;
  832. dma.setCallback(dma_callback);
  833. return; // Success!
  834. // else clean up any partial allocation...
  835. } // end descriptor memalign()
  836. free(pixelBuf[0]);
  837. pixelBuf[0] = pixelBuf[1] = NULL;
  838. } // end pixelBuf malloc()
  839. // Don't currently have a descriptor delete function in
  840. // ZeroDMA lib, but if we did, it would be called here.
  841. } // end addDescriptor()
  842. dma.free(); // Deallocate DMA channel
  843. }
  844. #endif // end USE_SPI_DMA
  845. }
  846. /*!
  847. @brief Allow changing the SPI clock speed after initialization
  848. @param freq Desired frequency of SPI clock, may not be the
  849. end frequency you get based on what the chip can do!
  850. */
  851. void Adafruit_SPITFT::setSPISpeed(uint32_t freq) {
  852. #if defined(SPI_HAS_TRANSACTION)
  853. hwspi.settings = SPISettings(freq, MSBFIRST, hwspi._mode);
  854. #else
  855. hwspi._freq = freq; // Save freq value for later
  856. #endif
  857. }
  858. /*!
  859. @brief Call before issuing command(s) or data to display. Performs
  860. chip-select (if required) and starts an SPI transaction (if
  861. using hardware SPI and transactions are supported). Required
  862. for all display types; not an SPI-specific function.
  863. */
  864. void Adafruit_SPITFT::startWrite(void) {
  865. SPI_BEGIN_TRANSACTION();
  866. if (_cs >= 0)
  867. SPI_CS_LOW();
  868. }
  869. /*!
  870. @brief Call after issuing command(s) or data to display. Performs
  871. chip-deselect (if required) and ends an SPI transaction (if
  872. using hardware SPI and transactions are supported). Required
  873. for all display types; not an SPI-specific function.
  874. */
  875. void Adafruit_SPITFT::endWrite(void) {
  876. if (_cs >= 0)
  877. SPI_CS_HIGH();
  878. SPI_END_TRANSACTION();
  879. }
  880. // -------------------------------------------------------------------------
  881. // Lower-level graphics operations. These functions require a chip-select
  882. // and/or SPI transaction around them (via startWrite(), endWrite() above).
  883. // Higher-level graphics primitives might start a single transaction and
  884. // then make multiple calls to these functions (e.g. circle or text
  885. // rendering might make repeated lines or rects) before ending the
  886. // transaction. It's more efficient than starting a transaction every time.
  887. /*!
  888. @brief Draw a single pixel to the display at requested coordinates.
  889. Not self-contained; should follow a startWrite() call.
  890. @param x Horizontal position (0 = left).
  891. @param y Vertical position (0 = top).
  892. @param color 16-bit pixel color in '565' RGB format.
  893. */
  894. void Adafruit_SPITFT::writePixel(int16_t x, int16_t y, uint16_t color) {
  895. if ((x >= 0) && (x < _width) && (y >= 0) && (y < _height)) {
  896. setAddrWindow(x, y, 1, 1);
  897. SPI_WRITE16(color);
  898. }
  899. }
  900. /*!
  901. @brief Issue a series of pixels from memory to the display. Not self-
  902. contained; should follow startWrite() and setAddrWindow() calls.
  903. @param colors Pointer to array of 16-bit pixel values in '565' RGB
  904. format.
  905. @param len Number of elements in 'colors' array.
  906. @param block If true (default case if unspecified), function blocks
  907. until DMA transfer is complete. This is simply IGNORED
  908. if DMA is not enabled. If false, the function returns
  909. immediately after the last DMA transfer is started,
  910. and one should use the dmaWait() function before
  911. doing ANY other display-related activities (or even
  912. any SPI-related activities, if using an SPI display
  913. that shares the bus with other devices).
  914. @param bigEndian If using DMA, and if set true, bitmap in memory is in
  915. big-endian order (most significant byte first). By
  916. default this is false, as most microcontrollers seem
  917. to be little-endian and 16-bit pixel values must be
  918. byte-swapped before issuing to the display (which tend
  919. to be big-endian when using SPI or 8-bit parallel).
  920. If an application can optimize around this -- for
  921. example, a bitmap in a uint16_t array having the byte
  922. values already reordered big-endian, this can save
  923. some processing time here, ESPECIALLY if using this
  924. function's non-blocking DMA mode. Not all cases are
  925. covered...this is really here only for SAMD DMA and
  926. much forethought on the application side.
  927. */
  928. void Adafruit_SPITFT::writePixels(uint16_t *colors, uint32_t len, bool block,
  929. bool bigEndian) {
  930. if (!len)
  931. return; // Avoid 0-byte transfers
  932. // avoid paramater-not-used complaints
  933. (void)block;
  934. (void)bigEndian;
  935. #if defined(ESP32) // ESP32 has a special SPI pixel-writing function...
  936. if (connection == TFT_HARD_SPI) {
  937. hwspi._spi->writePixels(colors, len * 2);
  938. return;
  939. }
  940. #elif defined(ARDUINO_ARCH_RP2040) || (defined(ARDUINO_NRF52_ADAFRUIT) && \
  941. defined(NRF52840_XXAA)) // Adafruit nRF52 use SPIM3 DMA at 32Mhz
  942. // TFT and SPI DMA endian is different we need to swap bytes
  943. if (!bigEndian) {
  944. for (uint32_t i = 0; i < len; i++) {
  945. colors[i] = __builtin_bswap16(colors[i]);
  946. }
  947. }
  948. // use the separate tx, rx buf variant to prevent overwrite the buffer
  949. hwspi._spi->transfer(colors, NULL, 2 * len);
  950. // swap back color buffer
  951. if (!bigEndian) {
  952. for (uint32_t i = 0; i < len; i++) {
  953. colors[i] = __builtin_bswap16(colors[i]);
  954. }
  955. }
  956. return;
  957. #elif defined(USE_SPI_DMA) && \
  958. (defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO))
  959. if ((connection == TFT_HARD_SPI) || (connection == TFT_PARALLEL)) {
  960. int maxSpan = maxFillLen / 2; // One scanline max
  961. uint8_t pixelBufIdx = 0; // Active pixel buffer number
  962. #if defined(__SAMD51__)
  963. if (connection == TFT_PARALLEL) {
  964. // Switch WR pin to PWM or CCL
  965. pinPeripheral(tft8._wr, wrPeripheral);
  966. }
  967. #endif // end __SAMD51__
  968. if (!bigEndian) { // Normal little-endian situation...
  969. while (len) {
  970. int count = (len < maxSpan) ? len : maxSpan;
  971. // Because TFT and SAMD endianisms are different, must swap
  972. // bytes from the 'colors' array passed into a DMA working
  973. // buffer. This can take place while the prior DMA transfer
  974. // is in progress, hence the need for two pixelBufs.
  975. for (int i = 0; i < count; i++) {
  976. pixelBuf[pixelBufIdx][i] = __builtin_bswap16(*colors++);
  977. }
  978. // The transfers themselves are relatively small, so we don't
  979. // need a long descriptor list. We just alternate between the
  980. // first two, sharing pixelBufIdx for that purpose.
  981. descriptor[pixelBufIdx].SRCADDR.reg =
  982. (uint32_t)pixelBuf[pixelBufIdx] + count * 2;
  983. descriptor[pixelBufIdx].BTCTRL.bit.SRCINC = 1;
  984. descriptor[pixelBufIdx].BTCNT.reg = count * 2;
  985. descriptor[pixelBufIdx].DESCADDR.reg = 0;
  986. while (dma_busy)
  987. ; // Wait for prior line to finish
  988. // Move new descriptor into place...
  989. memcpy(dptr, &descriptor[pixelBufIdx], sizeof(DmacDescriptor));
  990. dma_busy = true;
  991. dma.startJob(); // Trigger SPI DMA transfer
  992. if (connection == TFT_PARALLEL)
  993. dma.trigger();
  994. pixelBufIdx = 1 - pixelBufIdx; // Swap DMA pixel buffers
  995. len -= count;
  996. }
  997. } else { // bigEndian == true
  998. // With big-endian pixel data, this can be handled as a single
  999. // DMA transfer using chained descriptors. Even full screen, this
  1000. // needs only a relatively short descriptor list, each
  1001. // transferring a max of 32,767 (not 32,768) pixels. The list
  1002. // was allocated large enough to accommodate a full screen's
  1003. // worth of data, so this won't run past the end of the list.
  1004. int d, numDescriptors = (len + 32766) / 32767;
  1005. for (d = 0; d < numDescriptors; d++) {
  1006. int count = (len < 32767) ? len : 32767;
  1007. descriptor[d].SRCADDR.reg = (uint32_t)colors + count * 2;
  1008. descriptor[d].BTCTRL.bit.SRCINC = 1;
  1009. descriptor[d].BTCNT.reg = count * 2;
  1010. descriptor[d].DESCADDR.reg = (uint32_t)&descriptor[d + 1];
  1011. len -= count;
  1012. colors += count;
  1013. }
  1014. descriptor[d - 1].DESCADDR.reg = 0;
  1015. while (dma_busy)
  1016. ; // Wait for prior transfer (if any) to finish
  1017. // Move first descriptor into place and start transfer...
  1018. memcpy(dptr, &descriptor[0], sizeof(DmacDescriptor));
  1019. dma_busy = true;
  1020. dma.startJob(); // Trigger SPI DMA transfer
  1021. if (connection == TFT_PARALLEL)
  1022. dma.trigger();
  1023. } // end bigEndian
  1024. lastFillColor = 0x0000; // pixelBuf has been sullied
  1025. lastFillLen = 0;
  1026. if (block) {
  1027. while (dma_busy)
  1028. ; // Wait for last line to complete
  1029. #if defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO)
  1030. if (connection == TFT_HARD_SPI) {
  1031. // See SAMD51/21 note in writeColor()
  1032. hwspi._spi->setDataMode(hwspi._mode);
  1033. } else {
  1034. pinPeripheral(tft8._wr, PIO_OUTPUT); // Switch WR back to GPIO
  1035. }
  1036. #endif // end __SAMD51__ || ARDUINO_SAMD_ZERO
  1037. }
  1038. return;
  1039. }
  1040. #endif // end USE_SPI_DMA
  1041. // All other cases (bitbang SPI or non-DMA hard SPI or parallel),
  1042. // use a loop with the normal 16-bit data write function:
  1043. while (len--) {
  1044. SPI_WRITE16(*colors++);
  1045. }
  1046. }
  1047. /*!
  1048. @brief Wait for the last DMA transfer in a prior non-blocking
  1049. writePixels() call to complete. This does nothing if DMA
  1050. is not enabled, and is not needed if blocking writePixels()
  1051. was used (as is the default case).
  1052. */
  1053. void Adafruit_SPITFT::dmaWait(void) {
  1054. #if defined(USE_SPI_DMA) && (defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO))
  1055. while (dma_busy)
  1056. ;
  1057. #if defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO)
  1058. if (connection == TFT_HARD_SPI) {
  1059. // See SAMD51/21 note in writeColor()
  1060. hwspi._spi->setDataMode(hwspi._mode);
  1061. } else {
  1062. pinPeripheral(tft8._wr, PIO_OUTPUT); // Switch WR back to GPIO
  1063. }
  1064. #endif // end __SAMD51__ || ARDUINO_SAMD_ZERO
  1065. #endif
  1066. }
  1067. /*!
  1068. @brief Issue a series of pixels, all the same color. Not self-
  1069. contained; should follow startWrite() and setAddrWindow() calls.
  1070. @param color 16-bit pixel color in '565' RGB format.
  1071. @param len Number of pixels to draw.
  1072. */
  1073. void Adafruit_SPITFT::writeColor(uint16_t color, uint32_t len) {
  1074. if (!len)
  1075. return; // Avoid 0-byte transfers
  1076. uint8_t hi = color >> 8, lo = color;
  1077. #if defined(ESP32) // ESP32 has a special SPI pixel-writing function...
  1078. if (connection == TFT_HARD_SPI) {
  1079. #define SPI_MAX_PIXELS_AT_ONCE 32
  1080. #define TMPBUF_LONGWORDS (SPI_MAX_PIXELS_AT_ONCE + 1) / 2
  1081. #define TMPBUF_PIXELS (TMPBUF_LONGWORDS * 2)
  1082. static uint32_t temp[TMPBUF_LONGWORDS];
  1083. uint32_t c32 = color * 0x00010001;
  1084. uint16_t bufLen = (len < TMPBUF_PIXELS) ? len : TMPBUF_PIXELS, xferLen,
  1085. fillLen;
  1086. // Fill temp buffer 32 bits at a time
  1087. fillLen = (bufLen + 1) / 2; // Round up to next 32-bit boundary
  1088. for (uint32_t t = 0; t < fillLen; t++) {
  1089. temp[t] = c32;
  1090. }
  1091. // Issue pixels in blocks from temp buffer
  1092. while (len) { // While pixels remain
  1093. xferLen = (bufLen < len) ? bufLen : len; // How many this pass?
  1094. writePixels((uint16_t *)temp, xferLen);
  1095. len -= xferLen;
  1096. }
  1097. return;
  1098. }
  1099. #elif defined(ARDUINO_ARCH_RP2040) // RP2040 has a way to write a block at a time
  1100. // at most 2 scan lines
  1101. uint32_t const pixbufcount = min(len, ((uint32_t)2 * width()));
  1102. uint16_t pixbuf[pixbufcount];
  1103. color = __builtin_bswap16(color);
  1104. while (len) {
  1105. uint32_t const count = min(len, pixbufcount);
  1106. // fill buffer with color
  1107. for (uint32_t i = 0; i < count; i++) {
  1108. pixbuf[i] = color;
  1109. }
  1110. writePixels(pixbuf, count, true, true);
  1111. len -= count;
  1112. }
  1113. return;
  1114. #elif defined(ARDUINO_NRF52_ADAFRUIT) && \
  1115. defined(NRF52840_XXAA) // Adafruit nRF52840 use SPIM3 DMA at 32Mhz
  1116. // at most 2 scan lines
  1117. uint32_t const pixbufcount = min(len, ((uint32_t)2 * width()));
  1118. uint16_t *pixbuf = (uint16_t *)rtos_malloc(2 * pixbufcount);
  1119. // use SPI3 DMA if we could allocate buffer, else fall back to writing each
  1120. // pixel loop below
  1121. if (pixbuf) {
  1122. uint16_t const swap_color = __builtin_bswap16(color);
  1123. // fill buffer with color
  1124. for (uint32_t i = 0; i < pixbufcount; i++) {
  1125. pixbuf[i] = swap_color;
  1126. }
  1127. while (len) {
  1128. uint32_t const count = min(len, pixbufcount);
  1129. writePixels(pixbuf, count, true, true);
  1130. len -= count;
  1131. }
  1132. rtos_free(pixbuf);
  1133. return;
  1134. }
  1135. #else // !ESP32
  1136. #if defined(USE_SPI_DMA) && (defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO))
  1137. if (((connection == TFT_HARD_SPI) || (connection == TFT_PARALLEL)) &&
  1138. (len >= 16)) { // Don't bother with DMA on short pixel runs
  1139. int i, d, numDescriptors;
  1140. if (hi == lo) { // If high & low bytes are same...
  1141. onePixelBuf = color;
  1142. // Can do this with a relatively short descriptor list,
  1143. // each transferring a max of 32,767 (not 32,768) pixels.
  1144. // This won't run off the end of the allocated descriptor list,
  1145. // since we're using much larger chunks per descriptor here.
  1146. numDescriptors = (len + 32766) / 32767;
  1147. for (d = 0; d < numDescriptors; d++) {
  1148. int count = (len < 32767) ? len : 32767;
  1149. descriptor[d].SRCADDR.reg = (uint32_t)&onePixelBuf;
  1150. descriptor[d].BTCTRL.bit.SRCINC = 0;
  1151. descriptor[d].BTCNT.reg = count * 2;
  1152. descriptor[d].DESCADDR.reg = (uint32_t)&descriptor[d + 1];
  1153. len -= count;
  1154. }
  1155. descriptor[d - 1].DESCADDR.reg = 0;
  1156. } else {
  1157. // If high and low bytes are distinct, it's necessary to fill
  1158. // a buffer with pixel data (swapping high and low bytes because
  1159. // TFT and SAMD are different endianisms) and create a longer
  1160. // descriptor list pointing repeatedly to this data. We can do
  1161. // this slightly faster working 2 pixels (32 bits) at a time.
  1162. uint32_t *pixelPtr = (uint32_t *)pixelBuf[0],
  1163. twoPixels = __builtin_bswap16(color) * 0x00010001;
  1164. // We can avoid some or all of the buffer-filling if the color
  1165. // is the same as last time...
  1166. if (color == lastFillColor) {
  1167. // If length is longer than prior instance, fill only the
  1168. // additional pixels in the buffer and update lastFillLen.
  1169. if (len > lastFillLen) {
  1170. int fillStart = lastFillLen / 2,
  1171. fillEnd = (((len < maxFillLen) ? len : maxFillLen) + 1) / 2;
  1172. for (i = fillStart; i < fillEnd; i++)
  1173. pixelPtr[i] = twoPixels;
  1174. lastFillLen = fillEnd * 2;
  1175. } // else do nothing, don't set pixels or change lastFillLen
  1176. } else {
  1177. int fillEnd = (((len < maxFillLen) ? len : maxFillLen) + 1) / 2;
  1178. for (i = 0; i < fillEnd; i++)
  1179. pixelPtr[i] = twoPixels;
  1180. lastFillLen = fillEnd * 2;
  1181. lastFillColor = color;
  1182. }
  1183. numDescriptors = (len + maxFillLen - 1) / maxFillLen;
  1184. for (d = 0; d < numDescriptors; d++) {
  1185. int pixels = (len < maxFillLen) ? len : maxFillLen, bytes = pixels * 2;
  1186. descriptor[d].SRCADDR.reg = (uint32_t)pixelPtr + bytes;
  1187. descriptor[d].BTCTRL.bit.SRCINC = 1;
  1188. descriptor[d].BTCNT.reg = bytes;
  1189. descriptor[d].DESCADDR.reg = (uint32_t)&descriptor[d + 1];
  1190. len -= pixels;
  1191. }
  1192. descriptor[d - 1].DESCADDR.reg = 0;
  1193. }
  1194. memcpy(dptr, &descriptor[0], sizeof(DmacDescriptor));
  1195. #if defined(__SAMD51__)
  1196. if (connection == TFT_PARALLEL) {
  1197. // Switch WR pin to PWM or CCL
  1198. pinPeripheral(tft8._wr, wrPeripheral);
  1199. }
  1200. #endif // end __SAMD51__
  1201. dma_busy = true;
  1202. dma.startJob();
  1203. if (connection == TFT_PARALLEL)
  1204. dma.trigger();
  1205. while (dma_busy)
  1206. ; // Wait for completion
  1207. // Unfortunately blocking is necessary. An earlier version returned
  1208. // immediately and checked dma_busy on startWrite() instead, but it
  1209. // turns out to be MUCH slower on many graphics operations (as when
  1210. // drawing lines, pixel-by-pixel), perhaps because it's a volatile
  1211. // type and doesn't cache. Working on this.
  1212. #if defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO)
  1213. if (connection == TFT_HARD_SPI) {
  1214. // SAMD51: SPI DMA seems to leave the SPI peripheral in a freaky
  1215. // state on completion. Workaround is to explicitly set it back...
  1216. // (5/17/2019: apparently SAMD21 too, in certain cases, observed
  1217. // with ST7789 display.)
  1218. hwspi._spi->setDataMode(hwspi._mode);
  1219. } else {
  1220. pinPeripheral(tft8._wr, PIO_OUTPUT); // Switch WR back to GPIO
  1221. }
  1222. #endif // end __SAMD51__
  1223. return;
  1224. }
  1225. #endif // end USE_SPI_DMA
  1226. #endif // end !ESP32
  1227. // All other cases (non-DMA hard SPI, bitbang SPI, parallel)...
  1228. if (connection == TFT_HARD_SPI) {
  1229. #if defined(ESP8266)
  1230. do {
  1231. uint32_t pixelsThisPass = len;
  1232. if (pixelsThisPass > 50000)
  1233. pixelsThisPass = 50000;
  1234. len -= pixelsThisPass;
  1235. yield(); // Periodic yield() on long fills
  1236. while (pixelsThisPass--) {
  1237. hwspi._spi->write(hi);
  1238. hwspi._spi->write(lo);
  1239. }
  1240. } while (len);
  1241. #else // !ESP8266
  1242. while (len--) {
  1243. #if defined(__AVR__)
  1244. AVR_WRITESPI(hi);
  1245. AVR_WRITESPI(lo);
  1246. #elif defined(ESP32)
  1247. hwspi._spi->write(hi);
  1248. hwspi._spi->write(lo);
  1249. #else
  1250. hwspi._spi->transfer(hi);
  1251. hwspi._spi->transfer(lo);
  1252. #endif
  1253. }
  1254. #endif // end !ESP8266
  1255. } else if (connection == TFT_SOFT_SPI) {
  1256. #if defined(ESP8266)
  1257. do {
  1258. uint32_t pixelsThisPass = len;
  1259. if (pixelsThisPass > 20000)
  1260. pixelsThisPass = 20000;
  1261. len -= pixelsThisPass;
  1262. yield(); // Periodic yield() on long fills
  1263. while (pixelsThisPass--) {
  1264. for (uint16_t bit = 0, x = color; bit < 16; bit++) {
  1265. if (x & 0x8000)
  1266. SPI_MOSI_HIGH();
  1267. else
  1268. SPI_MOSI_LOW();
  1269. SPI_SCK_HIGH();
  1270. SPI_SCK_LOW();
  1271. x <<= 1;
  1272. }
  1273. }
  1274. } while (len);
  1275. #else // !ESP8266
  1276. while (len--) {
  1277. #if defined(__AVR__)
  1278. for (uint8_t bit = 0, x = hi; bit < 8; bit++) {
  1279. if (x & 0x80)
  1280. SPI_MOSI_HIGH();
  1281. else
  1282. SPI_MOSI_LOW();
  1283. SPI_SCK_HIGH();
  1284. SPI_SCK_LOW();
  1285. x <<= 1;
  1286. }
  1287. for (uint8_t bit = 0, x = lo; bit < 8; bit++) {
  1288. if (x & 0x80)
  1289. SPI_MOSI_HIGH();
  1290. else
  1291. SPI_MOSI_LOW();
  1292. SPI_SCK_HIGH();
  1293. SPI_SCK_LOW();
  1294. x <<= 1;
  1295. }
  1296. #else // !__AVR__
  1297. for (uint16_t bit = 0, x = color; bit < 16; bit++) {
  1298. if (x & 0x8000)
  1299. SPI_MOSI_HIGH();
  1300. else
  1301. SPI_MOSI_LOW();
  1302. SPI_SCK_HIGH();
  1303. x <<= 1;
  1304. SPI_SCK_LOW();
  1305. }
  1306. #endif // end !__AVR__
  1307. }
  1308. #endif // end !ESP8266
  1309. } else { // PARALLEL
  1310. if (hi == lo) {
  1311. #if defined(__AVR__)
  1312. len *= 2;
  1313. *tft8.writePort = hi;
  1314. while (len--) {
  1315. TFT_WR_STROBE();
  1316. }
  1317. #elif defined(USE_FAST_PINIO)
  1318. if (!tft8.wide) {
  1319. len *= 2;
  1320. *tft8.writePort = hi;
  1321. } else {
  1322. *(volatile uint16_t *)tft8.writePort = color;
  1323. }
  1324. while (len--) {
  1325. TFT_WR_STROBE();
  1326. }
  1327. #endif
  1328. } else {
  1329. while (len--) {
  1330. #if defined(__AVR__)
  1331. *tft8.writePort = hi;
  1332. TFT_WR_STROBE();
  1333. *tft8.writePort = lo;
  1334. #elif defined(USE_FAST_PINIO)
  1335. if (!tft8.wide) {
  1336. *tft8.writePort = hi;
  1337. TFT_WR_STROBE();
  1338. *tft8.writePort = lo;
  1339. } else {
  1340. *(volatile uint16_t *)tft8.writePort = color;
  1341. }
  1342. #endif
  1343. TFT_WR_STROBE();
  1344. }
  1345. }
  1346. }
  1347. }
  1348. /*!
  1349. @brief Draw a filled rectangle to the display. Not self-contained;
  1350. should follow startWrite(). Typically used by higher-level
  1351. graphics primitives; user code shouldn't need to call this and
  1352. is likely to use the self-contained fillRect() instead.
  1353. writeFillRect() performs its own edge clipping and rejection;
  1354. see writeFillRectPreclipped() for a more 'raw' implementation.
  1355. @param x Horizontal position of first corner.
  1356. @param y Vertical position of first corner.
  1357. @param w Rectangle width in pixels (positive = right of first
  1358. corner, negative = left of first corner).
  1359. @param h Rectangle height in pixels (positive = below first
  1360. corner, negative = above first corner).
  1361. @param color 16-bit fill color in '565' RGB format.
  1362. @note Written in this deep-nested way because C by definition will
  1363. optimize for the 'if' case, not the 'else' -- avoids branches
  1364. and rejects clipped rectangles at the least-work possibility.
  1365. */
  1366. void Adafruit_SPITFT::writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h,
  1367. uint16_t color) {
  1368. if (w && h) { // Nonzero width and height?
  1369. if (w < 0) { // If negative width...
  1370. x += w + 1; // Move X to left edge
  1371. w = -w; // Use positive width
  1372. }
  1373. if (x < _width) { // Not off right
  1374. if (h < 0) { // If negative height...
  1375. y += h + 1; // Move Y to top edge
  1376. h = -h; // Use positive height
  1377. }
  1378. if (y < _height) { // Not off bottom
  1379. int16_t x2 = x + w - 1;
  1380. if (x2 >= 0) { // Not off left
  1381. int16_t y2 = y + h - 1;
  1382. if (y2 >= 0) { // Not off top
  1383. // Rectangle partly or fully overlaps screen
  1384. if (x < 0) {
  1385. x = 0;
  1386. w = x2 + 1;
  1387. } // Clip left
  1388. if (y < 0) {
  1389. y = 0;
  1390. h = y2 + 1;
  1391. } // Clip top
  1392. if (x2 >= _width) {
  1393. w = _width - x;
  1394. } // Clip right
  1395. if (y2 >= _height) {
  1396. h = _height - y;
  1397. } // Clip bottom
  1398. writeFillRectPreclipped(x, y, w, h, color);
  1399. }
  1400. }
  1401. }
  1402. }
  1403. }
  1404. }
  1405. /*!
  1406. @brief Draw a horizontal line on the display. Performs edge clipping
  1407. and rejection. Not self-contained; should follow startWrite().
  1408. Typically used by higher-level graphics primitives; user code
  1409. shouldn't need to call this and is likely to use the self-
  1410. contained drawFastHLine() instead.
  1411. @param x Horizontal position of first point.
  1412. @param y Vertical position of first point.
  1413. @param w Line width in pixels (positive = right of first point,
  1414. negative = point of first corner).
  1415. @param color 16-bit line color in '565' RGB format.
  1416. */
  1417. void inline Adafruit_SPITFT::writeFastHLine(int16_t x, int16_t y, int16_t w,
  1418. uint16_t color) {
  1419. if ((y >= 0) && (y < _height) && w) { // Y on screen, nonzero width
  1420. if (w < 0) { // If negative width...
  1421. x += w + 1; // Move X to left edge
  1422. w = -w; // Use positive width
  1423. }
  1424. if (x < _width) { // Not off right
  1425. int16_t x2 = x + w - 1;
  1426. if (x2 >= 0) { // Not off left
  1427. // Line partly or fully overlaps screen
  1428. if (x < 0) {
  1429. x = 0;
  1430. w = x2 + 1;
  1431. } // Clip left
  1432. if (x2 >= _width) {
  1433. w = _width - x;
  1434. } // Clip right
  1435. writeFillRectPreclipped(x, y, w, 1, color);
  1436. }
  1437. }
  1438. }
  1439. }
  1440. /*!
  1441. @brief Draw a vertical line on the display. Performs edge clipping and
  1442. rejection. Not self-contained; should follow startWrite().
  1443. Typically used by higher-level graphics primitives; user code
  1444. shouldn't need to call this and is likely to use the self-
  1445. contained drawFastVLine() instead.
  1446. @param x Horizontal position of first point.
  1447. @param y Vertical position of first point.
  1448. @param h Line height in pixels (positive = below first point,
  1449. negative = above first point).
  1450. @param color 16-bit line color in '565' RGB format.
  1451. */
  1452. void inline Adafruit_SPITFT::writeFastVLine(int16_t x, int16_t y, int16_t h,
  1453. uint16_t color) {
  1454. if ((x >= 0) && (x < _width) && h) { // X on screen, nonzero height
  1455. if (h < 0) { // If negative height...
  1456. y += h + 1; // Move Y to top edge
  1457. h = -h; // Use positive height
  1458. }
  1459. if (y < _height) { // Not off bottom
  1460. int16_t y2 = y + h - 1;
  1461. if (y2 >= 0) { // Not off top
  1462. // Line partly or fully overlaps screen
  1463. if (y < 0) {
  1464. y = 0;
  1465. h = y2 + 1;
  1466. } // Clip top
  1467. if (y2 >= _height) {
  1468. h = _height - y;
  1469. } // Clip bottom
  1470. writeFillRectPreclipped(x, y, 1, h, color);
  1471. }
  1472. }
  1473. }
  1474. }
  1475. /*!
  1476. @brief A lower-level version of writeFillRect(). This version requires
  1477. all inputs are in-bounds, that width and height are positive,
  1478. and no part extends offscreen. NO EDGE CLIPPING OR REJECTION IS
  1479. PERFORMED. If higher-level graphics primitives are written to
  1480. handle their own clipping earlier in the drawing process, this
  1481. can avoid unnecessary function calls and repeated clipping
  1482. operations in the lower-level functions.
  1483. @param x Horizontal position of first corner. MUST BE WITHIN
  1484. SCREEN BOUNDS.
  1485. @param y Vertical position of first corner. MUST BE WITHIN SCREEN
  1486. BOUNDS.
  1487. @param w Rectangle width in pixels. MUST BE POSITIVE AND NOT
  1488. EXTEND OFF SCREEN.
  1489. @param h Rectangle height in pixels. MUST BE POSITIVE AND NOT
  1490. EXTEND OFF SCREEN.
  1491. @param color 16-bit fill color in '565' RGB format.
  1492. @note This is a new function, no graphics primitives besides rects
  1493. and horizontal/vertical lines are written to best use this yet.
  1494. */
  1495. inline void Adafruit_SPITFT::writeFillRectPreclipped(int16_t x, int16_t y,
  1496. int16_t w, int16_t h,
  1497. uint16_t color) {
  1498. setAddrWindow(x, y, w, h);
  1499. writeColor(color, (uint32_t)w * h);
  1500. }
  1501. // -------------------------------------------------------------------------
  1502. // Ever-so-slightly higher-level graphics operations. Similar to the 'write'
  1503. // functions above, but these contain their own chip-select and SPI
  1504. // transactions as needed (via startWrite(), endWrite()). They're typically
  1505. // used solo -- as graphics primitives in themselves, not invoked by higher-
  1506. // level primitives (which should use the functions above for better
  1507. // performance).
  1508. /*!
  1509. @brief Draw a single pixel to the display at requested coordinates.
  1510. Self-contained and provides its own transaction as needed
  1511. (see writePixel(x,y,color) for a lower-level variant).
  1512. Edge clipping is performed here.
  1513. @param x Horizontal position (0 = left).
  1514. @param y Vertical position (0 = top).
  1515. @param color 16-bit pixel color in '565' RGB format.
  1516. */
  1517. void Adafruit_SPITFT::drawPixel(int16_t x, int16_t y, uint16_t color) {
  1518. // Clip first...
  1519. if ((x >= 0) && (x < _width) && (y >= 0) && (y < _height)) {
  1520. // THEN set up transaction (if needed) and draw...
  1521. startWrite();
  1522. setAddrWindow(x, y, 1, 1);
  1523. SPI_WRITE16(color);
  1524. endWrite();
  1525. }
  1526. }
  1527. /*!
  1528. @brief Draw a filled rectangle to the display. Self-contained and
  1529. provides its own transaction as needed (see writeFillRect() or
  1530. writeFillRectPreclipped() for lower-level variants). Edge
  1531. clipping and rejection is performed here.
  1532. @param x Horizontal position of first corner.
  1533. @param y Vertical position of first corner.
  1534. @param w Rectangle width in pixels (positive = right of first
  1535. corner, negative = left of first corner).
  1536. @param h Rectangle height in pixels (positive = below first
  1537. corner, negative = above first corner).
  1538. @param color 16-bit fill color in '565' RGB format.
  1539. @note This repeats the writeFillRect() function almost in its entirety,
  1540. with the addition of a transaction start/end. It's done this way
  1541. (rather than starting the transaction and calling writeFillRect()
  1542. to handle clipping and so forth) so that the transaction isn't
  1543. performed at all if the rectangle is rejected. It's really not
  1544. that much code.
  1545. */
  1546. void Adafruit_SPITFT::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
  1547. uint16_t color) {
  1548. if (w && h) { // Nonzero width and height?
  1549. if (w < 0) { // If negative width...
  1550. x += w + 1; // Move X to left edge
  1551. w = -w; // Use positive width
  1552. }
  1553. if (x < _width) { // Not off right
  1554. if (h < 0) { // If negative height...
  1555. y += h + 1; // Move Y to top edge
  1556. h = -h; // Use positive height
  1557. }
  1558. if (y < _height) { // Not off bottom
  1559. int16_t x2 = x + w - 1;
  1560. if (x2 >= 0) { // Not off left
  1561. int16_t y2 = y + h - 1;
  1562. if (y2 >= 0) { // Not off top
  1563. // Rectangle partly or fully overlaps screen
  1564. if (x < 0) {
  1565. x = 0;
  1566. w = x2 + 1;
  1567. } // Clip left
  1568. if (y < 0) {
  1569. y = 0;
  1570. h = y2 + 1;
  1571. } // Clip top
  1572. if (x2 >= _width) {
  1573. w = _width - x;
  1574. } // Clip right
  1575. if (y2 >= _height) {
  1576. h = _height - y;
  1577. } // Clip bottom
  1578. startWrite();
  1579. writeFillRectPreclipped(x, y, w, h, color);
  1580. endWrite();
  1581. }
  1582. }
  1583. }
  1584. }
  1585. }
  1586. }
  1587. /*!
  1588. @brief Draw a horizontal line on the display. Self-contained and
  1589. provides its own transaction as needed (see writeFastHLine() for
  1590. a lower-level variant). Edge clipping and rejection is performed
  1591. here.
  1592. @param x Horizontal position of first point.
  1593. @param y Vertical position of first point.
  1594. @param w Line width in pixels (positive = right of first point,
  1595. negative = point of first corner).
  1596. @param color 16-bit line color in '565' RGB format.
  1597. @note This repeats the writeFastHLine() function almost in its
  1598. entirety, with the addition of a transaction start/end. It's
  1599. done this way (rather than starting the transaction and calling
  1600. writeFastHLine() to handle clipping and so forth) so that the
  1601. transaction isn't performed at all if the line is rejected.
  1602. */
  1603. void Adafruit_SPITFT::drawFastHLine(int16_t x, int16_t y, int16_t w,
  1604. uint16_t color) {
  1605. if ((y >= 0) && (y < _height) && w) { // Y on screen, nonzero width
  1606. if (w < 0) { // If negative width...
  1607. x += w + 1; // Move X to left edge
  1608. w = -w; // Use positive width
  1609. }
  1610. if (x < _width) { // Not off right
  1611. int16_t x2 = x + w - 1;
  1612. if (x2 >= 0) { // Not off left
  1613. // Line partly or fully overlaps screen
  1614. if (x < 0) {
  1615. x = 0;
  1616. w = x2 + 1;
  1617. } // Clip left
  1618. if (x2 >= _width) {
  1619. w = _width - x;
  1620. } // Clip right
  1621. startWrite();
  1622. writeFillRectPreclipped(x, y, w, 1, color);
  1623. endWrite();
  1624. }
  1625. }
  1626. }
  1627. }
  1628. /*!
  1629. @brief Draw a vertical line on the display. Self-contained and provides
  1630. its own transaction as needed (see writeFastHLine() for a lower-
  1631. level variant). Edge clipping and rejection is performed here.
  1632. @param x Horizontal position of first point.
  1633. @param y Vertical position of first point.
  1634. @param h Line height in pixels (positive = below first point,
  1635. negative = above first point).
  1636. @param color 16-bit line color in '565' RGB format.
  1637. @note This repeats the writeFastVLine() function almost in its
  1638. entirety, with the addition of a transaction start/end. It's
  1639. done this way (rather than starting the transaction and calling
  1640. writeFastVLine() to handle clipping and so forth) so that the
  1641. transaction isn't performed at all if the line is rejected.
  1642. */
  1643. void Adafruit_SPITFT::drawFastVLine(int16_t x, int16_t y, int16_t h,
  1644. uint16_t color) {
  1645. if ((x >= 0) && (x < _width) && h) { // X on screen, nonzero height
  1646. if (h < 0) { // If negative height...
  1647. y += h + 1; // Move Y to top edge
  1648. h = -h; // Use positive height
  1649. }
  1650. if (y < _height) { // Not off bottom
  1651. int16_t y2 = y + h - 1;
  1652. if (y2 >= 0) { // Not off top
  1653. // Line partly or fully overlaps screen
  1654. if (y < 0) {
  1655. y = 0;
  1656. h = y2 + 1;
  1657. } // Clip top
  1658. if (y2 >= _height) {
  1659. h = _height - y;
  1660. } // Clip bottom
  1661. startWrite();
  1662. writeFillRectPreclipped(x, y, 1, h, color);
  1663. endWrite();
  1664. }
  1665. }
  1666. }
  1667. }
  1668. /*!
  1669. @brief Essentially writePixel() with a transaction around it. I don't
  1670. think this is in use by any of our code anymore (believe it was
  1671. for some older BMP-reading examples), but is kept here in case
  1672. any user code relies on it. Consider it DEPRECATED.
  1673. @param color 16-bit pixel color in '565' RGB format.
  1674. */
  1675. void Adafruit_SPITFT::pushColor(uint16_t color) {
  1676. startWrite();
  1677. SPI_WRITE16(color);
  1678. endWrite();
  1679. }
  1680. /*!
  1681. @brief Draw a 16-bit image (565 RGB) at the specified (x,y) position.
  1682. For 16-bit display devices; no color reduction performed.
  1683. Adapted from https://github.com/PaulStoffregen/ILI9341_t3
  1684. by Marc MERLIN. See examples/pictureEmbed to use this.
  1685. 5/6/2017: function name and arguments have changed for
  1686. compatibility with current GFX library and to avoid naming
  1687. problems in prior implementation. Formerly drawBitmap() with
  1688. arguments in different order. Handles its own transaction and
  1689. edge clipping/rejection.
  1690. @param x Top left corner horizontal coordinate.
  1691. @param y Top left corner vertical coordinate.
  1692. @param pcolors Pointer to 16-bit array of pixel values.
  1693. @param w Width of bitmap in pixels.
  1694. @param h Height of bitmap in pixels.
  1695. */
  1696. void Adafruit_SPITFT::drawRGBBitmap(int16_t x, int16_t y, uint16_t *pcolors,
  1697. int16_t w, int16_t h) {
  1698. int16_t x2, y2; // Lower-right coord
  1699. if ((x >= _width) || // Off-edge right
  1700. (y >= _height) || // " top
  1701. ((x2 = (x + w - 1)) < 0) || // " left
  1702. ((y2 = (y + h - 1)) < 0))
  1703. return; // " bottom
  1704. int16_t bx1 = 0, by1 = 0, // Clipped top-left within bitmap
  1705. saveW = w; // Save original bitmap width value
  1706. if (x < 0) { // Clip left
  1707. w += x;
  1708. bx1 = -x;
  1709. x = 0;
  1710. }
  1711. if (y < 0) { // Clip top
  1712. h += y;
  1713. by1 = -y;
  1714. y = 0;
  1715. }
  1716. if (x2 >= _width)
  1717. w = _width - x; // Clip right
  1718. if (y2 >= _height)
  1719. h = _height - y; // Clip bottom
  1720. pcolors += by1 * saveW + bx1; // Offset bitmap ptr to clipped top-left
  1721. startWrite();
  1722. setAddrWindow(x, y, w, h); // Clipped area
  1723. while (h--) { // For each (clipped) scanline...
  1724. writePixels(pcolors, w); // Push one (clipped) row
  1725. pcolors += saveW; // Advance pointer by one full (unclipped) line
  1726. }
  1727. endWrite();
  1728. }
  1729. // -------------------------------------------------------------------------
  1730. // Miscellaneous class member functions that don't draw anything.
  1731. /*!
  1732. @brief Invert the colors of the display (if supported by hardware).
  1733. Self-contained, no transaction setup required.
  1734. @param i true = inverted display, false = normal display.
  1735. */
  1736. void Adafruit_SPITFT::invertDisplay(bool i) {
  1737. startWrite();
  1738. writeCommand(i ? invertOnCommand : invertOffCommand);
  1739. endWrite();
  1740. }
  1741. /*!
  1742. @brief Given 8-bit red, green and blue values, return a 'packed'
  1743. 16-bit color value in '565' RGB format (5 bits red, 6 bits
  1744. green, 5 bits blue). This is just a mathematical operation,
  1745. no hardware is touched.
  1746. @param red 8-bit red brightnesss (0 = off, 255 = max).
  1747. @param green 8-bit green brightnesss (0 = off, 255 = max).
  1748. @param blue 8-bit blue brightnesss (0 = off, 255 = max).
  1749. @return 'Packed' 16-bit color value (565 format).
  1750. */
  1751. uint16_t Adafruit_SPITFT::color565(uint8_t red, uint8_t green, uint8_t blue) {
  1752. return ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | (blue >> 3);
  1753. }
  1754. /*!
  1755. @brief Adafruit_SPITFT Send Command handles complete sending of commands and
  1756. data
  1757. @param commandByte The Command Byte
  1758. @param dataBytes A pointer to the Data bytes to send
  1759. @param numDataBytes The number of bytes we should send
  1760. */
  1761. void Adafruit_SPITFT::sendCommand(uint8_t commandByte, uint8_t *dataBytes,
  1762. uint8_t numDataBytes) {
  1763. SPI_BEGIN_TRANSACTION();
  1764. if (_cs >= 0)
  1765. SPI_CS_LOW();
  1766. SPI_DC_LOW(); // Command mode
  1767. spiWrite(commandByte); // Send the command byte
  1768. SPI_DC_HIGH();
  1769. for (int i = 0; i < numDataBytes; i++) {
  1770. if ((connection == TFT_PARALLEL) && tft8.wide) {
  1771. SPI_WRITE16(*(uint16_t *)dataBytes);
  1772. dataBytes += 2;
  1773. } else {
  1774. spiWrite(*dataBytes); // Send the data bytes
  1775. dataBytes++;
  1776. }
  1777. }
  1778. if (_cs >= 0)
  1779. SPI_CS_HIGH();
  1780. SPI_END_TRANSACTION();
  1781. }
  1782. /*!
  1783. @brief Adafruit_SPITFT Send Command handles complete sending of commands and
  1784. data
  1785. @param commandByte The Command Byte
  1786. @param dataBytes A pointer to the Data bytes to send
  1787. @param numDataBytes The number of bytes we should send
  1788. */
  1789. void Adafruit_SPITFT::sendCommand(uint8_t commandByte, const uint8_t *dataBytes,
  1790. uint8_t numDataBytes) {
  1791. SPI_BEGIN_TRANSACTION();
  1792. if (_cs >= 0)
  1793. SPI_CS_LOW();
  1794. SPI_DC_LOW(); // Command mode
  1795. spiWrite(commandByte); // Send the command byte
  1796. SPI_DC_HIGH();
  1797. for (int i = 0; i < numDataBytes; i++) {
  1798. if ((connection == TFT_PARALLEL) && tft8.wide) {
  1799. SPI_WRITE16(*(uint16_t *)dataBytes);
  1800. dataBytes += 2;
  1801. } else {
  1802. spiWrite(pgm_read_byte(dataBytes++));
  1803. }
  1804. }
  1805. if (_cs >= 0)
  1806. SPI_CS_HIGH();
  1807. SPI_END_TRANSACTION();
  1808. }
  1809. /*!
  1810. @brief Adafruit_SPITFT sendCommand16 handles complete sending of
  1811. commands and data for 16-bit parallel displays. Currently somewhat
  1812. rigged for the NT35510, which has the odd behavior of wanting
  1813. commands 16-bit, but subsequent data as 8-bit values, despite
  1814. the 16-bit bus (high byte is always 0). Also seems to require
  1815. issuing and incrementing address with each transfer.
  1816. @param commandWord The command word (16 bits)
  1817. @param dataBytes A pointer to the data bytes to send
  1818. @param numDataBytes The number of bytes we should send
  1819. */
  1820. void Adafruit_SPITFT::sendCommand16(uint16_t commandWord,
  1821. const uint8_t *dataBytes,
  1822. uint8_t numDataBytes) {
  1823. SPI_BEGIN_TRANSACTION();
  1824. if (_cs >= 0)
  1825. SPI_CS_LOW();
  1826. if (numDataBytes == 0) {
  1827. SPI_DC_LOW(); // Command mode
  1828. SPI_WRITE16(commandWord); // Send the command word
  1829. SPI_DC_HIGH(); // Data mode
  1830. }
  1831. for (int i = 0; i < numDataBytes; i++) {
  1832. SPI_DC_LOW(); // Command mode
  1833. SPI_WRITE16(commandWord); // Send the command word
  1834. SPI_DC_HIGH(); // Data mode
  1835. commandWord++;
  1836. SPI_WRITE16((uint16_t)pgm_read_byte(dataBytes++));
  1837. }
  1838. if (_cs >= 0)
  1839. SPI_CS_HIGH();
  1840. SPI_END_TRANSACTION();
  1841. }
  1842. /*!
  1843. @brief Read 8 bits of data from display configuration memory (not RAM).
  1844. This is highly undocumented/supported and should be avoided,
  1845. function is only included because some of the examples use it.
  1846. @param commandByte
  1847. The command register to read data from.
  1848. @param index
  1849. The byte index into the command to read from.
  1850. @return Unsigned 8-bit data read from display register.
  1851. */
  1852. /**************************************************************************/
  1853. uint8_t Adafruit_SPITFT::readcommand8(uint8_t commandByte, uint8_t index) {
  1854. uint8_t result;
  1855. startWrite();
  1856. SPI_DC_LOW(); // Command mode
  1857. spiWrite(commandByte);
  1858. SPI_DC_HIGH(); // Data mode
  1859. do {
  1860. result = spiRead();
  1861. } while (index--); // Discard bytes up to index'th
  1862. endWrite();
  1863. return result;
  1864. }
  1865. /*!
  1866. @brief Read 16 bits of data from display register.
  1867. For 16-bit parallel displays only.
  1868. @param addr Command/register to access.
  1869. @return Unsigned 16-bit data.
  1870. */
  1871. uint16_t Adafruit_SPITFT::readcommand16(uint16_t addr) {
  1872. #if defined(USE_FAST_PINIO) // NOT SUPPORTED without USE_FAST_PINIO
  1873. uint16_t result = 0;
  1874. if ((connection == TFT_PARALLEL) && tft8.wide) {
  1875. startWrite();
  1876. SPI_DC_LOW(); // Command mode
  1877. SPI_WRITE16(addr);
  1878. SPI_DC_HIGH(); // Data mode
  1879. TFT_RD_LOW(); // Read line LOW
  1880. #if defined(HAS_PORT_SET_CLR)
  1881. *(volatile uint16_t *)tft8.dirClr = 0xFFFF; // Input state
  1882. result = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  1883. *(volatile uint16_t *)tft8.dirSet = 0xFFFF; // Output state
  1884. #else // !HAS_PORT_SET_CLR
  1885. *(volatile uint16_t *)tft8.portDir = 0x0000; // Input state
  1886. result = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  1887. *(volatile uint16_t *)tft8.portDir = 0xFFFF; // Output state
  1888. #endif // end !HAS_PORT_SET_CLR
  1889. TFT_RD_HIGH(); // Read line HIGH
  1890. endWrite();
  1891. }
  1892. return result;
  1893. #else
  1894. (void)addr; // disable -Wunused-parameter warning
  1895. return 0;
  1896. #endif // end !USE_FAST_PINIO
  1897. }
  1898. // -------------------------------------------------------------------------
  1899. // Lowest-level hardware-interfacing functions. Many of these are inline and
  1900. // compile to different things based on #defines -- typically just a few
  1901. // instructions. Others, not so much, those are not inlined.
  1902. /*!
  1903. @brief Start an SPI transaction if using the hardware SPI interface to
  1904. the display. If using an earlier version of the Arduino platform
  1905. (before the addition of SPI transactions), this instead attempts
  1906. to set up the SPI clock and mode. No action is taken if the
  1907. connection is not hardware SPI-based. This does NOT include a
  1908. chip-select operation -- see startWrite() for a function that
  1909. encapsulated both actions.
  1910. */
  1911. inline void Adafruit_SPITFT::SPI_BEGIN_TRANSACTION(void) {
  1912. if (connection == TFT_HARD_SPI) {
  1913. #if defined(SPI_HAS_TRANSACTION)
  1914. hwspi._spi->beginTransaction(hwspi.settings);
  1915. #else // No transactions, configure SPI manually...
  1916. #if defined(__AVR__) || defined(TEENSYDUINO) || defined(ARDUINO_ARCH_STM32F1)
  1917. hwspi._spi->setClockDivider(SPI_CLOCK_DIV2);
  1918. #elif defined(__arm__)
  1919. hwspi._spi->setClockDivider(11);
  1920. #elif defined(ESP8266) || defined(ESP32)
  1921. hwspi._spi->setFrequency(hwspi._freq);
  1922. #elif defined(RASPI) || defined(ARDUINO_ARCH_STM32F1)
  1923. hwspi._spi->setClock(hwspi._freq);
  1924. #endif
  1925. hwspi._spi->setBitOrder(MSBFIRST);
  1926. hwspi._spi->setDataMode(hwspi._mode);
  1927. #endif // end !SPI_HAS_TRANSACTION
  1928. }
  1929. }
  1930. /*!
  1931. @brief End an SPI transaction if using the hardware SPI interface to
  1932. the display. No action is taken if the connection is not
  1933. hardware SPI-based or if using an earlier version of the Arduino
  1934. platform (before the addition of SPI transactions). This does
  1935. NOT include a chip-deselect operation -- see endWrite() for a
  1936. function that encapsulated both actions.
  1937. */
  1938. inline void Adafruit_SPITFT::SPI_END_TRANSACTION(void) {
  1939. #if defined(SPI_HAS_TRANSACTION)
  1940. if (connection == TFT_HARD_SPI) {
  1941. hwspi._spi->endTransaction();
  1942. }
  1943. #endif
  1944. }
  1945. /*!
  1946. @brief Issue a single 8-bit value to the display. Chip-select,
  1947. transaction and data/command selection must have been
  1948. previously set -- this ONLY issues the byte. This is another of
  1949. those functions in the library with a now-not-accurate name
  1950. that's being maintained for compatibility with outside code.
  1951. This function is used even if display connection is parallel.
  1952. @param b 8-bit value to write.
  1953. */
  1954. void Adafruit_SPITFT::spiWrite(uint8_t b) {
  1955. if (connection == TFT_HARD_SPI) {
  1956. #if defined(__AVR__)
  1957. AVR_WRITESPI(b);
  1958. #elif defined(ESP8266) || defined(ESP32)
  1959. hwspi._spi->write(b);
  1960. #else
  1961. hwspi._spi->transfer(b);
  1962. #endif
  1963. } else if (connection == TFT_SOFT_SPI) {
  1964. for (uint8_t bit = 0; bit < 8; bit++) {
  1965. if (b & 0x80)
  1966. SPI_MOSI_HIGH();
  1967. else
  1968. SPI_MOSI_LOW();
  1969. SPI_SCK_HIGH();
  1970. b <<= 1;
  1971. SPI_SCK_LOW();
  1972. }
  1973. } else { // TFT_PARALLEL
  1974. #if defined(__AVR__)
  1975. *tft8.writePort = b;
  1976. #elif defined(USE_FAST_PINIO)
  1977. if (!tft8.wide)
  1978. *tft8.writePort = b;
  1979. else
  1980. *(volatile uint16_t *)tft8.writePort = b;
  1981. #endif
  1982. TFT_WR_STROBE();
  1983. }
  1984. }
  1985. /*!
  1986. @brief Write a single command byte to the display. Chip-select and
  1987. transaction must have been previously set -- this ONLY sets
  1988. the device to COMMAND mode, issues the byte and then restores
  1989. DATA mode. There is no corresponding explicit writeData()
  1990. function -- just use spiWrite().
  1991. @param cmd 8-bit command to write.
  1992. */
  1993. void Adafruit_SPITFT::writeCommand(uint8_t cmd) {
  1994. SPI_DC_LOW();
  1995. spiWrite(cmd);
  1996. SPI_DC_HIGH();
  1997. }
  1998. /*!
  1999. @brief Read a single 8-bit value from the display. Chip-select and
  2000. transaction must have been previously set -- this ONLY reads
  2001. the byte. This is another of those functions in the library
  2002. with a now-not-accurate name that's being maintained for
  2003. compatibility with outside code. This function is used even if
  2004. display connection is parallel.
  2005. @return Unsigned 8-bit value read (always zero if USE_FAST_PINIO is
  2006. not supported by the MCU architecture).
  2007. */
  2008. uint8_t Adafruit_SPITFT::spiRead(void) {
  2009. uint8_t b = 0;
  2010. uint16_t w = 0;
  2011. if (connection == TFT_HARD_SPI) {
  2012. return hwspi._spi->transfer((uint8_t)0);
  2013. } else if (connection == TFT_SOFT_SPI) {
  2014. if (swspi._miso >= 0) {
  2015. for (uint8_t i = 0; i < 8; i++) {
  2016. SPI_SCK_HIGH();
  2017. b <<= 1;
  2018. if (SPI_MISO_READ())
  2019. b++;
  2020. SPI_SCK_LOW();
  2021. }
  2022. }
  2023. return b;
  2024. } else { // TFT_PARALLEL
  2025. if (tft8._rd >= 0) {
  2026. #if defined(USE_FAST_PINIO)
  2027. TFT_RD_LOW(); // Read line LOW
  2028. #if defined(__AVR__)
  2029. *tft8.portDir = 0x00; // Set port to input state
  2030. w = *tft8.readPort; // Read value from port
  2031. *tft8.portDir = 0xFF; // Restore port to output
  2032. #else // !__AVR__
  2033. if (!tft8.wide) { // 8-bit TFT connection
  2034. #if defined(HAS_PORT_SET_CLR)
  2035. *tft8.dirClr = 0xFF; // Set port to input state
  2036. w = *tft8.readPort; // Read value from port
  2037. *tft8.dirSet = 0xFF; // Restore port to output
  2038. #else // !HAS_PORT_SET_CLR
  2039. *tft8.portDir = 0x00; // Set port to input state
  2040. w = *tft8.readPort; // Read value from port
  2041. *tft8.portDir = 0xFF; // Restore port to output
  2042. #endif // end HAS_PORT_SET_CLR
  2043. } else { // 16-bit TFT connection
  2044. #if defined(HAS_PORT_SET_CLR)
  2045. *(volatile uint16_t *)tft8.dirClr = 0xFFFF; // Input state
  2046. w = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  2047. *(volatile uint16_t *)tft8.dirSet = 0xFFFF; // Output state
  2048. #else // !HAS_PORT_SET_CLR
  2049. *(volatile uint16_t *)tft8.portDir = 0x0000; // Input state
  2050. w = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  2051. *(volatile uint16_t *)tft8.portDir = 0xFFFF; // Output state
  2052. #endif // end !HAS_PORT_SET_CLR
  2053. }
  2054. TFT_RD_HIGH(); // Read line HIGH
  2055. #endif // end !__AVR__
  2056. #else // !USE_FAST_PINIO
  2057. w = 0; // Parallel TFT is NOT SUPPORTED without USE_FAST_PINIO
  2058. #endif // end !USE_FAST_PINIO
  2059. }
  2060. return w;
  2061. }
  2062. }
  2063. /*!
  2064. @brief Issue a single 16-bit value to the display. Chip-select,
  2065. transaction and data/command selection must have been
  2066. previously set -- this ONLY issues the word.
  2067. Thus operates ONLY on 'wide' (16-bit) parallel displays!
  2068. @param w 16-bit value to write.
  2069. */
  2070. void Adafruit_SPITFT::write16(uint16_t w) {
  2071. if (connection == TFT_PARALLEL) {
  2072. #if defined(USE_FAST_PINIO)
  2073. if (tft8.wide)
  2074. *(volatile uint16_t *)tft8.writePort = w;
  2075. #else
  2076. (void)w; // disable -Wunused-parameter warning
  2077. #endif
  2078. TFT_WR_STROBE();
  2079. }
  2080. }
  2081. /*!
  2082. @brief Write a single command word to the display. Chip-select and
  2083. transaction must have been previously set -- this ONLY sets
  2084. the device to COMMAND mode, issues the byte and then restores
  2085. DATA mode. This operates ONLY on 'wide' (16-bit) parallel
  2086. displays!
  2087. @param cmd 16-bit command to write.
  2088. */
  2089. void Adafruit_SPITFT::writeCommand16(uint16_t cmd) {
  2090. SPI_DC_LOW();
  2091. write16(cmd);
  2092. SPI_DC_HIGH();
  2093. }
  2094. /*!
  2095. @brief Read a single 16-bit value from the display. Chip-select and
  2096. transaction must have been previously set -- this ONLY reads
  2097. the byte. This operates ONLY on 'wide' (16-bit) parallel
  2098. displays!
  2099. @return Unsigned 16-bit value read (always zero if USE_FAST_PINIO is
  2100. not supported by the MCU architecture).
  2101. */
  2102. uint16_t Adafruit_SPITFT::read16(void) {
  2103. uint16_t w = 0;
  2104. if (connection == TFT_PARALLEL) {
  2105. if (tft8._rd >= 0) {
  2106. #if defined(USE_FAST_PINIO)
  2107. TFT_RD_LOW(); // Read line LOW
  2108. if (tft8.wide) { // 16-bit TFT connection
  2109. #if defined(HAS_PORT_SET_CLR)
  2110. *(volatile uint16_t *)tft8.dirClr = 0xFFFF; // Input state
  2111. w = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  2112. *(volatile uint16_t *)tft8.dirSet = 0xFFFF; // Output state
  2113. #else // !HAS_PORT_SET_CLR
  2114. *(volatile uint16_t *)tft8.portDir = 0x0000; // Input state
  2115. w = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  2116. *(volatile uint16_t *)tft8.portDir = 0xFFFF; // Output state
  2117. #endif // end !HAS_PORT_SET_CLR
  2118. }
  2119. TFT_RD_HIGH(); // Read line HIGH
  2120. #else // !USE_FAST_PINIO
  2121. w = 0; // Parallel TFT is NOT SUPPORTED without USE_FAST_PINIO
  2122. #endif // end !USE_FAST_PINIO
  2123. }
  2124. }
  2125. return w;
  2126. }
  2127. /*!
  2128. @brief Set the software (bitbang) SPI MOSI line HIGH.
  2129. */
  2130. inline void Adafruit_SPITFT::SPI_MOSI_HIGH(void) {
  2131. #if defined(USE_FAST_PINIO)
  2132. #if defined(HAS_PORT_SET_CLR)
  2133. #if defined(KINETISK)
  2134. *swspi.mosiPortSet = 1;
  2135. #else // !KINETISK
  2136. *swspi.mosiPortSet = swspi.mosiPinMask;
  2137. #endif
  2138. #else // !HAS_PORT_SET_CLR
  2139. *swspi.mosiPort |= swspi.mosiPinMaskSet;
  2140. #endif // end !HAS_PORT_SET_CLR
  2141. #else // !USE_FAST_PINIO
  2142. digitalWrite(swspi._mosi, HIGH);
  2143. #if defined(ESP32)
  2144. for (volatile uint8_t i = 0; i < 1; i++)
  2145. ;
  2146. #endif // end ESP32
  2147. #endif // end !USE_FAST_PINIO
  2148. }
  2149. /*!
  2150. @brief Set the software (bitbang) SPI MOSI line LOW.
  2151. */
  2152. inline void Adafruit_SPITFT::SPI_MOSI_LOW(void) {
  2153. #if defined(USE_FAST_PINIO)
  2154. #if defined(HAS_PORT_SET_CLR)
  2155. #if defined(KINETISK)
  2156. *swspi.mosiPortClr = 1;
  2157. #else // !KINETISK
  2158. *swspi.mosiPortClr = swspi.mosiPinMask;
  2159. #endif
  2160. #else // !HAS_PORT_SET_CLR
  2161. *swspi.mosiPort &= swspi.mosiPinMaskClr;
  2162. #endif // end !HAS_PORT_SET_CLR
  2163. #else // !USE_FAST_PINIO
  2164. digitalWrite(swspi._mosi, LOW);
  2165. #if defined(ESP32)
  2166. for (volatile uint8_t i = 0; i < 1; i++)
  2167. ;
  2168. #endif // end ESP32
  2169. #endif // end !USE_FAST_PINIO
  2170. }
  2171. /*!
  2172. @brief Set the software (bitbang) SPI SCK line HIGH.
  2173. */
  2174. inline void Adafruit_SPITFT::SPI_SCK_HIGH(void) {
  2175. #if defined(USE_FAST_PINIO)
  2176. #if defined(HAS_PORT_SET_CLR)
  2177. #if defined(KINETISK)
  2178. *swspi.sckPortSet = 1;
  2179. #else // !KINETISK
  2180. *swspi.sckPortSet = swspi.sckPinMask;
  2181. #if defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
  2182. for (volatile uint8_t i = 0; i < 1; i++)
  2183. ;
  2184. #endif
  2185. #endif
  2186. #else // !HAS_PORT_SET_CLR
  2187. *swspi.sckPort |= swspi.sckPinMaskSet;
  2188. #endif // end !HAS_PORT_SET_CLR
  2189. #else // !USE_FAST_PINIO
  2190. digitalWrite(swspi._sck, HIGH);
  2191. #if defined(ESP32)
  2192. for (volatile uint8_t i = 0; i < 1; i++)
  2193. ;
  2194. #endif // end ESP32
  2195. #endif // end !USE_FAST_PINIO
  2196. }
  2197. /*!
  2198. @brief Set the software (bitbang) SPI SCK line LOW.
  2199. */
  2200. inline void Adafruit_SPITFT::SPI_SCK_LOW(void) {
  2201. #if defined(USE_FAST_PINIO)
  2202. #if defined(HAS_PORT_SET_CLR)
  2203. #if defined(KINETISK)
  2204. *swspi.sckPortClr = 1;
  2205. #else // !KINETISK
  2206. *swspi.sckPortClr = swspi.sckPinMask;
  2207. #if defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
  2208. for (volatile uint8_t i = 0; i < 1; i++)
  2209. ;
  2210. #endif
  2211. #endif
  2212. #else // !HAS_PORT_SET_CLR
  2213. *swspi.sckPort &= swspi.sckPinMaskClr;
  2214. #endif // end !HAS_PORT_SET_CLR
  2215. #else // !USE_FAST_PINIO
  2216. digitalWrite(swspi._sck, LOW);
  2217. #if defined(ESP32)
  2218. for (volatile uint8_t i = 0; i < 1; i++)
  2219. ;
  2220. #endif // end ESP32
  2221. #endif // end !USE_FAST_PINIO
  2222. }
  2223. /*!
  2224. @brief Read the state of the software (bitbang) SPI MISO line.
  2225. @return true if HIGH, false if LOW.
  2226. */
  2227. inline bool Adafruit_SPITFT::SPI_MISO_READ(void) {
  2228. #if defined(USE_FAST_PINIO)
  2229. #if defined(KINETISK)
  2230. return *swspi.misoPort;
  2231. #else // !KINETISK
  2232. return *swspi.misoPort & swspi.misoPinMask;
  2233. #endif // end !KINETISK
  2234. #else // !USE_FAST_PINIO
  2235. return digitalRead(swspi._miso);
  2236. #endif // end !USE_FAST_PINIO
  2237. }
  2238. /*!
  2239. @brief Issue a single 16-bit value to the display. Chip-select,
  2240. transaction and data/command selection must have been
  2241. previously set -- this ONLY issues the word. Despite the name,
  2242. this function is used even if display connection is parallel;
  2243. name was maintaned for backward compatibility. Naming is also
  2244. not consistent with the 8-bit version, spiWrite(). Sorry about
  2245. that. Again, staying compatible with outside code.
  2246. @param w 16-bit value to write.
  2247. */
  2248. void Adafruit_SPITFT::SPI_WRITE16(uint16_t w) {
  2249. if (connection == TFT_HARD_SPI) {
  2250. #if defined(__AVR__)
  2251. AVR_WRITESPI(w >> 8);
  2252. AVR_WRITESPI(w);
  2253. #elif defined(ESP8266) || defined(ESP32)
  2254. hwspi._spi->write16(w);
  2255. #else
  2256. hwspi._spi->transfer(w >> 8);
  2257. hwspi._spi->transfer(w);
  2258. #endif
  2259. } else if (connection == TFT_SOFT_SPI) {
  2260. for (uint8_t bit = 0; bit < 16; bit++) {
  2261. if (w & 0x8000)
  2262. SPI_MOSI_HIGH();
  2263. else
  2264. SPI_MOSI_LOW();
  2265. SPI_SCK_HIGH();
  2266. SPI_SCK_LOW();
  2267. w <<= 1;
  2268. }
  2269. } else { // TFT_PARALLEL
  2270. #if defined(__AVR__)
  2271. *tft8.writePort = w >> 8;
  2272. TFT_WR_STROBE();
  2273. *tft8.writePort = w;
  2274. #elif defined(USE_FAST_PINIO)
  2275. if (!tft8.wide) {
  2276. *tft8.writePort = w >> 8;
  2277. TFT_WR_STROBE();
  2278. *tft8.writePort = w;
  2279. } else {
  2280. *(volatile uint16_t *)tft8.writePort = w;
  2281. }
  2282. #endif
  2283. TFT_WR_STROBE();
  2284. }
  2285. }
  2286. /*!
  2287. @brief Issue a single 32-bit value to the display. Chip-select,
  2288. transaction and data/command selection must have been
  2289. previously set -- this ONLY issues the longword. Despite the
  2290. name, this function is used even if display connection is
  2291. parallel; name was maintaned for backward compatibility. Naming
  2292. is also not consistent with the 8-bit version, spiWrite().
  2293. Sorry about that. Again, staying compatible with outside code.
  2294. @param l 32-bit value to write.
  2295. */
  2296. void Adafruit_SPITFT::SPI_WRITE32(uint32_t l) {
  2297. if (connection == TFT_HARD_SPI) {
  2298. #if defined(__AVR__)
  2299. AVR_WRITESPI(l >> 24);
  2300. AVR_WRITESPI(l >> 16);
  2301. AVR_WRITESPI(l >> 8);
  2302. AVR_WRITESPI(l);
  2303. #elif defined(ESP8266) || defined(ESP32)
  2304. hwspi._spi->write32(l);
  2305. #else
  2306. hwspi._spi->transfer(l >> 24);
  2307. hwspi._spi->transfer(l >> 16);
  2308. hwspi._spi->transfer(l >> 8);
  2309. hwspi._spi->transfer(l);
  2310. #endif
  2311. } else if (connection == TFT_SOFT_SPI) {
  2312. for (uint8_t bit = 0; bit < 32; bit++) {
  2313. if (l & 0x80000000)
  2314. SPI_MOSI_HIGH();
  2315. else
  2316. SPI_MOSI_LOW();
  2317. SPI_SCK_HIGH();
  2318. SPI_SCK_LOW();
  2319. l <<= 1;
  2320. }
  2321. } else { // TFT_PARALLEL
  2322. #if defined(__AVR__)
  2323. *tft8.writePort = l >> 24;
  2324. TFT_WR_STROBE();
  2325. *tft8.writePort = l >> 16;
  2326. TFT_WR_STROBE();
  2327. *tft8.writePort = l >> 8;
  2328. TFT_WR_STROBE();
  2329. *tft8.writePort = l;
  2330. #elif defined(USE_FAST_PINIO)
  2331. if (!tft8.wide) {
  2332. *tft8.writePort = l >> 24;
  2333. TFT_WR_STROBE();
  2334. *tft8.writePort = l >> 16;
  2335. TFT_WR_STROBE();
  2336. *tft8.writePort = l >> 8;
  2337. TFT_WR_STROBE();
  2338. *tft8.writePort = l;
  2339. } else {
  2340. *(volatile uint16_t *)tft8.writePort = l >> 16;
  2341. TFT_WR_STROBE();
  2342. *(volatile uint16_t *)tft8.writePort = l;
  2343. }
  2344. #endif
  2345. TFT_WR_STROBE();
  2346. }
  2347. }
  2348. /*!
  2349. @brief Set the WR line LOW, then HIGH. Used for parallel-connected
  2350. interfaces when writing data.
  2351. */
  2352. inline void Adafruit_SPITFT::TFT_WR_STROBE(void) {
  2353. #if defined(USE_FAST_PINIO)
  2354. #if defined(HAS_PORT_SET_CLR)
  2355. #if defined(KINETISK)
  2356. *tft8.wrPortClr = 1;
  2357. *tft8.wrPortSet = 1;
  2358. #else // !KINETISK
  2359. *tft8.wrPortClr = tft8.wrPinMask;
  2360. *tft8.wrPortSet = tft8.wrPinMask;
  2361. #endif // end !KINETISK
  2362. #else // !HAS_PORT_SET_CLR
  2363. *tft8.wrPort &= tft8.wrPinMaskClr;
  2364. *tft8.wrPort |= tft8.wrPinMaskSet;
  2365. #endif // end !HAS_PORT_SET_CLR
  2366. #else // !USE_FAST_PINIO
  2367. digitalWrite(tft8._wr, LOW);
  2368. digitalWrite(tft8._wr, HIGH);
  2369. #endif // end !USE_FAST_PINIO
  2370. }
  2371. /*!
  2372. @brief Set the RD line HIGH. Used for parallel-connected interfaces
  2373. when reading data.
  2374. */
  2375. inline void Adafruit_SPITFT::TFT_RD_HIGH(void) {
  2376. #if defined(USE_FAST_PINIO)
  2377. #if defined(HAS_PORT_SET_CLR)
  2378. *tft8.rdPortSet = tft8.rdPinMask;
  2379. #else // !HAS_PORT_SET_CLR
  2380. *tft8.rdPort |= tft8.rdPinMaskSet;
  2381. #endif // end !HAS_PORT_SET_CLR
  2382. #else // !USE_FAST_PINIO
  2383. digitalWrite(tft8._rd, HIGH);
  2384. #endif // end !USE_FAST_PINIO
  2385. }
  2386. /*!
  2387. @brief Set the RD line LOW. Used for parallel-connected interfaces
  2388. when reading data.
  2389. */
  2390. inline void Adafruit_SPITFT::TFT_RD_LOW(void) {
  2391. #if defined(USE_FAST_PINIO)
  2392. #if defined(HAS_PORT_SET_CLR)
  2393. *tft8.rdPortClr = tft8.rdPinMask;
  2394. #else // !HAS_PORT_SET_CLR
  2395. *tft8.rdPort &= tft8.rdPinMaskClr;
  2396. #endif // end !HAS_PORT_SET_CLR
  2397. #else // !USE_FAST_PINIO
  2398. digitalWrite(tft8._rd, LOW);
  2399. #endif // end !USE_FAST_PINIO
  2400. }
  2401. #endif // end __AVR_ATtiny85__