RTC.c 2.5 KB

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