You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

640 lines
24 KiB

9 months ago
  1. ////////////////////////////////////////////////////
  2. // TFT_eSPI driver functions for ESP32 processors //
  3. ////////////////////////////////////////////////////
  4. // Temporarily a separate file to TFT_eSPI_ESP32.h until board package low level API stabilises
  5. #ifndef _TFT_eSPI_ESP32H_
  6. #define _TFT_eSPI_ESP32H_
  7. #if !defined(DISABLE_ALL_LIBRARY_WARNINGS)
  8. #warning >>>>------>> DMA is not supported on the ESP32 S3 (possible future update)
  9. #endif
  10. // Processor ID reported by getSetup()
  11. #define PROCESSOR_ID 0x32
  12. // Include processor specific header
  13. #include "soc/spi_reg.h"
  14. #include "driver/spi_master.h"
  15. #if !defined(CONFIG_IDF_TARGET_ESP32S3) && !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32)
  16. #define CONFIG_IDF_TARGET_ESP32
  17. #endif
  18. #ifndef VSPI
  19. #define VSPI FSPI
  20. #endif
  21. // Fix IDF problems with ESP32S3
  22. // Note illogical enumerations: FSPI_HOST=SPI2_HOST=1 HSPI_HOST=SPI3_HOST=2
  23. #if CONFIG_IDF_TARGET_ESP32S3
  24. // Fix ESP32C3 IDF bug for missing definition (FSPI only tested at the moment)
  25. #ifndef REG_SPI_BASE // HSPI FSPI/VSPI
  26. #define REG_SPI_BASE(i) (((i)>1) ? (DR_REG_SPI3_BASE) : (DR_REG_SPI2_BASE))
  27. #endif
  28. // Fix ESP32S3 IDF bug for name change
  29. #ifndef SPI_MOSI_DLEN_REG
  30. #define SPI_MOSI_DLEN_REG(x) SPI_MS_DLEN_REG(x)
  31. #endif
  32. #endif
  33. // SUPPORT_TRANSACTIONS is mandatory for ESP32 so the hal mutex is toggled
  34. #if !defined (SUPPORT_TRANSACTIONS)
  35. #define SUPPORT_TRANSACTIONS
  36. #endif
  37. /*
  38. ESP32:
  39. FSPI not defined
  40. HSPI = 2, uses SPI2
  41. VSPI = 3, uses SPI3
  42. ESP32-S2:
  43. FSPI = 1, uses SPI2
  44. HSPI = 2, uses SPI3
  45. VSPI not defined
  46. ESP32 C3:
  47. FSPI = 0, uses SPI2 ???? To be checked
  48. HSPI = 1, uses SPI3 ???? To be checked
  49. VSPI not defined
  50. For ESP32/S2/C3/S3:
  51. SPI1_HOST = 0
  52. SPI2_HOST = 1
  53. SPI3_HOST = 2
  54. */
  55. // ESP32 specific SPI port selection
  56. #ifdef USE_HSPI_PORT
  57. #ifdef CONFIG_IDF_TARGET_ESP32
  58. #define SPI_PORT HSPI //HSPI is port 2 on ESP32
  59. #else
  60. #define SPI_PORT 3 //HSPI is port 3 on ESP32 S2
  61. #endif
  62. #elif defined(USE_FSPI_PORT)
  63. #define SPI_PORT 2 //FSPI(ESP32 S2)
  64. #else
  65. #ifdef CONFIG_IDF_TARGET_ESP32
  66. #define SPI_PORT VSPI
  67. #elif CONFIG_IDF_TARGET_ESP32S2
  68. #define SPI_PORT 2 //FSPI(ESP32 S2)
  69. #elif CONFIG_IDF_TARGET_ESP32S3
  70. #define SPI_PORT FSPI
  71. #endif
  72. #endif
  73. #ifdef RPI_DISPLAY_TYPE
  74. #define CMD_BITS (16-1)
  75. #else
  76. #define CMD_BITS (8-1)
  77. #endif
  78. // Initialise processor specific SPI functions, used by init()
  79. #define INIT_TFT_DATA_BUS // Not used
  80. // Define a generic flag for 8 bit parallel
  81. #if defined (ESP32_PARALLEL) // Specific to ESP32 for backwards compatibility
  82. #if !defined (TFT_PARALLEL_8_BIT)
  83. #define TFT_PARALLEL_8_BIT // Generic parallel flag
  84. #endif
  85. #endif
  86. // Ensure ESP32 specific flag is defined for 8 bit parallel
  87. #if defined (TFT_PARALLEL_8_BIT)
  88. #if !defined (ESP32_PARALLEL)
  89. #define ESP32_PARALLEL
  90. #endif
  91. #endif
  92. // Processor specific code used by SPI bus transaction startWrite and endWrite functions
  93. #if !defined (ESP32_PARALLEL)
  94. #define _spi_cmd (volatile uint32_t*)(SPI_CMD_REG(SPI_PORT))
  95. #define _spi_user (volatile uint32_t*)(SPI_USER_REG(SPI_PORT))
  96. #define _spi_mosi_dlen (volatile uint32_t*)(SPI_MOSI_DLEN_REG(SPI_PORT))
  97. #define _spi_w (volatile uint32_t*)(SPI_W0_REG(SPI_PORT))
  98. #if (TFT_SPI_MODE == SPI_MODE1) || (TFT_SPI_MODE == SPI_MODE2)
  99. #define SET_BUS_WRITE_MODE *_spi_user = SPI_USR_MOSI | SPI_CK_OUT_EDGE
  100. #define SET_BUS_READ_MODE *_spi_user = SPI_USR_MOSI | SPI_USR_MISO | SPI_DOUTDIN | SPI_CK_OUT_EDGE
  101. #else
  102. #define SET_BUS_WRITE_MODE *_spi_user = SPI_USR_MOSI
  103. #define SET_BUS_READ_MODE *_spi_user = SPI_USR_MOSI | SPI_USR_MISO | SPI_DOUTDIN
  104. #endif
  105. #else
  106. // Not applicable to parallel bus
  107. #define SET_BUS_WRITE_MODE
  108. #define SET_BUS_READ_MODE
  109. #endif
  110. // Code to check if DMA is busy, used by SPI bus transaction transaction and endWrite functions
  111. #if !defined(TFT_PARALLEL_8_BIT) && !defined(SPI_18BIT_DRIVER)
  112. #define ESP32_DMA
  113. // Code to check if DMA is busy, used by SPI DMA + transaction + endWrite functions
  114. #define DMA_BUSY_CHECK //dmaWait()
  115. #else
  116. #define DMA_BUSY_CHECK
  117. #endif
  118. #if defined(TFT_PARALLEL_8_BIT)
  119. #define SPI_BUSY_CHECK
  120. #else
  121. #define SPI_BUSY_CHECK while (*_spi_cmd&SPI_USR)
  122. #endif
  123. // If smooth font is used then it is likely SPIFFS will be needed
  124. #ifdef SMOOTH_FONT
  125. // Call up the SPIFFS (SPI FLASH Filing System) for the anti-aliased fonts
  126. #define FS_NO_GLOBALS
  127. #include <FS.h>
  128. #include "SPIFFS.h" // ESP32 only
  129. #define FONT_FS_AVAILABLE
  130. #endif
  131. ////////////////////////////////////////////////////////////////////////////////////////
  132. // Define the DC (TFT Data/Command or Register Select (RS))pin drive code
  133. ////////////////////////////////////////////////////////////////////////////////////////
  134. #ifndef TFT_DC
  135. #define DC_C // No macro allocated so it generates no code
  136. #define DC_D // No macro allocated so it generates no code
  137. #else
  138. #if defined (TFT_PARALLEL_8_BIT)
  139. // TFT_DC, by design, must be in range 0-31 for single register parallel write
  140. #if (TFT_DC >= 0) && (TFT_DC < 32)
  141. #define DC_C GPIO.out_w1tc = (1 << TFT_DC)
  142. #define DC_D GPIO.out_w1ts = (1 << TFT_DC)
  143. #elif (TFT_DC >= 32)
  144. #define DC_C GPIO.out1_w1tc.val = (1 << (TFT_DC- 32))
  145. #define DC_D GPIO.out1_w1ts.val = (1 << (TFT_DC- 32))
  146. #else
  147. #define DC_C
  148. #define DC_D
  149. #endif
  150. #else
  151. #if (TFT_DC >= 32)
  152. #ifdef RPI_DISPLAY_TYPE // RPi displays need a slower DC change
  153. #define DC_C GPIO.out1_w1ts.val = (1 << (TFT_DC - 32)); \
  154. GPIO.out1_w1tc.val = (1 << (TFT_DC - 32))
  155. #define DC_D GPIO.out1_w1tc.val = (1 << (TFT_DC - 32)); \
  156. GPIO.out1_w1ts.val = (1 << (TFT_DC - 32))
  157. #else
  158. #define DC_C GPIO.out1_w1tc.val = (1 << (TFT_DC - 32))//;GPIO.out1_w1tc.val = (1 << (TFT_DC - 32))
  159. #define DC_D GPIO.out1_w1ts.val = (1 << (TFT_DC - 32))//;GPIO.out1_w1ts.val = (1 << (TFT_DC - 32))
  160. #endif
  161. #elif (TFT_DC >= 0)
  162. #if defined (RPI_DISPLAY_TYPE)
  163. #if defined (ILI9486_DRIVER)
  164. // RPi ILI9486 display needs a slower DC change
  165. #define DC_C GPIO.out_w1tc = (1 << TFT_DC); \
  166. GPIO.out_w1tc = (1 << TFT_DC)
  167. #define DC_D GPIO.out_w1tc = (1 << TFT_DC); \
  168. GPIO.out_w1ts = (1 << TFT_DC)
  169. #else
  170. // Other RPi displays need a slower C->D change
  171. #define DC_C GPIO.out_w1tc = (1 << TFT_DC)
  172. #define DC_D GPIO.out_w1tc = (1 << TFT_DC); \
  173. GPIO.out_w1ts = (1 << TFT_DC)
  174. #endif
  175. #else
  176. #define DC_C GPIO.out_w1tc = (1 << TFT_DC)//;GPIO.out_w1tc = (1 << TFT_DC)
  177. #define DC_D GPIO.out_w1ts = (1 << TFT_DC)//;GPIO.out_w1ts = (1 << TFT_DC)
  178. #endif
  179. #else
  180. #define DC_C
  181. #define DC_D
  182. #endif
  183. #endif
  184. #endif
  185. ////////////////////////////////////////////////////////////////////////////////////////
  186. // Define the CS (TFT chip select) pin drive code
  187. ////////////////////////////////////////////////////////////////////////////////////////
  188. #ifndef TFT_CS
  189. #define TFT_CS -1 // Keep DMA code happy
  190. #define CS_L // No macro allocated so it generates no code
  191. #define CS_H // No macro allocated so it generates no code
  192. #else
  193. #if defined (TFT_PARALLEL_8_BIT)
  194. #if TFT_CS >= 32
  195. #define CS_L GPIO.out1_w1tc.val = (1 << (TFT_CS - 32))
  196. #define CS_H GPIO.out1_w1ts.val = (1 << (TFT_CS - 32))
  197. #elif TFT_CS >= 0
  198. #define CS_L GPIO.out_w1tc = (1 << TFT_CS)
  199. #define CS_H GPIO.out_w1ts = (1 << TFT_CS)
  200. #else
  201. #define CS_L
  202. #define CS_H
  203. #endif
  204. #else
  205. #if (TFT_CS >= 32)
  206. #ifdef RPI_DISPLAY_TYPE // RPi display needs a slower CS change
  207. #define CS_L GPIO.out1_w1ts.val = (1 << (TFT_CS - 32)); \
  208. GPIO.out1_w1tc.val = (1 << (TFT_CS - 32))
  209. #define CS_H GPIO.out1_w1tc.val = (1 << (TFT_CS - 32)); \
  210. GPIO.out1_w1ts.val = (1 << (TFT_CS - 32))
  211. #else
  212. #define CS_L GPIO.out1_w1tc.val = (1 << (TFT_CS - 32)); GPIO.out1_w1tc.val = (1 << (TFT_CS - 32))
  213. #define CS_H GPIO.out1_w1ts.val = (1 << (TFT_CS - 32))//;GPIO.out1_w1ts.val = (1 << (TFT_CS - 32))
  214. #endif
  215. #elif (TFT_CS >= 0)
  216. #ifdef RPI_DISPLAY_TYPE // RPi display needs a slower CS change
  217. #define CS_L GPIO.out_w1ts = (1 << TFT_CS); GPIO.out_w1tc = (1 << TFT_CS)
  218. #define CS_H GPIO.out_w1tc = (1 << TFT_CS); GPIO.out_w1ts = (1 << TFT_CS)
  219. #else
  220. #define CS_L GPIO.out_w1tc = (1 << TFT_CS); GPIO.out_w1tc = (1 << TFT_CS)
  221. #define CS_H GPIO.out_w1ts = (1 << TFT_CS)//;GPIO.out_w1ts = (1 << TFT_CS)
  222. #endif
  223. #else
  224. #define CS_L
  225. #define CS_H
  226. #endif
  227. #endif
  228. #endif
  229. ////////////////////////////////////////////////////////////////////////////////////////
  230. // Define the WR (TFT Write) pin drive code
  231. ////////////////////////////////////////////////////////////////////////////////////////
  232. #if defined (TFT_WR)
  233. #if (TFT_WR >= 32)
  234. // Note: it will be ~1.25x faster if the TFT_WR pin uses a GPIO pin lower than 32
  235. #define WR_L GPIO.out1_w1tc.val = (1 << (TFT_WR - 32))
  236. #define WR_H GPIO.out1_w1ts.val = (1 << (TFT_WR - 32))
  237. #elif (TFT_WR >= 0)
  238. // TFT_WR, for best performance, should be in range 0-31 for single register parallel write
  239. #define WR_L GPIO.out_w1tc = (1 << TFT_WR)
  240. #define WR_H GPIO.out_w1ts = (1 << TFT_WR)
  241. #else
  242. #define WR_L
  243. #define WR_H
  244. #endif
  245. #else
  246. #define WR_L
  247. #define WR_H
  248. #endif
  249. ////////////////////////////////////////////////////////////////////////////////////////
  250. // Define the touch screen chip select pin drive code
  251. ////////////////////////////////////////////////////////////////////////////////////////
  252. #ifndef TOUCH_CS
  253. #define T_CS_L // No macro allocated so it generates no code
  254. #define T_CS_H // No macro allocated so it generates no code
  255. #else // XPT2046 is slow, so use slower digitalWrite here
  256. #define T_CS_L digitalWrite(TOUCH_CS, LOW)
  257. #define T_CS_H digitalWrite(TOUCH_CS, HIGH)
  258. #endif
  259. ////////////////////////////////////////////////////////////////////////////////////////
  260. // Make sure SPI default pins are assigned if not specified by user or set to -1
  261. ////////////////////////////////////////////////////////////////////////////////////////
  262. #if !defined (TFT_PARALLEL_8_BIT)
  263. #ifdef USE_HSPI_PORT
  264. #ifndef TFT_MISO
  265. #define TFT_MISO -1
  266. #endif
  267. #ifndef TFT_MOSI
  268. #define TFT_MOSI 13
  269. #endif
  270. #if (TFT_MOSI == -1)
  271. #undef TFT_MOSI
  272. #define TFT_MOSI 13
  273. #endif
  274. #ifndef TFT_SCLK
  275. #define TFT_SCLK 14
  276. #endif
  277. #if (TFT_SCLK == -1)
  278. #undef TFT_SCLK
  279. #define TFT_SCLK 14
  280. #endif
  281. #else // VSPI port
  282. #ifndef TFT_MISO
  283. #define TFT_MISO -1
  284. #endif
  285. #ifndef TFT_MOSI
  286. #define TFT_MOSI 23
  287. #endif
  288. #if (TFT_MOSI == -1)
  289. #undef TFT_MOSI
  290. #define TFT_MOSI 23
  291. #endif
  292. #ifndef TFT_SCLK
  293. #define TFT_SCLK 18
  294. #endif
  295. #if (TFT_SCLK == -1)
  296. #undef TFT_SCLK
  297. #define TFT_SCLK 18
  298. #endif
  299. #if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32S2)
  300. #if (TFT_MISO == -1)
  301. #undef TFT_MISO
  302. #define TFT_MISO TFT_MOSI
  303. #endif
  304. #endif
  305. #endif
  306. #endif
  307. ////////////////////////////////////////////////////////////////////////////////////////
  308. // Define the parallel bus interface chip pin drive code
  309. ////////////////////////////////////////////////////////////////////////////////////////
  310. #if defined (TFT_PARALLEL_8_BIT)
  311. #if (TFT_D0 >= 32) // If D0 is a high GPIO assume all other data bits are high GPIO
  312. #define MASK_OFFSET 32
  313. #define GPIO_CLR_REG GPIO.out1_w1tc.val
  314. #define GPIO_SET_REG GPIO.out1_w1ts.val
  315. #else
  316. #define MASK_OFFSET 0
  317. #define GPIO_CLR_REG GPIO.out_w1tc
  318. #define GPIO_SET_REG GPIO.out_w1ts
  319. #endif
  320. // Create a bit set lookup table for data bus - wastes 1kbyte of RAM but speeds things up dramatically
  321. // can then use e.g. GPIO.out_w1ts = set_mask(0xFF); to set data bus to 0xFF
  322. #define PARALLEL_INIT_TFT_DATA_BUS \
  323. for (int32_t c = 0; c<256; c++) \
  324. { \
  325. xset_mask[c] = 0; \
  326. if ( c & 0x01 ) xset_mask[c] |= (1 << (TFT_D0-MASK_OFFSET)); \
  327. if ( c & 0x02 ) xset_mask[c] |= (1 << (TFT_D1-MASK_OFFSET)); \
  328. if ( c & 0x04 ) xset_mask[c] |= (1 << (TFT_D2-MASK_OFFSET)); \
  329. if ( c & 0x08 ) xset_mask[c] |= (1 << (TFT_D3-MASK_OFFSET)); \
  330. if ( c & 0x10 ) xset_mask[c] |= (1 << (TFT_D4-MASK_OFFSET)); \
  331. if ( c & 0x20 ) xset_mask[c] |= (1 << (TFT_D5-MASK_OFFSET)); \
  332. if ( c & 0x40 ) xset_mask[c] |= (1 << (TFT_D6-MASK_OFFSET)); \
  333. if ( c & 0x80 ) xset_mask[c] |= (1 << (TFT_D7-MASK_OFFSET)); \
  334. } \
  335. // Mask for the 8 data bits to set pin directions
  336. #define GPIO_DIR_MASK ((1 << (TFT_D0-MASK_OFFSET)) | (1 << (TFT_D1-MASK_OFFSET)) | (1 << (TFT_D2-MASK_OFFSET)) | (1 << (TFT_D3-MASK_OFFSET)) | (1 << (TFT_D4-MASK_OFFSET)) | (1 << (TFT_D5-MASK_OFFSET)) | (1 << (TFT_D6-MASK_OFFSET)) | (1 << (TFT_D7-MASK_OFFSET)))
  337. #if (TFT_WR >= 32)
  338. #if (TFT_D0 >= 32)
  339. // Data bits and the write line are cleared to 0 in one step (1.25x faster)
  340. #define GPIO_OUT_CLR_MASK (GPIO_DIR_MASK | (1 << (TFT_WR-32)))
  341. #elif (TFT_D0 >= 0)
  342. // Data bits and the write line are cleared sequentially
  343. #define GPIO_OUT_CLR_MASK (GPIO_DIR_MASK); WR_L
  344. #endif
  345. #elif (TFT_WR >= 0)
  346. #if (TFT_D0 >= 32)
  347. // Data bits and the write line are cleared sequentially
  348. #define GPIO_OUT_CLR_MASK (GPIO_DIR_MASK); WR_L
  349. #elif (TFT_D0 >= 0)
  350. // Data bits and the write line are cleared to 0 in one step (1.25x faster)
  351. #define GPIO_OUT_CLR_MASK (GPIO_DIR_MASK | (1 << TFT_WR))
  352. #endif
  353. #else
  354. #define GPIO_OUT_CLR_MASK
  355. #endif
  356. // A lookup table is used to set the different bit patterns, this uses 1kByte of RAM
  357. #define set_mask(C) xset_mask[C] // 63fps Sprite rendering test 33% faster, graphicstest only 1.8% faster than shifting in real time
  358. // Real-time shifting alternative to above to save 1KByte RAM, 47 fps Sprite rendering test
  359. /*#define set_mask(C) (((C)&0x80)>>7)<<TFT_D7 | (((C)&0x40)>>6)<<TFT_D6 | (((C)&0x20)>>5)<<TFT_D5 | (((C)&0x10)>>4)<<TFT_D4 | \
  360. (((C)&0x08)>>3)<<TFT_D3 | (((C)&0x04)>>2)<<TFT_D2 | (((C)&0x02)>>1)<<TFT_D1 | (((C)&0x01)>>0)<<TFT_D0
  361. //*/
  362. // Write 8 bits to TFT
  363. #define tft_Write_8(C) GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t)(C)); WR_H
  364. #if defined (SSD1963_DRIVER)
  365. // Write 18 bit color to TFT
  366. #define tft_Write_16(C) GPIO.out_w1tc = GPIO_OUT_CLR_MASK; GPIO.out_w1ts = set_mask((uint8_t) (((C) & 0xF800)>> 8)); WR_H; \
  367. GPIO.out_w1tc = GPIO_OUT_CLR_MASK; GPIO.out_w1ts = set_mask((uint8_t) (((C) & 0x07E0)>> 3)); WR_H; \
  368. GPIO.out_w1tc = GPIO_OUT_CLR_MASK; GPIO.out_w1ts = set_mask((uint8_t) (((C) & 0x001F)<< 3)); WR_H
  369. // 18 bit color write with swapped bytes
  370. #define tft_Write_16S(C) Cswap = ((C) >>8 | (C) << 8); tft_Write_16(Cswap)
  371. #else
  372. #ifdef PSEUDO_16_BIT
  373. // One write strobe for both bytes
  374. #define tft_Write_16(C) GPIO.out_w1tc = GPIO_OUT_CLR_MASK; GPIO.out_w1ts = set_mask((uint8_t) ((C) >> 0)); WR_H
  375. #define tft_Write_16S(C) GPIO.out_w1tc = GPIO_OUT_CLR_MASK; GPIO.out_w1ts = set_mask((uint8_t) ((C) >> 8)); WR_H
  376. #else
  377. // Write 16 bits to TFT
  378. #define tft_Write_16(C) GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 8)); WR_H; \
  379. GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 0)); WR_H
  380. // 16 bit write with swapped bytes
  381. #define tft_Write_16S(C) GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 0)); WR_H; \
  382. GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 8)); WR_H
  383. #endif
  384. #endif
  385. // Write 32 bits to TFT
  386. #define tft_Write_32(C) GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 24)); WR_H; \
  387. GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 16)); WR_H; \
  388. GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 8)); WR_H; \
  389. GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 0)); WR_H
  390. // Write two concatenated 16 bit values to TFT
  391. #define tft_Write_32C(C,D) GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 8)); WR_H; \
  392. GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 0)); WR_H; \
  393. GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((D) >> 8)); WR_H; \
  394. GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((D) >> 0)); WR_H
  395. // Write 16 bit value twice to TFT - used by drawPixel()
  396. #define tft_Write_32D(C) GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 8)); WR_H; \
  397. GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 0)); WR_H; \
  398. GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 8)); WR_H; \
  399. GPIO_CLR_REG = GPIO_OUT_CLR_MASK; GPIO_SET_REG = set_mask((uint8_t) ((C) >> 0)); WR_H
  400. // Read pin
  401. #ifdef TFT_RD
  402. #if (TFT_RD >= 32)
  403. #define RD_L GPIO.out1_w1tc.val = (1 << (TFT_RD - 32))
  404. #define RD_H GPIO.out1_w1ts.val = (1 << (TFT_RD - 32))
  405. #elif (TFT_RD >= 0)
  406. #define RD_L GPIO.out_w1tc = (1 << TFT_RD)
  407. //#define RD_L digitalWrite(TFT_WR, LOW)
  408. #define RD_H GPIO.out_w1ts = (1 << TFT_RD)
  409. //#define RD_H digitalWrite(TFT_WR, HIGH)
  410. #else
  411. #define RD_L
  412. #define RD_H
  413. #endif
  414. #else
  415. #define TFT_RD -1
  416. #define RD_L
  417. #define RD_H
  418. #endif
  419. ////////////////////////////////////////////////////////////////////////////////////////
  420. // Macros to write commands/pixel colour data to a SPI ILI948x TFT
  421. ////////////////////////////////////////////////////////////////////////////////////////
  422. #elif defined (SPI_18BIT_DRIVER) // SPI 18 bit colour
  423. // Write 8 bits to TFT
  424. #define tft_Write_8(C) spi.transfer(C)
  425. // Convert 16 bit colour to 18 bit and write in 3 bytes
  426. #define tft_Write_16(C) spi.transfer(((C) & 0xF800)>>8); \
  427. spi.transfer(((C) & 0x07E0)>>3); \
  428. spi.transfer(((C) & 0x001F)<<3)
  429. // Future option for transfer without wait
  430. #define tft_Write_16N(C) tft_Write_16(C)
  431. // Convert swapped byte 16 bit colour to 18 bit and write in 3 bytes
  432. #define tft_Write_16S(C) spi.transfer((C) & 0xF8); \
  433. spi.transfer(((C) & 0xE000)>>11 | ((C) & 0x07)<<5); \
  434. spi.transfer(((C) & 0x1F00)>>5)
  435. // Write 32 bits to TFT
  436. #define tft_Write_32(C) spi.write32(C)
  437. // Write two concatenated 16 bit values to TFT
  438. #define tft_Write_32C(C,D) spi.write32((C)<<16 | (D))
  439. // Write 16 bit value twice to TFT
  440. #define tft_Write_32D(C) spi.write32((C)<<16 | (C))
  441. ////////////////////////////////////////////////////////////////////////////////////////
  442. // Macros to write commands/pixel colour data to an Raspberry Pi TFT
  443. ////////////////////////////////////////////////////////////////////////////////////////
  444. #elif defined (RPI_DISPLAY_TYPE)
  445. // ESP32 low level SPI writes for 8, 16 and 32 bit values
  446. // to avoid the function call overhead
  447. #define TFT_WRITE_BITS(D, B) \
  448. WRITE_PERI_REG(SPI_MOSI_DLEN_REG(SPI_PORT), B-1); \
  449. WRITE_PERI_REG(SPI_W0_REG(SPI_PORT), D); \
  450. SET_PERI_REG_MASK(SPI_CMD_REG(SPI_PORT), SPI_USR); \
  451. while (READ_PERI_REG(SPI_CMD_REG(SPI_PORT))&SPI_USR);
  452. // Write 8 bits
  453. #define tft_Write_8(C) TFT_WRITE_BITS((C)<<8, 16)
  454. // Write 16 bits with corrected endianness for 16 bit colours
  455. #define tft_Write_16(C) TFT_WRITE_BITS((C)<<8 | (C)>>8, 16)
  456. // Future option for transfer without wait
  457. #define tft_Write_16N(C) tft_Write_16(C)
  458. // Write 16 bits
  459. #define tft_Write_16S(C) TFT_WRITE_BITS(C, 16)
  460. // Write 32 bits
  461. #define tft_Write_32(C) TFT_WRITE_BITS(C, 32)
  462. // Write two address coordinates
  463. #define tft_Write_32C(C,D) TFT_WRITE_BITS((C)<<24 | (C), 32); \
  464. TFT_WRITE_BITS((D)<<24 | (D), 32)
  465. // Write same value twice
  466. #define tft_Write_32D(C) tft_Write_32C(C,C)
  467. ////////////////////////////////////////////////////////////////////////////////////////
  468. // Macros for all other SPI displays
  469. ////////////////////////////////////////////////////////////////////////////////////////
  470. #else
  471. /* Old macros
  472. // ESP32 low level SPI writes for 8, 16 and 32 bit values
  473. // to avoid the function call overhead
  474. #define TFT_WRITE_BITS(D, B) \
  475. WRITE_PERI_REG(SPI_MOSI_DLEN_REG(SPI_PORT), B-1); \
  476. WRITE_PERI_REG(SPI_W0_REG(SPI_PORT), D); \
  477. SET_PERI_REG_MASK(SPI_CMD_REG(SPI_PORT), SPI_USR); \
  478. while (READ_PERI_REG(SPI_CMD_REG(SPI_PORT))&SPI_USR);
  479. // Write 8 bits
  480. #define tft_Write_8(C) TFT_WRITE_BITS(C, 8)
  481. // Write 16 bits with corrected endianness for 16 bit colours
  482. #define tft_Write_16(C) TFT_WRITE_BITS((C)<<8 | (C)>>8, 16)
  483. // Write 16 bits
  484. #define tft_Write_16S(C) TFT_WRITE_BITS(C, 16)
  485. // Write 32 bits
  486. #define tft_Write_32(C) TFT_WRITE_BITS(C, 32)
  487. // Write two address coordinates
  488. #define tft_Write_32C(C,D) TFT_WRITE_BITS((uint16_t)((D)<<8 | (D)>>8)<<16 | (uint16_t)((C)<<8 | (C)>>8), 32)
  489. // Write same value twice
  490. #define tft_Write_32D(C) TFT_WRITE_BITS((uint16_t)((C)<<8 | (C)>>8)<<16 | (uint16_t)((C)<<8 | (C)>>8), 32)
  491. //*/
  492. //* Replacement slimmer macros
  493. #if !defined(CONFIG_IDF_TARGET_ESP32S3)
  494. #define TFT_WRITE_BITS(D, B) *_spi_mosi_dlen = B-1; \
  495. *_spi_w = D; \
  496. *_spi_cmd = SPI_USR; \
  497. while (*_spi_cmd & SPI_USR);
  498. #else
  499. #define TFT_WRITE_BITS(D, B) *_spi_mosi_dlen = B-1; \
  500. *_spi_w = D; \
  501. *_spi_cmd = SPI_UPDATE; \
  502. while (*_spi_cmd & SPI_UPDATE); \
  503. *_spi_cmd = SPI_USR; \
  504. while (*_spi_cmd & SPI_USR);
  505. #endif
  506. // Write 8 bits
  507. #define tft_Write_8(C) TFT_WRITE_BITS(C, 8)
  508. // Write 16 bits with corrected endianness for 16 bit colours
  509. #define tft_Write_16(C) TFT_WRITE_BITS((C)<<8 | (C)>>8, 16)
  510. // Future option for transfer without wait
  511. #if !defined(CONFIG_IDF_TARGET_ESP32S3)
  512. #define tft_Write_16N(C) *_spi_mosi_dlen = 16-1; \
  513. *_spi_w = ((C)<<8 | (C)>>8); \
  514. *_spi_cmd = SPI_USR;
  515. #else
  516. #define tft_Write_16N(C) *_spi_mosi_dlen = 16-1; \
  517. *_spi_w = ((C)<<8 | (C)>>8); \
  518. *_spi_cmd = SPI_UPDATE; \
  519. while (*_spi_cmd & SPI_UPDATE); \
  520. *_spi_cmd = SPI_USR;
  521. #endif
  522. // Write 16 bits
  523. #define tft_Write_16S(C) TFT_WRITE_BITS(C, 16)
  524. // Write 32 bits
  525. #define tft_Write_32(C) TFT_WRITE_BITS(C, 32)
  526. // Write two address coordinates
  527. #define tft_Write_32C(C,D) TFT_WRITE_BITS((uint16_t)((D)<<8 | (D)>>8)<<16 | (uint16_t)((C)<<8 | (C)>>8), 32)
  528. // Write same value twice
  529. #define tft_Write_32D(C) TFT_WRITE_BITS((uint16_t)((C)<<8 | (C)>>8)<<16 | (uint16_t)((C)<<8 | (C)>>8), 32)
  530. //*/
  531. #endif
  532. #ifndef tft_Write_16N
  533. #define tft_Write_16N tft_Write_16
  534. #endif
  535. ////////////////////////////////////////////////////////////////////////////////////////
  536. // Macros to read from display using SPI or software SPI
  537. ////////////////////////////////////////////////////////////////////////////////////////
  538. #if !defined (TFT_PARALLEL_8_BIT)
  539. // Read from display using SPI or software SPI
  540. // Use a SPI read transfer
  541. #define tft_Read_8() spi.transfer(0)
  542. #endif
  543. // Concatenate a byte sequence A,B,C,D to CDAB, P is a uint8_t pointer
  544. #define DAT8TO32(P) ( (uint32_t)P[0]<<8 | P[1] | P[2]<<24 | P[3]<<16 )
  545. #endif // Header end