Skip to content

Commit 9ef0083

Browse files
mprsebulislaw
authored andcommitted
NRF52_DK: use new ticker driver designed for NRF51_DK
1 parent fb622a2 commit 9ef0083

File tree

5 files changed

+343
-257
lines changed

5 files changed

+343
-257
lines changed
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/*
2+
* Copyright (c) 2013 Nordic Semiconductor ASA
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this list
9+
* of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
12+
* integrated circuit in a product or a software update for such product, must reproduce
13+
* the above copyright notice, this list of conditions and the following disclaimer in
14+
* the documentation and/or other materials provided with the distribution.
15+
*
16+
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
17+
* used to endorse or promote products derived from this software without specific prior
18+
* written permission.
19+
*
20+
* 4. This software, with or without modification, must only be used with a
21+
* Nordic Semiconductor ASA integrated circuit.
22+
*
23+
* 5. Any software provided in binary or object form under this license must not be reverse
24+
* engineered, decompiled, modified and/or disassembled.
25+
*
26+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
27+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
30+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
33+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36+
*
37+
*/
38+
39+
#include "us_ticker_api.h"
40+
#include "common_rtc.h"
41+
#include "app_util.h"
42+
#include "nrf_drv_common.h"
43+
#include "lp_ticker_api.h"
44+
#include "mbed_critical.h"
45+
46+
#if defined(NRF52_ERRATA_20)
47+
#include "softdevice_handler.h"
48+
#endif
49+
50+
51+
//------------------------------------------------------------------------------
52+
// Common stuff used also by lp_ticker and rtc_api (see "common_rtc.h").
53+
//
54+
#include "app_util_platform.h"
55+
56+
bool m_common_rtc_enabled = false;
57+
uint32_t volatile m_common_rtc_overflows = 0;
58+
bool volatile lp_ticker_interrupt_fire = false;
59+
60+
__STATIC_INLINE void rtc_ovf_event_check(void)
61+
{
62+
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW)) {
63+
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW);
64+
/* Don't disable this event. It shall occur periodically.
65+
* It is needed for RTC. */
66+
67+
++m_common_rtc_overflows;
68+
}
69+
}
70+
71+
#if defined(TARGET_MCU_NRF51822)
72+
void common_rtc_irq_handler(void)
73+
#else
74+
void COMMON_RTC_IRQ_HANDLER(void)
75+
#endif
76+
{
77+
rtc_ovf_event_check();
78+
79+
#if DEVICE_LPTICKER
80+
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, LP_TICKER_EVENT) ||
81+
lp_ticker_interrupt_fire) {
82+
83+
if (lp_ticker_interrupt_fire) {
84+
lp_ticker_interrupt_fire = false;
85+
}
86+
87+
lp_ticker_irq_handler();
88+
}
89+
#endif
90+
}
91+
92+
/* Function for fix errata 20: RTC Register values are invalid. */
93+
__STATIC_INLINE void errata_20(void)
94+
{
95+
#if defined(NRF52_ERRATA_20)
96+
if (!softdevice_handler_is_enabled())
97+
{
98+
NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
99+
NRF_CLOCK->TASKS_LFCLKSTART = 1;
100+
101+
while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
102+
{
103+
}
104+
}
105+
NRF_RTC1->TASKS_STOP = 0;
106+
#endif
107+
}
108+
109+
void RTC1_IRQHandler(void);
110+
111+
void common_rtc_init(void)
112+
{
113+
if (m_common_rtc_enabled) {
114+
#if DEVICE_LPTICKER
115+
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, LP_TICKER_EVENT);
116+
nrf_rtc_int_disable(COMMON_RTC_INSTANCE, LP_TICKER_INT_MASK);
117+
#endif
118+
return;
119+
}
120+
121+
errata_20();
122+
123+
nrf_rtc_task_trigger(COMMON_RTC_INSTANCE, NRF_RTC_TASK_STOP);
124+
125+
NVIC_SetVector(RTC1_IRQn, (uint32_t)RTC1_IRQHandler);
126+
127+
/* RTC is driven by the low frequency (32.768 kHz) clock, a proper request
128+
* must be made to have it running.
129+
* Currently this clock is started in 'SystemInit' (see "system_nrf51.c"
130+
* or "system_nrf52.c", respectively).
131+
*/
132+
133+
nrf_rtc_prescaler_set(COMMON_RTC_INSTANCE, 0);
134+
135+
/* Clear all RTC events. */
136+
#if defined(TARGET_MCU_NRF51822)
137+
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, OS_TICK_EVENT);
138+
#endif
139+
#if DEVICE_LPTICKER
140+
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, LP_TICKER_EVENT);
141+
#endif
142+
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW);
143+
144+
/* Disable all RTC events (According to NRF_51 Reference Manual
145+
* RTC events can not be used to control RTC interrupts).
146+
* IRQ signal to NVIC is provided if interrupt is enabled.
147+
*/
148+
149+
nrf_rtc_event_disable(COMMON_RTC_INSTANCE, NRF_RTC_INT_OVERFLOW_MASK
150+
#if defined(TARGET_MCU_NRF51822)
151+
| OS_TICK_INT_MASK
152+
#endif
153+
#if DEVICE_LPTICKER
154+
| LP_TICKER_INT_MASK
155+
#endif
156+
);
157+
158+
/* This interrupt is enabled permanently, since overflow indications are needed
159+
* continuously.
160+
*/
161+
nrf_rtc_int_enable(COMMON_RTC_INSTANCE, NRF_RTC_INT_OVERFLOW_MASK);
162+
163+
/* Disable LP ticker interrupt for now. */
164+
#if DEVICE_LPTICKER
165+
nrf_rtc_int_disable(COMMON_RTC_INSTANCE, LP_TICKER_INT_MASK);
166+
#endif
167+
168+
nrf_drv_common_irq_enable(nrf_drv_get_IRQn(COMMON_RTC_INSTANCE),
169+
#ifdef NRF51
170+
APP_IRQ_PRIORITY_LOW
171+
#elif defined(NRF52) || defined(NRF52840_XXAA)
172+
APP_IRQ_PRIORITY_LOWEST
173+
#endif
174+
);
175+
176+
nrf_rtc_task_trigger(COMMON_RTC_INSTANCE, NRF_RTC_TASK_START);
177+
178+
m_common_rtc_enabled = true;
179+
}
180+
181+
void common_rtc_set_interrupt(uint32_t ticks_count, uint32_t cc_channel,
182+
uint32_t int_mask)
183+
{
184+
/* Set ticks value when interrupt should be fired.
185+
* Interrupt scheduling is performed in upper layers. */
186+
187+
core_util_critical_section_enter();
188+
189+
/* COMPARE occurs when a CC register is N and the COUNTER value transitions from N-1 to N.
190+
* If the COUNTER is N, writing N+2 to a CC register is guaranteed to trigger a
191+
* COMPARE event at N+2.
192+
*/
193+
const uint32_t now = nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
194+
195+
if (now == ticks_count ||
196+
RTC_WRAP(now + 1) == ticks_count) {
197+
ticks_count += 2;
198+
}
199+
200+
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, RTC_WRAP(ticks_count));
201+
202+
if (!nrf_rtc_int_is_enabled(COMMON_RTC_INSTANCE, int_mask)) {
203+
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, LP_TICKER_EVENT);
204+
nrf_rtc_int_enable(COMMON_RTC_INSTANCE, int_mask);
205+
}
206+
207+
core_util_critical_section_exit();
208+
}
209+
210+
/* Since there is no SysTick on NRF51, the RTC1 channel 0 is used as an
211+
* alternative source of RTOS ticks.
212+
*/
213+
#if defined(TARGET_MCU_NRF51822)
214+
215+
#include "mbed_toolchain.h"
216+
217+
218+
#define MAX_RTC_COUNTER_VAL ((1uL << RTC_COUNTER_BITS) - 1)
219+
220+
#ifndef RTC1_CONFIG_FREQUENCY
221+
#define RTC1_CONFIG_FREQUENCY 32678 // [Hz]
222+
#endif
223+
224+
225+
226+
void COMMON_RTC_IRQ_HANDLER(void)
227+
{
228+
if(!nrf_rtc_event_pending(COMMON_RTC_INSTANCE, OS_TICK_EVENT)) {
229+
common_rtc_irq_handler();
230+
}
231+
}
232+
233+
IRQn_Type mbed_get_m0_tick_irqn()
234+
{
235+
return SWI3_IRQn;
236+
}
237+
238+
239+
#endif // defined(TARGET_MCU_NRF51822)

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/common_rtc.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,29 @@
2020
#include "nrf_rtc.h"
2121

