diff --git a/meson.build b/meson.build index 97fb06cf..5fa02d33 100644 --- a/meson.build +++ b/meson.build @@ -257,6 +257,7 @@ dm1801_src = src + mk22fn512_src + ['platform/targets/DM-1801/platform.c', 'platform/drivers/display/UC1701_GD77.c', 'platform/drivers/keyboard/keyboard_GD77.c', 'platform/drivers/NVM/W25Qx.c', + 'platform/drivers/NVM/AT24Cx_GDx.c', 'platform/drivers/NVM/spiFlash_GDx.c', 'platform/drivers/ADC/ADC0_GDx.c', 'platform/drivers/baseband/rtx_GDx.c'] diff --git a/platform/drivers/NVM/AT24Cx.h b/platform/drivers/NVM/AT24Cx.h new file mode 100644 index 00000000..331a9893 --- /dev/null +++ b/platform/drivers/NVM/AT24Cx.h @@ -0,0 +1,51 @@ +/*************************************************************************** + * Copyright (C) 2020 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * 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 * + ***************************************************************************/ + +#ifndef AT24Cx_H +#define AT24Cx_H + +#include +#include + +/** + * Driver for ATMEL AT24Cx family of I2C EEPROM devices, used as external non + * volatile memory on various radios to store global settings and contact data. + */ + +/** + * Initialise driver for external EEPROM. + */ +void AT24Cx_init(); + +/** + * Terminate driver for external EEPROM. + */ +void AT24Cx_terminate(); + +/** + * Read data from EEPROM memory. + * + * @param addr: start address for read operation. + * @param buf: pointer to a buffer where data is written to. + * @param len: number of bytes to read. + */ +void AT24Cx_readData(uint32_t addr, void *buf, size_t len); + +#endif /* AT24Cx_H */ diff --git a/platform/drivers/NVM/AT24Cx_GDx.c b/platform/drivers/NVM/AT24Cx_GDx.c new file mode 100644 index 00000000..c613d4f4 --- /dev/null +++ b/platform/drivers/NVM/AT24Cx_GDx.c @@ -0,0 +1,62 @@ +/*************************************************************************** + * Copyright (C) 2020 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * 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 * + ***************************************************************************/ + +#include "AT24Cx.h" +#include +#include +#include +#include +#include + +static const uint8_t devAddr = 0xA0; /* EEPROM I2C address */ + +void AT24Cx_init() +{ + gpio_setMode(I2C_SDA, OPEN_DRAIN); + gpio_setMode(I2C_SCL, OPEN_DRAIN); + gpio_setAlternateFunction(I2C_SDA, 3); + gpio_setAlternateFunction(I2C_SCL, 3); + + i2c0_init(); +} + +void AT24Cx_terminate() +{ + i2c0_terminate(); + gpio_setMode(I2C_SDA, INPUT); + gpio_setMode(I2C_SCL, INPUT); +} + +void AT24Cx_readData(uint32_t addr, void* buf, size_t len) +{ + 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(); + + i2c0_write(devAddr, &a, 2, false); + delayUs(10); + i2c0_read(devAddr, buf, len); + + i2c0_releaseDevice(); +} diff --git a/platform/targets/DM-1801/hwconfig.h b/platform/targets/DM-1801/hwconfig.h index b1577853..24700816 100644 --- a/platform/targets/DM-1801/hwconfig.h +++ b/platform/targets/DM-1801/hwconfig.h @@ -78,4 +78,8 @@ #define FLASH_SDO GPIOE,4 #define FLASH_SDI GPIOA,19 +/* I2C for EEPROM and AT1846S */ +#define I2C_SDA GPIOE,25 +#define I2C_SCL GPIOE,24 + #endif diff --git a/tests/platform/dumpEeprom_GDx.c b/tests/platform/dumpEeprom_GDx.c new file mode 100644 index 00000000..5b321904 --- /dev/null +++ b/tests/platform/dumpEeprom_GDx.c @@ -0,0 +1,63 @@ +/*************************************************************************** + * Copyright (C) 2020 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN, * + * Frederik Saraci IU2NRO, * + * Silvano Seva IU2KWO * + * * + * 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 * + ***************************************************************************/ + +#include +#include +#include +#include + +void printChunk(void *chunk) +{ + uint8_t *ptr = ((uint8_t *) chunk); + for(size_t i = 0; i < 16; i++) printf("%02x ", ptr[i]); + for(size_t i = 0; i < 16; i++) + { + if((ptr[i] > 0x22) && (ptr[i] < 0x7f)) + { + printf("%c", ptr[i]); + } + else + { + printf("."); + } + } +} + +int main() +{ + + AT24Cx_init(); + + while(1) + { + getchar(); + + for(uint16_t addr = 0; addr < 0x10000; addr += 16) + { + uint8_t buf[16]; + AT24Cx_readData(addr, buf, 16); + printf("\r\n%lx: ", addr); + printChunk(buf); + puts("\r"); + } + } + + return 0; +}