Skip to content

Commit d630465

Browse files
committed
Wrapper for externally-hold eeprom variables
1 parent 0214199 commit d630465

File tree

5 files changed

+71
-36
lines changed

5 files changed

+71
-36
lines changed

examples/all_targets/EEPROM/EEPROM.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
using namespace hal;
44

5-
static EepromStorage<int> eepromVariable;
5+
int EEMEM eepromVariable_storage = 10;
6+
EepromWrapper<int> eepromVariable{eepromVariable_storage};
67

78
int main() {
89
Serial0.init(115200);

hal/periph/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ file(GLOB_RECURSE HAL_PERIPH_SOURCES
66

77
add_library(${NAME} STATIC
88
${HAL_PERIPH_SOURCES}
9-
MCU/eeprom.cpp
109
)
1110

1211

hal/periph/MCU/eeprom.cpp

Lines changed: 0 additions & 3 deletions
This file was deleted.

hal/periph/MCU/eeprom.h

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,61 @@
77

88
namespace hal {
99

10-
namespace details {
11-
class AddressCounter {
12-
public:
13-
static size_t getAndIncreaseBy(size_t c) {
14-
auto now = counter;
15-
counter += c;
16-
return now;
17-
}
18-
19-
private:
20-
static size_t counter;
21-
};
22-
23-
} // namespace details
24-
10+
/*!
11+
* EEPROM memory wrapper.
12+
* For given type T and object in eeprom memory it will ease access to
13+
* underlying type.
14+
* @tparam T Type of underlying object
15+
*/
2516
template<typename T>
26-
class EepromStorage : details::AddressCounter {
17+
class EepromWrapper {
2718
public:
28-
constexpr EepromStorage()
29-
: eeprom_ptr(get_pointer_from_addess(getAndIncreaseBy(size))) {
19+
/*!
20+
* Constructor. Provide object defined with EEMEM attribute (placed in
21+
* EEPROM memory).
22+
* @param data_eeprom Object to wrap
23+
*/
24+
explicit constexpr EepromWrapper(T& data_eeprom)
25+
: eeprom_ptr{&data_eeprom} {
3026
}
3127

28+
/*!
29+
* Read data from EEPROM.
30+
* @return Data read from EEPROM.
31+
*/
3232
T read() const {
3333
T data;
34-
this->read(gsl::make_span(&data, size));
34+
read(gsl::make_span(&data, size));
3535
return data;
3636
}
3737

38+
/*!
39+
* Read data from EEPROM. Allows reads without .read() method
40+
* @return Data read from EEPROM.
41+
*/
3842
operator T() const {
3943
return this->read();
4044
}
4145

46+
/*!
47+
* Write data to EEPROM object.
48+
* @param data Data to write
49+
*/
4250
void write(const T& data) const {
43-
this->write(gsl::make_span(&data, size));
51+
write(gsl::make_span(&data, size));
4452
}
4553

54+
/*!
55+
* Write data to EEPROM object.
56+
* @param data Data to write
57+
*/
4658
void operator=(const T& data) const {
4759
this->write(data);
4860
}
4961

5062
private:
51-
void* get_pointer_from_addess(size_t address) {
52-
return reinterpret_cast<void*>(address);
53-
}
54-
5563
void read(gsl::span<T> data_out) const {
56-
eeprom_read_block(static_cast<void*>(data_out.begin()), eeprom_ptr, size);
64+
eeprom_read_block(data_out.begin(), eeprom_ptr, size);
5765
}
5866
void write(gsl::span<const T> data_in) const {
5967
eeprom_write_block(data_in.begin(), eeprom_ptr, size);

unit_tests/periph/eeprom.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,35 @@
44

55
using namespace hal;
66

7-
static EepromStorage<bool> var_bool;
8-
static EepromStorage<char> var_char;
9-
static EepromStorage<float> var_float;
10-
static EepromStorage<int16_t> var_int16;
11-
static EepromStorage<uint16_t> var_uint16;
12-
static EepromStorage<int32_t> var_int32;
7+
bool EEMEM eeprom_var_bool{true};
8+
EepromWrapper<bool> var_bool{eeprom_var_bool};
9+
10+
char EEMEM eeprom_var_char{0x76};
11+
EepromWrapper<char> var_char{eeprom_var_char};
12+
13+
float EEMEM eeprom_var_float{-123.48174};
14+
EepromWrapper<float> var_float{eeprom_var_float};
15+
16+
int16_t EEMEM eeprom_var_int16{-4365};
17+
EepromWrapper<int16_t> var_int16{eeprom_var_int16};
18+
19+
uint16_t EEMEM eeprom_var_uint16{64123};
20+
EepromWrapper<uint16_t> var_uint16{eeprom_var_uint16};
21+
22+
int32_t EEMEM eeprom_var_int32{0x12FFFFFF};
23+
EepromWrapper<int32_t> var_int32{eeprom_var_int32};
1324

1425
TEST_GROUP(eeprom);
1526

27+
TEST(eeprom, default_values) {
28+
TEST_ASSERT_EQUAL(true, var_bool);
29+
TEST_ASSERT_EQUAL(0x76, var_char);
30+
TEST_ASSERT_EQUAL_FLOAT(-123.48174, var_float);
31+
TEST_ASSERT_EQUAL(-4365, var_int16);
32+
TEST_ASSERT_EQUAL(64123, var_uint16);
33+
TEST_ASSERT_EQUAL(0x12FFFFFF, var_int32);
34+
}
35+
1636
TEST(eeprom, simple) {
1737
var_bool = false;
1838
var_char = 127;
@@ -42,3 +62,13 @@ TEST(eeprom, simple) {
4262
TEST_ASSERT_EQUAL(var_uint16, 0);
4363
TEST_ASSERT_EQUAL(var_int32, 458898752L);
4464
}
65+
66+
TEST(eeprom, read_write) {
67+
var_bool.write(false);
68+
69+
TEST_ASSERT_EQUAL(var_bool.read(), false);
70+
71+
var_bool.write(true);
72+
73+
TEST_ASSERT_EQUAL(var_bool.read(), true);
74+
}

0 commit comments

Comments
 (0)