apm32f10x_sci2c.h 9.7 KB

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