Subversion Repositories MK-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, 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
#ifndef ULCDST7565_H
24
#define ULCDST7565_H
25
 
26
#include <U8glib.h>
27
#include "delay.h"
28
 
29
#define ST7565_CLK_PIN  DOGLCD_SCK
30
#define ST7565_DAT_PIN  DOGLCD_MOSI
31
#define ST7565_CS_PIN   DOGLCD_CS
32
#define ST7565_A0_PIN   DOGLCD_A0
33
 
34
#define LCD_PIXEL_WIDTH 128
35
#define LCD_PIXEL_HEIGHT 64
36
#define PAGE_HEIGHT 8
37
 
38
//set optimization so ARDUINO optimizes this file
39
#pragma GCC optimize (3)
40
 
41
// If you want you can define your own set of delays in Configuration.h
42
//#define ST7565_DELAY_1 DELAY_NS(0)
43
//#define ST7565_DELAY_2 DELAY_NS(0)
44
//#define ST7565_DELAY_3 DELAY_NS(0)
45
 
46
/*
47
#define ST7565_DELAY_1 u8g_10MicroDelay()
48
#define ST7565_DELAY_2 u8g_10MicroDelay()
49
#define ST7565_DELAY_3 u8g_10MicroDelay()
50
*/
51
 
52
#if F_CPU >= 20000000
53
  #define CPU_ST7565_DELAY_1 DELAY_NS(0)
54
  #define CPU_ST7565_DELAY_2 DELAY_NS(0)
55
  #define CPU_ST7565_DELAY_3 DELAY_NS(63)
56
#elif MB(3DRAG) || MB(K8200) || MB(K8400)
57
  #define CPU_ST7565_DELAY_1 DELAY_NS(0)
58
  #define CPU_ST7565_DELAY_2 DELAY_NS(188)
59
  #define CPU_ST7565_DELAY_3 DELAY_NS(0)
60
#elif MB(MINIRAMBO)
61
  #define CPU_ST7565_DELAY_1 DELAY_NS(0)
62
  #define CPU_ST7565_DELAY_2 DELAY_NS(250)
63
  #define CPU_ST7565_DELAY_3 DELAY_NS(0)
64
#elif MB(RAMBO)
65
  #define CPU_ST7565_DELAY_1 DELAY_NS(0)
66
  #define CPU_ST7565_DELAY_2 DELAY_NS(0)
67
  #define CPU_ST7565_DELAY_3 DELAY_NS(0)
68
#elif F_CPU == 16000000
69
  #define CPU_ST7565_DELAY_1 DELAY_NS(0)
70
  #define CPU_ST7565_DELAY_2 DELAY_NS(0)
71
  #define CPU_ST7565_DELAY_3 DELAY_NS(63)
72
#else
73
  #error "No valid condition for delays in 'ultralcd_st7565_u8glib_VIKI.h'"
74
#endif
75
 
76
#ifndef ST7565_DELAY_1
77
  #define ST7565_DELAY_1 CPU_ST7565_DELAY_1
78
#endif
79
#ifndef ST7565_DELAY_2
80
  #define ST7565_DELAY_2 CPU_ST7565_DELAY_2
81
#endif
82
#ifndef ST7565_DELAY_3
83
  #define ST7565_DELAY_3 CPU_ST7565_DELAY_3
84
#endif
85
 
86
// On Viki2 the LCD and the SD card share a single SPI
87
#define HARDWARE_SPI ((DOGLCD_SCK == SCK_PIN) && (DOGLCD_MOSI == MOSI_PIN))
88
 
89
#if HARDWARE_SPI  // using the hardware SPI
90
 
91
  #define ST7565_WRITE_BYTE(a)                 { SPDR = a; while (!TEST(SPSR, SPIF)); U8G_DELAY(); }
