-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathSAMDNVMSettingsStorage.cpp
188 lines (148 loc) · 4.86 KB
/
SAMDNVMSettingsStorage.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
#include "./SAMDNVMSettingsStorage.h"
#if defined(_SAMD21_)
#include "communication/SimpleFOCDebug.h"
SAMDNVMSettingsStorage::SAMDNVMSettingsStorage(uint32_t offset) {
_offset = offset;
};
SAMDNVMSettingsStorage::~SAMDNVMSettingsStorage(){};
void SAMDNVMSettingsStorage::init(){
SettingsStorage::init(this);
reset();
};
void SAMDNVMSettingsStorage::reset(){
_currptr = (uint8_t*) SIMPLEFOC_SAMDNVMSETTINGS_BASE + _offset; // add offset to NVM base address
};
void SAMDNVMSettingsStorage::beforeLoad(){
reset();
};
void SAMDNVMSettingsStorage::beforeSave(){
reset();
_writeIndex = 0;
// set manual write mode
_ctlB = NVMCTRL->CTRLB.reg;
NVMCTRL->CTRLB.reg |= NVMCTRL_CTRLB_MANW;
while (NVMCTRL->INTFLAG.bit.READY == 0) {};
// unlock region - user can do it with fuses
// ret_status_code = nvm_execute_command(NVM_COMMAND_UNLOCK_REGION, SIMPLEFOC_SAMDNVMSETTINGS_BASE, 0);
// if (ret_status_code != STATUS_OK)
// SimpleFOCDebug::println("NVM unlock error ", ret_status_code);
// SimpleFOCDebug::println("NVM lock: ", NVMCTRL->LOCK.reg);
// erase rows
// TODO handle case that it is more than one row
uint8_t cmd;
if (SIMPLEFOC_SAMDNVMSETTINGS_BASE >= RWWEE_BASE) {
cmd = 0x1A;
NVMCTRL->ADDR.reg = ((uint32_t)_currptr - SIMPLEFOC_SAMDNVMSETTINGS_BASE) / 2;
}
else {
cmd = NVMCTRL_CTRLA_CMD_ER;
NVMCTRL->ADDR.reg = ((uint32_t)_currptr) / 2;
}
NVMCTRL->STATUS.reg |= NVMCTRL_STATUS_MASK;
NVMCTRL->INTFLAG.bit.ERROR = 0;
NVMCTRL->CTRLA.reg = cmd | NVMCTRL_CTRLA_CMDEX_KEY;
delay(1);
while (NVMCTRL->INTFLAG.bit.READY == 0) {};
if (NVMCTRL->INTFLAG.bit.ERROR == 1)
SimpleFOCDebug::println("NVM erase error ", NVMCTRL->STATUS.reg);
else
SimpleFOCDebug::println("NVM erased row @", (int)_currptr);
};
void SAMDNVMSettingsStorage::afterSave() {
if (_writeIndex>0)
flushPage();
// restore ctlb
delay(1);
NVMCTRL->CTRLB.reg =_ctlB;
while (NVMCTRL->INTFLAG.bit.READY == 0) {};
}
void SAMDNVMSettingsStorage::flushPage(){
// if we have an odd number of bytes, pad with 0xFF
if (_writeIndex%2==1) {
_writeBuffer[_writeIndex++] = 0xFF;
}
// erase page buffer
// NVMCTRL->ADDR.reg = ((uint32_t)_currptr - SIMPLEFOC_SAMDNVMSETTINGS_BASE) / 2;
// NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMD_PBC | NVMCTRL_CTRLA_CMDEX_KEY;
// while (NVMCTRL->INTFLAG.bit.READY == 0) {};
// copy writeBuffer to NVM, using 16-bit writes
uint16_t* src = (uint16_t*)_writeBuffer;
uint16_t* dst = (uint16_t*)_currptr;
for (int i=0;i<_writeIndex/2;i++) {
*dst++ = *src++;
}
// write page
uint8_t cmd;
if (SIMPLEFOC_SAMDNVMSETTINGS_BASE >= RWWEE_BASE) {
cmd = 0x1C;
NVMCTRL->ADDR.reg = ((uint32_t)_currptr - SIMPLEFOC_SAMDNVMSETTINGS_BASE) / 2;
}
else {
cmd = NVMCTRL_CTRLA_CMD_WP;
NVMCTRL->ADDR.reg = ((uint32_t)_currptr) / 2;
}
NVMCTRL->STATUS.reg |= NVMCTRL_STATUS_MASK;
NVMCTRL->INTFLAG.bit.ERROR = 0;
NVMCTRL->CTRLA.reg = cmd | NVMCTRL_CTRLA_CMDEX_KEY;
delay(1);
while (NVMCTRL->INTFLAG.bit.READY == 0) {};
if (NVMCTRL->INTFLAG.bit.ERROR == 1)
SimpleFOCDebug::println("NVM write error ", NVMCTRL->STATUS.reg);
else
SimpleFOCDebug::println("NVM wrote page @", (int)_currptr);
// reset writeBuffer pointer and advance currptr to next page
_writeIndex = 0;
_currptr+=NVMCTRL_PAGE_SIZE;
}
int SAMDNVMSettingsStorage::readBytes(void* valueToSet, int numBytes) {
uint8_t* bytes = (uint8_t*)valueToSet;
for (int i=0;i<numBytes;i++) {
uint8_t val = *_currptr++;
bytes[i] = val;
}
return numBytes;
}
int SAMDNVMSettingsStorage::writeBytes(void* value, int numBytes) {
uint8_t* bytes = (uint8_t*)value;
for (int i=0;i<numBytes;i++) {
_writeBuffer[_writeIndex] = *bytes++;
_writeIndex++;
if (_writeIndex==NVMCTRL_PAGE_SIZE)
flushPage();
}
return numBytes;
}
RegisterIO& SAMDNVMSettingsStorage::operator>>(uint8_t& value){
uint8_t val;
uint8_t num = readBytes(&val, 1);
if (num==1)
value = val;
return *this;
};
RegisterIO& SAMDNVMSettingsStorage::operator>>(float& value){
float val;
uint8_t num = readBytes(&val, 4);
if (num==4)
value = val;
return *this;
};
RegisterIO& SAMDNVMSettingsStorage::operator>>(uint32_t& value){
uint32_t val;
uint8_t num = readBytes(&val, 4);
if (num==4)
value = val;
return *this;
};
RegisterIO& SAMDNVMSettingsStorage::operator<<(uint8_t value){
writeBytes(&value, 1);
return *this;
};
RegisterIO& SAMDNVMSettingsStorage::operator<<(float value){
writeBytes(&value, 4);
return *this;
};
RegisterIO& SAMDNVMSettingsStorage::operator<<(uint32_t value){
writeBytes(&value, 4);
return *this;
};
#endif