Contiki 3.x
clock.c
Go to the documentation of this file.
1 /*
2  * Contiki PIC32 Port project
3  *
4  * Copyright (c) 2012,
5  * Scuola Superiore Sant'Anna (http://www.sssup.it) and
6  * Consorzio Nazionale Interuniversitario per le Telecomunicazioni
7  * (http://www.cnit.it).
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the Institute nor the names of its contributors
20  * may be used to endorse or promote products derived from this software
21  * without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 
37 /**
38  * \addtogroup pic32 PIC32 Contiki Port
39  *
40  * @{
41  */
42 
43 /**
44  * \file cpu/pic32/clock.c
45  * \brief Clock routines.
46  * \author Giovanni Pellerano <giovanni.pellerano@evilaliv3.org>
47  * \author Daniele Alessandrelli <d.alessandrelli@sssup.it>
48  * \date 2012-03-21
49  */
50 
51 #include <pic32_clock.h>
52 #include <pic32_timer.h>
53 #include <pic32_irq.h>
54 
55 #include <sys/clock.h>
56 #include <sys/etimer.h>
57 
58 #include "contiki.h"
59 
60 static volatile clock_time_t ticks;
61 static volatile unsigned long seconds;
62 
63 /*---------------------------------------------------------------------------*/
64 static inline void
65 clock_callback(void)
66 {
67  ++ticks;
68 
69  if(etimer_pending()) {
71  }
72 
73 #if (CLOCK_CONF_SECOND & (CLOCK_CONF_SECOND - 1)) != 0
74 #error CLOCK_CONF_SECOND must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...).
75 #error Change CLOCK_CONF_SECOND in contiki-conf.h.
76 #endif
77 
78  if((ticks % CLOCK_SECOND) == 0) {
79  ++seconds;
80  }
81 }
82 /*---------------------------------------------------------------------------*/
83 clock_time_t
85 {
86  return ticks;
87 }
88 /*---------------------------------------------------------------------------*/
89 unsigned long
91 {
92  return seconds;
93 }
94 /*---------------------------------------------------------------------------*/
95 void
96 clock_set_seconds(unsigned long sec)
97 {
98  seconds = sec;
99 }
100 /*---------------------------------------------------------------------------*/
101 #define CLOCK_INIT(XX) \
102 void \
103 clock_init(void) \
104 { \
105  ticks = 0; \
106  seconds = 0; \
107  pic32_timer##XX##_init(CLOCK_SECOND);\
108  pic32_timer##XX##_enable_irq(); \
109  pic32_timer##XX##_start(); \
110 }
111 
112 #if PIC32_TIMER_CLOCK == 1
113 CLOCK_INIT(1)
114 #elif PIC32_TIMER_CLOCK == 2
115 CLOCK_INIT(2)
116 #elif PIC32_TIMER_CLOCK == 3
117 CLOCK_INIT(3)
118 #elif PIC32_TIMER_CLOCK == 4
119 CLOCK_INIT(4)
120 #elif PIC32_TIMER_CLOCK == 5
121 CLOCK_INIT(5)
122 #endif
123 
124 /*---------------------------------------------------------------------------*/
125 void
126 clock_delay_usec(uint16_t dt)
127 {
128  uint32_t now;
129  uint32_t stop;
130 
131  asm volatile("mfc0 %0, $9" : "=r"(now));
132 
133  /* The Count register is incremented every two system clock (SYSCLK) cycles. */
134 
135  stop = now + dt * ((pic32_clock_get_system_clock() / 1000000) / 2);
136 
137  for(;;) {
138  asm volatile("mfc0 %0, $9" : "=r"(now));
139  if((int32_t)(now - stop) >= 0) {
140  break;
141  }
142  }
143 }
144 /*---------------------------------------------------------------------------*/
145 /*
146  * Deprecated platform-specific routines.
147  *
148  */
149 inline void
150 clock_delay(unsigned int delay)
151 {
152  clock_delay_usec(delay);
153 }
154 /*---------------------------------------------------------------------------*/
155 
156 #if PIC32_TIMER_CLOCK == 1
157 TIMER_INTERRUPT(1, clock_callback);
158 #elif PIC32_TIMER_CLOCK == 2
159 TIMER_INTERRUPT(2, clock_callback);
160 #elif PIC32_TIMER_CLOCK == 3
161 TIMER_INTERRUPT(3, clock_callback);
162 #elif PIC32_TIMER_CLOCK == 4
163 TIMER_INTERRUPT(4, clock_callback);
164 #elif PIC32_TIMER_CLOCK == 5
165 TIMER_INTERRUPT(5, clock_callback);
166 #endif
167 
168 /** @} */
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition: clock.c:94
INTERRUPT interface for PIC32MX (pic32mx795f512l)
uint32_t pic32_clock_get_system_clock(void)
Calculate the system clock.
Definition: pic32_clock.c:82
CLOCK interface for PIC32MX (pic32mx795f512l)
CCIF clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
TIMER interface for PIC32MX (pic32mx795f512l)
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
Event timer header file.
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82