92
  #define ST7560_WriteSequence(count, pointer) { uint8_t *ptr = pointer; for (uint8_t i = 0; i <  count; i++) {SPDR = *ptr++; while (!TEST(SPSR, SPIF));} U8G_DELAY(); }
93
 
94
#else // !HARDWARE_SPI
95
 
96
  #define ST7565_SND_BIT \
97
    WRITE(ST7565_CLK_PIN, LOW);        ST7565_DELAY_1; \
98
    WRITE(ST7565_DAT_PIN, val & 0x80); ST7565_DELAY_2; \
99
    WRITE(ST7565_CLK_PIN, HIGH);       ST7565_DELAY_3; \
100
    WRITE(ST7565_CLK_PIN, LOW);\
101
    val <<= 1
102
 
103
  static void ST7565_SWSPI_SND_8BIT(uint8_t val) {
104
    ST7565_SND_BIT; // 1
105
    ST7565_SND_BIT; // 2
106
    ST7565_SND_BIT; // 3
107
    ST7565_SND_BIT; // 4
108
    ST7565_SND_BIT; // 5
109
    ST7565_SND_BIT; // 6
110
    ST7565_SND_BIT; // 7
111
    ST7565_SND_BIT; // 8
112
  }
113
 
114
  #define ST7565_WRITE_BYTE(a)                 { ST7565_SWSPI_SND_8BIT((uint8_t)a); U8G_DELAY(); }
115
  #define ST7560_WriteSequence(count, pointer) { uint8_t *ptr = pointer; for (uint8_t i = 0; i < count; i++) { ST7565_SWSPI_SND_8BIT(*ptr++); } U8G_DELAY(); }
116
 
117
#endif // !HARDWARE_SPI
118
 
119
#if DOGM_SPI_DELAY_US > 0
120
  #define U8G_DELAY() DELAY_US(DOGM_SPI_DELAY_US)
121
#else
122
  #define U8G_DELAY() u8g_10MicroDelay()
123
#endif
124
 
125
#define ST7565_CS()   do{ WRITE(ST7565_CS_PIN, HIGH); U8G_DELAY(); }while(0)
126
#define ST7565_NCS()      WRITE(ST7565_CS_PIN, LOW)
127
#define ST7565_A0()   do{ WRITE(ST7565_A0_PIN, HIGH); U8G_DELAY(); }while(0)
128
#define ST7565_NA0()      WRITE(ST7565_A0_PIN, LOW)
129
 
130
#define ST7565_ADC_REVERSE(N)    ST7565_WRITE_BYTE(0xA0 | ((N) & 0x1))
131
#define ST7565_BIAS_MODE(N)      ST7565_WRITE_BYTE(0xA2 | ((N) & 0x1))
132
#define ST7565_ALL_PIX(N)        ST7565_WRITE_BYTE(0xA4 | ((N) & 0x1))
133
#define ST7565_INVERTED(N)       ST7565_WRITE_BYTE(0xA6 | ((N) & 0x1))
134
#define ST7565_ON(N)             ST7565_WRITE_BYTE(0xAE | ((N) & 0x1))
135
#define ST7565_OUT_MODE(N)       ST7565_WRITE_BYTE(0xC0 | ((N) & 0x1) << 3)
136
#define ST7565_POWER_CONTROL(N)  ST7565_WRITE_BYTE(0x28 | (N))
137
#define ST7565_V0_RATIO(N)       ST7565_WRITE_BYTE(0x10 | ((N) & 0x7)) // Specific to Displaytech 64128N? (ST7565 is 0x20 | N)
138
#define ST7565_CONTRAST(N)   do{ ST7565_WRITE_BYTE(0x81); ST7565_WRITE_BYTE(N); }while(0)
139
 
