apm32e10x_sci2c.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*!
  2. * @file apm32e10x_sci2c.h
  3. *
  4. * @brief This file contains all the prototypes,enumeration and macros for the SCI2C(I2C3, I2C4) peripheral
  5. *
  6. * @version V1.0.2
  7. *
  8. * @date 2022-12-31
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2021-2023 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be useful and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. /* Define to prevent recursive inclusion */
  26. #ifndef __APM32E10X_SCI2C_H
  27. #define __APM32E10X_SCI2C_H
  28. /* Includes */
  29. #include "apm32e10x.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /** @addtogroup APM32E10x_StdPeriphDriver
  34. @{
  35. */
  36. /** @addtogroup SCI2C_Driver
  37. @{
  38. */
  39. /** @defgroup SCI2C_Macros Macros
  40. @{
  41. */
  42. /* Macros description */
  43. #define SCI2C_CTRL1_RESET_VALUE ((uint32_t)0x3E)
  44. #define SCI2C_TARADDR_RESET_VALUE ((uint32_t)0x1055)
  45. #define SCI2C_SLAADDR_RESET_VALUE ((uint32_t)0x55)
  46. #define SCI2C_HSMC_RESET_VALUE ((uint32_t)0x07)
  47. #define SCI2C_DATA_RESET_VALUE ((uint32_t)0x00)
  48. #define SCI2C_SSCHC_RESET_VALUE ((uint32_t)0x190)
  49. #define SCI2C_SSCLC_RESET_VALUE ((uint32_t)0x1D6)
  50. #define SCI2C_FSCHC_RESET_VALUE ((uint32_t)0x3C)
  51. #define SCI2C_FSCLC_RESET_VALUE ((uint32_t)0x82)
  52. #define SCI2C_HSCHC_RESET_VALUE ((uint32_t)0x06)
  53. #define SCI2C_HSCLC_RESET_VALUE ((uint32_t)0x10)
  54. #define SCI2C_INTEN_RESET_VALUE ((uint32_t)0x00)
  55. #define SCI2C_RFT_RESET_VALUE ((uint32_t)0x00)
  56. #define SCI2C_TFT_RESET_VALUE ((uint32_t)0x00)
  57. #define SCI2C_CTRL2_RESET_VALUE ((uint32_t)0x00)
  58. #define SCI2C_TFL_RESET_VALUE ((uint32_t)0x00)
  59. #define SCI2C_RFL_RESET_VALUE ((uint32_t)0x00)
  60. #define SCI2C_SDAHOLD_RESET_VALUE ((uint32_t)0x01)
  61. #define SCI2C_SDNO_RESET_VALUE ((uint32_t)0x00)
  62. #define SCI2C_DMACTRL_RESET_VALUE ((uint32_t)0x00)
  63. #define SCI2C_DTDL_RESET_VALUE ((uint32_t)0x00)
  64. #define SCI2C_DRDL_RESET_VALUE ((uint32_t)0x00)
  65. #define SCI2C_SDADLY_RESET_VALUE ((uint32_t)0x64)
  66. #define SCI2C_GCA_RESET_VALUE ((uint32_t)0x01)
  67. #define SCI2C_LSSSL_RESET_VALUE ((uint32_t)0x05)
  68. #define SCI2C_HSSSL_RESET_VALUE ((uint32_t)0x01)
  69. #define SCI2C_FIFO_DEPTH (0X08)
  70. /**@} end of group SCI2C_Macros */
  71. /** @defgroup SCI2C_Enumerations Enumerations
  72. @{
  73. */
  74. /**
  75. * @brief SCI2C speed enumeration
  76. */
  77. typedef enum
  78. {
  79. SCI2C_SPEED_STANDARD = 1,
  80. SCI2C_SPEED_FAST,
  81. SCI2C_SPEED_HIGH
  82. }SCI2C_SPEED_T;
  83. /**
  84. * @brief Address mode
  85. */
  86. typedef enum
  87. {
  88. SCI2C_ADDR_MODE_7BIT,
  89. SCI2C_ADDR_MODE_10BIT
  90. }SCI2C_ADDR_MODE_T;
  91. /**
  92. * @brief SCI2C mode enumeration
  93. */
  94. typedef enum
  95. {
  96. SCI2C_MODE_MASTER,
  97. SCI2C_MODE_SLAVE
  98. }SCI2C_MODE_T;
  99. /**
  100. * @brief Restart enable or disable
  101. */
  102. typedef enum
  103. {
  104. SCI2C_RESTART_DISABLE,
  105. SCI2C_RESTART_ENABLE
  106. }SCI2C_RESTART_T;
  107. /**
  108. * @brief Enable or disable generate stop condition
  109. */
  110. typedef enum
  111. {
  112. SCI2C_STOP_DISABLE,
  113. SCI2C_STOP_ENABLE
  114. }SCI2C_STOP_T;
  115. /**
  116. * @brief Data direction
  117. */
  118. typedef enum
  119. {
  120. SCI2C_DATA_DIR_WRITE,
  121. SCI2C_DATA_DIR_READ,
  122. }SCI2C_DATA_DIR_T;
  123. /**
  124. * @brief SCI2C interrupt
  125. */
  126. typedef enum
  127. {
  128. SCI2C_INT_RFU = BIT0, /*!< Rx FIFO underflow interrupt */
  129. SCI2C_INT_RFO = BIT1, /*!< Rx FIFO onverflow interrupt */
  130. SCI2C_INT_RFF = BIT2, /*!< Rx FIFO full interrupt */
  131. SCI2C_INT_TFO = BIT3, /*!< Tx FIFO onverflow interrupt */
  132. SCI2C_INT_TFE = BIT4, /*!< Tx FIFO empty interrupt */
  133. SCI2C_INT_RR = BIT5, /*!< Read request interrupt */
  134. SCI2C_INT_TA = BIT6, /*!< Tx abort interrupt */
  135. SCI2C_INT_RD = BIT7, /*!< Read done interrupt */
  136. SCI2C_INT_ACT = BIT8, /*!< Activity interrupt */
  137. SCI2C_INT_STPD = BIT9, /*!< Stop detect interrupt */
  138. SCI2C_INT_STAD = BIT10, /*!< Start detect interrupt */
  139. SCI2C_INT_GC = BIT11, /*!< Gernal call interrupt */
  140. SCI2C_INT_RSTAD = BIT12, /*!< Restart detect interrupt */
  141. SCI2C_INT_MOH = BIT13, /*!< Master on hold interrupt */
  142. SCI2C_INT_ALL = BIT15 /*!< All interrupt */
  143. }SCI2C_INT_T;
  144. /**
  145. * @brief Flag enumeration
  146. */
  147. typedef enum
  148. {
  149. SCI2C_FLAG_ACT = BIT0, /*!< Activity flag */
  150. SCI2C_FLAG_TFNF = BIT1, /*!< Tx FIFO not full flag */
  151. SCI2C_FLAG_TFE = BIT2, /*!< Tx FIFO empty flag */
  152. SCI2C_FLAG_RFNE = BIT3, /*!< Rx FIFO not empty flag */
  153. SCI2C_FLAG_RFF = BIT4, /*!< Rx FIFO full flag */
  154. SCI2C_FLAG_MA = BIT5, /*!< Master activity flag */
  155. SCI2C_FLAG_SA = BIT6, /*!< Slave activity flag */
  156. SCI2C_FLAG_I2CEN = BIT8 | BIT0, /*!< I2C enable flag */
  157. SCI2C_FLAG_SDWB = BIT8 | BIT1, /*!< Slave disable while busy flag */
  158. SCI2C_FLAG_SRDL = BIT8 | BIT2 /*!< Slave receive data lost flag */
  159. }SCI2C_FLAG_T;
  160. /**
  161. * @brief Tx abort source
  162. */
  163. typedef enum
  164. {
  165. SCI2C_TAS_AD7NA = BIT0, /*!< 7 bit address mode NACK */
  166. SCI2C_TAS_AD10FBNA = BIT1, /*!< 10 bit address mode first byte NACK */
  167. SCI2C_TAS_AD10SBNA = BIT2, /*!< 10 bit address mode second byte NACK */
  168. SCI2C_TAS_TDNA = BIT3, /*!< Tx data NACK */
  169. SCI2C_TAS_GCNA = BIT4, /*!< Gernal call NACK */
  170. SCI2C_TAS_GCR = BIT5, /*!< Gernal call read */
  171. SCI2C_TAS_HSAD = BIT6, /*!< High speed ack detected */
  172. SCI2C_TAS_SNR = BIT7, /*!< Start byte no restart */
  173. SCI2C_TAS_RNR10B = BIT8, /*!< Read 10bit address mode when restart disable */
  174. SCI2C_TAS_MSTDIS = BIT9, /*!< Master disable */
  175. SCI2C_TAS_ARBLOST = BIT10, /*!< Arbitration lost */
  176. SCI2C_TAS_LFTF = BIT11, /*!< Slave flush tx FIFO */
  177. SCI2C_TAS_SAL = BIT12, /*!< Slave arbitration lost */
  178. SCI2C_TAS_SRI = BIT13, /*!< Slave read done */
  179. SCI2C_TAS_USRARB = BIT14, /*!< User abort */
  180. SCI2C_TAS_FLUCNT = BIT15 /*!< Tx flush counter */
  181. }SCI2C_TAS_T;
  182. /**
  183. * @brief DMA Enable
  184. */
  185. typedef enum
  186. {
  187. SCI2C_DMA_RX = BIT0,
  188. SCI2C_DMA_TX = BIT1,
  189. }SCI2C_DMA_T;
  190. /**@} end of group SCI2C_Enumerations */
  191. /** @defgroup SCI2C_Structures
  192. @{
  193. */
  194. /**
  195. * @brief Struct description
  196. */
  197. typedef struct
  198. {
  199. uint16_t slaveAddr; /*!< Slave address. */
  200. SCI2C_MODE_T mode; /*!< Specifies mode, master mode or slave mode */
  201. SCI2C_SPEED_T speed; /*!< Specifies speed. Standard speed, fast speed or high speed. */
  202. uint16_t clkLowPeriod; /*!< SCL high period */
  203. uint16_t clkHighPeriod; /*!< SCL low period */
  204. uint8_t rxFifoThreshold; /*!< Rx FIFO threshold */
  205. uint8_t txFifoThreshold; /*!< Tx FIFO threshold */
  206. SCI2C_RESTART_T restart; /*!< Enable or disable restart */
  207. SCI2C_ADDR_MODE_T addrMode; /*!< Address mode. 7-bit or 10-bit mode. */
  208. }SCI2C_Config_T;
  209. /**@} end of group SCI2C_Structures */
  210. /** @defgroup SCI2C_Functions Functions
  211. @{
  212. */
  213. /* Reset */
  214. void SCI2C_Reset(SCI2C_T *i2c);
  215. /* Configuration */
  216. void SCI2C_Config(SCI2C_T *i2c, SCI2C_Config_T *sci2cConfig);
  217. void SCI2C_ConfigStructInit(SCI2C_Config_T *sci2cConfig);
  218. /* Stop detect */
  219. void SCI2C_EnableStopDetectAddressed(SCI2C_T *i2c);
  220. void SCI2C_DisableStopDetectAddressed(SCI2C_T *i2c);
  221. void SCI2C_EnableStopDetectMasterActivity(SCI2C_T *i2c);
  222. void SCI2C_DisableStopDetectMasterActivity(SCI2C_T *i2c);
  223. /* Restart */
  224. void SCI2C_EnableRestart(SCI2C_T *i2c);
  225. void SCI2C_DisableRestart(SCI2C_T *i2c);
  226. /* Speed */
  227. void SCI2C_ConfigSpeed(SCI2C_T *i2c, SCI2C_SPEED_T speed);
  228. /* Address */
  229. void SCI2C_ConfigMasterAddr(SCI2C_T *i2c, SCI2C_ADDR_MODE_T mode, uint16_t addr);
  230. void SCI2C_ConfigSlaveAddr(SCI2C_T *i2c, SCI2C_ADDR_MODE_T mode, uint16_t addr);
  231. /* Master mode and slave mode */
  232. void SCI2C_EnableMasterMode(SCI2C_T *i2c);
  233. void SCI2C_DisableMasterMode(SCI2C_T *i2c);
  234. void SCI2C_EnableSlaveMode(SCI2C_T *i2c);
  235. void SCI2C_DisableSlaveMode(SCI2C_T *i2c);
  236. void SCI2C_ConfigMasterCode(SCI2C_T *i2c, uint8_t code);
  237. /* Data */
  238. void SCI2C_ConfigDataDir(SCI2C_T *i2c, SCI2C_DATA_DIR_T dir);
  239. void SCI2C_TxData(SCI2C_T *i2c, uint8_t data);
  240. uint8_t SCI2C_RxData(SCI2C_T *i2c);
  241. void SCI2C_ConfigDataRegister(SCI2C_T *i2c, SCI2C_STOP_T stop, SCI2C_DATA_DIR_T dataDir, uint8_t data);
  242. /* Rx and Tx FIFO */
  243. uint8_t SCI2C_ReadRxFifoDataCnt(SCI2C_T *i2c);
  244. uint8_t SCI2C_ReadTxFifoDataCnt(SCI2C_T *i2c);
  245. void SCI2C_ConfigRxFifoThreshold(SCI2C_T *i2c, uint8_t threshold);
  246. void SCI2C_ConfigTxFifoThreshold(SCI2C_T *i2c, uint8_t threshold);
  247. /* I2C Enable, disable, abort, block */
  248. void SCI2C_Enable(SCI2C_T *i2c);
  249. void SCI2C_Disable(SCI2C_T *i2c);
  250. void SCI2C_Abort(SCI2C_T *i2c);
  251. void SCI2C_BlockTxCmd(SCI2C_T *i2c, uint8_t enable);
  252. /* SCL and SDA */
  253. void SCI2C_ConfigClkPeriod(SCI2C_T *i2c, SCI2C_SPEED_T speed, uint16_t highPeriod, uint16_t lowPeriod);
  254. void SCI2C_ConfigSDAHoldTime(SCI2C_T *i2c, uint16_t txHold, uint8_t rxHold);
  255. void SCI2C_ConfigSDADelayTime(SCI2C_T *i2c, uint8_t delay);
  256. /* ACK and NACK */
  257. void SCI2C_GernalCallAck(SCI2C_T *i2c, uint8_t enable);
  258. void SCI2C_SlaveDataNackOnly(SCI2C_T *i2c, uint8_t enable);
  259. /* Abort */
  260. uint32_t SCI2C_ReadTxAbortSource(SCI2C_T *i2c);
  261. /* DMA */
  262. void SCI2C_EnableDMA(SCI2C_T *i2c, SCI2C_DMA_T dma);
  263. void SCI2C_DisableDMA(SCI2C_T *i2c, SCI2C_DMA_T dma);
  264. void SCI2C_ConfigDMATxDataLevel(SCI2C_T *i2c, uint8_t cnt);
  265. void SCI2C_ConfigDMARxDataLevel(SCI2C_T *i2c, uint8_t cnt);
  266. /* Spike suppression limit */
  267. void SCI2C_ConfigSpikeSuppressionLimit(SCI2C_T *i2c, SCI2C_SPEED_T speed, uint8_t limit);
  268. /* Ingerrupt and flag */
  269. uint8_t SCI2C_ReadStatusFlag(SCI2C_T *i2c, SCI2C_FLAG_T flag);
  270. void SCI2C_ClearIntFlag(SCI2C_T *i2c, SCI2C_INT_T flag);
  271. uint8_t SCI2C_ReadIntFlag(SCI2C_T *i2c, SCI2C_INT_T flag);
  272. uint8_t SCI2C_ReadRawIntFlag(SCI2C_T *i2c, SCI2C_INT_T flag);
  273. void SCI2C_EnableInterrupt(SCI2C_T *i2c, uint16_t interrupt);
  274. void SCI2C_DisableInterrupt(SCI2C_T *i2c, uint16_t interrupt);
  275. /**@} end of group SCI2C_Functions */
  276. /**@} end of group SCI2C_Driver */
  277. /**@} end of group APM32E10x_StdPeriphDriver */
  278. #ifdef __cplusplus
  279. }
  280. #endif
  281. #endif /* __APM32E10X_SCI2C_H */