My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rtc176x.c 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*------------------------------------------------------------------------/
  2. / LPC176x RTC control module
  3. /-------------------------------------------------------------------------/
  4. /
  5. / Copyright (C) 2011, ChaN, all right reserved.
  6. /
  7. / * This software is a free software and there is NO WARRANTY.
  8. / * No restriction on use. You can use, modify and redistribute it for
  9. / personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
  10. / * Redistributions of source code must retain the above copyright notice.
  11. /
  12. /-------------------------------------------------------------------------*/
  13. #include "rtc176x.h"
  14. int rtc_initialize (void)
  15. {
  16. /* Enable PCLK to the RTC */
  17. __set_PCONP(PCRTC, 1);
  18. /* Start RTC with external XTAL */
  19. RTC_CCR = 0x11;
  20. return 1;
  21. }
  22. int rtc_gettime (RTC *rtc) /* 1:RTC valid, 0:RTC volatiled */
  23. {
  24. DWORD d, t;
  25. do {
  26. t = RTC_CTIME0;
  27. d = RTC_CTIME1;
  28. } while (t != RTC_CTIME0 || d != RTC_CTIME1);
  29. if (RTC_AUX & _BV(4)) { /* If power fail has been detected, return default time. */
  30. rtc->sec = 0; rtc->min = 0; rtc->hour = 0;
  31. rtc->wday = 0; rtc->mday = 1; rtc->month = 1; rtc->year = 2014;
  32. return 0;
  33. }
  34. rtc->sec = t & 63;
  35. rtc->min = (t >> 8) & 63;
  36. rtc->hour = (t >> 16) & 31;
  37. rtc->wday = (t >> 24) & 7;
  38. rtc->mday = d & 31;
  39. rtc->month = (d >> 8) & 15;
  40. rtc->year = (d >> 16) & 4095;
  41. return 1;
  42. }
  43. int rtc_settime (const RTC *rtc)
  44. {
  45. RTC_CCR = 0x12; /* Stop RTC */
  46. /* Update RTC registers */
  47. RTC_SEC = rtc->sec;
  48. RTC_MIN = rtc->min;
  49. RTC_HOUR = rtc->hour;
  50. RTC_DOW = rtc->wday;
  51. RTC_DOM = rtc->mday;
  52. RTC_MONTH = rtc->month;
  53. RTC_YEAR = rtc->year;
  54. RTC_AUX = _BV(4); /* Clear power fail flag */
  55. RTC_CCR = 0x11; /* Restart RTC, Disable calibration feature */
  56. return 1;
  57. }
  58. DWORD get_fattime (void) {
  59. RTC rtc;
  60. /* Get local time */
  61. rtc_gettime(&rtc);
  62. /* Pack date and time into a DWORD variable */
  63. return ((DWORD)(rtc.year - 1980) << 25)
  64. | ((DWORD)rtc.month << 21)
  65. | ((DWORD)rtc.mday << 16)
  66. | ((DWORD)rtc.hour << 11)
  67. | ((DWORD)rtc.min << 5)
  68. | ((DWORD)rtc.sec >> 1);
  69. }