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 "contiki.h"
48#include <stdint.h>
49#include <stdbool.h>
50#include "nrf.h"
51#include "sdk_config.h"
52#include "nrf_drv_rtc.h"
53#include "nrf_drv_clock.h"
54#include "nrf_delay.h"
55#include "app_error.h"
56
57/*---------------------------------------------------------------------------*/
58const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(PLATFORM_RTC_INSTANCE_ID); /**< RTC instance used for platform clock */
59/*---------------------------------------------------------------------------*/
60static volatile uint32_t ticks;
61void 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 */
69static void
70rtc_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 */
78static 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 */
88static 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/*---------------------------------------------------------------------------*/
109void
111{
112 ticks = 0;
113 lfclk_config();
114 rtc_config();
115}
116/*---------------------------------------------------------------------------*/
117clock_time_t
119{
120 return (clock_time_t)(ticks & 0xFFFFFFFF);
121}
122/*---------------------------------------------------------------------------*/
123void
124clock_update(void)
125{
126 ticks++;
127 if(etimer_pending()) {
129 }
130}
131/*---------------------------------------------------------------------------*/
132unsigned long
134{
135 return (unsigned long)ticks / CLOCK_CONF_SECOND;
136}
137/*---------------------------------------------------------------------------*/
138void
139clock_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/*---------------------------------------------------------------------------*/
148void
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 */
158void
159clock_delay(unsigned int i)
160{
162}
163/*---------------------------------------------------------------------------*/
164/**
165 * @}
166 * @}
167 * @}
168 */
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:130
void clock_init(void)
Arch-specific implementation of clock_init for the cc2538.
Definition: clock.c:93
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
clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:118
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
Definition: clock.c:164
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
static void rtc_config(void)
Function initialization and configuration of RTC driver instance.
Definition: clock.c:89
static void lfclk_config(void)
Function starting the internal LFCLK XTAL oscillator.
Definition: clock.c:79
static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
Function for handling the RTC0 interrupts.
Definition: clock.c:70
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.
static void start(void)
Start measurement.