Skip to content

Commit 601b96d

Browse files
committed
Restored alternative method of RTOS tick generation for NRF51, which was undesirably removed when the new us_ticker implementation was introduced.
1 parent 6c7d15d commit 601b96d

File tree

9 files changed

+436
-690
lines changed

9 files changed

+436
-690
lines changed

hal/targets/hal/TARGET_NORDIC/TARGET_NRF5/TARGET_MCU_NRF51822/sdk/nrf_drv_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
#define RTC0_INSTANCE_INDEX 0
139139
#endif
140140

141-
#define RTC1_ENABLED 1
141+
#define RTC1_ENABLED 0
142142

143143
#if (RTC1_ENABLED == 1)
144144
#define RTC1_CONFIG_FREQUENCY 32768

hal/targets/hal/TARGET_NORDIC/TARGET_NRF5/TARGET_MCU_NRF52832/sdk/nrf_drv_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
#define RTC0_INSTANCE_INDEX 0
139139
#endif
140140

141-
#define RTC1_ENABLED 1
141+
#define RTC1_ENABLED 0
142142

143143
#if (RTC1_ENABLED == 1)
144144
#define RTC1_CONFIG_FREQUENCY 32768

hal/targets/hal/TARGET_NORDIC/TARGET_NRF5/rtc_common.h renamed to hal/targets/hal/TARGET_NORDIC/TARGET_NRF5/common_rtc.h

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,26 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef RTC_COMMON_H
18-
#define RTC_COMMON_H
17+
#ifndef COMMON_RTC_H
18+
#define COMMON_RTC_H
1919

20-
#include "nrf_drv_rtc.h"
20+
#include "nrf_rtc.h"
2121

22-
#define RTC_COUNTER_BITS 24
22+
#define RTC_COUNTER_BITS 24u
2323

24-
#define LP_TICKER_CC_CHANNEL 0
25-
#define US_TICKER_CC_CHANNEL 1
24+
// Instance 0 is reserved for SoftDevice.
25+
// Instance 1 is used as a common one for us_ticker, lp_ticker and (in case
26+
// of NRF51) as an alternative tick source for RTOS.
27+
#define COMMON_RTC_INSTANCE NRF_RTC1
2628

27-
extern nrf_drv_rtc_t const m_rtc_common;
28-
extern bool m_rtc_common_enabled;
29-
extern uint32_t volatile m_rtc_common_overflows;
29+
#define US_TICKER_CC_CHANNEL 0
30+
#define OS_TICK_CC_CHANNEL 1
31+
#define LP_TICKER_CC_CHANNEL 2
3032

31-
void rtc_common_init(void);
32-
uint32_t rtc_common_32bit_ticks_get(void);
33+
extern bool m_common_rtc_enabled;
34+
extern uint32_t volatile m_common_rtc_overflows;
3335

34-
#endif // RTC_COMMON_H
36+
void common_rtc_init(void);
37+
uint32_t common_rtc_32bit_ticks_get(void);
38+
39+
#endif // COMMON_RTC_H

hal/targets/hal/TARGET_NORDIC/TARGET_NRF5/lp_ticker.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,37 @@
1717

1818
#if DEVICE_LOWPOWERTIMER
1919

20-
#include "rtc_common.h"
20+
#include "common_rtc.h"
2121
#include "sleep_api.h"
2222

2323
void lp_ticker_init(void)
2424
{
25-
rtc_common_init();
25+
common_rtc_init();
2626
}
2727

2828
uint32_t lp_ticker_read(void)
2929
{
30-
return rtc_common_32bit_ticks_get();
30+
return common_rtc_32bit_ticks_get();
3131
}
3232

3333
void lp_ticker_set_interrupt(uint32_t now, uint32_t time)
3434
{
3535
(void)now;
36-
// The passed 32-bit 'time' value is wrapped properly by the driver, so it
37-
// is usable by the 24-bit counter.
38-
ret_code_t result = nrf_drv_rtc_cc_set(&m_rtc_common, LP_TICKER_CC_CHANNEL,
39-
time, true);
40-
if (result != NRF_SUCCESS)
41-
{
42-
MBED_ASSERT(false);
43-
}
36+
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, LP_TICKER_CC_CHANNEL, RTC_WRAP(time));
37+
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_COMPARE_2);
38+
nrf_rtc_event_enable(COMMON_RTC_INSTANCE, NRF_RTC_INT_COMPARE2_MASK);
4439
}
4540

4641
uint32_t lp_ticker_get_overflows_counter(void)
4742
{
48-
// Cut out the part of 'm_rtc_common_overflows' used by
49-
// 'rtc_common_32bit_ticks_get()'.
50-
return (m_rtc_common_overflows >> (32 - RTC_COUNTER_BITS));
43+
// Cut out the part of 'm_common_rtc_overflows' used by
44+
// 'common_rtc_32bit_ticks_get()'.
45+
return (m_common_rtc_overflows >> (32u - RTC_COUNTER_BITS));
5146
}
5247

5348
uint32_t lp_ticker_get_compare_match(void)
5449
{
55-
return nrf_rtc_cc_get(m_rtc_common.p_reg, LP_TICKER_CC_CHANNEL);
50+
return nrf_rtc_cc_get(COMMON_RTC_INSTANCE, LP_TICKER_CC_CHANNEL);
5651
}
5752

5853
void lp_ticker_sleep_until(uint32_t now, uint32_t time)

0 commit comments

Comments
 (0)