Browse Source

RawHID tramsit on Teensy 4.0

teensy4-core
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

#if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE) #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
usb_serial_configure(); usb_serial_configure();
#endif #endif
#if defined(RAWHID_INTERFACE)
usb_rawhid_configure();
#endif
endpoint0_receive(NULL, 0, 0); endpoint0_receive(NULL, 0, 0);
return; return;
case 0x0880: // GET_CONFIGURATION case 0x0880: // GET_CONFIGURATION

+ 4
- 4
teensy4/usb_desc.h View File

#define RAWHID_INTERFACE 0 // RawHID #define RAWHID_INTERFACE 0 // RawHID
#define RAWHID_TX_ENDPOINT 3 #define RAWHID_TX_ENDPOINT 3
#define RAWHID_TX_SIZE 64 #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_ENDPOINT 4
#define RAWHID_RX_SIZE 64 #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_INTERFACE 1 // Serial emulation
#define SEREMU_TX_ENDPOINT 1 #define SEREMU_TX_ENDPOINT 1
#define SEREMU_TX_SIZE 64 #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_ENDPOINT 2
#define SEREMU_RX_SIZE 32 #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 ENDPOINT1_CONFIG ENDPOINT_RECEIVE_UNUSED + ENDPOINT_TRANSMIT_INTERRUPT
#define ENDPOINT2_CONFIG ENDPOINT_RECEIVE_INTERRUPT + ENDPOINT_TRANSMIT_UNUSED #define ENDPOINT2_CONFIG ENDPOINT_RECEIVE_INTERRUPT + ENDPOINT_TRANSMIT_UNUSED
#define ENDPOINT3_CONFIG ENDPOINT_RECEIVE_UNUSED + ENDPOINT_TRANSMIT_INTERRUPT #define ENDPOINT3_CONFIG ENDPOINT_RECEIVE_UNUSED + ENDPOINT_TRANSMIT_INTERRUPT

+ 39
- 1
teensy4/usb_rawhid.c View File

#include <string.h> // for memcpy() #include <string.h> // for memcpy()
//#include "HardwareSerial.h" //#include "HardwareSerial.h"


#include "debug/printf.h"

#ifdef RAWHID_INTERFACE // defined by usb_dev.h -> usb_desc.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) 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) int usb_rawhid_available(void)

+ 1
- 0
teensy4/usb_rawhid.h View File

#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void usb_rawhid_configure(void);
int usb_rawhid_recv(void *buffer, uint32_t timeout); int usb_rawhid_recv(void *buffer, uint32_t timeout);
int usb_rawhid_available(void); int usb_rawhid_available(void);
int usb_rawhid_send(const void *buffer, uint32_t timeout); int usb_rawhid_send(const void *buffer, uint32_t timeout);

Loading…
Cancel
Save