at24cxx.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. uint8_t i2c_buffer_write[BUFFER_SIZE];
  36. uint8_t i2c_buffer_read[BUFFER_SIZE];
  37. volatile uint8_t *i2c_write;
  38. volatile uint8_t *i2c_read;
  39. volatile uint16_t i2c_nbytes;
  40. volatile uint16_t i2c_write_dress;
  41. volatile uint16_t i2c_read_dress;
  42. volatile uint8_t i2c_process_flag = 0;
  43. /*!
  44. \brief I2C read and write functions
  45. \param[in] none
  46. \param[out] none
  47. \retval I2C_OK or I2C_FAIL
  48. */
  49. uint8_t i2c_24c02_test(void)
  50. {
  51. uint16_t i;
  52. printf("\r\nAT24C02 writing...\r\n");
  53. /* initialize i2c_buffer_write */
  54. for(i = 0; i < BUFFER_SIZE; i++) {
  55. i2c_buffer_write[i] = i;
  56. printf("0x%02X ", i2c_buffer_write[i]);
  57. if(15 == i % 16) {
  58. printf("\r\n");
  59. }
  60. }
  61. /* EEPROM data write */
  62. eeprom_buffer_write_interrupt(i2c_buffer_write, EEP_FIRST_PAGE, BUFFER_SIZE);
  63. printf("AT24C02 reading...\r\n");
  64. /* EEPROM data read */
  65. eeprom_buffer_read_interrupt(i2c_buffer_read, EEP_FIRST_PAGE, 2);
  66. eeprom_buffer_read_interrupt(i2c_buffer_read, EEP_FIRST_PAGE, BUFFER_SIZE);
  67. /* compare the read buffer and write buffer */
  68. for(i = 0; i < BUFFER_SIZE; i++) {
  69. if(i2c_buffer_read[i] != i2c_buffer_write[i]) {
  70. printf("0x%02X ", i2c_buffer_read[i]);
  71. printf("Err:data read and write aren't matching.\n\r");
  72. return I2C_FAIL;
  73. }
  74. printf("0x%02X ", i2c_buffer_read[i]);
  75. if(15 == i % 16) {
  76. printf("\r\n");
  77. }
  78. }
  79. printf("I2C-AT24C02 test passed!\n\r");
  80. return I2C_OK;
  81. }
  82. /*!
  83. \brief initialize peripherals used by the I2C EEPROM driver
  84. \param[in] none
  85. \param[out] none
  86. \retval none
  87. */
  88. void i2c_eeprom_init(void)
  89. {
  90. eeprom_address = EEPROM_BLOCK0_ADDRESS;
  91. }
  92. /*!
  93. \brief write buffer of data to the I2C EEPROM
  94. \param[in] p_buffer: pointer to the buffer containing the data to be written to the EEPROM
  95. \param[in] write_address: EEPROM's internal address to write to
  96. \param[in] number_of_byte: number of bytes to write to the EEPROM
  97. \param[out] none
  98. \retval none
  99. */
  100. void eeprom_buffer_write_interrupt(uint8_t *p_buffer, uint8_t write_address, uint16_t number_of_byte)
  101. {
  102. uint8_t number_of_page = 0, number_of_single = 0, address = 0, count = 0;
  103. address = write_address % I2C_PAGE_SIZE;
  104. count = I2C_PAGE_SIZE - address;
  105. number_of_page = number_of_byte / I2C_PAGE_SIZE;
  106. number_of_single = number_of_byte % I2C_PAGE_SIZE;
  107. i2c_process_flag = 0;
  108. /* if write_address is I2C_PAGE_SIZE aligned */
  109. if(0 == address) {
  110. while(number_of_page--) {
  111. i2c_nbytes = I2C_PAGE_SIZE;
  112. i2c_write_dress = write_address;
  113. i2c_write = p_buffer;
  114. /* write data by interrupt */
  115. eeprom_page_write_interrupt();
  116. eeprom_wait_standby_state();
  117. write_address += I2C_PAGE_SIZE;
  118. p_buffer += I2C_PAGE_SIZE;
  119. }
  120. if(0 != number_of_single) {
  121. i2c_nbytes = number_of_single;
  122. i2c_write_dress = write_address;
  123. i2c_write = p_buffer;
  124. /* write data by interrupt */
  125. eeprom_page_write_interrupt();
  126. eeprom_wait_standby_state();
  127. }
  128. } else {
  129. /* if write_address is not I2C_PAGE_SIZE aligned */
  130. if(number_of_byte < count) {
  131. i2c_nbytes = number_of_byte;
  132. i2c_write_dress = write_address;
  133. i2c_write = p_buffer;
  134. /* write data by interrupt */
  135. eeprom_page_write_interrupt();
  136. eeprom_wait_standby_state();
  137. } else {
  138. number_of_byte -= count;
  139. number_of_page = number_of_byte / I2C_PAGE_SIZE;
  140. number_of_single = number_of_byte % I2C_PAGE_SIZE;
  141. if(0 != count) {
  142. i2c_nbytes = count;
  143. i2c_write_dress = write_address;
  144. i2c_write = p_buffer;
  145. /* write data by interrupt */
  146. eeprom_page_write_interrupt();
  147. eeprom_wait_standby_state();
  148. write_address += count;
  149. p_buffer += count;
  150. }
  151. /* write page */
  152. while(number_of_page--) {
  153. i2c_nbytes = number_of_page;
  154. i2c_write_dress = write_address;
  155. i2c_write = p_buffer;
  156. /* write data by interrupt */
  157. eeprom_page_write_interrupt();
  158. eeprom_wait_standby_state();
  159. write_address += I2C_PAGE_SIZE;
  160. p_buffer += I2C_PAGE_SIZE;;
  161. }
  162. /* write single */
  163. if(0 != number_of_single) {
  164. i2c_nbytes = number_of_page;
  165. i2c_write_dress = write_address;
  166. i2c_write = p_buffer;
  167. /* write data by interrupt */
  168. eeprom_page_write_interrupt();
  169. eeprom_wait_standby_state();
  170. }
  171. }
  172. }
  173. i2c_process_flag = SET;
  174. }
  175. /*!
  176. \brief write more than one byte to the EEPROM by interrupt
  177. \param[in] none
  178. \param[out] none
  179. \retval none
  180. */
  181. void eeprom_page_write_interrupt()
  182. {
  183. /* enable the I2CX interrupt */
  184. i2c_interrupt_enable(I2CX, I2C_INT_ERR);
  185. i2c_interrupt_enable(I2CX, I2C_INT_EV);
  186. i2c_interrupt_enable(I2CX, I2C_INT_BUF);
  187. /* the master waits until the I2C bus is idle */
  188. while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY));
  189. /* the master sends a start condition to I2C bus */
  190. i2c_start_on_bus(I2CX);
  191. while((i2c_nbytes > 0));
  192. }
  193. /*!
  194. \brief read data from the EEPROM by interrupt
  195. \param[in] p_buffer: pointer to the buffer that receives the data read from the EEPROM
  196. \param[in] read_address: EEPROM's internal address to start reading from
  197. \param[in] number_of_byte: number of bytes to reads from the EEPROM
  198. \param[out] none
  199. \retval none
  200. */
  201. void eeprom_buffer_read_interrupt(uint8_t *p_buffer, uint8_t read_address, uint16_t number_of_byte)
  202. {
  203. i2c_read = p_buffer;
  204. i2c_read_dress = read_address;
  205. i2c_nbytes = number_of_byte;
  206. i2c_process_flag = SET;
  207. /* enable acknowledge */
  208. i2c_ack_config(I2CX, I2C_ACK_ENABLE);
  209. /* enable the I2CX interrupt */
  210. i2c_interrupt_enable(I2CX, I2C_INT_ERR);
  211. i2c_interrupt_enable(I2CX, I2C_INT_EV);
  212. i2c_interrupt_enable(I2CX, I2C_INT_BUF);
  213. /* wait until I2C bus is idle */
  214. while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY));
  215. if(2 == number_of_byte) {
  216. i2c_ackpos_config(I2CX, I2C_ACKPOS_NEXT);
  217. }
  218. /* send a start condition to I2C bus */
  219. i2c_start_on_bus(I2CX);
  220. while((i2c_nbytes > 0));
  221. }
  222. /*!
  223. \brief wait for EEPROM standby state
  224. \param[in] none
  225. \param[out] none
  226. \retval none
  227. */
  228. void eeprom_wait_standby_state(void)
  229. {
  230. __IO uint32_t val = 0;
  231. while(1) {
  232. /* wait until I2C bus is idle */
  233. while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY));
  234. /* send a start condition to I2C bus */
  235. i2c_start_on_bus(I2CX);
  236. /* wait until SBSEND bit is set */
  237. while(!i2c_flag_get(I2CX, I2C_FLAG_SBSEND));
  238. /* send slave address to I2C bus */
  239. i2c_master_addressing(I2CX, eeprom_address, I2C_TRANSMITTER);
  240. /* keep looping till the Address is acknowledged or the AE flag is set (address not acknowledged at time) */
  241. do {
  242. /* get the current value of the I2C_STAT0 register */
  243. val = I2C_STAT0(I2CX);
  244. } while(0 == (val & (I2C_STAT0_ADDSEND | I2C_STAT0_AERR)));
  245. /* check if the ADDSEND flag has been set */
  246. if(val & I2C_STAT0_ADDSEND) {
  247. /* clear ADDSEND flag */
  248. i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
  249. /* send a stop condition to I2C bus */
  250. i2c_stop_on_bus(I2CX);
  251. /* exit the function */
  252. return ;
  253. } else {
  254. /* clear the bit of AE */
  255. i2c_flag_clear(I2CX, I2C_FLAG_AERR);
  256. }
  257. /* send a stop condition to I2C bus */
  258. i2c_stop_on_bus(I2CX);
  259. /* wait until the stop condition is finished */
  260. while(I2C_CTL0(I2CX) & I2C_CTL0_STOP);
  261. }
  262. }