Contiki 3.x
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/ip/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(clock_time_t 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  uint32_t 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 }
void SysTick_SetReload(uint32_t Reload)
Sets SysTick Reload value.
void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
Configures the SysTick clock source.
void uart1_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart1.c:143
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
void halPowerUp(void)
Powers up microcontroller peripherals and board peripherals.
Definition: micro.c:65
STM32W radio driver header file
StStatus halSleepForQsWithOptions(uint32_t *duration, uint32_t gpioWakeBitMask)
Uses the system timer to enter ::SLEEPMODE_WAKETIMER for approximately the specified amount of time (...
Definition: system-timer.c:168
void halPowerDown(void)
Powers down microcontroller peripherals and board peripherals.
Definition: micro.c:60
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
uint32_t halGetWakeInfo(void)
Obtains the events that caused the last wake from sleep.
Definition: sleep.c:122
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.
int(* on)(void)
Turn the radio on.
Definition: radio.h:252
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
Event timer header file.
int(* off)(void)
Turn the radio off.
Definition: radio.h:255
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82