]> the.earth.li Git - energenie-attiny.git/blob - main.c
Add initial 433MHz / USB Relay code
[energenie-attiny.git] / main.c
1 /*
2  * Basic firmware to control Some Energenie 433MHz sockets (ENER002-4)
3  * as if they were a www.dcttech.com 4 port USB Relay board
4  *
5  * Copyright 2018 Jonathan McDowell <noodles@earth.li>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 #include <stdbool.h>
21 #include <avr/eeprom.h>
22 #include <avr/interrupt.h>
23 #include <avr/io.h>
24 #include <avr/wdt.h>
25 #include <util/delay.h>
26
27 #include <avr/pgmspace.h>
28 #include "usbdrv.h"
29 #include "libs-device/osccal.h"
30
31 #define CMD_ALL_ON 0xfe
32 #define CMD_ALL_OFF 0xfc
33 #define CMD_ON 0xff
34 #define CMD_OFF 0xfd
35 #define CMD_SET_SERIAL 0xfa
36
37 /* Energenie 24 bit address , last 4 all 0 for code */
38 #define ENER_ADDR 0x123450
39
40 uchar serno_read = 0;
41 uchar serno[6];
42 unsigned long cmd = 0;
43 int repeat = 0;
44
45 PROGMEM const char usbHidReportDescriptor[22] = {
46         0x06, 0x00, 0xff,               /* USAGE PAGE (Generic Desktop) */
47         0x09, 0x01,                     /* USAGE (Vendor Usage 1) */
48         0xa1, 0x01,                     /* COLLECTION (Application) */
49         0x15, 0x00,                     /*   LOGICAL_MINIMUM (0) */
50         0x26, 0xff, 0x00,               /*   LOGICAL_MAXIMUM (255) */
51         0x75, 0x08,                     /*   REPORT_SIZE (8) */
52         0x95, 0x08,                     /*   REPORT_COUNT (8) */
53         0x09, 0x00,                     /*   USAGE (Undefined) */
54         0xb2, 0x02, 0x01,               /*   FEATURE (Data, Var, Abs, Buf) */
55         0xc0                            /* END_COLLECTION */
56 };
57
58 void fetch_serno(void)
59 {
60         if (!serno_read) {
61                 eeprom_read_block(serno, 0, 6);
62                 if (serno[0] == 0xff) {
63                         /* If the EEPROM is blank, return a default serial # */
64                         serno[0] = 'U';
65                         serno[1] = 'N';
66                         serno[2] = 'S';
67                         serno[3] = 'E';
68                         serno[4] = 'T';
69                         serno[5] = 0;
70                 }
71                 serno_read = 1;
72         }
73 }
74
75 void update_serno(uchar *buf, uchar len)
76 {
77         uchar i;
78
79         /*
80          * I have no idea why this gets stored 3 times, but the original
81          * firmware does it.
82          */
83         eeprom_write_block(buf, (void *) 0x00, len);
84         eeprom_write_block(buf, (void *) 0x40, len);
85         eeprom_write_block(buf, (void *) 0x80, len);
86
87         for (i = 0; i < 6; i++) {
88                 serno[i] = buf[i];
89         }
90 }
91
92 usbMsgLen_t usbFunctionSetup(uchar data[8])
93 {
94         usbRequest_t *rq = (void *) data;
95
96         if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
97                 if ((rq->bRequest == USBRQ_HID_GET_REPORT) ||
98                                 (rq->bRequest == USBRQ_HID_SET_REPORT)) {
99                         return 0xFF;
100                 }
101         }
102
103         return 0;
104 }
105
106 uchar usbFunctionRead(uchar *data, uchar len)
107 {
108         uchar i;
109
110         if (len != 0) {
111                 fetch_serno();
112                 for (i = 0; i < 6; i++) {
113                         data[i] = serno[i];
114                 }
115                 data[6] = data[7] = 0;
116                 if (PORTB & (1 << PB0)) {
117                         data[7] = 1;
118                 }
119                 return len;
120         }
121
122         return 0;
123 }
124
125 uchar usbFunctionWrite(uchar *data, uchar len)
126 {
127         if (data[0] == CMD_ALL_ON) {
128                 cmd = ENER_ADDR | 0xd;
129                 repeat = 5;
130         } else if (data[0] == CMD_ALL_OFF) {
131                 cmd = ENER_ADDR | 0xc;
132                 repeat = 5;
133         } else if (data[0] == CMD_ON) {
134                 switch (data[1]) {
135                 case 1:
136                         cmd = ENER_ADDR | 0xf;
137                         repeat = 5;
138                         break;
139                 case 2:
140                         cmd = ENER_ADDR | 0x7;
141                         repeat = 5;
142                         break;
143                 case 3:
144                         cmd = ENER_ADDR | 0xb;
145                         repeat = 5;
146                         break;
147                 case 4:
148                         cmd = ENER_ADDR | 0x3;
149                         repeat = 5;
150                         break;
151                 default:
152                         break;
153                 }
154         } else if (data[0] == CMD_OFF) {
155                 switch (data[1]) {
156                 case 1:
157                         cmd = ENER_ADDR | 0xe;
158                         repeat = 5;
159                         break;
160                 case 2:
161                         cmd = ENER_ADDR | 0x6;
162                         repeat = 5;
163                         break;
164                 case 3:
165                         cmd = ENER_ADDR | 0xa;
166                         repeat = 5;
167                         break;
168                 case 4:
169                         cmd = ENER_ADDR | 0x2;
170                         repeat = 5;
171                         break;
172                 default:
173                         break;
174                 }
175         } else if (data[0] == CMD_SET_SERIAL) {
176                 update_serno(&data[1], 6);
177         }
178
179         return len;
180 }
181
182 void t433_transmit_bit(bool value)
183 {
184         PORTB |= 1 << PB0;
185         if (value)
186                 _delay_us(600);
187         else
188                 _delay_us(200);
189
190         PORTB &= ~(1 << PB0);
191         if (value)
192                 _delay_us(200);
193         else
194                 _delay_us(600);
195 }
196
197 void t433_send(unsigned long code, unsigned int length)
198 {
199         int i;
200
201         cli();
202         for (i = length - 1; i >= 0; i--) {
203                 if (code & (1L << i)) {
204                         t433_transmit_bit(true);
205                 } else {
206                         t433_transmit_bit(false);
207                 }
208         }
209         /* Send a sync bit */
210         PORTB |= 1 << PB0;
211         _delay_us(200);
212         PORTB &= ~(1 << PB0);
213         sei();
214         _delay_ms(30);
215 }
216
217 int __attribute__((noreturn)) main(void)
218 {
219         unsigned char i;
220
221         wdt_enable(WDTO_1S);
222
223         usbInit();
224         usbDeviceDisconnect();
225
226         i = 0;
227         while (--i) {
228                 wdt_reset();
229                 _delay_ms(1);
230         }
231
232         usbDeviceConnect();
233
234         /* Set the 433MHz transmitter bit to output mode */
235         DDRB |= (1 << PB0);
236         PORTB &= (1 << PB0);
237
238         sei(); /* We're ready to go; enable interrupts */
239
240         while (1) {
241                 wdt_reset();
242                 usbPoll();
243                 if (cmd) {
244                         t433_send(cmd, 24);
245                         if (--repeat == 0)
246                                 cmd = 0;
247                 }
248         }
249 }