lib_i2c.c 19 KB

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