1 /*
2   ******************************************************************************
3   * @file    HAL_I2C.h
4   * @version V1.0.0
5   * @date    2020
6   * @brief   Header file of I2C HAL module.
7   ******************************************************************************
8 */
9 
10 #ifndef __HAL_I2C_H__
11 #define __HAL_I2C_H__
12 
13 #include "ACM32Fxx_HAL.h"
14 
15 /****************  Bit definition for I2C CR register  ********************/
16 #define I2C_CR_STOPF_INTEN                  BIT20
17 #define I2C_CR_RX_ADDR3_INTEN               BIT19
18 #define I2C_CR_DMA_EN                       BIT18
19 #define I2C_CR_TXE_SEL                      BIT17
20 #define I2C_CR_MARLO_INTEN                  BIT16
21 #define I2C_CR_TX_AUTO_EN                   BIT15
22 #define I2C_CR_OD_MODE                      BIT14
23 #define I2C_CR_RX_ADDR2_INT_EN              BIT12
24 #define I2C_CR_OVR_INT_EN                   BIT11
25 #define I2C_CR_RXNE_INT_EN                  BIT10
26 #define I2C_CR_TXE_INT_EN                   BIT9
27 #define I2C_CR_RX_ADDR1_INT_EN              BIT8
28 #define I2C_CR_MTF_INT_EN                   BIT7
29 #define I2C_CR_TACK                         BIT6
30 #define I2C_CR_STOP                         BIT5
31 #define I2C_CR_START                        BIT4
32 #define I2C_CR_TX                           BIT3
33 #define I2C_CR_MASTER                       BIT2
34 #define I2C_CR_NOSTRETCH                    BIT1
35 #define I2C_CR_MEN                          BIT0
36 
37 /****************  Bit definition for I2C SR register  ********************/
38 #define I2C_SR_TIMEOUTBF                    BIT16
39 #define I2C_SR_TIMEOUTAF                    BIT15
40 #define I2C_SR_RX_ADDR3                     BIT14
41 #define I2C_SR_RX_ADDR2                     BIT12
42 #define I2C_SR_OVR                          BIT11
43 #define I2C_SR_RXNE                         BIT10
44 #define I2C_SR_TXE                          BIT9
45 #define I2C_SR_RX_ADDR1                     BIT8
46 #define I2C_SR_MTF                          BIT7
47 #define I2C_SR_MARLO                        BIT6
48 #define I2C_SR_TX_RX_FLAG                   BIT5
49 #define I2C_SR_BUS_BUSY                     BIT4
50 #define I2C_SR_SRW                          BIT3
51 #define I2C_SR_STOPF                        BIT2
52 #define I2C_SR_STARTF                       BIT1
53 #define I2C_SR_RACK                         BIT0
54 
55 /**************  Bit definition for I2C SLAVE ADDR2/3 register  **************/
56 #define I2C_ADDR3_EN                        BIT8
57 #define I2C_ADDR2_EN                        BIT0
58 
59 /**************  Bit definition for I2C TIMEOUT register  **************/
60 #define I2C_TIMEOUT_EXTEN                   BIT31
61 #define I2C_TOUTB_INTEN                     BIT30
62 #define I2C_EXT_MODE                        BIT29
63 #define I2C_TIMEOUT_TIMOUTEN                BIT15
64 #define I2C_TOUTA_INTEN                     BIT14
65 
66 /** @defgroup I2C_MODE
67  *  @{
68  */
69 #define    I2C_MODE_SLAVE     (0U)
70 #define    I2C_MODE_MASTER    (1U)
71 /**
72   * @}
73   */
74 
75 /** @defgroup CLOCK_SPEED
76  *  @{
77  */
78 #define    CLOCK_SPEED_STANDARD     (100000U)
79 #define    CLOCK_SPEED_FAST         (400000U)
80 #define    CLOCK_SPEED_FAST_PLUS    (1000000U)
81 /**
82   * @}
83   */
84 
85 
86 /** @defgroup TX_AUTO_EN
87  *  @{
88  */
89 #define    TX_AUTO_EN_DISABLE    (0U)
90 #define    TX_AUTO_EN_ENABLE     (1U)
91 /**
92   * @}
93   */
94 
95 
96 /** @defgroup NO_STRETCH_MODE
97  *  @{
98  */
99 #define    NO_STRETCH_MODE_STRETCH      (0U)
100 #define    NO_STRETCH_MODE_NOSTRETCH    (1U)
101 /**
102   * @}
103   */
104 
105 /** @defgroup SLAVE State machine
106  *  @{
107  */
108 #define    SLAVE_RX_STATE_IDLE         (0U)
109 #define    SLAVE_RX_STATE_RECEIVING    (1U)
110 #define    SLAVE_TX_STATE_IDLE         (0U)
111 #define    SLAVE_TX_STATE_SENDING      (1U)
112 /**
113   * @}
114   */
115 
116  /** @defgroup I2C_Memory_Address_Size I2C Memory Address Size
117   * @{
118   */
119 #define I2C_MEMADD_SIZE_8BIT            (0U)
120 #define I2C_MEMADD_SIZE_16BIT           (1U)
121 /**
122   * @}
123   */
124 
125 
126 /* Private macros ------------------------------------------------------------*/
127 /** @defgroup I2C_Private_Macros I2C Private Macros
128   * @{
129   */
130 #define I2C_MEM_ADD_MSB(__ADDRESS__)                       ((uint8_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)0xFF00)) >> 8)))
131 #define I2C_MEM_ADD_LSB(__ADDRESS__)                       ((uint8_t)((uint16_t)((__ADDRESS__) & (uint16_t)0x00FF)))
132 
133 /**
134   * @brief  I2C Configuration Structure definition
135   */
136 
137 #define __HAL_I2C_GET_FLAG(__HANDLE__, __FLAG__)   (((((__HANDLE__)->Instance->SR) & (__FLAG__))  == (__FLAG__) ) ? 1 : 0)
138 
139 typedef enum
140 {
141   RESET = 0,
142   SET = !RESET
143 } FlagStatus, ITStatus;
144 
145 typedef struct
146 {
147     uint32_t  I2C_Mode;               /* This parameter can be a value of @ref I2C_MODE */
148 
149     uint32_t  Tx_Auto_En;             /* This parameter can be a value of @ref TX_AUTO_EN */
150 
151     uint32_t  No_Stretch_Mode;        /* This parameter can be a value of @ref NO_STRETCH_MODE */
152 
153     uint32_t  Own_Address;            /* This parameter can be a 7-bit address */
154 
155     uint32_t  Clock_Speed;            /* This parameter can be a value of @ref CLOCK_SPEED */
156 } I2C_InitTypeDef;
157 
158 /******************************** Check I2C Parameter *******************************/
159 #define IS_I2C_ALL_MODE(I2C_MODE)    (((I2C_MODE) == I2C_MODE_SLAVE) || \
160                                       ((I2C_MODE) == I2C_MODE_MASTER))
161 
162 #define IS_I2C_CLOCK_SPEED(CLOCK_SPEED)    (((CLOCK_SPEED) > 0U) && ((CLOCK_SPEED) <=1000000U))
163 
164 #define IS_I2C_TX_AUTO_EN(TX_AUTO_EN)      (((TX_AUTO_EN) == TX_AUTO_EN_DISABLE) || \
165                                             ((TX_AUTO_EN) == TX_AUTO_EN_ENABLE))
166 
167 #define IS_I2C_STRETCH_EN(STRETCH_EN)    (((STRETCH_EN) == NO_STRETCH_MODE_STRETCH) || \
168                                           ((STRETCH_EN) == NO_STRETCH_MODE_NOSTRETCH))
169 
170 /**
171   * @brief  I2C handle Structure definition
172   */
173 typedef struct
174 {
175     I2C_TypeDef         *Instance;         /* I2C registers base address */
176 
177     I2C_InitTypeDef      Init;             /* I2C communication parameters */
178 
179     uint32_t               Slave_RxState;    /* I2C Slave state machine */
180     uint32_t               Slave_TxState;    /* I2C Slave state machine */
181 
182     uint8_t               *Rx_Buffer;        /* I2C Rx Buffer */
183     uint8_t               *Tx_Buffer;        /* I2C Tx Buffer */
184 
185     uint32_t               Rx_Size;          /* I2C Rx Size */
186     uint32_t               Tx_Size;          /* I2C Tx Size */
187 
188     uint32_t               Rx_Count;         /* I2C Rx Count */
189     uint32_t               Tx_Count;         /* I2C Tx Count */
190 
191     DMA_HandleTypeDef   *HDMA_Rx;          /* I2C Rx DMA handle parameters */
192     DMA_HandleTypeDef   *HDMA_Tx;          /* I2C Tx DMA handle parameters */
193 
194     void (*I2C_STOPF_Callback)(void);      /* I2C STOP flag interrupt callback */
195 
196 }I2C_HandleTypeDef;
197 
198 /******************************** I2C Instances *******************************/
199 #define IS_I2C_ALL_INSTANCE(INSTANCE)    (((INSTANCE) == I2C1) || ((INSTANCE) == I2C2))
200 
201 /* Function : HAL_I2C_IRQHandler */
202 void HAL_I2C_IRQHandler(I2C_HandleTypeDef *hi2c);
203 
204 /* Function : HAL_I2C_MspInit */
205 void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c);
206 
207 /* Function : HAL_I2C_MspDeInit */
208 void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c);
209 
210 /* Function : HAL_I2C_Init */
211 HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c);
212 
213 /* Function : HAL_I2C_DeInit */
214 HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c);
215 
216 /* Function : HAL_I2C_Master_Transmit */
217 HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
218 
219 /* Function : HAL_I2C_Master_Receive */
220 HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
221 
222 /* Function : HAL_I2C_Slave_Transmit */
223 HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size, uint32_t Timeout);
224 
225 /* Function : HAL_I2C_Slave_Receive */
226 HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size, uint32_t Timeout);
227 
228 /* Function : HAL_I2C_Slave_Transmit_IT */
229 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size);
230 
231 
232 /* Function : HAL_I2C_Slave_Receive_IT */
233 HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size);
234 
235 /* Function : HAL_I2C_Slave_Receive_DMA */
236 HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size);
237 
238 /* Function : HAL_I2C_Slave_Transmit_DMA */
239 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size);
240 
241 /* Function : HAL_I2C_Mem_Write */
242 HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout);
243 
244 /* Function : HAL_I2C_Mem_Read */
245 HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout);
246 
247 /* Function : HAL_I2C_GetSlaveRxState */
248 uint8_t HAL_I2C_GetSlaveRxState(I2C_HandleTypeDef *hi2c);
249 
250 /* Function : HAL_I2C_GetSlaveTxState */
251 uint8_t HAL_I2C_GetSlaveTxState(I2C_HandleTypeDef *hi2c);
252 
253 #endif
254