diff --git a/src/main.c b/src/main.c index e5e29bf..a663072 100644 --- a/src/main.c +++ b/src/main.c @@ -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(); diff --git a/src/pinout.h b/src/pinout.h index 5dfd3ce..340bcf7 100644 --- a/src/pinout.h +++ b/src/pinout.h @@ -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 diff --git a/src/power_switch.c b/src/power_switch.c new file mode 100644 index 0000000..d5764dc --- /dev/null +++ b/src/power_switch.c @@ -0,0 +1,52 @@ +#include + +#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; +} diff --git a/src/power_switch.h b/src/power_switch.h new file mode 100644 index 0000000..8f4ab10 --- /dev/null +++ b/src/power_switch.h @@ -0,0 +1,16 @@ +#ifndef POWER_SWITCH_H +#define POWER_SWITCH_H + +#include + +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