apm32f10x_sci2c.h 10 KB

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