Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MiscDrivers,Examples): Add 24LC256 EEPROM driver, add I2C EEPROM example for MAX32655 #751

Merged
merged 7 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuildCommandParser" id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser" keep-relative-paths="false" name="CDT GCC Build Output Parser" parameter="${PREFIX}(g?cc)|([gc]\+\+)|(clang)" prefer-non-shared="true"/>
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="true" env-hash="593024039320536518" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${GCC_PREFIX}gcc ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot; -DMXC_ASSERT_ENABLE -DARM_MATH_CM4 " prefer-non-shared="true">
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="true" env-hash="1400802835104524914" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${GCC_PREFIX}gcc ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot; -DMXC_ASSERT_ENABLE -DARM_MATH_CM4 " prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
Expand Down
19 changes: 16 additions & 3 deletions Examples/MAX32655/I2C_EEPROM/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Universal instructions on building, flashing, and debugging this project can be

```


****************** 24LC256 EEPROM DEMO *******************

This example communicates with an external 24LC256 EEPROM using the I2C.
Expand All @@ -51,15 +52,27 @@ The value: 0x03 is read from the address: 0x0003
The value: 0x04 is written to the address: 0x0004
The value: 0x04 is read from the address: 0x0004

EEPROM DEMO - Test2: Writing and Reading 256 Bytes:
EEPROM DEMO - Test2: Writing and Reading 1024 Bytes:

256 Bytes written to the EEPROM starting from the address: 0x0000. Each page (64 byte) filled with its own page number.
1024 Bytes written to the EEPROM starting from the address: 0x0000. Each page (64 byte) filled with its own page number.

256 bytes bytes read from the memory. The start address: 0x0000. The values read:
1024 bytes bytes read from the memory. The start address: 0x0000. The values read:
Page 0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Page 1: 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
Page 2: 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
Page 3: 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
Page 4: 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
Page 5: 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
Page 6: 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
Page 7: 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
Page 8: 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
Page 9: 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
Page 10: 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0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ress(not page start):

Expand Down
30 changes: 15 additions & 15 deletions Examples/MAX32655/I2C_EEPROM/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@

#define EEPROM_24LC256_I2C_SLAVE_ADDR0 0x50 //(0xA0 >> 1)

#define EEPROM_DEMO_BUFFER_PAGE_COUNT 4
#define EEPROM_DEMO_BUFFER_PAGE_COUNT 16
#define EEPROM_DEMO_BUFFER_SIZE _24LC256_EEPROM_PAGE_SIZE *EEPROM_DEMO_BUFFER_PAGE_COUNT // Bytes
// *****************************************************************************

/******************************* Globals *******************************/
static eeprom_24lc256_req_t eeprom1_req; ///< EEPROM request

/***** Functions *****/
int main(void)
{
int err = E_NO_ERROR;
Expand All @@ -73,28 +76,23 @@ int main(void)
printf("Press ENTER key to Continue\n\n");
getchar();

err = MXC_I2C_Init(I2C_MASTER, 1, 0);
err = Eeprom_24LC256_Init(&eeprom1_req, I2C_MASTER, EEPROM_24LC256_I2C_SLAVE_ADDR0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kenan-balci, this looks good. I have one more design suggestion:

  • Remove the &eeprom1_req as a required parameter for the functions.

You can move the eeprom_24lc256_req_t struct to be internal to the drivers so that the user doesn't need to pass the pointer in every time.

Since the user will not be interacting with this struct directly there's no need to expose it unless you want to preserve the ability to interface with multiple EEPROMs at the same time. If that's the case then you can leave it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My purpose is to give the driver the ability to communicate with more than one EEPROM, as you said. Up to 8 EEPROMs with different slave addresses can be connected to the same I2C bus. I tested communication with two EEPROMs, and the driver worked properly.

I2C_FREQ); // init the EEPROM
if (err != E_NO_ERROR) {
printf("EEPROM configure failed with error %i\n", err);
return err;
}

MXC_I2C_SetFrequency(I2C_MASTER, I2C_FREQ);

eeprom_24LC256_driver_t eeprom_24LC256 = eeprom_24LC256_Open();

eeprom_24LC256.init(I2C_MASTER, EEPROM_24LC256_I2C_SLAVE_ADDR0); // init the EEPROM

printf("EEPROM DEMO - Test 1: Writing and reading 1 byte:\n\n");
for (i = 0; i < 5; i++) {
err = eeprom_24LC256.write(eeprom_memory_addr, &written_val, 1);
err = Eeprom_24LC256_Write(&eeprom1_req, eeprom_memory_addr, &written_val, 1);
if (err != E_NO_ERROR) {
printf("EEPROM write error, error code = %d\n", err);
} else {
printf("The value: 0x%02X is written to the address: 0x%04X\n", written_val,
eeprom_memory_addr);
}
err = eeprom_24LC256.read(eeprom_memory_addr, &readed_val, 1);
err = Eeprom_24LC256_Read(&eeprom1_req, eeprom_memory_addr, &readed_val, 1);
Jake-Carter marked this conversation as resolved.
Show resolved Hide resolved
if (err != E_NO_ERROR) {
printf("EEPROM read error, error code = %d\n", err);
} else {
Expand All @@ -120,7 +118,8 @@ int main(void)
}
}

err = eeprom_24LC256.write(eeprom_memory_addr, &eeprom_demo_buffer[0], EEPROM_DEMO_BUFFER_SIZE);
err = Eeprom_24LC256_Write(&eeprom1_req, eeprom_memory_addr, &eeprom_demo_buffer[0],
EEPROM_DEMO_BUFFER_SIZE);
if (err != E_NO_ERROR) {
printf("EEPROM write error, error code = %d\n", err);
} else {
Expand All @@ -133,7 +132,8 @@ int main(void)
eeprom_demo_buffer[i] = 0;
}

err = eeprom_24LC256.read(eeprom_memory_addr, &eeprom_demo_buffer[0], EEPROM_DEMO_BUFFER_SIZE);
err = Eeprom_24LC256_Read(&eeprom1_req, eeprom_memory_addr, &eeprom_demo_buffer[0],
EEPROM_DEMO_BUFFER_SIZE);
if (err != E_NO_ERROR) {
printf("EEPROM read %d bytes error, error code = %d\n", EEPROM_DEMO_BUFFER_SIZE, err);
} else {
Expand Down Expand Up @@ -164,7 +164,7 @@ int main(void)
eeprom_demo_buffer[i] = test_val;
}

err = eeprom_24LC256.write(eeprom_memory_addr, &eeprom_demo_buffer[0], test_size);
err = Eeprom_24LC256_Write(&eeprom1_req, eeprom_memory_addr, &eeprom_demo_buffer[0], test_size);
if (err != E_NO_ERROR) {
printf("EEPROM write error, error code = %d\n", err);
} else {
Expand All @@ -177,7 +177,7 @@ int main(void)
eeprom_demo_buffer[i] = 0;
}

err = eeprom_24LC256.read(eeprom_memory_addr, &eeprom_demo_buffer[0], test_size);
err = Eeprom_24LC256_Read(&eeprom1_req, eeprom_memory_addr, &eeprom_demo_buffer[0], test_size);
if (err != E_NO_ERROR) {
printf("EEPROM read error, error code = %d\n", err);
} else {
Expand Down
110 changes: 45 additions & 65 deletions Libraries/MiscDrivers/EEPROM/eeprom_24lc256_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@
*
******************************************************************************/

/***** Includes *****/
#include "eeprom_24lc256_driver.h"

/******************************* Globals *******************************/
static mxc_i2c_req_t req; ///< I2C request

/******************************* Functions *******************************/
static int i2c_transfer(mxc_i2c_req_t *req, uint8_t *txData, int txSize, uint8_t *rxData,
int rxSize)
Expand All @@ -57,36 +55,37 @@ static int i2c_read(mxc_i2c_req_t *req, uint8_t *txData, uint8_t *rxData, int rx
return i2c_transfer(req, txData, 2, rxData, rxSize); // Create I2C read request
}

/**
* @brief Initializes I2C registers EEPROM
* @param i2c I2C registers
* @param addr Slave I2C address of EEPROM.
* @returns #E_NO_ERROR if init succeeded.
*
*/
static int eeprom_24LC256_init(mxc_i2c_regs_t *i2c, uint8_t addr)
int Eeprom_24LC256_Init(eeprom_24lc256_req_t *req, mxc_i2c_regs_t *i2c, uint8_t addr,
unsigned int i2c_freq)
{
req.i2c = i2c;
req.addr = addr;
req.tx_buf = NULL;
req.tx_len = 0;
req.rx_buf = NULL;
req.rx_len = 0;
req.restart = 0;
req.callback = NULL;
int err = E_NO_ERROR;
int return_val = 0;

err = MXC_I2C_Init(i2c, 1, 0);
if (err != E_NO_ERROR) {
return err;
}

return_val = MXC_I2C_SetFrequency(i2c, i2c_freq);
if (return_val <= 0) {
err = return_val;
return err;
}

req->i2c_req.i2c = i2c;
req->i2c_req.addr = addr;
req->i2c_req.tx_buf = NULL;
req->i2c_req.tx_len = 0;
req->i2c_req.rx_buf = NULL;
req->i2c_req.rx_len = 0;
req->i2c_req.restart = 0;
req->i2c_req.callback = NULL;

return E_NO_ERROR;
}

/**
* @brief Writes data from EEPROM
* @param addr Start address we want to read.
* @param data_buffer Data buffer to read.
* @param length Number of bytes to read.
* @returns #E_NO_ERROR if read succeeded. non-zero if an error occurred.
*
*/
static int eeprom_24LC256_read(uint16_t addr, uint8_t *data_buffer, uint16_t length)
int Eeprom_24LC256_Read(eeprom_24lc256_req_t *req, uint16_t addr, uint8_t *data_buffer,
uint16_t length)
{
int err = E_NO_ERROR;
uint16_t remaining = length;
Expand All @@ -104,7 +103,7 @@ static int eeprom_24LC256_read(uint16_t addr, uint8_t *data_buffer, uint16_t len
send_buffer[1] = (eeprom_addr_to_read & 0x00FF);

// Read
err = i2c_read(&req, send_buffer, current_data_buffer, remaining);
err = i2c_read(&req->i2c_req, send_buffer, current_data_buffer, remaining);
if (err != E_NO_ERROR) {
return err;
} else {
Expand All @@ -115,7 +114,7 @@ static int eeprom_24LC256_read(uint16_t addr, uint8_t *data_buffer, uint16_t len
send_buffer[1] = (eeprom_addr_to_read & 0x00FF);

// Read
err = i2c_read(&req, send_buffer, current_data_buffer, I2C_MAX_READ_SIZE);
err = i2c_read(&req->i2c_req, send_buffer, current_data_buffer, I2C_MAX_READ_SIZE);
if (err != E_NO_ERROR) {
return err;
}
Expand All @@ -127,42 +126,34 @@ static int eeprom_24LC256_read(uint16_t addr, uint8_t *data_buffer, uint16_t len
return E_NO_ERROR;
}

/**
* @brief Writes a small chunk of data directly to the EEPROM. The written memory should be in the same page of EEPROM (1 page = 64 bytes)
* @param addr Address we want to write to.
* @param data_buffer Data buffer to write.
* @param length Number of bytes to write.
* @returns #E_NO_ERROR if write succeeded. non-zero if an error occurred.
*
*/
static int eeprom_24LC256_write_chunk(uint16_t addr, uint8_t *data_buffer, uint16_t length)
int Eeprom_24LC256_Write_Chunk(eeprom_24lc256_req_t *req, uint16_t addr, uint8_t *data_buffer,
uint16_t length)
{
int err = E_NO_ERROR;
int i = 0;
uint8_t send_buffer[66]; // Page size (64) + 2 bytes

uint16_t remaining_size_until_page_end = _24LC256_EEPROM_PAGE_SIZE - (addr & 0x3F);
if (length > remaining_size_until_page_end) {
return E_BAD_PARAM;
}

send_buffer[0] = addr >> 8;
send_buffer[1] = (addr & 0x00FF);
for (i = 0; i < length; i++) {
send_buffer[i + 2] = data_buffer[i];
}

// Write
err = i2c_write(&req, send_buffer, (length + 2));
err = i2c_write(&req->i2c_req, send_buffer, (length + 2));
if (err != E_NO_ERROR) {
return err;
}
return E_NO_ERROR;
}

/**
* @brief Writes data to the EEPROM
* @param addr Address we want to write to.
* @param data_buffer Data buffer to write.
* @param length Number of bytes to write.
* @returns #E_NO_ERROR if write succeeded. non-zero if an error occurred.
*
*/
static int eeprom_24LC256_write(uint16_t addr, uint8_t *data_buffer, uint32_t length)
int Eeprom_24LC256_Write(eeprom_24lc256_req_t *req, uint16_t addr, uint8_t *data_buffer,
uint32_t length)
{
int err = E_NO_ERROR;
uint16_t remaining_data_length_to_write = length;
Expand All @@ -181,16 +172,16 @@ static int eeprom_24LC256_write(uint16_t addr, uint8_t *data_buffer, uint32_t le
if (remaining_data_length_to_write <=
remaining_size_until_page_end) // if remaining data size is smaller than remaining page size
{
err = eeprom_24LC256_write_chunk(eeprom_addr_to_write, current_data_buffer,
err = Eeprom_24LC256_Write_Chunk(req, eeprom_addr_to_write, current_data_buffer,
remaining_data_length_to_write);
if (err != E_NO_ERROR) {
return err;
} else {
remaining_data_length_to_write = 0;
}
} else {
err = eeprom_24LC256_write_chunk(
eeprom_addr_to_write, current_data_buffer,
err = Eeprom_24LC256_Write_Chunk(
req, eeprom_addr_to_write, current_data_buffer,
remaining_size_until_page_end); // Write until the end of the page
if (err != E_NO_ERROR) {
return err;
Expand All @@ -205,14 +196,14 @@ static int eeprom_24LC256_write(uint16_t addr, uint8_t *data_buffer, uint32_t le

while (remaining_data_length_to_write) {
if (remaining_data_length_to_write <= _24LC256_EEPROM_PAGE_SIZE) {
err = eeprom_24LC256_write_chunk(eeprom_addr_to_write, current_data_buffer,
err = Eeprom_24LC256_Write_Chunk(req, eeprom_addr_to_write, current_data_buffer,
remaining_data_length_to_write);
if (err != E_NO_ERROR) {
return err;
}
return E_NO_ERROR;
} else {
err = eeprom_24LC256_write_chunk(eeprom_addr_to_write, current_data_buffer,
err = Eeprom_24LC256_Write_Chunk(req, eeprom_addr_to_write, current_data_buffer,
_24LC256_EEPROM_PAGE_SIZE);
if (err != E_NO_ERROR) {
return err;
Expand All @@ -226,14 +217,3 @@ static int eeprom_24LC256_write(uint16_t addr, uint8_t *data_buffer, uint32_t le
}
return E_NO_ERROR;
}

eeprom_24LC256_driver_t eeprom_24LC256_Open(void)
{
eeprom_24LC256_driver_t SD;
SD.init = eeprom_24LC256_init;
SD.read = eeprom_24LC256_read;
SD.write_chunk = eeprom_24LC256_write_chunk;
SD.write = eeprom_24LC256_write;

return SD;
}
Loading
Loading