21
21
22
22
#include " Arduino.h"
23
23
#include " EEPROM.h"
24
+ #include " debug.h"
24
25
25
26
extern " C" {
26
27
#include " c_types.h"
@@ -49,10 +50,14 @@ EEPROMClass::EEPROMClass(void)
49
50
}
50
51
51
52
void EEPROMClass::begin (size_t size) {
52
- if (size <= 0 )
53
+ if (size <= 0 ) {
54
+ DEBUGV (" EEPROMClass::begin error, size == 0\n " );
53
55
return ;
54
- if (size > SPI_FLASH_SEC_SIZE)
56
+ }
57
+ if (size > SPI_FLASH_SEC_SIZE) {
58
+ DEBUGV (" EEPROMClass::begin error, %d > %d\n " , size, SPI_FLASH_SEC_SIZE);
55
59
size = SPI_FLASH_SEC_SIZE;
60
+ }
56
61
57
62
size = (size + 3 ) & (~3 );
58
63
@@ -67,8 +72,11 @@ void EEPROMClass::begin(size_t size) {
67
72
_size = size;
68
73
69
74
noInterrupts ();
70
- spi_flash_read (_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast <uint32_t *>(_data), _size);
75
+ auto ret = spi_flash_read (_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast <uint32_t *>(_data), _size);
71
76
interrupts ();
77
+ if (ret != SPI_FLASH_RESULT_OK) {
78
+ DEBUGV (" EEPROMClass::begin spi_flash_read failed, %d\n " , (int )ret);
79
+ }
72
80
73
81
_dirty = false ; // make sure dirty is cleared in case begin() is called 2nd+ time
74
82
}
@@ -88,19 +96,27 @@ void EEPROMClass::end() {
88
96
89
97
90
98
uint8_t EEPROMClass::read (int const address) {
91
- if (address < 0 || (size_t )address >= _size)
99
+ if (address < 0 || (size_t )address >= _size) {
100
+ DEBUGV (" EEPROMClass::read error, address %d > %d or %d < 0\n " , address, _size, address);
92
101
return 0 ;
93
- if (!_data)
102
+ }
103
+ if (!_data) {
104
+ DEBUGV (" EEPROMClass::read without ::begin\n " );
94
105
return 0 ;
106
+ }
95
107
96
108
return _data[address];
97
109
}
98
110
99
111
void EEPROMClass::write (int const address, uint8_t const value) {
100
- if (address < 0 || (size_t )address >= _size)
112
+ if (address < 0 || (size_t )address >= _size) {
113
+ DEBUGV (" EEPROMClass::write error, address %d > %d or %d < 0\n " , address, _size, address);
101
114
return ;
102
- if (!_data)
115
+ }
116
+ if (!_data) {
117
+ DEBUGV (" EEPROMClass::read without ::begin\n " );
103
118
return ;
119
+ }
104
120
105
121
// Optimise _dirty. Only flagged if data written is different.
106
122
uint8_t * pData = &_data[address];
@@ -121,14 +137,20 @@ bool EEPROMClass::commit() {
121
137
return false ;
122
138
123
139
noInterrupts ();
124
- if (spi_flash_erase_sector (_sector) == SPI_FLASH_RESULT_OK) {
125
- if (spi_flash_write (_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast <uint32_t *>(_data), _size) == SPI_FLASH_RESULT_OK) {
140
+ auto flashret = spi_flash_erase_sector (_sector);
141
+ if (flashret == SPI_FLASH_RESULT_OK) {
142
+ flashret = spi_flash_write (_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast <uint32_t *>(_data), _size);
143
+ if (flashret == SPI_FLASH_RESULT_OK) {
126
144
_dirty = false ;
127
145
ret = true ;
128
146
}
129
147
}
130
148
interrupts ();
131
149
150
+ if (flashret != SPI_FLASH_RESULT_OK) {
151
+ DEBUGV (" EEPROMClass::commit failed, %d\n " , (int )flashret);
152
+ }
153
+
132
154
return ret;
133
155
}
134
156
0 commit comments