Subversion Repositories Tronxy-X3A-Marlin

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ron 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
 
23
#ifndef ULCDST7920_H
24
#define ULCDST7920_H
25
 
26
#include <U8glib.h>
27
#include "delay.h"
28
 
29
#define ST7920_CLK_PIN  LCD_PINS_D4
30
#define ST7920_DAT_PIN  LCD_PINS_ENABLE
31
#define ST7920_CS_PIN   LCD_PINS_RS
32
 
33
//#define PAGE_HEIGHT 8   //128 byte framebuffer
34
#define PAGE_HEIGHT 16  //256 byte framebuffer
35
//#define PAGE_HEIGHT 32  //512 byte framebuffer
36
 
37
#define LCD_PIXEL_WIDTH 128
38
#define LCD_PIXEL_HEIGHT 64
39
 
40
//set optimization so ARDUINO optimizes this file
41
#pragma GCC optimize (3)
42
 
43
// If you want you can define your own set of delays in Configuration.h
44
//#define ST7920_DELAY_1 DELAY_NS(0)
45
//#define ST7920_DELAY_2 DELAY_NS(0)
46
//#define ST7920_DELAY_3 DELAY_NS(0)
47
 
48
#if F_CPU >= 20000000
49
  #define CPU_ST7920_DELAY_1 DELAY_NS(0)
50
  #define CPU_ST7920_DELAY_2 DELAY_NS(0)
51
  #define CPU_ST7920_DELAY_3 DELAY_NS(50)
52
#elif MB(3DRAG) || MB(K8200) || MB(K8400) || MB(SILVER_GATE)
53
  #define CPU_ST7920_DELAY_1 DELAY_NS(0)
54
  #define CPU_ST7920_DELAY_2 DELAY_NS(188)
55
  #define CPU_ST7920_DELAY_3 DELAY_NS(0)
56
#elif MB(MINIRAMBO) || MB(EINSY_RAMBO) || MB(EINSY_RETRO)
57
  #define CPU_ST7920_DELAY_1 DELAY_NS(0)
58
  #define CPU_ST7920_DELAY_2 DELAY_NS(250)
59
  #define CPU_ST7920_DELAY_3 DELAY_NS(0)
60
#elif MB(RAMBO)
61
  #define CPU_ST7920_DELAY_1 DELAY_NS(0)
62
  #define CPU_ST7920_DELAY_2 DELAY_NS(0)
63
  #define CPU_ST7920_DELAY_3 DELAY_NS(0)
64
#elif MB(BQ_ZUM_MEGA_3D)
65
  #define CPU_ST7920_DELAY_1 DELAY_NS(0)
66
  #define CPU_ST7920_DELAY_2 DELAY_NS(0)
67
  #define CPU_ST7920_DELAY_3 DELAY_NS(189)
68
#elif F_CPU == 16000000
69
  #define CPU_ST7920_DELAY_1 DELAY_NS(0)
70
  #define CPU_ST7920_DELAY_2 DELAY_NS(0)
71
  #define CPU_ST7920_DELAY_3 DELAY_NS(63)
72
#else
73
  #error "No valid condition for delays in 'ultralcd_st7920_u8glib_rrd.h'"
74
#endif
75
 
76
#ifndef ST7920_DELAY_1
77
  #define ST7920_DELAY_1 CPU_ST7920_DELAY_1
78
#endif
79
#ifndef ST7920_DELAY_2
80
  #define ST7920_DELAY_2 CPU_ST7920_DELAY_2
81
#endif
82
#ifndef ST7920_DELAY_3
83
  #define ST7920_DELAY_3 CPU_ST7920_DELAY_3
84
#endif
85
 
86
#define ST7920_SND_BIT \
87
  WRITE(ST7920_CLK_PIN, LOW);        ST7920_DELAY_1; \
88
  WRITE(ST7920_DAT_PIN, val & 0x80); ST7920_DELAY_2; \
89
  WRITE(ST7920_CLK_PIN, HIGH);       ST7920_DELAY_3; \
90
  val <<= 1
91
 
92
static void ST7920_SWSPI_SND_8BIT(uint8_t val) {
93
  ST7920_SND_BIT; // 1
94
  ST7920_SND_BIT; // 2
95
  ST7920_SND_BIT; // 3
96
  ST7920_SND_BIT; // 4
97
  ST7920_SND_BIT; // 5
98
  ST7920_SND_BIT; // 6
99
  ST7920_SND_BIT; // 7
100
  ST7920_SND_BIT; // 8
101
}
102
 
103
#if DOGM_SPI_DELAY_US > 0
104
  #define U8G_DELAY() DELAY_US(DOGM_SPI_DELAY_US)
105
#else
106
  #define U8G_DELAY() u8g_10MicroDelay()
107
#endif
108
 
