| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- #include "string.h"
- #include "bsp.h"
- #include "I2C.h"
- #include "RX8025T.h"
- #include "utils.h"
- STDATETIME stDateTime;
- SPECIALFLAG specialFlag;
- //DEVSTATE devState;
- uint8_t pubRam[32];
- //BCD code to 2 dec code
- uint8_t BCD2DEC(uint8_t temp)
- {
- temp = (temp >> 4) * 10 + (temp & 0x0f);
- return temp;
- }
- //DEC data to BCD ocde
- uint8_t DEC2BCD(uint8_t temp)
- {
- temp = (temp / 10) * 16 + (temp % 10);
- return temp;
- }
- //I2C Send byte, basic data send mode
- static uint8_t RX_I2C_SendByte(uint8_t ByteData)
- {
- uint8_t ack;
- uint8_t i;
- I2C_GPIO_Out_Config();
- for (i = 0; i < 8; i++)
- {
- BSP_I2Cx_SCL_LOW();
- Delay_us(I2C_DELAY_COUNT/2);
- if (ByteData&0x80)
- BSP_I2Cx_SDA_HIGH();
- else
- BSP_I2Cx_SDA_LOW();
- Delay_us(I2C_DELAY_COUNT/2);
- ByteData <<= 1; //200us data is set up in SDA pin
- BSP_I2Cx_SCL_HIGH();
- Delay_us(I2C_DELAY_COUNT); //data is kept in SDA pin
- }
- // get ack bit
- BSP_I2Cx_SCL_LOW(); //set SCL before set SDA input,if SCL is high, the SDA may turn to high and this is the I2CSTOP, the sendByte fails
- Delay_us(I2C_DELAY_COUNT);
- I2C_GPIO_In_Config();
- BSP_I2Cx_SCL_HIGH();
- Delay_us(I2C_DELAY_COUNT);
- ack = BSP_I2Cx_SDA_STATUS(); //read ack bit
- BSP_I2Cx_SCL_LOW();
- return (ack); //return ack bit, low means there is an ack, high means not.
- }
- //I2C recv byte
- static uint8_t RX_I2C_ReceiveByte(uint8_t last_char)
- {
- uint8_t data=0, i;
- I2C_GPIO_In_Config();
- for (i = 0; i < 8; i++)
- {
- BSP_I2Cx_SCL_LOW();
- Delay_us(I2C_DELAY_COUNT);
- BSP_I2Cx_SCL_HIGH();
- Delay_us(I2C_DELAY_COUNT);
- data <<=1;
- if (BSP_I2Cx_SDA_STATUS()) data++;
- }
- BSP_I2Cx_SCL_LOW();
- Delay_us(I2C_DELAY_COUNT);
- I2C_GPIO_Out_Config();
- if (last_char) //The 9th bit, high means NACK; low means ACK and keep recv data
- BSP_I2Cx_SDA_HIGH();
- else
- BSP_I2Cx_SDA_LOW();
- Delay_us(I2C_DELAY_COUNT);
- BSP_I2Cx_SCL_HIGH();
- Delay_us(I2C_DELAY_COUNT);
- BSP_I2Cx_SCL_LOW();
- return data;
- }
- void Get8025( uint8_t addr, uint8_t *data,uint8_t counter)
- {
- #if defined(HAL_I2C_MODULE_ENABLED)
- HAL_StatusTypeDef status = HAL_ERROR;
- status = HAL_I2C_Mem_Read(&I2cHandle, BSP_RTC_RD_ADDRESS,addr, I2C_MEMADD_SIZE_8BIT, data, counter, 1000);
- //return status;
- #else
- uint8_t i;
- I2C_Start();
- RX_I2C_SendByte(BSP_RTC_WR_ADDRESS);
- RX_I2C_SendByte(addr);
- I2C_Start();
- RX_I2C_SendByte(BSP_RTC_RD_ADDRESS);
- for (i = 0; i < counter; i++)
- {
- if(i<counter - 1)
- *data++ = RX_I2C_ReceiveByte(0);
- else
- *data++ = RX_I2C_ReceiveByte(1);
- }
- /*for (i = 0; i < counter - 1 ; i++)
- *data++ = RX_I2C_ReceiveByte(0);
- *data++ = RX_I2C_ReceiveByte(1);*/
- I2C_Stop();
- #endif
- }
- void Set8025( uint8_t addr, uint8_t *data,uint8_t counter)
- {
- #if defined(HAL_I2C_MODULE_ENABLED)
- HAL_StatusTypeDef status = HAL_ERROR;
- status = HAL_I2C_Mem_Write(&I2cHandle, BSP_RTC_WR_ADDRESS, addr, I2C_MEMADD_SIZE_8BIT, data, counter, 1000);
- //return status;
- #else
- uint8_t i;
- I2C_Start();
- RX_I2C_SendByte(BSP_RTC_WR_ADDRESS);
- RX_I2C_SendByte(addr);
- for(i = 0; i <counter; i++)
- {
- RX_I2C_SendByte(*data++);
- }
- I2C_Stop();
- #endif
- }
- void Init8025(void)
- {
- uint8_t da[3];
- da[0] = 0x00;
- da[1] = 0x00;
- da[2] = RX8025T_CTRL_CSEL_2S0 | RX8025T_CTRL_UIE;
- Set8025(RTC8025T_Extension,da,3);
- memset(pubRam,0,3);
- Get8025(RTC8025T_Extension,pubRam,3);
- if(pubRam[2] != da[2])
- {
- specialFlag.I2C8025F = 1;
- }
- else
- {
- specialFlag.I2C8025F = 0;
- }
- }
- void TimerDataHandle(uint8_t* pDate)
- {
- stDateTime.second = BCD2DEC(pDate[0]);
- stDateTime.minute = BCD2DEC(pDate[1]);
- if(pDate[2]==0x24)
- pDate[2] = 0;
- stDateTime.hour = BCD2DEC(pDate[2]);
- if(pDate[3] == 0x01)
- stDateTime.week = 0;
- else if(pDate[3] == 0x02)
- stDateTime.week = 1;
- else if(pDate[3] == 0x04)
- stDateTime.week = 2;
- else if(pDate[3] == 0x08)
- stDateTime.week = 3;
- else if(pDate[3] == 0x10)
- stDateTime.week = 4;
- else if(pDate[3] == 0x20)
- stDateTime.week = 5;
- else if(pDate[3] == 0x40)
- stDateTime.week = 6;
- stDateTime.day = BCD2DEC(pDate[4]);
- stDateTime.month = BCD2DEC(pDate[5]);
- stDateTime.year = BCD2DEC(pDate[6]);
- }
- void RtcSetDateTime(STDATETIME *pTime)
- {
- uint8_t Timebuf[7];
- Timebuf[0] = DEC2BCD(pTime->second);
- Timebuf[1] = DEC2BCD(pTime->minute);
- Timebuf[2] = DEC2BCD(pTime->hour);
- Timebuf[3] = (0x01)<<(pTime->week);
- Timebuf[4] = DEC2BCD(pTime->day);
- Timebuf[5] = DEC2BCD(pTime->month);
- Timebuf[6] = DEC2BCD(pTime->year);
- Set8025(0,Timebuf,7); //BCD code in Timebuf
- TimerDataHandle(Timebuf);
- }
- /*
- void RtcSetLocalTime()
- {
- struct tm *now_ptm;
- time_t timep;
- STDATETIME set_time; //values of year/month/day/hour/minute/sencode is in BCD code
- timep = time(NULL); //get the current RTC time stamp
- timep += 8 * 3600; //convert RTC time stamp to BeiJing time
- now_ptm = gmtime(&timep); //convert to dec code
- set_time.second = now_ptm->tm_sec; //[0,59]
- set_time.minute = now_ptm->tm_min; //[0,59]
- set_time.hour = now_ptm->tm_hour; //[0,23]
- set_time.week = now_ptm->tm_wday; //[0,6], 0 is sunday
- set_time.date = now_ptm->tm_mday; //[1,31]
- set_time.month = now_ptm->tm_mon + 1; //[0,11], 0 is janury
- set_time.year = now_ptm->tm_year - 100;//tm is counted from 1900
- set_time.reserve = 0;
- RtcSetDateTime(&set_time);
- }*/
- void UpdateDateTime(void)
- {
- uint8_t Timebuf[7];
- Get8025(RTC8025_Second, Timebuf, 7); //BCD code in Timebuf
- TimerDataHandle(Timebuf);
- }
- uint8_t TestDevice(void)
- {
- uint8_t da=0x55;
- Set8025(RTC8025_Test,&da,1);
- da = 0;
- Get8025(RTC8025_Test,&da,1);
- return (da==0x55);
- }
|