1 /*
2 * Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6 
7 #ifndef RENESAS_TFU
8 #define RENESAS_TFU
9 
10 /***********************************************************************************************************************
11  * Includes   <System Includes> , "Project Includes"
12  **********************************************************************************************************************/
13 
14 /* Mathematical Functions includes. */
15 #include <math.h>
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /** Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */
21 FSP_HEADER
22 
23 /*******************************************************************************************************************//**
24  * @addtogroup BSP_MCU
25  * @{
26  **********************************************************************************************************************/
27 
28 #if BSP_FEATURE_TFU_SUPPORTED
29 
30 /***********************************************************************************************************************
31  * Macro definitions
32  **********************************************************************************************************************/
33 
34  #define R_TFU_HYPOT_SCALING_FACTOR    0.607252935f
35 
36  #ifdef __GNUC__                       /* and (arm)clang */
37   #if (__STDC_VERSION__ < 199901L) && defined(__STRICT_ANSI__) && !defined(__cplusplus)
38 
39 /* No form of inline is available, it happens only when -std=c89, gnu89 and
40  * above are OK */
41    #warning \
42     "-std=c89 doesn't support type checking on TFU. Please use -std=gnu89 or higher for example -std=c99"
43   #else
44    #ifdef __GNUC_GNU_INLINE__
45 
46 /* gnu89 semantics of inline and extern inline are essentially the exact
47  * opposite of those in C99 */
48     #define BSP_TFU_INLINE    extern inline __attribute__((always_inline))
49    #else                               /* __GNUC_STDC_INLINE__ */
50     #define BSP_TFU_INLINE    static inline __attribute__((always_inline))
51    #endif
52   #endif
53  #elif __ICCARM__
54   #define BSP_TFU_INLINE
55  #else
56   #error "Compiler not supported!"
57  #endif
58 
59 /***********************************************************************************************************************
60  * Typedef definitions
61  **********************************************************************************************************************/
62 
63 /***********************************************************************************************************************
64  * Exported global variables
65  **********************************************************************************************************************/
66 
67 /***********************************************************************************************************************
68  * Inline Functions
69  **********************************************************************************************************************/
70 
71 /*******************************************************************************************************************//**
72  * Calculates sine of the given angle.
73  * @param[in]    angle  The value of an angle in radian.
74  *
75  * @retval Sine value of an angle.
76  **********************************************************************************************************************/
77  #if __ICCARM__
78   #pragma inline = forced
79  #endif
__sinf(float angle)80 BSP_TFU_INLINE float __sinf (float angle)
81 {
82     /* Set the angle to R_TFU->SCDT1 */
83     R_TFU->SCDT1 = angle;
84 
85     /* Read sin from R_TFU->SCDT1 */
86     return R_TFU->SCDT1;
87 }
88 
89 /*******************************************************************************************************************//**
90  * Calculates cosine of the given angle.
91  * @param[in]    angle  The value of an angle in radian.
92  *
93  * @retval Cosine value of an angle.
94  **********************************************************************************************************************/
95  #if __ICCARM__
96   #pragma inline = forced
97  #endif
__cosf(float angle)98 BSP_TFU_INLINE float __cosf (float angle)
99 {
100     /* Set the angle to R_TFU->SCDT1 */
101     R_TFU->SCDT1 = angle;
102 
103     /* Read cos from R_TFU->SCDT1 */
104     return R_TFU->SCDT0;
105 }
106 
107 /*******************************************************************************************************************//**
108  * Calculates sine and cosine of the given angle.
109  * @param[in]    angle  The value of an angle in radian.
110  * @param[out]   sin    Sine value of an angle.
111  * @param[out]   cos    Cosine value of an angle.
112  **********************************************************************************************************************/
113  #if __ICCARM__
114   #pragma inline = forced
115  #endif
__sincosf(float angle,float * sin,float * cos)116 BSP_TFU_INLINE void __sincosf (float angle, float * sin, float * cos)
117 {
118     /* Set the angle to R_TFU->SCDT1 */
119     R_TFU->SCDT1 = angle;
120 
121     /* Read sin from R_TFU->SCDT1 */
122     *sin = R_TFU->SCDT1;
123 
124     /* Read sin from R_TFU->SCDT1 */
125     *cos = R_TFU->SCDT0;
126 }
127 
128 /*******************************************************************************************************************//**
129  * Calculates the arc tangent based on given X-cordinate and Y-cordinate values.
130  * @param[in]    y_cord  Y-Axis cordinate value.
131  * @param[in]    x_cord  X-Axis cordinate value.
132  *
133  * @retval Arc tangent for given values.
134  **********************************************************************************************************************/
135  #if __ICCARM__
136   #pragma inline = forced
137  #endif
__atan2f(float y_cord,float x_cord)138 BSP_TFU_INLINE float __atan2f (float y_cord, float x_cord)
139 {
140     /* Set X-cordinate to R_TFU->ATDT0 */
141     R_TFU->ATDT0 = x_cord;
142 
143     /* set Y-cordinate to R_TFU->ATDT1 */
144     R_TFU->ATDT1 = y_cord;
145 
146     /* Read arctan(y/x) from R_TFU->ATDT1 */
147     return R_TFU->ATDT1;
148 }
149 
150 /*******************************************************************************************************************//**
151  * Calculates the hypotenuse based on given X-cordinate and Y-cordinate values.
152  * @param[in]    y_cord  Y-cordinate value.
153  * @param[in]    x_cord  X-cordinate value.
154  *
155  * @retval Hypotenuse for given values.
156  **********************************************************************************************************************/
157  #if __ICCARM__
158   #pragma inline = forced
159  #endif
__hypotf(float x_cord,float y_cord)160 BSP_TFU_INLINE float __hypotf (float x_cord, float y_cord)
161 {
162     /* Set X-coordinate to R_TFU->ATDT0 */
163     R_TFU->ATDT0 = x_cord;
164 
165     /* set Y-coordinate to R_TFU->ATDT1 */
166     R_TFU->ATDT1 = y_cord;
167 
168     /* Read sqrt (x_cord2 + y_cord2) from R_TFU->ATDT0 */
169     return R_TFU->ATDT0 * R_TFU_HYPOT_SCALING_FACTOR;
170 }
171 
172 /*******************************************************************************************************************//**
173  * Calculates the arc tangent and hypotenuse based on given X-cordinate and Y-cordinate values.
174  * @param[in]    y_cord  Y-cordinate value.
175  * @param[in]    x_cord  X-cordinate value.
176  * @param[out]   atan2   Arc tangent for given values.
177  * @param[out]   hypot   Hypotenuse for given values.
178  **********************************************************************************************************************/
179  #if __ICCARM__
180   #pragma inline = forced
181  #endif
__atan2hypotf(float y_cord,float x_cord,float * atan2,float * hypot)182 BSP_TFU_INLINE void __atan2hypotf (float y_cord, float x_cord, float * atan2, float * hypot)
183 {
184     /* Set X-coordinate to R_TFU->ATDT0 */
185     R_TFU->ATDT0 = x_cord;
186 
187     /* set Y-coordinate to R_TFU->ATDT1 */
188     R_TFU->ATDT1 = y_cord;
189 
190     /* Read arctan(y/x) from R_TFU->ATDT1 */
191     *atan2 = R_TFU->ATDT1;
192 
193     /* Read sqrt (x_cord2 + y_cord2) from R_TFU->ATDT0 */
194     *hypot = R_TFU->ATDT0 * R_TFU_HYPOT_SCALING_FACTOR;
195 }
196 
197  #if BSP_CFG_USE_TFU_MATHLIB
198   #define sinf(x)                    __sinf(x)
199   #define cosf(x)                    __cosf(x)
200   #define atan2f(y, x)               __atan2f(y, x)
201   #define hypotf(x, y)               __hypotf(x, y)
202   #define atan2hypotf(y, x, a, h)    __atan2hypotf(y, x, a, h)
203   #define sincosf(a, s, c)           __sincosf(a, s, c)
204  #endif
205 
206 /***********************************************************************************************************************
207  * Exported global functions (to be accessed by other files)
208  **********************************************************************************************************************/
209 
210 #endif
211 
212 /** @} (end addtogroup BSP_MCU) */
213 
214 /** Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
215 FSP_FOOTER
216 
217 #ifdef __cplusplus
218 }
219 #endif
220 
221 #endif                                 /* RENESAS_TFU */
222