u8g_dev_st7565_lm6063.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. u8g_dev_st7565_lm6063.c
  3. Universal 8bit Graphics Library
  4. Copyright (c) 2011, 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. */
  27. #include "u8g.h"
  28. #define WIDTH 128
  29. #define HEIGHT 64
  30. #define PAGE_HEIGHT 8
  31. #ifdef OLD_ADAFRUIT_CODE
  32. static const uint8_t OLD_u8g_dev_st7565_lm6063_init_seq[] PROGMEM = {
  33. U8G_ESC_CS(0), /* disable chip */
  34. U8G_ESC_ADR(0), /* instruction mode */
  35. U8G_ESC_RST(1), /* do reset low pulse with (1*16)+2 milliseconds */
  36. U8G_ESC_CS(1), /* enable chip */
  37. 0x040, /* set display start line */
  38. 0x0a1, /* ADC set to reverse */
  39. 0x0c8, /* common output mode: set scan direction normal operation/SHL Select / 17 Jan: seems to be a bug, must be 0x0c0 */
  40. 0x0a6, /* display normal, bit val 0: LCD pixel off. */
  41. 0x0a2, /* LCD bias 1/9 */
  42. 0x02f, /* all power control circuits on */
  43. /*0x0f8,*/ /* set booster ratio to */
  44. /*0x000, */ /* 4x */
  45. /*0x027,*/ /* set V0 voltage resistor ratio to large */
  46. 0x081, /* set contrast */
  47. 0x018, /* contrast value*/
  48. 0x0ac, /* indicator */
  49. 0x000, /* disable */
  50. 0x0af, /* display on */
  51. U8G_ESC_DLY(100), /* delay 100 ms */
  52. 0x0a5, /* display all points, ST7565 */
  53. U8G_ESC_DLY(100), /* delay 100 ms */
  54. U8G_ESC_DLY(100), /* delay 100 ms */
  55. 0x0a4, /* normal display */
  56. U8G_ESC_CS(0), /* disable chip */
  57. U8G_ESC_END /* end of sequence */
  58. };
  59. #endif
  60. /* init sequence from https://github.com/adafruit/ST7565-LCD/blob/master/ST7565/ST7565.cpp */
  61. static const uint8_t u8g_dev_st7565_lm6063_init_seq[] PROGMEM = {
  62. U8G_ESC_CS(0), /* disable chip */
  63. U8G_ESC_ADR(0), /* instruction mode */
  64. U8G_ESC_CS(1), /* enable chip */
  65. U8G_ESC_RST(15), /* do reset low pulse with (15*16)+2 milliseconds (=maximum delay)*/
  66. 0x0a3, /* 0x0a2: LCD bias 1/9 (suggested for the LM6063), 0x0a3: Used by Adafruit */
  67. 0x0a1, /* 0x0a1: ADC set to reverse (suggested for the LM6063), 0x0a0: Used by Adafruit -> normal mode */
  68. 0x0c0, /* common output mode: set scan direction normal operation/SHL Select, 0x0c0 --> SHL = 0, normal, 0x0c8 --> SHL = 1 */
  69. 0x040, /* set display start line */
  70. 0x028 | 0x04, /* power control: turn on voltage converter */
  71. U8G_ESC_DLY(50), /* delay 50 ms */
  72. 0x028 | 0x06, /* power control: turn on voltage regulator */
  73. U8G_ESC_DLY(50), /* delay 50 ms */
  74. 0x028 | 0x07, /* power control: turn on voltage follower */
  75. U8G_ESC_DLY(50), /* delay 50 ms */
  76. 0x026, /* set V0 voltage resistor ratio to 6 (Adafruit Value, no info from LM6063 Manual) */
  77. 0x0a6, /* display normal, bit val 0: LCD pixel off. */
  78. 0x081, /* set contrast */
  79. 0x018, /* contrast value*/
  80. /*0x0ac,*/ /* indicator */
  81. /*0x000,*/ /* disable */
  82. 0x0af, /* display on */
  83. U8G_ESC_DLY(100), /* delay 100 ms */
  84. 0x0a5, /* display all points, ST7565 */
  85. U8G_ESC_DLY(100), /* delay 100 ms */
  86. U8G_ESC_DLY(100), /* delay 100 ms */
  87. 0x0a4, /* normal display */
  88. U8G_ESC_CS(0), /* disable chip */
  89. U8G_ESC_END /* end of sequence */
  90. };
  91. static const uint8_t u8g_dev_st7565_lm6063_data_start[] PROGMEM = {
  92. U8G_ESC_ADR(0), /* instruction mode */
  93. U8G_ESC_CS(1), /* enable chip */
  94. 0x010, /* set upper 4 bit of the col adr to 0 */
  95. 0x000, /* set lower 4 bit of the col adr to 0 */
  96. U8G_ESC_END /* end of sequence */
  97. };
  98. static const uint8_t u8g_dev_st7565_st7565_sleep_on[] PROGMEM = {
  99. U8G_ESC_ADR(0), /* instruction mode */
  100. U8G_ESC_CS(1), /* enable chip */
  101. 0x0ac, /* static indicator off */
  102. 0x000, /* indicator register set (not sure if this is required) */
  103. 0x0ae, /* display off */
  104. 0x0a5, /* all points on */
  105. U8G_ESC_CS(0), /* disable chip, bugfix 12 nov 2014 */
  106. U8G_ESC_END /* end of sequence */
  107. };
  108. static const uint8_t u8g_dev_st7565_st7565_sleep_off[] PROGMEM = {
  109. U8G_ESC_ADR(0), /* instruction mode */
  110. U8G_ESC_CS(1), /* enable chip */
  111. 0x0a4, /* all points off */
  112. 0x0af, /* display on */
  113. U8G_ESC_DLY(50), /* delay 50 ms */
  114. U8G_ESC_CS(0), /* disable chip, bugfix 12 nov 2014 */
  115. U8G_ESC_END /* end of sequence */
  116. };
  117. uint8_t u8g_dev_st7565_lm6063_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  118. {
  119. switch(msg)
  120. {
  121. case U8G_DEV_MSG_INIT:
  122. u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_400NS);
  123. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_lm6063_init_seq);
  124. break;
  125. case U8G_DEV_MSG_STOP:
  126. break;
  127. case U8G_DEV_MSG_PAGE_NEXT:
  128. {
  129. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  130. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_lm6063_data_start);
  131. u8g_WriteByte(u8g, dev, 0x0b0 | pb->p.page); /* select current page (ST7565R) */
  132. u8g_SetAddress(u8g, dev, 1); /* data mode */
  133. if ( u8g_pb_WriteBuffer(pb, u8g, dev) == 0 )
  134. return 0;
  135. u8g_SetChipSelect(u8g, dev, 0);
  136. }
  137. break;
  138. case U8G_DEV_MSG_CONTRAST:
  139. u8g_SetChipSelect(u8g, dev, 1);
  140. u8g_SetAddress(u8g, dev, 0); /* instruction mode */
  141. u8g_WriteByte(u8g, dev, 0x081);
  142. u8g_WriteByte(u8g, dev, (*(uint8_t *)arg) >> 2);
  143. u8g_SetChipSelect(u8g, dev, 0);
  144. return 1;
  145. case U8G_DEV_MSG_SLEEP_ON:
  146. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_st7565_sleep_on);
  147. return 1;
  148. case U8G_DEV_MSG_SLEEP_OFF:
  149. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_st7565_sleep_off);
  150. return 1;
  151. }
  152. return u8g_dev_pb8v1_base_fn(u8g, dev, msg, arg);
  153. }
  154. uint8_t u8g_dev_st7565_lm6063_2x_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  155. {
  156. switch(msg)
  157. {
  158. case U8G_DEV_MSG_INIT:
  159. u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_400NS);
  160. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_lm6063_init_seq);
  161. break;
  162. case U8G_DEV_MSG_STOP:
  163. break;
  164. case U8G_DEV_MSG_PAGE_NEXT:
  165. {
  166. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  167. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_lm6063_data_start);
  168. u8g_WriteByte(u8g, dev, 0x0b0 | (2*pb->p.page)); /* select current page (ST7565R) */
  169. u8g_SetAddress(u8g, dev, 1); /* data mode */
  170. u8g_WriteSequence(u8g, dev, pb->width, pb->buf);
  171. u8g_SetChipSelect(u8g, dev, 0);
  172. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_lm6063_data_start);
  173. u8g_WriteByte(u8g, dev, 0x0b0 | (2*pb->p.page+1)); /* select current page (ST7565R) */
  174. u8g_SetAddress(u8g, dev, 1); /* data mode */
  175. u8g_WriteSequence(u8g, dev, pb->width, (uint8_t *)(pb->buf)+pb->width);
  176. u8g_SetChipSelect(u8g, dev, 0);
  177. }
  178. break;
  179. case U8G_DEV_MSG_CONTRAST:
  180. u8g_SetChipSelect(u8g, dev, 1);
  181. u8g_SetAddress(u8g, dev, 0); /* instruction mode */
  182. u8g_WriteByte(u8g, dev, 0x081);
  183. u8g_WriteByte(u8g, dev, (*(uint8_t *)arg) >> 2);
  184. u8g_SetChipSelect(u8g, dev, 0);
  185. return 1;
  186. case U8G_DEV_MSG_SLEEP_ON:
  187. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_st7565_sleep_on);
  188. return 1;
  189. case U8G_DEV_MSG_SLEEP_OFF:
  190. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_st7565_sleep_off);
  191. return 1;
  192. }
  193. return u8g_dev_pb16v1_base_fn(u8g, dev, msg, arg);
  194. }
  195. U8G_PB_DEV(u8g_dev_st7565_lm6063_sw_spi, WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_st7565_lm6063_fn, U8G_COM_SW_SPI);
  196. U8G_PB_DEV(u8g_dev_st7565_lm6063_hw_spi, WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_st7565_lm6063_fn, U8G_COM_HW_SPI);
  197. uint8_t u8g_dev_st7565_lm6063_2x_buf[WIDTH*2] U8G_NOCOMMON ;
  198. u8g_pb_t u8g_dev_st7565_lm6063_2x_pb = { {16, HEIGHT, 0, 0, 0}, WIDTH, u8g_dev_st7565_lm6063_2x_buf};
  199. u8g_dev_t u8g_dev_st7565_lm6063_2x_sw_spi = { u8g_dev_st7565_lm6063_2x_fn, &u8g_dev_st7565_lm6063_2x_pb, U8G_COM_SW_SPI };
  200. u8g_dev_t u8g_dev_st7565_lm6063_2x_hw_spi = { u8g_dev_st7565_lm6063_2x_fn, &u8g_dev_st7565_lm6063_2x_pb, U8G_COM_HW_SPI };