forked from isogashii/fernly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
511 lines (443 loc) · 10.2 KB
/
main.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
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include <string.h>
#include "bionic.h"
#include "memio.h"
#include "printf.h"
#include "serial.h"
#include "utils.h"
#include "scriptic.h"
#include "fernvale-pmic.h"
//#define AUTOMATED
#if !defined(AUTOMATED)
#define PROMPT "fernly> "
static int serial_get_line(char *bfr, int len)
{
int cur = 0;
while (cur < len) {
bfr[cur] = serial_getc();
serial_putc(bfr[cur]);
/* Carriage Return */
if (bfr[cur] == '\n') {
bfr[cur] = '\0';
return 0;
}
/* Linefeed */
else if (bfr[cur] == '\r') {
bfr[cur] = '\0';
return 0;
}
/* Backspace */
else if (bfr[cur] == 0x7f) {
bfr[cur] = '\0';
if (cur > 0) {
serial_putc('\b');
serial_putc(' ');
serial_putc('\b');
cur--;
}
}
/* Ctrl-U */
else if (bfr[cur] == 0x15) {
while (cur > 0) {
serial_putc('\b');
serial_putc(' ');
serial_putc('\b');
bfr[cur] = '\0';
cur--;
}
}
/* Ctrl-W */
else if (bfr[cur] == 0x17) {
while (cur > 0 && bfr[cur] != ' ') {
serial_putc('\b');
serial_putc(' ');
serial_putc('\b');
bfr[cur] = '\0';
cur--;
}
}
/* Escape code */
else if (bfr[cur] == 0x1b) {
/* Next two characters are escape codes */
uint8_t next = serial_getc();
/* Sanity check: next should be '[' */
(void)next;
next = serial_getc();
}
else
cur++;
}
bfr[len - 1] = '\0';
return -1;
}
#endif
static int list_registers(void)
{
int var;
serial_puts("Registers:\n");
serial_puts("CPSR: ");
asm volatile ("mrs %0, cpsr":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("SPSR: ");
asm volatile ("mrs %0, spsr":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("R0: ");
asm volatile ("mov %0, r0":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("R1: ");
asm volatile ("mov %0, r1":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("R2: ");
asm volatile ("mov %0, r2":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("R3: ");
asm volatile ("mov %0, r3":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("R4: ");
asm volatile ("mov %0, r4":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("R5: ");
asm volatile ("mov %0, r5":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("R6: ");
asm volatile ("mov %0, r6":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("R7: ");
asm volatile ("mov %0, r7":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("R8: ");
asm volatile ("mov %0, r8":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("R9: ");
asm volatile ("mov %0, r9":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("R10: ");
asm volatile ("mov %0, r10":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("FP: ");
asm volatile ("mov %0, r11":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("IP: ");
asm volatile ("mov %0, r12":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("SP: ");
asm volatile ("mov %0, r13":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("LR: ");
asm volatile ("mov %0, r14":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
serial_puts("PC: ");
asm volatile ("mov %0, r15":"=r" (var));
serial_puth(var, 8);
serial_puts("\n");
return 0;
}
static int shell_run_command(char *line);
static int do_init(void)
{
serial_init();
list_registers();
/* Disable system watchdog */
writel(0x2200, 0xa0030000);
/* Enable USB Download mode (required for no-battery operation) */
writew(0x8000, PMIC_CTRL10);
/* Disable battery watchdog */
writew(0x2, PMIC_CTRL9);
scriptic_run("set_plls");
scriptic_run("enable_psram");
serial_puts("\n\nFernly shell\n");
shell_run_command("bl 5");
shell_run_command("lcd init");
shell_run_command("lcd tpd");
/* Initialize the keypad */
scriptic_run("set_kbd");
return 0;
}
#ifdef AUTOMATED
static inline int get_hex(int bytes)
{
uint32_t word = 0;
uint8_t buf;
int i;
if (bytes == 4)
i = 28;
else if (bytes == 2)
i = 12;
else
i = 4;
while (i >= 0) {
buf = serial_getc();
if (buf > 96)
buf -= 87;
else if (buf > 64)
buf -= 55;
else
buf -= 48;
word |= (buf << i);
i -= 4;
}
return word;
}
/* Protocol:
* Stream is byte-oriented. The following commands are known:
*
* r - read an address
* w - write to an address
* z - zero-fill a region
*
* Responses:
*
* k - Ready for command
* ? - Unknown command
*/
static int loop(void)
{
int buf;
int size;
uint32_t offset;
uint32_t value;
serial_putc('k');
buf = serial_getc();
switch (buf) {
/* Read. Format: r[otf]aaaaaaaa
otf -> read One, Two, or Four bytes
a.. -> address to read
*/
case 'r':
size = serial_getc();
if (size == 'o') {
offset = get_hex(4);
value = readb(offset);
serial_puth(value, 2);
}
else if (size == 't') {
offset = get_hex(4);
value = readw(offset);
serial_puth(value, 4);
}
else {
offset = get_hex(4);
value = readl(offset);
serial_puth(value, 8);
}
break;
case 'f':
size = get_hex(4);
break;
/* Write. Format: w[otf]aaaaaaaavvvvvvvv
otf -> write One, Two, or Four bytes
a.. -> address to write
v.. -> value to write
*/
case 'w':
size = serial_getc();
if (size == 'o') {
offset = get_hex(4);
value = get_hex(1);
writeb(value, offset);
serial_puth(value, 2);
}
else if (size == 't') {
offset = get_hex(4);
value = get_hex(2);
writew(value, offset);
serial_puth(value, 4);
}
else {
offset = get_hex(4);
value = get_hex(4);
writel(value, offset);
serial_puth(value, 8);
}
break;
case 'z': {
uint32_t start;
uint32_t end;
start = get_hex(4);
end = get_hex(4);
while (start < end) {
*((uint32_t *)start) = 0;
start += 4;
}
}
break;
default:
serial_putc('?');
break;
}
return 0;
}
#else /* AUTOMATED */
static int cmd_help(int argc, char **argv);
extern int cmd_hex(int argc, char **argv);
extern int cmd_irq(int argc, char **argv);
extern int cmd_msleep(int argc, char **argv);
extern int cmd_peek(int argc, char **argv);
extern int cmd_poke(int argc, char **argv);
extern int cmd_spi(int argc, char **argv);
extern int cmd_spi_raw(int argc, char **argv);
extern int cmd_swi(int argc, char **argv);
extern int cmd_reboot(int argc, char **argv);
extern int cmd_led(int argc, char **argv);
extern int cmd_bl(int argc, char **argv);
extern int cmd_lcd(int argc, char **argv);
extern int cmd_load(int argc, char **argv);
extern int cmd_loadjump(int argc, char **argv);
extern int cmd_keypad(int argc, char **argv);
static const struct {
int (*func)(int argc, char **argv);
const char *name;
const char *help;
} commands[] = {
{
.func = cmd_help,
.name = "help",
.help = "Print help about available commands",
},
{
.func = cmd_reboot,
.name = "reboot",
.help = "Reboot Fernvale",
},
{
.func = cmd_msleep,
.name = "msleep",
.help = "Sleep for some number of milliseconds",
},
{
.func = cmd_hex,
.name = "hex",
.help = "Print area of memory as hex",
},
{
.func = cmd_peek,
.name = "peek",
.help = "Look at one area of memory",
},
{
.func = cmd_poke,
.name = "poke",
.help = "Write a value to an area of memory",
},
{
.func = cmd_irq,
.name = "irq",
.help = "Manipulate IRQs",
},
{
.func = cmd_spi,
.name = "spi",
.help = "Manipulate on-board SPI",
},
{
.func = cmd_spi_raw,
.name = "spi_raw",
.help = "Manipulate on-board SPI (raw interface)",
},
{
.func = cmd_swi,
.name = "swi",
.help = "Generate software interrupt",
},
{
.func = cmd_led,
.name = "led",
.help = "Turn the on-board LED on or off",
},
{
.func = cmd_bl,
.name = "bl",
.help = "Set the LCD backlight brightness",
},
{
.func = cmd_lcd,
.name = "lcd",
.help = "Manipulate the LCD",
},
{
.func = cmd_load,
.name = "load",
.help = "Load data to a specific area in memory",
},
{
.func = cmd_loadjump,
.name = "loadjmp",
.help = "Load data to a specific area in memory, "
"then jump to it",
},
{
.func = cmd_keypad,
.name = "keypad",
.help = "Read keys from keypad until # is pressed ",
},
};
int cmd_help(int argc, char **argv)
{
int i;
printf("Fernly shell help. Available commands:\n");
for (i = 0; i < sizeof(commands) / sizeof(*commands); i++) {
serial_puts("\t");
serial_puts(commands[i].name);
serial_puts("\t");
serial_puts(commands[i].help);
serial_puts("\n");
}
return 0;
}
static int shell_run_command(char *line)
{
char *lp, *cmd, *tokp;
char *args[8];
int i, n;
lp = _strtok(line, " \t", &tokp);
cmd = lp;
n = 0;
while ((lp = _strtok(NULL, " \t", &tokp)) != NULL) {
if (n >= 7) {
printf("too many arguments\r\n");
cmd = NULL;
break;
}
args[n++] = lp;
}
args[n] = NULL;
if (cmd == NULL)
return -1;
for (i = 0; i < sizeof(commands) / sizeof(*commands); i++)
if (!_strcasecmp(commands[i].name, cmd))
return commands[i].func(n, args);
printf("Unknown command: %s\n", cmd);
return 0;
}
static int loop(void)
{
char line[256];
serial_puts(PROMPT);
serial_get_line(line, sizeof(line));
printf("\n");
return shell_run_command(line);
}
#endif /* ! AUTOMATED */
int main(void)
{
do_init();
while (1)
loop();
return 0;
}