Adafruit_SPITFT.cpp 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  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. #endif
  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. ) {
  560. hwspi._spi->begin();
  561. }
  562. } else if (connection == TFT_SOFT_SPI) {
  563. pinMode(swspi._mosi, OUTPUT);
  564. digitalWrite(swspi._mosi, LOW);
  565. pinMode(swspi._sck, OUTPUT);
  566. digitalWrite(swspi._sck, LOW);
  567. if (swspi._miso >= 0) {
  568. pinMode(swspi._miso, INPUT);
  569. }
  570. } else { // TFT_PARALLEL
  571. // Initialize data pins. We were only passed d0, so scan
  572. // the pin description list looking for the other pins.
  573. // They'll be on the same PORT, and within the next 7 (or 15) bits
  574. // (because we need to write to a contiguous PORT byte or word).
  575. #if defined(__AVR__)
  576. // PORT registers are 8 bits wide, so just need a register match...
  577. for (uint8_t i = 0; i < NUM_DIGITAL_PINS; i++) {
  578. if ((PORTreg_t)portOutputRegister(digitalPinToPort(i)) ==
  579. tft8.writePort) {
  580. pinMode(i, OUTPUT);
  581. digitalWrite(i, LOW);
  582. }
  583. }
  584. #elif defined(USE_FAST_PINIO)
  585. #if defined(CORE_TEENSY)
  586. if (!tft8.wide) {
  587. *tft8.dirSet = 0xFF; // Set port to output
  588. *tft8.writePort = 0x00; // Write all 0s
  589. } else {
  590. *(volatile uint16_t *)tft8.dirSet = 0xFFFF;
  591. *(volatile uint16_t *)tft8.writePort = 0x0000;
  592. }
  593. #else // !CORE_TEENSY
  594. uint8_t portNum = g_APinDescription[tft8._d0].ulPort, // d0 PORT #
  595. dBit = g_APinDescription[tft8._d0].ulPin, // d0 bit in PORT
  596. lastBit = dBit + (tft8.wide ? 15 : 7);
  597. for (uint8_t i = 0; i < PINS_COUNT; i++) {
  598. if ((g_APinDescription[i].ulPort == portNum) &&
  599. (g_APinDescription[i].ulPin >= dBit) &&
  600. (g_APinDescription[i].ulPin <= (uint32_t)lastBit)) {
  601. pinMode(i, OUTPUT);
  602. digitalWrite(i, LOW);
  603. }
  604. }
  605. #endif // end !CORE_TEENSY
  606. #endif
  607. pinMode(tft8._wr, OUTPUT);
  608. digitalWrite(tft8._wr, HIGH);
  609. if (tft8._rd >= 0) {
  610. pinMode(tft8._rd, OUTPUT);
  611. digitalWrite(tft8._rd, HIGH);
  612. }
  613. }
  614. if (_rst >= 0) {
  615. // Toggle _rst low to reset
  616. pinMode(_rst, OUTPUT);
  617. digitalWrite(_rst, HIGH);
  618. delay(100);
  619. digitalWrite(_rst, LOW);
  620. delay(100);
  621. digitalWrite(_rst, HIGH);
  622. delay(200);
  623. }
  624. #if defined(USE_SPI_DMA) && (defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO))
  625. if (((connection == TFT_HARD_SPI) || (connection == TFT_PARALLEL)) &&
  626. (dma.allocate() == DMA_STATUS_OK)) { // Allocate channel
  627. // The DMA library needs to alloc at least one valid descriptor,
  628. // so we do that here. It's not used in the usual sense though,
  629. // just before a transfer we copy descriptor[0] to this address.
  630. if (dptr = dma.addDescriptor(NULL, NULL, 42, DMA_BEAT_SIZE_BYTE, false,
  631. false)) {
  632. // Alloc 2 scanlines worth of pixels on display's major axis,
  633. // whichever that is, rounding each up to 2-pixel boundary.
  634. int major = (WIDTH > HEIGHT) ? WIDTH : HEIGHT;
  635. major += (major & 1); // -> next 2-pixel bound, if needed.
  636. maxFillLen = major * 2; // 2 scanlines
  637. // Note to future self: if you decide to make the pixel buffer
  638. // much larger, remember that DMA transfer descriptors can't
  639. // exceed 65,535 bytes (not 65,536), meaning 32,767 pixels max.
  640. // Not that we have that kind of RAM to throw around right now.
  641. if ((pixelBuf[0] = (uint16_t *)malloc(maxFillLen * sizeof(uint16_t)))) {
  642. // Alloc OK. Get pointer to start of second scanline.
  643. pixelBuf[1] = &pixelBuf[0][major];
  644. // Determine number of DMA descriptors needed to cover
  645. // entire screen when entire 2-line pixelBuf is used
  646. // (round up for fractional last descriptor).
  647. int numDescriptors = (WIDTH * HEIGHT + (maxFillLen - 1)) / maxFillLen;
  648. // DMA descriptors MUST be 128-bit (16 byte) aligned.
  649. // memalign() is considered obsolete but it's replacements
  650. // (aligned_alloc() or posix_memalign()) are not currently
  651. // available in the version of ARM GCC in use, but this
  652. // is, so here we are.
  653. if ((descriptor = (DmacDescriptor *)memalign(
  654. 16, numDescriptors * sizeof(DmacDescriptor)))) {
  655. int dmac_id;
  656. volatile uint32_t *data_reg;
  657. if (connection == TFT_HARD_SPI) {
  658. // THIS IS AN AFFRONT TO NATURE, but I don't know
  659. // any "clean" way to get the sercom number from the
  660. // the SPIClass pointer (e.g. &SPI or &SPI1), which
  661. // is all we have to work with. SPIClass does contain
  662. // a SERCOM pointer but it is a PRIVATE member!
  663. // Doing an UNSPEAKABLY HORRIBLE THING here, directly
  664. // accessing the first 32-bit value in the SPIClass
  665. // structure, knowing that's (currently) where the
  666. // SERCOM pointer lives, but this ENTIRELY DEPENDS on
  667. // that structure not changing nor the compiler
  668. // rearranging things. Oh the humanity!
  669. if (*(SERCOM **)hwspi._spi == &sercom0) {
  670. dmac_id = SERCOM0_DMAC_ID_TX;
  671. data_reg = &SERCOM0->SPI.DATA.reg;
  672. #if defined SERCOM1
  673. } else if (*(SERCOM **)hwspi._spi == &sercom1) {
  674. dmac_id = SERCOM1_DMAC_ID_TX;
  675. data_reg = &SERCOM1->SPI.DATA.reg;
  676. #endif
  677. #if defined SERCOM2
  678. } else if (*(SERCOM **)hwspi._spi == &sercom2) {
  679. dmac_id = SERCOM2_DMAC_ID_TX;
  680. data_reg = &SERCOM2->SPI.DATA.reg;
  681. #endif
  682. #if defined SERCOM3
  683. } else if (*(SERCOM **)hwspi._spi == &sercom3) {
  684. dmac_id = SERCOM3_DMAC_ID_TX;
  685. data_reg = &SERCOM3->SPI.DATA.reg;
  686. #endif
  687. #if defined SERCOM4
  688. } else if (*(SERCOM **)hwspi._spi == &sercom4) {
  689. dmac_id = SERCOM4_DMAC_ID_TX;
  690. data_reg = &SERCOM4->SPI.DATA.reg;
  691. #endif
  692. #if defined SERCOM5
  693. } else if (*(SERCOM **)hwspi._spi == &sercom5) {
  694. dmac_id = SERCOM5_DMAC_ID_TX;
  695. data_reg = &SERCOM5->SPI.DATA.reg;
  696. #endif
  697. #if defined SERCOM6
  698. } else if (*(SERCOM **)hwspi._spi == &sercom6) {
  699. dmac_id = SERCOM6_DMAC_ID_TX;
  700. data_reg = &SERCOM6->SPI.DATA.reg;
  701. #endif
  702. #if defined SERCOM7
  703. } else if (*(SERCOM **)hwspi._spi == &sercom7) {
  704. dmac_id = SERCOM7_DMAC_ID_TX;
  705. data_reg = &SERCOM7->SPI.DATA.reg;
  706. #endif
  707. }
  708. dma.setPriority(DMA_PRIORITY_3);
  709. dma.setTrigger(dmac_id);
  710. dma.setAction(DMA_TRIGGER_ACTON_BEAT);
  711. // Initialize descriptor list.
  712. for (int d = 0; d < numDescriptors; d++) {
  713. // No need to set SRCADDR, DESCADDR or BTCNT --
  714. // those are done in the pixel-writing functions.
  715. descriptor[d].BTCTRL.bit.VALID = true;
  716. descriptor[d].BTCTRL.bit.EVOSEL = DMA_EVENT_OUTPUT_DISABLE;
  717. descriptor[d].BTCTRL.bit.BLOCKACT = DMA_BLOCK_ACTION_NOACT;
  718. descriptor[d].BTCTRL.bit.BEATSIZE = DMA_BEAT_SIZE_BYTE;
  719. descriptor[d].BTCTRL.bit.DSTINC = 0;
  720. descriptor[d].BTCTRL.bit.STEPSEL = DMA_STEPSEL_SRC;
  721. descriptor[d].BTCTRL.bit.STEPSIZE =
  722. DMA_ADDRESS_INCREMENT_STEP_SIZE_1;
  723. descriptor[d].DSTADDR.reg = (uint32_t)data_reg;
  724. }
  725. } else { // Parallel connection
  726. #if defined(__SAMD51__)
  727. int dmaChannel = dma.getChannel();
  728. // Enable event output, use EVOSEL output
  729. DMAC->Channel[dmaChannel].CHEVCTRL.bit.EVOE = 1;
  730. DMAC->Channel[dmaChannel].CHEVCTRL.bit.EVOMODE = 0;
  731. // CONFIGURE TIMER/COUNTER (for write strobe)
  732. Tc *timer = tcList[tcNum].tc; // -> Timer struct
  733. int id = tcList[tcNum].gclk; // Timer GCLK ID
  734. GCLK_PCHCTRL_Type pchctrl;
  735. // Set up timer clock source from GCLK
  736. GCLK->PCHCTRL[id].bit.CHEN = 0; // Stop timer
  737. while (GCLK->PCHCTRL[id].bit.CHEN)
  738. ; // Wait for it
  739. pchctrl.bit.GEN = GCLK_PCHCTRL_GEN_GCLK0_Val;
  740. pchctrl.bit.CHEN = 1; // Enable
  741. GCLK->PCHCTRL[id].reg = pchctrl.reg;
  742. while (!GCLK->PCHCTRL[id].bit.CHEN)
  743. ; // Wait for it
  744. // Disable timer/counter before configuring it
  745. timer->COUNT8.CTRLA.bit.ENABLE = 0;
  746. while (timer->COUNT8.SYNCBUSY.bit.STATUS)
  747. ;
  748. timer->COUNT8.WAVE.bit.WAVEGEN = 2; // NPWM
  749. timer->COUNT8.CTRLA.bit.MODE = 1; // 8-bit
  750. timer->COUNT8.CTRLA.bit.PRESCALER = 0; // 1:1
  751. while (timer->COUNT8.SYNCBUSY.bit.STATUS)
  752. ;
  753. timer->COUNT8.CTRLBCLR.bit.DIR = 1; // Count UP
  754. while (timer->COUNT8.SYNCBUSY.bit.CTRLB)
  755. ;
  756. timer->COUNT8.CTRLBSET.bit.ONESHOT = 1; // One-shot
  757. while (timer->COUNT8.SYNCBUSY.bit.CTRLB)
  758. ;
  759. timer->COUNT8.PER.reg = 6; // PWM top
  760. while (timer->COUNT8.SYNCBUSY.bit.PER)
  761. ;
  762. timer->COUNT8.CC[0].reg = 2; // Compare
  763. while (timer->COUNT8.SYNCBUSY.bit.CC0)
  764. ;
  765. // Enable async input events,
  766. // event action = restart.
  767. timer->COUNT8.EVCTRL.bit.TCEI = 1;
  768. timer->COUNT8.EVCTRL.bit.EVACT = 1;
  769. // Enable timer
  770. timer->COUNT8.CTRLA.reg |= TC_CTRLA_ENABLE;
  771. while (timer->COUNT8.SYNCBUSY.bit.STATUS)
  772. ;
  773. #if (wrPeripheral == PIO_CCL)
  774. // CONFIGURE CCL (inverts timer/counter output)
  775. MCLK->APBCMASK.bit.CCL_ = 1; // Enable CCL clock
  776. CCL->CTRL.bit.ENABLE = 0; // Disable to config
  777. CCL->CTRL.bit.SWRST = 1; // Reset CCL registers
  778. CCL->LUTCTRL[tcNum].bit.ENABLE = 0; // Disable LUT
  779. CCL->LUTCTRL[tcNum].bit.FILTSEL = 0; // No filter
  780. CCL->LUTCTRL[tcNum].bit.INSEL0 = 6; // TC input
  781. CCL->LUTCTRL[tcNum].bit.INSEL1 = 0; // MASK
  782. CCL->LUTCTRL[tcNum].bit.INSEL2 = 0; // MASK
  783. CCL->LUTCTRL[tcNum].bit.TRUTH = 1; // Invert in 0
  784. CCL->LUTCTRL[tcNum].bit.ENABLE = 1; // Enable LUT
  785. CCL->CTRL.bit.ENABLE = 1; // Enable CCL
  786. #endif
  787. // CONFIGURE EVENT SYSTEM
  788. // Set up event system clock source from GCLK...
  789. // Disable EVSYS, wait for disable
  790. GCLK->PCHCTRL[EVSYS_GCLK_ID_0].bit.CHEN = 0;
  791. while (GCLK->PCHCTRL[EVSYS_GCLK_ID_0].bit.CHEN)
  792. ;
  793. pchctrl.bit.GEN = GCLK_PCHCTRL_GEN_GCLK0_Val;
  794. pchctrl.bit.CHEN = 1; // Re-enable
  795. GCLK->PCHCTRL[EVSYS_GCLK_ID_0].reg = pchctrl.reg;
  796. // Wait for it, then enable EVSYS clock
  797. while (!GCLK->PCHCTRL[EVSYS_GCLK_ID_0].bit.CHEN)
  798. ;
  799. MCLK->APBBMASK.bit.EVSYS_ = 1;
  800. // Connect Timer EVU to ch 0
  801. EVSYS->USER[tcList[tcNum].evu].reg = 1;
  802. // Datasheet recommends single write operation;
  803. // reg instead of bit. Also datasheet: PATH bits
  804. // must be zero when using async!
  805. EVSYS_CHANNEL_Type ev;
  806. ev.reg = 0;
  807. ev.bit.PATH = 2; // Asynchronous
  808. ev.bit.EVGEN = 0x22 + dmaChannel; // DMA channel 0+
  809. EVSYS->Channel[0].CHANNEL.reg = ev.reg;
  810. // Initialize descriptor list.
  811. for (int d = 0; d < numDescriptors; d++) {
  812. // No need to set SRCADDR, DESCADDR or BTCNT --
  813. // those are done in the pixel-writing functions.
  814. descriptor[d].BTCTRL.bit.VALID = true;
  815. // Event strobe on beat xfer:
  816. descriptor[d].BTCTRL.bit.EVOSEL = 0x3;
  817. descriptor[d].BTCTRL.bit.BLOCKACT = DMA_BLOCK_ACTION_NOACT;
  818. descriptor[d].BTCTRL.bit.BEATSIZE =
  819. tft8.wide ? DMA_BEAT_SIZE_HWORD : DMA_BEAT_SIZE_BYTE;
  820. descriptor[d].BTCTRL.bit.SRCINC = 1;
  821. descriptor[d].BTCTRL.bit.DSTINC = 0;
  822. descriptor[d].BTCTRL.bit.STEPSEL = DMA_STEPSEL_SRC;
  823. descriptor[d].BTCTRL.bit.STEPSIZE =
  824. DMA_ADDRESS_INCREMENT_STEP_SIZE_1;
  825. descriptor[d].DSTADDR.reg = (uint32_t)tft8.writePort;
  826. }
  827. #endif // __SAMD51
  828. } // end parallel-specific DMA setup
  829. lastFillColor = 0x0000;
  830. lastFillLen = 0;
  831. dma.setCallback(dma_callback);
  832. return; // Success!
  833. // else clean up any partial allocation...
  834. } // end descriptor memalign()
  835. free(pixelBuf[0]);
  836. pixelBuf[0] = pixelBuf[1] = NULL;
  837. } // end pixelBuf malloc()
  838. // Don't currently have a descriptor delete function in
  839. // ZeroDMA lib, but if we did, it would be called here.
  840. } // end addDescriptor()
  841. dma.free(); // Deallocate DMA channel
  842. }
  843. #endif // end USE_SPI_DMA
  844. }
  845. /*!
  846. @brief Allow changing the SPI clock speed after initialization
  847. @param freq Desired frequency of SPI clock, may not be the
  848. end frequency you get based on what the chip can do!
  849. */
  850. void Adafruit_SPITFT::setSPISpeed(uint32_t freq) {
  851. #if defined(SPI_HAS_TRANSACTION)
  852. hwspi.settings = SPISettings(freq, MSBFIRST, hwspi._mode);
  853. #else
  854. hwspi._freq = freq; // Save freq value for later
  855. #endif
  856. }
  857. /*!
  858. @brief Call before issuing command(s) or data to display. Performs
  859. chip-select (if required) and starts an SPI transaction (if
  860. using hardware SPI and transactions are supported). Required
  861. for all display types; not an SPI-specific function.
  862. */
  863. void Adafruit_SPITFT::startWrite(void) {
  864. SPI_BEGIN_TRANSACTION();
  865. if (_cs >= 0)
  866. SPI_CS_LOW();
  867. }
  868. /*!
  869. @brief Call after issuing command(s) or data to display. Performs
  870. chip-deselect (if required) and ends an SPI transaction (if
  871. using hardware SPI and transactions are supported). Required
  872. for all display types; not an SPI-specific function.
  873. */
  874. void Adafruit_SPITFT::endWrite(void) {
  875. if (_cs >= 0)
  876. SPI_CS_HIGH();
  877. SPI_END_TRANSACTION();
  878. }
  879. // -------------------------------------------------------------------------
  880. // Lower-level graphics operations. These functions require a chip-select
  881. // and/or SPI transaction around them (via startWrite(), endWrite() above).
  882. // Higher-level graphics primitives might start a single transaction and
  883. // then make multiple calls to these functions (e.g. circle or text
  884. // rendering might make repeated lines or rects) before ending the
  885. // transaction. It's more efficient than starting a transaction every time.
  886. /*!
  887. @brief Draw a single pixel to the display at requested coordinates.
  888. Not self-contained; should follow a startWrite() call.
  889. @param x Horizontal position (0 = left).
  890. @param y Vertical position (0 = top).
  891. @param color 16-bit pixel color in '565' RGB format.
  892. */
  893. void Adafruit_SPITFT::writePixel(int16_t x, int16_t y, uint16_t color) {
  894. if ((x >= 0) && (x < _width) && (y >= 0) && (y < _height)) {
  895. setAddrWindow(x, y, 1, 1);
  896. SPI_WRITE16(color);
  897. }
  898. }
  899. /*!
  900. @brief Issue a series of pixels from memory to the display. Not self-
  901. contained; should follow startWrite() and setAddrWindow() calls.
  902. @param colors Pointer to array of 16-bit pixel values in '565' RGB
  903. format.
  904. @param len Number of elements in 'colors' array.
  905. @param block If true (default case if unspecified), function blocks
  906. until DMA transfer is complete. This is simply IGNORED
  907. if DMA is not enabled. If false, the function returns
  908. immediately after the last DMA transfer is started,
  909. and one should use the dmaWait() function before
  910. doing ANY other display-related activities (or even
  911. any SPI-related activities, if using an SPI display
  912. that shares the bus with other devices).
  913. @param bigEndian If using DMA, and if set true, bitmap in memory is in
  914. big-endian order (most significant byte first). By
  915. default this is false, as most microcontrollers seem
  916. to be little-endian and 16-bit pixel values must be
  917. byte-swapped before issuing to the display (which tend
  918. to be big-endian when using SPI or 8-bit parallel).
  919. If an application can optimize around this -- for
  920. example, a bitmap in a uint16_t array having the byte
  921. values already reordered big-endian, this can save
  922. some processing time here, ESPECIALLY if using this
  923. function's non-blocking DMA mode. Not all cases are
  924. covered...this is really here only for SAMD DMA and
  925. much forethought on the application side.
  926. */
  927. void Adafruit_SPITFT::writePixels(uint16_t *colors, uint32_t len, bool block,
  928. bool bigEndian) {
  929. if (!len)
  930. return; // Avoid 0-byte transfers
  931. #if defined(ESP32) // ESP32 has a special SPI pixel-writing function...
  932. if (connection == TFT_HARD_SPI) {
  933. hwspi._spi->writePixels(colors, len * 2);
  934. return;
  935. }
  936. #elif defined(ARDUINO_NRF52_ADAFRUIT) && \
  937. defined(NRF52840_XXAA) // Adafruit nRF52 use SPIM3 DMA at 32Mhz
  938. // TFT and SPI DMA endian is different we need to swap bytes
  939. if (!bigEndian) {
  940. for (uint32_t i = 0; i < len; i++) {
  941. colors[i] = __builtin_bswap16(colors[i]);
  942. }
  943. }
  944. // use the separate tx, rx buf variant to prevent overwrite the buffer
  945. hwspi._spi->transfer(colors, NULL, 2 * len);
  946. // swap back color buffer
  947. if (!bigEndian) {
  948. for (uint32_t i = 0; i < len; i++) {
  949. colors[i] = __builtin_bswap16(colors[i]);
  950. }
  951. }
  952. return;
  953. #elif defined(USE_SPI_DMA) && \
  954. (defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO))
  955. if ((connection == TFT_HARD_SPI) || (connection == TFT_PARALLEL)) {
  956. int maxSpan = maxFillLen / 2; // One scanline max
  957. uint8_t pixelBufIdx = 0; // Active pixel buffer number
  958. #if defined(__SAMD51__)
  959. if (connection == TFT_PARALLEL) {
  960. // Switch WR pin to PWM or CCL
  961. pinPeripheral(tft8._wr, wrPeripheral);
  962. }
  963. #endif // end __SAMD51__
  964. if (!bigEndian) { // Normal little-endian situation...
  965. while (len) {
  966. int count = (len < maxSpan) ? len : maxSpan;
  967. // Because TFT and SAMD endianisms are different, must swap
  968. // bytes from the 'colors' array passed into a DMA working
  969. // buffer. This can take place while the prior DMA transfer
  970. // is in progress, hence the need for two pixelBufs.
  971. for (int i = 0; i < count; i++) {
  972. pixelBuf[pixelBufIdx][i] = __builtin_bswap16(*colors++);
  973. }
  974. // The transfers themselves are relatively small, so we don't
  975. // need a long descriptor list. We just alternate between the
  976. // first two, sharing pixelBufIdx for that purpose.
  977. descriptor[pixelBufIdx].SRCADDR.reg =
  978. (uint32_t)pixelBuf[pixelBufIdx] + count * 2;
  979. descriptor[pixelBufIdx].BTCTRL.bit.SRCINC = 1;
  980. descriptor[pixelBufIdx].BTCNT.reg = count * 2;
  981. descriptor[pixelBufIdx].DESCADDR.reg = 0;
  982. while (dma_busy)
  983. ; // Wait for prior line to finish
  984. // Move new descriptor into place...
  985. memcpy(dptr, &descriptor[pixelBufIdx], sizeof(DmacDescriptor));
  986. dma_busy = true;
  987. dma.startJob(); // Trigger SPI DMA transfer
  988. if (connection == TFT_PARALLEL)
  989. dma.trigger();
  990. pixelBufIdx = 1 - pixelBufIdx; // Swap DMA pixel buffers
  991. len -= count;
  992. }
  993. } else { // bigEndian == true
  994. // With big-endian pixel data, this can be handled as a single
  995. // DMA transfer using chained descriptors. Even full screen, this
  996. // needs only a relatively short descriptor list, each
  997. // transferring a max of 32,767 (not 32,768) pixels. The list
  998. // was allocated large enough to accommodate a full screen's
  999. // worth of data, so this won't run past the end of the list.
  1000. int d, numDescriptors = (len + 32766) / 32767;
  1001. for (d = 0; d < numDescriptors; d++) {
  1002. int count = (len < 32767) ? len : 32767;
  1003. descriptor[d].SRCADDR.reg = (uint32_t)colors + count * 2;
  1004. descriptor[d].BTCTRL.bit.SRCINC = 1;
  1005. descriptor[d].BTCNT.reg = count * 2;
  1006. descriptor[d].DESCADDR.reg = (uint32_t)&descriptor[d + 1];
  1007. len -= count;
  1008. colors += count;
  1009. }
  1010. descriptor[d - 1].DESCADDR.reg = 0;
  1011. while (dma_busy)
  1012. ; // Wait for prior transfer (if any) to finish
  1013. // Move first descriptor into place and start transfer...
  1014. memcpy(dptr, &descriptor[0], sizeof(DmacDescriptor));
  1015. dma_busy = true;
  1016. dma.startJob(); // Trigger SPI DMA transfer
  1017. if (connection == TFT_PARALLEL)
  1018. dma.trigger();
  1019. } // end bigEndian
  1020. lastFillColor = 0x0000; // pixelBuf has been sullied
  1021. lastFillLen = 0;
  1022. if (block) {
  1023. while (dma_busy)
  1024. ; // Wait for last line to complete
  1025. #if defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO)
  1026. if (connection == TFT_HARD_SPI) {
  1027. // See SAMD51/21 note in writeColor()
  1028. hwspi._spi->setDataMode(hwspi._mode);
  1029. } else {
  1030. pinPeripheral(tft8._wr, PIO_OUTPUT); // Switch WR back to GPIO
  1031. }
  1032. #endif // end __SAMD51__ || ARDUINO_SAMD_ZERO
  1033. }
  1034. return;
  1035. }
  1036. #endif // end USE_SPI_DMA
  1037. // All other cases (bitbang SPI or non-DMA hard SPI or parallel),
  1038. // use a loop with the normal 16-bit data write function:
  1039. while (len--) {
  1040. SPI_WRITE16(*colors++);
  1041. }
  1042. }
  1043. /*!
  1044. @brief Wait for the last DMA transfer in a prior non-blocking
  1045. writePixels() call to complete. This does nothing if DMA
  1046. is not enabled, and is not needed if blocking writePixels()
  1047. was used (as is the default case).
  1048. */
  1049. void Adafruit_SPITFT::dmaWait(void) {
  1050. #if defined(USE_SPI_DMA) && (defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO))
  1051. while (dma_busy)
  1052. ;
  1053. #if defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO)
  1054. if (connection == TFT_HARD_SPI) {
  1055. // See SAMD51/21 note in writeColor()
  1056. hwspi._spi->setDataMode(hwspi._mode);
  1057. } else {
  1058. pinPeripheral(tft8._wr, PIO_OUTPUT); // Switch WR back to GPIO
  1059. }
  1060. #endif // end __SAMD51__ || ARDUINO_SAMD_ZERO
  1061. #endif
  1062. }
  1063. /*!
  1064. @brief Issue a series of pixels, all the same color. Not self-
  1065. contained; should follow startWrite() and setAddrWindow() calls.
  1066. @param color 16-bit pixel color in '565' RGB format.
  1067. @param len Number of pixels to draw.
  1068. */
  1069. void Adafruit_SPITFT::writeColor(uint16_t color, uint32_t len) {
  1070. if (!len)
  1071. return; // Avoid 0-byte transfers
  1072. uint8_t hi = color >> 8, lo = color;
  1073. #if defined(ESP32) // ESP32 has a special SPI pixel-writing function...
  1074. if (connection == TFT_HARD_SPI) {
  1075. #define SPI_MAX_PIXELS_AT_ONCE 32
  1076. #define TMPBUF_LONGWORDS (SPI_MAX_PIXELS_AT_ONCE + 1) / 2
  1077. #define TMPBUF_PIXELS (TMPBUF_LONGWORDS * 2)
  1078. static uint32_t temp[TMPBUF_LONGWORDS];
  1079. uint32_t c32 = color * 0x00010001;
  1080. uint16_t bufLen = (len < TMPBUF_PIXELS) ? len : TMPBUF_PIXELS, xferLen,
  1081. fillLen;
  1082. // Fill temp buffer 32 bits at a time
  1083. fillLen = (bufLen + 1) / 2; // Round up to next 32-bit boundary
  1084. for (uint32_t t = 0; t < fillLen; t++) {
  1085. temp[t] = c32;
  1086. }
  1087. // Issue pixels in blocks from temp buffer
  1088. while (len) { // While pixels remain
  1089. xferLen = (bufLen < len) ? bufLen : len; // How many this pass?
  1090. writePixels((uint16_t *)temp, xferLen);
  1091. len -= xferLen;
  1092. }
  1093. return;
  1094. }
  1095. #elif defined(ARDUINO_NRF52_ADAFRUIT) && \
  1096. defined(NRF52840_XXAA) // Adafruit nRF52840 use SPIM3 DMA at 32Mhz
  1097. // at most 2 scan lines
  1098. uint32_t const pixbufcount = min(len, ((uint32_t)2 * width()));
  1099. uint16_t *pixbuf = (uint16_t *)rtos_malloc(2 * pixbufcount);
  1100. // use SPI3 DMA if we could allocate buffer, else fall back to writing each
  1101. // pixel loop below
  1102. if (pixbuf) {
  1103. uint16_t const swap_color = __builtin_bswap16(color);
  1104. // fill buffer with color
  1105. for (uint32_t i = 0; i < pixbufcount; i++) {
  1106. pixbuf[i] = swap_color;
  1107. }
  1108. while (len) {
  1109. uint32_t const count = min(len, pixbufcount);
  1110. writePixels(pixbuf, count, true, true);
  1111. len -= count;
  1112. }
  1113. rtos_free(pixbuf);
  1114. return;
  1115. }
  1116. #else // !ESP32
  1117. #if defined(USE_SPI_DMA) && (defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO))
  1118. if (((connection == TFT_HARD_SPI) || (connection == TFT_PARALLEL)) &&
  1119. (len >= 16)) { // Don't bother with DMA on short pixel runs
  1120. int i, d, numDescriptors;
  1121. if (hi == lo) { // If high & low bytes are same...
  1122. onePixelBuf = color;
  1123. // Can do this with a relatively short descriptor list,
  1124. // each transferring a max of 32,767 (not 32,768) pixels.
  1125. // This won't run off the end of the allocated descriptor list,
  1126. // since we're using much larger chunks per descriptor here.
  1127. numDescriptors = (len + 32766) / 32767;
  1128. for (d = 0; d < numDescriptors; d++) {
  1129. int count = (len < 32767) ? len : 32767;
  1130. descriptor[d].SRCADDR.reg = (uint32_t)&onePixelBuf;
  1131. descriptor[d].BTCTRL.bit.SRCINC = 0;
  1132. descriptor[d].BTCNT.reg = count * 2;
  1133. descriptor[d].DESCADDR.reg = (uint32_t)&descriptor[d + 1];
  1134. len -= count;
  1135. }
  1136. descriptor[d - 1].DESCADDR.reg = 0;
  1137. } else {
  1138. // If high and low bytes are distinct, it's necessary to fill
  1139. // a buffer with pixel data (swapping high and low bytes because
  1140. // TFT and SAMD are different endianisms) and create a longer
  1141. // descriptor list pointing repeatedly to this data. We can do
  1142. // this slightly faster working 2 pixels (32 bits) at a time.
  1143. uint32_t *pixelPtr = (uint32_t *)pixelBuf[0],
  1144. twoPixels = __builtin_bswap16(color) * 0x00010001;
  1145. // We can avoid some or all of the buffer-filling if the color
  1146. // is the same as last time...
  1147. if (color == lastFillColor) {
  1148. // If length is longer than prior instance, fill only the
  1149. // additional pixels in the buffer and update lastFillLen.
  1150. if (len > lastFillLen) {
  1151. int fillStart = lastFillLen / 2,
  1152. fillEnd = (((len < maxFillLen) ? len : maxFillLen) + 1) / 2;
  1153. for (i = fillStart; i < fillEnd; i++)
  1154. pixelPtr[i] = twoPixels;
  1155. lastFillLen = fillEnd * 2;
  1156. } // else do nothing, don't set pixels or change lastFillLen
  1157. } else {
  1158. int fillEnd = (((len < maxFillLen) ? len : maxFillLen) + 1) / 2;
  1159. for (i = 0; i < fillEnd; i++)
  1160. pixelPtr[i] = twoPixels;
  1161. lastFillLen = fillEnd * 2;
  1162. lastFillColor = color;
  1163. }
  1164. numDescriptors = (len + maxFillLen - 1) / maxFillLen;
  1165. for (d = 0; d < numDescriptors; d++) {
  1166. int pixels = (len < maxFillLen) ? len : maxFillLen, bytes = pixels * 2;
  1167. descriptor[d].SRCADDR.reg = (uint32_t)pixelPtr + bytes;
  1168. descriptor[d].BTCTRL.bit.SRCINC = 1;
  1169. descriptor[d].BTCNT.reg = bytes;
  1170. descriptor[d].DESCADDR.reg = (uint32_t)&descriptor[d + 1];
  1171. len -= pixels;
  1172. }
  1173. descriptor[d - 1].DESCADDR.reg = 0;
  1174. }
  1175. memcpy(dptr, &descriptor[0], sizeof(DmacDescriptor));
  1176. #if defined(__SAMD51__)
  1177. if (connection == TFT_PARALLEL) {
  1178. // Switch WR pin to PWM or CCL
  1179. pinPeripheral(tft8._wr, wrPeripheral);
  1180. }
  1181. #endif // end __SAMD51__
  1182. dma_busy = true;
  1183. dma.startJob();
  1184. if (connection == TFT_PARALLEL)
  1185. dma.trigger();
  1186. while (dma_busy)
  1187. ; // Wait for completion
  1188. // Unfortunately blocking is necessary. An earlier version returned
  1189. // immediately and checked dma_busy on startWrite() instead, but it
  1190. // turns out to be MUCH slower on many graphics operations (as when
  1191. // drawing lines, pixel-by-pixel), perhaps because it's a volatile
  1192. // type and doesn't cache. Working on this.
  1193. #if defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO)
  1194. if (connection == TFT_HARD_SPI) {
  1195. // SAMD51: SPI DMA seems to leave the SPI peripheral in a freaky
  1196. // state on completion. Workaround is to explicitly set it back...
  1197. // (5/17/2019: apparently SAMD21 too, in certain cases, observed
  1198. // with ST7789 display.)
  1199. hwspi._spi->setDataMode(hwspi._mode);
  1200. } else {
  1201. pinPeripheral(tft8._wr, PIO_OUTPUT); // Switch WR back to GPIO
  1202. }
  1203. #endif // end __SAMD51__
  1204. return;
  1205. }
  1206. #endif // end USE_SPI_DMA
  1207. #endif // end !ESP32
  1208. // All other cases (non-DMA hard SPI, bitbang SPI, parallel)...
  1209. if (connection == TFT_HARD_SPI) {
  1210. #if defined(ESP8266)
  1211. do {
  1212. uint32_t pixelsThisPass = len;
  1213. if (pixelsThisPass > 50000)
  1214. pixelsThisPass = 50000;
  1215. len -= pixelsThisPass;
  1216. yield(); // Periodic yield() on long fills
  1217. while (pixelsThisPass--) {
  1218. hwspi._spi->write(hi);
  1219. hwspi._spi->write(lo);
  1220. }
  1221. } while (len);
  1222. #else // !ESP8266
  1223. while (len--) {
  1224. #if defined(__AVR__)
  1225. AVR_WRITESPI(hi);
  1226. AVR_WRITESPI(lo);
  1227. #elif defined(ESP32)
  1228. hwspi._spi->write(hi);
  1229. hwspi._spi->write(lo);
  1230. #else
  1231. hwspi._spi->transfer(hi);
  1232. hwspi._spi->transfer(lo);
  1233. #endif
  1234. }
  1235. #endif // end !ESP8266
  1236. } else if (connection == TFT_SOFT_SPI) {
  1237. #if defined(ESP8266)
  1238. do {
  1239. uint32_t pixelsThisPass = len;
  1240. if (pixelsThisPass > 20000)
  1241. pixelsThisPass = 20000;
  1242. len -= pixelsThisPass;
  1243. yield(); // Periodic yield() on long fills
  1244. while (pixelsThisPass--) {
  1245. for (uint16_t bit = 0, x = color; bit < 16; bit++) {
  1246. if (x & 0x8000)
  1247. SPI_MOSI_HIGH();
  1248. else
  1249. SPI_MOSI_LOW();
  1250. SPI_SCK_HIGH();
  1251. SPI_SCK_LOW();
  1252. x <<= 1;
  1253. }
  1254. }
  1255. } while (len);
  1256. #else // !ESP8266
  1257. while (len--) {
  1258. #if defined(__AVR__)
  1259. for (uint8_t bit = 0, x = hi; bit < 8; bit++) {
  1260. if (x & 0x80)
  1261. SPI_MOSI_HIGH();
  1262. else
  1263. SPI_MOSI_LOW();
  1264. SPI_SCK_HIGH();
  1265. SPI_SCK_LOW();
  1266. x <<= 1;
  1267. }
  1268. for (uint8_t bit = 0, x = lo; bit < 8; bit++) {
  1269. if (x & 0x80)
  1270. SPI_MOSI_HIGH();
  1271. else
  1272. SPI_MOSI_LOW();
  1273. SPI_SCK_HIGH();
  1274. SPI_SCK_LOW();
  1275. x <<= 1;
  1276. }
  1277. #else // !__AVR__
  1278. for (uint16_t bit = 0, x = color; bit < 16; bit++) {
  1279. if (x & 0x8000)
  1280. SPI_MOSI_HIGH();
  1281. else
  1282. SPI_MOSI_LOW();
  1283. SPI_SCK_HIGH();
  1284. x <<= 1;
  1285. SPI_SCK_LOW();
  1286. }
  1287. #endif // end !__AVR__
  1288. }
  1289. #endif // end !ESP8266
  1290. } else { // PARALLEL
  1291. if (hi == lo) {
  1292. #if defined(__AVR__)
  1293. len *= 2;
  1294. *tft8.writePort = hi;
  1295. while (len--) {
  1296. TFT_WR_STROBE();
  1297. }
  1298. #elif defined(USE_FAST_PINIO)
  1299. if (!tft8.wide) {
  1300. len *= 2;
  1301. *tft8.writePort = hi;
  1302. } else {
  1303. *(volatile uint16_t *)tft8.writePort = color;
  1304. }
  1305. while (len--) {
  1306. TFT_WR_STROBE();
  1307. }
  1308. #endif
  1309. } else {
  1310. while (len--) {
  1311. #if defined(__AVR__)
  1312. *tft8.writePort = hi;
  1313. TFT_WR_STROBE();
  1314. *tft8.writePort = lo;
  1315. #elif defined(USE_FAST_PINIO)
  1316. if (!tft8.wide) {
  1317. *tft8.writePort = hi;
  1318. TFT_WR_STROBE();
  1319. *tft8.writePort = lo;
  1320. } else {
  1321. *(volatile uint16_t *)tft8.writePort = color;
  1322. }
  1323. #endif
  1324. TFT_WR_STROBE();
  1325. }
  1326. }
  1327. }
  1328. }
  1329. /*!
  1330. @brief Draw a filled rectangle to the display. Not self-contained;
  1331. should follow startWrite(). Typically used by higher-level
  1332. graphics primitives; user code shouldn't need to call this and
  1333. is likely to use the self-contained fillRect() instead.
  1334. writeFillRect() performs its own edge clipping and rejection;
  1335. see writeFillRectPreclipped() for a more 'raw' implementation.
  1336. @param x Horizontal position of first corner.
  1337. @param y Vertical position of first corner.
  1338. @param w Rectangle width in pixels (positive = right of first
  1339. corner, negative = left of first corner).
  1340. @param h Rectangle height in pixels (positive = below first
  1341. corner, negative = above first corner).
  1342. @param color 16-bit fill color in '565' RGB format.
  1343. @note Written in this deep-nested way because C by definition will
  1344. optimize for the 'if' case, not the 'else' -- avoids branches
  1345. and rejects clipped rectangles at the least-work possibility.
  1346. */
  1347. void Adafruit_SPITFT::writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h,
  1348. uint16_t color) {
  1349. if (w && h) { // Nonzero width and height?
  1350. if (w < 0) { // If negative width...
  1351. x += w + 1; // Move X to left edge
  1352. w = -w; // Use positive width
  1353. }
  1354. if (x < _width) { // Not off right
  1355. if (h < 0) { // If negative height...
  1356. y += h + 1; // Move Y to top edge
  1357. h = -h; // Use positive height
  1358. }
  1359. if (y < _height) { // Not off bottom
  1360. int16_t x2 = x + w - 1;
  1361. if (x2 >= 0) { // Not off left
  1362. int16_t y2 = y + h - 1;
  1363. if (y2 >= 0) { // Not off top
  1364. // Rectangle partly or fully overlaps screen
  1365. if (x < 0) {
  1366. x = 0;
  1367. w = x2 + 1;
  1368. } // Clip left
  1369. if (y < 0) {
  1370. y = 0;
  1371. h = y2 + 1;
  1372. } // Clip top
  1373. if (x2 >= _width) {
  1374. w = _width - x;
  1375. } // Clip right
  1376. if (y2 >= _height) {
  1377. h = _height - y;
  1378. } // Clip bottom
  1379. writeFillRectPreclipped(x, y, w, h, color);
  1380. }
  1381. }
  1382. }
  1383. }
  1384. }
  1385. }
  1386. /*!
  1387. @brief Draw a horizontal line on the display. Performs edge clipping
  1388. and rejection. Not self-contained; should follow startWrite().
  1389. Typically used by higher-level graphics primitives; user code
  1390. shouldn't need to call this and is likely to use the self-
  1391. contained drawFastHLine() instead.
  1392. @param x Horizontal position of first point.
  1393. @param y Vertical position of first point.
  1394. @param w Line width in pixels (positive = right of first point,
  1395. negative = point of first corner).
  1396. @param color 16-bit line color in '565' RGB format.
  1397. */
  1398. void inline Adafruit_SPITFT::writeFastHLine(int16_t x, int16_t y, int16_t w,
  1399. uint16_t color) {
  1400. if ((y >= 0) && (y < _height) && w) { // Y on screen, nonzero width
  1401. if (w < 0) { // If negative width...
  1402. x += w + 1; // Move X to left edge
  1403. w = -w; // Use positive width
  1404. }
  1405. if (x < _width) { // Not off right
  1406. int16_t x2 = x + w - 1;
  1407. if (x2 >= 0) { // Not off left
  1408. // Line partly or fully overlaps screen
  1409. if (x < 0) {
  1410. x = 0;
  1411. w = x2 + 1;
  1412. } // Clip left
  1413. if (x2 >= _width) {
  1414. w = _width - x;
  1415. } // Clip right
  1416. writeFillRectPreclipped(x, y, w, 1, color);
  1417. }
  1418. }
  1419. }
  1420. }
  1421. /*!
  1422. @brief Draw a vertical line on the display. Performs edge clipping and
  1423. rejection. Not self-contained; should follow startWrite().
  1424. Typically used by higher-level graphics primitives; user code
  1425. shouldn't need to call this and is likely to use the self-
  1426. contained drawFastVLine() instead.
  1427. @param x Horizontal position of first point.
  1428. @param y Vertical position of first point.
  1429. @param h Line height in pixels (positive = below first point,
  1430. negative = above first point).
  1431. @param color 16-bit line color in '565' RGB format.
  1432. */
  1433. void inline Adafruit_SPITFT::writeFastVLine(int16_t x, int16_t y, int16_t h,
  1434. uint16_t color) {
  1435. if ((x >= 0) && (x < _width) && h) { // X on screen, nonzero height
  1436. if (h < 0) { // If negative height...
  1437. y += h + 1; // Move Y to top edge
  1438. h = -h; // Use positive height
  1439. }
  1440. if (y < _height) { // Not off bottom
  1441. int16_t y2 = y + h - 1;
  1442. if (y2 >= 0) { // Not off top
  1443. // Line partly or fully overlaps screen
  1444. if (y < 0) {
  1445. y = 0;
  1446. h = y2 + 1;
  1447. } // Clip top
  1448. if (y2 >= _height) {
  1449. h = _height - y;
  1450. } // Clip bottom
  1451. writeFillRectPreclipped(x, y, 1, h, color);
  1452. }
  1453. }
  1454. }
  1455. }
  1456. /*!
  1457. @brief A lower-level version of writeFillRect(). This version requires
  1458. all inputs are in-bounds, that width and height are positive,
  1459. and no part extends offscreen. NO EDGE CLIPPING OR REJECTION IS
  1460. PERFORMED. If higher-level graphics primitives are written to
  1461. handle their own clipping earlier in the drawing process, this
  1462. can avoid unnecessary function calls and repeated clipping
  1463. operations in the lower-level functions.
  1464. @param x Horizontal position of first corner. MUST BE WITHIN
  1465. SCREEN BOUNDS.
  1466. @param y Vertical position of first corner. MUST BE WITHIN SCREEN
  1467. BOUNDS.
  1468. @param w Rectangle width in pixels. MUST BE POSITIVE AND NOT
  1469. EXTEND OFF SCREEN.
  1470. @param h Rectangle height in pixels. MUST BE POSITIVE AND NOT
  1471. EXTEND OFF SCREEN.
  1472. @param color 16-bit fill color in '565' RGB format.
  1473. @note This is a new function, no graphics primitives besides rects
  1474. and horizontal/vertical lines are written to best use this yet.
  1475. */
  1476. inline void Adafruit_SPITFT::writeFillRectPreclipped(int16_t x, int16_t y,
  1477. int16_t w, int16_t h,
  1478. uint16_t color) {
  1479. setAddrWindow(x, y, w, h);
  1480. writeColor(color, (uint32_t)w * h);
  1481. }
  1482. // -------------------------------------------------------------------------
  1483. // Ever-so-slightly higher-level graphics operations. Similar to the 'write'
  1484. // functions above, but these contain their own chip-select and SPI
  1485. // transactions as needed (via startWrite(), endWrite()). They're typically
  1486. // used solo -- as graphics primitives in themselves, not invoked by higher-
  1487. // level primitives (which should use the functions above for better
  1488. // performance).
  1489. /*!
  1490. @brief Draw a single pixel to the display at requested coordinates.
  1491. Self-contained and provides its own transaction as needed
  1492. (see writePixel(x,y,color) for a lower-level variant).
  1493. Edge clipping is performed here.
  1494. @param x Horizontal position (0 = left).
  1495. @param y Vertical position (0 = top).
  1496. @param color 16-bit pixel color in '565' RGB format.
  1497. */
  1498. void Adafruit_SPITFT::drawPixel(int16_t x, int16_t y, uint16_t color) {
  1499. // Clip first...
  1500. if ((x >= 0) && (x < _width) && (y >= 0) && (y < _height)) {
  1501. // THEN set up transaction (if needed) and draw...
  1502. startWrite();
  1503. setAddrWindow(x, y, 1, 1);
  1504. SPI_WRITE16(color);
  1505. endWrite();
  1506. }
  1507. }
  1508. /*!
  1509. @brief Draw a filled rectangle to the display. Self-contained and
  1510. provides its own transaction as needed (see writeFillRect() or
  1511. writeFillRectPreclipped() for lower-level variants). Edge
  1512. clipping and rejection is performed here.
  1513. @param x Horizontal position of first corner.
  1514. @param y Vertical position of first corner.
  1515. @param w Rectangle width in pixels (positive = right of first
  1516. corner, negative = left of first corner).
  1517. @param h Rectangle height in pixels (positive = below first
  1518. corner, negative = above first corner).
  1519. @param color 16-bit fill color in '565' RGB format.
  1520. @note This repeats the writeFillRect() function almost in its entirety,
  1521. with the addition of a transaction start/end. It's done this way
  1522. (rather than starting the transaction and calling writeFillRect()
  1523. to handle clipping and so forth) so that the transaction isn't
  1524. performed at all if the rectangle is rejected. It's really not
  1525. that much code.
  1526. */
  1527. void Adafruit_SPITFT::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
  1528. uint16_t color) {
  1529. if (w && h) { // Nonzero width and height?
  1530. if (w < 0) { // If negative width...
  1531. x += w + 1; // Move X to left edge
  1532. w = -w; // Use positive width
  1533. }
  1534. if (x < _width) { // Not off right
  1535. if (h < 0) { // If negative height...
  1536. y += h + 1; // Move Y to top edge
  1537. h = -h; // Use positive height
  1538. }
  1539. if (y < _height) { // Not off bottom
  1540. int16_t x2 = x + w - 1;
  1541. if (x2 >= 0) { // Not off left
  1542. int16_t y2 = y + h - 1;
  1543. if (y2 >= 0) { // Not off top
  1544. // Rectangle partly or fully overlaps screen
  1545. if (x < 0) {
  1546. x = 0;
  1547. w = x2 + 1;
  1548. } // Clip left
  1549. if (y < 0) {
  1550. y = 0;
  1551. h = y2 + 1;
  1552. } // Clip top
  1553. if (x2 >= _width) {
  1554. w = _width - x;
  1555. } // Clip right
  1556. if (y2 >= _height) {
  1557. h = _height - y;
  1558. } // Clip bottom
  1559. startWrite();
  1560. writeFillRectPreclipped(x, y, w, h, color);
  1561. endWrite();
  1562. }
  1563. }
  1564. }
  1565. }
  1566. }
  1567. }
  1568. /*!
  1569. @brief Draw a horizontal line on the display. Self-contained and
  1570. provides its own transaction as needed (see writeFastHLine() for
  1571. a lower-level variant). Edge clipping and rejection is performed
  1572. here.
  1573. @param x Horizontal position of first point.
  1574. @param y Vertical position of first point.
  1575. @param w Line width in pixels (positive = right of first point,
  1576. negative = point of first corner).
  1577. @param color 16-bit line color in '565' RGB format.
  1578. @note This repeats the writeFastHLine() function almost in its
  1579. entirety, with the addition of a transaction start/end. It's
  1580. done this way (rather than starting the transaction and calling
  1581. writeFastHLine() to handle clipping and so forth) so that the
  1582. transaction isn't performed at all if the line is rejected.
  1583. */
  1584. void Adafruit_SPITFT::drawFastHLine(int16_t x, int16_t y, int16_t w,
  1585. uint16_t color) {
  1586. if ((y >= 0) && (y < _height) && w) { // Y on screen, nonzero width
  1587. if (w < 0) { // If negative width...
  1588. x += w + 1; // Move X to left edge
  1589. w = -w; // Use positive width
  1590. }
  1591. if (x < _width) { // Not off right
  1592. int16_t x2 = x + w - 1;
  1593. if (x2 >= 0) { // Not off left
  1594. // Line partly or fully overlaps screen
  1595. if (x < 0) {
  1596. x = 0;
  1597. w = x2 + 1;
  1598. } // Clip left
  1599. if (x2 >= _width) {
  1600. w = _width - x;
  1601. } // Clip right
  1602. startWrite();
  1603. writeFillRectPreclipped(x, y, w, 1, color);
  1604. endWrite();
  1605. }
  1606. }
  1607. }
  1608. }
  1609. /*!
  1610. @brief Draw a vertical line on the display. Self-contained and provides
  1611. its own transaction as needed (see writeFastHLine() for a lower-
  1612. level variant). Edge clipping and rejection is performed here.
  1613. @param x Horizontal position of first point.
  1614. @param y Vertical position of first point.
  1615. @param h Line height in pixels (positive = below first point,
  1616. negative = above first point).
  1617. @param color 16-bit line color in '565' RGB format.
  1618. @note This repeats the writeFastVLine() function almost in its
  1619. entirety, with the addition of a transaction start/end. It's
  1620. done this way (rather than starting the transaction and calling
  1621. writeFastVLine() to handle clipping and so forth) so that the
  1622. transaction isn't performed at all if the line is rejected.
  1623. */
  1624. void Adafruit_SPITFT::drawFastVLine(int16_t x, int16_t y, int16_t h,
  1625. uint16_t color) {
  1626. if ((x >= 0) && (x < _width) && h) { // X on screen, nonzero height
  1627. if (h < 0) { // If negative height...
  1628. y += h + 1; // Move Y to top edge
  1629. h = -h; // Use positive height
  1630. }
  1631. if (y < _height) { // Not off bottom
  1632. int16_t y2 = y + h - 1;
  1633. if (y2 >= 0) { // Not off top
  1634. // Line partly or fully overlaps screen
  1635. if (y < 0) {
  1636. y = 0;
  1637. h = y2 + 1;
  1638. } // Clip top
  1639. if (y2 >= _height) {
  1640. h = _height - y;
  1641. } // Clip bottom
  1642. startWrite();
  1643. writeFillRectPreclipped(x, y, 1, h, color);
  1644. endWrite();
  1645. }
  1646. }
  1647. }
  1648. }
  1649. /*!
  1650. @brief Essentially writePixel() with a transaction around it. I don't
  1651. think this is in use by any of our code anymore (believe it was
  1652. for some older BMP-reading examples), but is kept here in case
  1653. any user code relies on it. Consider it DEPRECATED.
  1654. @param color 16-bit pixel color in '565' RGB format.
  1655. */
  1656. void Adafruit_SPITFT::pushColor(uint16_t color) {
  1657. startWrite();
  1658. SPI_WRITE16(color);
  1659. endWrite();
  1660. }
  1661. /*!
  1662. @brief Draw a 16-bit image (565 RGB) at the specified (x,y) position.
  1663. For 16-bit display devices; no color reduction performed.
  1664. Adapted from https://github.com/PaulStoffregen/ILI9341_t3
  1665. by Marc MERLIN. See examples/pictureEmbed to use this.
  1666. 5/6/2017: function name and arguments have changed for
  1667. compatibility with current GFX library and to avoid naming
  1668. problems in prior implementation. Formerly drawBitmap() with
  1669. arguments in different order. Handles its own transaction and
  1670. edge clipping/rejection.
  1671. @param x Top left corner horizontal coordinate.
  1672. @param y Top left corner vertical coordinate.
  1673. @param pcolors Pointer to 16-bit array of pixel values.
  1674. @param w Width of bitmap in pixels.
  1675. @param h Height of bitmap in pixels.
  1676. */
  1677. void Adafruit_SPITFT::drawRGBBitmap(int16_t x, int16_t y, uint16_t *pcolors,
  1678. int16_t w, int16_t h) {
  1679. int16_t x2, y2; // Lower-right coord
  1680. if ((x >= _width) || // Off-edge right
  1681. (y >= _height) || // " top
  1682. ((x2 = (x + w - 1)) < 0) || // " left
  1683. ((y2 = (y + h - 1)) < 0))
  1684. return; // " bottom
  1685. int16_t bx1 = 0, by1 = 0, // Clipped top-left within bitmap
  1686. saveW = w; // Save original bitmap width value
  1687. if (x < 0) { // Clip left
  1688. w += x;
  1689. bx1 = -x;
  1690. x = 0;
  1691. }
  1692. if (y < 0) { // Clip top
  1693. h += y;
  1694. by1 = -y;
  1695. y = 0;
  1696. }
  1697. if (x2 >= _width)
  1698. w = _width - x; // Clip right
  1699. if (y2 >= _height)
  1700. h = _height - y; // Clip bottom
  1701. pcolors += by1 * saveW + bx1; // Offset bitmap ptr to clipped top-left
  1702. startWrite();
  1703. setAddrWindow(x, y, w, h); // Clipped area
  1704. while (h--) { // For each (clipped) scanline...
  1705. writePixels(pcolors, w); // Push one (clipped) row
  1706. pcolors += saveW; // Advance pointer by one full (unclipped) line
  1707. }
  1708. endWrite();
  1709. }
  1710. // -------------------------------------------------------------------------
  1711. // Miscellaneous class member functions that don't draw anything.
  1712. /*!
  1713. @brief Invert the colors of the display (if supported by hardware).
  1714. Self-contained, no transaction setup required.
  1715. @param i true = inverted display, false = normal display.
  1716. */
  1717. void Adafruit_SPITFT::invertDisplay(bool i) {
  1718. startWrite();
  1719. writeCommand(i ? invertOnCommand : invertOffCommand);
  1720. endWrite();
  1721. }
  1722. /*!
  1723. @brief Given 8-bit red, green and blue values, return a 'packed'
  1724. 16-bit color value in '565' RGB format (5 bits red, 6 bits
  1725. green, 5 bits blue). This is just a mathematical operation,
  1726. no hardware is touched.
  1727. @param red 8-bit red brightnesss (0 = off, 255 = max).
  1728. @param green 8-bit green brightnesss (0 = off, 255 = max).
  1729. @param blue 8-bit blue brightnesss (0 = off, 255 = max).
  1730. @return 'Packed' 16-bit color value (565 format).
  1731. */
  1732. uint16_t Adafruit_SPITFT::color565(uint8_t red, uint8_t green, uint8_t blue) {
  1733. return ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | (blue >> 3);
  1734. }
  1735. /*!
  1736. @brief Adafruit_SPITFT Send Command handles complete sending of commands and
  1737. data
  1738. @param commandByte The Command Byte
  1739. @param dataBytes A pointer to the Data bytes to send
  1740. @param numDataBytes The number of bytes we should send
  1741. */
  1742. void Adafruit_SPITFT::sendCommand(uint8_t commandByte, uint8_t *dataBytes,
  1743. uint8_t numDataBytes) {
  1744. SPI_BEGIN_TRANSACTION();
  1745. if (_cs >= 0)
  1746. SPI_CS_LOW();
  1747. SPI_DC_LOW(); // Command mode
  1748. spiWrite(commandByte); // Send the command byte
  1749. SPI_DC_HIGH();
  1750. for (int i = 0; i < numDataBytes; i++) {
  1751. if ((connection == TFT_PARALLEL) && tft8.wide) {
  1752. SPI_WRITE16(*(uint16_t *)dataBytes);
  1753. dataBytes += 2;
  1754. } else {
  1755. spiWrite(*dataBytes); // Send the data bytes
  1756. dataBytes++;
  1757. }
  1758. }
  1759. if (_cs >= 0)
  1760. SPI_CS_HIGH();
  1761. SPI_END_TRANSACTION();
  1762. }
  1763. /*!
  1764. @brief Adafruit_SPITFT Send Command handles complete sending of commands and
  1765. data
  1766. @param commandByte The Command Byte
  1767. @param dataBytes A pointer to the Data bytes to send
  1768. @param numDataBytes The number of bytes we should send
  1769. */
  1770. void Adafruit_SPITFT::sendCommand(uint8_t commandByte, const uint8_t *dataBytes,
  1771. uint8_t numDataBytes) {
  1772. SPI_BEGIN_TRANSACTION();
  1773. if (_cs >= 0)
  1774. SPI_CS_LOW();
  1775. SPI_DC_LOW(); // Command mode
  1776. spiWrite(commandByte); // Send the command byte
  1777. SPI_DC_HIGH();
  1778. for (int i = 0; i < numDataBytes; i++) {
  1779. if ((connection == TFT_PARALLEL) && tft8.wide) {
  1780. SPI_WRITE16(*(uint16_t *)dataBytes);
  1781. dataBytes += 2;
  1782. } else {
  1783. spiWrite(pgm_read_byte(dataBytes++));
  1784. }
  1785. }
  1786. if (_cs >= 0)
  1787. SPI_CS_HIGH();
  1788. SPI_END_TRANSACTION();
  1789. }
  1790. /*!
  1791. @brief Adafruit_SPITFT sendCommand16 handles complete sending of
  1792. commands and data for 16-bit parallel displays. Currently somewhat
  1793. rigged for the NT35510, which has the odd behavior of wanting
  1794. commands 16-bit, but subsequent data as 8-bit values, despite
  1795. the 16-bit bus (high byte is always 0). Also seems to require
  1796. issuing and incrementing address with each transfer.
  1797. @param commandWord The command word (16 bits)
  1798. @param dataBytes A pointer to the data bytes to send
  1799. @param numDataBytes The number of bytes we should send
  1800. */
  1801. void Adafruit_SPITFT::sendCommand16(uint16_t commandWord,
  1802. const uint8_t *dataBytes,
  1803. uint8_t numDataBytes) {
  1804. SPI_BEGIN_TRANSACTION();
  1805. if (_cs >= 0)
  1806. SPI_CS_LOW();
  1807. if (numDataBytes == 0) {
  1808. SPI_DC_LOW(); // Command mode
  1809. SPI_WRITE16(commandWord); // Send the command word
  1810. SPI_DC_HIGH(); // Data mode
  1811. }
  1812. for (int i = 0; i < numDataBytes; i++) {
  1813. SPI_DC_LOW(); // Command mode
  1814. SPI_WRITE16(commandWord); // Send the command word
  1815. SPI_DC_HIGH(); // Data mode
  1816. commandWord++;
  1817. SPI_WRITE16((uint16_t)pgm_read_byte(dataBytes++));
  1818. }
  1819. if (_cs >= 0)
  1820. SPI_CS_HIGH();
  1821. SPI_END_TRANSACTION();
  1822. }
  1823. /*!
  1824. @brief Read 8 bits of data from display configuration memory (not RAM).
  1825. This is highly undocumented/supported and should be avoided,
  1826. function is only included because some of the examples use it.
  1827. @param commandByte
  1828. The command register to read data from.
  1829. @param index
  1830. The byte index into the command to read from.
  1831. @return Unsigned 8-bit data read from display register.
  1832. */
  1833. /**************************************************************************/
  1834. uint8_t Adafruit_SPITFT::readcommand8(uint8_t commandByte, uint8_t index) {
  1835. uint8_t result;
  1836. startWrite();
  1837. SPI_DC_LOW(); // Command mode
  1838. spiWrite(commandByte);
  1839. SPI_DC_HIGH(); // Data mode
  1840. do {
  1841. result = spiRead();
  1842. } while (index--); // Discard bytes up to index'th
  1843. endWrite();
  1844. return result;
  1845. }
  1846. /*!
  1847. @brief Read 16 bits of data from display register.
  1848. For 16-bit parallel displays only.
  1849. @param addr Command/register to access.
  1850. @return Unsigned 16-bit data.
  1851. */
  1852. uint16_t Adafruit_SPITFT::readcommand16(uint16_t addr) {
  1853. #if defined(USE_FAST_PINIO) // NOT SUPPORTED without USE_FAST_PINIO
  1854. uint16_t result = 0;
  1855. if ((connection == TFT_PARALLEL) && tft8.wide) {
  1856. startWrite();
  1857. SPI_DC_LOW(); // Command mode
  1858. SPI_WRITE16(addr);
  1859. SPI_DC_HIGH(); // Data mode
  1860. TFT_RD_LOW(); // Read line LOW
  1861. #if defined(HAS_PORT_SET_CLR)
  1862. *(volatile uint16_t *)tft8.dirClr = 0xFFFF; // Input state
  1863. result = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  1864. *(volatile uint16_t *)tft8.dirSet = 0xFFFF; // Output state
  1865. #else // !HAS_PORT_SET_CLR
  1866. *(volatile uint16_t *)tft8.portDir = 0x0000; // Input state
  1867. result = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  1868. *(volatile uint16_t *)tft8.portDir = 0xFFFF; // Output state
  1869. #endif // end !HAS_PORT_SET_CLR
  1870. TFT_RD_HIGH(); // Read line HIGH
  1871. endWrite();
  1872. }
  1873. return result;
  1874. #else
  1875. return 0;
  1876. #endif // end !USE_FAST_PINIO
  1877. }
  1878. // -------------------------------------------------------------------------
  1879. // Lowest-level hardware-interfacing functions. Many of these are inline and
  1880. // compile to different things based on #defines -- typically just a few
  1881. // instructions. Others, not so much, those are not inlined.
  1882. /*!
  1883. @brief Start an SPI transaction if using the hardware SPI interface to
  1884. the display. If using an earlier version of the Arduino platform
  1885. (before the addition of SPI transactions), this instead attempts
  1886. to set up the SPI clock and mode. No action is taken if the
  1887. connection is not hardware SPI-based. This does NOT include a
  1888. chip-select operation -- see startWrite() for a function that
  1889. encapsulated both actions.
  1890. */
  1891. inline void Adafruit_SPITFT::SPI_BEGIN_TRANSACTION(void) {
  1892. if (connection == TFT_HARD_SPI) {
  1893. #if defined(SPI_HAS_TRANSACTION)
  1894. hwspi._spi->beginTransaction(hwspi.settings);
  1895. #else // No transactions, configure SPI manually...
  1896. #if defined(__AVR__) || defined(TEENSYDUINO) || defined(ARDUINO_ARCH_STM32F1)
  1897. hwspi._spi->setClockDivider(SPI_CLOCK_DIV2);
  1898. #elif defined(__arm__)
  1899. hwspi._spi->setClockDivider(11);
  1900. #elif defined(ESP8266) || defined(ESP32)
  1901. hwspi._spi->setFrequency(hwspi._freq);
  1902. #elif defined(RASPI) || defined(ARDUINO_ARCH_STM32F1)
  1903. hwspi._spi->setClock(hwspi._freq);
  1904. #endif
  1905. hwspi._spi->setBitOrder(MSBFIRST);
  1906. hwspi._spi->setDataMode(hwspi._mode);
  1907. #endif // end !SPI_HAS_TRANSACTION
  1908. }
  1909. }
  1910. /*!
  1911. @brief End an SPI transaction if using the hardware SPI interface to
  1912. the display. No action is taken if the connection is not
  1913. hardware SPI-based or if using an earlier version of the Arduino
  1914. platform (before the addition of SPI transactions). This does
  1915. NOT include a chip-deselect operation -- see endWrite() for a
  1916. function that encapsulated both actions.
  1917. */
  1918. inline void Adafruit_SPITFT::SPI_END_TRANSACTION(void) {
  1919. #if defined(SPI_HAS_TRANSACTION)
  1920. if (connection == TFT_HARD_SPI) {
  1921. hwspi._spi->endTransaction();
  1922. }
  1923. #endif
  1924. }
  1925. /*!
  1926. @brief Issue a single 8-bit value to the display. Chip-select,
  1927. transaction and data/command selection must have been
  1928. previously set -- this ONLY issues the byte. This is another of
  1929. those functions in the library with a now-not-accurate name
  1930. that's being maintained for compatibility with outside code.
  1931. This function is used even if display connection is parallel.
  1932. @param b 8-bit value to write.
  1933. */
  1934. void Adafruit_SPITFT::spiWrite(uint8_t b) {
  1935. if (connection == TFT_HARD_SPI) {
  1936. #if defined(__AVR__)
  1937. AVR_WRITESPI(b);
  1938. #elif defined(ESP8266) || defined(ESP32)
  1939. hwspi._spi->write(b);
  1940. #else
  1941. hwspi._spi->transfer(b);
  1942. #endif
  1943. } else if (connection == TFT_SOFT_SPI) {
  1944. for (uint8_t bit = 0; bit < 8; bit++) {
  1945. if (b & 0x80)
  1946. SPI_MOSI_HIGH();
  1947. else
  1948. SPI_MOSI_LOW();
  1949. SPI_SCK_HIGH();
  1950. b <<= 1;
  1951. SPI_SCK_LOW();
  1952. }
  1953. } else { // TFT_PARALLEL
  1954. #if defined(__AVR__)
  1955. *tft8.writePort = b;
  1956. #elif defined(USE_FAST_PINIO)
  1957. if (!tft8.wide)
  1958. *tft8.writePort = b;
  1959. else
  1960. *(volatile uint16_t *)tft8.writePort = b;
  1961. #endif
  1962. TFT_WR_STROBE();
  1963. }
  1964. }
  1965. /*!
  1966. @brief Write a single command byte to the display. Chip-select and
  1967. transaction must have been previously set -- this ONLY sets
  1968. the device to COMMAND mode, issues the byte and then restores
  1969. DATA mode. There is no corresponding explicit writeData()
  1970. function -- just use spiWrite().
  1971. @param cmd 8-bit command to write.
  1972. */
  1973. void Adafruit_SPITFT::writeCommand(uint8_t cmd) {
  1974. SPI_DC_LOW();
  1975. spiWrite(cmd);
  1976. SPI_DC_HIGH();
  1977. }
  1978. /*!
  1979. @brief Read a single 8-bit value from the display. Chip-select and
  1980. transaction must have been previously set -- this ONLY reads
  1981. the byte. This is another of those functions in the library
  1982. with a now-not-accurate name that's being maintained for
  1983. compatibility with outside code. This function is used even if
  1984. display connection is parallel.
  1985. @return Unsigned 8-bit value read (always zero if USE_FAST_PINIO is
  1986. not supported by the MCU architecture).
  1987. */
  1988. uint8_t Adafruit_SPITFT::spiRead(void) {
  1989. uint8_t b = 0;
  1990. uint16_t w = 0;
  1991. if (connection == TFT_HARD_SPI) {
  1992. return hwspi._spi->transfer((uint8_t)0);
  1993. } else if (connection == TFT_SOFT_SPI) {
  1994. if (swspi._miso >= 0) {
  1995. for (uint8_t i = 0; i < 8; i++) {
  1996. SPI_SCK_HIGH();
  1997. b <<= 1;
  1998. if (SPI_MISO_READ())
  1999. b++;
  2000. SPI_SCK_LOW();
  2001. }
  2002. }
  2003. return b;
  2004. } else { // TFT_PARALLEL
  2005. if (tft8._rd >= 0) {
  2006. #if defined(USE_FAST_PINIO)
  2007. TFT_RD_LOW(); // Read line LOW
  2008. #if defined(__AVR__)
  2009. *tft8.portDir = 0x00; // Set port to input state
  2010. w = *tft8.readPort; // Read value from port
  2011. *tft8.portDir = 0xFF; // Restore port to output
  2012. #else // !__AVR__
  2013. if (!tft8.wide) { // 8-bit TFT connection
  2014. #if defined(HAS_PORT_SET_CLR)
  2015. *tft8.dirClr = 0xFF; // Set port to input state
  2016. w = *tft8.readPort; // Read value from port
  2017. *tft8.dirSet = 0xFF; // Restore port to output
  2018. #else // !HAS_PORT_SET_CLR
  2019. *tft8.portDir = 0x00; // Set port to input state
  2020. w = *tft8.readPort; // Read value from port
  2021. *tft8.portDir = 0xFF; // Restore port to output
  2022. #endif // end HAS_PORT_SET_CLR
  2023. } else { // 16-bit TFT connection
  2024. #if defined(HAS_PORT_SET_CLR)
  2025. *(volatile uint16_t *)tft8.dirClr = 0xFFFF; // Input state
  2026. w = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  2027. *(volatile uint16_t *)tft8.dirSet = 0xFFFF; // Output state
  2028. #else // !HAS_PORT_SET_CLR
  2029. *(volatile uint16_t *)tft8.portDir = 0x0000; // Input state
  2030. w = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  2031. *(volatile uint16_t *)tft8.portDir = 0xFFFF; // Output state
  2032. #endif // end !HAS_PORT_SET_CLR
  2033. }
  2034. TFT_RD_HIGH(); // Read line HIGH
  2035. #endif // end !__AVR__
  2036. #else // !USE_FAST_PINIO
  2037. w = 0; // Parallel TFT is NOT SUPPORTED without USE_FAST_PINIO
  2038. #endif // end !USE_FAST_PINIO
  2039. }
  2040. return w;
  2041. }
  2042. }
  2043. /*!
  2044. @brief Issue a single 16-bit value to the display. Chip-select,
  2045. transaction and data/command selection must have been
  2046. previously set -- this ONLY issues the word.
  2047. Thus operates ONLY on 'wide' (16-bit) parallel displays!
  2048. @param w 16-bit value to write.
  2049. */
  2050. void Adafruit_SPITFT::write16(uint16_t w) {
  2051. if (connection == TFT_PARALLEL) {
  2052. #if defined(USE_FAST_PINIO)
  2053. if (tft8.wide)
  2054. *(volatile uint16_t *)tft8.writePort = w;
  2055. #endif
  2056. TFT_WR_STROBE();
  2057. }
  2058. }
  2059. /*!
  2060. @brief Write a single command word to the display. Chip-select and
  2061. transaction must have been previously set -- this ONLY sets
  2062. the device to COMMAND mode, issues the byte and then restores
  2063. DATA mode. This operates ONLY on 'wide' (16-bit) parallel
  2064. displays!
  2065. @param cmd 16-bit command to write.
  2066. */
  2067. void Adafruit_SPITFT::writeCommand16(uint16_t cmd) {
  2068. SPI_DC_LOW();
  2069. write16(cmd);
  2070. SPI_DC_HIGH();
  2071. }
  2072. /*!
  2073. @brief Read a single 16-bit value from the display. Chip-select and
  2074. transaction must have been previously set -- this ONLY reads
  2075. the byte. This operates ONLY on 'wide' (16-bit) parallel
  2076. displays!
  2077. @return Unsigned 16-bit value read (always zero if USE_FAST_PINIO is
  2078. not supported by the MCU architecture).
  2079. */
  2080. uint16_t Adafruit_SPITFT::read16(void) {
  2081. uint16_t w = 0;
  2082. if (connection == TFT_PARALLEL) {
  2083. if (tft8._rd >= 0) {
  2084. #if defined(USE_FAST_PINIO)
  2085. TFT_RD_LOW(); // Read line LOW
  2086. if (tft8.wide) { // 16-bit TFT connection
  2087. #if defined(HAS_PORT_SET_CLR)
  2088. *(volatile uint16_t *)tft8.dirClr = 0xFFFF; // Input state
  2089. w = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  2090. *(volatile uint16_t *)tft8.dirSet = 0xFFFF; // Output state
  2091. #else // !HAS_PORT_SET_CLR
  2092. *(volatile uint16_t *)tft8.portDir = 0x0000; // Input state
  2093. w = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  2094. *(volatile uint16_t *)tft8.portDir = 0xFFFF; // Output state
  2095. #endif // end !HAS_PORT_SET_CLR
  2096. }
  2097. TFT_RD_HIGH(); // Read line HIGH
  2098. #else // !USE_FAST_PINIO
  2099. w = 0; // Parallel TFT is NOT SUPPORTED without USE_FAST_PINIO
  2100. #endif // end !USE_FAST_PINIO
  2101. }
  2102. }
  2103. return w;
  2104. }
  2105. /*!
  2106. @brief Set the software (bitbang) SPI MOSI line HIGH.
  2107. */
  2108. inline void Adafruit_SPITFT::SPI_MOSI_HIGH(void) {
  2109. #if defined(USE_FAST_PINIO)
  2110. #if defined(HAS_PORT_SET_CLR)
  2111. #if defined(KINETISK)
  2112. *swspi.mosiPortSet = 1;
  2113. #else // !KINETISK
  2114. *swspi.mosiPortSet = swspi.mosiPinMask;
  2115. #endif
  2116. #else // !HAS_PORT_SET_CLR
  2117. *swspi.mosiPort |= swspi.mosiPinMaskSet;
  2118. #endif // end !HAS_PORT_SET_CLR
  2119. #else // !USE_FAST_PINIO
  2120. digitalWrite(swspi._mosi, HIGH);
  2121. #if defined(ESP32)
  2122. for (volatile uint8_t i = 0; i < 1; i++)
  2123. ;
  2124. #endif // end ESP32
  2125. #endif // end !USE_FAST_PINIO
  2126. }
  2127. /*!
  2128. @brief Set the software (bitbang) SPI MOSI line LOW.
  2129. */
  2130. inline void Adafruit_SPITFT::SPI_MOSI_LOW(void) {
  2131. #if defined(USE_FAST_PINIO)
  2132. #if defined(HAS_PORT_SET_CLR)
  2133. #if defined(KINETISK)
  2134. *swspi.mosiPortClr = 1;
  2135. #else // !KINETISK
  2136. *swspi.mosiPortClr = swspi.mosiPinMask;
  2137. #endif
  2138. #else // !HAS_PORT_SET_CLR
  2139. *swspi.mosiPort &= swspi.mosiPinMaskClr;
  2140. #endif // end !HAS_PORT_SET_CLR
  2141. #else // !USE_FAST_PINIO
  2142. digitalWrite(swspi._mosi, LOW);
  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 SCK line HIGH.
  2151. */
  2152. inline void Adafruit_SPITFT::SPI_SCK_HIGH(void) {
  2153. #if defined(USE_FAST_PINIO)
  2154. #if defined(HAS_PORT_SET_CLR)
  2155. #if defined(KINETISK)
  2156. *swspi.sckPortSet = 1;
  2157. #else // !KINETISK
  2158. *swspi.sckPortSet = swspi.sckPinMask;
  2159. #if defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
  2160. for (volatile uint8_t i = 0; i < 1; i++)
  2161. ;
  2162. #endif
  2163. #endif
  2164. #else // !HAS_PORT_SET_CLR
  2165. *swspi.sckPort |= swspi.sckPinMaskSet;
  2166. #endif // end !HAS_PORT_SET_CLR
  2167. #else // !USE_FAST_PINIO
  2168. digitalWrite(swspi._sck, HIGH);
  2169. #if defined(ESP32)
  2170. for (volatile uint8_t i = 0; i < 1; i++)
  2171. ;
  2172. #endif // end ESP32
  2173. #endif // end !USE_FAST_PINIO
  2174. }
  2175. /*!
  2176. @brief Set the software (bitbang) SPI SCK line LOW.
  2177. */
  2178. inline void Adafruit_SPITFT::SPI_SCK_LOW(void) {
  2179. #if defined(USE_FAST_PINIO)
  2180. #if defined(HAS_PORT_SET_CLR)
  2181. #if defined(KINETISK)
  2182. *swspi.sckPortClr = 1;
  2183. #else // !KINETISK
  2184. *swspi.sckPortClr = swspi.sckPinMask;
  2185. #if defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
  2186. for (volatile uint8_t i = 0; i < 1; i++)
  2187. ;
  2188. #endif
  2189. #endif
  2190. #else // !HAS_PORT_SET_CLR
  2191. *swspi.sckPort &= swspi.sckPinMaskClr;
  2192. #endif // end !HAS_PORT_SET_CLR
  2193. #else // !USE_FAST_PINIO
  2194. digitalWrite(swspi._sck, LOW);
  2195. #if defined(ESP32)
  2196. for (volatile uint8_t i = 0; i < 1; i++)
  2197. ;
  2198. #endif // end ESP32
  2199. #endif // end !USE_FAST_PINIO
  2200. }
  2201. /*!
  2202. @brief Read the state of the software (bitbang) SPI MISO line.
  2203. @return true if HIGH, false if LOW.
  2204. */
  2205. inline bool Adafruit_SPITFT::SPI_MISO_READ(void) {
  2206. #if defined(USE_FAST_PINIO)
  2207. #if defined(KINETISK)
  2208. return *swspi.misoPort;
  2209. #else // !KINETISK
  2210. return *swspi.misoPort & swspi.misoPinMask;
  2211. #endif // end !KINETISK
  2212. #else // !USE_FAST_PINIO
  2213. return digitalRead(swspi._miso);
  2214. #endif // end !USE_FAST_PINIO
  2215. }
  2216. /*!
  2217. @brief Issue a single 16-bit value to the display. Chip-select,
  2218. transaction and data/command selection must have been
  2219. previously set -- this ONLY issues the word. Despite the name,
  2220. this function is used even if display connection is parallel;
  2221. name was maintaned for backward compatibility. Naming is also
  2222. not consistent with the 8-bit version, spiWrite(). Sorry about
  2223. that. Again, staying compatible with outside code.
  2224. @param w 16-bit value to write.
  2225. */
  2226. void Adafruit_SPITFT::SPI_WRITE16(uint16_t w) {
  2227. if (connection == TFT_HARD_SPI) {
  2228. #if defined(__AVR__)
  2229. AVR_WRITESPI(w >> 8);
  2230. AVR_WRITESPI(w);
  2231. #elif defined(ESP8266) || defined(ESP32)
  2232. hwspi._spi->write16(w);
  2233. #else
  2234. hwspi._spi->transfer(w >> 8);
  2235. hwspi._spi->transfer(w);
  2236. #endif
  2237. } else if (connection == TFT_SOFT_SPI) {
  2238. for (uint8_t bit = 0; bit < 16; bit++) {
  2239. if (w & 0x8000)
  2240. SPI_MOSI_HIGH();
  2241. else
  2242. SPI_MOSI_LOW();
  2243. SPI_SCK_HIGH();
  2244. SPI_SCK_LOW();
  2245. w <<= 1;
  2246. }
  2247. } else { // TFT_PARALLEL
  2248. #if defined(__AVR__)
  2249. *tft8.writePort = w >> 8;
  2250. TFT_WR_STROBE();
  2251. *tft8.writePort = w;
  2252. #elif defined(USE_FAST_PINIO)
  2253. if (!tft8.wide) {
  2254. *tft8.writePort = w >> 8;
  2255. TFT_WR_STROBE();
  2256. *tft8.writePort = w;
  2257. } else {
  2258. *(volatile uint16_t *)tft8.writePort = w;
  2259. }
  2260. #endif
  2261. TFT_WR_STROBE();
  2262. }
  2263. }
  2264. /*!
  2265. @brief Issue a single 32-bit value to the display. Chip-select,
  2266. transaction and data/command selection must have been
  2267. previously set -- this ONLY issues the longword. Despite the
  2268. name, this function is used even if display connection is
  2269. parallel; name was maintaned for backward compatibility. Naming
  2270. is also not consistent with the 8-bit version, spiWrite().
  2271. Sorry about that. Again, staying compatible with outside code.
  2272. @param l 32-bit value to write.
  2273. */
  2274. void Adafruit_SPITFT::SPI_WRITE32(uint32_t l) {
  2275. if (connection == TFT_HARD_SPI) {
  2276. #if defined(__AVR__)
  2277. AVR_WRITESPI(l >> 24);
  2278. AVR_WRITESPI(l >> 16);
  2279. AVR_WRITESPI(l >> 8);
  2280. AVR_WRITESPI(l);
  2281. #elif defined(ESP8266) || defined(ESP32)
  2282. hwspi._spi->write32(l);
  2283. #else
  2284. hwspi._spi->transfer(l >> 24);
  2285. hwspi._spi->transfer(l >> 16);
  2286. hwspi._spi->transfer(l >> 8);
  2287. hwspi._spi->transfer(l);
  2288. #endif
  2289. } else if (connection == TFT_SOFT_SPI) {
  2290. for (uint8_t bit = 0; bit < 32; bit++) {
  2291. if (l & 0x80000000)
  2292. SPI_MOSI_HIGH();
  2293. else
  2294. SPI_MOSI_LOW();
  2295. SPI_SCK_HIGH();
  2296. SPI_SCK_LOW();
  2297. l <<= 1;
  2298. }
  2299. } else { // TFT_PARALLEL
  2300. #if defined(__AVR__)
  2301. *tft8.writePort = l >> 24;
  2302. TFT_WR_STROBE();
  2303. *tft8.writePort = l >> 16;
  2304. TFT_WR_STROBE();
  2305. *tft8.writePort = l >> 8;
  2306. TFT_WR_STROBE();
  2307. *tft8.writePort = l;
  2308. #elif defined(USE_FAST_PINIO)
  2309. if (!tft8.wide) {
  2310. *tft8.writePort = l >> 24;
  2311. TFT_WR_STROBE();
  2312. *tft8.writePort = l >> 16;
  2313. TFT_WR_STROBE();
  2314. *tft8.writePort = l >> 8;
  2315. TFT_WR_STROBE();
  2316. *tft8.writePort = l;
  2317. } else {
  2318. *(volatile uint16_t *)tft8.writePort = l >> 16;
  2319. TFT_WR_STROBE();
  2320. *(volatile uint16_t *)tft8.writePort = l;
  2321. }
  2322. #endif
  2323. TFT_WR_STROBE();
  2324. }
  2325. }
  2326. /*!
  2327. @brief Set the WR line LOW, then HIGH. Used for parallel-connected
  2328. interfaces when writing data.
  2329. */
  2330. inline void Adafruit_SPITFT::TFT_WR_STROBE(void) {
  2331. #if defined(USE_FAST_PINIO)
  2332. #if defined(HAS_PORT_SET_CLR)
  2333. #if defined(KINETISK)
  2334. *tft8.wrPortClr = 1;
  2335. *tft8.wrPortSet = 1;
  2336. #else // !KINETISK
  2337. *tft8.wrPortClr = tft8.wrPinMask;
  2338. *tft8.wrPortSet = tft8.wrPinMask;
  2339. #endif // end !KINETISK
  2340. #else // !HAS_PORT_SET_CLR
  2341. *tft8.wrPort &= tft8.wrPinMaskClr;
  2342. *tft8.wrPort |= tft8.wrPinMaskSet;
  2343. #endif // end !HAS_PORT_SET_CLR
  2344. #else // !USE_FAST_PINIO
  2345. digitalWrite(tft8._wr, LOW);
  2346. digitalWrite(tft8._wr, HIGH);
  2347. #endif // end !USE_FAST_PINIO
  2348. }
  2349. /*!
  2350. @brief Set the RD line HIGH. Used for parallel-connected interfaces
  2351. when reading data.
  2352. */
  2353. inline void Adafruit_SPITFT::TFT_RD_HIGH(void) {
  2354. #if defined(USE_FAST_PINIO)
  2355. #if defined(HAS_PORT_SET_CLR)
  2356. *tft8.rdPortSet = tft8.rdPinMask;
  2357. #else // !HAS_PORT_SET_CLR
  2358. *tft8.rdPort |= tft8.rdPinMaskSet;
  2359. #endif // end !HAS_PORT_SET_CLR
  2360. #else // !USE_FAST_PINIO
  2361. digitalWrite(tft8._rd, HIGH);
  2362. #endif // end !USE_FAST_PINIO
  2363. }
  2364. /*!
  2365. @brief Set the RD line LOW. Used for parallel-connected interfaces
  2366. when reading data.
  2367. */
  2368. inline void Adafruit_SPITFT::TFT_RD_LOW(void) {
  2369. #if defined(USE_FAST_PINIO)
  2370. #if defined(HAS_PORT_SET_CLR)
  2371. *tft8.rdPortClr = tft8.rdPinMask;
  2372. #else // !HAS_PORT_SET_CLR
  2373. *tft8.rdPort &= tft8.rdPinMaskClr;
  2374. #endif // end !HAS_PORT_SET_CLR
  2375. #else // !USE_FAST_PINIO
  2376. digitalWrite(tft8._rd, LOW);
  2377. #endif // end !USE_FAST_PINIO
  2378. }
  2379. #endif // end __AVR_ATtiny85__