1 #ifndef _BFLB_PWM_V2_H 2 #define _BFLB_PWM_V2_H 3 4 #include "bflb_core.h" 5 6 /** @addtogroup LHAL 7 * @{ 8 */ 9 10 /** @addtogroup PWM_V2 11 * @{ 12 */ 13 14 /** @defgroup PWM_CHANNEL pwm channel definition 15 * @{ 16 */ 17 #define PWM_CH0 0 18 #define PWM_CH1 1 19 #define PWM_CH2 2 20 #define PWM_CH3 3 21 #define PWM_V2_CH_MAX 4 22 /** 23 * @} 24 */ 25 26 /** @defgroup PWM_POLARITY pwm polarity definition 27 * @{ 28 */ 29 #define PWM_POLARITY_ACTIVE_LOW 0 30 #define PWM_POLARITY_ACTIVE_HIGH 1 31 /** 32 * @} 33 */ 34 35 /** @defgroup PWM_STATE pwm state definition 36 * @{ 37 */ 38 #define PWM_STATE_INACTIVE 0 39 #define PWM_STATE_ACTIVE 1 40 /** 41 * @} 42 */ 43 44 /** @defgroup PWM_TRIG_SDC_SRC pwm trigger adc source definition 45 * @{ 46 */ 47 #define PWM_TRIG_ADC_SRC_CH0_L 0 48 #define PWM_TRIG_ADC_SRC_CH0_H 1 49 #define PWM_TRIG_ADC_SRC_CH1_L 2 50 #define PWM_TRIG_ADC_SRC_CH1_H 3 51 #define PWM_TRIG_ADC_SRC_CH2_L 4 52 #define PWM_TRIG_ADC_SRC_CH2_H 5 53 #define PWM_TRIG_ADC_SRC_CH3_L 6 54 #define PWM_TRIG_ADC_SRC_CH3_H 7 55 #define PWM_TRIG_ADC_SRC_PERIOD 8 56 #define PWM_TRIG_ADC_SRC_NONE 0xF 57 /** 58 * @} 59 */ 60 61 /** @defgroup PWM_INTEN pwm interrupt enable definition 62 * @{ 63 */ 64 #define PWM_INTEN_CH0_L (1 << 0) 65 #define PWM_INTEN_CH0_H (1 << 1) 66 #define PWM_INTEN_CH1_L (1 << 2) 67 #define PWM_INTEN_CH1_H (1 << 3) 68 #define PWM_INTEN_CH2_L (1 << 4) 69 #define PWM_INTEN_CH2_H (1 << 5) 70 #define PWM_INTEN_CH3_L (1 << 6) 71 #define PWM_INTEN_CH3_H (1 << 7) 72 #define PWM_INTEN_PERIOD (1 << 8) 73 #define PWM_INTEN_BRAKE (1 << 9) 74 #define PWM_INTEN_REPT (1 << 10) 75 /** 76 * @} 77 */ 78 79 /** @defgroup PWM_INTSTS pwm interrupt status definition 80 * @{ 81 */ 82 #define PWM_INTSTS_CH0_L (1 << 0) 83 #define PWM_INTSTS_CH0_H (1 << 1) 84 #define PWM_INTSTS_CH1_L (1 << 2) 85 #define PWM_INTSTS_CH1_H (1 << 3) 86 #define PWM_INTSTS_CH2_L (1 << 4) 87 #define PWM_INTSTS_CH2_H (1 << 5) 88 #define PWM_INTSTS_CH3_L (1 << 6) 89 #define PWM_INTSTS_CH3_H (1 << 7) 90 #define PWM_INTSTS_PERIOD (1 << 8) 91 #define PWM_INTSTS_BRAKE (1 << 9) 92 #define PWM_INTSTS_REPT (1 << 10) 93 /** 94 * @} 95 */ 96 97 /** @defgroup PWM_INTCLR pwm interrupt clear definition 98 * @{ 99 */ 100 #define PWM_INTCLR_CH0_L (1 << 0) 101 #define PWM_INTCLR_CH0_H (1 << 1) 102 #define PWM_INTCLR_CH1_L (1 << 2) 103 #define PWM_INTCLR_CH1_H (1 << 3) 104 #define PWM_INTCLR_CH2_L (1 << 4) 105 #define PWM_INTCLR_CH2_H (1 << 5) 106 #define PWM_INTCLR_CH3_L (1 << 6) 107 #define PWM_INTCLR_CH3_H (1 << 7) 108 #define PWM_INTCLR_PERIOD (1 << 8) 109 #define PWM_INTCLR_BRAKE (1 << 9) 110 #define PWM_INTCLR_REPT (1 << 10) 111 /** 112 * @} 113 */ 114 115 /** @defgroup PWM_CMD pwm feature control cmd definition 116 * @{ 117 */ 118 #define PWM_CMD_SET_TRIG_ADC_SRC (0x01) 119 #define PWM_CMD_SET_EXT_BRAKE_POLARITY (0x02) 120 #define PWM_CMD_SET_EXT_BRAKE_ENABLE (0x03) 121 #define PWM_CMD_SET_SW_BRAKE_ENABLE (0x04) 122 #define PWM_CMD_SET_STOP_ON_REPT (0x05) 123 #define PWM_CMD_SET_REPT_COUNT (0x06) 124 /** 125 * @} 126 */ 127 128 /** 129 * @brief PWM channel configuration structure 130 * 131 * @param positive_polarity PWM posotive channel polarity, use @ref PWM_POLARITY 132 * @param negative_polarity PWM negative channel polarity, use @ref PWM_POLARITY 133 * @param positive_stop_state PWM posotive channel state when stop, use @ref PWM_STATE 134 * @param negative_stop_state PWM negative channel state when stop, use @ref PWM_STATE 135 * @param positive_brake_state PWM posotive channel state when brake, use @ref PWM_STATE 136 * @param negative_brake_state PWM negative channel state when brake, use @ref PWM_STATE 137 * @param dead_time PWM channel dead time 138 */ 139 struct bflb_pwm_v2_channel_config_s { 140 uint8_t positive_polarity; 141 uint8_t negative_polarity; 142 uint8_t positive_stop_state; 143 uint8_t negative_stop_state; 144 uint8_t positive_brake_state; 145 uint8_t negative_brake_state; 146 uint8_t dead_time; 147 }; 148 149 /** 150 * @brief PWM configuration structure 151 * 152 * @param clk_source PWM clock source, use @ref BFLB_SYSTEM_CLOCK 153 * @param clk_div PWM clock dividor, should be in 1~65535 154 * @param period PWM period count, should be in 2~65535 155 */ 156 struct bflb_pwm_v2_config_s { 157 uint8_t clk_source; 158 uint16_t clk_div; 159 uint16_t period; 160 }; 161 162 #ifdef __cplusplus 163 extern "C" { 164 #endif 165 166 /** 167 * @brief Initialize pwm. 168 * 169 * @param [in] dev device handle 170 * @param [in] config pointer to save pwm config 171 */ 172 void bflb_pwm_v2_init(struct bflb_device_s *dev, const struct bflb_pwm_v2_config_s *config); 173 174 /** 175 * @brief Deinitialize pwm. 176 * 177 * @param [in] dev device handle 178 */ 179 void bflb_pwm_v2_deinit(struct bflb_device_s *dev); 180 181 /** 182 * @brief Start pwm output. 183 * 184 * @param [in] dev device handle 185 */ 186 void bflb_pwm_v2_start(struct bflb_device_s *dev); 187 188 /** 189 * @brief Stop pwm output. 190 * 191 * @param [in] dev device handle 192 */ 193 void bflb_pwm_v2_stop(struct bflb_device_s *dev); 194 195 /** 196 * @brief Set pwm period to change pwm frequence. Frequcence(hz) = pwm source clock /div/period. 197 * 198 * @param [in] dev device handle 199 * @param [in] period pwm period 200 */ 201 void bflb_pwm_v2_set_period(struct bflb_device_s *dev, uint16_t period); 202 203 /** 204 * @brief Get pwm frequcency. 205 * 206 * @param [in] dev device handle 207 * @return frequcency 208 */ 209 float bflb_pwm_v2_get_frequency(struct bflb_device_s *dev); 210 211 /** 212 * @brief Config pwm channel. 213 * 214 * @param [in] dev device handle 215 * @param [in] ch channel number 216 * @param [in] config pointer to save pwm channel config 217 */ 218 void bflb_pwm_v2_channel_init(struct bflb_device_s *dev, uint8_t ch, struct bflb_pwm_v2_channel_config_s *config); 219 220 /** 221 * @brief Set pwm dutycycle. Dutycycle(%) = (high_threhold - low_threhold)/period * 100%. 222 * 223 * @param [in] dev device handle 224 * @param [in] ch channel number 225 * @param [in] low_threhold pwm low threhold 226 * @param [in] high_threhold pwm high threhold 227 */ 228 void bflb_pwm_v2_channel_set_threshold(struct bflb_device_s *dev, uint8_t ch, uint16_t low_threhold, uint16_t high_threhold); 229 230 /** 231 * @brief Start pwm positive output. 232 * 233 * @param [in] dev device handle 234 * @param [in] ch channel number 235 */ 236 void bflb_pwm_v2_channel_positive_start(struct bflb_device_s *dev, uint8_t ch); 237 238 /** 239 * @brief Start pwm negative output. 240 * 241 * @param [in] dev device handle 242 * @param [in] ch channel number 243 */ 244 void bflb_pwm_v2_channel_negative_start(struct bflb_device_s *dev, uint8_t ch); 245 246 /** 247 * @brief Stop pwm positive output. 248 * 249 * @param [in] dev device handle 250 * @param [in] ch channel number 251 */ 252 void bflb_pwm_v2_channel_positive_stop(struct bflb_device_s *dev, uint8_t ch); 253 254 /** 255 * @brief Stop pwm negative output. 256 * 257 * @param [in] dev device handle 258 * @param [in] ch channel number 259 */ 260 void bflb_pwm_v2_channel_negative_stop(struct bflb_device_s *dev, uint8_t ch); 261 262 /** 263 * @brief Enable pwm interrupt. 264 * 265 * @param [in] dev device handle 266 * @param [in] int_en interrupt type 267 * @param [in] enable true means enable, otherwise disable 268 */ 269 void bflb_pwm_v2_int_enable(struct bflb_device_s *dev, uint32_t int_en, bool enable); 270 271 /** 272 * @brief Get pwm interrupt status. 273 * 274 * @param [in] dev device handle 275 * @return interrupt status, use @ref PWM_INTSTS 276 */ 277 uint32_t bflb_pwm_v2_get_intstatus(struct bflb_device_s *dev); 278 279 /** 280 * @brief Clear pwm interrupt status. 281 * 282 * @param [in] dev device handle 283 * @param [in] int_clear clear value, use @ref PWM_INTCLR 284 */ 285 void bflb_pwm_v2_int_clear(struct bflb_device_s *dev, uint32_t int_clear); 286 287 /** 288 * @brief Control pwm feature. 289 * 290 * @param [in] dev device handle 291 * @param [in] cmd feature command, use @ref PWM_CMD 292 * @param [in] arg user data 293 * @return A negated errno value on failure. 294 */ 295 int bflb_pwm_v2_feature_control(struct bflb_device_s *dev, int cmd, size_t arg); 296 297 #ifdef __cplusplus 298 } 299 #endif 300 301 /** 302 * @} 303 */ 304 305 /** 306 * @} 307 */ 308 309 #endif