Lines Matching +full:bus +full:- +full:idle +full:- +full:timeout

5  * SPDX-License-Identifier: Apache-2.0
10 /* Include esp-idf headers first to avoid redefining BIT() macro */
31 #include "i2c-priv.h"
36 #define I2C_TRANSFER_TIMEOUT_MSEC 500 /* Transfer timeout period */
44 #define I2C_CLOCK_INVALID (-1)
49 I2C_STATUS_IDLE, /* idle status for current master command */
52 I2C_STATUS_TIMEOUT, /* I2C bus status error, and operation timeout */
151 const struct i2c_esp32_config *config = dev->config; in i2c_esp32_config_pin()
154 if (config->index >= SOC_I2C_NUM) { in i2c_esp32_config_pin()
156 return -EINVAL; in i2c_esp32_config_pin()
159 gpio_pin_set_dt(&config->sda.gpio, 1); in i2c_esp32_config_pin()
160 ret = gpio_pin_configure_dt(&config->sda.gpio, GPIO_PULL_UP | GPIO_OUTPUT | GPIO_INPUT); in i2c_esp32_config_pin()
161 esp_rom_gpio_matrix_out(config->sda.gpio.pin, config->sda.sig_out, 0, 0); in i2c_esp32_config_pin()
162 esp_rom_gpio_matrix_in(config->sda.gpio.pin, config->sda.sig_in, 0); in i2c_esp32_config_pin()
164 gpio_pin_set_dt(&config->scl.gpio, 1); in i2c_esp32_config_pin()
165 ret |= gpio_pin_configure_dt(&config->scl.gpio, GPIO_PULL_UP | GPIO_OUTPUT | GPIO_INPUT); in i2c_esp32_config_pin()
166 esp_rom_gpio_matrix_out(config->scl.gpio.pin, config->scl.sig_out, 0, 0); in i2c_esp32_config_pin()
167 esp_rom_gpio_matrix_in(config->scl.gpio.pin, config->scl.sig_in, 0); in i2c_esp32_config_pin()
174 * in this case, master should send several clock to make the slave release the bus.
176 * in this case, master device could send a stop signal to make esp32 slave release the bus.
180 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_master_clear_bus()
183 const struct i2c_esp32_config *config = dev->config; in i2c_master_clear_bus()
187 gpio_pin_configure_dt(&config->scl.gpio, GPIO_OUTPUT); in i2c_master_clear_bus()
188 gpio_pin_configure_dt(&config->sda.gpio, GPIO_OUTPUT | GPIO_INPUT); in i2c_master_clear_bus()
189 /* If a SLAVE device was in a read operation when the bus was interrupted, */ in i2c_master_clear_bus()
194 gpio_pin_set_dt(&config->sda.gpio, 1); in i2c_master_clear_bus()
196 while (!gpio_pin_get_dt(&config->sda.gpio) && (i++ < I2C_CLR_BUS_SCL_NUM)) { in i2c_master_clear_bus()
197 gpio_pin_set_dt(&config->scl.gpio, 1); in i2c_master_clear_bus()
199 gpio_pin_set_dt(&config->scl.gpio, 0); in i2c_master_clear_bus()
202 gpio_pin_set_dt(&config->sda.gpio, 0); /* setup for STOP */ in i2c_master_clear_bus()
203 gpio_pin_set_dt(&config->scl.gpio, 1); in i2c_master_clear_bus()
205 gpio_pin_set_dt(&config->sda.gpio, 1); /* STOP, SDA low -> high while SCL is HIGH */ in i2c_master_clear_bus()
208 i2c_ll_master_clr_bus(data->hal.dev); in i2c_master_clear_bus()
210 i2c_ll_update(data->hal.dev); in i2c_master_clear_bus()
215 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_hw_fsm_reset()
218 const struct i2c_esp32_config *config = dev->config; in i2c_hw_fsm_reset()
223 int timeout; in i2c_hw_fsm_reset() local
226 i2c_ll_get_scl_timing(data->hal.dev, &scl_high_period, &scl_low_period); in i2c_hw_fsm_reset()
227 i2c_ll_get_start_timing(data->hal.dev, &scl_rstart_setup, &scl_start_hold); in i2c_hw_fsm_reset()
228 i2c_ll_get_stop_timing(data->hal.dev, &scl_stop_setup, &scl_stop_hold); in i2c_hw_fsm_reset()
229 i2c_ll_get_sda_timing(data->hal.dev, &sda_sample, &sda_hold); in i2c_hw_fsm_reset()
230 i2c_ll_get_tout(data->hal.dev, &timeout); in i2c_hw_fsm_reset()
231 i2c_ll_get_filter(data->hal.dev, &filter_cfg); in i2c_hw_fsm_reset()
233 /* to reset the I2C hw module, we need re-enable the hw */ in i2c_hw_fsm_reset()
234 clock_control_off(config->clock_dev, config->clock_subsys); in i2c_hw_fsm_reset()
236 clock_control_on(config->clock_dev, config->clock_subsys); in i2c_hw_fsm_reset()
238 i2c_hal_master_init(&data->hal); in i2c_hw_fsm_reset()
239 i2c_ll_disable_intr_mask(data->hal.dev, I2C_LL_INTR_MASK); in i2c_hw_fsm_reset()
240 i2c_ll_clear_intr_mask(data->hal.dev, I2C_LL_INTR_MASK); in i2c_hw_fsm_reset()
241 i2c_ll_set_scl_timing(data->hal.dev, scl_high_period, scl_low_period); in i2c_hw_fsm_reset()
242 i2c_ll_set_start_timing(data->hal.dev, scl_rstart_setup, scl_start_hold); in i2c_hw_fsm_reset()
243 i2c_ll_set_stop_timing(data->hal.dev, scl_stop_setup, scl_stop_hold); in i2c_hw_fsm_reset()
244 i2c_ll_set_sda_timing(data->hal.dev, sda_sample, sda_hold); in i2c_hw_fsm_reset()
245 i2c_ll_set_tout(data->hal.dev, timeout); in i2c_hw_fsm_reset()
246 i2c_ll_set_filter(data->hal.dev, filter_cfg); in i2c_hw_fsm_reset()
248 i2c_ll_master_fsm_rst(data->hal.dev); in i2c_hw_fsm_reset()
251 i2c_ll_update(data->hal.dev); in i2c_hw_fsm_reset()
256 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_recover()
258 k_sem_take(&data->transfer_sem, K_FOREVER); in i2c_esp32_recover()
260 k_sem_give(&data->transfer_sem); in i2c_esp32_recover()
267 const struct i2c_esp32_config *config = dev->config; in i2c_esp32_configure_bitrate()
268 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_configure_bitrate()
273 i2c_hal_set_bus_timing(&data->hal, bitrate, sclk, clk_freq_mhz); in i2c_esp32_configure_bitrate()
275 if (config->scl_timeout > 0) { in i2c_esp32_configure_bitrate()
277 clk_freq_mhz / MHZ(1) * config->scl_timeout); in i2c_esp32_configure_bitrate()
278 i2c_ll_set_tout(data->hal.dev, timeout_cycles); in i2c_esp32_configure_bitrate()
279 LOG_DBG("SCL timeout: %d us, value: %d", config->scl_timeout, timeout_cycles); in i2c_esp32_configure_bitrate()
281 /* Disabling the timeout by clearing the I2C_TIME_OUT_EN bit does not seem to work, in i2c_esp32_configure_bitrate()
282 * at least for ESP32-C3 (tested with communication to bq76952 chip). So we set the in i2c_esp32_configure_bitrate()
283 * timeout to maximum supported value instead. in i2c_esp32_configure_bitrate()
285 i2c_ll_set_tout(data->hal.dev, I2C_LL_MAX_TIMEOUT); in i2c_esp32_configure_bitrate()
288 i2c_ll_update(data->hal.dev); in i2c_esp32_configure_bitrate()
293 const struct i2c_esp32_config *config = dev->config; in i2c_esp32_configure_data_mode()
294 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_configure_data_mode()
299 if (config->mode.tx_lsb_first) { in i2c_esp32_configure_data_mode()
303 if (config->mode.rx_lsb_first) { in i2c_esp32_configure_data_mode()
307 i2c_ll_set_data_mode(data->hal.dev, tx_mode, rx_mode); in i2c_esp32_configure_data_mode()
308 i2c_ll_set_filter(data->hal.dev, I2C_FILTER_CYC_NUM_DEF); in i2c_esp32_configure_data_mode()
309 i2c_ll_update(data->hal.dev); in i2c_esp32_configure_data_mode()
315 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_configure()
320 return -ENOTSUP; in i2c_esp32_configure()
335 return -ENOTSUP; in i2c_esp32_configure()
338 k_sem_take(&data->transfer_sem, K_FOREVER); in i2c_esp32_configure()
340 data->dev_config = dev_config; in i2c_esp32_configure()
344 k_sem_give(&data->transfer_sem); in i2c_esp32_configure()
351 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_get_config()
353 if (data->dev_config == 0) { in i2c_esp32_get_config()
355 return -EIO; in i2c_esp32_get_config()
358 *config = data->dev_config; in i2c_esp32_get_config()
365 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_reset_fifo()
368 i2c_ll_txfifo_rst(data->hal.dev); in i2c_esp32_reset_fifo()
369 i2c_ll_rxfifo_rst(data->hal.dev); in i2c_esp32_reset_fifo()
374 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_transmit()
378 i2c_ll_update(data->hal.dev); in i2c_esp32_transmit()
379 i2c_ll_trans_start(data->hal.dev); in i2c_esp32_transmit()
380 data->cmd_idx = 0; in i2c_esp32_transmit()
382 ret = k_sem_take(&data->cmd_sem, K_MSEC(I2C_TRANSFER_TIMEOUT_MSEC)); in i2c_esp32_transmit()
388 return -ETIMEDOUT; in i2c_esp32_transmit()
391 if (data->status == I2C_STATUS_TIMEOUT) { in i2c_esp32_transmit()
393 ret = -ETIMEDOUT; in i2c_esp32_transmit()
394 } else if (data->status == I2C_STATUS_ACK_ERROR) { in i2c_esp32_transmit()
395 ret = -EFAULT; in i2c_esp32_transmit()
403 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_master_start()
409 i2c_ll_write_cmd_reg(data->hal.dev, cmd, data->cmd_idx++); in i2c_esp32_master_start()
414 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_master_stop()
420 i2c_ll_write_cmd_reg(data->hal.dev, cmd, data->cmd_idx++); in i2c_esp32_master_stop()
425 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_write_addr()
429 data->status = I2C_STATUS_WRITE; in i2c_esp32_write_addr()
432 i2c_ll_write_txfifo(data->hal.dev, &addr_byte, 1); in i2c_esp32_write_addr()
433 if (data->dev_config & I2C_ADDR_10_BITS) { in i2c_esp32_write_addr()
435 i2c_ll_write_txfifo(data->hal.dev, &addr_byte, 1); in i2c_esp32_write_addr()
449 i2c_ll_write_cmd_reg(data->hal.dev, cmd, data->cmd_idx++); in i2c_esp32_write_addr()
450 i2c_ll_write_cmd_reg(data->hal.dev, cmd_end, data->cmd_idx++); in i2c_esp32_write_addr()
451 i2c_ll_master_enable_tx_it(data->hal.dev); in i2c_esp32_write_addr()
458 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_master_read()
460 uint32_t msg_len = msg->len; in i2c_esp32_master_read()
461 uint8_t *msg_buf = msg->buf; in i2c_esp32_master_read()
465 data->status = I2C_STATUS_READ; in i2c_esp32_master_read()
475 rd_filled = (msg_len > SOC_I2C_FIFO_LEN) ? SOC_I2C_FIFO_LEN : (msg_len - 1); in i2c_esp32_master_read()
489 i2c_ll_write_cmd_reg(data->hal.dev, cmd, data->cmd_idx++); in i2c_esp32_master_read()
490 i2c_ll_write_cmd_reg(data->hal.dev, cmd_end, data->cmd_idx++); in i2c_esp32_master_read()
491 i2c_ll_master_enable_tx_it(data->hal.dev); in i2c_esp32_master_read()
497 i2c_ll_read_rxfifo(data->hal.dev, msg_buf, rd_filled); in i2c_esp32_master_read()
499 msg_len -= rd_filled; in i2c_esp32_master_read()
513 if (msg->flags & I2C_MSG_RESTART) { in i2c_esp32_read_msg()
528 if (msg->flags & I2C_MSG_STOP) { in i2c_esp32_read_msg()
542 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_master_write()
544 uint32_t msg_len = msg->len; in i2c_esp32_master_write()
545 uint8_t *msg_buf = msg->buf; in i2c_esp32_master_write()
548 data->status = I2C_STATUS_WRITE; in i2c_esp32_master_write()
564 i2c_ll_write_txfifo(data->hal.dev, msg_buf, wr_filled); in i2c_esp32_master_write()
565 i2c_ll_write_cmd_reg(data->hal.dev, cmd, data->cmd_idx++); in i2c_esp32_master_write()
566 i2c_ll_write_cmd_reg(data->hal.dev, cmd_end, data->cmd_idx++); in i2c_esp32_master_write()
567 i2c_ll_master_enable_tx_it(data->hal.dev); in i2c_esp32_master_write()
575 msg_len -= wr_filled; in i2c_esp32_master_write()
586 if (msg->flags & I2C_MSG_RESTART) { in i2c_esp32_write_msg()
601 if (msg->flags & I2C_MSG_STOP) { in i2c_esp32_write_msg()
616 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_transfer()
618 uint32_t timeout = I2C_TRANSFER_TIMEOUT_MSEC * USEC_PER_MSEC; in i2c_esp32_transfer() local
625 while (i2c_ll_is_bus_busy(data->hal.dev)) { in i2c_esp32_transfer()
627 if (timeout-- == 0) { in i2c_esp32_transfer()
628 return -EBUSY; in i2c_esp32_transfer()
636 current->flags |= I2C_MSG_RESTART; in i2c_esp32_transfer()
643 if ((current->flags & I2C_MSG_RW_MASK) != (next->flags & I2C_MSG_RW_MASK)) { in i2c_esp32_transfer()
644 if (!(next->flags & I2C_MSG_RESTART)) { in i2c_esp32_transfer()
645 ret = -EINVAL; in i2c_esp32_transfer()
651 if (current->flags & I2C_MSG_STOP) { in i2c_esp32_transfer()
652 ret = -EINVAL; in i2c_esp32_transfer()
664 k_sem_take(&data->transfer_sem, K_FOREVER); in i2c_esp32_transfer()
667 addr &= BIT_MASK(data->dev_config & I2C_ADDR_10_BITS ? 10 : 7); in i2c_esp32_transfer()
670 for (; num_msgs > 0; num_msgs--, msgs++) { in i2c_esp32_transfer()
672 if (data->status == I2C_STATUS_TIMEOUT || i2c_ll_is_bus_busy(data->hal.dev)) { in i2c_esp32_transfer()
680 /* So we disable them when these two interrupt occurs and re-enable them here. */ in i2c_esp32_transfer()
681 i2c_ll_disable_intr_mask(data->hal.dev, I2C_LL_INTR_MASK); in i2c_esp32_transfer()
682 i2c_ll_clear_intr_mask(data->hal.dev, I2C_LL_INTR_MASK); in i2c_esp32_transfer()
684 if ((msgs->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ) { in i2c_esp32_transfer()
695 k_sem_give(&data->transfer_sem); in i2c_esp32_transfer()
703 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_isr()
706 if (data->status == I2C_STATUS_WRITE) { in i2c_esp32_isr()
707 i2c_hal_master_handle_tx_event(&data->hal, &evt_type); in i2c_esp32_isr()
708 } else if (data->status == I2C_STATUS_READ) { in i2c_esp32_isr()
709 i2c_hal_master_handle_rx_event(&data->hal, &evt_type); in i2c_esp32_isr()
713 data->status = I2C_STATUS_ACK_ERROR; in i2c_esp32_isr()
715 data->status = I2C_STATUS_TIMEOUT; in i2c_esp32_isr()
717 data->status = I2C_STATUS_TIMEOUT; in i2c_esp32_isr()
719 data->status = I2C_STATUS_DONE; in i2c_esp32_isr()
722 k_sem_give(&data->cmd_sem); in i2c_esp32_isr()
737 const struct i2c_esp32_config *config = dev->config; in i2c_esp32_init()
738 struct i2c_esp32_data *data = (struct i2c_esp32_data *const)(dev)->data; in i2c_esp32_init()
741 if (!gpio_is_ready_dt(&config->scl.gpio)) { in i2c_esp32_init()
743 return -EINVAL; in i2c_esp32_init()
746 if (!gpio_is_ready_dt(&config->sda.gpio)) { in i2c_esp32_init()
748 return -EINVAL; in i2c_esp32_init()
751 int ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); in i2c_esp32_init()
755 return -EINVAL; in i2c_esp32_init()
758 if (!device_is_ready(config->clock_dev)) { in i2c_esp32_init()
760 return -ENODEV; in i2c_esp32_init()
763 clock_control_on(config->clock_dev, config->clock_subsys); in i2c_esp32_init()
765 ret = esp_intr_alloc(config->irq_source, in i2c_esp32_init()
766 ESP_PRIO_TO_FLAGS(config->irq_priority) | in i2c_esp32_init()
767 ESP_INT_FLAGS_CHECK(config->irq_flags) | ESP_INTR_FLAG_IRAM, in i2c_esp32_init()
777 i2c_hal_master_init(&data->hal); in i2c_esp32_init()
781 return i2c_esp32_configure(dev, I2C_MODE_CONTROLLER | i2c_map_dt_bitrate(config->bitrate)); in i2c_esp32_init()
849 #error "Missing <sda-gpios> and <scl-gpios> properties to build for this target."
853 #error "Properties <sda-gpios> and <scl-gpios> are not required for this target."
862 #error "Missing <sda-gpios> and <scl-gpios> properties to build for this target."
866 #error "Properties <sda-gpios> and <scl-gpios> are not required for this target."