cancel
Showing results for 
Search instead for 
Did you mean: 

i2c slave address dependency

nikola2
Associate II
Posted on June 05, 2017 at 00:28

Hello,

I want to use a STM32L100 as I2C slave with two addresses. The master will receive different size of data and different data depending on the used address. This means that the slave address also includes a command.

I tried to use the HAL_I2C_AddrCallback() to check the address and then to respond with HAL_I2C_Slave_Transmit but it didn�t work.

I also tried to use HAL_I2C_Slave_Transmit_DMA() and then to change data in the data buffer in the HAL_I2C_AddrCallback() routine.

I would like to use the HAL libs for this problem to have a clean code. But I think that I have to make my own functions for that.

Does anybody have experience with this kind of I2C task?

Many thanks in advance!

Nikola

#dual-address #stm32 #i2c #hal #slave #address #variable-data-size #hal_i2c_addrcallback
1 REPLY 1
nikola2
Associate II
Posted on June 05, 2017 at 23:48

Solved it:

In main loop:

        while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY) {

        }

        // Enable I2C Listen IRQ

        if (HAL_I2C_EnableListen_IT(&hi2c1) != HAL_OK) {

            /* Transfer error in reception process */

            Error_Handler();

        }

in HAL_I2C_AddrCallback.

// Check I2C interface

    if (hi2c->Instance == I2C1) {

        __HAL_LOCK(hi2c);

        hi2c->State = HAL_I2C_STATE_BUSY_TX;

        hi2c->Mode = HAL_I2C_MODE_SLAVE;

        // Check address

        if (AddrMatchCode == 0x20) {

                 hi2c->Instance->DR = (uint8_t) AddrMatchCode;

        }

        // Check address

        if (AddrMatchCode == 0x22) {

                hi2c->Instance->DR = (uint8_t) 'A';

                // Wait for SR1_7 to be set

                while (((0x00000080 & hi2c->Instance->SR1) >> 7) != 1) {

                }

                hi2c->Instance->DR = (uint8_t) 'B';

                while (((0x00000080 & hi2c->Instance->SR1) >> 7) != 1) {

                }

                hi2c->Instance->DR = (uint8_t) 'C';

        }

        hi2c->State = HAL_I2C_STATE_READY;

        hi2c->Mode = HAL_I2C_MODE_NONE;

        __HAL_UNLOCK(hi2c);

    }