0
mirror of https://github.com/OneOfEleven/uv-k5-firmware-custom.git synced 2025-06-19 14:48:03 +03:00

Reduced eeprom wear

This commit is contained in:
OneOfEleven
2023-10-19 14:21:37 +01:00
parent add4d6d7e1
commit ba677b3440
18 changed files with 337 additions and 251 deletions

View File

@ -14,40 +14,60 @@
* limitations under the License.
*/
#include <string.h> // NULL and memcmp
#include "driver/eeprom.h"
#include "driver/i2c.h"
#include "driver/system.h"
void EEPROM_ReadBuffer(uint16_t Address, void *pBuffer, uint8_t Size)
void EEPROM_ReadBuffer(const uint16_t address, void *p_buffer, const unsigned int size)
{
I2C_Start();
if (p_buffer == NULL || address >= 0x2000 || size == 0)
return;
I2C_Start();
I2C_Write(0xA0);
I2C_Write((Address >> 8) & 0xFF);
I2C_Write((Address >> 0) & 0xFF);
I2C_Write((address >> 8) & 0xFF);
I2C_Write((address >> 0) & 0xFF);
I2C_Start();
I2C_Write(0xA1);
I2C_ReadBuffer(pBuffer, Size);
I2C_ReadBuffer(p_buffer, size);
I2C_Stop();
}
void EEPROM_WriteBuffer(uint16_t Address, const void *pBuffer)
void EEPROM_WriteBuffer(const uint16_t address, const void *p_buffer)
{
if (p_buffer == NULL || address >= 0x2000)
return;
#if 0
// normal way
I2C_Start();
I2C_Write(0xA0);
I2C_Write((Address >> 8) & 0xFF);
I2C_Write((Address >> 0) & 0xFF);
I2C_WriteBuffer(pBuffer, 8);
I2C_Write((address >> 8) & 0xFF);
I2C_Write((address >> 0) & 0xFF);
I2C_WriteBuffer(p_buffer, 8);
I2C_Stop();
SYSTEM_DelayMs(10);
#else
// eeprom wear reduction
// only write the data IF it's different to what's already in eeprom
uint8_t buffer[8];
EEPROM_ReadBuffer(address, buffer, 8);
if (memcmp(p_buffer, buffer, 8) != 0)
{
I2C_Start();
I2C_Write(0xA0);
I2C_Write((address >> 8) & 0xFF);
I2C_Write((address >> 0) & 0xFF);
I2C_WriteBuffer(p_buffer, 8);
I2C_Stop();
}
#endif
// give the EEPROM time to burn the data in (apparently takes 5ms)
SYSTEM_DelayMs(8);
}

View File

@ -19,8 +19,8 @@
#include <stdint.h>
void EEPROM_ReadBuffer(uint16_t Address, void *pBuffer, uint8_t Size);
void EEPROM_WriteBuffer(uint16_t Address, const void *pBuffer);
void EEPROM_ReadBuffer(const uint16_t address, void *p_buffer, const unsigned int size);
void EEPROM_WriteBuffer(const uint16_t address, const void *p_buffer);
#endif