RTC.c 2.5 KB

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