BSPACM  20150113
Board Support Package for ARM Cortex-M Microcontrollers
hires.h
Go to the documentation of this file.
1 /* Copyright 2015, Peter A. Bigot
2  *
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 are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * * Neither the name of the software nor the names of its contributors may be
16  * used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
50 #ifndef BSPACM_DEVICE_NRF51_INTERNAL_UTILITY_HIRES_H
51 #define BSPACM_DEVICE_NRF51_INTERNAL_UTILITY_HIRES_H
52 
53 #include <bspacm/core.h>
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif /* __cplusplus */
58 
59 #ifndef BSPACM_HIRES_TIMER_BASE
60 
65 #define BSPACM_HIRES_TIMER_BASE NRF_TIMER1_BASE
66 #endif /* BSPACM_HIRES_TIMER_BASE */
67 
76 #if NRF_TIMER0_BASE == BSPACM_HIRES_TIMER_BASE
77 #define BSPACM_HIRES_TIMER NRF_TIMER0
78 #elif NRF_TIMER1_BASE == BSPACM_HIRES_TIMER_BASE
79 #define BSPACM_HIRES_TIMER NRF_TIMER1
80 #elif NRF_TIMER2_BASE == BSPACM_HIRES_TIMER_BASE
81 #define BSPACM_HIRES_TIMER NRF_TIMER2
82 #else /* BSPACM_HIRES_TIMER_BASE */
83 #error Unrecognized high-resolution timer
84 #endif /* BSPACM_HIRES_TIMER_BASE */
85 
96 int
97 iBSPACMhiresInitialize (unsigned int freq_Hz);
98 
101 bool
102 bBSPACMhiresEnabled (void);
103 
111 int
112 iBSPACMhiresSetEnabled (bool enabled);
113 
121 inline
122 unsigned int
124 {
125  BSPACM_HIRES_TIMER->TASKS_CAPTURE[0] = 1;
126  return BSPACM_HIRES_TIMER->CC[0];
127 }
128 
131 inline
132 unsigned int
133 uiBSPACMhiresConvert_hfclk_hrt (unsigned int dur_hfclk)
134 {
135  return dur_hfclk >> BSPACM_HIRES_TIMER->PRESCALER;
136 }
137 
140 inline
141 unsigned int
142 uiBSPACMhiresConvert_hrt_hfclk (unsigned int dur_hrt)
143 {
144  return dur_hrt << BSPACM_HIRES_TIMER->PRESCALER;
145 }
146 
148 inline
149 unsigned int
150 uiBSPACMhiresConvert_us_hfclk (unsigned int dur_us)
151 {
152  return dur_us << 4;
153 }
154 
156 inline
157 unsigned int
158 uiBSPACMhiresConvert_us_hrt (unsigned int dur_us)
159 {
160  int shift = 4 - (int)BSPACM_HIRES_TIMER->PRESCALER;
161  if (0 < shift) {
162  return dur_us << shift;
163  }
164  return dur_us >> -shift;
165 }
166 
168 inline
169 unsigned int
170 uiBSPACMhiresConvert_hrt_us (unsigned int dur_hrt)
171 {
172  int shift = 4 - (int)BSPACM_HIRES_TIMER->PRESCALER;
173  if (0 < shift) {
174  return dur_hrt >> shift;
175  }
176  return dur_hrt << -shift;
177 }
178 
201 void
202 vBSPACMhiresSleep_us (unsigned long count_us);
203 
215 inline void
216 vBSPACMhiresSleep_ms (unsigned long count_ms)
217 {
218  vBSPACMhiresSleep_us(1000 * count_ms);
219 }
220 
221 #ifdef __cplusplus
222 }
223 #endif /* __cplusplus */
224 
225 #endif /* BSPACM_DEVICE_NRF51_INTERNAL_UTILITY_HIRES_H */
unsigned int uiBSPACMhiresConvert_hfclk_hrt(unsigned int dur_hfclk)
Definition: hires.h:133
unsigned int uiBSPACMhires(void)
Definition: hires.h:123
unsigned int uiBSPACMhiresConvert_hrt_us(unsigned int dur_hrt)
Definition: hires.h:170
unsigned int uiBSPACMhiresConvert_us_hfclk(unsigned int dur_us)
Definition: hires.h:150
unsigned int uiBSPACMhiresConvert_us_hrt(unsigned int dur_us)
Definition: hires.h:158
Common header included by all BSPACM leaf headers.
unsigned int uiBSPACMhiresConvert_hrt_hfclk(unsigned int dur_hrt)
Definition: hires.h:142
#define BSPACM_HIRES_TIMER
Definition: hires.h:77
int iBSPACMhiresInitialize(unsigned int freq_Hz)
bool bBSPACMhiresEnabled(void)
void vBSPACMhiresSleep_us(unsigned long count_us)
void vBSPACMhiresSleep_ms(unsigned long count_ms)
Definition: hires.h:216
int iBSPACMhiresSetEnabled(bool enabled)