Contiki 3.x
rtimer-arch.c
1 
2 #include <stdio.h>
3 
4 #include "rtimer-arch.h"
5 
6 #include "cpu.h"
7 #include "nvic.h"
8 
9 #include "contiki-conf.h"
10 
11 #ifdef SYSTICK_DEBUG
12 #include "gpio.h"
13 #endif
14 
15 /*---------------------------------------------------------------------------*/
16 static volatile rtimer_clock_t next_trigger;
17 /*---------------------------------------------------------------------------*/
18 
19 void
20 rtimer_arch_init(void)
21 {
22  /* TPM0 already configured and counting. */
23  return;
24 }
25 
26 void
27 rtimer_arch_schedule(rtimer_clock_t t)
28 {
29  rtimer_clock_t now;
30 
31  /* Sanity check Value. */
32  now = RTIMER_NOW();
33  //if((rtimer_clock_t)(t - now) < 14) {
34  // t = now + 14;
35  //}
36 
37  TPM0_C0V = t; /* Set TPM0_C0V Channel 0 compare value. */
38  next_trigger = t; /* Store the value. */
39 
40  NVIC_EnableIRQ(TPM0_IRQn); /* Enable TPM0 interrupt in NVIC. */
41  TPM0_C0SC = TPM_CnSC_CHF_MASK /* Enable channel interrupt & clear flag. */
42  | TPM_CnSC_CHIE_MASK
43  | TPM_CnSC_MSA_MASK;
44 
45 }
46 
47 
48 rtimer_clock_t
49 rtimer_arch_now(void)
50 {
51  /* Return the TPM value. */
52  return TPM0_CNT;
53 }
54 
55 rtimer_clock_t
57 {
58  return next_trigger;
59 }
60 
61 /*-----------------------------------------------------------------------------------------------------------------------------------*/
62 /* Interrupt Handler */
63 void TPM0_IRQHandler(void)
64 {
65  gpio_tgl_pin(SYSTICK_DEBUG_GPIO, port_pin_to_mask(SYSTICK_DEBUG_PIN));
66 
67  cpu_run();
68 
69  ENERGEST_ON(ENERGEST_TYPE_IRQ);
70  next_trigger = 0;
71 
72  //TPM0_C0SC = (TPM0_C0SC & ~(TPM_CnSC_CHIE_MASK)) | TPM_CnSC_CHF_MASK; /* Acknowledge interrupt & disable */
73  NVIC_ClearPendingIRQ(TPM0_IRQn); /* Clear the pending bit in NVIC. */
74  NVIC_DisableIRQ(TPM0_IRQn); /* Disable TPM0 interrupt in NVIC. */
75 
77 
78  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
79 }
__STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
Enable External Interrupt.
Definition: core_cm0.h:535
__STATIC_INLINE void NVIC_DisableIRQ(IRQn_Type IRQn)
Disable External Interrupt.
Definition: core_cm0.h:547
TPM0 single interrupt vector for all sources.
Definition: MKL25Z4.h:161
#define RTIMER_NOW()
Get the current clock time.
Definition: rtimer.h:135
rtimer_clock_t rtimer_arch_now()
Returns the current real-time clock time.
Definition: rtimer-arch.c:116
__STATIC_INLINE void NVIC_ClearPendingIRQ(IRQn_Type IRQn)
Clear Pending Interrupt.
Definition: core_cm0.h:587
void rtimer_arch_init(void)
We don't need to explicitly initialise anything but this routine is required by the API...
Definition: rtimer-arch.c:60
Header file for the MKL25Z-specific rtimer code
void rtimer_run_next(void)
Execute the next real-time task and schedule the next task, if any.
Definition: rtimer.c:92
void gpio_tgl_pin(GPIO_Type *Port, uint32_t Pin_Mask)
Toggle pins with Pin_Mask of port with GPIOn_BASE_PTR low.
Definition: gpio.c:153
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition: rtimer-arch.c:72
Header file for the MKL25Z NVIC functions.
uint32_t port_pin_to_mask(uint8_t pin)
Convert a pin number (0 to 31) to a pin mask.
Definition: gpio.c:46
rtimer_clock_t rtimer_arch_next_trigger()
Get the time of the next scheduled rtimer trigger.
Definition: rtimer-arch.c:106