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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 ENABLED(U8GLIB_ST7920)
  28. #include "ultralcd_st7920_u8glib_rrd_AVR.h"
  29. #ifndef ST7920_DELAY_1
  30. #ifdef BOARD_ST7920_DELAY_1
  31. #define ST7920_DELAY_1 BOARD_ST7920_DELAY_1
  32. #else
  33. #define ST7920_DELAY_1 CPU_ST7920_DELAY_1
  34. #endif
  35. #endif
  36. #ifndef ST7920_DELAY_2
  37. #ifdef BOARD_ST7920_DELAY_2
  38. #define ST7920_DELAY_2 BOARD_ST7920_DELAY_2
  39. #else
  40. #define ST7920_DELAY_2 CPU_ST7920_DELAY_2
  41. #endif
  42. #endif
  43. #ifndef ST7920_DELAY_3
  44. #ifdef BOARD_ST7920_DELAY_3
  45. #define ST7920_DELAY_3 BOARD_ST7920_DELAY_3
  46. #else
  47. #define ST7920_DELAY_3 CPU_ST7920_DELAY_3
  48. #endif
  49. #endif
  50. // Optimize this code with -O3
  51. #pragma GCC optimize (3)
  52. #ifdef ARDUINO_ARCH_STM32F1
  53. #define ST7920_DAT(V) !!((V) & 0x80)
  54. #else
  55. #define ST7920_DAT(V) ((V) & 0x80)
  56. #endif
  57. #define ST7920_SND_BIT do{ \
  58. WRITE(ST7920_CLK_PIN, LOW); ST7920_DELAY_1; \
  59. WRITE(ST7920_DAT_PIN, ST7920_DAT(val)); ST7920_DELAY_2; \
  60. WRITE(ST7920_CLK_PIN, HIGH); ST7920_DELAY_3; \
  61. val <<= 1; }while(0)
  62. // Optimize this code with -O3
  63. #pragma GCC optimize (3)
  64. void ST7920_SWSPI_SND_8BIT(uint8_t val) {
  65. ST7920_SND_BIT; // 1
  66. ST7920_SND_BIT; // 2
  67. ST7920_SND_BIT; // 3
  68. ST7920_SND_BIT; // 4
  69. ST7920_SND_BIT; // 5
  70. ST7920_SND_BIT; // 6
  71. ST7920_SND_BIT; // 7
  72. ST7920_SND_BIT; // 8
  73. }
  74. uint8_t u8g_dev_rrd_st7920_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
  75. uint8_t i, y;
  76. switch (msg) {
  77. case U8G_DEV_MSG_INIT: {
  78. OUT_WRITE(ST7920_CS_PIN, LOW);
  79. OUT_WRITE(ST7920_DAT_PIN, LOW);
  80. OUT_WRITE(ST7920_CLK_PIN, HIGH);
  81. ST7920_CS();
  82. u8g_Delay(120); // Initial delay for boot up
  83. ST7920_SET_CMD();
  84. ST7920_WRITE_BYTE(0x20); // Non-extended mode
  85. ST7920_WRITE_BYTE(0x08); // Display off, cursor+blink off
  86. ST7920_WRITE_BYTE(0x01); // Clear DDRAM ram
  87. u8g_Delay(15); // Delay for DDRAM clear
  88. ST7920_WRITE_BYTE(0x24); // Extended mode
  89. ST7920_WRITE_BYTE(0x26); // Extended mode + GDRAM active
  90. for (y = 0; y < (LCD_PIXEL_HEIGHT) / 2; y++) { // Clear GDRAM
  91. ST7920_WRITE_BYTE(0x80 | y); // Set y
  92. ST7920_WRITE_BYTE(0x80); // Set x = 0
  93. ST7920_SET_DAT();
  94. for (i = 0; i < 2 * (LCD_PIXEL_WIDTH) / 8; i++) // 2x width clears both segments
  95. ST7920_WRITE_BYTE(0);
  96. ST7920_SET_CMD();
  97. }
  98. ST7920_WRITE_BYTE(0x0C); // Display on, cursor+blink off
  99. ST7920_NCS();
  100. }
  101. break;
  102. case U8G_DEV_MSG_STOP: break;
  103. case U8G_DEV_MSG_PAGE_NEXT: {
  104. uint8_t* ptr;
  105. u8g_pb_t* pb = (u8g_pb_t*)(dev->dev_mem);
  106. y = pb->p.page_y0;
  107. ptr = (uint8_t*)pb->buf;
  108. ST7920_CS();
  109. for (i = 0; i < PAGE_HEIGHT; i ++) {
  110. ST7920_SET_CMD();
  111. if (y < 32) {
  112. ST7920_WRITE_BYTE(0x80 | y); // y
  113. ST7920_WRITE_BYTE(0x80); // x = 0
  114. }
  115. else {
  116. ST7920_WRITE_BYTE(0x80 | (y - 32)); // y
  117. ST7920_WRITE_BYTE(0x80 | 8); // x = 64
  118. }
  119. ST7920_SET_DAT();
  120. ST7920_WRITE_BYTES(ptr, (LCD_PIXEL_WIDTH) / 8); // ptr incremented inside of macro!
  121. y++;
  122. }
  123. ST7920_NCS();
  124. }
  125. break;
  126. }
  127. #if PAGE_HEIGHT == 8
  128. return u8g_dev_pb8h1_base_fn(u8g, dev, msg, arg);
  129. #elif PAGE_HEIGHT == 16
  130. return u8g_dev_pb16h1_base_fn(u8g, dev, msg, arg);
  131. #else
  132. return u8g_dev_pb32h1_base_fn(u8g, dev, msg, arg);
  133. #endif
  134. }
  135. uint8_t u8g_dev_st7920_128x64_rrd_buf[(LCD_PIXEL_WIDTH) * (PAGE_HEIGHT) / 8] U8G_NOCOMMON;
  136. 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 };
  137. 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 };
  138. #pragma GCC reset_options
  139. #if ENABLED(LIGHTWEIGHT_UI)
  140. #include "../../HAL/shared/HAL_ST7920.h"
  141. void ST7920_cs() { ST7920_CS(); }
  142. void ST7920_ncs() { ST7920_NCS(); }
  143. void ST7920_set_cmd() { ST7920_SET_CMD(); }
  144. void ST7920_set_dat() { ST7920_SET_DAT(); }
  145. void ST7920_write_byte(const uint8_t val) { ST7920_WRITE_BYTE(val); }
  146. #endif
  147. #endif // U8GLIB_ST7920
  148. #endif // !U8G_HAL_LINKS && (__AVR__ || ARDUINO_ARCH_STM32 || ARDUINO_ARCH_ESP32)