u8g_dev_st7565_64128n.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. u8g_dev_st7565_64128n.c (Displaytech)
  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. /* init sequence from https://github.com/adafruit/ST7565-LCD/blob/master/ST7565/ST7565.cpp */
  32. static const uint8_t u8g_dev_st7565_64128n_init_seq[] PROGMEM = {
  33. U8G_ESC_CS(0), /* disable chip */
  34. U8G_ESC_ADR(0), /* instruction mode */
  35. U8G_ESC_CS(1), /* enable chip */
  36. U8G_ESC_RST(15), /* do reset low pulse with (15*16)+2 milliseconds (=maximum delay)*/
  37. 0x0A2, /* 0x0a2: LCD bias 1/9 (according to Displaytech 64128N datasheet) */
  38. 0x0A0, /* Normal ADC Select (according to Displaytech 64128N datasheet) */
  39. 0x0c8, /* common output mode: set scan direction normal operation/SHL Select, 0x0c0 --> SHL = 0, normal, 0x0c8 --> SHL = 1 */
  40. 0x040, /* Display start line for Displaytech 64128N */
  41. 0x028 | 0x04, /* power control: turn on voltage converter */
  42. U8G_ESC_DLY(50), /* delay 50 ms */
  43. 0x028 | 0x06, /* power control: turn on voltage regulator */
  44. U8G_ESC_DLY(50), /* delay 50 ms */
  45. 0x028 | 0x07, /* power control: turn on voltage follower */
  46. U8G_ESC_DLY(50), /* delay 50 ms */
  47. 0x010, /* Set V0 voltage resistor ratio. Setting for controlling brightness of Displaytech 64128N */
  48. 0x0a6, /* display normal, bit val 0: LCD pixel off. */
  49. 0x081, /* set contrast */
  50. 0x01e, /* Contrast value. Setting for controlling brightness of Displaytech 64128N */
  51. 0x0af, /* display on */
  52. U8G_ESC_DLY(100), /* delay 100 ms */
  53. 0x0a5, /* display all points, ST7565 */
  54. U8G_ESC_DLY(100), /* delay 100 ms */
  55. U8G_ESC_DLY(100), /* delay 100 ms */
  56. 0x0a4, /* normal display */
  57. U8G_ESC_CS(0), /* disable chip */
  58. U8G_ESC_END /* end of sequence */
  59. };
  60. static const uint8_t u8g_dev_st7565_64128n_data_start[] PROGMEM = {
  61. U8G_ESC_ADR(0), /* instruction mode */
  62. U8G_ESC_CS(1), /* enable chip */
  63. 0x010, /* set upper 4 bit of the col adr to 0x10 */
  64. 0x000, /* set lower 4 bit of the col adr to 0x00. Changed for DisplayTech 64128N */
  65. U8G_ESC_END /* end of sequence */
  66. };
  67. static const uint8_t u8g_dev_st7565_64128n_sleep_on[] PROGMEM = {
  68. U8G_ESC_ADR(0), /* instruction mode */
  69. U8G_ESC_CS(1), /* enable chip */
  70. 0x0ac, /* static indicator off */
  71. 0x000, /* indicator register set (not sure if this is required) */
  72. 0x0ae, /* display off */
  73. 0x0a5, /* all points on */
  74. U8G_ESC_CS(0), /* disable chip, bugfix 12 nov 2014 */
  75. U8G_ESC_END /* end of sequence */
  76. };
  77. static const uint8_t u8g_dev_st7565_64128n_sleep_off[] PROGMEM = {
  78. U8G_ESC_ADR(0), /* instruction mode */
  79. U8G_ESC_CS(1), /* enable chip */
  80. 0x0a4, /* all points off */
  81. 0x0af, /* display on */
  82. U8G_ESC_DLY(50), /* delay 50 ms */
  83. U8G_ESC_CS(0), /* disable chip, bugfix 12 nov 2014 */
  84. U8G_ESC_END /* end of sequence */
  85. };
  86. uint8_t u8g_dev_st7565_64128n_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  87. {
  88. switch(msg)
  89. {
  90. case U8G_DEV_MSG_INIT:
  91. u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_400NS);
  92. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_64128n_init_seq);
  93. break;
  94. case U8G_DEV_MSG_STOP:
  95. break;
  96. case U8G_DEV_MSG_PAGE_NEXT:
  97. {
  98. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  99. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_64128n_data_start);
  100. u8g_WriteByte(u8g, dev, 0x0b0 | pb->p.page); /* select current page (ST7565R) */
  101. u8g_SetAddress(u8g, dev, 1); /* data mode */
  102. if ( u8g_pb_WriteBuffer(pb, u8g, dev) == 0 )
  103. return 0;
  104. u8g_SetChipSelect(u8g, dev, 0);
  105. }
  106. break;
  107. case U8G_DEV_MSG_CONTRAST:
  108. u8g_SetChipSelect(u8g, dev, 1);
  109. u8g_SetAddress(u8g, dev, 0); /* instruction mode */
  110. u8g_WriteByte(u8g, dev, 0x081);
  111. u8g_WriteByte(u8g, dev, (*(uint8_t *)arg) >> 2);
  112. u8g_SetChipSelect(u8g, dev, 0);
  113. return 1;
  114. case U8G_DEV_MSG_SLEEP_ON:
  115. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_64128n_sleep_on);
  116. return 1;
  117. case U8G_DEV_MSG_SLEEP_OFF:
  118. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_64128n_sleep_off);
  119. return 1;
  120. }
  121. return u8g_dev_pb8v1_base_fn(u8g, dev, msg, arg);
  122. }
  123. uint8_t u8g_dev_st7565_64128n_2x_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  124. {
  125. switch(msg)
  126. {
  127. case U8G_DEV_MSG_INIT:
  128. u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_400NS);
  129. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_64128n_init_seq);
  130. break;
  131. case U8G_DEV_MSG_STOP:
  132. break;
  133. case U8G_DEV_MSG_PAGE_NEXT:
  134. {
  135. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  136. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_64128n_data_start);
  137. u8g_WriteByte(u8g, dev, 0x0b0 | (2*pb->p.page)); /* select current page (ST7565R) */
  138. u8g_SetAddress(u8g, dev, 1); /* data mode */
  139. u8g_WriteSequence(u8g, dev, pb->width, pb->buf);
  140. u8g_SetChipSelect(u8g, dev, 0);
  141. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_64128n_data_start);
  142. u8g_WriteByte(u8g, dev, 0x0b0 | (2*pb->p.page+1)); /* select current page (ST7565R) */
  143. u8g_SetAddress(u8g, dev, 1); /* data mode */
  144. u8g_WriteSequence(u8g, dev, pb->width, (uint8_t *)(pb->buf)+pb->width);
  145. u8g_SetChipSelect(u8g, dev, 0);
  146. }
  147. break;
  148. case U8G_DEV_MSG_CONTRAST:
  149. u8g_SetChipSelect(u8g, dev, 1);
  150. u8g_SetAddress(u8g, dev, 0); /* instruction mode */
  151. u8g_WriteByte(u8g, dev, 0x081);
  152. u8g_WriteByte(u8g, dev, (*(uint8_t *)arg) >> 2);
  153. u8g_SetChipSelect(u8g, dev, 0);
  154. return 1;
  155. case U8G_DEV_MSG_SLEEP_ON:
  156. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_64128n_sleep_on);
  157. return 1;
  158. case U8G_DEV_MSG_SLEEP_OFF:
  159. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7565_64128n_sleep_off);
  160. return 1;
  161. }
  162. return u8g_dev_pb16v1_base_fn(u8g, dev, msg, arg);
  163. }
  164. U8G_PB_DEV(u8g_dev_st7565_64128n_sw_spi, WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_st7565_64128n_fn, U8G_COM_SW_SPI);
  165. U8G_PB_DEV(u8g_dev_st7565_64128n_hw_spi, WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_st7565_64128n_fn, U8G_COM_HW_SPI);
  166. U8G_PB_DEV(u8g_dev_st7565_64128n_parallel, WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_st7565_64128n_fn, U8G_COM_PARALLEL);
  167. uint8_t u8g_dev_st7565_64128n_2x_buf[WIDTH*2] U8G_NOCOMMON ;
  168. u8g_pb_t u8g_dev_st7565_64128n_2x_pb = { {16, HEIGHT, 0, 0, 0}, WIDTH, u8g_dev_st7565_64128n_2x_buf};
  169. u8g_dev_t u8g_dev_st7565_64128n_2x_sw_spi = { u8g_dev_st7565_64128n_2x_fn, &u8g_dev_st7565_64128n_2x_pb, U8G_COM_SW_SPI };
  170. u8g_dev_t u8g_dev_st7565_64128n_2x_hw_spi = { u8g_dev_st7565_64128n_2x_fn, &u8g_dev_st7565_64128n_2x_pb, U8G_COM_HW_SPI };
  171. u8g_dev_t u8g_dev_st7565_64128n_2x_hw_parallel = { u8g_dev_st7565_64128n_2x_fn, &u8g_dev_st7565_64128n_2x_pb, U8G_COM_PARALLEL };