PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

HW_Teensy3.h 13KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * Set your pins here
  3. * For 8-bit data bus, pins DB_8 to DB_15 are used.
  4. *
  5. * Teensy 3.0 pin definitions created by Dawnmist
  6. * http://forum.pjrc.com/threads/18002-Teensy-3-0-driving-an-SSD1289-with-utft?p=34719&viewfull=1#post34719
  7. *
  8. * There's 3 alternative sets of pin definitions that can be used with the
  9. * Teensy 3 using this header file. Which one is used is controlled by setting
  10. * the PORTS define to one of "USE_C_D_PORTS", "USE_B_D_PORTS" or
  11. * "USE_USER_PORTS".
  12. *
  13. * For LCDs with 16 bit data busses:
  14. *
  15. * If PORTS = USE_B_D_PORTS:
  16. * - Port B 0-3,16-19 and Port D 0-7 will be used. This is slightly slower
  17. * than USE_C_D_PORTS, because we need to do the write to port B in 2 steps
  18. * (clear pins 0-3,16-19, then set those pins to match our data). Port D
  19. * access is still a single write. This means it takes 3 writes to the pins
  20. * instead of the 2 used in USE_C_D_PORTS - but it leaves the SPI port pins
  21. * available for use, so gives the fastest result when you need to use an
  22. * SD Card/something on the SPI bus as well.
  23. * Port D (0-7) = DB_8=2, DB_9=14, DB_10=7, DB_11=8, DB_12=6, DB_13=20, DB_14=21, DB_15=5
  24. * Port B (0-3,16-19) = DB_0=16, DB_1=17, DB_2=19, DB_3=18, DB_4=0, DB_5=1, DB_6=32, DB_7=25
  25. *
  26. * If PORTS = USE_C_D_PORTS:
  27. * - Port C 0-7 and Port D 0-7 will be used. Because these are the bottom
  28. * 8 pins of each port, we can write to them in one write cycle each. This
  29. * is the fastest screen data write - but Port C's pins conflict with the
  30. * SPI port, so you won't have access to hardware SPI. If you're using an
  31. * SD Card, you will *need* the hardware SPI. This setting should only be
  32. * used when hardware SPI is *not* needed.
  33. * Port D (0-7) = DB_8=2, DB_9=14, DB_10=7, DB_11=8, DB_12=6, DB_13=20, DB_14=21, DB_15=5
  34. * Port C (0-7) = DB_0=15, DB_1=22, DB_2=23, DB_3=9, DB_4=10, DB_5=13, DB_6=11, DB_7=12
  35. *
  36. * If PORTS = USE_USER_PORTS
  37. * - This allows you to define your own set of pins to use. Since we're not
  38. * collating them into groupings that can have their writes optimized, it
  39. * will take 16 write cycles to write out the data for one pixel. However,
  40. * it also gives you the most flexibility in which pins you use for the
  41. * LCD data bus. If high-speed/optimized data writes is not important to you
  42. * but simplicity of wiring layout is (or if the above options conflict with
  43. * something else you're using on the teensy), you can use this setting to
  44. * set the pins where-ever you'd like them to be. By default it's set to
  45. * pins DB0-DB15 = 0-15. You should modify the "DB_0" through to "DB_15"
  46. * defines to match the pins you wish to use.
  47. *
  48. * For LCDs with 8 bit data busses:
  49. * Use the pin defines for DB_8 to DB_15 to connect to your LCD. This puts them
  50. * on Port D in both the USE_C_D_PORTS and USE_B_D_PORTS settings, making those
  51. * two settings equivalent (and a single write to set the pins on the bus).
  52. */
  53. #define USE_USER_PORTS 0
  54. #define USE_C_D_PORTS 1
  55. #define USE_B_D_PORTS 2
  56. // SET WHICH PIN DEFINITIONS TO USE HERE
  57. // (only uncomment 1 of these 3 lines)
  58. //
  59. #define PORTS USE_B_D_PORTS
  60. //#define PORTS USE_C_D_PORTS
  61. //#define PORTS USE_USER_PORTS
  62. #if (PORTS == USE_USER_PORTS)
  63. #pragma message("Using user-defined pins")
  64. #define DB_0 0
  65. #define DB_1 1
  66. #define DB_2 2
  67. #define DB_3 3
  68. #define DB_4 4
  69. #define DB_5 5
  70. #define DB_6 6
  71. #define DB_7 7
  72. #define DB_8 8
  73. #define DB_9 9
  74. #define DB_10 10
  75. #define DB_11 11
  76. #define DB_12 12
  77. #define DB_13 13
  78. #define DB_14 14
  79. #define DB_15 15
  80. #elif (PORTS == USE_C_D_PORTS)
  81. #pragma message("Using Ports C&D - pins should be connected to:")
  82. #pragma message ("DB_0=15, DB_1=22, DB_2=23, DB_3=9, DB_4=10, DB_5=13, DB_6=11, DB_7=12")
  83. #pragma message ("DB_8=2, DB_9=14, DB_10=7, DB_11=8, DB_12=6, DB_13=20, DB_14=21, DB_15=5")
  84. #pragma message ("If using a display with an 8-bit bus, use connections to DB_8 to DB_15")
  85. #define DB_0 15
  86. #define DB_1 22
  87. #define DB_2 23
  88. #define DB_3 9
  89. #define DB_4 10
  90. #define DB_5 13
  91. #define DB_6 11
  92. #define DB_7 12
  93. #define DB_8 2
  94. #define DB_9 14
  95. #define DB_10 7
  96. #define DB_11 8
  97. #define DB_12 6
  98. #define DB_13 20
  99. #define DB_14 21
  100. #define DB_15 5
  101. #elif (PORTS == USE_B_D_PORTS)
  102. #pragma message("Using Ports B&D - pins should be connected to:")
  103. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  104. #pragma message ("DB_0=16, DB_1=17, DB_2=19, DB_3=18, DB_4=0, DB_5=1, DB_6=29, DB_7=30")
  105. #else
  106. #pragma message ("DB_0=16, DB_1=17, DB_2=19, DB_3=18, DB_4=0, DB_5=1, DB_6=32, DB_7=25")
  107. #endif
  108. #pragma message ("DB_8=2, DB_9=14, DB_10=7, DB_11=8, DB_12=6, DB_13=20, DB_14=21, DB_15=5")
  109. #pragma message ("If using a display with an 8-bit bus, use connections to DB_8 to DB_15")
  110. #define DB_0 16
  111. #define DB_1 17
  112. #define DB_2 19
  113. #define DB_3 18
  114. #define DB_4 0
  115. #define DB_5 1
  116. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  117. #define DB_6 29
  118. #define DB_7 30
  119. #else
  120. #define DB_6 32
  121. #define DB_7 25
  122. #endif
  123. #define DB_8 2
  124. #define DB_9 14
  125. #define DB_10 7
  126. #define DB_11 8
  127. #define DB_12 6
  128. #define DB_13 20
  129. #define DB_14 21
  130. #define DB_15 5
  131. #endif
  132. // *** Hardware specific functions ***
  133. void UTFT::_hw_special_init()
  134. {
  135. }
  136. void UTFT::LCD_Writ_Bus(char ch,char cl, byte mode)
  137. {
  138. switch (mode)
  139. {
  140. case 1:
  141. // Not implemented
  142. break;
  143. case 8:
  144. #if (PORTS == USE_C_D_PORTS) || (PORTS == USE_B_D_PORTS)
  145. *(volatile uint8_t *)(&GPIOD_PDOR) = ch;
  146. pulse_low(P_WR, B_WR);
  147. *(volatile uint8_t *)(&GPIOD_PDOR) = cl;
  148. pulse_low(P_WR, B_WR);
  149. #else
  150. ((ch & 0x80) != 0) ? digitalWriteFast(DB_15, HIGH) : digitalWriteFast(DB_15, LOW);
  151. ((ch & 0x40) != 0) ? digitalWriteFast(DB_14, HIGH) : digitalWriteFast(DB_14, LOW);
  152. ((ch & 0x20) != 0) ? digitalWriteFast(DB_13, HIGH) : digitalWriteFast(DB_13, LOW);
  153. ((ch & 0x10) != 0) ? digitalWriteFast(DB_12, HIGH) : digitalWriteFast(DB_12, LOW);
  154. ((ch & 0x08) != 0) ? digitalWriteFast(DB_11, HIGH) : digitalWriteFast(DB_11, LOW);
  155. ((ch & 0x04) != 0) ? digitalWriteFast(DB_10, HIGH) : digitalWriteFast(DB_10, LOW);
  156. ((ch & 0x02) != 0) ? digitalWriteFast(DB_9, HIGH) : digitalWriteFast(DB_9, LOW);
  157. ((ch & 0x01) != 0) ? digitalWriteFast(DB_8, HIGH) : digitalWriteFast(DB_8, LOW);
  158. pulse_low(P_WR, B_WR);
  159. ((cl & 0x80) != 0) ? digitalWriteFast(DB_15, HIGH) : digitalWriteFast(DB_15, LOW);
  160. ((cl & 0x40) != 0) ? digitalWriteFast(DB_14, HIGH) : digitalWriteFast(DB_14, LOW);
  161. ((cl & 0x20) != 0) ? digitalWriteFast(DB_13, HIGH) : digitalWriteFast(DB_13, LOW);
  162. ((cl & 0x10) != 0) ? digitalWriteFast(DB_12, HIGH) : digitalWriteFast(DB_12, LOW);
  163. ((cl & 0x08) != 0) ? digitalWriteFast(DB_11, HIGH) : digitalWriteFast(DB_11, LOW);
  164. ((cl & 0x04) != 0) ? digitalWriteFast(DB_10, HIGH) : digitalWriteFast(DB_10, LOW);
  165. ((cl & 0x02) != 0) ? digitalWriteFast(DB_9, HIGH) : digitalWriteFast(DB_9, LOW);
  166. ((cl & 0x01) != 0) ? digitalWriteFast(DB_8, HIGH) : digitalWriteFast(DB_8, LOW);
  167. pulse_low(P_WR, B_WR);
  168. #endif
  169. break;
  170. case 16:
  171. #if (PORTS == USE_C_D_PORTS)
  172. *(volatile uint8_t *)(&GPIOD_PDOR) = ch;
  173. *(volatile uint8_t *)(&GPIOC_PDOR) = cl;
  174. #elif (PORTS == USE_B_D_PORTS)
  175. *(volatile uint8_t *)(&GPIOD_PDOR) = ch;
  176. // clear data lines B0-3,B16-19
  177. GPIOB_PCOR = 0x000F000F;
  178. // set data lines 0-3,16-19 if set in cl
  179. GPIOB_PSOR = (0x0F & cl) | ((cl >> 4) << 16);
  180. #else
  181. ((ch & 0x80) != 0) ? digitalWriteFast(DB_15, HIGH) : digitalWriteFast(DB_15, LOW);
  182. ((ch & 0x40) != 0) ? digitalWriteFast(DB_14, HIGH) : digitalWriteFast(DB_14, LOW);
  183. ((ch & 0x20) != 0) ? digitalWriteFast(DB_13, HIGH) : digitalWriteFast(DB_13, LOW);
  184. ((ch & 0x10) != 0) ? digitalWriteFast(DB_12, HIGH) : digitalWriteFast(DB_12, LOW);
  185. ((ch & 0x08) != 0) ? digitalWriteFast(DB_11, HIGH) : digitalWriteFast(DB_11, LOW);
  186. ((ch & 0x04) != 0) ? digitalWriteFast(DB_10, HIGH) : digitalWriteFast(DB_10, LOW);
  187. ((ch & 0x02) != 0) ? digitalWriteFast(DB_9, HIGH) : digitalWriteFast(DB_9, LOW);
  188. ((ch & 0x01) != 0) ? digitalWriteFast(DB_8, HIGH) : digitalWriteFast(DB_8, LOW);
  189. ((cl & 0x80) != 0) ? digitalWriteFast(DB_7, HIGH) : digitalWriteFast(DB_7, LOW);
  190. ((cl & 0x40) != 0) ? digitalWriteFast(DB_6, HIGH) : digitalWriteFast(DB_6, LOW);
  191. ((cl & 0x20) != 0) ? digitalWriteFast(DB_5, HIGH) : digitalWriteFast(DB_5, LOW);
  192. ((cl & 0x10) != 0) ? digitalWriteFast(DB_4, HIGH) : digitalWriteFast(DB_4, LOW);
  193. ((cl & 0x08) != 0) ? digitalWriteFast(DB_3, HIGH) : digitalWriteFast(DB_3, LOW);
  194. ((cl & 0x04) != 0) ? digitalWriteFast(DB_2, HIGH) : digitalWriteFast(DB_2, LOW);
  195. ((cl & 0x02) != 0) ? digitalWriteFast(DB_1, HIGH) : digitalWriteFast(DB_1, LOW);
  196. ((cl & 0x01) != 0) ? digitalWriteFast(DB_0, HIGH) : digitalWriteFast(DB_0, LOW);
  197. #endif
  198. pulse_low(P_WR, B_WR);
  199. break;
  200. }
  201. }
  202. void UTFT::_set_direction_registers(byte mode)
  203. {
  204. pinMode(DB_8, OUTPUT);
  205. pinMode(DB_9, OUTPUT);
  206. pinMode(DB_10, OUTPUT);
  207. pinMode(DB_11, OUTPUT);
  208. pinMode(DB_12, OUTPUT);
  209. pinMode(DB_13, OUTPUT);
  210. pinMode(DB_14, OUTPUT);
  211. pinMode(DB_15, OUTPUT);
  212. if (mode == 16)
  213. {
  214. pinMode(DB_0, OUTPUT);
  215. pinMode(DB_1, OUTPUT);
  216. pinMode(DB_2, OUTPUT);
  217. pinMode(DB_3, OUTPUT);
  218. pinMode(DB_4, OUTPUT);
  219. pinMode(DB_5, OUTPUT);
  220. pinMode(DB_6, OUTPUT);
  221. pinMode(DB_7, OUTPUT);
  222. }
  223. }
  224. void UTFT::_fast_fill_16(int ch, int cl, long pix)
  225. {
  226. long blocks;
  227. #if (PORTS == USE_C_D_PORTS)
  228. *(volatile uint8_t *)(&GPIOD_PDOR) = ch;
  229. *(volatile uint8_t *)(&GPIOC_PDOR) = cl;
  230. #elif (PORTS == USE_B_D_PORTS)
  231. *(volatile uint8_t *)(&GPIOD_PDOR) = ch;
  232. // clear data lines B0-3,B16-19
  233. GPIOB_PCOR = 0x000F000F;
  234. // set data lines 0-3,16-19 if set in cl
  235. GPIOB_PSOR = (0x0F & cl) | ((cl >> 4) << 16);
  236. #else
  237. ((ch & 0x80) != 0) ? digitalWriteFast(DB_15, HIGH) : digitalWriteFast(DB_15, LOW);
  238. ((ch & 0x40) != 0) ? digitalWriteFast(DB_14, HIGH) : digitalWriteFast(DB_14, LOW);
  239. ((ch & 0x20) != 0) ? digitalWriteFast(DB_13, HIGH) : digitalWriteFast(DB_13, LOW);
  240. ((ch & 0x10) != 0) ? digitalWriteFast(DB_12, HIGH) : digitalWriteFast(DB_12, LOW);
  241. ((ch & 0x08) != 0) ? digitalWriteFast(DB_11, HIGH) : digitalWriteFast(DB_11, LOW);
  242. ((ch & 0x04) != 0) ? digitalWriteFast(DB_10, HIGH) : digitalWriteFast(DB_10, LOW);
  243. ((ch & 0x02) != 0) ? digitalWriteFast(DB_9, HIGH) : digitalWriteFast(DB_9, LOW);
  244. ((ch & 0x01) != 0) ? digitalWriteFast(DB_8, HIGH) : digitalWriteFast(DB_8, LOW);
  245. ((cl & 0x80) != 0) ? digitalWriteFast(DB_7, HIGH) : digitalWriteFast(DB_7, LOW);
  246. ((cl & 0x40) != 0) ? digitalWriteFast(DB_6, HIGH) : digitalWriteFast(DB_6, LOW);
  247. ((cl & 0x20) != 0) ? digitalWriteFast(DB_5, HIGH) : digitalWriteFast(DB_5, LOW);
  248. ((cl & 0x10) != 0) ? digitalWriteFast(DB_4, HIGH) : digitalWriteFast(DB_4, LOW);
  249. ((cl & 0x08) != 0) ? digitalWriteFast(DB_3, HIGH) : digitalWriteFast(DB_3, LOW);
  250. ((cl & 0x04) != 0) ? digitalWriteFast(DB_2, HIGH) : digitalWriteFast(DB_2, LOW);
  251. ((cl & 0x02) != 0) ? digitalWriteFast(DB_1, HIGH) : digitalWriteFast(DB_1, LOW);
  252. ((cl & 0x01) != 0) ? digitalWriteFast(DB_0, HIGH) : digitalWriteFast(DB_0, LOW);
  253. #endif
  254. blocks = pix/16;
  255. for (int i=0; i<blocks; i++)
  256. {
  257. pulse_low(P_WR, B_WR);
  258. pulse_low(P_WR, B_WR);
  259. pulse_low(P_WR, B_WR);
  260. pulse_low(P_WR, B_WR);
  261. pulse_low(P_WR, B_WR);
  262. pulse_low(P_WR, B_WR);
  263. pulse_low(P_WR, B_WR);
  264. pulse_low(P_WR, B_WR);
  265. pulse_low(P_WR, B_WR);
  266. pulse_low(P_WR, B_WR);
  267. pulse_low(P_WR, B_WR);
  268. pulse_low(P_WR, B_WR);
  269. pulse_low(P_WR, B_WR);
  270. pulse_low(P_WR, B_WR);
  271. pulse_low(P_WR, B_WR);
  272. pulse_low(P_WR, B_WR);
  273. }
  274. if ((pix % 16) != 0)
  275. for (int i=0; i<(pix % 16); i++)
  276. {
  277. pulse_low(P_WR, B_WR);
  278. }
  279. }
  280. void UTFT::_fast_fill_8(int ch, long pix)
  281. {
  282. long blocks;
  283. #if (PORTS == USE_C_D_PORTS) || (PORTS == USE_B_D_PORTS)
  284. *(volatile uint8_t *)(&GPIOD_PDOR) = ch;
  285. #else
  286. ((ch & 0x80) != 0) ? digitalWriteFast(DB_15, HIGH) : digitalWriteFast(DB_15, LOW);
  287. ((ch & 0x40) != 0) ? digitalWriteFast(DB_14, HIGH) : digitalWriteFast(DB_14, LOW);
  288. ((ch & 0x20) != 0) ? digitalWriteFast(DB_13, HIGH) : digitalWriteFast(DB_13, LOW);
  289. ((ch & 0x10) != 0) ? digitalWriteFast(DB_12, HIGH) : digitalWriteFast(DB_12, LOW);
  290. ((ch & 0x08) != 0) ? digitalWriteFast(DB_11, HIGH) : digitalWriteFast(DB_11, LOW);
  291. ((ch & 0x04) != 0) ? digitalWriteFast(DB_10, HIGH) : digitalWriteFast(DB_10, LOW);
  292. ((ch & 0x02) != 0) ? digitalWriteFast(DB_9, HIGH) : digitalWriteFast(DB_9, LOW);
  293. ((ch & 0x01) != 0) ? digitalWriteFast(DB_8, HIGH) : digitalWriteFast(DB_8, LOW);
  294. #endif
  295. blocks = pix/16;
  296. for (int i=0; i<blocks; i++)
  297. {
  298. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  299. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  300. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  301. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  302. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  303. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  304. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  305. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  306. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  307. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  308. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  309. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  310. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  311. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  312. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  313. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  314. }
  315. if ((pix % 16) != 0)
  316. for (int i=0; i<(pix % 16); i++)
  317. {
  318. pulse_low(P_WR, B_WR);pulse_low(P_WR, B_WR);
  319. }
  320. }