i2c.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /****************************************************************************//**
  2. * @file i2c.h
  3. * @version V1.00
  4. * $Revision: 13 $
  5. * $Date: 15/11/20 7:27p $
  6. * @brief NUC472/NUC442 I2C driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __I2C_H__
  12. #define __I2C_H__
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver
  18. @{
  19. */
  20. /** @addtogroup NUC472_442_I2C_Driver I2C Driver
  21. @{
  22. */
  23. /** @addtogroup NUC472_442_I2C_EXPORTED_CONSTANTS I2C Exported Constants
  24. @{
  25. */
  26. #define I2C_STA 0x20 /*!< I2C START bit value \hideinitializer */
  27. #define I2C_STO 0x10 /*!< I2C STOP bit value \hideinitializer */
  28. #define I2C_SI 0x08 /*!< I2C SI bit value \hideinitializer */
  29. #define I2C_AA 0x04 /*!< I2C ACK bit value \hideinitializer */
  30. #define I2C_GCMODE_ENABLE 1 /*!< Enable I2C GC Mode \hideinitializer */
  31. #define I2C_GCMODE_DISABLE 0 /*!< Disable I2C GC Mode \hideinitializer */
  32. /*@}*/ /* end of group NUC472_442_I2C_EXPORTED_CONSTANTS */
  33. /** @addtogroup NUC472_442_I2C_EXPORTED_FUNCTIONS I2C Exported Functions
  34. @{
  35. */
  36. /**
  37. * @brief This macro sets the I2C control register at one time.
  38. * @param[in] i2c is the base address of I2C module.
  39. * @param[in] u8Ctrl is the register value of I2C control register.
  40. * @return none
  41. * \hideinitializer
  42. */
  43. #define I2C_SET_CONTROL_REG(i2c, u8Ctrl) ( (i2c)->CTL = ((i2c)->CTL & ~0x3c) | u8Ctrl )
  44. /**
  45. * @brief This macro only set START bit to the control register of I2C module.
  46. * @param[in] i2c is the base address of I2C module.
  47. * @return none
  48. * \hideinitializer
  49. */
  50. #define I2C_START(i2c) ( (i2c)->CTL = ((i2c)->CTL & ~I2C_CTL_SI_Msk) | I2C_CTL_STA_Msk )
  51. /**
  52. * @brief This macro only set STOP bit to the control register of I2C module.
  53. * @param[in] i2c is the base address of I2C module.
  54. * @return none
  55. * \hideinitializer
  56. */
  57. #define I2C_STOP(i2c) ( (i2c)->CTL = ((i2c)->CTL & ~0x3c) | I2C_CTL_SI_Msk | I2C_CTL_STO_Msk )
  58. /**
  59. * @brief This macro will return when I2C module is ready.
  60. * @param[in] i2c is the base address of I2C module.
  61. * @return none
  62. * \hideinitializer
  63. */
  64. #define I2C_WAIT_READY(i2c) while(!((i2c)->CTL & I2C_CTL_SI_Msk))
  65. /**
  66. * @brief This macro returns the data stored in data register of I2C module.
  67. * @param[in] i2c is the base address of I2C module.
  68. * @return Data.
  69. * \hideinitializer
  70. */
  71. #define I2C_GET_DATA(i2c) ( (i2c)->DAT )
  72. /**
  73. * @brief This macro writes the data to data register of I2C module.
  74. * @param[in] i2c is the base address of I2C module.
  75. * @param[in] u8Data is the data which will be write to data register of I2C module.
  76. * @return none
  77. * \hideinitializer
  78. */
  79. #define I2C_SET_DATA(i2c, u8Data) ( (i2c)->DAT = u8Data )
  80. /**
  81. * @brief This macro returns the status of I2C module.
  82. * @param[in] i2c is the base address of I2C module.
  83. * @return Status.
  84. * \hideinitializer
  85. */
  86. #define I2C_GET_STATUS(i2c) ( (i2c)->STATUS )
  87. /**
  88. * @brief This macro returns time-out flag.
  89. * @param[in] i2c is the base address of I2C module.
  90. * @return Status.
  91. * @retval 0 Flag is not set.
  92. * @retval 1 Flag is set.
  93. * \hideinitializer
  94. */
  95. #define I2C_GET_TIMEOUT_FLAG(i2c) ( ((i2c)->TOCTL & I2C_TOCTL_TOIF_Msk) == I2C_TOCTL_TOIF_Msk ? 1:0 )
  96. /**
  97. * @brief This macro returns wakeup flag.
  98. * @param[in] i2c is the base address of I2C module.
  99. * @return Status.
  100. * @retval 0 Flag is not set.
  101. * @retval 1 Flag is set.
  102. * \hideinitializer
  103. */
  104. #define I2C_GET_WAKEUP_FLAG(i2c) ( ((i2c)->WKSTS & I2C_WKSTS_WKIF_Msk) == I2C_WKSTS_WKIF_Msk ? 1:0 )
  105. /**
  106. * @brief This macro clears wakeup flag.
  107. * @param[in] i2c is the base address of I2C module.
  108. * @return none
  109. */
  110. #define I2C_CLEAR_WAKEUP_FLAG(i2c) ((i2c)->WKSTS |= I2C_WKSTS_WKIF_Msk )
  111. uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock);
  112. void I2C_Close(I2C_T *i2c);
  113. void I2C_ClearTimeoutFlag(I2C_T *i2c);
  114. void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack);
  115. void I2C_DisableInt(I2C_T *i2c);
  116. void I2C_EnableInt(I2C_T *i2c);
  117. uint32_t I2C_GetBusClockFreq(I2C_T *i2c);
  118. uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock);
  119. uint32_t I2C_GetIntFlag(I2C_T *i2c);
  120. uint32_t I2C_GetStatus(I2C_T *i2c);
  121. uint32_t I2C_GetData(I2C_T *i2c);
  122. void I2C_SetData(I2C_T *i2c, uint8_t u8Data);
  123. void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode);
  124. void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask);
  125. void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout);
  126. void I2C_DisableTimeout(I2C_T *i2c);
  127. void I2C_EnableWakeup(I2C_T *i2c);
  128. void I2C_DisableWakeup(I2C_T *i2c);
  129. /*@}*/ /* end of group NUC472_442_I2C_EXPORTED_FUNCTIONS */
  130. /*@}*/ /* end of group NUC472_442_I2C_Driver */
  131. /*@}*/ /* end of group NUC472_442_Device_Driver */
  132. #ifdef __cplusplus
  133. }
  134. #endif
  135. #endif //__I2C_H__
  136. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/