u8x8_d_st7571.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. u8x8_d_st7571.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2020, 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. ST7571: 128x129 2-bit graylevel LCD
  27. https://github.com/olikraus/u8g2/issues/921
  28. https://github.com/olikraus/u8g2/issues/1575 128x96
  29. https://github.com/olikraus/u8g2/issues/2575 another 128x96 display
  30. */
  31. #include "u8x8.h"
  32. static const uint8_t u8x8_d_st7571_128x128_powersave0_seq[] = {
  33. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  34. U8X8_C(0x071), /* exit power save mode */
  35. U8X8_C(0x0a8), /* disable powersave mode */
  36. U8X8_C(0x0af), /* display on */
  37. U8X8_END_TRANSFER(), /* disable chip */
  38. U8X8_END() /* end of sequence */
  39. };
  40. static const uint8_t u8x8_d_st7571_128x128_powersave1_seq[] = {
  41. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  42. U8X8_C(0x0ae), /* display off */
  43. U8X8_C(0x0a9), /* enter powersave mode */
  44. U8X8_END_TRANSFER(), /* disable chip */
  45. U8X8_END() /* end of sequence */
  46. };
  47. static const uint8_t u8x8_d_st7571_128x128_flip0_seq[] = {
  48. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  49. U8X8_C(0x0a0), /* segment remap a0/a1*/
  50. U8X8_C(0x0c8), /* c0: scan dir normal, c8: reverse */
  51. U8X8_END_TRANSFER(), /* disable chip */
  52. U8X8_END() /* end of sequence */
  53. };
  54. static const uint8_t u8x8_d_st7571_128x128_flip1_seq[] = {
  55. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  56. U8X8_C(0x0a1), /* segment remap a0/a1*/
  57. U8X8_C(0x0c0), /* c0: scan dir normal, c8: reverse */
  58. U8X8_END_TRANSFER(), /* disable chip */
  59. U8X8_END() /* end of sequence */
  60. };
  61. /*===================================================*/
  62. static uint8_t u8x8_d_st7571_generic(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  63. {
  64. uint16_t x;
  65. uint8_t c;
  66. uint8_t *ptr;
  67. switch(msg)
  68. {
  69. /* handled by the calling function
  70. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  71. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7571_128x128_display_info);
  72. break;
  73. */
  74. /* handled by the calling function
  75. case U8X8_MSG_DISPLAY_INIT:
  76. u8x8_d_helper_display_init(u8x8);
  77. u8x8_cad_SendSequence(u8x8, u8x8_d_st7571_128x128_init_seq);
  78. break;
  79. */
  80. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  81. if ( arg_int == 0 )
  82. u8x8_cad_SendSequence(u8x8, u8x8_d_st7571_128x128_powersave0_seq);
  83. else
  84. u8x8_cad_SendSequence(u8x8, u8x8_d_st7571_128x128_powersave1_seq);
  85. break;
  86. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  87. if ( arg_int == 0 )
  88. {
  89. u8x8_cad_SendSequence(u8x8, u8x8_d_st7571_128x128_flip0_seq);
  90. u8x8->x_offset = u8x8->display_info->default_x_offset;
  91. }
  92. else
  93. {
  94. u8x8_cad_SendSequence(u8x8, u8x8_d_st7571_128x128_flip1_seq);
  95. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  96. }
  97. break;
  98. #ifdef U8X8_WITH_SET_CONTRAST
  99. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  100. u8x8_cad_StartTransfer(u8x8);
  101. u8x8_cad_SendCmd(u8x8, 0x081 );
  102. u8x8_cad_SendArg(u8x8, arg_int>>2); // 6 bit for the ST7571
  103. u8x8_cad_EndTransfer(u8x8);
  104. break;
  105. #endif
  106. case U8X8_MSG_DISPLAY_DRAW_TILE:
  107. u8x8_cad_StartTransfer(u8x8);
  108. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  109. x *= 8;
  110. x += u8x8->x_offset;
  111. u8x8_cad_SendCmd(u8x8, 0x010 | (x>>4) );
  112. u8x8_cad_SendCmd(u8x8, 0x000 | ((x&15)));
  113. u8x8_cad_SendCmd(u8x8, 0x0b0 | (((u8x8_tile_t *)arg_ptr)->y_pos));
  114. do
  115. {
  116. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  117. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  118. /* SendData can not handle more than 255 bytes */
  119. /*
  120. if ( c > 31 )
  121. {
  122. u8x8_cad_SendData(u8x8, 31*8, ptr);
  123. ptr+=31*8;
  124. c -= 31;
  125. }
  126. */
  127. u8x8_cad_SendData(u8x8, c*8, ptr);
  128. arg_int--;
  129. } while( arg_int > 0 );
  130. u8x8_cad_EndTransfer(u8x8);
  131. break;
  132. default:
  133. return 0;
  134. }
  135. return 1;
  136. }
  137. /*===================================================*/
  138. /* QT-2832TSWUG02/ZJY-2832TSWZG02 */
  139. /* fixed the 0x40 and 0x48 commands, verified with FlipMode example: All ok */
  140. static const uint8_t u8x8_d_st7571_128x128_init_seq[] = {
  141. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  142. U8X8_C(0xAE), // Display OFF
  143. U8X8_C(0x38), // Mode Set
  144. U8X8_C(0xB8), // FR=1011 (85Hz), BE[1:0]=10, level 3 booster
  145. U8X8_C(0xA0), // ADC select
  146. U8X8_C(0xC8), // SHL select
  147. U8X8_CA(0x44, 0x00), // COM0 register
  148. U8X8_CA(0x40, 0x0), // initial display line (0x7f... strange but ok... maybe specific for the JLX128128)
  149. // 2 sep 2021: maybe this also wrong because the 0x44 command is overwritten later.
  150. // 4 Mar 2022: Changed to 0
  151. U8X8_C(0xAB), // OSC ON
  152. U8X8_C(0x25), // Voltage regulator
  153. U8X8_CA(0x81, 0x33), // Volume
  154. U8X8_C(0x54), // LCD Bias: 0x056=1/11 (1/11 according to JLX128128 datasheet), 0x054=1/9
  155. U8X8_CA(0x48, 0x80), // Duty 1/128 // 2 Sep 2021: Should this be 00x48???
  156. // 4 Mar 2022, cmd changed to 0x48, arg changed to 0x80
  157. U8X8_C(0x2C), // Power Control, VC: ON, VR: OFF, VF: OFF
  158. U8X8_DLY(200),
  159. U8X8_C(0x2E), // Power Control, VC: ON, VR: ON, VF: OFF
  160. U8X8_DLY(200),
  161. U8X8_C(0x2F), // Power Control, VC: ON, VR: ON, VF: ON
  162. U8X8_DLY(10),
  163. U8X8_C(0x7B), // command set 3
  164. U8X8_C(0x11), // black white mode
  165. U8X8_C(0x00), // exit command set 3
  166. U8X8_C(0xA6), // Display Inverse OFF
  167. U8X8_C(0xA4), // Disable Display All Pixel ON
  168. //U8X8_C(0xAF), // Display on
  169. U8X8_END_TRANSFER(), /* disable chip */
  170. U8X8_END() /* end of sequence */
  171. };
  172. static const u8x8_display_info_t u8x8_st7571_128x128_display_info =
  173. {
  174. /* chip_enable_level = */ 0,
  175. /* chip_disable_level = */ 1,
  176. /* post_chip_enable_wait_ns = */ 20,
  177. /* pre_chip_disable_wait_ns = */ 20,
  178. /* reset_pulse_width_ms = */ 5,
  179. /* post_reset_wait_ms = */ 5, /**/
  180. /* sda_setup_time_ns = */ 20, /* */
  181. /* sck_pulse_width_ns = */ 40, /* */
  182. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  183. /* spi_mode = */ 0, /* active high, rising edge */
  184. /* i2c_bus_clock_100kHz = */ 4, /* 400KHz */
  185. /* data_setup_time_ns = */ 15,
  186. /* write_pulse_width_ns = */ 70,
  187. /* tile_width = */ 16,
  188. /* tile_height = */ 16,
  189. /* default_x_offset = */ 0,
  190. /* flipmode_x_offset = */ 0,
  191. /* pixel_width = */ 128,
  192. /* pixel_height = */ 128
  193. };
  194. uint8_t u8x8_d_st7571_128x128(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  195. {
  196. if ( u8x8_d_st7571_generic(u8x8, msg, arg_int, arg_ptr) != 0 )
  197. return 1;
  198. switch(msg)
  199. {
  200. case U8X8_MSG_DISPLAY_INIT:
  201. u8x8_d_helper_display_init(u8x8);
  202. u8x8_cad_SendSequence(u8x8, u8x8_d_st7571_128x128_init_seq);
  203. break;
  204. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  205. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7571_128x128_display_info);
  206. break;
  207. default:
  208. return 0;
  209. }
  210. return 1;
  211. }
  212. /*===================================================*/
  213. /*
  214. https://github.com/olikraus/u8g2/issues/1575
  215. http://www.jlxlcd.cn/html/zh-detail-1211.html
  216. */
  217. static const uint8_t u8x8_d_st7571_128x96_init_seq[] = {
  218. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  219. U8X8_C(0xAE), // Display OFF
  220. U8X8_C(0x38), // Mode Set
  221. //U8X8_C(0xB8), // FR=1011 (85Hz), BE[1:0]=10, level 3 booster
  222. U8X8_C(0x94), // 128x96: 75 Hz
  223. U8X8_C(0xA0), // ADC select
  224. U8X8_C(0xC8), // SHL select
  225. U8X8_CA(0x44, 0x20), // 128x96: COM0 register
  226. U8X8_CA(0x40, 0x00), // 128x96 datasheet
  227. U8X8_C(0xAB), // OSC ON
  228. U8X8_C(0x27), // 128x96: Voltage regulator
  229. U8X8_CA(0x81, 0x28), // 128x96: Volume
  230. U8X8_C(0x57), // 128x96: LCD Bias: 1/12
  231. U8X8_CA(0x48, 0x61), // 128x96: Duty 1/96
  232. U8X8_C(0x2C), // Power Control, VC: ON, VR: OFF, VF: OFF
  233. U8X8_DLY(200),
  234. U8X8_C(0x2E), // Power Control, VC: ON, VR: ON, VF: OFF
  235. U8X8_DLY(200),
  236. U8X8_C(0x2F), // Power Control, VC: ON, VR: ON, VF: ON
  237. U8X8_DLY(10),
  238. U8X8_C(0x7B), // command set 3
  239. U8X8_C(0x11), // black white mode
  240. U8X8_C(0x00), // exit command set 3
  241. U8X8_C(0xA6), // Display Inverse OFF
  242. U8X8_C(0xA4), // Disable Display All Pixel ON
  243. //U8X8_C(0xAF), // Display on
  244. U8X8_END_TRANSFER(), /* disable chip */
  245. U8X8_END() /* end of sequence */
  246. };
  247. static const u8x8_display_info_t u8x8_st7571_128x96_display_info =
  248. {
  249. /* chip_enable_level = */ 0,
  250. /* chip_disable_level = */ 1,
  251. /* post_chip_enable_wait_ns = */ 20,
  252. /* pre_chip_disable_wait_ns = */ 20,
  253. /* reset_pulse_width_ms = */ 5,
  254. /* post_reset_wait_ms = */ 5, /**/
  255. /* sda_setup_time_ns = */ 20, /* */
  256. /* sck_pulse_width_ns = */ 40, /* */
  257. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  258. /* spi_mode = */ 0, /* active high, rising edge */
  259. /* i2c_bus_clock_100kHz = */ 4, /* 400KHz */
  260. /* data_setup_time_ns = */ 15,
  261. /* write_pulse_width_ns = */ 70,
  262. /* tile_width = */ 16,
  263. /* tile_height = */ 12,
  264. /* default_x_offset = */ 0,
  265. /* flipmode_x_offset = */ 0,
  266. /* pixel_width = */ 128,
  267. /* pixel_height = */ 96
  268. };
  269. uint8_t u8x8_d_st7571_128x96(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  270. {
  271. if ( u8x8_d_st7571_generic(u8x8, msg, arg_int, arg_ptr) != 0 )
  272. return 1;
  273. switch(msg)
  274. {
  275. case U8X8_MSG_DISPLAY_INIT:
  276. u8x8_d_helper_display_init(u8x8);
  277. u8x8_cad_SendSequence(u8x8, u8x8_d_st7571_128x96_init_seq);
  278. break;
  279. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  280. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7571_128x96_display_info);
  281. break;
  282. default:
  283. return 0;
  284. }
  285. return 1;
  286. }
  287. /*===================================================*/
  288. /*
  289. ccsb4736w g12896 display
  290. https://github.com/olikraus/u8g2/issues/2575
  291. reusing u8x8_st7571_128x96_display_info
  292. */
  293. static const uint8_t u8x8_d_st7571_g12896_init_seq[] = {
  294. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  295. U8X8_C(0xAE), // Display OFF
  296. U8X8_C(0x38), // Mode Set
  297. //U8X8_C(0xB8), // FR=1011 (85Hz), BE[1:0]=10, level 3 booster
  298. U8X8_C(0x94), // 128x96: 75 Hz
  299. U8X8_C(0xA0), // ADC select
  300. U8X8_C(0xC8), // SHL select
  301. U8X8_CA(0x44, 0x00), // 128x96: COM0 register https://github.com/olikraus/u8g2/issues/2575
  302. U8X8_CA(0x40, 0x00), // 128x96 datasheet
  303. U8X8_C(0xAB), // OSC ON
  304. U8X8_C(0x27), // 128x96: Voltage regulator
  305. U8X8_CA(0x81, 0x28), // 128x96: Volume
  306. U8X8_C(0x57), // 128x96: LCD Bias: 1/12
  307. U8X8_CA(0x48, 0x61), // 128x96: Duty 1/96
  308. U8X8_C(0x2C), // Power Control, VC: ON, VR: OFF, VF: OFF
  309. U8X8_DLY(200),
  310. U8X8_C(0x2E), // Power Control, VC: ON, VR: ON, VF: OFF
  311. U8X8_DLY(200),
  312. U8X8_C(0x2F), // Power Control, VC: ON, VR: ON, VF: ON
  313. U8X8_DLY(10),
  314. U8X8_C(0x7B), // command set 3
  315. U8X8_C(0x11), // black white mode
  316. U8X8_C(0x00), // exit command set 3
  317. U8X8_C(0xA6), // Display Inverse OFF
  318. U8X8_C(0xA4), // Disable Display All Pixel ON
  319. //U8X8_C(0xAF), // Display on
  320. U8X8_END_TRANSFER(), /* disable chip */
  321. U8X8_END() /* end of sequence */
  322. };
  323. uint8_t u8x8_d_st7571_g12896(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  324. {
  325. if ( u8x8_d_st7571_generic(u8x8, msg, arg_int, arg_ptr) != 0 )
  326. return 1;
  327. switch(msg)
  328. {
  329. case U8X8_MSG_DISPLAY_INIT:
  330. u8x8_d_helper_display_init(u8x8);
  331. u8x8_cad_SendSequence(u8x8, u8x8_d_st7571_g12896_init_seq);
  332. break;
  333. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  334. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7571_128x96_display_info);
  335. break;
  336. default:
  337. return 0;
  338. }
  339. return 1;
  340. }