Contiki-NG
Loading...
Searching...
No Matches
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#if CLOCK_SIZE != 4
55/* 64 bit variables may not be read atomically without extra handling */
56#error CLOCK_CONF_SIZE must be 4 (32 bit)
57#endif
58
59#ifdef NRF_CLOCK_CONF_RTC_INSTANCE
60#define NRF_CLOCK_RTC_INSTANCE NRF_CLOCK_CONF_RTC_INSTANCE
61#else
62#define NRF_CLOCK_RTC_INSTANCE 0
63#endif
64
65static void clock_update(void);
66
67/*---------------------------------------------------------------------------*/
68/**< RTC instance used for platform clock */
69static const nrfx_rtc_t rtc = NRFX_RTC_INSTANCE(NRF_CLOCK_RTC_INSTANCE);
70/*---------------------------------------------------------------------------*/
71static volatile clock_time_t ticks;
72/*---------------------------------------------------------------------------*/
73static void
74clock_handler(nrfx_clock_evt_type_t event)
75{
76 (void) event;
77}
78/*---------------------------------------------------------------------------*/
79/**
80 * @brief Function for handling the RTC<instance> interrupts
81 * @param int_type Type of interrupt to be handled
82 */
83static void
84rtc_handler(nrfx_rtc_int_type_t int_type)
85{
86 if(int_type == NRFX_RTC_INT_TICK) {
87 clock_update();
88 }
89}
90/*---------------------------------------------------------------------------*/
91/**
92 * @brief Function starting the internal LFCLK XTAL oscillator.
93 */
94static void
96{
97 nrfx_err_t err_code = nrfx_clock_init(clock_handler);
98
99 if(err_code != NRFX_SUCCESS) {
100 return;
101 }
102
103 nrfx_clock_enable();
104
105 nrfx_clock_lfclk_start();
106}
107/*---------------------------------------------------------------------------*/
108/**
109 * @brief Function initialization and configuration of RTC driver instance.
110 */
111static void
113{
114 nrfx_err_t err_code;
115
116 /*Initialize RTC instance */
117 nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG;
118 config.prescaler = 255;
119 config.interrupt_priority = 6;
120 config.reliable = 0;
121
122 err_code = nrfx_rtc_init(&rtc, &config, rtc_handler);
123
124 if(err_code != NRFX_SUCCESS) {
125 return;
126 }
127
128 /*Enable tick event & interrupt */
129 nrfx_rtc_tick_enable(&rtc, true);
130
131 /*Power on RTC instance */
132 nrfx_rtc_enable(&rtc);
133}
134/*---------------------------------------------------------------------------*/
135void
137{
138 ticks = 0;
139 lfclk_config();
140 rtc_config();
141}
142/*---------------------------------------------------------------------------*/
143clock_time_t
145{
146 return ticks;
147}
148/*---------------------------------------------------------------------------*/
149static void
150clock_update(void)
151{
152 ticks++;
153 if(etimer_pending() && !CLOCK_LT(ticks, etimer_next_expiration_time())) {
155 }
156}
157/*---------------------------------------------------------------------------*/
158unsigned long
160{
161 return (unsigned long)(ticks / CLOCK_SECOND);
162}
163/*---------------------------------------------------------------------------*/
164void
165clock_wait(clock_time_t i)
166{
167 clock_time_t start = clock_time();
168 while(clock_time() - start < i) {
169 __WFE();
170 }
171}
172/*---------------------------------------------------------------------------*/
173void
175{
176 NRFX_DELAY_US(dt);
177}
178/*---------------------------------------------------------------------------*/
179/**
180 * @brief Obsolete delay function but we implement it here since some code
181 * still uses it
182 */
183void
184clock_delay(unsigned int i)
185{
187}
188/*---------------------------------------------------------------------------*/
189/**
190 * @}
191 * @}
192 * @}
193 */
#define CLOCK_SECOND
A second, measured in system clock time.
Definition clock.h:103
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
clock_time_t etimer_next_expiration_time(void)
Get next event timer expiration time.
Definition etimer.c:231
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 RTC<instance> interrupts.
Definition clock-arch.c:84
static void rtc_config(void)
Function initialization and configuration of RTC driver instance.
Definition clock-arch.c:112
static const nrfx_rtc_t rtc
< RTC instance used for platform clock
Definition clock-arch.c:69
static void lfclk_config(void)
Function starting the internal LFCLK XTAL oscillator.
Definition clock-arch.c:95
static void start(void)
Start measurement.