1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2024-07-31     shelton      first version
9  */
10 
11 #ifndef __DRV_HARD_I2C_H__
12 #define __DRV_HARD_I2C_H__
13 
14 #include <rtthread.h>
15 #include <drivers/dev_i2c.h>
16 #include "drv_common.h"
17 #include "drv_dma.h"
18 
19 #define I2C_START                        0
20 #define I2C_END                          1
21 
22 #define I2C_EVENT_CHECK_NONE             ((uint32_t)0x00000000)
23 #define I2C_EVENT_CHECK_ACKFAIL          ((uint32_t)0x00000001)
24 #define I2C_EVENT_CHECK_STOP             ((uint32_t)0x00000002)
25 
26 typedef enum
27 {
28     I2C_INT_MA_TX = 0,
29     I2C_INT_MA_RX,
30     I2C_INT_SLA_TX,
31     I2C_INT_SLA_RX,
32     I2C_DMA_MA_TX,
33     I2C_DMA_MA_RX,
34     I2C_DMA_SLA_TX,
35     I2C_DMA_SLA_RX,
36 } i2c_mode_type;
37 
38 typedef enum
39 {
40     I2C_OK = 0,
41     I2C_ERR_STEP_1,
42     I2C_ERR_STEP_2,
43     I2C_ERR_STEP_3,
44     I2C_ERR_STEP_4,
45     I2C_ERR_STEP_5,
46     I2C_ERR_STEP_6,
47     I2C_ERR_STEP_7,
48     I2C_ERR_STEP_8,
49     I2C_ERR_STEP_9,
50     I2C_ERR_STEP_10,
51     I2C_ERR_STEP_11,
52     I2C_ERR_STEP_12,
53     I2C_ERR_START,
54     I2C_ERR_ADDR10,
55     I2C_ERR_TCRLD,
56     I2C_ERR_TDC,
57     I2C_ERR_ADDR,
58     I2C_ERR_STOP,
59     I2C_ERR_ACKFAIL,
60     I2C_ERR_TIMEOUT,
61     I2C_ERR_INTERRUPT,
62 } i2c_status_type;
63 
64 struct i2c_comm_type
65 {
66     rt_uint8_t *pbuff;
67     rt_uint16_t psize;
68     rt_uint16_t pcount;
69     i2c_mode_type mode;
70     rt_uint32_t timeout;
71     rt_uint32_t status;
72     i2c_status_type error_code;
73 };
74 
75 struct at32_i2c_handle
76 {
77     i2c_type *i2c_x;
78     const char *i2c_name;
79     rt_uint32_t timing;
80     IRQn_Type ev_irqn;
81     IRQn_Type er_irqn;
82     struct dma_config *dma_rx;
83     struct dma_config *dma_tx;
84     struct i2c_comm_type comm;
85     rt_uint16_t i2c_dma_flag;
86     struct rt_completion completion;
87 };
88 
89 struct at32_i2c
90 {
91     struct at32_i2c_handle *handle;
92     struct rt_i2c_bus_device i2c_bus;
93 };
94 
95 #endif
96