i2c.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**************************************************************************//**
  2. * @file I2C.h
  3. * @version V3.0
  4. * $Revision: 9 $
  5. * $Date: 14/02/10 3:04p $
  6. * @brief M051 Series I2C Driver Header File
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #ifndef __I2C_H__
  13. #define __I2C_H__
  14. #include "M051Series.h"
  15. #ifdef __cplusplus
  16. extern "C"
  17. {
  18. #endif
  19. /** @addtogroup M051_Device_Driver M051 Device Driver
  20. @{
  21. */
  22. /** @addtogroup M051_I2C_Driver I2C Driver
  23. @{
  24. */
  25. /** @addtogroup M051_I2C_EXPORTED_CONSTANTS I2C Exported Constants
  26. @{
  27. */
  28. /*---------------------------------------------------------------------------------------------------------*/
  29. /* I2CON constant definitions. */
  30. /*---------------------------------------------------------------------------------------------------------*/
  31. #define I2C_I2CON_STA_STO_SI 0x38UL /*!< I2CON setting for I2C control bits. It would set STA, STO and SI bits */
  32. #define I2C_I2CON_STA_STO_SI_AA 0x3CUL /*!< I2CON setting for I2C control bits. It would set STA, STO, SI and AA bits */
  33. #define I2C_I2CON_STA_SI 0x28UL /*!< I2CON setting for I2C control bits. It would set STA and SI bits */
  34. #define I2C_I2CON_STA_SI_AA 0x2CUL /*!< I2CON setting for I2C control bits. It would set STA, SI and AA bits */
  35. #define I2C_I2CON_STO_SI 0x18UL /*!< I2CON setting for I2C control bits. It would set STO and SI bits */
  36. #define I2C_I2CON_STO_SI_AA 0x1CUL /*!< I2CON setting for I2C control bits. It would set STO, SI and AA bits */
  37. #define I2C_I2CON_SI 0x08UL /*!< I2CON setting for I2C control bits. It would set SI bit */
  38. #define I2C_I2CON_SI_AA 0x0CUL /*!< I2CON setting for I2C control bits. It would set SI and AA bits */
  39. #define I2C_I2CON_STA 0x20UL /*!< I2CON setting for I2C control bits. It would set STA bit */
  40. #define I2C_I2CON_STO 0x10UL /*!< I2CON setting for I2C control bits. It would set STO bit */
  41. #define I2C_I2CON_AA 0x04UL /*!< I2CON setting for I2C control bits. It would set AA bit */
  42. /*@}*/ /* end of group M051_I2C_EXPORTED_CONSTANTS */
  43. /** @addtogroup M051_I2C_EXPORTED_FUNCTIONS I2C Exported Functions
  44. @{
  45. */
  46. /**
  47. * @brief The macro is used to set I2C bus condition at One Time
  48. *
  49. * @param[in] i2c Specify I2C port
  50. * @return None
  51. *
  52. * @details To control I2C bus conditions of START, STOP, SI, ACK.
  53. */
  54. #define I2C_SET_CONTROL_REG(i2c, u8Ctrl) ((i2c)->I2CON = ((i2c)->I2CON & ~0x3c) | u8Ctrl)
  55. /**
  56. * @brief The macro is used to set START condition of I2C Bus
  57. *
  58. * @param[in] i2c Specify I2C port
  59. * @return None
  60. *
  61. * @details To set the I2C bus START condition
  62. */
  63. #define I2C_START(i2c) ((i2c)->I2CON = ((i2c)->I2CON & ~I2C_I2CON_SI_Msk) | I2C_I2CON_STA_Msk)
  64. /**
  65. * @brief The macro is used to set STOP condition of I2C Bus
  66. *
  67. * @param[in] i2c Specify I2C port
  68. * @return None
  69. *
  70. * @details To set the I2C bus STOP condition
  71. */
  72. #define I2C_STOP(i2c) ((i2c)->I2CON = ((i2c)->I2CON & ~I2C_I2CON_SI_Msk) | I2C_I2CON_STO_Msk)
  73. /**
  74. * @brief The macro is used to wait I2C bus status get ready
  75. *
  76. * @param[in] i2c Specify I2C port
  77. * @return None
  78. *
  79. * @details When a new status is presented of I2C bus, SI flag will be set.
  80. */
  81. #define I2C_WAIT_READY(i2c) while(!((i2c)->I2CON & I2C_I2CON_SI_Msk))
  82. /**
  83. * @brief The macro is used to Read I2C Bus Data Register
  84. *
  85. * @param[in] i2c Specify I2C port
  86. * @return A byte of I2C data register
  87. *
  88. * @details I2C controller read data from bus and save it in I2CDAT
  89. */
  90. #define I2C_GET_DATA(i2c) ((i2c)->I2CDAT)
  91. /**
  92. * @brief The macro is used to write a Data to I2C Data Register
  93. *
  94. * @param[in] i2c Specify I2C port
  95. * @return None
  96. *
  97. * @details I2C controller send a byte data to bus.
  98. */
  99. #define I2C_SET_DATA(i2c, u8Data) ((i2c)->I2CDAT = u8Data)
  100. /**
  101. * @brief The macro is used to get I2C Bus Status Code
  102. *
  103. * @param[in] i2c Specify I2C port
  104. * @return I2C status code
  105. *
  106. * @details To get ths status code of I2C Bus.
  107. */
  108. #define I2C_GET_STATUS(i2c) ((i2c)->I2CSTATUS)
  109. /**
  110. * @brief The macro is used to get time-out flag
  111. *
  112. * @param[in] i2c Specify I2C port
  113. * @return Time-out flag status
  114. * @retval 0 Bus time-out is not happened
  115. * @retval 1 Bus time-out is happened
  116. *
  117. * @details To get time-out flag of I2C bus
  118. */
  119. #define I2C_GET_TIMEOUT_FLAG(i2c) ( (i2c->I2CTOC & I2C_I2CTOC_TIF_Msk) == I2C_I2CTOC_TIF_Msk ? 1:0 )
  120. /**
  121. * @brief The macro is used to get wake-up flag
  122. *
  123. * @param[in] i2c Specify I2C port
  124. * @return Wake-up flag status
  125. * @retval 0 Chip is not woken-up from power-down mode
  126. * @retval 1 Chip is woken-up from power-down mode
  127. *
  128. * @detail To get wake-up status of I2C bus
  129. */
  130. #define I2C_GET_WAKEUP_FLAG(i2c) ( ((i2c)->I2CWKUPSTS & I2C_I2CWKUPSTS_WKUPIF_Msk) == I2C_I2CWKUPSTS_WKUPIF_Msk ? 1:0 )
  131. /**
  132. * @brief The macro is used to clear wake-up flag
  133. *
  134. * @param[in] i2c Specify I2C port
  135. * @return None
  136. *
  137. * @detail To clear wake-up flag of I2C bus when wake-up flag is set.
  138. */
  139. #define I2C_CLEAR_WAKEUP_FLAG(i2c) ((i2c)->I2CWKUPSTS |= I2C_I2CWKUPSTS_WKUPIF_Msk)
  140. void I2C_ClearTimeoutFlag(I2C_T *i2c);
  141. void I2C_Close(I2C_T *i2c);
  142. void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack);
  143. void I2C_DisableInt(I2C_T *i2c);
  144. void I2C_EnableInt(I2C_T *i2c);
  145. uint32_t I2C_GetBusClockFreq(I2C_T *i2c);
  146. uint32_t I2C_GetIntFlag(I2C_T *i2c);
  147. uint32_t I2C_GetStatus(I2C_T *i2c);
  148. uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock);
  149. uint8_t I2C_GetData(I2C_T *i2c);
  150. void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode);
  151. void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask);
  152. uint32_t I2C_SetClockBusFreq(I2C_T *i2c, uint32_t u32BusClock);
  153. void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout);
  154. void I2C_DisableTimeout(I2C_T *i2c);
  155. void I2C_EnableWakeup(I2C_T *i2c);
  156. void I2C_DisableWakeup(I2C_T *i2c);
  157. void I2C_SetData(I2C_T *i2c, uint8_t u8Data);
  158. /*@}*/ /* end of group M051_I2C_EXPORTED_FUNCTIONS */
  159. /*@}*/ /* end of group M051_I2C_Driver */
  160. /*@}*/ /* end of group M051_Device_Driver */
  161. #ifdef __cplusplus
  162. }
  163. #endif
  164. #endif //__I2C_H__