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  */
33 /*---------------------------------------------------------------------------*/
34 /**
35 * \file
36 * Clock for STM32W.
37 * \author
38 * Salvatore Pitrulli <salvopitru@users.sourceforge.net>
39 */
40 /*---------------------------------------------------------------------------*/
41 
42 /*
43  * File customized for mbxxx platform. It uses systick timer to control button
44  * status without interrupts, as well as for system clock.
45  */
46 
47 #include PLATFORM_HEADER
48 #include "hal/error.h"
49 #include "hal/hal.h"
50 #include "dev/stm32w_systick.h"
51 
52 #include "sys/clock.h"
53 #include "sys/etimer.h"
54 #include "dev/button-sensor.h"
55 #include "uart1.h"
56 #include "dev/leds.h"
57 #include "dev/stm32w-radio.h"
58 
59 #define DEBUG DEBUG_NONE
60 #include "net/uip-debug.h"
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 /*---------------------------------------------------------------------------*/
70 void SysTick_Handler(void)
71 {
72 
73  count++;
74 
75  if(button_sensor.status(SENSORS_READY)){
76  button_sensor.value(0); // sensors_changed is called inside this function.
77  }
78 
79  if(etimer_pending()) {
81  }
82 
83  if (--second_countdown == 0) {
84  current_seconds++;
85  second_countdown = CLOCK_SECOND;
86  }
87 
88 }
89 
90 /*---------------------------------------------------------------------------*/
91 
92 void clock_init(void)
93 {
94 
95  ATOMIC(
96 
97  //Counts the number of ticks.
98  count = 0;
99 
100  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
101  SysTick_SetReload(RELOAD_VALUE);
102  SysTick_ITConfig(ENABLE);
103  SysTick_CounterCmd(SysTick_Counter_Enable);
104 
105  )
106 }
107 
108 /*---------------------------------------------------------------------------*/
109 
110 clock_time_t clock_time(void)
111 {
112  return count;
113 }
114 
115 /*---------------------------------------------------------------------------*/
116 /**
117  * Delay the CPU for a multiple of TODO
118  */
119 void clock_delay(unsigned int i)
120 {
121  for (; i > 0; i--) { /* Needs fixing XXX */
122  unsigned j;
123  for (j = 50; j > 0; j--)
124  asm ("nop");
125  }
126 }
127 
128 /*---------------------------------------------------------------------------*/
129 /**
130  * Wait for a multiple of 1 ms.
131  *
132  */
133 void clock_wait(int i)
134 {
135  clock_time_t start;
136 
137  start = clock_time();
138  while(clock_time() - start < (clock_time_t)i);
139 }
140 /*---------------------------------------------------------------------------*/
141 
142 unsigned long clock_seconds(void)
143 {
144  return current_seconds;
145 }
146 
147 void sleep_seconds(int seconds)
148 {
149  int32u quarter_seconds = seconds * 4;
150  uint8_t radio_on;
151 
152 
153  halPowerDown();
154  radio_on = stm32w_radio_is_on();
155  stm32w_radio_driver.off();
156 
157  halSleepForQsWithOptions(&quarter_seconds, 0);
158 
159 
160  ATOMIC(
161 
162  halPowerUp();
163 
164  // Update OS system ticks.
165  current_seconds += seconds - quarter_seconds / 4 ; // Passed seconds
166  count += seconds * CLOCK_SECOND - quarter_seconds * CLOCK_SECOND / 4 ;
167 
168  if(etimer_pending()) {
170  }
171 
172  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
173  SysTick_SetReload(RELOAD_VALUE);
174  SysTick_ITConfig(ENABLE);
175  SysTick_CounterCmd(SysTick_Counter_Enable);
176 
177  )
178 
179  stm32w_radio_driver.init();
180  if(radio_on){
181  stm32w_radio_driver.on();
182  }
183 
184  uart1_init(115200);
185  leds_init();
186  rtimer_init();
187 
188  PRINTF("WakeInfo: %04x\r\n", halGetWakeInfo());
189 
190 
191 }