2 * Basic firmware for an RGB LED notifier clone of the Riso Kaguku
5 * Copyright 2016 Jonathan McDowell <noodles@earth.li>
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.
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.
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/>.
20 #include <avr/eeprom.h>
21 #include <avr/interrupt.h>
24 #include <util/delay.h>
26 #include <avr/pgmspace.h>
28 #include "libs-device/osccal.h"
30 #define GREEN_BIT 1 /* Bit 0 on port B */
31 #define RED_BIT 2 /* Bit 1 on port B */
32 #define BLUE_BIT 4 /* Bit 2 on port B */
33 #define ALL_BITS (RED_BIT | GREEN_BIT | BLUE_BIT)
34 #define CMD_SET_SERIAL 0xfa
37 USB_STRING_DESCRIPTOR_HEADER(8),
38 'U', 'N', 'S', 'E', 'T', 'X', 'X', 'X',
41 PROGMEM const char usbDescriptorConfiguration[CONFIG_DESCRIPTOR_SIZE] = { /* USB configuration descriptor */
42 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
43 USBDESCR_CONFIG, /* descriptor type */
45 /* total length of data returned (including inlined descriptors) */
46 1, /* number of interfaces in this configuration */
47 1, /* index of this configuration */
48 0, /* configuration name string index */
49 (1 << 7) | USBATTR_REMOTEWAKE, /* attributes */
50 USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
51 /* interface descriptor follows inline: */
52 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
53 USBDESCR_INTERFACE, /* descriptor type */
54 0, /* index of this interface */
55 0, /* alternate setting for this interface */
56 2, /* endpoints excl 0: number of endpoint descriptors to follow */
57 USB_CFG_INTERFACE_CLASS,
58 USB_CFG_INTERFACE_SUBCLASS,
59 USB_CFG_INTERFACE_PROTOCOL,
60 0, /* string index for interface */
61 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
62 USBDESCR_HID, /* descriptor type: HID */
63 0x01, 0x01, /* BCD representation of HID version */
64 0x00, /* target country code */
65 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
66 0x22, /* descriptor type: report */
67 USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH, 0, /* total length of report descriptor */
68 /* endpoint descriptor for endpoint 1 */
69 7, /* sizeof(usbDescrEndpoint) */
70 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
71 (char)0x81, /* IN endpoint number 1 */
72 0x03, /* attrib: Interrupt endpoint */
73 8, 0, /* maximum packet size */
74 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
75 /* endpoint descriptor for endpoint 2 */
76 7, /* sizeof(usbDescrEndpoint) */
77 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
78 2, /* OUT endpoint number 2 */
79 0x03, /* attrib: Interrupt endpoint */
80 5, 0, /* maximum packet size */
81 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
84 PROGMEM const char usbHidReportDescriptor[22] = {
85 0x06, 0x00, 0xff, /* USAGE PAGE (Generic Desktop) */
86 0x09, 0x01, /* USAGE (Vendor Usage 1) */
87 0xa1, 0x01, /* COLLECTION (Application) */
88 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
89 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
90 0x75, 0x08, /* REPORT_SIZE (8 bits) */
91 0x95, 0x08, /* REPORT_COUNT (8 elements) */
92 0x09, 0x00, /* USAGE (Undefined) */
93 0xb2, 0x02, 0x01, /* FEATURE (Data, Var, Abs, Buf) */
94 0xc0 /* END_COLLECTION */
97 inline char hexdigit(int i)
99 return (i < 10) ? ('0' + i) : ('A' - 10 + i);
102 void fetch_serno(void)
106 eeprom_read_block(&serno, 0, 4);
107 if (serno == 0xffffffff) {
117 serno_str[1] = hexdigit((serno >> 28));
118 serno_str[2] = hexdigit((serno >> 24) & 0xF);
119 serno_str[3] = hexdigit((serno >> 20) & 0xF);
120 serno_str[4] = hexdigit((serno >> 16) & 0xF);
121 serno_str[5] = hexdigit((serno >> 12) & 0xF);
122 serno_str[6] = hexdigit((serno >> 8) & 0xF);
123 serno_str[7] = hexdigit((serno >> 4) & 0xF);
124 serno_str[8] = hexdigit( serno & 0xF);
128 void update_serno(uint32_t serno)
130 eeprom_write_block(&serno, 0x00, 4);
132 serno_str[1] = hexdigit((serno >> 28));
133 serno_str[2] = hexdigit((serno >> 24) & 0xF);
134 serno_str[3] = hexdigit((serno >> 20) & 0xF);
135 serno_str[4] = hexdigit((serno >> 16) & 0xF);
136 serno_str[5] = hexdigit((serno >> 12) & 0xF);
137 serno_str[6] = hexdigit((serno >> 8) & 0xF);
138 serno_str[7] = hexdigit((serno >> 4) & 0xF);
139 serno_str[8] = hexdigit( serno & 0xF);
142 usbMsgLen_t usbFunctionSetup(uchar data[8])
144 usbRequest_t *rq = (void *) data;
146 if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
147 if ((rq->bRequest == USBRQ_HID_GET_REPORT) ||
148 (rq->bRequest == USBRQ_HID_SET_REPORT)) {
156 usbMsgLen_t usbFunctionDescriptor(usbRequest_t *rq)
158 if (rq->wValue.bytes[1] == USBDESCR_STRING &&
159 rq->wValue.bytes[0] == 3) {
160 usbMsgPtr = (usbMsgPtr_t) serno_str;
161 return sizeof(serno_str);
167 uchar usbFunctionRead(uchar *data, uchar len)
169 uint8_t colour_map[8] = { 0, 2, 1, 5, 3, 6, 4, 7 };
176 if (PORTB & GREEN_BIT)
178 if (PORTB & BLUE_BIT)
180 /* Map RGB back to Riso Kagaku colour code. */
181 data[0] = colour_map[status];
188 uchar usbFunctionWrite(uchar *data, uchar len)
191 * The Riso Kagaku has an internal colour table, map it back to an
194 uint8_t colour_map[8] = { 0, 2, 1, 4, 6, 3, 5, 7 };
195 uint8_t rgb_val, port_val;
198 rgb_val = colour_map[data[0]];
200 port_val = PORTB & ~ALL_BITS;
205 port_val |= GREEN_BIT;
207 port_val |= BLUE_BIT;
210 } else if (data[0] == CMD_SET_SERIAL) {
211 update_serno(*(uint32_t *) &data[1]);
217 void usbFunctionWriteOut(uchar *data, uchar len)
220 * The Riso Kagaku has an internal colour table, map it back to an
223 uint8_t colour_map[8] = { 0, 2, 1, 4, 6, 3, 5, 7 };
224 uint8_t rgb_val, port_val;
230 rgb_val = colour_map[data[0]];
232 port_val = PORTB & ~ALL_BITS;
237 port_val |= GREEN_BIT;
239 port_val |= BLUE_BIT;
242 } else if (len == 5 && data[0] == CMD_SET_SERIAL) {
243 update_serno(*(uint32_t *) &data[1]);
247 int __attribute__((noreturn)) main(void)
256 usbDeviceDisconnect();
266 /* Set the LED bits to output mode */
271 sei(); /* We're ready to go; enable interrupts */