Adafruit_SPITFT.cpp 95 KB

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