]> the.earth.li Git - usb-relay-firmware.git/blob - main.c
Add initial README.md
[usb-relay-firmware.git] / main.c
1 /*
2  * Basic firmware for www.dcttech.com 1 port USB Relay
3  *
4  * Copyright 2016 Jonathan McDowell <noodles@earth.li>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 #include <avr/eeprom.h>
20 #include <avr/interrupt.h>
21 #include <avr/io.h>
22 #include <avr/wdt.h>
23 #include <util/delay.h>
24
25 #include <avr/pgmspace.h>
26 #include "usbdrv.h"
27 #include "libs-device/osccal.h"
28
29 #define RELAY_BIT 8 /* Bit 3 on port B */
30 #define CMD_ALL_ON 0xfe
31 #define CMD_ALL_OFF 0xfc
32 #define CMD_ON 0xff
33 #define CMD_OFF 0xfd
34 #define CMD_SET_SERIAL 0xfa
35
36 uchar serno_read = 0;
37 uchar serno[6];
38
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 */
50 };
51
52 void fetch_serno(void)
53 {
54         if (!serno_read) {
55                 eeprom_read_block(serno, 0, 6);
56                 if (serno[0] == 0xff) {
57                         /* If the EEPROM is blank, return a default serial # */
58                         serno[0] = 'U';
59                         serno[1] = 'N';
60                         serno[2] = 'S';
61                         serno[3] = 'E';
62                         serno[4] = 'T';
63                         serno[5] = 0;
64                 }
65                 serno_read = 1;
66         }
67 }
68
69 void update_serno(uchar *buf, uchar len)
70 {
71         uchar i;
72
73         /*
74          * I have no idea why this gets stored 3 times, but the original
75          * firmware does it.
76          */
77         eeprom_write_block(buf, (void *) 0x00, len);
78         eeprom_write_block(buf, (void *) 0x40, len);
79         eeprom_write_block(buf, (void *) 0x80, len);
80
81         for (i = 0; i < 6; i++) {
82                 serno[i] = buf[i];
83         }
84 }
85
86 usbMsgLen_t usbFunctionSetup(uchar data[8])
87 {
88         usbRequest_t *rq = (void *) data;
89
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)) {
93                         return 0xFF;
94                 }
95         }
96
97         return 0;
98 }
99
100 uchar usbFunctionRead(uchar *data, uchar len)
101 {
102         uchar i;
103
104         if (len != 0) {
105                 fetch_serno();
106                 for (i = 0; i < 6; i++) {
107                         data[i] = serno[i];
108                 }
109                 data[6] = data[7] = 0;
110                 if (PORTB & RELAY_BIT) {
111                         data[7] = 1;
112                 }
113                 return len;
114         }
115
116         return 0;
117 }
118
119 uchar usbFunctionWrite(uchar *data, uchar len)
120 {
121         if (data[0] == CMD_ALL_ON) {
122                 PORTB |= RELAY_BIT;
123         } else if (data[0] == CMD_ALL_OFF) {
124                 PORTB &= ~(RELAY_BIT);
125         } else if (data[0] == CMD_ON) {
126                 if (data[1] == 1) {
127                         PORTB |= RELAY_BIT;
128                 }
129         } else if (data[0] == CMD_OFF) {
130                 if (data[1] == 1) {
131                         PORTB &= ~(RELAY_BIT);
132                 }
133         } else if (data[0] == CMD_SET_SERIAL) {
134                 update_serno(&data[1], 6);
135         }
136
137         return len;
138 }
139
140 int __attribute__((noreturn)) main(void)
141 {
142         unsigned char i;
143
144         wdt_enable(WDTO_1S);
145
146         usbInit();
147         usbDeviceDisconnect();
148
149         i = 0;
150         while (--i) {
151                 wdt_reset();
152                 _delay_ms(1);
153         }
154
155         usbDeviceConnect();
156
157         /* Set the relay bit to output mode */
158         DDRB |= RELAY_BIT;
159
160         sei(); /* We're ready to go; enable interrupts */
161
162         while (1) {
163                 wdt_reset();
164                 usbPoll();
165         }
166 }