1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
#include <stdlib.h>
#include <stdarg.h>
#include "eve.h"
#define EVE_MEM_WRITE 0x800000
#define EVE_SPI_FLAGS(b) ( (b) ? (EVE_SPI_FLAG_TX | EVE_SPI_FLAG_BSWAP) : EVE_SPI_FLAG_BSWAP )
static char cmd_burst;
static uint16_t cmd_offset;
static char dl_burst;
static uint32_t dl_addr;
static uint8_t power_state;
static int lcd_absent = 0;
void eve_command(uint8_t command, uint8_t parameter) {
eve_spi_cs_set();
eve_spi_xchg24(((uint32_t)command << 16) | ((uint32_t)parameter << 8), 0);
eve_spi_cs_clear();
}
uint8_t eve_read8(uint32_t addr) {
uint8_t r;
eve_spi_cs_set();
eve_spi_xchg32(addr << 8, 0);
r = eve_spi_xchg8(0, EVE_SPI_FLAG_BSWAP);
eve_spi_cs_clear();
return r;
}
uint16_t eve_read16(uint32_t addr) {
uint16_t r;
eve_spi_cs_set();
eve_spi_xchg32(addr << 8, 0);
r = eve_spi_xchg16(0, EVE_SPI_FLAG_BSWAP);
eve_spi_cs_clear();
return r;
}
uint32_t eve_read32(uint32_t addr) {
uint32_t r;
eve_spi_cs_set();
eve_spi_xchg32(addr << 8, 0);
r = eve_spi_xchg32(0, EVE_SPI_FLAG_BSWAP);
eve_spi_cs_clear();
return r;
}
void eve_write8(uint32_t addr, uint8_t data) {
eve_spi_cs_set();
eve_spi_xchg24(addr | EVE_MEM_WRITE, 0);
eve_spi_xchg8(data, EVE_SPI_FLAG_BSWAP);
eve_spi_cs_clear();
}
void eve_write16(uint32_t addr, uint16_t data) {
eve_spi_cs_set();
eve_spi_xchg24(addr | EVE_MEM_WRITE, 0);
eve_spi_xchg16(data, EVE_SPI_FLAG_BSWAP);
eve_spi_cs_clear();
}
void eve_write32(uint32_t addr, uint32_t data) {
eve_spi_cs_set();
eve_spi_xchg24(addr | EVE_MEM_WRITE, 0);
eve_spi_xchg32(data, EVE_SPI_FLAG_BSWAP);
eve_spi_cs_clear();
}
void eve_readb(uint32_t addr, uint8_t *buf, size_t size) {
int i;
eve_spi_cs_set();
eve_spi_xchg32(addr << 8, 0);
for (i=0; i<size; i++) {
buf[i] = eve_spi_xchg8(0, 0);
}
eve_spi_cs_clear();
}
void eve_writeb(uint32_t addr, uint8_t *buf, size_t size) {
int i;
eve_spi_cs_set();
eve_spi_xchg24(addr | EVE_MEM_WRITE, 0);
for (i=0; i<size; i++) {
eve_spi_xchg8(buf[i], 0);
}
eve_spi_cs_clear();
}
static void dl_inc(uint32_t i) {
dl_addr += i;
}
void eve_dl_start(uint32_t addr, char burst) {
dl_addr = addr;
dl_burst = burst;
if (burst) {
eve_spi_lock();
eve_spi_cs_set();
eve_spi_xchg24(addr | EVE_MEM_WRITE, EVE_SPI_FLAG_TX);
}
}
void eve_dl_write(uint32_t dl) {
if (dl_burst) {
eve_spi_xchg32(dl, EVE_SPI_FLAGS(dl_burst));
} else {
eve_write32(dl_addr, dl);
}
dl_inc(4);
}
void eve_dl_end(void) {
if (dl_burst) {
eve_spi_flush();
eve_spi_cs_clear();
eve_spi_unlock();
dl_burst = 0;
}
}
void eve_dl_swap(void) {
eve_write8(REG_DLSWAP, EVE_DLSWAP_FRAME);
}
uint32_t eve_dl_get_addr(void) {
return dl_addr;
}
static void cmd_inc(uint16_t i) {
cmd_offset += i;
cmd_offset &= 0x0fff;
}
static void cmd_begin(uint32_t command, uint8_t flags) {
if (!cmd_burst) {
uint32_t addr = EVE_RAM_CMD + cmd_offset;
eve_spi_cs_set();
eve_spi_xchg24(addr | EVE_MEM_WRITE, 0);
}
eve_spi_xchg32(command, flags);
cmd_inc(4);
}
static void cmd_end(void) {
if (!cmd_burst) eve_spi_cs_clear();
}
static void cmd_string(const char *str, uint8_t flags) {
int i = 0;
while (str[i] != 0) {
eve_spi_xchg8(str[i], flags);
i++;
}
eve_spi_xchg8(0, flags);
i++;
cmd_inc(i);
}
static void cmd_buffer(const char *buf, int size, uint8_t flags) {
int i = 0;
for (i=0; i<size; i++) {
eve_spi_xchg8(buf[i], flags);
}
cmd_inc(size);
}
static void cmd_padding(uint8_t flags) {
int i = cmd_offset & 3; /* equivalent to cmd_offset % 4 */
if (i) {
i = 4 - i; /* 3, 2 or 1 */
cmd_inc(i);
while (i > 0) {
eve_spi_xchg8(0, flags);
i--;
}
}
}
void eve_cmd(uint32_t cmd, const char *fmt, ...) {
uint8_t flags = EVE_SPI_FLAGS(cmd_burst);
va_list argv;
uint16_t *p;
int i = 0;
int cont = 0;
va_start(argv, fmt);
cmd_begin(cmd, flags);
while (fmt[i]) {
switch (fmt[i]) {
case 'b':
eve_spi_xchg8(va_arg(argv, int), flags);
cmd_inc(1);
break;
case 'h':
eve_spi_xchg16(va_arg(argv, int), flags);
cmd_inc(2);
break;
case 'w':
eve_spi_xchg32(va_arg(argv, int), flags);
cmd_inc(4);
break;
case '&':
p = va_arg(argv, uint16_t *);
*p = cmd_offset;
eve_spi_xchg32(0, flags);
cmd_inc(4);
break;
case 's':
cmd_string(va_arg(argv, const char *), flags);
break;
case 'p':
cmd_buffer(va_arg(argv, const char *), va_arg(argv, int), flags);
break;
case '0':
cmd_padding(flags);
break;
case '+':
cont = 1;
break;
}
if (cont) break;
i++;
}
va_end(argv);
if (!cont) eve_cmd_end();
}
void eve_cmd_write(uint8_t *buffer, size_t size) {
cmd_buffer(buffer, size, EVE_SPI_FLAGS(cmd_burst));
}
void eve_cmd_end(void) {
cmd_padding(EVE_SPI_FLAGS(cmd_burst));
cmd_end();
}
uint32_t eve_cmd_result(uint16_t offset) {
return eve_read32(EVE_RAM_CMD + offset);
}
void eve_cmd_dl(uint32_t dl) {
cmd_begin(dl, EVE_SPI_FLAGS(cmd_burst));
cmd_end();
}
int eve_cmd_done(void) {
uint16_t r = eve_read16(REG_CMD_READ);
if (r == 0xfff) {
eve_copro_reset();
return EVE_ERR;
}
return (r == cmd_offset);
}
int eve_cmd_exec(int w) {
eve_write16(REG_CMD_WRITE, cmd_offset);
if (w) {
int r;
do {
r = eve_cmd_done();
if (r < 0) break;
} while (!r);
if (r < 0) return EVE_ERR;
}
return EVE_OK;
}
void eve_cmd_burst_start(void) {
uint32_t addr = EVE_RAM_CMD + cmd_offset;
eve_spi_lock();
eve_spi_cs_set();
eve_spi_xchg24(addr | EVE_MEM_WRITE, EVE_SPI_FLAG_TX);
cmd_burst = 1;
}
void eve_cmd_burst_end(void) {
eve_spi_flush();
eve_spi_cs_clear();
eve_spi_unlock();
cmd_burst = 0;
}
uint16_t eve_handle_intr(void) {
uint16_t intr_flags;
intr_flags = eve_read16(REG_INT_FLAGS);
if (intr_flags & (EVE_INT_CONVCOMPLETE | EVE_INT_TAG)) eve_handle_touch(intr_flags);
return intr_flags;
}
int eve_init(void) {
uint8_t chipid = 0;
uint8_t reset = 0x07;
uint16_t timeout;
eve_command(EVE_CORERST, 0); /* reset */
eve_command(EVE_CLKEXT, 0);
eve_command(EVE_CLKSEL, 0x46); /* set clock to 72 MHz */
eve_command(EVE_ACTIVE, 0); /* start EVE */
eve_sleep(4);
timeout = 0;
while (chipid != 0x7c) { /* if chipid is not 0x7c, continue to read it until it is, EVE needs a moment for it's power on self-test and configuration */
eve_sleep(1);
chipid = eve_read8(REG_ID);
timeout++;
if (timeout > 400) return EVE_ERR;
}
timeout = 0;
while (reset != 0x00) { /* check if EVE is in working status */
eve_sleep(1);
reset = eve_read8(REG_CPURESET) & 0x07;
timeout++;
if(timeout > 50) return EVE_ERR;
}
eve_write32(REG_FREQUENCY, 72000000); /* tell EVE that we changed the frequency from default to 72MHz for BT8xx */
eve_write8(REG_PWM_DUTY, 0x00);
eve_write16(REG_GPIOX, 0x0000);
eve_write16(REG_GPIOX_DIR, 0x8000);
/* initialize display */
eve_write16(REG_HCYCLE, EVE_HCYCLE); /* total number of clocks per line, incl front/back porch */
eve_write16(REG_HOFFSET, EVE_HOFFSET); /* start of active line */
eve_write16(REG_HSYNC0, EVE_HSYNC0); /* start of horizontal sync pulse */
eve_write16(REG_HSYNC1, EVE_HSYNC1); /* end of horizontal sync pulse */
eve_write16(REG_HSIZE, EVE_HSIZE); /* active display width */
eve_write16(REG_VCYCLE, EVE_VCYCLE); /* total number of lines per screen, including pre/post */
eve_write16(REG_VOFFSET, EVE_VOFFSET); /* start of active screen */
eve_write16(REG_VSYNC0, EVE_VSYNC0); /* start of vertical sync pulse */
eve_write16(REG_VSYNC1, EVE_VSYNC1); /* end of vertical sync pulse */
eve_write16(REG_VSIZE, EVE_VSIZE); /* active display height */
eve_write8(REG_SWIZZLE, EVE_SWIZZLE); /* FT8xx output to LCD - pin order */
eve_write8(REG_PCLK_POL, EVE_PCLKPOL); /* LCD data is clocked in on this PCLK edge */
eve_write8(REG_CSPREAD, EVE_CSPREAD); /* helps with noise, when set to 1 fewer signals are changed simultaneously, reset-default: 1 */
/* do not set PCLK yet - wait for just after the first display list */
/* disable audio */
eve_write16(REG_SOUND, 0x0060); /* set synthesizer to mute */
eve_write8(REG_PLAY, 0x01);
eve_write8(REG_VOL_SOUND, 0x00); /* turn synthesizer volume off */
eve_write8(REG_VOL_PB, 0x00); /* turn recorded audio volume off */
/* configure interrupts */
eve_write16(REG_INT_MASK, 0);
/* write a basic display-list to get things started */
eve_dl_start(EVE_RAM_DL, 0);
eve_dl_write(CLEAR_COLOR_RGB(0,0,0));
eve_dl_write(CLEAR(1,1,1));
eve_dl_write(DISPLAY());
eve_dl_swap();
#ifdef EVE_PCLK_FREQ
eve_cmd(CMD_PCLKFREQ, "www", EVE_PCLK_FREQ, 0, 0);
eve_cmd_exec(1);
#endif
/* nothing is being displayed yet... the pixel clock is still 0x00 */
return EVE_OK;
}
void eve_clk_start(void) {
uint16_t gpiox;
eve_write8(REG_PCLK, EVE_PCLK); /* start clocking data to the LCD panel */
gpiox = eve_read16(REG_GPIOX) | 0x8000;
eve_write16(REG_GPIOX, gpiox); /* enable the DISP signal to the LCD panel, it is set to output in REG_GPIOX_DIR */
}
void eve_clk_stop(void) {
uint16_t gpiox;
gpiox = eve_read16(REG_GPIOX) & ~0x8000;
eve_write16(REG_GPIOX, gpiox);
eve_write8(REG_PCLK, 0);
}
void eve_intr_enable(void) {
eve_write8(REG_INT_EN, 0x01);
while(eve_read8(REG_INT_FLAGS));
}
void eve_intr_disable(void) {
eve_write8(REG_INT_EN, 0x00);
while(eve_read8(REG_INT_FLAGS));
}
void eve_activate(void) {
eve_command(EVE_ACTIVE, 0);
eve_sleep(40);
}
void eve_pwr_standby(void) {
if (power_state != EVE_PSTATE_ACTIVE) return;
eve_command(EVE_STANDBY, 0);
power_state = EVE_PSTATE_STANDBY;
}
void eve_pwr_sleep(void) {
if (power_state != EVE_PSTATE_ACTIVE) return;
eve_clk_stop();
eve_intr_disable();
eve_command(EVE_SLEEP, 0);
power_state = EVE_PSTATE_SLEEP;
}
void eve_pwr_wake(void) {
eve_activate();
if (power_state == EVE_PSTATE_SLEEP) {
eve_intr_enable();
eve_clk_start();
}
power_state = EVE_PSTATE_ACTIVE;
}
int eve_gpio_get(int gpio) {
uint16_t reg = eve_read16(REG_GPIOX);
return !!(reg & (1 << gpio));
}
void eve_gpio_set(int gpio, int val) {
uint16_t reg = eve_read16(REG_GPIOX);
uint16_t reg_val = (1 << gpio);
reg = val ? reg | reg_val : reg & ~reg_val;
eve_write16(REG_GPIOX, reg);
}
uint8_t eve_gpio_get_dir(void) {
uint16_t reg = eve_read16(REG_GPIOX_DIR);
return reg & 0x000f;
}
void eve_gpio_set_dir(uint8_t dir) {
uint16_t reg = eve_read16(REG_GPIOX_DIR);
reg &= 0xfff0;
reg |= dir & 0x0f;
eve_write16(REG_GPIOX_DIR, reg);
}
void eve_brightness(uint8_t b) {
if (lcd_absent) b = 0;
eve_write8(REG_PWM_DUTY, b);
}
void eve_lcd_absent(void) {
lcd_absent = 1;
}
void eve_copro_reset(void) {
uint16_t ptr;
cmd_offset = 0;
ptr = eve_read16(REG_COPRO_PATCH_PTR);
eve_write8(REG_CPURESET, 1);
eve_write16(REG_CMD_READ, 0);
eve_write16(REG_CMD_WRITE, 0);
eve_write16(REG_CMD_DL, 0);
eve_write8(REG_CPURESET, 0);
eve_write16(REG_COPRO_PATCH_PTR, ptr);
eve_write8(REG_PCLK, EVE_PCLK);
}
|