#include #include #include "libopencm3/stm32/common/timer_common_all.h" #include "pinout.h" #include "charge_pump.h" void charge_pump_init(void) { // set alternate function to Timer 1, channel 1 output gpio_set_af(CHARGE_PUMP_PORT, GPIO_AF2, CHARGE_PUMP_DRV_PIN); // set the GPIO to low until the charge pump is started gpio_clear(CHARGE_PUMP_PORT, CHARGE_PUMP_DRV_PIN); gpio_mode_setup(CHARGE_PUMP_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, CHARGE_PUMP_DRV_PIN); // *** TIM1 *** // Configure channel 1 for PWM // Ch1 = charge pump output timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set up PWM channel timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM1); timer_enable_oc_output(TIM1, TIM_OC1); timer_enable_oc_preload(TIM1, TIM_OC1); timer_set_oc_polarity_high(TIM1, TIM_OC1); // prescaler timer_set_prescaler(TIM1, 48 - 1); // Timer runs at 1 MHz // auto-reload value timer_set_period(TIM1, 2000 - 1); // overflow every 2000 cycles = 500 Hz // output compare value timer_set_oc_value(TIM1, TIM_OC1, 100); // High for 100 cycles = 100 μs // only generate interrupt on overflow timer_update_on_overflow(TIM1); // enable main output timer_enable_break_main_output(TIM1); } void charge_pump_start(void) { // start the timer timer_enable_counter(TIM1); // set mode to AF to pass through the PWM signal from the timer gpio_mode_setup(CHARGE_PUMP_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, CHARGE_PUMP_DRV_PIN); } void charge_pump_stop(void) { // stop the timer timer_disable_counter(TIM1); // set the output static low gpio_clear(CHARGE_PUMP_PORT, CHARGE_PUMP_DRV_PIN); gpio_mode_setup(CHARGE_PUMP_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, CHARGE_PUMP_DRV_PIN); }