Added power switch driver

This commit is contained in:
Thomas Kolb 2021-06-05 16:33:21 +02:00
parent 5764d87a08
commit 6644b29257
4 changed files with 109 additions and 2 deletions

View File

@ -16,6 +16,7 @@
#include "led_chplex.h"
#include "charge_pump.h"
#include "power_switch.h"
volatile int wait_frame = 1;
@ -102,10 +103,13 @@ int main(void)
bool ledtest_done = false;
bool startup_done = false;
uint8_t switchtest = 0; // FIXME: delme: just for testing
init_clock();
init_gpio();
charge_pump_init();
power_switch_init();
led_chplex_init();
led_chplex_on(LED_CHPLEX_IDX_SOLAR_ON);
@ -119,9 +123,34 @@ int main(void)
ledtest_done = ledtest(timebase_ms);
} else if(!startup_done) {
charge_pump_start();
power_switch_load_on(); // FIXME: just for testing!
startup_done = true;
} else if(timebase_ms % 500 == 0) {
led_chplex_toggle(LED_CHPLEX_IDX_DISCHARGE_PULSE);
} else {
if(timebase_ms % 500 == 0) {
led_chplex_toggle(LED_CHPLEX_IDX_DISCHARGE_PULSE);
}
// FIXME: just for testing
if(timebase_ms % 3000 == 0) {
switchtest++;
if(switchtest & 0x01) {
power_switch_solar_on();
led_chplex_on(LED_CHPLEX_IDX_SOLAR_ON);
} else {
power_switch_solar_off();
led_chplex_off(LED_CHPLEX_IDX_SOLAR_ON);
}
if(switchtest & 0x02) {
power_switch_load_on();
led_chplex_on(LED_CHPLEX_IDX_LOAD_ON);
} else {
power_switch_load_off();
led_chplex_off(LED_CHPLEX_IDX_LOAD_ON);
}
}
}
led_chplex_periodic();

View File

@ -17,4 +17,14 @@
#define CHARGE_PUMP_DRV_PIN GPIO8 // Timer 1, Channel 1
/* Solar switch */
#define SOLAR_SWITCH_PORT GPIOA
#define SOLAR_SWITCH_PIN GPIO11
/* Load switch */
#define LOAD_SWITCH_PORT GPIOA
#define LOAD_SWITCH_PIN GPIO12
#endif // PINOUT_H

52
src/power_switch.c Normal file
View File

@ -0,0 +1,52 @@
#include <libopencm3/stm32/gpio.h>
#include "pinout.h"
#include "power_switch.h"
void power_switch_init(void)
{
// Solar switch: output, initially low = off
gpio_clear(SOLAR_SWITCH_PORT, SOLAR_SWITCH_PIN);
gpio_mode_setup(SOLAR_SWITCH_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, SOLAR_SWITCH_PIN);
// Load switch: output, initially low = off
gpio_clear(LOAD_SWITCH_PORT, LOAD_SWITCH_PIN);
gpio_mode_setup(LOAD_SWITCH_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LOAD_SWITCH_PIN);
}
void power_switch_solar_on(void)
{
gpio_set(SOLAR_SWITCH_PORT, SOLAR_SWITCH_PIN);
}
void power_switch_solar_off(void)
{
gpio_clear(SOLAR_SWITCH_PORT, SOLAR_SWITCH_PIN);
}
bool power_switch_solar_status(void)
{
return gpio_get(SOLAR_SWITCH_PORT, SOLAR_SWITCH_PIN) != 0;
}
void power_switch_load_on(void)
{
gpio_set(LOAD_SWITCH_PORT, LOAD_SWITCH_PIN);
}
void power_switch_load_off(void)
{
gpio_clear(LOAD_SWITCH_PORT, LOAD_SWITCH_PIN);
}
bool power_switch_load_status(void)
{
return gpio_get(LOAD_SWITCH_PORT, LOAD_SWITCH_PIN) != 0;
}

16
src/power_switch.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef POWER_SWITCH_H
#define POWER_SWITCH_H
#include <stdbool.h>
void power_switch_init(void);
void power_switch_solar_on(void);
void power_switch_solar_off(void);
bool power_switch_solar_status(void);
void power_switch_load_on(void);
void power_switch_load_off(void);
bool power_switch_load_status(void);
#endif // POWER_SWITCH_H