Merge pull request #7172 from mprse/NRF5x_updates

Unify RTC, lp ticker, and us ticker for NRF51 and NRF52 series
pull/7280/head
Cruz Monrreal 2018-06-20 16:08:05 -05:00 committed by GitHub
commit 84d6b79dec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 28 additions and 303 deletions

View File

@ -72,7 +72,6 @@ int32_t flash_init(flash_t *obj)
if (first_init) {
first_init = false;
lp_ticker_init();
}
return 0;

View File

@ -39,7 +39,7 @@
#include "i2c_api.h"
#if DEVICE_I2C
#if (defined(DEVICE_I2C) && defined(DEVICE_LPTICKER))
#include "mbed_assert.h"
#include "mbed_error.h"

View File

@ -73,7 +73,6 @@ void trng_init(trng_t *obj)
if (first_init) {
first_init = false;
lp_ticker_init();
}
}

View File

@ -1,53 +0,0 @@
/* mbed Microcontroller Library
* Copyright (c) 2015 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef COMMON_RTC_H
#define COMMON_RTC_H
#include "nrf_rtc.h"
#define RTC_COUNTER_BITS 24u
#define RTC_FREQ 32768u
// Instance 0 is reserved for SoftDevice.
// Instance 1 is used as a common one for lp_ticker and (in case
// of NRF51) as an alternative tick source for RTOS.
#define COMMON_RTC_INSTANCE NRF_RTC1
#define COMMON_RTC_IRQ_HANDLER RTC1_IRQHandler
#define OS_TICK_CC_CHANNEL 0
#define LP_TICKER_CC_CHANNEL 1
#define COMMON_RTC_EVENT_COMPARE(channel) \
CONCAT_2(NRF_RTC_EVENT_COMPARE_, channel)
#define COMMON_RTC_INT_COMPARE_MASK(channel) \
CONCAT_3(NRF_RTC_INT_COMPARE, channel, _MASK)
#define OS_TICK_EVENT COMMON_RTC_EVENT_COMPARE(OS_TICK_CC_CHANNEL)
#define OS_TICK_INT_MASK COMMON_RTC_INT_COMPARE_MASK(OS_TICK_CC_CHANNEL)
#define LP_TICKER_EVENT COMMON_RTC_EVENT_COMPARE(LP_TICKER_CC_CHANNEL)
#define LP_TICKER_INT_MASK COMMON_RTC_INT_COMPARE_MASK(LP_TICKER_CC_CHANNEL)
extern bool m_common_rtc_enabled;
extern uint32_t volatile m_common_rtc_overflows;
extern bool volatile lp_ticker_interrupt_fire;
void common_rtc_init(void);
uint32_t common_rtc_32bit_ticks_get(void);
uint64_t common_rtc_64bit_us_get(void);
void common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,
uint32_t int_mask);
#endif // COMMON_RTC_H

View File

@ -36,7 +36,7 @@
*
*/
#if DEVICE_FLASH
#if (defined(DEVICE_FLASH) && defined(DEVICE_LPTICKER))
#include "hal/flash_api.h"
#include "hal/lp_ticker_api.h"
@ -84,8 +84,6 @@ int32_t flash_init(flash_t *obj)
result = nrf_fstorage_init(&nordic_fstorage, &nrf_fstorage_nvmc, NULL);
#endif
/* Initialize low power ticker for timeouts. */
lp_ticker_init();
}
/* Convert Nordic SDK error code to mbed HAL. */

View File

@ -36,8 +36,7 @@
*
*/
#if DEVICE_I2C
#if (defined(DEVICE_I2C) && defined(DEVICE_LPTICKER))
/* I2C
*
* This HAL implementation uses the nrf_drv_twi.h API primarily but switches to TWI for the
@ -131,8 +130,6 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
if (first_init) {
first_init = false;
/* Initialize low power ticker. Used for timeouts. */
lp_ticker_init();
/* Register interrupt handlers in driver with the NVIC. */
NVIC_SetVector(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn, (uint32_t) SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler);

View File

