cancel
Showing results for 
Search instead for 
Did you mean: 

making i2c slave using modified HAL driver respond to read or write operations automatically

alan2
Associate II
Posted on February 13, 2015 at 12:04

I wanted to have a simple slave device that could respond to a read OR write from a i2c master.

the current HAL drivers don't support this so i added a few things to the HAL driver.

the attached files are modified versions of existing HAL drivers.

in stm32f0xx_hal_conf.h i removed ref to exisiting i2c driver

//&sharpdefine HAL_I2C_MODULE_ENABLED

and added all this

&sharpdefine GS_HAL_I2C_MODULE_ENABLED

&sharpifdef GS_HAL_I2C_MODULE_ENABLED

 &sharpinclude ''gs_stm32f0xx_hal_i2c.h''

&sharpendif /* GS_HAL_I2C_MODULE_ENABLED */

in order to start i2c slave for example just run once

eg.

  i2c_ret_state = GS_HAL_I2C_Slave_Transmit_Receive_IT(&hi2c1, (uint8_t *)&i2c_tx_buff, sizeof(i2c_tx_buff), (uint8_t *)&i2c_rx_buff, sizeof(i2c_rx_buff)) ;

and use IT callbacks to know whats been happening.

don't forget to change i2c handler to point to new routine

eg.

void I2C1_IRQHandler(void)

{

  HAL_NVIC_ClearPendingIRQ(I2C1_IRQn);

  if ( hi2c1.Instance->ISR & (I2C_FLAG_BERR | I2C_FLAG_ARLO | I2C_FLAG_OVR)) {

    HAL_I2C_ER_IRQHandler(&hi2c1);

  }

  else {

      GS_HAL_I2C_EV_IRQHandler(&hi2c1);

  }

}

i'm sure all this can be tidyed up more but this is first go at it. hope this helps and maybe something like this can be added to existing HAL drivers in future.

#i2c-slave-hal-autonomous
1 REPLY 1
alexis24
Associate
Posted on April 11, 2015 at 16:00

Hello,

Thank you for sharing 🙂 That's exactly what I need to do with the stm32l0.

Do you have a bigger other piece of your code with a 'real' and full use case ? 

Cheers,

Alexis