At24xx.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*********************************************************************************************************
  2. * Module : At24xx.c
  3. * Abstract :
  4. * Version : 1.0.0
  5. * Author : Leopul(COPYRIGHT 2025 - 2027 Leopul. All rights reserved.)
  6. * Complete : 2025-02-28
  7. * Content : None
  8. * Note : None
  9. *********************************************************************************************************/
  10. #include "At24xx.h"
  11. #include "SysTick.h"
  12. #include "I2c.h"
  13. #include "gd32f30x_libopt.h"
  14. #include "stdint.h"
  15. #include "main.h"
  16. #define CHECK_ADDR 8000
  17. #define AT24C01 127
  18. #define AT24C02 255
  19. #define AT24C04 511
  20. #define AT24C08 1023
  21. #define AT24C16 2047
  22. #define AT24C32 4095
  23. #define AT24C64 8191
  24. #define AT24C128 16383
  25. #define AT24C256 32767
  26. #define USER_DEFINE_EEPROM_TYPE AT24C64
  27. static void read_iic_data(void);
  28. void at24xx_init(void)
  29. {
  30. InitI2C();
  31. sts_data_t *data = get_sts();
  32. //data->read_eeprom = read_iic_data;
  33. // data->write_at24 = at24xx_write;
  34. // data->read_at24 = at24xx_read;
  35. }
  36. uint8_t at24xx_check(void)
  37. {
  38. uint8_t temp = 0;
  39. temp = at24xx_read_one_byte(CHECK_ADDR);
  40. if(temp == 0x55)
  41. {
  42. return 0;
  43. }else
  44. {
  45. at24xx_write_one_byte(CHECK_ADDR,0x55);
  46. temp = at24xx_read_one_byte(CHECK_ADDR);
  47. if(temp == 0x55)
  48. {
  49. return 0;
  50. }
  51. }
  52. return 1;
  53. }
  54. void at24xx_read(unsigned short read_addr, unsigned char *buff,unsigned short read_num)
  55. {
  56. while(read_num)
  57. {
  58. *buff = at24xx_read_one_byte(read_addr);
  59. read_addr++;
  60. buff++;
  61. read_num --;
  62. }
  63. }
  64. void at24xx_write(unsigned short write_addr, unsigned char *buff,unsigned short write_num)
  65. {
  66. while(write_num)
  67. {
  68. at24xx_write_one_byte(write_addr,*buff);
  69. write_addr++;
  70. buff++;
  71. write_num --;
  72. }
  73. }
  74. unsigned char at24xx_read_one_byte(unsigned short read_addr)
  75. {
  76. uint8_t temp = 0;
  77. GenI2CStartSig();
  78. if(USER_DEFINE_EEPROM_TYPE > AT24C16)
  79. {
  80. I2CSendByte(0XA0);
  81. I2CWaitAck();
  82. I2CSendByte(read_addr >> 8);
  83. }else
  84. {
  85. I2CSendByte(0XA0 + ((read_addr / 256) << 1));
  86. }
  87. I2CWaitAck();
  88. I2CSendByte(read_addr % 256);
  89. I2CWaitAck();
  90. GenI2CStartSig();
  91. I2CSendByte(0XA1);
  92. I2CWaitAck();
  93. temp = I2CReadByte(0);
  94. GenI2CStopSig();
  95. return temp;
  96. }
  97. void at24xx_write_one_byte(unsigned short write_addr,unsigned char data)
  98. {
  99. GenI2CStartSig();
  100. if(USER_DEFINE_EEPROM_TYPE > AT24C16)
  101. {
  102. I2CSendByte(0XA0);
  103. I2CWaitAck();
  104. I2CSendByte(write_addr >> 8);
  105. }
  106. else
  107. {
  108. I2CSendByte(0XA0 + ((write_addr / 256) << 1));
  109. }
  110. I2CWaitAck();
  111. I2CSendByte(write_addr % 256);
  112. I2CWaitAck();
  113. I2CSendByte(data);
  114. I2CWaitAck();
  115. GenI2CStopSig();
  116. DelayNms(10);
  117. }