Added the charge pump driver

This commit is contained in:
Thomas Kolb 2021-06-05 15:49:29 +02:00
parent 8c317f2904
commit 5764d87a08
4 changed files with 95 additions and 2 deletions

66
src/charge_pump.c Normal file
View file

@ -0,0 +1,66 @@
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/timer.h>
#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);
}

9
src/charge_pump.h Normal file
View file

@ -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

View file

@ -15,6 +15,7 @@
#include <fxp_basic.h> #include <fxp_basic.h>
#include "led_chplex.h" #include "led_chplex.h"
#include "charge_pump.h"
volatile int wait_frame = 1; 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_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 //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: // 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 // Port B is needed for the LEDs
rcc_periph_clock_enable(RCC_GPIOB); rcc_periph_clock_enable(RCC_GPIOB);
} }
@ -93,10 +100,13 @@ int main(void)
//uint32_t cpuload = 0; //uint32_t cpuload = 0;
uint64_t timebase_ms = 0; uint64_t timebase_ms = 0;
bool ledtest_done = false; bool ledtest_done = false;
bool startup_done = false;
init_clock(); init_clock();
init_gpio(); init_gpio();
charge_pump_init();
led_chplex_init(); led_chplex_init();
led_chplex_on(LED_CHPLEX_IDX_SOLAR_ON); led_chplex_on(LED_CHPLEX_IDX_SOLAR_ON);
@ -107,11 +117,13 @@ int main(void)
if(!ledtest_done) { if(!ledtest_done) {
ledtest_done = ledtest(timebase_ms); ledtest_done = ledtest(timebase_ms);
} else if(!startup_done) {
charge_pump_start();
startup_done = true;
} else if(timebase_ms % 500 == 0) { } else if(timebase_ms % 500 == 0) {
led_chplex_toggle(LED_CHPLEX_IDX_DISCHARGE_PULSE); led_chplex_toggle(LED_CHPLEX_IDX_DISCHARGE_PULSE);
} }
led_chplex_periodic(); led_chplex_periodic();
timebase_ms++; timebase_ms++;

View file

@ -11,4 +11,10 @@
#define LED_ALL_PINS (LED_A_PIN | LED_B_PIN | LED_C_PIN) #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 #endif // PINOUT_H