|
@@ -1,276 +0,0 @@
|
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
|
|
-/*
|
24
|
|
- * PLEASE NOTE >>>
|
25
|
|
- * We need our custom implementation for Software SPI, as the default implementation
|
26
|
|
- * of U8GLIB, when running in an ARM based board, is too fast and the display will not
|
27
|
|
- * recognize commands and/or data at such speeds. This implementation autoderives the
|
28
|
|
- * required delays to get the maximum possible performance by using the F_CPU macro that
|
29
|
|
- * specifies the CPU speed. According to the ST7920 datasheet, the maximum SCLK is 1MHz.
|
30
|
|
- */
|
31
|
|
-
|
32
|
|
-#ifndef ULCDST7920_SWSPI_H
|
33
|
|
-#define ULCDST7920_SWSPI_H
|
34
|
|
-
|
35
|
|
-#include "../../inc/MarlinConfig.h"
|
36
|
|
-
|
37
|
|
-#if ENABLED(U8GLIB_ST7920)
|
38
|
|
-
|
39
|
|
-#include <U8glib.h>
|
40
|
|
-#include "HAL_LCD_com_defines.h"
|
41
|
|
-
|
42
|
|
-#define ST7920_CLK_PIN LCD_PINS_D4
|
43
|
|
-#define ST7920_DAT_PIN LCD_PINS_ENABLE
|
44
|
|
-#define ST7920_CS_PIN LCD_PINS_RS
|
45
|
|
-
|
46
|
|
-//#define PAGE_HEIGHT 8 //128 byte framebuffer
|
47
|
|
-#define PAGE_HEIGHT 16 //256 byte framebuffer
|
48
|
|
-//#define PAGE_HEIGHT 32 //512 byte framebuffer
|
49
|
|
-
|
50
|
|
-#define LCD_PIXEL_WIDTH 128
|
51
|
|
-#define LCD_PIXEL_HEIGHT 64
|
52
|
|
-
|
53
|
|
-//set optimization so ARDUINO optimizes this file
|
54
|
|
-#pragma GCC optimize (3)
|
55
|
|
-
|
56
|
|
-/* ---------------- Delay Cycles routine -------------- */
|
57
|
|
-
|
58
|
|
-#ifdef __arm__
|
59
|
|
-/* https://blueprints.launchpad.net/gcc-arm-embedded/+spec/delay-cycles */
|
60
|
|
-
|
61
|
|
-#define nop() __asm__ __volatile__("nop;\n\t":::)
|
62
|
|
-
|
63
|
|
-FORCE_INLINE static void __delay_4cycles(uint32_t cy) { // +1 cycle
|
64
|
|
- #if ARCH_PIPELINE_RELOAD_CYCLES<2
|
65
|
|
- #define EXTRA_NOP_CYCLES "nop"
|
66
|
|
- #else
|
67
|
|
- #define EXTRA_NOP_CYCLES ""
|
68
|
|
- #endif
|
69
|
|
-
|
70
|
|
- __asm__ __volatile__(
|
71
|
|
- ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax
|
72
|
|
-
|
73
|
|
- "loop%=:" "\n\t"
|
74
|
|
- " subs %[cnt],#1" "\n\t"
|
75
|
|
- EXTRA_NOP_CYCLES "\n\t"
|
76
|
|
- " bne loop%=" "\n\t"
|
77
|
|
- : [cnt]"+r"(cy) // output: +r means input+output
|
78
|
|
- : // input:
|
79
|
|
- : "cc" // clobbers:
|
80
|
|
- );
|
81
|
|
-}
|
82
|
|
-
|
83
|
|
-FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
|
84
|
|
-
|
85
|
|
- if (__builtin_constant_p(x)) {
|
86
|
|
-
|
87
|
|
- #define MAXNOPS 4
|
88
|
|
-
|
89
|
|
- if (x <= (MAXNOPS)) {
|
90
|
|
- switch(x) { case 4: nop(); case 3: nop(); case 2: nop(); case 1: nop(); }
|
91
|
|
- }
|
92
|
|
- else { // because of +1 cycle inside delay_4cycles
|
93
|
|
- const uint32_t rem = (x - 1) % (MAXNOPS);
|
94
|
|
- switch(rem) { case 3: nop(); case 2: nop(); case 1: nop(); }
|
95
|
|
- if ((x = (x - 1) / (MAXNOPS)))
|
96
|
|
- __delay_4cycles(x); // if need more then 4 nop loop is more optimal
|
97
|
|
- }
|
98
|
|
- }
|
99
|
|
- else
|
100
|
|
- __delay_4cycles(x / 4);
|
101
|
|
- }
|
102
|
|
-
|
103
|
|
-#ifdef __TEST_DELAY
|
104
|
|
-
|
105
|
|
- void calibrateTimer() {
|
106
|
|
-
|
107
|
|
- // Use DWT to calibrate cycles
|
108
|
|
- uint32_t count = 0;
|
109
|
|
-
|
110
|
|
- // addresses of registers
|
111
|
|
- volatile uint32_t *DWT_CONTROL = (uint32_t *)0xE0001000,
|
112
|
|
- *DWT_CYCCNT = (uint32_t *)0xE0001004,
|
113
|
|
- *DEMCR = (uint32_t *)0xE000EDFC;
|
114
|
|
-
|
115
|
|
- cli();
|
116
|
|
-
|
117
|
|
- // enable the use DWT
|
118
|
|
- *DEMCR = *DEMCR | 0x01000000;
|
119
|
|
-
|
120
|
|
- // Reset cycle counter
|
121
|
|
- *DWT_CYCCNT = 0;
|
122
|
|
-
|
123
|
|
- // enable cycle counter
|
124
|
|
- *DWT_CONTROL = *DWT_CONTROL | 1;
|
125
|
|
-
|
126
|
|
- // Perform a delay of 10000 cycles
|
127
|
|
- DELAY_CYCLES(10000U);
|
128
|
|
-
|
129
|
|
- // number of cycles stored in count variable
|
130
|
|
- count = *DWT_CYCCNT;
|
131
|
|
-
|
132
|
|
- sei();
|
133
|
|
-
|
134
|
|
- SERIAL_ECHO_START();
|
135
|
|
- SERIAL_ECHOLNPAIR("calibrated Cycles: ", (int)count);
|
136
|
|
- }
|
137
|
|
-
|
138
|
|
-#endif // __TEST_DELAY
|
139
|
|
-
|
140
|
|
-#elif defined(__AVR__)
|
141
|
|
- #define DELAY_CYCLES(cycles) __builtin_avr_delay_cycles(cycles)
|
142
|
|
-#else
|
143
|
|
- #error "DELAY_CYCLES not implemented for this architecture."
|
144
|
|
-#endif
|
145
|
|
-
|
146
|
|
-/* ---------------- Delay in nanoseconds and in microseconds */
|
147
|
|
-
|
148
|
|
-#define DELAY_NS(x) DELAY_CYCLES( (x) * (F_CPU/1000000) / 1000)
|
149
|
|
-#define DELAY_US(x) DELAY_CYCLES( (x) * (F_CPU/1000000))
|
150
|
|
-
|
151
|
|
-/* ---------------- ST7920 commands ------------------------ */
|
152
|
|
-
|
153
|
|
-#ifdef __arm__
|
154
|
|
-
|
155
|
|
- /* ARM: Plain implementation is more than enough */
|
156
|
|
- static void ST7920_SWSPI_SND_8BIT(uint8_t val) {
|
157
|
|
- uint8_t n = 8;
|
158
|
|
- do {
|
159
|
|
- WRITE(ST7920_CLK_PIN, LOW);
|
160
|
|
- WRITE(ST7920_DAT_PIN, val & 0x80);
|
161
|
|
- DELAY_NS(700); /* RE-ARM requires 700ns to be stable, RAMPS4DUE works with 500ns */
|
162
|
|
- WRITE(ST7920_CLK_PIN, HIGH);
|
163
|
|
- DELAY_NS(700); /* RE-ARM requires 700ns to be stable, RAMPS4DUE works with 500ns */
|
164
|
|
- val <<= 1;
|
165
|
|
- } while (--n);
|
166
|
|
- }
|
167
|
|
-
|
168
|
|
-#else // !ARM
|
169
|
|
-
|
170
|
|
- /* AVR: Unrolling loop makes sense */
|
171
|
|
- #define ST7920_SND_BIT(nr) \
|
172
|
|
- WRITE(ST7920_CLK_PIN, LOW); \
|
173
|
|
- WRITE(ST7920_DAT_PIN, TEST(val, nr)); \
|
174
|
|
- DELAY_NS(700); \
|
175
|
|
- WRITE(ST7920_CLK_PIN, HIGH); \
|
176
|
|
- DELAY_NS(700);
|
177
|
|
-
|
178
|
|
- static void ST7920_SWSPI_SND_8BIT(const uint8_t val) {
|
179
|
|
- ST7920_SND_BIT(7); // MSBit
|
180
|
|
- ST7920_SND_BIT(6); //
|
181
|
|
- ST7920_SND_BIT(5); //
|
182
|
|
- ST7920_SND_BIT(4); //
|
183
|
|
- ST7920_SND_BIT(3); //
|
184
|
|
- ST7920_SND_BIT(2); //
|
185
|
|
- ST7920_SND_BIT(1); //
|
186
|
|
- ST7920_SND_BIT(0); // LSBit
|
187
|
|
- }
|
188
|
|
-
|
189
|
|
-#endif // !ARM
|
190
|
|
-
|
191
|
|
-#define ST7920_CS() { WRITE(ST7920_CS_PIN,1); DELAY_NS(200); }
|
192
|
|
-#define ST7920_NCS() { WRITE(ST7920_CS_PIN,0); }
|
193
|
|
-#define ST7920_SET_CMD() { ST7920_SWSPI_SND_8BIT(0xF8); DELAY_US(3); }
|
194
|
|
-#define ST7920_SET_DAT() { ST7920_SWSPI_SND_8BIT(0xFA); DELAY_US(3); }
|
195
|
|
-#define ST7920_WRITE_BYTE(a) { ST7920_SWSPI_SND_8BIT((uint8_t)((a)&0xF0u)); ST7920_SWSPI_SND_8BIT((uint8_t)((a)<<4u)); DELAY_US(3); }
|
196
|
|
-#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++; } DELAY_US(3); }
|
197
|
|
-
|
198
|
|
-
|
199
|
|
-uint8_t u8g_dev_st7920_custom_sw_spi_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
|
200
|
|
-
|
201
|
|
- uint8_t i, y;
|
202
|
|
- switch (msg) {
|
203
|
|
- case U8G_DEV_MSG_INIT: {
|
204
|
|
-
|
205
|
|
- /* Set to output and write */
|
206
|
|
- OUT_WRITE(ST7920_CS_PIN, LOW);
|
207
|
|
- OUT_WRITE(ST7920_DAT_PIN, LOW);
|
208
|
|
- OUT_WRITE(ST7920_CLK_PIN, HIGH);
|
209
|
|
-
|
210
|
|
- ST7920_CS();
|
211
|
|
- u8g_Delay(120); //initial delay for boot up
|
212
|
|
-
|
213
|
|
- ST7920_SET_CMD();
|
214
|
|
- ST7920_WRITE_BYTE(0x08); //display off, cursor+blink off
|
215
|
|
- ST7920_WRITE_BYTE(0x01); //clear CGRAM ram
|
216
|
|
- u8g_Delay(15); //delay for CGRAM clear
|
217
|
|
- ST7920_WRITE_BYTE(0x3E); //extended mode + GDRAM active
|
218
|
|
- for (y = 0; y < (LCD_PIXEL_HEIGHT) / 2; y++) { //clear GDRAM
|
219
|
|
- ST7920_WRITE_BYTE(0x80 | y); //set y
|
220
|
|
- ST7920_WRITE_BYTE(0x80); //set x = 0
|
221
|
|
- ST7920_SET_DAT();
|
222
|
|
- for (i = 0; i < 2 * (LCD_PIXEL_WIDTH) / 8; i++) //2x width clears both segments
|
223
|
|
- ST7920_WRITE_BYTE(0);
|
224
|
|
- ST7920_SET_CMD();
|
225
|
|
- }
|
226
|
|
-
|
227
|
|
- ST7920_WRITE_BYTE(0x0C); //display on, cursor+blink off
|
228
|
|
- ST7920_NCS();
|
229
|
|
- }
|
230
|
|
- break;
|
231
|
|
-
|
232
|
|
- case U8G_DEV_MSG_STOP:
|
233
|
|
- break;
|
234
|
|
-
|
235
|
|
- case U8G_DEV_MSG_PAGE_NEXT: {
|
236
|
|
- u8g_pb_t* pb = (u8g_pb_t*)(dev->dev_mem);
|
237
|
|
- y = pb->p.page_y0;
|
238
|
|
- uint8_t* ptr = (uint8_t*)pb->buf;
|
239
|
|
-
|
240
|
|
- ST7920_CS();
|
241
|
|
- for (i = 0; i < PAGE_HEIGHT; i ++) {
|
242
|
|
- ST7920_SET_CMD();
|
243
|
|
- if (y < 32) {
|
244
|
|
- ST7920_WRITE_BYTE(0x80 | y); //y
|
245
|
|
- ST7920_WRITE_BYTE(0x80); //x=0
|
246
|
|
- }
|
247
|
|
- else {
|
248
|
|
- ST7920_WRITE_BYTE(0x80 | (y - 32)); //y
|
249
|
|
- ST7920_WRITE_BYTE(0x80 | 8); //x=64
|
250
|
|
- }
|
251
|
|
- ST7920_SET_DAT();
|
252
|
|
- ST7920_WRITE_BYTES(ptr, (LCD_PIXEL_WIDTH) / 8); //ptr is incremented inside of macro
|
253
|
|
- y++;
|
254
|
|
- }
|
255
|
|
-
|
256
|
|
- ST7920_NCS();
|
257
|
|
- }
|
258
|
|
- break;
|
259
|
|
- }
|
260
|
|
- #if PAGE_HEIGHT == 8
|
261
|
|
- return u8g_dev_pb8h1_base_fn(u8g, dev, msg, arg);
|
262
|
|
- #elif PAGE_HEIGHT == 16
|
263
|
|
- return u8g_dev_pb16h1_base_fn(u8g, dev, msg, arg);
|
264
|
|
- #else
|
265
|
|
- return u8g_dev_pb32h1_base_fn(u8g, dev, msg, arg);
|
266
|
|
- #endif
|
267
|
|
-}
|
268
|
|
-
|
269
|
|
-static uint8_t u8g_dev_st7920_128x64_custom_sw_spi_buf[(LCD_PIXEL_WIDTH) * (PAGE_HEIGHT) / 8] U8G_NOCOMMON;
|
270
|
|
-static u8g_pb_t u8g_dev_st7920_128x64_custom_sw_spi_pb = {{PAGE_HEIGHT, LCD_PIXEL_HEIGHT, 0, 0, 0}, LCD_PIXEL_WIDTH, u8g_dev_st7920_128x64_custom_sw_spi_buf};
|
271
|
|
-u8g_dev_t u8g_dev_st7920_128x64_custom_sw_spi = {u8g_dev_st7920_custom_sw_spi_128x64_fn, &u8g_dev_st7920_128x64_custom_sw_spi_pb, &u8g_com_null_fn};
|
272
|
|
-
|
273
|
|
-#pragma GCC reset_options
|
274
|
|
-
|
275
|
|
-#endif // U8GLIB_ST7920
|
276
|
|
-#endif // ULCDST7920_SWSPI_H
|