1
0

apm32f10x_sci2c.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*!
  2. * @file apm32f10x_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.4
  7. *
  8. * @date 2022-12-01
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2020-2022 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 __APM32F10X_SCI2C_H
  27. #define __APM32F10X_SCI2C_H
  28. /* Includes */
  29. #include "apm32f10x.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /** @addtogroup APM32F10x_StdPeriphDriver
  34. @{
  35. */
  36. /** @addtogroup SCI2C_Driver SCI2C Driver
  37. @{
  38. */
  39. /** @defgroup SCI2C_Enumerations Enumerations
  40. @{
  41. */
  42. /**
  43. * @brief SCI2C speed enumeration
  44. */
  45. typedef enum
  46. {
  47. SCI2C_SPEED_STANDARD = 1,
  48. SCI2C_SPEED_FAST,
  49. SCI2C_SPEED_HIGH
  50. } SCI2C_SPEED_T;
  51. /**
  52. * @brief Address mode
  53. */
  54. typedef enum
  55. {
  56. SCI2C_ADDR_MODE_7BIT,
  57. SCI2C_ADDR_MODE_10BIT
  58. } SCI2C_ADDR_MODE_T;
  59. /**
  60. * @brief SCI2C mode enumeration
  61. */
  62. typedef enum
  63. {
  64. SCI2C_MODE_MASTER,
  65. SCI2C_MODE_SLAVE
  66. } SCI2C_MODE_T;
  67. /**
  68. * @brief Restart enable or disable
  69. */
  70. typedef enum
  71. {
  72. SCI2C_RESTART_DISABLE,
  73. SCI2C_RESTART_ENABLE
  74. } SCI2C_RESTART_T;
  75. /**
  76. * @brief Enable or disable generate stop condition
  77. */
  78. typedef enum
  79. {
  80. SCI2C_STOP_DISABLE,
  81. SCI2C_STOP_ENABLE
  82. } SCI2C_STOP_T;
  83. /**
  84. * @brief Data direction
  85. */
  86. typedef enum
  87. {
  88. SCI2C_DATA_DIR_WRITE,
  89. SCI2C_DATA_DIR_READ,
  90. } SCI2C_DATA_DIR_T;
  91. /**
  92. * @brief SCI2C interrupt
  93. */
  94. typedef enum
  95. {
  96. SCI2C_INT_RFU = BIT0, /*!< Rx FIFO underflow interrupt */
  97. SCI2C_INT_RFO = BIT1, /*!< Rx FIFO onverflow interrupt */
  98. SCI2C_INT_RFF = BIT2, /*!< Rx FIFO full interrupt */
  99. SCI2C_INT_TFO = BIT3, /*!< Tx FIFO onverflow interrupt */
  100. SCI2C_INT_TFE = BIT4, /*!< Tx FIFO empty interrupt */
  101. SCI2C_INT_RR = BIT5, /*!< Read request interrupt */
  102. SCI2C_INT_TA = BIT6, /*!< Tx abort interrupt */
  103. SCI2C_INT_RD = BIT7, /*!< Read done interrupt */
  104. SCI2C_INT_ACT = BIT8, /*!< Activity interrupt */
  105. SCI2C_INT_STPD = BIT9, /*!< Stop detect interrupt */
  106. SCI2C_INT_STAD = BIT10, /*!< Start detect interrupt */
  107. SCI2C_INT_GC = BIT11, /*!< Gernal call interrupt */
  108. SCI2C_INT_RSTAD = BIT12, /*!< Restart detect interrupt */
  109. SCI2C_INT_MOH = BIT13, /*!< Master on hold interrupt */
  110. SCI2C_INT_ALL = BIT15 /*!< All interrupt */
  111. } SCI2C_INT_T;
  112. /**
  113. * @brief Flag enumeration
  114. */
  115. typedef enum
  116. {
  117. SCI2C_FLAG_ACT = BIT0, /*!< Activity flag */
  118. SCI2C_FLAG_TFNF = BIT1, /*!< Tx FIFO not full flag */
  119. SCI2C_FLAG_TFE = BIT2, /*!< Tx FIFO empty flag */
  120. SCI2C_FLAG_RFNE = BIT3, /*!< Rx FIFO not empty flag */
  121. SCI2C_FLAG_RFF = BIT4, /*!< Rx FIFO full flag */
  122. SCI2C_FLAG_MA = BIT5, /*!< Master activity flag */
  123. SCI2C_FLAG_SA = BIT6, /*!< Slave activity flag */
  124. SCI2C_FLAG_I2CEN = BIT8 | BIT0, /*!< I2C enable flag */
  125. SCI2C_FLAG_SDWB = BIT8 | BIT1, /*!< Slave disable while busy flag */
  126. SCI2C_FLAG_SRDL = BIT8 | BIT2 /*!< Slave receive data lost flag */
  127. } SCI2C_FLAG_T;
  128. /**
  129. * @brief Tx abort source
  130. */
  131. typedef enum
  132. {
  133. SCI2C_TAS_AD7NA = BIT0, /*!< 7 bit address mode NACK */
  134. SCI2C_TAS_AD10FBNA = BIT1, /*!< 10 bit address mode first byte NACK */
  135. SCI2C_TAS_AD10SBNA = BIT2, /*!< 10 bit address mode second byte NACK */
  136. SCI2C_TAS_TDNA = BIT3, /*!< Tx data NACK */
  137. SCI2C_TAS_GCNA = BIT4, /*!< Gernal call NACK */
  138. SCI2C_TAS_GCR = BIT5, /*!< Gernal call read */
  139. SCI2C_TAS_HSAD = BIT6, /*!< High speed ack detected */
  140. SCI2C_TAS_SNR = BIT7, /*!< Start byte no restart */
  141. SCI2C_TAS_RNR10B = BIT8, /*!< Read 10bit address mode when restart disable */
  142. SCI2C_TAS_MSTDIS = BIT9, /*!< Master disable */
  143. SCI2C_TAS_ARBLOST = BIT10, /*!< Arbitration lost */
  144. SCI2C_TAS_LFTF = BIT11, /*!< Slave flush tx FIFO */
  145. SCI2C_TAS_SAL = BIT12, /*!< Slave arbitration lost */
  146. SCI2C_TAS_SRI = BIT13, /*!< Slave read done */
  147. SCI2C_TAS_USRARB = BIT14, /*!< User abort */
  148. SCI2C_TAS_FLUCNT = BIT15 /*!< Tx flush counter */
  149. } SCI2C_TAS_T;
  150. /**
  151. * @brief DMA Enable
  152. */
  153. typedef enum
  154. {
  155. SCI2C_DMA_RX = BIT0,
  156. SCI2C_DMA_TX = BIT1,
  157. } SCI2C_DMA_T;
  158. /**@} end of group SCI2C_Enumerations */
  159. /** @defgroup SCI2C_Macros Macros
  160. @{
  161. */
  162. /* Macros description */
  163. #define SCI2C_CTRL1_RESET_VALUE ((uint32_t)0x3E)
  164. #define SCI2C_TARADDR_RESET_VALUE ((uint32_t)0x1055)
  165. #define SCI2C_SLAADDR_RESET_VALUE ((uint32_t)0x55)
  166. #define SCI2C_HSMC_RESET_VALUE ((uint32_t)0x07)
  167. #define SCI2C_DATA_RESET_VALUE ((uint32_t)0x00)
  168. #define SCI2C_SSCHC_RESET_VALUE ((uint32_t)0x190)
  169. #define SCI2C_SSCLC_RESET_VALUE ((uint32_t)0x1D6)
  170. #define SCI2C_FSCHC_RESET_VALUE ((uint32_t)0x3C)
  171. #define SCI2C_FSCLC_RESET_VALUE ((uint32_t)0x82)
  172. #define SCI2C_HSCHC_RESET_VALUE ((uint32_t)0x06)
  173. #define SCI2C_HSCLC_RESET_VALUE ((uint32_t)0x10)
  174. #define SCI2C_INTEN_RESET_VALUE ((uint32_t)0x00)
  175. #define SCI2C_RFT_RESET_VALUE ((uint32_t)0x00)
  176. #define SCI2C_TFT_RESET_VALUE ((uint32_t)0x00)
  177. #define SCI2C_CTRL2_RESET_VALUE ((uint32_t)0x00)
  178. #define SCI2C_TFL_RESET_VALUE ((uint32_t)0x00)
  179. #define SCI2C_RFL_RESET_VALUE ((uint32_t)0x00)
  180. #define SCI2C_SDAHOLD_RESET_VALUE ((uint32_t)0x01)
  181. #define SCI2C_SDNO_RESET_VALUE ((uint32_t)0x00)
  182. #define SCI2C_DMACTRL_RESET_VALUE ((uint32_t)0x00)
  183. #define SCI2C_DTDL_RESET_VALUE ((uint32_t)0x00)
  184. #define SCI2C_DRDL_RESET_VALUE ((uint32_t)0x00)
  185. #define SCI2C_SDADLY_RESET_VALUE ((uint32_t)0x64)
  186. #define SCI2C_GCA_RESET_VALUE ((uint32_t)0x01)
  187. #define SCI2C_LSSSL_RESET_VALUE ((uint32_t)0x05)
  188. #define SCI2C_HSSSL_RESET_VALUE ((uint32_t)0x01)
  189. #define SCI2C_FIFO_DEPTH (0X08)
  190. /**@} end of group SCI2C_Macros */
  191. /** @defgroup SCI2C_Structures Structures
  192. @{
  193. */
  194. /**
  195. * @brief Structure 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_Structure */
  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 APM32F10x_StdPeriphDriver */
  278. #ifdef __cplusplus
  279. }
  280. #endif
  281. #endif /* __APM32F10X_SCI2C_H */