u8x8_d_st7588.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. u8x8_d_st7588.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2017, 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. ST7588
  27. - has 4 different I2C addresses
  28. - I2C protocol is identical to SSD13xx
  29. */
  30. #include "u8x8.h"
  31. /* function set, bit 2: power down, bit 3: MY, bit 4: MX, bit 5: must be 1 */
  32. #define FS (0x020)
  33. /* not a real power down for the ST7588... just a display off */
  34. static const uint8_t u8x8_d_st7588_128x64_powersave0_seq[] = {
  35. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  36. U8X8_C( FS | 0x00 ), /* select 00 commands */
  37. //U8X8_C( 0x08 ), /* display off */
  38. U8X8_C( 0x0c ), /* display on */
  39. U8X8_END_TRANSFER(), /* disable chip */
  40. U8X8_END() /* end of sequence */
  41. };
  42. static const uint8_t u8x8_d_st7588_128x64_powersave1_seq[] = {
  43. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  44. U8X8_C( FS | 0x00 ), /* select 00 commands */
  45. U8X8_C( 0x08 ), /* display off */
  46. //U8X8_C( 0x0c ), /* display on */
  47. U8X8_END_TRANSFER(), /* disable chip */
  48. U8X8_END() /* end of sequence */
  49. };
  50. static uint8_t u8x8_d_st7588_128x64_generic(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  51. {
  52. uint8_t x, c;
  53. uint8_t *ptr;
  54. switch(msg)
  55. {
  56. /* handled by the calling function
  57. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  58. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7588_128x64_display_info);
  59. break;
  60. */
  61. /* handled by the calling function
  62. case U8X8_MSG_DISPLAY_INIT:
  63. u8x8_d_helper_display_init(u8x8);
  64. u8x8_cad_SendSequence(u8x8, u8x8_d_st7588_128x64_init_seq);
  65. break;
  66. */
  67. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  68. if ( arg_int == 0 )
  69. u8x8_cad_SendSequence(u8x8, u8x8_d_st7588_128x64_powersave0_seq);
  70. else
  71. u8x8_cad_SendSequence(u8x8, u8x8_d_st7588_128x64_powersave1_seq);
  72. /* restore orientation */
  73. if ( u8x8->x_offset == 0 )
  74. u8x8_cad_SendCmd(u8x8, FS ); /* select 00 commands */
  75. else
  76. u8x8_cad_SendCmd(u8x8, FS ^ 0x018 ); /* select 00 commands */
  77. break;
  78. #ifdef U8X8_WITH_SET_CONTRAST
  79. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  80. u8x8_cad_StartTransfer(u8x8);
  81. u8x8_cad_SendCmd(u8x8, FS );
  82. u8x8_cad_SendArg(u8x8, 4 | (arg_int>>7) );
  83. u8x8_cad_SendCmd(u8x8, FS | 1);
  84. u8x8_cad_SendArg(u8x8, 0x080 | arg_int );
  85. /* restore orientation */
  86. if ( u8x8->x_offset == 0 )
  87. u8x8_cad_SendCmd(u8x8, FS ); /* select 00 commands */
  88. else
  89. u8x8_cad_SendCmd(u8x8, FS ^ 0x018 ); /* select 00 commands */
  90. u8x8_cad_EndTransfer(u8x8);
  91. break;
  92. #endif
  93. case U8X8_MSG_DISPLAY_DRAW_TILE:
  94. u8x8_cad_StartTransfer(u8x8);
  95. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  96. x *= 8;
  97. x += u8x8->x_offset;
  98. if ( u8x8->x_offset == 0 )
  99. u8x8_cad_SendCmd(u8x8, FS ); /* select 00 commands */
  100. else
  101. u8x8_cad_SendCmd(u8x8, FS ^ 0x018 ); /* select 00 commands */
  102. u8x8_cad_SendCmd(u8x8, 0x040 | (((u8x8_tile_t *)arg_ptr)->y_pos));
  103. u8x8_cad_SendCmd(u8x8, 0x0e0 | ((x&15)));
  104. u8x8_cad_SendCmd(u8x8, 0x0f0 | (x>>4) );
  105. do
  106. {
  107. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  108. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  109. u8x8_cad_SendData(u8x8, c*8, ptr); /* note: SendData can not handle more than 255 bytes */
  110. arg_int--;
  111. } while( arg_int > 0 );
  112. u8x8_cad_EndTransfer(u8x8);
  113. break;
  114. default:
  115. return 0;
  116. }
  117. return 1;
  118. }
  119. /*=============================================*/
  120. static const u8x8_display_info_t u8x8_st7588_128x64_display_info =
  121. {
  122. /* chip_enable_level = */ 0,
  123. /* chip_disable_level = */ 1,
  124. /* post_chip_enable_wait_ns = */ 150,
  125. /* pre_chip_disable_wait_ns = */ 30,
  126. /* reset_pulse_width_ms = */ 5,
  127. /* post_reset_wait_ms = */ 5, /**/
  128. /* sda_setup_time_ns = */ 60, /* */
  129. /* sck_pulse_width_ns = */ 60, /* */
  130. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  131. /* spi_mode = */ 0, /* active high, rising edge */
  132. /* i2c_bus_clock_100kHz = */ 4, /* 400KHz */
  133. /* data_setup_time_ns = */ 80,
  134. /* write_pulse_width_ns = */ 50,
  135. /* tile_width = */ 16,
  136. /* tile_height = */ 8,
  137. /* default_x_offset = */ 0, /* must be 0, because this is checked also for normal mode */
  138. /* flipmode_x_offset = */ 4,
  139. /* pixel_width = */ 128,
  140. /* pixel_height = */ 64
  141. };
  142. static const uint8_t u8x8_d_st7588_128x64_init_seq[] = {
  143. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  144. U8X8_C( FS | 0x03 ), /* select 11 commands */
  145. U8X8_C( 0x03 ), /* software reset */
  146. U8X8_C( FS | 0x00 ), /* select 00 commands */
  147. U8X8_C( 0x08 ), /* display off */
  148. //U8X8_C( 0x0c ), /* display on */
  149. U8X8_C( FS | 0x01 ), /* select 01 commands */
  150. U8X8_C( 0x08 ), /* display confguration */
  151. U8X8_C( 0x12 ), /* bias 1/9 */
  152. U8X8_C( 0x8f ), /* Vop, lower 7 bits */
  153. U8X8_C( FS | 0x00 ), /* select 00 commands */
  154. U8X8_C( 0x05), /* Bit 0 contains high/low range for Vop */
  155. U8X8_C( FS | 0x03 ), /* select 11 commands */
  156. U8X8_C( 0x0b), /* Frame Rate: 73 Hz */
  157. U8X8_END_TRANSFER(), /* disable chip */
  158. U8X8_END() /* end of sequence */
  159. };
  160. static const uint8_t u8x8_d_st7588_jlx12864_flip0_seq[] = {
  161. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  162. U8X8_C( FS ), /* normal mode */
  163. U8X8_END_TRANSFER(), /* disable chip */
  164. U8X8_END() /* end of sequence */
  165. };
  166. static const uint8_t u8x8_d_st7588_jlx12864_flip1_seq[] = {
  167. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  168. U8X8_C( FS ^ 0x018 ), /* normal mode */
  169. U8X8_END_TRANSFER(), /* disable chip */
  170. U8X8_END() /* end of sequence */
  171. };
  172. uint8_t u8x8_d_st7588_jlx12864(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  173. {
  174. if ( u8x8_d_st7588_128x64_generic(u8x8, msg, arg_int, arg_ptr) != 0 )
  175. return 1;
  176. if ( msg == U8X8_MSG_DISPLAY_SETUP_MEMORY )
  177. {
  178. u8x8_SetI2CAddress(u8x8, 0x07e); /* the JLX12864 has 0x07e as a default address for I2C */
  179. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7588_128x64_display_info);
  180. return 1;
  181. }
  182. else if ( msg == U8X8_MSG_DISPLAY_INIT )
  183. {
  184. u8x8_d_helper_display_init(u8x8);
  185. u8x8_cad_SendSequence(u8x8, u8x8_d_st7588_128x64_init_seq);
  186. return 1;
  187. }
  188. else if ( msg == U8X8_MSG_DISPLAY_SET_FLIP_MODE )
  189. {
  190. if ( arg_int == 0 )
  191. {
  192. u8x8_cad_SendSequence(u8x8, u8x8_d_st7588_jlx12864_flip0_seq);
  193. u8x8->x_offset = u8x8->display_info->default_x_offset;
  194. }
  195. else
  196. {
  197. u8x8_cad_SendSequence(u8x8, u8x8_d_st7588_jlx12864_flip1_seq);
  198. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  199. }
  200. return 1;
  201. }
  202. return 0;
  203. }