aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal
diff options
context:
space:
mode:
authorFabio Utzig <utzig@utzig.org>2016-02-15 18:12:37 -0200
committerFabio Utzig <utzig@utzig.org>2016-02-15 18:12:37 -0200
commitfac5a0d26c0d5a13d0847984ee96103441fab6cd (patch)
tree0a6898ee4bea921fdf53aaa48c0d9b7a6825cd82 /os/hal
parent97bf343a8b3cea8618965d93668ae98915088208 (diff)
parent5ef170def22f5327f3c8252799e9f499eb50e5a6 (diff)
downloadChibiOS-Contrib-fac5a0d26c0d5a13d0847984ee96103441fab6cd.tar.gz
ChibiOS-Contrib-fac5a0d26c0d5a13d0847984ee96103441fab6cd.tar.bz2
ChibiOS-Contrib-fac5a0d26c0d5a13d0847984ee96103441fab6cd.zip
Merge pull request #33 from sdalu/rtc
RTC for System Ticks
Diffstat (limited to 'os/hal')
-rw-r--r--os/hal/boards/NRF51-DK/board.h3
-rw-r--r--os/hal/boards/WVSHARE_BLE400/board.h2
-rw-r--r--os/hal/ports/NRF51/NRF51822/gpt_lld.h2
-rw-r--r--os/hal/ports/NRF51/NRF51822/hal_lld.c23
-rw-r--r--os/hal/ports/NRF51/NRF51822/hal_lld.h22
-rw-r--r--os/hal/ports/NRF51/NRF51822/st_lld.c191
-rw-r--r--os/hal/ports/NRF51/NRF51822/st_lld.h137
7 files changed, 360 insertions, 20 deletions
diff --git a/os/hal/boards/NRF51-DK/board.h b/os/hal/boards/NRF51-DK/board.h
index 5e0c738..04c8daf 100644
--- a/os/hal/boards/NRF51-DK/board.h
+++ b/os/hal/boards/NRF51-DK/board.h
@@ -22,7 +22,8 @@
#define BOARD_NAME "nRF51 DK"
/* Board oscillators-related settings. */
-#define XTAL_VALUE 16000000
+#define NRF51_XTAL_VALUE 16000000
+#define NRF51_LFCLK_SOURCE 1
/* GPIO pins. */
#define BTN1 17
diff --git a/os/hal/boards/WVSHARE_BLE400/board.h b/os/hal/boards/WVSHARE_BLE400/board.h
index 57578b5..536b7f6 100644
--- a/os/hal/boards/WVSHARE_BLE400/board.h
+++ b/os/hal/boards/WVSHARE_BLE400/board.h
@@ -22,7 +22,7 @@
#define BOARD_NAME "WvShare BLE400"
/* Board oscillators-related settings. */
-#define XTAL_VALUE 16000000
+#define NRF51_XTAL_VALUE 16000000
/* GPIO pins. */
#define KEY1 16
diff --git a/os/hal/ports/NRF51/NRF51822/gpt_lld.h b/os/hal/ports/NRF51/NRF51822/gpt_lld.h
index d707cda..2c84d6c 100644
--- a/os/hal/ports/NRF51/NRF51822/gpt_lld.h
+++ b/os/hal/ports/NRF51/NRF51822/gpt_lld.h
@@ -94,7 +94,7 @@
#if !NRF51_GPT_USE_TIMER0 && !NRF51_GPT_USE_TIMER1 && \
!NRF51_GPT_USE_TIMER2
-#error "GPT driver activated but no TIM peripheral assigned"
+#error "GPT driver activated but no TIMER peripheral assigned"
#endif
#if 0
diff --git a/os/hal/ports/NRF51/NRF51822/hal_lld.c b/os/hal/ports/NRF51/NRF51822/hal_lld.c
index e1d2ed5..af5e377 100644
--- a/os/hal/ports/NRF51/NRF51822/hal_lld.c
+++ b/os/hal/ports/NRF51/NRF51822/hal_lld.c
@@ -55,6 +55,29 @@
*/
void hal_lld_init(void)
{
+ /* High frequency clock initialisation
+ * (If NRF51_XTAL_VALUE is not defined assume its an RC oscillator)
+ */
+ NRF_CLOCK->TASKS_HFCLKSTOP = 1;
+#if defined(NRF51_XTAL_VALUE)
+#if NRF51_XTAL_VALUE == 16000000
+ NRF_CLOCK->XTALFREQ = 0xFF;
+#elif NRF51_XTAL_VALUE == 32000000
+ NRF_CLOCK->XTALFREQ = 0x00;
+#endif
+#endif
+
+
+ /* Low frequency clock initialisation
+ * Clock is only started if st driver requires it
+ */
+ NRF_CLOCK->TASKS_LFCLKSTOP = 1;
+ NRF_CLOCK->LFCLKSRC = NRF51_LFCLK_SOURCE;
+
+#if (OSAL_ST_MODE != OSAL_ST_MODE_NONE) && \
+ (NRF51_SYSTEM_TICKS == NRF51_SYSTEM_TICKS_AS_RTC)
+ NRF_CLOCK->TASKS_LFCLKSTART = 1;
+#endif
}
/**
diff --git a/os/hal/ports/NRF51/NRF51822/hal_lld.h b/os/hal/ports/NRF51/NRF51822/hal_lld.h
index 436789c..283c207 100644
--- a/os/hal/ports/NRF51/NRF51822/hal_lld.h
+++ b/os/hal/ports/NRF51/NRF51822/hal_lld.h
@@ -39,14 +39,36 @@
* @}
*/
+/**
+ * @brief Frequency valuefor the Low Frequency Clock
+ */
+#define NRF51_LFCLK_FREQUENCY 32768
+
/*===========================================================================*/
/* Driver pre-compile time settings. */
/*===========================================================================*/
+/**
+ * @brief Select source of Low Frequency Clock (LFCLK)
+ * @details Possible values for source are:
+ * 0 : RC oscillator
+ * 1 : External cristal
+ * 2 : Synthetized clock from High Frequency Clock (HFCLK)
+ * When cristal is not available it's preferable to use the
+ * internal RC oscillator that synthezing the clock.
+ */
+#if !defined(NRF51_LFCLK_SOURCE) || defined(__DOXYGEN__)
+#define NRF51_LFCLK_SOURCE 0
+#endif
+
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
+#if (NRF51_LFCLK_SOURCE < 0) || (NRF51_LFCLK_SOURCE > 2)
+#error "Possible value for NRF51_LFCLK_SOURCE are 0=RC, 1=XTAL, 2=Synth"
+#endif
+
/*===========================================================================*/
/* Driver data structures and types. */
/*===========================================================================*/
diff --git a/os/hal/ports/NRF51/NRF51822/st_lld.c b/os/hal/ports/NRF51/NRF51822/st_lld.c
index 526db35..f65117b 100644
--- a/os/hal/ports/NRF51/NRF51822/st_lld.c
+++ b/os/hal/ports/NRF51/NRF51822/st_lld.c
@@ -1,5 +1,6 @@
/*
ChibiOS - Copyright (C) 2015 Fabio Utzig
+ 2016 Stephane D'Alu
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -51,9 +52,55 @@
/*===========================================================================*/
#if (OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) || defined(__DOXYGEN__)
+#if NRF51_ST_USE_RTC0 == TRUE
/**
- * @brief System Timer vector.
- * @details This interrupt is used for system tick in periodic mode.
+ * @brief System Timer vector (RTC0)
+ * @details This interrupt is used for system tick in periodic mode
+ * if selected with NRF51_ST_USE_RTC0
+ *
+ * @isr
+ */
+OSAL_IRQ_HANDLER(Vector6C) {
+
+ OSAL_IRQ_PROLOGUE();
+
+ NRF_RTC0->EVENTS_TICK = 0;
+
+ osalSysLockFromISR();
+ osalOsTimerHandlerI();
+ osalSysUnlockFromISR();
+
+ OSAL_IRQ_EPILOGUE();
+}
+#endif
+
+#if NRF51_ST_USE_RTC1 == TRUE
+/**
+ * @brief System Timer vector (RTC1)
+ * @details This interrupt is used for system tick in periodic mode
+ * if selected with NRF51_ST_USE_RTC1
+ *
+ * @isr
+ */
+OSAL_IRQ_HANDLER(Vector84) {
+
+ OSAL_IRQ_PROLOGUE();
+
+ NRF_RTC1->EVENTS_TICK = 0;
+
+ osalSysLockFromISR();
+ osalOsTimerHandlerI();
+ osalSysUnlockFromISR();
+
+ OSAL_IRQ_EPILOGUE();
+}
+#endif
+
+#if NRF51_ST_USE_TIMER0 == TRUE
+/**
+ * @brief System Timer vector. (TIMER0)
+ * @details This interrupt is used for system tick in periodic mode
+ * if selected with NRF51_ST_USE_TIMER0
*
* @isr
*/
@@ -71,8 +118,73 @@ OSAL_IRQ_HANDLER(Vector60) {
OSAL_IRQ_EPILOGUE();
}
+#endif
#endif /* OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC */
+#if (OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING) || defined(__DOXYGEN__)
+#if NRF51_ST_USE_RTC0 == TRUE
+/**
+ * @brief System Timer vector (RTC0)
+ * @details This interrupt is used for freerunning mode (tick-less)
+ * if selected with NRF51_ST_USE_RTC0
+ *
+ * @isr
+ */
+OSAL_IRQ_HANDLER(Vector6C) {
+
+ OSAL_IRQ_PROLOGUE();
+
+ if (NRF_RTC0->EVENTS_COMPARE[0]) {
+ NRF_RTC0->EVENTS_COMPARE[0] = 0;
+
+ osalSysLockFromISR();
+ osalOsTimerHandlerI();
+ osalSysUnlockFromISR();
+ }
+
+#if OSAL_ST_RESOLUTION == 16
+ if (NRF_RTC0->EVENTS_COMPARE[1]) {
+ NRF_RTC0->EVENTS_COMPARE[1] = 0;
+ NRF_RTC0->TASKS_CLEAR = 1;
+ }
+#endif
+
+ OSAL_IRQ_EPILOGUE();
+}
+#endif
+
+#if NRF51_ST_USE_RTC1 == TRUE
+/**
+ * @brief System Timer vector (RTC1)
+ * @details This interrupt is used for freerunning mode (tick-less)
+ * if selected with NRF51_ST_USE_RTC1
+ *
+ * @isr
+ */
+OSAL_IRQ_HANDLER(Vector84) {
+
+ OSAL_IRQ_PROLOGUE();
+
+ if (NRF_RTC1->EVENTS_COMPARE[0]) {
+ NRF_RTC1->EVENTS_COMPARE[0] = 0;
+
+ osalSysLockFromISR();
+ osalOsTimerHandlerI();
+ osalSysUnlockFromISR();
+ }
+
+#if OSAL_ST_RESOLUTION == 16
+ if (NRF_RTC1->EVENTS_COMPARE[1]) {
+ NRF_RTC1->EVENTS_COMPARE[1] = 0;
+ NRF_RTC1->TASKS_CLEAR = 1;
+ }
+#endif
+
+ OSAL_IRQ_EPILOGUE();
+}
+#endif
+#endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */
+
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
@@ -83,8 +195,75 @@ OSAL_IRQ_HANDLER(Vector60) {
* @notapi
*/
void st_lld_init(void) {
+#if OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING
+
+#if NRF51_ST_USE_RTC0 == TRUE
+ /* Using RTC with prescaler */
+ NRF_RTC0->TASKS_STOP = 1;
+ NRF_RTC0->PRESCALER = (NRF51_LFCLK_FREQUENCY / OSAL_ST_FREQUENCY) - 1;
+ NRF_RTC0->EVTENCLR = RTC_EVTENSET_COMPARE0_Msk;
+ NRF_RTC0->EVENTS_COMPARE[0] = 0;
+ NRF_RTC0->INTENSET = RTC_INTENSET_COMPARE0_Msk;
+#if OSAL_ST_RESOLUTION == 16
+ NRF_RTC0->CC[1] = 0x10000; /* 2^16 */
+ NRF_RTC0->EVENTS_COMPARE[1] = 0;
+ NRF_RTC0->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
+ NRF_RTC0->INTENSET = RTC_INTENSET_COMPARE1_Msk;
+#endif
+ NRF_RTC0->TASKS_CLEAR = 1;
+
+ /* Start timer */
+ nvicEnableVector(RTC0_IRQn, NRF51_ST_PRIORITY);
+ NRF_RTC0->TASKS_START = 1;
+#endif /* NRF51_ST_USE_RTC0 == TRUE */
+
+#if NRF51_ST_USE_RTC1 == TRUE
+ /* Using RTC with prescaler */
+ NRF_RTC1->TASKS_STOP = 1;
+ NRF_RTC1->PRESCALER = (NRF51_LFCLK_FREQUENCY / OSAL_ST_FREQUENCY) - 1;
+ NRF_RTC1->EVTENCLR = RTC_EVTENSET_COMPARE0_Msk;
+ NRF_RTC1->EVENTS_COMPARE[0] = 0;
+ NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE0_Msk;
+#if OSAL_ST_RESOLUTION == 16
+ NRF_RTC1->CC[1] = 0x10000; /* 2^16 */
+ NRF_RTC1->EVENTS_COMPARE[1] = 0;
+ NRF_RTC1->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
+ NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE1_Msk;
+#endif
+ NRF_RTC1->TASKS_CLEAR = 1;
+
+ /* Start timer */
+ nvicEnableVector(RTC1_IRQn, NRF51_ST_PRIORITY);
+ NRF_RTC1->TASKS_START = 1;
+#endif /* NRF51_ST_USE_RTC1 == TRUE */
+
+#endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */
#if OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC
+
+#if NRF51_ST_USE_RTC0 == TRUE
+ /* Using RTC with prescaler */
+ NRF_RTC0->TASKS_STOP = 1;
+ NRF_RTC0->PRESCALER = (NRF51_LFCLK_FREQUENCY / OSAL_ST_FREQUENCY) - 1;
+ NRF_RTC0->INTENSET = RTC_INTENSET_TICK_Msk;
+
+ /* Start timer */
+ nvicEnableVector(RTC0_IRQn, NRF51_ST_PRIORITY);
+ NRF_RTC0->TASKS_START = 1;
+#endif /* NRF51_ST_USE_RTC0 == TRUE */
+
+#if NRF51_ST_USE_RTC1 == TRUE
+ /* Using RTC with prescaler */
+ NRF_RTC1->TASKS_STOP = 1;
+ NRF_RTC1->PRESCALER = (NRF51_LFCLK_FREQUENCY / OSAL_ST_FREQUENCY) - 1;
+ NRF_RTC1->INTENSET = RTC_INTENSET_TICK_Msk;
+
+ /* Start timer */
+ nvicEnableVector(RTC1_IRQn, NRF51_ST_PRIORITY);
+ NRF_RTC1->TASKS_START = 1;
+#endif /* NRF51_ST_USE_RTC1 == TRUE */
+
+#if NRF51_ST_USE_TIMER0 == TRUE
NRF_TIMER0->TASKS_CLEAR = 1;
/*
@@ -102,12 +281,12 @@ void st_lld_init(void) {
NRF_TIMER0->SHORTS = 1;
NRF_TIMER0->INTENSET = 0x10000;
- nvicEnableVector(TIMER0_IRQn, 8);
-
/* Start timer */
+ nvicEnableVector(TIMER0_IRQn, NRF51_ST_PRIORITY);
NRF_TIMER0->TASKS_START = 1;
-#endif
-
+#endif /* NRF51_ST_USE_TIMER0 == TRUE */
+
+#endif /* OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC */
}
#endif /* OSAL_ST_MODE != OSAL_ST_MODE_NONE */
diff --git a/os/hal/ports/NRF51/NRF51822/st_lld.h b/os/hal/ports/NRF51/NRF51822/st_lld.h
index 2e0672e..ab25dbb 100644
--- a/os/hal/ports/NRF51/NRF51822/st_lld.h
+++ b/os/hal/ports/NRF51/NRF51822/st_lld.h
@@ -27,6 +27,8 @@
#ifndef _ST_LLD_H_
#define _ST_LLD_H_
+#include "halconf.h"
+
/*===========================================================================*/
/* Driver constants. */
/*===========================================================================*/
@@ -35,10 +37,92 @@
/* Driver pre-compile time settings. */
/*===========================================================================*/
+/**
+ * @brief Use RTC0 to generates system ticks
+ */
+#if !defined(NRF51_ST_USE_RTC0) || defined(__DOXYGEN__)
+#if !defined(SOFTDEVICE_PRESENT)
+#define NRF51_ST_USE_RTC0 TRUE
+#else
+#define NRF51_ST_USE_RTC0 FALSE
+#endif
+#endif
+
+/**
+ * @brief Use RTC1 to generates system ticks
+ */
+#if !defined(NRF51_ST_USE_RTC1) || defined(__DOXYGEN__)
+#if !defined(SOFTDEVICE_PRESENT)
+#define NRF51_ST_USE_RTC1 FALSE
+#else
+#define NRF51_ST_USE_RTC1 TRUE
+#endif
+#endif
+
+/**
+ * @brief Use TIMER0 to generates system ticks
+ */
+#if !defined(NRF51_ST_USE_TIMER0) || defined(__DOXYGEN__)
+#define NRF51_ST_USE_TIMER0 FALSE
+#endif
+
+/**
+ * @brief ST interrupt priority level setting.
+ */
+#if !defined(NRF51_ST_PRIORITY) || defined(__DOXYGEN__)
+#if !defined(SOFTDEVICE_PRESENT)
+#define NRF51_ST_PRIORITY 8
+#else
+#define NRF51_ST_PRIORITY 1
+#endif
+#endif
+
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
+#if OSAL_ST_MODE != OSAL_ST_MODE_NONE
+#if (NRF51_ST_USE_TIMER0 == TRUE) && (NRF51_GPT_USE_TIMER0 == TRUE)
+#error "TIMER0 already used by GPT driver"
+#endif
+
+#if (NRF51_ST_USE_RTC0 == FALSE) && \
+ (NRF51_ST_USE_RTC1 == FALSE) && \
+ (NRF51_ST_USE_TIMER0 == FALSE)
+#error "One clock source is needed, enable one (RTC0, RTC1, or TIMER0)"
+#endif
+
+#if ((NRF51_ST_USE_RTC0 == TRUE ? 1 : 0) + \
+ (NRF51_ST_USE_RTC1 == TRUE ? 1 : 0) + \
+ (NRF51_ST_USE_TIMER0 == TRUE ? 1 : 0)) > 1
+#error "Only one clock source can be used (RTC0, RTC1, or TIMER0)"
+#endif
+
+#if defined(SOFTDEVICE_PRESENT)
+#if NRF51_ST_USE_RTC0 == TRUE
+#error "RTC0 cannot be used for system ticks when SOFTDEVICE present"
+#endif
+
+#if NRF51_ST_USE_TIMER0 == TRUE
+#error "TIMER0 cannot be used for system ticks when SOFTDEVICE present"
+#endif
+
+#if NRF51_ST_PRIORITY != 1
+#error "ST priority must be 1 when SOFTDEVICE present"
+#endif
+
+#endif /* defined(SOFTDEVICE_PRESENT) */
+#endif /* OSAL_ST_MODE != OSAL_ST_MODE_NONE */
+
+#if OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING
+#if defined(CH_CFG_ST_TIMEDELTA) && (CH_CFG_ST_TIMEDELTA < 5)
+#error "CH_CFG_ST_TIMEDELTA is too low"
+#endif
+#if NRF51_ST_USE_TIMER0 == TRUE
+#error "Freeruning (tick-less) mode not supported with TIMER, use RTC"
+#endif
+#endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */
+
/*===========================================================================*/
/* Driver data structures and types. */
/*===========================================================================*/
@@ -71,8 +155,12 @@ extern "C" {
* @notapi
*/
static inline systime_t st_lld_get_counter(void) {
-
- return (systime_t)0;
+#if NRF51_ST_USE_RTC0 == TRUE
+ return (systime_t)NRF_RTC0->COUNTER;
+#endif
+#if NRF51_ST_USE_RTC1 == TRUE
+ return (systime_t)NRF_RTC1->COUNTER;
+#endif
}
/**
@@ -85,8 +173,16 @@ static inline systime_t st_lld_get_counter(void) {
* @notapi
*/
static inline void st_lld_start_alarm(systime_t abstime) {
-
- (void)abstime;
+#if NRF51_ST_USE_RTC0 == TRUE
+ NRF_RTC0->CC[0] = abstime;
+ NRF_RTC0->EVENTS_COMPARE[0] = 0;
+ NRF_RTC0->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
+#endif
+#if NRF51_ST_USE_RTC1 == TRUE
+ NRF_RTC1->CC[0] = abstime;
+ NRF_RTC1->EVENTS_COMPARE[0] = 0;
+ NRF_RTC1->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
+#endif
}
/**
@@ -95,7 +191,14 @@ static inline void st_lld_start_alarm(systime_t abstime) {
* @notapi
*/
static inline void st_lld_stop_alarm(void) {
-
+#if NRF51_ST_USE_RTC0 == TRUE
+ NRF_RTC0->EVTENCLR = RTC_EVTENCLR_COMPARE0_Msk;
+ NRF_RTC0->EVENTS_COMPARE[0] = 0;
+#endif
+#if NRF51_ST_USE_RTC1 == TRUE
+ NRF_RTC1->EVTENCLR = RTC_EVTENCLR_COMPARE0_Msk;
+ NRF_RTC1->EVENTS_COMPARE[0] = 0;
+#endif
}
/**
@@ -106,8 +209,12 @@ static inline void st_lld_stop_alarm(void) {
* @notapi
*/
static inline void st_lld_set_alarm(systime_t abstime) {
-
- (void)abstime;
+#if NRF51_ST_USE_RTC0 == TRUE
+ NRF_RTC0->CC[0] = abstime;
+#endif
+#if NRF51_ST_USE_RTC1 == TRUE
+ NRF_RTC1->CC[0] = abstime;
+#endif
}
/**
@@ -118,8 +225,12 @@ static inline void st_lld_set_alarm(systime_t abstime) {
* @notapi
*/
static inline systime_t st_lld_get_alarm(void) {
-
- return (systime_t)0;
+#if NRF51_ST_USE_RTC0 == TRUE
+ return (systime_t)NRF_RTC0->CC[0];
+#endif
+#if NRF51_ST_USE_RTC1 == TRUE
+ return (systime_t)NRF_RTC1->CC[0];
+#endif
}
/**
@@ -132,8 +243,12 @@ static inline systime_t st_lld_get_alarm(void) {
* @notapi
*/
static inline bool st_lld_is_alarm_active(void) {
-
- return false;
+#if NRF51_ST_USE_RTC0 == TRUE
+ return NRF_RTC0->EVTEN & RTC_EVTEN_COMPARE0_Msk;
+#endif
+#if NRF51_ST_USE_RTC1 == TRUE
+ return NRF_RTC1->EVTEN & RTC_EVTEN_COMPARE0_Msk;
+#endif
}
#endif /* _ST_LLD_H_ */