Contiki 3.x
clock.c
1 /*
2  * Copyright (c) 2012, 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 copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *
30  */
31 /*---------------------------------------------------------------------------*/
32 #include <stdio.h>
33 #include "contiki.h"
34 #include "platform-conf.h"
35 #include "contiki-conf.h"
36 #include "dev/leds.h"
37 #include "stm32l1xx.h"
38 #include "stm32l1xx_hal_rcc.h"
39 #include "stm32l1xx_hal_cortex.h"
40 #include "stm32l1xx_hal.h"
41 #include "st-lib.h"
42 /*---------------------------------------------------------------------------*/
43 #define RELOAD_VALUE ((F_CPU / CLOCK_CONF_SECOND) - 1)
44 /*---------------------------------------------------------------------------*/
45 static volatile unsigned long seconds = 0;
46 static volatile clock_time_t ticks;
47 void st_lib_sys_tick_handler(void);
48 /*---------------------------------------------------------------------------*/
49 void
50 st_lib_sys_tick_handler(void)
51 {
52  ticks++;
53  if((ticks % CLOCK_SECOND) == 0) {
54  seconds++;
55  energest_flush();
56  }
57  HAL_IncTick();
58 
59  if(etimer_pending()) {
61  }
62 }
63 /*---------------------------------------------------------------------------*/
64 void
66 {
67  ticks = 0;
68  st_lib_hal_systick_clk_source_config(SYSTICK_CLKSOURCE_HCLK);
69  st_lib_hal_systick_config(RELOAD_VALUE);
70 }
71 /*---------------------------------------------------------------------------*/
72 unsigned long
74 {
75  return seconds;
76 }
77 /*---------------------------------------------------------------------------*/
78 void
79 clock_set_seconds(unsigned long sec)
80 {
81  seconds = sec;
82 }
83 /*---------------------------------------------------------------------------*/
84 clock_time_t
86 {
87  return ticks;
88 }
89 /*---------------------------------------------------------------------------*/
90 void
91 clock_delay(unsigned int i)
92 {
93  for(; i > 0; i--) {
94  unsigned int j;
95  for(j = 50; j > 0; j--) {
96  asm ("nop");
97  }
98  }
99 }
100 /*---------------------------------------------------------------------------*/
101 /* Wait for a multiple of clock ticks (7.8 ms at 128 Hz). */
102 void
103 clock_wait(clock_time_t t)
104 {
105  clock_time_t start;
106  start = clock_time();
107  while(clock_time() - start < (clock_time_t)t) ;
108 }
109 /*---------------------------------------------------------------------------*/
static void start(void)
Start measurement.
Header file for the STM32Cube HAL APIs.
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition: etimer.c:231
clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
void clock_init()
Initialize the clock library.
Definition: clock.c:76
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
Definition: clock.c:170
void clock_wait(clock_time_t i)
Wait for a given number of ticks.
Definition: clock.c:162
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:54
void clock_set_seconds(unsigned long sec)
Set the value of the platform seconds.
Definition: clock.c:147