u8x8_d_st7302.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. u8x8_d_st7302.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2024, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. st7302:
  27. Ultra-Low Power Active Matrix 240 x 320
  28. Mono TFT Display Driver with Controller
  29. https://github.com/olikraus/u8g2/issues/2436
  30. No Hardware Flip
  31. No U8x8 Support
  32. */
  33. #include "u8x8.h"
  34. #include <string.h>
  35. static const uint8_t u8x8_d_st7302_122x250_powersave0_seq[] = {
  36. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  37. U8X8_C(0x29), // display on
  38. U8X8_END_TRANSFER(), /* disable chip */
  39. U8X8_END() /* end of sequence */
  40. };
  41. static const uint8_t u8x8_d_st7302_122x250_powersave1_seq[] = {
  42. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  43. U8X8_C(0x028), /* display off */
  44. U8X8_END_TRANSFER(), /* disable chip */
  45. U8X8_END() /* end of sequence */
  46. };
  47. static const uint8_t u8x8_d_st7302_122x250_flip0_seq[] = {
  48. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  49. //U8X8_CA(0x36, 0x60), // Memory Control
  50. U8X8_END_TRANSFER(), /* disable chip */
  51. U8X8_END() /* end of sequence */
  52. };
  53. static const uint8_t u8x8_d_st7302_122x250_flip1_seq[] = {
  54. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  55. //U8X8_CA(0x36, 0xa0), // Memory Control
  56. U8X8_END_TRANSFER(), /* disable chip */
  57. U8X8_END() /* end of sequence */
  58. };
  59. /*===================================================*/
  60. /* see also: http://www.technoblogy.com/show?3YB0 */
  61. #ifdef NOT_USED
  62. static uint8_t *u8x8_st7302_convert_60(u8x8_t *u8x8, uint8_t *p)
  63. {
  64. static uint8_t buf[6];
  65. uint8_t bytes_per_row = u8x8->display_info->tile_width;
  66. memset(buf, 0, 6);
  67. // U8X8_CA(0x36, 0x60), // Memory Control
  68. // first row, left 12 pixel
  69. if ( p[0] & 0x80 )
  70. buf[2] |= 0x02;
  71. if ( p[0] & 0x40 )
  72. buf[2] |= 0x08;
  73. if ( p[0] & 0x20 )
  74. buf[2] |= 0x20;
  75. if ( p[0] & 0x10 )
  76. buf[2] |= 0x80;
  77. if ( p[0] & 0x08 )
  78. buf[1] |= 0x02;
  79. if ( p[0] & 0x04 )
  80. buf[1] |= 0x08;
  81. if ( p[0] & 0x02 )
  82. buf[1] |= 0x20;
  83. if ( p[0] & 0x01 )
  84. buf[1] |= 0x80;
  85. if ( p[1] & 0x80 )
  86. buf[0] |= 0x02;
  87. if ( p[1] & 0x40 )
  88. buf[0] |= 0x08;
  89. if ( p[1] & 0x20 )
  90. buf[0] |= 0x20;
  91. if ( p[1] & 0x10 )
  92. buf[0] |= 0x80;
  93. // first row, right 12 pixel
  94. if ( p[1] & 0x08 )
  95. buf[5] |= 0x02;
  96. if ( p[1] & 0x04 )
  97. buf[5] |= 0x08;
  98. if ( p[1] & 0x02 )
  99. buf[5] |= 0x20;
  100. if ( p[1] & 0x01 )
  101. buf[5] |= 0x80;
  102. if ( p[2] & 0x80 )
  103. buf[4] |= 0x02;
  104. if ( p[2] & 0x40 )
  105. buf[4] |= 0x08;
  106. if ( p[2] & 0x20 )
  107. buf[4] |= 0x20;
  108. if ( p[2] & 0x10 )
  109. buf[4] |= 0x80;
  110. if ( p[2] & 0x08 )
  111. buf[3] |= 0x02;
  112. if ( p[2] & 0x04 )
  113. buf[3] |= 0x08;
  114. if ( p[2] & 0x02 )
  115. buf[3] |= 0x20;
  116. if ( p[2] & 0x01 )
  117. buf[3] |= 0x80;
  118. p += u8x8->display_info->tile_width;
  119. // second row, left 12 pixel
  120. if ( p[0] & 0x80 )
  121. buf[2] |= 0x01;
  122. if ( p[0] & 0x40 )
  123. buf[2] |= 0x04;
  124. if ( p[0] & 0x20 )
  125. buf[2] |= 0x10;
  126. if ( p[0] & 0x10 )
  127. buf[2] |= 0x40;
  128. if ( p[0] & 0x08 )
  129. buf[1] |= 0x01;
  130. if ( p[0] & 0x04 )
  131. buf[1] |= 0x04;
  132. if ( p[0] & 0x02 )
  133. buf[1] |= 0x10;
  134. if ( p[0] & 0x01 )
  135. buf[1] |= 0x40;
  136. if ( p[1] & 0x80 )
  137. buf[0] |= 0x01;
  138. if ( p[1] & 0x40 )
  139. buf[0] |= 0x04;
  140. if ( p[1] & 0x20 )
  141. buf[0] |= 0x10;
  142. if ( p[1] & 0x10 )
  143. buf[0] |= 0x40;
  144. // second row, right 12 pixel
  145. if ( p[1] & 0x08 )
  146. buf[5] |= 0x01;
  147. if ( p[1] & 0x04 )
  148. buf[5] |= 0x04;
  149. if ( p[1] & 0x02 )
  150. buf[5] |= 0x10;
  151. if ( p[1] & 0x01 )
  152. buf[5] |= 0x40;
  153. if ( p[2] & 0x80 )
  154. buf[4] |= 0x01;
  155. if ( p[2] & 0x40 )
  156. buf[4] |= 0x04;
  157. if ( p[2] & 0x20 )
  158. buf[4] |= 0x10;
  159. if ( p[2] & 0x10 )
  160. buf[4] |= 0x40;
  161. if ( p[2] & 0x08 )
  162. buf[3] |= 0x01;
  163. if ( p[2] & 0x04 )
  164. buf[3] |= 0x04;
  165. if ( p[2] & 0x02 )
  166. buf[3] |= 0x10;
  167. if ( p[2] & 0x01 )
  168. buf[3] |= 0x40;
  169. return buf;
  170. }
  171. #endif
  172. static uint8_t *u8x8_st7302_convert_a0(u8x8_t *u8x8, uint8_t *p)
  173. {
  174. static uint8_t buf[6];
  175. static uint8_t map1[16] = {
  176. /* 0x00 0000 */ 0,
  177. /* 0x01 0001 */0x01,
  178. /* 0x02 0010 */0x04,
  179. /* 0x03 0011 */0x04+0x01,
  180. /* 0x04 0100 */0x10,
  181. /* 0x05 0101 */0x10+0x01,
  182. /* 0x06 0110 */0x10+0x04,
  183. /* 0x07 0111 */0x10+0x04+0x01,
  184. /* 0x08 1000 */ 0x40,
  185. /* 0x09 1001 */ 0x40+0x01,
  186. /* 0x0a 1010 */ 0x40+0x04,
  187. /* 0x0b 1011 */ 0x40+0x04+0x01,
  188. /* 0x0c 1100 */ 0x40+0x10,
  189. /* 0x0d 1101 */ 0x40+0x10+0x01,
  190. /* 0x0e 1110 */ 0x40+0x10+0x04,
  191. /* 0x0f 1111 */ 0x40+0x10+0x04+0x01
  192. };
  193. static uint8_t map2[16] = {
  194. /* 0x00 0000 */ 0,
  195. /* 0x01 0001 */0x02,
  196. /* 0x02 0010 */0x08,
  197. /* 0x03 0011 */0x08+0x02,
  198. /* 0x04 0100 */0x20,
  199. /* 0x05 0101 */0x20+0x02,
  200. /* 0x06 0110 */0x20+0x08,
  201. /* 0x07 0111 */0x20+0x08+0x02,
  202. /* 0x08 1000 */ 0x80,
  203. /* 0x09 1001 */ 0x80+0x02,
  204. /* 0x0a 1010 */ 0x80+0x08,
  205. /* 0x0b 1011 */ 0x80+0x08+0x02,
  206. /* 0x0c 1100 */ 0x80+0x20,
  207. /* 0x0d 1101 */ 0x80+0x20+0x02,
  208. /* 0x0e 1110 */ 0x80+0x20+0x08,
  209. /* 0x0f 1111 */ 0x80+0x20+0x08+0x02
  210. };
  211. memset(buf, 0, 6);
  212. // U8X8_CA(0x36, 0x0), // Memory Control
  213. // first row, left 12 pixel
  214. buf[0] |= map1[p[0]>>4];
  215. buf[1] |= map1[p[0] & 0x0f];
  216. buf[2] |= map1[p[1]>>4];
  217. // first row, right 12 pixel
  218. buf[3] |= map1[p[1] & 0x0f];
  219. buf[4] |= map1[p[2]>>4];
  220. buf[5] |= map1[p[2] & 0x0f];
  221. p += u8x8->display_info->tile_width;
  222. // second row, left 12 pixel
  223. buf[0] |= map2[p[0]>>4];
  224. buf[1] |= map2[p[0] & 0x0f];
  225. buf[2] |= map2[p[1]>>4];
  226. // second row, right 12 pixel
  227. buf[3] |= map2[p[1] & 0x0f];
  228. buf[4] |= map2[p[2]>>4];
  229. buf[5] |= map2[p[2] & 0x0f];
  230. return buf;
  231. }
  232. /*===================================================*/
  233. /*
  234. see also:
  235. https://github.com/zhcong/ST7302-for-arduino/blob/c9390fabcacefe7c36a113cd3e62959418c13b97/libraries/ST7302SPI/ST7302SPI.cpp#L21
  236. */
  237. static const uint8_t u8x8_d_st7302_122x250_init_seq[] = {
  238. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  239. U8X8_C(0x01), // software reset
  240. U8X8_DLY(100),
  241. U8X8_C(0x28), // display off
  242. U8X8_CAA(0xC7, 0x26, 0xE9), // disable OSC
  243. U8X8_CA(0xD1, 0x00), // Booster disable
  244. U8X8_DLY(20),
  245. U8X8_C(0x10), // sleep in: enter sleep mode
  246. U8X8_DLY(20),
  247. U8X8_C(0x01), // software reset
  248. U8X8_DLY(20), // wait
  249. U8X8_C(0x38), // High Power Mode
  250. U8X8_CA(0xEB, 0x02), // Disable NVM Load
  251. U8X8_CA(0xD7, 0x68), // NVM Load Control: Enable ID1 ID2 ID3 Load
  252. U8X8_CA(0xD1, 0x01), // Booster Enable
  253. U8X8_CA(0xC0, 0x80), // Gate Voltage Setting VGH=12V (upper 4 bit, 8V-15V); VGL=-5V (lower 4 bit, -5V .. -10V)
  254. U8X8_C(0x0C1), // Source Voltage Control 1
  255. U8X8_A6(0x28,0x28,0x28,0x28,0x14,0x00), // Source high voltage in reflective and transmissive mode, gamma voltage 1&2
  256. U8X8_C(0x0C2), // Source Voltage Control 1
  257. U8X8_A4(0x00,0x00,0x00,0x00), // Source low voltage in reflective and transmissive mode
  258. U8X8_CA(0xCB, 0x14), // VCOMH: 0x14 = 4V (0x28 = 5V)
  259. U8X8_CAA(0xB4, 0xE5, 0x77), // Update Period Gate EQ Control, why 0x77??? it should be 0x66 according to the datasheet
  260. U8X8_A8(0xF1, 0xFF, 0xFF, 0x4F, 0xF1, 0xFF, 0xFF, 0X4F),
  261. U8X8_CA(0xB0, 0x64), // Duty Cycle... this must be before sleep out
  262. U8X8_C(0x11), // sleep out: furn off sleep mode
  263. U8X8_DLY(120),
  264. U8X8_CAA(0xC7, 0xA6, 0xE9), // Enable OSC
  265. U8X8_CA(0x36, 0xa0), // Memory Control
  266. U8X8_CA(0x3A, 0x11), // Data Format
  267. U8X8_CA(0xB9, 0x23), // Source Setting: Clear RAM off
  268. U8X8_CA(0xB8, 0x09), // Panel Setting / Panel Layout
  269. U8X8_CAA(0x2A, 0x05, 0x36), // col addr
  270. U8X8_CAA(0x2B, 0x00, 0xC7), // row addr
  271. U8X8_CA(0xD0, 0x1F), // Not in datasheet
  272. U8X8_C(0x29), // display on
  273. U8X8_CA(0x72, 0x00), // Not in datasheet
  274. U8X8_CAA(0xB2,1, 5), // Frame Rate for High and Low Power Mode (hier: 32Hz and 8Hz)
  275. U8X8_C(0x39), // enable Low Power Mode...: 8Hz, see above
  276. U8X8_DLY(100),
  277. /*
  278. U8X8_CAA(0x2A, 0x19, 0x3a), // col addr 0x14 < col < 0x3b
  279. U8X8_CAA(0x2B, 115, 115), // row addr (0..250, y in u8g2), 0 is top row in u8g2
  280. U8X8_C(0x2C), // write start
  281. U8X8_D1(0xff), // pixel data
  282. U8X8_D1(0xff), // pixel data
  283. U8X8_D1(0xff), // pixel data
  284. U8X8_D1(0xff), // pixel data
  285. U8X8_D1(0xff), // pixel data
  286. U8X8_D1(0xff), // pixel data
  287. U8X8_D1(0xff), // pixel data
  288. U8X8_D1(0xff), // pixel data
  289. U8X8_D1(0xff), // pixel data
  290. U8X8_D1(0xff), // pixel data
  291. U8X8_D1(0xff), // pixel data
  292. U8X8_D1(0xff), // pixel data
  293. U8X8_CAA(0x2A, 0x19, 0x3a), // col addr 0x14 < col < 0x3b
  294. U8X8_CAA(0x2B, 116, 116), // row addr (0..250, y in u8g2), 0 is top row in u8g2
  295. U8X8_C(0x2C), // write start
  296. U8X8_D1(0x80), // pixel data
  297. U8X8_D1(0x00), // pixel data
  298. U8X8_D1(0x00), // pixel data
  299. U8X8_D1(0x00), // pixel data
  300. U8X8_D1(0x00), // pixel data
  301. U8X8_D1(0x00), // pixel data
  302. U8X8_D1(0x00), // pixel data
  303. U8X8_D1(0x00), // pixel data
  304. U8X8_D1(0x00), // pixel data
  305. U8X8_D1(0x00), // pixel data
  306. U8X8_D1(0x00), // pixel data
  307. U8X8_D1(0x00), // pixel data
  308. */
  309. U8X8_END_TRANSFER(), /* disable chip */
  310. U8X8_END() /* end of sequence */
  311. };
  312. static const u8x8_display_info_t u8x8_st7302_122x250_display_info =
  313. {
  314. /* chip_enable_level = */ 0,
  315. /* chip_disable_level = */ 1,
  316. /* post_chip_enable_wait_ns = */ 20,
  317. /* pre_chip_disable_wait_ns = */ 20,
  318. /* reset_pulse_width_ms = */ 3,
  319. /* post_reset_wait_ms = */ 3, /**/
  320. /* sda_setup_time_ns = */ 10, /* */
  321. /* sck_pulse_width_ns = */ 30, /* */
  322. /* sck_clock_hz = */ 2000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  323. /* spi_mode = */ 0, /* active high, rising edge */
  324. /* i2c_bus_clock_100kHz = */ 4, /* 400KHz */
  325. /* data_setup_time_ns = */ 15,
  326. /* write_pulse_width_ns = */ 70,
  327. /* tile_width = */ 16,
  328. /* tile_height = */ 32,
  329. /* default_x_offset = */ 0,
  330. /* flipmode_x_offset = */ 0,
  331. /* pixel_width = */ 122,
  332. /* pixel_height = */ 250
  333. };
  334. uint8_t u8x8_d_st7302_122x250(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  335. {
  336. uint16_t x;
  337. uint8_t c, i, y;
  338. uint8_t *ptr;
  339. switch(msg)
  340. {
  341. case U8X8_MSG_DISPLAY_INIT:
  342. u8x8_d_helper_display_init(u8x8);
  343. u8x8_cad_SendSequence(u8x8, u8x8_d_st7302_122x250_init_seq);
  344. break;
  345. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  346. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7302_122x250_display_info);
  347. break;
  348. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  349. if ( arg_int == 0 )
  350. u8x8_cad_SendSequence(u8x8, u8x8_d_st7302_122x250_powersave0_seq);
  351. else
  352. u8x8_cad_SendSequence(u8x8, u8x8_d_st7302_122x250_powersave1_seq);
  353. break;
  354. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  355. if ( arg_int == 0 )
  356. {
  357. u8x8_cad_SendSequence(u8x8, u8x8_d_st7302_122x250_flip0_seq);
  358. u8x8->x_offset = u8x8->display_info->default_x_offset;
  359. }
  360. else
  361. {
  362. u8x8_cad_SendSequence(u8x8, u8x8_d_st7302_122x250_flip1_seq);
  363. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  364. }
  365. break;
  366. #ifdef U8X8_WITH_SET_CONTRAST
  367. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  368. u8x8_cad_StartTransfer(u8x8);
  369. u8x8_cad_SendCmd(u8x8, 0x081 );
  370. u8x8_cad_SendArg(u8x8, arg_int<<2 );
  371. u8x8_cad_SendArg(u8x8, arg_int>>6 );
  372. u8x8_cad_EndTransfer(u8x8);
  373. break;
  374. #endif
  375. case U8X8_MSG_DISPLAY_DRAW_TILE:
  376. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  377. x *= 8;
  378. x += u8x8->x_offset;
  379. y= (((u8x8_tile_t *)arg_ptr)->y_pos);
  380. y*=4;
  381. y+=115; // specific for the 122x250 LCD
  382. u8x8_cad_StartTransfer(u8x8);
  383. for( i = 0; i < 4; i++ )
  384. {
  385. u8x8_cad_SendCmd(u8x8, 0x2a);
  386. u8x8_cad_SendArg(u8x8, 0x19); // specific for the 122x250 LCD
  387. u8x8_cad_SendArg(u8x8, 0x3a );
  388. u8x8_cad_SendCmd(u8x8, 0x2b );
  389. u8x8_cad_SendArg(u8x8, y+i);
  390. u8x8_cad_SendArg(u8x8, y+i);
  391. u8x8_cad_SendCmd(u8x8, 0x02c ); // write data
  392. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  393. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  394. ptr += u8x8->display_info->tile_width*i*2;
  395. c = (c+2)/3; // calculate the number of 24 bit blocks to send
  396. while( c > 0 )
  397. {
  398. u8x8_cad_SendData(u8x8, 6, u8x8_st7302_convert_a0(u8x8, ptr));
  399. ptr+=3;
  400. --c;
  401. }
  402. }
  403. u8x8_cad_EndTransfer(u8x8);
  404. break;
  405. default:
  406. return 0;
  407. }
  408. return 1;
  409. }