Contiki-NG
rtimer-arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, Toshiba BRIL
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /**
31  * \addtogroup nrf52840
32  * @{
33  *
34  * \file
35  * Architecture dependent rtimer implementation header file.
36  */
37 /*---------------------------------------------------------------------------*/
38 #include "contiki.h"
39 #include "nrf.h"
40 #include "nrf_timer.h"
41 
42 #include <stdint.h>
43 #include <stddef.h>
44 /*---------------------------------------------------------------------------*/
45 /*
46  * Use Timer RTIMER_TIMER at 62500Hz. Generates 1 tick per exactly 16 usecs,
47  * which is exactly 1 .15.4 symbol period.
48  */
49 #define TIMER_INSTANCE NRF_TIMER0
50 /*---------------------------------------------------------------------------*/
51 void
53 {
54  nrf_timer_event_clear(TIMER_INSTANCE, NRF_TIMER_EVENT_COMPARE0);
55 
56  nrf_timer_frequency_set(TIMER_INSTANCE, NRF_TIMER_FREQ_62500Hz);
57  nrf_timer_bit_width_set(TIMER_INSTANCE, NRF_TIMER_BIT_WIDTH_32);
58  nrf_timer_mode_set(TIMER_INSTANCE, NRF_TIMER_MODE_TIMER);
59  nrf_timer_int_enable(TIMER_INSTANCE, NRF_TIMER_INT_COMPARE0_MASK);
60  NVIC_ClearPendingIRQ(TIMER0_IRQn);
61  NVIC_EnableIRQ(TIMER0_IRQn);
62  nrf_timer_task_trigger(TIMER_INSTANCE, NRF_TIMER_TASK_START);
63 }
64 /*---------------------------------------------------------------------------*/
65 /**
66  * \brief Schedules an rtimer task to be triggered at time t
67  * \param t The time when the task will need executed.
68  *
69  * \e t is an absolute time, in other words the task will be executed AT
70  * time \e t, not IN \e t rtimer ticks.
71  *
72  * This function schedules a one-shot event with the nRF RTC.
73  */
74 void
75 rtimer_arch_schedule(rtimer_clock_t t)
76 {
77  nrf_timer_cc_write(TIMER_INSTANCE, NRF_TIMER_CC_CHANNEL0, t);
78 }
79 /*---------------------------------------------------------------------------*/
80 rtimer_clock_t
82 {
83  nrf_timer_task_trigger(TIMER_INSTANCE, NRF_TIMER_TASK_CAPTURE1);
84  return nrf_timer_cc_read(TIMER_INSTANCE, NRF_TIMER_CC_CHANNEL1);
85 }
86 /*---------------------------------------------------------------------------*/
87 void
88 TIMER0_IRQHandler(void)
89 {
90  if(nrf_timer_event_check(TIMER_INSTANCE, NRF_TIMER_EVENT_COMPARE0)) {
91  nrf_timer_event_clear(TIMER_INSTANCE, NRF_TIMER_EVENT_COMPARE0);
93  }
94 }
95 /*---------------------------------------------------------------------------*/
96 /**
97  * @}
98  */
rtimer_clock_t rtimer_arch_now()
Returns the current real-time clock time.
Definition: rtimer-arch.c:115
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition: rtimer-arch.c:71
void rtimer_arch_init(void)
We don&#39;t need to explicitly initialise anything but this routine is required by the API...
Definition: rtimer-arch.c:59
void rtimer_run_next(void)
Execute the next real-time task and schedule the next task, if any.
Definition: rtimer.c:92