From 24ed427a2bec418f7165862dfd50569e4e62c41e Mon Sep 17 00:00:00 2001 From: nedko Date: Mon, 5 May 2025 15:11:46 +0300 Subject: [PATCH] Added firmware --- firmware/.gitignore | 1 + firmware/Makefile | 11 +++++++++ firmware/main.c | 54 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 firmware/.gitignore create mode 100644 firmware/Makefile create mode 100644 firmware/main.c diff --git a/firmware/.gitignore b/firmware/.gitignore new file mode 100644 index 0000000..e0fc268 --- /dev/null +++ b/firmware/.gitignore @@ -0,0 +1 @@ +magic.hex diff --git a/firmware/Makefile b/firmware/Makefile new file mode 100644 index 0000000..e8252f5 --- /dev/null +++ b/firmware/Makefile @@ -0,0 +1,11 @@ +all: + make build + make program + +build: + avr-gcc main.c -o magic.hex -mmcu=attiny84a -Wall -Wextra -Os + +program: + avrdude -p attiny84 -c usbtiny -U flash:w:magic.hex + +.PHONY: all build program diff --git a/firmware/main.c b/firmware/main.c new file mode 100644 index 0000000..e27f1f6 --- /dev/null +++ b/firmware/main.c @@ -0,0 +1,54 @@ +#include +#include + +#include + +int main() +{ + uint8_t input; + uint8_t temp; + uint8_t count; + uint8_t i; + + // Setup + PORTA = (1 << PORTA7) | (1 << PORTA6) | (1 << PORTA5) | (1 << PORTA4); + DDRA = (1 << DDA7) | (1 << DDA6) | (1 << DDA5) | (1 << DDA4); + PORTB = (1 << PORTB2); + DDRB = (1 << DDB2); + PRR = (1 << PRTIM1) | (1 << PRTIM0) | (1 << PRUSI) | (1 << PRADC); + + // Read Inputs + input = PINA; + temp = input; + count = 0; + + // How many inputs are set + for (i = 0; i < 4; ++i) + { + if (temp & 0x01) + { + ++count; + } + temp >>= 1; + } + + // If none or more than 1 are providing power - do nothing until reboot + if (count != 1) + { + goto end; + } + + // Set Upstream Facing Port + input = ~input; + input <<= 4; + PORTA = input; + PORTB &= ~(1 << PORTB2); + +end: + // Power Down Mode + MCUCR = (1 << SM1) | (1 << SE); + while (1) + { + sleep_cpu(); + } +}