Merged in ziggurat29/nuttx/stm32l4_update_rtc_impl (pull request #14)

Stm32l4_update_rtc_impl
This commit is contained in:
Gregory Nutt 2016-05-08 01:24:09 -06:00
commit 0143b3869a
8 changed files with 1043 additions and 298 deletions

View File

@ -86,17 +86,17 @@ static int stm32l4_exti_alarm_isr(int irq, void *context)
{
int ret = OK;
/* Clear the pending EXTI interrupt */
putreg32(EXTI1_RTC_ALARM, STM32L4_EXTI1_PR);
/* And dispatch the interrupt to the handler */
/* Dispatch the interrupt to the handler */
if (stm32l4_exti_callback)
{
ret = stm32l4_exti_callback(irq, context);
}
/* Clear the pending EXTI interrupt */
putreg32(EXTI1_RTC_ALARM, STM32L4_EXTI1_PR);
return ret;
}

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

View File

@ -34,8 +34,6 @@
*
****************************************************************************/
/* REVISIT: This driver is *not* thread-safe! */
/****************************************************************************
* Included Files
****************************************************************************/
@ -44,6 +42,7 @@
#include <sys/types.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <nuttx/arch.h>
@ -54,10 +53,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 +87,14 @@ struct stm32l4_lowerhalf_s
/* Data following is private to this driver and not visible outside of
* this file.
*/
sem_t devsem; /* Threads can only exclusively access the RTC */
#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
@ -138,11 +218,25 @@ static struct stm32l4_lowerhalf_s g_rtc_lowerhalf =
static int stm32l4_rdtime(FAR struct rtc_lowerhalf_s *lower,
FAR struct rtc_time *rtctime)
{
FAR struct stm32l4_lowerhalf_s *priv;
int ret;
priv = (FAR struct stm32l4_lowerhalf_s *)lower;
if (sem_wait(&priv->devsem) != OK)
{
return -errno;
}
/* This operation depends on the fact that struct rtc_time is cast
* compatible with struct tm.
*/
return up_rtc_getdatetime((FAR struct tm *)rtctime);
ret = up_rtc_getdatetime((FAR struct tm *)rtctime);
sem_post(&priv->devsem);
return ret;
}
/****************************************************************************
@ -164,13 +258,223 @@ 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
FAR struct stm32l4_lowerhalf_s *priv;
int ret;
priv = (FAR struct stm32l4_lowerhalf_s *)lower;
if (sem_wait(&priv->devsem) != OK)
{
return -errno;
}
/* 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);
ret = stm32l4_rtc_setdatetime((FAR const struct tm *)rtctime);
sem_post(&priv->devsem);
return ret;
}
/****************************************************************************
* 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 (sem_wait(&priv->devsem) != OK)
{
return -errno;
}
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;
}
}
sem_post(&priv->devsem);
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;
if (sem_wait(&priv->devsem) != OK)
{
return -errno;
}
/* 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);
}
sem_post(&priv->devsem);
return ret;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@ -199,6 +503,8 @@ static int stm32l4_settime(FAR struct rtc_lowerhalf_s *lower,
FAR struct rtc_lowerhalf_s *stm32l4_rtc_lowerhalf(void)
{
sem_init(&g_rtc_lowerhalf.devsem, 0, 1);
return (FAR struct rtc_lowerhalf_s *)&g_rtc_lowerhalf;
}

File diff suppressed because it is too large Load Diff

View File

@ -69,6 +69,7 @@ CONFIG_DEBUG_LIB=y
CONFIG_DEBUG_LEDS=y
CONFIG_DEBUG_ANALOG=y
CONFIG_DEBUG_GPIO=y
# CONFIG_DEBUG_RTC is not set
CONFIG_DEBUG_SPI=y
CONFIG_ARCH_HAVE_STACKCHECK=y
# CONFIG_STACK_COLORATION is not set
@ -290,6 +291,9 @@ CONFIG_STM32L4_FIREWALL=y
CONFIG_STM32L4_FLASH_PREFETCH=y
CONFIG_STM32L4_DISABLE_IDLE_SLEEP_DURING_DEBUG=y
# CONFIG_ARCH_BOARD_STM32L4_CUSTOM_CLOCKCONFIG is not set
CONFIG_STM32L4_RTC_LSECLOCK=y
# CONFIG_STM32L4_RTC_LSICLOCK is not set
# CONFIG_STM32L4_RTC_HSECLOCK is not set
CONFIG_STM32L4_SAI1PLL=y
# CONFIG_STM32L4_SAI2PLL is not set
@ -377,7 +381,14 @@ CONFIG_NSH_MMCSDMINOR=0
#
# Board-Specific Options
#
# CONFIG_LIB_BOARDCTL is not set
CONFIG_LIB_BOARDCTL=y
# CONFIG_BOARDCTL_RESET is not set
# CONFIG_BOARDCTL_UNIQUEID is not set
# CONFIG_BOARDCTL_TSCTEST is not set
# CONFIG_BOARDCTL_ADCTEST is not set
# CONFIG_BOARDCTL_PWMTEST is not set
# CONFIG_BOARDCTL_GRAPHICS is not set
# CONFIG_BOARDCTL_IOCTL is not set
#
# RTOS Features
@ -396,9 +407,6 @@ CONFIG_USEC_PER_TICK=10000
# CONFIG_SYSTEM_TIME64 is not set
# CONFIG_CLOCK_MONOTONIC is not set
# CONFIG_JULIAN_TIME is not set
CONFIG_START_YEAR=2014
CONFIG_START_MONTH=5
CONFIG_START_DAY=5
CONFIG_MAX_WDOGPARMS=2
CONFIG_PREALLOC_WDOGS=8
CONFIG_WDOG_INTRESERVE=1
@ -517,7 +525,13 @@ CONFIG_SPI_EXCHANGE=y
# Timer Driver Support
#
# CONFIG_TIMER is not set
# CONFIG_RTC is not set
CONFIG_RTC=y
CONFIG_RTC_DATETIME=y
CONFIG_RTC_ALARM=y
CONFIG_RTC_NALARMS=2
CONFIG_RTC_DRIVER=y
CONFIG_RTC_IOCTL=y
# CONFIG_RTC_EXTERNAL is not set
# CONFIG_WATCHDOG is not set
# CONFIG_ANALOG is not set
# CONFIG_AUDIO_DEVICES is not set
@ -755,6 +769,11 @@ CONFIG_BUILTIN_PROXY_STACKSIZE=1024
#
# Examples
#
CONFIG_EXAMPLES_ALARM=y
CONFIG_EXAMPLES_ALARM_PRIORITY=100
CONFIG_EXAMPLES_ALARM_STACKSIZE=2048
CONFIG_EXAMPLES_ALARM_DEVPATH="/dev/rtc0"
CONFIG_EXAMPLES_ALARM_SIGNO=1
# CONFIG_EXAMPLES_CHAT is not set
# CONFIG_EXAMPLES_CONFIGDATA is not set
# CONFIG_EXAMPLES_CPUHOG is not set
@ -813,6 +832,7 @@ CONFIG_EXAMPLES_NSAMPLES=8
# CONFIG_EXAMPLES_TIFF is not set
# CONFIG_EXAMPLES_TOUCHSCREEN is not set
# CONFIG_EXAMPLES_WEBSERVER is not set
# CONFIG_EXAMPLES_USBSERIAL is not set
# CONFIG_EXAMPLES_USBTERM is not set
# CONFIG_EXAMPLES_WATCHDOG is not set
@ -944,7 +964,7 @@ CONFIG_NSH_FILEIOSIZE=512
#
CONFIG_NSH_CONSOLE=y
# CONFIG_NSH_ALTCONDEV is not set
# CONFIG_NSH_ARCHINIT is not set
CONFIG_NSH_ARCHINIT=y
# CONFIG_NSH_LOGIN is not set
# CONFIG_NSH_CONSOLE_LOGIN is not set

View File

@ -52,7 +52,15 @@
************************************************************************************/
/* Configuration ********************************************************************/
#define HAVE_RTC_DRIVER 1
#define HAVE_MMCSD 1
/* Check if we can support the RTC driver */
#if !defined(CONFIG_RTC) || !defined(CONFIG_RTC_DRIVER)
# undef HAVE_RTC_DRIVER
#endif
#if !defined(CONFIG_STM32_SDIO) || !defined(CONFIG_MMCSD) || \
!defined(CONFIG_MMCSD_SDIO)
# undef HAVE_MMCSD

View File

@ -55,6 +55,11 @@
#include "nucleo-l476rg.h"
#ifdef HAVE_RTC_DRIVER
# include <nuttx/timers/rtc.h>
# include "stm32l4_rtc.h"
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@ -83,9 +88,12 @@ void up_netinitialize(void)
int board_app_initialize(void)
{
#if defined(HAVE_MMCSD) || defined(CONFIG_AJOYSTICK)
int ret;
#ifdef HAVE_RTC_DRIVER
FAR struct rtc_lowerhalf_s *rtclower;
#endif
int ret;
(void)ret;
/* Configure CPU load estimation */
@ -93,6 +101,30 @@ int board_app_initialize(void)
cpuload_initialize_once();
#endif
#ifdef HAVE_RTC_DRIVER
/* Instantiate the STM32L4 lower-half RTC driver */
rtclower = stm32l4_rtc_lowerhalf();
if (!rtclower)
{
sdbg("ERROR: Failed to instantiate the RTC lower-half driver\n");
return -ENOMEM;
}
else
{
/* Bind the lower half driver and register the combined RTC driver
* as /dev/rtc0
*/
ret = rtc_initialize(0, rtclower);
if (ret < 0)
{
sdbg("ERROR: Failed to bind/register the RTC driver: %d\n", ret);
return ret;
}
}
#endif
#ifdef HAVE_MMCSD
/* First, get an instance of the SDIO interface */

View File

@ -543,7 +543,8 @@ CONFIG_SPI_EXCHANGE=y
# CONFIG_TIMER is not set
CONFIG_RTC=y
CONFIG_RTC_DATETIME=y
# CONFIG_RTC_ALARM is not set
CONFIG_RTC_ALARM=y
CONFIG_RTC_NALARMS=2
CONFIG_RTC_DRIVER=y
CONFIG_RTC_IOCTL=y
# CONFIG_RTC_EXTERNAL is not set
@ -834,6 +835,11 @@ CONFIG_BUILTIN_PROXY_STACKSIZE=1024
#
# Examples
#
CONFIG_EXAMPLES_ALARM=y
CONFIG_EXAMPLES_ALARM_PRIORITY=100
CONFIG_EXAMPLES_ALARM_STACKSIZE=2048
CONFIG_EXAMPLES_ALARM_DEVPATH="/dev/rtc0"
CONFIG_EXAMPLES_ALARM_SIGNO=1
CONFIG_EXAMPLES_BUTTONS=y
CONFIG_EXAMPLES_BUTTONS_MIN=0
CONFIG_EXAMPLES_BUTTONS_MAX=4