AnsweredAssumed Answered

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

Question asked by beaker on Feb 13, 2015
Latest reply on Apr 11, 2015 by duque.alexis
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
//#define HAL_I2C_MODULE_ENABLED

and added all this
#define GS_HAL_I2C_MODULE_ENABLED

#ifdef GS_HAL_I2C_MODULE_ENABLED
 #include "gs_stm32f0xx_hal_i2c.h"
#endif /* 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.

Attachments

Outcomes