Goodix
GR551x API Reference  V1_6_06_B5676
gr55xx_delay.h
Go to the documentation of this file.
1 /**
2  ****************************************************************************************
3  *
4  * @file gr55xx_delay.h
5  *
6  * @brief PERIPHERAL API DELAY DRIVER
7  *
8  ****************************************************************************************
9  * @attention
10  #####Copyright (c) 2019 GOODIX
11  All rights reserved.
12 
13  Redistribution and use in source and binary forms, with or without
14  modification, are permitted provided that the following conditions are met:
15  * Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17  * Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20  * Neither the name of GOODIX nor the names of its contributors may be used
21  to endorse or promote products derived from this software without
22  specific prior written permission.
23 
24  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  POSSIBILITY OF SUCH DAMAGE.
35  ****************************************************************************************
36  */
37 
38 #ifndef __GR55xx_DELAY_H__
39 #define __GR55xx_DELAY_H__
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 #include "gr55xx.h"
46 
47 
48 #if defined ( __CC_ARM )
49 
50 #ifndef __STATIC_FORCEINLINE
51 #define __STATIC_FORCEINLINE static __forceinline /**< Static inline define */
52 #endif
53 
54 #elif defined ( __GNUC__ )
55 
56 #ifndef __STATIC_FORCEINLINE
57 #define __STATIC_FORCEINLINE __attribute__((always_inline)) static inline /**< Static inline define */
58 #endif
59 
60 #else
61 
62 #ifndef __STATIC_FORCEINLINE
63 #define __STATIC_FORCEINLINE __STATIC_INLINE /**< Static inline define */
64 #endif
65 
66 #endif
67 
68 /**
69  * @brief Function for delaying execution for number of microseconds.
70  *
71  * @note GR55xxx is based on ARM Cortex-M4, and this fuction is based on Data Watchpoint and Trace (DWT) unit so delay is precise.
72  *
73  * @param number_of_us: The maximum delay time is about 67 seconds in 64M system clock.
74  * The faster the system clock, the shorter the maximum delay time.
75  *
76  */
77 __STATIC_FORCEINLINE void delay_us(uint32_t number_of_us)
78 {
79 #if defined(GR551xx)
80  uint8_t clocks[] = {64, 48, 16, 24, 16, 32};
81  uint32_t cycles = number_of_us * (clocks[AON->PWR_RET01 & AON_PWR_REG01_SYS_CLK_SEL]);
82 #else
83  uint8_t clocks[] = {96, 64, 16, 48, 24, 16, 32};
84  uint32_t cycles = number_of_us * clocks[AON_CTL->MCU_CLK_CTRL & AON_CTL_MCU_CLK_CTRL_SEL];
85 #endif
86 
87  if (number_of_us == 0)
88  {
89  return;
90  }
91 
92  // Save the DEMCR register value which is used to restore it
93  uint32_t core_debug_initial = CoreDebug->DEMCR;
94  // Enable DWT
95  CoreDebug->DEMCR = core_debug_initial | CoreDebug_DEMCR_TRCENA_Msk;
96 
97  // Save the CTRL register value which is used to restore it
98  uint32_t dwt_ctrl_initial = DWT->CTRL;
99  // Enable cycle counter
100  DWT->CTRL = dwt_ctrl_initial | DWT_CTRL_CYCCNTENA_Msk;
101 
102  // Get start value of the cycle counter.
103  uint32_t cyccnt_initial = DWT->CYCCNT;
104 
105  // Wait time end
106  while ((DWT->CYCCNT - cyccnt_initial) < cycles)
107  {}
108 
109  // Restore registers.
110  DWT->CTRL = dwt_ctrl_initial;
111  CoreDebug->DEMCR = core_debug_initial;
112 }
113 
114 /**
115  * @brief Function for delaying execution for number of milliseconds.
116  *
117  * @note GR55xx is based on ARM Cortex-M4, and this fuction is based on Data Watchpoint and Trace (DWT) unit so delay is precise.
118  *
119  * @note Function internally calls @ref delay_us so the maximum delay is the
120  * same as in case of @ref delay_us.
121  *
122  * @param number_of_ms: The maximum delay time is about 67 seconds in 64M system clock.
123  * The faster the system clock, the shorter the maximum delay time.
124  *
125  */
126 __STATIC_FORCEINLINE void delay_ms(uint32_t number_of_ms)
127 {
128  delay_us(1000 * number_of_ms);
129  return;
130 }
131 
132 #ifdef __cplusplus
133 }
134 #endif
135 
136 #endif /* __GR55xx_DELAY_H__ */
delay_us
__STATIC_FORCEINLINE void delay_us(uint32_t number_of_us)
Function for delaying execution for number of microseconds.
Definition: gr55xx_delay.h:77
__STATIC_FORCEINLINE
#define __STATIC_FORCEINLINE
Static inline define.
Definition: gr55xx_delay.h:63
delay_ms
__STATIC_FORCEINLINE void delay_ms(uint32_t number_of_ms)
Function for delaying execution for number of milliseconds.
Definition: gr55xx_delay.h:126