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.1KB

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