nu_i2c.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /****************************************************************************//**
  2. * @file i2c.h
  3. * @version V1.00
  4. * @brief NUC980 series I2C driver header file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #ifndef __NU_I2C_H__
  10. #define __NU_I2C_H__
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "nuc980.h"
  15. /** @addtogroup Standard_Driver Standard Driver
  16. @{
  17. */
  18. /** @addtogroup I2C_Driver I2C Driver
  19. @{
  20. */
  21. /** @addtogroup I2C_EXPORTED_CONSTANTS I2C Exported Constants
  22. @{
  23. */
  24. /*---------------------------------------------------------------------------------------------------------*/
  25. /* I2C_CTL constant definitions. */
  26. /*---------------------------------------------------------------------------------------------------------*/
  27. #define I2C_CTL_STA_SI 0x28UL /*!< I2C_CTL setting for I2C control bits. It would set STA and SI bits \hideinitializer */
  28. #define I2C_CTL_STA_SI_AA 0x2CUL /*!< I2C_CTL setting for I2C control bits. It would set STA, SI and AA bits \hideinitializer */
  29. #define I2C_CTL_STO_SI 0x18UL /*!< I2C_CTL setting for I2C control bits. It would set STO and SI bits \hideinitializer */
  30. #define I2C_CTL_STO_SI_AA 0x1CUL /*!< I2C_CTL setting for I2C control bits. It would set STO, SI and AA bits \hideinitializer */
  31. #define I2C_CTL_SI 0x08UL /*!< I2C_CTL setting for I2C control bits. It would set SI bit \hideinitializer */
  32. #define I2C_CTL_SI_AA 0x0CUL /*!< I2C_CTL setting for I2C control bits. It would set SI and AA bits \hideinitializer */
  33. #define I2C_CTL_STA 0x20UL /*!< I2C_CTL setting for I2C control bits. It would set STA bit \hideinitializer */
  34. #define I2C_CTL_STO 0x10UL /*!< I2C_CTL setting for I2C control bits. It would set STO bit \hideinitializer */
  35. #define I2C_CTL_AA 0x04UL /*!< I2C_CTL setting for I2C control bits. It would set AA bit \hideinitializer */
  36. /*---------------------------------------------------------------------------------------------------------*/
  37. /* I2C GCMode constant definitions. */
  38. /*---------------------------------------------------------------------------------------------------------*/
  39. #define I2C_GCMODE_ENABLE 1 /*!< Enable I2C GC Mode \hideinitializer */
  40. #define I2C_GCMODE_DISABLE 0 /*!< Disable I2C GC Mode \hideinitializer */
  41. /*@}*/ /* end of group I2C_EXPORTED_CONSTANTS */
  42. typedef struct
  43. {
  44. volatile uint32_t CTL0; /*!< [0x0000] I2C Control Register 0 */
  45. volatile uint32_t ADDR0; /*!< [0x0004] I2C Slave Address Register0 */
  46. volatile uint32_t DAT; /*!< [0x0008] I2C Data Register */
  47. volatile uint32_t STATUS0; /*!< [0x000c] I2C Status Register 0 */
  48. volatile uint32_t CLKDIV; /*!< [0x0010] I2C Clock Divided Register */
  49. volatile uint32_t TOCTL; /*!< [0x0014] I2C Time-out Control Register */
  50. volatile uint32_t ADDR1; /*!< [0x0018] I2C Slave Address Register1 */
  51. volatile uint32_t ADDR2; /*!< [0x001c] I2C Slave Address Register2 */
  52. volatile uint32_t ADDR3; /*!< [0x0020] I2C Slave Address Register3 */
  53. volatile uint32_t ADDRMSK0; /*!< [0x0024] I2C Slave Address Mask Register0 */
  54. volatile uint32_t ADDRMSK1; /*!< [0x0028] I2C Slave Address Mask Register1 */
  55. volatile uint32_t ADDRMSK2; /*!< [0x002c] I2C Slave Address Mask Register2 */
  56. volatile uint32_t ADDRMSK3; /*!< [0x0030] I2C Slave Address Mask Register3 */
  57. volatile uint32_t RESERVE0[2];
  58. volatile uint32_t WKCTL; /*!< [0x003c] I2C Wake-up Control Register */
  59. volatile uint32_t WKSTS; /*!< [0x0040] I2C Wake-up Status Register */
  60. volatile uint32_t CTL1; /*!< [0x0044] I2C Control Register 1 */
  61. volatile uint32_t STATUS1; /*!< [0x0048] I2C Status Register 1 */
  62. volatile uint32_t TMCTL; /*!< [0x004c] I2C Timing Configure Control Register */
  63. volatile uint32_t BUSCTL; /*!< [0x0050] I2C Bus Management Control Register */
  64. volatile uint32_t BUSTCTL; /*!< [0x0054] I2C Bus Management Timer Control Register */
  65. volatile uint32_t BUSSTS; /*!< [0x0058] I2C Bus Management Status Register */
  66. volatile uint32_t PKTSIZE; /*!< [0x005c] I2C Packet Error Checking Byte Number Register */
  67. volatile uint32_t PKTCRC; /*!< [0x0060] I2C Packet Error Checking Byte Value Register */
  68. volatile uint32_t BUSTOUT; /*!< [0x0064] I2C Bus Management Timer Register */
  69. volatile uint32_t CLKTOUT; /*!< [0x0068] I2C Bus Management Clock Low Timer Register */
  70. } I2C_T;
  71. #define I2C_CTL0_AA_Pos (2) /*!< I2C_T::CTL: AA Position */
  72. #define I2C_CTL0_AA_Msk (0x1ul << I2C_CTL0_AA_Pos) /*!< I2C_T::CTL: AA Mask */
  73. #define I2C_CTL0_SI_Pos (3) /*!< I2C_T::CTL: SI Position */
  74. #define I2C_CTL0_SI_Msk (0x1ul << I2C_CTL0_SI_Pos) /*!< I2C_T::CTL: SI Mask */
  75. #define I2C_CTL0_STO_Pos (4) /*!< I2C_T::CTL: STO Position */
  76. #define I2C_CTL0_STO_Msk (0x1ul << I2C_CTL0_STO_Pos) /*!< I2C_T::CTL: STO Mask */
  77. #define I2C_CTL0_STA_Pos (5) /*!< I2C_T::CTL: STA Position */
  78. #define I2C_CTL0_STA_Msk (0x1ul << I2C_CTL0_STA_Pos) /*!< I2C_T::CTL: STA Mask */
  79. #define I2C_CTL0_I2CEN_Pos (6) /*!< I2C_T::CTL: I2CEN Position */
  80. #define I2C_CTL0_I2CEN_Msk (0x1ul << I2C_CTL0_I2CEN_Pos) /*!< I2C_T::CTL: I2CEN Mask */
  81. #define I2C_CTL0_INTEN_Pos (7) /*!< I2C_T::CTL: INTEN Position */
  82. #define I2C_CTL0_INTEN_Msk (0x1ul << I2C_CTL0_INTEN_Pos) /*!< I2C_T::CTL: INTEN Mask */
  83. #define I2C_ADDR0_GC_Pos (0) /*!< I2C_T::ADDR0: GC Position */
  84. #define I2C_ADDR0_GC_Msk (0x1ul << I2C_ADDR0_GC_Pos) /*!< I2C_T::ADDR0: GC Mask */
  85. #define I2C_ADDR0_ADDR_Pos (1) /*!< I2C_T::ADDR0: ADDR Position */
  86. #define I2C_ADDR0_ADDR_Msk (0x3fful << I2C_ADDR0_ADDR_Pos) /*!< I2C_T::ADDR0: ADDR Mask */
  87. #define I2C_DAT_DAT_Pos (0) /*!< I2C_T::DAT: DAT Position */
  88. #define I2C_DAT_DAT_Msk (0xfful << I2C_DAT_DAT_Pos) /*!< I2C_T::DAT: DAT Mask */
  89. #define I2C_STATUS0_STATUS_Pos (0) /*!< I2C_T::STATUS: STATUS Position */
  90. #define I2C_STATUS0_STATUS_Msk (0xfful << I2C_STATUS_STATUS0_Pos) /*!< I2C_T::STATUS: STATUS Mask */
  91. #define I2C_CLKDIV_DIVIDER_Pos (0) /*!< I2C_T::CLKDIV: DIVIDER Position */
  92. #define I2C_CLKDIV_DIVIDER_Msk (0x3fful << I2C_CLKDIV_DIVIDER_Pos) /*!< I2C_T::CLKDIV: DIVIDER Mask */
  93. #define I2C_TOCTL_TOIF_Pos (0) /*!< I2C_T::TOCTL: TOIF Position */
  94. #define I2C_TOCTL_TOIF_Msk (0x1ul << I2C_TOCTL_TOIF_Pos) /*!< I2C_T::TOCTL: TOIF Mask */
  95. #define I2C_TOCTL_TOCDIV4_Pos (1) /*!< I2C_T::TOCTL: TOCDIV4 Position */
  96. #define I2C_TOCTL_TOCDIV4_Msk (0x1ul << I2C_TOCTL_TOCDIV4_Pos) /*!< I2C_T::TOCTL: TOCDIV4 Mask */
  97. #define I2C_TOCTL_TOCEN_Pos (2) /*!< I2C_T::TOCTL: TOCEN Position */
  98. #define I2C_TOCTL_TOCEN_Msk (0x1ul << I2C_TOCTL_TOCEN_Pos) /*!< I2C_T::TOCTL: TOCEN Mask */
  99. #define I2C_ADDR1_GC_Pos (0) /*!< I2C_T::ADDR1: GC Position */
  100. #define I2C_ADDR1_GC_Msk (0x1ul << I2C_ADDR1_GC_Pos) /*!< I2C_T::ADDR1: GC Mask */
  101. #define I2C_ADDR1_ADDR_Pos (1) /*!< I2C_T::ADDR1: ADDR Position */
  102. #define I2C_ADDR1_ADDR_Msk (0x3fful << I2C_ADDR1_ADDR_Pos) /*!< I2C_T::ADDR1: ADDR Mask */
  103. #define I2C_ADDR2_GC_Pos (0) /*!< I2C_T::ADDR2: GC Position */
  104. #define I2C_ADDR2_GC_Msk (0x1ul << I2C_ADDR2_GC_Pos) /*!< I2C_T::ADDR2: GC Mask */
  105. #define I2C_ADDR2_ADDR_Pos (1) /*!< I2C_T::ADDR2: ADDR Position */
  106. #define I2C_ADDR2_ADDR_Msk (0x3fful << I2C_ADDR2_ADDR_Pos) /*!< I2C_T::ADDR2: ADDR Mask */
  107. #define I2C_ADDR3_GC_Pos (0) /*!< I2C_T::ADDR3: GC Position */
  108. #define I2C_ADDR3_GC_Msk (0x1ul << I2C_ADDR3_GC_Pos) /*!< I2C_T::ADDR3: GC Mask */
  109. #define I2C_ADDR3_ADDR_Pos (1) /*!< I2C_T::ADDR3: ADDR Position */
  110. #define I2C_ADDR3_ADDR_Msk (0x3fful << I2C_ADDR3_ADDR_Pos) /*!< I2C_T::ADDR3: ADDR Mask */
  111. #define I2C_ADDRMSK0_ADDRMSK_Pos (1) /*!< I2C_T::ADDRMSK0: ADDRMSK Position */
  112. #define I2C_ADDRMSK0_ADDRMSK_Msk (0x3fful << I2C_ADDRMSK0_ADDRMSK_Pos) /*!< I2C_T::ADDRMSK0: ADDRMSK Mask */
  113. #define I2C_ADDRMSK1_ADDRMSK_Pos (1) /*!< I2C_T::ADDRMSK1: ADDRMSK Position */
  114. #define I2C_ADDRMSK1_ADDRMSK_Msk (0x3fful << I2C_ADDRMSK1_ADDRMSK_Pos) /*!< I2C_T::ADDRMSK1: ADDRMSK Mask */
  115. #define I2C_ADDRMSK2_ADDRMSK_Pos (1) /*!< I2C_T::ADDRMSK2: ADDRMSK Position */
  116. #define I2C_ADDRMSK2_ADDRMSK_Msk (0x3fful << I2C_ADDRMSK2_ADDRMSK_Pos) /*!< I2C_T::ADDRMSK2: ADDRMSK Mask */
  117. #define I2C_ADDRMSK3_ADDRMSK_Pos (1) /*!< I2C_T::ADDRMSK3: ADDRMSK Position */
  118. #define I2C_ADDRMSK3_ADDRMSK_Msk (0x3fful << I2C_ADDRMSK3_ADDRMSK_Pos) /*!< I2C_T::ADDRMSK3: ADDRMSK Mask */
  119. #define I2C_WKCTL_WKEN_Pos (0) /*!< I2C_T::WKCTL: WKEN Position */
  120. #define I2C_WKCTL_WKEN_Msk (0x1ul << I2C_WKCTL_WKEN_Pos) /*!< I2C_T::WKCTL: WKEN Mask */
  121. #define I2C_WKCTL_NHDBUSEN_Pos (7) /*!< I2C_T::WKCTL: NHDBUSEN Position */
  122. #define I2C_WKCTL_NHDBUSEN_Msk (0x1ul << I2C_WKCTL_NHDBUSEN_Pos) /*!< I2C_T::WKCTL: NHDBUSEN Mask */
  123. #define I2C_WKSTS_WKIF_Pos (0) /*!< I2C_T::WKSTS: WKIF Position */
  124. #define I2C_WKSTS_WKIF_Msk (0x1ul << I2C_WKSTS_WKIF_Pos) /*!< I2C_T::WKSTS: WKIF Mask */
  125. #define I2C_WKSTS_WKAKDONE_Pos (1) /*!< I2C_T::WKSTS: WKAKDONE Position */
  126. #define I2C_WKSTS_WKAKDONE_Msk (0x1ul << I2C_WKSTS_WKAKDONE_Pos) /*!< I2C_T::WKSTS: WKAKDONE Mask */
  127. #define I2C_WKSTS_WRSTSWK_Pos (2) /*!< I2C_T::WKSTS: WRSTSWK Position */
  128. #define I2C_WKSTS_WRSTSWK_Msk (0x1ul << I2C_WKSTS_WRSTSWK_Pos) /*!< I2C_T::WKSTS: WRSTSWK Mask */
  129. #define I2C_CTL1_TXPDMAEN_Pos (0) /*!< I2C_T::CTL1: TXPDMAEN Position */
  130. #define I2C_CTL1_TXPDMAEN_Msk (0x1ul << I2C_CTL1_TXPDMAEN_Pos) /*!< I2C_T::CTL1: TXPDMAEN Mask */
  131. #define I2C_CTL1_RXPDMAEN_Pos (1) /*!< I2C_T::CTL1: RXPDMAEN Position */
  132. #define I2C_CTL1_RXPDMAEN_Msk (0x1ul << I2C_CTL1_RXPDMAEN_Pos) /*!< I2C_T::CTL1: RXPDMAEN Mask */
  133. #define I2C_CTL1_PDMARST_Pos (2) /*!< I2C_T::CTL1: PDMARST Position */
  134. #define I2C_CTL1_PDMARST_Msk (0x1ul << I2C_CTL1_PDMARST_Pos) /*!< I2C_T::CTL1: PDMARST Mask */
  135. #define I2C_CTL1_PDMASTR_Pos (8) /*!< I2C_T::CTL1: PDMASTR Position */
  136. #define I2C_CTL1_PDMASTR_Msk (0x1ul << I2C_CTL1_PDMASTR_Pos) /*!< I2C_T::CTL1: PDMASTR Mask */
  137. #define I2C_CTL1_ADDR10EN_Pos (9) /*!< I2C_T::CTL1: ADDR10EN Position */
  138. #define I2C_CTL1_ADDR10EN_Msk (0x1ul << I2C_CTL1_ADDR10EN_Pos) /*!< I2C_T::CTL1: ADDR10EN Mask */
  139. #define I2C_STATUS1_ADMAT0_Pos (0) /*!< I2C_T::STATUS1: ADMAT0 Position */
  140. #define I2C_STATUS1_ADMAT0_Msk (0x1ul << I2C_STATUS1_ADMAT0_Pos) /*!< I2C_T::STATUS1: ADMAT0 Mask */
  141. #define I2C_STATUS1_ADMAT1_Pos (1) /*!< I2C_T::STATUS1: ADMAT1 Position */
  142. #define I2C_STATUS1_ADMAT1_Msk (0x1ul << I2C_STATUS1_ADMAT1_Pos) /*!< I2C_T::STATUS1: ADMAT1 Mask */
  143. #define I2C_STATUS1_ADMAT2_Pos (2) /*!< I2C_T::STATUS1: ADMAT2 Position */
  144. #define I2C_STATUS1_ADMAT2_Msk (0x1ul << I2C_STATUS1_ADMAT2_Pos) /*!< I2C_T::STATUS1: ADMAT2 Mask */
  145. #define I2C_STATUS1_ADMAT3_Pos (3) /*!< I2C_T::STATUS1: ADMAT3 Position */
  146. #define I2C_STATUS1_ADMAT3_Msk (0x1ul << I2C_STATUS1_ADMAT3_Pos) /*!< I2C_T::STATUS1: ADMAT3 Mask */
  147. #define I2C_STATUS1_ONBUSY_Pos (8) /*!< I2C_T::STATUS1: ONBUSY Position */
  148. #define I2C_STATUS1_ONBUSY_Msk (0x1ul << I2C_STATUS1_ONBUSY_Pos) /*!< I2C_T::STATUS1: ONBUSY Mask */
  149. #define I2C_TMCTL_STCTL_Pos (0) /*!< I2C_T::TMCTL: STCTL Position */
  150. #define I2C_TMCTL_STCTL_Msk (0x1fful << I2C_TMCTL_STCTL_Pos) /*!< I2C_T::TMCTL: STCTL Mask */
  151. #define I2C_TMCTL_HTCTL_Pos (16) /*!< I2C_T::TMCTL: HTCTL Position */
  152. #define I2C_TMCTL_HTCTL_Msk (0x1fful << I2C_TMCTL_HTCTL_Pos) /*!< I2C_T::TMCTL: HTCTL Mask */
  153. #define I2C_BUSCTL_ACKMEN_Pos (0) /*!< I2C_T::BUSCTL: ACKMEN Position */
  154. #define I2C_BUSCTL_ACKMEN_Msk (0x1ul << I2C_BUSCTL_ACKMEN_Pos) /*!< I2C_T::BUSCTL: ACKMEN Mask */
  155. #define I2C_BUSCTL_PECEN_Pos (1) /*!< I2C_T::BUSCTL: PECEN Position */
  156. #define I2C_BUSCTL_PECEN_Msk (0x1ul << I2C_BUSCTL_PECEN_Pos) /*!< I2C_T::BUSCTL: PECEN Mask */
  157. #define I2C_BUSCTL_BMDEN_Pos (2) /*!< I2C_T::BUSCTL: BMDEN Position */
  158. #define I2C_BUSCTL_BMDEN_Msk (0x1ul << I2C_BUSCTL_BMDEN_Pos) /*!< I2C_T::BUSCTL: BMDEN Mask */
  159. #define I2C_BUSCTL_BMHEN_Pos (3) /*!< I2C_T::BUSCTL: BMHEN Position */
  160. #define I2C_BUSCTL_BMHEN_Msk (0x1ul << I2C_BUSCTL_BMHEN_Pos) /*!< I2C_T::BUSCTL: BMHEN Mask */
  161. #define I2C_BUSCTL_ALERTEN_Pos (4) /*!< I2C_T::BUSCTL: ALERTEN Position */
  162. #define I2C_BUSCTL_ALERTEN_Msk (0x1ul << I2C_BUSCTL_ALERTEN_Pos) /*!< I2C_T::BUSCTL: ALERTEN Mask */
  163. #define I2C_BUSCTL_SCTLOSTS_Pos (5) /*!< I2C_T::BUSCTL: SCTLOSTS Position */
  164. #define I2C_BUSCTL_SCTLOSTS_Msk (0x1ul << I2C_BUSCTL_SCTLOSTS_Pos) /*!< I2C_T::BUSCTL: SCTLOSTS Mask */
  165. #define I2C_BUSCTL_SCTLOEN_Pos (6) /*!< I2C_T::BUSCTL: SCTLOEN Position */
  166. #define I2C_BUSCTL_SCTLOEN_Msk (0x1ul << I2C_BUSCTL_SCTLOEN_Pos) /*!< I2C_T::BUSCTL: SCTLOEN Mask */
  167. #define I2C_BUSCTL_BUSEN_Pos (7) /*!< I2C_T::BUSCTL: BUSEN Position */
  168. #define I2C_BUSCTL_BUSEN_Msk (0x1ul << I2C_BUSCTL_BUSEN_Pos) /*!< I2C_T::BUSCTL: BUSEN Mask */
  169. #define I2C_BUSCTL_PECTXEN_Pos (8) /*!< I2C_T::BUSCTL: PECTXEN Position */
  170. #define I2C_BUSCTL_PECTXEN_Msk (0x1ul << I2C_BUSCTL_PECTXEN_Pos) /*!< I2C_T::BUSCTL: PECTXEN Mask */
  171. #define I2C_BUSCTL_TIDLE_Pos (9) /*!< I2C_T::BUSCTL: TIDLE Position */
  172. #define I2C_BUSCTL_TIDLE_Msk (0x1ul << I2C_BUSCTL_TIDLE_Pos) /*!< I2C_T::BUSCTL: TIDLE Mask */
  173. #define I2C_BUSCTL_PECCLR_Pos (10) /*!< I2C_T::BUSCTL: PECCLR Position */
  174. #define I2C_BUSCTL_PECCLR_Msk (0x1ul << I2C_BUSCTL_PECCLR_Pos) /*!< I2C_T::BUSCTL: PECCLR Mask */
  175. #define I2C_BUSCTL_ACKM9SI_Pos (11) /*!< I2C_T::BUSCTL: ACKM9SI Position */
  176. #define I2C_BUSCTL_ACKM9SI_Msk (0x1ul << I2C_BUSCTL_ACKM9SI_Pos) /*!< I2C_T::BUSCTL: ACKM9SI Mask */
  177. #define I2C_BUSCTL_BCDIEN_Pos (12) /*!< I2C_T::BUSCTL: BCDIEN Position */
  178. #define I2C_BUSCTL_BCDIEN_Msk (0x1ul << I2C_BUSCTL_BCDIEN_Pos) /*!< I2C_T::BUSCTL: BCDIEN Mask */
  179. #define I2C_BUSCTL_PECDIEN_Pos (13) /*!< I2C_T::BUSCTL: PECDIEN Position */
  180. #define I2C_BUSCTL_PECDIEN_Msk (0x1ul << I2C_BUSCTL_PECDIEN_Pos) /*!< I2C_T::BUSCTL: PECDIEN Mask */
  181. #define I2C_BUSTCTL_BUSTOEN_Pos (0) /*!< I2C_T::BUSTCTL: BUSTOEN Position */
  182. #define I2C_BUSTCTL_BUSTOEN_Msk (0x1ul << I2C_BUSTCTL_BUSTOEN_Pos) /*!< I2C_T::BUSTCTL: BUSTOEN Mask */
  183. #define I2C_BUSTCTL_CLKTOEN_Pos (1) /*!< I2C_T::BUSTCTL: CLKTOEN Position */
  184. #define I2C_BUSTCTL_CLKTOEN_Msk (0x1ul << I2C_BUSTCTL_CLKTOEN_Pos) /*!< I2C_T::BUSTCTL: CLKTOEN Mask */
  185. #define I2C_BUSTCTL_BUSTOIEN_Pos (2) /*!< I2C_T::BUSTCTL: BUSTOIEN Position */
  186. #define I2C_BUSTCTL_BUSTOIEN_Msk (0x1ul << I2C_BUSTCTL_BUSTOIEN_Pos) /*!< I2C_T::BUSTCTL: BUSTOIEN Mask */
  187. #define I2C_BUSTCTL_CLKTOIEN_Pos (3) /*!< I2C_T::BUSTCTL: CLKTOIEN Position */
  188. #define I2C_BUSTCTL_CLKTOIEN_Msk (0x1ul << I2C_BUSTCTL_CLKTOIEN_Pos) /*!< I2C_T::BUSTCTL: CLKTOIEN Mask */
  189. #define I2C_BUSTCTL_TORSTEN_Pos (4) /*!< I2C_T::BUSTCTL: TORSTEN Position */
  190. #define I2C_BUSTCTL_TORSTEN_Msk (0x1ul << I2C_BUSTCTL_TORSTEN_Pos) /*!< I2C_T::BUSTCTL: TORSTEN Mask */
  191. #define I2C_BUSSTS_BUSY_Pos (0) /*!< I2C_T::BUSSTS: BUSY Position */
  192. #define I2C_BUSSTS_BUSY_Msk (0x1ul << I2C_BUSSTS_BUSY_Pos) /*!< I2C_T::BUSSTS: BUSY Mask */
  193. #define I2C_BUSSTS_BCDONE_Pos (1) /*!< I2C_T::BUSSTS: BCDONE Position */
  194. #define I2C_BUSSTS_BCDONE_Msk (0x1ul << I2C_BUSSTS_BCDONE_Pos) /*!< I2C_T::BUSSTS: BCDONE Mask */
  195. #define I2C_BUSSTS_PECERR_Pos (2) /*!< I2C_T::BUSSTS: PECERR Position */
  196. #define I2C_BUSSTS_PECERR_Msk (0x1ul << I2C_BUSSTS_PECERR_Pos) /*!< I2C_T::BUSSTS: PECERR Mask */
  197. #define I2C_BUSSTS_ALERT_Pos (3) /*!< I2C_T::BUSSTS: ALERT Position */
  198. #define I2C_BUSSTS_ALERT_Msk (0x1ul << I2C_BUSSTS_ALERT_Pos) /*!< I2C_T::BUSSTS: ALERT Mask */
  199. #define I2C_BUSSTS_SCTLDIN_Pos (4) /*!< I2C_T::BUSSTS: SCTLDIN Position */
  200. #define I2C_BUSSTS_SCTLDIN_Msk (0x1ul << I2C_BUSSTS_SCTLDIN_Pos) /*!< I2C_T::BUSSTS: SCTLDIN Mask */
  201. #define I2C_BUSSTS_BUSTO_Pos (5) /*!< I2C_T::BUSSTS: BUSTO Position */
  202. #define I2C_BUSSTS_BUSTO_Msk (0x1ul << I2C_BUSSTS_BUSTO_Pos) /*!< I2C_T::BUSSTS: BUSTO Mask */
  203. #define I2C_BUSSTS_CLKTO_Pos (6) /*!< I2C_T::BUSSTS: CLKTO Position */
  204. #define I2C_BUSSTS_CLKTO_Msk (0x1ul << I2C_BUSSTS_CLKTO_Pos) /*!< I2C_T::BUSSTS: CLKTO Mask */
  205. #define I2C_BUSSTS_PECDONE_Pos (7) /*!< I2C_T::BUSSTS: PECDONE Position */
  206. #define I2C_BUSSTS_PECDONE_Msk (0x1ul << I2C_BUSSTS_PECDONE_Pos) /*!< I2C_T::BUSSTS: PECDONE Mask */
  207. #define I2C_PKTSIZE_PLDSIZE_Pos (0) /*!< I2C_T::PKTSIZE: PLDSIZE Position */
  208. #define I2C_PKTSIZE_PLDSIZE_Msk (0x1fful << I2C_PKTSIZE_PLDSIZE_Pos) /*!< I2C_T::PKTSIZE: PLDSIZE Mask */
  209. #define I2C_PKTCRC_PECCRC_Pos (0) /*!< I2C_T::PKTCRC: PECCRC Position */
  210. #define I2C_PKTCRC_PECCRC_Msk (0xfful << I2C_PKTCRC_PECCRC_Pos) /*!< I2C_T::PKTCRC: PECCRC Mask */
  211. #define I2C_BUSTOUT_BUSTO_Pos (0) /*!< I2C_T::BUSTOUT: BUSTO Position */
  212. #define I2C_BUSTOUT_BUSTO_Msk (0xfful << I2C_BUSTOUT_BUSTO_Pos) /*!< I2C_T::BUSTOUT: BUSTO Mask */
  213. #define I2C_CLKTOUT_CLKTO_Pos (0) /*!< I2C_T::CLKTOUT: CLKTO Position */
  214. #define I2C_CLKTOUT_CLKTO_Msk (0xfful << I2C_CLKTOUT_CLKTO_Pos) /*!< I2C_T::CLKTOUT: CLKTO Mask */
  215. #define I2C0 ((I2C_T *) I2C0_BA)
  216. #define I2C1 ((I2C_T *) I2C1_BA)
  217. #define I2C2 ((I2C_T *) I2C2_BA)
  218. #define I2C3 ((I2C_T *) I2C3_BA)
  219. /** @addtogroup I2C_EXPORTED_FUNCTIONS I2C Exported Functions
  220. @{
  221. */
  222. /**
  223. * @brief The macro is used to set I2C bus condition at One Time
  224. *
  225. * @param[in] i2c Specify I2C port
  226. * @param[in] u8Ctrl A byte writes to I2C control register
  227. *
  228. * @return None
  229. *
  230. * @details Set I2C_CTL register to control I2C bus conditions of START, STOP, SI, ACK.
  231. * \hideinitializer
  232. */
  233. #define I2C_SET_CONTROL_REG(i2c, u8Ctrl) ((i2c)->CTL0 = ((i2c)->CTL0 & ~0x3c) | (u8Ctrl))
  234. /**
  235. * @brief The macro is used to set START condition of I2C Bus
  236. *
  237. * @param[in] i2c Specify I2C port
  238. *
  239. * @return None
  240. *
  241. * @details Set the I2C bus START condition in I2C_CTL register.
  242. * \hideinitializer
  243. */
  244. #define I2C_START(i2c) ((i2c)->CTL0 = ((i2c)->CTL0 & ~I2C_CTL0_SI_Msk) | I2C_CTL0_STA_Msk)
  245. /**
  246. * @brief The macro is used to wait I2C bus status get ready
  247. *
  248. * @param[in] i2c Specify I2C port
  249. *
  250. * @return None
  251. *
  252. * @details When a new status is presented of I2C bus, the SI flag will be set in I2C_CTL register.
  253. * \hideinitializer
  254. */
  255. #define I2C_WAIT_READY(i2c) while(!((i2c)->CTL0 & I2C_CTL0_SI_Msk))
  256. /**
  257. * @brief The macro is used to Read I2C Bus Data Register
  258. *
  259. * @param[in] i2c Specify I2C port
  260. *
  261. * @return A byte of I2C data register
  262. *
  263. * @details I2C controller read data from bus and save it in I2CDAT register.
  264. * \hideinitializer
  265. */
  266. #define I2C_GET_DATA(i2c) ((i2c)->DAT)
  267. /**
  268. * @brief Write a Data to I2C Data Register
  269. *
  270. * @param[in] i2c Specify I2C port
  271. * @param[in] u8Data A byte that writes to data register
  272. *
  273. * @return None
  274. *
  275. * @details When write a data to I2C_DAT register, the I2C controller will shift it to I2C bus.
  276. * \hideinitializer
  277. */
  278. #define I2C_SET_DATA(i2c, u8Data) ((i2c)->DAT = (u8Data))
  279. /**
  280. * @brief Get I2C Bus status code
  281. *
  282. * @param[in] i2c Specify I2C port
  283. *
  284. * @return I2C status code
  285. *
  286. * @details To get this status code to monitor I2C bus event.
  287. * \hideinitializer
  288. */
  289. #define I2C_GET_STATUS(i2c) ((i2c)->STATUS0)
  290. /**
  291. * @brief Get Time-out flag from I2C Bus
  292. *
  293. * @param[in] i2c Specify I2C port
  294. *
  295. * @retval 0 I2C Bus time-out is not happened
  296. * @retval 1 I2C Bus time-out is happened
  297. *
  298. * @details When I2C bus occurs time-out event, the time-out flag will be set.
  299. * \hideinitializer
  300. */
  301. #define I2C_GET_TIMEOUT_FLAG(i2c) ( ((i2c)->TOCTL & I2C_TOCTL_TOIF_Msk) == I2C_TOCTL_TOIF_Msk ? 1:0 )
  302. /**
  303. * @brief To get wake-up flag from I2C Bus
  304. *
  305. * @param[in] i2c Specify I2C port
  306. *
  307. * @retval 0 Chip is not woken-up from power-down mode
  308. * @retval 1 Chip is woken-up from power-down mode
  309. *
  310. * @details I2C bus occurs wake-up event, wake-up flag will be set.
  311. * \hideinitializer
  312. */
  313. #define I2C_GET_WAKEUP_FLAG(i2c) ( ((i2c)->WKSTS & I2C_WKSTS_WKIF_Msk) == I2C_WKSTS_WKIF_Msk ? 1:0 )
  314. /**
  315. * @brief To clear wake-up flag
  316. *
  317. * @param[in] i2c Specify I2C port
  318. *
  319. * @return None
  320. *
  321. * @details If wake-up flag is set, use this macro to clear it.
  322. * \hideinitializer
  323. */
  324. #define I2C_CLEAR_WAKEUP_FLAG(i2c) ((i2c)->WKSTS = I2C_WKSTS_WKIF_Msk)
  325. /**
  326. * @brief Enable RX PDMA function.
  327. * @param[in] i2c The pointer of the specified I2C module.
  328. * @return None.
  329. * @details Set RXPDMAEN bit of I2C_CTL1 register to enable RX PDMA transfer function.
  330. * \hideinitializer
  331. */
  332. #define I2C_ENABLE_RX_PDMA(i2c) ((i2c)->CTL1 |= I2C_CTL1_RXPDMAEN_Msk)
  333. /**
  334. * @brief Enable TX PDMA function.
  335. * @param[in] i2c The pointer of the specified I2C module.
  336. * @return None.
  337. * @details Set TXPDMAEN bit of I2C_CTL1 register to enable TX PDMA transfer function.
  338. * \hideinitializer
  339. */
  340. #define I2C_ENABLE_TX_PDMA(i2c) ((i2c)->CTL1 |= I2C_CTL1_TXPDMAEN_Msk)
  341. /**
  342. * @brief Disable RX PDMA transfer.
  343. * @param[in] i2c The pointer of the specified I2C module.
  344. * @return None.
  345. * @details Clear RXPDMAEN bit of I2C_CTL1 register to disable RX PDMA transfer function.
  346. * \hideinitializer
  347. */
  348. #define I2C_DISABLE_RX_PDMA(i2c) ((i2c)->CTL1 &= ~I2C_CTL1_RXPDMAEN_Msk)
  349. /**
  350. * @brief Disable TX PDMA transfer.
  351. * @param[in] i2c The pointer of the specified I2C module.
  352. * @return None.
  353. * @details Clear TXPDMAEN bit of I2C_CTL1 register to disable TX PDMA transfer function.
  354. * \hideinitializer
  355. */
  356. #define I2C_DISABLE_TX_PDMA(i2c) ((i2c)->CTL1 &= ~I2C_CTL1_TXPDMAEN_Msk)
  357. /**
  358. * @brief Enable PDMA stretch function.
  359. * @param[in] i2c The pointer of the specified I2C module.
  360. * @return None.
  361. * @details Enable this function is to stretch bus by hardware after PDMA transfer is done if SI is not cleared.
  362. * \hideinitializer
  363. */
  364. #define I2C_ENABLE_PDMA_STRETCH(i2c) ((i2c)->CTL1 |= I2C_CTL1_PDMASTR_Msk)
  365. /**
  366. * @brief Disable PDMA stretch function.
  367. * @param[in] i2c The pointer of the specified I2C module.
  368. * @return None.
  369. * @details I2C will send STOP after PDMA transfers done automatically.
  370. * \hideinitializer
  371. */
  372. #define I2C_DISABLE_PDMA_STRETCH(i2c) ((i2c)->CTL1 &= ~I2C_CTL1_PDMASTR_Msk)
  373. /**
  374. * @brief Reset PDMA function.
  375. * @param[in] i2c The pointer of the specified I2C module.
  376. * @return None.
  377. * @details I2C PDMA engine will be reset after this function is called.
  378. * \hideinitializer
  379. */
  380. #define I2C_DISABLE_RST_PDMA(i2c) ((i2c)->CTL1 |= I2C_CTL1_PDMARST_Msk)
  381. /*---------------------------------------------------------------------------------------------------------*/
  382. /* inline functions */
  383. /*---------------------------------------------------------------------------------------------------------*/
  384. /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
  385. void I2C_STOP(I2C_T *i2c);
  386. void I2C_ClearTimeoutFlag(I2C_T *i2c);
  387. void I2C_Close(I2C_T *i2c);
  388. void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack);
  389. void I2C_DisableInt(I2C_T *i2c);
  390. void I2C_EnableInt(I2C_T *i2c);
  391. uint32_t I2C_GetBusClockFreq(I2C_T *i2c);
  392. uint32_t I2C_GetIntFlag(I2C_T *i2c);
  393. uint32_t I2C_GetStatus(I2C_T *i2c);
  394. uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock);
  395. uint8_t I2C_GetData(I2C_T *i2c);
  396. void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode);
  397. void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask);
  398. uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock);
  399. void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout);
  400. void I2C_DisableTimeout(I2C_T *i2c);
  401. void I2C_EnableWakeup(I2C_T *i2c);
  402. void I2C_DisableWakeup(I2C_T *i2c);
  403. void I2C_SetData(I2C_T *i2c, uint8_t u8Data);
  404. uint8_t I2C_WriteByte(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t data);
  405. uint32_t I2C_WriteMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t data[], uint32_t u32wLen);
  406. uint8_t I2C_WriteByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data);
  407. uint32_t I2C_WriteMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data[], uint32_t u32wLen);
  408. uint8_t I2C_WriteByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data);
  409. uint32_t I2C_WriteMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data[], uint32_t u32wLen);
  410. uint8_t I2C_ReadByte(I2C_T *i2c, uint8_t u8SlaveAddr);
  411. uint32_t I2C_ReadMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t rdata[], uint32_t u32rLen);
  412. uint8_t I2C_ReadByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr);
  413. uint32_t I2C_ReadMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t rdata[], uint32_t u32rLen);
  414. uint8_t I2C_ReadByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr);
  415. uint32_t I2C_ReadMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t rdata[], uint32_t u32rLen);
  416. /*@}*/ /* end of group I2C_EXPORTED_FUNCTIONS */
  417. /*@}*/ /* end of group I2C_Driver */
  418. /*@}*/ /* end of group Standard_Driver */
  419. #endif
  420. /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/