Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nickshl/DevCore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornickshl <nicolai.shlapunov@gmail.com>2022-04-06 01:19:56 +0300
committernickshl <nicolai.shlapunov@gmail.com>2022-04-06 01:19:56 +0300
commite88edbdd0a9f214563ec7b51bef693e62a2dbbf1 (patch)
treec3a60c0be738314497842e9a448e51245b1ba930
parent56a4cb5c5e1bd2f63248e4979eea21e6af1221d3 (diff)
EEPROM page & total size. Some bug fixes.
-rw-r--r--Drivers/StHalIic.cpp3
-rw-r--r--Libraries/Eeprom24.cpp38
-rw-r--r--Libraries/Eeprom24.h25
-rw-r--r--Libraries/Vl53l0x.cpp4
4 files changed, 54 insertions, 16 deletions
diff --git a/Drivers/StHalIic.cpp b/Drivers/StHalIic.cpp
index 49d648e..995c170 100644
--- a/Drivers/StHalIic.cpp
+++ b/Drivers/StHalIic.cpp
@@ -1,3 +1,4 @@
+
//******************************************************************************
// @file StHalIic.cpp
// @author Nicolai Shlapunov
@@ -73,7 +74,7 @@ Result StHalIic::IsDeviceReady(uint16_t addr, uint8_t retries)
// Shift address one bit left - HAL blow away LSB, not MSB.
addr <<= 1U;
// Check device status
- HAL_StatusTypeDef hal_result = HAL_I2C_IsDeviceReady(&hi2c1, addr, retries, i2c_tx_timeout_ms);
+ HAL_StatusTypeDef hal_result = HAL_I2C_IsDeviceReady(&hi2c, addr, retries, i2c_tx_timeout_ms);
// Convert operation result to Result
result = ConvertResult(hal_result);
// Return result
diff --git a/Libraries/Eeprom24.cpp b/Libraries/Eeprom24.cpp
index e7893c9..d9ccb2e 100644
--- a/Libraries/Eeprom24.cpp
+++ b/Libraries/Eeprom24.cpp
@@ -43,10 +43,20 @@ Result Eeprom24::Init()
// *****************************************************************************
Result Eeprom24::Read(uint16_t addr, uint8_t* rx_buf_ptr, uint16_t size)
{
- Result result = Result::ERR_NULL_PTR;
+ Result result = Result::RESULT_OK;
// Check input parameters
- if(rx_buf_ptr != nullptr)
+ if(rx_buf_ptr == nullptr)
+ {
+ // Set error
+ result = Result::ERR_NULL_PTR;
+ }
+ else if(addr + size > size_bytes)
+ {
+ // Set error
+ result = Result::ERR_INVALID_SIZE;
+ }
+ else
{
// Transfer: write two bytes address then read data
result = iic.Transfer(I2C_ADDR, (uint8_t*)&addr, sizeof(addr), rx_buf_ptr, size);
@@ -60,15 +70,21 @@ Result Eeprom24::Read(uint16_t addr, uint8_t* rx_buf_ptr, uint16_t size)
// *****************************************************************************
Result Eeprom24::Write(uint16_t addr, uint8_t* tx_buf_ptr, uint16_t size)
{
- Result result = Result::ERR_NULL_PTR;
+ Result result = Result::RESULT_OK;
// Check input parameters
- if(tx_buf_ptr != nullptr)
+ if(tx_buf_ptr == nullptr)
+ {
+ // Set error
+ result = Result::ERR_NULL_PTR;
+ }
+ else if(addr + size > size_bytes)
+ {
+ // Set error
+ result = Result::ERR_INVALID_SIZE;
+ }
+ else
{
- // Clear result to enter in to cycle
- result = Result::RESULT_OK;
- // Allocate buffer for address + data
- uint8_t buf[2U + PAGE_SIZE_BYTES];
// Disable write protection
if(write_protection != nullptr)
{
@@ -78,12 +94,12 @@ Result Eeprom24::Write(uint16_t addr, uint8_t* tx_buf_ptr, uint16_t size)
while(size && result.IsGood())
{
// Get data size
- uint8_t data_size = size < PAGE_SIZE_BYTES ? size : PAGE_SIZE_BYTES;
+ uint8_t data_size = size < page_size_bytes ? size : page_size_bytes;
// For the first page
- if((addr % PAGE_SIZE_BYTES) != 0U)
+ if((addr % page_size_bytes) != 0U)
{
// Calculate data size from start address to the end of current page
- data_size = PAGE_SIZE_BYTES - (addr % PAGE_SIZE_BYTES);
+ data_size = page_size_bytes - (addr % page_size_bytes);
// If size less than remaining page bytes - use size
data_size = size < data_size ? size : data_size;
}
diff --git a/Libraries/Eeprom24.h b/Libraries/Eeprom24.h
index ac4ff64..275517c 100644
--- a/Libraries/Eeprom24.h
+++ b/Libraries/Eeprom24.h
@@ -64,7 +64,12 @@ class Eeprom24
// *************************************************************************
// *** Public: Constructor *********************************************
// *************************************************************************
- explicit Eeprom24(IIic& iic_ref, IGpio* wp = nullptr) : iic(iic_ref), write_protection(wp) {};
+ explicit Eeprom24(IIic& iic_ref, IGpio* wp = nullptr, uint32_t size = 0x2000u, uint8_t page_size = 32u) : iic(iic_ref), write_protection(wp), size_bytes(size), page_size_bytes(page_size) {buf = new uint8_t[page_size + 2u];}
+
+ // *************************************************************************
+ // *** Public: Constructor *********************************************
+ // *************************************************************************
+ ~Eeprom24() {delete buf;}
// *************************************************************************
// *** Public: Init ****************************************************
@@ -81,12 +86,25 @@ class Eeprom24
// *************************************************************************
Result Write(uint16_t addr, uint8_t* tx_buf_ptr, uint16_t size);
+ // *************************************************************************
+ // *** Public: Write ***************************************************
+ // *************************************************************************
+ uint8_t GetSize() {return size_bytes;}
+
+ // *************************************************************************
+ // *** Public: Write ***************************************************
+ // *************************************************************************
+ uint8_t GetPageSize() {return page_size_bytes;}
+
private:
// Chip address
static const uint8_t I2C_ADDR = 0x50U;
+ // Size of EEPROM in bytes
+ uint32_t size_bytes = 0x2000u;
+
// Page size in bytes
- static const uint8_t PAGE_SIZE_BYTES = 64U;
+ uint8_t page_size_bytes = 32u;
// Writing timeout in ms
static const uint8_t WRITING_TIMEOUT_MS = 10U;
@@ -94,6 +112,9 @@ class Eeprom24
// Repetition counter for tracking timeout
uint8_t repetition_cnt = 0U;
+ // Buffer pointer to allocate memory for address + data
+ uint8_t* buf = nullptr;
+
// Reference to the I2C handle
IIic& iic;
// Write protection pin
diff --git a/Libraries/Vl53l0x.cpp b/Libraries/Vl53l0x.cpp
index 0f7e531..24f20c1 100644
--- a/Libraries/Vl53l0x.cpp
+++ b/Libraries/Vl53l0x.cpp
@@ -896,7 +896,7 @@ Result Vl53l0x::WriteMulti(uint8_t reg, uint8_t* src, uint8_t count)
{
buf[i + 1U] = src[i];
}
- result = iic.Write(i2c_addr, buf, sizeof(count) + 1U);
+ result = iic.Write(i2c_addr, buf, count + 1U);
}
return result;
@@ -908,5 +908,5 @@ Result Vl53l0x::WriteMulti(uint8_t reg, uint8_t* src, uint8_t count)
Result Vl53l0x::ReadMulti(uint8_t reg, uint8_t* dst, uint8_t count)
{
// Transfer & return result
- return iic.Transfer(i2c_addr, &reg, sizeof(reg), dst, sizeof(count));
+ return iic.Transfer(i2c_addr, &reg, sizeof(reg), dst, count);
}