Skip to content

Commit 490437a

Browse files
committed
STM32: I2C: Move up get_i2c_obj
so that it can be used as well in sync mode
1 parent 0bee690 commit 490437a

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

targets/TARGET_STM/TARGET_STM32F4/common_objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ struct spi_s {
8484
};
8585

8686
struct i2c_s {
87+
/* The 1st 2 members I2CName i2c
88+
* and I2C_HandleTypeDef handle should
89+
* be kept as the first members of this struct
90+
*/
8791
I2CName i2c;
8892
I2C_HandleTypeDef handle;
8993
IRQn_Type event_i2cIRQ;

targets/TARGET_STM/TARGET_STM32F4/i2c_api.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,20 @@ void i2c_frequency(i2c_t *obj, int hz)
165165
handle->Instance->CR1 |= I2C_CR1_ACK;
166166
}
167167
#endif
168+
}
169+
170+
i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
168171

172+
/* Aim of the function is to get i2c_s pointer using hi2c pointer */
173+
/* Highly inspired from magical linux kernel's "container_of" */
174+
/* (which was not directly used since not compatible with IAR toolchain) */
175+
struct i2c_s *obj_s;
176+
i2c_t *obj;
177+
178+
obj_s = (struct i2c_s *)( (char *)hi2c - offsetof(struct i2c_s,handle));
179+
obj = (i2c_t *)( (char *)obj_s - offsetof(i2c_t,i2c));
180+
181+
return (obj);
169182
}
170183

171184
inline int i2c_start(i2c_t *obj) {
@@ -570,21 +583,6 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
570583

571584
#if DEVICE_I2C_ASYNCH
572585

573-
574-
i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
575-
576-
/* Aim of the function is to get i2c_s pointer using hi2c pointer */
577-
/* Highly inspired from magical linux kernel's "container_of" */
578-
/* (which was not directly used since not compatible with IAR toolchain) */
579-
struct i2c_s *obj_s;
580-
i2c_t *obj;
581-
582-
obj_s = (struct i2c_s *)( (char *)hi2c - offsetof(struct i2c_s,handle));
583-
obj = (i2c_t *)( (char *)obj_s - offsetof(i2c_t,i2c));
584-
585-
return (obj);
586-
}
587-
588586
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c){
589587
/* Get object ptr based on handler ptr */
590588
i2c_t *obj = get_i2c_obj(hi2c);

0 commit comments

Comments
 (0)