u8x8_d_st7586s_s028hn118a.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. u8x8_d_st7586s_s028hn118a.c
  3. Display: 384x136 pixel
  4. ST7586s: 384 x 160 x 2
  5. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  6. Copyright (c) 2018, olikraus@gmail.com
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. * Redistributions of source code must retain the above copyright notice, this list
  11. of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above copyright notice, this
  13. list of conditions and the following disclaimer in the documentation and/or other
  14. materials provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  16. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  20. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  27. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "u8g2.h"
  30. static const uint8_t u8x8_d_st7586s_sleep_on[] = {
  31. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  32. U8X8_C(0x010), /* set power save mode */
  33. U8X8_END_TRANSFER(), /* disable chip */
  34. U8X8_END() /* end of sequence */
  35. };
  36. static const uint8_t u8x8_d_st7586s_sleep_off[] = {
  37. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  38. U8X8_C(0x011), //Sleep out
  39. U8X8_DLY(50), /* delay 50 ms */
  40. U8X8_END_TRANSFER(), /* disable chip */
  41. U8X8_END() /* end of sequence */
  42. };
  43. static const uint8_t u8x8_d_st7586s_s028hn118a_flip0_seq[] = {
  44. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  45. U8X8_C(0x036), /* Scan Direction Setting */
  46. U8X8_A(0x000), /* COM0 -> COM159 SEG0 -> SEG384 */
  47. U8X8_C(0x037), /* Start line 0 */
  48. U8X8_A(0x000),
  49. U8X8_END_TRANSFER(), /* disable chip */
  50. U8X8_END() /* end of sequence */
  51. };
  52. static const uint8_t u8x8_d_st7586s_s028hn118a_flip1_seq[] = {
  53. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  54. U8X8_C(0x036), /* Scan Direction Setting */
  55. U8X8_A(0x0C8), /* COM159 -> COM0 SEG384 -> SEG0 */
  56. U8X8_C(0x037), /* Start line 24 */
  57. U8X8_A(0x018),
  58. U8X8_END_TRANSFER(), /* disable chip */
  59. U8X8_END() /* end of sequence */
  60. };
  61. static uint8_t u8x8_d_st7586s_common(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  62. uint8_t c;
  63. uint8_t *ptr;
  64. uint8_t i, byte;
  65. uint32_t input;
  66. uint8_t output[8];
  67. switch (msg) {
  68. case U8X8_MSG_DISPLAY_DRAW_TILE:
  69. u8x8_cad_StartTransfer(u8x8); // OK Start transfer
  70. u8x8_cad_SendCmd(u8x8, 0x02B); /* Row Address Setting */
  71. u8x8_cad_SendArg(u8x8, 0x000);
  72. u8x8_cad_SendArg(u8x8, 0x008 * ((u8x8_tile_t *)arg_ptr)->y_pos);
  73. u8x8_cad_SendArg(u8x8, 0x000);
  74. u8x8_cad_SendArg(u8x8, u8x8->display_info->pixel_height - 1); /* should this be u8x8->display_info->pixel_height - 1 */
  75. u8x8_cad_SendCmd(u8x8, 0x02C); /* cmd write display data to ram */
  76. c = ((u8x8_tile_t *) arg_ptr)->cnt; //
  77. c *= 8;
  78. ptr = ((u8x8_tile_t *) arg_ptr)->tile_ptr; //
  79. // The ST7586S has an unusual 3 pixels per byte format so here we read in 3 bytes (24 pixels) and
  80. // pack that into 8 bytes of 3 pixels each
  81. while (c > 0) {
  82. input = (((uint32_t)ptr[0] << 16) | ((uint32_t)ptr[1] << 8) | (uint32_t)ptr[2]);
  83. for (i=0; i<8; i++)
  84. {
  85. byte = 0;
  86. if (input & 0x800000) // if bit 23
  87. byte = byte | 0xC0; //set pixel 1
  88. if (input & 0x400000) // if bit 22
  89. byte = byte | 0x18; //set pixel 2
  90. if (input & 0x200000) // if bit 22
  91. byte = byte | 0x3; //set pixel 3
  92. output[i] = byte;
  93. input <<= 3;
  94. }
  95. u8x8_cad_SendData(u8x8, 8, output);
  96. ptr += 3;
  97. c -= 3;
  98. }
  99. u8x8_cad_EndTransfer(u8x8);
  100. break;
  101. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  102. if (arg_int == 0)
  103. u8x8_cad_SendSequence(u8x8, u8x8_d_st7586s_sleep_off);
  104. else
  105. u8x8_cad_SendSequence(u8x8, u8x8_d_st7586s_sleep_on);
  106. break;
  107. #ifdef U8X8_WITH_SET_CONTRAST
  108. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  109. u8x8_cad_StartTransfer(u8x8);
  110. u8x8_cad_SendCmd(u8x8, 0x0C0);
  111. u8x8_cad_SendArg(u8x8, arg_int);
  112. u8x8_cad_SendArg(u8x8, 0);
  113. u8x8_cad_EndTransfer(u8x8);
  114. break;
  115. #endif
  116. default:
  117. return 0;
  118. }
  119. return 1;
  120. }
  121. static const uint8_t u8x8_d_st7586s_s028hn118a_init_seq[] = {
  122. U8X8_START_TRANSFER(),/* enable chip */
  123. U8X8_END_TRANSFER(),/* disable chip */
  124. // U8G_ESC_RST(15), /* hardware reset */
  125. U8X8_DLY(60), /* Delay 60 ms */
  126. U8X8_START_TRANSFER(),/* enable chip */
  127. U8X8_C(0x001), // Soft reset
  128. U8X8_DLY(60), // Delay 120 ms
  129. U8X8_C(0x011), // Sleep Out
  130. U8X8_C(0x028), // Display OFF
  131. U8X8_DLY(25), // Delay 50 ms
  132. U8X8_CAA(0x0C0,0x0E5,0x00),// Vop = F0h in trace a bit too dark
  133. U8X8_CA(0x0C3,0x004), // BIAS = 1/10 0x04 in trace
  134. U8X8_CA(0x0C4,0x005), // Booster = x6 0x05 in trace
  135. U8X8_CA(0x0D0,0x01D), // Enable Analog Circuit
  136. U8X8_CA(0x0B3,0x000), // Set FOSC divider
  137. U8X8_CA(0x0B5,0x08B), // N-Line = 0
  138. U8X8_C(0x039), // 0x39 Monochrome mode. 0x38 - gray Mode
  139. U8X8_C(0x03A), // Enable DDRAM Interface
  140. U8X8_A(0x002), // monochrome and 4-level
  141. U8X8_C(0x036), // Scan Direction Setting
  142. U8X8_A(0x000), // COM0 -> COM159 SEG0 -> SEG384
  143. U8X8_C(0x0B0), // Duty Setting (num rows - 1)
  144. U8X8_A(0x087), // should be 0x87 but caused flicker 0x9F
  145. U8X8_C(0x020), // Display inversion off
  146. U8X8_C(0x02A), // Column Address Setting
  147. U8X8_A(0x000), // COL0 -> COL127
  148. U8X8_A(0x000), //
  149. U8X8_A(0x000), //
  150. U8X8_A(0x07f), // 128*3=384 pixels
  151. U8X8_C(0x02B), // Row Address Setting
  152. U8X8_A(0x000), // ROW0 -> ROW135
  153. U8X8_A(0x000), //
  154. U8X8_A(0x000), //
  155. U8X8_A(0x087), // 136 pixels
  156. U8X8_C(0x0F1), // Frame rate monochrome
  157. U8X8_A(0x00C), // The factory firmware set this to 49.0 Hz 0x07
  158. U8X8_A(0x00C), // This caused a shimmer under 50Hz LED lights
  159. U8X8_A(0x00C), // 69.0 Hz (0x0C) fixes this and should avoid the
  160. U8X8_A(0x00C), // issue in the US too
  161. U8X8_C(0x029), // Display ON
  162. U8X8_END_TRANSFER(),/* disable chip */
  163. U8X8_END() /* end of sequence */
  164. };
  165. static const u8x8_display_info_t u8x8_st7586s_s028hn118a_display_info =
  166. {
  167. /* chip_enable_level = */ 0,
  168. /* chip_disable_level = */ 1,
  169. /* post_chip_enable_wait_ns = */ 5,
  170. /* pre_chip_disable_wait_ns = */ 5,
  171. /* reset_pulse_width_ms = */ 1,
  172. /* post_reset_wait_ms = */ 6,
  173. /* sda_setup_time_ns = */ 20,
  174. /* sck_pulse_width_ns = */ 100, /* datasheet ST7586S */
  175. /* sck_clock_hz = */ 8000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  176. /* ST7586+Atmega128RFA1 works with 8MHz */
  177. /* spi_mode = */ 3, /* active high, rising edge */
  178. /* i2c_bus_clock_100kHz = */ 4,
  179. /* data_setup_time_ns = */ 20, /* datasheet suggests min 20 */
  180. /* write_pulse_width_ns = */ 40,
  181. /* tile_width = */ 48,
  182. /* tile_height = */ 17,
  183. /* default_x_offset = */ 0,
  184. /* flipmode_x_offset = */ 0,
  185. /* pixel_width = */ 384,
  186. /* pixel_height = */ 136
  187. };
  188. /*******************************************************************************
  189. * st7586s_s028hn118a driver. This is the display in the SMART Response XE. This requires 16 bit mode.
  190. ******************************************************************************/
  191. uint8_t u8x8_d_st7586s_s028hn118a(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  192. if (u8x8_d_st7586s_common(u8x8, msg, arg_int, arg_ptr) != 0)
  193. return 1;
  194. switch (msg) {
  195. case U8X8_MSG_DISPLAY_INIT:
  196. u8x8_d_helper_display_init(u8x8);
  197. u8x8_cad_SendSequence(u8x8, u8x8_d_st7586s_s028hn118a_init_seq);
  198. break;
  199. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  200. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7586s_s028hn118a_display_info);
  201. break;
  202. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  203. if ( arg_int == 0 )
  204. {
  205. u8x8_cad_SendSequence(u8x8, u8x8_d_st7586s_s028hn118a_flip0_seq);
  206. u8x8->x_offset = u8x8->display_info->default_x_offset;
  207. }
  208. else
  209. {
  210. u8x8_cad_SendSequence(u8x8, u8x8_d_st7586s_s028hn118a_flip1_seq);
  211. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  212. }
  213. break;
  214. default:
  215. return 0;
  216. }
  217. return 1;
  218. }