u8x8_d_st75161.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. u8x8_d_st75161.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. https://github.com/olikraus/u8g2/issues/2481
  27. 0x030 ext 00
  28. 0x031 ext 01
  29. 0x038 ext 10
  30. 0x039 ext 11
  31. cad 011
  32. normal mode:
  33. 0x00c bit format
  34. U8X8_CA( 0xbc, 0x00 ), data scan dir
  35. U8X8_A( 0xa6 ),
  36. y: 0 offset
  37. flip mode:
  38. 0x008 bit format
  39. U8X8_CA( 0xbc, 0x03 ), data scan dir
  40. U8X8_A( 0xa6 ),
  41. y: 5 offset
  42. */
  43. #include "u8x8.h"
  44. /* not a real power down for the st75256... just a display off */
  45. static const uint8_t u8x8_d_st75256_256x128_powersave0_seq[] = {
  46. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  47. U8X8_C( 0x030 ), /* select 00 commands */
  48. U8X8_C( 0x94 ), /* sleep out */
  49. U8X8_DLY(10),
  50. U8X8_C( 0xaf ), /* display on */
  51. U8X8_END_TRANSFER(), /* disable chip */
  52. U8X8_END() /* end of sequence */
  53. };
  54. static const uint8_t u8x8_d_st75256_256x128_powersave1_seq[] = {
  55. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  56. U8X8_C( 0x030 ), /* select 00 commands */
  57. U8X8_C( 0xae ), /* display off */
  58. U8X8_C( 0x95 ), /* sleep in */
  59. U8X8_END_TRANSFER(), /* disable chip */
  60. U8X8_END() /* end of sequence */
  61. };
  62. /* marked as unused to avoid compiler warning, issue 1802 */
  63. #ifdef NOT_USED
  64. static const uint8_t u8x8_d_st75256_jlx256128_flip0_seq[] = {
  65. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  66. U8X8_C( 0x030 ), /* select 00 commands */
  67. U8X8_CA( 0xbc, 0x00 ), /* data scan dir */
  68. U8X8_A( 0xa6 ), /* ??? */
  69. //U8X8_C( 0x030 ), /* select 00 commands */
  70. U8X8_C( 0x00c ), /* data format LSB top */
  71. U8X8_END_TRANSFER(), /* disable chip */
  72. U8X8_END() /* end of sequence */
  73. };
  74. static const uint8_t u8x8_d_st75256_jlx256128_flip1_seq[] = {
  75. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  76. U8X8_C( 0x030 ), /* select 00 commands */
  77. U8X8_CA( 0xbc, 0x03 ), /* data scan dir */
  78. U8X8_A( 0xa6 ), /* ??? */
  79. //U8X8_C( 0x030 ), /* select 00 commands */
  80. U8X8_C( 0x008 ), /* data format MSB top */
  81. U8X8_END_TRANSFER(), /* disable chip */
  82. U8X8_END() /* end of sequence */
  83. };
  84. #endif
  85. static const uint8_t u8x8_d_st75161_jlx160160_flip0_seq[] = {
  86. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  87. U8X8_C( 0x030 ), /* select 00 commands */
  88. U8X8_CA( 0xbc, 0x00 ), /* data scan dir */
  89. U8X8_A( 0xa6 ), /* ??? */
  90. //U8X8_C( 0x030 ), /* select 00 commands */
  91. U8X8_C( 0x00c ), /* data format LSB top */
  92. U8X8_CA( 0x0AB, 0 ), /* start line */
  93. U8X8_END_TRANSFER(), /* disable chip */
  94. U8X8_END() /* end of sequence */
  95. };
  96. static const uint8_t u8x8_d_st75161_jlx160160_flip1_seq[] = {
  97. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  98. U8X8_C( 0x030 ), /* select 00 commands */
  99. U8X8_CA( 0xbc, 0x03 ), /* data scan dir */
  100. U8X8_A( 0xa6 ), /* ??? */
  101. //U8X8_C( 0x030 ), /* select 00 commands */
  102. U8X8_C( 0x008 ), /* data format MSB top */
  103. U8X8_CA( 0x0AB, 0 ), /* start line */
  104. U8X8_END_TRANSFER(), /* disable chip */
  105. U8X8_END() /* end of sequence */
  106. };
  107. /* marked as unused to avoid compiler warning, issue 1802 */
  108. #ifdef NOT_USED
  109. static const uint8_t u8x8_d_st75256_jlx256160_flip0_seq[] = {
  110. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  111. U8X8_C( 0x030 ), /* select 00 commands */
  112. U8X8_CA( 0xbc, 0x00 ), /* data scan dir */
  113. U8X8_A( 0xa6 ), /* ??? */
  114. //U8X8_C( 0x030 ), /* select 00 commands */
  115. U8X8_C( 0x00c ), /* data format LSB top */
  116. U8X8_END_TRANSFER(), /* disable chip */
  117. U8X8_END() /* end of sequence */
  118. };
  119. static const uint8_t u8x8_d_st75256_jlx256160_flip1_seq[] = {
  120. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  121. U8X8_C( 0x030 ), /* select 00 commands */
  122. U8X8_CA( 0xbc, 0x03 ), /* data scan dir */
  123. U8X8_A( 0xa6 ), /* ??? */
  124. //U8X8_C( 0x030 ), /* select 00 commands */
  125. U8X8_C( 0x008 ), /* data format MSB top */
  126. U8X8_END_TRANSFER(), /* disable chip */
  127. U8X8_END() /* end of sequence */
  128. };
  129. #endif
  130. /*=============================================*/
  131. /* jlx160160 https://github.com/olikraus/u8g2/issues/1642 */
  132. static const u8x8_display_info_t u8x8_st75161_jlx160160_display_info =
  133. {
  134. /* chip_enable_level = */ 0,
  135. /* chip_disable_level = */ 1,
  136. /* post_chip_enable_wait_ns = */ 20,
  137. /* pre_chip_disable_wait_ns = */ 20,
  138. /* reset_pulse_width_ms = */ 5,
  139. /* post_reset_wait_ms = */ 5, /**/
  140. /* sda_setup_time_ns = */ 20, /* */
  141. /* sck_pulse_width_ns = */ 40, /* */
  142. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  143. /* spi_mode = */ 0, /* active high, rising edge */
  144. /* i2c_bus_clock_100kHz = */ 4, /* 400KHz */
  145. /* data_setup_time_ns = */ 15,
  146. /* write_pulse_width_ns = */ 70,
  147. /* tile_width = */ 20,
  148. /* tile_height = */ 20,
  149. /* default_x_offset = */ 0, /* y (!) offset in flipmode 0 */
  150. /* flipmode_x_offset = */ 1, /* y (!) offset in flipmode 0 */
  151. /* pixel_width = */ 160,
  152. /* pixel_height = */ 160
  153. };
  154. /*
  155. from the JLX160160 datasheet:
  156. transfer_command_lcd(0x30);//EXT=0
  157. transfer_command_lcd(0x94);//Sleep out
  158. transfer_command_lcd(0x31);//EXT=1
  159. transfer_command_lcd(0xD7);//Autoread disable
  160. transfer_data_lcd(0X9F);//
  161. transfer_command_lcd(0x32);//Analog SET
  162. transfer_data_lcd(0x00);//OSC Frequency adjustment
  163. transfer_data_lcd(0x01);//Frequency on booster capacitors->6KHz
  164. transfer_data_lcd(0x00);//Bias=1/14
  165. transfer_command_lcd(0x20);// Gray Level
  166. transfer_data_lcd(0x01);
  167. transfer_data_lcd(0x03);
  168. transfer_data_lcd(0x05);
  169. transfer_data_lcd(0x07);
  170. transfer_data_lcd(0x09);
  171. transfer_data_lcd(0x0b);
  172. transfer_data_lcd(0x0d);
  173. transfer_data_lcd(0x10);
  174. transfer_data_lcd(0x11);
  175. transfer_data_lcd(0x13);
  176. transfer_data_lcd(0x15);
  177. transfer_data_lcd(0x17);
  178. transfer_data_lcd(0x19);
  179. transfer_data_lcd(0x1b);
  180. transfer_data_lcd(0x1d);
  181. transfer_data_lcd(0x1f);
  182. transfer_command_lcd(0x31);//EXT=1
  183. transfer_command_lcd(0xf0);//Without this instruction, the voltage will be increased slowly. 0.5s
  184. transfer_data_lcd(0x0f);
  185. transfer_data_lcd(0x0f);
  186. transfer_data_lcd(0x0f);
  187. transfer_data_lcd(0x0f);
  188. transfer_command_lcd(0x30);//EXT=0
  189. transfer_command_lcd(0x75);//Page Address setting
  190. transfer_data_lcd(0X00);// XS=0
  191. transfer_data_lcd(0X28);// XE=159 0x28
  192. transfer_command_lcd(0x15);//Clumn Address setting
  193. transfer_data_lcd(0X00);// XS=0
  194. transfer_data_lcd(0Xff);// XE=256
  195. transfer_command_lcd(0xBC);//Data scan direction
  196. transfer_data_lcd(0x00);//MX.MY=Normal
  197. transfer_ command_lcd (0xA6);
  198. transfer_command_lcd(0xCA);//Display Control
  199. transfer_data_lcd(0X00);//
  200. transfer_data_lcd(0X9F);//Duty=160
  201. transfer_data_lcd(0X20);//Nline=off
  202. transfer_command_lcd(0xF0);//Display Mode
  203. transfer_data_lcd(0X10);//10=Monochrome Mode,11=4Gray
  204. transfer_command_lcd(0x81);//EV control
  205. transfer_data_lcd(0x1d);//Fine-tune the contrast value, 0x00~0x3f
  206. transfer_data_lcd(0x04);//Coarse contrast adjustment 0x00~0x07
  207. transfer_command_lcd(0x20);//Power control
  208. transfer_data_lcd(0x0B);//D0=regulator ; D1=follower ; D3=booste,
  209. on:1 off:0
  210. delay(20);
  211. transfer_command_lcd(0xAF); //Display on
  212. */
  213. static const uint8_t u8x8_d_st75161_jlx160160_init_seq[] = {
  214. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  215. U8X8_DLY(20),
  216. /*
  217. U8X8_C( 0x030 ), // Extension Command 1
  218. U8X8_C( 0x06E ),
  219. U8X8_C( 0x031 ), // Extension Command 2
  220. U8X8_CA( 0x0d7, 0x09f), // Set auto-read instruction, Disable Auto Read
  221. U8X8_CA( 0x0e0 ,0x000), // Enable OTP Read
  222. U8X8_DLY(10),
  223. U8X8_C( 0x0e3 ), // OTP Read
  224. U8X8_DLY(20),
  225. U8X8_C(0xe1), // OTP Control Out
  226. */
  227. U8X8_C( 0x030 ), /* select 00 commands */
  228. U8X8_C( 0x094 ), /* sleep out */
  229. //U8X8_C( 0x030 ), /* select 00 commands */
  230. U8X8_C( 0x0ae ), /* display off */
  231. U8X8_C( 0x031 ), /* select 01 commands */
  232. U8X8_CA( 0x0d7, 0x09f ), /* disable auto read */
  233. //U8X8_C( 0x031 ), /* select 01 commands */
  234. U8X8_C( 0x032 ), /* analog circuit set */
  235. U8X8_A( 0x000 ), /* code example: OSC Frequency adjustment */
  236. U8X8_A( 0x001 ), /* Frequency on booster capacitors 1 = 6KHz? */
  237. U8X8_A( 0x000 ), /* Bias: 0: 1/14, 1: 1/13, 2: 1/12, 3: 1/11, 4:1/10, 5:1/9 */
  238. U8X8_C( 0x031 ), /* select 01 commands */
  239. U8X8_C( 0x020 ), /* gray levels */
  240. U8X8_A( 0x01 ),
  241. U8X8_A( 0x03 ),
  242. U8X8_A( 0x05 ),
  243. U8X8_A( 0x07 ),
  244. U8X8_A( 0x09),
  245. U8X8_A( 0x0b ),
  246. U8X8_A( 0x0d ),
  247. U8X8_A( 0x10 ),
  248. U8X8_A( 0x11 ),
  249. U8X8_A( 0x13 ),
  250. U8X8_A( 0x15 ),
  251. U8X8_A( 0x17 ),
  252. U8X8_A( 0x19 ),
  253. U8X8_A( 0x1b ),
  254. U8X8_A( 0x1d ),
  255. U8X8_A( 0x1f ),
  256. U8X8_C( 0x031 ), /* select 01 commands */
  257. U8X8_CA(0x51, 0xfb), // Booster Level x10
  258. U8X8_CAAAA(0xf0, 15, 15, 15, 15), /* Frame Rate Temparature Range */
  259. U8X8_C( 0x030 ), /* select 00 commands */
  260. U8X8_CAA(0x75, 0, 0x28), /* row range 0..159*/
  261. U8X8_CAA(0x15, 0, 255), /* col range */
  262. //U8X8_C( 0x030 ), /* select 00 commands */
  263. U8X8_CA( 0xbc, 0x00 ), /* data scan dir */
  264. U8X8_A( 0xa6 ), /* ??? */
  265. //U8X8_C( 0x030 ), /* select 00 commands */
  266. U8X8_C( 0x00c ), /* data format LSB top */
  267. //U8X8_C( 0x030 ), /* select 00 commands */
  268. U8X8_C( 0xca ), /* display control, 3 args follow */
  269. U8X8_A( 0x00 ), /* 0x00: no clock division, 0x04: devide clock */
  270. U8X8_A( 0x9f ), /* 1/160 duty */
  271. U8X8_A( 0x20 ), /* nline off */
  272. //U8X8_C( 0x030 ), /* select 00 commands */
  273. U8X8_CA( 0x0f0, 0x010 ), /* monochrome mode = 0x010, graylevel = 0x011*/
  274. //U8X8_C( 0x030 ), /* select 00 commands */
  275. U8X8_CAA( 0x81, 0x1d, 0x04 ), /* Volume control */
  276. //U8X8_C( 0x030 ), /* select 00 commands */
  277. U8X8_CA( 0x020, 0x00b ), /* Power control: Regulator, follower & booster on */
  278. U8X8_DLY(100),
  279. //U8X8_C( 0xaf ),
  280. U8X8_END_TRANSFER(), /* disable chip */
  281. U8X8_END() /* end of sequence */
  282. };
  283. uint8_t u8x8_d_st75161_jlx160160(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  284. {
  285. uint8_t x, c;
  286. uint8_t *ptr;
  287. switch(msg)
  288. {
  289. case U8X8_MSG_DISPLAY_DRAW_TILE:
  290. u8x8_cad_StartTransfer(u8x8);
  291. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  292. x *= 8;
  293. u8x8_cad_SendCmd(u8x8, 0x030 ); /* select command set */
  294. u8x8_cad_SendCmd(u8x8, 0x075 ); /* row */
  295. u8x8_cad_SendArg(u8x8, (((u8x8_tile_t *)arg_ptr)->y_pos+u8x8->x_offset)); // x offset is reused as y offset
  296. u8x8_cad_SendArg(u8x8, 0x027);
  297. u8x8_cad_SendCmd(u8x8, 0x015 ); /* col */
  298. u8x8_cad_SendArg(u8x8, x);
  299. u8x8_cad_SendArg(u8x8, 0x9f);
  300. u8x8_cad_SendCmd(u8x8, 0x05c );
  301. do
  302. {
  303. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  304. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  305. c *= 8;
  306. u8x8_cad_SendData(u8x8, c, ptr);
  307. arg_int--;
  308. } while( arg_int > 0 );
  309. u8x8_cad_EndTransfer(u8x8);
  310. return 1;
  311. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  312. //u8x8_SetI2CAddress(u8x8, 0x078); /* lowest I2C adr of the ST75161 */
  313. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st75161_jlx160160_display_info);
  314. return 1;
  315. case U8X8_MSG_DISPLAY_INIT:
  316. u8x8_d_helper_display_init(u8x8);
  317. u8x8_cad_SendSequence(u8x8, u8x8_d_st75161_jlx160160_init_seq);
  318. return 1;
  319. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  320. if ( arg_int == 0 )
  321. u8x8_cad_SendSequence(u8x8, u8x8_d_st75256_256x128_powersave0_seq);
  322. else
  323. u8x8_cad_SendSequence(u8x8, u8x8_d_st75256_256x128_powersave1_seq);
  324. return 1;
  325. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  326. if ( arg_int == 0 )
  327. {
  328. u8x8_cad_SendSequence(u8x8, u8x8_d_st75161_jlx160160_flip0_seq);
  329. u8x8->x_offset = u8x8->display_info->default_x_offset;
  330. }
  331. else
  332. {
  333. u8x8_cad_SendSequence(u8x8, u8x8_d_st75161_jlx160160_flip1_seq);
  334. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  335. }
  336. return 1;
  337. #ifdef U8X8_WITH_SET_CONTRAST
  338. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  339. u8x8_cad_StartTransfer(u8x8);
  340. u8x8_cad_SendCmd(u8x8, 0x030 );
  341. u8x8_cad_SendCmd(u8x8, 0x081 ); /* there are 9 bit for the volume control */
  342. u8x8_cad_SendArg(u8x8, (arg_int & 0x1f)<<1 ); /* lower 6 bit */
  343. u8x8_cad_SendArg(u8x8, (arg_int>>5)); /* upper 3 bit */
  344. u8x8_cad_EndTransfer(u8x8);
  345. return 1;
  346. #endif
  347. }
  348. return 0;
  349. }