Contiki 3.x
clock.c
Go to the documentation of this file.
1 /**
2  * \file
3  * Clock module library port for MK60DZ10.
4  * \author
5  * Tony Persson <tony.persson@rubico.com>
6  */
7 
8 #include "contiki.h"
9 #include "contiki-conf.h"
10 #include "sys/clock.h"
11 #include "sys/etimer.h"
12 #include "K60.h"
13 #include "power-modes.h"
14 
15 #define DEBUG 0
16 #if DEBUG
17 #include "stdio.h"
18 #define PRINTF(...) printf(__VA_ARGS__)
19 #else
20 #define PRINTF(...)
21 #endif
22 
23 static volatile clock_time_t current_tick;
24 static volatile unsigned long current_seconds = 0;
25 static volatile unsigned long second_countdown = CLOCK_SECOND;
26 
27 /*
28  * Get the system time.
29  */
30 clock_time_t
32 {
33  return current_tick;
34 }
35 
36 /*
37  * Get the system time in seconds.
38  */
39 unsigned long
41 {
42  return current_seconds;
43 }
44 
45 /*
46  * Get the system time in seconds.
47  */
48 void
49 clock_set_seconds(unsigned long sec)
50 {
51  current_seconds = sec;
52 }
53 
54 #if 0
55 /*
56  * (Deprecated) Delay the CPU.
57  */
58 void
59 clock_delay(unsigned int delay)
60 {
61 }
62 #endif /* 0 */
63 
64 /* clock_wait is untested and is not currently needed by any Mulle code.
65  * Remove this #if 0 when a use case arises. */
66 #if 0
67 /*
68  * Delay the CPU for a number of clock ticks.
69  */
70 void
71 clock_wait(clock_time_t delay)
72 {
73  clock_time_t target = clock_time() + delay;
74  while (target > clock_time())
75  {
76  /* Wait for event (timer interrupt or anything else) */
77  /* Make sure that we do not enter STOP mode since then any peripherals
78  * currently in use may halt, e.g. UART and SPI buses. */
79  power_mode_wait(); /* power_mode_wait clears the DEEPSLEEP bit before executing WFI/WFE. */
80  }
81 }
82 #endif /* 0 */
83 
84 /*
85  * Initialize the clock module.
86  *
87  * Generates interrupt from external 32kHz crystal.
88  */
89 /* TODO(Henrik) move to platform, init may differ between platforms. */
90 void
92 {
93  /* Setup Low Power Timer (LPT) */
94 
95  /* Enable LPT clock gate */
96  BITBAND_REG(SIM->SCGC5, SIM_SCGC5_LPTIMER_SHIFT) = 1;
97 
98  /* Disable timer to change settings. */
99  /* Logic is reset when the timer is disabled, TCF flag is also cleared on disable. */
100  LPTMR0->CSR = 0x00;
101  /* Underflow every x+1 clocks */
102  LPTMR0->CMR = (LPTMR_CMR_COMPARE((32768 / CLOCK_SECOND) - 1));
103  /* Prescaler bypass, LPTMR is clocked directly by ERCLK32K. */
104  LPTMR0->PSR = (LPTMR_PSR_PBYP_MASK | LPTMR_PSR_PCS(0b10));
105  /* Enable timer, enable interrupts. */
106  LPTMR0->CSR |= (LPTMR_CSR_TEN_MASK | LPTMR_CSR_TIE_MASK);
107 
108  /* Enable LPT interrupt */
110 }
111 
112 /*
113  * LPTMR ISR
114  */
115 void
116 _isr_lpt(void)
117 {
118  /* Clear timer compare flag by writing a 1 to it */
119  BITBAND_REG(LPTMR0->CSR, LPTMR_CSR_TCF_SHIFT) = 1;
120  PRINTF("LPT: Interrupt\n");
121 
122  /* Contiki event polling */
123  current_tick++;
124 
125  if(--second_countdown == 0) {
126  current_seconds++;
127  second_countdown = CLOCK_SECOND;
128  }
129  if(etimer_pending()) {
131  }
132 }
LPTimer interrupt.
Definition: MK60D10.h:181
__STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
Enable External Interrupt.
Definition: core_cm0.h:535
#define LPTMR0
Peripheral LPTMR0 base pointer.
Definition: MK60D10.h:5399
#define SIM
Peripheral SIM base pointer.
Definition: MK60D10.h:7650
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
K60 hardware register header wrapper.
CCIF clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
Power mode switching functions for the K60 CPU.
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
void clock_set_seconds(unsigned long sec)
Set the value of the platform seconds.
Definition: clock.c:49
CCIF unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:57
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition: etimer.c:223
void clock_delay(unsigned int delay)
Obsolete delay function but we implement it here since some code still uses it.
Definition: clock.c:60
void clock_wait(clock_time_t t)
Wait for a given number of ticks.
Definition: clock.c:166
#define BITBAND_REG(Reg, Bit)
Macro to access a single bit of a peripheral register (bit band region 0x40000000 to 0x400FFFFF) usin...
Definition: MK60D10.h:71
Event timer header file.
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82