/* Receive data from the host */ static void cdc_bulk_data_out(usbd_device *usbd_dev, uint8_t ep) { uint8_t buf[USB_CDC_MAX_PACKET_SIZE]; uint16_t len = usbd_ep_read_packet(usbd_dev, ep, (void*)buf, sizeof(buf)); if (len > 0 && (cdc_rx_callback != NULL)) { cdc_rx_callback(buf, len); } }
/* Receive data from the host */ static void hid_interrupt_out(usbd_device *usbd_dev, uint8_t ep) { uint8_t buf[USB_HID_MAX_PACKET_SIZE]; uint16_t len = usbd_ep_read_packet(usbd_dev, ep, (void*)buf, sizeof(buf)); if (len > 0 && (hid_report_out_callback != NULL)) { hid_report_out_callback(buf, len); } }
static void tmc_data_rx_cb(usbd_device *usbd_dev, uint8_t ep) { uint8_t buf[64]; (void) ep; int len = usbd_ep_read_packet(usbd_dev, 0x01, buf, 64); /* TODO We actually need a state machine to handle >= than single transfer*/ struct usb_tmc_bulk_header *bhin = (struct usb_tmc_bulk_header *)buf; switch (bhin->MsgID) { case USB_TMC_MSGID_OUT_DEV_DEP_MSG_OUT: printf("dev dep out btag: %d\n", bhin->bTag); /* umm, what? should just drop the packet I guess?*/ #if 0 if (bhin->bTag != ~(bhin->bTagInverse)) { printf("ignoring invalid: bTag != bTagInverse?!!"); return; } #endif /* Could also assert that reserved is zero, but let's forgive things */ if (bhin->command_specific.dev_dep_msg_out.transferSize > 64) { printf("transfer > 1 packet! (UNHANDLED)\n"); return; } if (bhin->command_specific.dev_dep_msg_out.bmTransferAttributes & USB_TMC_BULK_HEADER_BMTRANSFER_ATTRIB_EOM) { /* exit state machine here */ printf("single frame packet :)\n"); } scpi_glue_input(&buf[sizeof(struct usb_tmc_bulk_header)], bhin->command_specific.dev_dep_msg_out.transferSize, true); case USB_TMC_MSGID_OUT_REQUEST_DEV_DEP_MSG_IN: /* WILL need state machiens here too I guess :( */ printf("req_dev_dep_in for max %" PRIu32 " bytes, btag: %d\n", bhin->command_specific.req_dev_dep_msg_in.transferSize, bhin->bTag); if (bhin->command_specific.req_dev_dep_msg_in.bmTransferAttributes & USB_TMC_BULK_HEADER_BMTRANSFER_ATTRIB_TERMCHAR) { printf("FAIL! requested term char!\n"); return; /* TODO reply error? */ } bhin->MsgID = USB_TMC_MSGID_IN_DEV_DEP_MSG_IN; bhin->command_specific.dev_dep_msg_in.transferSize = output_buffer_idx; /* only support short stuff now! */ bhin->command_specific.dev_dep_msg_in.bmTransferAttributes = USB_TMC_BULK_HEADER_BMTRANSFER_ATTRIB_EOM; memcpy(&buf[sizeof(struct usb_tmc_bulk_header) + 1], output_buffer, output_buffer_idx); usbd_ep_write_packet(tmc_dev, 0x82, buf, sizeof(struct usb_tmc_bulk_header) + output_buffer_idx); output_buffer_idx = 0; return; case USB_TMC_MSGID_OUT_VENDOR_SPECIFIC_OUT: printf("vendor_out (UNHANDLED)\n"); return; case USB_TMC_MSGID_OUT_REQUEST_VENDOR_SPECIFIC_IN: printf("req_vendor_in (UNHANDLED)\n"); return; } }
void gdb_usb_out_cb(usbd_device *dev, uint8_t ep) { (void)ep; usbd_ep_nak_set(dev, CDCACM_GDB_ENDPOINT, 1); count_new = usbd_ep_read_packet(dev, CDCACM_GDB_ENDPOINT, double_buffer_out, CDCACM_PACKET_SIZE); if(!count_new) { usbd_ep_nak_set(dev, CDCACM_GDB_ENDPOINT, 0); } }
static void __cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep) { (void)ep; char buf[64]; int len = usbd_ep_read_packet(usbd_dev, 0x01, buf, 64); if (len) { while (usbd_ep_write_packet(usbd_dev, 0x82, buf, len) == 0); } gpio_toggle(GPIOC, GPIO5); }
void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep) { (void)ep; char buf[CDCACM_PACKET_SIZE]; int len = usbd_ep_read_packet(dev, CDCACM_UART_ENDPOINT, buf, CDCACM_PACKET_SIZE); for(int i = 0; i < len; i++) uart_send_blocking(USBUART, buf[i]); }
/* Receive data from the host */ static void vcdc_bulk_data_out(usbd_device *usbd_dev, uint8_t ep) { uint8_t buf[USB_VCDC_MAX_PACKET_SIZE]; uint16_t len = usbd_ep_read_packet(usbd_dev, ep, (void*)buf, sizeof(buf)); uint16_t i; for (i=0; i < len && !vcdc_rx_buffer_full(); i++) { vcdc_rx_buffer_put(buf[i]); } if (len > 0 && (vcdc_rx_callback != NULL)) { vcdc_rx_callback(); } }
static void cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep) { (void)ep; (void)usbd_dev; char buf[64]; int len = usbd_ep_read_packet(usbd_dev, 0x01, buf, 64); if(!handlerp) return; for(int i = 0; i < len; i++) { handlerp->handleByte(buf[i]); } }
static void cdcacm_data_rx_cb(u8 ep) { (void)ep; char buf[64]; int len = usbd_ep_read_packet(0x01, buf, 64); if (len) { rotate_letters(buf, (unsigned int)len); while (usbd_ep_write_packet(0x82, buf, len) == 0) ; } /* flash the LEDs so we know we're doing something */ gpio_toggle(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15); }
unsigned char gdb_if_getchar(void) { while(!(out_ptr < count_out)) { /* Detach if port closed */ if(!cdcacm_get_dtr()) return 0x04; while(cdcacm_get_config() != 1); count_out = usbd_ep_read_packet(usbdev, CDCACM_GDB_ENDPOINT, buffer_out, CDCACM_PACKET_SIZE); out_ptr = 0; } return buffer_out[out_ptr++]; }
static int usb_control_recv_chunk(void) { u16 packetsize = MIN(_usbd_device.desc->bMaxPacketSize0, control_state.req.wLength - control_state.ctrl_len); u16 size = usbd_ep_read_packet(0, control_state.ctrl_buf + control_state.ctrl_len, packetsize); if (size != packetsize) { usbd_ep_stall_set(0, 1); return -1; } control_state.ctrl_len += size; return packetsize; }
void gdb_usb_out_cb(usbd_device *dev, uint8_t ep) { (void)ep; static uint8_t buf[CDCACM_PACKET_SIZE]; usbd_ep_nak_set(dev, CDCACM_GDB_ENDPOINT, 1); uint32_t count = usbd_ep_read_packet(dev, CDCACM_GDB_ENDPOINT, (uint8_t *)buf, CDCACM_PACKET_SIZE); uint32_t idx; for (idx=0; idx<count; idx++) { buffer_out[head_out++ % sizeof(buffer_out)] = buf[idx]; } usbd_ep_nak_set(dev, CDCACM_GDB_ENDPOINT, 0); }
void _usbd_control_out(usbd_device *usbd_dev, uint8_t ea) { (void)ea; switch (usbd_dev->control_state.state) { case DATA_OUT: if (usb_control_recv_chunk(usbd_dev) < 0) { break; } if ((usbd_dev->control_state.req.wLength - usbd_dev->control_state.ctrl_len) <= usbd_dev->desc->bMaxPacketSize0) { usbd_dev->control_state.state = LAST_DATA_OUT; } break; case LAST_DATA_OUT: if (usb_control_recv_chunk(usbd_dev) < 0) { break; } /* * We have now received the full data payload. * Invoke callback to process. */ if (usb_control_request_dispatch(usbd_dev, &(usbd_dev->control_state.req))) { /* Got to status stage on success. */ usbd_ep_write_packet(usbd_dev, 0, NULL, 0); usbd_dev->control_state.state = STATUS_IN; } else { stall_transaction(usbd_dev); } break; case STATUS_OUT: usbd_ep_read_packet(usbd_dev, 0, NULL, 0); usbd_dev->control_state.state = IDLE; if (usbd_dev->control_state.complete) { usbd_dev->control_state.complete(usbd_dev, &(usbd_dev->control_state.req)); } usbd_dev->control_state.complete = NULL; break; default: stall_transaction(usbd_dev); } }
static void gdb_if_update_buf(void) { while (cdcacm_get_config() != 1); #ifdef STM32F4 asm volatile ("cpsid i; isb"); if (count_new) { memcpy(buffer_out, double_buffer_out, count_new); count_out = count_new; count_new = 0; out_ptr = 0; usbd_ep_nak_set(usbdev, CDCACM_GDB_ENDPOINT, 0); } asm volatile ("cpsie i; isb"); #else count_out = usbd_ep_read_packet(usbdev, CDCACM_GDB_ENDPOINT, buffer_out, CDCACM_PACKET_SIZE); out_ptr = 0; #endif }
unsigned char gdb_if_getchar_to(int timeout) { timeout_counter = timeout/100; if(!(out_ptr < count_out)) do { /* Detach if port closed */ if(!cdcacm_get_dtr()) return 0x04; count_out = usbd_ep_read_packet(usbdev, CDCACM_GDB_ENDPOINT, buffer_out, CDCACM_PACKET_SIZE); out_ptr = 0; } while(timeout_counter && !(out_ptr < count_out)); if(out_ptr < count_out) return gdb_if_getchar(); return -1; }
static int usb_control_recv_chunk(usbd_device *usbd_dev) { uint16_t packetsize = MIN(usbd_dev->desc->bMaxPacketSize0, usbd_dev->control_state.req.wLength - usbd_dev->control_state.ctrl_len); uint16_t size = usbd_ep_read_packet(usbd_dev, 0, usbd_dev->control_state.ctrl_buf + usbd_dev->control_state.ctrl_len, packetsize); if (size != packetsize) { stall_transaction(usbd_dev); return -1; } usbd_dev->control_state.ctrl_len += size; return packetsize; }
void _usbd_control_setup(usbd_device *usbd_dev, uint8_t ea) { struct usb_setup_data *req = &usbd_dev->control_state.req; (void)ea; usbd_dev->control_state.complete = NULL; if (usbd_ep_read_packet(usbd_dev, 0, req, 8) != 8) { stall_transaction(usbd_dev); return; } if (req->wLength == 0) { usb_control_setup_read(usbd_dev, req); } else if (req->bmRequestType & 0x80) { usb_control_setup_read(usbd_dev, req); } else { usb_control_setup_write(usbd_dev, req); } }
void _usbd_control_setup(u8 ea) { struct usb_setup_data *req = &control_state.req; (void)ea; control_state.complete = NULL; if (usbd_ep_read_packet(0, req, 8) != 8) { usbd_ep_stall_set(0, 1); return; } if (req->wLength == 0) { usb_control_setup_read(req); } else if (req->bmRequestType & 0x80) { usb_control_setup_read(req); } else { usb_control_setup_write(req); } }
void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep) { (void)ep; char buf[CDCACM_PACKET_SIZE]; int len = usbd_ep_read_packet(dev, CDCACM_UART_ENDPOINT, buf, CDCACM_PACKET_SIZE); #if defined(BLACKMAGIC) /* Don't bother if uart is disabled. * This will be the case on mini while we're being debugged. */ if(!(RCC_APB2ENR & RCC_APB2ENR_USART1EN)) return; #endif gpio_set(LED_PORT_UART, LED_UART); for(int i = 0; i < len; i++) usart_send_blocking(USBUSART, buf[i]); gpio_clear(LED_PORT_UART, LED_UART); }
void _usbd_control_out(u8 ea) { (void)ea; switch (control_state.state) { case DATA_OUT: if (usb_control_recv_chunk() < 0) break; if ((control_state.req.wLength - control_state.ctrl_len) <= _usbd_device.desc->bMaxPacketSize0) control_state.state = LAST_DATA_OUT; break; case LAST_DATA_OUT: if (usb_control_recv_chunk() < 0) break; /* * We have now received the full data payload. * Invoke callback to process. */ if (usb_control_request_dispatch(&control_state.req)) { /* Got to status stage on success. */ usbd_ep_write_packet(0, NULL, 0); control_state.state = STATUS_IN; } else { usbd_ep_stall_set(0, 1); } break; case STATUS_OUT: usbd_ep_read_packet(0, NULL, 0); control_state.state = IDLE; if (control_state.complete) control_state.complete(&control_state.req); control_state.complete = NULL; break; default: usbd_ep_stall_set(0, 1); } }