1 #include <twr_eeprom.h>
6 #include <twr_scheduler.h>
8 #define _TWR_EEPROM_BASE DATA_EEPROM_BASE
9 #define _TWR_EEPROM_END DATA_EEPROM_BANK2_END
10 #define _TWR_EEPROM_IS_BUSY() ((FLASH->SR & FLASH_SR_BSY) != 0UL)
25 static bool _twr_eeprom_is_busy(
twr_tick_t timeout);
26 static void _twr_eeprom_unlock(
void);
27 static void _twr_eeprom_lock(
void);
28 static bool _twr_eeprom_write(uint32_t address,
size_t *i, uint8_t *buffer,
size_t length);
29 static void _twr_eeprom_async_write_task(
void *param);
34 address += _TWR_EEPROM_BASE;
37 if ((address + length) > (_TWR_EEPROM_END + 1))
43 if (_twr_eeprom_is_busy(50))
54 _twr_eeprom_write(address, &i, (uint8_t *) buffer, length);
60 if (memcmp(buffer, (
void *) address, length) != 0UL)
72 if (_twr_eeprom.running)
77 _twr_eeprom.address = address += _TWR_EEPROM_BASE;
80 if ((_twr_eeprom.address + length) > (_TWR_EEPROM_END + 1))
86 _twr_eeprom.buffer = (uint8_t *) buffer;
88 _twr_eeprom.length = length;
90 _twr_eeprom.event_handler = event_handler;
92 _twr_eeprom.event_param = event_param;
98 _twr_eeprom.running =
true;
105 if (_twr_eeprom.running)
109 _twr_eeprom.running =
false;
116 address += _TWR_EEPROM_BASE;
119 if ((address + length) > (_TWR_EEPROM_END + 1))
126 memcpy(buffer, (
void *) address, length);
135 return _TWR_EEPROM_END - _TWR_EEPROM_BASE + 1;
138 static bool _twr_eeprom_is_busy(
twr_tick_t timeout)
142 while (_TWR_EEPROM_IS_BUSY())
153 static void _twr_eeprom_unlock(
void)
158 if ((FLASH->PECR & FLASH_PECR_PELOCK) != 0)
160 FLASH->PEKEYR = FLASH_PEKEY1;
161 FLASH->PEKEYR = FLASH_PEKEY2;
167 static void _twr_eeprom_lock(
void)
172 FLASH->PECR |= FLASH_PECR_PELOCK;
177 static bool _twr_eeprom_write(uint32_t address,
size_t *i, uint8_t *buffer,
size_t length)
179 uint32_t addr = address + *i;
181 uint8_t mod = addr % 4;
189 mod = (addr % 2) + 2;
203 uint32_t value = ((uint32_t) buffer[*i + 3]) << 24 | ((uint32_t) buffer[*i + 2]) << 16 | ((uint32_t) buffer[*i + 1]) << 8 | buffer[*i];
205 if (*((uint32_t *) addr) != value)
207 *((uint32_t *) addr) = value;
216 uint16_t value = ((uint16_t) buffer[*i + 1]) << 8 | (uint16_t) buffer[*i];
218 if (*((uint16_t *) addr) != value)
220 *((uint16_t *) addr) = value;
229 uint8_t value = buffer[*i];
231 if (*((uint8_t *) addr) != value)
233 *((uint8_t *) addr) = value;
241 while (_TWR_EEPROM_IS_BUSY())
250 static void _twr_eeprom_async_write_task(
void *param)
254 if (_TWR_EEPROM_IS_BUSY())
261 _twr_eeprom_unlock();
263 while(_twr_eeprom.i < _twr_eeprom.length)
265 if(_twr_eeprom_write(_twr_eeprom.address, &_twr_eeprom.i, _twr_eeprom.buffer, _twr_eeprom.length))
273 if(_twr_eeprom.i < _twr_eeprom.length)
280 _twr_eeprom.running =
false;
284 if (memcmp(_twr_eeprom.buffer, (
void *) _twr_eeprom.address, _twr_eeprom.length) != 0UL)
286 if (_twr_eeprom.event_handler != NULL)
293 if (_twr_eeprom.event_handler != NULL)
bool twr_eeprom_async_write(uint32_t address, const void *buffer, size_t length, void(*event_handler)(twr_eepromc_event_t, void *), void *event_param)
Async write buffer to EEPROM area and verify it.
bool twr_eeprom_read(uint32_t address, void *buffer, size_t length)
Read buffer from EEPROM area.
size_t twr_eeprom_get_size(void)
Return size of EEPROM area.
bool twr_eeprom_write(uint32_t address, const void *buffer, size_t length)
Write buffer to EEPROM area and verify it.
void twr_eeprom_async_cancel(void)
Cancel async write.
@ TWR_EEPROM_EVENT_ASYNC_WRITE_ERROR
EEPROM event sync write error.
@ TWR_EEPROM_EVENT_ASYNC_WRITE_DONE
EEPROM event sync write done.
void twr_irq_disable(void)
Disable interrupt requests globally (call can be nested)
void twr_irq_enable(void)
Enable interrupt requests globally (call can be nested)
void twr_scheduler_unregister(twr_scheduler_task_id_t task_id)
Unregister specified task.
size_t twr_scheduler_task_id_t
Task ID assigned by scheduler.
void twr_scheduler_plan_current_now(void)
Schedule current task for immediate execution.
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
twr_tick_t twr_tick_get(void)
Get absolute timestamp since start of program.
uint64_t twr_tick_t
Timestamp data type.