11
11
12
12
#define DT_DRV_COMPAT infineon_xmc4_i2c
13
13
14
+ #include <zephyr/logging/log.h>
15
+ LOG_MODULE_REGISTER (i2c_infineon_xmc4 , CONFIG_I2C_LOG_LEVEL );
16
+
14
17
#include <zephyr/drivers/i2c.h>
15
18
#include <zephyr/drivers/pinctrl.h>
16
19
20
+ #include "i2c-priv.h"
21
+
17
22
#include <xmc_i2c.h>
18
23
#include <xmc_usic.h>
19
24
20
25
#define USIC_IRQ_MIN 84
21
26
#define IRQS_PER_USIC 6
22
27
23
- #include <zephyr/logging/log.h>
24
- LOG_MODULE_REGISTER (i2c_infineon_xmc4 , CONFIG_I2C_LOG_LEVEL );
25
-
26
28
#define I2C_XMC_EVENTS_MASK ( \
27
29
XMC_I2C_CH_EVENT_RECEIVE_START | \
28
30
XMC_I2C_CH_EVENT_DATA_LOST | \
@@ -61,6 +63,7 @@ struct ifx_xmc4_i2c_data {
61
63
uint8_t target_wr_byte ;
62
64
uint8_t target_wr_buffer [CONFIG_I2C_INFINEON_XMC4_TARGET_BUF ];
63
65
bool ignore_slave_select ;
66
+ bool is_configured ;
64
67
};
65
68
66
69
/* Device config structure */
@@ -69,7 +72,7 @@ struct ifx_xmc4_i2c_config {
69
72
const struct pinctrl_dev_config * pcfg ;
70
73
uint8_t scl_src ;
71
74
uint8_t sda_src ;
72
- uint32_t master_frequency ;
75
+ uint32_t bitrate ;
73
76
void (* irq_config_func )(const struct device * dev );
74
77
};
75
78
@@ -78,24 +81,22 @@ static int ifx_xmc4_i2c_configure(const struct device *dev, uint32_t dev_config)
78
81
struct ifx_xmc4_i2c_data * data = dev -> data ;
79
82
const struct ifx_xmc4_i2c_config * config = dev -> config ;
80
83
81
- if (dev_config != 0 ) {
82
- switch (I2C_SPEED_GET (dev_config )) {
83
- case I2C_SPEED_STANDARD :
84
- data -> cfg .baudrate = XMC4_I2C_SPEED_STANDARD ;
85
- break ;
86
- case I2C_SPEED_FAST :
87
- data -> cfg .baudrate = XMC4_I2C_SPEED_FAST ;
88
- break ;
89
- default :
90
- LOG_ERR ("Unsupported speed" );
91
- return - ERANGE ;
92
- }
84
+ /* This is deprecated and could be ignored in the future */
85
+ if (dev_config & I2C_ADDR_10_BITS ) {
86
+ LOG_ERR ("Use I2C_MSG_ADDR_10_BITS instead of I2C_ADDR_10_BITS" );
87
+ return - EIO ;
88
+ }
93
89
94
- /* This is deprecated and could be ignored in the future */
95
- if (dev_config & I2C_ADDR_10_BITS ) {
96
- LOG_ERR ("Use I2C_MSG_ADDR_10_BITS instead of I2C_ADDR_10_BITS" );
97
- return - EIO ;
98
- }
90
+ switch (I2C_SPEED_GET (dev_config )) {
91
+ case I2C_SPEED_STANDARD :
92
+ data -> cfg .baudrate = XMC4_I2C_SPEED_STANDARD ;
93
+ break ;
94
+ case I2C_SPEED_FAST :
95
+ data -> cfg .baudrate = XMC4_I2C_SPEED_FAST ;
96
+ break ;
97
+ default :
98
+ LOG_ERR ("Unsupported speed" );
99
+ return - ERANGE ;
99
100
}
100
101
101
102
data -> dev_config = dev_config ;
@@ -105,6 +106,8 @@ static int ifx_xmc4_i2c_configure(const struct device *dev, uint32_t dev_config)
105
106
return - EIO ;
106
107
}
107
108
109
+ XMC_I2C_CH_Stop (config -> i2c );
110
+
108
111
/* Configure the I2C resource */
109
112
data -> cfg .normal_divider_mode = false;
110
113
XMC_I2C_CH_Init (config -> i2c , & data -> cfg );
@@ -118,6 +121,7 @@ static int ifx_xmc4_i2c_configure(const struct device *dev, uint32_t dev_config)
118
121
config -> irq_config_func (dev );
119
122
}
120
123
XMC_I2C_CH_Start (config -> i2c );
124
+ data -> is_configured = true;
121
125
122
126
/* Release semaphore */
123
127
k_sem_give (& data -> operation_sem );
@@ -128,26 +132,18 @@ static int ifx_xmc4_i2c_configure(const struct device *dev, uint32_t dev_config)
128
132
static int ifx_xmc4_i2c_get_config (const struct device * dev , uint32_t * dev_config )
129
133
{
130
134
struct ifx_xmc4_i2c_data * data = dev -> data ;
131
- uint32_t config ;
135
+ const struct ifx_xmc4_i2c_config * config = dev -> config ;
132
136
133
- switch (data -> cfg .baudrate ) {
134
- case XMC4_I2C_SPEED_STANDARD :
135
- config = I2C_SPEED_SET (I2C_SPEED_STANDARD );
136
- break ;
137
- case XMC4_I2C_SPEED_FAST :
138
- config = I2C_SPEED_SET (I2C_SPEED_FAST );
139
- break ;
140
- default :
141
- LOG_ERR ("Unsupported speed" );
142
- return - ERANGE ;
143
- }
137
+ if (!data -> is_configured ) {
138
+ /* if not yet configured return configuration that will be used */
139
+ /* when transfer() is called */
140
+ uint32_t bitrate_cfg = i2c_map_dt_bitrate (config -> bitrate );
144
141
145
- if ( data -> dev_config & I2C_MODE_CONTROLLER ) {
146
- config |= I2C_MODE_CONTROLLER ;
142
+ * dev_config = I2C_MODE_CONTROLLER | bitrate_cfg ;
143
+ return 0 ;
147
144
}
148
145
149
- /* Return current configuration */
150
- * dev_config = config ;
146
+ * dev_config = data -> dev_config ;
151
147
152
148
return 0 ;
153
149
}
@@ -173,6 +169,16 @@ static int ifx_xmc4_i2c_transfer(const struct device *dev, struct i2c_msg *msg,
173
169
return 0 ;
174
170
}
175
171
172
+ if (!data -> is_configured ) {
173
+ int ret ;
174
+ uint32_t bitrate_cfg = i2c_map_dt_bitrate (config -> bitrate );
175
+
176
+ ret = ifx_xmc4_i2c_configure (dev , I2C_MODE_CONTROLLER | bitrate_cfg );
177
+ if (ret ) {
178
+ return ret ;
179
+ }
180
+ }
181
+
176
182
/* Acquire semaphore (block I2C transfer for another thread) */
177
183
if (k_sem_take (& data -> operation_sem , K_FOREVER )) {
178
184
return - EIO ;
@@ -295,17 +301,14 @@ static int ifx_xmc4_i2c_init(const struct device *dev)
295
301
}
296
302
297
303
/* Configure dt provided device signals when available */
298
- ret = pinctrl_apply_state (config -> pcfg , PINCTRL_STATE_DEFAULT );
299
- if (ret < 0 ) {
300
- return ret ;
301
- }
302
-
303
- return 0 ;
304
+ return pinctrl_apply_state (config -> pcfg , PINCTRL_STATE_DEFAULT );
304
305
}
305
306
306
307
static int ifx_xmc4_i2c_target_register (const struct device * dev , struct i2c_target_config * cfg )
307
308
{
308
- struct ifx_xmc4_i2c_data * data = (struct ifx_xmc4_i2c_data * )dev -> data ;
309
+ struct ifx_xmc4_i2c_data * data = dev -> data ;
310
+ const struct ifx_xmc4_i2c_config * config = dev -> config ;
311
+ uint32_t bitrate_cfg ;
309
312
310
313
if (!cfg ||
311
314
!cfg -> callbacks -> read_requested ||
@@ -328,7 +331,16 @@ static int ifx_xmc4_i2c_target_register(const struct device *dev, struct i2c_tar
328
331
data -> p_target_config = cfg ;
329
332
data -> cfg .address = cfg -> address << 1 ;
330
333
331
- if (ifx_xmc4_i2c_configure (dev , I2C_SPEED_SET (I2C_SPEED_STANDARD )) != 0 ) {
334
+ if (data -> is_configured ) {
335
+ uint32_t bitrate ;
336
+
337
+ bitrate = I2C_SPEED_GET (data -> dev_config );
338
+ bitrate_cfg = i2c_map_dt_bitrate (bitrate );
339
+ } else {
340
+ bitrate_cfg = i2c_map_dt_bitrate (config -> bitrate );
341
+ }
342
+
343
+ if (ifx_xmc4_i2c_configure (dev , bitrate_cfg ) != 0 ) {
332
344
/* Release semaphore */
333
345
k_sem_give (& data -> target_sem );
334
346
return - EIO ;
@@ -340,7 +352,7 @@ static int ifx_xmc4_i2c_target_register(const struct device *dev, struct i2c_tar
340
352
341
353
static int ifx_xmc4_i2c_target_unregister (const struct device * dev , struct i2c_target_config * cfg )
342
354
{
343
- struct ifx_xmc4_i2c_data * data = ( struct ifx_xmc4_i2c_data * ) dev -> data ;
355
+ struct ifx_xmc4_i2c_data * data = dev -> data ;
344
356
const struct ifx_xmc4_i2c_config * config = dev -> config ;
345
357
346
358
/* Acquire semaphore (block I2C operation for another thread) */
@@ -454,7 +466,7 @@ static const struct i2c_driver_api i2c_xmc4_driver_api = {
454
466
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
455
467
.scl_src = DT_INST_ENUM_IDX(n, scl_src), \
456
468
.sda_src = DT_INST_ENUM_IDX(n, sda_src), \
457
- .master_frequency = DT_INST_PROP_OR(n, clock_frequency, XMC4_I2C_SPEED_STANDARD), \
469
+ .bitrate = DT_INST_PROP_OR(n, clock_frequency, I2C_SPEED_STANDARD), \
458
470
XMC4_IRQ_HANDLER_STRUCT_INIT(n) \
459
471
}; \
460
472
\
0 commit comments