From 9efa2f8f771a83378477a9c86c7a43eadb16bcb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Fri, 3 Apr 2020 16:26:42 +0200 Subject: [PATCH] Use Zephyr EEPROM driver --- .../mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts | 4 + .../mppt_1210_hus_0v4/mppt_1210_hus_0v4.yaml | 1 + .../mppt_1210_hus_0v4_defconfig | 3 + .../mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts | 4 + .../mppt_1210_hus_0v7/mppt_1210_hus_0v7.yaml | 1 + .../mppt_1210_hus_0v7_defconfig | 3 + boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts | 21 +- .../arm/mppt_2420_hpx/mppt_2420_hpx_defconfig | 5 + boards/arm/mppt_2420_hpx/pinmux.c | 4 + .../mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts | 19 ++ .../mppt_2420_lc_0v10_defconfig | 7 + boards/arm/mppt_2420_lc_0v10/pinmux.c | 4 + .../arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts | 4 + .../pwm_2420_lus_0v3/pwm_2420_lus_0v3.yaml | 1 + .../pwm_2420_lus_0v3_defconfig | 3 + src/eeprom.cpp | 188 ++++-------------- src/eeprom.h | 26 +-- 17 files changed, 127 insertions(+), 171 deletions(-) diff --git a/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts b/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts index 45c08d9..62a7e79 100755 --- a/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts +++ b/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.dts @@ -88,3 +88,7 @@ &adc1 { status = "okay"; }; + +&eeprom { + status = "okay"; +}; diff --git a/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.yaml b/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.yaml index 17e4d3f..1d4c5e7 100755 --- a/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.yaml +++ b/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4.yaml @@ -15,3 +15,4 @@ supported: - spi - watchdog - adc + - eeprom diff --git a/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4_defconfig b/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4_defconfig index cd0265b..039e685 100755 --- a/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4_defconfig +++ b/boards/arm/mppt_1210_hus_0v4/mppt_1210_hus_0v4_defconfig @@ -30,3 +30,6 @@ CONFIG_CLOCK_STM32_PLL_MULTIPLIER=4 CONFIG_CLOCK_STM32_PLL_DIVISOR=2 CONFIG_CLOCK_STM32_AHB_PRESCALER=1 CONFIG_CLOCK_STM32_APB1_PRESCALER=1 + +CONFIG_EEPROM=y +CONFIG_EEPROM_STM32=y diff --git a/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts b/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts index 95e313b..0d8fc91 100755 --- a/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts +++ b/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.dts @@ -87,3 +87,7 @@ &adc1 { status = "okay"; }; + +&eeprom { + status = "okay"; +}; diff --git a/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.yaml b/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.yaml index f41092e..0cd3e63 100755 --- a/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.yaml +++ b/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7.yaml @@ -15,3 +15,4 @@ supported: - spi - watchdog - adc + - eeprom diff --git a/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7_defconfig b/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7_defconfig index cd0265b..039e685 100755 --- a/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7_defconfig +++ b/boards/arm/mppt_1210_hus_0v7/mppt_1210_hus_0v7_defconfig @@ -30,3 +30,6 @@ CONFIG_CLOCK_STM32_PLL_MULTIPLIER=4 CONFIG_CLOCK_STM32_PLL_DIVISOR=2 CONFIG_CLOCK_STM32_AHB_PRESCALER=1 CONFIG_CLOCK_STM32_APB1_PRESCALER=1 + +CONFIG_EEPROM=y +CONFIG_EEPROM_STM32=y diff --git a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts index 252e2a3..6837bdc 100644 --- a/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts +++ b/boards/arm/mppt_2420_hpx/mppt_2420_hpx.dts @@ -148,4 +148,23 @@ pwm { status = "okay"; }; -}; \ No newline at end of file +}; + +&i2c3 { + status = "okay"; + clock-frequency = ; + eeprom@50 { + // Microchip 24AA32A + compatible = "atmel,at24"; + reg = <0x50>; + label = "EEPROM_0"; + size = <32768>; + pagesize = <32>; + address-width = <16>; + /* + * timeout of 5 ms as suggested in datasheet seems too optimistic + * (several write errors occured during testing) + */ + timeout = <7>; + }; +}; diff --git a/boards/arm/mppt_2420_hpx/mppt_2420_hpx_defconfig b/boards/arm/mppt_2420_hpx/mppt_2420_hpx_defconfig index f2f5bfa..7ef43f5 100644 --- a/boards/arm/mppt_2420_hpx/mppt_2420_hpx_defconfig +++ b/boards/arm/mppt_2420_hpx/mppt_2420_hpx_defconfig @@ -57,3 +57,8 @@ CONFIG_UART_CONSOLE=y CONFIG_ARM_MPU=y CONFIG_PWM=y + +CONFIG_I2C=y +CONFIG_I2C_3=y +CONFIG_EEPROM=y +CONFIG_EEPROM_AT24=y diff --git a/boards/arm/mppt_2420_hpx/pinmux.c b/boards/arm/mppt_2420_hpx/pinmux.c index a4b460f..93a7bf9 100644 --- a/boards/arm/mppt_2420_hpx/pinmux.c +++ b/boards/arm/mppt_2420_hpx/pinmux.c @@ -28,6 +28,10 @@ static const struct pin_config pinconf[] = { {STM32_PIN_PA15, STM32G4X_PINMUX_FUNC_PA15_I2C1_SCL}, {STM32_PIN_PB7, STM32G4X_PINMUX_FUNC_PB7_I2C1_SDA}, #endif /* CONFIG_I2C_1 */ +#ifdef CONFIG_I2C_3 + {STM32_PIN_PC8, STM32G4X_PINMUX_FUNC_PC8_I2C3_SCL}, + {STM32_PIN_PC9, STM32G4X_PINMUX_FUNC_PC9_I2C3_SDA}, +#endif /* CONFIG_I2C_1 */ #ifdef CONFIG_PWM_STM32_2 {STM32_PIN_PA5, STM32G4X_PINMUX_FUNC_PA5_PWM2_CH1}, #endif /* CONFIG_PWM_STM32_2 */ diff --git a/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts index 22b6603..86f615d 100755 --- a/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts +++ b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10.dts @@ -69,6 +69,25 @@ clock-frequency = ; }; +&i2c2 { + status = "okay"; + clock-frequency = ; + eeprom@50 { + // Microchip 24AA32A + compatible = "atmel,at24"; + reg = <0x50>; + label = "EEPROM_0"; + size = <32768>; + pagesize = <32>; // 24AA01: 8 bytes + address-width = <16>; // 24AA01: 8 bit + /* + * timeout of 5 ms as suggested in datasheet seems too optimistic + * (several write errors occured during testing) + */ + timeout = <7>; + }; +}; + &spi1 { status = "okay"; }; diff --git a/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10_defconfig b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10_defconfig index 4b6b721..4fe1927 100755 --- a/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10_defconfig +++ b/boards/arm/mppt_2420_lc_0v10/mppt_2420_lc_0v10_defconfig @@ -30,6 +30,13 @@ CONFIG_CLOCK_STM32_PLL_MULTIPLIER=6 CONFIG_CLOCK_STM32_AHB_PRESCALER=1 CONFIG_CLOCK_STM32_APB1_PRESCALER=1 +CONFIG_I2C=y +CONFIG_I2C_1=y +CONFIG_I2C_2=y + CONFIG_CAN=y CONFIG_CAN_1=y CONFIG_EXT_THINGSET_CAN=y + +CONFIG_EEPROM=y +CONFIG_EEPROM_AT24=y diff --git a/boards/arm/mppt_2420_lc_0v10/pinmux.c b/boards/arm/mppt_2420_lc_0v10/pinmux.c index 23abb01..1a4c280 100755 --- a/boards/arm/mppt_2420_lc_0v10/pinmux.c +++ b/boards/arm/mppt_2420_lc_0v10/pinmux.c @@ -35,6 +35,10 @@ static const struct pin_config pinconf[] = { {STM32_PIN_PB6, STM32F0_PINMUX_FUNC_PB6_I2C1_SCL}, {STM32_PIN_PB7, STM32F0_PINMUX_FUNC_PB7_I2C1_SDA}, #endif /* CONFIG_I2C_1 */ +#ifdef CONFIG_I2C_2 + {STM32_PIN_PB10, STM32F0_PINMUX_FUNC_PB10_I2C2_SCL}, + {STM32_PIN_PB11, STM32F0_PINMUX_FUNC_PB11_I2C2_SDA}, +#endif /* CONFIG_I2C_2 */ #ifdef CONFIG_SPI_1 {STM32_PIN_PB3, STM32F0_PINMUX_FUNC_PB3_SPI1_SCK}, {STM32_PIN_PB4, STM32F0_PINMUX_FUNC_PB4_SPI1_MISO}, diff --git a/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts index 6b387b5..8ad7138 100644 --- a/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts +++ b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.dts @@ -84,3 +84,7 @@ &adc1 { status = "okay"; }; + +&eeprom { + status = "okay"; +}; diff --git a/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.yaml b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.yaml index 017513e..d657416 100644 --- a/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.yaml +++ b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3.yaml @@ -15,3 +15,4 @@ supported: - spi - watchdog - adc + - eeprom diff --git a/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3_defconfig b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3_defconfig index cd0265b..039e685 100644 --- a/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3_defconfig +++ b/boards/arm/pwm_2420_lus_0v3/pwm_2420_lus_0v3_defconfig @@ -30,3 +30,6 @@ CONFIG_CLOCK_STM32_PLL_MULTIPLIER=4 CONFIG_CLOCK_STM32_PLL_DIVISOR=2 CONFIG_CLOCK_STM32_AHB_PRESCALER=1 CONFIG_CLOCK_STM32_APB1_PRESCALER=1 + +CONFIG_EEPROM=y +CONFIG_EEPROM_STM32=y diff --git a/src/eeprom.cpp b/src/eeprom.cpp index dba3ebd..0d9b56c 100644 --- a/src/eeprom.cpp +++ b/src/eeprom.cpp @@ -8,6 +8,12 @@ #include +#ifndef UNIT_TEST +#include +#include +#include +#endif + #include "mcu.h" #include "board.h" #include "thingset.h" @@ -36,11 +42,15 @@ const uint16_t eeprom_data_objects[] = { 0xA6 // day count }; -#if !defined(UNIT_TEST) && (defined(STM32F0) || defined(STM32L0)) +#ifndef UNIT_TEST uint32_t _calc_crc(const uint8_t *buf, size_t len) { +#if defined(CONFIG_SOC_SERIES_STM32G4X) + RCC->AHB1ENR |= RCC_AHB1ENR_CRCEN; +#else RCC->AHBENR |= RCC_AHBENR_CRCEN; +#endif // we keep standard polynomial 0x04C11DB7 (same for STM32L0 and STM32F0) // and we don't care for endianness here @@ -57,153 +67,15 @@ uint32_t _calc_crc(const uint8_t *buf, size_t len) } } +#if defined(CONFIG_SOC_SERIES_STM32G4X) + RCC->AHB1ENR &= ~(RCC_AHB1ENR_CRCEN); +#else RCC->AHBENR &= ~(RCC_AHBENR_CRCEN); - return CRC->DR; -} - -#endif // UNIT_TEST - -#if (defined(EEPROM_24AA01) || defined(EEPROM_24AA32)) && defined(__MBED__) - -#ifdef EEPROM_24AA01 -#define EEPROM_PAGE_SIZE 8 // see datasheet of 24AA01 -#define EEPROM_ADDRESS_SIZE 1 // bytes -#elif defined(EEPROM_24AA32) -#define EEPROM_PAGE_SIZE 32 // see datasheet of 24AA32 -#define EEPROM_ADDRESS_SIZE 2 // bytes #endif -int device = 0b1010000 << 1; // 8-bit address -I2C i2c_eeprom(PIN_EEPROM_SDA, PIN_EEPROM_SCL); - -void eeprom_init (int i2c_address) -{ - device = i2c_address; + return CRC->DR; } -int eeprom_write (unsigned int addr, uint8_t* data, int len) -{ - int err = 0; - uint8_t buf[EEPROM_PAGE_SIZE + 2]; // page size + 2 address bytes - - for (uint16_t pos = 0; pos < len; pos += EEPROM_PAGE_SIZE) { - if (EEPROM_ADDRESS_SIZE == 1) { - buf[0] = (addr + pos) & 0xFF; - } - else { - buf[0] = (addr + pos) >> 8; // high byte - buf[1] = (addr + pos) & 0xFF; // low byte - } - - int page_len = 0; - for (int i = 0; i < EEPROM_PAGE_SIZE && pos + i < len; i++) { - buf[i+EEPROM_ADDRESS_SIZE] = data[pos + i]; - page_len++; - } - - // send I2C start + device address + memory address + data + I2C stop - err = i2c_eeprom.write(device, (char*)buf, page_len + 2); - - if (err != 0) - return -1; // write error - - // ACK polling as described in datasheet. - // EEPROM won't send an ACK from a new request until it finished writing. - for (int counter = 0; counter < 100; counter++) { - err = i2c_eeprom.write(device, (char*)buf, 2); - if (err == 0) { - //printf("EEPROM write finished, counter: %d\n", counter); - break; - } - } - - } - return err; - } - -int eeprom_read (unsigned int addr, uint8_t* ret, int len) -{ - uint8_t buf[2]; - int err = 0; - - if (EEPROM_ADDRESS_SIZE == 1) { - // write the address we want to read - buf[0] = addr & 0xFF; - // send memory address without stop (repeated = true) - err = i2c_eeprom.write(device, (char*)buf, 1, true); - } - else { - // same as above, but with 2 bytes address - buf[0] = addr >> 8; // high byte - buf[1] = addr & 0xFF; // low byte - err = i2c_eeprom.write(device, (char*)buf, 2, true); - } - - if (err != 0) - return -1; // read error - - wait(0.02); - - return i2c_eeprom.read(device, (char*)ret, len); -} - -#elif defined(STM32L0) // internal EEPROM - -int eeprom_write (unsigned int addr, const uint8_t* data, int len) -{ - int timeout = 0; - - // Wait till no operation is on going - while ((FLASH->SR & FLASH_SR_BSY) != 0 && timeout < 100) { - timeout++; - } - - // Perform unlock sequence if EEPROM is locked - if ((FLASH->PECR & FLASH_PECR_PELOCK) != 0) { - FLASH->PEKEYR = 0x89ABCDEFU; // FLASH_PEKEY1 - FLASH->PEKEYR = 0x02030405U; // FLASH_PEKEY2 - } - - if (addr + len > DATA_EEPROM_BANK1_END - DATA_EEPROM_BASE) { - return -1; - } - - // write data byte-wise to EEPROM - for (int i = 0; i < len; i++) { - *(uint8_t *)(DATA_EEPROM_BASE + addr + i) = data[i]; - } - - // Wait till no operation is on going - while ((FLASH->SR & FLASH_SR_BSY) != 0 && timeout < 200) { - timeout++; - } - // Lock the NVM again by setting PELOCK in PECR - FLASH->PECR |= FLASH_PECR_PELOCK; - - return 0; -} - -int eeprom_read (unsigned int addr, uint8_t* ret, int len) -{ - if (addr + len > DATA_EEPROM_BANK1_END - DATA_EEPROM_BASE) { - return -1; - } - - memcpy(ret, ((uint8_t*)addr) + DATA_EEPROM_BASE, len); - return 0; -} - -#else - -// ToDo for Zephyr -int eeprom_write (unsigned int addr, const uint8_t* data, int len) { return 1; } -int eeprom_read (unsigned int addr, uint8_t* ret, int len) { return 1; } - -#endif // STM32L0 - - -#if (defined(PIN_EEPROM_SDA) && defined(PIN_EEPROM_SCL)) || defined(STM32L0) - // EEPROM layout: // bytes 0-1: Version number // bytes 2-3: number of data bytes @@ -213,11 +85,15 @@ int eeprom_read (unsigned int addr, uint8_t* ret, int len) { return 1; } void eeprom_restore_data() { uint8_t buf_req[300]; // ThingSet request buffer + int err; + + struct device *eeprom_dev = device_get_binding("EEPROM_0"); // EEPROM header uint8_t buf_header[EEPROM_HEADER_SIZE] = {}; // initialize to avoid compiler warning - if (eeprom_read(0, buf_header, EEPROM_HEADER_SIZE) < 0) { - printf("EEPROM: read error!\n"); + err = eeprom_read(eeprom_dev, 0, buf_header, EEPROM_HEADER_SIZE); + if (err != 0) { + printf("EEPROM: read error: %d\n", err); return; } uint16_t version = *((uint16_t*)&buf_header[0]); @@ -230,7 +106,8 @@ void eeprom_restore_data() // buf_header[4], buf_header[5], buf_header[6], buf_header[7]); if (version == EEPROM_VERSION && len <= sizeof(buf_req)) { - eeprom_read(EEPROM_HEADER_SIZE, buf_req, len); + + err = eeprom_read(eeprom_dev, EEPROM_HEADER_SIZE, buf_req, len); //printf("Data (len=%d): ", len); //for (int i = 0; i < len; i++) printf("%.2x ", buf_req[i]); @@ -240,7 +117,8 @@ void eeprom_restore_data() printf("EEPROM: Data objects read and updated, ThingSet result: %d\n", status); } else { - printf("EEPROM: CRC of data not correct, expected 0x%x (data_len = %d)\n", (unsigned int)crc, len); + printf("EEPROM: CRC of data not correct, expected 0x%x (data_len = %d)\n", + (unsigned int)crc, len); } } else { @@ -252,7 +130,10 @@ void eeprom_store_data() { uint8_t buf[300]; - int len = ts.pub_msg_cbor(buf + EEPROM_HEADER_SIZE, sizeof(buf) - EEPROM_HEADER_SIZE, eeprom_data_objects, sizeof(eeprom_data_objects)/sizeof(uint16_t)); + struct device *eeprom_dev = device_get_binding("EEPROM_0"); + + int len = ts.pub_msg_cbor(buf + EEPROM_HEADER_SIZE, sizeof(buf) - EEPROM_HEADER_SIZE, + eeprom_data_objects, sizeof(eeprom_data_objects)/sizeof(uint16_t)); uint32_t crc = _calc_crc(buf + EEPROM_HEADER_SIZE, len); // store EEPROM_VERSION, number of bytes and CRC @@ -269,11 +150,14 @@ void eeprom_store_data() if (len == 0) { printf("EEPROM: Data could not be stored, ThingSet error: %d\n", len); } - else if (eeprom_write(0, buf, len + EEPROM_HEADER_SIZE) < 0) { - printf("EEPROM: Write error.\n"); - } else { - printf("EEPROM: Data successfully stored.\n"); + int err = eeprom_write(eeprom_dev, 0, buf, len + EEPROM_HEADER_SIZE); + if (err == 0) { + printf("EEPROM: Data successfully stored.\n"); + } + else { + printf("EEPROM: Write error.\n"); + } } } diff --git a/src/eeprom.h b/src/eeprom.h index 9f5f2e4..a93cf07 100644 --- a/src/eeprom.h +++ b/src/eeprom.h @@ -7,34 +7,24 @@ #ifndef EEPROM_H #define EEPROM_H -#include - -/** @file +/** + * @file * * @brief Handling of internal or external EEPROM to store device configuration */ -/** Write data to EEPROM address - * - * @returns 0 for success - */ -int eeprom_write(unsigned int addr, const uint8_t* data, int len); - -/** Read data from EEPROM address - * - * @returns 0 for success - */ -int eeprom_read(unsigned int addr, const uint8_t* ret, int len); - -/** Store current charge controller data to EEPROM +/** + * Store current charge controller data to EEPROM */ void eeprom_store_data(); -/** Restore charge controller data from EEPROM and write to variables in RAM +/** + * Restore charge controller data from EEPROM and write to variables in RAM */ void eeprom_restore_data(); -/** Stores data to EEPROM every 6 hours (can be called regularly) +/** + * Stores data to EEPROM every 6 hours (can be called regularly) */ void eeprom_update();