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

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