-
Notifications
You must be signed in to change notification settings - Fork 6
/
console.c
508 lines (411 loc) · 10.7 KB
/
console.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "stm32f4xx_conf.h"
#include "stm32f4xx.h"
#include "console.h"
#include "config.h"
#include "fifo.h"
#include "i2c.h"
#include "spi.h"
#include "gpio.h"
#include "adc.h"
#include "dac.h"
#include "pwm.h"
typedef struct {
char *commandStr;
void (*fn)(uint32_t argc, char *argv[]);
char *helpStr;
} command_t;
fifo_t usbRxFifo;
static uint8_t *uid = (uint8_t *)(0x1FFF7A10);
static char cmdBuff[CMD_BUFF_SIZE];
static uint32_t argc;
static char* argv[ARGV_MAX];
static uint8_t rxBuff[TX_RX_BUFF_SIZE];
static uint8_t txBuff[TX_RX_BUFF_SIZE];
static void helpFn(uint32_t argc, char *argv[]);
static void i2cCmd(uint32_t argc, char *argv[]);
static void adcCmd(uint32_t argc, char *argv[]);
static void adcNumCmd(uint32_t argc, char *argv[]);
static void dacCmd(uint32_t argc, char *argv[]);
static void dacEnableCmd(uint32_t argc, char *argv[]);
static void spiCmd(uint32_t argc, char *argv[]);
static void spiCfgCmd(uint32_t argc, char *argv[]);
static void spiSetCSCmd(uint32_t argc, char *argv[]);
static void gpioCmd(uint32_t argc, char *argv[]);
static void gpioCfgCmd(uint32_t argc, char *argv[]);
static void pwmCmd(uint32_t argc, char *argv[]);
static void snCmd(uint32_t argc, char *argv[]);
static void versionCmd(uint32_t argc, char *argv[]);
static const char versionStr[] = SILTA_VERSION;
static command_t commands[] = {
{"i2c", i2cCmd, "i2c <addr> <rdlen> [wrbytes (04 D1 ..)]"},
{"adcnum", adcNumCmd, "adcnum <port[A-E]> <pin0-15>"},
{"adc", adcCmd, "adc <adc_num>"},
{"dac", dacCmd, "dac <dac_num> <val>"},
{"dacenable", dacEnableCmd, "dacenable"},
{"spi", spiCmd, "spi <rwbytes (04 D1 ..)>"},
{"spicfg", spiCfgCmd, "spicfg <speed> <cpol> <cpha>"},
{"spics", spiSetCSCmd, "<port[A-E]> <pin0-15>"},
{"config", cfgCmd, "<key> [value]"},
{"gpio", gpioCmd, "<port[A-E]> <pin0-15> [value]"},
{"gpiocfg", gpioCfgCmd, "<port[A-E]> <pin0-15> <in|outpp|outod> [pullup|pulldown|nopull]"},
{"pwm", pwmCmd, "<channel> <position(1000-2000)"},
{"sn", snCmd, "sn"},
{"version", versionCmd, "version"},
// Add new commands here!
{"help", helpFn, "Print this!"},
{NULL, NULL, NULL}
};
//
// Print the help menu
//
static void helpFn(uint32_t argc, char *argv[]) {
command_t *command = commands;
if(argc < 2) {
while(command->commandStr != NULL) {
printf("%s - %s\n", command->commandStr, command->helpStr);
command++;
}
} else {
while(command->commandStr != NULL) {
if(strcmp(command->commandStr, argv[1]) == 0) {
printf("%s - %s\n", command->commandStr, command->helpStr);
break;
}
command++;
}
}
}
#define I2C_ADDR_OFFSET (1)
#define I2C_RLEN_OFFSET (2)
#define I2C_WBUFF_OFFSET (3)
static void i2cCmd(uint32_t argc, char *argv[]) {
int32_t rval;
do {
if(argc < 3) {
printf("ERR: I2C Not enough arguments\n");
break;
}
uint8_t addr = strtoul(argv[I2C_ADDR_OFFSET], NULL, 16);
uint8_t rLen = strtoul(argv[I2C_RLEN_OFFSET], NULL, 10);
uint8_t wLen = argc - I2C_WBUFF_OFFSET;
if(wLen > sizeof(txBuff)) {
printf("ERR: I2C Not enough space in txBuff\n");
break;
}
if(rLen > sizeof(rxBuff)) {
printf("ERR: I2C Not enough space in rxBuff\n");
break;
}
if(wLen > 0) {
for(uint32_t byte = 0; byte < wLen; byte++) {
txBuff[byte] = strtoul(argv[I2C_WBUFF_OFFSET + byte], NULL, 16);
}
}
rval = i2c(I2C1, addr, wLen, txBuff, rLen, rxBuff);
if(rval) {
printf("ERR %ld\n", rval);
} else {
printf("OK ");
for(uint32_t byte = 0; byte < rLen; byte++) {
printf("%02X ", rxBuff[byte]);
}
printf("\n");
}
} while (0);
}
static void adcNumCmd(uint32_t argc, char *argv[]) {
do {
int32_t adcNum;
if(argc < 3) {
printf("ERR Invalid args\n");
break;
}
char port = toupper((uint32_t)argv[1][0]);
uint8_t pin = strtoul(argv[2], NULL, 10);
GPIO_TypeDef *GPIOx = NULL;
if ((port < 'A') || (port > 'E')) {
printf("ERR Invalid port\n");
break;
}
if (pin > 15) {
printf("ERR Invalid pin\n");
break;
}
GPIOx = (GPIO_TypeDef *)(GPIOA_BASE + (uint32_t)(port - 'A') * (GPIOB_BASE - GPIOA_BASE));
adcNum = adcGetPin(GPIOx, pin);
if(adcNum >= 0) {
printf("OK %ld\n", adcNum);
} else {
printf("ERR Not an adc pin\n");
}
} while(0);
}
static void adcCmd(uint32_t argc, char *argv[]) {
do {
int32_t adcVal;
if(argc < 2) {
printf("ERR Invalid args\n");
break;
}
uint8_t pin = strtoul(argv[1], NULL, 10);
if (pin > 15) {
printf("ERR Invalid pin\n");
break;
}
adcVal = adcRead(pin);
if(adcVal >= 0) {
printf("OK %ld\n", adcVal);
} else {
printf("ERR Invalid adcnum\n");
}
} while(0);
}
static void dacEnableCmd(uint32_t argc, char *argv[]) {
dacInit();
printf("OK\n");
}
static void dacCmd(uint32_t argc, char *argv[]) {
do {
if(argc < 3) {
printf("ERR Invalid args\n");
break;
}
uint8_t dac = strtoul(argv[1], NULL, 10);
uint16_t val = strtoul(argv[2], NULL, 10);
if (dac > 1) {
printf("ERR Invalid dac\n");
break;
}
dacSet(dac, val);
printf("OK\n");
} while(0);
}
#define SPI_WBUFF_OFFSET (1)
static void spiCmd(uint32_t argc, char *argv[]) {
int32_t rval = 0;
do {
uint32_t rwLen = argc - SPI_WBUFF_OFFSET;
if(argc < 2) {
printf("ERR: SPI Not enough arguments\n");
break;
}
for(uint32_t byte = 0; byte < rwLen; byte++) {
txBuff[byte] = strtoul(argv[SPI_WBUFF_OFFSET + byte], NULL, 16);
}
rval = spi(0, rwLen, txBuff, rxBuff);
if(rval) {
printf("ERR %ld\n", rval);
} else {
printf("OK ");
for(uint32_t byte = 0; byte < rwLen; byte++) {
printf("%02X ", rxBuff[byte]);
}
printf("\n");
}
} while (0);
}
static void spiCfgCmd(uint32_t argc, char *argv[]) {
if(argc < 3) {
printf("ERR: SPI Not enough arguments\n");
} else {
uint32_t speed = strtoul(argv[1], NULL, 10);
uint32_t cpol = strtoul(argv[2], NULL, 10);
uint32_t cpha = strtoul(argv[3], NULL, 10);
spiConfig(0, speed, cpol, cpha);
spiInit(0);
printf("OK\n");
}
}
static void spiSetCSCmd(uint32_t argc, char *argv[]) {
do {
if(argc < 3) {
printf("ERR Invalid args\n");
break;
}
char port = toupper((uint32_t)argv[1][0]);
uint8_t pin = strtoul(argv[2], NULL, 10);
GPIO_TypeDef *GPIOx = NULL;
if ((port < 'A') || (port > 'E')) {
printf("ERR Invalid port\n");
break;
}
if (pin > 15) {
printf("ERR Invalid pin\n");
break;
}
GPIOx = (GPIO_TypeDef *)(GPIOA_BASE + (uint32_t)(port - 'A') * (GPIOB_BASE - GPIOA_BASE));
spiSetCS(0, GPIOx, pin);
printf("OK\n");
} while(0);
}
//
// Set/get GPIO pins
//
static void gpioCmd(uint32_t argc, char *argv[]) {
if(argc > 2) {
do {
char port = toupper((uint32_t)argv[1][0]);
uint8_t pin = strtoul(argv[2], NULL, 10);
GPIO_TypeDef *GPIOx = NULL;
if ((port < 'A') || (port > 'E')) {
printf("ERR Invalid port\n");
break;
}
if (pin > 15) {
printf("ERR Invalid pin\n");
break;
}
GPIOx = (GPIO_TypeDef *)(GPIOA_BASE + (uint32_t)(port - 'A') * (GPIOB_BASE - GPIOA_BASE));
if(argc == 3) {
int32_t value = gpioGet(GPIOx, pin);
if (value < 0) {
printf("ERR\n");
} else {
printf("OK %ld\n", value);
}
} else {
int32_t rval = gpioSet(GPIOx, pin, (argv[3][0] != '0'));
if (rval < 0) {
printf("ERR\n");
} else {
printf("OK\n");
}
}
} while (0);
} else {
printf("ERR Invalid args\n");
}
}
//
// Configure GPIO pins as input/output(open drain or push-pull)
// and set pull-up/down resistors
//
static void gpioCfgCmd(uint32_t argc, char *argv[]) {
if(argc > 2) {
do {
GPIO_InitTypeDef gpioSettings;
char port = toupper((uint32_t)argv[1][0]);
uint8_t pin = strtoul(argv[2], NULL, 10);
GPIO_TypeDef *GPIOx = NULL;
if ((port < 'A') || (port > 'E')) {
printf("ERR Invalid port\n");
break;
}
if (pin > 15) {
printf("ERR Invalid pin\n");
break;
}
GPIOx = (GPIO_TypeDef *)(GPIOA_BASE + (uint32_t)(port - 'A') * (GPIOB_BASE - GPIOA_BASE));
gpioSettings.GPIO_Pin = (1 << pin);
gpioSettings.GPIO_Mode = GPIO_Mode_IN;
gpioSettings.GPIO_Speed = GPIO_Speed_50MHz;
gpioSettings.GPIO_OType = GPIO_OType_PP;
gpioSettings.GPIO_PuPd = GPIO_PuPd_NOPULL;
// in|outpp|outod
if (strcmp("outpp", argv[3]) == 0) {
gpioSettings.GPIO_Mode = GPIO_Mode_OUT;
gpioSettings.GPIO_OType = GPIO_OType_PP;
} else if (strcmp("outod", argv[3]) == 0) {
gpioSettings.GPIO_Mode = GPIO_Mode_OUT;
gpioSettings.GPIO_OType = GPIO_OType_OD;
} else if (strcmp("analog", argv[3]) == 0) {
if(adcGetPin(GPIOx, pin) >= 0) {
gpioSettings.GPIO_Mode = GPIO_Mode_AN;
} else {
printf("ERR Not an analog pin\n");
break;
}
}
if (argc > 4) {
// nopull|pullup|pulldown
if (strcmp("pullup", argv[4]) == 0) {
gpioSettings.GPIO_PuPd = GPIO_PuPd_UP;
} else if (strcmp("pulldown", argv[4]) == 0) {
gpioSettings.GPIO_PuPd = GPIO_PuPd_DOWN;
}
}
GPIO_Init(GPIOx, &gpioSettings);
printf("OK\n");
} while (0);
} else {
printf("ERR Invalid args\n");
}
}
static void pwmCmd(uint32_t argc, char *argv[]) {
do {
if(argc < 3) {
printf("ERR Invalid args\n");
break;
}
uint8_t ch = strtoul(argv[1], NULL, 10);
uint16_t pos = strtoul(argv[2], NULL, 10);
if (ch > 1) {
printf("ERR Invalid dac\n");
break;
}
pwmSetCCR(ch, pos);
printf("OK\n");
} while(0);
}
static void snCmd(uint32_t argc, char *argv[]) {
printf("OK ");
// Print 96-bit serial number
for(uint8_t byte = 0; byte < 12; byte++) {
printf("%02X ", uid[byte]);
}
printf("\n");
}
static void versionCmd(uint32_t argc, char *argv[]) {
printf("OK %s\n", versionStr);
}
void consoleProcess() {
uint32_t inBytes = fifoSize(&usbRxFifo);
if(inBytes > 0) {
uint32_t newLine = 0;
for(int32_t index = 0; index < inBytes; index++){
if((fifoPeek(&usbRxFifo, index) == '\n') || (fifoPeek(&usbRxFifo, index) == '\r')) {
newLine = index + 1;
break;
}
}
if(newLine > sizeof(cmdBuff)) {
newLine = sizeof(cmdBuff) - 1;
}
if(newLine) {
uint8_t *pBuf = (uint8_t *)cmdBuff;
while(newLine--){
*pBuf++ = fifoPop(&usbRxFifo);
}
// If it's an \r\n combination, discard the second one
if((fifoPeek(&usbRxFifo, 0) == '\n') || (fifoPeek(&usbRxFifo, 0) == '\r')) {
fifoPop(&usbRxFifo);
}
*(pBuf - 1) = 0; // String terminator
argc = 0;
// Get command
argv[argc] = strtok(cmdBuff, " ");
// Get arguments (if any)
while ((argc < sizeof(argv)/sizeof(char *)) && (argv[argc] != NULL)){
argc++;
argv[argc] = strtok(NULL, " ");
}
if(argc > 0) {
command_t *command = commands;
while(command->commandStr != NULL) {
if(strcmp(command->commandStr, argv[0]) == 0) {
command->fn(argc, argv);
break;
}
command++;
}
if(command->commandStr == NULL) {
printf("ERR Unknown command '%s'\n", argv[0]);
}
}
}
}
}