-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataBackup.cpp
306 lines (277 loc) · 9.53 KB
/
DataBackup.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
/*
MIT License
Copyright (c) 2017 Ricardo Alcaraz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*Made for Atmega32u4 */
#include "DataBackup.h"
#include "Arduino.h"
#include "FastCRC_tables.h"
//Constructors
template <class T>
DataBackup<T>::DataBackup(const T &data) {
this->partitions = 1;
this->data_address = &data;
this->data_size = sizeof(T);
this->signature = 0b01011010;
}
/*Returns 8 bit CRC of the data
*Inputs: None
*Outputs: uint8 - 8 bit crc
*/
template <class T>
uint8_t DataBackup<T>::getCRC8() {
return key.smbus( (const uint8_t*) data_address, (const uint16_t) data_size );
}
/* Function to backup generic object to EEPROM
* Inputs: uint16 - index
* Outputs: none
*/
template <class T>
uint16_t DataBackup<T>::EEPROMBackup( uint16_t index ) {
/*Initializing and grabbing the value of the first byte and placing it into a variable*/
uint8_t *ptr = (uint8_t*) &data_address;
/*This loop will run until it reaches the end of the structure in memory*/
for(int count = 0; count < data_size; count++){
/*We save each byte individually to the EEPROM*/
EEPROM_write(index, *ptr);
/*Increment the pointer and index*/
*ptr++;
index++;
}
return index;
}
template <class T>
T DataBackup<T>::EEPROMRestore(const T &data, uint16_t index) {
/*Initializing and grabbing the value of the first byte and placing it into a variable*/
uint8_t *ptr = (uint8_t*) &data;
/*This loop will run until it reaches the end of the structure in memory*/
for(int count = 0; count < data_size; count++){
/*Save EEPROM data to the pointer we initialized*/
*ptr = EEPROM_read(index);
/*Increment the pointer and index*/
*ptr++;
index++;
}
return data;
}
/*Function to backup generic object data with CRC and a signature
*Inputs:None
*Outputs:None
*/
template <class T>
void DataBackup<T>::lowPowerBackup() {
uint16_t index = 0;
//Writing the low power backup signature to the first byte of the EEPROM
EEPROM_write(index, signature); index++;
//Backing up the data
index = EEPROMBackup( index );
//Writing CRC value to last value in memory
uint8_t crc = ~(getCRC8());
EEPROM_write( ++index, crc );
}
/*Function to backup generic object data with CRC and a signature
*Inputs:None
*Outputs:None
*/
template <class T>
T DataBackup<T>::lowPowerRestore() {
uint16_t index = 0;
T temp;
//Writing the low power backup signature to the first byte of the EEPROM
uint8_t signature_check = EEPROM_read(0, signature);
if( signature_check == signature ){
T temp = EEPROMRestore( temp, 1 );
}
uint8_t temp_crc = key.smbus( (const uint8_t*) &temp, sizeof(T) );
uint8_t crc = EEPROM_read( data_size + 1 );
if( temp_crc & crc == 0 ) {
return temp;
} else {
return;
}
}
/*
* Write data to EEPROM, NOTE: interrupts are disabled while writing
* @param uiAddress 16 bit interger pointing to the address of the data to write
* @param ucData 8 bit value signifying the data being written
*/
template <class T>
void DataBackup<T>::EEPROM_write(uint16_t uiAddress, uint8_t ucData) {
/*Store SREG value before we disable Interrupts*/
char SREG_save = SREG;
noInterrupts();
/* Wait for completion of any Flash Write
Note:Only necessary if Flash Memory Manipulation is taking place */
while(SPMCSR &(1<<SPMEN));
/* Wait for completion of previous write */
while(EECR & (1<<EEPE));
/* Set up address and Data Registers */
EEAR = uiAddress;
EEDR = ucData;
/* Write logical one to EEMPE */
EECR |= (1<<EEMPE);
/* Start eeprom write by setting EEPE */
EECR |= (1<<EEPE);
/*Restore the SREG value*/
SREG = SREG_save;
}
/*
* Read data from EEPROM, NOTE: interrupts are disabled while writing
* @param uiAddress 16 bit interger pointing to the address of the data to read
* @return 8 bit value signifying the data that was read
*/
template <class T>
uint8_t DataBackup<T>::EEPROM_read(uint16_t uiAddress) {
/*Store SREG value before we disable Interrupts*/
char SREG_save = SREG;
noInterrupts();
/* Wait for completion of any Flash Write
Note:Only necessary if Flash Memory Manipulation is taking place */
while(SPMCSR &(1<<SPMEN));
/* Wait for completion of previous write */
while(EECR & (1<<EEPE));
/* Set up address register */
EEAR = uiAddress;
/* Start eeprom read by writing EERE */
EECR |= (1<<EERE);
/*Restore the SREG value*/
SREG = SREG_save;
/* Return data from Data Register */
return EEDR;
}
/* Function to setup the ADC as a voltage reader
* Sets the ADC inputs to be a bandgap reference and the VCC
* INPUTS: None
* OUTPUTS: None
*/
template <class T>
void DataBackup<T>::vccReadSetup(){
//We want 0b011110 for the mux bits and 0b10 for the Voltage ref bits
/*Setting voltage refereance to Vcc and ensuring a right adjust*/
ADMUX &= ~( (1<<REFS1) & (1<<ADLAR) & (1<<MUX0) );
/*Setting the values for our bandgap reference input*/
ADMUX |= (1<<REFS0) | (1<<MUX4) | (1<<MUX3) | (1<<MUX2) | (1<<MUX1);
ADCSRB &= ~( (1<<MUX5) );
/*Setting the prescaler to clock/128*/
ADCSRA |= ( (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) );
/*ADC needs a 1ms settling time before the ADC is ready to be used*/
delay(1);
}
/* Reads the Vcc voltage by comparing the Vcc input with the bandgap reference
* Answer is multiplied by 10 to simplify calculations
* INPUTS: None
* @return vcc10 - Vcc*10;
*/
template <class T>
uint8_t DataBackup<T>::readVcc(){
/*pushing the ADC registers to ensure we don't mess anything up*/
uint8_t ADCA_backup = ADCSRA;
uint8_t ADCB_backup = ADCSRB;
uint8_t ADMUX_backup = ADMUX;
vccReadSetup();
/*Start Conversion*/
ADCSRA |= (1<<ADSC);
/*Wait for conversion to be ready*/
while( ADCSRA & (1<<ADSC) );
/*Read the results and concatenate it into a 16 bit value*/
uint8_t low = ADCL;
uint8_t high = ADCH;
uint16_t adc = (high << 8) | low;
/*Vcc = (1.1V * 1024) / ADC*/
/*Multiply by 10 to simplify math*/
/*Vcc10 = (11V * 1024)/ADC */
uint8_t vcc10 = (uint8_t) ( (11*1024) / adc );
/*Popping the register backups*/
ADMUX = ADMUX_backup;
ADCSRB = ADCB_backup;
ADCSRA = ADCA_backup;
/*The data sheet recommends turning the ADC off when not in use when running on battery power
*The ADC does not automatically turn off when it goes into sleep modes*/
return vcc10;
}
/* FastCRC library code is placed under the MIT license
* Copyright (c) 2014,2015 Frank Bösing
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// ================= 8-BIT CRC ===================
/** Constructor
*/
FastCRC8::FastCRC8(){}
/** SMBUS CRC
* aka CRC-8
* @param data Pointer to Data
* @param datalen Length of Data
* @return CRC value
*/
uint8_t FastCRC8::smbus_upd(const uint8_t *data, uint16_t datalen)
{
uint8_t crc = seed;
if (datalen) do {
crc = pgm_read_byte(&crc_table_smbus[crc ^ *data]);
data++;
} while (--datalen);
seed = crc;
return crc;
}
uint8_t FastCRC8::smbus(const uint8_t *data, const uint16_t datalen)
{
// poly=0x07 init=0x00 refin=false refout=false xorout=0x00 check=0xf4
seed = 0x00;
return smbus_upd(data, datalen);
}
/** MAXIM 8-Bit CRC
* equivalent to _crc_ibutton_update() in crc16.h from avr_libc
* @param data Pointer to Data
* @param datalen Length of Data
* @return CRC value
*/
uint8_t FastCRC8::maxim_upd(const uint8_t *data, uint16_t datalen)
{
uint8_t crc = seed;
if (datalen) do {
crc = pgm_read_byte(&crc_table_maxim[crc ^ *data]);
data++;
} while (--datalen);
seed = crc;
return crc;
}
uint8_t FastCRC8::maxim(const uint8_t *data, const uint16_t datalen)
{
// poly=0x31 init=0x00 refin=true refout=true xorout=0x00 check=0xa1
seed = 0x00;
return maxim_upd(data, datalen);
}