109
#define ST7920_CS()              { WRITE(ST7920_CS_PIN,1); U8G_DELAY(); }
110
#define ST7920_NCS()             { WRITE(ST7920_CS_PIN,0); }
111
#define ST7920_SET_CMD()         { ST7920_SWSPI_SND_8BIT(0xF8); U8G_DELAY(); }
112
#define ST7920_SET_DAT()         { ST7920_SWSPI_SND_8BIT(0xFA); U8G_DELAY(); }
113
#define ST7920_WRITE_BYTE(a)     { ST7920_SWSPI_SND_8BIT((uint8_t)((a)&0xF0u)); ST7920_SWSPI_SND_8BIT((uint8_t)((a)<<4u)); U8G_DELAY(); }
114
#define ST7920_WRITE_BYTES(p,l)  { for (uint8_t i = l + 1; --i;) { ST7920_SWSPI_SND_8BIT(*p&0xF0); ST7920_SWSPI_SND_8BIT(*p<<4); p++; } U8G_DELAY(); }
115
 
116
uint8_t u8g_dev_rrd_st7920_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
117
  uint8_t i, y;
118
  switch (msg) {
119
    case U8G_DEV_MSG_INIT: {
120
      OUT_WRITE(ST7920_CS_PIN, LOW);
121
      OUT_WRITE(ST7920_DAT_PIN, LOW);
122
      OUT_WRITE(ST7920_CLK_PIN, HIGH);
123
 
124
      ST7920_CS();
125
      u8g_Delay(120);                 //initial delay for boot up
126
      ST7920_SET_CMD();
127
      ST7920_WRITE_BYTE(0x20);       //non-extended mode
128
      ST7920_WRITE_BYTE(0x08);       //display off, cursor+blink off
129
      ST7920_WRITE_BYTE(0x01);       //clear DDRAM ram
130
      u8g_Delay(15);                    //delay for DDRAM clear
131
      ST7920_WRITE_BYTE(0x24);       //extended mode
132
      ST7920_WRITE_BYTE(0x26);       //extended mode + GDRAM active
133
      for (y = 0; y < (LCD_PIXEL_HEIGHT) / 2; y++) { //clear GDRAM
134
        ST7920_WRITE_BYTE(0x80 | y); //set y
135
        ST7920_WRITE_BYTE(0x80);     //set x = 0
136
        ST7920_SET_DAT();
137
        for (i = 0; i < 2 * (LCD_PIXEL_WIDTH) / 8; i++) //2x width clears both segments
138
          ST7920_WRITE_BYTE(0);
139
        ST7920_SET_CMD();
140
      }
141
      ST7920_WRITE_BYTE(0x0C); //display on, cursor+blink off
142
      ST7920_NCS();
143
    }
144
    break;
145
 
146
    case U8G_DEV_MSG_STOP: break;
147
 
148
    case U8G_DEV_MSG_PAGE_NEXT: {
149
      uint8_t* ptr;
150
      u8g_pb_t* pb = (u8g_pb_t*)(dev->dev_mem);
151
      y = pb->p.page_y0;
152
      ptr = (uint8_t*)pb->buf;
153
 
154
      ST7920_CS();
155
      for (i = 0; i < PAGE_HEIGHT; i ++) {
156
        ST7920_SET_CMD();
157
        if (y < 32) {
158
          ST7920_WRITE_BYTE(0x80 | y);       //y
159
          ST7920_WRITE_BYTE(0x80);           //x=0
160
        }
161
        else {
162
          ST7920_WRITE_BYTE(0x80 | (y - 32)); //y
163
          ST7920_WRITE_BYTE(0x80 | 8);       //x=64
164
        }
165
        ST7920_SET_DAT();
166
        ST7920_WRITE_BYTES(ptr, (LCD_PIXEL_WIDTH) / 8); //ptr is incremented inside of macro
167
        y++;
168
      }
169
      ST7920_NCS();
170
    }
171
    break;
172
  }
173
  #if PAGE_HEIGHT == 8
174
    return u8g_dev_pb8h1_base_fn(u8g, dev, msg, arg);
175
  #elif PAGE_HEIGHT == 16
176
    return u8g_dev_pb16h1_base_fn(u8g, dev, msg, arg);
177
  #else
178
    return u8g_dev_pb32h1_base_fn(u8g, dev, msg, arg);
179
  #endif
180
}
181
 
182
uint8_t   u8g_dev_st7920_128x64_rrd_buf[(LCD_PIXEL_WIDTH) * (PAGE_HEIGHT) / 8] U8G_NOCOMMON;
183
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};
184
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};
185
 
186
class U8GLIB_ST7920_128X64_RRD : public U8GLIB {
187
 public:
188
  U8GLIB_ST7920_128X64_RRD(uint8_t dummy) : U8GLIB(&u8g_dev_st7920_128x64_rrd_sw_spi) { UNUSED(dummy); }
189
};
190
 
191
#if ENABLED(LIGHTWEIGHT_UI)
192
  // We have to include the code for the lightweight UI here
193
  // as it relies on macros that are only defined in this file.
194
  #include "status_screen_lite_ST7920_spi.h"
195
#endif
196
 
197
#pragma GCC reset_options
198
 
199
#endif // ULCDST7920_H