1 /* 2 * Copyright (c) 2023 HPMicro 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 */ 7 #ifndef HPM_MCL_ANALOG_H 8 #define HPM_MCL_ANALOG_H 9 10 #include "hpm_mcl_common.h" 11 #include "hpm_mcl_filter.h" 12 13 enum { 14 mcl_analog_init_error = MAKE_STATUS(mcl_group_analog, 0), 15 mcl_analog_not_ready = MAKE_STATUS(mcl_group_analog, 1), 16 mcl_analog_update_location_error = MAKE_STATUS(mcl_group_analog, 2), 17 mcl_analog_get_value_error = MAKE_STATUS(mcl_group_analog, 3), 18 }; 19 20 typedef enum { 21 analog_status_null = 0, 22 analog_status_init = 1, 23 analog_status_run = 2, 24 analog_status_fail = 3, 25 } mcl_analog_status_t; 26 27 /** 28 * @brief Analog function channel numbers 29 * 30 */ 31 typedef enum { 32 analog_a_current = 0, 33 analog_b_current = 1, 34 analog_c_current = 2, 35 analog_a_voltage = 3, 36 analog_b_voltage = 4, 37 analog_c_voltage = 5, 38 analog_vbus = 6, 39 } mcl_analog_chn_t; 40 41 /** 42 * @brief callback function 43 * 44 */ 45 typedef struct { 46 hpm_mcl_stat_t (*init)(void); 47 hpm_mcl_stat_t (*update_sample_location)(mcl_analog_chn_t chn, uint32_t tick); 48 _FUNC_OPTIONAL_ hpm_mcl_stat_t (*process_by_user)(mcl_analog_chn_t chn, int32_t adc_value, float *output); 49 hpm_mcl_stat_t (*get_value)(mcl_analog_chn_t chn, int32_t *value); 50 } mcl_analog_callback_t; 51 52 53 /** 54 * @brief Configuration of analog functions 55 * 56 */ 57 typedef struct { 58 mcl_analog_callback_t callback; 59 bool enable_a_current; 60 bool enable_b_current; 61 bool enable_c_current; 62 bool enable_a_voltage; 63 bool enable_b_voltage; 64 bool enable_c_voltage; 65 bool enable_vbus; 66 bool enable_process_by_user; 67 bool enable_filter[MCL_ANALOG_CHN_NUM]; 68 } mcl_analog_cfg_t; 69 70 /** 71 * @brief Operational data of the analog function 72 * 73 */ 74 typedef struct { 75 mcl_analog_cfg_t *cfg; 76 physical_board_analog_t *board_parameter; 77 mcl_filter_iir_df1_t *iir[MCL_ANALOG_CHN_NUM]; 78 int32_t *num_current_sample_res; 79 mcl_analog_status_t status; 80 } mcl_analog_t; 81 82 #ifdef __cplusplus 83 extern "C" { 84 #endif 85 86 /** 87 * @brief Initialize data structures for analog functions 88 * 89 * @param analog @ref mcl_analog_t 90 * @param cfg @ref mcl_analog_cfg_t 91 * @param mcl_cfg @ref mcl_cfg_t 92 * @return hpm_mcl_stat_t 93 */ 94 hpm_mcl_stat_t hpm_mcl_analog_init(mcl_analog_t *analog, mcl_analog_cfg_t *cfg, mcl_cfg_t *mcl_cfg); 95 96 /** 97 * @brief Get the value of the analog channel 98 * 99 * @param analog @ref mcl_analog_t 100 * @param chn @ref mcl_analog_chn_t 101 * @param value Processed Sample Values 102 * @return hpm_mcl_stat_t 103 */ 104 hpm_mcl_stat_t hpm_mcl_analog_get_value(mcl_analog_t *analog, mcl_analog_chn_t chn, float *value); 105 106 /** 107 * @brief Initializing the iir filter for analog functions 108 * 109 * @param analog @ref mcl_analog_t 110 * @param chn @ref mcl_analog_chn_t 111 * @param iir @ref mcl_filter_iir_df1_t 112 * @return hpm_mcl_stat_t 113 */ 114 hpm_mcl_stat_t hpm_mcl_analog_iir_filter_init(mcl_analog_t *analog, mcl_analog_chn_t chn, mcl_filter_iir_df1_t *iir); 115 116 /** 117 * @brief Turn off the iir filter for analog functions 118 * 119 * @param analog @ref mcl_analog_t 120 * @param chn @ref mcl_analog_chn_t 121 * @return hpm_mcl_stat_t 122 */ 123 hpm_mcl_stat_t hpm_mcl_analog_disable_iir_filter(mcl_analog_t *analog, mcl_analog_chn_t chn); 124 125 /** 126 * @brief Current conversion of stepper motors 127 * 128 * @param analog @ref mcl_analog_t 129 * @param value Raw data acquired by the analog function 130 * @param chn @ref mcl_analog_chn_t 131 * @param angle electrical angle 132 * @param output Transformed data 133 * @return hpm_mcl_stat_t 134 */ 135 hpm_mcl_stat_t hpm_mcl_analog_step_convert(mcl_analog_t *analog, float value, mcl_analog_chn_t chn, float angle, float *output); 136 137 #ifdef __cplusplus 138 } 139 #endif 140 141 #endif 142