Lines Matching +full:bitrate +full:- +full:data
4 * SPDX-License-Identifier: Apache-2.0
18 #include "i2c-priv.h"
27 #define M_TRAN_DATA_ACK 0x28 /* Master Transmit Data ACK */
28 #define M_TRAN_DATA_NACK 0x30 /* Master Transmit Data NACK */
32 #define M_RECE_DATA_ACK 0x50 /* Master Receive Data ACK */
33 #define M_RECE_DATA_NACK 0x58 /* Master Receive Data NACK */
39 #define S_TRAN_DATA_ACK 0xB8 /* Slave Transmit Data ACK */
40 #define S_TRAN_DATA_NACK 0xC0 /* Slave Transmit Data NACK */
41 #define S_TRAN_LAST_DATA_ACK 0xC8 /* Slave Transmit Last Data ACK */
44 #define S_RECE_DATA_ACK 0x80 /* Slave Receive Data ACK */
45 #define S_RECE_DATA_NACK 0x88 /* Slave Receive Data NACK */
50 #define GC_DATA_ACK 0x90 /* GC mode Data ACK */
51 #define GC_DATA_NACK 0x98 /* GC mode Data NACK */
67 uint32_t bitrate; member
93 /* ACK/NACK last data byte, dependent on whether or not message merge is allowed */
96 const struct i2c_numaker_config *config = dev->config; in m_numaker_i2c_master_xfer_msg_read_last_byte()
97 struct i2c_numaker_data *data = dev->data; in m_numaker_i2c_master_xfer_msg_read_last_byte() local
98 I2C_T *i2c_base = config->i2c_base; in m_numaker_i2c_master_xfer_msg_read_last_byte()
101 __ASSERT_NO_MSG(data->master_xfer.msgs_pos < data->master_xfer.msgs_end); in m_numaker_i2c_master_xfer_msg_read_last_byte()
102 /* Should invoke with exactly one data byte remaining for read */ in m_numaker_i2c_master_xfer_msg_read_last_byte()
103 __ASSERT_NO_MSG((data->master_xfer.msgs_pos->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ); in m_numaker_i2c_master_xfer_msg_read_last_byte()
104 __ASSERT_NO_MSG((data->master_xfer.buf_end - data->master_xfer.buf_pos) == 1); in m_numaker_i2c_master_xfer_msg_read_last_byte()
107 bool do_stop_prev = data->master_xfer.msgs_pos->flags & I2C_MSG_STOP; in m_numaker_i2c_master_xfer_msg_read_last_byte()
110 data->master_xfer.msgs_pos++; in m_numaker_i2c_master_xfer_msg_read_last_byte()
113 if (data->master_xfer.msgs_pos < data->master_xfer.msgs_end) { in m_numaker_i2c_master_xfer_msg_read_last_byte()
115 struct i2c_msg *msgs_pos = data->master_xfer.msgs_pos; in m_numaker_i2c_master_xfer_msg_read_last_byte()
116 bool is_read_next = (msgs_pos->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ; in m_numaker_i2c_master_xfer_msg_read_last_byte()
117 bool do_restart_next = data->master_xfer.msgs_pos->flags & I2C_MSG_RESTART; in m_numaker_i2c_master_xfer_msg_read_last_byte()
130 /* NACK last data byte (required for Master Receiver) */ in m_numaker_i2c_master_xfer_msg_read_last_byte()
133 /* ACK last data byte, so to merge adjacent messages into one transaction */ in m_numaker_i2c_master_xfer_msg_read_last_byte()
137 /* NACK last data byte (required for Master Receiver) */ in m_numaker_i2c_master_xfer_msg_read_last_byte()
142 data->master_xfer.msgs_pos--; in m_numaker_i2c_master_xfer_msg_read_last_byte()
148 const struct i2c_numaker_config *config = dev->config; in m_numaker_i2c_master_xfer_end()
149 struct i2c_numaker_data *data = dev->data; in m_numaker_i2c_master_xfer_end() local
150 I2C_T *i2c_base = config->i2c_base; in m_numaker_i2c_master_xfer_end()
158 k_sem_give(&data->master_xfer.xfer_sync); in m_numaker_i2c_master_xfer_end()
162 /* Read next data byte, involving ACK/NACK last data byte and message merge */
165 const struct i2c_numaker_config *config = dev->config; in m_numaker_i2c_master_xfer_msg_read_next_byte()
166 struct i2c_numaker_data *data = dev->data; in m_numaker_i2c_master_xfer_msg_read_next_byte() local
167 I2C_T *i2c_base = config->i2c_base; in m_numaker_i2c_master_xfer_msg_read_next_byte()
169 switch (data->master_xfer.buf_end - data->master_xfer.buf_pos) { in m_numaker_i2c_master_xfer_msg_read_next_byte()
171 /* Last data byte ACKed, we'll do message merge */ in m_numaker_i2c_master_xfer_msg_read_next_byte()
175 /* Read last data byte for this message */ in m_numaker_i2c_master_xfer_msg_read_next_byte()
179 /* ACK non-last data byte */ in m_numaker_i2c_master_xfer_msg_read_next_byte()
187 const struct i2c_numaker_config *config = dev->config; in m_numaker_i2c_master_xfer_msg_end()
188 struct i2c_numaker_data *data = dev->data; in m_numaker_i2c_master_xfer_msg_end() local
189 I2C_T *i2c_base = config->i2c_base; in m_numaker_i2c_master_xfer_msg_end()
192 __ASSERT_NO_MSG(data->master_xfer.msgs_pos < data->master_xfer.msgs_end); in m_numaker_i2c_master_xfer_msg_end()
194 __ASSERT_NO_MSG((data->master_xfer.buf_end - data->master_xfer.buf_pos) == 0); in m_numaker_i2c_master_xfer_msg_end()
197 bool is_read_prev = (data->master_xfer.msgs_pos->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ; in m_numaker_i2c_master_xfer_msg_end()
198 bool do_stop_prev = data->master_xfer.msgs_pos->flags & I2C_MSG_STOP; in m_numaker_i2c_master_xfer_msg_end()
201 data->master_xfer.msgs_pos++; in m_numaker_i2c_master_xfer_msg_end()
204 if (data->master_xfer.msgs_pos < data->master_xfer.msgs_end) { in m_numaker_i2c_master_xfer_msg_end()
206 struct i2c_msg *msgs_pos = data->master_xfer.msgs_pos; in m_numaker_i2c_master_xfer_msg_end()
207 bool is_read_next = (msgs_pos->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ; in m_numaker_i2c_master_xfer_msg_end()
208 bool do_restart_next = data->master_xfer.msgs_pos->flags & I2C_MSG_RESTART; in m_numaker_i2c_master_xfer_msg_end()
232 data->master_xfer.buf_beg = data->master_xfer.msgs_pos->buf; in m_numaker_i2c_master_xfer_msg_end()
233 data->master_xfer.buf_pos = data->master_xfer.msgs_pos->buf; in m_numaker_i2c_master_xfer_msg_end()
234 data->master_xfer.buf_end = data->master_xfer.msgs_pos->buf + in m_numaker_i2c_master_xfer_msg_end()
235 data->master_xfer.msgs_pos->len; in m_numaker_i2c_master_xfer_msg_end()
241 * Interrupt flag not cleared, expect to re-enter ISR with in m_numaker_i2c_master_xfer_msg_end()
257 const struct i2c_numaker_config *config = dev->config; in i2c_numaker_configure()
258 struct i2c_numaker_data *data = dev->data; in i2c_numaker_configure() local
259 uint32_t bitrate; in i2c_numaker_configure() local
263 LOG_ERR("10-bits address not supported"); in i2c_numaker_configure()
264 return -ENOTSUP; in i2c_numaker_configure()
269 bitrate = KHZ(100); in i2c_numaker_configure()
272 bitrate = KHZ(400); in i2c_numaker_configure()
275 bitrate = MHZ(1); in i2c_numaker_configure()
279 return -ENOTSUP; in i2c_numaker_configure()
282 I2C_T *i2c_base = config->i2c_base; in i2c_numaker_configure()
285 k_sem_take(&data->lock, K_FOREVER); in i2c_numaker_configure()
286 irq_disable(config->irq_n); in i2c_numaker_configure()
289 if (data->slave_xfer.slave_addressed) { in i2c_numaker_configure()
291 err = -EBUSY; in i2c_numaker_configure()
296 I2C_Open(i2c_base, bitrate); in i2c_numaker_configure()
298 i2c_base->CTL0 |= (I2C_CTL0_INTEN_Msk | I2C_CTL0_I2CEN_Msk); in i2c_numaker_configure()
299 data->dev_config = dev_config; in i2c_numaker_configure()
303 irq_enable(config->irq_n); in i2c_numaker_configure()
304 k_sem_give(&data->lock); in i2c_numaker_configure()
311 struct i2c_numaker_data *data = dev->data; in i2c_numaker_get_config() local
314 return -EINVAL; in i2c_numaker_get_config()
317 k_sem_take(&data->lock, K_FOREVER); in i2c_numaker_get_config()
318 *dev_config = data->dev_config; in i2c_numaker_get_config()
319 k_sem_give(&data->lock); in i2c_numaker_get_config()
331 * 2. Do data transfer via above callback (ISR)
336 const struct i2c_numaker_config *config = dev->config; in i2c_numaker_transfer()
337 struct i2c_numaker_data *data = dev->data; in i2c_numaker_transfer() local
338 I2C_T *i2c_base = config->i2c_base; in i2c_numaker_transfer()
341 k_sem_take(&data->lock, K_FOREVER); in i2c_numaker_transfer()
342 irq_disable(config->irq_n); in i2c_numaker_transfer()
344 if (data->slave_xfer.slave_addressed) { in i2c_numaker_transfer()
346 err = -EBUSY; in i2c_numaker_transfer()
355 data->master_xfer.addr = addr; in i2c_numaker_transfer()
356 data->master_xfer.msgs_beg = msgs; in i2c_numaker_transfer()
357 data->master_xfer.msgs_pos = msgs; in i2c_numaker_transfer()
358 data->master_xfer.msgs_end = msgs + num_msgs; in i2c_numaker_transfer()
363 irq_enable(config->irq_n); in i2c_numaker_transfer()
364 k_sem_take(&data->master_xfer.xfer_sync, K_FOREVER); in i2c_numaker_transfer()
365 irq_disable(config->irq_n); in i2c_numaker_transfer()
368 if (data->master_xfer.msgs_pos != data->master_xfer.msgs_end) { in i2c_numaker_transfer()
372 is_read = (data->master_xfer.msgs_pos->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ; in i2c_numaker_transfer()
373 is_10bit = data->master_xfer.msgs_pos->flags & I2C_MSG_ADDR_10_BITS; in i2c_numaker_transfer()
375 LOG_ERR("MSG IDX: %d", data->master_xfer.msgs_pos - data->master_xfer.msgs_beg); in i2c_numaker_transfer()
376 LOG_ERR("ADDR (%d-bit): 0x%04X", is_10bit ? 10 : 7, addr); in i2c_numaker_transfer()
379 data->master_xfer.msgs_pos->len, in i2c_numaker_transfer()
380 data->master_xfer.buf_pos - data->master_xfer.buf_beg); in i2c_numaker_transfer()
381 err = -EIO; in i2c_numaker_transfer()
392 if (data->slave_xfer.slave_config) { in i2c_numaker_transfer()
399 irq_enable(config->irq_n); in i2c_numaker_transfer()
400 k_sem_give(&data->lock); in i2c_numaker_transfer()
409 if (!slave_config || !slave_config->callbacks) { in i2c_numaker_slave_register()
410 return -EINVAL; in i2c_numaker_slave_register()
413 if (slave_config->flags & I2C_ADDR_10_BITS) { in i2c_numaker_slave_register()
414 LOG_ERR("10-bits address not supported"); in i2c_numaker_slave_register()
415 return -ENOTSUP; in i2c_numaker_slave_register()
418 const struct i2c_numaker_config *config = dev->config; in i2c_numaker_slave_register()
419 struct i2c_numaker_data *data = dev->data; in i2c_numaker_slave_register() local
420 I2C_T *i2c_base = config->i2c_base; in i2c_numaker_slave_register()
423 k_sem_take(&data->lock, K_FOREVER); in i2c_numaker_slave_register()
424 irq_disable(config->irq_n); in i2c_numaker_slave_register()
426 if (data->slave_xfer.slave_config) { in i2c_numaker_slave_register()
427 err = -EBUSY; in i2c_numaker_slave_register()
431 data->slave_xfer.slave_config = slave_config; in i2c_numaker_slave_register()
435 slave_config->address, in i2c_numaker_slave_register()
439 data->slave_xfer.slave_addressed = false; in i2c_numaker_slave_register()
446 irq_enable(config->irq_n); in i2c_numaker_slave_register()
447 k_sem_give(&data->lock); in i2c_numaker_slave_register()
455 const struct i2c_numaker_config *config = dev->config; in i2c_numaker_slave_unregister()
456 struct i2c_numaker_data *data = dev->data; in i2c_numaker_slave_unregister() local
457 I2C_T *i2c_base = config->i2c_base; in i2c_numaker_slave_unregister()
461 return -EINVAL; in i2c_numaker_slave_unregister()
464 k_sem_take(&data->lock, K_FOREVER); in i2c_numaker_slave_unregister()
465 irq_disable(config->irq_n); in i2c_numaker_slave_unregister()
467 if (data->slave_xfer.slave_config != slave_config) { in i2c_numaker_slave_unregister()
468 err = -EINVAL; in i2c_numaker_slave_unregister()
472 if (data->slave_xfer.slave_addressed) { in i2c_numaker_slave_unregister()
474 err = -EBUSY; in i2c_numaker_slave_unregister()
485 data->slave_xfer.slave_addressed = false; in i2c_numaker_slave_unregister()
489 data->slave_xfer.slave_config = NULL; in i2c_numaker_slave_unregister()
493 irq_enable(config->irq_n); in i2c_numaker_slave_unregister()
494 k_sem_give(&data->lock); in i2c_numaker_slave_unregister()
502 const struct i2c_numaker_config *config = dev->config; in i2c_numaker_recover_bus()
503 struct i2c_numaker_data *data = dev->data; in i2c_numaker_recover_bus() local
504 I2C_T *i2c_base = config->i2c_base; in i2c_numaker_recover_bus()
506 k_sem_take(&data->lock, K_FOREVER); in i2c_numaker_recover_bus()
509 k_sem_give(&data->lock); in i2c_numaker_recover_bus()
516 const struct i2c_numaker_config *config = dev->config; in i2c_numaker_isr()
517 struct i2c_numaker_data *data = dev->data; in i2c_numaker_isr() local
518 I2C_T *i2c_base = config->i2c_base; in i2c_numaker_isr()
520 struct i2c_target_config *slave_config = data->slave_xfer.slave_config; in i2c_numaker_isr()
522 slave_config ? slave_config->callbacks : NULL; in i2c_numaker_isr()
538 data->master_xfer.buf_beg = data->master_xfer.msgs_pos->buf; in i2c_numaker_isr()
539 data->master_xfer.buf_pos = data->master_xfer.msgs_pos->buf; in i2c_numaker_isr()
540 data->master_xfer.buf_end = data->master_xfer.msgs_pos->buf + in i2c_numaker_isr()
541 data->master_xfer.msgs_pos->len; in i2c_numaker_isr()
544 struct i2c_msg *msgs_pos = data->master_xfer.msgs_pos; in i2c_numaker_isr()
545 bool is_read = (msgs_pos->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ; in i2c_numaker_isr()
546 uint16_t addr = data->master_xfer.addr; in i2c_numaker_isr()
553 case M_TRAN_DATA_ACK: /* Master Transmit Data ACK */ in i2c_numaker_isr()
554 __ASSERT_NO_MSG(data->master_xfer.buf_pos); in i2c_numaker_isr()
555 if (data->master_xfer.buf_pos < data->master_xfer.buf_end) { in i2c_numaker_isr()
556 I2C_SET_DATA(i2c_base, *data->master_xfer.buf_pos++); in i2c_numaker_isr()
564 case M_TRAN_DATA_NACK: /* Master Transmit Data NACK */ in i2c_numaker_isr()
570 case M_RECE_DATA_ACK: /* Master Receive Data ACK */ in i2c_numaker_isr()
571 __ASSERT_NO_MSG(data->master_xfer.buf_pos); in i2c_numaker_isr()
574 __ASSERT_NO_MSG(data->master_xfer.buf_pos < data->master_xfer.buf_end); in i2c_numaker_isr()
576 __ASSERT_NO_MSG((data->master_xfer.buf_end - in i2c_numaker_isr()
577 data->master_xfer.buf_pos) >= 1); in i2c_numaker_isr()
578 *data->master_xfer.buf_pos++ = I2C_GET_DATA(i2c_base); in i2c_numaker_isr()
583 case M_RECE_DATA_NACK: /* Master Receive Data NACK */ in i2c_numaker_isr()
584 __ASSERT_NO_MSG((data->master_xfer.buf_end - data->master_xfer.buf_pos) == 1); in i2c_numaker_isr()
585 *data->master_xfer.buf_pos++ = I2C_GET_DATA(i2c_base); in i2c_numaker_isr()
599 data->slave_xfer.slave_addressed = true; in i2c_numaker_isr()
600 if (slave_callbacks->read_requested(slave_config, &data_byte) == 0) { in i2c_numaker_isr()
601 /* Non-last data byte */ in i2c_numaker_isr()
610 case S_TRAN_DATA_ACK: /* Slave Transmit Data ACK */ in i2c_numaker_isr()
611 if (slave_callbacks->read_processed(slave_config, &data_byte) == 0) { in i2c_numaker_isr()
612 /* Non-last data byte */ in i2c_numaker_isr()
621 case S_TRAN_DATA_NACK: /* Slave Transmit Data NACK */ in i2c_numaker_isr()
622 case S_TRAN_LAST_DATA_ACK: /* Slave Transmit Last Data ACK */ in i2c_numaker_isr()
624 data->slave_xfer.slave_addressed = false; in i2c_numaker_isr()
625 slave_callbacks->stop(slave_config); in i2c_numaker_isr()
629 case S_RECE_DATA_ACK: /* Slave Receive Data ACK */ in i2c_numaker_isr()
631 if (slave_callbacks->write_received(slave_config, data_byte) == 0) { in i2c_numaker_isr()
632 /* Write OK, ACK next data byte */ in i2c_numaker_isr()
635 /* Write FAILED, NACK next data byte */ in i2c_numaker_isr()
639 case S_RECE_DATA_NACK: /* Slave Receive Data NACK */ in i2c_numaker_isr()
641 data->slave_xfer.slave_addressed = false; in i2c_numaker_isr()
642 slave_callbacks->stop(slave_config); in i2c_numaker_isr()
647 data->slave_xfer.slave_addressed = true; in i2c_numaker_isr()
648 if (slave_callbacks->write_requested(slave_config) == 0) { in i2c_numaker_isr()
658 data->slave_xfer.slave_addressed = false; in i2c_numaker_isr()
659 slave_callbacks->stop(slave_config); in i2c_numaker_isr()
675 const struct i2c_numaker_config *config = dev->config; in i2c_numaker_init()
676 struct i2c_numaker_data *data = dev->data; in i2c_numaker_init() local
681 if (!device_is_ready(config->reset.dev)) { in i2c_numaker_init()
683 return -ENODEV; in i2c_numaker_init()
687 memset(data, 0x00, sizeof(*data)); in i2c_numaker_init()
689 k_sem_init(&data->lock, 1, 1); in i2c_numaker_init()
690 k_sem_init(&data->master_xfer.xfer_sync, 0, 1); in i2c_numaker_init()
696 scc_subsys.pcc.clk_modidx = config->clk_modidx; in i2c_numaker_init()
697 scc_subsys.pcc.clk_src = config->clk_src; in i2c_numaker_init()
698 scc_subsys.pcc.clk_div = config->clk_div; in i2c_numaker_init()
701 err = clock_control_on(config->clkctrl_dev, (clock_control_subsys_t) &scc_subsys); in i2c_numaker_init()
706 err = clock_control_configure(config->clkctrl_dev, in i2c_numaker_init()
714 err = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT); in i2c_numaker_init()
720 reset_line_toggle_dt(&config->reset); in i2c_numaker_init()
722 err = i2c_numaker_configure(dev, I2C_MODE_CONTROLLER | i2c_map_dt_bitrate(config->bitrate)); in i2c_numaker_init()
727 config->irq_config_func(dev); in i2c_numaker_init()
773 .bitrate = DT_INST_PROP(inst, clock_frequency), \