update RTC implementation to include the various alarm related stuff recently added to STM32 arch

This commit is contained in:
ziggurat29
2016-05-05 11:16:00 -05:00
parent dedcbeba2e
commit 273680a6e9
3 changed files with 729 additions and 53 deletions
+32 -15
View File
@@ -43,6 +43,7 @@
#ifndef __ARCH_ARM_SRC_STM32L4_STM32L4_RTC_H
#define __ARCH_ARM_SRC_STM32L4_STM32L4_RTC_H
#include <time.h>
#include <nuttx/config.h>
#include "chip.h"
@@ -62,9 +63,30 @@
#ifndef __ASSEMBLY__
#ifdef CONFIG_RTC_ALARM
/* The form of an alarm callback */
typedef CODE void (*alarmcb_t)(void);
typedef CODE void (*alm_callback_t)(FAR void *arg, unsigned int alarmid);
enum alm_id_e
{
RTC_ALARMA = 0, /* RTC ALARM A */
RTC_ALARMB, /* RTC ALARM B */
RTC_ALARM_LAST
};
/* Structure used to pass parmaters to set an alarm */
struct alm_setalarm_s
{
int as_id; /* enum alm_id_e */
struct tm as_time; /* Alarm expiration time */
alm_callback_t as_cb; /* Callback (if non-NULL) */
FAR void *as_arg; /* Argument for callback */
};
#endif /* CONFIG_RTC_ALARM */
/****************************************************************************
* Public Data
@@ -93,8 +115,7 @@ extern "C"
* during initialization to set up the system time when CONFIG_RTC and
* CONFIG_RTC_DATETIME are selected (and CONFIG_RTC_HIRES is not).
*
* NOTE: Some date/time RTC hardware is capability of sub-second accuracy.
* That sub-second accuracy is returned through 'nsec'.
* NOTE: The sub-second accuracy is returned through 'nsec'.
*
* Input Parameters:
* tp - The location to return the high resolution time value.
@@ -130,43 +151,39 @@ struct tm;
int stm32l4_rtc_setdatetime(FAR const struct tm *tp);
#endif
#ifdef CONFIG_RTC_ALARM
/****************************************************************************
* Name: stm32l4_rtc_setalarm
*
* Description:
* Set up an alarm.
* Set an alarm to an asbolute time using associated hardware.
*
* Input Parameters:
* tp - the time to set the alarm
* callback - the function to call when the alarm expires.
* alminfo - Information about the alarm configuration.
*
* Returned Value:
* Zero (OK) on success; a negated errno on failure
*
****************************************************************************/
#ifdef CONFIG_RTC_ALARM
struct timespec;
int stm32l4_rtc_setalarm(FAR const struct timespec *tp, alarmcb_t callback);
#endif
int stm32l4_rtc_setalarm(FAR struct alm_setalarm_s *alminfo);
/****************************************************************************
* Name: stm32l4_rtc_cancelalarm
*
* Description:
* Cancel a pending alarm alarm
* Cancel an alaram.
*
* Input Parameters:
* none
* alarmid - Identifies the alarm to be cancelled
*
* Returned Value:
* Zero (OK) on success; a negated errno on failure
*
****************************************************************************/
#ifdef CONFIG_RTC_ALARM
int stm32l4_rtc_cancelalarm(void);
#endif
int stm32l4_rtc_cancelalarm(enum alm_id_e alarmid);
#endif /* CONFIG_RTC_ALARM */
/****************************************************************************
* Name: stm32l4_rtc_lowerhalf
+270 -5
View File
@@ -44,6 +44,7 @@
#include <sys/types.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <nuttx/arch.h>
@@ -54,10 +55,25 @@
#ifdef CONFIG_RTC_DRIVER
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define STM32_NALARMS 2
/****************************************************************************
* Private Types
****************************************************************************/
#ifdef CONFIG_RTC_ALARM
struct stm32l4_cbinfo_s
{
volatile rtc_alarm_callback_t cb; /* Callback when the alarm expires */
volatile FAR void *priv; /* Private argurment to accompany callback */
uint8_t id; /* Identifies the alarm */
};
#endif
/* This is the private type for the RTC state. It must be cast compatible
* with struct rtc_lowerhalf_s.
*/
@@ -73,6 +89,12 @@ struct stm32l4_lowerhalf_s
/* Data following is private to this driver and not visible outside of
* this file.
*/
#ifdef CONFIG_RTC_ALARM
/* Alarm callback information */
struct stm32l4_cbinfo_s cbinfo[STM32_NALARMS];
#endif
};
/****************************************************************************
@@ -85,6 +107,15 @@ static int stm32l4_rdtime(FAR struct rtc_lowerhalf_s *lower,
static int stm32l4_settime(FAR struct rtc_lowerhalf_s *lower,
FAR const struct rtc_time *rtctime);
#ifdef CONFIG_RTC_ALARM
static int stm32l4_setalarm(FAR struct rtc_lowerhalf_s *lower,
FAR const struct lower_setalarm_s *alarminfo);
static int stm32l4_setrelative(FAR struct rtc_lowerhalf_s *lower,
FAR const struct lower_setrelative_s *alarminfo);
static int stm32l4_cancelalarm(FAR struct rtc_lowerhalf_s *lower,
int alarmid);
#endif
/****************************************************************************
* Private Data
****************************************************************************/
@@ -96,9 +127,9 @@ static const struct rtc_ops_s g_rtc_ops =
.rdtime = stm32l4_rdtime,
.settime = stm32l4_settime,
#ifdef CONFIG_RTC_ALARM
.setalarm = NULL,
.setrelative = NULL,
.cancelalarm = NULL,
.setalarm = stm32l4_setalarm,
.setrelative = stm32l4_setrelative,
.cancelalarm = stm32l4_cancelalarm,
#endif
#ifdef CONFIG_RTC_IOCTL
.ioctl = NULL,
@@ -120,7 +151,56 @@ static struct stm32l4_lowerhalf_s g_rtc_lowerhalf =
****************************************************************************/
/****************************************************************************
* Name: stm32l4_rdtime
* Name: stm32l4_alarm_callback
*
* Description:
* This is the function that is called from the RTC driver when the alarm
* goes off. It just invokes the upper half drivers callback.
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/
#ifdef CONFIG_RTC_ALARM
static void stm32l4_alarm_callback(FAR void *arg, unsigned int alarmid)
{
FAR struct stm32l4_lowerhalf_s *lower;
FAR struct stm32l4_cbinfo_s *cbinfo;
rtc_alarm_callback_t cb;
FAR void *priv;
DEBUGASSERT(priv != NULL);
DEBUGASSERT(alarmid == RTC_ALARMA || alarmid == RTC_ALARMB);
lower = (struct stm32l4_lowerhalf_s *)arg;
cbinfo = &lower->cbinfo[alarmid];
/* Sample and clear the callback information to minimize the window in
* time in which race conditions can occur.
*/
cb = (rtc_alarm_callback_t)cbinfo->cb;
priv = (FAR void *)cbinfo->priv;
cbinfo->cb = NULL;
cbinfo->priv = NULL;
/* Perform the callback */
if (cb != NULL)
{
cb(priv, alarmid);
}
}
#endif /* CONFIG_RTC_ALARM */
/****************************************************************************
* Name: stm32_rdtime
*
* Description:
* Implements the rdtime() method of the RTC driver interface
@@ -143,6 +223,7 @@ static int stm32l4_rdtime(FAR struct rtc_lowerhalf_s *lower,
*/
return up_rtc_getdatetime((FAR struct tm *)rtctime);
}
/****************************************************************************
@@ -164,13 +245,197 @@ static int stm32l4_rdtime(FAR struct rtc_lowerhalf_s *lower,
static int stm32l4_settime(FAR struct rtc_lowerhalf_s *lower,
FAR const struct rtc_time *rtctime)
{
/* This operation depends on the fact that struct rtc_time is cast
/* This operation depends on the fact that struct rtc_time is cast
* compatible with struct tm.
*/
return stm32l4_rtc_setdatetime((FAR const struct tm *)rtctime);
}
/****************************************************************************
* Name: stm32l4_setalarm
*
* Description:
* Set a new alarm. This function implements the setalarm() method of the
* RTC driver interface
*
* Input Parameters:
* lower - A reference to RTC lower half driver state structure
* alarminfo - Provided information needed to set the alarm
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned
* on any failure.
*
****************************************************************************/
#ifdef CONFIG_RTC_ALARM
static int stm32l4_setalarm(FAR struct rtc_lowerhalf_s *lower,
FAR const struct lower_setalarm_s *alarminfo)
{
FAR struct stm32l4_lowerhalf_s *priv;
FAR struct stm32l4_cbinfo_s *cbinfo;
struct alm_setalarm_s lowerinfo;
int ret = -EINVAL;
/* ID0-> Alarm A; ID1 -> Alarm B */
DEBUGASSERT(lower != NULL && alarminfo != NULL);
DEBUGASSERT(alarminfo->id == RTC_ALARMA || alarminfo->id == RTC_ALARMB);
priv = (FAR struct stm32l4_lowerhalf_s *)lower;
if (alarminfo->id == RTC_ALARMA || alarminfo->id == RTC_ALARMB)
{
/* Remember the callback information */
cbinfo = &priv->cbinfo[alarminfo->id];
cbinfo->cb = alarminfo->cb;
cbinfo->priv = alarminfo->priv;
cbinfo->id = alarminfo->id;
/* Set the alarm */
lowerinfo.as_id = alarminfo->id;
lowerinfo.as_cb = stm32l4_alarm_callback;
lowerinfo.as_arg = priv;
memcpy(&lowerinfo.as_time, &alarminfo->time, sizeof(struct tm));
/* And set the alarm */
ret = stm32l4_rtc_setalarm(&lowerinfo);
if (ret < 0)
{
cbinfo->cb = NULL;
cbinfo->priv = NULL;
}
}
return ret;
}
#endif
/****************************************************************************
* Name: stm32l4_setrelative
*
* Description:
* Set a new alarm relative to the current time. This function implements
* the setrelative() method of the RTC driver interface
*
* Input Parameters:
* lower - A reference to RTC lower half driver state structure
* alarminfo - Provided information needed to set the alarm
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned
* on any failure.
*
****************************************************************************/
#ifdef CONFIG_RTC_ALARM
static int stm32l4_setrelative(FAR struct rtc_lowerhalf_s *lower,
FAR const struct lower_setrelative_s *alarminfo)
{
struct lower_setalarm_s setalarm;
struct tm time;
time_t seconds;
int ret = -EINVAL;
ASSERT(lower != NULL && alarminfo != NULL);
DEBUGASSERT(alarminfo->id == RTC_ALARMA || alarminfo->id == RTC_ALARMB);
if ((alarminfo->id == RTC_ALARMA || alarminfo->id == RTC_ALARMB) &&
alarminfo->reltime > 0)
{
/* Disable preemption while we do this so that we don't have to worry
* about being suspended and working on an old time.
*/
sched_lock();
/* Get the current time in broken out format */
ret = up_rtc_getdatetime(&time);
if (ret >= 0)
{
/* Convert to seconds since the epoch */
seconds = mktime(&time);
/* Add the seconds offset. Add one to the number of seconds
* because we are unsure of the phase of the timer.
*/
seconds += (alarminfo->reltime + 1);
/* And convert the time back to broken out format */
(void)gmtime_r(&seconds, (FAR struct tm *)&setalarm.time);
/* The set the alarm using this absolute time */
setalarm.id = alarminfo->id;
setalarm.cb = alarminfo->cb;
setalarm.priv = alarminfo->priv;
ret = stm32l4_setalarm(lower, &setalarm);
}
sched_unlock();
}
return ret;
}
#endif
/****************************************************************************
* Name: stm32l4_cancelalarm
*
* Description:
* Cancel the current alarm. This function implements the cancelalarm()
* method of the RTC driver interface
*
* Input Parameters:
* lower - A reference to RTC lower half driver state structure
* alarminfo - Provided information needed to set the alarm
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned
* on any failure.
*
****************************************************************************/
#ifdef CONFIG_RTC_ALARM
static int stm32l4_cancelalarm(FAR struct rtc_lowerhalf_s *lower, int alarmid)
{
FAR struct stm32l4_lowerhalf_s *priv;
FAR struct stm32l4_cbinfo_s *cbinfo;
int ret = -EINVAL;
DEBUGASSERT(lower != NULL);
DEBUGASSERT(alarmid == RTC_ALARMA || alarmid == RTC_ALARMB);
priv = (FAR struct stm32l4_lowerhalf_s *)lower;
/* ID0-> Alarm A; ID1 -> Alarm B */
if (alarmid == RTC_ALARMA || alarmid == RTC_ALARMB)
{
/* Nullify callback information to reduce window for race conditions */
cbinfo = &priv->cbinfo[alarmid];
cbinfo->cb = NULL;
cbinfo->priv = NULL;
/* Then cancel the alarm */
ret = stm32l4_rtc_cancelalarm((enum alm_id_e)alarmid);
}
return ret;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
File diff suppressed because it is too large Load Diff