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 nrf52840
32  * @{
33  *
34  * \addtogroup nrf52840-dev Device drivers
35  * @{
36  *
37  * \addtogroup nrf52840-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 "sdk_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 /** \brief Function starting the internal LFCLK XTAL oscillator.
77  */
78 static void
80 {
81  ret_code_t err_code = nrf_drv_clock_init();
82  APP_ERROR_CHECK(err_code);
83  nrf_drv_clock_lfclk_request(NULL);
84 }
85 /**
86  * \brief Function initialization and configuration of RTC driver instance.
87  */
88 static void
90 {
91  uint32_t err_code;
92 
93  /*Initialize RTC instance */
94  nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
95  config.prescaler = 255;
96  config.interrupt_priority = 6;
97  config.reliable = 0;
98 
99  err_code = nrf_drv_rtc_init(&rtc, &config, 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  lfclk_config();
114  rtc_config();
115 }
116 /*---------------------------------------------------------------------------*/
117 clock_time_t
119 {
120  return (clock_time_t)(ticks & 0xFFFFFFFF);
121 }
122 /*---------------------------------------------------------------------------*/
123 void
124 clock_update(void)
125 {
126  ticks++;
127  if(etimer_pending()) {
129  }
130 }
131 /*---------------------------------------------------------------------------*/
132 unsigned long
134 {
135  return (unsigned long)ticks / CLOCK_CONF_SECOND;
136 }
137 /*---------------------------------------------------------------------------*/
138 void
139 clock_wait(clock_time_t i)
140 {
141  clock_time_t start;
142  start = clock_time();
143  while(clock_time() - start < (clock_time_t)i) {
144  __WFE();
145  }
146 }
147 /*---------------------------------------------------------------------------*/
148 void
149 clock_delay_usec(uint16_t dt)
150 {
151  nrf_delay_us(dt);
152 }
153 /*---------------------------------------------------------------------------*/
154 /**
155  * \brief Obsolete delay function but we implement it here since some code
156  * still uses it
157  */
158 void
159 clock_delay(unsigned int i)
160 {
161  clock_delay_usec(i);
162 }
163 /*---------------------------------------------------------------------------*/
164 /**
165  * @}
166  * @}
167  * @}
168  */
const nrf_drv_rtc_t rtc
RTC instance used for platform clock.
Definition: clock.c:58
#define PLATFORM_RTC_INSTANCE_ID
nRF52 RTC instance to be used for Contiki clock driver.
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
static void rtc_config(void)
Function initialization and configuration of RTC driver instance.
Definition: clock.c:89
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:79
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
static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
Function for handling the RTC0 interrupts.
Definition: clock.c:70
static int config(int type, int c, nrf_drv_gpiote_pin_t pin)
Configuration function for the button sensor for all buttons.
void clock_init(void)
Arch-specific implementation of clock_init for the cc2538.
Definition: clock.c:93