-
Notifications
You must be signed in to change notification settings - Fork 5
/
counter.cpp
196 lines (169 loc) · 5.03 KB
/
counter.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
#include <Arduino.h>
#include <spi_flash.h>
#include "counter.h"
const uint32_t HEADER_ID = 0x52425300;
const uint32_t HEADER_SIZE = 2 * sizeof(uint32_t);
const uint32_t EMPTY_BIT_PATTERN = 0xffffffff;
const int READ_BUFFER_SIZE = 64;
//#define PRINT(MSG) Serial.print(MSG)
//#define PRINTLN(MSG) Serial.println(MSG)
//#define PRINTNUMLN(NUM) Serial.println(NUM)
//#define PRINT(MSG) printf(MSG)
//#define PRINTLN(MSG) printf("%s\n",MSG)
//#define PRINTNUMLN(NUM) printf("%d\n",NUM)
#define PRINT(MSG)
#define PRINTLN(MSG)
#define PRINTNUMLN(NUM)
uint32_t Counter::countBits(uint32_t value)
{
uint32_t result = 0;
// Special case -> return directly, if all bits are cleared.
if (value == 0UL) {
return 32;
}
// Otherwise count the *cleared* bits.
for (int i = 0; i < 32; ++i) {
result += (1 - (value & 1));
value >>= 1;
}
return result;
}
bool Counter::incrementBits(uint32_t & value)
{
uint32_t bitMask = 1;
for (int i = 0; i < 32; ++i) {
if (value & bitMask) {
value = value & ~bitMask;
// One bit was cleared, no overflow.
return false;
}
bitMask <<= 1;
}
// Overflow
return true;
}
Counter::Counter() : sectorSize(0), blockStart{ 0 }, currentBits(0), currentValue(0), activeBlock(0), blockOffset(0), initalized(false)
{ }
void Counter::init(uint16_t sector, uint32_t sectorSize)
{
this->sectorSize = sectorSize;
blockStart[0] = sector * sectorSize;
blockStart[1] = blockStart[0] + sectorSize;
initFlash();
info();
}
void Counter::info() {
PRINT("Active buffer: ");
PRINTNUMLN(activeBlock);
PRINT("Current offset: ");
PRINTNUMLN(blockOffset);
PRINT("Current value : ");
PRINTNUMLN(currentValue);
PRINT("Current bits : ");
PRINTNUMLN(currentBits);
}
void Counter::initFlash()
{
uint32_t header[2][2];
// Check if blocks contain valid headers ...
for (int i = 0; i < 2; ++i) {
SpiFlashOpResult result = spi_flash_read(blockStart[i], header[i], HEADER_SIZE);
if (result != SPI_FLASH_RESULT_OK) {
PRINTLN("ERROR: initFlash: Couldn't read header!");
return;
}
}
if ((header[0][0] != HEADER_ID) || (header[1][0] != HEADER_ID)) {
// No, erase blocks ...
PRINTLN("Initializing blocks ...");
// Somehow we have to mark the active buffer. This is done be checking the offset at the beginning.
// The buffer with the highest offset is active.
initializeBlock(0, 0);
header[0][1] = 0;
initializeBlock(1, 0);
header[1][1] = 0;
}
activeBlock = header[0][1] > header[1][1] ? 0 : 1;
restoreCounter(header[activeBlock][1]);
}
void Counter::initializeBlock(int blockId, uint32_t startValue) {
SpiFlashOpResult result = spi_flash_erase_sector(blockStart[blockId] / sectorSize);
if (result != SPI_FLASH_RESULT_OK) {
PRINTLN("ERROR: initializeBlock: Erase failed!");
return;
}
uint32_t header[2];
header[0] = HEADER_ID;
header[1] = startValue;
result = spi_flash_write(blockStart[blockId], header, HEADER_SIZE);
if (result != SPI_FLASH_RESULT_OK) {
PRINTLN("ERROR: initializeBlock: Write failed!");
return;
}
}
void Counter::restoreCounter(uint32_t startValue)
{
currentValue = startValue;
blockOffset = HEADER_SIZE;
uint32_t counterBits[READ_BUFFER_SIZE];
uint32_t *pCurrentBits = counterBits;
while (blockOffset < sectorSize) {
SpiFlashOpResult result = spi_flash_read(blockStart[activeBlock] + blockOffset, counterBits, sizeof(counterBits));
if (result != SPI_FLASH_RESULT_OK) {
PRINTLN("ERROR: restoreCounter: Could not read flash!");
return;
}
pCurrentBits = counterBits;
for (int i = 0; i < READ_BUFFER_SIZE; ++i) {
if (blockOffset >= sectorSize) {
break;
}
if (*pCurrentBits == EMPTY_BIT_PATTERN) {
if (pCurrentBits != counterBits) {
blockOffset -= sizeof(uint32_t);
--pCurrentBits;
}
goto restoreFinished;
}
currentValue += countBits(*(pCurrentBits++));
blockOffset += sizeof(uint32_t);
}
}
restoreFinished:
currentBits = *pCurrentBits;
initalized = true;
}
void Counter::switchBlock()
{
activeBlock = (activeBlock + 1) % 2;
initializeBlock(activeBlock, currentValue);
blockOffset = HEADER_SIZE;
}
void Counter::increment()
{
if (initalized) {
bool overflow = incrementBits(currentBits);
if (overflow) {
blockOffset += sizeof(uint32_t);
if (blockOffset >= sectorSize) {
switchBlock();
}
currentBits = EMPTY_BIT_PATTERN;
incrementBits(currentBits);
}
if (spi_flash_write(blockStart[activeBlock] + blockOffset, ¤tBits, sizeof(uint32_t)) == SPI_FLASH_RESULT_OK) {
//PRINTLN("Counter flashed.");
++currentValue;
}
else {
PRINTLN("ERROR: increment: Could not write!");
}
}
}
uint32_t Counter::get()
{
if (initalized) {
return currentValue;
}
return 0x0;
}