Browse Source

RawHID receive on Teensy 4.0

teensy4-core
PaulStoffregen 5 years ago
parent
commit
3b5a6f717f
3 changed files with 31 additions and 10 deletions
  1. +1
    -1
      teensy4/usb.c
  2. +2
    -3
      teensy4/usb_desc.h
  3. +28
    -6
      teensy4/usb_rawhid.c

+ 1
- 1
teensy4/usb.c View File

completestatus &= endpointN_notify_mask; completestatus &= endpointN_notify_mask;
if (completestatus) { if (completestatus) {
int i; // TODO: optimize with __builtin_ctz() int i; // TODO: optimize with __builtin_ctz()
for (i=2; i < NUM_ENDPOINTS; i++) {
for (i=2; i <= NUM_ENDPOINTS; i++) {
if (completestatus & (1 << i)) { // receive if (completestatus & (1 << i)) { // receive
run_callbacks(endpoint_queue_head + i * 2); run_callbacks(endpoint_queue_head + i * 2);
} }

+ 2
- 3
teensy4/usb_desc.h View File

#define RAWHID_RX_SIZE 64 #define RAWHID_RX_SIZE 64
#define RAWHID_RX_INTERVAL 1 // TODO: is this ok for 480 Mbit speed #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 2
#define SEREMU_TX_SIZE 64 #define SEREMU_TX_SIZE 64
#define SEREMU_TX_INTERVAL 1 // TODO: is this ok for 480 Mbit speed #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 // TODO: is this ok for 480 Mbit speed #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 ENDPOINT2_CONFIG ENDPOINT_RECEIVE_INTERRUPT + ENDPOINT_TRANSMIT_INTERRUPT
#define ENDPOINT3_CONFIG ENDPOINT_RECEIVE_UNUSED + ENDPOINT_TRANSMIT_INTERRUPT #define ENDPOINT3_CONFIG ENDPOINT_RECEIVE_UNUSED + ENDPOINT_TRANSMIT_INTERRUPT
#define ENDPOINT4_CONFIG ENDPOINT_RECEIVE_INTERRUPT + ENDPOINT_TRANSMIT_UNUSED #define ENDPOINT4_CONFIG ENDPOINT_RECEIVE_INTERRUPT + ENDPOINT_TRANSMIT_UNUSED



+ 28
- 6
teensy4/usb_rawhid.c View File

static uint8_t txbuffer[RAWHID_TX_SIZE * TX_NUM]; static uint8_t txbuffer[RAWHID_TX_SIZE * TX_NUM];
static uint8_t tx_head=0; static uint8_t tx_head=0;


static transfer_t rx_transfer[1] __attribute__ ((used, aligned(32)));
static uint8_t rx_buffer[RAWHID_RX_SIZE];

extern volatile uint8_t usb_configuration; extern volatile uint8_t usb_configuration;



static void rx_event(transfer_t *t) static void rx_event(transfer_t *t)
{ {
//printf("rx\n");
} }


void usb_rawhid_configure(void) void usb_rawhid_configure(void)
{ {
printf("usb_rawhid_configure\n"); printf("usb_rawhid_configure\n");
memset(tx_transfer, 0, sizeof(tx_transfer)); memset(tx_transfer, 0, sizeof(tx_transfer));
memset(rx_transfer, 0, sizeof(rx_transfer));
tx_head = 0; tx_head = 0;
usb_config_tx(RAWHID_TX_ENDPOINT, RAWHID_TX_SIZE, 0, NULL); usb_config_tx(RAWHID_TX_ENDPOINT, RAWHID_TX_SIZE, 0, NULL);
usb_config_rx(RAWHID_RX_ENDPOINT, RAWHID_RX_SIZE, 0, rx_event); usb_config_rx(RAWHID_RX_ENDPOINT, RAWHID_RX_SIZE, 0, rx_event);
//usb_config_rx(RAWHID_RX_ENDPOINT, RAWHID_RX_SIZE, 0, NULL); // why does this not work?
usb_prepare_transfer(rx_transfer + 0, rx_buffer, RAWHID_RX_SIZE, 0);
usb_receive(RAWHID_RX_ENDPOINT, rx_transfer + 0);
} }


int usb_rawhid_recv(void *buffer, uint32_t timeout) int usb_rawhid_recv(void *buffer, uint32_t timeout)
{
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(rx_transfer);
if (!(status & 0x80)) break; // transfer descriptor ready
if (systick_millis_count - wait_begin_at > timeout) return 0;
yield();
}
memcpy(buffer, rx_buffer, RAWHID_RX_SIZE);
memset(rx_transfer, 0, sizeof(rx_transfer));
usb_prepare_transfer(rx_transfer + 0, rx_buffer, RAWHID_RX_SIZE, 0);
usb_receive(RAWHID_RX_ENDPOINT, rx_transfer + 0);
return RAWHID_RX_SIZE;
}

int usb_rawhid_send(const void *buffer, uint32_t timeout)
{ {
transfer_t *xfer = tx_transfer + tx_head; transfer_t *xfer = tx_transfer + tx_head;
uint32_t wait_begin_at = systick_millis_count; uint32_t wait_begin_at = systick_millis_count;


int usb_rawhid_available(void) int usb_rawhid_available(void)
{ {
if (!usb_configuration) return 0;
if (!(usb_transfer_status(rx_transfer) & 0x80)) return RAWHID_RX_SIZE;
return 0; return 0;
} }


int usb_rawhid_send(const void *buffer, uint32_t timeout)
{
return -1;
}

#endif // RAWHID_INTERFACE #endif // RAWHID_INTERFACE

Loading…
Cancel
Save