HPM SDK
HPMicro Software Development Kit
hpm_mcl_common.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023-2025 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 {
43 };
44 
45 
50 void mcl_user_delay_us(uint64_t tick);
51 
52 #define MCL_DEBUG printf
53 #define MCL_PI HPM_PI_FLOAT
54 #define MCL_2PI HPM_2_PI_FLOAT
55 #define MCL_PI_DIV3 (MCL_PI / 3.0f)
56 
57 #define MCL_DELAY_US(x) mcl_user_delay_us(x)
58 #define MCL_DELAY_MS(x) MCL_DELAY_US(1000*x)
59 #define MCL_EMPTY
60 
61 #define MCL_ASSERT_BOOL(b, code_extend, errcode) \
62  do { \
63  if (!b) { \
64  code_extend; \
65  MCL_DEBUG("errcode:%d, file:%s, line:%d.\r\n", errcode, __FILE__, __LINE__); \
66  return errcode; \
67  } \
68  } while (0)
69 
70 #define MCL_ASSERT_EXEC_CODE_BOOL(b, code_extend) \
71  do { \
72  if (!b) { \
73  code_extend; \
74  } \
75  } while (0)
76 
77 #define MCL_ASSERT(x, return_errcode) MCL_ASSERT_BOOL(((uint32_t)(x) != 0), MCL_EMPTY, return_errcode)
78 
79 #define MCL_ASSERT_EXEC_CODE(x, code) MCL_ASSERT_EXEC_CODE_BOOL((x), code)
80 
81 #define MCL_ASSERT_EXEC_CODE_AND_RETURN(x, code, return_errcode) MCL_ASSERT_BOOL((x), code, return_errcode)
82 
83 #ifdef NDEBUG
84  #define MCL_ASSERT_EXEC_CODE_OPT(x, code) ((void)0)
85  #define MCL_ASSERT_OPT(x, return_errcode) ((void)0)
86  #define MCL_ASSERT_EXEC_CODE_AND_RETURN_OPT(x, code, return_errcode) ((void)0)
87 #else
88  #define MCL_ASSERT_EXEC_CODE_OPT MCL_ASSERT_EXEC_CODE
89  #define MCL_ASSERT_OPT MCL_ASSERT
90  #define MCL_ASSERT_EXEC_CODE_AND_RETURN_OPT MCL_ASSERT_EXEC_CODE_AND_RETURN
91 #endif
92 
93 #define MCL_FUNCTION_EXC_IF_ENABLE(b, str_f, f) \
94  do { \
95  if (b) { \
96  str_f = f; \
97  } \
98  } while (0)
99 
100 #define MCL_FUNCTION_EXC_IF_ELSE_ENABLE(b, str_f, _if, _else) \
101  do { \
102  if (b) { \
103  str_f = _if; \
104  } else { \
105  str_f = _else; \
106  } \
107  } while (0)
108 
109 #define MCL_FUNCTION_INIT_IF_EMPTY(str_function, function) MCL_FUNCTION_EXC_IF_ENABLE((str_function == NULL), str_function, function)
110 
111 #define MCL_FUNCTION_INIT_IF_NO_EMPTY(function, str_function) MCL_FUNCTION_EXC_IF_ENABLE((str_function != NULL), function, str_function)
112 
113 #define MCL_STATUS_SET_IF_TRUE(real, str, status) MCL_FUNCTION_EXC_IF_ENABLE((real), str, status)
114 
115 #define MCL_VALUE_SET_IF_TRUE(real, str, value) MCL_FUNCTION_EXC_IF_ENABLE((real), str, value)
116 
117 #define MCL_FUNCTION_SET_IF_ELSE_TRUE(real, str, _if, _else) MCL_FUNCTION_EXC_IF_ELSE_ENABLE((real), str, _if, _else)
118 
123 #define _FUNC_OPTIONAL_
124 
129 #define MCL_ANGLE_MOD_X(down, up, val) \
130 ({ \
131  float val_; \
132  if ((val) > up) { \
133  val_ = (val); \
134  do { \
135  val_ = (val_) - (up - down); \
136  } while ((val_) > up); \
137  } else if ((val) < down) { \
138  val_ = (val); \
139  do { \
140  val_ = (val_) + (up - down); \
141  } while ((val_) < down); \
142  } else { \
143  val_ = (val); \
144  } \
145  (val_); \
146 })
147 
159 #define MCL_GET_ANGLE_DELTA(val, offset) \
160 ({ \
161  float val_; \
162  float temp; \
163  val_ = 0; \
164  temp = 0; \
165  if ((val) > 0) { \
166  temp = (val) - offset; \
167  } else if ((val) < 0) { \
168  temp = (val) + offset; \
169  } else { \
170  val_ = 0; \
171  } \
172  if (fabs(val) < fabs(temp)) { \
173  val_ = val; \
174  } else {\
175  val_ = temp; \
176  } \
177  (val_); \
178 })
179 
184 #define MCL_VALUE_LIMIT(val, min, max) \
185  do { \
186  if ((val) > (max)) { \
187  val = max; \
188  } else if ((val) < (min)) { \
189  val = min; \
190  } \
191  } while (0)
192 
197 #define MCL_GET_ADC_12BIT_VALID_DATA(x) ((x & 0xffff) >> 4)
198 
199 
205 #define MCL_USEC_TO_HZ(usec) (1000000.0f / (usec))
206 
211 #define MCL_PERIOD_TO_FREQUENCY(period) (1.0f / (period))
212 
213 
218 #define MCL_FREQUENCY_TO_PERIOD(frequency) (1.0f / (frequency))
219 
228 #define MCL_USEC_TO_SEC(usec) ((usec) / 1000000.0f)
229 
237 #define MCL_USEC_TO_MSEC(usec) ((usec) / 1000.0f)
238 
239 typedef struct {
242 } mcl_cfg_t;
243 
244 
245 #endif
#define MAKE_STATUS(group, code)
Definition: hpm_common.h:144
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_not_supported
Definition: hpm_mcl_common.h:42
@ 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
@ 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
Definition: hpm_mcl_common.h:239
mcl_physical_para_q_t physical_q
Definition: hpm_mcl_common.h:241
mcl_physical_para_t physical
Definition: hpm_mcl_common.h:240
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