libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
libxr_def.hpp
1#pragma once
2
3#include <cmath>
4#include <cstdint>
5#include <type_traits>
6
7#ifndef M_PI
8#define M_PI 3.14159265358979323846
9#endif
10
11#ifndef M_2PI
12#define M_2PI (2.0 * M_PI)
13#endif
14
15#ifndef M_1G
18constexpr double M_1G = 9.80665;
19#endif
20
21#ifndef DEF2STR
22#define XR_TO_STR(_arg) #_arg
23#define DEF2STR(_arg) XR_TO_STR(_arg)
24#endif
25
26#ifndef UNUSED
29#define UNUSED(_x) ((void)(_x))
30#endif
31
32#ifndef OFFSET_OF
35#define OFFSET_OF(type, member) ((size_t)&((type *)0)->member)
36#endif
37
38#ifndef MEMBER_SIZE_OF
41#define MEMBER_SIZE_OF(type, member) (sizeof(decltype(((type *)0)->member)))
42#endif
43
44#ifndef CONTAINER_OF
47#define CONTAINER_OF(ptr, type, member) \
48 ((type *)((char *)(ptr) - OFFSET_OF(type, member))) // NOLINT
49#endif
50
52static constexpr size_t LIBXR_CACHE_LINE_SIZE = (sizeof(void *) == 8) ? 64 : 32;
53
59enum class ErrorCode : int8_t
60{
61 OK = 0,
62 FAILED = -1,
63 INIT_ERR = -2,
64 ARG_ERR = -3,
65 STATE_ERR = -4,
66 SIZE_ERR = -5,
67 CHECK_ERR = -6,
68 NOT_SUPPORT = -7,
69 NOT_FOUND = -8,
70 NO_RESPONSE = -9,
71 NO_MEM = -10,
72 NO_BUFF = -11,
73 TIMEOUT = -12,
74 EMPTY = -13,
75 FULL = -14,
76 BUSY = -15,
77 PTR_NULL = -16,
78 OUT_OF_RANGE = -17
79};
80
86enum class SizeLimitMode : uint8_t
87{
88 EQUAL = 0,
89 LESS = 1,
90 MORE = 2,
91 NONE = 3
92};
93
94#ifdef ASSERT
95#undef ASSERT
96#endif
97
98#ifdef LIBXR_DEBUG_BUILD
104#define ASSERT(arg) \
105 do \
106 { \
107 if (!(arg)) \
108 { \
109 libxr_fatal_error(__FILE__, __LINE__, false); \
110 } \
111 } while (0)
112
118#define ASSERT_ISR(arg) \
119 do \
120 { \
121 if (!(arg)) \
122 { \
123 libxr_fatal_error(__FILE__, __LINE__, true); \
124 } \
125 } while (0)
126#else
127#define ASSERT(arg) ((void)0)
128#define ASSERT_ISR(arg) ((void)0)
129#endif
130
138extern void libxr_fatal_error(const char *file, uint32_t line, bool in_isr);
139
140namespace LibXR
141{
142using ErrorCode = ErrorCode;
143using SizeLimitMode = SizeLimitMode;
144
154template <typename T1, typename T2>
155constexpr auto max(T1 a, T2 b) -> typename std::common_type<T1, T2>::type
156{
157 return (a > b) ? a : b;
158}
159
169template <typename T1, typename T2>
170constexpr auto min(T1 a, T2 b) -> typename std::common_type<T1, T2>::type
171{
172 return (a < b) ? a : b;
173}
174} // namespace LibXR
LibXR 命名空间
Definition ch32_gpio.hpp:9
constexpr auto max(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最大值
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值