rtc.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. rtc.c
  3. PA0 TAMP2 Button
  4. PA2 TAMP3 Button
  5. */
  6. #include <string.h>
  7. #include "stm32l031xx.h"
  8. #include "delay.h"
  9. #include "u8g2.h"
  10. #include "rtc.h"
  11. /* read RTC register into bcd array */
  12. void rtc_register_to_bcd(rtc_t *rtc)
  13. {
  14. uint32_t r;
  15. int i;
  16. r = RTC->TR;
  17. i = 0;
  18. do
  19. {
  20. rtc->bcd[i] = r & 15;
  21. r >>= 4;
  22. i++;
  23. } while( i < 6 );
  24. rtc->bcd[1] &= 7; /* seconds */
  25. rtc->bcd[3] &= 7; /* minutes */
  26. rtc->bcd[5] &= 3; /* hours */
  27. r = RTC->DR;
  28. i = 6;
  29. do
  30. {
  31. rtc->bcd[i] = r & 15;
  32. r >>= 4;
  33. i++;
  34. } while( i < 12 );
  35. rtc->bcd[7] &= 3; /* days */
  36. rtc->bcd[9] &= 1; /* months */
  37. }
  38. static uint8_t rtc_bcd_to_uint8(rtc_t *rtc, int idx) U8G2_NOINLINE;
  39. static uint8_t rtc_bcd_to_uint8(rtc_t *rtc, int idx)
  40. {
  41. return rtc->bcd[idx+1]*10 + rtc->bcd[idx];
  42. }
  43. /* convert the content of the bcd array to the ymd&hms vars */
  44. void rtc_bcd_to_ymd_hms(rtc_t *rtc)
  45. {
  46. rtc->sec = rtc_bcd_to_uint8(rtc, 0);
  47. rtc->min = rtc_bcd_to_uint8(rtc, 2);
  48. rtc->hour = rtc_bcd_to_uint8(rtc, 4);
  49. rtc->day = rtc_bcd_to_uint8(rtc, 6);
  50. rtc->month = rtc_bcd_to_uint8(rtc, 8);
  51. rtc->year = rtc_bcd_to_uint8(rtc, 10);
  52. }
  53. void rtc_draw_time(rtc_t *rtc, u8g2_t *u8g2)
  54. {
  55. char s[10];
  56. strcpy(s, u8x8_u8toa(rtc->hour, 2));
  57. strcat(s, ":");
  58. strcat(s, u8x8_u8toa(rtc->min, 2));
  59. strcat(s, ":");
  60. strcat(s, u8x8_u8toa(rtc->sec, 2));
  61. u8g2_SetFont(u8g2, u8g2_font_helvB18_tn);
  62. u8g2_DrawStr(u8g2, 0,23, s);
  63. }