My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ultralcd_st7565_u8glib_VIKI.h 10KB

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 <src/Marlin.h>
  25. #if ENABLED(U8GLIB_ST7565_64128N)
  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. #include <U8glib.h>
  31. #define WIDTH 128
  32. #define HEIGHT 64
  33. #define PAGE_HEIGHT 8
  34. //set optimization so ARDUINO optimizes this file
  35. #pragma GCC optimize (3)
  36. // If you want you can define your own set of delays in Configuration.h
  37. //#define ST7565_DELAY_1 DELAY_0_NOP
  38. //#define ST7565_DELAY_2 DELAY_0_NOP
  39. //#define ST7565_DELAY_3 DELAY_0_NOP
  40. /*
  41. #define ST7565_DELAY_1 u8g_10MicroDelay()
  42. #define ST7565_DELAY_2 u8g_10MicroDelay()
  43. #define ST7565_DELAY_3 u8g_10MicroDelay()
  44. */
  45. #if F_CPU >= 20000000
  46. #define CPU_ST7565_DELAY_1 DELAY_0_NOP
  47. #define CPU_ST7565_DELAY_2 DELAY_0_NOP
  48. #define CPU_ST7565_DELAY_3 DELAY_1_NOP
  49. #elif (MOTHERBOARD == BOARD_3DRAG) || (MOTHERBOARD == BOARD_K8200) || (MOTHERBOARD == BOARD_K8400)
  50. #define CPU_ST7565_DELAY_1 DELAY_0_NOP
  51. #define CPU_ST7565_DELAY_2 DELAY_3_NOP
  52. #define CPU_ST7565_DELAY_3 DELAY_0_NOP
  53. #elif (MOTHERBOARD == BOARD_MINIRAMBO)
  54. #define CPU_ST7565_DELAY_1 DELAY_0_NOP
  55. #define CPU_ST7565_DELAY_2 DELAY_4_NOP
  56. #define CPU_ST7565_DELAY_3 DELAY_0_NOP
  57. #elif (MOTHERBOARD == BOARD_RAMBO)
  58. #define CPU_ST7565_DELAY_1 DELAY_0_NOP
  59. #define CPU_ST7565_DELAY_2 DELAY_0_NOP
  60. #define CPU_ST7565_DELAY_3 DELAY_0_NOP
  61. #elif F_CPU == 16000000
  62. #define CPU_ST7565_DELAY_1 DELAY_0_NOP
  63. #define CPU_ST7565_DELAY_2 DELAY_0_NOP
  64. #define CPU_ST7565_DELAY_3 DELAY_1_NOP
  65. #else
  66. #error "No valid condition for delays in 'ultralcd_st7565_u8glib_VIKI.h'"
  67. #endif
  68. #ifndef ST7565_DELAY_1
  69. #define ST7565_DELAY_1 CPU_ST7565_DELAY_1
  70. #endif
  71. #ifndef ST7565_DELAY_2
  72. #define ST7565_DELAY_2 CPU_ST7565_DELAY_2
  73. #endif
  74. #ifndef ST7565_DELAY_3
  75. #define ST7565_DELAY_3 CPU_ST7565_DELAY_3
  76. #endif
  77. #if ENABLED(SHARED_SPI) // Re-ARM requires that the LCD and the SD card share a single SPI
  78. #define ST7565_WRITE_BYTE(a) { spiSend((uint8_t)a); U8G_DELAY; }
  79. #define ST7560_WriteSequence(count, pointer) { uint8_t *ptr = pointer; for (uint8_t i = 0; i < count; i++) {spiSend( *ptr++);} DELAY_10US; }
  80. #else
  81. #define ST7565_SND_BIT \
  82. WRITE(ST7565_CLK_PIN, LOW); ST7565_DELAY_1; \
  83. WRITE(ST7565_DAT_PIN, val & 0x80); ST7565_DELAY_2; \
  84. WRITE(ST7565_CLK_PIN, HIGH); ST7565_DELAY_3; \
  85. WRITE(ST7565_CLK_PIN, LOW);\
  86. val <<= 1
  87. static void ST7565_SWSPI_SND_8BIT(uint8_t val) {
  88. ST7565_SND_BIT; // 1
  89. ST7565_SND_BIT; // 2
  90. ST7565_SND_BIT; // 3
  91. ST7565_SND_BIT; // 4
  92. ST7565_SND_BIT; // 5
  93. ST7565_SND_BIT; // 6
  94. ST7565_SND_BIT; // 7
  95. ST7565_SND_BIT; // 8
  96. }
  97. #define ST7565_WRITE_BYTE(a) { ST7565_SWSPI_SND_8BIT((uint8_t)a); U8G_DELAY; }
  98. #define ST7560_WriteSequence(count, pointer) { uint8_t *ptr = pointer; for (uint8_t i = 0; i < count; i++) {ST7565_SWSPI_SND_8BIT( *ptr++);} DELAY_10US; }
  99. #endif
  100. #if defined(DOGM_SPI_DELAY_US) && DOGM_SPI_DELAY_US > 0
  101. #define U8G_DELAY delayMicroseconds(DOGM_SPI_DELAY_US)
  102. #else
  103. #define U8G_DELAY u8g_10MicroDelay()
  104. #endif
  105. #define ST7565_CS() { WRITE(ST7565_CS_PIN,1); U8G_DELAY; }
  106. #define ST7565_NCS() { WRITE(ST7565_CS_PIN,0); }
  107. #define ST7565_A0() { WRITE(ST7565_A0_PIN,1); U8G_DELAY; }
  108. #define ST7565_NA0() { WRITE(ST7565_A0_PIN,0); }
  109. uint8_t u8g_dev_st7565_64128n_2x_VIKI_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
  110. switch (msg) {
  111. case U8G_DEV_MSG_INIT:
  112. {
  113. OUT_WRITE(ST7565_CS_PIN, LOW);
  114. #if ENABLED(SHARED_SPI)
  115. u8g_Delay(250);
  116. spiBegin();
  117. #ifndef SPI_SPEED
  118. #define SPI_SPEED SPI_FULL_SPEED // use same SPI speed as SD card
  119. #endif
  120. spiInit(SPI_SPEED);
  121. #else
  122. OUT_WRITE(ST7565_DAT_PIN, LOW);
  123. OUT_WRITE(ST7565_CLK_PIN, LOW);
  124. #endif
  125. OUT_WRITE(ST7565_A0_PIN, LOW);
  126. ST7565_CS(); /* disable chip */
  127. ST7565_NA0(); /* instruction mode */
  128. ST7565_NCS(); /* enable chip */
  129. ST7565_WRITE_BYTE(0x0A2); /* 0x0a2: LCD bias 1/9 (according to Displaytech 64128N datasheet) */
  130. ST7565_WRITE_BYTE(0x0A0); /* Normal ADC Select (according to Displaytech 64128N datasheet) */
  131. ST7565_WRITE_BYTE(0x0c8); /* common output mode: set scan direction normal operation/SHL Select; 0x0c0 --> SHL = 0; normal; 0x0c8 --> SHL = 1 */
  132. ST7565_WRITE_BYTE(0x040); /* Display start line for Displaytech 64128N */
  133. ST7565_WRITE_BYTE(0x028 | 0x04); /* power control: turn on voltage converter */
  134. // U8G_ESC_DLY(50); /* delay 50 ms - hangs after a reset if used */
  135. ST7565_WRITE_BYTE(0x028 | 0x06); /* power control: turn on voltage regulator */
  136. // U8G_ESC_DLY(50); /* delay 50 ms - hangs after a reset if used */
  137. ST7565_WRITE_BYTE(0x028 | 0x07); /* power control: turn on voltage follower */
  138. // U8G_ESC_DLY(50); /* delay 50 ms - hangs after a reset if used */
  139. ST7565_WRITE_BYTE(0x010); /* Set V0 voltage resistor ratio. Setting for controlling brightness of Displaytech 64128N */
  140. ST7565_WRITE_BYTE(0x0a6); /* display normal, bit val 0: LCD pixel off. */
  141. ST7565_WRITE_BYTE(0x081); /* set contrast */
  142. ST7565_WRITE_BYTE(0x01e); /* Contrast value. Setting for controlling brightness of Displaytech 64128N */
  143. ST7565_WRITE_BYTE(0x0af); /* display on */
  144. U8G_ESC_DLY(100); /* delay 100 ms */
  145. ST7565_WRITE_BYTE(0x0a5); /* display all points; ST7565 */
  146. U8G_ESC_DLY(100); /* delay 100 ms */
  147. U8G_ESC_DLY(100); /* delay 100 ms */
  148. ST7565_WRITE_BYTE(0x0a4); /* normal display */
  149. ST7565_CS(); /* disable chip */
  150. } /* end of sequence */
  151. break;
  152. case U8G_DEV_MSG_STOP:
  153. break;
  154. case U8G_DEV_MSG_PAGE_NEXT:
  155. { u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  156. ST7565_CS(); /* disable chip */
  157. ST7565_NA0(); /* instruction mode */
  158. ST7565_NCS(); /* enable chip */
  159. ST7565_WRITE_BYTE(0x010); /* set upper 4 bit of the col adr to 0x10 */
  160. ST7565_WRITE_BYTE(0x000); /* set lower 4 bit of the col adr to 0x00. Changed for DisplayTech 64128N */
  161. /* end of sequence */
  162. ST7565_WRITE_BYTE(0x0b0 | (2*pb->p.page));; /* select current page (ST7565R) */
  163. ST7565_A0(); /* data mode */
  164. ST7560_WriteSequence( (uint8_t) pb->width, (uint8_t *)pb->buf);
  165. ST7565_CS(); /* disable chip */
  166. ST7565_NA0(); /* instruction mode */
  167. ST7565_NCS(); /* enable chip */
  168. ST7565_WRITE_BYTE(0x010); /* set upper 4 bit of the col adr to 0x10 */
  169. ST7565_WRITE_BYTE(0x000); /* set lower 4 bit of the col adr to 0x00. Changed for DisplayTech 64128N */
  170. /* end of sequence */
  171. ST7565_WRITE_BYTE(0x0b0 | (2*pb->p.page+1)); /* select current page (ST7565R) */
  172. ST7565_A0(); /* data mode */
  173. ST7560_WriteSequence( (uint8_t) pb->width, (uint8_t *)(pb->buf)+pb->width);
  174. ST7565_CS(); /* disable chip */
  175. }
  176. break;
  177. case U8G_DEV_MSG_CONTRAST:
  178. ST7565_NCS();
  179. ST7565_NA0(); /* instruction mode */
  180. ST7565_WRITE_BYTE(0x081);
  181. ST7565_WRITE_BYTE((*(uint8_t *)arg) >> 2);
  182. ST7565_CS(); /* disable chip */
  183. return 1;
  184. case U8G_DEV_MSG_SLEEP_ON:
  185. ST7565_NA0(); /* instruction mode */
  186. ST7565_NCS(); /* enable chip */
  187. ST7565_WRITE_BYTE(0x0ac); /* static indicator off */
  188. ST7565_WRITE_BYTE(0x000); /* indicator register set (not sure if this is required) */
  189. ST7565_WRITE_BYTE(0x0ae); /* display off */
  190. ST7565_WRITE_BYTE(0x0a5); /* all points on */
  191. ST7565_CS(); /* disable chip , bugfix 12 nov 2014 */
  192. /* end of sequence */
  193. return 1;
  194. case U8G_DEV_MSG_SLEEP_OFF:
  195. ST7565_NA0(); /* instruction mode */
  196. ST7565_NCS(); /* enable chip */
  197. ST7565_WRITE_BYTE(0x0a4); /* all points off */
  198. ST7565_WRITE_BYTE(0x0af); /* display on */
  199. U8G_ESC_DLY(50); /* delay 50 ms */
  200. ST7565_CS(); /* disable chip , bugfix 12 nov 2014 */
  201. /* end of sequence */
  202. return 1;
  203. }
  204. return u8g_dev_pb16v1_base_fn(u8g, dev, msg, arg);
  205. }
  206. uint8_t u8g_dev_st7565_64128n_2x_VIKI_buf[WIDTH*2] U8G_NOCOMMON ;
  207. u8g_pb_t u8g_dev_st7565_64128n_2x_VIKI_pb = { {16, HEIGHT, 0, 0, 0}, WIDTH, u8g_dev_st7565_64128n_2x_VIKI_buf};
  208. 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};
  209. class U8GLIB_ST7565_64128n_2x_VIKI : public U8GLIB {
  210. public:
  211. U8GLIB_ST7565_64128n_2x_VIKI(uint8_t dummy)
  212. : U8GLIB(&u8g_dev_st7565_64128n_2x_VIKI_sw_spi)
  213. { }
  214. U8GLIB_ST7565_64128n_2x_VIKI(uint8_t sck, uint8_t mosi, uint8_t cs, uint8_t a0, uint8_t reset = U8G_PIN_NONE)
  215. : U8GLIB(&u8g_dev_st7565_64128n_2x_VIKI_sw_spi)
  216. { }
  217. };
  218. #pragma GCC reset_options
  219. #endif // U8GLIB_ST7565
  220. #endif // ULCDST7565_H