Skip to content

Commit 9cec32b

Browse files
committed
fix pit timer, but vtimer still not working
1 parent 11646c9 commit 9cec32b

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

cpu/mkw2x/periph/timer.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
/** Type for timer state */
3333
typedef struct {
3434
void (*cb)(int);
35+
uint32_t value;
3536
} timer_conf_t;
3637

3738
/** Timer state memory */
@@ -87,6 +88,7 @@ int timer_init(tim_t dev, unsigned int ticks_per_us, void (*callback)(int))
8788

8889
/* set callback function */
8990
config[dev].cb = callback;
91+
config[dev].value = 0;
9092

9193
/* enable the timer's interrupt */
9294
timer_irq_enable(dev);
@@ -99,13 +101,14 @@ int timer_init(tim_t dev, unsigned int ticks_per_us, void (*callback)(int))
99101

100102
int timer_set(tim_t dev, int channel, unsigned int timeout)
101103
{
102-
//int now = timer_read(dev);
103-
//return timer_set_absolute(dev, channel, now + timeout - 1);
104-
return timer_set_absolute(dev, channel, timeout);
104+
int now = timer_read(dev);
105+
return timer_set_absolute(dev, channel, now + timeout - 1);
106+
//return timer_set_absolute(dev, channel, timeout);
105107
}
106108

107109
int timer_set_absolute(tim_t dev, int channel, unsigned int value)
108110
{
111+
config[dev].value = value;
109112
switch (dev) {
110113
#if TIMER_0_EN
111114
case TIMER_0:
@@ -135,6 +138,7 @@ int timer_clear(tim_t dev, int channel)
135138
switch (dev) {
136139
#if TIMER_0_EN
137140
case TIMER_0:
141+
config[dev].value = 0;
138142
TIMER_0_DEV->CHANNEL[1].TCTRL |= (1 << PIT_TCTRL_TIE_SHIFT);
139143
TIMER_0_DEV->CHANNEL[1].TFLG = (1 << PIT_TFLG_TIF_SHIFT);
140144
break;
@@ -158,12 +162,12 @@ unsigned int timer_read(tim_t dev)
158162
switch (dev) {
159163
#if TIMER_0_EN
160164
case TIMER_0:
161-
return TIMER_0_DEV->CHANNEL[1].CVAL;
165+
return config[dev].value - TIMER_0_DEV->CHANNEL[1].CVAL;
162166
break;
163167
#endif
164168
#if TIMER_1_EN
165169
case TIMER_1:
166-
return TIMER_1_DEV->CHANNEL[3].CVAL;
170+
return TIMER_1_MAX_VALUE - TIMER_1_DEV->CHANNEL[3].CVAL;
167171
break;
168172
#endif
169173
case TIMER_UNDEFINED:
@@ -196,6 +200,7 @@ void timer_stop(tim_t dev)
196200
#if TIMER_0_EN
197201
case TIMER_0:
198202
TIMER_0_DEV->CHANNEL[1].TCTRL &= ~(1 << PIT_TCTRL_TEN_SHIFT);
203+
config[dev].value = 0;
199204
break;
200205
#endif
201206
#if TIMER_1_EN
@@ -249,6 +254,7 @@ void timer_reset(tim_t dev)
249254
switch (dev) {
250255
#if TIMER_0_EN
251256
case TIMER_0:
257+
config[dev].value = 0;
252258
TIMER_0_DEV->CHANNEL[1].LDVAL = TIMER_0_MAX_VALUE;
253259
break;
254260
#endif
@@ -264,6 +270,7 @@ void timer_reset(tim_t dev)
264270

265271
void TIMER_0_ISR(void)
266272
{
273+
LED_RED_TOGGLE;
267274
TIMER_0_DEV->CHANNEL[1].TFLG = (1 << PIT_TFLG_TIF_SHIFT);
268275
config[0].cb(0);
269276
if (sched_context_switch_request) {

0 commit comments

Comments
 (0)