2222
#define RTC_COUNTER_BITS 24u
23+
#define RTC_FREQ 32768u
2324

2425
// Instance 0 is reserved for SoftDevice.
25-
// Instance 1 is used as a common one for us_ticker, lp_ticker and (in case
26+
// Instance 1 is used as a common one for lp_ticker and (in case
2627
// of NRF51) as an alternative tick source for RTOS.
27-
// ["us_ticker.c" uses hard coded addresses of the 'NRF_RTC1->EVENT_COMPARE[1]'
28-
// register in inline assembly implementations of COMMON_RTC_IRQ_HANDLER,
29-
// please remember to update those in case of doing changes here]
3028
#define COMMON_RTC_INSTANCE NRF_RTC1
3129
#define COMMON_RTC_IRQ_HANDLER RTC1_IRQHandler
32-
#define US_TICKER_CC_CHANNEL 0
33-
#define OS_TICK_CC_CHANNEL 1
34-
#define LP_TICKER_CC_CHANNEL 2
35-
36-
#define US_TICKER_SW_IRQ_MASK 0x1
37-
#define LP_TICKER_SW_IRQ_MASK 0x2
30+
#define OS_TICK_CC_CHANNEL 0
31+
#define LP_TICKER_CC_CHANNEL 1
3832

3933
#define COMMON_RTC_EVENT_COMPARE(channel) \
4034
CONCAT_2(NRF_RTC_EVENT_COMPARE_, channel)
4135
#define COMMON_RTC_INT_COMPARE_MASK(channel) \
4236
CONCAT_3(NRF_RTC_INT_COMPARE, channel, _MASK)
4337

