#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(isecond); 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); }