cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to Read and Write to I2C EEPROM

anuj
Associate II
Posted on February 23, 2017 at 21:43

I am using atmega AT24C256 EEPROM and STM32F0 discovery module. I am struggling to read/Write to the memory. Here is the example I trying to use:-

&sharpdefine I2C1_DEVICE_ADDRESS   0x50           /* A0 = A1 = A2 = 0 */

&sharpdefine MEMORY_ADDRESS 0x07

Buffer[0] = 'M';

HAL_I2C_Mem_Write(&hi2c1, (uint16_t) I2C1_DEVICE_ADDRESS<<1, MEMORY_ADDRESS, 1, Buffer, 1, 5);

HAL_Delay(1000);

Buffer[0] = 0x00;

HAL_Delay(1000);

HAL_I2C_Mem_Read(&hi2c1, (uint16_t) I2C1_DEVICE_ADDRESS<<1, MEMORY_ADDRESS, 1, Buffer, 1, 5); 

if (Buffer[0] == 0x4D) // if xBuffer[0] = 'M'

{

Test = 1;

}

On reading from memory, I do not get the  'M' or but some '255' instead. Where is the problem? Do I need to configure clock properly? I used CUBEMX for basic routines.

#cubemx #eeprom #hal #stm32f0 #i2c
13 REPLIES 13
Posted on March 07, 2017 at 16:34

fixed it, just changed the mcu, mcu was sending i2c with some strange spikes i saw on my portable oscilloscope, i guess thats why it did not work,

i used ur code and added 

<<1 to the slave address, thanks.

Buffer[0] = 'M';

HAL_I2C_Mem_Write(&hi2c1, 0x57<<1, 0x0007, 2, Buffer, 1, 5);

HAL_Delay(10);

Buffer[0] = 0x00;

HAL_I2C_Mem_Write(&hi2c1,

0x

57<<1

,

0x0007

, 2, Buffer, 0, 5);

HAL_I2C_Mem_Read(&hi2c1,

0x

57<<1

, 0x0007, 2, Buffer, 1, 5); 
srinivas reddy
Associate II
Posted on June 15, 2017 at 12:43

I also have problem writing and reading the data EEPROM

My EEPROM DevAddress is 0xA0

the MSB bit of 

DevAddress is 0 for writing and 1 for readeing.

val = HAL_I2C_GetState (&hi2c1);

print('HAL_I2C_GetState = %x', val);

