1 /* 2 * Copyright (c) 2023 HPMicro 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 */ 7 #ifndef HPM_MCL_DRIVERS_H 8 #define HPM_MCL_DRIVERS_H 9 #include "hpm_mcl_common.h" 10 11 enum { 12 mcl_drivers_init_error = MAKE_STATUS(mcl_group_drivers, 0), 13 mcl_drivers_not_ready = MAKE_STATUS(mcl_group_drivers, 1), 14 mcl_drivers_update_error = MAKE_STATUS(mcl_group_drivers, 2), 15 }; 16 17 /** 18 * @brief Status of the drive function 19 * 20 */ 21 typedef enum { 22 drivers_status_null = 0, 23 drivers_status_init = 1, 24 drivers_status_run = 2, 25 drivers_status_fail = 3, 26 } mcl_drivers_status_t; 27 28 /** 29 * @brief Channels for drive functions 30 * 31 */ 32 typedef enum { 33 /** 34 * @brief Drive channels for three-phase motors. 35 * 36 */ 37 mcl_drivers_chn_a, 38 mcl_drivers_chn_b, 39 mcl_drivers_chn_c, 40 /** 41 * @brief Four channels of the stepper motor 42 * 43 */ 44 mcl_drivers_chn_a0, 45 mcl_drivers_chn_a1, 46 mcl_drivers_chn_b0, 47 mcl_drivers_chn_b1, 48 } mcl_drivers_channel_t; 49 50 /** 51 * @brief Callbacks for Driver Functions 52 * 53 */ 54 typedef struct { 55 void (*init)(void); 56 hpm_mcl_stat_t (*update_duty_cycle)(mcl_drivers_channel_t chn, float duty); 57 _FUNC_OPTIONAL_ hpm_mcl_stat_t (*update_frequency)(mcl_drivers_channel_t chn, uint32_t freqc); 58 _FUNC_OPTIONAL_ hpm_mcl_stat_t (*update_phase_offset)(mcl_drivers_channel_t chn, int32_t tick); 59 hpm_mcl_stat_t (*disable_all_drivers)(void); 60 hpm_mcl_stat_t (*enable_all_drivers)(void); 61 hpm_mcl_stat_t (*disable_drivers)(mcl_drivers_channel_t chn); 62 hpm_mcl_stat_t (*enable_drivers)(mcl_drivers_channel_t chn); 63 } mcl_drivers_callback_t; 64 65 /** 66 * @brief Driver Function Configuration 67 * 68 */ 69 typedef struct { 70 mcl_drivers_callback_t callback; 71 } mcl_drivers_cfg_t; 72 73 /** 74 * @brief Driver function running data 75 * 76 */ 77 typedef struct { 78 mcl_drivers_cfg_t *cfg; 79 mcl_drivers_status_t status; 80 } mcl_drivers_t; 81 82 #ifdef __cplusplus 83 extern "C" { 84 #endif 85 86 /** 87 * @brief Initialising data for driver functions 88 * 89 * @param drivers @ref mcl_drivers_t 90 * @param cfg @ref mcl_drivers_cfg_t 91 * @return hpm_mcl_stat_t 92 */ 93 hpm_mcl_stat_t hpm_mcl_drivers_init(mcl_drivers_t *drivers, mcl_drivers_cfg_t *cfg); 94 95 /** 96 * @brief Update the duty cycle of a brushless motor 97 * 98 * @param drivers @ref mcl_drivers_t 99 * @param duty_a a-phase duty cycle 100 * @param duty_b b-phase duty cycle 101 * @param duty_c c-phase duty cycle 102 * @return hpm_mcl_stat_t 103 */ 104 hpm_mcl_stat_t hpm_mcl_drivers_update_bldc_duty(mcl_drivers_t *drivers, float duty_a, float duty_b, float duty_c); 105 106 /** 107 * @brief Update the duty cycle of a step motor 108 * 109 * @param drivers @ref mcl_drivers_t 110 * @param duty_a0 a0-phase duty cycle 111 * @param duty_a1 a1-phase duty cycle 112 * @param duty_b0 b0-phase duty cycle 113 * @param duty_b1 b1-phase duty cycle 114 * @return hpm_mcl_stat_t 115 */ 116 hpm_mcl_stat_t hpm_mcl_drivers_update_step_duty(mcl_drivers_t *drivers, float duty_a0, float duty_a1, float duty_b0, float duty_b1); 117 118 /** 119 * @brief Switch off all drive outputs 120 * 121 * @param drivers @ref mcl_drivers_t 122 * @return hpm_mcl_stat_t 123 */ 124 hpm_mcl_stat_t hpm_mcl_disable_all_drivers(mcl_drivers_t *drivers); 125 126 /** 127 * @brief Enable all drive outputs 128 * 129 * @param drivers @ref mcl_drivers_t 130 * @return hpm_mcl_stat_t 131 */ 132 hpm_mcl_stat_t hpm_mcl_enable_all_drivers(mcl_drivers_t *drivers); 133 134 #ifdef __cplusplus 135 } 136 #endif 137 138 #endif 139