20 static uint16_t round_up_divide(uint32_t a, uint32_t b) {
21 const uint32_t result = ((a - 1) / b) + 1;
22 return (uint16_t)result;
25 static void spin_while_status_bit(
const dif_i2c_t *i2c, uint32_t bit,
29 reg = mmio_region_read32(i2c->
base_addr, I2C_STATUS_REG_OFFSET);
38 if (i2c == NULL ||
status == NULL) {
42 uint32_t reg = mmio_region_read32(i2c->
base_addr, I2C_CTRL_REG_OFFSET);
47 reg = mmio_region_read32(i2c->
base_addr, I2C_STATUS_REG_OFFSET);
66 if (i2c == NULL || events == NULL) {
70 mmio_region_read32(i2c->
base_addr, I2C_CONTROLLER_EVENTS_REG_OFFSET);
74 reg, I2C_CONTROLLER_EVENTS_UNHANDLED_NACK_TIMEOUT_BIT);
91 I2C_CONTROLLER_EVENTS_UNHANDLED_NACK_TIMEOUT_BIT,
97 mmio_region_write32(i2c->
base_addr, I2C_CONTROLLER_EVENTS_REG_OFFSET, reg);
103 if (i2c == NULL || events == NULL) {
107 mmio_region_read32(i2c->
base_addr, I2C_TARGET_EVENTS_REG_OFFSET);
129 mmio_region_write32(i2c->
base_addr, I2C_TARGET_EVENTS_REG_OFFSET, reg);
140 uint32_t clock_period_nanos) {
147 .scl_time_high_cycles = round_up_divide(4000, clock_period_nanos),
148 .scl_time_low_cycles = round_up_divide(4700, clock_period_nanos),
149 .start_signal_setup_cycles =
150 round_up_divide(4700, clock_period_nanos),
151 .start_signal_hold_cycles = round_up_divide(4000, clock_period_nanos),
152 .data_signal_setup_cycles = round_up_divide(250, clock_period_nanos),
153 .data_signal_hold_cycles = 1,
154 .stop_signal_setup_cycles = round_up_divide(4000, clock_period_nanos),
155 .stop_signal_hold_cycles = round_up_divide(4700, clock_period_nanos),
159 .scl_time_high_cycles = round_up_divide(600, clock_period_nanos),
160 .scl_time_low_cycles = round_up_divide(1300, clock_period_nanos),
161 .start_signal_setup_cycles = round_up_divide(600, clock_period_nanos),
162 .start_signal_hold_cycles = round_up_divide(600, clock_period_nanos),
163 .data_signal_setup_cycles = round_up_divide(100, clock_period_nanos),
164 .data_signal_hold_cycles = 1,
165 .stop_signal_setup_cycles = round_up_divide(600, clock_period_nanos),
166 .stop_signal_hold_cycles = round_up_divide(1300, clock_period_nanos),
170 .scl_time_high_cycles = round_up_divide(260, clock_period_nanos),
171 .scl_time_low_cycles = round_up_divide(500, clock_period_nanos),
172 .start_signal_setup_cycles = round_up_divide(260, clock_period_nanos),
173 .start_signal_hold_cycles = round_up_divide(260, clock_period_nanos),
174 .data_signal_setup_cycles = round_up_divide(50, clock_period_nanos),
175 .data_signal_hold_cycles = 1,
176 .stop_signal_setup_cycles = round_up_divide(260, clock_period_nanos),
177 .stop_signal_hold_cycles = round_up_divide(500, clock_period_nanos),
184 static const uint32_t kNanosPerKBaud = 1000000;
188 if (config == NULL) {
191 uint32_t lowest_target_device_speed_khz;
194 lowest_target_device_speed_khz = 100;
197 lowest_target_device_speed_khz = 400;
200 lowest_target_device_speed_khz = 1000;
212 config->rise_cycles = round_up_divide(timing_config.
sda_rise_nanos,
214 config->fall_cycles = round_up_divide(timing_config.
sda_fall_nanos,
218 uint32_t slowest_scl_period_nanos =
219 kNanosPerKBaud / lowest_target_device_speed_khz;
220 if (scl_period_nanos < slowest_scl_period_nanos) {
221 scl_period_nanos = slowest_scl_period_nanos;
223 uint16_t scl_period_cycles =
227 int32_t lengthened_high_cycles = scl_period_cycles -
228 config->scl_time_low_cycles -
229 config->rise_cycles - config->fall_cycles;
230 if (lengthened_high_cycles > (int32_t)config->scl_time_high_cycles) {
231 if (lengthened_high_cycles < 0 || lengthened_high_cycles > UINT16_MAX) {
234 config->scl_time_high_cycles = (uint16_t)lengthened_high_cycles;
254 uint32_t timing0 = 0;
256 config.scl_time_high_cycles);
258 config.scl_time_low_cycles);
259 mmio_region_write32(i2c->
base_addr, I2C_TIMING0_REG_OFFSET, timing0);
261 uint32_t timing1 = 0;
266 mmio_region_write32(i2c->
base_addr, I2C_TIMING1_REG_OFFSET, timing1);
268 uint32_t timing2 = 0;
270 config.start_signal_setup_cycles);
272 config.start_signal_hold_cycles);
273 mmio_region_write32(i2c->
base_addr, I2C_TIMING2_REG_OFFSET, timing2);
275 uint32_t timing3 = 0;
277 config.data_signal_setup_cycles);
279 config.data_signal_hold_cycles);
280 mmio_region_write32(i2c->
base_addr, I2C_TIMING3_REG_OFFSET, timing3);
282 uint32_t timing4 = 0;
284 config.stop_signal_setup_cycles);
287 mmio_region_write32(i2c->
base_addr, I2C_TIMING4_REG_OFFSET, timing4);
297 uint32_t reg = mmio_region_read32(i2c->
base_addr, I2C_FIFO_CTRL_REG_OFFSET);
299 mmio_region_write32(i2c->
base_addr, I2C_FIFO_CTRL_REG_OFFSET, reg);
309 uint32_t reg = mmio_region_read32(i2c->
base_addr, I2C_FIFO_CTRL_REG_OFFSET);
311 mmio_region_write32(i2c->
base_addr, I2C_FIFO_CTRL_REG_OFFSET, reg);
321 uint32_t reg = mmio_region_read32(i2c->
base_addr, I2C_FIFO_CTRL_REG_OFFSET);
323 mmio_region_write32(i2c->
base_addr, I2C_FIFO_CTRL_REG_OFFSET, reg);
333 uint32_t reg = mmio_region_read32(i2c->
base_addr, I2C_FIFO_CTRL_REG_OFFSET);
335 mmio_region_write32(i2c->
base_addr, I2C_FIFO_CTRL_REG_OFFSET, reg);
346 if (i2c == NULL || rx_level > I2C_PARAM_FIFO_DEPTH ||
347 fmt_level >= I2C_PARAM_FIFO_DEPTH) {
351 uint32_t ctrl_value =
352 mmio_region_read32(i2c->
base_addr, I2C_HOST_FIFO_CONFIG_REG_OFFSET);
354 ctrl_value, I2C_HOST_FIFO_CONFIG_RX_THRESH_FIELD, rx_level);
356 ctrl_value, I2C_HOST_FIFO_CONFIG_FMT_THRESH_FIELD, fmt_level);
357 mmio_region_write32(i2c->
base_addr, I2C_HOST_FIFO_CONFIG_REG_OFFSET,
369 if (i2c == NULL || acq_level > I2C_PARAM_ACQ_FIFO_DEPTH ||
370 tx_level >= I2C_PARAM_FIFO_DEPTH) {
374 uint32_t ctrl_value =
375 mmio_region_read32(i2c->
base_addr, I2C_TARGET_FIFO_CONFIG_REG_OFFSET);
377 ctrl_value, I2C_TARGET_FIFO_CONFIG_TX_THRESH_FIELD, tx_level);
379 ctrl_value, I2C_TARGET_FIFO_CONFIG_ACQ_THRESH_FIELD, acq_level);
380 mmio_region_write32(i2c->
base_addr, I2C_TARGET_FIFO_CONFIG_REG_OFFSET,
397 uint32_t reg = mmio_region_read32(i2c->
base_addr, I2C_CTRL_REG_OFFSET);
399 mmio_region_write32(i2c->
base_addr, I2C_CTRL_REG_OFFSET, reg);
415 uint32_t reg = mmio_region_read32(i2c->
base_addr, I2C_CTRL_REG_OFFSET);
417 mmio_region_write32(i2c->
base_addr, I2C_CTRL_REG_OFFSET, reg);
433 uint32_t reg = mmio_region_read32(i2c->
base_addr, I2C_CTRL_REG_OFFSET);
435 mmio_region_write32(i2c->
base_addr, I2C_CTRL_REG_OFFSET, reg);
451 uint32_t reg = mmio_region_read32(i2c->
base_addr, I2C_CTRL_REG_OFFSET);
453 mmio_region_write32(i2c->
base_addr, I2C_CTRL_REG_OFFSET, reg);
469 uint32_t reg = mmio_region_read32(i2c->
base_addr, I2C_CTRL_REG_OFFSET);
471 mmio_region_write32(i2c->
base_addr, I2C_CTRL_REG_OFFSET, reg);
487 uint32_t reg = mmio_region_read32(i2c->
base_addr, I2C_CTRL_REG_OFFSET);
490 mmio_region_write32(i2c->
base_addr, I2C_CTRL_REG_OFFSET, reg);
506 uint32_t reg = mmio_region_read32(i2c->
base_addr, I2C_CTRL_REG_OFFSET);
508 mmio_region_write32(i2c->
base_addr, I2C_CTRL_REG_OFFSET, reg);
524 uint32_t reg = mmio_region_read32(i2c->
base_addr, I2C_OVRD_REG_OFFSET);
526 mmio_region_write32(i2c->
base_addr, I2C_OVRD_REG_OFFSET, reg);
537 uint32_t override_val =
538 mmio_region_read32(i2c->
base_addr, I2C_OVRD_REG_OFFSET);
541 mmio_region_write32(i2c->
base_addr, I2C_OVRD_REG_OFFSET, override_val);
547 uint16_t *scl_samples,
548 uint16_t *sda_samples) {
553 uint32_t samples = mmio_region_read32(i2c->
base_addr, I2C_VAL_REG_OFFSET);
554 if (scl_samples != NULL) {
559 if (sda_samples != NULL) {
578 mmio_region_read32(i2c->
base_addr, I2C_HOST_FIFO_STATUS_REG_OFFSET);
579 if (fmt_fifo_level != NULL) {
581 values, I2C_HOST_FIFO_STATUS_FMTLVL_FIELD);
583 if (rx_fifo_level != NULL) {
585 values, I2C_HOST_FIFO_STATUS_RXLVL_FIELD);
590 mmio_region_read32(i2c->
base_addr, I2C_TARGET_FIFO_STATUS_REG_OFFSET);
591 if (tx_fifo_level != NULL) {
593 values, I2C_TARGET_FIFO_STATUS_TXLVL_FIELD);
595 if (acq_fifo_level != NULL) {
597 values, I2C_TARGET_FIFO_STATUS_ACQLVL_FIELD);
604 if (i2c == NULL || count == NULL) {
609 mmio_region_read32(i2c->
base_addr, I2C_TARGET_ACK_CTRL_REG_OFFSET);
620 if (count > I2C_TARGET_ACK_CTRL_NBYTES_MASK) {
626 mmio_region_write32(i2c->
base_addr, I2C_TARGET_ACK_CTRL_REG_OFFSET, reg);
637 mmio_region_write32(i2c->
base_addr, I2C_TARGET_ACK_CTRL_REG_OFFSET, reg);
643 if (i2c == NULL || data == NULL) {
648 mmio_region_read32(i2c->
base_addr, I2C_ACQ_FIFO_NEXT_DATA_REG_OFFSET);
658 uint32_t values = mmio_region_read32(i2c->
base_addr, I2C_RDATA_REG_OFFSET);
668 if (i2c == NULL || buffer == NULL) {
673 spin_while_status_bit(i2c, I2C_STATUS_RXEMPTY_BIT,
true);
674 uint32_t values = mmio_region_read32(i2c->
base_addr, I2C_RDATA_REG_OFFSET);
682 uint32_t *fmt_byte) {
687 if (has_write_flags && has_read_flags) {
706 const uint8_t *bytes,
708 if (i2c == NULL || bytes == NULL || size == 0) {
711 uint32_t fmt_byte = 0;
714 for (
size_t i = 0; i < size; ++i) {
717 mmio_region_write32(i2c->
base_addr, I2C_FDATA_REG_OFFSET, reg);
718 spin_while_status_bit(i2c, I2C_STATUS_FMTFULL_BIT,
true);
730 uint32_t fmt_byte = 0;
733 mmio_region_write32(i2c->
base_addr, I2C_FDATA_REG_OFFSET, fmt_byte);
745 if (suppress_nak_irq) {
790 uint32_t tx_byte = 0;
792 mmio_region_write32(i2c->
base_addr, I2C_TXDATA_REG_OFFSET, tx_byte);
804 mmio_region_read32(i2c->
base_addr, I2C_ACQDATA_REG_OFFSET);
808 if (signal != NULL) {
815 dif_i2c_scl_timeout_t timeout_type,
821 bool timeout_en =
false;
822 uint8_t timeout_mode = 0;
823 switch (timeout_type) {
829 timeout_mode = I2C_TIMEOUT_CTRL_MODE_VALUE_STRETCH_TIMEOUT;
833 timeout_mode = I2C_TIMEOUT_CTRL_MODE_VALUE_BUS_TIMEOUT;
844 mmio_region_write32(i2c->
base_addr, I2C_TIMEOUT_CTRL_REG_OFFSET, config);
876 mmio_region_write32(i2c->
base_addr, I2C_TARGET_ID_REG_OFFSET, config);
885 mmio_region_write32(i2c->
base_addr, I2C_HOST_TIMEOUT_CTRL_REG_OFFSET, cycles);