gd32f10x_i2c.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /**
  2. ******************************************************************************
  3. * @brief I2C functions of the firmware library.
  4. ******************************************************************************
  5. */
  6. /* Includes ------------------------------------------------------------------*/
  7. #include "gd32f10x_i2c.h"
  8. #include "gd32f10x_rcc.h"
  9. /** @addtogroup GD32F10x_Firmware
  10. * @{
  11. */
  12. /** @defgroup I2C
  13. * @brief I2C driver modules
  14. * @{
  15. */
  16. /** @defgroup I2C_Private_Defines
  17. * @{
  18. */
  19. /* I2C CTLR1_I2CEN mask */
  20. #define CTLR1_I2CEN_SET I2C_CTLR1_I2CEN
  21. #define CTLR1_I2CEN_RESET ((uint16_t)~I2C_CTLR1_I2CEN)
  22. /* I2C CTLR1_GENSTA mask */
  23. #define CTLR1_GENSTA_SET I2C_CTLR1_GENSTA
  24. #define CTLR1_GENSTA_RESET ((uint16_t)~I2C_CTLR1_GENSTA)
  25. /* I2C CTLR1_GENSTP mask */
  26. #define CTLR1_GENSTP_SET I2C_CTLR1_GENSTP
  27. #define CTLR1_GENSTP_RESET ((uint16_t)~I2C_CTLR1_GENSTP)
  28. /* I2C CTLR1_ACKEN mask */
  29. #define CTLR1_ACKEN_SET I2C_CTLR1_ACKEN
  30. #define CTLR1_ACKEN_RESET ((uint16_t)~I2C_CTLR1_ACKEN)
  31. /* I2C CTLR1_GCEN mask */
  32. #define CTLR1_GCEN_SET I2C_CTLR1_GCEN
  33. #define CTLR1_GCEN_RESET ((uint16_t)~I2C_CTLR1_GCEN)
  34. /* I2C CTLR1_SRESET mask */
  35. #define CTLR1_SRESET_SET I2C_CTLR1_SRESET
  36. #define CTLR1_SRESET_RESET ((uint16_t)~I2C_CTLR1_SRESET)
  37. /* I2C CTLR1_SALT mask */
  38. #define CTLR1_SALT_SET I2C_CTLR1_SALT
  39. #define CTLR1_SALT_RESET ((uint16_t)~I2C_CTLR1_SALT)
  40. /* I2C CTLR1_PECTRANS mask */
  41. #define CTLR1_PECTRANS_SET I2C_CTLR1_PECTRANS
  42. #define CTLR1_PECTRANS_RESET ((uint16_t)~I2C_CTLR1_PECTRANS)
  43. /* I2C CTLR1_PECEN mask */
  44. #define CTLR1_PECEN_SET I2C_CTLR1_PECEN
  45. #define CTLR1_PECEN_RESET ((uint16_t)~I2C_CTLR1_PECEN)
  46. /* I2C CTLR1_ARPEN mask */
  47. #define CTLR1_ARPEN_SET I2C_CTLR1_ARPEN
  48. #define CTLR1_ARPEN_RESET ((uint16_t)~I2C_CTLR1_ARPEN)
  49. /* I2C CTLR1_DISSTRC mask */
  50. #define CTLR1_DISSTRC_SET I2C_CTLR1_DISSTRC
  51. #define CTLR1_DISSTRC_RESET ((uint16_t)~I2C_CTLR1_DISSTRC)
  52. /* I2C registers Masks */
  53. #define CTLR1_CLEAR_MASK ((uint16_t)0xFBF5)
  54. /* I2C CTLR2_DMAON mask */
  55. #define CTLR2_DMAON_SET I2C_CTLR2_DMAON
  56. #define CTLR2_DMAON_RESET ((uint16_t)~I2C_CTLR2_DMAON)
  57. /* I2C CTLR2_DMALST mask */
  58. #define CTLR2_DMALST_SET I2C_CTLR2_DMALST
  59. #define CTLR2_DMALST_RESET ((uint16_t)~I2C_CTLR2_DMALST)
  60. /* I2C CTLR2_I2CCLK mask */
  61. #define CTLR2_I2CCLK_RESET ((uint16_t)0xFFC0)
  62. /* I2C I2CCLK_Freq_MAX */
  63. #define I2CCLK_FREQ_MAX I2C_CTLR2_I2CCLK
  64. /* I2C AR1_ADDRESS0 mask */
  65. #define AR1_ADDRESS0_SET I2C_AR1_ADDRESS0
  66. #define AR1_ADDRESS0_RESET ((uint16_t)~I2C_AR1_ADDRESS0)
  67. /* I2C AR2_DUADEN mask */
  68. #define AR2_DUADEN_SET ((uint16_t)0x0001)
  69. #define AR2_DUADEN_RESET ((uint16_t)0xFFFE)
  70. /* I2C AR2_ADDRESS2 mask */
  71. #define AR2_ADDRESS2_RESET ((uint16_t)0xFF01)
  72. /* I2C CLKR_FAST mask */
  73. #define CLKR_FAST_SET ((uint16_t)0x8000)
  74. /* I2C CLKR_CLKC mask */
  75. #define CLKR_CLKC_SET ((uint16_t)0x0FFF)
  76. /* I2C STR_FLAG mask */
  77. #define STR_FLAG_MASK ((uint32_t)0x00FFFFFF)
  78. /* I2C Interrupt Enable mask */
  79. #define INTEN_MASK ((uint32_t)0x07000000)
  80. /**
  81. * @}
  82. */
  83. /** @defgroup I2C_Private_Functions
  84. * @{
  85. */
  86. /**
  87. * @brief Reset the I2Cx interface.
  88. * @param I2Cx: the I2C interface where x can be 1 or 2.
  89. * @retval None.
  90. */
  91. void I2C_DeInit(I2C_TypeDef *I2Cx)
  92. {
  93. if (I2Cx == I2C1) {
  94. /* Enable I2C1 reset state */
  95. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_I2C1RST, ENABLE);
  96. /* Release I2C1 from reset state */
  97. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_I2C1RST, DISABLE);
  98. } else {
  99. /* Enable I2C2 reset state */
  100. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_I2C2RST, ENABLE);
  101. /* Release I2C2 from reset state */
  102. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_I2C2RST, DISABLE);
  103. }
  104. }
  105. /**
  106. * @brief Initialize the I2Cx interface parameters.
  107. * @param I2Cx: the I2C interface where x can be 1 or 2.
  108. * @param I2C_InitParaStruct: the sturct I2C_InitPara pointer.
  109. * @retval None.
  110. */
  111. void I2C_Init(I2C_TypeDef *I2Cx, I2C_InitPara *I2C_InitParaStruct)
  112. {
  113. uint16_t temp = 0, freqrange = 0;
  114. uint16_t result = 0x04;
  115. uint32_t pclk1 = 8000000;
  116. RCC_ClocksPara rcc_clocks;
  117. /* Disable I2C */
  118. I2Cx->CTLR1 &= CTLR1_I2CEN_RESET;
  119. /* CTLR2 */
  120. temp = I2Cx->CTLR2;
  121. /* I2CCLK[5:0] */
  122. temp &= CTLR2_I2CCLK_RESET;
  123. RCC_GetClocksFreq(&rcc_clocks);
  124. pclk1 = rcc_clocks.APB1_Frequency;
  125. freqrange = (uint16_t)(pclk1 / 1000000);
  126. if (freqrange > I2CCLK_FREQ_MAX) {
  127. freqrange = I2CCLK_FREQ_MAX;
  128. }
  129. temp |= freqrange;
  130. I2Cx->CTLR2 = temp;
  131. /* RTR and CLKR */
  132. temp = 0;
  133. if (I2C_InitParaStruct->I2C_BitRate <= 100000) {
  134. /* Standard mode */
  135. result = (uint16_t)(pclk1 / (I2C_InitParaStruct->I2C_BitRate << 1));
  136. if (result < 0x04) {
  137. /* The CLKR.CLKC higher than 0x04 in standard mode*/
  138. result = 0x04;
  139. }
  140. temp |= result;
  141. I2Cx->CLKR = temp;
  142. /* RTR */
  143. if (freqrange >= I2CCLK_FREQ_MAX) {
  144. I2Cx->RTR = I2CCLK_FREQ_MAX;
  145. } else {
  146. I2Cx->RTR = freqrange + 1;
  147. }
  148. } else {
  149. /* Fast mode */
  150. if (I2C_InitParaStruct->I2C_DutyCycle == I2C_DUTYCYCLE_2) {
  151. /* I2C_DutyCycle == 2 */
  152. result = (uint16_t)(pclk1 / (I2C_InitParaStruct->I2C_BitRate * 3));
  153. } else {
  154. /* I2C_DutyCycle == 16/9 */
  155. result = (uint16_t)(pclk1 / (I2C_InitParaStruct->I2C_BitRate * 25));
  156. result |= I2C_DUTYCYCLE_16_9;
  157. }
  158. if ((result & CLKR_CLKC_SET) == 0) {
  159. /* The CLKR.CLKC higher than 0x01 in fast mode*/
  160. result |= (uint16_t)0x0001;
  161. }
  162. temp |= (uint16_t)(result | CLKR_FAST_SET);
  163. /* RTR */
  164. I2Cx->RTR = (uint16_t)(((freqrange * (uint16_t)300) / (uint16_t)1000) + (uint16_t)1);
  165. }
  166. I2Cx->CLKR = temp;
  167. /* CTLR1 */
  168. I2Cx->CTLR1 |= CTLR1_I2CEN_SET;
  169. temp = I2Cx->CTLR1;
  170. temp &= CTLR1_CLEAR_MASK;
  171. temp |= (uint16_t)((uint32_t)I2C_InitParaStruct->I2C_Protocol | CTLR1_ACKEN_SET);
  172. I2Cx->CTLR1 = temp;
  173. /* AR1 */
  174. I2Cx->AR1 = (I2C_InitParaStruct->I2C_AddressingMode | I2C_InitParaStruct->I2C_DeviceAddress);
  175. }
  176. /**
  177. * @brief Initial the sturct I2C_InitPara.
  178. * @param I2C_InitParaStruct: the sturct I2C_InitPara pointer.
  179. * @retval None.
  180. */
  181. void I2C_ParaInit(I2C_InitPara *I2C_InitParaStruct)
  182. {
  183. I2C_InitParaStruct->I2C_Protocol = I2C_PROTOCOL_I2C;
  184. I2C_InitParaStruct->I2C_DutyCycle = I2C_DUTYCYCLE_2;
  185. I2C_InitParaStruct->I2C_BitRate = 10000;
  186. I2C_InitParaStruct->I2C_AddressingMode = I2C_ADDRESSING_MODE_7BIT;
  187. I2C_InitParaStruct->I2C_DeviceAddress = 0x08;
  188. }
  189. /**
  190. * @brief Enable or disable the I2Cx interface.
  191. * @param I2Cx: the I2C interface where x can be 1 or 2.
  192. * @param NewValue: new value of the I2Cx interface.
  193. * This parameter can be: ENABLE or DISABLE.
  194. * @retval None.
  195. */
  196. void I2C_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  197. {
  198. if (NewValue != DISABLE) {
  199. /* Enable the I2Cx interface */
  200. I2Cx->CTLR1 |= CTLR1_I2CEN_SET;
  201. } else {
  202. /* Disable the I2Cx interface */
  203. I2Cx->CTLR1 &= CTLR1_I2CEN_RESET;
  204. }
  205. }
  206. /**
  207. * @brief Enable or disable the I2Cx DMA requests.
  208. * @param I2Cx: the I2C interface where x can be 1 or 2.
  209. * @param NewValue: new value of the I2C DMA transfer.
  210. * This parameter can be: ENABLE or DISABLE.
  211. * @retval None.
  212. */
  213. void I2C_DMA_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  214. {
  215. if (NewValue != DISABLE) {
  216. /* Enable the I2Cx DMA requests */
  217. I2Cx->CTLR2 |= CTLR2_DMAON_SET;
  218. } else {
  219. /* Disable the I2Cx DMA requests */
  220. I2Cx->CTLR2 &= CTLR2_DMAON_RESET;
  221. }
  222. }
  223. /**
  224. * @brief Enable or disable the DMA last.
  225. * @param I2Cx: the I2C interface where x can be 1 or 2.
  226. * @param NewValue: new value of the I2C DMA last transfer.
  227. * This parameter can be: ENABLE or DISABLE.
  228. * @retval None.
  229. */
  230. void I2C_DMALastTransfer_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  231. {
  232. if (NewValue != DISABLE) {
  233. /* Next DMA transfer is the last transfer */
  234. I2Cx->CTLR2 |= CTLR2_DMALST_SET;
  235. } else {
  236. /* Next DMA transfer is not the last transfer */
  237. I2Cx->CTLR2 &= CTLR2_DMALST_RESET;
  238. }
  239. }
  240. /**
  241. * @brief Enable or disable I2C_CTLR1_GENSTA control bit.
  242. * @param I2Cx: the I2C interface where x can be 1 or 2.
  243. * @param NewValue: new value of the I2C START condition generation.
  244. * This parameter can be: ENABLE or DISABLE.
  245. * @retval None.
  246. */
  247. void I2C_StartOnBus_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  248. {
  249. if (NewValue != DISABLE) {
  250. /* Enable I2C_CTLR1_GENSTA control bit */
  251. I2Cx->CTLR1 |= CTLR1_GENSTA_SET;
  252. } else {
  253. /* Disable I2C_CTLR1_GENSTA control bit */
  254. I2Cx->CTLR1 &= CTLR1_GENSTA_RESET;
  255. }
  256. }
  257. /**
  258. * @brief Enable or disable I2C_CTLR1_GENSTP control bit.
  259. * @param I2Cx: the I2C interface where x can be 1 or 2.
  260. * @param NewValue: new value of the I2C STOP condition generation.
  261. * This parameter can be: ENABLE or DISABLE.
  262. * @retval None.
  263. */
  264. void I2C_StopOnBus_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  265. {
  266. if (NewValue != DISABLE) {
  267. /* Enable I2C_CTLR1_GENSTP control bit */
  268. I2Cx->CTLR1 |= CTLR1_GENSTP_SET;
  269. } else {
  270. /* Disable I2C_CTLR1_GENSTP control bit */
  271. I2Cx->CTLR1 &= CTLR1_GENSTP_RESET;
  272. }
  273. }
  274. /**
  275. * @brief Enable or disable the I2C_CTLR1_ACKEN control bit.
  276. * @param I2Cx: the I2C interface where x can be 1 or 2.
  277. * @param NewValue: new value of the I2C Acknowledgement.
  278. * This parameter can be: ENABLE or DISABLE.
  279. * @retval None.
  280. */
  281. void I2C_Acknowledge_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  282. {
  283. if (NewValue != DISABLE) {
  284. /* Enable I2C_CTLR1_ACKEN control bit */
  285. I2Cx->CTLR1 |= CTLR1_ACKEN_SET;
  286. } else {
  287. /* Disable I2C_CTLR1_ACKEN control bit */
  288. I2Cx->CTLR1 &= CTLR1_ACKEN_RESET;
  289. }
  290. }
  291. /**
  292. * @brief Set the I2Cx own address2.
  293. * @param I2Cx: the I2C interface where x can be 1 or 2.
  294. * @param Address: specifies the 7bit I2C own address2.
  295. * @retval None.
  296. */
  297. void I2C_OwnAddress2(I2C_TypeDef *I2Cx, uint8_t Address)
  298. {
  299. uint16_t temp = 0;
  300. temp = I2Cx->AR2;
  301. temp &= AR2_ADDRESS2_RESET;
  302. temp |= (uint16_t)((uint16_t)Address & (uint16_t)0x00FE);
  303. /* I2C_AR2_ADDRESS2 */
  304. I2Cx->AR2 = temp;
  305. }
  306. /**
  307. * @brief Enable or disable the I2C_AR2_DUADEN control bit.
  308. * @param I2Cx: the I2C interface where x can be 1 or 2.
  309. * @param NewValue: new value of the I2C dual addressing mode.
  310. * This parameter can be: ENABLE or DISABLE.
  311. * @retval None.
  312. */
  313. void I2C_DualAddress_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  314. {
  315. if (NewValue != DISABLE) {
  316. /* Enable the I2C_AR2_DUADEN control bit */
  317. I2Cx->AR2 |= AR2_DUADEN_SET;
  318. } else {
  319. /* Disable the I2C_AR2_DUADEN control bit */
  320. I2Cx->AR2 &= AR2_DUADEN_RESET;
  321. }
  322. }
  323. /**
  324. * @brief Enable or disable I2C_CTLR1_GCEN control bit.
  325. * @param I2Cx: the I2C interface where x can be 1 or 2.
  326. * @param NewValue: new value of the I2C General call.
  327. * This parameter can be: ENABLE or DISABLE.
  328. * @retval None.
  329. */
  330. void I2C_GeneralCall_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  331. {
  332. if (NewValue != DISABLE) {
  333. /* Enable I2C_CTLR1_GCEN control bit */
  334. I2Cx->CTLR1 |= CTLR1_GCEN_SET;
  335. } else {
  336. /* Disable I2C_CTLR1_GCEN control bit */
  337. I2Cx->CTLR1 &= CTLR1_GCEN_RESET;
  338. }
  339. }
  340. /**
  341. * @brief Enable or disable the specified I2C interrupt.
  342. * @param I2Cx: the I2C interface where x can be 1 or 2.
  343. * @param I2C_INT: the I2C interrupt sources.
  344. * This parameter can be any combination of the following values:
  345. * @arg I2C_INT_EIE: Error interrupt
  346. * @arg I2C_INT_EE: Event interrupt
  347. * @arg I2C_INT_BIE: Buffer interrupt
  348. * @param NewValue: new value of the specified I2C interrupts.
  349. * This parameter can be: ENABLE or DISABLE.
  350. * @retval None.
  351. */
  352. void I2C_INTConfig(I2C_TypeDef *I2Cx, uint16_t I2C_INT, TypeState NewValue)
  353. {
  354. if (NewValue != DISABLE) {
  355. /* Enable the I2C interrupt */
  356. I2Cx->CTLR2 |= I2C_INT;
  357. } else {
  358. /* Disable the I2C interrupt */
  359. I2Cx->CTLR2 &= (uint16_t)~I2C_INT;
  360. }
  361. }
  362. /**
  363. * @brief Send one byte to bus.
  364. * @param I2Cx: the I2C interface where x can be 1 or 2.
  365. * @param Data: Byte to be transmitted.
  366. * @retval None.
  367. */
  368. void I2C_SendData(I2C_TypeDef *I2Cx, uint8_t Data)
  369. {
  370. /* Write Data to DTR */
  371. I2Cx->DTR = Data;
  372. }
  373. /**
  374. * @brief Receive one byte from the bus.
  375. * @param I2Cx: the I2C interface where x can be 1 or 2.
  376. * @retval The received data.
  377. */
  378. uint8_t I2C_ReceiveData(I2C_TypeDef *I2Cx)
  379. {
  380. /* Read the DTR register */
  381. return (uint8_t)I2Cx->DTR;
  382. }
  383. /**
  384. * @brief Addressing a device on the I2C bus.
  385. * @param I2Cx: the I2C interface where x can be 1 or 2.
  386. * @param Address: the slave device's address.
  387. * @param I2C_Direction: Transmitter or a Receiver.
  388. * This parameter can be one of the following values:
  389. * @arg I2C_DIRECTION_TRANSMITTER: Transmitter mode
  390. * @arg I2C_DIRECTION_RECEIVER: Receiver mode
  391. * @retval None.
  392. */
  393. void I2C_AddressingDevice_7bit(I2C_TypeDef *I2Cx, uint8_t Address, uint8_t I2C_Direction)
  394. {
  395. if (I2C_Direction != I2C_DIRECTION_TRANSMITTER) {
  396. /* Receiver mode */
  397. Address |= AR1_ADDRESS0_SET;
  398. } else {
  399. /* Transmitter mode */
  400. Address &= AR1_ADDRESS0_RESET;
  401. }
  402. I2Cx->DTR = Address;
  403. }
  404. /**
  405. * @brief Read the I2Cx register.
  406. * @param I2Cx: the I2C interface where x can be 1 or 2.
  407. * @param I2C_Register: the I2Cx register.
  408. * This parameter can be one of the following values:
  409. * @arg I2C_REGISTER_CTLR1: CTLR1 register.
  410. * @arg I2C_REGISTER_CTLR2: CTLR2 register.
  411. * @arg I2C_REGISTER_AR1: AR1 register.
  412. * @arg I2C_REGISTER_AR2: AR2 register.
  413. * @arg I2C_REGISTER_DTR: DTR register.
  414. * @arg I2C_REGISTER_STR1: STR1 register.
  415. * @arg I2C_REGISTER_STR2: STR2 register.
  416. * @arg I2C_REGISTER_CLKR: CLKR register.
  417. * @arg I2C_REGISTER_RTR: RTR register.
  418. * @retval The value of the read register.
  419. */
  420. uint16_t I2C_ReadRegister(I2C_TypeDef *I2Cx, uint8_t I2C_Register)
  421. {
  422. __IO uint32_t temp = 0;
  423. temp = (uint32_t) I2Cx;
  424. temp += I2C_Register;
  425. /* Return the selected register value */
  426. return (*(__IO uint16_t *) temp);
  427. }
  428. /**
  429. * @brief Enable or disable the I2C_CTLR1_SRESET control bit.
  430. * @param I2Cx: the I2C interface where x can be 1 or 2.
  431. * @param NewValue: new value of the I2C software reset.
  432. * This parameter can be: ENABLE or DISABLE.
  433. * @retval None.
  434. */
  435. void I2C_SoftwareReset_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  436. {
  437. if (NewValue != DISABLE) {
  438. /* Enable I2C_CTLR1_SRESET control bit */
  439. I2Cx->CTLR1 |= CTLR1_SRESET_SET;
  440. } else {
  441. /* Disable I2C_CTLR1_SRESET control bit */
  442. I2Cx->CTLR1 &= CTLR1_SRESET_RESET;
  443. }
  444. }
  445. /**
  446. * @brief Enable or disable the I2C_CTLR1_POAP control bit.
  447. * @param I2Cx: the I2C interface where x can be 1 or 2.
  448. * @param I2C_NACKPosition: the position of NACK.
  449. * This parameter can be one of the following values:
  450. * @arg I2C_NACKPOSITION_NEXT: the next byte is the last received byte
  451. * @arg I2C_NACKPOSITION_CURRENT: the current byte is the last received byte
  452. * @retval None.
  453. */
  454. void I2C_NACKPosition_Enable(I2C_TypeDef *I2Cx, uint16_t I2C_NACKPosition)
  455. {
  456. if (I2C_NACKPosition == I2C_NACKPOSITION_NEXT) {
  457. /* Next byte will be the last received byte */
  458. I2Cx->CTLR1 |= I2C_NACKPOSITION_NEXT;
  459. } else {
  460. /* Current byte is the last received byte */
  461. I2Cx->CTLR1 &= I2C_NACKPOSITION_CURRENT;
  462. }
  463. }
  464. /**
  465. * @brief Enable or disable the I2C_CTLR1_SALT control bit.
  466. * @param I2Cx: the I2C interface where x can be 1 or 2.
  467. * @param NewValue: new value of the I2C software reset.
  468. * This parameter can be: ENABLE or DISABLE.
  469. * @retval None
  470. */
  471. void I2C_SMBusAlertSend_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  472. {
  473. if (NewValue != DISABLE) {
  474. /* Enable I2C_CTLR1_SALT control bit */
  475. I2Cx->CTLR1 |= CTLR1_SALT_SET;
  476. } else {
  477. /* Disable I2C_CTLR1_SALT control bit */
  478. I2Cx->CTLR1 &= CTLR1_SALT_RESET;
  479. }
  480. }
  481. /**
  482. * @brief Enable or disable the I2C_CTLR1_PECTRANS control bit.
  483. * @param I2Cx: the I2C interface where x can be 1 or 2.
  484. * @param NewValue: new value of the I2C PEC transmission.
  485. * This parameter can be: ENABLE or DISABLE.
  486. * @retval None
  487. */
  488. void I2C_PECTransmit_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  489. {
  490. if (NewValue != DISABLE) {
  491. /* Enable I2C_CTLR1_PECTRANS control bit */
  492. I2Cx->CTLR1 |= CTLR1_PECTRANS_SET;
  493. } else {
  494. /* Disable I2C_CTLR1_PECTRANS control bit */
  495. I2Cx->CTLR1 &= CTLR1_PECTRANS_RESET;
  496. }
  497. }
  498. /**
  499. * @brief Enable or disable the I2C_CTLR1_POAP control bit.
  500. * @param I2Cx: the I2C interface where x can be 1 or 2.
  501. * @param I2C_PECPosition: the position of PEC.
  502. * This parameter can be one of the following values:
  503. * @arg I2C_PECPOSITION_NEXT: the next byte is PEC
  504. * @arg I2C_PECPOSITION_CURRENT: the current byte is PEC
  505. * @retval None.
  506. */
  507. void I2C_PECPosition_Enable(I2C_TypeDef *I2Cx, uint16_t I2C_PECPosition)
  508. {
  509. if (I2C_PECPosition == I2C_PECPOSITION_NEXT) {
  510. /* The next byte is PEC */
  511. I2Cx->CTLR1 |= I2C_PECPOSITION_NEXT;
  512. } else {
  513. /* The current byte is PEC */
  514. I2Cx->CTLR1 &= I2C_PECPOSITION_CURRENT;
  515. }
  516. }
  517. /**
  518. * @brief Enable or disable the I2C_CTLR1_PECEN control bit.
  519. * @param I2Cx: the I2C interface where x can be 1 or 2.
  520. * @param NewValue: new value of the I2Cx PEC value calculation.
  521. * This parameter can be: ENABLE or DISABLE.
  522. * @retval None.
  523. */
  524. void I2C_PEC_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  525. {
  526. if (NewValue != DISABLE) {
  527. /* Enable I2C_CTLR1_PECEN control bit */
  528. I2Cx->CTLR1 |= CTLR1_PECEN_SET;
  529. } else {
  530. /* Disable I2C_CTLR1_PECEN control bit */
  531. I2Cx->CTLR1 &= CTLR1_PECEN_RESET;
  532. }
  533. }
  534. /**
  535. * @brief The Packet Error Checking value.
  536. * @param I2Cx: the I2C interface where x can be 1 or 2.
  537. * @retval The PEC value.
  538. */
  539. uint8_t I2C_GetPECValue(I2C_TypeDef *I2Cx)
  540. {
  541. /* I2C_STR2_ECV */
  542. return ((I2Cx->STR2) >> 8);
  543. }
  544. /**
  545. * @brief Enable or disable the I2C_CTLR1_ARPEN control bit.
  546. * @param I2Cx: the I2C interface where x can be 1 or 2.
  547. * @param NewValue: new value of the I2Cx ARP.
  548. * This parameter can be: ENABLE or DISABLE.
  549. * @retval None.
  550. */
  551. void I2C_ARP_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  552. {
  553. if (NewValue != DISABLE) {
  554. /* Enable I2C_CTLR1_ARPEN control bit */
  555. I2Cx->CTLR1 |= CTLR1_ARPEN_SET;
  556. } else {
  557. /* Disable I2C_CTLR1_ARPEN control bit */
  558. I2Cx->CTLR1 &= CTLR1_ARPEN_RESET;
  559. }
  560. }
  561. /**
  562. * @brief Enable or disable the I2C_CTLR1_DISSTRC control bit.
  563. * @param I2Cx: the I2C interface where x can be 1 or 2.
  564. * @param NewValue: new value of the I2Cx Clock stretching.
  565. * This parameter can be: ENABLE or DISABLE.
  566. * @retval None.
  567. */
  568. void I2C_StretchClock_Enable(I2C_TypeDef *I2Cx, TypeState NewValue)
  569. {
  570. if (NewValue == DISABLE) {
  571. /* Enable I2C_CTLR1_DISSTRC control bit */
  572. I2Cx->CTLR1 |= CTLR1_DISSTRC_SET;
  573. } else {
  574. /* Enable I2C_CTLR1_DISSTRC control bit */
  575. I2Cx->CTLR1 &= CTLR1_DISSTRC_RESET;
  576. }
  577. }
  578. /**
  579. * @brief Enable or disable the I2C_CLKR_DTCY control bit.
  580. * @param I2Cx: the I2C interface where x can be 1 or 2.
  581. * @param I2C_DutyCycle: the duty cycle in fast mode.
  582. * This parameter can be one of the following values:
  583. * @arg I2C_DUTYCYCLE_2: I2C fast mode Tlow/Thigh = 2
  584. * @arg I2C_DUTYCYCLE_16_9: I2C fast mode Tlow/Thigh = 16/9
  585. * @retval None.
  586. */
  587. void I2C_FastModeDutyCycle(I2C_TypeDef *I2Cx, uint16_t I2C_DutyCycle)
  588. {
  589. if (I2C_DutyCycle != I2C_DUTYCYCLE_16_9) {
  590. /* DutyCycle=2:1 */
  591. I2Cx->CLKR &= I2C_DUTYCYCLE_2;
  592. } else {
  593. /* DutyCycle=16:9 */
  594. I2Cx->CLKR |= I2C_DUTYCYCLE_16_9;
  595. }
  596. }
  597. /**
  598. * @brief Detect I2Cx State Machine.
  599. * @param I2Cx: the I2C interface where x can be 1 or 2.
  600. * @param I2C_State: I2Cx State Machine.
  601. * This parameter can be one of the following values:
  602. * @arg I2C_PROGRAMMINGMODE_SLAVE_TRANSMITTER_ADDSEND
  603. * @arg I2C_PROGRAMMINGMODE_SLAVE_RECEIVER_ADDSEND
  604. * @arg I2C_PROGRAMMINGMODE_SLAVE_TRANSMITTER_SECONDADDRESS_SELECTED
  605. * @arg I2C_PROGRAMMINGMODE_SLAVE_RECEIVER_SECONDADDRESS_SELECTED
  606. * @arg I2C_PROGRAMMINGMODE_SLAVE_GENERALCALLADDRESS_SELECTED
  607. * @arg I2C_PROGRAMMINGMODE_SLAVE_BYTE_RECEIVED
  608. * @arg (I2C_PROGRAMMINGMODE_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUMODF)
  609. * @arg (I2C_PROGRAMMINGMODE_SLAVE_BYTE_RECEIVED | I2C_FLAG_RXGC)
  610. * @arg I2C_PROGRAMMINGMODE_SLAVE_BYTE_TRANSMITTED
  611. * @arg (I2C_PROGRAMMINGMODE_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUMODF)
  612. * @arg (I2C_PROGRAMMINGMODE_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_RXGC)
  613. * @arg I2C_PROGRAMMINGMODE_SLAVE_ACK_FAILURE
  614. * @arg I2C_PROGRAMMINGMODE_SLAVE_STOP_DETECTED
  615. * @arg I2C_PROGRAMMINGMODE_MASTER_SBSEND
  616. * @arg I2C_PROGRAMMINGMODE_MASTER_TRANSMITTER_ADDSEND
  617. * @arg I2C_PROGRAMMINGMODE_MASTER_RECEIVER_ADDSEND
  618. * @arg I2C_PROGRAMMINGMODE_MASTER_BYTE_RECEIVED
  619. * @arg I2C_PROGRAMMINGMODE_MASTER_BYTE_TRANSMITTING
  620. * @arg I2C_PROGRAMMINGMODE_MASTER_BYTE_TRANSMITTED
  621. * @arg I2C_PROGRAMMINGMODE_MASTER_ADD10SEND
  622. * @retval The detected result(SUCCESS or ERROR).
  623. */
  624. TypeState I2C_StateDetect(I2C_TypeDef *I2Cx, uint32_t I2C_State)
  625. {
  626. uint32_t currentstate = 0;
  627. uint32_t flag1 = 0, flag2 = 0;
  628. TypeState state = ERROR;
  629. /* Read the I2Cx status register */
  630. flag1 = I2Cx->STR1;
  631. flag2 = I2Cx->STR2;
  632. flag2 = flag2 << 16;
  633. /* Get the state value from I2C status register */
  634. currentstate = (flag1 | flag2) & STR_FLAG_MASK;
  635. if ((currentstate & I2C_State) == I2C_State) {
  636. /* I2C_State is detected */
  637. state = SUCCESS;
  638. } else {
  639. /* I2C_State is not detected */
  640. state = ERROR;
  641. }
  642. return state;
  643. }
  644. /**
  645. * @brief Get the I2Cx Current State.
  646. * @param I2Cx: the I2C interface where x can be 1 or 2.
  647. * @retval The current state.
  648. */
  649. uint32_t I2C_GetCurrentState(I2C_TypeDef *I2Cx)
  650. {
  651. uint32_t currentstate = 0;
  652. uint32_t flag1 = 0, flag2 = 0;
  653. /* Read the I2Cx status register */
  654. flag1 = I2Cx->STR1;
  655. flag2 = I2Cx->STR2;
  656. flag2 = flag2 << 16;
  657. /* Get the last event value from I2C status register */
  658. currentstate = (flag1 | flag2) & STR_FLAG_MASK;
  659. return currentstate;
  660. }
  661. /**
  662. * @brief Get the bit flag of STRx registers.
  663. * @param I2Cx: the I2C interface where x can be 1 or 2.
  664. * @param I2C_FLAG: the flag of STRx registers.
  665. * This parameter can be one of the following values:
  666. * @arg I2C_FLAG_DUMODF: Dual flag (Slave mode)
  667. * @arg I2C_FLAG_HSTSMB: SMBus host header (Slave mode)
  668. * @arg I2C_FLAG_DEFSMB: SMBus default header (Slave mode)
  669. * @arg I2C_FLAG_RXGC: General call header flag (Slave mode)
  670. * @arg I2C_FLAG_TRS: Transmitter or Receiver flag
  671. * @arg I2C_FLAG_I2CBSY: Bus busy flag
  672. * @arg I2C_FLAG_MASTER: Master or Slave flag
  673. * @arg I2C_FLAG_SMBALTS: SMBus Alert flag
  674. * @arg I2C_FLAG_SMBTO: Timeout or Tlow error flag
  675. * @arg I2C_FLAG_PECE: PEC error in reception flag
  676. * @arg I2C_FLAG_RXORE: Overrun or Underrun flag (Slave mode)
  677. * @arg I2C_FLAG_AE: Acknowledge failure flag
  678. * @arg I2C_FLAG_LOSTARB: Arbitration lost flag (Master mode)
  679. * @arg I2C_FLAG_BE: Bus error flag
  680. * @arg I2C_FLAG_TBE: Data register empty flag (Transmitter)
  681. * @arg I2C_FLAG_RBNE: Data register not empty (Receiver) flag
  682. * @arg I2C_FLAG_STPSEND: Stop detection flag (Slave mode)
  683. * @arg I2C_FLAG_ADD10SEND: 10-bit header sent flag (Master mode)
  684. * @arg I2C_FLAG_BTC: Byte transfer finished flag
  685. * @arg I2C_FLAG_ADDSEND: Address sent flag (Master mode) or Address matched flag (Slave mode)
  686. * @arg I2C_FLAG_SBSEND: Start bit flag (Master mode)
  687. * @retval The new value of I2C_FLAG (SET or RESET).
  688. */
  689. TypeState I2C_GetBitState(I2C_TypeDef *I2Cx, uint32_t I2C_FLAG)
  690. {
  691. __IO uint32_t i2creg = 0, i2cxbase = 0;
  692. /* Get the I2Cx peripheral base address */
  693. i2cxbase = (uint32_t)I2Cx;
  694. /* Read flag register index */
  695. i2creg = I2C_FLAG >> 28;
  696. /* Get bit[23:0] of the flag */
  697. I2C_FLAG &= STR_FLAG_MASK;
  698. if (i2creg != 0) {
  699. /* Flag in I2Cx STR1 register */
  700. i2cxbase += 0x14;
  701. } else {
  702. /* Flag in I2Cx STR2 Register */
  703. I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);
  704. /* Get the I2Cx STR2 register address */
  705. i2cxbase += 0x18;
  706. }
  707. if (((*(__IO uint32_t *)i2cxbase) & I2C_FLAG) != (uint32_t)RESET) {
  708. /* I2C_FLAG is set */
  709. return SET;
  710. } else {
  711. /* I2C_FLAG is reset */
  712. return RESET;
  713. }
  714. }
  715. /**
  716. * @brief Clear the bit flag of STRx registers.
  717. * @param I2Cx: the I2C interface where x can be 1 or 2.
  718. * @param I2C_FLAG: the flag of STRx registers.
  719. * This parameter can be any combination of the following values:
  720. * @arg I2C_FLAG_SMBALTS: SMBus Alert flag
  721. * @arg I2C_FLAG_SMBTO: Timeout or Tlow error flag
  722. * @arg I2C_FLAG_PECE: PEC error in reception flag
  723. * @arg I2C_FLAG_RXORE: Overrun or Underrun flag (Slave mode)
  724. * @arg I2C_FLAG_AE: Acknowledge failure flag
  725. * @arg I2C_FLAG_LOSTARB: Arbitration lost flag (Master mode)
  726. * @arg I2C_FLAG_BE: Bus error flag
  727. * @retval None.
  728. */
  729. void I2C_ClearBitState(I2C_TypeDef *I2Cx, uint32_t I2C_FLAG)
  730. {
  731. uint32_t flagpos = 0;
  732. /* Get the I2C flag position */
  733. flagpos = I2C_FLAG & STR_FLAG_MASK;
  734. /* Clear the selected I2C flag */
  735. I2Cx->STR1 = (uint16_t)~flagpos;
  736. }
  737. /**
  738. * @brief Get the interrupt bit flag.
  739. * @param I2Cx: the I2C interface where x can be 1 or 2.
  740. * @param I2C_INT: the interrupt bit flag.
  741. * This parameter can be one of the following values:
  742. * @arg I2C_INT_SMBALTS: SMBus Alert flag
  743. * @arg I2C_INT_SMBTO: Timeout or Tlow error flag
  744. * @arg I2C_INT_PECE: PEC error in reception flag
  745. * @arg I2C_INT_RXORE: Overrun or Underrun flag (Slave mode)
  746. * @arg I2C_INT_AE: Acknowledge failure flag
  747. * @arg I2C_INT_LOSTARB: Arbitration lost flag (Master mode)
  748. * @arg I2C_INT_BE: Bus error flag
  749. * @arg I2C_INT_TBE: Data register empty flag (Transmitter)
  750. * @arg I2C_INT_RBNE: Data register not empty (Receiver) flag
  751. * @arg I2C_INT_STPSEND: Stop detection flag (Slave mode)
  752. * @arg I2C_INT_ADD10SEND: 10-bit header sent flag (Master mode)
  753. * @arg I2C_INT_BTC: Byte transfer finished flag
  754. * @arg I2C_INT_ADDSEND: Address sent flag (Master mode) or Address matched flag (Slave mode)
  755. * @arg I2C_INT_SBSEND: Start bit flag (Master mode)
  756. * @retval The new value of I2C_INT (SET or RESET).
  757. */
  758. TypeState I2C_GetIntBitState(I2C_TypeDef *I2Cx, uint32_t I2C_INT)
  759. {
  760. uint32_t enablestatus = 0;
  761. /* Check if the interrupt source is enabled or not */
  762. enablestatus = (uint32_t)(((I2C_INT & INTEN_MASK) >> 16) & (I2Cx->CTLR2)) ;
  763. /* Get bit[23:0] of the flag */
  764. I2C_INT &= STR_FLAG_MASK;
  765. if (((I2Cx->STR1 & I2C_INT) != (uint32_t)RESET) && enablestatus) {
  766. /* I2C_INT is set */
  767. return SET;
  768. } else {
  769. /* I2C_INT is reset */
  770. return RESET;
  771. }
  772. }
  773. /**
  774. * @brief Clear the interrupt bit flag.
  775. * @param I2Cx: the I2C interface where x can be 1 or 2.
  776. * @param I2C_INT: the interrupt bit flag.
  777. * This parameter can be any combination of the following values:
  778. * @arg I2C_INT_SMBALTS: SMBus Alert interrupt
  779. * @arg I2C_INT_SMBTO: Timeout or Tlow error interrupt
  780. * @arg I2C_INT_PECE: PEC error in reception interrupt
  781. * @arg I2C_INT_RXORE: Overrun or Underrun interrupt (Slave mode)
  782. * @arg I2C_INT_AE: Acknowledge failure interrupt
  783. * @arg I2C_INT_LOSTARB: Arbitration lost interrupt (Master mode)
  784. * @arg I2C_INT_BE: Bus error interrupt
  785. * @retval None.
  786. */
  787. void I2C_ClearIntBitState(I2C_TypeDef *I2Cx, uint32_t I2C_INT)
  788. {
  789. uint32_t flagpos = 0;
  790. /* Get the I2C flag position */
  791. flagpos = I2C_INT & STR_FLAG_MASK;
  792. /* Clear the selected I2C flag */
  793. I2Cx->STR1 = (uint16_t)~flagpos;
  794. }
  795. /**
  796. * @}
  797. */
  798. /**
  799. * @}
  800. */
  801. /**
  802. * @}
  803. */