ultralcd_st7565_u8glib_VIKI.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016, 2017 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifndef ULCDST7565_H
  23. #define ULCDST7565_H
  24. #include <U8glib.h>
  25. #include "delay.h"
  26. #define ST7565_CLK_PIN DOGLCD_SCK
  27. #define ST7565_DAT_PIN DOGLCD_MOSI
  28. #define ST7565_CS_PIN DOGLCD_CS
  29. #define ST7565_A0_PIN DOGLCD_A0
  30. #define LCD_PIXEL_WIDTH 128
  31. #define LCD_PIXEL_HEIGHT 64
  32. #define PAGE_HEIGHT 8
  33. //set optimization so ARDUINO optimizes this file
  34. #pragma GCC optimize (3)
  35. // If you want you can define your own set of delays in Configuration.h
  36. //#define ST7565_DELAY_1 DELAY_NS(0)
  37. //#define ST7565_DELAY_2 DELAY_NS(0)
  38. //#define ST7565_DELAY_3 DELAY_NS(0)
  39. /*
  40. #define ST7565_DELAY_1 u8g_10MicroDelay()
  41. #define ST7565_DELAY_2 u8g_10MicroDelay()
  42. #define ST7565_DELAY_3 u8g_10MicroDelay()
  43. */
  44. #if F_CPU >= 20000000
  45. #define CPU_ST7565_DELAY_1 DELAY_NS(0)
  46. #define CPU_ST7565_DELAY_2 DELAY_NS(0)
  47. #define CPU_ST7565_DELAY_3 DELAY_NS(63)
  48. #elif MB(3DRAG) || MB(K8200) || MB(K8400)
  49. #define CPU_ST7565_DELAY_1 DELAY_NS(0)
  50. #define CPU_ST7565_DELAY_2 DELAY_NS(188)
  51. #define CPU_ST7565_DELAY_3 DELAY_NS(0)
  52. #elif MB(MINIRAMBO)
  53. #define CPU_ST7565_DELAY_1 DELAY_NS(0)
  54. #define CPU_ST7565_DELAY_2 DELAY_NS(250)
  55. #define CPU_ST7565_DELAY_3 DELAY_NS(0)
  56. #elif MB(RAMBO)
  57. #define CPU_ST7565_DELAY_1 DELAY_NS(0)
  58. #define CPU_ST7565_DELAY_2 DELAY_NS(0)
  59. #define CPU_ST7565_DELAY_3 DELAY_NS(0)
  60. #elif F_CPU == 16000000
  61. #define CPU_ST7565_DELAY_1 DELAY_NS(0)
  62. #define CPU_ST7565_DELAY_2 DELAY_NS(0)
  63. #define CPU_ST7565_DELAY_3 DELAY_NS(63)
  64. #else
  65. #error "No valid condition for delays in 'ultralcd_st7565_u8glib_VIKI.h'"
  66. #endif
  67. #ifndef ST7565_DELAY_1
  68. #define ST7565_DELAY_1 CPU_ST7565_DELAY_1
  69. #endif
  70. #ifndef ST7565_DELAY_2
  71. #define ST7565_DELAY_2 CPU_ST7565_DELAY_2
  72. #endif
  73. #ifndef ST7565_DELAY_3
  74. #define ST7565_DELAY_3 CPU_ST7565_DELAY_3
  75. #endif
  76. // On Viki2 the LCD and the SD card share a single SPI
  77. #define HARDWARE_SPI ((DOGLCD_SCK == SCK_PIN) && (DOGLCD_MOSI == MOSI_PIN))
  78. #if HARDWARE_SPI // using the hardware SPI
  79. #define ST7565_WRITE_BYTE(a) { SPDR = a; while (!TEST(SPSR, SPIF)); U8G_DELAY(); }
  80. #define ST7560_WriteSequence(count, pointer) { uint8_t *ptr = pointer; for (uint8_t i = 0; i < count; i++) {SPDR = *ptr++; while (!TEST(SPSR, SPIF));} U8G_DELAY(); }
  81. #else // !HARDWARE_SPI
  82. #define ST7565_SND_BIT \
  83. WRITE(ST7565_CLK_PIN, LOW); ST7565_DELAY_1; \
  84. WRITE(ST7565_DAT_PIN, val & 0x80); ST7565_DELAY_2; \
  85. WRITE(ST7565_CLK_PIN, HIGH); ST7565_DELAY_3; \
  86. WRITE(ST7565_CLK_PIN, LOW);\
  87. val <<= 1
  88. static void ST7565_SWSPI_SND_8BIT(uint8_t val) {
  89. ST7565_SND_BIT; // 1
  90. ST7565_SND_BIT; // 2
  91. ST7565_SND_BIT; // 3
  92. ST7565_SND_BIT; // 4
  93. ST7565_SND_BIT; // 5
  94. ST7565_SND_BIT; // 6
  95. ST7565_SND_BIT; // 7
  96. ST7565_SND_BIT; // 8
  97. }
  98. #define ST7565_WRITE_BYTE(a) { ST7565_SWSPI_SND_8BIT((uint8_t)a); U8G_DELAY(); }
  99. #define ST7560_WriteSequence(count, pointer) { uint8_t *ptr = pointer; for (uint8_t i = 0; i < count; i++) { ST7565_SWSPI_SND_8BIT(*ptr++); } U8G_DELAY(); }
  100. #endif // !HARDWARE_SPI
  101. #if DOGM_SPI_DELAY_US > 0
  102. #define U8G_DELAY() DELAY_US(DOGM_SPI_DELAY_US)
  103. #else
  104. #define U8G_DELAY() u8g_10MicroDelay()
  105. #endif
  106. #define ST7565_CS() do{ WRITE(ST7565_CS_PIN, HIGH); U8G_DELAY(); }while(0)
  107. #define ST7565_NCS() WRITE(ST7565_CS_PIN, LOW)
  108. #define ST7565_A0() do{ WRITE(ST7565_A0_PIN, HIGH); U8G_DELAY(); }while(0)
  109. #define ST7565_NA0() WRITE(ST7565_A0_PIN, LOW)
  110. #define ST7565_ADC_REVERSE(N) ST7565_WRITE_BYTE(0xA0 | ((N) & 0x1))
  111. #define ST7565_BIAS_MODE(N) ST7565_WRITE_BYTE(0xA2 | ((N) & 0x1))
  112. #define ST7565_ALL_PIX(N) ST7565_WRITE_BYTE(0xA4 | ((N) & 0x1))
  113. #define ST7565_INVERTED(N) ST7565_WRITE_BYTE(0xA6 | ((N) & 0x1))
  114. #define ST7565_ON(N) ST7565_WRITE_BYTE(0xAE | ((N) & 0x1))
  115. #define ST7565_OUT_MODE(N) ST7565_WRITE_BYTE(0xC0 | ((N) & 0x1) << 3)
  116. #define ST7565_POWER_CONTROL(N) ST7565_WRITE_BYTE(0x28 | (N))
  117. #define ST7565_V0_RATIO(N) ST7565_WRITE_BYTE(0x10 | ((N) & 0x7)) // Specific to Displaytech 64128N? (ST7565 is 0x20 | N)
  118. #define ST7565_CONTRAST(N) do{ ST7565_WRITE_BYTE(0x81); ST7565_WRITE_BYTE(N); }while(0)
  119. #define ST7565_COLUMN_ADR(N) do{ ST7565_WRITE_BYTE(0x10 | (((N) >> 4) & 0xF)); ST7565_WRITE_BYTE((N) & 0xF); }while(0)
  120. #define ST7565_PAGE_ADR(N) ST7565_WRITE_BYTE(0xB0 | (N))
  121. #define ST7565_START_LINE(N) ST7565_WRITE_BYTE(0x40 | (N))
  122. #define ST7565_SLEEP_MODE() ST7565_WRITE_BYTE(0xAC)
  123. #define ST7565_NOOP() ST7565_WRITE_BYTE(0xE3)
  124. uint8_t u8g_dev_st7565_64128n_2x_VIKI_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
  125. switch (msg) {
  126. case U8G_DEV_MSG_INIT: {
  127. OUT_WRITE(ST7565_CS_PIN, LOW);
  128. OUT_WRITE(ST7565_DAT_PIN, LOW);
  129. OUT_WRITE(ST7565_CLK_PIN, LOW);
  130. #if HARDWARE_SPI
  131. OUT_WRITE(SDSS, 1); // must be set to an output first or else will never go into master mode
  132. SPCR = 0x50; // enable SPI in master mode at fast speed
  133. SPSR = 1; // kick it up to 2x speed mode
  134. #endif
  135. OUT_WRITE(ST7565_A0_PIN, LOW);
  136. ST7565_CS(); // chip select off
  137. ST7565_NA0(); // instruction mode
  138. ST7565_NCS(); // chip select
  139. ST7565_BIAS_MODE(0); // 0xA2: LCD bias 1/9 (according to Displaytech 64128N datasheet)
  140. ST7565_ADC_REVERSE(0); // Normal (not flipped) ADC Select (according to Displaytech 64128N datasheet)
  141. ST7565_OUT_MODE(1); // common output mode: set scan direction normal operation/SHL Select; 0x0C0 --> SHL = 0; normal; 0x0C8 --> SHL = 1
  142. ST7565_START_LINE(0); // Display start line for Displaytech 64128N
  143. ST7565_POWER_CONTROL(0x4); // power control: turn on Booster
  144. U8G_ESC_DLY(50); // delay 50 ms - hangs after a reset if used
  145. ST7565_POWER_CONTROL(0x6); // power control: turn on Booster, Voltage Regulator
  146. U8G_ESC_DLY(50); // delay 50 ms - hangs after a reset if used
  147. ST7565_POWER_CONTROL(0x7); // power control: turn on Booster, Voltage Regulator, Voltage Follower
  148. U8G_ESC_DLY(50); // delay 50 ms - hangs after a reset if used
  149. ST7565_V0_RATIO(0); // Set V0 Voltage Resistor ratio. Setting for controlling brightness of Displaytech 64128N
  150. ST7565_INVERTED(0); // display normal, bit val 0: LCD pixel off.
  151. ST7565_CONTRAST(0x1E); // Contrast value. Setting for controlling contrast of Displaytech 64128N
  152. ST7565_ON(1); // display on
  153. U8G_ESC_DLY(100); // delay 100 ms
  154. ST7565_ALL_PIX(1); // display all points; ST7565
  155. U8G_ESC_DLY(100); // delay 100 ms
  156. U8G_ESC_DLY(100); // delay 100 ms
  157. ST7565_ALL_PIX(0); // normal display
  158. ST7565_CS(); // chip select off
  159. } // end of sequence
  160. break;
  161. case U8G_DEV_MSG_STOP: break;
  162. case U8G_DEV_MSG_PAGE_NEXT: {
  163. u8g_pb_t *pb = (u8g_pb_t*)(dev->dev_mem);
  164. ST7565_CS(); // chip select off
  165. ST7565_NA0(); // instruction mode
  166. ST7565_NCS(); // chip select
  167. ST7565_COLUMN_ADR(0x00); // high 4 bits to 0, low 4 bits to 0. Changed for DisplayTech 64128N
  168. // end of sequence
  169. ST7565_PAGE_ADR(2 * pb->p.page); // select current page (ST7565R)
  170. ST7565_A0(); // data mode
  171. ST7560_WriteSequence((uint8_t)pb->width, (uint8_t*)pb->buf);
  172. ST7565_CS(); // chip select off
  173. ST7565_NA0(); // instruction mode
  174. ST7565_NCS(); // chip select
  175. ST7565_COLUMN_ADR(0x00); // high 4 bits to 0, low 4 bits to 0
  176. // end of sequence
  177. ST7565_PAGE_ADR(2 * pb->p.page + 1); // select current page (ST7565R)
  178. ST7565_A0(); // data mode
  179. ST7560_WriteSequence((uint8_t)pb->width, (uint8_t*)(pb->buf) + pb->width);
  180. ST7565_CS(); // chip select off
  181. }
  182. break;
  183. case U8G_DEV_MSG_CONTRAST:
  184. ST7565_NCS();
  185. ST7565_NA0(); // instruction mode
  186. ST7565_CONTRAST((*(uint8_t*)arg) >> 2);
  187. ST7565_CS(); // chip select off
  188. return 1;
  189. case U8G_DEV_MSG_SLEEP_ON:
  190. ST7565_NA0(); // instruction mode
  191. ST7565_NCS(); // chip select
  192. ST7565_SLEEP_MODE(); // static indicator off
  193. //ST7565_WRITE_BYTE(0x00); // indicator register set (not sure if this is required)
  194. ST7565_ON(0); // display off
  195. ST7565_ALL_PIX(1); // all points on
  196. ST7565_CS(); // chip select off
  197. return 1;
  198. case U8G_DEV_MSG_SLEEP_OFF:
  199. ST7565_NA0(); // instruction mode
  200. ST7565_NCS(); // chip select
  201. ST7565_ALL_PIX(0); // all points off
  202. ST7565_ON(1); // display on
  203. U8G_ESC_DLY(50); // delay 50 ms
  204. ST7565_CS(); // chip select off
  205. return 1;
  206. }
  207. return u8g_dev_pb16v1_base_fn(u8g, dev, msg, arg);
  208. }
  209. uint8_t u8g_dev_st7565_64128n_2x_VIKI_buf[LCD_PIXEL_WIDTH * 2] U8G_NOCOMMON;
  210. u8g_pb_t u8g_dev_st7565_64128n_2x_VIKI_pb = {{16, LCD_PIXEL_HEIGHT, 0, 0, 0}, LCD_PIXEL_WIDTH, u8g_dev_st7565_64128n_2x_VIKI_buf};
  211. u8g_dev_t u8g_dev_st7565_64128n_2x_VIKI_sw_spi = {u8g_dev_st7565_64128n_2x_VIKI_fn, &u8g_dev_st7565_64128n_2x_VIKI_pb, &u8g_com_null_fn};
  212. class U8GLIB_ST7565_64128n_2x_VIKI : public U8GLIB {
  213. public:
  214. U8GLIB_ST7565_64128n_2x_VIKI(uint8_t dummy)
  215. : U8GLIB(&u8g_dev_st7565_64128n_2x_VIKI_sw_spi)
  216. { }
  217. U8GLIB_ST7565_64128n_2x_VIKI(uint8_t sck, uint8_t mosi, uint8_t cs, uint8_t a0, uint8_t reset = U8G_PIN_NONE)
  218. : U8GLIB(&u8g_dev_st7565_64128n_2x_VIKI_sw_spi)
  219. { }
  220. };
  221. #pragma GCC reset_options
  222. #endif // ULCDST7565_H