at24cxx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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_dma_timeout(i2c_buffer_write, EEP_FIRST_PAGE, BUFFER_SIZE);
  57. printf("AT24C02 reading...\r\n");
  58. /* EEPROM data read */
  59. eeprom_buffer_read_dma_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 more than one byte to the EEPROM with a single write cycle
  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[in] number_of_byte: number of bytes to write to the EEPROM
  90. \param[out] none
  91. \retval none
  92. */
  93. uint8_t eeprom_page_write_dma_timeout(uint8_t *p_buffer, uint8_t write_address, uint8_t number_of_byte)
  94. {
  95. dma_parameter_struct dma_init_struct;
  96. uint8_t state = I2C_START;
  97. uint16_t timeout = 0;
  98. uint8_t i2c_timeout_flag = 0;
  99. /* enable acknowledge */
  100. i2c_ack_config(I2CX, I2C_ACK_ENABLE);
  101. while(!(i2c_timeout_flag)) {
  102. switch(state) {
  103. case I2C_START:
  104. /* i2c master sends start signal only when the bus is idle */
  105. while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
  106. timeout++;
  107. }
  108. if(timeout < I2C_TIME_OUT) {
  109. i2c_start_on_bus(I2CX);
  110. timeout = 0;
  111. state = I2C_SEND_ADDRESS;
  112. } else {
  113. i2c_bus_reset();
  114. timeout = 0;
  115. state = I2C_START;
  116. printf("i2c bus is busy in WRITE!\n");
  117. }
  118. break;
  119. case I2C_SEND_ADDRESS:
  120. /* i2c master sends START signal successfully */
  121. while((! i2c_flag_get(I2CX, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
  122. timeout++;
  123. }
  124. if(timeout < I2C_TIME_OUT) {
  125. i2c_master_addressing(I2CX, eeprom_address, I2C_TRANSMITTER);
  126. timeout = 0;
  127. state = I2C_CLEAR_ADDRESS_FLAG;
  128. } else {
  129. timeout = 0;
  130. state = I2C_START;
  131. printf("i2c master sends start signal timeout in WRITE!\n");
  132. }
  133. break;
  134. case I2C_CLEAR_ADDRESS_FLAG:
  135. /* address flag set means i2c slave sends ACK */
  136. while((! i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
  137. timeout++;
  138. }
  139. if(timeout < I2C_TIME_OUT) {
  140. i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
  141. timeout = 0;
  142. state = I2C_TRANSMIT_DATA;
  143. } else {
  144. timeout = 0;
  145. state = I2C_START;
  146. printf("i2c master clears address flag timeout in WRITE!\n");
  147. }
  148. break;
  149. case I2C_TRANSMIT_DATA:
  150. /* wait until the transmit data buffer is empty */
  151. while((! i2c_flag_get(I2CX, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
  152. timeout++;
  153. }
  154. if(timeout < I2C_TIME_OUT) {
  155. /* send the EEPROM's internal address to write to : only one byte address */
  156. i2c_data_transmit(I2CX, write_address);
  157. timeout = 0;
  158. } else {
  159. timeout = 0;
  160. state = I2C_START;
  161. printf("i2c master sends EEPROM's internal address timeout in WRITE!\n");
  162. }
  163. /* wait until BTC bit is set */
  164. while(!i2c_flag_get(I2CX, I2C_FLAG_BTC));
  165. dma_deinit(I2C_DMA, DMA_TX_CH);
  166. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  167. dma_init_struct.memory_addr = (uint32_t)p_buffer;
  168. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  169. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  170. dma_init_struct.number = number_of_byte;
  171. dma_init_struct.periph_addr = (uint32_t)&I2C_DATA(I2CX);
  172. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  173. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  174. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  175. dma_init(I2C_DMA, DMA_TX_CH, &dma_init_struct);
  176. /* enable I2CX DMA */
  177. i2c_dma_config(I2CX, I2C_DMA_ON);
  178. /* enable DMA0 channel5 */
  179. dma_channel_enable(I2C_DMA, DMA_TX_CH);
  180. /* wait until BTC bit is set */
  181. while(!dma_flag_get(I2C_DMA, DMA_TX_CH, DMA_FLAG_FTF));
  182. /* wait until BTC bit is set */
  183. while((!i2c_flag_get(I2CX, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
  184. timeout++;
  185. }
  186. if(timeout < I2C_TIME_OUT) {
  187. timeout = 0;
  188. state = I2C_STOP;
  189. } else {
  190. timeout = 0;
  191. state = I2C_START;
  192. printf("i2c master sends data timeout in WRITE!\n");
  193. }
  194. break;
  195. case I2C_STOP:
  196. /* send a stop condition to I2C bus */
  197. i2c_stop_on_bus(I2CX);
  198. /* i2c master sends STOP signal successfully */
  199. while((I2C_CTL0(I2CX) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
  200. timeout++;
  201. }
  202. if(timeout < I2C_TIME_OUT) {
  203. timeout = 0;
  204. state = I2C_END;
  205. i2c_timeout_flag = I2C_OK;
  206. } else {
  207. timeout = 0;
  208. state = I2C_START;
  209. printf("i2c master sends stop signal timeout in WRITE!\n");
  210. }
  211. break;
  212. default:
  213. state = I2C_START;
  214. i2c_timeout_flag = I2C_OK;
  215. timeout = 0;
  216. printf("i2c master sends start signal in WRITE.\n");
  217. break;
  218. }
  219. }
  220. return I2C_END;
  221. }
  222. /*!
  223. \brief write buffer of data to the EEPROM use timeout function
  224. \param[in] p_buffer: pointer to the buffer containing the data to be written to the EEPROM
  225. \param[in] write_address: EEPROM's internal address to write
  226. \param[in] number_of_byte: number of bytes to write to the EEPROM
  227. \param[out] none
  228. \retval none
  229. */
  230. void eeprom_buffer_write_dma_timeout(uint8_t *p_buffer, uint8_t write_address, uint16_t number_of_byte)
  231. {
  232. uint8_t number_of_page = 0, number_of_single = 0, address = 0, count = 0;
  233. address = write_address % I2C_PAGE_SIZE;
  234. count = I2C_PAGE_SIZE - address;
  235. number_of_page = number_of_byte / I2C_PAGE_SIZE;
  236. number_of_single = number_of_byte % I2C_PAGE_SIZE;
  237. /* if write_address is I2C_PAGE_SIZE aligned */
  238. if(0 == address) {
  239. while(number_of_page--) {
  240. eeprom_page_write_dma_timeout(p_buffer, write_address, I2C_PAGE_SIZE);
  241. eeprom_wait_standby_state_timeout();
  242. write_address += I2C_PAGE_SIZE;
  243. p_buffer += I2C_PAGE_SIZE;
  244. }
  245. if(0 != number_of_single) {
  246. eeprom_page_write_dma_timeout(p_buffer, write_address, number_of_single);
  247. eeprom_wait_standby_state_timeout();
  248. }
  249. } else {
  250. /* if write_address is not I2C_PAGE_SIZE aligned */
  251. if(number_of_byte < count) {
  252. eeprom_page_write_dma_timeout(p_buffer, write_address, number_of_byte);
  253. eeprom_wait_standby_state_timeout();
  254. } else {
  255. number_of_byte -= count;
  256. number_of_page = number_of_byte / I2C_PAGE_SIZE;
  257. number_of_single = number_of_byte % I2C_PAGE_SIZE;
  258. if(0 != count) {
  259. eeprom_page_write_dma_timeout(p_buffer, write_address, count);
  260. eeprom_wait_standby_state_timeout();
  261. write_address += count;
  262. p_buffer += count;
  263. }
  264. /* write page */
  265. while(number_of_page--) {
  266. eeprom_page_write_dma_timeout(p_buffer, write_address, I2C_PAGE_SIZE);
  267. eeprom_wait_standby_state_timeout();
  268. write_address += I2C_PAGE_SIZE;
  269. p_buffer += I2C_PAGE_SIZE;
  270. }
  271. /* write single */
  272. if(0 != number_of_single) {
  273. eeprom_page_write_dma_timeout(p_buffer, write_address, number_of_single);
  274. eeprom_wait_standby_state_timeout();
  275. }
  276. }
  277. }
  278. }
  279. /*!
  280. \brief read data from the EEPROM
  281. \param[in] p_buffer: pointer to the buffer that receives the data read from the EEPROM
  282. \param[in] read_address: EEPROM's internal address to start reading from
  283. \param[in] number_of_byte: number of bytes to reads from the EEPROM
  284. \param[out] none
  285. \retval none
  286. */
  287. uint8_t eeprom_buffer_read_dma_timeout(uint8_t *p_buffer, uint8_t read_address, uint16_t number_of_byte)
  288. {
  289. dma_parameter_struct dma_init_struct;
  290. uint8_t state = I2C_START;
  291. uint8_t read_cycle = 0;
  292. uint16_t timeout = 0;
  293. uint8_t i2c_timeout_flag = 0;
  294. /* enable acknowledge */
  295. i2c_ack_config(I2CX, I2C_ACK_ENABLE);
  296. while(!(i2c_timeout_flag)) {
  297. switch(state) {
  298. case I2C_START:
  299. if(RESET == read_cycle) {
  300. /* disable I2CX */
  301. i2c_disable(I2CX);
  302. /* enable I2CX */
  303. i2c_enable(I2CX);
  304. /* enable acknowledge */
  305. i2c_ack_config(I2CX, I2C_ACK_ENABLE);
  306. /* i2c master sends start signal only when the bus is idle */
  307. while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
  308. timeout++;
  309. }
  310. if(timeout < I2C_TIME_OUT) {
  311. /* send the start signal */
  312. i2c_start_on_bus(I2CX);
  313. timeout = 0;
  314. state = I2C_SEND_ADDRESS;
  315. } else {
  316. i2c_bus_reset();
  317. timeout = 0;
  318. state = I2C_START;
  319. printf("i2c bus is busy in READ!\n");
  320. }
  321. } else {
  322. i2c_start_on_bus(I2CX);
  323. timeout = 0;
  324. state = I2C_SEND_ADDRESS;
  325. }
  326. break;
  327. case I2C_SEND_ADDRESS:
  328. /* i2c master sends START signal successfully */
  329. while((! i2c_flag_get(I2CX, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
  330. timeout++;
  331. }
  332. if(timeout < I2C_TIME_OUT) {
  333. if(RESET == read_cycle) {
  334. i2c_master_addressing(I2CX, eeprom_address, I2C_TRANSMITTER);
  335. state = I2C_CLEAR_ADDRESS_FLAG;
  336. } else {
  337. i2c_master_addressing(I2CX, eeprom_address, I2C_RECEIVER);
  338. state = I2C_CLEAR_ADDRESS_FLAG;
  339. }
  340. timeout = 0;
  341. } else {
  342. timeout = 0;
  343. state = I2C_START;
  344. read_cycle = 0;
  345. printf("i2c master sends start signal timeout in READ!\n");
  346. }
  347. break;
  348. case I2C_CLEAR_ADDRESS_FLAG:
  349. /* address flag set means i2c slave sends ACK */
  350. while((!i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
  351. timeout++;
  352. }
  353. if(timeout < I2C_TIME_OUT) {
  354. i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
  355. timeout = 0;
  356. state = I2C_TRANSMIT_DATA;
  357. } else {
  358. timeout = 0;
  359. state = I2C_START;
  360. read_cycle = 0;
  361. printf("i2c master clears address flag timeout in READ!\n");
  362. }
  363. break;
  364. case I2C_TRANSMIT_DATA:
  365. if(RESET == read_cycle) {
  366. /* wait until the transmit data buffer is empty */
  367. while((! i2c_flag_get(I2CX, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
  368. timeout++;
  369. }
  370. if(timeout < I2C_TIME_OUT) {
  371. /* send the EEPROM's internal address to write to : only one byte address */
  372. i2c_data_transmit(I2CX, read_address);
  373. timeout = 0;
  374. } else {
  375. timeout = 0;
  376. state = I2C_START;
  377. read_cycle = 0;
  378. printf("i2c master wait data buffer is empty timeout in READ!\n");
  379. }
  380. /* wait until BTC bit is set */
  381. while((!i2c_flag_get(I2CX, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
  382. timeout++;
  383. }
  384. if(timeout < I2C_TIME_OUT) {
  385. timeout = 0;
  386. state = I2C_START;
  387. read_cycle++;
  388. } else {
  389. timeout = 0;
  390. state = I2C_START;
  391. read_cycle = 0;
  392. printf("i2c master sends EEPROM's internal address timeout in READ!\n");
  393. }
  394. } else {
  395. /* one byte master reception procedure (polling) */
  396. if(number_of_byte < 2) {
  397. /* disable acknowledge */
  398. i2c_ack_config(I2CX, I2C_ACK_DISABLE);
  399. /* clear ADDSEND register by reading I2C_STAT0 then I2C_STAT1 register (I2C_STAT0 has already been read) */
  400. i2c_flag_get(I2CX, I2C_FLAG_ADDSEND);
  401. /* send a stop condition to I2C bus*/
  402. i2c_stop_on_bus(I2CX);
  403. /* wait for the byte to be received */
  404. while(!i2c_flag_get(I2CX, I2C_FLAG_RBNE));
  405. /* read the byte received from the EEPROM */
  406. *p_buffer = i2c_data_receive(I2CX);
  407. /* decrement the read bytes counter */
  408. number_of_byte--;
  409. timeout = 0;
  410. state = I2C_STOP;
  411. } else { /* more than one byte master reception procedure (DMA) */
  412. dma_deinit(I2C_DMA, DMA_RX_CH);
  413. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  414. dma_init_struct.memory_addr = (uint32_t)p_buffer;
  415. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  416. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  417. dma_init_struct.number = number_of_byte;
  418. dma_init_struct.periph_addr = (uint32_t)&I2C_DATA(I2CX);
  419. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  420. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  421. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  422. dma_init(I2C_DMA, DMA_RX_CH, &dma_init_struct);
  423. i2c_dma_last_transfer_config(I2CX, I2C_DMALST_ON);
  424. /* enable I2CX DMA */
  425. i2c_dma_config(I2CX, I2C_DMA_ON);
  426. /* enable DMA0 channel5 */
  427. dma_channel_enable(I2C_DMA, DMA_RX_CH);
  428. /* wait until BTC bit is set */
  429. while(!dma_flag_get(I2C_DMA, DMA_RX_CH, DMA_FLAG_FTF));
  430. state = I2C_STOP;
  431. }
  432. }
  433. break;
  434. case I2C_STOP:
  435. /* send a stop condition to I2C bus */
  436. i2c_stop_on_bus(I2CX);
  437. /* i2c master sends STOP signal successfully */
  438. while((I2C_CTL0(I2CX) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
  439. timeout++;
  440. }
  441. if(timeout < I2C_TIME_OUT) {
  442. timeout = 0;
  443. state = I2C_END;
  444. i2c_timeout_flag = I2C_OK;
  445. } else {
  446. timeout = 0;
  447. state = I2C_START;
  448. read_cycle = 0;
  449. printf("i2c master sends stop signal timeout in READ!\n");
  450. }
  451. break;
  452. default:
  453. state = I2C_START;
  454. read_cycle = 0;
  455. i2c_timeout_flag = I2C_OK;
  456. timeout = 0;
  457. printf("i2c master sends start signal in READ.\n");
  458. break;
  459. }
  460. }
  461. return I2C_END;
  462. }
  463. /*!
  464. \brief wait for EEPROM standby state use timeout function
  465. \param[in] none
  466. \param[out] none
  467. \retval none
  468. */
  469. uint8_t eeprom_wait_standby_state_timeout(void)
  470. {
  471. uint8_t state = I2C_START;
  472. uint16_t timeout = 0;
  473. uint8_t i2c_timeout_flag = 0;
  474. while(!(i2c_timeout_flag)) {
  475. switch(state) {
  476. case I2C_START:
  477. /* i2c master sends start signal only when the bus is idle */
  478. while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
  479. timeout++;
  480. }
  481. if(timeout < I2C_TIME_OUT) {
  482. i2c_start_on_bus(I2CX);
  483. timeout = 0;
  484. state = I2C_SEND_ADDRESS;
  485. } else {
  486. i2c_bus_reset();
  487. timeout = 0;
  488. state = I2C_START;
  489. printf("i2c bus is busy in EEPROM standby!\n");
  490. }
  491. break;
  492. case I2C_SEND_ADDRESS:
  493. /* i2c master sends START signal successfully */
  494. while((! i2c_flag_get(I2CX, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
  495. timeout++;
  496. }
  497. if(timeout < I2C_TIME_OUT) {
  498. i2c_master_addressing(I2CX, eeprom_address, I2C_TRANSMITTER);
  499. timeout = 0;
  500. state = I2C_CLEAR_ADDRESS_FLAG;
  501. } else {
  502. timeout = 0;
  503. state = I2C_START;
  504. printf("i2c master sends start signal timeout in EEPROM standby!\n");
  505. }
  506. break;
  507. case I2C_CLEAR_ADDRESS_FLAG:
  508. while((!((i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) || (i2c_flag_get(I2CX, I2C_FLAG_AERR)))) && (timeout < I2C_TIME_OUT)) {
  509. timeout++;
  510. }
  511. if(timeout < I2C_TIME_OUT) {
  512. if(i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) {
  513. i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
  514. timeout = 0;
  515. /* send a stop condition to I2C bus */
  516. i2c_stop_on_bus(I2CX);
  517. i2c_timeout_flag = I2C_OK;
  518. /* exit the function */
  519. return I2C_END;
  520. } else {
  521. /* clear the bit of AE */
  522. i2c_flag_clear(I2CX, I2C_FLAG_AERR);
  523. timeout = 0;
  524. state = I2C_STOP;
  525. }
  526. } else {
  527. timeout = 0;
  528. state = I2C_START;
  529. printf("i2c master clears address flag timeout in EEPROM standby!\n");
  530. }
  531. break;
  532. case I2C_STOP:
  533. /* send a stop condition to I2C bus */
  534. i2c_stop_on_bus(I2CX);
  535. /* i2c master sends STOP signal successfully */
  536. while((I2C_CTL0(I2CX) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
  537. timeout++;
  538. }
  539. if(timeout < I2C_TIME_OUT) {
  540. timeout = 0;
  541. state = I2C_START;
  542. } else {
  543. timeout = 0;
  544. state = I2C_START;
  545. printf("i2c master sends stop signal timeout in EEPROM standby!\n");
  546. }
  547. break;
  548. default:
  549. state = I2C_START;
  550. timeout = 0;
  551. printf("i2c master sends start signal end in EEPROM standby!.\n");
  552. break;
  553. }
  554. }
  555. return I2C_END;
  556. }