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_st7920_u8glib_rrd_AVR.cpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. // NOTE - the HAL version of the rrd device uses a generic ST7920 device. See the
  23. // file u8g_dev_st7920_128x64_HAL.cpp for the HAL version.
  24. #include "../../inc/MarlinConfigPre.h"
  25. #if !defined(U8G_HAL_LINKS) && ANY(__AVR__, ARDUINO_ARCH_STM32, ARDUINO_ARCH_ESP32)
  26. #include "../../inc/MarlinConfig.h"
  27. #if IS_U8GLIB_ST7920
  28. #include "ultralcd_st7920_u8glib_rrd_AVR.h"
  29. // Optimize this code with -O3
  30. #pragma GCC optimize (3)
  31. #ifndef ST7920_DELAY_1
  32. #ifndef LCD_ST7920_DELAY_1
  33. #define LCD_ST7920_DELAY_1 0
  34. #endif
  35. #ifndef BOARD_ST7920_DELAY_1
  36. #define BOARD_ST7920_DELAY_1 0
  37. #endif
  38. #ifndef CPU_ST7920_DELAY_1
  39. #define CPU_ST7920_DELAY_1 0
  40. #endif
  41. #if LCD_ST7920_DELAY_1 || BOARD_ST7920_DELAY_1 || CPU_ST7920_DELAY_1
  42. #define ST7920_DELAY_1 DELAY_NS(_MAX(LCD_ST7920_DELAY_1, BOARD_ST7920_DELAY_1, CPU_ST7920_DELAY_1))
  43. #else
  44. #define ST7920_DELAY_1
  45. #endif
  46. #endif
  47. #ifndef ST7920_DELAY_2
  48. #ifndef LCD_ST7920_DELAY_2
  49. #define LCD_ST7920_DELAY_2 0
  50. #endif
  51. #ifndef BOARD_ST7920_DELAY_2
  52. #define BOARD_ST7920_DELAY_2 0
  53. #endif
  54. #ifndef CPU_ST7920_DELAY_2
  55. #define CPU_ST7920_DELAY_2 0
  56. #endif
  57. #if LCD_ST7920_DELAY_2 || BOARD_ST7920_DELAY_2 || CPU_ST7920_DELAY_2
  58. #define ST7920_DELAY_2 DELAY_NS(_MAX(LCD_ST7920_DELAY_2, BOARD_ST7920_DELAY_2, CPU_ST7920_DELAY_2))
  59. #else
  60. #define ST7920_DELAY_2
  61. #endif
  62. #endif
  63. #ifndef ST7920_DELAY_3
  64. #ifndef LCD_ST7920_DELAY_3
  65. #define LCD_ST7920_DELAY_3 0
  66. #endif
  67. #ifndef BOARD_ST7920_DELAY_3
  68. #define BOARD_ST7920_DELAY_3 0
  69. #endif
  70. #ifndef CPU_ST7920_DELAY_3
  71. #define CPU_ST7920_DELAY_3 0
  72. #endif
  73. #if LCD_ST7920_DELAY_3 || BOARD_ST7920_DELAY_3 || CPU_ST7920_DELAY_3
  74. #define ST7920_DELAY_3 DELAY_NS(_MAX(LCD_ST7920_DELAY_3, BOARD_ST7920_DELAY_3, CPU_ST7920_DELAY_3))
  75. #else
  76. #define ST7920_DELAY_3
  77. #endif
  78. #endif
  79. #ifdef ARDUINO_ARCH_STM32F1
  80. #define ST7920_DAT(V) !!((V) & 0x80)
  81. #else
  82. #define ST7920_DAT(V) ((V) & 0x80)
  83. #endif
  84. #define ST7920_SND_BIT do{ \
  85. WRITE(ST7920_CLK_PIN, LOW); ST7920_DELAY_1; \
  86. WRITE(ST7920_DAT_PIN, ST7920_DAT(val)); ST7920_DELAY_2; \
  87. WRITE(ST7920_CLK_PIN, HIGH); ST7920_DELAY_3; \
  88. val <<= 1; }while(0)
  89. // Optimize this code with -O3
  90. #pragma GCC optimize (3)
  91. void ST7920_SWSPI_SND_8BIT(uint8_t val) {
  92. ST7920_SND_BIT; // 1
  93. ST7920_SND_BIT; // 2
  94. ST7920_SND_BIT; // 3
  95. ST7920_SND_BIT; // 4
  96. ST7920_SND_BIT; // 5
  97. ST7920_SND_BIT; // 6
  98. ST7920_SND_BIT; // 7
  99. ST7920_SND_BIT; // 8
  100. }
  101. uint8_t u8g_dev_rrd_st7920_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
  102. uint8_t i, y;
  103. switch (msg) {
  104. case U8G_DEV_MSG_INIT: {
  105. OUT_WRITE(ST7920_CS_PIN, LOW);
  106. OUT_WRITE(ST7920_DAT_PIN, LOW);
  107. OUT_WRITE(ST7920_CLK_PIN, HIGH);
  108. ST7920_CS();
  109. u8g_Delay(120); // Initial delay for boot up
  110. ST7920_SET_CMD();
  111. ST7920_WRITE_BYTE(0x20); // Non-extended mode
  112. ST7920_WRITE_BYTE(0x08); // Display off, cursor+blink off
  113. ST7920_WRITE_BYTE(0x01); // Clear DDRAM ram
  114. u8g_Delay(15); // Delay for DDRAM clear
  115. ST7920_WRITE_BYTE(0x24); // Extended mode
  116. ST7920_WRITE_BYTE(0x26); // Extended mode + GDRAM active
  117. for (y = 0; y < (LCD_PIXEL_HEIGHT) / 2; y++) { // Clear GDRAM
  118. ST7920_WRITE_BYTE(0x80 | y); // Set y
  119. ST7920_WRITE_BYTE(0x80); // Set x = 0
  120. ST7920_SET_DAT();
  121. for (i = 0; i < 2 * (LCD_PIXEL_WIDTH) / 8; i++) // 2x width clears both segments
  122. ST7920_WRITE_BYTE(0);
  123. ST7920_SET_CMD();
  124. }
  125. ST7920_WRITE_BYTE(0x0C); // Display on, cursor+blink off
  126. ST7920_NCS();
  127. }
  128. break;
  129. case U8G_DEV_MSG_STOP: break;
  130. case U8G_DEV_MSG_PAGE_NEXT: {
  131. uint8_t *ptr;
  132. u8g_pb_t *pb = (u8g_pb_t*)(dev->dev_mem);
  133. y = pb->p.page_y0;
  134. ptr = (uint8_t*)pb->buf;
  135. ST7920_CS();
  136. for (i = 0; i < PAGE_HEIGHT; i ++) {
  137. ST7920_SET_CMD();
  138. if (y < 32) {
  139. ST7920_WRITE_BYTE(0x80 | y); // y
  140. ST7920_WRITE_BYTE(0x80); // x = 0
  141. }
  142. else {
  143. ST7920_WRITE_BYTE(0x80 | (y - 32)); // y
  144. ST7920_WRITE_BYTE(0x80 | 8); // x = 64
  145. }
  146. ST7920_SET_DAT();
  147. ST7920_WRITE_BYTES(ptr, (LCD_PIXEL_WIDTH) / 8); // ptr incremented inside of macro!
  148. y++;
  149. }
  150. ST7920_NCS();
  151. }
  152. break;
  153. }
  154. #if PAGE_HEIGHT == 8
  155. return u8g_dev_pb8h1_base_fn(u8g, dev, msg, arg);
  156. #elif PAGE_HEIGHT == 16
  157. return u8g_dev_pb16h1_base_fn(u8g, dev, msg, arg);
  158. #else
  159. return u8g_dev_pb32h1_base_fn(u8g, dev, msg, arg);
  160. #endif
  161. }
  162. uint8_t u8g_dev_st7920_128x64_rrd_buf[(LCD_PIXEL_WIDTH) * (PAGE_HEIGHT) / 8] U8G_NOCOMMON;
  163. 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 };
  164. 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 };
  165. #pragma GCC reset_options
  166. #if ENABLED(LIGHTWEIGHT_UI)
  167. #include "../../HAL/shared/HAL_ST7920.h"
  168. void ST7920_cs() { ST7920_CS(); }
  169. void ST7920_ncs() { ST7920_NCS(); }
  170. void ST7920_set_cmd() { ST7920_SET_CMD(); }
  171. void ST7920_set_dat() { ST7920_SET_DAT(); }
  172. void ST7920_write_byte(const uint8_t val) { ST7920_WRITE_BYTE(val); }
  173. #endif
  174. #endif // IS_U8GLIB_ST7920
  175. #endif // !U8G_HAL_LINKS && (__AVR__ || ARDUINO_ARCH_STM32 || ARDUINO_ARCH_ESP32)