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 /*---------------------------------------------------------------------------*/
55 const nrfx_rtc_t rtc = NRFX_RTC_INSTANCE(0); /**< RTC instance used for platform clock */
56 /*---------------------------------------------------------------------------*/
57 static volatile uint32_t ticks;
58 void clock_update(void);
59 
60 /**
61  * @brief Function for handling the RTC0 interrupts
62  * @param int_type Type of interrupt to be handled
63  */
64 static void
65 rtc_handler(nrfx_rtc_int_type_t int_type)
66 {
67  if(int_type == NRFX_RTC_INT_TICK) {
68  clock_update();
69  }
70 }
71 /**
72  * @brief Function starting the internal LFCLK XTAL oscillator.
73  */
74 static void
76 {
77  nrfx_err_t err_code = nrfx_clock_init(NULL);
78 
79  if(err_code != NRFX_SUCCESS) {
80  return;
81  }
82 
83  nrfx_clock_lfclk_start();
84 }
85 /**
86  * @brief Function initialization and configuration of RTC driver instance.
87  */
88 static void
90 {
91  nrfx_err_t err_code;
92 
93  /*Initialize RTC instance */
94  nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG;
95  config.prescaler = 255;
96  config.interrupt_priority = 6;
97  config.reliable = 0;
98 
99  err_code = nrfx_rtc_init(&rtc, &config, rtc_handler);
100 
101  if(err_code != NRFX_SUCCESS) {
102  return;
103  }
104 
105  /*Enable tick event & interrupt */
106  nrfx_rtc_tick_enable(&rtc, true);
107 
108  /*Power on RTC instance */
109  nrfx_rtc_enable(&rtc);
110 }
111 /*---------------------------------------------------------------------------*/
112 void
114 {
115  ticks = 0;
116  lfclk_config();
117  rtc_config();
118 }
119 /*---------------------------------------------------------------------------*/
120 clock_time_t
122 {
123  return (clock_time_t)(ticks & 0xFFFFFFFF);
124 }
125 /*---------------------------------------------------------------------------*/
126 void
127 clock_update(void)
128 {
129  ticks++;
130  if(etimer_pending()) {
132  }
133 }
134 /*---------------------------------------------------------------------------*/
135 unsigned long
137 {
138  return (unsigned long)ticks / CLOCK_CONF_SECOND;
139 }
140 /*---------------------------------------------------------------------------*/
141 void
142 clock_wait(clock_time_t i)
143 {
144  clock_time_t start;
145  start = clock_time();
146  while(clock_time() - start < (clock_time_t)i) {
147  __WFE();
148  }
149 }
150 /*---------------------------------------------------------------------------*/
151 void
152 clock_delay_usec(uint16_t dt)
153 {
154  NRFX_DELAY_US(dt);
155 }
156 /*---------------------------------------------------------------------------*/
157 /**
158  * @brief Obsolete delay function but we implement it here since some code
159  * still uses it
160  */
161 void
162 clock_delay(unsigned int i)
163 {
164  clock_delay_usec(i);
165 }
166 /*---------------------------------------------------------------------------*/
167 /**
168  * @}
169  * @}
170  * @}
171  */
const nrfx_rtc_t rtc
RTC instance used for platform clock.
Definition: clock-arch.c:55
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition: clock-arch.c:152
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
void clock_wait(clock_time_t i)
Wait for a given number of ticks.
Definition: clock-arch.c:142
static void start(void)
Start measurement.
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock-arch.c:136
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
Definition: clock-arch.c:162
static void rtc_handler(nrfx_rtc_int_type_t int_type)
Function for handling the RTC0 interrupts.
Definition: clock-arch.c:65
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition: etimer.c:231
static int config(int type, int c, nrf_drv_gpiote_pin_t pin)
Configuration function for the button sensor for all buttons.
static void lfclk_config(void)
Function starting the internal LFCLK XTAL oscillator.
Definition: clock-arch.c:75
void clock_init(void)
Initialize the clock library.
Definition: clock-arch.c:113
clock_time_t clock_time(void)
Get the current clock time.
Definition: clock-arch.c:121
static void rtc_config(void)
Function initialization and configuration of RTC driver instance.
Definition: clock-arch.c:89