--- /dev/null
+/*
+ * Daemon for X-mas Led Strip
+ *
+ * (c) 2025 Martin Mares <mj@ucw.cz>
+ */
+
+#include <ucw/lib.h>
+#include <ucw/log.h>
+#include <ucw/string.h>
+#include <ucw/unaligned.h>
+
+#include <math.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+#include <threads.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <libusb.h>
+#include <mosquitto.h>
+
+typedef unsigned char byte;
+typedef uint32_t u32;
+typedef unsigned int uint;
+
+#include "../firmware/interface.h"
+
+static mtx_t light_mutex;
+static cnd_t light_cond;
+static byte lights[NPIX_NUM_LEDS][3];
+static bool light_refresh;
+
+/*** MQTT ***/
+
+static struct mosquitto *mosq;
+static bool mqtt_connected;
+
+static void mqtt_publish(const char *topic, const char *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+
+ if (mqtt_connected) {
+ char m[256];
+ int l = vsnprintf(m, sizeof(m), fmt, args);
+ int err = mosquitto_publish(mosq, NULL, topic, l, m, 0, true);
+ if (err != MOSQ_ERR_SUCCESS)
+ msg(L_ERROR, "Mosquitto: Publish failed, error=%d", err);
+ }
+
+ va_end(args);
+}
+
+static void mqtt_conn_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int status)
+{
+ if (!status) {
+ msg(L_DEBUG, "MQTT: Connection established");
+ mqtt_connected = true;
+ mqtt_publish("status/led-strip", "ok");
+ if (mosquitto_subscribe(mosq, NULL, "ksp/led-strip", 1) != MOSQ_ERR_SUCCESS)
+ die("Mosquitto: subscribe failed");
+ } else if (mqtt_connected) {
+ msg(L_DEBUG, "MQTT: Connection lost");
+ mqtt_connected = false;
+ }
+}
+
+static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level, const char *message)
+{
+ msg(L_DEBUG, "MQTT(%d): %s", level, message);
+}
+
+static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m)
+{
+ char val[4096];
+ if (m->payloadlen >= sizeof(val) - 1) {
+ msg(L_ERROR, "Invalid value for topic %s", m->topic);
+ return;
+ }
+ memcpy(val, m->payload, m->payloadlen);
+ val[m->payloadlen] = 0;
+ msg(L_DEBUG, "MQTT < %s %s", m->topic, val);
+
+ if (strcmp(m->topic, "ksp/led-strip"))
+ return;
+
+ mtx_lock(&light_mutex);
+ uint i = 0;
+ char *x = val;
+ while (*x) {
+ if (*x == ' ')
+ x++;
+ else {
+ char *end;
+ unsigned long val = strtoul(x, &end, 16);
+ if (end == x)
+ break;
+ if (i >= NPIX_NUM_LEDS)
+ break;
+ lights[i][0] = (val >> 16) & 0xff;
+ lights[i][1] = (val >> 8) & 0xff;
+ lights[i][2] = (val >> 0) & 0xff;
+ i++;
+ x = end;
+ }
+ }
+ if (i == 0) {
+ for (uint i=0; i<NPIX_NUM_LEDS; i++)
+ lights[i][0] = lights[i][1] = lights[i][2] = 7;
+ } else {
+ uint m = i;
+ while (i < NPIX_NUM_LEDS) {
+ lights[i][0] = lights[i % m][0];
+ lights[i][1] = lights[i % m][1];
+ lights[i][2] = lights[i % m][2];
+ i++;
+ }
+ }
+ light_refresh = 1;
+ cnd_broadcast(&light_cond);
+ mtx_unlock(&light_mutex);
+}
+
+static void mqtt_init(void)
+{
+ mosquitto_lib_init();
+
+ mosq = mosquitto_new("led-strip", 1, NULL);
+ if (!mosq)
+ die("Mosquitto: Initialization failed");
+
+ mosquitto_connect_callback_set(mosq, mqtt_conn_callback);
+ mosquitto_log_callback_set(mosq, mqtt_log_callback);
+ mosquitto_message_callback_set(mosq, mqtt_msg_callback);
+
+ if (mosquitto_will_set(mosq, "status/led-strip", 4, "dead", 0, true) != MOSQ_ERR_SUCCESS)
+ die("Mosquitto: Unable to set will");
+
+ if (mosquitto_connect_async(mosq, "192.168.42.2", 1883, 60) != MOSQ_ERR_SUCCESS)
+ die("Mosquitto: Unable to connect");
+
+ if (mosquitto_loop_start(mosq))
+ die("Mosquitto: Cannot start service thread");
+}
+
+/*** USB ***/
+
+static struct libusb_context *usb_ctxt;
+static struct libusb_device_handle *devh;
+
+static void usb_error(const char *msg, ...)
+{
+ va_list args;
+ va_start(args, msg);
+ ucw_vmsg(L_ERROR, msg, args);
+ va_end(args);
+
+ if (devh) {
+ libusb_close(devh);
+ devh = NULL;
+ }
+}
+
+static void open_device(void)
+{
+ int err;
+ libusb_device **devlist;
+ ssize_t devn = libusb_get_device_list(usb_ctxt, &devlist);
+ if (devn < 0)
+ die("Cannot enumerate USB devices: error %d", (int) devn);
+
+ for (ssize_t i=0; i<devn; i++) {
+ struct libusb_device_descriptor desc;
+ libusb_device *dev = devlist[i];
+ if (!libusb_get_device_descriptor(dev, &desc)) {
+ if (desc.idVendor == NPIX_USB_VENDOR && desc.idProduct == NPIX_USB_PRODUCT) {
+ msg(L_INFO, "Found LED strip device at usb%d.%d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
+
+ if (err = libusb_open(dev, &devh)) {
+ usb_error("Cannot open device: error %d", err);
+ goto out;
+ }
+ libusb_reset_device(devh);
+ if (err = libusb_claim_interface(devh, 0)) {
+ usb_error("Cannot claim interface: error %d", err);
+ goto out;
+ }
+
+ goto out;
+ }
+ }
+ }
+
+out:
+ libusb_free_device_list(devlist, 1);
+}
+
+static void init_usb(void)
+{
+ int err;
+ if (err = libusb_init(&usb_ctxt))
+ die("Cannot initialize libusb: error %d", err);
+ // libusb_set_debug(usb_ctxt, 3);
+ open_device();
+}
+
+/*** Main ***/
+
+int main(int argc UNUSED, char **argv)
+{
+ log_init(argv[0]);
+ log_default_stream()->levels &= ~(1U << L_DEBUG);
+
+ mtx_init(&light_mutex, mtx_plain);
+ cnd_init(&light_cond);
+
+ mqtt_init();
+ init_usb();
+
+ bool need_resend = true;
+ for (;;) {
+ if (!devh) {
+ msg(L_INFO, "Waiting for device to appear...");
+ while (!devh) {
+ sleep(5);
+ open_device();
+ }
+ need_resend = true;
+ }
+
+ mtx_lock(&light_mutex);
+ while (!need_resend && !light_refresh)
+ cnd_wait(&light_cond, &light_mutex);
+ light_refresh = 0;
+ need_resend = 0;
+ mtx_unlock(&light_mutex);
+
+ msg(L_DEBUG, "Sending USB packet");
+
+ uint batch = 63/3;
+ for (uint i=0; i < NPIX_NUM_LEDS; i += batch) {
+ byte packet[64];
+ uint n = NPIX_NUM_LEDS - i;
+ if (n > batch)
+ n = batch;
+ packet[0] = i;
+ mtx_lock(&light_mutex);
+ for (uint j=0; j<n; j++) {
+ // Packet format is GRB
+ packet[1 + 3*j + 0] = lights[i+j][1];
+ packet[1 + 3*j + 1] = lights[i+j][0];
+ packet[1 + 3*j + 2] = lights[i+j][2];
+ }
+ mtx_unlock(&light_mutex);
+
+ int len = 1 + 3*n;
+ int err, transferred;
+ if (err = libusb_bulk_transfer(devh, 0x01, packet, len, &transferred, 1000)) {
+ usb_error("USB transfer failed: error %d", err);
+ break;
+ } else if (transferred != len)
+ usb_error("USB short transfer: %d out of %d bytes", transferred, len);
+ }
+ }
+}
--- /dev/null
+/*
+ * X-mas Neopixel (WS2812B) Strip
+ *
+ * (c) 2025 Martin Mareš <mj@ucw.cz>
+ */
+
+#include "util.h"
+
+#include <libopencm3/cm3/cortex.h>
+#include <libopencm3/cm3/nvic.h>
+#include <libopencm3/cm3/systick.h>
+#include <libopencm3/cm3/scb.h>
+#include <libopencm3/stm32/dma.h>
+#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/timer.h>
+#include <libopencm3/stm32/usart.h>
+#include <libopencm3/usb/dfu.h>
+#include <libopencm3/usb/usbd.h>
+
+#include <string.h>
+
+#include "interface.h"
+
+/*** Hardware init ***/
+
+static void clock_init(void)
+{
+ rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
+
+ rcc_periph_clock_enable(RCC_GPIOA);
+ rcc_periph_clock_enable(RCC_GPIOB);
+ rcc_periph_clock_enable(RCC_GPIOC);
+ rcc_periph_clock_enable(RCC_USART1);
+ rcc_periph_clock_enable(RCC_TIM4);
+ rcc_periph_clock_enable(RCC_DMA1);
+
+ rcc_periph_reset_pulse(RST_GPIOA);
+ rcc_periph_reset_pulse(RST_GPIOB);
+ rcc_periph_reset_pulse(RST_GPIOC);
+ rcc_periph_reset_pulse(RST_USART1);
+ rcc_periph_reset_pulse(RST_TIM4);
+}
+
+static void gpio_init(void)
+{
+ // PA9 = TXD1 for debugging console
+ // PA10 = RXD1 for debugging console
+ gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
+ gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
+
+ // PC13 = BluePill LED
+ gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
+ gpio_clear(GPIOC, GPIO13);
+
+ // PB8 = data for Neopixel
+ gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO8);
+}
+
+static void usart_init(void)
+{
+ usart_set_baudrate(USART1, 115200);
+ usart_set_databits(USART1, 8);
+ usart_set_stopbits(USART1, USART_STOPBITS_1);
+ usart_set_mode(USART1, USART_MODE_TX);
+ usart_set_parity(USART1, USART_PARITY_NONE);
+ usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
+
+ usart_enable(USART1);
+}
+
+/*** System ticks ***/
+
+static volatile u32 ms_ticks;
+
+void sys_tick_handler(void)
+{
+ ms_ticks++;
+}
+
+static void tick_init(void)
+{
+ systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
+ systick_counter_enable();
+ systick_interrupt_enable();
+}
+
+static void delay_ms(uint ms)
+{
+ u32 start_ticks = ms_ticks;
+ while (ms_ticks - start_ticks < ms)
+ ;
+}
+
+/*** Neopixels ***/
+
+#define NPIX_PERIOD 90 // timer runs on 72 MHz, so 90 periods = 1250 ns
+#define NPIX_RESET 10 // length of reset pulse in LED slots
+ // The chip needs longer reset pulse than documented.
+#define B0 30
+#define B1 60
+
+static byte neopixel_leds[NPIX_NUM_LEDS][3];
+static byte neopixel_buf[2*24];
+static uint neopixel_index;
+
+static void neopixel_set(uint led, byte r, byte g, byte b)
+{
+ cm_disable_interrupts();
+ neopixel_leds[led][0] = r;
+ neopixel_leds[led][1] = g;
+ neopixel_leds[led][2] = b;
+ cm_enable_interrupts();
+}
+
+static void neopixel_init(void)
+{
+ for (uint i=0; i < NPIX_NUM_LEDS; i++)
+ neopixel_set(i, 7, 7, 7);
+
+ // neopixel_buf is zero-initialized, which is a start of the reset sequence
+ memset(neopixel_buf, 0, sizeof(neopixel_buf));
+ neopixel_index = NPIX_NUM_LEDS;
+
+ // TIM4 update is connected to DMA1 channel 7
+
+ // FIXME: Strange programming sequence as specified in manual
+
+ dma_channel_reset(DMA1, 7);
+
+ dma_set_peripheral_address(DMA1, 7, (u32) &TIM_CCR3(TIM4));
+ dma_set_memory_address(DMA1, 7, (u32) neopixel_buf);
+ dma_set_number_of_data(DMA1, 7, ARRAY_SIZE(neopixel_buf));
+ dma_set_priority(DMA1, 7, DMA_CCR_PL_VERY_HIGH);
+
+ dma_set_read_from_memory(DMA1, 7);
+ dma_enable_circular_mode(DMA1, 7);
+
+ dma_set_memory_size(DMA1, 7, DMA_CCR_MSIZE_8BIT);
+ dma_enable_memory_increment_mode(DMA1, 7);
+
+ dma_set_peripheral_size(DMA1, 7, DMA_CCR_PSIZE_16BIT);
+ dma_disable_peripheral_increment_mode(DMA1, 7);
+
+ dma_enable_half_transfer_interrupt(DMA1, 7);
+ dma_enable_transfer_complete_interrupt(DMA1, 7);
+ nvic_enable_irq(NVIC_DMA1_CHANNEL7_IRQ);
+
+ dma_enable_channel(DMA1, 7);
+
+ timer_set_prescaler(TIM4, 0);
+ timer_set_mode(TIM4, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
+ timer_disable_preload(TIM4);
+ timer_set_period(TIM4, NPIX_PERIOD - 1);
+
+ timer_set_oc_mode(TIM4, TIM_OC3, TIM_OCM_PWM1);
+ timer_set_oc_value(TIM4, TIM_OC3, 0);
+ timer_set_oc_polarity_high(TIM4, TIM_OC3);
+ timer_enable_oc_output(TIM4, TIM_OC3);
+
+ timer_set_dma_on_update_event(TIM4);
+ TIM_DIER(TIM4) |= TIM_DIER_UDE;
+
+ timer_enable_counter(TIM4);
+}
+
+static inline void next_led(byte *buf)
+{
+ if (neopixel_index < NPIX_NUM_LEDS) {
+ byte *led = neopixel_leds[neopixel_index++];
+ byte r = led[0], g = led[1], b = led[2];
+ for (uint m=0x80; m; m >>= 1)
+ *buf++ = (g & m) ? B1 : B0;
+ for (uint m=0x80; m; m >>= 1)
+ *buf++ = (r & m) ? B1 : B0;
+ for (uint m=0x80; m; m >>= 1)
+ *buf++ = (b & m) ? B1 : B0;
+ } else {
+ for (uint i=0; i < 24; i++)
+ *buf++ = 0;
+ neopixel_index++;
+ if (neopixel_index == NPIX_NUM_LEDS + NPIX_RESET)
+ neopixel_index = 0;
+ }
+}
+
+void dma1_channel7_isr(void)
+{
+ if (DMA1_ISR & DMA_ISR_HTIF7) {
+ // Half transfer
+ DMA1_IFCR |= DMA_IFCR_CHTIF7;
+ next_led(neopixel_buf);
+ } else if (DMA1_ISR & DMA_ISR_TCIF7) {
+ // Transfer completed
+ DMA1_IFCR |= DMA_IFCR_CTCIF7;
+ next_led(neopixel_buf + 24);
+ }
+}
+
+/*** USB ***/
+
+static usbd_device *usbd_dev;
+
+enum usb_string {
+ STR_MANUFACTURER = 1,
+ STR_PRODUCT,
+ STR_SERIAL,
+};
+
+static char usb_serial_number[13];
+
+static const char *usb_strings[] = {
+ "United Computer Wizards",
+ "X-mas Neopixel Strip",
+ usb_serial_number,
+};
+
+static const struct usb_device_descriptor device = {
+ .bLength = USB_DT_DEVICE_SIZE,
+ .bDescriptorType = USB_DT_DEVICE,
+ .bcdUSB = 0x0200,
+ .bDeviceClass = 0xFF,
+ .bDeviceSubClass = 0,
+ .bDeviceProtocol = 0,
+ .bMaxPacketSize0 = 64,
+ .idVendor = NPIX_USB_VENDOR,
+ .idProduct = NPIX_USB_PRODUCT,
+ .bcdDevice = NPIX_USB_VERSION,
+ .iManufacturer = STR_MANUFACTURER,
+ .iProduct = STR_PRODUCT,
+ .iSerialNumber = STR_SERIAL,
+ .bNumConfigurations = 1,
+};
+
+static const struct usb_endpoint_descriptor endpoints[] = {{
+ // Bulk end-point for sending LED values
+ .bLength = USB_DT_ENDPOINT_SIZE,
+ .bDescriptorType = USB_DT_ENDPOINT,
+ .bEndpointAddress = 0x01,
+ .bmAttributes = USB_ENDPOINT_ATTR_BULK,
+ .wMaxPacketSize = 64,
+ .bInterval = 1,
+}};
+
+static const struct usb_interface_descriptor iface = {
+ .bLength = USB_DT_INTERFACE_SIZE,
+ .bDescriptorType = USB_DT_INTERFACE,
+ .bInterfaceNumber = 0,
+ .bAlternateSetting = 0,
+ .bNumEndpoints = 1,
+ .bInterfaceClass = 0xFF,
+ .bInterfaceSubClass = 0,
+ .bInterfaceProtocol = 0,
+ .iInterface = 0,
+ .endpoint = endpoints,
+};
+
+static const struct usb_dfu_descriptor dfu_function = {
+ .bLength = sizeof(struct usb_dfu_descriptor),
+ .bDescriptorType = DFU_FUNCTIONAL,
+ .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
+ .wDetachTimeout = 255,
+ .wTransferSize = 1024,
+ .bcdDFUVersion = 0x0100,
+};
+
+static const struct usb_interface_descriptor dfu_iface = {
+ .bLength = USB_DT_INTERFACE_SIZE,
+ .bDescriptorType = USB_DT_INTERFACE,
+ .bInterfaceNumber = 1,
+ .bAlternateSetting = 0,
+ .bNumEndpoints = 0,
+ .bInterfaceClass = 0xFE,
+ .bInterfaceSubClass = 1,
+ .bInterfaceProtocol = 1,
+ .iInterface = 0,
+
+ .extra = &dfu_function,
+ .extralen = sizeof(dfu_function),
+};
+
+static const struct usb_interface ifaces[] = {{
+ .num_altsetting = 1,
+ .altsetting = &iface,
+}, {
+ .num_altsetting = 1,
+ .altsetting = &dfu_iface,
+}};
+
+static const struct usb_config_descriptor config = {
+ .bLength = USB_DT_CONFIGURATION_SIZE,
+ .bDescriptorType = USB_DT_CONFIGURATION,
+ .wTotalLength = 0,
+ .bNumInterfaces = 2,
+ .bConfigurationValue = 1,
+ .iConfiguration = 0,
+ .bmAttributes = 0x80,
+ .bMaxPower = 100, // multiplied by 2 mA
+ .interface = ifaces,
+};
+
+static byte usb_configured;
+static uint8_t usbd_control_buffer[64];
+
+static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
+{
+ // Reset to bootloader, which implements the rest of DFU
+ debug_printf("Switching to DFU\n");
+ debug_flush();
+ scb_reset_core();
+}
+
+static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
+ struct usb_setup_data *req,
+ uint8_t **buf UNUSED,
+ uint16_t *len UNUSED,
+ void (**complete)(usbd_device *dev, struct usb_setup_data *req))
+{
+ if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
+ return USBD_REQ_NOTSUPP;
+
+ *complete = dfu_detach_complete;
+ return USBD_REQ_HANDLED;
+}
+
+static byte usb_rx_buf[64];
+static bool got_first_message;
+
+static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
+{
+ // We received a frame from the USB host
+ uint len = usbd_ep_read_packet(dev, 0x01, usb_rx_buf, sizeof(usb_rx_buf));
+ debug_printf("USB: Host sent %u bytes\n", len);
+ byte *rx = usb_rx_buf;
+ if (len < 1)
+ return;
+ uint j = rx[0];
+ for (uint i=1; i+3 <= len && j < NPIX_NUM_LEDS; i += 3)
+ neopixel_set(j++, rx[i], rx[i+1], rx[i+2]);
+ got_first_message = true;
+}
+
+static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
+{
+ usbd_register_control_callback(
+ dev,
+ USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
+ 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;
+}
+
+static void reset_cb(void)
+{
+ debug_printf("USB: Reset\n");
+ usb_configured = 0;
+}
+
+static volatile bool usb_event_pending;
+
+void usb_lp_can_rx0_isr(void)
+{
+ /*
+ * We handle USB in the main loop to avoid race conditions between
+ * USB interrupts and other code. However, we need an interrupt to
+ * up the main loop from sleep.
+ *
+ * We set up only the low-priority ISR, because high-priority ISR handles
+ * only double-buffered bulk transfers and isochronous transfers.
+ */
+ nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
+ usb_event_pending = 1;
+}
+
+static void usb_init(void)
+{
+ // Simulate USB disconnect
+ gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
+ gpio_clear(GPIOA, GPIO11 | GPIO12);
+ delay_ms(100);
+
+ usbd_dev = usbd_init(
+ &st_usbfs_v1_usb_driver,
+ &device,
+ &config,
+ usb_strings,
+ ARRAY_SIZE(usb_strings),
+ usbd_control_buffer,
+ sizeof(usbd_control_buffer)
+ );
+ usbd_register_reset_callback(usbd_dev, reset_cb);
+ usbd_register_set_config_callback(usbd_dev, set_config_cb);
+ usb_event_pending = 1;
+}
+
+/*** Main ***/
+
+int main(void)
+{
+ clock_init();
+ gpio_init();
+ usart_init();
+ tick_init();
+ neopixel_init();
+ usb_init();
+
+ debug_printf("Hello, world!\n");
+
+ uint i=0;
+ u32 last_blink = 0;
+
+ for (;;) {
+ if (ms_ticks - last_blink >= 100) {
+ last_blink = ms_ticks;
+ debug_led_toggle();
+ if (!got_first_message) {
+ neopixel_set(i, 0, 0, 7);
+ i = (i+1) % NPIX_NUM_LEDS;
+ neopixel_set(i, 0, 255, 0);
+ }
+ }
+ if (usb_event_pending) {
+ usbd_poll(usbd_dev);
+ usb_event_pending = 0;
+ nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
+ nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
+ }
+ wait_for_interrupt();
+ }
+
+ return 0;
+}