Skip to content

Commit d006eaa

Browse files
0xc0170fkjagodzinski
authored andcommitted
us ticker: fix fire interrupt handling
Few targets need more than just pending IRQ set. They include some flags to be set that are checked in IRQ handler. This is the case for targets in this commit.
1 parent 6e32343 commit d006eaa

File tree

11 files changed

+53
-14
lines changed

11 files changed

+53
-14
lines changed

targets/TARGET_ARM_SSG/TARGET_BEETLE/us_ticker.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void us_ticker_set_interrupt(timestamp_t timestamp) {
100100

101101
void us_ticker_fire_interrupt(void)
102102
{
103-
uint32_t us_ticker_irqn1 = Timer_GetIRQn(TIMER1);
103+
uint32_t us_ticker_irqn1 = Timer_GetIRQn(TIMER0);
104104
NVIC_SetPendingIRQ((IRQn_Type)us_ticker_irqn1);
105105
}
106106

targets/TARGET_Freescale/TARGET_KLXX/us_ticker.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ void us_ticker_set_interrupt(timestamp_t timestamp) {
199199

200200
void us_ticker_fire_interrupt(void)
201201
{
202+
us_ticker_int_counter = 0;
203+
us_ticker_int_remainder = 0;
204+
202205
#if defined(TARGET_KL43Z)
203206
NVIC_SetPendingIRQ(LPTMR0_IRQn);
204207
#else

targets/TARGET_Maxim/TARGET_MAX32600/us_ticker.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
236236

237237
void us_ticker_fire_interrupt(void)
238238
{
239-
NVIC_SetPendingIRQ(US_TIMER_IRQn);
239+
US_TIMER->ctrl &= ~MXC_F_TMR_CTRL_ENABLE0; // disable timer
240+
US_TIMER->term_cnt32 = 1;
241+
US_TIMER->ctrl |= MXC_F_TMR_CTRL_ENABLE0; // enable timer
240242
}
241243

242244
//******************************************************************************

targets/TARGET_Maxim/TARGET_MAX32610/us_ticker.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
236236

237237
void us_ticker_fire_interrupt(void)
238238
{
239-
NVIC_SetPendingIRQ(US_TIMER_IRQn);
239+
US_TIMER->ctrl &= ~MXC_F_TMR_CTRL_ENABLE0; // disable timer
240+
US_TIMER->term_cnt32 = 1;
241+
US_TIMER->ctrl |= MXC_F_TMR_CTRL_ENABLE0; // enable timer
240242
}
241243

242244
//******************************************************************************

targets/TARGET_Maxim/TARGET_MAX32620/us_ticker.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
267267

268268
void us_ticker_fire_interrupt(void)
269269
{
270-
NVIC_SetPendingIRQ(US_TIMER_IRQn);
270+
US_TIMER->ctrl &= ~MXC_F_TMR_CTRL_ENABLE0; // disable timer
271+
US_TIMER->term_cnt32 = 1;
272+
US_TIMER->ctrl |= MXC_F_TMR_CTRL_ENABLE0; // enable timer
271273
}
272274

273275
//******************************************************************************

targets/TARGET_Maxim/TARGET_MAX32625/us_ticker.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
230230

231231
void us_ticker_fire_interrupt(void)
232232
{
233-
NVIC_SetPendingIRQ(US_TIMER_IRQn);
233+
TMR32_SetCompare(US_TIMER, 1);
234234
}
235235

236236
//******************************************************************************

targets/TARGET_Maxim/TARGET_MAX32630/us_ticker.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
230230

231231
void us_ticker_fire_interrupt(void)
232232
{
233-
NVIC_SetPendingIRQ(US_TIMER_IRQn);
233+
TMR32_SetCompare(US_TIMER, 1);
234234
}
235235

236236
//******************************************************************************

targets/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "PeripheralNames.h"
2121
#include "nrf_delay.h"
2222
#include "mbed_toolchain.h"
23+
#include "mbed_critical.h"
2324

2425
/*
2526
* Note: The micro-second timer API on the nRF51 platform is implemented using
@@ -52,6 +53,8 @@
5253
#define RTC_UNITS_TO_MICROSECONDS(RTC_UNITS) (((RTC_UNITS) * (uint64_t)1000000) / RTC_CLOCK_FREQ)
5354
#define MICROSECONDS_TO_RTC_UNITS(MICROS) ((((uint64_t)(MICROS) * RTC_CLOCK_FREQ) + 999999) / 1000000)
5455

56+
#define US_TICKER_SW_IRQ_MASK 0x1
57+
5558
static bool us_ticker_inited = false;
5659
static volatile uint32_t overflowCount; /**< The number of times the 24-bit RTC counter has overflowed. */
5760
static volatile bool us_ticker_callbackPending = false;
@@ -62,6 +65,9 @@ static bool os_tick_started = false; /**< flag indicating i
6265
*/
6366
static uint32_t previous_tick_cc_value = 0;
6467

68+
// us ticker fire interrupt flag for IRQ handler
69+
volatile uint8_t m_common_sw_irq_flag = 0;
70+
6571
/*
6672
RTX provide the following definitions which are used by the tick code:
6773
* os_trv: The number (minus 1) of clock cycle between two tick.
@@ -181,6 +187,11 @@ static inline uint32_t rtc1_getCounter(void)
181187
*/
182188
void us_ticker_handler(void)
183189
{
190+
if (m_common_sw_irq_flag & US_TICKER_SW_IRQ_MASK) {
191+
m_common_sw_irq_flag &= ~US_TICKER_SW_IRQ_MASK;
192+
us_ticker_irq_handler();
193+
}
194+
184195
if (NRF_RTC1->EVENTS_OVRFLW) {
185196
overflowCount++;
186197
NRF_RTC1->EVENTS_OVRFLW = 0;
@@ -287,7 +298,10 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
287298

288299
void us_ticker_fire_interrupt(void)
289300
{
301+
core_util_critical_section_enter();
302+
m_common_sw_irq_flag |= US_TICKER_SW_IRQ_MASK;
290303
NVIC_SetPendingIRQ(RTC1_IRQn);
304+
core_util_critical_section_exit();
291305
}
292306

293307
void us_ticker_disable_interrupt(void)

targets/TARGET_NORDIC/TARGET_NRF5/common_rtc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#define OS_TICK_CC_CHANNEL 1
3434
#define LP_TICKER_CC_CHANNEL 2
3535

36+
#define US_TICKER_SW_IRQ_MASK 0x1
37+
#define LP_TICKER_SW_IRQ_MASK 0x2
38+
3639
#define COMMON_RTC_EVENT_COMPARE(channel) \
3740
CONCAT_2(NRF_RTC_EVENT_COMPARE_, channel)
3841
#define COMMON_RTC_INT_COMPARE_MASK(channel) \
@@ -47,6 +50,7 @@
4750

4851
extern bool m_common_rtc_enabled;
4952
extern uint32_t volatile m_common_rtc_overflows;
53+
extern uint8_t volatile m_common_sw_irq_flag;
5054

5155
void common_rtc_init(void);
5256
uint32_t common_rtc_32bit_ticks_get(void);

targets/TARGET_NORDIC/TARGET_NRF5/lp_ticker.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#if DEVICE_LOWPOWERTIMER
1919

2020
#include "common_rtc.h"
21+
#include "mbed_critical.h"
2122

2223
void lp_ticker_init(void)
2324
{
@@ -37,10 +38,10 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
3738

3839
void lp_ticker_fire_interrupt(void)
3940
{
40-
uint32_t closest_safe_compare = common_rtc_32bit_ticks_get() + 2;
41-
42-
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, LP_TICKER_CC_CHANNEL, RTC_WRAP(closest_safe_compare));
43-
nrf_rtc_event_enable(COMMON_RTC_INSTANCE, LP_TICKER_INT_MASK);
41+
core_util_critical_section_enter();
42+
m_common_sw_irq_flag |= LP_TICKER_SW_IRQ_MASK;
43+
NVIC_SetPendingIRQ(RTC1_IRQn);
44+
core_util_critical_section_exit();
4445
}
4546

4647
void lp_ticker_disable_interrupt(void)

targets/TARGET_NORDIC/TARGET_NRF5/us_ticker.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
bool m_common_rtc_enabled = false;
5656
uint32_t volatile m_common_rtc_overflows = 0;
5757

58+
// lp/us ticker fire interrupt flag for IRQ handler
59+
volatile uint8_t m_common_sw_irq_flag = 0;
60+
5861
__STATIC_INLINE void rtc_ovf_event_check(void)
5962
{
6063
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW)) {
@@ -74,11 +77,19 @@ void COMMON_RTC_IRQ_HANDLER(void)
7477

7578
rtc_ovf_event_check();
7679

80+
if (m_common_sw_irq_flag & US_TICKER_SW_IRQ_MASK) {
81+
m_common_sw_irq_flag &= ~US_TICKER_SW_IRQ_MASK;
82+
us_ticker_irq_handler();
83+
}
7784
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, US_TICKER_EVENT)) {
7885
us_ticker_irq_handler();
7986
}
8087

8188
#if DEVICE_LOWPOWERTIMER
89+
if (m_common_sw_irq_flag & LP_TICKER_SW_IRQ_MASK) {
90+
m_common_sw_irq_flag &= ~LP_TICKER_SW_IRQ_MASK;
91+
lp_ticker_irq_handler();
92+
}
8293
if (nrf_rtc_event_pending(COMMON_RTC_INSTANCE, LP_TICKER_EVENT)) {
8394

8495
lp_ticker_irq_handler();
@@ -273,10 +284,10 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
273284

274285
void us_ticker_fire_interrupt(void)
275286
{
276-
uint32_t closest_safe_compare = common_rtc_32bit_ticks_get() + 2;
277-
278-
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, US_TICKER_CC_CHANNEL, RTC_WRAP(closest_safe_compare));
279-
nrf_rtc_event_enable(COMMON_RTC_INSTANCE, US_TICKER_INT_MASK);
287+
core_util_critical_section_enter();
288+
m_common_sw_irq_flag |= US_TICKER_SW_IRQ_MASK;
289+
NVIC_SetPendingIRQ(RTC1_IRQn);
290+
core_util_critical_section_exit();
280291
}
281292

282293
void us_ticker_disable_interrupt(void)

0 commit comments

Comments
 (0)