Contiki 3.x
clock.c
1 /*
2  * Copyright (c) 2014, Analog Devices, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /**
32  * \author Ian Martin <martini@redwirellc.com>
33  */
34 
35 #include <time.h>
36 
37 #include "contiki.h"
38 
39 #ifndef BIT
40 #define BIT(n) (1 << (n))
41 #endif
42 
43 #define clock() (0xffff - TCR[CLOCK_CHANNEL])
44 
45 void
47 {
48 #if (CLOCK_CHANNEL <= 7)
49  TAU0EN = 1; /* Enable Timer Array Unit 0. */
50  TT0 = 0x00ff; /* Stop the Timer Array Unit. */
51  TPS0 = (TPS0 & 0xfff0) | CLOCK_SCALER;
52  TMR[CLOCK_CHANNEL] = 0x0000; /* default value */
53 
54 #if (CLOCK_CHANNEL == 0)
55  TDR00 = 0xffff;
56 #elif (CLOCK_CHANNEL == 1)
57  TDR01 = 0xffff;
58 #elif (CLOCK_CHANNEL == 2)
59  TDR02 = 0xffff;
60 #elif (CLOCK_CHANNEL == 3)
61  TDR03 = 0xffff;
62 #elif (CLOCK_CHANNEL == 4)
63  TDR04 = 0xffff;
64 #elif (CLOCK_CHANNEL == 5)
65  TDR05 = 0xffff;
66 #elif (CLOCK_CHANNEL == 6)
67  TDR06 = 0xffff;
68 #elif (CLOCK_CHANNEL == 7)
69  TDR07 = 0xffff;
70 #else
71 #error Invalid CLOCK_CHANNEL
72 #endif
73 
74  TE0 |= BIT(CLOCK_CHANNEL); /* Start timer channel 0. */
75  TS0 |= BIT(CLOCK_CHANNEL); /* Start counting. */
76 #else
77  TAU1EN = 1; /* Enable Timer Array Unit 1. */
78  TT1 = 0x00ff; /* Stop the Timer Array Unit. */
79  TPS1 = (TPS1 & 0xfff0) | CLOCK_SCALER;
80  TMR[CLOCK_CHANNEL] = 0x0000; /* default value */
81 
82 #if (CLOCK_CHANNEL == 8)
83  TDR00 = 0xffff;
84 #elif (CLOCK_CHANNEL == 9)
85  TDR01 = 0xffff;
86 #elif (CLOCK_CHANNEL == 10)
87  TDR02 = 0xffff;
88 #elif (CLOCK_CHANNEL == 11)
89  TDR03 = 0xffff;
90 #elif (CLOCK_CHANNEL == 12)
91  TDR04 = 0xffff;
92 #elif (CLOCK_CHANNEL == 13)
93  TDR05 = 0xffff;
94 #elif (CLOCK_CHANNEL == 14)
95  TDR06 = 0xffff;
96 #elif (CLOCK_CHANNEL == 15)
97  TDR07 = 0xffff;
98 #else
99 #error Invalid CLOCK_CHANNEL
100 #endif
101 
102  TE1 |= BIT(CLOCK_CHANNEL); /* Start timer channel. */
103  TS1 |= BIT(CLOCK_CHANNEL); /* Start counting. */
104 #endif
105 }
106 /*---------------------------------------------------------------------------*/
107 clock_time_t
109 {
110  return clock();
111 }
112 /*---------------------------------------------------------------------------*/
113 unsigned long
115 {
116  return clock() / CLOCK_CONF_SECOND;
117 }
118 /*---------------------------------------------------------------------------*/
119 
120 void
121 clock_wait(clock_time_t t)
122 {
123  clock_time_t t0;
124  t0 = clock();
125  while(clock() - t0 < t) ;
126 }
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
#define BIT(x)
Useful to reference a single bit of a byte.
CCIF clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
CCIF unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:57
void clock_wait(clock_time_t t)
Wait for a given number of ticks.
Definition: clock.c:166