Skip to content

Commit e251312

Browse files
authoredNov 29, 2022
Merge pull request #18990 from maribu/backport/2022.10/cpu/qn908x
cpu/qn908x: use bitarithm_test_and_clear() & fix cb [backport 2022.10]
2 parents c3e83d0 + f0572a5 commit e251312

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed
 

‎cpu/qn908x/periph/timer.c

+15-13
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525

2626
#include <stdlib.h>
2727

28-
#include "cpu.h"
28+
#include "bitarithm.h"
2929
#include "board.h"
30-
#include "periph_conf.h"
30+
#include "cpu.h"
3131
#include "periph/timer.h"
32+
#include "periph_conf.h"
3233

3334
#include "vendor/drivers/fsl_clock.h"
3435

@@ -66,9 +67,9 @@ static const clock_ip_name_t ctimers_clocks[FSL_FEATURE_SOC_CTIMER_COUNT] =
6667
#error "ERROR in board timer configuration: too many timers defined"
6768
#endif
6869

69-
int timer_init(tim_t tim, unsigned long freq, timer_cb_t cb, void *arg)
70+
int timer_init(tim_t tim, uint32_t freq, timer_cb_t cb, void *arg)
7071
{
71-
DEBUG("timer_init(%u, %lu)\n", tim, freq);
72+
DEBUG("timer_init(%u, %" PRIu32 ")\n", tim, freq);
7273
if (tim >= TIMER_NUMOF) {
7374
return -1;
7475
}
@@ -84,7 +85,7 @@ int timer_init(tim_t tim, unsigned long freq, timer_cb_t cb, void *arg)
8485
uint32_t core_freq = CLOCK_GetFreq(kCLOCK_ApbClk);
8586
uint32_t prescale = (core_freq + freq / 2) / freq - 1;
8687
if (prescale == (uint32_t)(-1)) {
87-
DEBUG("timer_init: Frequency %lu is too fast for core_freq=%lu",
88+
DEBUG("timer_init: Frequency %" PRIu32 " is too fast for core_freq=%" PRIu32,
8889
freq, core_freq);
8990
return -1;
9091
}
@@ -144,14 +145,15 @@ static inline void isr_ctimer_n(CTIMER_Type *dev, uint32_t ctimer_num)
144145
{
145146
DEBUG("isr_ctimer_%" PRIu32 " flags=0x%" PRIx32 "\n",
146147
ctimer_num, dev->IR);
147-
for (uint32_t i = 0; i < TIMER_CHANNELS; i++) {
148-
if (dev->IR & (1u << i)) {
149-
/* Note: setting the bit to 1 in the flag register will clear the
150-
* bit. */
151-
dev->IR = 1u << i;
152-
dev->MCR &= ~(CTIMER_MCR_MR0I_MASK << (i * 3));
153-
isr_ctx[ctimer_num].cb(isr_ctx[ctimer_num].arg, 0);
154-
}
148+
unsigned state = dev->IR & ((1 << TIMER_CHANNELS) - 1);
149+
while (state) {
150+
uint8_t channel;
151+
state = bitarithm_test_and_clear(state, &channel);
152+
/* Note: setting the bit to 1 in the flag register will clear the
153+
* bit. */
154+
dev->IR = 1u << channel;
155+
dev->MCR &= ~(CTIMER_MCR_MR0I_MASK << (channel * 3));
156+
isr_ctx[ctimer_num].cb(isr_ctx[ctimer_num].arg, channel);
155157
}
156158
cortexm_isr_end();
157159
}

0 commit comments

Comments
 (0)
Please sign in to comment.