Skip to content

Commit 6a31d1a

Browse files
authored
Merge pull request #14 from anangl/hal_improvements
Enabled lp_ticker after adjusting its implementation to the new API.
2 parents fb7b463 + 75ba6b7 commit 6a31d1a

File tree

4 files changed

+49
-62
lines changed

4 files changed

+49
-62
lines changed

hal/targets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@
13751375
"supported_form_factors": ["ARDUINO"],
13761376
"inherits": ["MCU_NRF51_32K"],
13771377
"progen": {"target": "nrf51-dk"},
1378-
"device_has": ["ANALOGIN", "ERROR_PATTERN", "I2C", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE"]
1378+
"device_has": ["ANALOGIN", "ERROR_PATTERN", "I2C", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE"]
13791379
},
13801380
"NRF51_DK_BOOT": {
13811381
"supported_form_factors": ["ARDUINO"],
@@ -1773,6 +1773,6 @@
17731773
"NRF52_PAN_62",
17741774
"NRF52_PAN_63"
17751775
],
1776-
"device_has": ["ANALOGIN", "ERROR_PATTERN", "I2C", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE"]
1776+
"device_has": ["ANALOGIN", "ERROR_PATTERN", "I2C", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE"]
17771777
}
17781778
}

hal/targets/hal/TARGET_NORDIC/TARGET_NRF5/common_rtc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@ extern uint32_t volatile m_common_rtc_overflows;
5050

5151
void common_rtc_init(void);
5252
uint32_t common_rtc_32bit_ticks_get(void);
53+
uint64_t common_rtc_64bit_us_get(void);
54+
void common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,
55+
uint32_t int_mask);
5356

5457
#endif // COMMON_RTC_H

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,31 @@
1818
#if DEVICE_LOWPOWERTIMER
1919

2020
#include "common_rtc.h"
21-
#include "sleep_api.h"
2221

2322
void lp_ticker_init(void)
2423
{
2524
common_rtc_init();
2625
}
2726

28-
uint32_t lp_ticker_read(void)
27+
uint32_t lp_ticker_read()
2928
{
30-
return common_rtc_32bit_ticks_get();
29+
return (uint32_t)common_rtc_64bit_us_get();
3130
}
3231

33-
void lp_ticker_set_interrupt(uint32_t now, uint32_t time)
32+
void lp_ticker_set_interrupt(timestamp_t timestamp)
3433
{
35-
(void)now;
36-
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, LP_TICKER_CC_CHANNEL, RTC_WRAP(time));
37-
nrf_rtc_event_enable(COMMON_RTC_INSTANCE, LP_TICKER_INT_MASK);
34+
common_rtc_set_interrupt(timestamp,
35+
LP_TICKER_CC_CHANNEL, LP_TICKER_INT_MASK);
3836
}
3937

40-
uint32_t lp_ticker_get_overflows_counter(void)
38+
void lp_ticker_disable_interrupt(void)
4139
{
42-
// Cut out the part of 'm_common_rtc_overflows' used by
43-
// 'common_rtc_32bit_ticks_get()'.
44-
return (m_common_rtc_overflows >> (32u - RTC_COUNTER_BITS));
40+
nrf_rtc_event_disable(COMMON_RTC_INSTANCE, LP_TICKER_INT_MASK);
4541
}
4642

47-
uint32_t lp_ticker_get_compare_match(void)
43+
void lp_ticker_clear_interrupt(void)
4844
{
49-
return nrf_rtc_cc_get(COMMON_RTC_INSTANCE, LP_TICKER_CC_CHANNEL);
50-
}
51-
52-
void lp_ticker_sleep_until(uint32_t now, uint32_t time)
53-
{
54-
lp_ticker_set_interrupt(now, time);
55-
sleep_t sleep_obj;
56-
mbed_enter_sleep(&sleep_obj);
57-
mbed_exit_sleep(&sleep_obj);
45+
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, LP_TICKER_EVENT);
5846
}
5947

6048
#endif // DEVICE_LOWPOWERTIMER

hal/targets/hal/TARGET_NORDIC/TARGET_NRF5/us_ticker.c

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "us_ticker_api.h"
4040
#include "common_rtc.h"
4141
#include "app_util.h"
42+
#include "lp_ticker_api.h"
4243

4344

4445
//------------------------------------------------------------------------------
@@ -56,30 +57,19 @@ void common_rtc_irq_handler(void)
5657
void COMMON_RTC_IRQ_HANDLER(void)
5758
#endif
5859
{
59-
nrf_rtc_event_t event;
60-
uint32_t int_mask;
61-
62-
event = US_TICKER_EVENT;
63-
int_mask = US_TICKER_INT_MASK;
64-
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, event)) {
65-
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, event);
66-
nrf_rtc_event_disable(COMMON_RTC_INSTANCE, int_mask);
67-
60+
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, US_TICKER_EVENT)) {
6861
us_ticker_irq_handler();
6962
}
7063

