From 8fe62a0f907639db23f14d17a45a7970854f1d22 Mon Sep 17 00:00:00 2001 From: Stephane D'Alu Date: Fri, 5 Feb 2016 23:54:23 +0100 Subject: use mcuconf.h with NRF51_SYSTEM_TICKS to select the timer source (TIMER or RTC) --- os/hal/ports/NRF51/NRF51822/hal_lld.c | 16 +++------- os/hal/ports/NRF51/NRF51822/hal_lld.h | 24 +++++++++++++-- os/hal/ports/NRF51/NRF51822/st_lld.c | 58 ++++++++++++++++++++++++++++++++++- os/hal/ports/NRF51/NRF51822/st_lld.h | 25 +++++++++++++++ 4 files changed, 109 insertions(+), 14 deletions(-) (limited to 'os/hal/ports') diff --git a/os/hal/ports/NRF51/NRF51822/hal_lld.c b/os/hal/ports/NRF51/NRF51822/hal_lld.c index 30fe6c0..af5e377 100644 --- a/os/hal/ports/NRF51/NRF51822/hal_lld.c +++ b/os/hal/ports/NRF51/NRF51822/hal_lld.c @@ -66,22 +66,16 @@ void hal_lld_init(void) NRF_CLOCK->XTALFREQ = 0x00; #endif #endif + /* Low frequency clock initialisation - * If source not specified, use the internal RC (0) which is prefered - * over synthetized clock from the high frequency clock (2) + * Clock is only started if st driver requires it */ -#if defined(NRF51_LFCLK_SOURCE) -#if (NRF51_LFCLK_SOURCE >=0) && (NRF51_LFCLK_SOURCE <= 2) + NRF_CLOCK->TASKS_LFCLKSTOP = 1; NRF_CLOCK->LFCLKSRC = NRF51_LFCLK_SOURCE; -#else -#error "Possible value for NRF51_LFCLK_SOURCE are 0=RC, 1=XTAL, 2=Synth" -#endif -#else - NRF_CLOCK->LFCLKSRC = 0; -#endif -#if (OSAL_ST_MODE != OSAL_ST_MODE_NONE) +#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 b685667..24eff21 100644 --- a/os/hal/ports/NRF51/NRF51822/hal_lld.h +++ b/os/hal/ports/NRF51/NRF51822/hal_lld.h @@ -39,15 +39,35 @@ * @} */ +/** + * @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 + /*===========================================================================*/ -/* Driver constants and error checks. */ +/* Derived constants and error checks. */ /*===========================================================================*/ -#define NRF51_LFCLK_FREQUENCY 32768 +#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 d8c7b6c..a43a679 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,12 @@ /*===========================================================================*/ #if (OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC) || defined(__DOXYGEN__) + +#if (NRF51_SYSTEM_TICKS == NRF51_SYSTEM_TICKS_AS_RTC) /** * @brief System Timer vector (RTC0) - * @details This interrupt is used for system tick in periodic mode. + * @details This interrupt is used for system tick in periodic mode + * if selected with NRF51_SYSTEM_TICKS == NRF51_SYSTEM_TICKS_AS_RTC * * @isr */ @@ -69,6 +73,32 @@ OSAL_IRQ_HANDLER(Vector6C) { OSAL_IRQ_EPILOGUE(); } +#endif + +#if (NRF51_SYSTEM_TICKS == NRF51_SYSTEM_TICKS_AS_TIMER) +/** + * @brief System Timer vector. (TIMER0) + * @details This interrupt is used for system tick in periodic mode + * if selected with NRF51_SYSTEM_TICKS == NRF51_SYSTEM_TICKS_AS_TIMER + * + * @isr + */ +OSAL_IRQ_HANDLER(Vector60) { + + OSAL_IRQ_PROLOGUE(); + + /* Clear timer compare event */ + if (NRF_TIMER0->EVENTS_COMPARE[0] != 0) + NRF_TIMER0->EVENTS_COMPARE[0] = 0; + + osalSysLockFromISR(); + osalOsTimerHandlerI(); + osalSysUnlockFromISR(); + + OSAL_IRQ_EPILOGUE(); +} +#endif + #endif /* OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC */ /*===========================================================================*/ @@ -86,6 +116,8 @@ void st_lld_init(void) { #endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */ #if OSAL_ST_MODE == OSAL_ST_MODE_PERIODIC + +#if (NRF51_SYSTEM_TICKS == NRF51_SYSTEM_TICKS_AS_RTC) /* Using RTC with prescaler */ NRF_RTC0->TASKS_STOP = 1; NRF_RTC0->PRESCALER = (NRF51_LFCLK_FREQUENCY / OSAL_ST_FREQUENCY) - 1; @@ -96,6 +128,30 @@ void st_lld_init(void) { NRF_RTC0->TASKS_START = 1; #endif +#if (NRF51_SYSTEM_TICKS == NRF51_SYSTEM_TICKS_AS_TIMER) + NRF_TIMER0->TASKS_CLEAR = 1; + + /* + * Using 32-bit mode with prescaler 16 configures this + * timer with a 1MHz clock. + */ + NRF_TIMER0->BITMODE = 3; + NRF_TIMER0->PRESCALER = 4; + + /* + * Configure timer 0 compare capture 0 to generate interrupt + * and clear timer value when event is generated. + */ + NRF_TIMER0->CC[0] = (1000000 / OSAL_ST_FREQUENCY) - 1; + NRF_TIMER0->SHORTS = 1; + NRF_TIMER0->INTENSET = 0x10000; + + /* Start timer */ + nvicEnableVector(TIMER0_IRQn, 8); + NRF_TIMER0->TASKS_START = 1; +#endif + +#endif } #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..c62ff2c 100644 --- a/os/hal/ports/NRF51/NRF51822/st_lld.h +++ b/os/hal/ports/NRF51/NRF51822/st_lld.h @@ -31,14 +31,39 @@ /* Driver constants. */ /*===========================================================================*/ +/** + * @brief SPI0 interrupt priority level setting. + */ +#define NRF51_SYSTEM_TICKS_AS_TIMER 1 + +/** + * @brief SPI0 interrupt priority level setting. + */ +#define NRF51_SYSTEM_TICKS_AS_RTC 2 + /*===========================================================================*/ /* Driver pre-compile time settings. */ /*===========================================================================*/ +/** + * @brief Select the method to generates system ticks + * @details Possibles values are: + * NRF51_SYSTEM_TICKS_AS_TIMER for TIMER0 + * NRF51_SYSTEM_TICKS_AS_RTC for RTC0 + */ +#if !defined(NRF51_SYSTEM_TICKS) || defined(__DOXYGEN__) +#define NRF51_SYSTEM_TICKS NRF51_SYSTEM_TICKS_AS_RTC +#endif + /*===========================================================================*/ /* Derived constants and error checks. */ /*===========================================================================*/ +#if ((NRF51_SYSTEM_TICKS != NRF51_SYSTEM_TICKS_AS_RTC) && \ + (NRF51_SYSTEM_TICKS != NRF51_SYSTEM_TICKS_AS_TIMER)) +#error "NRF51_SYSTEM_TICKS illegal value" +#endif + /*===========================================================================*/ /* Driver data structures and types. */ /*===========================================================================*/ -- cgit v1.2.3