Browse Source

RawHID tramsit on Teensy 4.0

main
PaulStoffregen 5 years ago
parent
commit
53fc8d42fc
4 changed files with 47 additions and 5 deletions
  1. +3
    -0
      teensy4/usb.c
  2. +4
    -4
      teensy4/usb_desc.h
  3. +39
    -1
      teensy4/usb_rawhid.c
  4. +1
    -0
      teensy4/usb_rawhid.h

+ 3
- 0
teensy4/usb.c View File

@@ -369,6 +369,9 @@ static void endpoint0_setup(uint64_t setupdata)
#if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
usb_serial_configure();
#endif
#if defined(RAWHID_INTERFACE)
usb_rawhid_configure();
#endif
endpoint0_receive(NULL, 0, 0);
return;
case 0x0880: // GET_CONFIGURATION

+ 4
- 4
teensy4/usb_desc.h View File

@@ -542,17 +542,17 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define RAWHID_INTERFACE 0 // RawHID
#define RAWHID_TX_ENDPOINT 3
#define RAWHID_TX_SIZE 64
#define RAWHID_TX_INTERVAL 1
#define RAWHID_TX_INTERVAL 1 // TODO: is this ok for 480 Mbit speed
#define RAWHID_RX_ENDPOINT 4
#define RAWHID_RX_SIZE 64
#define RAWHID_RX_INTERVAL 1
#define RAWHID_RX_INTERVAL 1 // TODO: is this ok for 480 Mbit speed
#define SEREMU_INTERFACE 1 // Serial emulation
#define SEREMU_TX_ENDPOINT 1
#define SEREMU_TX_SIZE 64
#define SEREMU_TX_INTERVAL 1
#define SEREMU_TX_INTERVAL 1 // TODO: is this ok for 480 Mbit speed
#define SEREMU_RX_ENDPOINT 2
#define SEREMU_RX_SIZE 32
#define SEREMU_RX_INTERVAL 2
#define SEREMU_RX_INTERVAL 2 // TODO: is this ok for 480 Mbit speed
#define ENDPOINT1_CONFIG ENDPOINT_RECEIVE_UNUSED + ENDPOINT_TRANSMIT_INTERRUPT
#define ENDPOINT2_CONFIG ENDPOINT_RECEIVE_INTERRUPT + ENDPOINT_TRANSMIT_UNUSED
#define ENDPOINT3_CONFIG ENDPOINT_RECEIVE_UNUSED + ENDPOINT_TRANSMIT_INTERRUPT

+ 39
- 1
teensy4/usb_rawhid.c View File

@@ -34,11 +34,49 @@
#include <string.h> // for memcpy()
//#include "HardwareSerial.h"

#include "debug/printf.h"

#ifdef RAWHID_INTERFACE // defined by usb_dev.h -> usb_desc.h

#define TX_NUM 4
static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
static uint8_t txbuffer[RAWHID_TX_SIZE * TX_NUM];
static uint8_t tx_head=0;

extern volatile uint8_t usb_configuration;

static void rx_event(transfer_t *t)
{

}

void usb_rawhid_configure(void)
{
printf("usb_rawhid_configure\n");
memset(tx_transfer, 0, sizeof(tx_transfer));
tx_head = 0;
usb_config_tx(RAWHID_TX_ENDPOINT, RAWHID_TX_SIZE, 0, NULL);
usb_config_rx(RAWHID_RX_ENDPOINT, RAWHID_RX_SIZE, 0, rx_event);
}

int usb_rawhid_recv(void *buffer, uint32_t timeout)
{
return -1;
transfer_t *xfer = tx_transfer + tx_head;
uint32_t wait_begin_at = systick_millis_count;

while (1) {
if (!usb_configuration) return -1; // usb not enumerated by host
uint32_t status = usb_transfer_status(xfer);
if (!(status & 0x80)) break; // transfer descriptor ready
if (systick_millis_count - wait_begin_at > timeout) return 0;
yield();
}
uint8_t *txdata = txbuffer + (tx_head * RAWHID_TX_SIZE);
memcpy(txdata, buffer, RAWHID_TX_SIZE);
usb_prepare_transfer(xfer, txdata, RAWHID_TX_SIZE, 0);
usb_transmit(RAWHID_TX_ENDPOINT, xfer);
if (++tx_head >= TX_NUM) tx_head = 0;
return RAWHID_TX_SIZE;
}

int usb_rawhid_available(void)

+ 1
- 0
teensy4/usb_rawhid.h View File

@@ -40,6 +40,7 @@
#ifdef __cplusplus
extern "C" {
#endif
void usb_rawhid_configure(void);
int usb_rawhid_recv(void *buffer, uint32_t timeout);
int usb_rawhid_available(void);
int usb_rawhid_send(const void *buffer, uint32_t timeout);

Loading…
Cancel
Save