-
Notifications
You must be signed in to change notification settings - Fork 1
/
pers.cpp
executable file
·438 lines (388 loc) · 10 KB
/
pers.cpp
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
/*
* Author - Erez Raviv <erezraviv@gmail.com>
*
* Based on th9x -> http://code.google.com/p/th9x/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "er9x.h"
#include <stdlib.h>
#include "templates.h"
#include "language.h"
#ifdef FRSKY
#include "frsky.h"
#endif
static void validateName( char *name, uint8_t size ) ;
const prog_char APM Str_EEPROM_Overflow[] = STR_EE_OFLOW ;
EFile theFile; //used for any file operation
EFile theFile2; //sometimes we need two files
EFile theWriteFile; //separate write file
#define FILE_TYP_GENERAL 1
#define FILE_TYP_MODEL 2
/*#define partCopy(sizeDst,sizeSrc) \
pSrc -= (sizeSrc); \
pDst -= (sizeDst); \
memmove(pDst, pSrc, (sizeSrc)); \
memset (pDst+(sizeSrc), 0, (sizeDst)-(sizeSrc));
#define fullCopy(size) partCopy(size,size)
*/
void generalDefault()
{
memset(&g_eeGeneral,0,sizeof(g_eeGeneral));
g_eeGeneral.myVers = MDVERS;
// g_eeGeneral.currModel= 0;
g_eeGeneral.contrast = lcd_nomContrast;
g_eeGeneral.vBatWarn = 90;
g_eeGeneral.stickMode= 1;
for (uint8_t i = 0; i < 7; ++i) {
g_eeGeneral.calibMid[i] = 0x200;
g_eeGeneral.calibSpanNeg[i] = 0x300;
g_eeGeneral.calibSpanPos[i] = 0x300;
}
strncpy_P(g_eeGeneral.ownerName,PSTR(STR_ME),10);
g_eeGeneral.chkSum = evalChkSum() ;
}
uint16_t evalChkSum()
{
uint16_t sum=0;
uint16_t *p ;
p = ( uint16_t *)g_eeGeneral.calibMid ;
for (int i=0; i<12;i++)
{
sum += *p++ ;
}
return sum;
}
static bool eeLoadGeneral()
{
theFile.openRd(FILE_GENERAL);
memset(&g_eeGeneral, 0, sizeof(EEGeneral));
// uint8_t sz = theFile.readRlc((uint8_t*)&g_eeGeneral, sizeof(EEGeneral));
theFile.readRlc((uint8_t*)&g_eeGeneral, sizeof(EEGeneral));
validateName( g_eeGeneral.ownerName, sizeof(g_eeGeneral.ownerName) ) ;
// for(uint8_t i=0; i<sizeof(g_eeGeneral.ownerName);i++) // makes sure name is valid
// {
// uint8_t idx = char2idx(g_eeGeneral.ownerName[i]);
// g_eeGeneral.ownerName[i] = idx2char(idx);
// }
if(g_eeGeneral.myVers<MDVERS)
{
sysFlags |= sysFLAG_OLD_EEPROM; // if old EEPROM - Raise flag
g_eeGeneral.myVers = MDVERS; // update myvers
STORE_GENERALVARS;
}
// if(sz>(sizeof(EEGeneral)-20)) for(uint8_t i=0; i<12;i++) sum+=g_eeGeneral.calibMid[i];
return g_eeGeneral.chkSum == evalChkSum() ;
}
void modelDefaultWrite(uint8_t id)
{
memset(&g_model, 0, sizeof(ModelData));
strncpy_P(g_model.name,PSTR(STR_MODEL), 10);
div_t qr ;
qr = div( id+1, 10 ) ;
g_model.name[5]='0'+qr.quot;
g_model.name[6]='0'+qr.rem;
// g_model.mdVers = MDVERS;
#ifdef NO_TEMPLATES
applyTemplate(); //default 4 channel template
#else
applyTemplate(0); //default 4 channel template
#endif
theFile.writeRlc(FILE_MODEL(id),FILE_TYP_MODEL,(uint8_t*)&g_model,sizeof(g_model),200);
}
void eeLoadModelName(uint8_t id,char*buf,uint8_t len)
{
if(id<MAX_MODELS)
{
//eeprom_read_block(buf,(void*)modelEeOfs(id),sizeof(g_model.name));
theFile.openRd(FILE_MODEL(id));
memset(buf,' ',len);
if(theFile.readRlc((uint8_t*)buf,sizeof(g_model.name)) == sizeof(g_model.name) )
{
uint16_t sz=theFile.size();
buf+=len;
while(sz)
{
div_t qr ;
qr = div( sz, 10 ) ;
--buf;
*buf='0'+qr.rem;
sz = qr.quot;
}
}
}
}
//uint16_t eeFileSize(uint8_t id)
//{
// theFile.openRd(FILE_MODEL(id));
// return theFile.size();
//}
static void validateName( char *name, uint8_t size )
{
for(uint8_t i=0; i<size;i++) // makes sure name is valid
{
// uint8_t idx = char2idx(name[i]);
name[i] = idx2char(char2idx(name[i])) ;
}
}
bool eeModelExists(uint8_t id)
{
return EFile::exists(FILE_MODEL(id));
}
#ifdef FIX_MODE
extern MixData *mixaddress( uint8_t idx ) ;
#endif
void eeLoadModel(uint8_t id)
{
if(id<MAX_MODELS)
{
theFile.openRd(FILE_MODEL(id));
memset(&g_model, 0, sizeof(ModelData));
uint16_t sz = theFile.readRlc((uint8_t*)&g_model, sizeof(g_model));
if(sz<256) // if not loaded a fair amount
{
modelDefaultWrite(id);
}
validateName( g_model.name, sizeof(g_model.name) ) ;
// for(uint8_t i=0; i<sizeof(g_model.name);i++) // makes sure name is valid
// {
// uint8_t idx = char2idx(g_model.name[i]);
// g_model.name[i] = idx2char(idx);
// }
if ( g_model.numBlades == 0 )
{
g_model.numBlades = g_model.xnumBlades + 2 ;
}
resetTimer1();
resetTimer2();
#ifdef FRSKY
FrskyAlarmSendState |= 0x40 ; // Get RSSI Alarms
FRSKY_setModelAlarms();
#endif
#ifdef FIX_MODE
// check for updating mix sources
if ( g_model.modelVersion < 2 )
{
for(uint8_t i=0;i<MAX_MIXERS;i++)
{
MixData *md = mixaddress( i ) ;
if (md->srcRaw)
{
if (md->srcRaw <= 4) // Stick
{
md->srcRaw = modeFixValue( md->srcRaw-1 ) ;
}
}
}
for (uint8_t i = 0 ; i < NUM_CSW ; i += 1 )
{
CSwData *cs = &g_model.customSw[i];
uint8_t cstate = CS_STATE(cs->func);
uint8_t t = 0 ;
if(cstate == CS_VOFS)
{
t = 1 ;
}
else if(cstate == CS_VCOMP)
{
t = 1 ;
if (cs->v2)
{
if (cs->v2 <= 4) // Stick
{
cs->v2 = modeFixValue( cs->v2-1 ) ;
}
}
}
if ( t )
{
if (cs->v1)
{
if (cs->v1 <= 4) // Stick
{
cs->v1 = modeFixValue( cs->v1-1 ) ;
}
}
}
}
#if defined(CPUM128) || defined(CPUM2561)
for (uint8_t i = NUM_CSW ; i < NUM_CSW+EXTRA_CSW ; i += 1 )
{
CxSwData *cs = &g_model.xcustomSw[i-NUM_CSW];
uint8_t cstate = CS_STATE(cs->func);
uint8_t t = 0 ;
if(cstate == CS_VOFS)
{
t = 1 ;
}
else if(cstate == CS_VCOMP)
{
t = 1 ;
if (cs->v2)
{
if (cs->v2 <= 4) // Stick
{
cs->v2 = modeFixValue( cs->v2-1 ) ;
}
}
}
if ( t )
{
if (cs->v1)
{
if (cs->v1 <= 4) // Stick
{
cs->v1 = modeFixValue( cs->v1-1 ) ;
}
}
}
}
#endif // CPUs
alert(PSTR("CHECK MIX SOURCES"));
g_model.modelVersion = 2 ;
eeDirty( EE_MODEL ) ;
eeWaitComplete() ;
}
#endif // FIX_MODE
}
}
bool eeDuplicateModel(uint8_t id)
{
uint8_t i;
for( i=id+1; i<MAX_MODELS; i++)
{
if(! EFile::exists(FILE_MODEL(i))) break;
}
if(i==MAX_MODELS) return false; //no free space in directory left
theFile.openRd(FILE_MODEL(id));
theFile2.create(FILE_MODEL(i),FILE_TYP_MODEL,600);
uint8_t buf[15];
uint8_t l;
while((l=theFile.read(buf,15)))
{
theFile2.write(buf,l);
// if(theFile.write_errno()==ERR_TMO)
// {
// //wait for 10ms and try again
// uint16_t tgtime = get_tmr10ms() + 100;
// while (!=tgtime);
// theFile2.write(buf,l);
// }
wdt_reset();
}
theFile2.closeTrunc();
//todo error handling
return true;
}
void eeReadAll()
{
if(!EeFsOpen() ||
EeFsck() < 0 ||
!eeLoadGeneral()
)
{
alert(PSTR(STR_BAD_EEPROM), true);
message(PSTR(STR_EE_FORMAT));
EeFsFormat();
//alert(PSTR("format ok"));
generalDefault();
// alert(PSTR("default ok"));
uint16_t sz = theFile.writeRlc(FILE_GENERAL,FILE_TYP_GENERAL,(uint8_t*)&g_eeGeneral,sizeof(EEGeneral),200);
if(sz!=sizeof(EEGeneral)) alert(PSTR(STR_GENWR_ERROR));
modelDefaultWrite(0);
//alert(PSTR("modef ok"));
//alert(PSTR("modwrite ok"));
}
eeLoadModel(g_eeGeneral.currModel);
}
static uint8_t s_eeDirtyMsk;
static uint16_t s_eeDirtyTime10ms;
void eeDirty(uint8_t msk)
{
if(!msk) return;
s_eeDirtyMsk |= msk;
s_eeDirtyTime10ms = get_tmr10ms() ;
}
#define WRITE_DELAY_10MS 100
uint8_t Ee_lock = 0 ;
extern uint8_t heartbeat;
void eeWaitComplete()
{
while(s_eeDirtyMsk)
{
eeCheck(true) ;
if(heartbeat == 0x3)
{
wdt_reset();
heartbeat = 0;
}
}
}
void eeCheck(bool immediately)
{
uint8_t msk = s_eeDirtyMsk;
if(!msk) return;
if( !immediately && (( get_tmr10ms() - s_eeDirtyTime10ms) < WRITE_DELAY_10MS)) return;
if ( Ee_lock ) return ;
Ee_lock = EE_LOCK ; // Lock eeprom writing from recursion
if ( msk & EE_TRIM )
{
Ee_lock |= EE_TRIM_LOCK ; // So the lower levels know what is happening
}
s_eeDirtyMsk = 0;
if(msk & EE_GENERAL){
if(theWriteFile.writeRlc(FILE_TMP, FILE_TYP_GENERAL, (uint8_t*)&g_eeGeneral,
sizeof(EEGeneral),20) == sizeof(EEGeneral))
{
EFile::swap(FILE_GENERAL,FILE_TMP);
}else{
if(theWriteFile.write_errno()==ERR_TMO){
s_eeDirtyMsk |= EE_GENERAL; //try again
s_eeDirtyTime10ms = get_tmr10ms() - WRITE_DELAY_10MS;
if(heartbeat == 0x3)
{
wdt_reset();
heartbeat = 0;
}
}else{
alert(Str_EEPROM_Overflow);
}
}
//first finish GENERAL, then MODEL !!avoid Toggle effect
}
else if(msk & EE_MODEL){
if(theWriteFile.writeRlc(FILE_TMP, FILE_TYP_MODEL, (uint8_t*)&g_model,
sizeof(g_model),20) == sizeof(g_model))
{
EFile::swap(FILE_MODEL(g_eeGeneral.currModel),FILE_TMP);
}else{
if(theWriteFile.write_errno()==ERR_TMO){
s_eeDirtyMsk |= EE_MODEL; //try again
if ( msk & EE_TRIM )
{
s_eeDirtyMsk |= EE_TRIM; //try again
}
s_eeDirtyTime10ms = get_tmr10ms() - WRITE_DELAY_10MS;
if(heartbeat == 0x3)
{
wdt_reset();
heartbeat = 0;
}
}else{
if ( ( msk & EE_TRIM ) == 0 ) // Don't stop if trim adjust
{
alert(Str_EEPROM_Overflow);
}
}
}
}
Ee_lock = 0 ; // UnLock eeprom writing
}