From de22ef39992ee7b5cf574c14076a56d662f3467e Mon Sep 17 00:00:00 2001 From: Lukasz Skrzypczak Date: Wed, 28 Apr 2021 13:40:19 +0200 Subject: [PATCH] [EGD-6576] WiP I2C HAL API need to be modified WiP --- module-bsp/board/rt1051/bsp/eeprom/M24256.hpp | 4 +-- module-bsp/board/rt1051/bsp/eeprom/eeprom.cpp | 28 ++++++++++++++++--- module-bsp/bsp/eeprom/eeprom.hpp | 2 +- .../rt1051/purefs/src/blkdev/disk_emmc.cpp | 19 +++++++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/module-bsp/board/rt1051/bsp/eeprom/M24256.hpp b/module-bsp/board/rt1051/bsp/eeprom/M24256.hpp index 6e610ae1a2fad2a456e64b4e6efcbc0cc6af5623..3855d2993ed8d538740c3f4ea0b4d4338f76e4c1 100644 --- a/module-bsp/board/rt1051/bsp/eeprom/M24256.hpp +++ b/module-bsp/board/rt1051/bsp/eeprom/M24256.hpp @@ -7,8 +7,8 @@ namespace bsp::eeprom { - constexpr inline auto M24256_MEM_DEVICE_ADDR = 0xA0; - constexpr inline auto M24256_ID_DEVICE_ADDR = 0xB0; + constexpr inline auto M24256_MEM_DEVICE_ADDR = (0xA0 >> 1); + constexpr inline auto M24256_ID_DEVICE_ADDR = (0xB0 >> 1); constexpr inline auto M24256_PAGE_SIZE = 64; constexpr inline auto M24256_TOTAL_SIZE = (32 * 1024); // bytes diff --git a/module-bsp/board/rt1051/bsp/eeprom/eeprom.cpp b/module-bsp/board/rt1051/bsp/eeprom/eeprom.cpp index bd1fcf4d3169328f81e3056cfb510b2a61bedf24..36810081abec8487a9a4ec2cfaab435cdc4e33e0 100644 --- a/module-bsp/board/rt1051/bsp/eeprom/eeprom.cpp +++ b/module-bsp/board/rt1051/bsp/eeprom/eeprom.cpp @@ -9,6 +9,8 @@ #include "fsl_common.h" #include "log/log.hpp" +// generic i2c read() & write() doesn't provide correct i2c frame sequence for eeprom. i2c HAL API should be modified. + namespace bsp::eeprom { namespace @@ -16,7 +18,7 @@ namespace bsp::eeprom std::shared_ptr i2c; drivers::I2CAddress addr = { - .deviceAddress = static_cast(M24256_MEM_DEVICE_ADDR), .subAddress = 0, .subAddressSize = 1}; + .deviceAddress = static_cast(M24256_MEM_DEVICE_ADDR), .subAddress = 0, .subAddressSize = 2}; } // namespace @@ -26,7 +28,7 @@ namespace bsp::eeprom i2cParams.baudrate = static_cast(BoardDefinitions::EEPROM_I2C_BAUDRATE); i2c = drivers::DriverI2C::Create(static_cast(BoardDefinitions::EEPROM_I2C), i2cParams); - return isPresent() ? kStatus_Success : kStatus_Fail; + return isPresent(0) ? kStatus_Success : kStatus_Fail; } bool isPresent(int busid) @@ -47,17 +49,26 @@ namespace bsp::eeprom size_t bl_len = static_cast(eeprom_block_size(busid)); size_t chunks = len / bl_len; + size_t reminder = static_cast(len % bl_len); + + LOG_DEBUG("[EEPROM - R] chunks: %d, rem: %d", chunks, reminder); if (chunks > 0) { for (size_t i = 0; i < chunks; i++) { + LOG_DEBUG("[EEPROM - W] writing chunk %d of %d", i, chunks); written += i2c->Write(addr, reinterpret_cast(ptr), static_cast(bl_len)); ptr += bl_len; + addr.subAddress += bl_len; } } //reminder - written += i2c->Write(addr, reinterpret_cast(ptr), static_cast(len % bl_len)); + if (reminder > 0) + { + LOG_DEBUG("[EEPROM - W] writing remaining %d bytes", reminder); + written += i2c->Write(addr, reinterpret_cast(ptr), reminder); + } return static_cast(written); } @@ -72,17 +83,26 @@ namespace bsp::eeprom size_t bl_len = static_cast(eeprom_block_size(busid)); size_t chunks = len / bl_len; + size_t reminder = static_cast(len % bl_len); + + LOG_DEBUG("[EEPROM - R] chunks: %d, rem: %d", chunks, reminder); if (chunks > 0) { for (size_t i = 0; i < chunks; i++) { + LOG_DEBUG("[EEPROM - R] reading chunk %d of %d", i, chunks); read += i2c->Read(addr, reinterpret_cast(ptr), static_cast(bl_len)); ptr += bl_len; + addr.subAddress += bl_len; } } //reminder - read += i2c->Read(addr, reinterpret_cast(ptr), static_cast(len % bl_len)); + if (reminder > 0) + { + LOG_DEBUG("[EEPROM - R] reading remaining %d bytes", reminder); + read += i2c->Read(addr, reinterpret_cast(ptr), reminder); + } return static_cast(read); } diff --git a/module-bsp/bsp/eeprom/eeprom.hpp b/module-bsp/bsp/eeprom/eeprom.hpp index ce927bde04d5760649d25b5630cfa477a8fd4638..e2e16e1156d1e990b3cfa5929338ec4fff6b7caa 100644 --- a/module-bsp/bsp/eeprom/eeprom.hpp +++ b/module-bsp/bsp/eeprom/eeprom.hpp @@ -18,7 +18,7 @@ namespace bsp::eeprom int init(); - bool isPresent(); + bool isPresent(int busid); int eeprom_write( int busid, addr_t mem_addr, const char *buf, size_t len ); diff --git a/module-vfs/board/rt1051/purefs/src/blkdev/disk_emmc.cpp b/module-vfs/board/rt1051/purefs/src/blkdev/disk_emmc.cpp index f2c3c8d145bfe2b32ae6c49133fd728333b87b45..26df0561265e563ff8f9f3fa5947c98aa2fa06bf 100644 --- a/module-vfs/board/rt1051/purefs/src/blkdev/disk_emmc.cpp +++ b/module-vfs/board/rt1051/purefs/src/blkdev/disk_emmc.cpp @@ -9,6 +9,8 @@ #include #include "board/rt1051/bsp/eMMC/fsl_mmc.h" #include "bsp/BoardDefinitions.hpp" +#include "bsp/eeprom/eeprom.hpp" +#include "board/rt1051/bsp/eeprom/M24256.hpp" namespace purefs::blkdev { @@ -38,6 +40,23 @@ namespace purefs::blkdev initStatus = err; return initStatus; } + //EEPROM test + auto isEEPPresent = bsp::eeprom::init(); + LOG_DEBUG("EEPROM init %s", isEEPPresent ? "SUCCESS" : "FAIL"); + char buf[256] = {0}; + LOG_DEBUG("EEPROM: writing 16 B"); + auto written = bsp::eeprom::eeprom_write(0, 0x0000, reinterpret_cast(const_cast(buf)), 16); + auto read = bsp::eeprom::eeprom_write(0, 0x0000, buf, 16); + LOG_DEBUG("... w=%d, r=%d %s", written, read, (written == read) ? "SUCCESS" : "FAIL"); + LOG_DEBUG("EEPROM: writing 64 B"); + written = bsp::eeprom::eeprom_write(0, 0x0000, reinterpret_cast(const_cast(buf)), 64); + read = bsp::eeprom::eeprom_write(0, 0x0000, buf, 64); + LOG_DEBUG("... w=%d, r=%d %s", written, read, (written == read) ? "SUCCESS" : "FAIL"); + LOG_DEBUG("EEPROM: writing 256 B"); + written = bsp::eeprom::eeprom_write(0, 0x0000, reinterpret_cast(const_cast(buf)), 256); + read = bsp::eeprom::eeprom_write(0, 0x0000, buf, 256); + LOG_DEBUG("... w=%d, r=%d %s", written, read, (written == read) ? "SUCCESS" : "FAIL"); + return statusBlkDevSuccess; }