Lines Matching +full:mhz +full:- +full:delay +full:- +full:count

5  * SPDX-License-Identifier: Apache-2.0
28 #include "i2c-priv.h"
39 #define STM32_I2C_SPEED_FREQ_FAST_PLUS 2U /* 1 MHz */
67 uint32_t tscldel; /* SCL delay */
68 uint32_t tsdadel; /* SDA delay */
124 const struct i2c_stm32_config *cfg = dev->config; in msg_init()
125 struct i2c_stm32_data *data = dev->data; in msg_init()
126 I2C_TypeDef *i2c = cfg->i2c; in msg_init()
129 LL_I2C_SetTransferSize(i2c, msg->len); in msg_init()
131 if (I2C_ADDR_10_BITS & data->dev_config) { in msg_init()
141 if (!(msg->flags & I2C_MSG_STOP) && next_msg_flags && in msg_init()
149 LL_I2C_SetTransferSize(i2c, msg->len); in msg_init()
152 data->master_active = true; in msg_init()
164 const struct i2c_stm32_config *cfg = dev->config; in stm32_i2c_disable_transfer_interrupts()
165 struct i2c_stm32_data *data = dev->data; in stm32_i2c_disable_transfer_interrupts()
166 I2C_TypeDef *i2c = cfg->i2c; in stm32_i2c_disable_transfer_interrupts()
174 if (!data->smbalert_active) { in stm32_i2c_disable_transfer_interrupts()
181 const struct i2c_stm32_config *cfg = dev->config; in stm32_i2c_enable_transfer_interrupts()
182 I2C_TypeDef *i2c = cfg->i2c; in stm32_i2c_enable_transfer_interrupts()
192 const struct i2c_stm32_config *cfg = dev->config; in stm32_i2c_master_mode_end()
193 struct i2c_stm32_data *data = dev->data; in stm32_i2c_master_mode_end()
194 I2C_TypeDef *i2c = cfg->i2c; in stm32_i2c_master_mode_end()
203 data->master_active = false; in stm32_i2c_master_mode_end()
204 if (!data->slave_attached && !data->smbalert_active) { in stm32_i2c_master_mode_end()
208 if (!data->smbalert_active) { in stm32_i2c_master_mode_end()
212 k_sem_give(&data->device_sync_sem); in stm32_i2c_master_mode_end()
218 const struct i2c_stm32_config *cfg = dev->config; in stm32_i2c_slave_event()
219 struct i2c_stm32_data *data = dev->data; in stm32_i2c_slave_event()
220 I2C_TypeDef *i2c = cfg->i2c; in stm32_i2c_slave_event()
224 if (data->slave_cfg->flags != I2C_TARGET_FLAGS_ADDR_10_BITS) { in stm32_i2c_slave_event()
229 if (data->slave_cfg != NULL && in stm32_i2c_slave_event()
230 slave_address == data->slave_cfg->address) { in stm32_i2c_slave_event()
231 slave_cfg = data->slave_cfg; in stm32_i2c_slave_event()
232 } else if (data->slave2_cfg != NULL && in stm32_i2c_slave_event()
233 slave_address == data->slave2_cfg->address) { in stm32_i2c_slave_event()
234 slave_cfg = data->slave2_cfg; in stm32_i2c_slave_event()
244 if (data->slave_cfg != NULL) { in stm32_i2c_slave_event()
245 slave_cfg = data->slave_cfg; in stm32_i2c_slave_event()
252 slave_cb = slave_cfg->callbacks; in stm32_i2c_slave_event()
257 if (slave_cb->read_processed(slave_cfg, &val) < 0) { in stm32_i2c_slave_event()
268 if (slave_cb->write_received(slave_cfg, val)) { in stm32_i2c_slave_event()
286 slave_cb->stop(slave_cfg); in stm32_i2c_slave_event()
299 if (slave_cb->write_requested(slave_cfg) < 0) { in stm32_i2c_slave_event()
307 if (slave_cb->read_requested(slave_cfg, &val) < 0) { in stm32_i2c_slave_event()
323 const struct i2c_stm32_config *cfg = dev->config; in i2c_stm32_target_register()
324 struct i2c_stm32_data *data = dev->data; in i2c_stm32_target_register()
325 I2C_TypeDef *i2c = cfg->i2c; in i2c_stm32_target_register()
330 return -EINVAL; in i2c_stm32_target_register()
333 if (data->slave_cfg && data->slave2_cfg) { in i2c_stm32_target_register()
334 return -EBUSY; in i2c_stm32_target_register()
337 if (data->master_active) { in i2c_stm32_target_register()
338 return -EBUSY; in i2c_stm32_target_register()
341 bitrate_cfg = i2c_map_dt_bitrate(cfg->bitrate); in i2c_stm32_target_register()
353 /* Enable wake-up from stop */ in i2c_stm32_target_register()
355 LL_I2C_EnableWakeUpFromStop(cfg->i2c); in i2c_stm32_target_register()
361 if (!data->slave_cfg) { in i2c_stm32_target_register()
362 data->slave_cfg = config; in i2c_stm32_target_register()
363 if (data->slave_cfg->flags == I2C_TARGET_FLAGS_ADDR_10_BITS) { in i2c_stm32_target_register()
364 LL_I2C_SetOwnAddress1(i2c, config->address, LL_I2C_OWNADDRESS1_10BIT); in i2c_stm32_target_register()
365 LOG_DBG("i2c: target #1 registered with 10-bit address"); in i2c_stm32_target_register()
367 LL_I2C_SetOwnAddress1(i2c, config->address << 1U, LL_I2C_OWNADDRESS1_7BIT); in i2c_stm32_target_register()
368 LOG_DBG("i2c: target #1 registered with 7-bit address"); in i2c_stm32_target_register()
375 data->slave2_cfg = config; in i2c_stm32_target_register()
377 if (data->slave2_cfg->flags == I2C_TARGET_FLAGS_ADDR_10_BITS) { in i2c_stm32_target_register()
378 return -EINVAL; in i2c_stm32_target_register()
380 LL_I2C_SetOwnAddress2(i2c, config->address << 1U, in i2c_stm32_target_register()
386 data->slave_attached = true; in i2c_stm32_target_register()
396 const struct i2c_stm32_config *cfg = dev->config; in i2c_stm32_target_unregister()
397 struct i2c_stm32_data *data = dev->data; in i2c_stm32_target_unregister()
398 I2C_TypeDef *i2c = cfg->i2c; in i2c_stm32_target_unregister()
400 if (!data->slave_attached) { in i2c_stm32_target_unregister()
401 return -EINVAL; in i2c_stm32_target_unregister()
404 if (data->master_active) { in i2c_stm32_target_unregister()
405 return -EBUSY; in i2c_stm32_target_unregister()
408 if (config == data->slave_cfg) { in i2c_stm32_target_unregister()
410 data->slave_cfg = NULL; in i2c_stm32_target_unregister()
413 } else if (config == data->slave2_cfg) { in i2c_stm32_target_unregister()
415 data->slave2_cfg = NULL; in i2c_stm32_target_unregister()
419 return -EINVAL; in i2c_stm32_target_unregister()
423 if (data->slave_cfg || data->slave2_cfg) { in i2c_stm32_target_unregister()
424 LOG_DBG("i2c: target#%c still registered", data->slave_cfg?'1':'2'); in i2c_stm32_target_unregister()
436 if (!data->smbalert_active) { in i2c_stm32_target_unregister()
442 /* Disable wake-up from STOP */ in i2c_stm32_target_unregister()
450 data->slave_attached = false; in i2c_stm32_target_unregister()
459 const struct i2c_stm32_config *cfg = dev->config; in stm32_i2c_event()
460 struct i2c_stm32_data *data = dev->data; in stm32_i2c_event()
461 I2C_TypeDef *i2c = cfg->i2c; in stm32_i2c_event()
464 if (data->slave_attached && !data->master_active) { in stm32_i2c_event()
469 if (data->current.len) { in stm32_i2c_event()
472 LL_I2C_TransmitData8(i2c, *data->current.buf); in stm32_i2c_event()
477 *data->current.buf = LL_I2C_ReceiveData8(i2c); in stm32_i2c_event()
480 data->current.buf++; in stm32_i2c_event()
481 data->current.len--; in stm32_i2c_event()
487 data->current.is_nack = 1U; in stm32_i2c_event()
507 if (data->current.msg->flags & I2C_MSG_STOP) { in stm32_i2c_event()
511 k_sem_give(&data->device_sync_sem); in stm32_i2c_event()
522 const struct i2c_stm32_config *cfg = dev->config; in stm32_i2c_error()
523 struct i2c_stm32_data *data = dev->data; in stm32_i2c_error()
524 I2C_TypeDef *i2c = cfg->i2c; in stm32_i2c_error()
527 if (data->slave_attached && !data->master_active) { in stm32_i2c_error()
535 data->current.is_arlo = 1U; in stm32_i2c_error()
541 data->current.is_err = 1U; in stm32_i2c_error()
548 if (data->smbalert_cb_func != NULL) { in stm32_i2c_error()
549 data->smbalert_cb_func(data->smbalert_cb_dev); in stm32_i2c_error()
558 return -EIO; in stm32_i2c_error()
591 const struct i2c_stm32_config *cfg = dev->config; in stm32_i2c_msg_write()
592 struct i2c_stm32_data *data = dev->data; in stm32_i2c_msg_write()
593 I2C_TypeDef *i2c = cfg->i2c; in stm32_i2c_msg_write()
596 data->current.len = msg->len; in stm32_i2c_msg_write()
597 data->current.buf = msg->buf; in stm32_i2c_msg_write()
598 data->current.is_write = 1U; in stm32_i2c_msg_write()
599 data->current.is_nack = 0U; in stm32_i2c_msg_write()
600 data->current.is_err = 0U; in stm32_i2c_msg_write()
601 data->current.msg = msg; in stm32_i2c_msg_write()
608 if (k_sem_take(&data->device_sync_sem, in stm32_i2c_msg_write()
611 k_sem_take(&data->device_sync_sem, K_FOREVER); in stm32_i2c_msg_write()
615 if (data->current.is_nack || data->current.is_err || in stm32_i2c_msg_write()
616 data->current.is_arlo || is_timeout) { in stm32_i2c_msg_write()
622 if (data->current.is_arlo) { in stm32_i2c_msg_write()
624 data->current.is_arlo); in stm32_i2c_msg_write()
625 data->current.is_arlo = 0U; in stm32_i2c_msg_write()
628 if (data->current.is_nack) { in stm32_i2c_msg_write()
630 data->current.is_nack = 0U; in stm32_i2c_msg_write()
633 if (data->current.is_err) { in stm32_i2c_msg_write()
635 data->current.is_err); in stm32_i2c_msg_write()
636 data->current.is_err = 0U; in stm32_i2c_msg_write()
643 return -EIO; in stm32_i2c_msg_write()
649 const struct i2c_stm32_config *cfg = dev->config; in stm32_i2c_msg_read()
650 struct i2c_stm32_data *data = dev->data; in stm32_i2c_msg_read()
651 I2C_TypeDef *i2c = cfg->i2c; in stm32_i2c_msg_read()
654 data->current.len = msg->len; in stm32_i2c_msg_read()
655 data->current.buf = msg->buf; in stm32_i2c_msg_read()
656 data->current.is_write = 0U; in stm32_i2c_msg_read()
657 data->current.is_arlo = 0U; in stm32_i2c_msg_read()
658 data->current.is_err = 0U; in stm32_i2c_msg_read()
659 data->current.is_nack = 0U; in stm32_i2c_msg_read()
660 data->current.msg = msg; in stm32_i2c_msg_read()
667 if (k_sem_take(&data->device_sync_sem, in stm32_i2c_msg_read()
670 k_sem_take(&data->device_sync_sem, K_FOREVER); in stm32_i2c_msg_read()
674 if (data->current.is_nack || data->current.is_err || in stm32_i2c_msg_read()
675 data->current.is_arlo || is_timeout) { in stm32_i2c_msg_read()
681 if (data->current.is_arlo) { in stm32_i2c_msg_read()
683 data->current.is_arlo); in stm32_i2c_msg_read()
684 data->current.is_arlo = 0U; in stm32_i2c_msg_read()
687 if (data->current.is_nack) { in stm32_i2c_msg_read()
689 data->current.is_nack = 0U; in stm32_i2c_msg_read()
692 if (data->current.is_err) { in stm32_i2c_msg_read()
694 data->current.is_err); in stm32_i2c_msg_read()
695 data->current.is_err = 0U; in stm32_i2c_msg_read()
702 return -EIO; in stm32_i2c_msg_read()
708 const struct i2c_stm32_config *cfg = dev->config; in check_errors()
709 I2C_TypeDef *i2c = cfg->i2c; in check_errors()
740 return -EIO; in check_errors()
746 const struct i2c_stm32_config *cfg = dev->config; in msg_done()
747 I2C_TypeDef *i2c = cfg->i2c; in msg_done()
752 return -EIO; in msg_done()
771 const struct i2c_stm32_config *cfg = dev->config; in stm32_i2c_msg_write()
772 I2C_TypeDef *i2c = cfg->i2c; in stm32_i2c_msg_write()
774 uint8_t *buf = msg->buf; in stm32_i2c_msg_write()
778 len = msg->len; in stm32_i2c_msg_write()
786 return -EIO; in stm32_i2c_msg_write()
792 len--; in stm32_i2c_msg_write()
795 return msg_done(dev, msg->flags); in stm32_i2c_msg_write()
801 const struct i2c_stm32_config *cfg = dev->config; in stm32_i2c_msg_read()
802 I2C_TypeDef *i2c = cfg->i2c; in stm32_i2c_msg_read()
804 uint8_t *buf = msg->buf; in stm32_i2c_msg_read()
808 len = msg->len; in stm32_i2c_msg_read()
812 return -EIO; in stm32_i2c_msg_read()
818 len--; in stm32_i2c_msg_read()
821 return msg_done(dev, msg->flags); in stm32_i2c_msg_read()
828 * "DEEP_INDENTATION: Too many leading tabs - consider code refactoring
837 int32_t error = (int32_t)tscl - (int32_t)ti2cspeed; \
840 error = -error; \
845 i2c_valid_timing[count].scll = scll; \
846 i2c_valid_timing[count].sclh = sclh; \
847 ret = count; \
884 for (uint32_t count = 0; count < STM32_I2C_VALID_TIMING_NBR; count++) { in i2c_compute_scll_sclh() local
886 uint32_t tpresc = (i2c_valid_timing[count].presc + 1U) * ti2cclk; in i2c_compute_scll_sclh()
895 * tI2CCLK < (tLOW - tfilters) / 4 and tI2CCLK < tHIGH in i2c_compute_scll_sclh()
898 (ti2cclk < ((tscl_l - tafdel_min - dnf_delay) / 4U))) { in i2c_compute_scll_sclh()
924 * "DEEP_INDENTATION: Too many leading tabs - consider code refactoring
968 * SDADEL >= {tf +tHD;DAT(min) - tAF(min) - tDNF - [3 x tI2CCLK]} / {tPRESC} in i2c_compute_presc_scldel_sdadel()
969 * SDADEL <= {tVD;DAT(max) - tr - tAF(max) - tDNF- [4 x tI2CCLK]} / {tPRESC} in i2c_compute_presc_scldel_sdadel()
972 (int32_t)stm32_i2c_charac[i2c_speed].hddat_min - in i2c_compute_presc_scldel_sdadel()
973 (int32_t)tafdel_min - in i2c_compute_presc_scldel_sdadel()
977 tsdadel_max = (int32_t)stm32_i2c_charac[i2c_speed].vddat_max - in i2c_compute_presc_scldel_sdadel()
978 (int32_t)stm32_i2c_charac[i2c_speed].trise - in i2c_compute_presc_scldel_sdadel()
979 (int32_t)tafdel_max - in i2c_compute_presc_scldel_sdadel()
983 /* {[tr+ tSU;DAT(min)] / [tPRESC]} - 1 <= SCLDEL */ in i2c_compute_presc_scldel_sdadel()
1018 const struct i2c_stm32_config *cfg = dev->config; in stm32_i2c_configure_timing()
1019 struct i2c_stm32_data *data = dev->data; in stm32_i2c_configure_timing()
1020 I2C_TypeDef *i2c = cfg->i2c; in stm32_i2c_configure_timing()
1024 uint32_t i2c_freq = cfg->bitrate; in stm32_i2c_configure_timing()
1026 /* Reset valid timing count at the beginning of each new computation */ in stm32_i2c_configure_timing()
1049 data->current_timing.periph_clock = clock; in stm32_i2c_configure_timing()
1050 data->current_timing.i2c_speed = i2c_freq; in stm32_i2c_configure_timing()
1051 data->current_timing.timing_setting = timing; in stm32_i2c_configure_timing()
1061 const struct i2c_stm32_config *cfg = dev->config; in stm32_i2c_configure_timing()
1062 struct i2c_stm32_data *data = dev->data; in stm32_i2c_configure_timing()
1063 I2C_TypeDef *i2c = cfg->i2c; in stm32_i2c_configure_timing()
1070 for (uint32_t i = 0; i < cfg->n_timings; i++) { in stm32_i2c_configure_timing()
1071 const struct i2c_config_timing *preset = &cfg->timings[i]; in stm32_i2c_configure_timing()
1072 uint32_t speed = i2c_map_dt_bitrate(preset->i2c_speed); in stm32_i2c_configure_timing()
1074 if ((I2C_SPEED_GET(speed) == I2C_SPEED_GET(data->dev_config)) in stm32_i2c_configure_timing()
1075 && (preset->periph_clock == clock)) { in stm32_i2c_configure_timing()
1077 LL_I2C_SetTiming(i2c, preset->timing_setting); in stm32_i2c_configure_timing()
1083 switch (I2C_SPEED_GET(data->dev_config)) { in stm32_i2c_configure_timing()
1098 "see \"timings\" property of st,stm32-i2c-v2 devicetree binding"); in stm32_i2c_configure_timing()
1099 return -EINVAL; in stm32_i2c_configure_timing()
1111 if ((sclh - 1) > 255 || (scll - 1) > 255) { in stm32_i2c_configure_timing()
1116 if (sdadel > 15 || (scldel - 1) > 15) { in stm32_i2c_configure_timing()
1121 timing = __LL_I2C_CONVERT_TIMINGS(presc - 1, in stm32_i2c_configure_timing()
1122 scldel - 1, sdadel, sclh - 1, scll - 1); in stm32_i2c_configure_timing()
1128 return -EINVAL; in stm32_i2c_configure_timing()
1150 * bits turned off. This will cause the backend to use reload-mode, in stm32_i2c_transaction()
1162 do { /* do ... while to allow zero-length transactions */ in stm32_i2c_transaction()
1179 rest -= msg.len; in stm32_i2c_transaction()