-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.c
393 lines (339 loc) · 9.04 KB
/
cmd.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
#include <string.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "m128_hal.h"
#include "cmd.h"
#include "uart.h"
#include "1wire.h"
#include "fan.h"
#include "settings.h"
#include "spi.h"
#include "ipc.h"
#include "aaps_a.h"
int (*pt2Function)(uint32_t, uint8_t) = 0;
static uint8_t initialized = 0;
static cmd_input_t cmd_input;
static void find_service(const char * service);
uint32_t param = 0;
uint8_t slave = 0xFF; //No slave can have id 0xFF
/*
* Functions that can be registered
*/
static int help(uint32_t n, uint8_t a);
//static int reboot(void);
static int reboot(uint32_t n, uint8_t a);
//static int fan0_speed(uint16_t speed);
static int fan0_speed(uint32_t speed, uint8_t not_used);
static int fan1_speed(uint32_t speed, uint8_t not_used);
//static int fan1_speed(uint16_t speed);
//static int set_relay_d(uint16_t enable);
static int set_relay_d(uint32_t enable, uint8_t not_used);
#define CHAR_BACKSPACE 0x7F
struct cmd_list_t {
const char *name;
int (*func)(uint32_t, uint8_t);
};
#define ASCII_CR 0x0D
ISR(USART2_RX_vect)
{
char c = UART_DATA_REG;
if(isalnum(c) || c == ASCII_CR || c == ' ' || c == '_')
{
if (c == ASCII_CR) {
c = 0x00;
}
/*
* This checks for input buffer overflow.
* If this scenario appears we need to change
* the size of CMD_INPUT_BUFFER_SIZE-
*/
if (cmd_input.pos == (CMD_INPUT_BUFFER_SIZE)) {
cmd_input.pos--;
if (c == ASCII_CR)
c = 0x00;
}
cmd_input.buffer[cmd_input.pos] = c;
cmd_input.pos++;
} else if (c == CHAR_BACKSPACE) {
cmd_input.pos--;
}
}
void cmd_init(void)
{
memset(cmd_input.buffer, 0, CMD_INPUT_BUFFER_SIZE);
cmd_input.pos = 0;
printk("Cmd initialized\n");
initialized = 1;
}
void pending_cmd(void)
{
if(initialized)
{
if (cmd_input.pos != 0 )
{
if (cmd_input.buffer[cmd_input.pos-1] == 0x0 && cmd_input.pos > 1)
{
find_service(cmd_input.buffer);
if( pt2Function != 0) {
pt2Function(param, slave);
pt2Function = 0;
} else {
printk("%s not found\n", cmd_input.buffer);
}
memset(cmd_input.buffer, 0, CMD_INPUT_BUFFER_SIZE);
cmd_input.pos = 0;
}
}
}
}
static struct cmd_list_t cmd_list[] = {
{ "help", help },
{ "raw_v", raw_v },
{ "voltage", voltage },
{ "raw_c", raw_c },
{ "current", current },
{ "reboot", reboot },
{ "fan0", fan0_speed },
{ "fan1", fan1_speed },
{ "relayd", set_relay_d },
{ "relay", set_relay },
{ "gettemp", get_aaps_a_temp },
{ "getadc", get_adc },
};
static void find_service(const char * service)
{
uint8_t i;
size_t list_len = sizeof(cmd_list) / sizeof(cmd_list[0]);
char *delimiter = strchr(service, ' ');
if(delimiter) {
param = atol(delimiter+1);
*delimiter = 0x00;
/* Check if command has a channel as
* additional parameter
*/
delimiter = strchr(delimiter + 1, ' ');
if(delimiter) {
/* TODO: This lookup must be done in
* some other way
*/
/* TODO: This is a bug if this doesn't convert
* to a integer within number of available
* channels.
*/
slave = atoi(delimiter+1);
//printk("lookup: %u\n", slave);
} else {
}
}
for (i = 0; i < list_len; i++)
{
if (strcmp(service, cmd_list[i].name) == 0)
pt2Function = cmd_list[i].func;
}
}
/*
* Functions that can be registered
*/
static int help(uint32_t n, uint8_t a)
{
uint8_t i;
char *name;
size_t len;
size_t list_len = sizeof(cmd_list) / sizeof(cmd_list[0]);
for (i = 1; i < list_len; i++)
{
len = strlen(cmd_list[i].name) + 1 + 1;
name = malloc(len);
if (name) {
memcpy(name, cmd_list[i].name, len - 2);
name[len-2] = '\n';
name[len-1] = 0;
//printk("Allocated %u bytes\n", len);
} else {
/* Out of memory */
return -1;
}
printk(name);
free(name);
name = NULL;
}
return 0;
}
int current(uint32_t raw, uint8_t slave)
{
printk("current\n");
uint32_t translated_current = raw;
translated_current /= 78; //µA per bit
dac_current_limit_calc = raw;
raw_c(translated_current, slave);
return 0;
}
int raw_c(uint32_t current, uint8_t slave)
{
printk("raw_c\n");
ipc_ret_t res;
uint8_t total_len = 5;
uint8_t payload_len = total_len - IPC_PKT_OVERHEAD;
struct ipc_packet_t pkt =
{
.len = total_len,
.cmd = IPC_CMD_SET_CURRENT_LIMIT,
};
dac_current_limit = current;
if (!display_calculated_values)
dac_current_limit = current;
pkt.data = malloc(payload_len);
if (pkt.data == NULL)
printk("malloc4 failed\n");
pkt.data[0] = current & 0xff;
pkt.data[1] = (current >> 8) & 0xff;
pkt.crc = crc8(pkt.data, payload_len);
res = ipc_put_pkt(slave, &pkt);
if (res != IPC_RET_OK)
printk("put packet failed [%u]\n", res);
free(pkt.data);
return 0;
}
int voltage(uint32_t raw, uint8_t slave)
{
printk("voltage\n");
uint32_t translated_voltage = raw;
translated_voltage /= 500; /* <--- This is a trim value */
dac_voltage = translated_voltage;
printk("dac_voltage: %lu\n", translated_voltage);
dac_voltage_calc = raw;
raw_v(translated_voltage, slave);
return 0;
}
int raw_v(uint32_t voltage, uint8_t slave)
{
printk("raw_v\n");
ipc_ret_t res;
uint8_t total_len = 5;
uint8_t payload_len = total_len - IPC_PKT_OVERHEAD;
struct ipc_packet_t pkt =
{
.len = total_len,
.cmd = IPC_CMD_SET_VOLTAGE,
};
dac_voltage = voltage;
pkt.data = malloc(payload_len);
if (pkt.data == NULL)
printk("raw_v failed\n");
pkt.data[0] = voltage & 0xff;
pkt.data[1] = (voltage >> 8) & 0xff;
pkt.crc = crc8(pkt.data, payload_len);
res = ipc_put_pkt(slave, &pkt);
if (res != IPC_RET_OK)
printk("raw_v pkt failed [%u]\n", res);
free(pkt.data);
return 0;
}
static int reboot(uint32_t n, uint8_t a)
{
printk("Rebooting...\n");
wdt_enable(WDTO_250MS);
/* Wait for WD reset */
while(1)
;
return 0;
}
static int fan0_speed(uint32_t speed, uint8_t not_used)
{
printk("FAN0 speed: %u\n", speed);
set_fan_speed(SYS_FAN0, speed);
return 0;
}
static int fan1_speed(uint32_t speed, uint8_t not_used)
{
printk("FAN1 speed: %u\n", speed);
set_fan_speed(SYS_FAN1, speed);
return 0;
}
static int set_relay_d(uint32_t enable, uint8_t not_used)
{
ipc_ret_t res;
struct ipc_packet_t pkt =
{
.len = 4,
.cmd = IPC_CMD_SET_RELAY_D,
};
pkt.data = malloc(1);
if (pkt.data == NULL)
printk("malloc9 failed\n");
pkt.data[0] = enable ? 1 : 0;
pkt.crc = crc8(pkt.data, 1);
res = ipc_put_pkt(slave, &pkt);
if (res != IPC_RET_OK)
printk("put packet failed [%u]\n", res);
free(pkt.data);
return 0;
}
int set_relay(uint32_t enable, uint8_t slave)
{
/* TODO: This is a total hack! Remove it! */
ipc_ret_t res;
struct ipc_packet_t pkt =
{
.len = 4,
.cmd = IPC_CMD_SET_RELAY,
};
pkt.data = malloc(1);
if (pkt.data == NULL)
printk("malloc99 failed\n");
pkt.data[0] = enable ? 1 : 0;
pkt.crc = crc8(pkt.data, 1);
res = ipc_put_pkt(slave, &pkt);
if (res != IPC_RET_OK)
printk("put packet failed [%u]\n", res);
send_set_led(IPC_LED_GREEN, pkt.data[0]);
free(pkt.data);
slave = 0xff;
return 0;
}
int get_aaps_a_temp(uint32_t channel, uint8_t slave)
{
ipc_ret_t res;
uint8_t payload_len = 1;
uint8_t total_len = payload_len + IPC_PKT_OVERHEAD;
struct ipc_packet_t pkt =
{
.len = total_len,
.cmd = IPC_CMD_GET_TEMP,
};
pkt.data = malloc(payload_len);
if (pkt.data == NULL)
printk("malloc11 failed\n");
pkt.data[0] = channel & 0xff;
//printk("Temp sensor: %u\n", pkt.data[0]);
pkt.crc = crc8(pkt.data, payload_len);
res = ipc_put_pkt(slave, &pkt);
if (res != IPC_RET_OK)
printk("put packet failed [%u]\n", res);
free(pkt.data);
return 0;
}
int get_adc(uint32_t channel, uint8_t slave)
{
ipc_ret_t res;
uint8_t payload_len = 1;
uint8_t total_len = payload_len + IPC_PKT_OVERHEAD;
struct ipc_packet_t pkt =
{
.len = total_len,
.cmd = IPC_CMD_GET_ADC,
};
pkt.data = malloc(payload_len);
if (pkt.data == NULL)
printk("malloc2 failed\n");
pkt.data[0] = channel & 0xff;
pkt.crc = crc8(pkt.data, payload_len);
res = ipc_put_pkt(slave, &pkt);
if (res != IPC_RET_OK)
printk("put packet failed [%u]\n", res);
free(pkt.data);
return 0;
}