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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // 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/MarlinConfig.h"
  25. #if ENABLED(U8GLIB_ST7920)
  26. #if !(defined(U8G_HAL_LINKS) || defined(__SAM3X8E__))
  27. #define ST7920_CLK_PIN LCD_PINS_D4
  28. #define ST7920_DAT_PIN LCD_PINS_ENABLE
  29. #define ST7920_CS_PIN LCD_PINS_RS
  30. //#define PAGE_HEIGHT 8 //128 byte framebuffer
  31. #define PAGE_HEIGHT 16 //256 byte framebuffer
  32. //#define PAGE_HEIGHT 32 //512 byte framebuffer
  33. #define LCD_PIXEL_WIDTH 128
  34. #define LCD_PIXEL_HEIGHT 64
  35. #include <U8glib.h>
  36. //set optimization so ARDUINO optimizes this file
  37. #pragma GCC optimize (3)
  38. // If you want you can define your own set of delays in Configuration.h
  39. //#define ST7920_DELAY_1 DELAY_0_NOP
  40. //#define ST7920_DELAY_2 DELAY_0_NOP
  41. //#define ST7920_DELAY_3 DELAY_0_NOP
  42. #if F_CPU >= 20000000
  43. #define CPU_ST7920_DELAY_1 DELAY_0_NOP
  44. #define CPU_ST7920_DELAY_2 DELAY_0_NOP
  45. #define CPU_ST7920_DELAY_3 DELAY_1_NOP
  46. #elif MB(3DRAG) || MB(K8200) || MB(K8400) || MB(SILVER_GATE)
  47. #define CPU_ST7920_DELAY_1 DELAY_0_NOP
  48. #define CPU_ST7920_DELAY_2 DELAY_3_NOP
  49. #define CPU_ST7920_DELAY_3 DELAY_0_NOP
  50. #elif MB(MINIRAMBO)
  51. #define CPU_ST7920_DELAY_1 DELAY_0_NOP
  52. #define CPU_ST7920_DELAY_2 DELAY_4_NOP
  53. #define CPU_ST7920_DELAY_3 DELAY_0_NOP
  54. #elif MB(RAMBO)
  55. #define CPU_ST7920_DELAY_1 DELAY_0_NOP
  56. #define CPU_ST7920_DELAY_2 DELAY_0_NOP
  57. #define CPU_ST7920_DELAY_3 DELAY_0_NOP
  58. #elif F_CPU == 16000000
  59. #define CPU_ST7920_DELAY_1 DELAY_0_NOP
  60. #define CPU_ST7920_DELAY_2 DELAY_0_NOP
  61. #define CPU_ST7920_DELAY_3 DELAY_1_NOP
  62. #else
  63. #error "No valid condition for delays in 'ultralcd_st7920_u8glib_rrd.h'"
  64. #endif
  65. #ifndef ST7920_DELAY_1
  66. #define ST7920_DELAY_1 CPU_ST7920_DELAY_1
  67. #endif
  68. #ifndef ST7920_DELAY_2
  69. #define ST7920_DELAY_2 CPU_ST7920_DELAY_2
  70. #endif
  71. #ifndef ST7920_DELAY_3
  72. #define ST7920_DELAY_3 CPU_ST7920_DELAY_3
  73. #endif
  74. #if defined(DOGM_SPI_DELAY_US) && DOGM_SPI_DELAY_US > 0
  75. #define U8G_DELAY() delayMicroseconds(DOGM_SPI_DELAY_US)
  76. #else
  77. #define U8G_DELAY() u8g_10MicroDelay()
  78. #endif
  79. static void ST7920_WRITE_BYTE(uint8_t val) {
  80. for (uint8_t i = 0; i < 8; i++) {
  81. WRITE(ST7920_DAT_PIN, val & 0x80);
  82. WRITE(ST7920_CLK_PIN, HIGH);
  83. WRITE(ST7920_CLK_PIN, LOW);
  84. val <<= 1;
  85. }
  86. }
  87. #define ST7920_SET_CMD() { ST7920_WRITE_BYTE(0xF8); U8G_DELAY(); }
  88. #define ST7920_SET_DAT() { ST7920_WRITE_BYTE(0xFA); U8G_DELAY(); }
  89. #define ST7920_WRITE_NIBBLES(a) { ST7920_WRITE_BYTE((uint8_t)((a)&0xF0u)); ST7920_WRITE_BYTE((uint8_t)((a)<<4u)); U8G_DELAY(); }
  90. #define ST7920_WRITE_NIBBLES_P(p,l) { for (uint8_t i = l + 1; --i;) { ST7920_WRITE_BYTE(*p&0xF0); ST7920_WRITE_BYTE(*p<<4); p++; } U8G_DELAY(); }
  91. #define ST7920_CS() { WRITE(ST7920_CS_PIN,1); U8G_DELAY(); }
  92. #define ST7920_NCS() { WRITE(ST7920_CS_PIN,0); }
  93. uint8_t u8g_dev_rrd_st7920_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
  94. uint8_t i, y;
  95. switch (msg) {
  96. case U8G_DEV_MSG_INIT: {
  97. OUT_WRITE(ST7920_CS_PIN, LOW);
  98. OUT_WRITE(ST7920_DAT_PIN, LOW);
  99. OUT_WRITE(ST7920_CLK_PIN, LOW);
  100. ST7920_CS();
  101. u8g_Delay(120); //initial delay for boot up
  102. ST7920_SET_CMD();
  103. ST7920_WRITE_NIBBLES(0x08); //display off, cursor+blink off
  104. ST7920_WRITE_NIBBLES(0x01); //clear CGRAM ram
  105. u8g_Delay(15); //delay for CGRAM clear
  106. ST7920_WRITE_NIBBLES(0x3E); //extended mode + GDRAM active
  107. for (y = 0; y < (LCD_PIXEL_HEIGHT) / 2; y++) { //clear GDRAM
  108. ST7920_WRITE_NIBBLES(0x80 | y); //set y
  109. ST7920_WRITE_NIBBLES(0x80); //set x = 0
  110. ST7920_SET_DAT();
  111. for (i = 0; i < 2 * (LCD_PIXEL_WIDTH) / 8; i++) //2x width clears both segments
  112. ST7920_WRITE_NIBBLES(0);
  113. ST7920_SET_CMD();
  114. }
  115. ST7920_WRITE_NIBBLES(0x0C); //display on, cursor+blink off
  116. ST7920_NCS();
  117. }
  118. break;
  119. case U8G_DEV_MSG_STOP: break;
  120. case U8G_DEV_MSG_PAGE_NEXT: {
  121. uint8_t* ptr;
  122. u8g_pb_t* pb = (u8g_pb_t*)(dev->dev_mem);
  123. y = pb->p.page_y0;
  124. ptr = (uint8_t*)pb->buf;
  125. ST7920_CS();
  126. for (i = 0; i < PAGE_HEIGHT; i ++) {
  127. ST7920_SET_CMD();
  128. if (y < 32) {
  129. ST7920_WRITE_NIBBLES(0x80 | y); //y
  130. ST7920_WRITE_NIBBLES(0x80); //x=0
  131. }
  132. else {
  133. ST7920_WRITE_NIBBLES(0x80 | (y - 32)); //y
  134. ST7920_WRITE_NIBBLES(0x80 | 8); //x=64
  135. }
  136. ST7920_SET_DAT();
  137. ST7920_WRITE_NIBBLES_P(ptr, (LCD_PIXEL_WIDTH) / 8); //ptr is incremented inside of macro
  138. y++;
  139. }
  140. ST7920_NCS();
  141. }
  142. break;
  143. }
  144. #if PAGE_HEIGHT == 8
  145. return u8g_dev_pb8h1_base_fn(u8g, dev, msg, arg);
  146. #elif PAGE_HEIGHT == 16
  147. return u8g_dev_pb16h1_base_fn(u8g, dev, msg, arg);
  148. #else
  149. return u8g_dev_pb32h1_base_fn(u8g, dev, msg, arg);
  150. #endif
  151. }
  152. uint8_t u8g_dev_st7920_128x64_rrd_buf[(LCD_PIXEL_WIDTH) * (PAGE_HEIGHT) / 8] U8G_NOCOMMON;
  153. 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};
  154. 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};
  155. #pragma GCC reset_options
  156. #endif // U8G_HAL_LINKS
  157. #endif // U8GLIB_ST7920