| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- #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<NumByteToWrite;i++)
- {
- I2C_WriteOneByte(WriteAddr+i,pBuffer[i]);
- Delay_ms(EEROM_DELAY);
- val = I2C_ReadOneByte(WriteAddr+i);
- if(val==pBuffer[i])
- {
- val=0;
- }
- }
- }
- #ifdef __GNUC__
- #pragma GCC optimize ("O0")
- #elif defined(__ICCARM__)
- #pragma optimize=none
- #endif
- void I2C_ReadMultipleBytes(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)
- {
- uint16_t i=0;
- for(i=0;i<NumByteToRead;i++)
- {
- pBuffer[i] = I2C_ReadOneByte(ReadAddr+i);
- }
- }
- void TestEEROM(void)
- {
- uint32_t startTick, curTick;
- int n=0;
- startTick = HAL_GetTick();
- for(n=0; n< EEROM_TEST_COUNT; n++)
- {
- //I2C_WriteOneByte(EEROM_TEST_ADDR+n, (EEROM_TEST_COUNT - n) & 0xff);
- I2C_WriteOneByte(EEROM_TEST_ADDR + n, (n & 0xff));
- //I2C_WriteOneByte(EEROM_TEST_ADDR+n, 0xa5);
- Delay_ms(3);
- }
- curTick = HAL_GetTick();
- //n=sprintf((char*)debugBuffer, "EEROM write time: %d ~ %d\r\n\r\n", startTick, curTick);
- //ReportMessage(debugBuffer,n);
- for(n=0; n<EEROM_TEST_COUNT; n++)
- {
- //debugBuffer[n] = I2C_ReadOneByte(EEROM_TEST_ADDR+n);
- I2C_ReadOneByte(EEROM_TEST_ADDR+n);
- //DebugRun();
- }
- }
|