Contiki-NG
clock-arch.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 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 nrf
33 * @{
34 *
35 * \addtogroup nrf-sys System drivers
36 * @{
37 *
38 * \addtogroup nrf-clock Clock driver
39 * @{
40 *
41 * \file
42 * Software clock implementation for the nRF.
43 * \author
44 * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
45 *
46 */
47/*---------------------------------------------------------------------------*/
48#include "contiki.h"
49
50#include "nrfx_config.h"
51#include "nrfx_rtc.h"
52#include "nrfx_clock.h"
53
54/*---------------------------------------------------------------------------*/
55const nrfx_rtc_t rtc = NRFX_RTC_INSTANCE(0); /**< RTC instance used for platform clock */
56/*---------------------------------------------------------------------------*/
57static volatile uint32_t ticks;
58void clock_update(void);
59
60static void
61clock_handler(nrfx_clock_evt_type_t event)
62{
63 (void) event;
64}
65
66/**
67 * @brief Function for handling the RTC0 interrupts
68 * @param int_type Type of interrupt to be handled
69 */
70static void
71rtc_handler(nrfx_rtc_int_type_t int_type)
72{
73 if(int_type == NRFX_RTC_INT_TICK) {
74 clock_update();
75 }
76}
77/**
78 * @brief Function starting the internal LFCLK XTAL oscillator.
79 */
80static void
82{
83 nrfx_err_t err_code = nrfx_clock_init(clock_handler);
84
85 if(err_code != NRFX_SUCCESS) {
86 return;
87 }
88
89 nrfx_clock_enable();
90
91 nrfx_clock_lfclk_start();
92}
93/**
94 * @brief Function initialization and configuration of RTC driver instance.
95 */
96static void
98{
99 nrfx_err_t err_code;
100
101 /*Initialize RTC instance */
102 nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG;
103 config.prescaler = 255;
104 config.interrupt_priority = 6;
105 config.reliable = 0;
106
107 err_code = nrfx_rtc_init(&rtc, &config, rtc_handler);
108
109 if(err_code != NRFX_SUCCESS) {
110 return;
111 }
112
113 /*Enable tick event & interrupt */
114 nrfx_rtc_tick_enable(&rtc, true);
115
116 /*Power on RTC instance */
117 nrfx_rtc_enable(&rtc);
118}
119/*---------------------------------------------------------------------------*/
120void
122{
123 ticks = 0;
124 lfclk_config();
125 rtc_config();
126}
127/*---------------------------------------------------------------------------*/
128clock_time_t
130{
131 return (clock_time_t)(ticks & 0xFFFFFFFF);
132}
133/*---------------------------------------------------------------------------*/
134void
135clock_update(void)
136{
137 ticks++;
138 if(etimer_pending()) {
140 }
141}
142/*---------------------------------------------------------------------------*/
143unsigned long
145{
146 return (unsigned long)ticks / CLOCK_CONF_SECOND;
147}
148/*---------------------------------------------------------------------------*/
149void
150clock_wait(clock_time_t i)
151{
152 clock_time_t start;
153 start = clock_time();
154 while(clock_time() - start < (clock_time_t)i) {
155 __WFE();
156 }
157}
158/*---------------------------------------------------------------------------*/
159void
161{
162 NRFX_DELAY_US(dt);
163}
164/*---------------------------------------------------------------------------*/
165/**
166 * @brief Obsolete delay function but we implement it here since some code
167 * still uses it
168 */
169void
170clock_delay(unsigned int i)
171{
173}
174/*---------------------------------------------------------------------------*/
175/**
176 * @}
177 * @}
178 * @}
179 */
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition: etimer.c:231
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 rtc_handler(nrfx_rtc_int_type_t int_type)
Function for handling the RTC0 interrupts.
Definition: clock-arch.c:71
static void rtc_config(void)
Function initialization and configuration of RTC driver instance.
Definition: clock-arch.c:97
const nrfx_rtc_t rtc
RTC instance used for platform clock.
Definition: clock-arch.c:55
static void lfclk_config(void)
Function starting the internal LFCLK XTAL oscillator.
Definition: clock-arch.c:81
static void start(void)
Start measurement.