Contiki 3.x
clock.c
Go to the documentation of this file.
1 /**
2  * \addtogroup mbxxx-platform
3  *
4  * @{
5  */
6 
7 /*
8  * Copyright (c) 2010, STMicroelectronics.
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
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * 3. The name of the author may not be used to endorse or promote
21  * products derived from this software without specific prior
22  * written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
25  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
28  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
30  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 /**
39 * \file
40 * Clock.
41 * \author
42 * Salvatore Pitrulli <salvopitru@users.sourceforge.net>
43 */
44 
45 #include PLATFORM_HEADER
46 #include <stdio.h>
47 #include "hal/error.h"
48 #include "hal/hal.h"
49 #include "dev/stm32w-systick.h"
50 
51 #include "contiki.h"
52 #include "sys/clock.h"
53 
54 #include "uart1.h"
55 #include "dev/leds.h"
56 #include "dev/stm32w-radio.h"
57 
58 #define DEBUG DEBUG_NONE
59 #include "net/ip/uip-debug.h"
60 
61 /*--------------------------------------------------------------------------*/
62 /* The value that will be load in the SysTick value register. */
63 #define RELOAD_VALUE 24000-1 /* 1 ms with a 24 MHz clock */
64 
65 static volatile clock_time_t count;
66 static volatile unsigned long current_seconds = 0;
67 static unsigned int second_countdown = CLOCK_SECOND;
68 /*---------------------------------------------------------------------------*/
69 void
70 SysTick_Handler(void)
71 {
72  count++;
73  if(etimer_pending()) {
75  }
76 
77  if(--second_countdown == 0) {
78  current_seconds++;
79  second_countdown = CLOCK_SECOND;
80  }
81 
82 }
83 /*---------------------------------------------------------------------------*/
84 void
86 {
87  ATOMIC(
88  /* Counts the number of ticks. */
89  count = 0;
90  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
91  SysTick_SetReload(RELOAD_VALUE);
92  SysTick_ITConfig(ENABLE);
93  SysTick_CounterCmd(SysTick_Counter_Enable);)
94 }
95 /*---------------------------------------------------------------------------*/
96 clock_time_t
98 {
99  return count;
100 }
101 /*---------------------------------------------------------------------------*/
102 /**
103  * Delay the CPU for a multiple of TODO
104  */
105 void
106 clock_delay(unsigned int i)
107 {
108  for(; i > 0; i--) { /* Needs fixing XXX */
109  unsigned j;
110  for(j = 50; j > 0; j--) {
111  asm("nop");
112  }
113  }
114 }
115 /*---------------------------------------------------------------------------*/
116 /**
117  * Wait for a multiple of 1 ms.
118  */
119 void
120 clock_wait(clock_time_t i)
121 {
122  clock_time_t start;
123 
124  start = clock_time();
125  while(clock_time() - start < (clock_time_t) i);
126 }
127 /*---------------------------------------------------------------------------*/
128 unsigned long
130 {
131  return current_seconds;
132 }
133 /*--------------------------------------------------------------------------*/
134 /** @} */
void SysTick_SetReload(uint32_t Reload)
Sets SysTick Reload value.
void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
Configures the SysTick clock source.
STM32W radio driver header file
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 SysTick_ITConfig(FunctionalState NewState)
Enables or disables the SysTick Interrupt.
void SysTick_CounterCmd(uint32_t SysTick_Counter)
Enables or disables the SysTick counter.
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
Generic set of HAL includes for all platforms.
Return codes for API functions and module definitions.
void clock_delay(unsigned int delay)
Obsolete delay function but we implement it here since some code still uses it.
Definition: clock.c:60
A set of debugging macros.
#define ATOMIC(blah)
A block of code may be made atomic by wrapping it with this macro.
Definition: gnu.h:459
void clock_wait(clock_time_t t)
Wait for a given number of ticks.
Definition: clock.c:166
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82