Contiki-NG
clock-arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
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 cc13xx-cc26xx-cpu
32  * @{
33  *
34  * \defgroup cc13xx-cc26xx-clock CC13xx/CC26xx clock library
35  *
36  * @{
37  * \file
38  * Implementation of the clock libary for CC13xx/CC26xx.
39  * \author
40  * Edvard Pettersen <e.pettersen@ti.com>
41  */
42 /*---------------------------------------------------------------------------*/
43 #include "contiki.h"
44 #include "sys/etimer.h"
45 /*---------------------------------------------------------------------------*/
46 #include <ti/devices/DeviceFamily.h>
47 #include DeviceFamily_constructPath(driverlib/aon_rtc.h)
48 #include DeviceFamily_constructPath(driverlib/cpu.h)
49 #include DeviceFamily_constructPath(driverlib/interrupt.h)
50 #include DeviceFamily_constructPath(driverlib/prcm.h)
51 #include DeviceFamily_constructPath(driverlib/timer.h)
52 
53 #include <ti/drivers/dpl/ClockP.h>
54 #include <ti/drivers/dpl/HwiP.h>
55 #include <ti/drivers/power/PowerCC26XX.h>
56 /*---------------------------------------------------------------------------*/
57 static volatile clock_time_t count;
58 static ClockP_Struct etimer_clock;
59 /*---------------------------------------------------------------------------*/
60 static void
61 clock_update_cb(void)
62 {
63  const uintptr_t key = HwiP_disable();
64  count += 1;
65  HwiP_restore(key);
66 
67  /* Notify the etimer system. */
68  if(etimer_pending()) {
70  }
71 }
72 /*---------------------------------------------------------------------------*/
73 static inline clock_time_t
74 get_count(void)
75 {
76  clock_time_t count_read;
77 
78  const uintptr_t key = HwiP_disable();
79  count_read = count;
80  HwiP_restore(key);
81 
82  return count_read;
83 }
84 /*---------------------------------------------------------------------------*/
85 void
87 {
88  /* ClockP_getSystemTickPeriod() returns ticks per us. */
89  const uint32_t clockp_ticks_second =
90  (uint32_t)(1000 * 1000) / (CLOCK_SECOND) / ClockP_getSystemTickPeriod();
91 
92  count = 0;
93 
94  ClockP_Params params;
95  ClockP_Params_init(&params);
96 
97  params.period = clockp_ticks_second;
98  params.startFlag = true;
99 
100  ClockP_construct(&etimer_clock, (ClockP_Fxn)clock_update_cb,
101  clockp_ticks_second, &params);
102 }
103 /*---------------------------------------------------------------------------*/
104 clock_time_t
106 {
107  return get_count();
108 }
109 /*---------------------------------------------------------------------------*/
110 unsigned long
112 {
113  return (unsigned long)get_count() / CLOCK_SECOND;
114 }
115 /*---------------------------------------------------------------------------*/
116 void
117 clock_wait(clock_time_t i)
118 {
119  clock_time_t start;
120 
121  start = clock_time();
122  while(clock_time() - start < i);
123 }
124 /*---------------------------------------------------------------------------*/
125 void
126 clock_delay_usec(uint16_t usec)
127 {
128  ClockP_usleep(usec);
129 }
130 /*---------------------------------------------------------------------------*/
131 /**
132  * \brief Obsolete delay function but we implement it here since some code
133  * still uses it.
134  */
135 void
136 clock_delay(unsigned int i)
137 {
138  clock_delay_usec(i);
139 }
140 /*---------------------------------------------------------------------------*/
141 /**
142  * @}
143  * @}
144  */
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
Definition: clock-arch.c:136
static bool start(void)
Start measurement.
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
clock_time_t clock_time(void)
Get the current clock time.
Definition: clock-arch.c:105
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
Event timer header file.
void clock_wait(clock_time_t i)
Wait for a given number of ticks.
Definition: clock-arch.c:117
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition: etimer.c:231
void clock_init(void)
Initialize the clock library.
Definition: clock-arch.c:86
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock-arch.c:111
void clock_delay_usec(uint16_t usec)
Delay a given number of microseconds.
Definition: clock-arch.c:126