-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFRAM_RINGBUFFER.cpp
More file actions
186 lines (147 loc) · 3.39 KB
/
FRAM_RINGBUFFER.cpp
File metadata and controls
186 lines (147 loc) · 3.39 KB
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
//
// FILE: FRAM_RINGBUFFER.h
// AUTHOR: Rob Tillaart
// DATE: 2022-10-03
// PURPOSE: Arduino library for I2C FRAM based RING BUFFER
// URL: https://github.com/RobTillaart/FRAM_I2C
//
#include "FRAM_RINGBUFFER.h"
FRAM_RINGBUFFER::FRAM_RINGBUFFER()
{
}
uint32_t FRAM_RINGBUFFER::begin(FRAM *fram, uint32_t size, uint32_t start)
{
_fram = fram;
_size = size;
_start = start + 20; // allocate 5 uint32_t for storage.
flush();
_saved = false;
return _start + _size; // first free FRAM location.
}
//////////////////////////////////////////////////////////////////
//
// ADMINISTRATIVE
//
void FRAM_RINGBUFFER::flush()
{
_front = _tail = _start;
_count = 0;
_saved = false;
}
uint32_t FRAM_RINGBUFFER::size()
{
return _size;
}
uint32_t FRAM_RINGBUFFER::count()
{
return _count;
}
bool FRAM_RINGBUFFER::full()
{
return _count == _size;
}
bool FRAM_RINGBUFFER::empty()
{
return _count == 0;
}
uint32_t FRAM_RINGBUFFER::free()
{
return _size - _count;
}
float FRAM_RINGBUFFER::freePercent()
{
return (100.0 * _count) / _size;
}
// DEBUG
// uint32_t FRAM_RINGBUFFER::tail() { return _tail; };
// uint32_t FRAM_RINGBUFFER::front() { return _front; };
//////////////////////////////////////////////////////////////////
//
// BYTE INTERFACE
//
int FRAM_RINGBUFFER::write(uint8_t value)
{
if (full()) return FRAM_RB_ERR_BUF_FULL;
_fram->write8(_front, value);
_saved = false;
_front++;
_count++;
if (_front >= _start + _size) _front = _start;
return 1;
}
int FRAM_RINGBUFFER::read()
{
if (empty()) return FRAM_RB_ERR_BUF_EMPTY;
int value = _fram->read8(_tail);
_saved = false;
_tail++;
_count--;
if (_tail >= _start + _size) _tail = _start;
return value;
}
int FRAM_RINGBUFFER::peek()
{
if (empty()) return FRAM_RB_ERR_BUF_EMPTY;
int value = _fram->read8(_tail);
return value;
}
///////////////////////////////////////////////////
//
// MAKE RINGBUFFER PERSISTENT OVER REBOOTS
//
bool FRAM_RINGBUFFER::isSaved()
{
return _saved;
}
void FRAM_RINGBUFFER::save()
{
uint32_t pos = _start - 20;
if (not _saved)
{
uint32_t checksum = _size + _front + _tail + _count;
_fram->write32(pos + 0, _size );
_fram->write32(pos + 4, _front);
_fram->write32(pos + 8, _tail );
_fram->write32(pos + 12, _count);
_fram->write32(pos + 16, checksum);
_saved = true;
}
}
bool FRAM_RINGBUFFER::load()
{
uint32_t pos = _start - 20;
uint32_t size = _fram->read32(pos + 0);
uint32_t front = _fram->read32(pos + 4);
uint32_t tail = _fram->read32(pos + 8);
uint32_t count = _fram->read32(pos + 12);
uint32_t checksum = _fram->read32(pos + 16);
// checksum test should be enough.
// optional these are possible
// (_start <= _front) && (_front < _start + _size);
// (_start <= _tail) && (_tail < _start + _size);
_saved = (checksum == size + front + tail + count);
if (_saved)
{
_size = size;
_front = front;
_tail = tail;
_count = count;
}
return _saved;
}
void FRAM_RINGBUFFER::wipe()
{
uint32_t pos = _start - 20; // also overwrite metadata
while (pos < _start + _size - 4) // prevent writing adjacent FRAM
{
_fram->write32(pos, 0xFFFFFFFF);
pos += 4;
}
while (pos < _start + _size) // if _size not a multiple of 4.
{
_fram->write8(pos, 0xFF);
pos++;
}
flush(); // reset internal variables too.
}
// -- END OF FILE --