-
Notifications
You must be signed in to change notification settings - Fork 13
/
nvm.cpp
98 lines (89 loc) · 2.51 KB
/
nvm.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
#include "EEPROM.h"
#include "nvm.h"
#include "nvm_default.h"
#include "cpuID.h"
#define HASH 0x456F
#define SLOTS_PER_PIN 20
extern void *eeprom_begin; // exported by linker
/**
*
*/
class NVMeeprom : public EEPROMClass
{
public:
NVMeeprom()
{
uint32_t flahSize=cpuID::getFlashSize();
uint32 pageBase0=(uint32_t)&eeprom_begin;
EEPROMClass::init(pageBase0,pageBase0+0x800,0x800);
}
};
bool NVM::loaded=false;
/**
*
* @param pin
* @param calibration
* @return
*/
bool NVM::hasCalibration()
{
NVMeeprom eep;
#if 1
if(eep.read(0)==HASH)
return true;
#endif
return false;
}
bool NVM::loadTestPin(int pin, TestPinCalibration &calibration)
{
NVMeeprom eep;
int calibrationHash=eep.read(0);
if(calibrationHash != HASH)
{
calibration.resUp =WIRE_RESISTANCE_AND_INTERNAL;
calibration.resDown =WIRE_RESISTANCE_AND_INTERNAL;
calibration.capOffsetInPf =INTERNAL_CAPACITANCE_IN_PF;
for(int i=0;i<CALIBRATION_VERY_SMALL_SIZE;i++)
calibration.capOffsetHighInPfMu16[i] =0;
calibration.inductanceInUF =INTERNAL_INDUCTANCE_IN_UF;
return true; // default value
}
calibration.resUp= eep.read(SLOTS_PER_PIN*pin+1+0);
calibration.resDown= eep.read(SLOTS_PER_PIN*pin+1+1);;
calibration.capOffsetInPf= eep.read(SLOTS_PER_PIN*pin+1+2)+1;; // there is a ~ 3/4 pf Error
calibration.inductanceInUF= eep.read(SLOTS_PER_PIN*pin+1+3);;
for(int i=0;i<CALIBRATION_VERY_SMALL_SIZE;i++)
calibration.capOffsetHighInPfMu16[i]= eep.read(SLOTS_PER_PIN*pin+5+i);;
//calibration.capOffsetHighInPfMu16[0]=16.*40.2;
return true;
}
/**
*
* @param pin
* @param calibration
* @return
*/
bool NVM::saveTestPin(int pin, const TestPinCalibration &calibration)
{
NVMeeprom eep;
eep.write(SLOTS_PER_PIN*pin+1+0, calibration.resUp);
eep.write(SLOTS_PER_PIN*pin+1+1, calibration.resDown);;
eep.write(SLOTS_PER_PIN*pin+1+2,calibration.capOffsetInPf);;
eep.write(SLOTS_PER_PIN*pin+1+3,calibration.inductanceInUF);;
for(int i=0;i<CALIBRATION_VERY_SMALL_SIZE;i++)
eep.write(SLOTS_PER_PIN*pin+5+i,calibration.capOffsetHighInPfMu16[i]);;
return true;
}
bool NVM::reset()
{
NVMeeprom eep;
eep.format();
return true;
}
bool NVM::doneWriting()
{
NVMeeprom eep;
eep.write(0,HASH);
return true;
}
// EOF