7164
#if DEVICE_LOWPOWERTIMER
72-
event = LP_TICKER_EVENT;
73-
int_mask = LP_TICKER_INT_MASK;
74-
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, event)) {
75-
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, event);
76-
nrf_rtc_event_disable(COMMON_RTC_INSTANCE, int_mask);
65+
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, LP_TICKER_EVENT)) {
66+
67+
lp_ticker_irq_handler();
7768
}
7869
#endif
7970

80-
event = NRF_RTC_EVENT_OVERFLOW;
81-
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, event)) {
82-
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, event);
71+
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW)) {
72+
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW);
8373
// Don't disable this event. It shall occur periodically.
8474

8575
++m_common_rtc_overflows;
@@ -151,27 +141,16 @@ uint32_t common_rtc_32bit_ticks_get(void)
151141
ticks += (m_common_rtc_overflows << RTC_COUNTER_BITS);
152142
return ticks;
153143
}
154-
//------------------------------------------------------------------------------
155-
156-
157-
void us_ticker_init(void)
158-
{
159-
common_rtc_init();
160-
}
161144

162-
static uint64_t us_ticker_64bit_get(void)
145+
uint64_t common_rtc_64bit_us_get(void)
163146
{
164147
uint32_t ticks = common_rtc_32bit_ticks_get();
165148
// [ticks -> microseconds]
166149
return ROUNDED_DIV(((uint64_t)ticks) * 1000000, RTC_INPUT_FREQ);
167150
}
168151

169-
uint32_t us_ticker_read()
170-
{
171-
return (uint32_t)us_ticker_64bit_get();
172-
}
173-
174-
void us_ticker_set_interrupt(timestamp_t timestamp)
152+
void common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,
153+
uint32_t int_mask)
175154
{
176155
// The internal counter is clocked with a frequency that cannot be easily
177156
// multiplied to 1 MHz, therefore besides the translation of values
@@ -182,12 +161,13 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
182161
// is then translated to counter ticks. Finally, the lower 24 bits of thus
183162
// calculated value is written to the counter compare register to prepare
184163
// the interrupt generation.
185-
uint64_t current_time64 = us_ticker_64bit_get();
164+
uint64_t current_time64 = common_rtc_64bit_us_get();
186165
// [add upper 32 bits from the current time to the timestamp value]
187-
uint64_t timestamp64 = timestamp + (current_time64 & ~(uint64_t)0xFFFFFFFF);
166+
uint64_t timestamp64 = us_timestamp +
167+
(current_time64 & ~(uint64_t)0xFFFFFFFF);
188168
// [if the original timestamp value happens to be after the 32 bit counter
189169
// of microsends overflows, correct the upper 32 bits accordingly]
190-
if (timestamp < (uint32_t)(current_time64 & 0xFFFFFFFF)) {
170+
if (us_timestamp < (uint32_t)(current_time64 & 0xFFFFFFFF)) {
191171
timestamp64 += ((uint64_t)1 << 32);
192172
}
193173
// [microseconds -> ticks, always round the result up to avoid too early
@@ -205,9 +185,26 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
205185
compare_value = closest_safe_compare;
206186
}
207187

208-
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, US_TICKER_CC_CHANNEL,
209-
RTC_WRAP(compare_value));
210-
nrf_rtc_event_enable(COMMON_RTC_INSTANCE, US_TICKER_INT_MASK);
188+
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, RTC_WRAP(compare_value));
189+
nrf_rtc_event_enable(COMMON_RTC_INSTANCE, int_mask);
190+
}
191+
//------------------------------------------------------------------------------
192+
193+
194+
void us_ticker_init(void)
195+
{
196+
common_rtc_init();
197+
}
198+
199+
uint32_t us_ticker_read()
200+
{
201+
return (uint32_t)common_rtc_64bit_us_get();
202+
}
203+
204+
void us_ticker_set_interrupt(timestamp_t timestamp)
205+
{
206+
common_rtc_set_interrupt(timestamp,
207+
US_TICKER_CC_CHANNEL, US_TICKER_INT_MASK);
211208
}
212209

213210
void us_ticker_disable_interrupt(void)
@@ -217,6 +214,5 @@ void us_ticker_disable_interrupt(void)
217214

218215
void us_ticker_clear_interrupt(void)
219216
{
220-
// No implementation needed. The event that triggers the interrupt is
221-
// cleared in 'common_rtc_irq_handler'.
217+
nrf_rtc_event_clear(COMMON_RTC_INSTANCE, US_TICKER_EVENT);
222218
}

0 commit comments

Comments
 (0)