cancel
Showing results for 
Search instead for 
Did you mean: 

How doi use the stm32f207vgt6 HAL Library to write and read the EEPROM using I2C.

srinivas reddy
Associate II
Posted on June 13, 2017 at 08:30

void MX_I2C1_Init(void)

{

HAL_I2C_MspInit(&hi2c1);

hi2c1.Instance = I2C1;

hi2c1.Init.ClockSpeed = 100000;

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;

hi2c1.Mode = HAL_I2C_MODE_MASTER;

if (HAL_I2C_Init(&hi2c1) != HAL_OK)

{

xprintf_temp('I2c1 Init error');

Error_Handler();

}

/* Enable the selected I2C peripheral */

I2C1->CR1 |= I2C_CR1_PE;

}

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

PB6 ------> I2C1_SCL

PB7 ------> I2C1_SDA

*/

GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

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();

/* USER CODE BEGIN I2C1_MspInit 1 */

/* USER CODE END I2C1_MspInit 1 */

}

}

char temp[15],val = 0;

val = HAL_I2C_Master_Transmit(&hi2c1,0x18,'EEPROM TEST',15,100000);

HAL_I2C_Master_Transmit it retuns 1(i.e HAL_ERROR)

val = HAL_I2C_Master_Receive(&hi2c1,0x18,temp,15,100000);

HAL_I2C_Master_Receive it retuns 1(i.e 

HAL_ERROR)

while am receiving the data (i.e temp data) . am not getting original data.

thanks 

6 REPLIES 6
john doe
Lead
Posted on June 13, 2017 at 14:34

on the premise that your device's address in the data sheet is 0x18

    uint8_t d[3];

    d[0] = (reg_addr >> 8) & 0xFF;                        // high byte of 16-bit register address

    d[1] = (reg_addr) & 0xFF;                            // low byte of 16-bit register address

    d[2] = value;                                        // what value are we writing to the register

    while (HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(0x18<<1), 3, 100) != HAL_OK) {}  //wait until the device is ready

    if (HAL_I2C_Master_Transmit(&hi2c1,                    // i2c handle

                               (uint16_t)(0x18<<1),     // i2c address, left aligned

                               (uint8_t *)d,             // pointer to the buffer containing the data

                               sizeof(d),                 // how many bytes are we transmitting

                               1000)                    // timeout value

                               != HAL_OK)                // result code

    {

        printf('master transmit failed\r\n');

        Error_Handler();

     }

read  is the same way except you do a transmit then a receive

        uint8_t value;

        while (HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(0x18<<1), 3, 100) != HAL_OK) {}  //wait until the device is ready

        while (HAL_I2C_Master_Receive(&hi2c1,                // i2c handle

                                     (uint16_t)(0x18<<1),    // i2c address, left aligned

                                     (uint8_t *)&value,        // pointer to address of where to store received data

                                     1,                        // expecting one byte to be returned

                                     100)                    // timeout

                                     != HAL_OK)                // result code

    {

        printf('master receive failed\r\n');

        Error_Handler();

     }

to read and write registers, its often more handy to use Mem_Read and Mem_Write

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

    status =  HAL_I2C_Mem_Read(&hi2c1,                        // i2c handle

                              (uint8_t)(dev_addr<<1),        // i2c address, left aligned

                              (uint8_t)reg_addr,            // register address

                              I2C_MEMADD_SIZE_16BIT,            // 16bit register addresses

                              (uint8_t*)(&array),            // write returned data to this variable

                              cnt,                        // how many bytes to expect returned

                              100);                            // timeout

    if (status != HAL_OK)

    {

       Error_Handler();

    }

Posted on June 14, 2017 at 11:36

  this code struct at

while (HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(0x18<<1), 3, 100) != HAL_OK) {}  //wait until the device is ready

and what will be the value of 

reg_addr,value and dev_addr .

HAL_I2C_Master_Transmit(&hi2c1,0x18<<1,'EEPROM TEST',15,100000);

HAL_I2C_Master_Receive(&hi2c1,0x18<<1,temp,15,100000);

in this am getting the size of the buffer, but didn't get the correct data.

am using EEPROM 24C256 in that SCL and SDA is connected to  External pullup 4K7.

when am used standard peripheral library its working good.

Posted on June 14, 2017 at 12:16

reg_addr is the register address you wish to access in the eeprom

dev_addr is the i2c address of the device

the HAL wants a pointer to a buffer so try (uint8_t *)&temp instead of just temp

Posted on June 14, 2017 at 13:11

HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(0x18<<1), 3, 100) returns 

HAL_ERROR.

Still i didn't get any idea why my code is not working..

please can any one healp me..

thanks.

Posted on June 14, 2017 at 13:45

it looks like you've modified initialization code that the cubemx application generated. try the initialization code it generates without modifying it

also, what does HAL_I2C_GetState(&hi2c1) return when you call it after MX_HAL_I2C_Init()

its either your initialization code or your wiring, at this point.

Posted on June 15, 2017 at 09:04

 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 = ÿÿÿÿÿÿÿÿÿÿ