-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparams.c
462 lines (389 loc) · 10.6 KB
/
params.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
/**
* Edward ZHANG, 20180205
* @file param.c
* @brief parameter tuning and management interface
*/
#include "ch.h"
#include "hal.h"
#include "params.h"
#include "flash.h"
#include <string.h>
#define PARAM_FLASH_SECTOR 11U
#define PARAM_FLASH_ADDR 0x080E0000
#define PARAM_FLASH_HALF_BLOCK 64U
#define PARAM_FLASH_BLOCK 128U
static uint32_t param_valid_flag = 0;
static uint32_t param_private_flag = 0;
static p_param_t params[PARAMS_NUM_MAX];
static int8_t subparams[PARAMS_NUM_MAX][8];
static uint8_t params_total = 0;
static uint8_t subparams_total = 0;
static param_name_t param_name[PARAMS_NUM_MAX];
static param_name_t subparam_name[PARAMS_NUM_MAX];
/* for testing*/
p_param_t* param_p = params;
void* param_valid = ¶m_valid_flag;
void* param_private = ¶m_private_flag;
#define RXBUF_SIZE 13
static uint8_t rxbuf[RXBUF_SIZE];
#ifdef PARAMS_USE_UART
static thread_reference_t uart_receive_thread_handler = NULL;
static void sendLine(const char* const string)
{
char terminator = '\n';
uartStopSend(UART_PARAMS);
if(string != NULL)
uartStartSend(UART_PARAMS, strlen(string), string);
else
{
char null_char = '*';
uartStartSend(UART_PARAMS, 1, &null_char);
}
chThdSleepMilliseconds(20);
uartStopSend(UART_PARAMS);
uartStartSend(UART_PARAMS, 1, &terminator);
chThdSleepMilliseconds(5);
}
static THD_WORKING_AREA(params_tx_wa, 256);
static THD_FUNCTION(params_tx,p)
{
chRegSetThreadName("param transmitter");
(void)p;
chThdSleepMilliseconds(100);
char version[] = PARAMS_VERSION;
uartStopSend(UART_PARAMS);
uartStartSend(UART_PARAMS, 1, ¶ms_total);
chThdSleepMilliseconds(5);
uartStopSend(UART_PARAMS);
uartStartSend(UART_PARAMS, 1, &subparams_total);
chThdSleepMilliseconds(5);
uartStopSend(UART_PARAMS);
uartStartSend(UART_PARAMS, 4, (uint8_t*)¶m_private_flag);
chThdSleepMilliseconds(10);
char mode = 1;
uartStopSend(UART_PARAMS);
uartStartSend(UART_PARAMS, 1, &mode); //mode: CHIBIOS USB
chThdSleepMilliseconds(5);
sendLine(version);
uint8_t i;
uint32_t flag;
flag = 1;
for (i = 0; i < PARAMS_NUM_MAX; i++)
{
if(flag¶m_valid_flag)
{
uartStopSend(UART_PARAMS);
uartStartSend(UART_PARAMS, 8, subparams[i]);
chThdSleepMilliseconds(10);
uartStopSend(UART_PARAMS);
uartStartSend(UART_PARAMS, 1, &i);
chThdSleepMilliseconds(5);
}
flag = flag<<1;
}
flag = 1;
for(i = 0; i<PARAMS_NUM_MAX; i++)
{
if(flag¶m_valid_flag)
{
uartStopSend(UART_PARAMS);
uartStartSend(UART_PARAMS, 4*subparams[i][0], (uint8_t*)(params[i]));
chThdSleepMilliseconds(10);
}
flag = flag<<1;
}
flag = 1;
for(i = 0; i<PARAMS_NUM_MAX; i++)
{
if(flag¶m_valid_flag)
{
sendLine(param_name[i]);
sendLine(subparam_name[i]);
}
flag = flag<<1;
}
chThdExit(MSG_OK);
}
/*
* This callback is invoked when a receive buffer has been completely written.
*/
static void rxend(UARTDriver *uartp)
{
if(uartp == UART_PARAMS)
{
uint8_t param_index, subParam_index;
switch(rxbuf[0])
{
case 0xFD:
param_index = rxbuf[2] - '0';
subParam_index = rxbuf[3] - '0';
if((1<<param_index)¶m_valid_flag &&
subParam_index < subparams[param_index][0])
{
subparams[param_index][subParam_index + 1] = (int8_t)(rxbuf[4] - '0');
}
break;
case 0xFB:
param_save_flash();
break;
case 0xFA:
chSysLockFromISR();
thread_t* uart_transmit_thread = chThdCreateI(params_tx_wa,sizeof(params_tx_wa),
NORMALPRIO - 7,params_tx,NULL);
chThdStartI(uart_transmit_thread);
chSysUnlockFromISR();
break;
case 0xF9:
param_index = rxbuf[2] - '0';
subParam_index = rxbuf[3] - '0';
if((1<<param_index)¶m_valid_flag &&
subParam_index < subparams[param_index][0])
{
chSysLockFromISR();
uint8_t i, byte;
uint32_t data = 0;
for (i = 0; i < 8; i++)
{
byte = rxbuf[i + 4];
if(byte >= '0' && byte <= '9')
byte -= '0';
else if(byte >= 'a' && byte <= 'f')
byte -= ('a' - 10);
data |= ((byte & 0x0f) << (28 - 4*i));
}
params[param_index][subParam_index] = *(param_t*)(&data);
chSysUnlockFromISR();
}
break;
default:
{/*
uartStopSend(UART_PARAMS);
uartStartSend(UART_PARAMS, 3, "wtf");
*/
}
}
chSysLockFromISR();
chThdResumeI(&uart_receive_thread_handler,MSG_OK);
chSysUnlockFromISR();
}
}
/*
* UART driver configuration structure.
*/
static UARTConfig uart_cfg = {
NULL,NULL,rxend,NULL,NULL,
PARAMS_BR,
0,
0,
0
};
static THD_WORKING_AREA(params_rx_wa, 128);
static THD_FUNCTION(params_rx,p)
{
chRegSetThreadName("param receiver");
(void)p;
while(!chThdShouldTerminateX())
{
uartStopReceive(UART_PARAMS);
uartStartReceive(UART_PARAMS, RXBUF_SIZE, rxbuf);
chSysLock();
chThdSuspendTimeoutS(&uart_receive_thread_handler, MS2ST(1000));
chSysUnlock();
}
}
#endif
#ifdef PARAMS_USE_USB
#include "usbcfg.h"
static void sendLine(BaseSequentialStream * chp, const char* const string)
{
uint8_t i;
char* byte = string;
if(string != NULL)
for (i = 0; i < strlen(string); i++)
chSequentialStreamPut(chp, *byte++);
else
chSequentialStreamPut(chp, '*');
chThdSleepMilliseconds(20);
chSequentialStreamPut(chp, '\n');
chThdSleepMilliseconds(5);
}
void cmd_param_rx(BaseSequentialStream * chp, int argc, char *argv[])
{
uint8_t* data = argv[0];
uint8_t index = data[0] - '0';
uint8_t sub_index = data[1] - '0';
if((1<<index)¶m_valid_flag &&
sub_index < subparams[index][0])
{
chSysLock();
uint8_t i, byte;
uint32_t result = 0;
for (i = 0; i < 8; i++)
{
byte = data[i + 2];
if(byte >= '0' && byte <= '9')
byte -= '0';
else if(byte >= 'a' && byte <= 'f')
byte -= ('a' - 10);
result |= ((byte & 0x0f) << (28 - 4*i));
}
params[index][sub_index] = *(param_t*)(&result);
chSysUnlock();
}
}
void cmd_param_scale(BaseSequentialStream * chp, int argc, char *argv[])
{
uint8_t* data = argv[0];
uint8_t index = data[0] - '0';
uint8_t sub_index = data[1] - '0';
int8_t scale = data[2] - '0';
if((1<<index)¶m_valid_flag &&
sub_index < subparams[index][0])
subparams[index][sub_index + 1] = scale;
}
void cmd_param_update(BaseSequentialStream * chp, int argc, char *argv[])
{
if(!strcmp(argv[0],"update----"))
param_save_flash();
}
void cmd_param_tx(BaseSequentialStream * chp, int argc, char *argv[])
{
char version[] = PARAMS_VERSION;
chThdSleepMilliseconds(100);
chSequentialStreamPut(chp, params_total);
chThdSleepMilliseconds(10);
chSequentialStreamPut(chp, subparams_total);
chThdSleepMilliseconds(10);
char* byte = (char*)¶m_private_flag;
uint8_t i,j;
for (i = 0; i < 4; i++)
chSequentialStreamPut(chp, *byte++);
chThdSleepMilliseconds(10);
chSequentialStreamPut(chp, 0); //mode: CHIBIOS UART
chThdSleepMilliseconds(10);
sendLine(chp, version);
uint32_t flag;
flag = 1;
for (i = 0; i < PARAMS_NUM_MAX; i++)
{
if(flag¶m_valid_flag)
{
byte = (char*)subparams[i];
for (j = 0; j < 8; j++)
chSequentialStreamPut(chp, *byte++);
chThdSleepMilliseconds(10);
chSequentialStreamPut(chp, i);
chThdSleepMilliseconds(10);
}
flag = flag<<1;
}
flag = 1;
for(i = 0; i<PARAMS_NUM_MAX; i++)
{
if(flag¶m_valid_flag)
{
byte = (char*)params[i];
for (j = 0; j < 4*subparams[i][0]; j++)
chSequentialStreamPut(chp, *byte++);
chThdSleepMilliseconds(10);
}
flag = flag<<1;
}
flag = 1;
for(i = 0; i<PARAMS_NUM_MAX; i++)
{
if(flag¶m_valid_flag)
{
sendLine(chp, param_name[i]);
sendLine(chp, subparam_name[i]);
}
flag = flag<<1;
}
}
#endif
static uint8_t param_load_flash(const uint8_t param_pos, const uint8_t param_num)
{
uint8_t result = 0;
flashaddr_t address = PARAM_FLASH_ADDR + 16 + param_pos*PARAM_FLASH_BLOCK;
flashRead(address,subparams[param_pos],8);
uint8_t i;
if(subparams[param_pos][0] != param_num)
{
for(i = 1; i < 8; i++)
subparams[param_pos][i] = 0;
subparams[param_pos][0] = param_num;
result = 1;
}
flashRead(address + PARAM_FLASH_HALF_BLOCK,
(char*)(params[param_pos]), subparams[param_pos][0]*4);
for (i = 0; i < param_num; i++)
//Check the validity of the read-out value
if(params[param_pos][i] != params[param_pos][i] ||
params[param_pos][i] > 1.701411e38 ||
params[param_pos][i] < -1.701411e38)
{
params[param_pos][i] = 0.0f;
result = 2;
}
return result;
}
uint8_t params_set(param_t* const p_param,
const uint8_t param_pos,
const uint8_t param_num,
param_name_t const Param_name,
param_name_t const subParam_name,
param_public_flag_t param_private)
{
uint8_t result = 0;
//Maximum num of parameters and subparameters supported
if(param_num > 7 || param_pos >= PARAMS_NUM_MAX)
return 1;
//Check whether this position is occupied or not
if(param_valid_flag & ((uint32_t)1<<param_pos))
return 2;
else
param_valid_flag |= ((uint32_t)1<<param_pos);
params[param_pos] = p_param;
subparams[param_pos][0] = param_num;
//Read out raw data on flash, and check validity
if(param_load_flash(param_pos, param_num))
result = 3;
param_name[param_pos] = Param_name;
subparam_name[param_pos] = subParam_name;
if(param_private == PARAM_PRIVATE)
param_private_flag |= ((uint32_t)1 << param_pos);
params_total++;
subparams_total += param_num;
return result;
}
void param_save_flash(void)
{
LEDR_ON();
LEDG_OFF();
flashSectorErase(PARAM_FLASH_SECTOR);
uint8_t i;
uint32_t flag = 1;
flashaddr_t address;
for(i = 0; i< PARAMS_NUM_MAX; i++)
{
if(flag¶m_valid_flag)
{
address = PARAM_FLASH_ADDR + 16 + PARAM_FLASH_BLOCK*i;
flashWrite(address,subparams[i],8);
flashWrite(address + PARAM_FLASH_HALF_BLOCK,
(char*)(params[i]), subparams[i][0]*4);
}
flag = flag<<1;
}
LEDR_OFF();
}
void params_init(void)
{
param_valid_flag = 0;
param_private_flag = 0;
#ifdef PARAMS_USE_UART
uartStart(UART_PARAMS, &uart_cfg);
chThdCreateStatic(params_rx_wa,sizeof(params_rx_wa),
NORMALPRIO + 7,params_rx,NULL);
#endif
}