#include <string.h>
+static void ep82_send(u32 key_code);
+
/*** Hardware init ***/
static void clock_init(void)
i2c_set_speed(I2C1, i2c_speed_sm_100k, rcc_apb1_frequency / 1000000);
i2c_peripheral_enable(I2C1);
- disp[0] = 0x82;
- disp[1] = 0xdc;
- disp[2] = 0xd6;
- disp[3] = 0xb2;
+ disp[0] = 0x10;
+ disp[1] = 0x10;
+ disp[2] = 0x10;
+ disp[3] = 0x10;
display_update();
}
ir_bits = 1;
ir_code = 0;
} else if (between(space, 2000, 2300)) {
- debug_printf("IR ==> REP\n");
+ debug_printf("IR: => REP\n");
ir_bits = IR_ERR;
}
}
ir_bits = IR_ERR;
}
if (ir_bits == 33) {
- debug_printf("IR ==> %08x\n", (uint)ir_code);
+ debug_printf("IR: => %08x\n", (uint)ir_code);
disp[3] ^= 0x01;
display_update();
ir_bits = IR_ERR;
+ ep82_send(ir_code);
}
} else {
ir_bits = IR_ERR;
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
.wMaxPacketSize = 64,
.bInterval = 1,
+}, {
+ // Bulk end-point for receiving remote control keys
+ .bLength = USB_DT_ENDPOINT_SIZE,
+ .bDescriptorType = USB_DT_ENDPOINT,
+ .bEndpointAddress = 0x82,
+ .bmAttributes = USB_ENDPOINT_ATTR_BULK,
+ .wMaxPacketSize = 4,
+ .bInterval = 1,
}};
static const struct usb_interface_descriptor iface = {
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
- .bNumEndpoints = 1,
+ .bNumEndpoints = 2,
.bInterfaceClass = 0xFF,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.interface = ifaces,
};
-static byte usb_configured;
+static bool usb_configured;
static uint8_t usbd_control_buffer[64];
+static bool usb_tx_in_flight;
+static byte ep82_tx_buffer[4];
+
+static byte disp_alive;
+
static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
{
// Reset to bootloader, which implements the rest of DFU
if (buf[4])
disp[1] |= 0x01;
display_update();
+ disp_alive = 10;
+ }
+}
+
+static void ep82_send(u32 key_code)
+{
+ if (usb_tx_in_flight) {
+ debug_printf("USB: Send overrun!\n");
+ return;
}
+
+ debug_printf("USB: Sending key to host\n");
+ put_u32_be(ep82_tx_buffer, key_code);
+ usbd_ep_write_packet(usbd_dev, 0x82, ep82_tx_buffer, 4);
+ usb_tx_in_flight = true;
+}
+
+static void ep82_cb(usbd_device *dev UNUSED, uint8_t ep UNUSED)
+{
+ // We completed sending a frame to the USB host
+ usb_tx_in_flight = false;
+ debug_printf("USB: Key sending complete\n");
}
static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
dfu_control_cb);
usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
- usb_configured = 1;
+ usbd_ep_setup(dev, 0x82, USB_ENDPOINT_ATTR_BULK, 4, ep82_cb);
+ usb_configured = true;
}
static void reset_cb(void)
{
debug_printf("USB: Reset\n");
- usb_configured = 0;
+ usb_configured = false;
}
static volatile bool usb_event_pending;
if (ms_ticks - last_blink >= 500) {
debug_led_toggle();
last_blink = ms_ticks;
+ if (disp_alive) {
+ if (!--disp_alive) {
+ disp[0] = (disp[0] & 0x01) | 0x10;
+ disp[1] = (disp[1] & 0x01) | 0x10;
+ disp[2] = (disp[2] & 0x01) | 0x10;
+ disp[3] = (disp[3] & 0x01) | 0x10;
+ }
+ }
disp[0] ^= 0x01;
display_update();
}