31 #define I2C_REG_CON_OFS 0x00000000UL 32 #define I2C_REG_TAR_OFS 0x00000004UL 33 #define I2C_REG_SAR_OFS 0x00000008UL 34 #define I2C_REG_DATA_OFS 0x00000010UL 35 #define I2C_REG_SS_HCNT_OFS 0x00000014UL 36 #define I2C_REG_SS_LCNT_OFS 0x00000018UL 37 #define I2C_REG_FS_HCNT_OFS 0x0000001CUL 38 #define I2C_REG_FS_LCNT_OFS 0x00000020UL 39 #define I2C_REG_HS_HCNT_OFS 0x00000024UL 40 #define I2C_REG_HS_LCNT_OFS 0x00000028UL 41 #define I2C_REG_ISR_OFS 0x0000002CUL 42 #define I2C_REG_IMR_OFS 0x00000030UL 43 #define I2C_REG_RISR_OFS 0x00000034UL 44 #define I2C_REG_RFTL_OFS 0x00000038UL 45 #define I2C_REG_TFTL_OFS 0x0000003CUL 46 #define I2C_REG_ICR_OFS 0x00000040UL 47 #define I2C_REG_ICRU_OFS 0x00000044UL 48 #define I2C_REG_ICRO_OFS 0x00000048UL 49 #define I2C_REG_ICTO_OFS 0x0000004CUL 50 #define I2C_REG_CRR_OFS 0x00000050UL 51 #define I2C_REG_CTA_OFS 0x00000054UL 52 #define I2C_REG_CRD_OFS 0x00000058UL 53 #define I2C_REG_CACT_OFS 0x0000005CUL 54 #define I2C_REG_CSD_OFS 0x00000060UL 55 #define I2C_REG_CSAD_OFS 0x00000064UL 56 #define I2C_REG_CLR_GEN_CALL_OFS 0x00000068UL 57 #define I2C_REG_ICE_OFS 0x0000006CUL 58 #define I2C_REG_IS_OFS 0x00000070UL 59 #define I2C_REG_TXFLR_OFS 0x00000074UL 60 #define I2C_REG_RXFLR_OFS 0x00000078UL 61 #define I2C_REG_SDAH_OFS 0x0000007CUL 62 #define I2C_REG_ABTS_OFS 0x00000080UL 63 #define I2C_REG_NACK_OFS 0x00000084UL 64 #define I2C_REG_DMA_CR_OFS 0x00000088UL 65 #define I2C_REG_DMA_TDLR_OFS 0x0000008CUL 66 #define I2C_REG_DMA_RDLR_OFS 0x00000090UL 67 #define I2C_REG_SDAS_OFS 0x00000094UL 68 #define I2C_REG_ACK_GC_OFS 0x00000098UL 69 #define I2C_REG_ES_OFS 0x0000009CUL 70 #define I2C_REG_FS_SPK_OFS 0x000000A0UL 71 #define I2C_REG_HS_SPK_OFS 0x000000A4UL 72 #define I2C_REG_ICRD_OFS 0x000000A8UL 74 #define I2C_FIFO_DEPTH 8 75 #define I2C_RX_FIFO_TL 4 76 #define I2C_TX_FIFO_TL 4 91 I2C_ERR_INVALID_PARAM = -1,
92 I2C_ERR_INVALID_STATE = -2,
93 I2C_ERR_RX_UNDER_RUN = -3,
94 I2C_ERR_RX_OVER_RUN = -4,
95 I2C_ERR_TX_OVER_FLOW = -5,
96 I2C_ERR_ADDR_NO_ACK = -6,
97 I2C_ERR_TXDATA_NO_ACK = -7,
98 I2C_ERR_SDA_STUCK_AT_LOW = -8,
100 I2C_ERR_DMA_BUSY = -10,
101 I2C_ERR_DMA_FAIL = -11,
102 I2C_ERR_DMA_ALLOC_BUF_FAIL = -12,
114 typedef struct i2c_init {
118 void (*callback)(
void *arg,
int id,
int status);
126 #define I2C_CON_MASTER_MODE 0x00000001UL 127 #define I2C_CON_SPEED 0x00000006UL 128 #define I2C_CON_10BIT_SLAVE 0x00000008UL 129 #define I2C_CON_RESTART_EN 0x00000020UL 131 #define I2C_CON_SLAVE_DISABLE 0x00000040UL 132 #define I2C_CON_SLAVE_STOP_DET 0x00000080UL 133 #define I2C_CON_TX_EMPTY_CTRL 0x00000100UL 134 #define I2C_CON_RX_FIFO_FULL_HLD_CTRL 0x00000200UL 136 #define I2C_CON_SPEED_STD (0x1UL << 1) 137 #define I2C_CON_SPEED_FAST (0x2UL << 1) 139 static INLINE
void i2c_con_clear(uint32_t i2c_base)
141 WR_WORD((i2c_base + I2C_REG_CON_OFS), 0);
144 static INLINE
void i2c_speed(uint32_t i2c_base,
int speed)
146 uint32_t reg = RD_WORD(i2c_base + I2C_REG_CON_OFS);
148 reg &= ~I2C_CON_SPEED;
150 if (speed == I2C_SPEED_100K) {
151 reg |= I2C_CON_SPEED_STD;
153 reg |= I2C_CON_SPEED_FAST;
156 WR_WORD(i2c_base + I2C_REG_CON_OFS, reg);
159 static INLINE
void i2c_master_enable(uint32_t i2c_base)
161 uint32_t reg = RD_WORD(i2c_base + I2C_REG_CON_OFS);
163 reg |= I2C_CON_MASTER_MODE;
164 reg |= I2C_CON_SLAVE_DISABLE;
166 WR_WORD(i2c_base + I2C_REG_CON_OFS, reg);
169 static INLINE
void i2c_slave_enable(uint32_t i2c_base)
171 uint32_t reg = RD_WORD(i2c_base + I2C_REG_CON_OFS);
173 reg &= ~I2C_CON_MASTER_MODE;
174 reg &= ~I2C_CON_SLAVE_DISABLE;
176 WR_WORD(i2c_base + I2C_REG_CON_OFS, reg);
179 static INLINE
void i2c_slave_stop_det_enable(uint32_t i2c_base)
181 uint32_t reg = RD_WORD(i2c_base + I2C_REG_CON_OFS);
183 reg |= I2C_CON_SLAVE_STOP_DET;
185 WR_WORD(i2c_base + I2C_REG_CON_OFS, reg);
188 static INLINE
void i2c_restart_enable(uint32_t i2c_base)
190 uint32_t reg = RD_WORD(i2c_base + I2C_REG_CON_OFS);
192 reg |= I2C_CON_RESTART_EN;
194 WR_WORD(i2c_base + I2C_REG_CON_OFS, reg);
196 static INLINE
void i2c_restart_disable(uint32_t i2c_base)
198 uint32_t reg = RD_WORD(i2c_base + I2C_REG_CON_OFS);
200 reg &= ~I2C_CON_RESTART_EN;
202 WR_WORD(i2c_base + I2C_REG_CON_OFS, reg);
206 #define I2C_TAR 0x000003FFUL 208 static INLINE
void i2c_tar(uint32_t i2c_base, uint8_t taddr)
210 WR_WORD(i2c_base + I2C_REG_TAR_OFS, taddr);
215 #define I2C_SAR 0x000003FFUL 217 static INLINE
void i2c_sar(uint32_t i2c_base, uint8_t saddr)
219 WR_WORD(i2c_base + I2C_REG_SAR_OFS, saddr);
224 #define I2C_DATA 0x000000FFUL 225 #define I2C_READ 0x00000100UL 226 #define I2C_STOP 0x00000200UL 227 #define I2C_RESTART 0x00000400UL 229 static INLINE
void i2c_write(uint32_t i2c_base, uint8_t data,
int read,
int stop,
int restart)
241 WR_WORD(i2c_base + I2C_REG_DATA_OFS, reg);
244 static INLINE uint8_t i2c_read(uint32_t i2c_base)
246 return (RD_WORD(i2c_base + I2C_REG_DATA_OFS) & 0xFF);
251 #define I2C_SS_SCL_HCNT 0x0000FFFFUL 253 static INLINE
void i2c_ss_scl_hcnt(uint32_t i2c_base, uint16_t hcnt)
255 WR_WORD(i2c_base + I2C_REG_SS_HCNT_OFS, hcnt);
259 #define I2C_SS_SCL_LCNT 0x0000FFFFUL 261 static INLINE
void i2c_ss_scl_lcnt(uint32_t i2c_base, uint16_t lcnt)
263 WR_WORD(i2c_base + I2C_REG_SS_LCNT_OFS, lcnt);
267 #define I2C_FS_SCL_HCNT 0x0000FFFFUL 269 static INLINE
void i2c_fs_scl_hcnt(uint32_t i2c_base, uint16_t hcnt)
271 WR_WORD(i2c_base + I2C_REG_FS_HCNT_OFS, hcnt);
275 #define I2C_FS_SCL_LCNT 0x0000FFFFUL 277 static INLINE
void i2c_fs_scl_lcnt(uint32_t i2c_base, uint16_t lcnt)
279 WR_WORD(i2c_base + I2C_REG_FS_LCNT_OFS, lcnt);
286 #define I2C_INTR_RX_UNDER 0x00000001UL 287 #define I2C_INTR_RX_OVER 0x00000002UL 288 #define I2C_INTR_RX_FULL 0x00000004UL 289 #define I2C_INTR_TX_OVER 0x00000008UL 290 #define I2C_INTR_TX_EMPTY 0x00000010UL 291 #define I2C_INTR_RD_REQ 0x00000020UL 292 #define I2C_INTR_TX_ABRT 0x00000040UL 293 #define I2C_INTR_RX_DONE 0x00000080UL 294 #define I2C_INTR_ACTIVITY 0x00000100UL 295 #define I2C_INTR_STOP_DET 0x00000200UL 296 #define I2C_INTR_START_DET 0x00000400UL 299 #define I2C_INTR_MST_ON_HOLD 0x00002000UL 301 #define I2C_INTR_MASK_ALL 0x00007FFFUL 302 static INLINE uint32_t i2c_intr_status(uint32_t i2c_base)
304 return RD_WORD(i2c_base + I2C_REG_ISR_OFS);
307 static INLINE uint32_t i2c_intr_raw_status(uint32_t i2c_base)
309 return RD_WORD(i2c_base + I2C_REG_RISR_OFS);
312 static INLINE
void i2c_intr_mask(uint32_t i2c_base, uint32_t mask)
314 uint32_t reg = RD_WORD(i2c_base + I2C_REG_IMR_OFS);
316 WR_WORD(i2c_base + I2C_REG_IMR_OFS, reg);
319 static INLINE
void i2c_intr_unmask(uint32_t i2c_base, uint32_t unmask)
321 uint32_t reg = RD_WORD(i2c_base + I2C_REG_IMR_OFS);
323 WR_WORD(i2c_base + I2C_REG_IMR_OFS, reg);
326 static INLINE
void i2c_intr_clr(uint32_t i2c_base)
328 RD_WORD(i2c_base + I2C_REG_ICR_OFS);
331 static INLINE
void i2c_intr_clr_rxu(uint32_t i2c_base)
333 RD_WORD(i2c_base + I2C_REG_ICRU_OFS);
336 static INLINE
void i2c_intr_clr_rxo(uint32_t i2c_base)
338 RD_WORD(i2c_base + I2C_REG_ICRO_OFS);
341 static INLINE
void i2c_intr_clr_txo(uint32_t i2c_base)
343 RD_WORD(i2c_base + I2C_REG_ICTO_OFS);
346 static INLINE
void i2c_intr_clr_req(uint32_t i2c_base)
348 RD_WORD(i2c_base + I2C_REG_CRR_OFS);
351 static INLINE
void i2c_intr_clr_abt(uint32_t i2c_base)
353 RD_WORD(i2c_base + I2C_REG_CTA_OFS);
356 static INLINE
void i2c_intr_clr_rxd(uint32_t i2c_base)
358 RD_WORD(i2c_base + I2C_REG_CRD_OFS);
361 static INLINE
void i2c_intr_clr_activity(uint32_t i2c_base)
363 RD_WORD(i2c_base + I2C_REG_CACT_OFS);
366 static INLINE
void i2c_intr_clr_stop(uint32_t i2c_base)
368 RD_WORD(i2c_base + I2C_REG_CSD_OFS);
371 static INLINE
void i2c_intr_clr_start(uint32_t i2c_base)
373 RD_WORD(i2c_base + I2C_REG_CSAD_OFS);
377 #define I2C_RX_TL 0x000000FFUL 379 static INLINE
void i2c_rx_fifo_tl(uint32_t i2c_base, uint8_t level)
381 WR_WORD(i2c_base + I2C_REG_RFTL_OFS, level);
384 static INLINE uint8_t i2c_get_rx_fifo_tl(uint32_t i2c_base)
386 return (RD_WORD(i2c_base + I2C_REG_RFTL_OFS) & 0xFF);
390 #define I2C_TX_TL 0x000000FFUL 392 static INLINE
void i2c_tx_fifo_tl(uint32_t i2c_base, uint16_t level)
394 WR_WORD(i2c_base + I2C_REG_TFTL_OFS, level);
397 static INLINE uint8_t i2c_get_tx_fifo_tl(uint32_t i2c_base)
399 return (RD_WORD(i2c_base + I2C_REG_TFTL_OFS) & 0xFF);
404 #define I2C_ES_ENABLE 0x00000001UL 405 #define I2C_ES_SLV_DISABLE_BUSY 0x00000002UL 406 #define I2C_ES_SLV_RX_DATA_LOST 0x00000004UL 407 static INLINE uint32_t i2c_enable_status(uint32_t i2c_base)
409 return (RD_WORD(i2c_base + I2C_REG_ES_OFS));
413 #define I2C_ENABLE 0x00000001UL 414 #define I2C_ABORT 0x00000002UL 416 static INLINE
void i2c_enable(uint32_t i2c_base)
418 uint32_t reg = RD_WORD(i2c_base + I2C_REG_ICE_OFS);
420 WR_WORD(i2c_base + I2C_REG_ICE_OFS, reg);
423 static INLINE
void i2c_disable(uint32_t i2c_base)
425 uint32_t reg = RD_WORD(i2c_base + I2C_REG_ICE_OFS);
427 WR_WORD(i2c_base + I2C_REG_ICE_OFS, reg);
432 static INLINE
void i2c_abort(uint32_t i2c_base,
int enable)
434 uint32_t reg = RD_WORD(i2c_base + I2C_REG_ICE_OFS);
440 WR_WORD(i2c_base + I2C_REG_ICE_OFS, reg);
446 #define I2C_STATUS_ACTIVITY 0x00000001UL 447 #define I2C_STATUS_TFNF 0x00000002UL 448 #define I2C_STATUS_TFE 0x00000004UL 449 #define I2C_STATUS_RFNE 0x00000008UL 450 #define I2C_STATUS_RFF 0x00000010UL 452 static INLINE uint32_t i2c_status(uint32_t i2c_base)
454 return (RD_WORD(i2c_base + I2C_REG_IS_OFS));
458 static INLINE uint8_t i2c_rx_fifo_level(uint32_t i2c_base)
460 return (RD_WORD(i2c_base + I2C_REG_RXFLR_OFS) & 0xF);
463 static INLINE uint8_t i2c_tx_fifo_level(uint32_t i2c_base)
465 return (RD_WORD(i2c_base + I2C_REG_TXFLR_OFS) & 0xF);
469 #define I2C_ABRT_ADDR_NOACK 0x00000001UL 470 #define I2C_ABRT_TXDATA_NOACK 0x00000008UL 471 #define I2C_ABRT_SLVFLUSH_TXFIFO 0x00002000UL 472 #define I2C_ABRT_SLV_ARBLOST 0x00004000UL 473 #define I2C_ABRT_SLVRD_INTX 0x00008000UL 474 #define I2C_ABRT_USER_ABRT 0x00010000UL 475 #define I2C_ABRT_SDA_STUCK_AT_LOW 0x00020000UL 476 #define I2C_ABRT_TX_FLUSH_CNT 0xFF800000UL 478 static INLINE uint32_t i2c_abt_source(uint32_t i2c_base)
480 return (RD_WORD(i2c_base + I2C_REG_ABTS_OFS));
484 #define I2C_TX_DMA_EN 0x00000002UL 485 #define I2C_RX_DMA_EN 0x00000001UL 487 static INLINE
void i2c_dma_tx_enable(uint32_t i2c_base)
489 uint32_t reg = RD_WORD(i2c_base + I2C_REG_DMA_CR_OFS);
490 reg |= I2C_TX_DMA_EN;
491 WR_WORD(i2c_base + I2C_REG_DMA_CR_OFS, reg);
494 static INLINE
void i2c_dma_tx_disable(uint32_t i2c_base)
496 uint32_t reg = RD_WORD(i2c_base + I2C_REG_DMA_CR_OFS);
497 reg &= ~I2C_TX_DMA_EN;
498 WR_WORD(i2c_base + I2C_REG_DMA_CR_OFS, reg);
501 static INLINE
void i2c_dma_rx_enable(uint32_t i2c_base)
503 uint32_t reg = RD_WORD(i2c_base + I2C_REG_DMA_CR_OFS);
504 reg |= I2C_RX_DMA_EN;
505 WR_WORD(i2c_base + I2C_REG_DMA_CR_OFS, reg);
508 static INLINE
void i2c_dma_rx_disable(uint32_t i2c_base)
510 uint32_t reg = RD_WORD(i2c_base + I2C_REG_DMA_CR_OFS);
511 reg &= ~I2C_RX_DMA_EN;
512 WR_WORD(i2c_base + I2C_REG_DMA_CR_OFS, reg);
517 static INLINE
void i2c_dma_tx_data_level(uint32_t i2c_base, uint32_t level)
519 WR_WORD(i2c_base + I2C_REG_DMA_TDLR_OFS, (level & 0xF));
522 static INLINE
void i2c_dma_rx_data_level(uint32_t i2c_base, uint32_t level)
524 WR_WORD(i2c_base + I2C_REG_DMA_RDLR_OFS, (level & 0xF));
527 static INLINE
void i2c_reset(
int id)
529 uint32_t reg = RD_WORD(GLOBAL_REG_RESET_CTRL_1);
533 reg &= ~GLOBAL_REG_RESET_CTRL_1_CTL_RESET_1_D0_CPU_I2C_0_IC_RSTN_REG;
535 reg &= ~GLOBAL_REG_RESET_CTRL_1_CTL_RESET_1_D0_CPU_I2C_1_IC_RSTN_REG;
537 WR_WORD(GLOBAL_REG_RESET_CTRL_1, reg);
539 reg |= GLOBAL_REG_RESET_CTRL_1_CTL_RESET_1_D0_CPU_I2C_0_IC_RSTN_REG;
541 reg |= GLOBAL_REG_RESET_CTRL_1_CTL_RESET_1_D0_CPU_I2C_1_IC_RSTN_REG;
543 WR_WORD(GLOBAL_REG_RESET_CTRL_1, reg);
589 int hal_mi2c_read(
void *hdl, uint8_t tar, uint8_t *buffer, uint16_t buffer_len);
590 int hal_mi2c_read_dma(
void *hdl, uint8_t tar, uint8_t *buffer, uint16_t buffer_len);
591 int hal_mi2c_read_poll(
void *hdl, uint8_t tar, uint8_t *buffer, uint16_t buffer_len);
606 int hal_mi2c_write(
void *hdl, uint8_t tar, uint8_t *buffer, uint16_t buffer_len);
607 int hal_mi2c_write_dma(
void *hdl, uint8_t tar, uint8_t *buffer, uint16_t buffer_len);
608 int hal_mi2c_write_poll(
void *hdl, uint8_t tar, uint8_t *buffer, uint16_t buffer_len);
675 int hal_si2c_read(
void *hdl, uint8_t sar, uint8_t *buffer, uint16_t buffer_len);
676 int hal_si2c_read_dma(
void *hdl, uint8_t sar, uint8_t *buffer, uint16_t buffer_len);
691 int hal_si2c_write(
void *hdl, uint8_t sar, uint8_t *buffer, uint16_t buffer_len);
692 int hal_si2c_write_dma(
void *hdl, uint8_t sar, uint8_t *buffer, uint16_t buffer_len);
709 int hal_mi2c_write_read(
void *hdl, uint8_t tar, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len);
int hal_mi2c_write(void *hdl, uint8_t tar, uint8_t *buffer, uint16_t buffer_len)
I2c master write function.
int hal_mi2c_mix_tran_continue_poll(void *hdl, uint8_t tar, int dir_write, int restart, uint8_t *buffer, uint16_t buffer_len)
Continue of I2c master read/write mixed function.
int hal_si2c_write(void *hdl, uint8_t sar, uint8_t *buffer, uint16_t buffer_len)
I2c slave write function.
int hal_si2c_read(void *hdl, uint8_t sar, uint8_t *buffer, uint16_t buffer_len)
I2c slave read function.
int hal_mi2c_write_read(void *hdl, uint8_t tar, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
I2c master write and read function.
void hal_i2c_close(void *hdl)
Close the I2c driver.
int hal_mi2c_mix_tran_start_poll(void *hdl, int speed, uint8_t tar, int dir_write, uint8_t *buffer, uint16_t buffer_len)
Start of I2c master read/write mixed function.
void * hal_i2c_open(int id, i2c_init_t *init)
Open the I2c driver.
int hal_i2c_set_tmo(void *hdl, uint32_t tmo)
Set the I2c timeout.
int hal_mi2c_read(void *hdl, uint8_t tar, uint8_t *buffer, uint16_t buffer_len)
I2c master read function.
int hal_mi2c_mix_tran_end_poll(void *hdl, uint8_t tar, int dir_write, int restart, uint8_t *buffer, uint16_t buffer_len)
End of I2c master read/write mixed function.