speeduino/speeduino/rtc_common.ino

130 lines
2.5 KiB
Arduino
Raw Normal View History

#include "globals.h"
2021-10-12 18:42:52 -07:00
#include RTC_LIB_H //Defined in each boards .h file
2021-02-03 17:22:30 -08:00
#ifdef RTC_ENABLED
#include "rtc_common.h"
2021-10-12 18:42:52 -07:00
2021-01-21 21:16:30 -08:00
void initRTC()
{
#if defined(CORE_TEENSY35) || defined(CORE_TEENSY36)|| defined(CORE_TEENSY41)
2021-10-12 17:53:46 -07:00
setSyncProvider(getTeensy3Time);
#elif defined(CORE_STM32)
2021-10-12 18:42:52 -07:00
2021-10-12 17:53:46 -07:00
#endif
2021-01-21 21:16:30 -08:00
}
uint8_t rtc_getSecond()
{
uint8_t tempSecond = 0;
#ifdef RTC_ENABLED
#if defined(CORE_TEENSY)
tempSecond = second();
#elif defined(CORE_STM32)
tempSecond = rtc.getSeconds();
#endif
#endif
return tempSecond;
}
uint8_t rtc_getMinute()
{
uint8_t tempMinute = 0;
#ifdef RTC_ENABLED
#if defined(CORE_TEENSY)
tempMinute = minute();
#elif defined(CORE_STM32)
tempMinute = rtc.getMinutes();
#endif
#endif
return tempMinute;
}
uint8_t rtc_getHour()
{
uint8_t tempHour = 0;
#ifdef RTC_ENABLED
#if defined(CORE_TEENSY)
tempHour = hour();
#elif defined(CORE_STM32)
tempHour = rtc.getHours();
#endif
#endif
return tempHour;
}
uint8_t rtc_getDay()
{
uint8_t tempDay = 0;
#ifdef RTC_ENABLED
#if defined(CORE_TEENSY)
tempDay = day();
#elif defined(CORE_STM32)
tempDay = rtc.getDay();
#endif
#endif
return tempDay;
}
uint8_t rtc_getDOW()
{
uint8_t dow = 0;
#ifdef RTC_ENABLED
#if defined(CORE_TEENSY)
dow = weekday();
#elif defined(CORE_STM32)
dow = rtc.getWeekDay();
#endif
#endif
return dow;
}
uint8_t rtc_getMonth()
{
uint8_t tempMonth = 0;
#ifdef RTC_ENABLED
#if defined(CORE_TEENSY)
tempMonth = month();
#elif defined(CORE_STM32)
tempMonth = rtc.getMonth();
#endif
#endif
return tempMonth;
}
uint16_t rtc_getYear()
{
uint16_t tempYear = 0;
#ifdef RTC_ENABLED
#if defined(CORE_TEENSY)
tempYear = year();
#elif defined(CORE_STM32)
//year in stm32 rtc is a byte. So add year 2000 to make it correct
tempYear = (2000+rtc.getYear());
2021-01-21 21:16:30 -08:00
#endif
#endif
return tempYear;
}
void rtc_setTime(byte second, byte minute, byte hour, byte day, byte month, uint16_t year)
{
#ifdef RTC_ENABLED
#if defined(CORE_TEENSY)
2021-12-09 19:26:13 -08:00
setTime(hour, minute, second, day, month, year);
Teensy3Clock.set(now());
2021-01-21 21:16:30 -08:00
#elif defined(CORE_STM32)
2022-06-26 18:54:47 -07:00
//If RTC time has not been set earlier (no battery etc.) we need to stop the RTC and restart it with LSE_CLOCK to have accurate RTC.
if (!rtc.isTimeSet()) {
rtc.end();
rtc.setClockSource(STM32RTC::LSE_CLOCK);
rtc.begin();
}
2021-01-21 21:16:30 -08:00
rtc.setTime(hour, minute, second);
//year in stm32 rtc is a byte. so subtract year 2000 to fit
rtc.setDate(day, month, (year-2000));
2021-01-21 21:16:30 -08:00
#endif
#endif
2021-02-03 17:22:30 -08:00
}
#endif