at24cxx.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*!
  2. \file at24cxx.c
  3. \brief the read and write function file
  4. \version 2022-05-30, V1.0.0, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2022, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "at24cxx.h"
  30. #include "i2c.h"
  31. #include <stdio.h>
  32. #define EEPROM_BLOCK0_ADDRESS 0xA0
  33. #define BUFFER_SIZE 256
  34. uint16_t eeprom_address;
  35. /*!
  36. \brief I2C read and write functions
  37. \param[in] none
  38. \param[out] none
  39. \retval I2C_OK or I2C_FAIL
  40. */
  41. uint8_t i2c_24c02_test(void)
  42. {
  43. uint16_t i;
  44. uint8_t i2c_buffer_write[BUFFER_SIZE];
  45. uint8_t i2c_buffer_read[BUFFER_SIZE];
  46. printf("\r\nAT24C02 writing...\r\n");
  47. /* initialize i2c_buffer_write */
  48. for(i = 0; i < BUFFER_SIZE; i++) {
  49. i2c_buffer_write[i] = i;
  50. printf("0x%02X ", i2c_buffer_write[i]);
  51. if(15 == i % 16) {
  52. printf("\r\n");
  53. }
  54. }
  55. /* EEPROM data write */
  56. eeprom_buffer_write_timeout(i2c_buffer_write, EEP_FIRST_PAGE, BUFFER_SIZE);
  57. printf("AT24C02 reading...\r\n");
  58. /* EEPROM data read */
  59. eeprom_buffer_read_timeout(i2c_buffer_read, EEP_FIRST_PAGE, BUFFER_SIZE);
  60. /* compare the read buffer and write buffer */
  61. for(i = 0; i < BUFFER_SIZE; i++) {
  62. if(i2c_buffer_read[i] != i2c_buffer_write[i]) {
  63. printf("0x%02X ", i2c_buffer_read[i]);
  64. printf("Err:data read and write aren't matching.\n\r");
  65. return I2C_FAIL;
  66. }
  67. printf("0x%02X ", i2c_buffer_read[i]);
  68. if(15 == i % 16) {
  69. printf("\r\n");
  70. }
  71. }
  72. printf("I2C-AT24C02 test passed!\n\r");
  73. return I2C_OK;
  74. }
  75. /*!
  76. \brief initialize peripherals used by the I2C EEPROM driver
  77. \param[in] none
  78. \param[out] none
  79. \retval none
  80. */
  81. void i2c_eeprom_init(void)
  82. {
  83. eeprom_address = EEPROM_BLOCK0_ADDRESS;
  84. }
  85. /*!
  86. \brief write one byte to the EEPROM and use timeout function
  87. \param[in] p_buffer: pointer to the buffer containing the data to be written to the EEPROM
  88. \param[in] write_address: EEPROM's internal address to write to
  89. \param[out] none
  90. \retval none
  91. */
  92. uint8_t eeprom_byte_write_timeout(uint8_t *p_buffer, uint8_t write_address)
  93. {
  94. uint8_t state = I2C_START;
  95. uint16_t timeout = 0;
  96. uint8_t i2c_timeout_flag = 0;
  97. /* enable acknowledge */
  98. i2c_ack_config(I2CX, I2C_ACK_ENABLE);
  99. while(!(i2c_timeout_flag)) {
  100. switch(state) {
  101. case I2C_START:
  102. /* i2c master sends start signal only when the bus is idle */
  103. while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
  104. timeout++;
  105. }
  106. if(timeout < I2C_TIME_OUT) {
  107. i2c_start_on_bus(I2CX);
  108. timeout = 0;
  109. state = I2C_SEND_ADDRESS;
  110. } else {
  111. timeout = 0;
  112. state = I2C_START;
  113. printf("i2c bus is busy in WRITE BYTE!\n");
  114. }
  115. break;
  116. case I2C_SEND_ADDRESS:
  117. /* i2c master sends START signal successfully */
  118. while((! i2c_flag_get(I2CX, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
  119. timeout++;
  120. }
  121. if(timeout < I2C_TIME_OUT) {
  122. i2c_master_addressing(I2CX, eeprom_address, I2C_TRANSMITTER);
  123. timeout = 0;
  124. state = I2C_CLEAR_ADDRESS_FLAG;
  125. } else {
  126. timeout = 0;
  127. state = I2C_START;
  128. printf("i2c master sends start signal timeout in WRITE BYTE!\n");
  129. }
  130. break;
  131. case I2C_CLEAR_ADDRESS_FLAG:
  132. /* address flag set means i2c slave sends ACK */
  133. while((! i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
  134. timeout++;
  135. }
  136. if(timeout < I2C_TIME_OUT) {
  137. i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
  138. timeout = 0;
  139. state = I2C_TRANSMIT_DATA;
  140. } else {
  141. timeout = 0;
  142. state = I2C_START;
  143. printf("i2c master clears address flag timeout in WRITE BYTE!\n");
  144. }
  145. break;
  146. case I2C_TRANSMIT_DATA:
  147. /* wait until the transmit data buffer is empty */
  148. while((! i2c_flag_get(I2CX, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
  149. timeout++;
  150. }
  151. if(timeout < I2C_TIME_OUT) {
  152. /* send the EEPROM's internal address to write to : only one byte address */
  153. i2c_data_transmit(I2CX, write_address);
  154. timeout = 0;
  155. } else {
  156. timeout = 0;
  157. state = I2C_START;
  158. printf("i2c master sends data timeout in WRITE BYTE!\n");
  159. }
  160. /* wait until BTC bit is set */
  161. while((!i2c_flag_get(I2CX, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
  162. timeout++;
  163. }
  164. if(timeout < I2C_TIME_OUT) {
  165. /* send the EEPROM's internal address to write to : only one byte address */
  166. i2c_data_transmit(I2CX, *p_buffer);
  167. timeout = 0;
  168. } else {
  169. timeout = 0;
  170. state = I2C_START;
  171. printf("i2c master sends data timeout in WRITE BYTE!\n");
  172. }
  173. /* wait until BTC bit is set */
  174. while((!i2c_flag_get(I2CX, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
  175. timeout++;
  176. }
  177. if(timeout < I2C_TIME_OUT) {
  178. state = I2C_STOP;
  179. timeout = 0;
  180. } else {
  181. timeout = 0;
  182. state = I2C_START;
  183. printf("i2c master sends data timeout in WRITE BYTE!\n");
  184. }
  185. break;
  186. case I2C_STOP:
  187. /* send a stop condition to I2C bus */
  188. i2c_stop_on_bus(I2CX);
  189. /* i2c master sends STOP signal successfully */
  190. while((I2C_CTL0(I2CX) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
  191. timeout++;
  192. }
  193. if(timeout < I2C_TIME_OUT) {
  194. timeout = 0;
  195. state = I2C_END;
  196. i2c_timeout_flag = I2C_OK;
  197. } else {
  198. timeout = 0;
  199. state = I2C_START;
  200. printf("i2c master sends stop signal timeout in WRITE BYTE!\n");
  201. }
  202. break;
  203. default:
  204. state = I2C_START;
  205. i2c_timeout_flag = I2C_OK;
  206. timeout = 0;
  207. printf("i2c master sends start signal in WRITE BYTE.\n");
  208. break;
  209. }
  210. }
  211. return I2C_END;
  212. }
  213. /*!
  214. \brief write more than one byte to the EEPROM with a single write cycle
  215. \param[in] p_buffer: pointer to the buffer containing the data to be written to the EEPROM
  216. \param[in] write_address: EEPROM's internal address to write to
  217. \param[in] number_of_byte: number of bytes to write to the EEPROM
  218. \param[out] none
  219. \retval none
  220. */
  221. uint8_t eeprom_page_write_timeout(uint8_t *p_buffer, uint8_t write_address, uint8_t number_of_byte)
  222. {
  223. uint8_t state = I2C_START;
  224. uint16_t timeout = 0;
  225. uint8_t i2c_timeout_flag = 0;
  226. /* enable acknowledge */
  227. i2c_ack_config(I2CX, I2C_ACK_ENABLE);
  228. while(!(i2c_timeout_flag)) {
  229. switch(state) {
  230. case I2C_START:
  231. /* i2c master sends start signal only when the bus is idle */
  232. while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
  233. timeout++;
  234. }
  235. if(timeout < I2C_TIME_OUT) {
  236. i2c_start_on_bus(I2CX);
  237. timeout = 0;
  238. state = I2C_SEND_ADDRESS;
  239. } else {
  240. i2c_bus_reset();
  241. timeout = 0;
  242. state = I2C_START;
  243. printf("i2c bus is busy in WRITE!\n");
  244. }
  245. break;
  246. case I2C_SEND_ADDRESS:
  247. /* i2c master sends START signal successfully */
  248. while((! i2c_flag_get(I2CX, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
  249. timeout++;
  250. }
  251. if(timeout < I2C_TIME_OUT) {
  252. i2c_master_addressing(I2CX, eeprom_address, I2C_TRANSMITTER);
  253. timeout = 0;
  254. state = I2C_CLEAR_ADDRESS_FLAG;
  255. } else {
  256. timeout = 0;
  257. state = I2C_START;
  258. printf("i2c master sends start signal timeout in WRITE!\n");
  259. }
  260. break;
  261. case I2C_CLEAR_ADDRESS_FLAG:
  262. /* address flag set means i2c slave sends ACK */
  263. while((! i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
  264. timeout++;
  265. }
  266. if(timeout < I2C_TIME_OUT) {
  267. i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
  268. timeout = 0;
  269. state = I2C_TRANSMIT_DATA;
  270. } else {
  271. timeout = 0;
  272. state = I2C_START;
  273. printf("i2c master clears address flag timeout in WRITE!\n");
  274. }
  275. break;
  276. case I2C_TRANSMIT_DATA:
  277. /* wait until the transmit data buffer is empty */
  278. while((! i2c_flag_get(I2CX, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
  279. timeout++;
  280. }
  281. if(timeout < I2C_TIME_OUT) {
  282. /* send the EEPROM's internal address to write to : only one byte address */
  283. i2c_data_transmit(I2CX, write_address);
  284. timeout = 0;
  285. } else {
  286. timeout = 0;
  287. state = I2C_START;
  288. printf("i2c master sends EEPROM's internal address timeout in WRITE!\n");
  289. }
  290. /* wait until BTC bit is set */
  291. while((!i2c_flag_get(I2CX, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
  292. timeout++;
  293. }
  294. if(timeout < I2C_TIME_OUT) {
  295. timeout = 0;
  296. } else {
  297. timeout = 0;
  298. state = I2C_START;
  299. printf("i2c master sends data timeout in WRITE!\n");
  300. }
  301. while(number_of_byte--) {
  302. i2c_data_transmit(I2CX, *p_buffer);
  303. /* point to the next byte to be written */
  304. p_buffer++;
  305. /* wait until BTC bit is set */
  306. while((!i2c_flag_get(I2CX, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
  307. timeout++;
  308. }
  309. if(timeout < I2C_TIME_OUT) {
  310. timeout = 0;
  311. } else {
  312. timeout = 0;
  313. state = I2C_START;
  314. printf("i2c master sends data timeout in WRITE!\n");
  315. }
  316. }
  317. timeout = 0;
  318. state = I2C_STOP;
  319. break;
  320. case I2C_STOP:
  321. /* send a stop condition to I2C bus */
  322. i2c_stop_on_bus(I2CX);
  323. /* i2c master sends STOP signal successfully */
  324. while((I2C_CTL0(I2CX) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
  325. timeout++;
  326. }
  327. if(timeout < I2C_TIME_OUT) {
  328. timeout = 0;
  329. state = I2C_END;
  330. i2c_timeout_flag = I2C_OK;
  331. } else {
  332. timeout = 0;
  333. state = I2C_START;
  334. printf("i2c master sends stop signal timeout in WRITE!\n");
  335. }
  336. break;
  337. default:
  338. state = I2C_START;
  339. i2c_timeout_flag = I2C_OK;
  340. timeout = 0;
  341. printf("i2c master sends start signal in WRITE.\n");
  342. break;
  343. }
  344. }
  345. return I2C_END;
  346. }
  347. /*!
  348. \brief write buffer of data to the EEPROM use timeout function
  349. \param[in] p_buffer: pointer to the buffer containing the data to be written to the EEPROM
  350. \param[in] write_address: EEPROM's internal address to write to
  351. \param[in] number_of_byte: number of bytes to write to the EEPROM
  352. \param[out] none
  353. \retval none
  354. */
  355. void eeprom_buffer_write_timeout(uint8_t *p_buffer, uint8_t write_address, uint16_t number_of_byte)
  356. {
  357. uint8_t number_of_page = 0, number_of_single = 0, address = 0, count = 0;
  358. address = write_address % I2C_PAGE_SIZE;
  359. count = I2C_PAGE_SIZE - address;
  360. number_of_page = number_of_byte / I2C_PAGE_SIZE;
  361. number_of_single = number_of_byte % I2C_PAGE_SIZE;
  362. /* if write_address is I2C_PAGE_SIZE aligned */
  363. if(0 == address) {
  364. while(number_of_page--) {
  365. eeprom_page_write_timeout(p_buffer, write_address, I2C_PAGE_SIZE);
  366. eeprom_wait_standby_state_timeout();
  367. write_address += I2C_PAGE_SIZE;
  368. p_buffer += I2C_PAGE_SIZE;
  369. }
  370. if(0 != number_of_single) {
  371. eeprom_page_write_timeout(p_buffer, write_address, number_of_single);
  372. eeprom_wait_standby_state_timeout();
  373. }
  374. } else {
  375. /* if write_address is not I2C_PAGE_SIZE aligned */
  376. if(number_of_byte < count) {
  377. eeprom_page_write_timeout(p_buffer, write_address, number_of_byte);
  378. eeprom_wait_standby_state_timeout();
  379. } else {
  380. number_of_byte -= count;
  381. number_of_page = number_of_byte / I2C_PAGE_SIZE;
  382. number_of_single = number_of_byte % I2C_PAGE_SIZE;
  383. if(0 != count) {
  384. eeprom_page_write_timeout(p_buffer, write_address, count);
  385. eeprom_wait_standby_state_timeout();
  386. write_address += count;
  387. p_buffer += count;
  388. }
  389. /* write page */
  390. while(number_of_page--) {
  391. eeprom_page_write_timeout(p_buffer, write_address, I2C_PAGE_SIZE);
  392. eeprom_wait_standby_state_timeout();
  393. write_address += I2C_PAGE_SIZE;
  394. p_buffer += I2C_PAGE_SIZE;
  395. }
  396. /* write single */
  397. if(0 != number_of_single) {
  398. eeprom_page_write_timeout(p_buffer, write_address, number_of_single);
  399. eeprom_wait_standby_state_timeout();
  400. }
  401. }
  402. }
  403. }
  404. /*!
  405. \brief read data from the EEPROM
  406. \param[in] p_buffer: pointer to the buffer that receives the data read from the EEPROM
  407. \param[in] read_address: EEPROM's internal address to start reading from
  408. \param[in] number_of_byte: number of bytes to reads from the EEPROM
  409. \param[out] none
  410. \retval none
  411. */
  412. uint8_t eeprom_buffer_read_timeout(uint8_t *p_buffer, uint8_t read_address, uint16_t number_of_byte)
  413. {
  414. uint8_t state = I2C_START;
  415. uint8_t read_cycle = 0;
  416. uint16_t timeout = 0;
  417. uint8_t i2c_timeout_flag = 0;
  418. /* enable acknowledge */
  419. i2c_ack_config(I2CX, I2C_ACK_ENABLE);
  420. while(!(i2c_timeout_flag)) {
  421. switch(state) {
  422. case I2C_START:
  423. if(RESET == read_cycle) {
  424. /* i2c master sends start signal only when the bus is idle */
  425. while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
  426. timeout++;
  427. }
  428. if(timeout < I2C_TIME_OUT) {
  429. /* whether to send ACK or not for the next byte */
  430. if(2 == number_of_byte) {
  431. i2c_ackpos_config(I2CX, I2C_ACKPOS_NEXT);
  432. }
  433. } else {
  434. i2c_bus_reset();
  435. timeout = 0;
  436. state = I2C_START;
  437. printf("i2c bus is busy in READ!\n");
  438. }
  439. }
  440. /* send the start signal */
  441. i2c_start_on_bus(I2CX);
  442. timeout = 0;
  443. state = I2C_SEND_ADDRESS;
  444. break;
  445. case I2C_SEND_ADDRESS:
  446. /* i2c master sends START signal successfully */
  447. while((! i2c_flag_get(I2CX, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
  448. timeout++;
  449. }
  450. if(timeout < I2C_TIME_OUT) {
  451. if(RESET == read_cycle) {
  452. i2c_master_addressing(I2CX, eeprom_address, I2C_TRANSMITTER);
  453. state = I2C_CLEAR_ADDRESS_FLAG;
  454. } else {
  455. i2c_master_addressing(I2CX, eeprom_address, I2C_RECEIVER);
  456. if(number_of_byte < 3) {
  457. /* disable acknowledge */
  458. i2c_ack_config(I2CX, I2C_ACK_DISABLE);
  459. }
  460. state = I2C_CLEAR_ADDRESS_FLAG;
  461. }
  462. timeout = 0;
  463. } else {
  464. timeout = 0;
  465. state = I2C_START;
  466. read_cycle = 0;
  467. printf("i2c master sends start signal timeout in READ!\n");
  468. }
  469. break;
  470. case I2C_CLEAR_ADDRESS_FLAG:
  471. /* address flag set means i2c slave sends ACK */
  472. while((!i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
  473. timeout++;
  474. }
  475. if(timeout < I2C_TIME_OUT) {
  476. i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
  477. if((SET == read_cycle) && (1 == number_of_byte)) {
  478. /* send a stop condition to I2C bus */
  479. i2c_stop_on_bus(I2CX);
  480. }
  481. timeout = 0;
  482. state = I2C_TRANSMIT_DATA;
  483. } else {
  484. timeout = 0;
  485. state = I2C_START;
  486. read_cycle = 0;
  487. printf("i2c master clears address flag timeout in READ!\n");
  488. }
  489. break;
  490. case I2C_TRANSMIT_DATA:
  491. if(RESET == read_cycle) {
  492. /* wait until the transmit data buffer is empty */
  493. while((! i2c_flag_get(I2CX, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
  494. timeout++;
  495. }
  496. if(timeout < I2C_TIME_OUT) {
  497. /* send the EEPROM's internal address to write to : only one byte address */
  498. i2c_data_transmit(I2CX, read_address);
  499. timeout = 0;
  500. } else {
  501. timeout = 0;
  502. state = I2C_START;
  503. read_cycle = 0;
  504. printf("i2c master wait data buffer is empty timeout in READ!\n");
  505. }
  506. /* wait until BTC bit is set */
  507. while((!i2c_flag_get(I2CX, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
  508. timeout++;
  509. }
  510. if(timeout < I2C_TIME_OUT) {
  511. timeout = 0;
  512. state = I2C_START;
  513. read_cycle++;
  514. } else {
  515. timeout = 0;
  516. state = I2C_START;
  517. read_cycle = 0;
  518. printf("i2c master sends EEPROM's internal address timeout in READ!\n");
  519. }
  520. } else {
  521. while(number_of_byte) {
  522. timeout++;
  523. if(3 == number_of_byte) {
  524. /* wait until BTC bit is set */
  525. while(!i2c_flag_get(I2CX, I2C_FLAG_BTC));
  526. /* disable acknowledge */
  527. i2c_ack_config(I2CX, I2C_ACK_DISABLE);
  528. }
  529. if(2 == number_of_byte) {
  530. /* wait until BTC bit is set */
  531. while(!i2c_flag_get(I2CX, I2C_FLAG_BTC));
  532. /* send a stop condition to I2C bus */
  533. i2c_stop_on_bus(I2CX);
  534. }
  535. /* wait until RBNE bit is set */
  536. if(i2c_flag_get(I2CX, I2C_FLAG_RBNE)) {
  537. /* read a byte from the EEPROM */
  538. *p_buffer = i2c_data_receive(I2CX);
  539. /* point to the next location where the byte read will be saved */
  540. p_buffer++;
  541. /* decrement the read bytes counter */
  542. number_of_byte--;
  543. timeout = 0;
  544. }
  545. if(timeout > I2C_TIME_OUT) {
  546. timeout = 0;
  547. state = I2C_START;
  548. read_cycle = 0;
  549. printf("i2c master sends data timeout in READ!\n");
  550. }
  551. }
  552. timeout = 0;
  553. state = I2C_STOP;
  554. }
  555. break;
  556. case I2C_STOP:
  557. /* i2c master sends STOP signal successfully */
  558. while((I2C_CTL0(I2CX) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
  559. timeout++;
  560. }
  561. if(timeout < I2C_TIME_OUT) {
  562. timeout = 0;
  563. state = I2C_END;
  564. i2c_timeout_flag = I2C_OK;
  565. } else {
  566. timeout = 0;
  567. state = I2C_START;
  568. read_cycle = 0;
  569. printf("i2c master sends stop signal timeout in READ!\n");
  570. }
  571. break;
  572. default:
  573. state = I2C_START;
  574. read_cycle = 0;
  575. i2c_timeout_flag = I2C_OK;
  576. timeout = 0;
  577. printf("i2c master sends start signal in READ.\n");
  578. break;
  579. }
  580. }
  581. return I2C_END;
  582. }
  583. /*!
  584. \brief wait for EEPROM standby state use timeout function
  585. \param[in] none
  586. \param[out] none
  587. \retval none
  588. */
  589. uint8_t eeprom_wait_standby_state_timeout()
  590. {
  591. uint8_t state = I2C_START;
  592. uint16_t timeout = 0;
  593. uint8_t i2c_timeout_flag = 0;
  594. while(!(i2c_timeout_flag)) {
  595. switch(state) {
  596. case I2C_START:
  597. /* i2c master sends start signal only when the bus is idle */
  598. while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
  599. timeout++;
  600. }
  601. if(timeout < I2C_TIME_OUT) {
  602. i2c_start_on_bus(I2CX);
  603. timeout = 0;
  604. state = I2C_SEND_ADDRESS;
  605. } else {
  606. i2c_bus_reset();
  607. timeout = 0;
  608. state = I2C_START;
  609. printf("i2c bus is busy in EEPROM standby!\n");
  610. }
  611. break;
  612. case I2C_SEND_ADDRESS:
  613. /* i2c master sends START signal successfully */
  614. while((! i2c_flag_get(I2CX, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
  615. timeout++;
  616. }
  617. if(timeout < I2C_TIME_OUT) {
  618. i2c_master_addressing(I2CX, eeprom_address, I2C_TRANSMITTER);
  619. timeout = 0;
  620. state = I2C_CLEAR_ADDRESS_FLAG;
  621. } else {
  622. timeout = 0;
  623. state = I2C_START;
  624. printf("i2c master sends start signal timeout in EEPROM standby!\n");
  625. }
  626. break;
  627. case I2C_CLEAR_ADDRESS_FLAG:
  628. while((!((i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) || (i2c_flag_get(I2CX, I2C_FLAG_AERR)))) && (timeout < I2C_TIME_OUT)) {
  629. timeout++;
  630. }
  631. if(timeout < I2C_TIME_OUT) {
  632. if(i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) {
  633. i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
  634. timeout = 0;
  635. /* send a stop condition to I2C bus */
  636. i2c_stop_on_bus(I2CX);
  637. i2c_timeout_flag = I2C_OK;
  638. /* exit the function */
  639. return I2C_END;
  640. } else {
  641. /* clear the bit of AE */
  642. i2c_flag_clear(I2CX, I2C_FLAG_AERR);
  643. timeout = 0;
  644. state = I2C_STOP;
  645. }
  646. } else {
  647. timeout = 0;
  648. state = I2C_START;
  649. printf("i2c master clears address flag timeout in EEPROM standby!\n");
  650. }
  651. break;
  652. case I2C_STOP:
  653. /* send a stop condition to I2C bus */
  654. i2c_stop_on_bus(I2CX);
  655. /* i2c master sends STOP signal successfully */
  656. while((I2C_CTL0(I2CX) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
  657. timeout++;
  658. }
  659. if(timeout < I2C_TIME_OUT) {
  660. timeout = 0;
  661. state = I2C_START;
  662. } else {
  663. timeout = 0;
  664. state = I2C_START;
  665. printf("i2c master sends stop signal timeout in EEPROM standby!\n");
  666. }
  667. break;
  668. default:
  669. state = I2C_START;
  670. timeout = 0;
  671. printf("i2c master sends start signal end in EEPROM standby!.\n");
  672. break;
  673. }
  674. }
  675. return I2C_END;
  676. }