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