1 /***********************************************************************************************************************
2 * Copyright [2020-2022] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved.
3 *
4 * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products
5 * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are
6 * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use
7 * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property
8 * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas
9 * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION
10 * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT
11 * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES
12 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR
13 * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM
14 * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION
15 * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING,
16 * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS,
17 * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY
18 * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS.
19 **********************************************************************************************************************/
20
21 #ifndef RENESAS_TFU
22 #define RENESAS_TFU
23
24 /***********************************************************************************************************************
25 * Includes <System Includes> , "Project Includes"
26 **********************************************************************************************************************/
27
28 /* Mathematical Functions includes. */
29 #ifdef __cplusplus
30 #include <cmath>
31 #else
32 #include <math.h>
33 #endif
34
35 /** Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */
36 FSP_HEADER
37
38 /*******************************************************************************************************************//**
39 * @addtogroup BSP_MCU
40 * @{
41 **********************************************************************************************************************/
42
43 /***********************************************************************************************************************
44 * Macro definitions
45 **********************************************************************************************************************/
46
47 #define R_TFU_HYPOT_SCALING_FACTOR 0.607252935f
48
49 #ifdef __GNUC__ /* and (arm)clang */
50 #if (__STDC_VERSION__ < 199901L) && defined(__STRICT_ANSI__) && !defined(__cplusplus)
51
52 /* No form of inline is available, it happens only when -std=c89, gnu89 and
53 * above are OK */
54 #warning \
55 "-std=c89 doesn't support type checking on TFU. Please use -std=gnu89 or higher for example -std=c99"
56 #else
57 #ifdef __GNUC_GNU_INLINE__
58
59 /* gnu89 semantics of inline and extern inline are essentially the exact
60 * opposite of those in C99 */
61 #define BSP_TFU_INLINE extern inline __attribute__((always_inline))
62 #else /* __GNUC_STDC_INLINE__ */
63 #define BSP_TFU_INLINE static inline __attribute__((always_inline))
64 #endif
65 #endif
66 #elif __ICCARM__
67 #define BSP_TFU_INLINE
68 #else
69 #error "Compiler not supported!"
70 #endif
71
72 /***********************************************************************************************************************
73 * Typedef definitions
74 **********************************************************************************************************************/
75
76 /***********************************************************************************************************************
77 * Exported global variables
78 **********************************************************************************************************************/
79
80 /***********************************************************************************************************************
81 * Inline Functions
82 **********************************************************************************************************************/
83
84 /*******************************************************************************************************************//**
85 * Calculates sine of the given angle.
86 * @param[in] angle The value of an angle in radian.
87 *
88 * @retval Sine value of an angle.
89 **********************************************************************************************************************/
90 #if __ICCARM__
91 #pragma inline = forced
92 #endif
__sinf(float angle)93 BSP_TFU_INLINE float __sinf (float angle)
94 {
95 /* Set the angle to R_TFU->SCDT1 */
96 R_TFU->SCDT1 = angle;
97
98 /* Read sin from R_TFU->SCDT1 */
99 return R_TFU->SCDT1;
100 }
101
102 /*******************************************************************************************************************//**
103 * Calculates cosine of the given angle.
104 * @param[in] angle The value of an angle in radian.
105 *
106 * @retval Cosine value of an angle.
107 **********************************************************************************************************************/
108 #if __ICCARM__
109 #pragma inline = forced
110 #endif
__cosf(float angle)111 BSP_TFU_INLINE float __cosf (float angle)
112 {
113 /* Set the angle to R_TFU->SCDT1 */
114 R_TFU->SCDT1 = angle;
115
116 /* Read cos from R_TFU->SCDT1 */
117 return R_TFU->SCDT0;
118 }
119
120 /*******************************************************************************************************************//**
121 * Calculates sine and cosine of the given angle.
122 * @param[in] angle The value of an angle in radian.
123 * @param[out] sin Sine value of an angle.
124 * @param[out] cos Cosine value of an angle.
125 **********************************************************************************************************************/
126 #if __ICCARM__
127 #pragma inline = forced
128 #endif
__sincosf(float angle,float * sin,float * cos)129 BSP_TFU_INLINE void __sincosf (float angle, float * sin, float * cos)
130 {
131 /* Set the angle to R_TFU->SCDT1 */
132 R_TFU->SCDT1 = angle;
133
134 /* Read sin from R_TFU->SCDT1 */
135 *sin = R_TFU->SCDT1;
136
137 /* Read sin from R_TFU->SCDT1 */
138 *cos = R_TFU->SCDT0;
139 }
140
141 /*******************************************************************************************************************//**
142 * Calculates the arc tangent based on given X-cordinate and Y-cordinate values.
143 * @param[in] y_cord Y-Axis cordinate value.
144 * @param[in] x_cord X-Axis cordinate value.
145 *
146 * @retval Arc tangent for given values.
147 **********************************************************************************************************************/
148 #if __ICCARM__
149 #pragma inline = forced
150 #endif
__atan2f(float y_cord,float x_cord)151 BSP_TFU_INLINE float __atan2f (float y_cord, float x_cord)
152 {
153 /* Set X-cordinate to R_TFU->ATDT0 */
154 R_TFU->ATDT0 = x_cord;
155
156 /* set Y-cordinate to R_TFU->ATDT1 */
157 R_TFU->ATDT1 = y_cord;
158
159 /* Read arctan(y/x) from R_TFU->ATDT1 */
160 return R_TFU->ATDT1;
161 }
162
163 /*******************************************************************************************************************//**
164 * Calculates the hypotenuse based on given X-cordinate and Y-cordinate values.
165 * @param[in] y_cord Y-cordinate value.
166 * @param[in] x_cord X-cordinate value.
167 *
168 * @retval Hypotenuse for given values.
169 **********************************************************************************************************************/
170 #if __ICCARM__
171 #pragma inline = forced
172 #endif
__hypotf(float x_cord,float y_cord)173 BSP_TFU_INLINE float __hypotf (float x_cord, float y_cord)
174 {
175 /* Set X-coordinate to R_TFU->ATDT0 */
176 R_TFU->ATDT0 = x_cord;
177
178 /* set Y-coordinate to R_TFU->ATDT1 */
179 R_TFU->ATDT1 = y_cord;
180
181 /* Read sqrt (x_cord2 + y_cord2) from R_TFU->ATDT0 */
182 return R_TFU->ATDT0 * R_TFU_HYPOT_SCALING_FACTOR;
183 }
184
185 /*******************************************************************************************************************//**
186 * Calculates the arc tangent and hypotenuse based on given X-cordinate and Y-cordinate values.
187 * @param[in] y_cord Y-cordinate value.
188 * @param[in] x_cord X-cordinate value.
189 * @param[out] atan2 Arc tangent for given values.
190 * @param[out] hypot Hypotenuse for given values.
191 **********************************************************************************************************************/
192 #if __ICCARM__
193 #pragma inline = forced
194 #endif
__atan2hypotf(float y_cord,float x_cord,float * atan2,float * hypot)195 BSP_TFU_INLINE void __atan2hypotf (float y_cord, float x_cord, float * atan2, float * hypot)
196 {
197 /* Set X-coordinate to R_TFU->ATDT0 */
198 R_TFU->ATDT0 = x_cord;
199
200 /* set Y-coordinate to R_TFU->ATDT1 */
201 R_TFU->ATDT1 = y_cord;
202
203 /* Read arctan(y/x) from R_TFU->ATDT1 */
204 *atan2 = R_TFU->ATDT1;
205
206 /* Read sqrt (x_cord2 + y_cord2) from R_TFU->ATDT0 */
207 *hypot = R_TFU->ATDT0 * R_TFU_HYPOT_SCALING_FACTOR;
208 }
209
210 #if BSP_CFG_USE_TFU_MATHLIB
211 #define sinf(x) __sinf(x)
212 #define cosf(x) __cosf(x)
213 #define atan2f(y, x) __atan2f(y, x)
214 #define hypotf(x, y) __hypotf(x, y)
215 #define atan2hypotf(y, x, a, h) __atan2hypotf(y, x, a, h)
216 #define sincosf(a, s, c) __sincosf(a, s, c)
217 #endif
218
219 /***********************************************************************************************************************
220 * Exported global functions (to be accessed by other files)
221 **********************************************************************************************************************/
222
223 /** @} (end addtogroup BSP_MCU) */
224
225 /** Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
226 FSP_FOOTER
227
228 #endif /* RENESAS_TFU */
229