forked from robomasterhkust/SerialTunerGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.c
278 lines (238 loc) · 6.54 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
/**
* Edward ZHANG, 20171111
* @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 uint8_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];
#define RXBUF_SIZE 7
static uint8_t rxbuf[RXBUF_SIZE];
static thread_reference_t uart_receive_thread_handler = NULL;
static void uart_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, 128);
static THD_FUNCTION(params_tx,p)
{
chRegSetThreadName("param transmitter");
(void)p;
chThdSleepMilliseconds(10);
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(5);
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)
{
uart_sendLine(param_name[i]);
uart_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)
{
switch(rxbuf[0])
{
case 'p':
if((1<<rxbuf[1])¶m_valid_flag &&
rxbuf[2] < subparams[rxbuf[1]][0])
{
chSysLockFromISR();
params[rxbuf[1]][rxbuf[2]] = *((param_t*)(rxbuf+3));
chSysUnlockFromISR();
}
break;
case 's':
if((1<<rxbuf[1])¶m_valid_flag &&
rxbuf[2] < subparams[rxbuf[1]][0])
subparams[rxbuf[1]][rxbuf[2] + 1] = rxbuf[3];
break;
case 'u':
param_save_flash();
break;
case 'g':
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;
}
chSysLockFromISR();
chThdResumeI(&uart_receive_thread_handler,MSG_OK);
chSysUnlockFromISR();
}
}
/*
* UART driver configuration structure.
*/
static UARTConfig uart_cfg = {
NULL,NULL,rxend,NULL,NULL,
115200,
0,
0,
0
};
static THD_WORKING_AREA(params_rx_wa, 128);
static THD_FUNCTION(params_rx,p)
{
chRegSetThreadName("param receiver");
(void)p;
while(!chThdShouldTerminateX())
{
uartStartReceive(UART_PARAMS, RXBUF_SIZE, rxbuf);
chSysLock();
chThdSuspendS(&uart_receive_thread_handler);
chSysUnlock();
}
}
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 = 1;
}
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)
{
//Maximum num of parameters and subparameters supported
if(param_num > 7 || param_pos >= PARAMS_NUM_MAX)
return 1;
uint8_t result = 0;
//Check whether this position is occupied or not
if(param_valid_flag & (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;
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 params_init(void)
{
param_valid_flag = 0;
param_private_flag = 0;
uartStart(UART_PARAMS, &uart_cfg);
chThdCreateStatic(params_rx_wa,sizeof(params_rx_wa),
NORMALPRIO + 7,params_rx,NULL);
}
void param_save_flash(void)
{
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;
}
}