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 14 typedef uint32_t hpm_mcl_stat_t; 15 16 enum { 17 mcl_group_common = 0, 18 mcl_group_motor = 1, 19 mcl_group_encoder = 2, 20 mcl_group_analog = 3, 21 mcl_group_drivers = 4, 22 }; 23 24 /** 25 * @brief User-defined data with data and enable bits 26 * 27 */ 28 typedef struct { 29 float value; 30 bool enable; 31 } mcl_user_value_t; 32 33 enum { 34 mcl_success = MAKE_STATUS(status_group_common, 0), 35 mcl_fail = MAKE_STATUS(status_group_common, 1), 36 mcl_invalid_argument = MAKE_STATUS(status_group_common, 2), 37 mcl_invalid_pointer = MAKE_STATUS(status_group_common, 3), 38 mcl_timeout = MAKE_STATUS(status_group_common, 4), 39 mcl_in_development = MAKE_STATUS(status_group_common, 5), /**< Functions under development */ 40 }; 41 42 43 /** 44 * @brief user define code 45 * 46 */ 47 void mcl_user_delay_us(uint64_t tick); 48 49 #define MCL_PI (3.1415926535f) 50 #define MCL_2PI (2.0f * MCL_PI) 51 52 #define MCL_DELAY_US(x) mcl_user_delay_us(x) 53 #define MCL_DELAY_MS(x) MCL_DELAY_US(1000*x) 54 #define MCL_EMPTY 55 56 #define MCL_ASSERT_BOOL(b, code_extend, errcode) \ 57 do { \ 58 if (!b) { \ 59 code_extend; \ 60 return errcode; \ 61 } \ 62 } while (0) 63 64 #define MCL_ASSERT_EXEC_CODE_BOOL(b, code_extend) \ 65 do { \ 66 if (!b) { \ 67 code_extend; \ 68 } \ 69 } while (0) 70 71 #define MCL_ASSERT(x, return_errcode) MCL_ASSERT_BOOL(((uint32_t)(x) != 0), MCL_EMPTY, return_errcode) 72 73 #define MCL_ASSERT_EXEC_CODE(x, code) MCL_ASSERT_EXEC_CODE_BOOL((x), code) 74 75 #define MCL_ASSERT_EXEC_CODE_AND_RETURN(x, code, return_errcode) MCL_ASSERT_BOOL((x), code, return_errcode) 76 77 #ifdef NDEBUG 78 #define MCL_ASSERT_EXEC_CODE_OPT(x, code) ((void)0) 79 #define MCL_ASSERT_OPT(x, return_errcode) ((void)0) 80 #define MCL_ASSERT_EXEC_CODE_AND_RETURN_OPT(x, code, return_errcode) ((void)0) 81 #else 82 #define MCL_ASSERT_EXEC_CODE_OPT MCL_ASSERT_EXEC_CODE 83 #define MCL_ASSERT_OPT MCL_ASSERT 84 #define MCL_ASSERT_EXEC_CODE_AND_RETURN_OPT MCL_ASSERT_EXEC_CODE_AND_RETURN 85 #endif 86 87 #define MCL_FUNCTION_EXC_IF_ENABLE(b, str_f, f) \ 88 do { \ 89 if (b) { \ 90 str_f = f; \ 91 } \ 92 } while (0) 93 94 #define MCL_FUNCTION_EXC_IF_ELSE_ENABLE(b, str_f, _if, _else) \ 95 do { \ 96 if (b) { \ 97 str_f = _if; \ 98 } else { \ 99 str_f = _else; \ 100 } \ 101 } while (0) 102 103 #define MCL_FUNCTION_INIT_IF_EMPTY(str_function, function) MCL_FUNCTION_EXC_IF_ENABLE((str_function == NULL), str_function, function) 104 105 #define MCL_FUNCTION_INIT_IF_NO_EMPTY(function, str_function) MCL_FUNCTION_EXC_IF_ENABLE((str_function != NULL), function, str_function) 106 107 #define MCL_STATUS_SET_IF_TRUE(real, str, status) MCL_FUNCTION_EXC_IF_ENABLE((real), str, status) 108 109 #define MCL_VALUE_SET_IF_TRUE(real, str, value) MCL_FUNCTION_EXC_IF_ENABLE((real), str, value) 110 111 #define MCL_FUNCTION_SET_IF_ELSE_TRUE(real, str, _if, _else) MCL_FUNCTION_EXC_IF_ELSE_ENABLE((real), str, _if, _else) 112 113 /** 114 * @brief If used, this means that the callback function is optional 115 * 116 */ 117 #define _FUNC_OPTIONAL_ 118 119 /** 120 * @brief Remainder of angle 121 * 122 */ 123 #define MCL_ANGLE_MOD_X(down, up, val) \ 124 ({ \ 125 float val_; \ 126 if ((val) > up) { \ 127 val_ = (val) - (up - down); \ 128 } else if ((val) < down) { \ 129 val_ = (val) + (up - down); \ 130 } else { \ 131 val_ = (val); \ 132 } \ 133 (val_); \ 134 }) 135 136 /** 137 * @brief Determine if a floating point number is 0 138 * 139 */ 140 #define MCL_FLOAT_IS_ZERO(val) ((val < 0.000001) && (val > -0.000001)) 141 142 143 /** 144 * @brief Data Range Limits 145 * 146 */ 147 #define MCL_VALUE_LIMIT(val, min, max) \ 148 do { \ 149 if ((val) > (max)) { \ 150 val = max; \ 151 } else if ((val) < (min)) { \ 152 val = min; \ 153 } \ 154 } while (0) 155 156 /** 157 * @brief Get ADC data with 12bit valid bits 158 * 159 */ 160 #define MCL_GET_ADC_12BIT_VALID_DATA(x) ((x & 0xffff) >> 4) 161 162 typedef struct { 163 mcl_physical_para_t physical; 164 mcl_physical_para_q_t physical_q; 165 } mcl_cfg_t; 166 167 168 #endif 169