@ -1,75 +0,0 @@
/* mbed Microcontroller Library
* Copyright (c) 2015 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lp_ticker_api.h"
#include "common_rtc.h"
#include "platform/mbed_critical.h"
#if DEVICE_LPTICKER
/* LP ticker is driven by 32kHz clock and counter length is 24 bits. */
const ticker_info_t* lp_ticker_get_info()
{
static const ticker_info_t info = {
RTC_FREQ,
RTC_COUNTER_BITS
};
return &info;
}
void lp_ticker_init(void)
{
common_rtc_init();
}
void lp_ticker_free(void)
{
// A common counter is used for RTC, lp_ticker and us_ticker, so it can't be
// disabled here, but this does not cause any extra cost.
}
uint32_t lp_ticker_read()
{
return nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
}
void lp_ticker_set_interrupt(timestamp_t timestamp)
{
common_rtc_set_interrupt(timestamp,
LP_TICKER_CC_CHANNEL, LP_TICKER_INT_MASK);
}
void lp_ticker_fire_interrupt(void)
{
core_util_critical_section_enter();
lp_ticker_interrupt_fire = true;
NVIC_SetPendingIRQ(RTC1_IRQn);
core_util_critical_section_exit();
}
void lp_ticker_disable_interrupt(void)
{
nrf_rtc_int_disable(COMMON_RTC_INSTANCE, LP_TICKER_INT_MASK);
}
void lp_ticker_clear_interrupt(void)
{
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, LP_TICKER_EVENT);
}
#endif // DEVICE_LPTICKER

View File

@ -1,142 +0,0 @@
/*
* Copyright (c) 2013 Nordic Semiconductor ASA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
* integrated circuit in a product or a software update for such product, must reproduce
* the above copyright notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary or object form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "us_ticker.h"
#include "us_ticker_api.h"
#include "nrf_timer.h"
#include "app_util_platform.h"
#include "nrf_drv_common.h"
#include "mbed_critical.h"
bool us_ticker_initialized = false;
/* us ticker is driven by 1MHz clock and counter length is 16 bits. */
const ticker_info_t* us_ticker_get_info()
{
static const ticker_info_t info = {
US_TICKER_FREQ,
US_TICKER_COUNTER_BITS
};
return &info;
}
void us_ticker_init(void)
{
if (us_ticker_initialized) {
nrf_timer_event_clear(NRF_TIMER1, NRF_TIMER_EVENT_COMPARE0);
nrf_timer_int_disable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0));
return;
}
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_STOP);
nrf_timer_int_disable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0));
/* Configure timer as follows:
* - timer mode,
* - timer width 16 bits,
* - timer freq 1 MHz.
*/
nrf_timer_mode_set(NRF_TIMER1, NRF_TIMER_MODE_TIMER);
nrf_timer_frequency_set(NRF_TIMER1, NRF_TIMER_FREQ_1MHz);
nrf_timer_bit_width_set(NRF_TIMER1, NRF_TIMER_BIT_WIDTH_16);
nrf_timer_cc_write(NRF_TIMER1, NRF_TIMER_CC_CHANNEL0, 0);
nrf_timer_event_clear(NRF_TIMER1, NRF_TIMER_EVENT_COMPARE0);
NVIC_SetVector(TIMER1_IRQn, (uint32_t)us_ticker_irq_handler);
nrf_drv_common_irq_enable(TIMER1_IRQn,
#ifdef NRF51
APP_IRQ_PRIORITY_LOW
#elif defined(NRF52) || defined(NRF52840_XXAA)
APP_IRQ_PRIORITY_LOWEST
#endif
);
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_START);
us_ticker_initialized = true;
}
uint32_t us_ticker_read()
{
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_CAPTURE1);
return nrf_timer_cc_read(NRF_TIMER1, NRF_TIMER_CC_CHANNEL1);
}
void us_ticker_set_interrupt(timestamp_t timestamp)
{
core_util_critical_section_enter();
nrf_timer_cc_write(NRF_TIMER1, NRF_TIMER_CC_CHANNEL0, timestamp & 0xFFFF);
if (!nrf_timer_int_enable_check(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0))) {
nrf_timer_event_clear(NRF_TIMER1, NRF_TIMER_EVENT_COMPARE0);
nrf_timer_int_enable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0));
}
core_util_critical_section_exit();
}
void us_ticker_fire_interrupt(void)
{
NVIC_SetPendingIRQ(TIMER1_IRQn);
}
void us_ticker_disable_interrupt(void)
{
nrf_timer_int_disable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0));
}
void us_ticker_clear_interrupt(void)
{
nrf_timer_event_clear(NRF_TIMER1, NRF_TIMER_EVENT_COMPARE0);
}
void us_ticker_free(void)
{
// A common counter is used for RTC, lp_ticker and us_ticker, so it can't be
// disabled here, but this does not cause any extra cost.
}

View File