val = HAL_I2C_Master_Transmit (&hi2c1, (uint16_t) 0xA0, '

EEPROM

', 10, 10000);

print

('HAL_I2C_Master_Transmit error = %d', val);

val = HAL_I2C_Master_Receive (&hi2c1, (uint16_t) 0xA1,&temp, 10, 10000);

print

('HAL_I2C_Master_Receive error = %d', val);

print

('HAL_I2C_Master_Receive data = %s', (uint8_t *)&temp);

am getting:

HAL_I2C_GetState = 20   //

 0x20 i.e 

HAL_I2C_STATE_READY             = 0x20U,   /*!< Peripheral Initialized and ready for use  */

HAL_I2C_Master_Transmit error = 0

HAL_I2C_Master_Receive error = 0

HAL_I2C_Master_Receive data = ÿÿÿÿÿÿÿÿÿÿ

thnaks.

Posted on June 15, 2017 at 14:44

 ,

 ,

here is my ds3231 code, taken liberally from adafruit's library.

uint16_t ds3231_read_reg(uint16_t register_pointer)

 ,

{

 ,

 , , , HAL_StatusTypeDef status = HAL_OK,

 ,

 , , , uint8_t timeArray[7] = {0},

 , ,  ,while (HAL_I2C_IsDeviceReady(&,hi2c1, (uint16_t)(DS3231_ADDRESS<,<,1), 3, 100) != HAL_OK) {}

 ,

 , , , status = HAL_I2C_Mem_Read(&,hi2c1,  , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , , ,  ,// i2c handle

 ,

 , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , (uint16_t)(DS3231_ADDRESS<,<,1),  , ,  ,// i2c address, left aligned

 ,

 , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , (uint16_t)register_pointer,  , ,  , , ,  ,// register address

 ,

 , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , I2C_MEMADD_SIZE_8BIT,  , ,  , , ,  , , ,  , , ,  ,// ds3231 uses 8-bit register addresses

 ,

 , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , (uint8_t*)(&,timeArray), , ,  , , ,  , , ,  ,// place to put returned value

 ,

 , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , 7, , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , , ,  ,// return seven bytes

 ,

 , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , 100), , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , , ,  , , ,  ,// timeout

 , , , /* Check the communication status */

 ,

 , , , if(status != HAL_OK)

 ,

 , , , {

 , , , }

 , ,  ,uint8_t ampm = 0,

 ,

 , ,  ,uint8_t ss = , bcd2bin(timeArray[0] &, 0x7F),

 ,

 , ,  ,uint8_t mm = , bcd2bin(timeArray[1]),

 ,

 , ,  ,uint8_t hh = , bcd2bin(timeArray[2]),

 ,

 , ,  ,uint8_t d , = , bcd2bin(timeArray[4]),

 ,

 , ,  ,uint8_t m , = , bcd2bin(timeArray[5]),

 ,

 , ,  ,uint16_t y , = bcd2bin(timeArray[6]) + 2000,

 ,

 , ,  ,uint8_t dow = ((uint16_t)date2days(y,m,d) + 6) % 7,

 , ,  ,printf('%s, %s %d, %d , , ', dayNames[dow], monthNames[m], d, y),

 , ,  ,if (hh == 12) {

 ,

 , ,  , , ,  ,ampm = 1, // its 12:00pm

 ,

 , ,  , , ,  ,}

 ,

 , ,  ,if (hh >, 12) {

 ,

 , ,  , , ,  ,hh -= 12, // 12hr mode - 13:00 is 1:00pm

 ,

 , ,  , , ,  ,ampm = 1,

 ,

 , ,  , , ,  ,}

 ,

 , ,  ,if (hh == 0) {

 ,

 , ,  , , ,  ,hh = 12, // midnight is 12:00 am

 ,

 , ,  , , ,  ,ampm = 0,

 ,

 , ,  , , ,  ,}

 , ,  ,if (hh <, 10) {

 ,

 , ,  , , ,  ,printf(' %d', hh),

 ,

 , ,  ,} else {

 ,

 , ,  , , ,  ,printf('%d', hh),

 ,

 , ,  ,}

 ,

 , ,  ,printf(':%02d:%02d %s', mm,ss, amPmString[ampm]),

 , ,  ,printf('\r\n'),

 , , , return bcdToDec(timeArray[0]),

 ,

}

and here is my i2c initialization code, just as the STM32CubeMX application generated, for an L476 nucleo board.

/* Includes ------------------------------------------------------------------*/

 ,

♯ include 'i2c.h'

♯ include 'gpio.h'

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

I2C_HandleTypeDef hi2c1,

/* I2C1 init function */

 ,

void MX_I2C1_Init(void)

 ,

{

 , hi2c1.Instance = I2C1,

 ,

 , hi2c1.Init.Timing = 0x10909CEC,

 ,

 , hi2c1.Init.OwnAddress1 = 0,

 ,

 , hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT,

 ,

 , hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE,

 ,

 , hi2c1.Init.OwnAddress2 = 0,

 ,

 , hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK,

 ,

 , hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE,

 ,

 , hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE,

 ,

 , if (HAL_I2C_Init(&,hi2c1) != HAL_OK)

 ,

 , {

 ,

 , , , Error_Handler(),

 ,

 , }

 , , , /**Configure Analogue filter

 ,

 , , , */

 ,

 , if (HAL_I2CEx_ConfigAnalogFilter(&,hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)

 ,

 , {

 ,

 , , , Error_Handler(),

 ,

 , }

}

void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)

 ,

{

 , GPIO_InitTypeDef GPIO_InitStruct,

 ,

 , if(i2cHandle->,Instance==I2C1)

 ,

 , {

 ,

 , /* USER CODE BEGIN I2C1_MspInit 0 */

 , /* USER CODE END I2C1_MspInit 0 */

 ,

 ,

 ,

 , , , /**I2C1 GPIO Configuration , ,  ,

 ,

 , , , PB8 , , , , ------>, I2C1_SCL

 ,

 , , , PB9 , , , , ------>, I2C1_SDA

 ,

 , , , */

 ,

 , , , GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9,

 ,

 , , , GPIO_InitStruct.Mode = GPIO_MODE_AF_OD,

 ,

 , , , GPIO_InitStruct.Pull = GPIO_PULLUP,

 ,

 , , , GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH,

 ,

 , , , GPIO_InitStruct.Alternate = GPIO_AF4_I2C1,

 ,

 , , , HAL_GPIO_Init(GPIOB, &,GPIO_InitStruct),

 , , , /* Peripheral clock enable */

 ,

 , , , __HAL_RCC_I2C1_CLK_ENABLE(),

 , , , /* Peripheral interrupt init */

 ,

 , , , HAL_NVIC_SetPriority(I2C1_EV_IRQn, 0, 0),

 ,

 , , , HAL_NVIC_EnableIRQ(I2C1_EV_IRQn),

 ,

 , , , HAL_NVIC_SetPriority(I2C1_ER_IRQn, 0, 0),

 ,

 , , , HAL_NVIC_EnableIRQ(I2C1_ER_IRQn),

 ,

 , /* USER CODE BEGIN I2C1_MspInit 1 */

 , /* USER CODE END I2C1_MspInit 1 */

 ,

 , }

 ,

}

void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)

 ,

