Adafruit_SPITFT.cpp 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572
  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(ARDUINO_ARCH_RP2040)
  969. spi_inst_t *pi_spi = hwspi._spi == &SPI ? spi0 : spi1;
  970. if (!bigEndian) {
  971. // switch to 16-bit writes
  972. hw_write_masked(&spi_get_hw(pi_spi)->cr0, 15 << SPI_SSPCR0_DSS_LSB,
  973. SPI_SSPCR0_DSS_BITS);
  974. spi_write16_blocking(pi_spi, colors, len);
  975. // switch back to 8-bit
  976. hw_write_masked(&spi_get_hw(pi_spi)->cr0, 7 << SPI_SSPCR0_DSS_LSB,
  977. SPI_SSPCR0_DSS_BITS);
  978. } else {
  979. spi_write_blocking(pi_spi, (uint8_t *)colors, len * 2);
  980. }
  981. return;
  982. #elif defined(USE_SPI_DMA) && \
  983. (defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO))
  984. if ((connection == TFT_HARD_SPI) || (connection == TFT_PARALLEL)) {
  985. int maxSpan = maxFillLen / 2; // One scanline max
  986. uint8_t pixelBufIdx = 0; // Active pixel buffer number
  987. #if defined(__SAMD51__)
  988. if (connection == TFT_PARALLEL) {
  989. // Switch WR pin to PWM or CCL
  990. pinPeripheral(tft8._wr, wrPeripheral);
  991. }
  992. #endif // end __SAMD51__
  993. if (!bigEndian) { // Normal little-endian situation...
  994. while (len) {
  995. int count = (len < maxSpan) ? len : maxSpan;
  996. // Because TFT and SAMD endianisms are different, must swap
  997. // bytes from the 'colors' array passed into a DMA working
  998. // buffer. This can take place while the prior DMA transfer
  999. // is in progress, hence the need for two pixelBufs.
  1000. swapBytes(colors, count, pixelBuf[pixelBufIdx]);
  1001. colors += count;
  1002. // The transfers themselves are relatively small, so we don't
  1003. // need a long descriptor list. We just alternate between the
  1004. // first two, sharing pixelBufIdx for that purpose.
  1005. descriptor[pixelBufIdx].SRCADDR.reg =
  1006. (uint32_t)pixelBuf[pixelBufIdx] + count * 2;
  1007. descriptor[pixelBufIdx].BTCTRL.bit.SRCINC = 1;
  1008. descriptor[pixelBufIdx].BTCNT.reg = count * 2;
  1009. descriptor[pixelBufIdx].DESCADDR.reg = 0;
  1010. while (dma_busy)
  1011. ; // Wait for prior line to finish
  1012. // Move new descriptor into place...
  1013. memcpy(dptr, &descriptor[pixelBufIdx], sizeof(DmacDescriptor));
  1014. dma_busy = true;
  1015. dma.startJob(); // Trigger SPI DMA transfer
  1016. if (connection == TFT_PARALLEL)
  1017. dma.trigger();
  1018. pixelBufIdx = 1 - pixelBufIdx; // Swap DMA pixel buffers
  1019. len -= count;
  1020. }
  1021. } else { // bigEndian == true
  1022. // With big-endian pixel data, this can be handled as a single
  1023. // DMA transfer using chained descriptors. Even full screen, this
  1024. // needs only a relatively short descriptor list, each
  1025. // transferring a max of 32,767 (not 32,768) pixels. The list
  1026. // was allocated large enough to accommodate a full screen's
  1027. // worth of data, so this won't run past the end of the list.
  1028. int d, numDescriptors = (len + 32766) / 32767;
  1029. for (d = 0; d < numDescriptors; d++) {
  1030. int count = (len < 32767) ? len : 32767;
  1031. descriptor[d].SRCADDR.reg = (uint32_t)colors + count * 2;
  1032. descriptor[d].BTCTRL.bit.SRCINC = 1;
  1033. descriptor[d].BTCNT.reg = count * 2;
  1034. descriptor[d].DESCADDR.reg = (uint32_t)&descriptor[d + 1];
  1035. len -= count;
  1036. colors += count;
  1037. }
  1038. descriptor[d - 1].DESCADDR.reg = 0;
  1039. while (dma_busy)
  1040. ; // Wait for prior transfer (if any) to finish
  1041. // Move first descriptor into place and start transfer...
  1042. memcpy(dptr, &descriptor[0], sizeof(DmacDescriptor));
  1043. dma_busy = true;
  1044. dma.startJob(); // Trigger SPI DMA transfer
  1045. if (connection == TFT_PARALLEL)
  1046. dma.trigger();
  1047. } // end bigEndian
  1048. lastFillColor = 0x0000; // pixelBuf has been sullied
  1049. lastFillLen = 0;
  1050. if (block) {
  1051. while (dma_busy)
  1052. ; // Wait for last line to complete
  1053. #if defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO)
  1054. if (connection == TFT_HARD_SPI) {
  1055. // See SAMD51/21 note in writeColor()
  1056. hwspi._spi->setDataMode(hwspi._mode);
  1057. } else {
  1058. pinPeripheral(tft8._wr, PIO_OUTPUT); // Switch WR back to GPIO
  1059. }
  1060. #endif // end __SAMD51__ || ARDUINO_SAMD_ZERO
  1061. }
  1062. return;
  1063. }
  1064. #endif // end USE_SPI_DMA
  1065. // All other cases (bitbang SPI or non-DMA hard SPI or parallel),
  1066. // use a loop with the normal 16-bit data write function:
  1067. if (!bigEndian) {
  1068. while (len--) {
  1069. SPI_WRITE16(*colors++);
  1070. }
  1071. } else {
  1072. // Well this is awkward. SPI_WRITE16() was designed for little-endian
  1073. // hosts and big-endian displays as that's nearly always the typical
  1074. // case. If the bigEndian flag was set, data is already in display's
  1075. // order...so each pixel needs byte-swapping before being issued.
  1076. // Rather than having a separate big-endian SPI_WRITE16 (adding more
  1077. // bloat), it's preferred if calling function is smart and only uses
  1078. // bigEndian where DMA is supported. But we gotta handle this...
  1079. while (len--) {
  1080. SPI_WRITE16(__builtin_bswap16(*colors++));
  1081. }
  1082. }
  1083. }
  1084. /*!
  1085. @brief Wait for the last DMA transfer in a prior non-blocking
  1086. writePixels() call to complete. This does nothing if DMA
  1087. is not enabled, and is not needed if blocking writePixels()
  1088. was used (as is the default case).
  1089. */
  1090. void Adafruit_SPITFT::dmaWait(void) {
  1091. #if defined(USE_SPI_DMA) && (defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO))
  1092. while (dma_busy)
  1093. ;
  1094. #if defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO)
  1095. if (connection == TFT_HARD_SPI) {
  1096. // See SAMD51/21 note in writeColor()
  1097. hwspi._spi->setDataMode(hwspi._mode);
  1098. } else {
  1099. pinPeripheral(tft8._wr, PIO_OUTPUT); // Switch WR back to GPIO
  1100. }
  1101. #endif // end __SAMD51__ || ARDUINO_SAMD_ZERO
  1102. #endif
  1103. }
  1104. /*!
  1105. @brief Issue a series of pixels, all the same color. Not self-
  1106. contained; should follow startWrite() and setAddrWindow() calls.
  1107. @param color 16-bit pixel color in '565' RGB format.
  1108. @param len Number of pixels to draw.
  1109. */
  1110. void Adafruit_SPITFT::writeColor(uint16_t color, uint32_t len) {
  1111. if (!len)
  1112. return; // Avoid 0-byte transfers
  1113. uint8_t hi = color >> 8, lo = color;
  1114. #if defined(ESP32) // ESP32 has a special SPI pixel-writing function...
  1115. if (connection == TFT_HARD_SPI) {
  1116. #define SPI_MAX_PIXELS_AT_ONCE 32
  1117. #define TMPBUF_LONGWORDS (SPI_MAX_PIXELS_AT_ONCE + 1) / 2
  1118. #define TMPBUF_PIXELS (TMPBUF_LONGWORDS * 2)
  1119. static uint32_t temp[TMPBUF_LONGWORDS];
  1120. uint32_t c32 = color * 0x00010001;
  1121. uint16_t bufLen = (len < TMPBUF_PIXELS) ? len : TMPBUF_PIXELS, xferLen,
  1122. fillLen;
  1123. // Fill temp buffer 32 bits at a time
  1124. fillLen = (bufLen + 1) / 2; // Round up to next 32-bit boundary
  1125. for (uint32_t t = 0; t < fillLen; t++) {
  1126. temp[t] = c32;
  1127. }
  1128. // Issue pixels in blocks from temp buffer
  1129. while (len) { // While pixels remain
  1130. xferLen = (bufLen < len) ? bufLen : len; // How many this pass?
  1131. writePixels((uint16_t *)temp, xferLen);
  1132. len -= xferLen;
  1133. }
  1134. return;
  1135. }
  1136. #elif defined(ARDUINO_NRF52_ADAFRUIT) && \
  1137. defined(NRF52840_XXAA) // Adafruit nRF52840 use SPIM3 DMA at 32Mhz
  1138. // at most 2 scan lines
  1139. uint32_t const pixbufcount = min(len, ((uint32_t)2 * width()));
  1140. uint16_t *pixbuf = (uint16_t *)rtos_malloc(2 * pixbufcount);
  1141. // use SPI3 DMA if we could allocate buffer, else fall back to writing each
  1142. // pixel loop below
  1143. if (pixbuf) {
  1144. uint16_t const swap_color = __builtin_bswap16(color);
  1145. // fill buffer with color
  1146. for (uint32_t i = 0; i < pixbufcount; i++) {
  1147. pixbuf[i] = swap_color;
  1148. }
  1149. while (len) {
  1150. uint32_t const count = min(len, pixbufcount);
  1151. writePixels(pixbuf, count, true, true);
  1152. len -= count;
  1153. }
  1154. rtos_free(pixbuf);
  1155. return;
  1156. }
  1157. #else // !ESP32
  1158. #if defined(USE_SPI_DMA) && (defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO))
  1159. if (((connection == TFT_HARD_SPI) || (connection == TFT_PARALLEL)) &&
  1160. (len >= 16)) { // Don't bother with DMA on short pixel runs
  1161. int i, d, numDescriptors;
  1162. if (hi == lo) { // If high & low bytes are same...
  1163. onePixelBuf = color;
  1164. // Can do this with a relatively short descriptor list,
  1165. // each transferring a max of 32,767 (not 32,768) pixels.
  1166. // This won't run off the end of the allocated descriptor list,
  1167. // since we're using much larger chunks per descriptor here.
  1168. numDescriptors = (len + 32766) / 32767;
  1169. for (d = 0; d < numDescriptors; d++) {
  1170. int count = (len < 32767) ? len : 32767;
  1171. descriptor[d].SRCADDR.reg = (uint32_t)&onePixelBuf;
  1172. descriptor[d].BTCTRL.bit.SRCINC = 0;
  1173. descriptor[d].BTCNT.reg = count * 2;
  1174. descriptor[d].DESCADDR.reg = (uint32_t)&descriptor[d + 1];
  1175. len -= count;
  1176. }
  1177. descriptor[d - 1].DESCADDR.reg = 0;
  1178. } else {
  1179. // If high and low bytes are distinct, it's necessary to fill
  1180. // a buffer with pixel data (swapping high and low bytes because
  1181. // TFT and SAMD are different endianisms) and create a longer
  1182. // descriptor list pointing repeatedly to this data. We can do
  1183. // this slightly faster working 2 pixels (32 bits) at a time.
  1184. uint32_t *pixelPtr = (uint32_t *)pixelBuf[0],
  1185. twoPixels = __builtin_bswap16(color) * 0x00010001;
  1186. // We can avoid some or all of the buffer-filling if the color
  1187. // is the same as last time...
  1188. if (color == lastFillColor) {
  1189. // If length is longer than prior instance, fill only the
  1190. // additional pixels in the buffer and update lastFillLen.
  1191. if (len > lastFillLen) {
  1192. int fillStart = lastFillLen / 2,
  1193. fillEnd = (((len < maxFillLen) ? len : maxFillLen) + 1) / 2;
  1194. for (i = fillStart; i < fillEnd; i++)
  1195. pixelPtr[i] = twoPixels;
  1196. lastFillLen = fillEnd * 2;
  1197. } // else do nothing, don't set pixels or change lastFillLen
  1198. } else {
  1199. int fillEnd = (((len < maxFillLen) ? len : maxFillLen) + 1) / 2;
  1200. for (i = 0; i < fillEnd; i++)
  1201. pixelPtr[i] = twoPixels;
  1202. lastFillLen = fillEnd * 2;
  1203. lastFillColor = color;
  1204. }
  1205. numDescriptors = (len + maxFillLen - 1) / maxFillLen;
  1206. for (d = 0; d < numDescriptors; d++) {
  1207. int pixels = (len < maxFillLen) ? len : maxFillLen, bytes = pixels * 2;
  1208. descriptor[d].SRCADDR.reg = (uint32_t)pixelPtr + bytes;
  1209. descriptor[d].BTCTRL.bit.SRCINC = 1;
  1210. descriptor[d].BTCNT.reg = bytes;
  1211. descriptor[d].DESCADDR.reg = (uint32_t)&descriptor[d + 1];
  1212. len -= pixels;
  1213. }
  1214. descriptor[d - 1].DESCADDR.reg = 0;
  1215. }
  1216. memcpy(dptr, &descriptor[0], sizeof(DmacDescriptor));
  1217. #if defined(__SAMD51__)
  1218. if (connection == TFT_PARALLEL) {
  1219. // Switch WR pin to PWM or CCL
  1220. pinPeripheral(tft8._wr, wrPeripheral);
  1221. }
  1222. #endif // end __SAMD51__
  1223. dma_busy = true;
  1224. dma.startJob();
  1225. if (connection == TFT_PARALLEL)
  1226. dma.trigger();
  1227. while (dma_busy)
  1228. ; // Wait for completion
  1229. // Unfortunately blocking is necessary. An earlier version returned
  1230. // immediately and checked dma_busy on startWrite() instead, but it
  1231. // turns out to be MUCH slower on many graphics operations (as when
  1232. // drawing lines, pixel-by-pixel), perhaps because it's a volatile
  1233. // type and doesn't cache. Working on this.
  1234. #if defined(__SAMD51__) || defined(ARDUINO_SAMD_ZERO)
  1235. if (connection == TFT_HARD_SPI) {
  1236. // SAMD51: SPI DMA seems to leave the SPI peripheral in a freaky
  1237. // state on completion. Workaround is to explicitly set it back...
  1238. // (5/17/2019: apparently SAMD21 too, in certain cases, observed
  1239. // with ST7789 display.)
  1240. hwspi._spi->setDataMode(hwspi._mode);
  1241. } else {
  1242. pinPeripheral(tft8._wr, PIO_OUTPUT); // Switch WR back to GPIO
  1243. }
  1244. #endif // end __SAMD51__
  1245. return;
  1246. }
  1247. #endif // end USE_SPI_DMA
  1248. #endif // end !ESP32
  1249. // All other cases (non-DMA hard SPI, bitbang SPI, parallel)...
  1250. if (connection == TFT_HARD_SPI) {
  1251. #if defined(ESP8266)
  1252. do {
  1253. uint32_t pixelsThisPass = len;
  1254. if (pixelsThisPass > 50000)
  1255. pixelsThisPass = 50000;
  1256. len -= pixelsThisPass;
  1257. yield(); // Periodic yield() on long fills
  1258. while (pixelsThisPass--) {
  1259. hwspi._spi->write(hi);
  1260. hwspi._spi->write(lo);
  1261. }
  1262. } while (len);
  1263. #elif defined(ARDUINO_ARCH_RP2040)
  1264. spi_inst_t *pi_spi = hwspi._spi == &SPI ? spi0 : spi1;
  1265. color = __builtin_bswap16(color);
  1266. while (len--)
  1267. spi_write_blocking(pi_spi, (uint8_t *)&color, 2);
  1268. #else // !ESP8266 && !ARDUINO_ARCH_RP2040
  1269. while (len--) {
  1270. #if defined(__AVR__)
  1271. AVR_WRITESPI(hi);
  1272. AVR_WRITESPI(lo);
  1273. #elif defined(ESP32)
  1274. hwspi._spi->write(hi);
  1275. hwspi._spi->write(lo);
  1276. #else
  1277. hwspi._spi->transfer(hi);
  1278. hwspi._spi->transfer(lo);
  1279. #endif
  1280. }
  1281. #endif // end !ESP8266
  1282. } else if (connection == TFT_SOFT_SPI) {
  1283. #if defined(ESP8266)
  1284. do {
  1285. uint32_t pixelsThisPass = len;
  1286. if (pixelsThisPass > 20000)
  1287. pixelsThisPass = 20000;
  1288. len -= pixelsThisPass;
  1289. yield(); // Periodic yield() on long fills
  1290. while (pixelsThisPass--) {
  1291. for (uint16_t bit = 0, x = color; bit < 16; bit++) {
  1292. if (x & 0x8000)
  1293. SPI_MOSI_HIGH();
  1294. else
  1295. SPI_MOSI_LOW();
  1296. SPI_SCK_HIGH();
  1297. SPI_SCK_LOW();
  1298. x <<= 1;
  1299. }
  1300. }
  1301. } while (len);
  1302. #else // !ESP8266
  1303. while (len--) {
  1304. #if defined(__AVR__)
  1305. for (uint8_t bit = 0, x = hi; bit < 8; bit++) {
  1306. if (x & 0x80)
  1307. SPI_MOSI_HIGH();
  1308. else
  1309. SPI_MOSI_LOW();
  1310. SPI_SCK_HIGH();
  1311. SPI_SCK_LOW();
  1312. x <<= 1;
  1313. }
  1314. for (uint8_t bit = 0, x = lo; bit < 8; bit++) {
  1315. if (x & 0x80)
  1316. SPI_MOSI_HIGH();
  1317. else
  1318. SPI_MOSI_LOW();
  1319. SPI_SCK_HIGH();
  1320. SPI_SCK_LOW();
  1321. x <<= 1;
  1322. }
  1323. #else // !__AVR__
  1324. for (uint16_t bit = 0, x = color; bit < 16; bit++) {
  1325. if (x & 0x8000)
  1326. SPI_MOSI_HIGH();
  1327. else
  1328. SPI_MOSI_LOW();
  1329. SPI_SCK_HIGH();
  1330. x <<= 1;
  1331. SPI_SCK_LOW();
  1332. }
  1333. #endif // end !__AVR__
  1334. }
  1335. #endif // end !ESP8266
  1336. } else { // PARALLEL
  1337. if (hi == lo) {
  1338. #if defined(__AVR__)
  1339. len *= 2;
  1340. *tft8.writePort = hi;
  1341. while (len--) {
  1342. TFT_WR_STROBE();
  1343. }
  1344. #elif defined(USE_FAST_PINIO)
  1345. if (!tft8.wide) {
  1346. len *= 2;
  1347. *tft8.writePort = hi;
  1348. } else {
  1349. *(volatile uint16_t *)tft8.writePort = color;
  1350. }
  1351. while (len--) {
  1352. TFT_WR_STROBE();
  1353. }
  1354. #endif
  1355. } else {
  1356. while (len--) {
  1357. #if defined(__AVR__)
  1358. *tft8.writePort = hi;
  1359. TFT_WR_STROBE();
  1360. *tft8.writePort = lo;
  1361. #elif defined(USE_FAST_PINIO)
  1362. if (!tft8.wide) {
  1363. *tft8.writePort = hi;
  1364. TFT_WR_STROBE();
  1365. *tft8.writePort = lo;
  1366. } else {
  1367. *(volatile uint16_t *)tft8.writePort = color;
  1368. }
  1369. #endif
  1370. TFT_WR_STROBE();
  1371. }
  1372. }
  1373. }
  1374. }
  1375. /*!
  1376. @brief Draw a filled rectangle to the display. Not self-contained;
  1377. should follow startWrite(). Typically used by higher-level
  1378. graphics primitives; user code shouldn't need to call this and
  1379. is likely to use the self-contained fillRect() instead.
  1380. writeFillRect() performs its own edge clipping and rejection;
  1381. see writeFillRectPreclipped() for a more 'raw' implementation.
  1382. @param x Horizontal position of first corner.
  1383. @param y Vertical position of first corner.
  1384. @param w Rectangle width in pixels (positive = right of first
  1385. corner, negative = left of first corner).
  1386. @param h Rectangle height in pixels (positive = below first
  1387. corner, negative = above first corner).
  1388. @param color 16-bit fill color in '565' RGB format.
  1389. @note Written in this deep-nested way because C by definition will
  1390. optimize for the 'if' case, not the 'else' -- avoids branches
  1391. and rejects clipped rectangles at the least-work possibility.
  1392. */
  1393. void Adafruit_SPITFT::writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h,
  1394. uint16_t color) {
  1395. if (w && h) { // Nonzero width and height?
  1396. if (w < 0) { // If negative width...
  1397. x += w + 1; // Move X to left edge
  1398. w = -w; // Use positive width
  1399. }
  1400. if (x < _width) { // Not off right
  1401. if (h < 0) { // If negative height...
  1402. y += h + 1; // Move Y to top edge
  1403. h = -h; // Use positive height
  1404. }
  1405. if (y < _height) { // Not off bottom
  1406. int16_t x2 = x + w - 1;
  1407. if (x2 >= 0) { // Not off left
  1408. int16_t y2 = y + h - 1;
  1409. if (y2 >= 0) { // Not off top
  1410. // Rectangle partly or fully overlaps screen
  1411. if (x < 0) {
  1412. x = 0;
  1413. w = x2 + 1;
  1414. } // Clip left
  1415. if (y < 0) {
  1416. y = 0;
  1417. h = y2 + 1;
  1418. } // Clip top
  1419. if (x2 >= _width) {
  1420. w = _width - x;
  1421. } // Clip right
  1422. if (y2 >= _height) {
  1423. h = _height - y;
  1424. } // Clip bottom
  1425. writeFillRectPreclipped(x, y, w, h, color);
  1426. }
  1427. }
  1428. }
  1429. }
  1430. }
  1431. }
  1432. /*!
  1433. @brief Draw a horizontal line on the display. Performs edge clipping
  1434. and rejection. Not self-contained; should follow startWrite().
  1435. Typically used by higher-level graphics primitives; user code
  1436. shouldn't need to call this and is likely to use the self-
  1437. contained drawFastHLine() instead.
  1438. @param x Horizontal position of first point.
  1439. @param y Vertical position of first point.
  1440. @param w Line width in pixels (positive = right of first point,
  1441. negative = point of first corner).
  1442. @param color 16-bit line color in '565' RGB format.
  1443. */
  1444. void inline Adafruit_SPITFT::writeFastHLine(int16_t x, int16_t y, int16_t w,
  1445. uint16_t color) {
  1446. if ((y >= 0) && (y < _height) && w) { // Y on screen, nonzero width
  1447. if (w < 0) { // If negative width...
  1448. x += w + 1; // Move X to left edge
  1449. w = -w; // Use positive width
  1450. }
  1451. if (x < _width) { // Not off right
  1452. int16_t x2 = x + w - 1;
  1453. if (x2 >= 0) { // Not off left
  1454. // Line partly or fully overlaps screen
  1455. if (x < 0) {
  1456. x = 0;
  1457. w = x2 + 1;
  1458. } // Clip left
  1459. if (x2 >= _width) {
  1460. w = _width - x;
  1461. } // Clip right
  1462. writeFillRectPreclipped(x, y, w, 1, color);
  1463. }
  1464. }
  1465. }
  1466. }
  1467. /*!
  1468. @brief Draw a vertical line on the display. Performs edge clipping and
  1469. rejection. Not self-contained; should follow startWrite().
  1470. Typically used by higher-level graphics primitives; user code
  1471. shouldn't need to call this and is likely to use the self-
  1472. contained drawFastVLine() instead.
  1473. @param x Horizontal position of first point.
  1474. @param y Vertical position of first point.
  1475. @param h Line height in pixels (positive = below first point,
  1476. negative = above first point).
  1477. @param color 16-bit line color in '565' RGB format.
  1478. */
  1479. void inline Adafruit_SPITFT::writeFastVLine(int16_t x, int16_t y, int16_t h,
  1480. uint16_t color) {
  1481. if ((x >= 0) && (x < _width) && h) { // X on screen, nonzero height
  1482. if (h < 0) { // If negative height...
  1483. y += h + 1; // Move Y to top edge
  1484. h = -h; // Use positive height
  1485. }
  1486. if (y < _height) { // Not off bottom
  1487. int16_t y2 = y + h - 1;
  1488. if (y2 >= 0) { // Not off top
  1489. // Line partly or fully overlaps screen
  1490. if (y < 0) {
  1491. y = 0;
  1492. h = y2 + 1;
  1493. } // Clip top
  1494. if (y2 >= _height) {
  1495. h = _height - y;
  1496. } // Clip bottom
  1497. writeFillRectPreclipped(x, y, 1, h, color);
  1498. }
  1499. }
  1500. }
  1501. }
  1502. /*!
  1503. @brief A lower-level version of writeFillRect(). This version requires
  1504. all inputs are in-bounds, that width and height are positive,
  1505. and no part extends offscreen. NO EDGE CLIPPING OR REJECTION IS
  1506. PERFORMED. If higher-level graphics primitives are written to
  1507. handle their own clipping earlier in the drawing process, this
  1508. can avoid unnecessary function calls and repeated clipping
  1509. operations in the lower-level functions.
  1510. @param x Horizontal position of first corner. MUST BE WITHIN
  1511. SCREEN BOUNDS.
  1512. @param y Vertical position of first corner. MUST BE WITHIN SCREEN
  1513. BOUNDS.
  1514. @param w Rectangle width in pixels. MUST BE POSITIVE AND NOT
  1515. EXTEND OFF SCREEN.
  1516. @param h Rectangle height in pixels. MUST BE POSITIVE AND NOT
  1517. EXTEND OFF SCREEN.
  1518. @param color 16-bit fill color in '565' RGB format.
  1519. @note This is a new function, no graphics primitives besides rects
  1520. and horizontal/vertical lines are written to best use this yet.
  1521. */
  1522. inline void Adafruit_SPITFT::writeFillRectPreclipped(int16_t x, int16_t y,
  1523. int16_t w, int16_t h,
  1524. uint16_t color) {
  1525. setAddrWindow(x, y, w, h);
  1526. writeColor(color, (uint32_t)w * h);
  1527. }
  1528. // -------------------------------------------------------------------------
  1529. // Ever-so-slightly higher-level graphics operations. Similar to the 'write'
  1530. // functions above, but these contain their own chip-select and SPI
  1531. // transactions as needed (via startWrite(), endWrite()). They're typically
  1532. // used solo -- as graphics primitives in themselves, not invoked by higher-
  1533. // level primitives (which should use the functions above for better
  1534. // performance).
  1535. /*!
  1536. @brief Draw a single pixel to the display at requested coordinates.
  1537. Self-contained and provides its own transaction as needed
  1538. (see writePixel(x,y,color) for a lower-level variant).
  1539. Edge clipping is performed here.
  1540. @param x Horizontal position (0 = left).
  1541. @param y Vertical position (0 = top).
  1542. @param color 16-bit pixel color in '565' RGB format.
  1543. */
  1544. void Adafruit_SPITFT::drawPixel(int16_t x, int16_t y, uint16_t color) {
  1545. // Clip first...
  1546. if ((x >= 0) && (x < _width) && (y >= 0) && (y < _height)) {
  1547. // THEN set up transaction (if needed) and draw...
  1548. startWrite();
  1549. setAddrWindow(x, y, 1, 1);
  1550. SPI_WRITE16(color);
  1551. endWrite();
  1552. }
  1553. }
  1554. /*!
  1555. @brief Draw a filled rectangle to the display. Self-contained and
  1556. provides its own transaction as needed (see writeFillRect() or
  1557. writeFillRectPreclipped() for lower-level variants). Edge
  1558. clipping and rejection is performed here.
  1559. @param x Horizontal position of first corner.
  1560. @param y Vertical position of first corner.
  1561. @param w Rectangle width in pixels (positive = right of first
  1562. corner, negative = left of first corner).
  1563. @param h Rectangle height in pixels (positive = below first
  1564. corner, negative = above first corner).
  1565. @param color 16-bit fill color in '565' RGB format.
  1566. @note This repeats the writeFillRect() function almost in its entirety,
  1567. with the addition of a transaction start/end. It's done this way
  1568. (rather than starting the transaction and calling writeFillRect()
  1569. to handle clipping and so forth) so that the transaction isn't
  1570. performed at all if the rectangle is rejected. It's really not
  1571. that much code.
  1572. */
  1573. void Adafruit_SPITFT::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
  1574. uint16_t color) {
  1575. if (w && h) { // Nonzero width and height?
  1576. if (w < 0) { // If negative width...
  1577. x += w + 1; // Move X to left edge
  1578. w = -w; // Use positive width
  1579. }
  1580. if (x < _width) { // Not off right
  1581. if (h < 0) { // If negative height...
  1582. y += h + 1; // Move Y to top edge
  1583. h = -h; // Use positive height
  1584. }
  1585. if (y < _height) { // Not off bottom
  1586. int16_t x2 = x + w - 1;
  1587. if (x2 >= 0) { // Not off left
  1588. int16_t y2 = y + h - 1;
  1589. if (y2 >= 0) { // Not off top
  1590. // Rectangle partly or fully overlaps screen
  1591. if (x < 0) {
  1592. x = 0;
  1593. w = x2 + 1;
  1594. } // Clip left
  1595. if (y < 0) {
  1596. y = 0;
  1597. h = y2 + 1;
  1598. } // Clip top
  1599. if (x2 >= _width) {
  1600. w = _width - x;
  1601. } // Clip right
  1602. if (y2 >= _height) {
  1603. h = _height - y;
  1604. } // Clip bottom
  1605. startWrite();
  1606. writeFillRectPreclipped(x, y, w, h, color);
  1607. endWrite();
  1608. }
  1609. }
  1610. }
  1611. }
  1612. }
  1613. }
  1614. /*!
  1615. @brief Draw a horizontal line on the display. Self-contained and
  1616. provides its own transaction as needed (see writeFastHLine() for
  1617. a lower-level variant). Edge clipping and rejection is performed
  1618. here.
  1619. @param x Horizontal position of first point.
  1620. @param y Vertical position of first point.
  1621. @param w Line width in pixels (positive = right of first point,
  1622. negative = point of first corner).
  1623. @param color 16-bit line color in '565' RGB format.
  1624. @note This repeats the writeFastHLine() function almost in its
  1625. entirety, with the addition of a transaction start/end. It's
  1626. done this way (rather than starting the transaction and calling
  1627. writeFastHLine() to handle clipping and so forth) so that the
  1628. transaction isn't performed at all if the line is rejected.
  1629. */
  1630. void Adafruit_SPITFT::drawFastHLine(int16_t x, int16_t y, int16_t w,
  1631. uint16_t color) {
  1632. if ((y >= 0) && (y < _height) && w) { // Y on screen, nonzero width
  1633. if (w < 0) { // If negative width...
  1634. x += w + 1; // Move X to left edge
  1635. w = -w; // Use positive width
  1636. }
  1637. if (x < _width) { // Not off right
  1638. int16_t x2 = x + w - 1;
  1639. if (x2 >= 0) { // Not off left
  1640. // Line partly or fully overlaps screen
  1641. if (x < 0) {
  1642. x = 0;
  1643. w = x2 + 1;
  1644. } // Clip left
  1645. if (x2 >= _width) {
  1646. w = _width - x;
  1647. } // Clip right
  1648. startWrite();
  1649. writeFillRectPreclipped(x, y, w, 1, color);
  1650. endWrite();
  1651. }
  1652. }
  1653. }
  1654. }
  1655. /*!
  1656. @brief Draw a vertical line on the display. Self-contained and provides
  1657. its own transaction as needed (see writeFastHLine() for a lower-
  1658. level variant). Edge clipping and rejection is performed here.
  1659. @param x Horizontal position of first point.
  1660. @param y Vertical position of first point.
  1661. @param h Line height in pixels (positive = below first point,
  1662. negative = above first point).
  1663. @param color 16-bit line color in '565' RGB format.
  1664. @note This repeats the writeFastVLine() function almost in its
  1665. entirety, with the addition of a transaction start/end. It's
  1666. done this way (rather than starting the transaction and calling
  1667. writeFastVLine() to handle clipping and so forth) so that the
  1668. transaction isn't performed at all if the line is rejected.
  1669. */
  1670. void Adafruit_SPITFT::drawFastVLine(int16_t x, int16_t y, int16_t h,
  1671. uint16_t color) {
  1672. if ((x >= 0) && (x < _width) && h) { // X on screen, nonzero height
  1673. if (h < 0) { // If negative height...
  1674. y += h + 1; // Move Y to top edge
  1675. h = -h; // Use positive height
  1676. }
  1677. if (y < _height) { // Not off bottom
  1678. int16_t y2 = y + h - 1;
  1679. if (y2 >= 0) { // Not off top
  1680. // Line partly or fully overlaps screen
  1681. if (y < 0) {
  1682. y = 0;
  1683. h = y2 + 1;
  1684. } // Clip top
  1685. if (y2 >= _height) {
  1686. h = _height - y;
  1687. } // Clip bottom
  1688. startWrite();
  1689. writeFillRectPreclipped(x, y, 1, h, color);
  1690. endWrite();
  1691. }
  1692. }
  1693. }
  1694. }
  1695. /*!
  1696. @brief Essentially writePixel() with a transaction around it. I don't
  1697. think this is in use by any of our code anymore (believe it was
  1698. for some older BMP-reading examples), but is kept here in case
  1699. any user code relies on it. Consider it DEPRECATED.
  1700. @param color 16-bit pixel color in '565' RGB format.
  1701. */
  1702. void Adafruit_SPITFT::pushColor(uint16_t color) {
  1703. startWrite();
  1704. SPI_WRITE16(color);
  1705. endWrite();
  1706. }
  1707. /*!
  1708. @brief Draw a 16-bit image (565 RGB) at the specified (x,y) position.
  1709. For 16-bit display devices; no color reduction performed.
  1710. Adapted from https://github.com/PaulStoffregen/ILI9341_t3
  1711. by Marc MERLIN. See examples/pictureEmbed to use this.
  1712. 5/6/2017: function name and arguments have changed for
  1713. compatibility with current GFX library and to avoid naming
  1714. problems in prior implementation. Formerly drawBitmap() with
  1715. arguments in different order. Handles its own transaction and
  1716. edge clipping/rejection.
  1717. @param x Top left corner horizontal coordinate.
  1718. @param y Top left corner vertical coordinate.
  1719. @param pcolors Pointer to 16-bit array of pixel values.
  1720. @param w Width of bitmap in pixels.
  1721. @param h Height of bitmap in pixels.
  1722. */
  1723. void Adafruit_SPITFT::drawRGBBitmap(int16_t x, int16_t y, uint16_t *pcolors,
  1724. int16_t w, int16_t h) {
  1725. int16_t x2, y2; // Lower-right coord
  1726. if ((x >= _width) || // Off-edge right
  1727. (y >= _height) || // " top
  1728. ((x2 = (x + w - 1)) < 0) || // " left
  1729. ((y2 = (y + h - 1)) < 0))
  1730. return; // " bottom
  1731. int16_t bx1 = 0, by1 = 0, // Clipped top-left within bitmap
  1732. saveW = w; // Save original bitmap width value
  1733. if (x < 0) { // Clip left
  1734. w += x;
  1735. bx1 = -x;
  1736. x = 0;
  1737. }
  1738. if (y < 0) { // Clip top
  1739. h += y;
  1740. by1 = -y;
  1741. y = 0;
  1742. }
  1743. if (x2 >= _width)
  1744. w = _width - x; // Clip right
  1745. if (y2 >= _height)
  1746. h = _height - y; // Clip bottom
  1747. pcolors += by1 * saveW + bx1; // Offset bitmap ptr to clipped top-left
  1748. startWrite();
  1749. setAddrWindow(x, y, w, h); // Clipped area
  1750. while (h--) { // For each (clipped) scanline...
  1751. writePixels(pcolors, w); // Push one (clipped) row
  1752. pcolors += saveW; // Advance pointer by one full (unclipped) line
  1753. }
  1754. endWrite();
  1755. }
  1756. // -------------------------------------------------------------------------
  1757. // Miscellaneous class member functions that don't draw anything.
  1758. /*!
  1759. @brief Invert the colors of the display (if supported by hardware).
  1760. Self-contained, no transaction setup required.
  1761. @param i true = inverted display, false = normal display.
  1762. */
  1763. void Adafruit_SPITFT::invertDisplay(bool i) {
  1764. startWrite();
  1765. writeCommand(i ? invertOnCommand : invertOffCommand);
  1766. endWrite();
  1767. }
  1768. /*!
  1769. @brief Given 8-bit red, green and blue values, return a 'packed'
  1770. 16-bit color value in '565' RGB format (5 bits red, 6 bits
  1771. green, 5 bits blue). This is just a mathematical operation,
  1772. no hardware is touched.
  1773. @param red 8-bit red brightnesss (0 = off, 255 = max).
  1774. @param green 8-bit green brightnesss (0 = off, 255 = max).
  1775. @param blue 8-bit blue brightnesss (0 = off, 255 = max).
  1776. @return 'Packed' 16-bit color value (565 format).
  1777. */
  1778. uint16_t Adafruit_SPITFT::color565(uint8_t red, uint8_t green, uint8_t blue) {
  1779. return ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | (blue >> 3);
  1780. }
  1781. /*!
  1782. @brief Adafruit_SPITFT Send Command handles complete sending of commands and
  1783. data
  1784. @param commandByte The Command Byte
  1785. @param dataBytes A pointer to the Data bytes to send
  1786. @param numDataBytes The number of bytes we should send
  1787. */
  1788. void Adafruit_SPITFT::sendCommand(uint8_t commandByte, uint8_t *dataBytes,
  1789. uint8_t numDataBytes) {
  1790. SPI_BEGIN_TRANSACTION();
  1791. if (_cs >= 0)
  1792. SPI_CS_LOW();
  1793. SPI_DC_LOW(); // Command mode
  1794. spiWrite(commandByte); // Send the command byte
  1795. SPI_DC_HIGH();
  1796. for (int i = 0; i < numDataBytes; i++) {
  1797. if ((connection == TFT_PARALLEL) && tft8.wide) {
  1798. SPI_WRITE16(*(uint16_t *)dataBytes);
  1799. dataBytes += 2;
  1800. } else {
  1801. spiWrite(*dataBytes); // Send the data bytes
  1802. dataBytes++;
  1803. }
  1804. }
  1805. if (_cs >= 0)
  1806. SPI_CS_HIGH();
  1807. SPI_END_TRANSACTION();
  1808. }
  1809. /*!
  1810. @brief Adafruit_SPITFT Send Command handles complete sending of commands and
  1811. data
  1812. @param commandByte The Command Byte
  1813. @param dataBytes A pointer to the Data bytes to send
  1814. @param numDataBytes The number of bytes we should send
  1815. */
  1816. void Adafruit_SPITFT::sendCommand(uint8_t commandByte, const uint8_t *dataBytes,
  1817. uint8_t numDataBytes) {
  1818. SPI_BEGIN_TRANSACTION();
  1819. if (_cs >= 0)
  1820. SPI_CS_LOW();
  1821. SPI_DC_LOW(); // Command mode
  1822. spiWrite(commandByte); // Send the command byte
  1823. SPI_DC_HIGH();
  1824. for (int i = 0; i < numDataBytes; i++) {
  1825. if ((connection == TFT_PARALLEL) && tft8.wide) {
  1826. SPI_WRITE16(*(uint16_t *)dataBytes);
  1827. dataBytes += 2;
  1828. } else {
  1829. spiWrite(pgm_read_byte(dataBytes++));
  1830. }
  1831. }
  1832. if (_cs >= 0)
  1833. SPI_CS_HIGH();
  1834. SPI_END_TRANSACTION();
  1835. }
  1836. /*!
  1837. @brief Adafruit_SPITFT sendCommand16 handles complete sending of
  1838. commands and data for 16-bit parallel displays. Currently somewhat
  1839. rigged for the NT35510, which has the odd behavior of wanting
  1840. commands 16-bit, but subsequent data as 8-bit values, despite
  1841. the 16-bit bus (high byte is always 0). Also seems to require
  1842. issuing and incrementing address with each transfer.
  1843. @param commandWord The command word (16 bits)
  1844. @param dataBytes A pointer to the data bytes to send
  1845. @param numDataBytes The number of bytes we should send
  1846. */
  1847. void Adafruit_SPITFT::sendCommand16(uint16_t commandWord,
  1848. const uint8_t *dataBytes,
  1849. uint8_t numDataBytes) {
  1850. SPI_BEGIN_TRANSACTION();
  1851. if (_cs >= 0)
  1852. SPI_CS_LOW();
  1853. if (numDataBytes == 0) {
  1854. SPI_DC_LOW(); // Command mode
  1855. SPI_WRITE16(commandWord); // Send the command word
  1856. SPI_DC_HIGH(); // Data mode
  1857. }
  1858. for (int i = 0; i < numDataBytes; i++) {
  1859. SPI_DC_LOW(); // Command mode
  1860. SPI_WRITE16(commandWord); // Send the command word
  1861. SPI_DC_HIGH(); // Data mode
  1862. commandWord++;
  1863. SPI_WRITE16((uint16_t)pgm_read_byte(dataBytes++));
  1864. }
  1865. if (_cs >= 0)
  1866. SPI_CS_HIGH();
  1867. SPI_END_TRANSACTION();
  1868. }
  1869. /*!
  1870. @brief Read 8 bits of data from display configuration memory (not RAM).
  1871. This is highly undocumented/supported and should be avoided,
  1872. function is only included because some of the examples use it.
  1873. @param commandByte
  1874. The command register to read data from.
  1875. @param index
  1876. The byte index into the command to read from.
  1877. @return Unsigned 8-bit data read from display register.
  1878. */
  1879. /**************************************************************************/
  1880. uint8_t Adafruit_SPITFT::readcommand8(uint8_t commandByte, uint8_t index) {
  1881. uint8_t result;
  1882. startWrite();
  1883. SPI_DC_LOW(); // Command mode
  1884. spiWrite(commandByte);
  1885. SPI_DC_HIGH(); // Data mode
  1886. do {
  1887. result = spiRead();
  1888. } while (index--); // Discard bytes up to index'th
  1889. endWrite();
  1890. return result;
  1891. }
  1892. /*!
  1893. @brief Read 16 bits of data from display register.
  1894. For 16-bit parallel displays only.
  1895. @param addr Command/register to access.
  1896. @return Unsigned 16-bit data.
  1897. */
  1898. uint16_t Adafruit_SPITFT::readcommand16(uint16_t addr) {
  1899. #if defined(USE_FAST_PINIO) // NOT SUPPORTED without USE_FAST_PINIO
  1900. uint16_t result = 0;
  1901. if ((connection == TFT_PARALLEL) && tft8.wide) {
  1902. startWrite();
  1903. SPI_DC_LOW(); // Command mode
  1904. SPI_WRITE16(addr);
  1905. SPI_DC_HIGH(); // Data mode
  1906. TFT_RD_LOW(); // Read line LOW
  1907. #if defined(HAS_PORT_SET_CLR)
  1908. *(volatile uint16_t *)tft8.dirClr = 0xFFFF; // Input state
  1909. result = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  1910. *(volatile uint16_t *)tft8.dirSet = 0xFFFF; // Output state
  1911. #else // !HAS_PORT_SET_CLR
  1912. *(volatile uint16_t *)tft8.portDir = 0x0000; // Input state
  1913. result = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  1914. *(volatile uint16_t *)tft8.portDir = 0xFFFF; // Output state
  1915. #endif // end !HAS_PORT_SET_CLR
  1916. TFT_RD_HIGH(); // Read line HIGH
  1917. endWrite();
  1918. }
  1919. return result;
  1920. #else
  1921. (void)addr; // disable -Wunused-parameter warning
  1922. return 0;
  1923. #endif // end !USE_FAST_PINIO
  1924. }
  1925. // -------------------------------------------------------------------------
  1926. // Lowest-level hardware-interfacing functions. Many of these are inline and
  1927. // compile to different things based on #defines -- typically just a few
  1928. // instructions. Others, not so much, those are not inlined.
  1929. /*!
  1930. @brief Start an SPI transaction if using the hardware SPI interface to
  1931. the display. If using an earlier version of the Arduino platform
  1932. (before the addition of SPI transactions), this instead attempts
  1933. to set up the SPI clock and mode. No action is taken if the
  1934. connection is not hardware SPI-based. This does NOT include a
  1935. chip-select operation -- see startWrite() for a function that
  1936. encapsulated both actions.
  1937. */
  1938. inline void Adafruit_SPITFT::SPI_BEGIN_TRANSACTION(void) {
  1939. if (connection == TFT_HARD_SPI) {
  1940. #if defined(SPI_HAS_TRANSACTION)
  1941. hwspi._spi->beginTransaction(hwspi.settings);
  1942. #else // No transactions, configure SPI manually...
  1943. #if defined(__AVR__) || defined(TEENSYDUINO) || defined(ARDUINO_ARCH_STM32F1)
  1944. hwspi._spi->setClockDivider(SPI_CLOCK_DIV2);
  1945. #elif defined(__arm__)
  1946. hwspi._spi->setClockDivider(11);
  1947. #elif defined(ESP8266) || defined(ESP32)
  1948. hwspi._spi->setFrequency(hwspi._freq);
  1949. #elif defined(RASPI) || defined(ARDUINO_ARCH_STM32F1)
  1950. hwspi._spi->setClock(hwspi._freq);
  1951. #endif
  1952. hwspi._spi->setBitOrder(MSBFIRST);
  1953. hwspi._spi->setDataMode(hwspi._mode);
  1954. #endif // end !SPI_HAS_TRANSACTION
  1955. }
  1956. }
  1957. /*!
  1958. @brief End an SPI transaction if using the hardware SPI interface to
  1959. the display. No action is taken if the connection is not
  1960. hardware SPI-based or if using an earlier version of the Arduino
  1961. platform (before the addition of SPI transactions). This does
  1962. NOT include a chip-deselect operation -- see endWrite() for a
  1963. function that encapsulated both actions.
  1964. */
  1965. inline void Adafruit_SPITFT::SPI_END_TRANSACTION(void) {
  1966. #if defined(SPI_HAS_TRANSACTION)
  1967. if (connection == TFT_HARD_SPI) {
  1968. hwspi._spi->endTransaction();
  1969. }
  1970. #endif
  1971. }
  1972. /*!
  1973. @brief Issue a single 8-bit value to the display. Chip-select,
  1974. transaction and data/command selection must have been
  1975. previously set -- this ONLY issues the byte. This is another of
  1976. those functions in the library with a now-not-accurate name
  1977. that's being maintained for compatibility with outside code.
  1978. This function is used even if display connection is parallel.
  1979. @param b 8-bit value to write.
  1980. */
  1981. void Adafruit_SPITFT::spiWrite(uint8_t b) {
  1982. if (connection == TFT_HARD_SPI) {
  1983. #if defined(__AVR__)
  1984. AVR_WRITESPI(b);
  1985. #elif defined(ESP8266) || defined(ESP32)
  1986. hwspi._spi->write(b);
  1987. #elif defined(ARDUINO_ARCH_RP2040)
  1988. spi_inst_t *pi_spi = hwspi._spi == &SPI ? spi0 : spi1;
  1989. spi_write_blocking(pi_spi, &b, 1);
  1990. #else
  1991. hwspi._spi->transfer(b);
  1992. #endif
  1993. } else if (connection == TFT_SOFT_SPI) {
  1994. for (uint8_t bit = 0; bit < 8; bit++) {
  1995. if (b & 0x80)
  1996. SPI_MOSI_HIGH();
  1997. else
  1998. SPI_MOSI_LOW();
  1999. SPI_SCK_HIGH();
  2000. b <<= 1;
  2001. SPI_SCK_LOW();
  2002. }
  2003. } else { // TFT_PARALLEL
  2004. #if defined(__AVR__)
  2005. *tft8.writePort = b;
  2006. #elif defined(USE_FAST_PINIO)
  2007. if (!tft8.wide)
  2008. *tft8.writePort = b;
  2009. else
  2010. *(volatile uint16_t *)tft8.writePort = b;
  2011. #endif
  2012. TFT_WR_STROBE();
  2013. }
  2014. }
  2015. /*!
  2016. @brief Write a single command byte to the display. Chip-select and
  2017. transaction must have been previously set -- this ONLY sets
  2018. the device to COMMAND mode, issues the byte and then restores
  2019. DATA mode. There is no corresponding explicit writeData()
  2020. function -- just use spiWrite().
  2021. @param cmd 8-bit command to write.
  2022. */
  2023. void Adafruit_SPITFT::writeCommand(uint8_t cmd) {
  2024. SPI_DC_LOW();
  2025. spiWrite(cmd);
  2026. SPI_DC_HIGH();
  2027. }
  2028. /*!
  2029. @brief Read a single 8-bit value from the display. Chip-select and
  2030. transaction must have been previously set -- this ONLY reads
  2031. the byte. This is another of those functions in the library
  2032. with a now-not-accurate name that's being maintained for
  2033. compatibility with outside code. This function is used even if
  2034. display connection is parallel.
  2035. @return Unsigned 8-bit value read (always zero if USE_FAST_PINIO is
  2036. not supported by the MCU architecture).
  2037. */
  2038. uint8_t Adafruit_SPITFT::spiRead(void) {
  2039. uint8_t b = 0;
  2040. uint16_t w = 0;
  2041. if (connection == TFT_HARD_SPI) {
  2042. return hwspi._spi->transfer((uint8_t)0);
  2043. } else if (connection == TFT_SOFT_SPI) {
  2044. if (swspi._miso >= 0) {
  2045. for (uint8_t i = 0; i < 8; i++) {
  2046. SPI_SCK_HIGH();
  2047. b <<= 1;
  2048. if (SPI_MISO_READ())
  2049. b++;
  2050. SPI_SCK_LOW();
  2051. }
  2052. }
  2053. return b;
  2054. } else { // TFT_PARALLEL
  2055. if (tft8._rd >= 0) {
  2056. #if defined(USE_FAST_PINIO)
  2057. TFT_RD_LOW(); // Read line LOW
  2058. #if defined(__AVR__)
  2059. *tft8.portDir = 0x00; // Set port to input state
  2060. w = *tft8.readPort; // Read value from port
  2061. *tft8.portDir = 0xFF; // Restore port to output
  2062. #else // !__AVR__
  2063. if (!tft8.wide) { // 8-bit TFT connection
  2064. #if defined(HAS_PORT_SET_CLR)
  2065. *tft8.dirClr = 0xFF; // Set port to input state
  2066. w = *tft8.readPort; // Read value from port
  2067. *tft8.dirSet = 0xFF; // Restore port to output
  2068. #else // !HAS_PORT_SET_CLR
  2069. *tft8.portDir = 0x00; // Set port to input state
  2070. w = *tft8.readPort; // Read value from port
  2071. *tft8.portDir = 0xFF; // Restore port to output
  2072. #endif // end HAS_PORT_SET_CLR
  2073. } else { // 16-bit TFT connection
  2074. #if defined(HAS_PORT_SET_CLR)
  2075. *(volatile uint16_t *)tft8.dirClr = 0xFFFF; // Input state
  2076. w = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  2077. *(volatile uint16_t *)tft8.dirSet = 0xFFFF; // Output state
  2078. #else // !HAS_PORT_SET_CLR
  2079. *(volatile uint16_t *)tft8.portDir = 0x0000; // Input state
  2080. w = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  2081. *(volatile uint16_t *)tft8.portDir = 0xFFFF; // Output state
  2082. #endif // end !HAS_PORT_SET_CLR
  2083. }
  2084. TFT_RD_HIGH(); // Read line HIGH
  2085. #endif // end !__AVR__
  2086. #else // !USE_FAST_PINIO
  2087. w = 0; // Parallel TFT is NOT SUPPORTED without USE_FAST_PINIO
  2088. #endif // end !USE_FAST_PINIO
  2089. }
  2090. return w;
  2091. }
  2092. }
  2093. /*!
  2094. @brief Issue a single 16-bit value to the display. Chip-select,
  2095. transaction and data/command selection must have been
  2096. previously set -- this ONLY issues the word.
  2097. Thus operates ONLY on 'wide' (16-bit) parallel displays!
  2098. @param w 16-bit value to write.
  2099. */
  2100. void Adafruit_SPITFT::write16(uint16_t w) {
  2101. if (connection == TFT_PARALLEL) {
  2102. #if defined(USE_FAST_PINIO)
  2103. if (tft8.wide)
  2104. *(volatile uint16_t *)tft8.writePort = w;
  2105. #else
  2106. (void)w; // disable -Wunused-parameter warning
  2107. #endif
  2108. TFT_WR_STROBE();
  2109. }
  2110. }
  2111. /*!
  2112. @brief Write a single command word to the display. Chip-select and
  2113. transaction must have been previously set -- this ONLY sets
  2114. the device to COMMAND mode, issues the byte and then restores
  2115. DATA mode. This operates ONLY on 'wide' (16-bit) parallel
  2116. displays!
  2117. @param cmd 16-bit command to write.
  2118. */
  2119. void Adafruit_SPITFT::writeCommand16(uint16_t cmd) {
  2120. SPI_DC_LOW();
  2121. write16(cmd);
  2122. SPI_DC_HIGH();
  2123. }
  2124. /*!
  2125. @brief Read a single 16-bit value from the display. Chip-select and
  2126. transaction must have been previously set -- this ONLY reads
  2127. the byte. This operates ONLY on 'wide' (16-bit) parallel
  2128. displays!
  2129. @return Unsigned 16-bit value read (always zero if USE_FAST_PINIO is
  2130. not supported by the MCU architecture).
  2131. */
  2132. uint16_t Adafruit_SPITFT::read16(void) {
  2133. uint16_t w = 0;
  2134. if (connection == TFT_PARALLEL) {
  2135. if (tft8._rd >= 0) {
  2136. #if defined(USE_FAST_PINIO)
  2137. TFT_RD_LOW(); // Read line LOW
  2138. if (tft8.wide) { // 16-bit TFT connection
  2139. #if defined(HAS_PORT_SET_CLR)
  2140. *(volatile uint16_t *)tft8.dirClr = 0xFFFF; // Input state
  2141. w = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  2142. *(volatile uint16_t *)tft8.dirSet = 0xFFFF; // Output state
  2143. #else // !HAS_PORT_SET_CLR
  2144. *(volatile uint16_t *)tft8.portDir = 0x0000; // Input state
  2145. w = *(volatile uint16_t *)tft8.readPort; // 16-bit read
  2146. *(volatile uint16_t *)tft8.portDir = 0xFFFF; // Output state
  2147. #endif // end !HAS_PORT_SET_CLR
  2148. }
  2149. TFT_RD_HIGH(); // Read line HIGH
  2150. #else // !USE_FAST_PINIO
  2151. w = 0; // Parallel TFT is NOT SUPPORTED without USE_FAST_PINIO
  2152. #endif // end !USE_FAST_PINIO
  2153. }
  2154. }
  2155. return w;
  2156. }
  2157. /*!
  2158. @brief Set the software (bitbang) SPI MOSI line HIGH.
  2159. */
  2160. inline void Adafruit_SPITFT::SPI_MOSI_HIGH(void) {
  2161. #if defined(USE_FAST_PINIO)
  2162. #if defined(HAS_PORT_SET_CLR)
  2163. #if defined(KINETISK)
  2164. *swspi.mosiPortSet = 1;
  2165. #else // !KINETISK
  2166. *swspi.mosiPortSet = swspi.mosiPinMask;
  2167. #endif
  2168. #else // !HAS_PORT_SET_CLR
  2169. *swspi.mosiPort |= swspi.mosiPinMaskSet;
  2170. #endif // end !HAS_PORT_SET_CLR
  2171. #else // !USE_FAST_PINIO
  2172. digitalWrite(swspi._mosi, HIGH);
  2173. #if defined(ESP32)
  2174. for (volatile uint8_t i = 0; i < 1; i++)
  2175. ;
  2176. #endif // end ESP32
  2177. #endif // end !USE_FAST_PINIO
  2178. }
  2179. /*!
  2180. @brief Set the software (bitbang) SPI MOSI line LOW.
  2181. */
  2182. inline void Adafruit_SPITFT::SPI_MOSI_LOW(void) {
  2183. #if defined(USE_FAST_PINIO)
  2184. #if defined(HAS_PORT_SET_CLR)
  2185. #if defined(KINETISK)
  2186. *swspi.mosiPortClr = 1;
  2187. #else // !KINETISK
  2188. *swspi.mosiPortClr = swspi.mosiPinMask;
  2189. #endif
  2190. #else // !HAS_PORT_SET_CLR
  2191. *swspi.mosiPort &= swspi.mosiPinMaskClr;
  2192. #endif // end !HAS_PORT_SET_CLR
  2193. #else // !USE_FAST_PINIO
  2194. digitalWrite(swspi._mosi, LOW);
  2195. #if defined(ESP32)
  2196. for (volatile uint8_t i = 0; i < 1; i++)
  2197. ;
  2198. #endif // end ESP32
  2199. #endif // end !USE_FAST_PINIO
  2200. }
  2201. /*!
  2202. @brief Set the software (bitbang) SPI SCK line HIGH.
  2203. */
  2204. inline void Adafruit_SPITFT::SPI_SCK_HIGH(void) {
  2205. #if defined(USE_FAST_PINIO)
  2206. #if defined(HAS_PORT_SET_CLR)
  2207. #if defined(KINETISK)
  2208. *swspi.sckPortSet = 1;
  2209. #else // !KINETISK
  2210. *swspi.sckPortSet = swspi.sckPinMask;
  2211. #if defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
  2212. for (volatile uint8_t i = 0; i < 1; i++)
  2213. ;
  2214. #endif
  2215. #endif
  2216. #else // !HAS_PORT_SET_CLR
  2217. *swspi.sckPort |= swspi.sckPinMaskSet;
  2218. #endif // end !HAS_PORT_SET_CLR
  2219. #else // !USE_FAST_PINIO
  2220. digitalWrite(swspi._sck, HIGH);
  2221. #if defined(ESP32)
  2222. for (volatile uint8_t i = 0; i < 1; i++)
  2223. ;
  2224. #endif // end ESP32
  2225. #endif // end !USE_FAST_PINIO
  2226. }
  2227. /*!
  2228. @brief Set the software (bitbang) SPI SCK line LOW.
  2229. */
  2230. inline void Adafruit_SPITFT::SPI_SCK_LOW(void) {
  2231. #if defined(USE_FAST_PINIO)
  2232. #if defined(HAS_PORT_SET_CLR)
  2233. #if defined(KINETISK)
  2234. *swspi.sckPortClr = 1;
  2235. #else // !KINETISK
  2236. *swspi.sckPortClr = swspi.sckPinMask;
  2237. #if defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
  2238. for (volatile uint8_t i = 0; i < 1; i++)
  2239. ;
  2240. #endif
  2241. #endif
  2242. #else // !HAS_PORT_SET_CLR
  2243. *swspi.sckPort &= swspi.sckPinMaskClr;
  2244. #endif // end !HAS_PORT_SET_CLR
  2245. #else // !USE_FAST_PINIO
  2246. digitalWrite(swspi._sck, LOW);
  2247. #if defined(ESP32)
  2248. for (volatile uint8_t i = 0; i < 1; i++)
  2249. ;
  2250. #endif // end ESP32
  2251. #endif // end !USE_FAST_PINIO
  2252. }
  2253. /*!
  2254. @brief Read the state of the software (bitbang) SPI MISO line.
  2255. @return true if HIGH, false if LOW.
  2256. */
  2257. inline bool Adafruit_SPITFT::SPI_MISO_READ(void) {
  2258. #if defined(USE_FAST_PINIO)
  2259. #if defined(KINETISK)
  2260. return *swspi.misoPort;
  2261. #else // !KINETISK
  2262. return *swspi.misoPort & swspi.misoPinMask;
  2263. #endif // end !KINETISK
  2264. #else // !USE_FAST_PINIO
  2265. return digitalRead(swspi._miso);
  2266. #endif // end !USE_FAST_PINIO
  2267. }
  2268. /*!
  2269. @brief Issue a single 16-bit value to the display. Chip-select,
  2270. transaction and data/command selection must have been
  2271. previously set -- this ONLY issues the word. Despite the name,
  2272. this function is used even if display connection is parallel;
  2273. name was maintaned for backward compatibility. Naming is also
  2274. not consistent with the 8-bit version, spiWrite(). Sorry about
  2275. that. Again, staying compatible with outside code.
  2276. @param w 16-bit value to write.
  2277. */
  2278. void Adafruit_SPITFT::SPI_WRITE16(uint16_t w) {
  2279. if (connection == TFT_HARD_SPI) {
  2280. #if defined(__AVR__)
  2281. AVR_WRITESPI(w >> 8);
  2282. AVR_WRITESPI(w);
  2283. #elif defined(ESP8266) || defined(ESP32)
  2284. hwspi._spi->write16(w);
  2285. #elif defined(ARDUINO_ARCH_RP2040)
  2286. spi_inst_t *pi_spi = hwspi._spi == &SPI ? spi0 : spi1;
  2287. w = __builtin_bswap16(w);
  2288. spi_write_blocking(pi_spi, (uint8_t *)&w, 2);
  2289. #else
  2290. // MSB, LSB because TFTs are generally big-endian
  2291. hwspi._spi->transfer(w >> 8);
  2292. hwspi._spi->transfer(w);
  2293. #endif
  2294. } else if (connection == TFT_SOFT_SPI) {
  2295. for (uint8_t bit = 0; bit < 16; bit++) {
  2296. if (w & 0x8000)
  2297. SPI_MOSI_HIGH();
  2298. else
  2299. SPI_MOSI_LOW();
  2300. SPI_SCK_HIGH();
  2301. SPI_SCK_LOW();
  2302. w <<= 1;
  2303. }
  2304. } else { // TFT_PARALLEL
  2305. #if defined(__AVR__)
  2306. *tft8.writePort = w >> 8;
  2307. TFT_WR_STROBE();
  2308. *tft8.writePort = w;
  2309. #elif defined(USE_FAST_PINIO)
  2310. if (!tft8.wide) {
  2311. *tft8.writePort = w >> 8;
  2312. TFT_WR_STROBE();
  2313. *tft8.writePort = w;
  2314. } else {
  2315. *(volatile uint16_t *)tft8.writePort = w;
  2316. }
  2317. #endif
  2318. TFT_WR_STROBE();
  2319. }
  2320. }
  2321. /*!
  2322. @brief Issue a single 32-bit value to the display. Chip-select,
  2323. transaction and data/command selection must have been
  2324. previously set -- this ONLY issues the longword. Despite the
  2325. name, this function is used even if display connection is
  2326. parallel; name was maintaned for backward compatibility. Naming
  2327. is also not consistent with the 8-bit version, spiWrite().
  2328. Sorry about that. Again, staying compatible with outside code.
  2329. @param l 32-bit value to write.
  2330. */
  2331. void Adafruit_SPITFT::SPI_WRITE32(uint32_t l) {
  2332. if (connection == TFT_HARD_SPI) {
  2333. #if defined(__AVR__)
  2334. AVR_WRITESPI(l >> 24);
  2335. AVR_WRITESPI(l >> 16);
  2336. AVR_WRITESPI(l >> 8);
  2337. AVR_WRITESPI(l);
  2338. #elif defined(ESP8266) || defined(ESP32)
  2339. hwspi._spi->write32(l);
  2340. #elif defined(ARDUINO_ARCH_RP2040)
  2341. spi_inst_t *pi_spi = hwspi._spi == &SPI ? spi0 : spi1;
  2342. l = __builtin_bswap32(l);
  2343. spi_write_blocking(pi_spi, (uint8_t *)&l, 4);
  2344. #else
  2345. hwspi._spi->transfer(l >> 24);
  2346. hwspi._spi->transfer(l >> 16);
  2347. hwspi._spi->transfer(l >> 8);
  2348. hwspi._spi->transfer(l);
  2349. #endif
  2350. } else if (connection == TFT_SOFT_SPI) {
  2351. for (uint8_t bit = 0; bit < 32; bit++) {
  2352. if (l & 0x80000000)
  2353. SPI_MOSI_HIGH();
  2354. else
  2355. SPI_MOSI_LOW();
  2356. SPI_SCK_HIGH();
  2357. SPI_SCK_LOW();
  2358. l <<= 1;
  2359. }
  2360. } else { // TFT_PARALLEL
  2361. #if defined(__AVR__)
  2362. *tft8.writePort = l >> 24;
  2363. TFT_WR_STROBE();
  2364. *tft8.writePort = l >> 16;
  2365. TFT_WR_STROBE();
  2366. *tft8.writePort = l >> 8;
  2367. TFT_WR_STROBE();
  2368. *tft8.writePort = l;
  2369. #elif defined(USE_FAST_PINIO)
  2370. if (!tft8.wide) {
  2371. *tft8.writePort = l >> 24;
  2372. TFT_WR_STROBE();
  2373. *tft8.writePort = l >> 16;
  2374. TFT_WR_STROBE();
  2375. *tft8.writePort = l >> 8;
  2376. TFT_WR_STROBE();
  2377. *tft8.writePort = l;
  2378. } else {
  2379. *(volatile uint16_t *)tft8.writePort = l >> 16;
  2380. TFT_WR_STROBE();
  2381. *(volatile uint16_t *)tft8.writePort = l;
  2382. }
  2383. #endif
  2384. TFT_WR_STROBE();
  2385. }
  2386. }
  2387. /*!
  2388. @brief Set the WR line LOW, then HIGH. Used for parallel-connected
  2389. interfaces when writing data.
  2390. */
  2391. inline void Adafruit_SPITFT::TFT_WR_STROBE(void) {
  2392. #if defined(USE_FAST_PINIO)
  2393. #if defined(HAS_PORT_SET_CLR)
  2394. #if defined(KINETISK)
  2395. *tft8.wrPortClr = 1;
  2396. *tft8.wrPortSet = 1;
  2397. #else // !KINETISK
  2398. *tft8.wrPortClr = tft8.wrPinMask;
  2399. *tft8.wrPortSet = tft8.wrPinMask;
  2400. #endif // end !KINETISK
  2401. #else // !HAS_PORT_SET_CLR
  2402. *tft8.wrPort &= tft8.wrPinMaskClr;
  2403. *tft8.wrPort |= tft8.wrPinMaskSet;
  2404. #endif // end !HAS_PORT_SET_CLR
  2405. #else // !USE_FAST_PINIO
  2406. digitalWrite(tft8._wr, LOW);
  2407. digitalWrite(tft8._wr, HIGH);
  2408. #endif // end !USE_FAST_PINIO
  2409. }
  2410. /*!
  2411. @brief Set the RD line HIGH. Used for parallel-connected interfaces
  2412. when reading data.
  2413. */
  2414. inline void Adafruit_SPITFT::TFT_RD_HIGH(void) {
  2415. #if defined(USE_FAST_PINIO)
  2416. #if defined(HAS_PORT_SET_CLR)
  2417. *tft8.rdPortSet = tft8.rdPinMask;
  2418. #else // !HAS_PORT_SET_CLR
  2419. *tft8.rdPort |= tft8.rdPinMaskSet;
  2420. #endif // end !HAS_PORT_SET_CLR
  2421. #else // !USE_FAST_PINIO
  2422. digitalWrite(tft8._rd, HIGH);
  2423. #endif // end !USE_FAST_PINIO
  2424. }
  2425. /*!
  2426. @brief Set the RD line LOW. Used for parallel-connected interfaces
  2427. when reading data.
  2428. */
  2429. inline void Adafruit_SPITFT::TFT_RD_LOW(void) {
  2430. #if defined(USE_FAST_PINIO)
  2431. #if defined(HAS_PORT_SET_CLR)
  2432. *tft8.rdPortClr = tft8.rdPinMask;
  2433. #else // !HAS_PORT_SET_CLR
  2434. *tft8.rdPort &= tft8.rdPinMaskClr;
  2435. #endif // end !HAS_PORT_SET_CLR
  2436. #else // !USE_FAST_PINIO
  2437. digitalWrite(tft8._rd, LOW);
  2438. #endif // end !USE_FAST_PINIO
  2439. }
  2440. #endif // end __AVR_ATtiny85__