ultralcd_st7920_u8glib_rrd.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 ULCDST7920_H
  23. #define ULCDST7920_H
  24. #include <U8glib.h>
  25. #include "delay.h"
  26. #define ST7920_CLK_PIN LCD_PINS_D4
  27. #define ST7920_DAT_PIN LCD_PINS_ENABLE
  28. #define ST7920_CS_PIN LCD_PINS_RS
  29. //#define PAGE_HEIGHT 8 //128 byte framebuffer
  30. #define PAGE_HEIGHT 16 //256 byte framebuffer
  31. //#define PAGE_HEIGHT 32 //512 byte framebuffer
  32. #define LCD_PIXEL_WIDTH 128
  33. #define LCD_PIXEL_HEIGHT 64
  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 ST7920_DELAY_1 DELAY_NS(0)
  38. //#define ST7920_DELAY_2 DELAY_NS(0)
  39. //#define ST7920_DELAY_3 DELAY_NS(0)
  40. #if F_CPU >= 20000000
  41. #define CPU_ST7920_DELAY_1 DELAY_NS(0)
  42. #define CPU_ST7920_DELAY_2 DELAY_NS(0)
  43. #define CPU_ST7920_DELAY_3 DELAY_NS(50)
  44. #elif MB(3DRAG) || MB(K8200) || MB(K8400) || MB(SILVER_GATE)
  45. #define CPU_ST7920_DELAY_1 DELAY_NS(0)
  46. #define CPU_ST7920_DELAY_2 DELAY_NS(188)
  47. #define CPU_ST7920_DELAY_3 DELAY_NS(0)
  48. #elif MB(MINIRAMBO)
  49. #define CPU_ST7920_DELAY_1 DELAY_NS(0)
  50. #define CPU_ST7920_DELAY_2 DELAY_NS(250)
  51. #define CPU_ST7920_DELAY_3 DELAY_NS(0)
  52. #elif MB(RAMBO)
  53. #define CPU_ST7920_DELAY_1 DELAY_NS(0)
  54. #define CPU_ST7920_DELAY_2 DELAY_NS(0)
  55. #define CPU_ST7920_DELAY_3 DELAY_NS(0)
  56. #elif MB(BQ_ZUM_MEGA_3D)
  57. #define CPU_ST7920_DELAY_1 DELAY_NS(0)
  58. #define CPU_ST7920_DELAY_2 DELAY_NS(0)
  59. #define CPU_ST7920_DELAY_3 DELAY_NS(189)
  60. #elif F_CPU == 16000000
  61. #define CPU_ST7920_DELAY_1 DELAY_NS(0)
  62. #define CPU_ST7920_DELAY_2 DELAY_NS(0)
  63. #define CPU_ST7920_DELAY_3 DELAY_NS(63)
  64. #else
  65. #error "No valid condition for delays in 'ultralcd_st7920_u8glib_rrd.h'"
  66. #endif
  67. #ifndef ST7920_DELAY_1
  68. #define ST7920_DELAY_1 CPU_ST7920_DELAY_1
  69. #endif
  70. #ifndef ST7920_DELAY_2
  71. #define ST7920_DELAY_2 CPU_ST7920_DELAY_2
  72. #endif
  73. #ifndef ST7920_DELAY_3
  74. #define ST7920_DELAY_3 CPU_ST7920_DELAY_3
  75. #endif
  76. #define ST7920_SND_BIT \
  77. WRITE(ST7920_CLK_PIN, LOW); ST7920_DELAY_1; \
  78. WRITE(ST7920_DAT_PIN, val & 0x80); ST7920_DELAY_2; \
  79. WRITE(ST7920_CLK_PIN, HIGH); ST7920_DELAY_3; \
  80. val <<= 1
  81. static void ST7920_SWSPI_SND_8BIT(uint8_t val) {
  82. ST7920_SND_BIT; // 1
  83. ST7920_SND_BIT; // 2
  84. ST7920_SND_BIT; // 3
  85. ST7920_SND_BIT; // 4
  86. ST7920_SND_BIT; // 5
  87. ST7920_SND_BIT; // 6
  88. ST7920_SND_BIT; // 7
  89. ST7920_SND_BIT; // 8
  90. }
  91. #if DOGM_SPI_DELAY_US > 0
  92. #define U8G_DELAY() DELAY_US(DOGM_SPI_DELAY_US)
  93. #else
  94. #define U8G_DELAY() u8g_10MicroDelay()
  95. #endif
  96. #define ST7920_CS() { WRITE(ST7920_CS_PIN,1); U8G_DELAY(); }
  97. #define ST7920_NCS() { WRITE(ST7920_CS_PIN,0); }
  98. #define ST7920_SET_CMD() { ST7920_SWSPI_SND_8BIT(0xF8); U8G_DELAY(); }
  99. #define ST7920_SET_DAT() { ST7920_SWSPI_SND_8BIT(0xFA); U8G_DELAY(); }
  100. #define ST7920_WRITE_BYTE(a) { ST7920_SWSPI_SND_8BIT((uint8_t)((a)&0xF0u)); ST7920_SWSPI_SND_8BIT((uint8_t)((a)<<4u)); U8G_DELAY(); }
  101. #define ST7920_WRITE_BYTES(p,l) { for (uint8_t i = l + 1; --i;) { ST7920_SWSPI_SND_8BIT(*p&0xF0); ST7920_SWSPI_SND_8BIT(*p<<4); p++; } U8G_DELAY(); }
  102. uint8_t u8g_dev_rrd_st7920_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
  103. uint8_t i, y;
  104. switch (msg) {
  105. case U8G_DEV_MSG_INIT: {
  106. OUT_WRITE(ST7920_CS_PIN, LOW);
  107. OUT_WRITE(ST7920_DAT_PIN, LOW);
  108. OUT_WRITE(ST7920_CLK_PIN, HIGH);
  109. ST7920_CS();
  110. u8g_Delay(120); //initial delay for boot up
  111. ST7920_SET_CMD();
  112. ST7920_WRITE_BYTE(0x20); //non-extended mode
  113. ST7920_WRITE_BYTE(0x08); //display off, cursor+blink off
  114. ST7920_WRITE_BYTE(0x01); //clear DDRAM ram
  115. u8g_Delay(15); //delay for DDRAM clear
  116. ST7920_WRITE_BYTE(0x24); //extended mode
  117. ST7920_WRITE_BYTE(0x26); //extended mode + GDRAM active
  118. for (y = 0; y < (LCD_PIXEL_HEIGHT) / 2; y++) { //clear GDRAM
  119. ST7920_WRITE_BYTE(0x80 | y); //set y
  120. ST7920_WRITE_BYTE(0x80); //set x = 0
  121. ST7920_SET_DAT();
  122. for (i = 0; i < 2 * (LCD_PIXEL_WIDTH) / 8; i++) //2x width clears both segments
  123. ST7920_WRITE_BYTE(0);
  124. ST7920_SET_CMD();
  125. }
  126. ST7920_WRITE_BYTE(0x0C); //display on, cursor+blink off
  127. ST7920_NCS();
  128. }
  129. break;
  130. case U8G_DEV_MSG_STOP: break;
  131. case U8G_DEV_MSG_PAGE_NEXT: {
  132. uint8_t* ptr;
  133. u8g_pb_t* pb = (u8g_pb_t*)(dev->dev_mem);
  134. y = pb->p.page_y0;
  135. ptr = (uint8_t*)pb->buf;
  136. ST7920_CS();
  137. for (i = 0; i < PAGE_HEIGHT; i ++) {
  138. ST7920_SET_CMD();
  139. if (y < 32) {
  140. ST7920_WRITE_BYTE(0x80 | y); //y
  141. ST7920_WRITE_BYTE(0x80); //x=0
  142. }
  143. else {
  144. ST7920_WRITE_BYTE(0x80 | (y - 32)); //y
  145. ST7920_WRITE_BYTE(0x80 | 8); //x=64
  146. }
  147. ST7920_SET_DAT();
  148. ST7920_WRITE_BYTES(ptr, (LCD_PIXEL_WIDTH) / 8); //ptr is incremented inside of macro
  149. y++;
  150. }
  151. ST7920_NCS();
  152. }
  153. break;
  154. }
  155. #if PAGE_HEIGHT == 8
  156. return u8g_dev_pb8h1_base_fn(u8g, dev, msg, arg);
  157. #elif PAGE_HEIGHT == 16
  158. return u8g_dev_pb16h1_base_fn(u8g, dev, msg, arg);
  159. #else
  160. return u8g_dev_pb32h1_base_fn(u8g, dev, msg, arg);
  161. #endif
  162. }
  163. uint8_t u8g_dev_st7920_128x64_rrd_buf[(LCD_PIXEL_WIDTH) * (PAGE_HEIGHT) / 8] U8G_NOCOMMON;
  164. u8g_pb_t u8g_dev_st7920_128x64_rrd_pb = {{PAGE_HEIGHT, LCD_PIXEL_HEIGHT, 0, 0, 0}, LCD_PIXEL_WIDTH, u8g_dev_st7920_128x64_rrd_buf};
  165. u8g_dev_t u8g_dev_st7920_128x64_rrd_sw_spi = {u8g_dev_rrd_st7920_128x64_fn, &u8g_dev_st7920_128x64_rrd_pb, &u8g_com_null_fn};
  166. class U8GLIB_ST7920_128X64_RRD : public U8GLIB {
  167. public:
  168. U8GLIB_ST7920_128X64_RRD(uint8_t dummy) : U8GLIB(&u8g_dev_st7920_128x64_rrd_sw_spi) { UNUSED(dummy); }
  169. };
  170. #if ENABLED(LIGHTWEIGHT_UI)
  171. // We have to include the code for the lightweight UI here
  172. // as it relies on macros that are only defined in this file.
  173. #include "status_screen_lite_ST7920_spi.h"
  174. #endif
  175. #pragma GCC reset_options
  176. #endif // ULCDST7920_H