Skip to content

Commit 12ee47a

Browse files
Daniel Wagenknechtsuperna9999
Daniel Wagenknecht
authored andcommitted
i2c: stm32_v2: implement slave support
This patch adds I2C Slave support conforming to the syscalls and funcs, only for the STM32 V2 I2C Driver for the moment. It is capable of handling multi-master bus setups. Signed-off-by: Neil Armstrong <[email protected]> Signed-off-by: Daniel Wagenknecht <[email protected]>
1 parent 2aa4c4f commit 12ee47a

File tree

4 files changed

+268
-30
lines changed

4 files changed

+268
-30
lines changed

drivers/i2c/Kconfig.stm32

+4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ config I2C_STM32_V2
2727
select HAS_DTS_I2C
2828
select USE_STM32_LL_I2C
2929
select USE_STM32_LL_RCC if SOC_SERIES_STM32F0X || SOC_SERIES_STM32F3X
30+
select I2C_STM32_INTERRUPT if I2C_SLAVE
31+
default n
3032
help
3133
Enable I2C support on the STM32 F0, F3 and L4X family of processors.
3234
This driver also supports the F7 and L0 series.
35+
If I2C_SLAVE is enabled it selects I2C_STM32_INTERRUPT, since slave mode
36+
is only supported by this driver with interrupts enabled.
3337

3438
config I2C_STM32_INTERRUPT
3539
bool "STM32 MCU I2C Interrupt Support"

drivers/i2c/i2c_ll_stm32.c

+12-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define SYS_LOG_LEVEL CONFIG_SYS_LOG_I2C_LEVEL
1919
#include <logging/sys_log.h>
2020

21-
static int i2c_stm32_runtime_configure(struct device *dev, u32_t config)
21+
int i2c_stm32_runtime_configure(struct device *dev, u32_t config)
2222
{
2323
const struct i2c_stm32_config *cfg = DEV_CFG(dev);
2424
struct i2c_stm32_data *data = DEV_DATA(dev);
@@ -53,9 +53,11 @@ static int i2c_stm32_runtime_configure(struct device *dev, u32_t config)
5353
static int i2c_stm32_transfer(struct device *dev, struct i2c_msg *msg,
5454
u8_t num_msgs, u16_t slave)
5555
{
56+
#if defined(CONFIG_I2C_STM32_V1)
5657
const struct i2c_stm32_config *cfg = DEV_CFG(dev);
57-
struct i2c_msg *current, *next;
5858
I2C_TypeDef *i2c = cfg->i2c;
59+
#endif
60+
struct i2c_msg *current, *next;
5961
int ret = 0;
6062

6163
/* Check for validity of all messages, to prevent having to abort
@@ -108,7 +110,9 @@ static int i2c_stm32_transfer(struct device *dev, struct i2c_msg *msg,
108110
}
109111

110112
/* Send out messages */
113+
#if defined(CONFIG_I2C_STM32_V1)
111114
LL_I2C_Enable(i2c);
115+
#endif
112116

113117
current = msg;
114118

@@ -135,15 +139,19 @@ static int i2c_stm32_transfer(struct device *dev, struct i2c_msg *msg,
135139
current++;
136140
num_msgs--;
137141
};
138-
142+
#if defined(CONFIG_I2C_STM32_V1)
139143
LL_I2C_Disable(i2c);
140-
144+
#endif
141145
return ret;
142146
}
143147

144148
static const struct i2c_driver_api api_funcs = {
145149
.configure = i2c_stm32_runtime_configure,
146150
.transfer = i2c_stm32_transfer,
151+
#if defined(CONFIG_I2C_SLAVE) && defined(CONFIG_I2C_STM32_V2)
152+
.slave_register = i2c_stm32_slave_register,
153+
.slave_unregister = i2c_stm32_slave_unregister,
154+
#endif
147155
};
148156

149157
static int i2c_stm32_init(struct device *dev)

drivers/i2c/i2c_ll_stm32.h

+14
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,40 @@ struct i2c_stm32_data {
3434
unsigned int flags;
3535
#endif
3636
unsigned int is_write;
37+
unsigned int is_arlo;
3738
unsigned int is_nack;
3839
unsigned int is_err;
3940
struct i2c_msg *msg;
4041
unsigned int len;
4142
u8_t *buf;
4243
} current;
44+
#ifdef CONFIG_I2C_SLAVE
45+
bool master_active;
46+
struct i2c_slave_config *slave_cfg;
47+
bool slave_attached;
48+
#endif
4349
};
4450

4551
s32_t stm32_i2c_msg_write(struct device *dev, struct i2c_msg *msg, u8_t *flg,
4652
u16_t sadr);
4753
s32_t stm32_i2c_msg_read(struct device *dev, struct i2c_msg *msg, u8_t *flg,
4854
u16_t sadr);
4955
s32_t stm32_i2c_configure_timing(struct device *dev, u32_t clk);
56+
int i2c_stm32_runtime_configure(struct device *dev, u32_t config);
5057

5158
void stm32_i2c_event_isr(void *arg);
5259
void stm32_i2c_error_isr(void *arg);
5360
#ifdef CONFIG_I2C_STM32_COMBINED_INTERRUPT
5461
void stm32_i2c_combined_isr(void *arg);
5562
#endif
5663

64+
#ifdef CONFIG_I2C_SLAVE
65+
int i2c_stm32_slave_register(struct device *dev,
66+
struct i2c_slave_config *config);
67+
int i2c_stm32_slave_unregister(struct device *dev,
68+
struct i2c_slave_config *config);
69+
#endif
70+
5771
#define DEV_DATA(dev) ((struct i2c_stm32_data * const)(dev)->driver_data)
5872
#define DEV_CFG(dev) \
5973
((const struct i2c_stm32_config * const)(dev)->config->config_info)

0 commit comments

Comments
 (0)