PER Firmware
Loading...
Searching...
No Matches
common_defs.h
Go to the documentation of this file.
1
12#ifndef COMMON_DEFS_H
13#define COMMON_DEFS_H
14
15#include <stdint.h>
16
17/* Math Functions */
18static inline int32_t min_i(int32_t x, int32_t y) { return (x < y) ? x : y; }
19static inline float min_f(float x, float y) { return (x < y) ? x : y; }
20#define MIN(x, y) _Generic((x), float: min_f, double: min_f, default: min_i)((x), (y))
21
22static inline int32_t max_i(int32_t x, int32_t y) { return (x < y) ? y : x; }
23static inline float max_f(float x, float y) { return (x < y) ? y : x; }
24#define MAX(x, y) _Generic((x), float: max_f, double: max_f, default: max_i)((x), (y))
25
26static inline int32_t clamp_i(int32_t x, int32_t min, int32_t max) {
27 if (x < min) return min;
28 if (x > max) return max;
29 return x;
30}
31static inline float clamp_f(float x, float min, float max) {
32 if (x < min) return min;
33 if (x > max) return max;
34 return x;
35}
36#define CLAMP(x, min, max) _Generic((x), \
37 float: clamp_f, \
38 double: clamp_f, \
39 default: clamp_i \
40)((x), (min), (max))
41
42static inline int32_t abs_i(int32_t x) { return (x < 0) ? -x : x; }
43static inline float abs_f(float x) { return (x < 0.0f) ? -x : x; }
44#define ABS(x) _Generic((x), float: abs_f, double: abs_f, default: abs_i)(x)
45
46// Base-2 logarithm that rounds down
47static inline uint32_t LOG2_DOWN(uint32_t x) {
48 return 31U - (uint32_t)__builtin_clz(x);
49}
50
51// Base-2 logarithm that rounds up
52static inline uint32_t LOG2_UP(uint32_t x) {
53 return LOG2_DOWN(x - 1) + 1;
54}
55
56static inline uint32_t ROUNDDOWN(uint32_t a, uint32_t n) {
57 return a - (a % n);
58}
59
60// Round up to the nearest multiple of n
61static inline uint32_t ROUNDUP(uint32_t a, uint32_t n) {
62 return ROUNDDOWN(a + n - 1, n);
63}
64
65#define MAX2(x, y) ((x) > (y) ? (x) : (y))
66#define MAX4(a, b, c, d) (MAX2(MAX2((a), (b)), MAX2((c), (d))))
67
68/* Constants */
69static constexpr float PI = 3.14159f;
70
71/* Unit Conversions */
72#define DEG_TO_RAD (PI / 180.0f)
73#define G_TO_M_S (9.80665f)
74
75/* Per-Node HSI RCC Trim Constants */
76#define HSI_TRIM_TORQUE_VECTOR 15
77#define HSI_TRIM_MAIN_MODULE 15
78#define HSI_TRIM_PDU 16
79#define HSI_TRIM_DASHBOARD 15
80#define HSI_TRIM_DAQ 17
81#define HSI_TRIM_A_BOX 16
82
83#endif // COMMON_DEFS_H