2 * Basic firmware for www.dcttech.com 1 port USB Relay
4 * Copyright 2016 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <avr/eeprom.h>
20 #include <avr/interrupt.h>
23 #include <util/delay.h>
25 #include <avr/pgmspace.h>
27 #include "libs-device/osccal.h"
29 #define RELAY_BIT 8 /* Bit 3 on port B */
30 #define CMD_ALL_ON 0xfe
31 #define CMD_ALL_OFF 0xfc
34 #define CMD_SET_SERIAL 0xfa
39 PROGMEM const char usbHidReportDescriptor[22] = {
40 0x06, 0x00, 0xff, /* USAGE PAGE (Generic Desktop) */
41 0x09, 0x01, /* USAGE (Vendor Usage 1) */
42 0xa1, 0x01, /* COLLECTION (Application) */
43 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
44 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
45 0x75, 0x08, /* REPORT_SIZE (8) */
46 0x95, 0x08, /* REPORT_COUNT (8) */
47 0x09, 0x00, /* USAGE (Undefined) */
48 0xb2, 0x02, 0x01, /* FEATURE (Data, Var, Abs, Buf) */
49 0xc0 /* END_COLLECTION */
52 void fetch_serno(void)
55 eeprom_read_block(serno, 0, 6);
56 if (serno[0] == 0xff) {
57 /* If the EEPROM is blank, return a default serial # */
69 void update_serno(uchar *buf, uchar len)
74 * I have no idea why this gets stored 3 times, but the original
77 eeprom_write_block(buf, (void *) 0x00, len);
78 eeprom_write_block(buf, (void *) 0x40, len);
79 eeprom_write_block(buf, (void *) 0x80, len);
81 for (i = 0; i < 6; i++) {
86 usbMsgLen_t usbFunctionSetup(uchar data[8])
88 usbRequest_t *rq = (void *) data;
90 if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
91 if ((rq->bRequest == USBRQ_HID_GET_REPORT) ||
92 (rq->bRequest == USBRQ_HID_SET_REPORT)) {
100 uchar usbFunctionRead(uchar *data, uchar len)
106 for (i = 0; i < 6; i++) {
109 data[6] = data[7] = 0;
110 if (PORTB & RELAY_BIT) {
119 uchar usbFunctionWrite(uchar *data, uchar len)
121 if (data[0] == CMD_ALL_ON) {
123 } else if (data[0] == CMD_ALL_OFF) {
124 PORTB &= ~(RELAY_BIT);
125 } else if (data[0] == CMD_ON) {
129 } else if (data[0] == CMD_OFF) {
131 PORTB &= ~(RELAY_BIT);
133 } else if (data[0] == CMD_SET_SERIAL) {
134 update_serno(&data[1], 6);
140 int __attribute__((noreturn)) main(void)
147 usbDeviceDisconnect();
157 /* Set the relay bit to output mode */
160 sei(); /* We're ready to go; enable interrupts */