HPM SDK
HPMicro Software Development Kit
hpm_mcl_common.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 #ifndef HPM_MCL_COMMON_H
8 #define HPM_MCL_COMMON_H
9 #include "hpm_common.h"
10 #include "hpm_mcl_cfg.h"
11 #include "hpm_mcl_physical.h"
12 #include "hpm_mcl_math.h"
13 #include "stdio.h"
14 
15 typedef uint32_t hpm_mcl_stat_t;
16 
17 enum {
23 };
24 
29 typedef struct {
30  float value;
31  bool enable;
33 
34 enum {
42 };
43 
44 
49 void mcl_user_delay_us(uint64_t tick);
50 
51 #define MCL_DEBUG printf
52 #define MCL_PI HPM_PI
53 #define MCL_2PI HPM_2_PI
54 #define MCL_PI_DIV3 (MCL_PI / 3.0f)
55 
56 #define MCL_DELAY_US(x) mcl_user_delay_us(x)
57 #define MCL_DELAY_MS(x) MCL_DELAY_US(1000*x)
58 #define MCL_EMPTY
59 
60 #define MCL_ASSERT_BOOL(b, code_extend, errcode) \
61  do { \
62  if (!b) { \
63  code_extend; \
64  MCL_DEBUG("errcode:%d, file:%s, line:%d.\r\n", errcode, __FILE__, __LINE__); \
65  return errcode; \
66  } \
67  } while (0)
68 
69 #define MCL_ASSERT_EXEC_CODE_BOOL(b, code_extend) \
70  do { \
71  if (!b) { \
72  code_extend; \
73  } \
74  } while (0)
75 
76 #define MCL_ASSERT(x, return_errcode) MCL_ASSERT_BOOL(((uint32_t)(x) != 0), MCL_EMPTY, return_errcode)
77 
78 #define MCL_ASSERT_EXEC_CODE(x, code) MCL_ASSERT_EXEC_CODE_BOOL((x), code)
79 
80 #define MCL_ASSERT_EXEC_CODE_AND_RETURN(x, code, return_errcode) MCL_ASSERT_BOOL((x), code, return_errcode)
81 
82 #ifdef NDEBUG
83  #define MCL_ASSERT_EXEC_CODE_OPT(x, code) ((void)0)
84  #define MCL_ASSERT_OPT(x, return_errcode) ((void)0)
85  #define MCL_ASSERT_EXEC_CODE_AND_RETURN_OPT(x, code, return_errcode) ((void)0)
86 #else
87  #define MCL_ASSERT_EXEC_CODE_OPT MCL_ASSERT_EXEC_CODE
88  #define MCL_ASSERT_OPT MCL_ASSERT
89  #define MCL_ASSERT_EXEC_CODE_AND_RETURN_OPT MCL_ASSERT_EXEC_CODE_AND_RETURN
90 #endif
91 
92 #define MCL_FUNCTION_EXC_IF_ENABLE(b, str_f, f) \
93  do { \
94  if (b) { \
95  str_f = f; \
96  } \
97  } while (0)
98 
99 #define MCL_FUNCTION_EXC_IF_ELSE_ENABLE(b, str_f, _if, _else) \
100  do { \
101  if (b) { \
102  str_f = _if; \
103  } else { \
104  str_f = _else; \
105  } \
106  } while (0)
107 
108 #define MCL_FUNCTION_INIT_IF_EMPTY(str_function, function) MCL_FUNCTION_EXC_IF_ENABLE((str_function == NULL), str_function, function)
109 
110 #define MCL_FUNCTION_INIT_IF_NO_EMPTY(function, str_function) MCL_FUNCTION_EXC_IF_ENABLE((str_function != NULL), function, str_function)
111 
112 #define MCL_STATUS_SET_IF_TRUE(real, str, status) MCL_FUNCTION_EXC_IF_ENABLE((real), str, status)
113 
114 #define MCL_VALUE_SET_IF_TRUE(real, str, value) MCL_FUNCTION_EXC_IF_ENABLE((real), str, value)
115 
116 #define MCL_FUNCTION_SET_IF_ELSE_TRUE(real, str, _if, _else) MCL_FUNCTION_EXC_IF_ELSE_ENABLE((real), str, _if, _else)
117 
122 #define _FUNC_OPTIONAL_
123 
128 #define MCL_ANGLE_MOD_X(down, up, val) \
129 ({ \
130  float val_; \
131  if ((val) > up) { \
132  val_ = (val); \
133  do { \
134  val_ = (val_) - (up - down); \
135  } while ((val_) > up); \
136  } else if ((val) < down) { \
137  val_ = (val); \
138  do { \
139  val_ = (val_) + (up - down); \
140  } while ((val_) < down); \
141  } else { \
142  val_ = (val); \
143  } \
144  (val_); \
145 })
146 
158 #define MCL_GET_ANGLE_DELTA(val, offset) \
159 ({ \
160  float val_; \
161  float temp; \
162  val_ = 0; \
163  temp = 0; \
164  if ((val) > 0) { \
165  temp = (val) - offset; \
166  } else if ((val) < 0) { \
167  temp = (val) + offset; \
168  } else { \
169  val_ = 0; \
170  } \
171  if (fabs(val) < fabs(temp)) { \
172  val_ = val; \
173  } else {\
174  val_ = temp; \
175  } \
176  (val_); \
177 })
178 
183 #define MCL_VALUE_LIMIT(val, min, max) \
184  do { \
185  if ((val) > (max)) { \
186  val = max; \
187  } else if ((val) < (min)) { \
188  val = min; \
189  } \
190  } while (0)
191 
196 #define MCL_GET_ADC_12BIT_VALID_DATA(x) ((x & 0xffff) >> 4)
197 
198 typedef struct {
201 } mcl_cfg_t;
202 
203 
204 #endif
#define MAKE_STATUS(group, code)
Definition: hpm_common.h:132
@ mcl_group_common
Definition: hpm_mcl_common.h:18
@ mcl_group_analog
Definition: hpm_mcl_common.h:21
@ mcl_group_motor
Definition: hpm_mcl_common.h:19
@ mcl_group_encoder
Definition: hpm_mcl_common.h:20
@ mcl_group_drivers
Definition: hpm_mcl_common.h:22
void mcl_user_delay_us(uint64_t tick)
user define code
uint32_t hpm_mcl_stat_t
Definition: hpm_mcl_common.h:15
@ mcl_timeout
Definition: hpm_mcl_common.h:39
@ mcl_in_development
Definition: hpm_mcl_common.h:40
@ mcl_fail
Definition: hpm_mcl_common.h:36
@ mcl_invalid_argument
Definition: hpm_mcl_common.h:37
@ mcl_invalid_pointer
Definition: hpm_mcl_common.h:38
@ mcl_success
Definition: hpm_mcl_common.h:35
@ mcl_running
Definition: hpm_mcl_common.h:41
Definition: hpm_mcl_common.h:198
mcl_physical_para_q_t physical_q
Definition: hpm_mcl_common.h:200
mcl_physical_para_t physical
Definition: hpm_mcl_common.h:199
Definition: hpm_mcl_physical.h:115
Definition: hpm_mcl_physical.h:109
User-defined data with data and enable bits.
Definition: hpm_mcl_common.h:29
float value
Definition: hpm_mcl_common.h:30
bool enable
Definition: hpm_mcl_common.h:31