44-
#define US_TICKER_EVENT COMMON_RTC_EVENT_COMPARE(US_TICKER_CC_CHANNEL)
45-
#define US_TICKER_INT_MASK COMMON_RTC_INT_COMPARE_MASK(US_TICKER_CC_CHANNEL)
4638
#define OS_TICK_EVENT COMMON_RTC_EVENT_COMPARE(OS_TICK_CC_CHANNEL)
4739
#define OS_TICK_INT_MASK COMMON_RTC_INT_COMPARE_MASK(OS_TICK_CC_CHANNEL)
4840
#define LP_TICKER_EVENT COMMON_RTC_EVENT_COMPARE(LP_TICKER_CC_CHANNEL)
4941
#define LP_TICKER_INT_MASK COMMON_RTC_INT_COMPARE_MASK(LP_TICKER_CC_CHANNEL)
5042

5143
extern bool m_common_rtc_enabled;
5244
extern uint32_t volatile m_common_rtc_overflows;
53-
extern uint8_t volatile m_common_sw_irq_flag;
45+
extern bool volatile lp_ticker_interrupt_fire;
5446

5547
void common_rtc_init(void);
5648
uint32_t common_rtc_32bit_ticks_get(void);

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/lp_ticker.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,35 @@
1414
* limitations under the License.
1515
*/
1616
#include "lp_ticker_api.h"
17+
#include "common_rtc.h"
18+
#include "platform/mbed_critical.h"
1719

18-
#if DEVICE_LOWPOWERTIMER
20+
#if DEVICE_LPTICKER
1921

20-
#include "common_rtc.h"
21-
#include "mbed_critical.h"
22+
/* LP ticker is driven by 32kHz clock and counter length is 24 bits. */
23+
const ticker_info_t* lp_ticker_get_info()
24+
{
25+
static const ticker_info_t info = {
26+
RTC_FREQ,
27+
RTC_COUNTER_BITS
28+
};
29+
return &info;
30+
}
2231

2332
void lp_ticker_init(void)
2433
{
2534
common_rtc_init();
2635
}
2736

37+
void lp_ticker_free(void)
38+
{
39+
// A common counter is used for RTC, lp_ticker and us_ticker, so it can't be
40+
// disabled here, but this does not cause any extra cost.
41+
}
42+
2843
uint32_t lp_ticker_read()
2944
{
30-
return (uint32_t)common_rtc_64bit_us_get();
45+
return nrf_rtc_counter_get(COMMON_RTC_INSTANCE);
3146
}
3247

3348
void lp_ticker_set_interrupt(timestamp_t timestamp)
@@ -39,19 +54,22 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
3954
void lp_ticker_fire_interrupt(void)
4055
{
4156
core_util_critical_section_enter();
42-
m_common_sw_irq_flag |= LP_TICKER_SW_IRQ_MASK;
57+
58+
lp_ticker_interrupt_fire = true;
59+
4360
NVIC_SetPendingIRQ(RTC1_IRQn);
61+
4462
core_util_critical_section_exit();
4563
}
4664

4765
void lp_ticker_disable_interrupt(void)
4866
{
49-
nrf_rtc_event_disable(COMMON_RTC_INSTANCE, LP_TICKER_INT_MASK);
67+
nrf_rtc_int_disable(COMMON_RTC_INSTANCE, LP_TICKER_INT_MASK);
5068
}
5169

5270
void lp_ticker_clear_interrupt(void)
5371
{
5472
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, LP_TICKER_EVENT);
5573
}
5674

57-
#endif // DEVICE_LOWPOWERTIMER
75+
#endif // DEVICE_LPTICKER

0 commit comments

Comments
 (0)