From 5764d87a0814a78a6afbfbcba5a32fe7fd91e04b Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sat, 5 Jun 2021 15:49:29 +0200 Subject: [PATCH] Added the charge pump driver --- src/charge_pump.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++ src/charge_pump.h | 9 +++++++ src/main.c | 14 +++++++++- src/pinout.h | 8 +++++- 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 src/charge_pump.c create mode 100644 src/charge_pump.h diff --git a/src/charge_pump.c b/src/charge_pump.c new file mode 100644 index 0000000..825c5d3 --- /dev/null +++ b/src/charge_pump.c @@ -0,0 +1,66 @@ +#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, 10000 - 1); // overflow every 10000 cycles = 100 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); +} diff --git a/src/charge_pump.h b/src/charge_pump.h new file mode 100644 index 0000000..36096c9 --- /dev/null +++ b/src/charge_pump.h @@ -0,0 +1,9 @@ +#ifndef CHARGE_PUMP_H +#define CHARGE_PUMP_H + +void charge_pump_init(void); + +void charge_pump_start(void); +void charge_pump_stop(void); + +#endif // CHARGE_PUMP_H diff --git a/src/main.c b/src/main.c index 0ead9c3..e5e29bf 100644 --- a/src/main.c +++ b/src/main.c @@ -15,6 +15,7 @@ #include #include "led_chplex.h" +#include "charge_pump.h" volatile int wait_frame = 1; @@ -34,7 +35,13 @@ static void init_clock(void) rcc_clock_setup_in_hse_8mhz_out_48mhz(); // generate 48 MHz from external 8 MHz crystal //rcc_clock_setup_in_hsi_out_48mhz(); // generate ~48 MHz from internal RC oscillator + // enable TIM1 for PWM generation + rcc_periph_clock_enable(RCC_TIM1); + // enable GPIO clocks: + // Port A is needed for the charge pump, extension port and analog input + rcc_periph_clock_enable(RCC_GPIOA); + // Port B is needed for the LEDs rcc_periph_clock_enable(RCC_GPIOB); } @@ -93,10 +100,13 @@ int main(void) //uint32_t cpuload = 0; uint64_t timebase_ms = 0; bool ledtest_done = false; + bool startup_done = false; init_clock(); init_gpio(); + charge_pump_init(); + led_chplex_init(); led_chplex_on(LED_CHPLEX_IDX_SOLAR_ON); @@ -107,11 +117,13 @@ int main(void) if(!ledtest_done) { ledtest_done = ledtest(timebase_ms); + } else if(!startup_done) { + charge_pump_start(); + startup_done = true; } else if(timebase_ms % 500 == 0) { led_chplex_toggle(LED_CHPLEX_IDX_DISCHARGE_PULSE); } - led_chplex_periodic(); timebase_ms++; diff --git a/src/pinout.h b/src/pinout.h index 202729b..5dfd3ce 100644 --- a/src/pinout.h +++ b/src/pinout.h @@ -3,7 +3,7 @@ /* LEDs */ -#define LED_PORT GPIOB +#define LED_PORT GPIOB #define LED_A_PIN GPIO0 #define LED_B_PIN GPIO1 @@ -11,4 +11,10 @@ #define LED_ALL_PINS (LED_A_PIN | LED_B_PIN | LED_C_PIN) +/* Charge pump */ + +#define CHARGE_PUMP_PORT GPIOA + +#define CHARGE_PUMP_DRV_PIN GPIO8 // Timer 1, Channel 1 + #endif // PINOUT_H