Skip to content

Commit ba5bbb2

Browse files
dspic: Timer bug fix (zephyrproject-rtos#29)
Fixed the issue in calculating the elapsed time Signed-off-by: Udhayanandhan Jayakumar <[email protected]>
1 parent 775ca4d commit ba5bbb2

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

drivers/timer/mchp_dspic33_timer.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,25 @@ static struct k_spinlock lock;
2121

2222
uint8_t map_prescaler_to_bits(uint32_t val)
2323
{
24+
uint8_t ret_val;
2425
switch (val) {
2526
case 1:
26-
return 0b00;
27+
ret_val = 0b00;
28+
break;
2729
case 8:
28-
return 0b01;
30+
ret_val = 0b01;
31+
break;
2932
case 64:
30-
return 0b10;
33+
ret_val = 0b10;
34+
break;
3135
case 128:
32-
return 0b11;
36+
ret_val = 0b11;
37+
break;
3338
default:
34-
return 0b00;
39+
ret_val = 0b00;
40+
break;
3541
}
42+
return ret_val;
3643
}
3744

3845
static void configure_timer1(void)
@@ -78,7 +85,9 @@ uint32_t sys_clock_elapsed(void)
7885
Since timer1 counter always starts from 0 when the sys_clock_announce
7986
Call is made, the ticks elapsed is current timer1 count divided by
8087
Number of cycles per tick */
81-
ticks_elapsed = (uint32_t)TMR1 / (uint32_t)TIMER1_CYCLES_PER_TICK;
88+
ticks_elapsed = (uint32_t)TMR1 < (uint32_t)TIMER1_CYCLES_PER_TICK
89+
? 0
90+
: (uint32_t)TMR1 / (uint32_t)TIMER1_CYCLES_PER_TICK;
8291
k_spin_unlock(&lock, key);
8392
} while (0);
8493

dts/dspic/p33ak128mc106.dtsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
timer1: timer@1E00 {
6363
compatible = "microchip,dspic33-timer";
6464
reg = <0x1E00 0xC>;
65-
clock-frequency = <8000000>;
65+
clock-frequency = <4000000>;
6666
prescalar = <8>;
6767
interrupt-parent = <&intc0>;
6868
interrupts = <48 1>;

0 commit comments

Comments
 (0)