2020-12-28 17:31:20 +00:00
|
|
|
/***************************************************************************
|
2025-04-04 17:06:57 +00:00
|
|
|
* Copyright (C) 2020 - 2025 by Federico Amedeo Izzo IU2NUO, *
|
2022-06-02 07:56:05 +00:00
|
|
|
* Niccolò Izzo IU2KIN *
|
|
|
|
* Frederik Saraci IU2NRO *
|
|
|
|
* Silvano Seva IU2KWO *
|
2020-12-28 17:31:20 +00:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/> *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2023-07-21 06:35:39 +00:00
|
|
|
#include <peripherals/gpio.h>
|
2021-01-02 14:17:15 +00:00
|
|
|
#include <interfaces/delays.h>
|
|
|
|
#include <stdint.h>
|
2023-07-18 11:49:38 +00:00
|
|
|
#include <string.h>
|
2021-01-02 14:17:15 +00:00
|
|
|
#include <I2C0.h>
|
2023-07-18 11:49:38 +00:00
|
|
|
#include "AT24Cx.h"
|
2020-12-28 17:31:20 +00:00
|
|
|
|
2023-07-18 11:49:38 +00:00
|
|
|
static const uint8_t DEV_ADDR = 0xA0; /* EEPROM I2C address */
|
|
|
|
static const size_t PAGE_SIZE = 128;
|
2020-12-28 17:31:20 +00:00
|
|
|
|
|
|
|
void AT24Cx_init()
|
|
|
|
{
|
2021-03-14 10:45:15 +00:00
|
|
|
/*
|
|
|
|
* Nothing to do here, on GDx devices the I2C bus is initialised in
|
|
|
|
* platform_init() before starting all the other modules.
|
|
|
|
*/
|
2020-12-28 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AT24Cx_terminate()
|
|
|
|
{
|
2021-03-14 10:45:15 +00:00
|
|
|
|
2020-12-28 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 11:49:38 +00:00
|
|
|
int AT24Cx_readData(uint32_t addr, void* buf, size_t len)
|
2020-12-28 17:31:20 +00:00
|
|
|
{
|
|
|
|
uint16_t a = __builtin_bswap16((uint16_t) addr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On GDx devices the I2C bus is shared between the EEPROM and the AT1846S,
|
|
|
|
* so we have to acquire exclusive ownership before exchanging data
|
|
|
|
*/
|
|
|
|
i2c0_lockDeviceBlocking();
|
|
|
|
|
2023-07-18 11:49:38 +00:00
|
|
|
i2c0_write(DEV_ADDR, &a, 2, false);
|
2020-12-28 17:31:20 +00:00
|
|
|
delayUs(10);
|
2023-07-18 11:49:38 +00:00
|
|
|
i2c0_read(DEV_ADDR, buf, len);
|
2020-12-28 17:31:20 +00:00
|
|
|
|
|
|
|
i2c0_releaseDevice();
|
2023-07-18 11:49:38 +00:00
|
|
|
|
|
|
|
return 0;
|
2020-12-28 17:31:20 +00:00
|
|
|
}
|
2023-07-18 11:49:38 +00:00
|
|
|
|
|
|
|
int AT24Cx_writeData(uint32_t addr, const void *buf, size_t len)
|
|
|
|
{
|
|
|
|
size_t toWrite;
|
|
|
|
uint8_t writeBuf[PAGE_SIZE + 2];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On GDx devices the I2C bus is shared between the EEPROM and the AT1846S,
|
|
|
|
* so we have to acquire exclusive ownership before exchanging data
|
|
|
|
*/
|
|
|
|
i2c0_lockDeviceBlocking();
|
|
|
|
|
|
|
|
while(len > 0)
|
|
|
|
{
|
|
|
|
toWrite = len;
|
|
|
|
if(toWrite >= PAGE_SIZE)
|
|
|
|
toWrite = PAGE_SIZE;
|
|
|
|
|
|
|
|
writeBuf[0] = (addr >> 8) & 0xFF; /* High address byte */
|
|
|
|
writeBuf[1] = (addr & 0xFF); /* Low address byte */
|
|
|
|
memcpy(&writeBuf[2], buf, toWrite); /* Data */
|
|
|
|
|
|
|
|
i2c0_write(DEV_ADDR, writeBuf, toWrite + 2, true);
|
|
|
|
|
|
|
|
len -= toWrite;
|
|
|
|
buf = ((const uint8_t *) buf) + toWrite;
|
|
|
|
addr += toWrite;
|
|
|
|
|
|
|
|
/* Wait for the write cycle to end (max 5ms as per datasheet) */
|
|
|
|
sleepFor(0, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
i2c0_releaseDevice();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-18 12:16:19 +00:00
|
|
|
static int nvm_api_read(const struct nvmDevice *dev, uint32_t offset, void *data, size_t len)
|
|
|
|
{
|
|
|
|
(void) dev;
|
|
|
|
|
|
|
|
return AT24Cx_readData(offset, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nvm_api_write(const struct nvmDevice *dev, uint32_t offset, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
(void) dev;
|
|
|
|
|
|
|
|
return AT24Cx_writeData(offset, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-05 19:07:32 +00:00
|
|
|
const struct nvmOps AT24Cx_ops =
|
2023-11-18 12:16:19 +00:00
|
|
|
{
|
|
|
|
.read = nvm_api_read,
|
|
|
|
.write = nvm_api_write,
|
|
|
|
.erase = NULL,
|
2024-04-05 19:07:32 +00:00
|
|
|
.sync = NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct nvmInfo AT24Cx_info =
|
|
|
|
{
|
|
|
|
.write_size = 1,
|
|
|
|
.erase_size = 1,
|
|
|
|
.erase_cycles = 1000000,
|
|
|
|
.device_info = NVM_EEPROM,
|
2023-11-18 12:16:19 +00:00
|
|
|
};
|