From a799831773c9fb19a8ab63f8b6801e67a4412a5f Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 14 May 2023 14:34:24 +0200 Subject: [PATCH] test-display2: Sending IR keys over USB and display timeout --- test-display2/main.c | 66 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/test-display2/main.c b/test-display2/main.c index d8e9cdf..e12ee02 100644 --- a/test-display2/main.c +++ b/test-display2/main.c @@ -21,6 +21,8 @@ #include +static void ep82_send(u32 key_code); + /*** Hardware init ***/ static void clock_init(void) @@ -166,10 +168,10 @@ static void display_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(); } @@ -311,7 +313,7 @@ static void ir_decode(void) 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; } } @@ -327,10 +329,11 @@ static void ir_decode(void) 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; @@ -381,6 +384,14 @@ static const struct usb_endpoint_descriptor endpoints[] = {{ .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 = { @@ -388,7 +399,7 @@ 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, @@ -440,9 +451,14 @@ static const struct usb_config_descriptor config = { .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 @@ -480,7 +496,28 @@ static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED) 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) @@ -491,13 +528,14 @@ 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; @@ -560,6 +598,14 @@ int main(void) 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(); } -- 2.39.2