140
#define ST7565_COLUMN_ADR(N) do{ ST7565_WRITE_BYTE(0x10 | (((N) >> 4) & 0xF)); ST7565_WRITE_BYTE((N) & 0xF); }while(0)
141
#define ST7565_PAGE_ADR(N)       ST7565_WRITE_BYTE(0xB0 | (N))
142
#define ST7565_START_LINE(N)     ST7565_WRITE_BYTE(0x40 | (N))
143
#define ST7565_SLEEP_MODE()      ST7565_WRITE_BYTE(0xAC)
144
#define ST7565_NOOP()            ST7565_WRITE_BYTE(0xE3)
145
 
146
uint8_t u8g_dev_st7565_64128n_2x_VIKI_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
147
  switch (msg) {
148
 
149
    case U8G_DEV_MSG_INIT: {
150
 
151
      OUT_WRITE(ST7565_CS_PIN, LOW);
152
      OUT_WRITE(ST7565_DAT_PIN, LOW);
153
      OUT_WRITE(ST7565_CLK_PIN, LOW);
154
 
155
      #if HARDWARE_SPI
156
        OUT_WRITE(SDSS, 1);   // must be set to an output first or else will never go into master mode
157
        SPCR = 0x50;          // enable SPI in master mode at fast speed
158
        SPSR = 1;             // kick it up to 2x speed mode
159
      #endif
160
 
161
      OUT_WRITE(ST7565_A0_PIN, LOW);
162
 
163
      ST7565_CS();                      // chip select off
164
      ST7565_NA0();                     // instruction mode
165
      ST7565_NCS();                     // chip select
166
 
167
      ST7565_BIAS_MODE(0);              // 0xA2: LCD bias 1/9 (according to Displaytech 64128N datasheet)
168
      ST7565_ADC_REVERSE(0);            // Normal (not flipped) ADC Select (according to Displaytech 64128N datasheet)
169
 
170
      ST7565_OUT_MODE(1);               // common output mode: set scan direction normal operation/SHL Select; 0x0C0 --> SHL = 0; normal; 0x0C8 --> SHL = 1
171
      ST7565_START_LINE(0);             // Display start line for Displaytech 64128N
172
 
173
      ST7565_POWER_CONTROL(0x4);        // power control: turn on Booster
174
      U8G_ESC_DLY(50);                  // delay 50 ms - hangs after a reset if used
175
 
176
      ST7565_POWER_CONTROL(0x6);        // power control: turn on Booster, Voltage Regulator
177
      U8G_ESC_DLY(50);                  // delay 50 ms - hangs after a reset if used
178
 
179
      ST7565_POWER_CONTROL(0x7);        // power control: turn on Booster, Voltage Regulator, Voltage Follower
180
      U8G_ESC_DLY(50);                  // delay 50 ms - hangs after a reset if used
181
 
182
      ST7565_V0_RATIO(0);               // Set V0 Voltage Resistor ratio. Setting for controlling brightness of Displaytech 64128N
183
 
184
      ST7565_INVERTED(0);               // display normal, bit val 0: LCD pixel off.
185
 
186
      ST7565_CONTRAST(0x1E);            // Contrast value. Setting for controlling contrast of Displaytech 64128N
187
 
188
      ST7565_ON(1);                     // display on
189
 
190
      U8G_ESC_DLY(100);                 // delay 100 ms
191
      ST7565_ALL_PIX(1);                // display all points; ST7565
192
      U8G_ESC_DLY(100);                 // delay 100 ms
193
      U8G_ESC_DLY(100);                 // delay 100 ms
194
      ST7565_ALL_PIX(0);                // normal display
195
      ST7565_CS();                      // chip select off
196
    }                                   // end of sequence
197
    break;
198
 
199
    case U8G_DEV_MSG_STOP: break;
200
 
201
    case U8G_DEV_MSG_PAGE_NEXT: {
202
      u8g_pb_t *pb = (u8g_pb_t*)(dev->dev_mem);
203
      ST7565_CS();                      // chip select off
204
      ST7565_NA0();                     // instruction mode
205
      ST7565_NCS();                     // chip select
206
      ST7565_COLUMN_ADR(0x00);          // high 4 bits to 0, low 4 bits to 0. Changed for DisplayTech 64128N
207
                                        // end of sequence
208
      ST7565_PAGE_ADR(2 * pb->p.page);  // select current page (ST7565R)
209
      ST7565_A0();                      // data mode
210
      ST7560_WriteSequence((uint8_t)pb->width, (uint8_t*)pb->buf);
211
      ST7565_CS();                      // chip select off
212
      ST7565_NA0();                     // instruction mode
213
      ST7565_NCS();                     // chip select
214
      ST7565_COLUMN_ADR(0x00);          // high 4 bits to 0, low 4 bits to 0
215
                                        // end of sequence
216
      ST7565_PAGE_ADR(2 * pb->p.page + 1); // select current page (ST7565R)
217
      ST7565_A0();                      // data mode
218
      ST7560_WriteSequence((uint8_t)pb->width, (uint8_t*)(pb->buf) + pb->width);
219
      ST7565_CS();                      // chip select off
220
    }
221
    break;
222
 
223
    case U8G_DEV_MSG_CONTRAST:
224
      ST7565_NCS();
225
      ST7565_NA0();                     // instruction mode
226
      ST7565_CONTRAST((*(uint8_t*)arg) >> 2);
227
      ST7565_CS();                      // chip select off
228
      return 1;
229
 
230
    case U8G_DEV_MSG_SLEEP_ON:
231
      ST7565_NA0();                     // instruction mode
232
      ST7565_NCS();                     // chip select
233
      ST7565_SLEEP_MODE();              // static indicator off
234
      //ST7565_WRITE_BYTE(0x00);        // indicator register set (not sure if this is required)
235
      ST7565_ON(0);                     // display off
236
      ST7565_ALL_PIX(1);                // all points on
237
      ST7565_CS();                      // chip select off
238
      return 1;
239
 
240
    case U8G_DEV_MSG_SLEEP_OFF:
241
      ST7565_NA0();                     // instruction mode
242
      ST7565_NCS();                     // chip select
243
      ST7565_ALL_PIX(0);                // all points off
244
      ST7565_ON(1);                     // display on
245
      U8G_ESC_DLY(50);                  // delay 50 ms
246
      ST7565_CS();                      // chip select off
247
      return 1;
248
  }