@ -18,9 +18,9 @@
#define US_TICKER_H
/* TIMER0 is reserved for SoftDevice. We will use TIMER1 for us ticker
* which counter size is 16 bits. */
* which counter size is 32 bits. */
#define US_TICKER_COUNTER_BITS 16u
#define US_TICKER_COUNTER_BITS 32u
#define US_TICKER_FREQ 1000000
#endif // US_TICKER_H

View File

@ -173,13 +173,7 @@ void common_rtc_init(void)
nrf_rtc_int_disable(COMMON_RTC_INSTANCE, LP_TICKER_INT_MASK);
#endif
nrf_drv_common_irq_enable(nrf_drv_get_IRQn(COMMON_RTC_INSTANCE),
#ifdef NRF51
APP_IRQ_PRIORITY_LOW
#elif defined(NRF52) || defined(NRF52840_XXAA)
APP_IRQ_PRIORITY_LOWEST
#endif
);
nrf_drv_common_irq_enable(nrf_drv_get_IRQn(COMMON_RTC_INSTANCE), APP_IRQ_PRIORITY_HIGH);
nrf_rtc_task_trigger(COMMON_RTC_INSTANCE, NRF_RTC_TASK_START);
@ -194,6 +188,9 @@ void common_rtc_set_interrupt(uint32_t ticks_count, uint32_t cc_channel,
core_util_critical_section_enter();
/* Wrap ticks_count before comparisons. */
ticks_count = RTC_WRAP(ticks_count);
/* COMPARE occurs when a CC register is N and the COUNTER value transitions from N-1 to N.
* If the COUNTER is N, writing N+2 to a CC register is guaranteed to trigger a
* COMPARE event at N+2.
@ -202,10 +199,10 @@ void common_rtc_set_interrupt(uint32_t ticks_count, uint32_t cc_channel,
if (now == ticks_count ||
RTC_WRAP(now + 1) == ticks_count) {
ticks_count += 2;
ticks_count = RTC_WRAP(ticks_count + 2);
}
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, RTC_WRAP(ticks_count));
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, ticks_count);
if (!nrf_rtc_int_is_enabled(COMMON_RTC_INSTANCE, int_mask)) {
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, LP_TICKER_EVENT);

View File

@ -70,14 +70,18 @@ void us_ticker_init(void)
/* Configure timer as follows:
* - timer mode,
* - timer width 16 bits,
* - timer width 16 bits for NRF51 and 32 bits for NRF52,
* - timer freq 1 MHz.
*/
nrf_timer_mode_set(NRF_TIMER1, NRF_TIMER_MODE_TIMER);
nrf_timer_frequency_set(NRF_TIMER1, NRF_TIMER_FREQ_1MHz);
#ifdef NRF52
nrf_timer_bit_width_set(NRF_TIMER1, NRF_TIMER_BIT_WIDTH_32);
#else
nrf_timer_bit_width_set(NRF_TIMER1, NRF_TIMER_BIT_WIDTH_16);
#endif
nrf_timer_cc_write(NRF_TIMER1, NRF_TIMER_CC_CHANNEL0, 0);
@ -85,17 +89,13 @@ void us_ticker_init(void)
NVIC_SetVector(TIMER1_IRQn, (uint32_t)us_ticker_irq_handler);
nrf_drv_common_irq_enable(TIMER1_IRQn,
#ifdef NRF51
APP_IRQ_PRIORITY_LOW
#elif defined(NRF52) || defined(NRF52840_XXAA)
APP_IRQ_PRIORITY_LOWEST
#endif
);
nrf_drv_common_irq_enable(TIMER1_IRQn, APP_IRQ_PRIORITY_HIGH);
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_START);
/* Bug fix. First value can't be trusted. */
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_CAPTURE1);
us_ticker_initialized = true;
}
@ -110,7 +110,9 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
{
core_util_critical_section_enter();
nrf_timer_cc_write(NRF_TIMER1, NRF_TIMER_CC_CHANNEL0, timestamp & 0xFFFF);
const uint32_t counter_mask = ((1ULL << US_TICKER_COUNTER_BITS) - 1);
nrf_timer_cc_write(NRF_TIMER1, NRF_TIMER_CC_CHANNEL0, timestamp & counter_mask);
if (!nrf_timer_int_enable_check(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0))) {
nrf_timer_event_clear(NRF_TIMER1, NRF_TIMER_EVENT_COMPARE0);
@ -137,6 +139,9 @@ void us_ticker_clear_interrupt(void)
void us_ticker_free(void)
{
// A common counter is used for RTC, lp_ticker and us_ticker, so it can't be
// disabled here, but this does not cause any extra cost.
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_STOP);
nrf_timer_int_disable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0));
us_ticker_initialized = false;
}