cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_I2C_Master_Receive

deokar
Associate II
Posted on April 19, 2017 at 11:49

Hello,

I have simple interfacing of STM32F0R8T6 with M24C01 EEPROM. It's been one week I am trying to read from EEPROM but buffer is responding with 0xFF value. My program successfully write values on EEPROM but fails to read them.

My function call:

HAL_Init();

SystemClock_Config();

MX_GPIO_Init();

MX_I2C1_Init();

uint8_t tab_write[3]= {1,2,3};

uint8_t tab_read[3];

HAL_I2C_Master_Transmit(&hi2c1, 0xA0, tab_write, 3, 1000);      // device address = 0xA0

HAL_I2C_Master_Receive(&hi2c1, 0xA0, tab_read, 3, 1000);

static void MX_I2C1_Init(void)

{

hi2c1.Instance = I2C1;

hi2c1.Init.Timing = 0x50330309;         // 400 Khz

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;

}

I checked some tutorials as well as examples provided by ST by seeing them this code should work but it is not working.

When I use Error_Handler() for both functions it enters error handler of Receive function.

Am I missing something?

Thank you for comments.

#eeprom #stm32f0-i2c #hal_i2c_master_receive
1 ACCEPTED SOLUTION

Accepted Solutions
john doe
Lead
Posted on April 19, 2017 at 14:46

not sure how you got the address A0 to work at all. HAL wants left justified address.

uint16_t mem_read_reg(uint16_t register_pointer)

{

    HAL_StatusTypeDef status = HAL_OK;

    uint16_t return_value = 0;

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

    status = HAL_I2C_Mem_Read(&hi2c1,                         // i2c handle

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

                             (uint16_t)register_pointer,     // register address

                             I2C_MEMADD_SIZE_16BIT,         // eeprom uses 16-bit register addresses

                             &return_value,                    // place to put returned value

                             1,                                // return one byte

                             100);                            // timeout

    /* Check the communication status */

    if(status != HAL_OK)

    {

    }

    return return_value;

}

View solution in original post

3 REPLIES 3
john doe
Lead
Posted on April 19, 2017 at 14:46

not sure how you got the address A0 to work at all. HAL wants left justified address.

uint16_t mem_read_reg(uint16_t register_pointer)

{

    HAL_StatusTypeDef status = HAL_OK;

    uint16_t return_value = 0;

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

    status = HAL_I2C_Mem_Read(&hi2c1,                         // i2c handle

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

                             (uint16_t)register_pointer,     // register address

                             I2C_MEMADD_SIZE_16BIT,         // eeprom uses 16-bit register addresses

                             &return_value,                    // place to put returned value

                             1,                                // return one byte

                             100);                            // timeout

    /* Check the communication status */

    if(status != HAL_OK)

    {

    }

    return return_value;

}
S.Ma
Principal
Posted on April 19, 2017 at 15:28

In your example, you are writing @01=02 and @02=03 as the byte written after writing the slave write adr is the eep sub adr location. After write, wait 10msec, do a start a0 01 to ask readinf from location 01, then read 2 bytes by S a1.a xx.a xx.n P content read should be 02 and 03

Posted on April 19, 2017 at 15:42

Thank you for your precious reply. I didn't have idea about this. As writing operation was accepting 0xA0 as a device address I didn't think about aligning address to left. Now its working.