Contiki-NG
Loading...
Searching...
No Matches
rtimer-arch.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020, Toshiba BRIL
3 * Copyright (C) 2020 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31/*---------------------------------------------------------------------------*/
32/**
33 * \addtogroup nrf
34 * @{
35 *
36 * \addtogroup nrf-sys System drivers
37 * @{
38 *
39 * \addtogroup nrf-rtimer Rtimer driver
40 * @{
41 *
42 * \file
43 * Implementation of the architecture dependent rtimer functions for the nRF
44 * \author
45 * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
46 *
47 */
48/*---------------------------------------------------------------------------*/
49#include "contiki.h"
50
51#include "nrf.h"
52#include "hal/nrf_timer.h"
53
54#ifdef NRF_RTIMER_CONF_TIMER_INSTANCE
55#define TIMER_INSTANCE NRF_RTIMER_CONF_TIMER_INSTANCE
56#else
57#define TIMER_INSTANCE 0
58#endif
59
60#if TIMER_INSTANCE == 0
61#define NRF_RTIMER_TIMER NRF_TIMER0
62#define NRF_RTIMER_IRQn TIMER0_IRQn
63#define NRF_RTIMER_IRQHandler TIMER0_IRQHandler
64#elif TIMER_INSTANCE == 1
65#define NRF_RTIMER_TIMER NRF_TIMER1
66#define NRF_RTIMER_IRQn TIMER1_IRQn
67#define NRF_RTIMER_IRQHandler TIMER1_IRQHandler
68#else
69#error Unsupported timer for rtimer
70#endif
71
72/*---------------------------------------------------------------------------*/
73void
75{
76 nrf_timer_event_clear(NRF_RTIMER_TIMER, NRF_TIMER_EVENT_COMPARE0);
77
78 nrf_timer_frequency_set(NRF_RTIMER_TIMER, NRF_TIMER_FREQ_62500Hz);
79 nrf_timer_bit_width_set(NRF_RTIMER_TIMER, NRF_TIMER_BIT_WIDTH_32);
80 nrf_timer_mode_set(NRF_RTIMER_TIMER, NRF_TIMER_MODE_TIMER);
81 nrf_timer_int_enable(NRF_RTIMER_TIMER, NRF_TIMER_INT_COMPARE0_MASK);
82 NVIC_ClearPendingIRQ(NRF_RTIMER_IRQn);
83 NVIC_EnableIRQ(NRF_RTIMER_IRQn);
84 nrf_timer_task_trigger(NRF_RTIMER_TIMER, NRF_TIMER_TASK_START);
85}
86/*---------------------------------------------------------------------------*/
87void
88rtimer_arch_schedule(rtimer_clock_t t)
89{
90 /*
91 * This function schedules a one-shot event with the nRF RTC.
92 */
93 nrf_timer_cc_set(NRF_RTIMER_TIMER, NRF_TIMER_CC_CHANNEL0, t);
94}
95/*---------------------------------------------------------------------------*/
96rtimer_clock_t
98{
99 nrf_timer_task_trigger(NRF_RTIMER_TIMER, NRF_TIMER_TASK_CAPTURE1);
100 return nrf_timer_cc_get(NRF_RTIMER_TIMER, NRF_TIMER_CC_CHANNEL1);
101}
102/*---------------------------------------------------------------------------*/
103void
104NRF_RTIMER_IRQHandler(void)
105{
106 if(nrf_timer_event_check(NRF_RTIMER_TIMER, NRF_TIMER_EVENT_COMPARE0)) {
107 nrf_timer_event_clear(NRF_RTIMER_TIMER, NRF_TIMER_EVENT_COMPARE0);
109 }
110}
111/*---------------------------------------------------------------------------*/
112/**
113 * @}
114 * @}
115 * @}
116 */
void rtimer_arch_init(void)
We don't need to explicitly initialise anything but this routine is required by the API.
Definition rtimer-arch.c:59
rtimer_clock_t rtimer_arch_now()
Returns the current real-time clock time.
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition rtimer-arch.c:65
void rtimer_run_next(void)
Execute the next real-time task and schedule the next task, if any.
Definition rtimer.c:78