-
Notifications
You must be signed in to change notification settings - Fork 2
/
vi_bare.c
352 lines (303 loc) · 7.31 KB
/
vi_bare.c
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
#include "wl_def.h"
#define FIXME abort
#ifdef INTEGRATOR
/* Memory mapped peripherals. */
#define clcdc ((volatile uint32_t *)0xc0000000)
#define kmi0 ((volatile uint32_t *)0x18000000)
#endif
#ifdef LUMINARY
#include "luminary.h"
int read_buttons()
{
int buttons;
buttons = HWREG(GPIOE + 0x03c);
if (HWREG(GPIOF + 0x008))
buttons |= 0x10;
/* Buttons are active low. */
buttons ^= 0x1f;
return buttons;
}
static void ssi_flush_rx()
{
while (HWREG(SSI0 + 0x00c) & 0x04)
HWREG(SSI0 + 0x008);
}
static volatile byte *ssi_data;
static volatile int ssi_tosend;
static volatile int ssi_left;
void ssi_isr()
{
uint32_t status;
uint32_t mask;
while (1) {
status = HWREG(SSI0 + 0x00c);
mask = ssi_tosend ? 0x06 : 0x04;
if ((status & mask) == 0)
break;
/* Send data if RX FIFO empty and TX FIFO not full. */
if (ssi_tosend && (status & 0x06) == 2) {
HWREG(SSI0 + 0x008) = *(ssi_data++);
ssi_tosend--;
}
/* Pull data out of the RX fifo. */
if (status & 0x04) {
HWREG(SSI0 + 0x008);
ssi_left--;
}
}
if (!ssi_tosend) {
/* Mask SSI interrupts. */
HWREG(SSI0 + 0x014) = 0x0;
}
}
static void oled_write(const uint8_t *p, int n, int command)
{
int left;
if (command)
HWREG(GPIOC + 0x200) = 0;
else
HWREG(GPIOC + 0x200) = 0x80;
/* Use the RX fifo to determine when the transfer completes. */
left = n;
while (left) {
uint32_t status;
status = HWREG(SSI0 + 0x00c);
/* Send data if RX FIFO empty and TX FIFO not full. */
if (n == left && (status & 0x06) == 2) {
HWREG(SSI0 + 0x008) = *(p++);
n--;
}
/* Pull data out of the RX fifo. */
if (status & 0x04) {
HWREG(SSI0 + 0x008);
left--;
}
}
}
static void oled_init()
{
int i;
const uint8_t *p;
p = oled_init_strings;
while (*p) {
i = *(p++);
oled_write(p, i, 1);
p += i;
}
}
static void oled_setwindow()
{
static byte cmd[6] = {0x15, 0, 63, 0x75, 0, /* height-1 */0};
cmd[5] = oled_height - 1;
oled_write(cmd, 6, 1);
}
void oled_clear()
{
uint8_t buf[4];
int i;
oled_setwindow();
memset(buf, 0, 4);
for (i = 0; i < oled_height * (128 / 8); i++) {
oled_write(buf, 4, 0);
}
}
void oled_render()
{
uint8_t buf[16];
oled_setwindow();
buf[0] = 0xA0;
buf[1] = 0x52;
oled_write(buf, 2, 1);
ssi_data = framebuffer;
ssi_tosend = ssi_left = 128 * 64 / 2;
HWREG(GPIOC + 0x200) = 0x80;
/* Enable SSI interrupts. */
HWREG(SSI0 + 0x014) = 0xc;
/* Wait for transfer to complete. */
asm volatile ("1:\n\t"
"cpsid i\n\t"
"ldr r0, [%0]\n\t"
"cmp r0, #0\n\t"
"it ne\n\t"
"wfine\n\t"
"cpsie i\n\t"
"bne 1b\n\t"
: : "r" (&ssi_tosend) : "r0");
/* Pull remaining bytes out of FIFO. */
while (ssi_left) {
uint32_t status;
status = HWREG(SSI0 + 0x00c);
if (status & 0x04) {
HWREG(SSI0 + 0x008);
ssi_left--;
}
}
}
/* Turn off screen to prevernt burn-in. */
void VL_ScreenSaver()
{
static const byte offcmd[] = {0xAE, 0xAD, 0x02};
oled_write(offcmd, 3, 1);
while (read_buttons() == 0)
asm volatile ("wfi");
oled_init();
}
#endif
/* Input. */
void INL_Update()
{
#ifdef INTEGRATOR
int scancode;
while (kmi0[1] & 0x10) {
int press = 1;
scancode = kmi0[2];
#if 0
printf ("scancode %x\n", scancode);
#else
if (scancode == 0xe0) {
scancode = kmi0[2] | 0x100;
}
if ((scancode & 0xff) == 0xf0) {
press = 0;
scancode = (scancode & 0x100) | kmi0[2];
}
switch (scancode) {
case 0x29: scancode = sc_Space; break;
case 0x14: scancode = sc_Control; break;
case 0x175: scancode = sc_UpArrow; break;
case 0x172: scancode = sc_DownArrow; break;
case 0x16b: scancode = sc_LeftArrow; break;
case 0x174: scancode = sc_RightArrow; break;
default: scancode = 0; break;
}
if (scancode == 0)
continue;
keyboard_handler(scancode, press);
#endif
}
#endif
#ifdef LUMINARY
int buttons = 0;
static int oldbuttons = 0;
int i;
static const byte scancodes[5] = {sc_UpArrow, sc_DownArrow, sc_LeftArrow,
sc_RightArrow, sc_Space};
buttons = read_buttons();
buttons ^= oldbuttons;
oldbuttons ^= buttons;
for (i = 0; i < 5; i++) {
int mask = 1 << i;
if (buttons & mask) {
keyboard_handler(scancodes[i], (oldbuttons & mask) != 0);
}
}
#endif
}
/* Graphics bits. */
byte framebuffer[128 * 64 / 2];
void VL_Startup()
{
#ifdef INTEGRATOR
/* Initialize CLCDC. */
clcdc[0] = 0x1c; // Horizontal timing (128 pixels)
clcdc[1] = 0x3f; // Vertical timing (64 rows)
clcdc[4] = (uint32_t)framebuffer; // base address
clcdc[7] = 0x827; // 8-bit TFT
#endif
}
void VW_UpdateScreen()
{
#ifdef LUMINARY
oled_render();
#endif
}
#ifdef INTEGRATOR
void VL_SetPalette(const byte *palette)
{
int i;
uint32_t val;
const byte *p = palette;
for (i = 0; i < 128; i++) {
val = *(p++) >> 1;
val |= (*(p++) & 0x3e) << 4;
val |= (*(p++) & 0x3e) << 9;
val |= (*(p++) & 0x3e) << 15;
val |= (*(p++) & 0x3e) << 20;
val |= (*(p++) & 0x3e) << 25;
clcdc[i + 0x80] = val;
}
}
void VL_GetPalette(byte *palette)
{
FIXME();
}
#endif
#ifdef DEBUG
void Quit(const char *error)
{
FIXME();
}
#endif
void TimerInit();
static void sys_init()
{
#ifdef LUMINARY
/* Enable peripherals. */
/* Timer0, SSI0. */
HWREG(SYSCTL + 0x104) |= 0x00010010;
/* GPIO[A-G] */
HWREG(SYSCTL + 0x108) |= 0x0000007f;
/* Bump LDO voltage to workaround silicon bugs. */
//HWREG(SYSCTL + 0x034) = 0x1b;
/* Clock the PLL to 50MHz. */
HWREG(SYSCTL + 0x060) = 0x01d40b80;
/* Wait for PLL to sync, then enable. */
while ((HWREG(SYSCTL + 0x050) & 0x40) == 0)
/* no-op */ ;
HWREG(SYSCTL + 0x060) &= ~0x0800;
/* Enable SSI pins (GPIOA 2, 3, 4, 5). */
HWREG(GPIOA + 0x420) |= 0x3c; /* HW fn. */
HWREG(GPIOA + 0x508) |= 0x3c; /* 8mA drive strength. */
HWREG(GPIOA + 0x51c) |= 0x3c; /* Digital. */
HWREG(GPIOA + 0x510) |= 0x3c; /* Pull-up. */
/* D/Cn output for OLED. */
HWREG(GPIOC + 0x400) |= 0x80; /* Output */
HWREG(GPIOC + 0x51c) |= 0x80; /* Digital */
HWREG(GPIOC + 0x508) |= 0x80; /* 8mA drive strength. */
HWREG(GPIOC + 0x510) |= 0x80; /* Pull-up. */
HWREG(GPIOC + 0x200) = 0x00;
/* Explicitly enable +15V power to the OLED.
Needed on rev C board, harmless on earlier boards. */
HWREG(GPIOC + 0x400) |= 0x40; /* Output */
HWREG(GPIOC + 0x51c) |= 0x40; /* Digital */
HWREG(GPIOC + 0x508) |= 0x40; /* 8mA drive strength. */
HWREG(GPIOC + 0x510) |= 0x40; /* Pull-up. */
HWREG(GPIOC + 0x100) = 0x40; /* Enable. */
/* User pushbutton input. */
HWREG(GPIOF + 0x51c) |= 0x02; /* Digital */
HWREG(GPIOF + 0x510) |= 0x02; /* Pull-up. */
/* Direction buttons. */
HWREG(GPIOE + 0x51c) |= 0x0f; /* Digital */
HWREG(GPIOE + 0x510) |= 0x0f; /* Pull-up. */
/* Enable fss device select. */
HWREG(GPIOA + 0x420) |= 0x08;
HWREG(GPIOA + 0x020) = 0;
/* 50MHz / 3.5Mbit = 14.2 cycles/bit. */
HWREG(SSI0 + 0x010) = 2; /* prescale /2 */
HWREG(SSI0 + 0x000) = 0x0647; /* SCR=6 (+1), SPI polarity=1, 8bit */
HWREG(SSI0 + 0x04) = 2; /* Enable. */
ssi_flush_rx();
oled_init();
oled_clear();
#endif
}
int main(int argc, char **argv)
{
#ifndef LUMINARY
vwidth = 128;
vheight = 96;
#endif
sys_init();
TimerInit();
return WolfMain(argc, argv);
}