249
  return u8g_dev_pb16v1_base_fn(u8g, dev, msg, arg);
250
}
251
 
252
uint8_t u8g_dev_st7565_64128n_2x_VIKI_buf[LCD_PIXEL_WIDTH * 2] U8G_NOCOMMON;
253
u8g_pb_t u8g_dev_st7565_64128n_2x_VIKI_pb = {{16, LCD_PIXEL_HEIGHT, 0, 0, 0}, LCD_PIXEL_WIDTH, u8g_dev_st7565_64128n_2x_VIKI_buf};
254
u8g_dev_t u8g_dev_st7565_64128n_2x_VIKI_sw_spi = {u8g_dev_st7565_64128n_2x_VIKI_fn, &u8g_dev_st7565_64128n_2x_VIKI_pb, &u8g_com_null_fn};
255
 
256
class U8GLIB_ST7565_64128n_2x_VIKI : public U8GLIB {
257
  public:
258
  U8GLIB_ST7565_64128n_2x_VIKI(uint8_t dummy)
259
    : U8GLIB(&u8g_dev_st7565_64128n_2x_VIKI_sw_spi)
260
    {  }
261
  U8GLIB_ST7565_64128n_2x_VIKI(uint8_t sck, uint8_t mosi, uint8_t cs, uint8_t a0, uint8_t reset = U8G_PIN_NONE)
262
    : U8GLIB(&u8g_dev_st7565_64128n_2x_VIKI_sw_spi)
263
    {  }
264
};
265
 
266
#pragma GCC reset_options
267
 
268
#endif // ULCDST7565_H