From: Martin Mares Date: Sun, 24 Jun 2018 15:17:06 +0000 (+0200) Subject: Communicating with the host X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=c4a32db300dc148fc71dcdf74c143fb0c61c0a35;p=home-hw.git Communicating with the host --- diff --git a/Inc/usb.h b/Inc/usb.h index 114a537..6d5eaf4 100644 --- a/Inc/usb.h +++ b/Inc/usb.h @@ -211,7 +211,7 @@ static inline int usb_ep_is_stalled(struct usb *usb, byte ep_addr) return ((ep_addr & 0x80) ? usb->hpcd->IN_ep : usb->hpcd->OUT_ep) [ep_addr & 0x7f].is_stall; } -static inline HAL_StatusTypeDef usb_ep_transmit(struct usb *usb, byte ep_addr, const byte *buf, u32 size) +static inline HAL_StatusTypeDef usb_ep_send(struct usb *usb, byte ep_addr, const byte *buf, u32 size) { return HAL_PCD_EP_Transmit(usb->hpcd, ep_addr, (byte *) buf, size); } diff --git a/Src/usbdev.c b/Src/usbdev.c index 3e6e76a..1c05584 100644 --- a/Src/usbdev.c +++ b/Src/usbdev.c @@ -88,6 +88,9 @@ static const byte desc_languages[] = { /*** Callbacks ***/ +static byte usb_rx_buf[64]; +static byte usb_tx_buf[64]; + void usb_dev_reset(struct usb *usb) { usb->desc_device = desc_device; @@ -102,10 +105,15 @@ void usb_dev_reset(struct usb *usb) void usb_dev_configure(struct usb *usb) { + usb_ep_open(usb, 0x01, USB_EP_TYPE_BULK, 64); + usb_ep_open(usb, 0x81, USB_EP_TYPE_BULK, 64); + usb_ep_receive(usb, 0x01, usb_rx_buf, 64); } void usb_dev_unconfigure(struct usb *usb) { + usb_ep_close(usb, 0x01); + usb_ep_close(usb, 0x81); } bool usb_dev_setup_hook(struct usb *usb, struct setup_request *setup) @@ -123,8 +131,14 @@ void usb_dev_ctl_send_done(struct usb *usb) void usb_dev_recv_done(struct usb *usb, byte epnum) { + if (epnum == 0x01) + { + usb_tx_buf[0]++; + usb_ep_send(usb, 0x81, usb_tx_buf, 33); + } } void usb_dev_send_done(struct usb *usb, byte epnum) { + usb_ep_receive(usb, 0x01, usb_rx_buf, 64); } diff --git a/host/Makefile b/host/Makefile new file mode 100644 index 0000000..d4da827 --- /dev/null +++ b/host/Makefile @@ -0,0 +1,6 @@ +CFLAGS=-std=gnu99 -O2 -Wall -Wextra -Wno-parentheses +LDLIBS=-lusb-1.0 + +all: test + +test: test.c diff --git a/host/test.c b/host/test.c new file mode 100644 index 0000000..d071452 --- /dev/null +++ b/host/test.c @@ -0,0 +1,86 @@ +#include +#include +#include + +struct libusb_context *usb_ctxt; +struct libusb_device_handle *devh; + +static libusb_device *find_device(void) +{ + libusb_device **devlist; + ssize_t devn = libusb_get_device_list(usb_ctxt, &devlist); + if (devn < 0) + { + fprintf(stderr, "Cannot enumerate USB devices: error %d\n", (int) devn); + exit(1); + } + + for (ssize_t i=0; i