u8x8_d_st7571.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. static const uint8_t u8x8_d_st7571_128x128_init_seq[] = {
  139. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  140. U8X8_C(0xAE), // Display OFF
  141. U8X8_C(0x38), // Mode Set
  142. U8X8_C(0xB8), // FR=1011 (85Hz), BE[1:0]=10, level 3 booster
  143. U8X8_C(0xA0), // ADC select
  144. U8X8_C(0xC8), // SHL select
  145. U8X8_CA(0x44, 0x00), // COM0 register
  146. U8X8_CA(0x40, 0x7f), // initial display line (0x7f... strange but ok... maybe specific for the JLX128128)
  147. // 2 sep 2021: maybe this also wrong because the 0x44 command is overwritten later.
  148. U8X8_C(0xAB), // OSC ON
  149. U8X8_C(0x25), // Voltage regulator
  150. U8X8_CA(0x81, 0x33), // Volume
  151. U8X8_C(0x54), // LCD Bias: 0x056=1/11 (1/11 according to JLX128128 datasheet), 0x054=1/9
  152. U8X8_CA(0x44, 0x7f), // Duty 1/128 // 2 Sep 2021: Should this be 00x48???
  153. U8X8_C(0x2C), // Power Control, VC: ON, VR: OFF, VF: OFF
  154. U8X8_DLY(200),
  155. U8X8_C(0x2E), // Power Control, VC: ON, VR: ON, VF: OFF
  156. U8X8_DLY(200),
  157. U8X8_C(0x2F), // Power Control, VC: ON, VR: ON, VF: ON
  158. U8X8_DLY(10),
  159. U8X8_C(0x7B), // command set 3
  160. U8X8_C(0x11), // black white mode
  161. U8X8_C(0x00), // exit command set 3
  162. U8X8_C(0xA6), // Display Inverse OFF
  163. U8X8_C(0xA4), // Disable Display All Pixel ON
  164. //U8X8_C(0xAF), // Display on
  165. U8X8_END_TRANSFER(), /* disable chip */
  166. U8X8_END() /* end of sequence */
  167. };
  168. static const u8x8_display_info_t u8x8_st7571_128x128_display_info =
  169. {
  170. /* chip_enable_level = */ 0,
  171. /* chip_disable_level = */ 1,
  172. /* post_chip_enable_wait_ns = */ 20,
  173. /* pre_chip_disable_wait_ns = */ 20,
  174. /* reset_pulse_width_ms = */ 5,
  175. /* post_reset_wait_ms = */ 5, /**/
  176. /* sda_setup_time_ns = */ 20, /* */
  177. /* sck_pulse_width_ns = */ 40, /* */
  178. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  179. /* spi_mode = */ 0, /* active high, rising edge */
  180. /* i2c_bus_clock_100kHz = */ 4, /* 400KHz */
  181. /* data_setup_time_ns = */ 15,
  182. /* write_pulse_width_ns = */ 70,
  183. /* tile_width = */ 16,
  184. /* tile_hight = */ 16,
  185. /* default_x_offset = */ 0,
  186. /* flipmode_x_offset = */ 0,
  187. /* pixel_width = */ 128,
  188. /* pixel_height = */ 128
  189. };
  190. uint8_t u8x8_d_st7571_128x128(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  191. {
  192. if ( u8x8_d_st7571_generic(u8x8, msg, arg_int, arg_ptr) != 0 )
  193. return 1;
  194. switch(msg)
  195. {
  196. case U8X8_MSG_DISPLAY_INIT:
  197. u8x8_d_helper_display_init(u8x8);
  198. u8x8_cad_SendSequence(u8x8, u8x8_d_st7571_128x128_init_seq);
  199. break;
  200. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  201. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7571_128x128_display_info);
  202. break;
  203. default:
  204. return 0;
  205. }
  206. return 1;
  207. }
  208. /*===================================================*/
  209. /*
  210. https://github.com/olikraus/u8g2/issues/1575
  211. http://www.jlxlcd.cn/html/zh-detail-1211.html
  212. */
  213. static const uint8_t u8x8_d_st7571_128x96_init_seq[] = {
  214. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  215. U8X8_C(0xAE), // Display OFF
  216. U8X8_C(0x38), // Mode Set
  217. //U8X8_C(0xB8), // FR=1011 (85Hz), BE[1:0]=10, level 3 booster
  218. U8X8_C(0x94), // 128x96: 75 Hz
  219. U8X8_C(0xA0), // ADC select
  220. U8X8_C(0xC8), // SHL select
  221. U8X8_CA(0x44, 0x20), // 128x96: COM0 register
  222. U8X8_CA(0x40, 0x00), // 128x96 datasheet
  223. U8X8_C(0xAB), // OSC ON
  224. U8X8_C(0x27), // 128x96: Voltage regulator
  225. U8X8_CA(0x81, 0x28), // 128x96: Volume
  226. U8X8_C(0x57), // 128x96: LCD Bias: 1/12
  227. U8X8_CA(0x48, 0x61), // 128x96: Duty 1/96
  228. U8X8_C(0x2C), // Power Control, VC: ON, VR: OFF, VF: OFF
  229. U8X8_DLY(200),
  230. U8X8_C(0x2E), // Power Control, VC: ON, VR: ON, VF: OFF
  231. U8X8_DLY(200),
  232. U8X8_C(0x2F), // Power Control, VC: ON, VR: ON, VF: ON
  233. U8X8_DLY(10),
  234. U8X8_C(0x7B), // command set 3
  235. U8X8_C(0x11), // black white mode
  236. U8X8_C(0x00), // exit command set 3
  237. U8X8_C(0xA6), // Display Inverse OFF
  238. U8X8_C(0xA4), // Disable Display All Pixel ON
  239. //U8X8_C(0xAF), // Display on
  240. U8X8_END_TRANSFER(), /* disable chip */
  241. U8X8_END() /* end of sequence */
  242. };
  243. static const u8x8_display_info_t u8x8_st7571_128x96_display_info =
  244. {
  245. /* chip_enable_level = */ 0,
  246. /* chip_disable_level = */ 1,
  247. /* post_chip_enable_wait_ns = */ 20,
  248. /* pre_chip_disable_wait_ns = */ 20,
  249. /* reset_pulse_width_ms = */ 5,
  250. /* post_reset_wait_ms = */ 5, /**/
  251. /* sda_setup_time_ns = */ 20, /* */
  252. /* sck_pulse_width_ns = */ 40, /* */
  253. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  254. /* spi_mode = */ 0, /* active high, rising edge */
  255. /* i2c_bus_clock_100kHz = */ 4, /* 400KHz */
  256. /* data_setup_time_ns = */ 15,
  257. /* write_pulse_width_ns = */ 70,
  258. /* tile_width = */ 16,
  259. /* tile_hight = */ 12,
  260. /* default_x_offset = */ 0,
  261. /* flipmode_x_offset = */ 0,
  262. /* pixel_width = */ 128,
  263. /* pixel_height = */ 96
  264. };
  265. uint8_t u8x8_d_st7571_128x96(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  266. {
  267. if ( u8x8_d_st7571_generic(u8x8, msg, arg_int, arg_ptr) != 0 )
  268. return 1;
  269. switch(msg)
  270. {
  271. case U8X8_MSG_DISPLAY_INIT:
  272. u8x8_d_helper_display_init(u8x8);
  273. u8x8_cad_SendSequence(u8x8, u8x8_d_st7571_128x96_init_seq);
  274. break;
  275. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  276. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7571_128x96_display_info);
  277. break;
  278. default:
  279. return 0;
  280. }
  281. return 1;
  282. }