| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- /*********************************************************************************************************
- * 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);
- }
- static void read_iic_data(void)
- {
- sts_data_t *sts_d = get_sts();
- if(sts_d)
- {
- uint8_t * r_addr =&sts_d->m_data.input_th;
- at24xx_read(IIC_INPUT_THRESHOLD_START,r_addr,sizeof(iic_in_threshold_t) * 2);
- for(uint8_t i = 0 ;i < 2 ;i ++)
- {
- if(sts_d->m_data.input_th[i].voltage_max > INVALID_VALUE) sts_d->m_data.input_th[i].voltage_max = 0;
- if(sts_d->m_data.input_th[i].voltage_min > INVALID_VALUE) sts_d->m_data.input_th[i].voltage_min = 0;
- if(sts_d->m_data.input_th[i].freq_max > INVALID_VALUE) sts_d->m_data.input_th[i].freq_max = 0;
- if(sts_d->m_data.input_th[i].freq_min > INVALID_VALUE) sts_d->m_data.input_th[i].freq_min = 0;
-
- sts_d->input_wanning_en[i] = 0;
-
- if(sts_d->m_data.input_th[i].voltage_max <= SMALL_VALUE)
- UNSET_VOLTAGE_MAX_EN(sts_d->input_wanning_en[i]);
- else
- SET_VOLTAGE_MAX_EN(sts_d->input_wanning_en[i]);
-
- if(sts_d->m_data.input_th[i].voltage_min <= SMALL_VALUE)
- UNSET_VOLTAGE_MIN_EN(sts_d->input_wanning_en[i]);
- else
- SET_VOLTAGE_MIN_EN(sts_d->input_wanning_en[i]);
-
- if(sts_d->m_data.input_th[i].freq_max <= SMALL_VALUE)
- UNSET_FREQ_MAX_EN(sts_d->input_wanning_en[i]);
- else
- SET_FREQ_MAX_EN(sts_d->input_wanning_en[i]);
- if(sts_d->m_data.input_th[i].freq_min <= SMALL_VALUE)
- UNSET_FREQ_MIN_EN(sts_d->input_wanning_en[i]);
- else
- SET_FREQ_MIN_EN(sts_d->input_wanning_en[i]);
-
- }
-
- r_addr=&sts_d->m_data.output_th;
- at24xx_read(IIC_OUTPUT_THRESHOLD_START,r_addr,sizeof(iic_out_threshold_t));
- {
- if(sts_d->m_data.output_th.voltage_max > INVALID_VALUE) sts_d->m_data.output_th.voltage_max = 0;
- if(sts_d->m_data.output_th.voltage_min > INVALID_VALUE) sts_d->m_data.output_th.voltage_min = 0;
- if(sts_d->m_data.output_th.current_max > INVALID_VALUE) sts_d->m_data.output_th.current_max = 0;
- if(sts_d->m_data.output_th.power_max > INVALID_VALUE) sts_d->m_data.output_th.power_max = 0;
- if(sts_d->m_data.output_th.consumer_max > INVALID_VALUE) sts_d->m_data.output_th.consumer_max = 0;
-
- sts_d ->output_wanning_en = 0;
-
- if(sts_d->m_data.output_th.voltage_max <= SMALL_VALUE)
- UNSET_VOLTAGE_MAX_EN(sts_d ->output_wanning_en);
- else
- SET_VOLTAGE_MAX_EN(sts_d ->output_wanning_en);
-
- if(sts_d->m_data.output_th.voltage_min <= SMALL_VALUE)
- UNSET_VOLTAGE_MIN_EN(sts_d ->output_wanning_en);
- else
- SET_VOLTAGE_MIN_EN(sts_d ->output_wanning_en);
- if(sts_d->m_data.output_th.current_max <= SMALL_VALUE)
- UNSET_CURRENT_MAX_EN(sts_d ->output_wanning_en);
- else
- SET_CURRENT_MAX_EN(sts_d ->output_wanning_en);
- if(sts_d->m_data.output_th.power_max <= SMALL_VALUE)
- UNSET_POWER_MAX_EN(sts_d ->output_wanning_en);
- else
- SET_POWER_MAX_EN(sts_d ->output_wanning_en);
- if(sts_d->m_data.output_th.consumer_max <= SMALL_VALUE)
- UNSET_CONSUMER_MAX_EN(sts_d ->output_wanning_en);
- else
- SET_CONSUMER_MAX_EN(sts_d ->output_wanning_en);
- }
-
- r_addr = &sts_d->m_data.temprature_th;
- at24xx_read(IIC_TEMPRATURE_THRESHOLD_START,r_addr,sizeof(iic_temp_threshold_t));
- {
- if(sts_d->m_data.temprature_th.fans_max_temp > 0x7fff) sts_d->m_data.temprature_th.fans_max_temp = 0;
- if(sts_d->m_data.temprature_th.fans_min_temp > 0x7fff) sts_d->m_data.temprature_th.fans_min_temp = 0;
- }
- r_addr = &sts_d->m_data.over_func;
- at24xx_read(IIC_OVER_FUNC_START,r_addr,sizeof(iic_over_func));
- if(sts_d->m_data.over_func > 0x003F)
- {
- sts_d->m_data.over_func = 0;
- }
- r_addr = &sts_d->iic_consumer;
- uint32_t buff = 0;
- at24xx_read(IIC_CONSUMER_START,&buff,4);
- if((uint32_t)buff == 0)
- {
- sts_d->iic_consumer = 0;
- }else
- {
- at24xx_read(IIC_CONSUMER_START,r_addr,sizeof(float));
- }
-
-
- r_addr = &sts_d->m_data.out_status;
- at24xx_read(IIC_OUT_CHANNEL,r_addr,sizeof(uint16_t));
- if(sts_d->m_data.out_status > 1)
- {
- sts_d->m_data.out_status = 0;
- }
- }
- }
|