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 <libxr_mem.hpp>
6#include <type_traits>
7
8#ifndef M_PI
9#define M_PI 3.14159265358979323846
10#endif
11
12#ifndef M_2PI
13#define M_2PI (2.0 * M_PI)
14#endif
15
16#ifndef M_1G
19constexpr double M_1G = 9.80665;
20#endif
21
22#ifndef DEF2STR
23#define XR_TO_STR(_arg) #_arg
24#define DEF2STR(_arg) XR_TO_STR(_arg)
25#endif
26
27#ifndef UNUSED
30#define UNUSED(_x) ((void)(_x))
31#endif
32
33#ifndef OFFSET_OF
36#define OFFSET_OF(type, member) ((size_t)&((type *)0)->member)
37#endif
38
39#ifndef MEMBER_SIZE_OF
42#define MEMBER_SIZE_OF(type, member) (sizeof(decltype(((type *)0)->member)))
43#endif
44
45#ifndef CONTAINER_OF
48#define CONTAINER_OF(ptr, type, member) \
49 ((type *)((char *)(ptr) - OFFSET_OF(type, member))) // NOLINT
50#endif
51
53static constexpr size_t LIBXR_CACHE_LINE_SIZE = (sizeof(void *) == 8) ? 64 : 32;
54static constexpr size_t LIBXR_ALIGN_SIZE = (sizeof(void *));
55
61enum class ErrorCode : int8_t
62{
63 OK = 0,
64 FAILED = -1,
65 INIT_ERR = -2,
66 ARG_ERR = -3,
67 STATE_ERR = -4,
68 SIZE_ERR = -5,
69 CHECK_ERR = -6,
70 NOT_SUPPORT = -7,
71 NOT_FOUND = -8,
72 NO_RESPONSE = -9,
73 NO_MEM = -10,
74 NO_BUFF = -11,
75 TIMEOUT = -12,
76 EMPTY = -13,
77 FULL = -14,
78 BUSY = -15,
79 PTR_NULL = -16,
80 OUT_OF_RANGE = -17
81};
82
88enum class SizeLimitMode : uint8_t
89{
90 EQUAL = 0,
91 LESS = 1,
92 MORE = 2,
93 NONE = 3
94};
95
96#ifdef ASSERT
97#undef ASSERT
98#endif
99
100#ifdef LIBXR_DEBUG_BUILD
106#define ASSERT(arg) \
107 do \
108 { \
109 if (!(arg)) \
110 { \
111 libxr_fatal_error(__FILE__, __LINE__, false); \
112 } \
113 } while (0)
114
120#define ASSERT_ISR(arg) \
121 do \
122 { \
123 if (!(arg)) \
124 { \
125 libxr_fatal_error(__FILE__, __LINE__, true); \
126 } \
127 } while (0)
128#else
129#define ASSERT(arg) (void(arg), (void)0)
130#define ASSERT_ISR(arg) (void(arg), (void)0)
131#endif
132
140extern void libxr_fatal_error(const char *file, uint32_t line, bool in_isr);
141
142namespace LibXR
143{
144using ErrorCode = ErrorCode;
145using SizeLimitMode = SizeLimitMode;
146
156template <typename T1, typename T2>
157constexpr auto max(T1 a, T2 b) -> typename std::common_type<T1, T2>::type
158{
159 return (a > b) ? a : b;
160}
161
171template <typename T1, typename T2>
172constexpr auto min(T1 a, T2 b) -> typename std::common_type<T1, T2>::type
173{
174 return (a < b) ? a : b;
175}
176} // 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
计算两个数的最小值