Contiki 3.x
clock.c
1 /**
2  * Copyright (c) 2014, Analog Devices, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted (subject to the limitations in the
6  * disclaimer below) provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * - Neither the name of Analog Devices, Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
21  * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /**
35  * \author Jim Paris <jim.paris@rigado.com>
36  */
37 
38 #include <aducrf101-contiki.h>
39 #include <sys/clock.h>
40 #include <sys/etimer.h>
41 
42 static volatile clock_time_t current_clock = 0;
43 static volatile unsigned long current_seconds = 0;
44 static unsigned int second_countdown = CLOCK_SECOND;
45 
46 #define SAMPLE_STACK_POINTER
47 #ifdef SAMPLE_STACK_POINTER
48 volatile uint32_t *__min_sampled_sp = (uint32_t *)0xFFFFFFFF;
49 #endif
50 
51 void
52 SysTick_Handler(void)
53 {
54 #ifdef SAMPLE_STACK_POINTER
55  /* Take note of the lowest stack pointer we ever saw.
56  When compiling against newlib, the total free bytes of
57  RAM not ever used by heap or stack can be found via GDB:
58  (gdb) p (char *)__min_sampled_sp - (char *)_sbrk(0)
59  */
60  uint32_t *sp = (uint32_t *)&sp;
61  if (sp < __min_sampled_sp)
62  __min_sampled_sp = sp;
63 #endif
64 
65  current_clock++;
66  if(etimer_pending()) {
68  }
69  if(--second_countdown == 0) {
70  current_seconds++;
71  second_countdown = CLOCK_SECOND;
72  }
73 }
74 /*---------------------------------------------------------------------------*/
75 void
77 {
79 }
80 /*---------------------------------------------------------------------------*/
81 clock_time_t
83 {
84  return current_clock;
85 }
86 /*---------------------------------------------------------------------------*/
87 unsigned long
89 {
90  return current_seconds;
91 }
92 /*---------------------------------------------------------------------------*/
93 void
94 clock_delay_usec(uint16_t usec)
95 {
96  /* Delay by watching the SysTick value change. */
97  int32_t remaining = (int32_t)usec * F_CPU / 1000000;
98  int32_t old = SysTick->VAL;
99  while(remaining > 0) {
100  int32_t new = SysTick->VAL;
101  if(new > old) { /* wraparound */
102  old += SysTick->LOAD;
103  }
104  remaining -= (old - new);
105  old = new;
106  }
107 }
#define SysTick
Definition: core_cm0.h:495
#define F_CPU
CPU core frequency resulting from the chosen divisors and multipliers.
Definition: config-clocks.h:87
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition: clock.c:94
__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
System Tick Configuration.
Definition: core_cm0.h:676
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
CCIF clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
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
Event timer header file.
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82