Contiki-NG
Toggle main menu visibility
Loading...
Searching...
No Matches
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
/*---------------------------------------------------------------------------*/
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
79
lfclk_config
(
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
89
rtc_config
(
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
110
clock_init
(
void
)
111
{
112
ticks = 0;
113
lfclk_config
();
114
rtc_config
();
115
}
116
/*---------------------------------------------------------------------------*/
117
clock_time_t
118
clock_time
(
void
)
119
{
120
return
(clock_time_t)(ticks & 0xFFFFFFFF);
121
}
122
/*---------------------------------------------------------------------------*/
123
void
124
clock_update(
void
)
125
{
126
ticks++;
127
if
(
etimer_pending
()) {
128
etimer_request_poll
();
129
}
130
}
131
/*---------------------------------------------------------------------------*/
132
unsigned
long
133
clock_seconds
(
void
)
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
*/
clock_seconds
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition
clock.c:130
clock_init
void clock_init(void)
Arch-specific implementation of clock_init for the cc2538.
Definition
clock.c:93
clock_delay_usec
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition
clock.c:150
clock_wait
void clock_wait(clock_time_t i)
Wait for a given number of ticks.
Definition
clock.c:136
clock_time
clock_time_t clock_time(void)
Get the current clock time.
Definition
clock.c:118
clock_delay
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
Definition
clock.c:164
etimer_pending
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition
etimer.c:225
etimer_request_poll
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition
etimer.c:145
rtc_handler
static void rtc_handler(nrfx_rtc_int_type_t int_type)
Function for handling the RTC<instance> interrupts.
Definition
clock-arch.c:84
rtc
static const nrfx_rtc_t rtc
< RTC instance used for platform clock
Definition
clock-arch.c:69
rtc_config
static void rtc_config(void)
Function initialization and configuration of RTC driver instance.
Definition
clock.c:89
lfclk_config
static void lfclk_config(void)
Function starting the internal LFCLK XTAL oscillator.
Definition
clock.c:79
rtc_handler
static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
Function for handling the RTC0 interrupts.
Definition
clock.c:70
PLATFORM_RTC_INSTANCE_ID
#define PLATFORM_RTC_INSTANCE_ID
nRF52 RTC instance to be used for Contiki clock driver.
Definition
nrf52840-board-def.h:79
start
static void start(void)
Start measurement.
Definition
hdc-1000-sensor.c:142
arch
cpu
nrf52840
clock.c
Generated on
for Contiki-NG by
1.17.0