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.

u8g_dev_st7920_128x64_HAL.cpp 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. /**
  23. * Based on u8g_dev_st7920_128x64.c
  24. *
  25. * Universal 8bit Graphics Library
  26. *
  27. * Copyright (c) 2011, olikraus@gmail.com
  28. * All rights reserved.
  29. *
  30. * Redistribution and use in source and binary forms, with or without modification,
  31. * are permitted provided that the following conditions are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright notice, this list
  34. * of conditions and the following disclaimer.
  35. *
  36. * * Redistributions in binary form must reproduce the above copyright notice, this
  37. * list of conditions and the following disclaimer in the documentation and/or other
  38. * materials provided with the distribution.
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  41. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  42. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  43. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  45. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  47. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  48. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  49. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  50. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  51. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  52. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53. */
  54. #include "../../inc/MarlinConfigPre.h"
  55. #if HAS_GRAPHICAL_LCD
  56. #include "HAL_LCD_com_defines.h"
  57. #define PAGE_HEIGHT 8
  58. /* init sequence from https://github.com/adafruit/ST7565-LCD/blob/master/ST7565/ST7565.cpp */
  59. static const uint8_t u8g_dev_st7920_128x64_HAL_init_seq[] PROGMEM = {
  60. U8G_ESC_CS(0), // disable chip
  61. U8G_ESC_ADR(0), // instruction mode
  62. U8G_ESC_RST(15), // do reset low pulse with (15*16)+2 milliseconds (=maximum delay)
  63. U8G_ESC_DLY(100), // 8 Dez 2012: additional delay 100 ms because of reset
  64. U8G_ESC_CS(1), // enable chip
  65. U8G_ESC_DLY(50), // delay 50 ms
  66. 0x038, // 8 Bit interface (DL=1), basic instruction set (RE=0)
  67. 0x00C, // display on, cursor & blink off; 0x08: all off
  68. 0x006, // Entry mode: Cursor move to right, DDRAM address counter (AC) plus 1, no shift
  69. 0x002, // disable scroll, enable CGRAM adress
  70. 0x001, // clear RAM, needs 1.6 ms
  71. U8G_ESC_DLY(100), // delay 100 ms
  72. U8G_ESC_CS(0), // disable chip
  73. U8G_ESC_END // end of sequence
  74. };
  75. void clear_graphics_DRAM(u8g_t *u8g, u8g_dev_t *dev) {
  76. u8g_SetChipSelect(u8g, dev, 1);
  77. u8g_Delay(1);
  78. u8g_SetAddress(u8g, dev, 0); // cmd mode
  79. u8g_WriteByte(u8g, dev, 0x08); //display off, cursor+blink off
  80. u8g_WriteByte(u8g, dev, 0x3E); //extended mode + GDRAM active
  81. LOOP_L_N(y, (LCD_PIXEL_HEIGHT) / 2) { //clear GDRAM
  82. u8g_WriteByte(u8g, dev, 0x80 | y); //set y
  83. u8g_WriteByte(u8g, dev, 0x80); //set x = 0
  84. u8g_SetAddress(u8g, dev, 1); /* data mode */
  85. LOOP_L_N(i, 2 * (LCD_PIXEL_WIDTH) / 8) //2x width clears both segments
  86. u8g_WriteByte(u8g, dev, 0);
  87. u8g_SetAddress(u8g, dev, 0); /* cmd mode */
  88. }
  89. u8g_WriteByte(u8g, dev, 0x0C); //display on, cursor+blink off
  90. u8g_SetChipSelect(u8g, dev, 0);
  91. }
  92. uint8_t u8g_dev_st7920_128x64_HAL_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
  93. switch (msg) {
  94. case U8G_DEV_MSG_INIT:
  95. u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_400NS);
  96. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7920_128x64_HAL_init_seq);
  97. clear_graphics_DRAM(u8g, dev);
  98. break;
  99. case U8G_DEV_MSG_STOP:
  100. break;
  101. case U8G_DEV_MSG_PAGE_NEXT: {
  102. uint8_t y, i;
  103. uint8_t *ptr;
  104. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  105. u8g_SetAddress(u8g, dev, 0); /* cmd mode */
  106. u8g_SetChipSelect(u8g, dev, 1);
  107. y = pb->p.page_y0;
  108. ptr = (uint8_t *)pb->buf;
  109. for (i = 0; i < 8; i ++) {
  110. u8g_SetAddress(u8g, dev, 0); /* cmd mode */
  111. u8g_WriteByte(u8g, dev, 0x03E ); /* enable extended mode */
  112. if (y < 32) {
  113. u8g_WriteByte(u8g, dev, 0x080 | y ); /* y pos */
  114. u8g_WriteByte(u8g, dev, 0x080 ); /* set x pos to 0*/
  115. }
  116. else {
  117. u8g_WriteByte(u8g, dev, 0x080 | (y-32) ); /* y pos */
  118. u8g_WriteByte(u8g, dev, 0x080 | 8); /* set x pos to 64*/
  119. }
  120. u8g_SetAddress(u8g, dev, 1); /* data mode */
  121. u8g_WriteSequence(u8g, dev, (LCD_PIXEL_WIDTH) / 8, ptr);
  122. ptr += (LCD_PIXEL_WIDTH) / 8;
  123. y++;
  124. }
  125. u8g_SetChipSelect(u8g, dev, 0);
  126. }
  127. break;
  128. }
  129. return u8g_dev_pb8h1_base_fn(u8g, dev, msg, arg);
  130. }
  131. uint8_t u8g_dev_st7920_128x64_HAL_4x_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
  132. switch (msg) {
  133. case U8G_DEV_MSG_INIT:
  134. u8g_InitCom(u8g, dev, U8G_SPI_CLK_CYCLE_400NS);
  135. u8g_WriteEscSeqP(u8g, dev, u8g_dev_st7920_128x64_HAL_init_seq);
  136. clear_graphics_DRAM(u8g, dev);
  137. break;
  138. case U8G_DEV_MSG_STOP:
  139. break;
  140. case U8G_DEV_MSG_PAGE_NEXT: {
  141. uint8_t y, i;
  142. uint8_t *ptr;
  143. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  144. u8g_SetAddress(u8g, dev, 0); /* cmd mode */
  145. u8g_SetChipSelect(u8g, dev, 1);
  146. y = pb->p.page_y0;
  147. ptr = (uint8_t *)pb->buf;
  148. for (i = 0; i < 32; i ++) {
  149. u8g_SetAddress(u8g, dev, 0); /* cmd mode */
  150. u8g_WriteByte(u8g, dev, 0x03E ); /* enable extended mode */
  151. if (y < 32) {
  152. u8g_WriteByte(u8g, dev, 0x080 | y ); /* y pos */
  153. u8g_WriteByte(u8g, dev, 0x080 ); /* set x pos to 0*/
  154. }
  155. else {
  156. u8g_WriteByte(u8g, dev, 0x080 | (y-32) ); /* y pos */
  157. u8g_WriteByte(u8g, dev, 0x080 | 8); /* set x pos to 64*/
  158. }
  159. u8g_SetAddress(u8g, dev, 1); /* data mode */
  160. u8g_WriteSequence(u8g, dev, (LCD_PIXEL_WIDTH) / 8, ptr);
  161. ptr += (LCD_PIXEL_WIDTH) / 8;
  162. y++;
  163. }
  164. u8g_SetChipSelect(u8g, dev, 0);
  165. }
  166. break;
  167. }
  168. return u8g_dev_pb32h1_base_fn(u8g, dev, msg, arg);
  169. }
  170. U8G_PB_DEV(u8g_dev_st7920_128x64_HAL_sw_spi, LCD_PIXEL_WIDTH, LCD_PIXEL_HEIGHT, PAGE_HEIGHT, u8g_dev_st7920_128x64_HAL_fn, U8G_COM_ST7920_HAL_SW_SPI);
  171. #define QWIDTH ((LCD_PIXEL_WIDTH) * 4)
  172. uint8_t u8g_dev_st7920_128x64_HAL_4x_buf[QWIDTH] U8G_NOCOMMON ;
  173. u8g_pb_t u8g_dev_st7920_128x64_HAL_4x_pb = { { 32, LCD_PIXEL_HEIGHT, 0, 0, 0 }, LCD_PIXEL_WIDTH, u8g_dev_st7920_128x64_HAL_4x_buf};
  174. u8g_dev_t u8g_dev_st7920_128x64_HAL_4x_sw_spi = { u8g_dev_st7920_128x64_HAL_4x_fn, &u8g_dev_st7920_128x64_HAL_4x_pb, U8G_COM_ST7920_HAL_SW_SPI };
  175. U8G_PB_DEV(u8g_dev_st7920_128x64_HAL_hw_spi, LCD_PIXEL_WIDTH, LCD_PIXEL_HEIGHT, PAGE_HEIGHT, u8g_dev_st7920_128x64_HAL_fn, U8G_COM_ST7920_HAL_HW_SPI);
  176. u8g_dev_t u8g_dev_st7920_128x64_HAL_4x_hw_spi = { u8g_dev_st7920_128x64_HAL_4x_fn, &u8g_dev_st7920_128x64_HAL_4x_pb, U8G_COM_ST7920_HAL_HW_SPI };
  177. #if NONE(__AVR__, ARDUINO_ARCH_STM32, ARDUINO_ARCH_ESP32) || defined(U8G_HAL_LINKS)
  178. // Also use this device for HAL version of rrd class. This results in the same device being used
  179. // for the ST7920 for HAL systems no matter what is selected in ultralcd_impl_DOGM.h.
  180. u8g_dev_t u8g_dev_st7920_128x64_rrd_sw_spi = { u8g_dev_st7920_128x64_HAL_4x_fn, &u8g_dev_st7920_128x64_HAL_4x_pb, U8G_COM_ST7920_HAL_SW_SPI };
  181. #endif
  182. #endif // HAS_GRAPHICAL_LCD