1 /**************************************************************************//**
2  * @file     usci_i2c.h
3  * @version  V3.0
4  * $Revision: 1 $
5  * $Date: 16/07/07 7:50p $
6  * @brief    USCI I2C(UI2C) driver header file
7  *
8  * @copyright SPDX-License-Identifier: Apache-2.0
9  * @copyright Copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
10  ******************************************************************************/
11 #ifndef __USCI_I2C_H__
12 #define __USCI_I2C_H__
13 
14 #ifdef __cplusplus
15 extern "C"
16 {
17 #endif
18 
19 
20 /** @addtogroup Standard_Driver Standard Driver
21   @{
22 */
23 
24 /** @addtogroup USCI_I2C_Driver USCI_I2C Driver
25   @{
26 */
27 
28 /** @addtogroup USCI_I2C_EXPORTED_CONSTANTS USCI_I2C Exported Constants
29   @{
30 */
31 
32 /*---------------------------------------------------------------------------------------------------------*/
33 /* USCI_I2C master event definitions                                                                       */
34 /*---------------------------------------------------------------------------------------------------------*/
35 enum UI2C_MASTER_EVENT
36 {
37     MASTER_SEND_ADDRESS = 10u,    /*!< Master send address to Slave */
38     MASTER_SEND_H_WR_ADDRESS,    /*!< Master send High address to Slave */
39     MASTER_SEND_H_RD_ADDRESS,    /*!< Master send address to Slave (Read ADDR) */
40     MASTER_SEND_L_ADDRESS,       /*!< Master send Low address to Slave */
41     MASTER_SEND_DATA,            /*!< Master Send Data to Slave */
42     MASTER_SEND_REPEAT_START,    /*!< Master send repeat start to Slave */
43     MASTER_READ_DATA,            /*!< Master Get Data from Slave */
44     MASTER_STOP,                 /*!< Master send stop to Slave */
45     MASTER_SEND_START            /*!< Master send start to Slave */
46 };
47 
48 /*---------------------------------------------------------------------------------------------------------*/
49 /* USCI_I2C slave event definitions                                                                        */
50 /*---------------------------------------------------------------------------------------------------------*/
51 enum UI2C_SLAVE_EVENT
52 {
53     SLAVE_ADDRESS_ACK = 100u,      /*!< Slave send address ACK */
54     SLAVE_H_WR_ADDRESS_ACK,       /*!< Slave send High address ACK */
55     SLAVE_L_WR_ADDRESS_ACK,       /*!< Slave send Low address ACK */
56     SLAVE_GET_DATA,               /*!< Slave Get Data from Master (Write CMD) */
57     SLAVE_SEND_DATA,              /*!< Slave Send Data to Master (Read CMD) */
58     SLAVE_H_RD_ADDRESS_ACK,       /*!< Slave send High address ACK */
59     SLAVE_L_RD_ADDRESS_ACK        /*!< Slave send Low address ACK */
60 };
61 
62 /*---------------------------------------------------------------------------------------------------------*/
63 /*  USCI_CTL constant definitions.                                                                         */
64 /*---------------------------------------------------------------------------------------------------------*/
65 #define UI2C_CTL_PTRG              (0x20U)    /*!< USCI_CTL setting for I2C control bits. It would set PTRG bit */
66 #define UI2C_CTL_STA               (0x08U)    /*!< USCI_CTL setting for I2C control bits. It would set STA bit */
67 #define UI2C_CTL_STO               (0x04U)    /*!< USCI_CTL setting for I2C control bits. It would set STO bit */
68 #define UI2C_CTL_AA                (0x02U)    /*!< USCI_CTL setting for I2C control bits. It would set AA bit  */
69 
70 /*---------------------------------------------------------------------------------------------------------*/
71 /*  USCI_I2C GCMode constant definitions.                                                                  */
72 /*---------------------------------------------------------------------------------------------------------*/
73 #define UI2C_GCMODE_ENABLE         (1U)    /*!< Enable  USCI_I2C GC Mode */
74 #define UI2C_GCMODE_DISABLE        (0U)    /*!< Disable USCI_I2C GC Mode */
75 
76 /*---------------------------------------------------------------------------------------------------------*/
77 /*  USCI_I2C Wakeup Mode constant definitions.                                                             */
78 /*---------------------------------------------------------------------------------------------------------*/
79 #define UI2C_DATA_TOGGLE_WK        (0x0U << UI2C_WKCTL_WKADDREN_Pos)    /*!< Wakeup according data toggle */
80 #define UI2C_ADDR_MATCH_WK         (0x1U << UI2C_WKCTL_WKADDREN_Pos)    /*!< Wakeup according address match */
81 
82 /*---------------------------------------------------------------------------------------------------------*/
83 /* USCI_I2C interrupt mask definitions                                                                     */
84 /*---------------------------------------------------------------------------------------------------------*/
85 #define UI2C_TO_INT_MASK           (0x001U)    /*!< Time-out interrupt mask */
86 #define UI2C_STAR_INT_MASK         (0x002U)    /*!< Start condition received interrupt mask */
87 #define UI2C_STOR_INT_MASK         (0x004U)    /*!< Stop condition received interrupt mask */
88 #define UI2C_NACK_INT_MASK         (0x008U)    /*!< Non-acknowledge interrupt mask */
89 #define UI2C_ARBLO_INT_MASK        (0x010U)    /*!< Arbitration lost interrupt mask */
90 #define UI2C_ERR_INT_MASK          (0x020U)    /*!< Error interrupt mask */
91 #define UI2C_ACK_INT_MASK          (0x040U)    /*!< Acknowledge interrupt mask */
92 
93 /**@}*/ /* end of group USCI_I2C_EXPORTED_CONSTANTS */
94 
95 
96 /** @addtogroup USCI_I2C_EXPORTED_FUNCTIONS USCI_I2C Exported Functions
97   @{
98 */
99 
100 /**
101  *    @brief        This macro sets the USCI_I2C protocol control register at one time
102  *
103  *    @param[in]    ui2c      The pointer of the specified USCI_I2C module.
104  *    @param[in]    u8Ctrl    Set the register value of USCI_I2C control register.
105  *
106  *    @return       None
107  *
108  *    @details      Set UI2C_PROTCTL register to control USCI_I2C bus conditions of START, STOP, PTRG, ACK.
109  */
110 #define UI2C_SET_CONTROL_REG(ui2c, u8Ctrl) ((ui2c)->PROTCTL = ((ui2c)->PROTCTL & ~0x2EU) | (u8Ctrl))
111 
112 /**
113  *    @brief        This macro only set START bit to protocol control register of USCI_I2C module.
114  *
115  *    @param[in]    ui2c      The pointer of the specified USCI_I2C module.
116  *
117  *    @return       None
118  *
119  *    @details      Set the USCI_I2C bus START condition in UI2C_PROTCTL register.
120  */
121 #define UI2C_START(ui2c) ((ui2c)->PROTCTL = ((ui2c)->PROTCTL & ~UI2C_PROTCTL_PTRG_Msk) | UI2C_PROTCTL_STA_Msk)
122 
123 /**
124  *    @brief        This macro only set STOP bit to the control register of USCI_I2C module
125  *
126  *    @param[in]    ui2c      The pointer of the specified USCI_I2C module.
127  *
128  *    @return       None
129  *
130  *    @details      Set the USCI_I2C bus STOP condition in UI2C_PROTCTL register.
131  */
132 #define UI2C_STOP(ui2c) ((ui2c)->PROTCTL = ((ui2c)->PROTCTL & ~0x2E) | (UI2C_PROTCTL_PTRG_Msk | UI2C_PROTCTL_STO_Msk))
133 
134 /**
135  *    @brief        This macro returns the data stored in data register of USCI_I2C module
136  *
137  *    @param[in]    ui2c      The pointer of the specified USCI_I2C module.
138  *
139  *    @return       Data
140  *
141  *    @details      Read a byte data value of UI2C_RXDAT register from USCI_I2C bus
142  */
143 #define UI2C_GET_DATA(ui2c) ((ui2c)->RXDAT)
144 
145 /**
146  *    @brief        This macro writes the data to data register of USCI_I2C module
147  *
148  *    @param[in]    ui2c     The pointer of the specified USCI_I2C module.
149  *    @param[in]    u8Data   The data which will be written to data register of USCI_I2C module.
150  *
151  *    @return       None
152  *
153  *    @details      Write a byte data value of UI2C_TXDAT register, then sends address or data to USCI I2C bus
154  */
155 #define UI2C_SET_DATA(ui2c, u8Data) ((ui2c)->TXDAT = (u8Data))
156 
157 /**
158  *    @brief        This macro returns time-out flag
159  *
160  *    @param[in]    ui2c     The pointer of the specified USCI_I2C module.
161  *
162  *    @retval       0        USCI_I2C bus time-out is not happened
163  *    @retval       1        USCI_I2C bus time-out is happened
164  *
165  *    @details      USCI_I2C bus occurs time-out event, the time-out flag will be set. If not occurs time-out event, this bit is cleared.
166  */
167 #define UI2C_GET_TIMEOUT_FLAG(ui2c) (((ui2c)->PROTSTS & UI2C_PROTSTS_TOIF_Msk) == UI2C_PROTSTS_TOIF_Msk ? 1:0)
168 
169 /**
170  *    @brief        This macro returns wake-up flag
171  *
172  *    @param[in]    ui2c     The pointer of the specified USCI_I2C module.
173  *
174  *    @retval       0        Chip is not woken-up from power-down mode
175  *    @retval       1        Chip is woken-up from power-down mode
176  *
177  *    @details      USCI_I2C controller wake-up flag will be set when USCI_I2C bus occurs wake-up from deep-sleep.
178  */
179 #define UI2C_GET_WAKEUP_FLAG(ui2c) (((ui2c)->WKSTS & UI2C_WKSTS_WKF_Msk) == UI2C_WKSTS_WKF_Msk ? 1:0)
180 
181 /**
182  *    @brief        This macro is used to clear USCI_I2C wake-up flag
183  *
184  *    @param[in]    ui2c     The pointer of the specified USCI_I2C module.
185  *
186  *    @return       None
187  *
188  *    @details      If USCI_I2C wake-up flag is set, use this macro to clear it.
189  */
190 #define UI2C_CLR_WAKEUP_FLAG(ui2c)  ((ui2c)->WKSTS = UI2C_WKSTS_WKF_Msk)
191 
192 /**
193  *    @brief        This macro disables the USCI_I2C 10-bit address mode
194  *
195  *    @param[in]    ui2c     The pointer of the specified USCI_I2C module.
196  *
197  *    @return       None
198  *
199  *    @details      The UI2C_I2C is 7-bit address mode, when disable USCI_I2C 10-bit address match function.
200  */
201 #define UI2C_DISABLE_10BIT_ADDR_MODE(ui2c)  ((ui2c)->PROTCTL &= ~(UI2C_PROTCTL_ADDR10EN_Msk))
202 
203 /**
204  *    @brief        This macro enables the 10-bit address mode
205  *
206  *    @param[in]    ui2c     The pointer of the specified USCI_I2C module.
207  *
208  *    @return       None
209  *
210  *    @details      To enable USCI_I2C 10-bit address match function.
211  */
212 #define UI2C_ENABLE_10BIT_ADDR_MODE(ui2c)  ((ui2c)->PROTCTL |= UI2C_PROTCTL_ADDR10EN_Msk)
213 
214 /**
215  *    @brief        This macro gets USCI_I2C protocol interrupt flag or bus status
216  *
217  *    @param[in]    ui2c     The pointer of the specified USCI_I2C module.
218  *
219  *    @return       A word data of USCI_I2C_PROTSTS register
220  *
221  *    @details      Read a word data of USCI_I2C PROTSTS register to get USCI_I2C bus Interrupt flags or status.
222  */
223 #define UI2C_GET_PROT_STATUS(ui2c)    ((ui2c)->PROTSTS)
224 
225 /**
226  *    @brief        This macro clears specified protocol interrupt flag
227  *    @param[in]    ui2c     The pointer of the specified USCI_I2C module.
228  *    @param[in]    u32IntTypeFlag Interrupt Type Flag, should be
229  *                                  - \ref UI2C_PROTSTS_ACKIF_Msk
230  *                                  - \ref UI2C_PROTSTS_ERRIF_Msk
231  *                                  - \ref UI2C_PROTSTS_ARBLOIF_Msk
232  *                                  - \ref UI2C_PROTSTS_NACKIF_Msk
233  *                                  - \ref UI2C_PROTSTS_STORIF_Msk
234  *                                  - \ref UI2C_PROTSTS_STARIF_Msk
235  *                                  - \ref UI2C_PROTSTS_TOIF_Msk
236  *    @return None
237  *
238  *    @details      To clear interrupt flag when USCI_I2C occurs interrupt and set interrupt flag.
239  */
240 #define UI2C_CLR_PROT_INT_FLAG(ui2c,u32IntTypeFlag)    ((ui2c)->PROTSTS = (u32IntTypeFlag))
241 
242 /**
243  *    @brief        This macro enables specified protocol interrupt
244  *    @param[in]    ui2c     The pointer of the specified USCI_I2C module.
245  *    @param[in]    u32IntSel Interrupt Type, should be
246  *                                  - \ref UI2C_PROTIEN_ACKIEN_Msk
247  *                                  - \ref UI2C_PROTIEN_ERRIEN_Msk
248  *                                  - \ref UI2C_PROTIEN_ARBLOIEN_Msk
249  *                                  - \ref UI2C_PROTIEN_NACKIEN_Msk
250  *                                  - \ref UI2C_PROTIEN_STORIEN_Msk
251  *                                  - \ref UI2C_PROTIEN_STARIEN_Msk
252  *                                  - \ref UI2C_PROTIEN_TOIEN_Msk
253  *    @return None
254  *
255  *    @details      Set specified USCI_I2C protocol interrupt bits to enable interrupt function.
256  */
257 #define UI2C_ENABLE_PROT_INT(ui2c, u32IntSel)    ((ui2c)->PROTIEN |= (u32IntSel))
258 
259 /**
260  *    @brief        This macro disables specified protocol interrupt
261  *    @param[in]    ui2c     The pointer of the specified USCI_I2C module.
262  *    @param[in]    u32IntSel Interrupt Type, should be
263  *                                  - \ref UI2C_PROTIEN_ACKIEN_Msk
264  *                                  - \ref UI2C_PROTIEN_ERRIEN_Msk
265  *                                  - \ref UI2C_PROTIEN_ARBLOIEN_Msk
266  *                                  - \ref UI2C_PROTIEN_NACKIEN_Msk
267  *                                  - \ref UI2C_PROTIEN_STORIEN_Msk
268  *                                  - \ref UI2C_PROTIEN_STARIEN_Msk
269  *                                  - \ref UI2C_PROTIEN_TOIEN_Msk
270  *    @return None
271  *
272  *    @details      Clear specified USCI_I2C protocol interrupt bits to disable interrupt funtion.
273  */
274 #define UI2C_DISABLE_PROT_INT(ui2c, u32IntSel)    ((ui2c)->PROTIEN &= ~ (u32IntSel))
275 
276 
277 uint32_t UI2C_Open(UI2C_T *ui2c, uint32_t u32BusClock);
278 void UI2C_Close(UI2C_T *ui2c);
279 void UI2C_ClearTimeoutFlag(UI2C_T *ui2c);
280 void UI2C_Trigger(UI2C_T *ui2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Ptrg, uint8_t u8Ack);
281 void UI2C_DisableInt(UI2C_T *ui2c, uint32_t u32Mask);
282 void UI2C_EnableInt(UI2C_T *ui2c, uint32_t u32Mask);
283 uint32_t UI2C_GetBusClockFreq(UI2C_T *ui2c);
284 uint32_t UI2C_SetBusClockFreq(UI2C_T *ui2c, uint32_t u32BusClock);
285 uint32_t UI2C_GetIntFlag(UI2C_T *ui2c, uint32_t u32Mask);
286 void UI2C_ClearIntFlag(UI2C_T* ui2c, uint32_t u32Mask);
287 uint32_t UI2C_GetData(UI2C_T *ui2c);
288 void UI2C_SetData(UI2C_T *ui2c, uint8_t u8Data);
289 void UI2C_SetSlaveAddr(UI2C_T *ui2c, uint8_t u8SlaveNo, uint16_t u16SlaveAddr, uint8_t u8GCMode);
290 void UI2C_SetSlaveAddrMask(UI2C_T *ui2c, uint8_t u8SlaveNo, uint16_t u16SlaveAddrMask);
291 void UI2C_EnableTimeout(UI2C_T *ui2c, uint32_t u32TimeoutCnt);
292 void UI2C_DisableTimeout(UI2C_T *ui2c);
293 void UI2C_EnableWakeup(UI2C_T *ui2c, uint8_t u8WakeupMode);
294 void UI2C_DisableWakeup(UI2C_T *ui2c);
295 uint8_t UI2C_WriteByte(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t data);
296 uint32_t UI2C_WriteMultiBytes(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t data[], uint32_t u32wLen);
297 uint8_t UI2C_WriteByteOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data);
298 uint32_t UI2C_WriteMultiBytesOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data[], uint32_t u32wLen);
299 uint8_t UI2C_WriteByteTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data);
300 uint32_t UI2C_WriteMultiBytesTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data[], uint32_t u32wLen);
301 uint8_t UI2C_ReadByte(UI2C_T *ui2c, uint8_t u8SlaveAddr);
302 uint32_t UI2C_ReadMultiBytes(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t rdata[], uint32_t u32rLen);
303 uint8_t UI2C_ReadByteOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr);
304 uint32_t UI2C_ReadMultiBytesOneReg(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t rdata[], uint32_t u32rLen);
305 uint8_t UI2C_ReadByteTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr);
306 uint32_t UI2C_ReadMultiBytesTwoRegs(UI2C_T *ui2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t rdata[], uint32_t u32rLen);
307 /**@}*/ /* end of group USCI_I2C_EXPORTED_FUNCTIONS */
308 
309 /**@}*/ /* end of group USCI_I2C_Driver */
310 
311 /**@}*/ /* end of group Standard_Driver */
312 
313 #ifdef __cplusplus
314 }
315 #endif
316 
317 #endif
318