dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_inlinedelay.h
Go to the documentation of this file.
1 
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20  * for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, see <http://www.gnu.org/licenses/>
24  *
25  * Additional note on redistribution: The copyright and license notices above
26  * must be maintained in each individual source file that is a derivative work
27  * of this source file; otherwise redistribution is prohibited.
28  */
29 
30 #ifndef _PIOS_INLINEDELAY_H
31 #define _PIOS_INLINEDELAY_H
32 
33 #include <pios.h>
34 
37 {
38  RCC_ClocksTypeDef clocks;
39 
40  RCC_GetClocksFreq(&clocks);
41  uint32_t us_ticks = clocks.SYSCLK_Frequency / 1000000;
42  PIOS_DEBUG_Assert(us_ticks > 1);
43 
44  uint32_t other_us_ticks = PIOS_SYSCLK / 1000000;
45 
46  PIOS_Assert(us_ticks = other_us_ticks);
47 }
48 
49 static inline uint32_t PIOS_INLINEDELAY_NsToCycles(uint32_t ns)
50 {
51  /* assumptions: integral number of MHz. */
52 
53  /* compute the number of system clocks per microsecond */
54 
55  uint32_t ticks = PIOS_SYSCLK / 1000;
56 
57  ticks <<= 10; /* 22.10 KHz */
58 
59  ticks += 500000;
60 
61  ticks /= 1000000; /* 22.10 GHz */
62 
63  ticks *= ns; /* 22.10 GHz * ns = 22.10 cycles */
64 
65  return (ticks + 512) >> 10;
66 }
67 
68 static inline uint32_t PIOS_INLINEDELAY_GetCycleCnt()
69 {
70  CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
71  return DWT->CYCCNT;
72 }
73 
74 static inline void PIOS_INLINEDELAY_TillCycleCnt(uint32_t whence)
75 {
76  uint32_t value;
77 
78  /* DWT access should have been enabled to get value already */
79 
80  do {
81  value = whence - DWT->CYCCNT;
82  } while (value < 0xe0000000); // Windowed against overflow.
83 }
84 
85 #endif /* _PIOS_INLINEDELAY_H */
86 
87 
static void PIOS_INLINEDELAY_AssertClockSpeed()
Main PiOS header to include all the compiled in PiOS options.
static uint32_t PIOS_INLINEDELAY_NsToCycles(uint32_t ns)
#define PIOS_DEBUG_Assert(test)
Definition: pios_debug.h:51
uint16_t value
Definition: storm32bgc.c:155
static void PIOS_INLINEDELAY_TillCycleCnt(uint32_t whence)
static uint32_t PIOS_INLINEDELAY_GetCycleCnt()
static uint32_t us_ticks
Definition: pios_delay.c:38
#define PIOS_SYSCLK
Definition: pios_board.h:143
#define PIOS_Assert(test)
Definition: pios_debug.h:52