Contiki 3.x
clock.c
1 /*
2  * Copyright (C) 2015, Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "sys/clock.h"
32 #include "sys/etimer.h"
33 
34 #include "contiki-conf.h"
35 #include "drivers/legacy_pc/rtc.h"
36 
37 #if CLOCK_CONF_SECOND == 2
38 #define FREQ RTC_2_HZ
39 #elif CLOCK_CONF_SECOND == 4
40 #define FREQ RTC_4_HZ
41 #elif CLOCK_CONF_SECOND == 8
42 #define FREQ RTC_8_HZ
43 #elif CLOCK_CONF_SECOND == 16
44 #define FREQ RTC_16_HZ
45 #elif CLOCK_CONF_SECOND == 32
46 #define FREQ RTC_32_HZ
47 #elif CLOCK_CONF_SECOND == 64
48 #define FREQ RTC_64_HZ
49 #elif CLOCK_CONF_SECOND == 128
50 #define FREQ RTC_128_HZ
51 #elif CLOCK_CONF_SECOND == 256
52 #define FREQ RTC_256_HZ
53 #elif CLOCK_CONF_SECOND == 512
54 #define FREQ RTC_512_HZ
55 #elif CLOCK_CONF_SECOND == 1024
56 #define FREQ RTC_1024_HZ
57 #elif CLOCK_CONF_SECOND == 2048
58 #define FREQ RTC_2048_HZ
59 #elif CLOCK_CONF_SECOND == 4096
60 #define FREQ RTC_4096_HZ
61 #elif CLOCK_CONF_SECOND == 8192
62 #define FREQ RTC_8192_HZ
63 #else
64 #define FREQ -1
65 #error "RTC is being used thus CLOCK_CONF_SECOND has to be a value defined by rtc_frequency_t."
66 #endif
67 
68 static volatile clock_time_t tick_count = 0;
69 
70 static void
71 update_ticks(void)
72 {
73  clock_time_t expire = etimer_next_expiration_time();
74 
75  tick_count++;
76 
77  /* Notify etimer library if the next event timer has expired */
78  if(expire != 0 && tick_count >= expire) {
80  }
81 }
82 /*---------------------------------------------------------------------------*/
83 void
85 {
86  rtc_init(FREQ, update_ticks);
87 }
88 /*---------------------------------------------------------------------------*/
89 clock_time_t
91 {
92  return tick_count;
93 }
94 /*---------------------------------------------------------------------------*/
95 unsigned long
97 {
98  return tick_count / CLOCK_CONF_SECOND;
99 }
100 /*---------------------------------------------------------------------------*/
101 void
102 clock_wait(clock_time_t t)
103 {
104  clock_time_t initial = tick_count;
105 
106  while(tick_count < t + initial);
107 }
108 /*---------------------------------------------------------------------------*/
109 void
110 clock_set_seconds(unsigned long sec)
111 {
112  /* Stubbed function */
113 }
114 /*---------------------------------------------------------------------------*/
115 void
116 clock_delay_usec(uint16_t t)
117 {
118  /* Stubbed function */
119 }
clock_time_t etimer_next_expiration_time(void)
Get next event timer expiration time.
Definition: etimer.c:237
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
void clock_init()
Initialize the clock library.
Definition: clock.c:76
static void update_ticks(void)
Update the software clock ticks and seconds.
Definition: clock.c:193
Event timer header file.
void clock_wait(clock_time_t i)
Wait for a given number of ticks.
Definition: clock.c:162
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:54
void clock_delay_usec(uint16_t usec)
Delay a given number of microseconds.
Definition: clock.c:94
void clock_set_seconds(unsigned long sec)
Set the value of the platform seconds.
Definition: clock.c:147