Contiki-NG
Loading...
Searching...
No Matches
clock-arch.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
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 copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30/*---------------------------------------------------------------------------*/
31/**
32 * \addtogroup gecko
33 * @{
34 *
35 * \addtogroup gecko-sys System drivers
36 * @{
37 *
38 * \addtogroup gecko-clock Clock driver
39 * @{
40 *
41 * \file
42 * Software clock implementation for the gecko.
43 * \author
44 * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
45 *
46 */
47/*---------------------------------------------------------------------------*/
48#include "contiki.h"
49
50#include "sl_sleeptimer.h"
51/*---------------------------------------------------------------------------*/
52static volatile uint32_t ticks;
53static sl_sleeptimer_timer_handle_t periodic_timer;
54/*---------------------------------------------------------------------------*/
55static void
56on_periodic_timeout(sl_sleeptimer_timer_handle_t *handle,
57 void *data)
58{
59 ticks++;
60 if(etimer_pending()) {
62 }
63}
64/*---------------------------------------------------------------------------*/
65static void
66delay_callback(sl_sleeptimer_timer_handle_t *handle,
67 void *data)
68{
69 volatile bool *wait_flag = (bool *)data;
70
71 (void)handle; /* Unused parameter. */
72
73 *wait_flag = false;
74}
75/*---------------------------------------------------------------------------*/
76void
78{
79 uint32_t timer_frequency;
80
81 sl_sleeptimer_init();
82 timer_frequency = sl_sleeptimer_get_timer_frequency();
83 ticks = 0;
84 sl_sleeptimer_start_periodic_timer(&periodic_timer,
85 timer_frequency / CLOCK_CONF_SECOND,
86 on_periodic_timeout, NULL,
87 0,
88 SL_SLEEPTIMER_NO_HIGH_PRECISION_HF_CLOCKS_REQUIRED_FLAG);
89}
90/*---------------------------------------------------------------------------*/
91clock_time_t
93{
94 return (clock_time_t)(ticks & 0xFFFFFFFF);
95}
96/*---------------------------------------------------------------------------*/
97unsigned long
99{
100 return (unsigned long)ticks / CLOCK_CONF_SECOND;
101}
102/*---------------------------------------------------------------------------*/
103void
104clock_wait(clock_time_t i)
105{
106 clock_time_t start;
107 start = clock_time();
108 while(clock_time() - start < (clock_time_t)i) {
109 __WFE();
110 }
111}
112/*---------------------------------------------------------------------------*/
113void
115{
116 volatile bool wait = true;
117 sl_status_t error_code;
118 sl_sleeptimer_timer_handle_t delay_timer;
119 uint32_t timer_frequency = sl_sleeptimer_get_timer_frequency();
120 uint32_t delay = (uint32_t)((((uint32_t)dt * timer_frequency) + 999999) / 1000000L);
121
122 error_code = sl_sleeptimer_start_timer(&delay_timer,
123 delay,
124 delay_callback,
125 (void *)&wait,
126 0,
127 0);
128 if(error_code == SL_STATUS_OK) {
129 while(wait) { /* Active delay loop. */
130 }
131 }
132}
133/*---------------------------------------------------------------------------*/
134/**
135 * @brief Obsolete delay function but we implement it here since some code
136 * still uses it
137 */
138void
139clock_delay(unsigned int i)
140{
142}
143/*---------------------------------------------------------------------------*/
144/**
145 * @}
146 * @}
147 * @}
148 */
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition etimer.c:225
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition etimer.c:145
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition clock-arch.c:98
void clock_init(void)
Initialize the clock library.
Definition clock-arch.c:77
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition clock-arch.c:114
void clock_wait(clock_time_t i)
Wait for a given number of ticks.
Definition clock-arch.c:104
clock_time_t clock_time(void)
Get the current clock time.
Definition clock-arch.c:92
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
Definition clock-arch.c:139
static void start(void)
Start measurement.