Contiki 2.5
clock.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, STMicroelectronics.
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
11  * copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided
13  * with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior
16  * written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This file is part of the Contiki OS
31  *
32  * $Id: clock.c,v 1.1 2010/10/25 09:03:38 salvopitru Exp $
33  */
34 /*---------------------------------------------------------------------------*/
35 /**
36 * \file
37 * Clock.
38 * \author
39 * Salvatore Pitrulli <salvopitru@users.sourceforge.net>
40 */
41 /*---------------------------------------------------------------------------*/
42 
43 #include PLATFORM_HEADER
44 #include "hal/error.h"
45 #include "hal/hal.h"
46 #include "dev/stm32w_systick.h"
47 
48 #include "contiki.h"
49 
50 #include "uart1.h"
51 #include "dev/leds.h"
52 #include "dev/stm32w-radio.h"
53 
54 #define DEBUG DEBUG_NONE
55 #include "net/uip-debug.h"
56 
57 // The value that will be load in the SysTick value register.
58 #define RELOAD_VALUE 24000-1 // 1 ms with a 24 MHz clock
59 
60 static volatile clock_time_t count;
61 static volatile unsigned long current_seconds = 0;
62 static unsigned int second_countdown = CLOCK_SECOND;
63 
64 /*---------------------------------------------------------------------------*/
65 void SysTick_Handler(void)
66 {
67 
68  count++;
69 
70  if(etimer_pending()) {
72  }
73 
74  if (--second_countdown == 0) {
75  current_seconds++;
76  second_countdown = CLOCK_SECOND;
77  }
78 
79 }
80 
81 /*---------------------------------------------------------------------------*/
82 
83 void clock_init(void)
84 {
85 
86  ATOMIC(
87 
88  //Counts the number of ticks.
89  count = 0;
90 
91  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
92  SysTick_SetReload(RELOAD_VALUE);
93  SysTick_ITConfig(ENABLE);
94  SysTick_CounterCmd(SysTick_Counter_Enable);
95 
96  )
97 
98 }
99 
100 /*---------------------------------------------------------------------------*/
101 
102 clock_time_t clock_time(void)
103 {
104  return count;
105 }
106 
107 /*---------------------------------------------------------------------------*/
108 /**
109  * Delay the CPU for a multiple of TODO
110  */
111 void clock_delay(unsigned int i)
112 {
113  for (; i > 0; i--) { /* Needs fixing XXX */
114  unsigned j;
115  for (j = 50; j > 0; j--)
116  asm ("nop");
117  }
118 }
119 
120 /*---------------------------------------------------------------------------*/
121 /**
122  * Wait for a multiple of 1 ms.
123  *
124  */
125 void clock_wait(int i)
126 {
127  clock_time_t start;
128 
129  start = clock_time();
130  while(clock_time() - start < (clock_time_t)i);
131 }
132 /*---------------------------------------------------------------------------*/
133 
134 unsigned long clock_seconds(void)
135 {
136  return current_seconds;
137 }
138 
139 #include <stdio.h>
140 
141 void sleep_seconds(int seconds)
142 {
143  int32u quarter_seconds = seconds * 4;
144  uint8_t radio_on;
145 
146 
147  halPowerDown();
148  radio_on = stm32w_radio_is_on();
149  stm32w_radio_driver.off();
150 
151  halSleepForQsWithOptions(&quarter_seconds, 0);
152 
153 
154  ATOMIC(
155 
156  halPowerUp();
157 
158  // Update OS system ticks.
159  current_seconds += seconds - quarter_seconds / 4 ; // Passed seconds
160  count += seconds * CLOCK_SECOND - quarter_seconds * CLOCK_SECOND / 4 ;
161 
162  if(etimer_pending()) {
164  }
165 
166  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
167  SysTick_SetReload(RELOAD_VALUE);
168  SysTick_ITConfig(ENABLE);
169  SysTick_CounterCmd(SysTick_Counter_Enable);
170 
171  )
172 
173  stm32w_radio_driver.init();
174  if(radio_on){
175  stm32w_radio_driver.on();
176  }
177 
178  uart1_init(115200);
179  leds_init();
180  rtimer_init();
181 
182  PRINTF("WakeInfo: %04x\r\n", halGetWakeInfo());
183 
184 
185 }