Contiki-NG
clock.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the names of its
13  * contributors may be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27  * OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /**
31  * \addtogroup nrf52832
32  * @{
33  *
34  * \addtogroup nrf52832-dev Device drivers
35  * @{
36  *
37  * \addtogroup nrf52832-clock Clock driver
38  * @{
39  *
40  * \file
41  * Software clock implementation for the nRF52.
42  * \author
43  * Wojciech Bober <wojciech.bober@nordicsemi.no>
44  *
45  */
46 /*---------------------------------------------------------------------------*/
47 #include <stdint.h>
48 #include <stdbool.h>
49 #include "nrf.h"
50 #include "nrf_drv_config.h"
51 #include "nrf_drv_rtc.h"
52 #include "nrf_drv_clock.h"
53 #include "nrf_delay.h"
54 #include "app_error.h"
55 #include "contiki.h"
56 
57 /*---------------------------------------------------------------------------*/
58 const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(PLATFORM_RTC_INSTANCE_ID); /**< RTC instance used for platform clock */
59 /*---------------------------------------------------------------------------*/
60 static volatile uint32_t ticks;
61 void clock_update(void);
62 
63 #define TICKS (RTC1_CONFIG_FREQUENCY/CLOCK_CONF_SECOND)
64 
65 /**
66  * \brief Function for handling the RTC0 interrupts
67  * \param int_type Type of interrupt to be handled
68  */
69 static void
70 rtc_handler(nrf_drv_rtc_int_type_t int_type)
71 {
72  if (int_type == NRF_DRV_RTC_INT_TICK) {
73  clock_update();
74  }
75 }
76 
77 #ifndef SOFTDEVICE_PRESENT
78 /** \brief Function starting the internal LFCLK XTAL oscillator.
79  */
80 static void
82 {
83  ret_code_t err_code = nrf_drv_clock_init(NULL);
84  APP_ERROR_CHECK(err_code);
85 
86  nrf_drv_clock_lfclk_request();
87 }
88 #endif
89 
90 /**
91  * \brief Function initialization and configuration of RTC driver instance.
92  */
93 static void
95 {
96  uint32_t err_code;
97 
98  //Initialize RTC instance
99  err_code = nrf_drv_rtc_init(&rtc, NULL, rtc_handler);
100  APP_ERROR_CHECK(err_code);
101 
102  //Enable tick event & interrupt
103  nrf_drv_rtc_tick_enable(&rtc, true);
104 
105  //Power on RTC instance
106  nrf_drv_rtc_enable(&rtc);
107 }
108 /*---------------------------------------------------------------------------*/
109 void
111 {
112  ticks = 0;
113 #ifndef SOFTDEVICE_PRESENT
114  lfclk_config();
115 #endif
116  rtc_config();
117 }
118 /*---------------------------------------------------------------------------*/
119 clock_time_t
121 {
122  return (clock_time_t)(ticks & 0xFFFFFFFF);
123 }
124 /*---------------------------------------------------------------------------*/
125 void
126 clock_update(void)
127 {
128  ticks++;
129  if (etimer_pending()) {
131  }
132 }
133 /*---------------------------------------------------------------------------*/
134 unsigned long
136 {
137  return (unsigned long)ticks/CLOCK_CONF_SECOND;
138 }
139 /*---------------------------------------------------------------------------*/
140 void
141 clock_wait(clock_time_t i)
142 {
143  clock_time_t start;
144  start = clock_time();
145  while (clock_time() - start < (clock_time_t)i) {
146  __WFE();
147  }
148 }
149 /*---------------------------------------------------------------------------*/
150 void
151 clock_delay_usec(uint16_t dt)
152 {
153  nrf_delay_us(dt);
154 }
155 /*---------------------------------------------------------------------------*/
156 /**
157  * \brief Obsolete delay function but we implement it here since some code
158  * still uses it
159  */
160 void
161 clock_delay(unsigned int i)
162 {
163  clock_delay_usec(i);
164 }
165 /*---------------------------------------------------------------------------*/
166 /**
167  * @}
168  * @}
169  * @}
170  */
const nrf_drv_rtc_t rtc
RTC instance used for platform clock.
Definition: clock.c:58
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
__STATIC_INLINE void __WFE(void)
Wait For Event.
Definition: cmsis_gcc.h:394
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition: clock.c:150
void clock_wait(clock_time_t i)
Wait for a given number of ticks.
Definition: clock.c:136
static void lfclk_config(void)
Function starting the internal LFCLK XTAL oscillator.
Definition: clock.c:81
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:130
static void start(void)
Start measurement.
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition: etimer.c:231
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
Definition: clock.c:164
clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:118
#define PLATFORM_RTC_INSTANCE_ID
nRF52 RTC instance to be used for Contiki clock driver.
Definition: nrf52dk-def.h:103
static void rtc_config(void)
Function initialization and configuration of RTC driver instance.
Definition: clock.c:94
static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
Function for handling the RTC0 interrupts.
Definition: clock.c:70
void clock_init(void)
Arch-specific implementation of clock_init for the cc2538.
Definition: clock.c:93