STM32: I2C: Move up get_i2c_obj

so that it can be used as well in sync mode
pull/3238/head
Laurent MEUNIER 2016-10-20 17:57:48 +02:00
parent 0bee69023c
commit 490437ae29
2 changed files with 17 additions and 15 deletions

View File

@ -84,6 +84,10 @@ struct spi_s {
};
struct i2c_s {
/* The 1st 2 members I2CName i2c
* and I2C_HandleTypeDef handle should
* be kept as the first members of this struct
*/
I2CName i2c;
I2C_HandleTypeDef handle;
IRQn_Type event_i2cIRQ;

View File

@ -165,7 +165,20 @@ void i2c_frequency(i2c_t *obj, int hz)
handle->Instance->CR1 |= I2C_CR1_ACK;
}
#endif
}
i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
/* Aim of the function is to get i2c_s pointer using hi2c pointer */
/* Highly inspired from magical linux kernel's "container_of" */
/* (which was not directly used since not compatible with IAR toolchain) */
struct i2c_s *obj_s;
i2c_t *obj;
obj_s = (struct i2c_s *)( (char *)hi2c - offsetof(struct i2c_s,handle));
obj = (i2c_t *)( (char *)obj_s - offsetof(i2c_t,i2c));
return (obj);
}
inline int i2c_start(i2c_t *obj) {
@ -570,21 +583,6 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
#if DEVICE_I2C_ASYNCH
i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
/* Aim of the function is to get i2c_s pointer using hi2c pointer */
/* Highly inspired from magical linux kernel's "container_of" */
/* (which was not directly used since not compatible with IAR toolchain) */
struct i2c_s *obj_s;
i2c_t *obj;
obj_s = (struct i2c_s *)( (char *)hi2c - offsetof(struct i2c_s,handle));
obj = (i2c_t *)( (char *)obj_s - offsetof(i2c_t,i2c));
return (obj);
}
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c){
/* Get object ptr based on handler ptr */
i2c_t *obj = get_i2c_obj(hi2c);