RX8025T.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include "string.h"
  2. #include "bsp.h"
  3. #include "I2C.h"
  4. #include "RX8025T.h"
  5. #include "utils.h"
  6. STDATETIME stDateTime;
  7. SPECIALFLAG specialFlag;
  8. //DEVSTATE devState;
  9. uint8_t pubRam[32];
  10. //BCD code to 2 dec code
  11. uint8_t BCD2DEC(uint8_t temp)
  12. {
  13. temp = (temp >> 4) * 10 + (temp & 0x0f);
  14. return temp;
  15. }
  16. //DEC data to BCD ocde
  17. uint8_t DEC2BCD(uint8_t temp)
  18. {
  19. temp = (temp / 10) * 16 + (temp % 10);
  20. return temp;
  21. }
  22. //I2C Send byte, basic data send mode
  23. static uint8_t RX_I2C_SendByte(uint8_t ByteData)
  24. {
  25. uint8_t ack;
  26. uint8_t i;
  27. I2C_GPIO_Out_Config();
  28. for (i = 0; i < 8; i++)
  29. {
  30. BSP_I2Cx_SCL_LOW();
  31. Delay_us(I2C_DELAY_COUNT/2);
  32. if (ByteData&0x80)
  33. BSP_I2Cx_SDA_HIGH();
  34. else
  35. BSP_I2Cx_SDA_LOW();
  36. Delay_us(I2C_DELAY_COUNT/2);
  37. ByteData <<= 1; //200us data is set up in SDA pin
  38. BSP_I2Cx_SCL_HIGH();
  39. Delay_us(I2C_DELAY_COUNT); //data is kept in SDA pin
  40. }
  41. // get ack bit
  42. 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
  43. Delay_us(I2C_DELAY_COUNT);
  44. I2C_GPIO_In_Config();
  45. BSP_I2Cx_SCL_HIGH();
  46. Delay_us(I2C_DELAY_COUNT);
  47. ack = BSP_I2Cx_SDA_STATUS(); //read ack bit
  48. BSP_I2Cx_SCL_LOW();
  49. return (ack); //return ack bit, low means there is an ack, high means not.
  50. }
  51. //I2C recv byte
  52. static uint8_t RX_I2C_ReceiveByte(uint8_t last_char)
  53. {
  54. uint8_t data=0, i;
  55. I2C_GPIO_In_Config();
  56. for (i = 0; i < 8; i++)
  57. {
  58. BSP_I2Cx_SCL_LOW();
  59. Delay_us(I2C_DELAY_COUNT);
  60. BSP_I2Cx_SCL_HIGH();
  61. Delay_us(I2C_DELAY_COUNT);
  62. data <<=1;
  63. if (BSP_I2Cx_SDA_STATUS()) data++;
  64. }
  65. BSP_I2Cx_SCL_LOW();
  66. Delay_us(I2C_DELAY_COUNT);
  67. I2C_GPIO_Out_Config();
  68. if (last_char) //The 9th bit, high means NACK; low means ACK and keep recv data
  69. BSP_I2Cx_SDA_HIGH();
  70. else
  71. BSP_I2Cx_SDA_LOW();
  72. Delay_us(I2C_DELAY_COUNT);
  73. BSP_I2Cx_SCL_HIGH();
  74. Delay_us(I2C_DELAY_COUNT);
  75. BSP_I2Cx_SCL_LOW();
  76. return data;
  77. }
  78. void Get8025( uint8_t addr, uint8_t *data,uint8_t counter)
  79. {
  80. #if defined(HAL_I2C_MODULE_ENABLED)
  81. HAL_StatusTypeDef status = HAL_ERROR;
  82. status = HAL_I2C_Mem_Read(&I2cHandle, BSP_RTC_RD_ADDRESS,addr, I2C_MEMADD_SIZE_8BIT, data, counter, 1000);
  83. //return status;
  84. #else
  85. uint8_t i;
  86. I2C_Start();
  87. RX_I2C_SendByte(BSP_RTC_WR_ADDRESS);
  88. RX_I2C_SendByte(addr);
  89. I2C_Start();
  90. RX_I2C_SendByte(BSP_RTC_RD_ADDRESS);
  91. for (i = 0; i < counter; i++)
  92. {
  93. if(i<counter - 1)
  94. *data++ = RX_I2C_ReceiveByte(0);
  95. else
  96. *data++ = RX_I2C_ReceiveByte(1);
  97. }
  98. /*for (i = 0; i < counter - 1 ; i++)
  99. *data++ = RX_I2C_ReceiveByte(0);
  100. *data++ = RX_I2C_ReceiveByte(1);*/
  101. I2C_Stop();
  102. #endif
  103. }
  104. void Set8025( uint8_t addr, uint8_t *data,uint8_t counter)
  105. {
  106. #if defined(HAL_I2C_MODULE_ENABLED)
  107. HAL_StatusTypeDef status = HAL_ERROR;
  108. status = HAL_I2C_Mem_Write(&I2cHandle, BSP_RTC_WR_ADDRESS, addr, I2C_MEMADD_SIZE_8BIT, data, counter, 1000);
  109. //return status;
  110. #else
  111. uint8_t i;
  112. I2C_Start();
  113. RX_I2C_SendByte(BSP_RTC_WR_ADDRESS);
  114. RX_I2C_SendByte(addr);
  115. for(i = 0; i <counter; i++)
  116. {
  117. RX_I2C_SendByte(*data++);
  118. }
  119. I2C_Stop();
  120. #endif
  121. }
  122. void Init8025(void)
  123. {
  124. uint8_t da[3];
  125. da[0] = 0x00;
  126. da[1] = 0x00;
  127. da[2] = RX8025T_CTRL_CSEL_2S0 | RX8025T_CTRL_UIE;
  128. Set8025(RTC8025T_Extension,da,3);
  129. memset(pubRam,0,3);
  130. Get8025(RTC8025T_Extension,pubRam,3);
  131. if(pubRam[2] != da[2])
  132. {
  133. specialFlag.I2C8025F = 1;
  134. }
  135. else
  136. {
  137. specialFlag.I2C8025F = 0;
  138. }
  139. }
  140. void TimerDataHandle(uint8_t* pDate)
  141. {
  142. stDateTime.second = BCD2DEC(pDate[0]);
  143. stDateTime.minute = BCD2DEC(pDate[1]);
  144. if(pDate[2]==0x24)
  145. pDate[2] = 0;
  146. stDateTime.hour = BCD2DEC(pDate[2]);
  147. if(pDate[3] == 0x01)
  148. stDateTime.week = 0;
  149. else if(pDate[3] == 0x02)
  150. stDateTime.week = 1;
  151. else if(pDate[3] == 0x04)
  152. stDateTime.week = 2;
  153. else if(pDate[3] == 0x08)
  154. stDateTime.week = 3;
  155. else if(pDate[3] == 0x10)
  156. stDateTime.week = 4;
  157. else if(pDate[3] == 0x20)
  158. stDateTime.week = 5;
  159. else if(pDate[3] == 0x40)
  160. stDateTime.week = 6;
  161. stDateTime.day = BCD2DEC(pDate[4]);
  162. stDateTime.month = BCD2DEC(pDate[5]);
  163. stDateTime.year = BCD2DEC(pDate[6]);
  164. }
  165. void RtcSetDateTime(STDATETIME *pTime)
  166. {
  167. uint8_t Timebuf[7];
  168. Timebuf[0] = DEC2BCD(pTime->second);
  169. Timebuf[1] = DEC2BCD(pTime->minute);
  170. Timebuf[2] = DEC2BCD(pTime->hour);
  171. Timebuf[3] = (0x01)<<(pTime->week);
  172. Timebuf[4] = DEC2BCD(pTime->day);
  173. Timebuf[5] = DEC2BCD(pTime->month);
  174. Timebuf[6] = DEC2BCD(pTime->year);
  175. Set8025(0,Timebuf,7); //BCD code in Timebuf
  176. TimerDataHandle(Timebuf);
  177. }
  178. /*
  179. void RtcSetLocalTime()
  180. {
  181. struct tm *now_ptm;
  182. time_t timep;
  183. STDATETIME set_time; //values of year/month/day/hour/minute/sencode is in BCD code
  184. timep = time(NULL); //get the current RTC time stamp
  185. timep += 8 * 3600; //convert RTC time stamp to BeiJing time
  186. now_ptm = gmtime(&timep); //convert to dec code
  187. set_time.second = now_ptm->tm_sec; //[0,59]
  188. set_time.minute = now_ptm->tm_min; //[0,59]
  189. set_time.hour = now_ptm->tm_hour; //[0,23]
  190. set_time.week = now_ptm->tm_wday; //[0,6], 0 is sunday
  191. set_time.date = now_ptm->tm_mday; //[1,31]
  192. set_time.month = now_ptm->tm_mon + 1; //[0,11], 0 is janury
  193. set_time.year = now_ptm->tm_year - 100;//tm is counted from 1900
  194. set_time.reserve = 0;
  195. RtcSetDateTime(&set_time);
  196. }*/
  197. void UpdateDateTime(void)
  198. {
  199. uint8_t Timebuf[7];
  200. Get8025(RTC8025_Second, Timebuf, 7); //BCD code in Timebuf
  201. TimerDataHandle(Timebuf);
  202. }
  203. uint8_t TestDevice(void)
  204. {
  205. uint8_t da=0x55;
  206. Set8025(RTC8025_Test,&da,1);
  207. da = 0;
  208. Get8025(RTC8025_Test,&da,1);
  209. return (da==0x55);
  210. }