usb_magic_hub/firmware/main.c
2025-05-05 15:11:46 +03:00

55 lines
873 B
C

#include <avr/io.h>
#include <avr/sleep.h>
#include <stdint.h>
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();
}
}