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 #include "gr55xx.h"
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 #if defined ( __CC_ARM )
48
49 #ifndef __STATIC_FORCEINLINE
50 #define __STATIC_FORCEINLINE static __forceinline /**< Static inline define */
51 #endif
52
53 #elif defined ( __GNUC__ )
54
55 #ifndef __STATIC_FORCEINLINE
56 #define __STATIC_FORCEINLINE __attribute__((always_inline)) static inline /**< Static inline define */
57 #endif
58
59 #else
60
61 #ifndef __STATIC_FORCEINLINE
62 #define __STATIC_FORCEINLINE __STATIC_INLINE /**< Static inline define */
63 #endif
64
65 #endif
66
67 /**
68 * @brief Function for delaying execution for number of microseconds.
69 *
70 * @note GR55xxx is based on ARM Cortex-M4, and this fuction is based on Data Watchpoint and Trace (DWT) unit
71 * 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 #if defined(GR5515_E)
78 void delay_us(uint32_t number_of_us);
79 #else
80 __STATIC_FORCEINLINE void delay_us(uint32_t number_of_us)
81 {
82 const uint8_t clocks[] = {64, 48, 16, 24, 16, 32};
83 uint32_t cycles = number_of_us * (clocks[AON->PWR_RET01 & AON_PWR_REG01_SYS_CLK_SEL]);
84
85 if (number_of_us == 0) {
86 return;
87 }
88
89 // Save the DEMCR register value which is used to restore it
90 uint32_t core_debug_initial = CoreDebug->DEMCR;
91 // Enable DWT
92 CoreDebug->DEMCR = core_debug_initial | CoreDebug_DEMCR_TRCENA_Msk;
93
94 // Save the CTRL register value which is used to restore it
95 uint32_t dwt_ctrl_initial = DWT->CTRL;
96 // Enable cycle counter
97 DWT->CTRL = dwt_ctrl_initial | DWT_CTRL_CYCCNTENA_Msk;
98
99 // Get start value of the cycle counter.
100 uint32_t cyccnt_initial = DWT->CYCCNT;
101
102 // Wait time end
103 while ((DWT->CYCCNT - cyccnt_initial) < cycles) { }
104
105 // Restore registers.
106 DWT->CTRL = dwt_ctrl_initial;
107 CoreDebug->DEMCR = core_debug_initial;
108 }
109 #endif
110
111 /**
112 * @brief Function for delaying execution for number of milliseconds.
113 *
114 * @note GR55xx is based on ARM Cortex-M4, and this fuction is based on Data Watchpoint and Trace (DWT)
115 * unit so delay is precise.
116 *
117 * @note Function internally calls @ref delay_us so the maximum delay is the
118 * same as in case of @ref delay_us.
119 *
120 * @param number_of_ms: The maximum delay time is about 67 seconds in 64M system clock.
121 * The faster the system clock, the shorter the maximum delay time.
122 *
123 */
124 #if defined(GR5515_E)
125 void delay_ms(uint32_t number_of_ms);
126 #else
127 #define DELAY_CON_FACTOR 1000
delay_ms(uint32_t number_of_ms)128 __STATIC_FORCEINLINE void delay_ms(uint32_t number_of_ms)
129 {
130 delay_us(DELAY_CON_FACTOR * number_of_ms);
131 return;
132 }
133 #endif
134
135 #ifdef __cplusplus
136 }
137 #endif
138
139 #endif /* __GR55xx_DELAY_H__ */
140