| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /*********************************************************************************************************
- * Module : At24xx.c
- * Abstract :
- * Version : 1.0.0
- * Author : Leopul(COPYRIGHT 2025 - 2027 Leopul. All rights reserved.)
- * Complete : 2025-02-28
- * Content : None
- * Note : None
- *********************************************************************************************************/
- #include "At24xx.h"
- #include "SysTick.h"
- #include "I2c.h"
- #include "gd32f30x_libopt.h"
- #include "stdint.h"
- #include "main.h"
- #define CHECK_ADDR 8000
- #define AT24C01 127
- #define AT24C02 255
- #define AT24C04 511
- #define AT24C08 1023
- #define AT24C16 2047
- #define AT24C32 4095
- #define AT24C64 8191
- #define AT24C128 16383
- #define AT24C256 32767
- #define USER_DEFINE_EEPROM_TYPE AT24C64
-
- static void read_iic_data(void);
- void at24xx_init(void)
- {
- InitI2C();
- sts_data_t *data = get_sts();
- //data->read_eeprom = read_iic_data;
- // data->write_at24 = at24xx_write;
- // data->read_at24 = at24xx_read;
- }
- uint8_t at24xx_check(void)
- {
- uint8_t temp = 0;
- temp = at24xx_read_one_byte(CHECK_ADDR);
- if(temp == 0x55)
- {
- return 0;
- }else
- {
- at24xx_write_one_byte(CHECK_ADDR,0x55);
- temp = at24xx_read_one_byte(CHECK_ADDR);
- if(temp == 0x55)
- {
- return 0;
- }
- }
- return 1;
- }
- void at24xx_read(unsigned short read_addr, unsigned char *buff,unsigned short read_num)
- {
- while(read_num)
- {
- *buff = at24xx_read_one_byte(read_addr);
- read_addr++;
- buff++;
- read_num --;
- }
- }
- void at24xx_write(unsigned short write_addr, unsigned char *buff,unsigned short write_num)
- {
- while(write_num)
- {
- at24xx_write_one_byte(write_addr,*buff);
- write_addr++;
- buff++;
- write_num --;
- }
- }
- unsigned char at24xx_read_one_byte(unsigned short read_addr)
- {
- uint8_t temp = 0;
- GenI2CStartSig();
- if(USER_DEFINE_EEPROM_TYPE > AT24C16)
- {
- I2CSendByte(0XA0);
- I2CWaitAck();
- I2CSendByte(read_addr >> 8);
- }else
- {
- I2CSendByte(0XA0 + ((read_addr / 256) << 1));
- }
- I2CWaitAck();
- I2CSendByte(read_addr % 256);
- I2CWaitAck();
-
- GenI2CStartSig();
- I2CSendByte(0XA1);
- I2CWaitAck();
-
- temp = I2CReadByte(0);
-
- GenI2CStopSig();
-
- return temp;
- }
- void at24xx_write_one_byte(unsigned short write_addr,unsigned char data)
- {
- GenI2CStartSig();
- if(USER_DEFINE_EEPROM_TYPE > AT24C16)
- {
- I2CSendByte(0XA0);
- I2CWaitAck();
- I2CSendByte(write_addr >> 8);
- }
- else
- {
- I2CSendByte(0XA0 + ((write_addr / 256) << 1));
- }
-
- I2CWaitAck();
-
- I2CSendByte(write_addr % 256);
- I2CWaitAck();
-
- I2CSendByte(data);
- I2CWaitAck();
-
- GenI2CStopSig();
-
- DelayNms(10);
- }
|