I2C eeprom write fix for SAMD (#786)

* I2C eeprom write fix for samd

* Implement extEEPROM library for SAMD
This commit is contained in:
Olivier
2017-03-06 16:45:58 +01:00
committed by Thomas Mørch
parent 7358521566
commit 7824e12823
11 changed files with 807 additions and 52 deletions
+10 -41
View File
@@ -41,62 +41,30 @@ ISR (WDT_vect)
*/
void i2c_eeprom_write_byte(unsigned int eeaddress, byte data )
{
int rdata = data;
Wire.beginTransmission(I2C_EEP_ADDRESS);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(rdata);
Wire.endTransmission();
}
byte i2c_eeprom_read_byte(unsigned int eeaddress )
{
byte rdata = 0xFF;
Wire.beginTransmission(I2C_EEP_ADDRESS);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(I2C_EEP_ADDRESS,1);
if (Wire.available()) {
rdata = Wire.read();
}
return rdata;
}
void hwReadConfigBlock(void* buf, void* adr, size_t length)
{
uint8_t* dst = static_cast<uint8_t*>(buf);
int offs = reinterpret_cast<int>(adr);
while (length-- > 0) {
*dst++ = i2c_eeprom_read_byte(offs++);
}
const int offs = reinterpret_cast<int>(adr);
(void)eep.read(offs, dst, length);
}
void hwWriteConfigBlock(void* buf, void* adr, size_t length)
{
uint8_t* src = static_cast<uint8_t*>(buf);
int offs = reinterpret_cast<int>(adr);
while (length-- > 0) {
i2c_eeprom_write_byte(offs++, *src++);
}
const int offs = reinterpret_cast<int>(adr);
// use update() instead of write() to reduce e2p wear off
(void)eep.update(offs, src, length);
}
uint8_t hwReadConfig(int adr)
{
uint8_t value;
hwReadConfigBlock(&value, reinterpret_cast<void*>(adr), 1);
return value;
return eep.read(adr);
}
void hwWriteConfig(int adr, uint8_t value)
{
uint8_t curr = hwReadConfig(adr);
if (curr != value) {
hwWriteConfigBlock(&value, reinterpret_cast<void*>(adr), 1);
}
(void)eep.update(adr, value);
}
void hwInit()
@@ -105,7 +73,8 @@ void hwInit()
#if defined(MY_GATEWAY_SERIAL)
while (!MY_SERIALDEVICE) {}
#endif
Wire.begin();
// ext. EEPROM on sensebender GW
eep.begin(MY_EXT_EEPROM_TWI_CLOCK);
}
void hwWatchdogReset()
+10 -5
View File
@@ -20,22 +20,27 @@
#define MyHwSAMD_h
#include "MyHw.h"
#include <Wire.h>
#ifdef __cplusplus
#include <Arduino.h>
//#include <SPI.h>
#endif
#include <avr/dtostrf.h>
#define I2C_EEP_ADDRESS 0x50
// defines for sensebender gw variant.h
#define MY_EXT_EEPROM_I2C_ADDRESS (0x50)
#define MY_EXT_EEPROM_SIZE (kbits_512)
#define MY_EXT_EEPROM_PAGE_SIZE (32u)
#define MY_EXT_EEPROM_TWI_CLOCK (twiClock400kHz)
extEEPROM eep(MY_EXT_EEPROM_SIZE, 1, MY_EXT_EEPROM_PAGE_SIZE,
MY_EXT_EEPROM_I2C_ADDRESS); //device size, number of devices, page size
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define snprintf_P(s, f, ...) snprintf((s), (f), __VA_ARGS__)
uint8_t configBlock[1024];
// Define these as macros to save valuable space
#define hwDigitalWrite(__pin, __value) digitalWrite(__pin, __value)
#define hwDigitalRead(__pin) digitalRead(__pin)