rtc.c 1.4 KB

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