{

 , if(i2cHandle->,Instance==I2C1)

 ,

 , {

 ,

 , /* USER CODE BEGIN I2C1_MspDeInit 0 */

 , /* USER CODE END I2C1_MspDeInit 0 */

 ,

 , , , /* Peripheral clock disable */

 ,

 , , , __HAL_RCC_I2C1_CLK_DISABLE(),

 ,

 ,

 ,

 , , , /**I2C1 GPIO Configuration , ,  ,

 ,

 , , , PB8 , , , , ------>, I2C1_SCL

 ,

 , , , PB9 , , , , ------>, I2C1_SDA

 ,

 , , , */

 ,

 , , , HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8|GPIO_PIN_9),

 , , , /* Peripheral interrupt Deinit*/

 ,

 , , , HAL_NVIC_DisableIRQ(I2C1_EV_IRQn),

 , , , HAL_NVIC_DisableIRQ(I2C1_ER_IRQn),

 , }

 ,

 , /* USER CODE BEGIN I2C1_MspDeInit 1 */

 , /* USER CODE END I2C1_MspDeInit 1 */

 ,

}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/**

 ,

 , * @}

 ,

 , */

/**

 ,

 , * @}

 ,

 , */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Shashank TS_2
Associate II
Posted on January 22, 2018 at 07:30

Hello, Am using STM32F0 uC for interfacing with a AT24C64. I am not able to write sequentially to the EEPROM after one page, i.e 32 Bytes. Can someone please if the logic is wrong.

This is the code:

data[0]=0x00; //First word address 8bit

data[1]=0x00; //Second word address 8bit

for(i=2;i<34;i++)

{

data[i]=0x11;

}

HAL_I2C_Master_Transmit(&hi2c1, EEPROM_ADDRESS<<1, data, 34, 50); //data is int type,34 is number bytes of data

data[0]=0x00; //First word address 8bit

data[1]=0x20; //Second word address 8bit// Next Page

for(i=2;i<34;i++)

{

data[i]=0x22;

}

HAL_I2C_Master_Transmit(&hi2c1, EEPROM_ADDRESS<<1, data, 34, 50); //data is int type,34 is number bytes of data

while (1)

{

/* USER CODE END WHILE */

data[0]=0x00;

data[1]=0x00;

HAL_I2C_Master_Transmit(&hi2c1, EEPROM_ADDRESS<<1, data, 2, 50); //data is int type pointing to first address here

for(i=0;i<32;i++)

{

HAL_I2C_Master_Receive(&hi2c1, EEPROM_ADDRESS<<1, &dataout[i], 1, 50);//maximum is 32 byte data locations

}

data[0]=0x00;

data[1]=0x20;

HAL_I2C_Master_Transmit(&hi2c1, EEPROM_ADDRESS<<1, data, 2, 50); //data is int type pointing to first address here

for(i=0;i<32;i++)

{

HAL_I2C_Master_Receive(&hi2c1, EEPROM_ADDRESS<<1, &dataout_1[i], 1, 50);//maximum is 32 byte data locations

}

}