BaitAndSwitch/commands.c
2023-08-16 21:07:52 +02:00

115 lines
1.7 KiB
C

#include "board.h"
#include "commands.h"
#include "usb.h"
#include "main.h"
#include <LUFA/Drivers/USB/USB.h>
extern FILE USBSerialStream;
static void DB(char const *s) {
if (connected) {
fputs(s, &USBSerialStream);
fputs("\r\n", &USBSerialStream);
}
}
void connectCTL(void) {
DB("CC");
connectPowerCTL();
disconnectPowerDUT();
connectDataCTL();
}
void connectDUT(void) {
DB("CD");
connectPowerDUT();
disconnectPowerCTL();
connectDataDUT();
}
void connectDataCTL(void) {
DB("CDC");
PortBIndOff();
CtrlDataOn();
OutEnOn();
PortAIndOn();
}
void connectDataDUT(void) {
DB("CDD");
PortAIndOff();
DutDataOn();
OutEnOn();
PortBIndOn();
}
void connectPowerCTL(void) {
DB("CPC");
PortAPowOn();
}
void connectPowerDUT(void) {
DB("CPD");
PortBPowOn();
}
void disconnect(void) {
DB("D");
disconnectData();
disconnectPower();
}
void disconnectData(void) {
DB("DD");
OutEnOff();
PortAIndOff();
PortBIndOff();
}
void disconnectPower(void) {
DB("DP");
disconnectPowerCTL();
disconnectPowerDUT();
}
void disconnectPowerCTL(void) {
DB("DPC");
PortAPowOff();
}
void disconnectPowerDUT(void) {
DB("DPD");
PortBPowOff();
}
void clearID(void) {
DB("CI");
SysLedOff();
}
void setID(void) {
DB("SI");
SysLedOn();
}
void status(void) {
char buf[30];
snprintf(buf, sizeof buf,
"PC:%d, PD:%d, "
"DT:%s\r\n"
, PortAPowPort(), PortBPowPort()
, OutEnPort() ? (CtrlDataPort() ? "Ctrl" : "DUT") : "off"
);
CDC_Device_SendString(&VirtualSerial_CDC_Interface, buf);
}
void debug(void) {
char buf[20];
snprintf(buf, sizeof buf,
"(%02x %02x %02x)\r\n"
, (unsigned) (PINB & 0x87), (unsigned) (PINC & 0xF6), (unsigned) (PIND & 0xFF)
);
CDC_Device_SendString(&VirtualSerial_CDC_Interface, buf);
}