Okay, the USB stuff works! I can send a command to the device and receive the

reply.
This commit is contained in:
David Given
2018-09-29 12:13:00 +02:00
parent 09ec9065cc
commit 7d98ec7e01
5 changed files with 46 additions and 10 deletions

View File

@@ -62,6 +62,20 @@ CY_ISR(dma_finished_isr)
}
#endif
static void handle_usb_packet(void)
{
static uint8_t buffer[64];
int length = USBFS_GetEPCount(FLUXENGINE_CMD_OUT_EP_NUM);
USBFS_ReadOutEP(FLUXENGINE_CMD_OUT_EP_NUM, buffer, length);
UART_PutString("read packet\r");
while (USBFS_GetEPState(FLUXENGINE_CMD_IN_EP_NUM) != USBFS_IN_BUFFER_EMPTY)
;
char c = 99;
USBFS_LoadInEP(FLUXENGINE_CMD_IN_EP_NUM, (uint8_t*) &c, sizeof(c));
}
int main(void)
{
CyGlobalIntEnable;
@@ -84,10 +98,10 @@ int main(void)
;
UART_PutString("USB ready\r");
//CyDmaChEnable(dma_channel, true);
USBFS_EnableOutEP(FLUXENGINE_OUT_EP);
USBFS_EnableOutEP(FLUXENGINE_CMD_OUT_EP_NUM);
}
if (USBFS_GetEPState(FLUXENGINE_OUT_EP) == USBFS_OUT_BUFFER_FULL)
UART_PutString("packet received!\r");
if (USBFS_GetEPState(FLUXENGINE_CMD_OUT_EP_NUM) == USBFS_OUT_BUFFER_FULL)
handle_usb_packet();
}
}

View File

@@ -15,6 +15,7 @@
extern void error(const char* message, ...);
extern void usb_init(void);
extern void usb_send(void* ptr, int len);
extern void usb_cmd_send(void* ptr, int len);
extern void usb_cmd_recv(void* ptr, int len);
#endif

6
main.c
View File

@@ -45,7 +45,11 @@ int main(int argc, char* const* argv)
{
usb_init();
usb_send("x", 1);
usb_cmd_send("x", 1);
char c;
usb_cmd_recv(&c, 1);
printf("got reply: %d\n", c);
#if 0
parse_options(argc, argv);

View File

@@ -7,11 +7,18 @@ enum
FLUXENGINE_VID = 0xF055,
FLUXENGINE_PID = 0x9973,
/* libusb uses these numbers */
FLUXENGINE_DATA_OUT_EP = 0x01,
FLUXENGINE_DATA_IN_EP = 0x82,
FLUXENGINE_CMD_OUT_EP = 0x03,
FLUXENGINE_CMD_IN_EP = 0x84,
/* the PSoC code uses these, sigh */
FLUXENGINE_DATA_OUT_EP_NUM = FLUXENGINE_DATA_OUT_EP & 0x0f,
FLUXENGINE_DATA_IN_EP_NUM = FLUXENGINE_DATA_IN_EP & 0x0f,
FLUXENGINE_CMD_OUT_EP_NUM = FLUXENGINE_CMD_OUT_EP & 0x0f,
FLUXENGINE_CMD_IN_EP_NUM = FLUXENGINE_CMD_IN_EP & 0x0f,
};
enum

18
usb.c
View File

@@ -1,6 +1,8 @@
#include "globals.h"
#include <libusb.h>
#define TIMEOUT 5000
static libusb_device_handle* device;
void usb_init(void)
@@ -27,10 +29,18 @@ void usb_init(void)
error("could not claim interface: %s", libusb_strerror(i));
}
void usb_send(void* ptr, int len)
void usb_cmd_send(void* ptr, int len)
{
int i = libusb_bulk_transfer(device, FLUXENGINE_DATA_OUT_EP,
ptr, len, &len, 0);
int i = libusb_interrupt_transfer(device, FLUXENGINE_CMD_OUT_EP,
ptr, len, &len, TIMEOUT);
if (i < 0)
error("failed to send request: %s", libusb_strerror(i));
error("failed to send command: %s", libusb_strerror(i));
}
void usb_cmd_recv(void* ptr, int len)
{
int i = libusb_interrupt_transfer(device, FLUXENGINE_CMD_IN_EP,
ptr, len, &len, TIMEOUT);
if (i < 0)
error("failed to receive command reply: %s", libusb_strerror(i));
}