|
28 | 28 | #include "os_type.h"
|
29 | 29 | #include "osapi.h"
|
30 | 30 | #include "spi_flash.h"
|
31 |
| -extern uint32_t _SPIFFS_end; |
32 | 31 | }
|
33 | 32 |
|
34 |
| -#define CONFIG_START_SECTOR (((uint32_t)_SPIFFS_end - 0x40200000) / 4096) |
35 |
| -#define CONFIG_SECTOR (CONFIG_START_SECTOR + 0) |
36 |
| -#define CONFIG_ADDR (SPI_FLASH_SEC_SIZE * CONFIG_SECTOR) |
37 |
| - |
38 |
| -EEPROMClass::EEPROMClass() |
39 |
| -: _data(0), _size(0), _dirty(false) |
| 33 | +EEPROMClass::EEPROMClass(uint32_t sector) |
| 34 | +: _sector(sector) |
| 35 | +, _data(0) |
| 36 | +, _size(0) |
| 37 | +, _dirty(false) |
40 | 38 | {
|
41 | 39 | }
|
42 | 40 |
|
43 |
| -void EEPROMClass::begin(size_t size) |
44 |
| -{ |
45 |
| - if (size <= 0) |
46 |
| - return; |
47 |
| - if (size > SPI_FLASH_SEC_SIZE) |
48 |
| - size = SPI_FLASH_SEC_SIZE; |
| 41 | +void EEPROMClass::begin(size_t size) { |
| 42 | + if (size <= 0) |
| 43 | + return; |
| 44 | + if (size > SPI_FLASH_SEC_SIZE) |
| 45 | + size = SPI_FLASH_SEC_SIZE; |
| 46 | + |
| 47 | + if (_data) { |
| 48 | + delete[] _data; |
| 49 | + } |
49 | 50 |
|
50 |
| - _data = new uint8_t[size]; |
51 |
| - _size = size; |
| 51 | + _data = new uint8_t[size]; |
| 52 | + _size = size; |
52 | 53 |
|
53 |
| - noInterrupts(); |
54 |
| - spi_flash_read(CONFIG_ADDR, reinterpret_cast<uint32_t*>(_data), _size); |
55 |
| - interrupts(); |
| 54 | + noInterrupts(); |
| 55 | + spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size); |
| 56 | + interrupts(); |
56 | 57 | }
|
57 | 58 |
|
58 |
| -void EEPROMClass::end() |
59 |
| -{ |
60 |
| - if (!_size) |
61 |
| - return; |
| 59 | +void EEPROMClass::end() { |
| 60 | + if (!_size) |
| 61 | + return; |
62 | 62 |
|
63 |
| - commit(); |
64 |
| - if(_data) { |
65 |
| - delete[] _data; |
66 |
| - } |
67 |
| - _data = 0; |
68 |
| - _size = 0; |
| 63 | + commit(); |
| 64 | + if(_data) { |
| 65 | + delete[] _data; |
| 66 | + } |
| 67 | + _data = 0; |
| 68 | + _size = 0; |
69 | 69 | }
|
70 | 70 |
|
71 | 71 |
|
72 |
| -uint8_t EEPROMClass::read(int address) |
73 |
| -{ |
74 |
| - if (address < 0 || (size_t)address >= _size) |
75 |
| - return 0; |
76 |
| - if(!_data) |
77 |
| - return 0; |
| 72 | +uint8_t EEPROMClass::read(int address) { |
| 73 | + if (address < 0 || (size_t)address >= _size) |
| 74 | + return 0; |
| 75 | + if(!_data) |
| 76 | + return 0; |
78 | 77 |
|
79 |
| - return _data[address]; |
| 78 | + return _data[address]; |
80 | 79 | }
|
81 | 80 |
|
82 |
| -void EEPROMClass::write(int address, uint8_t value) |
83 |
| -{ |
84 |
| - if (address < 0 || (size_t)address >= _size) |
85 |
| - return; |
86 |
| - if(!_data) |
87 |
| - return; |
| 81 | +void EEPROMClass::write(int address, uint8_t value) { |
| 82 | + if (address < 0 || (size_t)address >= _size) |
| 83 | + return; |
| 84 | + if(!_data) |
| 85 | + return; |
88 | 86 |
|
89 |
| - _data[address] = value; |
90 |
| - _dirty = true; |
| 87 | + _data[address] = value; |
| 88 | + _dirty = true; |
91 | 89 | }
|
92 | 90 |
|
93 |
| -bool EEPROMClass::commit() |
94 |
| -{ |
95 |
| - bool ret = false; |
96 |
| - if (!_size) |
97 |
| - return false; |
98 |
| - if(!_dirty) |
99 |
| - return true; |
100 |
| - if(!_data) |
101 |
| - return false; |
102 |
| - |
103 |
| - noInterrupts(); |
104 |
| - if(spi_flash_erase_sector(CONFIG_SECTOR) == SPI_FLASH_RESULT_OK) { |
105 |
| - if(spi_flash_write(CONFIG_ADDR, reinterpret_cast<uint32_t*>(_data), _size) == SPI_FLASH_RESULT_OK) { |
106 |
| - _dirty = false; |
107 |
| - ret = true; |
108 |
| - } |
| 91 | +bool EEPROMClass::commit() { |
| 92 | + bool ret = false; |
| 93 | + if (!_size) |
| 94 | + return false; |
| 95 | + if(!_dirty) |
| 96 | + return true; |
| 97 | + if(!_data) |
| 98 | + return false; |
| 99 | + |
| 100 | + noInterrupts(); |
| 101 | + if(spi_flash_erase_sector(_sector) == SPI_FLASH_RESULT_OK) { |
| 102 | + if(spi_flash_write(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size) == SPI_FLASH_RESULT_OK) { |
| 103 | + _dirty = false; |
| 104 | + ret = true; |
109 | 105 | }
|
110 |
| - interrupts(); |
| 106 | + } |
| 107 | + interrupts(); |
111 | 108 |
|
112 |
| - return ret; |
| 109 | + return ret; |
113 | 110 | }
|
114 | 111 |
|
115 |
| -uint8_t * EEPROMClass::getDataPtr() |
116 |
| -{ |
117 |
| - _dirty = true; |
118 |
| - return &_data[0]; |
| 112 | +uint8_t * EEPROMClass::getDataPtr() { |
| 113 | + _dirty = true; |
| 114 | + return &_data[0]; |
119 | 115 | }
|
120 | 116 |
|
121 |
| - |
122 |
| -EEPROMClass EEPROM; |
| 117 | +extern "C" uint32_t _SPIFFS_end; |
| 118 | +EEPROMClass EEPROM((((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE)); |
0 commit comments