RTC.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * V1.0
  3. */
  4. /*
  5. * RTC.c
  6. *
  7. * Created on: 15 janv. 2019
  8. * Author: e_rcluze
  9. */
  10. #include "RTC.h"
  11. I2C_HandleTypeDef *I2C_RTC;
  12. /** Private Fonction*/
  13. HAL_StatusTypeDef readRTC(uint8_t reg, uint8_t *tab, int len) {
  14. HAL_StatusTypeDef res = HAL_I2C_Master_Transmit(I2C_RTC, RTC_ADD, &reg, 1, 10000);
  15. if(res != HAL_OK)
  16. return res;
  17. return HAL_I2C_Master_Receive(I2C_RTC, RTC_ADD, tab, len, 1000);
  18. }
  19. HAL_StatusTypeDef writeRTC(uint8_t reg, uint8_t *tab, int len) {
  20. uint8_t t[len + 1];
  21. t[0] = reg;
  22. for (int i = 0; i < len; i++)
  23. t[i + 1] = tab[i];
  24. return HAL_I2C_Master_Transmit(I2C_RTC, RTC_ADD, t, len + 1, 1000);
  25. }
  26. /** Public Fonction*/
  27. void initRTC(I2C_HandleTypeDef *typ) {
  28. I2C_RTC = typ;
  29. }
  30. HAL_StatusTypeDef readHeure(Heure *heure) {
  31. uint8_t tab[2];
  32. HAL_StatusTypeDef res = readRTC(MINUTES, tab, 2);
  33. heure->minute = (((tab[0] & 0x70) >> 4) * 10) + (tab[0] & 0X0F);
  34. heure->heure = (((tab[1] & 0X30) >> 4) * 10) + (tab[1] & 0X0F);
  35. return res;
  36. }
  37. /** Public Fonction*/
  38. HAL_StatusTypeDef readDate(Date *date) {
  39. uint8_t tab[7];
  40. HAL_StatusTypeDef res = readRTC(SECOND, tab, 7);
  41. date->second = (((tab[0] & 0X70) >> 4) * 10) + (tab[0] & 0X0F);
  42. date->minute = (((tab[1] & 0x70) >> 4) * 10) + (tab[1] & 0X0F);
  43. date->heure = (((tab[2] & 0X30) >> 4) * 10) + (tab[2] & 0X0F);
  44. date->jour = (((tab[4] & 0X30) >> 4) * 10) + (tab[4] & 0X0F);
  45. date->mois = (((tab[5] & 0X10) >> 4) * 10) + (tab[5] & 0X0F);
  46. date->annee = ((tab[6] >> 4) * 10) + (tab[6] & 0X0F);
  47. return res;
  48. }
  49. HAL_StatusTypeDef writeDate(Date *date) {
  50. uint8_t tab[7];
  51. tab[0] = ((date->second / 10) << 4) + (date->second % 10);
  52. tab[1] = ((date->minute / 10) << 4) + (date->minute % 10);
  53. tab[2] = ((date->heure / 10) << 4) + (date->heure % 10);
  54. tab[3] = date->joursemaine;
  55. tab[4] = ((date->jour / 10) << 4) + (date->jour % 10);
  56. tab[5] = ((date->mois / 10) << 4) + (date->mois % 10);
  57. tab[6] = ((date->annee / 10) << 4) + (date->annee % 10);
  58. return writeRTC(SECOND, tab, 7);
  59. }
  60. HAL_StatusTypeDef redemareRTC() {
  61. uint8_t pt[1];
  62. HAL_StatusTypeDef res = readRTC(ALARM_HOURS, pt, 1);
  63. if (res != HAL_OK)
  64. return res;
  65. pt[0] = pt[0] & !0x40;
  66. return writeRTC(ALARM_HOURS, pt, 1);
  67. }
  68. void testRTC() {
  69. Date d;
  70. d.annee = 2020;
  71. d.mois = 1;
  72. d.jour = 20;
  73. d.joursemaine = 1;
  74. d.heure = 14;
  75. d.minute = 0;
  76. d.second = 0;
  77. writeDate(&d);
  78. redemareRTC();
  79. HAL_Delay(2000);
  80. readDate(&d);
  81. HAL_Delay(1);
  82. }
  83. uint16_t HeureEnMinute() {
  84. Heure h;
  85. readHeure(&h);
  86. return h.minute+h.heure*60;
  87. }
  88. void testBatteryRTC() {
  89. redemareRTC();
  90. Date d;
  91. readDate(&d);
  92. HAL_Delay(1);
  93. }