RTC. API changes.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@3405 35acf78f-673a-0410-8e92-d51de3d6d3f4
master
barthess 2011-09-25 10:03:59 +00:00
parent 5edc2c8b69
commit 41fd0fb5fb
5 changed files with 96 additions and 49 deletions

View File

@ -74,10 +74,20 @@ extern "C" {
rtccb_t secondcb, rtccb_t alarmcb); rtccb_t secondcb, rtccb_t alarmcb);
#endif /* RTC_SUPPORTS_CALLBACKS */ #endif /* RTC_SUPPORTS_CALLBACKS */
void rtcSetTime(uint32_t tv_sec); void rtcSetTime(RTCDateTime *timespec);
uint32_t rtcGetTime(uint16_t *msec); void rtcGetTime(RTCDateTime *timespec);
void rtcSetAlarm(uint32_t tv_alarm);
uint32_t rtcGetAlarm(void);
void rtcSetAlarm(RTCDateTime *timespec);
void rtcGetAlarm(RTCDateTime *timespec);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -245,18 +245,20 @@ void rtc_lld_set_callback(RTCDriver *rtcp, rtccb_t overflowcb,
/** /**
* @brief Set current time. * @brief Set current time.
* *
* @param[in] tv_sec time value in UNIX notation. * @param[in] timespec pointer to variable storing time.
* *
* @note Fractional part will be silently ignored. There is no possibility
* to change it on STM32F1xx platform.
* @notapi * @notapi
*/ */
void rtc_lld_set_time(uint32_t tv_sec){ void rtc_lld_set_time(RTCDateTime *timespec){
while(!(RTC->CRL & RTC_CRL_RTOFF)) while(!(RTC->CRL & RTC_CRL_RTOFF))
; ;
RTC->CRL |= RTC_CRL_CNF; /* switch on configure mode */ RTC->CRL |= RTC_CRL_CNF; /* switch on configure mode */
RTC->CNTH = (uint16_t)((tv_sec >> 16) & 0xFFFF); /* write time */ RTC->CNTH = (uint16_t)((timespec->tv_sec >> 16) & 0xFFFF);
RTC->CNTL = (uint16_t)(tv_sec & 0xFFFF); RTC->CNTL = (uint16_t)(timespec->tv_sec & 0xFFFF);
RTC->CRL &= ~RTC_CRL_CNF; /* switch off configure mode */ RTC->CRL &= ~RTC_CRL_CNF; /* switch off configure mode */
while(!(RTC->CRL & RTC_CRL_RTOFF)) /* wait for completion */ while(!(RTC->CRL & RTC_CRL_RTOFF)) /* wait for completion */
@ -264,36 +266,41 @@ void rtc_lld_set_time(uint32_t tv_sec){
} }
/** /**
* @brief Return return seconds since UNIX epoch. * @brief Get current time.
* *
* @param[in] msec pointer to variable for storing fractional part of * @param[in] msec pointer to variable for storing fractional part of
* time (milliseconds). * time (milliseconds).
* *
* @notapi * @notapi
*/ */
inline uint32_t rtc_lld_get_time(uint16_t *msec){ inline void rtc_lld_get_time(RTCDateTime *timespec){
uint32_t time_frac = 0; uint32_t time_frac = 0;
if(msec != NULL){
time_frac = (((uint32_t)RTC->DIVH) << 16) + (RTC->DIVL); time_frac = (((uint32_t)RTC->DIVH) << 16) + (RTC->DIVL);
*msec = (uint16_t)(((STM32_LSECLK - time_frac) * 1000) / STM32_LSECLK);
} timespec->tv_msec = (uint16_t)(((STM32_LSECLK - time_frac) * 1000) / STM32_LSECLK);
return ((RTC->CNTH << 16) + RTC->CNTL); timespec->tv_sec = (RTC->CNTH << 16) + RTC->CNTL;
} }
/** /**
* @brief Set alarm date in UNIX notation. * @brief Set alarm time.
*
* @param[in] timespec pointer to variable storing time of alarm.
*
* @note Fractional part will be silently ignored. There is no possibility
* to change it on STM32F1xx platform.
*
* @note Default value after BKP domain reset is 0xFFFFFFFF * @note Default value after BKP domain reset is 0xFFFFFFFF
* *
* @notapi * @notapi
*/ */
void rtc_lld_set_alarm(uint32_t tv_alarm){ void rtc_lld_set_alarm(RTCDateTime *timespec){
while(!(RTC->CRL & RTC_CRL_RTOFF)) while(!(RTC->CRL & RTC_CRL_RTOFF))
; ;
RTC->CRL |= RTC_CRL_CNF; /* switch on configure mode */ RTC->CRL |= RTC_CRL_CNF; /* switch on configure mode */
RTC->ALRH = (uint16_t)((tv_alarm >> 16) & 0xFFFF); /* write time */ RTC->ALRH = (uint16_t)((timespec->tv_sec >> 16) & 0xFFFF);
RTC->ALRL = (uint16_t)(tv_alarm & 0xFFFF); RTC->ALRL = (uint16_t)(timespec->tv_sec & 0xFFFF);
RTC->CRL &= ~RTC_CRL_CNF; /* switch off configure mode */ RTC->CRL &= ~RTC_CRL_CNF; /* switch off configure mode */
#if !(RTC_SUPPORTS_CALLBACKS) #if !(RTC_SUPPORTS_CALLBACKS)
@ -306,13 +313,19 @@ void rtc_lld_set_alarm(uint32_t tv_alarm){
} }
/** /**
* @brief Get current alarm date in UNIX notation. * @brief Get current alarm time.
*
* @param[in] timespec pointer to variable storing time of alarm.
*
* @note Fractional part will be silently ignored. There is no possibility
* to change it on STM32F1xx platform.
*
* @note Default value after BKP domain reset is 0xFFFFFFFF * @note Default value after BKP domain reset is 0xFFFFFFFF
* *
* @notapi * @notapi
*/ */
inline uint32_t rtc_lld_get_alarm(void){ inline void rtc_lld_get_alarm(RTCDateTime *timespec){
return ((RTC->ALRH << 16) + RTC->ALRL); timespec->tv_sec = ((RTC->ALRH << 16) + RTC->ALRL);
} }

View File

@ -59,6 +59,21 @@
/* Driver data structures and types. */ /* Driver data structures and types. */
/*===========================================================================*/ /*===========================================================================*/
typedef struct {
uint32_t tv_sec;
uint32_t tv_msec;
}RTCDateTime;
/** /**
* @brief Structure representing an RTC driver. * @brief Structure representing an RTC driver.
* @note This driver if dummy when callbacks disabled. * @note This driver if dummy when callbacks disabled.
@ -99,10 +114,12 @@ extern "C" {
void rtc_lld_init(void); void rtc_lld_init(void);
void rtc_lld_set_callback(RTCDriver *rtcp, rtccb_t overflow_cb, void rtc_lld_set_callback(RTCDriver *rtcp, rtccb_t overflow_cb,
rtccb_t second_cb, rtccb_t alarm_cb); rtccb_t second_cb, rtccb_t alarm_cb);
void rtc_lld_set_time(uint32_t tv_sec);
uint32_t rtc_lld_get_time(uint16_t *msec); void rtc_lld_set_time(RTCDateTime *timespec);
uint32_t rtc_lld_get_alarm(void); void rtc_lld_get_time(RTCDateTime *timespec);
void rtc_lld_set_alarm(uint32_t);
void rtc_lld_get_alarm(RTCDateTime *timespec);
void rtc_lld_set_alarm(RTCDateTime *timespec);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -15,11 +15,12 @@ PLATFORMSRC = ${CHIBIOS}/os/hal/platforms/STM32F1xx/hal_lld.c \
${CHIBIOS}/os/hal/platforms/STM32/GPIOv1/pal_lld.c \ ${CHIBIOS}/os/hal/platforms/STM32/GPIOv1/pal_lld.c \
${CHIBIOS}/os/hal/platforms/STM32/DMAv1/stm32_dma.c \ ${CHIBIOS}/os/hal/platforms/STM32/DMAv1/stm32_dma.c \
${CHIBIOS}/os/hal/platforms/STM32/USBv1/usb_lld.c \ ${CHIBIOS}/os/hal/platforms/STM32/USBv1/usb_lld.c \
${CHIBIOS}/os/hal/platforms/STM32/rtc_lld.c ${CHIBIOS}/os/hal/platforms/STM32/RTCv1/rtc_lld.c
# Required include directories # Required include directories
PLATFORMINC = ${CHIBIOS}/os/hal/platforms/STM32F1xx \ PLATFORMINC = ${CHIBIOS}/os/hal/platforms/STM32F1xx \
${CHIBIOS}/os/hal/platforms/STM32 \ ${CHIBIOS}/os/hal/platforms/STM32 \
${CHIBIOS}/os/hal/platforms/STM32/GPIOv1 \ ${CHIBIOS}/os/hal/platforms/STM32/GPIOv1 \
${CHIBIOS}/os/hal/platforms/STM32/DMAv1 \ ${CHIBIOS}/os/hal/platforms/STM32/DMAv1 \
${CHIBIOS}/os/hal/platforms/STM32/USBv1 ${CHIBIOS}/os/hal/platforms/STM32/USBv1 \
${CHIBIOS}/os/hal/platforms/STM32/RTCv1

View File

@ -83,36 +83,42 @@ void rtcSetCallback(RTCDriver *rtcp, rtccb_t overflowcb,
/** /**
* @brief Set current time. * @brief Set current time.
* @param[in] tv_sec - time value in UNIX notation.
*/
void rtcSetTime(uint32_t tv_sec){
rtc_lld_set_time(tv_sec);
}
/**
* @brief Return return seconds since UNIX epoch.
* *
* @param[in] msec pointer to variable for storing fractional part of * @param[in] timespec pointer to variable storing time.
* time (milliseconds). */
void rtcSetTime(RTCDateTime *timespec){
chDbgCheck((timespec != NULL), "rtcSetTime");
rtc_lld_set_time(timespec);
}
/**
* @brief Get current time.
* *
* @notapi * @param[in] timespec pointer to variable storing time.
*/ */
inline uint32_t rtcGetTime(uint16_t *msec){ void rtcGetTime(RTCDateTime *timespec){
return rtc_lld_get_time(msec); chDbgCheck((timespec != NULL), "rtcGetTime");
rtc_lld_get_time(timespec);
} }
/** /**
* @brief Set alarm date in UNIX notation. * @brief Set alarm time.
*
* @param[in] timespec pointer to variable storing time of alarm.
*/ */
void rtcSetAlarm(uint32_t tv_alarm){ void rtcSetAlarm(RTCDateTime *timespec){
rtc_lld_set_alarm(tv_alarm); chDbgCheck((timespec != NULL), "rtcSetAlarm");
rtc_lld_set_alarm(timespec);
} }
/** /**
* @brief Get current alarm date in UNIX notation. * @brief Get current alarm.
*
* @param[in] timespec pointer to variable to store alarm time.
*/ */
inline uint32_t rtcGetAlarm(void){ void rtcGetAlarm(RTCDateTime *timespec){
return rtc_lld_get_alarm(); chDbgCheck((timespec != NULL), "rtcGetAlarm");
rtc_lld_get_alarm(timespec);
} }
#endif /* HAL_USE_RTC */ #endif /* HAL_USE_RTC */