u8x8_d_st7571.c 11 KB

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