lib_i2c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /**
  2. ******************************************************************************
  3. * @file lib_i2c.c
  4. * @author Application Team
  5. * @version V4.5.0
  6. * @date 2019-05-14
  7. * @brief IIC library.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. ******************************************************************************
  12. */
  13. #include "lib_i2c.h"
  14. //registers default reset values
  15. #define I2C_ADDR_RSTValue 0
  16. #define I2C_CTRL_RSTValue 0
  17. #define I2C_CTRL2_RSTValue 0
  18. /* Private Functions -------------------------------------------------------- */
  19. static uint16_t I2C_CheckState(uint8_t State);
  20. static void I2C_SendStart(void);
  21. static void I2C_SendRestart(void);
  22. static void I2C_SendByte(uint8_t dat);
  23. static void I2C_SendStop(void);
  24. static uint8_t I2C_ReceiveByte(void);
  25. static void I2C_ClearBus(uint32_t remap);
  26. static void I2C_WaitForCrossPage(uint8_t sla);
  27. /**
  28. * @brief Check required state.
  29. * @param State:
  30. Required state.
  31. * @retval 0: state OK
  32. !0: state Error, [15:8]Required status code, [7:0] real status code.
  33. */
  34. static uint16_t I2C_CheckState(uint8_t State)
  35. {
  36. uint16_t ret;
  37. if (I2C_GetStatusCode() != State)
  38. {
  39. ret = (State<<8)|(I2C_GetStatusCode());
  40. return ret;
  41. }
  42. else
  43. {
  44. return 0;
  45. }
  46. }
  47. /**
  48. * @brief Send start signal.
  49. * @param None
  50. * @retval None
  51. */
  52. static void I2C_SendStart(void)
  53. {
  54. I2C_GenerateSTART(ENABLE);
  55. while (I2C_GetINTStatus() == 0);
  56. I2C_GenerateSTART(DISABLE);
  57. }
  58. /**
  59. * @brief Send restart signal.
  60. * @param None
  61. * @retval None
  62. */
  63. static void I2C_SendRestart(void)
  64. {
  65. I2C_GenerateSTART(ENABLE);
  66. I2C_ClearINTStatus();
  67. while (I2C_GetINTStatus() == 0);
  68. I2C_GenerateSTART(DISABLE);
  69. }
  70. /**
  71. * @brief Send stop signal.
  72. * @param None
  73. * @retval None
  74. */
  75. static void I2C_SendStop(void)
  76. {
  77. I2C_GenerateSTOP(ENABLE);
  78. I2C_ClearINTStatus();
  79. I2C_GenerateSTOP(DISABLE);
  80. }
  81. /**
  82. * @brief Send data.
  83. * @param dat:data to send.
  84. * @retval None
  85. */
  86. static void I2C_SendByte(uint8_t dat)
  87. {
  88. I2C_SendData(dat);
  89. I2C_ClearINTStatus();
  90. while (I2C_GetINTStatus() == 0);
  91. }
  92. /**
  93. * @brief Receive byte.
  94. * @param None
  95. * @retval Byte received
  96. */
  97. static uint8_t I2C_ReceiveByte(void)
  98. {
  99. I2C_ClearINTStatus();
  100. while (I2C_GetINTStatus() == 0);
  101. return I2C_ReceiveData();
  102. }
  103. /**
  104. * @brief Wait for cross page operation done.
  105. * @param None
  106. * @retval None
  107. */
  108. static void I2C_WaitForCrossPage(uint8_t sla)
  109. {
  110. do
  111. {
  112. I2C_SendRestart();
  113. I2C_SendByte(sla); //device address
  114. }while (I2C_GetStatusCode() !=0x18);
  115. I2C_SendStop(); //stop
  116. }
  117. static void I2C_ClearBus(uint32_t remap)
  118. {
  119. __IO uint8_t i, j;
  120. if (remap) // I2C remap enable, SCL IOC4
  121. {
  122. GPIOC->DAT &= ~BIT4;
  123. GPIOC->ATT |= BIT4;
  124. GPIOC->OEN &= ~BIT4;
  125. for (i=0; i<9; i++)
  126. {
  127. GPIOC->DAT |= BIT4;
  128. for (j=0; j<100; j++)
  129. __NOP();
  130. GPIOC->DAT &= ~BIT4;
  131. for (j=0; j<100; j++)
  132. __NOP();
  133. }
  134. GPIOC->DAT |= BIT4;
  135. GPIOC->OEN |= BIT4;
  136. GPIOC->IE &= ~BIT4;
  137. }
  138. else // I2C remap disable, SCL IOB13
  139. {
  140. GPIOB->DAT &= ~BIT13;
  141. GPIOB->ATT |= BIT13;
  142. GPIOB->OEN &= ~BIT13;
  143. for (i=0; i<9; i++)
  144. {
  145. GPIOB->DAT |= BIT13;
  146. for (j=0; j<100; j++)
  147. __NOP();
  148. GPIOB->DAT &= ~BIT13;
  149. for (j=0; j<100; j++)
  150. __NOP();
  151. }
  152. GPIOB->DAT |= BIT13;
  153. GPIOB->OEN |= BIT13;
  154. GPIOB->IE &= ~BIT13;
  155. }
  156. }
  157. /* Exported Functions ------------------------------------------------------- */
  158. /**
  159. * @brief Initializes the I2C peripheral registers to their default reset values.
  160. * @param remap: I2C_REMAP_ENABLE or I2C_REMAP_DISABLE
  161. * @retval None
  162. */
  163. void I2C_DeInit(uint32_t remap)
  164. {
  165. I2C->CTRL &= ~I2C_CTRL_EN;
  166. I2C->ADDR = I2C_ADDR_RSTValue;
  167. I2C->CTRL = I2C_CTRL_RSTValue;
  168. I2C->CTRL2 = I2C_CTRL2_RSTValue;
  169. I2C_ClearBus(remap);
  170. }
  171. /**
  172. * @brief Fills each InitStruct member with its default value.
  173. * @param InitStruct: pointer to an I2C_InitType structure which will be initialized.
  174. * @retval None
  175. */
  176. void I2C_StructInit(I2C_InitType *InitStruct)
  177. {
  178. /*--------------- Reset I2C init structure parameters values ---------------*/
  179. /* Initialize the AssertAcknowledge member */
  180. InitStruct->AssertAcknowledge = I2C_ASSERTACKNOWLEDGE_DISABLE;
  181. /* Initialize the ClockSource member */
  182. InitStruct->ClockSource = I2C_CLOCKSOURCE_APBD256;
  183. /* Initialize the GeneralCallAck member */
  184. InitStruct->GeneralCallAck = I2C_GENERALCALLACK_DISABLE;
  185. /* Initialize the SlaveAddr member */
  186. InitStruct->SlaveAddr = 0;
  187. }
  188. /**
  189. * @brief I2C initialization.
  190. * @param InitStruct: I2C configuration.
  191. SlaveAddr: Own I2C slave address (7 bit)
  192. GeneralCallAck:
  193. I2C_GENERALCALLACK_ENABLE
  194. I2C_GENERALCALLACK_DISABLE
  195. AssertAcknowledge:
  196. I2C_ASSERTACKNOWLEDGE_ENABLE
  197. I2C_ASSERTACKNOWLEDGE_DISABLE
  198. ClockSource:
  199. I2C_CLOCKSOURCE_APBD256
  200. I2C_CLOCKSOURCE_APBD224
  201. I2C_CLOCKSOURCE_APBD192
  202. I2C_CLOCKSOURCE_APBD160
  203. I2C_CLOCKSOURCE_APBD960
  204. I2C_CLOCKSOURCE_APBD120
  205. I2C_CLOCKSOURCE_APBD60
  206. I2C_CLOCKSOURCE_TIM3OFD8
  207. * @retval None.
  208. */
  209. void I2C_Init(I2C_InitType *InitStruct)
  210. {
  211. uint32_t tmp;
  212. /* Check parameters */
  213. assert_parameters(IS_I2C_GC(InitStruct->GeneralCallAck));
  214. assert_parameters(IS_I2C_AA(InitStruct->AssertAcknowledge));
  215. assert_parameters(IS_I2C_CLKSRC(InitStruct->ClockSource));
  216. I2C->ADDR = InitStruct->SlaveAddr\
  217. |InitStruct->GeneralCallAck;
  218. tmp = I2C->CTRL;
  219. tmp &= ~(I2C_CTRL_CR\
  220. |I2C_CTRL_AA);
  221. tmp |= (InitStruct->ClockSource\
  222. |InitStruct->AssertAcknowledge);
  223. I2C->CTRL = tmp;
  224. }
  225. /**
  226. * @brief Interrupt configure.
  227. * @param NewState:
  228. ENABLE
  229. DISABLE
  230. * @retval None.
  231. */
  232. void I2C_INTConfig(uint32_t NewState)
  233. {
  234. /* Check parameters */
  235. assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  236. if (NewState == ENABLE)
  237. I2C->CTRL2 |= I2C_CTRL2_INTEN;
  238. else
  239. I2C->CTRL2 &= ~I2C_CTRL2_INTEN;
  240. }
  241. /**
  242. * @brief Get interrupt status.
  243. * @param None
  244. * @retval Interrupt status.
  245. */
  246. uint8_t I2C_GetINTStatus(void)
  247. {
  248. if (I2C->CTRL&I2C_CTRL_SI)
  249. return 1;
  250. else
  251. return 0;
  252. }
  253. /**
  254. * @brief Clear interrupt status.
  255. * @param None
  256. * @retval None.
  257. */
  258. void I2C_ClearINTStatus(void)
  259. {
  260. I2C->CTRL &= ~I2C_CTRL_SI;
  261. }
  262. /**
  263. * @brief Read a packge of data from slave device.
  264. * @param InitStruct: I2C_WRType
  265. SlaveAddr : Slave device address
  266. SubAddress : start of slave device sub-address
  267. PageRange : maximum range of page to Read operation
  268. pBuffer : Read data pointer
  269. Length : sum of Read datas
  270. SubAddrType:
  271. I2C_SUBADDR_1BYTE (Slave device sub-address type: 1 byte)
  272. I2C_SUBADDR_2BYTE (Slave device sub-address type: 2 bytes)
  273. I2C_SUBADDR_OTHER (Slave device sub-address type: othres)
  274. * @retval 0: true
  275. £¡0£ºstatus code
  276. bit15~8 status code(true)
  277. bit7~0 status code(false)
  278. */
  279. uint16_t I2C_MasterReadBytes(I2C_WRType *InitStruct)
  280. {
  281. uint32_t i;
  282. uint16_t ret_val;
  283. /* Check parameters */
  284. assert_parameters(I2C_SUBADDR_TYPE(InitStruct->SubAddrType));
  285. I2C_AssertAcknowledgeConfig(ENABLE); //Enable AA
  286. /*-------------------------------- START -----------------------------------*/
  287. I2C_SendStart();
  288. ret_val = I2C_CheckState(0x08);
  289. if (ret_val) return ret_val;
  290. /*------------------------------ Send SLA+W --------------------------------*/
  291. /* Slave device sub-address type: 1 byte */
  292. if (InitStruct->SubAddrType == I2C_SUBADDR_1BYTE)
  293. {
  294. I2C_SendByte(InitStruct->SlaveAddr);
  295. ret_val = I2C_CheckState(0x18);
  296. if (ret_val) return ret_val;
  297. I2C_SendByte(InitStruct->SubAddress&0xFF);
  298. ret_val = I2C_CheckState(0x28);
  299. if (ret_val) return ret_val;
  300. }
  301. /* Slave device sub-address type: 2 bytes */
  302. if (InitStruct->SubAddrType == I2C_SUBADDR_2BYTE)
  303. {
  304. I2C_SendByte(InitStruct->SlaveAddr);
  305. ret_val = I2C_CheckState(0x18);
  306. if (ret_val) return ret_val;
  307. I2C_SendByte((InitStruct->SubAddress>>8)&0xFF);
  308. ret_val = I2C_CheckState(0x28);
  309. if (ret_val) return ret_val;
  310. I2C_SendByte(InitStruct->SubAddress&0xFF);
  311. ret_val = I2C_CheckState(0x28);
  312. if (ret_val) return ret_val;
  313. }
  314. /* Slave device sub-address type: othres */
  315. if (InitStruct->SubAddrType == I2C_SUBADDR_OTHER)
  316. {
  317. if (InitStruct->PageRange < 256) // 8 + x
  318. {
  319. I2C_SendByte(InitStruct->SlaveAddr|((InitStruct->SubAddress>>7)&0xE));
  320. ret_val = I2C_CheckState(0x18);
  321. if (ret_val) return ret_val;
  322. I2C_SendByte(InitStruct->SubAddress&0xFF);
  323. ret_val = I2C_CheckState(0x28);
  324. if (ret_val) return ret_val;
  325. }
  326. else // 16 + x
  327. {
  328. I2C_SendByte(InitStruct->SlaveAddr|((InitStruct->SubAddress>>15)&0xE));
  329. ret_val = I2C_CheckState(0x18);
  330. if (ret_val) return ret_val;
  331. I2C_SendByte((InitStruct->SubAddress>>8)&0xFF);
  332. ret_val = I2C_CheckState(0x28);
  333. if (ret_val) return ret_val;
  334. I2C_SendByte(InitStruct->SubAddress&0xFF);
  335. ret_val = I2C_CheckState(0x28);
  336. if (ret_val) return ret_val;
  337. }
  338. }
  339. /*------------------------------- Restart ----------------------------------*/
  340. I2C_SendRestart(); //restart
  341. ret_val = I2C_CheckState(0x10);
  342. if (ret_val) return ret_val;
  343. /*----------------------------- Send SLA+R ---------------------------------*/
  344. /* Slave device sub-address type: othres */
  345. if (InitStruct->SubAddrType == I2C_SUBADDR_OTHER)
  346. {
  347. if (InitStruct->PageRange < 256) // 8 + x
  348. I2C_SendByte(InitStruct->SlaveAddr|0x01|((InitStruct->SubAddress>>7)&0xE));
  349. else // 16 + x
  350. I2C_SendByte(InitStruct->SlaveAddr|0x01|((InitStruct->SubAddress>>15)&0xE));
  351. }
  352. else
  353. I2C_SendByte(InitStruct->SlaveAddr|0x01);
  354. ret_val = I2C_CheckState(0x40);
  355. if (ret_val) return ret_val;
  356. /*----------------------------- Read datas ---------------------------------*/
  357. for (i=0; i<(InitStruct->Length-1); i++)
  358. {
  359. *InitStruct->pBuffer = I2C_ReceiveByte();
  360. InitStruct->pBuffer++;
  361. ret_val = I2C_CheckState(0x50);
  362. if (ret_val) return ret_val;
  363. }
  364. /*-------------------- Read the last data, disable AA ----------------------*/
  365. I2C_AssertAcknowledgeConfig(DISABLE);
  366. *InitStruct->pBuffer = I2C_ReceiveByte();
  367. ret_val = I2C_CheckState(0x58);
  368. if (ret_val) return ret_val;
  369. /*--------------------------------- Stop -----------------------------------*/
  370. I2C_SendStop(); //stop
  371. return 0;
  372. }
  373. /**
  374. * @brief Write a packge of data to slave device.
  375. * @param InitStruct: I2C_WRType
  376. SlaveAddr : Slave device address
  377. SubAddress : start of slave device sub-address
  378. PageRange : maximum range of page to write operation
  379. pBuffer : write data pointer
  380. Length : sum of write datas
  381. SubAddrType:
  382. I2C_SUBADDR_1BYTE (Slave device sub-address type: 1 byte)
  383. I2C_SUBADDR_2BYTE (Slave device sub-address type: 2 bytes)
  384. I2C_SUBADDR_OTHER (Slave device sub-address type: othres)
  385. * @retval 0: true
  386. £¡0£ºstatus code
  387. bit15~8 status code(true)
  388. bit7~0 status code(false)
  389. */
  390. uint16_t I2C_MasterWriteBytes(I2C_WRType *InitStruct)
  391. {
  392. uint16_t ret_val;
  393. uint32_t i;
  394. /* Check parameters */
  395. assert_parameters(I2C_SUBADDR_TYPE(InitStruct->SubAddrType));
  396. I2C_AssertAcknowledgeConfig(ENABLE); //Enable AA
  397. /*-------------------------------- START -----------------------------------*/
  398. I2C_SendStart();
  399. ret_val = I2C_CheckState(0x08);
  400. if (ret_val) return ret_val;
  401. /*------------------------------ Send SLA+W --------------------------------*/
  402. /* Slave device sub-address type: 1 byte */
  403. if (InitStruct->SubAddrType == I2C_SUBADDR_1BYTE)
  404. {
  405. I2C_SendByte(InitStruct->SlaveAddr);
  406. ret_val = I2C_CheckState(0x18);
  407. if (ret_val) return ret_val;
  408. I2C_SendByte(InitStruct->SubAddress&0xFF);
  409. ret_val = I2C_CheckState(0x28);
  410. if (ret_val) return ret_val;
  411. }
  412. /* Slave device sub-address type: 2 bytes */
  413. else if (InitStruct->SubAddrType == I2C_SUBADDR_2BYTE)
  414. {
  415. I2C_SendByte(InitStruct->SlaveAddr); //device address
  416. ret_val = I2C_CheckState(0x18);
  417. if (ret_val) return ret_val;
  418. I2C_SendByte((InitStruct->SubAddress>>8)&0xFF); //first word address
  419. ret_val = I2C_CheckState(0x28);
  420. if (ret_val) return ret_val;
  421. I2C_SendByte(InitStruct->SubAddress&0xFF); //second word address
  422. ret_val = I2C_CheckState(0x28);
  423. if (ret_val) return ret_val;
  424. }
  425. /* Slave device sub-address type: othres */
  426. else
  427. {
  428. if (InitStruct->PageRange < 256) // 8 + x
  429. {
  430. I2C_SendByte(InitStruct->SlaveAddr|((InitStruct->SubAddress>>7)&0xE));
  431. ret_val = I2C_CheckState(0x18);
  432. if (ret_val) return ret_val;
  433. I2C_SendByte(InitStruct->SubAddress&0xFF);
  434. ret_val = I2C_CheckState(0x28);
  435. if (ret_val) return ret_val;
  436. }
  437. else // 16 + x
  438. {
  439. I2C_SendByte(InitStruct->SlaveAddr|((InitStruct->SubAddress>>15)&0xE));
  440. ret_val = I2C_CheckState(0x18);
  441. if (ret_val) return ret_val;
  442. I2C_SendByte((InitStruct->SubAddress>>8)&0xFF);
  443. ret_val = I2C_CheckState(0x28);
  444. if (ret_val) return ret_val;
  445. I2C_SendByte(InitStruct->SubAddress&0xFF);
  446. ret_val = I2C_CheckState(0x28);
  447. if (ret_val) return ret_val;
  448. }
  449. }
  450. /*----------------------------- Write datas --------------------------------*/
  451. for (i=0; i<(InitStruct->Length); i++)
  452. {
  453. /* Reach the page boundary */
  454. if ((i > 0) && ((InitStruct->SubAddress+i)%InitStruct->PageRange == 0))
  455. {
  456. I2C_SendStop();
  457. I2C_WaitForCrossPage(InitStruct->SlaveAddr);
  458. I2C_SendStart(); //start
  459. ret_val = I2C_CheckState(0x08);
  460. if (ret_val) return ret_val;
  461. /* WriteAddr: 1 byte */
  462. if (InitStruct->SubAddrType == I2C_SUBADDR_1BYTE)
  463. {
  464. I2C_SendByte(InitStruct->SlaveAddr);
  465. ret_val = I2C_CheckState(0x18);
  466. if (ret_val) return ret_val;
  467. I2C_SendByte((InitStruct->SubAddress+i)&0xFF);
  468. ret_val = I2C_CheckState(0x28);
  469. if (ret_val) return ret_val;
  470. }
  471. /* WriteAddr: 2 byte */
  472. if (InitStruct->SubAddrType == I2C_SUBADDR_2BYTE)
  473. {
  474. I2C_SendByte(InitStruct->SlaveAddr); //device address
  475. ret_val = I2C_CheckState(0x18);
  476. if (ret_val) return ret_val;
  477. I2C_SendByte(((InitStruct->SubAddress+i)>>8)&0xFF); //first word address
  478. ret_val = I2C_CheckState(0x28);
  479. if (ret_val) return ret_val;
  480. I2C_SendByte((InitStruct->SubAddress+i)&0xFF); //second word address
  481. ret_val = I2C_CheckState(0x28);
  482. if (ret_val) return ret_val;
  483. }
  484. /* WriteAddr: (16 or 8)+x*/
  485. if (InitStruct->SubAddrType == I2C_SUBADDR_OTHER)
  486. {
  487. if (InitStruct->PageRange < 256) // 8 + x
  488. {
  489. I2C_SendByte(InitStruct->SlaveAddr|(((InitStruct->SubAddress+i)>>7)&0xE));
  490. ret_val = I2C_CheckState(0x18);
  491. if (ret_val) return ret_val;
  492. I2C_SendByte((InitStruct->SubAddress+i)&0xFF);
  493. ret_val = I2C_CheckState(0x28);
  494. if (ret_val) return ret_val;
  495. }
  496. else // 16 + x
  497. {
  498. I2C_SendByte(InitStruct->SlaveAddr|(((InitStruct->SubAddress+i)>>15)&0xE));
  499. ret_val = I2C_CheckState(0x18);
  500. if (ret_val) return ret_val;
  501. I2C_SendByte(((InitStruct->SubAddress+i)>>8)&0xFF);
  502. ret_val = I2C_CheckState(0x28);
  503. if (ret_val) return ret_val;
  504. I2C_SendByte((InitStruct->SubAddress+i)&0xFF);
  505. ret_val = I2C_CheckState(0x28);
  506. if (ret_val) return ret_val;
  507. }
  508. }
  509. I2C_SendByte(*InitStruct->pBuffer);
  510. InitStruct->pBuffer++;
  511. ret_val = I2C_CheckState(0x28);
  512. if (ret_val) return ret_val;
  513. }
  514. /* Not reaching the page boundary */
  515. else
  516. {
  517. I2C_SendByte(*InitStruct->pBuffer);
  518. InitStruct->pBuffer++;
  519. ret_val = I2C_CheckState(0x28);
  520. if (ret_val) return ret_val;
  521. }
  522. }
  523. I2C_SendStop();
  524. I2C_WaitForCrossPage(InitStruct->SlaveAddr);
  525. return 0;
  526. }
  527. /**
  528. * @brief I2C enable.
  529. * @param NewState:
  530. ENABLE
  531. DISABLE
  532. * @retval None.
  533. */
  534. void I2C_Cmd(uint32_t NewState)
  535. {
  536. /* Check parameters */
  537. assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  538. if (NewState == ENABLE)
  539. I2C->CTRL |= I2C_CTRL_EN;
  540. else
  541. I2C->CTRL &= ~I2C_CTRL_EN;
  542. }
  543. /* I2C Exported Functions Group5:
  544. Others ------------------------------------*/
  545. /**
  546. * @brief Assert acknowledge configure.
  547. * @param NewState:
  548. ENABLE
  549. DISABLE
  550. * @retval None.
  551. */
  552. void I2C_AssertAcknowledgeConfig(uint32_t NewState)
  553. {
  554. /* Check parameters */
  555. assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  556. if (NewState == ENABLE)
  557. I2C->CTRL |= I2C_CTRL_AA;
  558. else
  559. I2C->CTRL &= ~I2C_CTRL_AA;
  560. }
  561. /**
  562. * @brief Receive a byte data.
  563. * @param None.
  564. * @retval Data received.
  565. */
  566. uint8_t I2C_ReceiveData(void)
  567. {
  568. return I2C->DATA;
  569. }
  570. /**
  571. * @brief Sends a byte data.
  572. * @param Dat:data to transmit.
  573. * @retval None
  574. */
  575. void I2C_SendData(uint8_t Dat)
  576. {
  577. I2C->DATA = Dat;
  578. }
  579. /**
  580. * @brief Generate start signal.
  581. * @param NewState:
  582. ENABLE
  583. DISABLE
  584. * @retval None.
  585. */
  586. void I2C_GenerateSTART(uint32_t NewState)
  587. {
  588. /* Check parameters */
  589. assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  590. if (NewState == ENABLE)
  591. I2C->CTRL |= I2C_CTRL_STA;
  592. else
  593. I2C->CTRL &= ~I2C_CTRL_STA;
  594. }
  595. /**
  596. * @brief Generate stop signal.
  597. * @param NewState:
  598. ENABLE
  599. DISABLE
  600. * @retval None.
  601. */
  602. void I2C_GenerateSTOP(uint32_t NewState)
  603. {
  604. /* Check parameters */
  605. assert_parameters(IS_FUNCTIONAL_STATE(NewState));
  606. if (NewState == ENABLE)
  607. I2C->CTRL |= I2C_CTRL_STO;
  608. else
  609. I2C->CTRL &= ~I2C_CTRL_STO;
  610. }
  611. /**
  612. * @brief Get status code.
  613. * @param None
  614. * @retval status code.
  615. */
  616. uint8_t I2C_GetStatusCode(void)
  617. {
  618. return (I2C->STS&I2C_STS_STS);
  619. }
  620. /*********************************** END OF FILE ******************************/