]> mj.ucw.cz Git - home-hw.git/commitdiff
Communicating with the host
authorMartin Mares <mj@ucw.cz>
Sun, 24 Jun 2018 15:17:06 +0000 (17:17 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 24 Jun 2018 15:17:06 +0000 (17:17 +0200)
Inc/usb.h
Src/usbdev.c
host/Makefile [new file with mode: 0644]
host/test.c [new file with mode: 0644]

index 114a5378ea81f4e7e2361dfbc60f4f666778ab59..6d5eaf417ba6d195e67a399026bf53d1384200d9 100644 (file)
--- 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;
 }
 
   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);
 }
 {
   return HAL_PCD_EP_Transmit(usb->hpcd, ep_addr, (byte *) buf, size);
 }
index 3e6e76a3bec050b4b4c03265def7d3a73c22c21d..1c05584f5dfdf310ca677db9070ce784b1c08eb1 100644 (file)
@@ -88,6 +88,9 @@ static const byte desc_languages[] = {
 
 /*** Callbacks ***/
 
 
 /*** 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;
 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)
 {
 
 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)
 {
 }
 
 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)
 }
 
 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)
 {
 
 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)
 {
 }
 
 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 (file)
index 0000000..d4da827
--- /dev/null
@@ -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 (file)
index 0000000..d071452
--- /dev/null
@@ -0,0 +1,86 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libusb-1.0/libusb.h>
+
+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<devn; i++)
+    {
+      struct libusb_device_descriptor desc;
+      libusb_device *dev = devlist[i];
+      if (!libusb_get_device_descriptor(dev, &desc))
+       {
+         if (desc.idVendor == 0x4242 && desc.idProduct == 0x0001)
+           {
+             printf("Found device at usb%d.%d\n", libusb_get_bus_number(dev), libusb_get_device_address(dev));
+             // FIXME: Free device list
+             return dev;
+           }
+       }
+    }
+
+  libusb_free_device_list(devlist, 1);
+  fprintf(stderr, "Device not found\n");
+  exit(1);
+}
+
+int main(void)
+{
+  int err;
+  if (err = libusb_init(&usb_ctxt))
+    {
+      fprintf(stderr, "Cannot initialize libusb: error %d\n", err);
+      exit(1);
+    }
+  libusb_set_debug(usb_ctxt, 3);
+
+  libusb_device *dev = find_device();
+
+  if (err = libusb_open(dev, &devh))
+    {
+      fprintf(stderr, "Cannot open device: error %d\n", err);
+      exit(1);
+    }
+  libusb_reset_device(devh);
+  if (err = libusb_claim_interface(devh, 0))
+    {
+      fprintf(stderr, "Cannot claim interface: error %d\n", err);
+      exit(1);
+    }
+
+  for (;;)
+    {
+      unsigned char req[64] = { 1, 2, 3, 4 };
+      int transferred;
+      if (err = libusb_bulk_transfer(devh, 0x01, req, 32, &transferred, 2000))
+       {
+         fprintf(stderr, "Transfer failed: error %d\n", err);
+         exit(1);
+       }
+      printf("Transferred %d bytes\n", transferred);
+
+#if 1
+      unsigned char resp[1000];
+      int received;
+      if (err = libusb_bulk_transfer(devh, 0x81, resp, 1000, &received, 2000))
+       {
+         fprintf(stderr, "Receive failed: error %d\n", err);
+         exit(1);
+       }
+      printf("Received %d bytes [%02x]\n", received, resp[0]);
+#endif
+    }
+
+  return 0;
+}