1

I am interfacing AT24C02 external eeprom using stm32f401 nucleo. I am facing few issues.

Lets consider some address

/* 24c02 Device Address */
#define EEPROM_ADDRESS                          (uint8_t)0xA0

/*Demo addr*/
#define DEMO_BYTE_ADDR                          (uint16_t)0x01
#define DEMO_WORD_ADDR                          (uint16_t)0x02
#define DEMO_FLOAT_ADDR                         (uint16_t)0x06
#define DEMO_STRING_ADDR                        (uint16_t)0x0A

1. Writing and reading simultaneously.
I am able to write and read data to and from eeprom simultaneously.

/*Writing Byte*/
uint8_t byte;
eep_write_byte(DEMO_BYTE_ADDR, 50);
eep_read_byte(DEMO_BYTE_ADDR, &byte);

/*Wrinting word*/
uint32_t word;
eep_write_word(DEMO_WORD_ADDR, 123456789);
word = eep_read_word(DEMO_WORD_ADDR);

/*Writing float*/
float fData;
eep_write_float(DEMO_FLOAT_ADDR, 9876.54);
fData = eep_read_float(DEMO_FLOAT_ADDR);

This code works fine. Below is the snapshot of output.

OUTPUT OF THE ABOVE CODE

2. Problems with writing string After the above portion of code i have written some lines to write string and read string. As you can see in output buffer dest contains some value.

uint8_t dest[50] = {0};
eep_write_string(DEMO_STRING_ADDR, (uint8_t*)"Hello World!", strlen("Hello World!"));
eep_read_string(DEMO_STRING_ADDR, dest, strlen("Hello World!"));

3. After writing all these values the reading shows corrupted data After the above portion of the code if i read back all the addresses i have wrote gives me corrupted data.

eep_read_byte(DEMO_BYTE_ADDR, &byte);
word = eep_read_word(DEMO_WORD_ADDR);
fData = eep_read_float(DEMO_FLOAT_ADDR);
eep_read_string(DEMO_STRING_ADDR, dest, strlen("Hello World!"));

Below is the snapshot of out for this code.

Corrupted Data

As you can see all the data is now corrupted.

you can find eeprom.c from this link https://pastebin.com/2vYWYhnw or just scroll below.

/*
 * eeprom.c
 *
 *  Created on: 04-Jan-2021
 *      Author: DEVJEET MANDAL
 */

#include "i2c.h"
#include "eeprom.h"

#include "stdio.h"
#include "stdlib.h"

/* Low Level function */
void eep_small_delay(void)          {
    for (uint32_t i = 0; i < 65535; i++)
        __asm__("NOP");
}

void i2c_error(void)    {
    HAL_I2C_DeInit(&hi2c1);     //De-init i2c bus
    __asm__("NOP");
    HAL_I2C_Init(&hi2c1);       //re-init i2c bus
    __asm__("NOP");
}

eep_status_t i2c_write(uint16_t u8_reg_addr, uint8_t *u8_data, uint16_t len)            {
    HAL_StatusTypeDef xStatus = HAL_ERROR;
    xStatus = HAL_I2C_Mem_Write(&hi2c1, EEPROM_ADDRESS, u8_reg_addr, I2C_MEMADD_SIZE_16BIT, u8_data, len, 100);
    HAL_Delay(5);
    if (xStatus != HAL_OK)  {i2c_error();}
    return xStatus;
}

eep_status_t i2c_read(uint16_t u8_reg_addr, uint8_t *u8_data, uint16_t len)         {
    HAL_StatusTypeDef xStatus = HAL_ERROR;
    xStatus = HAL_I2C_Mem_Read(&hi2c1, EEPROM_ADDRESS, u8_reg_addr, I2C_MEMADD_SIZE_16BIT, u8_data, len, 100);
    eep_small_delay();
    if (xStatus != HAL_OK)  {i2c_error();}
    return xStatus;
}

/* High Level Functions */
eep_status_t eep_write_byte(uint16_t u8_reg_addr, uint8_t u8_data)      {
    return i2c_write(u8_reg_addr, &u8_data, 1);
}

eep_status_t eep_read_byte(uint16_t u8_reg_addr, uint8_t *u8_data)      {
    return i2c_read(u8_reg_addr, u8_data, 1);
}

eep_status_t eep_is_data_avaiable(void)                     {
    eep_status_t xStatus = EEP_ERROR;
    uint8_t data = 0;
    eep_read_byte(EEPROM_DATA_AVAILABLE_ADDR, &data);
    if (data == 0)      {xStatus = EEP_ERROR;}
    else if (data == 1) {xStatus = EEP_OK;}
    else                {xStatus = EEP_ERROR;}
    return xStatus;
}

eep_status_t eep_set_data_available(uint8_t val)            {
    return eep_write_byte(EEPROM_DATA_AVAILABLE_ADDR, val);
}

eep_status_t eep_write_word(uint16_t reg_addr, uint32_t value)      {
    uint8_t val_byte[4] = {0};
    val_byte[0] = (value >> 24) & 0xFF;
    val_byte[1] = (value >> 16) & 0xFF;
    val_byte[2] = (value >> 8) & 0xFF;
    val_byte[3] = (value >> 0) & 0xFF;
    return i2c_write(reg_addr, val_byte, 4);
}

eep_status_t eep_write_float(uint16_t reg_addr, float value)        {
    union FtoHex{
        float fval;
        uint32_t hval;
    }float_to_hex;

    float_to_hex.fval = value;
    return eep_write_word(reg_addr, float_to_hex.hval);
}

uint32_t eep_read_word(uint16_t reg_addr)                   {
    uint8_t val_buff[4] = {0};
    i2c_read(reg_addr, val_buff, 4);
    return ((val_buff[0] << 24) | (val_buff[1] << 16) | (val_buff[2] << 8) | (val_buff[3] << 0));
}

float eep_read_float(uint8_t reg_addr)  {
    union FtoHex{
        float fval;
        uint32_t hval;
    }float_to_hex;

    float_to_hex.hval = eep_read_word(reg_addr);
    return float_to_hex.fval;
}

void eep_write_string(uint16_t reg_addr, uint8_t *src, uint16_t len)        {
    i2c_write(reg_addr, src, len);
}

void eep_read_string(uint16_t reg_addr, uint8_t *dest, uint16_t len)            {
    i2c_read(reg_addr, dest, len);
}

//---------------------------------------------------------------------

Almost forgot to mention i m running I2C @400Khz. Though i have tried 100KHz which results same. Below is the HAL generated I2C init.

/* I2C1 init function */
void MX_I2C1_Init(void)
{

  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 400000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }

} 

Now the question is why this problem. Basically the HAL should take care of all the situation and i will just send data to it. Any help will be appreciated.

Regards.

Ehsan Habib
  • 135
  • 1
  • 5
  • 12

1 Answers1

0

You missed that AT24C02 is organized in 16 byte (write) pages. The corruption is caused by rollover/wrap @ offset 0x0F when writing the string starting @ offset 0x0A. See the data sheet for details.

HS2
  • 164
  • 1
  • 1
  • 4
  • I read the datasheet but only found the roll over in page write. so if I m not getting wrong if I continuously write data it will be overwritten as soon as pages end. So what i did i did not write the string but only byte and word and float. the string i wrote in address 0x10. But the same problem. – Ehsan Habib Jan 05 '21 at 14:51