#include "bsp.h" #include "I2C.h" #include "eerom.h" #include "utils.h" //#define EEROM_DELAY 3 #define EEROM_DELAY 5 #define DEFAULT_EEROM 0 #define EEROM_TEST_ENABLE 0 #define EEROM_TEST_COUNT 256 #define EEROM_TEST_ADDR 256 //IIC send a byte //return ACK from slave // 1: ACK // 0: NACK #ifdef __GNUC__ #pragma GCC optimize ("O0") #elif defined(__ICCARM__) #pragma optimize=none #endif static uint8_t I2C_SendByte(uint8_t txd) { uint8_t t; I2C_GPIO_Out_Config(); BSP_I2Cx_SCL_LOW(); for(t=0;t<8;t++) { if(txd&0x80) BSP_I2Cx_SDA_HIGH(); else BSP_I2Cx_SDA_LOW(); txd<<=1; Delay_us(I2C_DELAY_COUNT); //For TEA5767, the delay here is necessary BSP_I2Cx_SCL_HIGH(); Delay_us(I2C_DELAY_COUNT); BSP_I2Cx_SCL_LOW(); Delay_us(I2C_DELAY_COUNT); } return 0; } //Read a byte; ack=1, send an ack, ack=0, NACK #ifdef __GNUC__ #pragma GCC optimize ("O0") #elif defined(__ICCARM__) #pragma optimize=none #endif static uint8_t I2C_ReceiveByte(uint8_t ack) { uint8_t i,receive=0; BSP_I2Cx_SCL_LOW(); I2C_GPIO_In_Config();//SDA input for(i=0;i<8;i++ ) { BSP_I2Cx_SCL_LOW(); Delay_us(I2C_DELAY_COUNT); BSP_I2Cx_SCL_HIGH(); Delay_us(I2C_DELAY_COUNT/2); receive <<= 1; if(BSP_I2Cx_SDA_STATUS())receive++; Delay_us(I2C_DELAY_COUNT/2); } if(ack) I2C_Ack();//Send ACK else I2C_NAck(); BSP_I2Cx_SCL_LOW(); return receive; } //Get data at the AT24CXX //ReadAddr: start byteaddress //return: data #ifdef __GNUC__ #pragma GCC optimize ("O0") #elif defined(__ICCARM__) #pragma optimize=none #endif uint8_t I2C_ReadOneByte(uint16_t ReadAddr) { uint8_t temp=0; uint8_t test[10]={0}; I2C_Start(); test[0]=I2C_SendByte(EE_WR_ADDRESS(ReadAddr)); //send chip address and write command //test[0]=I2C_SendByte(BSP_EEROM_WR_ADDRESS); // test[0]++; test[1]=I2C_Wait_Ack(); test[1]++; //#if EEPROM>=32 #if 1 temp=(uint8_t)(ReadAddr>>8); test[2]=I2C_SendByte(temp);//Send address high byte test[2]++; test[3]=I2C_Wait_Ack(); test[3]++; #endif temp=(uint8_t)(ReadAddr&0xff); test[4]=I2C_SendByte(temp); //Send address low byte test[4]++; test[5]=I2C_Wait_Ack(); test[5]++; I2C_Start(); //test[6]=I2C_SendByte(EE_RD_ADDRESS(ReadAddr)); //Start recv test[6]=I2C_SendByte(BSP_EEROM_RD_ADDRESS); //Start recv test[6]++; test[7]=I2C_Wait_Ack(); test[7]++; temp=I2C_ReceiveByte(0); I2C_Stop();//stop test[8]++; return temp; } // write data into AT24CXX //WriteAddr : dst address //DataToWrite: data #ifdef __GNUC__ #pragma GCC optimize ("O0") #elif defined(__ICCARM__) #pragma optimize=none #endif void I2C_WriteOneByte(uint16_t WriteAddr,uint8_t DataToWrite) { uint8_t temp[10]={0}; BSP_EEROM_WRITE_ENABLE(); I2C_Start(); temp[0]=I2C_SendByte(EE_WR_ADDRESS(WriteAddr)); //send chip address and write command temp[0]++; temp[1]=I2C_Wait_Ack(); temp[1]++; //#if EEPROM>=32 #if 1 temp[2]=I2C_SendByte(WriteAddr>>8);//Send address high byte temp[2]++; temp[3]=I2C_Wait_Ack(); temp[3]++; #endif temp[4]=I2C_SendByte(WriteAddr&0xff); //Send address low byte temp[4]++; temp[5]=I2C_Wait_Ack(); temp[5]++; temp[6]=I2C_SendByte(DataToWrite); //Send data byte temp[6]++; temp[7]=I2C_Wait_Ack(); temp[7]++; I2C_Stop();// temp[8]++; BSP_EEROM_WRITE_FORBIDEN(); } #ifdef __GNUC__ #pragma GCC optimize ("O0") #elif defined(__ICCARM__) #pragma optimize=none #endif void I2C_WriteBuffer(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite) { BSP_EEROM_WRITE_ENABLE(); I2C_Start(); I2C_SendByte(EE_WR_ADDRESS(WriteAddr)); //send chip address and write command I2C_Wait_Ack(); //#if EEPROM>=32 #if 1 I2C_SendByte(WriteAddr>>8);//Send address high byte I2C_Wait_Ack(); #endif I2C_SendByte(WriteAddr&0xff); //Send address low byte I2C_Wait_Ack(); while(NumByteToWrite--) { I2C_SendByte(*pBuffer++); //Send data byte I2C_Wait_Ack(); } I2C_Stop();// BSP_EEROM_WRITE_FORBIDEN(); } #ifdef __GNUC__ #pragma GCC optimize ("O0") #elif defined(__ICCARM__) #pragma optimize=none #endif void I2C_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead) { I2C_Start(); I2C_SendByte(EE_WR_ADDRESS(ReadAddr)); //send chip address and write command //I2C_SendByte(BSP_EEROM_WR_ADDRESS); I2C_Wait_Ack(); //#if EEPROM>=32 #if 1 I2C_SendByte(ReadAddr>>8); //Send address high byte I2C_Wait_Ack(); #endif I2C_SendByte(ReadAddr&0xff); //Send address low byte I2C_Wait_Ack(); I2C_Start(); I2C_SendByte(EE_RD_ADDRESS(ReadAddr)); //Send readaddress while(NumByteToRead--) { I2C_Wait_Ack(); if(NumByteToRead == 1) *pBuffer++ = I2C_ReceiveByte(0); else *pBuffer++ = I2C_ReceiveByte(1); } I2C_NAck();//nACK I2C_Stop();// } #ifdef __GNUC__ #pragma GCC optimize ("O0") #elif defined(__ICCARM__) #pragma optimize=none #endif void I2C_WriteMultipleBytes(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite) { uint16_t i=0; uint16_t val=0; for(i=0;i