소스 검색

Merge pull request #455 from KurtE/t4_rawhid

RX with 4 transfers using similar code to seremu
teensy4-core
Paul Stoffregen 4 년 전
부모
커밋
9460296113
No account linked to committer's email address
3개의 변경된 파일73개의 추가작업 그리고 20개의 파일을 삭제
  1. +1
    -0
      teensy4/usb.c
  2. +12
    -0
      teensy4/usb_inst.cpp
  3. +60
    -20
      teensy4/usb_rawhid.c

+ 1
- 0
teensy4/usb.c 파일 보기

@@ -3,6 +3,7 @@
#include "usb_desc.h"
#include "usb_serial.h"
#include "usb_seremu.h"
#include "usb_rawhid.h"
#include "usb_keyboard.h"
#include "usb_mouse.h"
#include "usb_joystick.h"

+ 12
- 0
teensy4/usb_inst.cpp 파일 보기

@@ -39,6 +39,18 @@ usb_serial_class Serial;
#endif
#endif

#ifdef CDC2_DATA_INTERFACE
#ifdef CDC2_STATUS_INTERFACE
usb_serial2_class SerialUSB1;
#endif
#endif

#ifdef CDC3_DATA_INTERFACE
#ifdef CDC3_STATUS_INTERFACE
usb_serial3_class SerialUSB2;
#endif
#endif

#ifdef MIDI_INTERFACE
usb_midi_class usbMIDI;
#endif

+ 60
- 20
teensy4/usb_rawhid.c 파일 보기

@@ -30,6 +30,7 @@

#include "usb_dev.h"
#include "usb_rawhid.h"
#include "avr/pgmspace.h" // for PROGMEM, DMAMEM, FASTRUN
#include "core_pins.h" // for yield(), millis()
#include <string.h> // for memcpy()
//#include "HardwareSerial.h"
@@ -40,47 +41,84 @@

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

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

#define RX_NUM 4
static transfer_t rx_transfer[RX_NUM] __attribute__ ((used, aligned(32)));
DMAMEM static uint8_t rx_buffer[RAWHID_RX_SIZE * RX_NUM] __attribute__ ((aligned(32)));
static volatile uint8_t rx_head;
static volatile uint8_t rx_tail;
static uint8_t rx_list[RX_NUM + 1];
static volatile uint32_t rx_available;
static void rx_queue_transfer(int i);
static void rx_event(transfer_t *t);
extern volatile uint8_t usb_configuration;


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

void usb_rawhid_configure(void)
{
printf("usb_rawhid_configure\n");
memset(tx_transfer, 0, sizeof(tx_transfer));
memset(rx_transfer, 0, sizeof(rx_transfer));
tx_head = 0;
rx_head = 0;
rx_tail = 0;
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, 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 i;
for (i=0; i < RX_NUM; i++) rx_queue_transfer(i);
}

/*************************************************************************/
/** Receive **/
/*************************************************************************/

static void rx_queue_transfer(int i)
{
void *buffer = rx_buffer + i * RAWHID_RX_SIZE;
arm_dcache_delete(buffer, RAWHID_RX_SIZE);
//memset(buffer, )
NVIC_DISABLE_IRQ(IRQ_USB1);
usb_prepare_transfer(rx_transfer + i, buffer, RAWHID_RX_SIZE, i);
usb_receive(RAWHID_RX_ENDPOINT, rx_transfer + i);
NVIC_ENABLE_IRQ(IRQ_USB1);
}

static void rx_event(transfer_t *t)
{
int i = t->callback_param;
//printf("rx event i=%d\n", i);
// received a packet with data
uint32_t head = rx_head;
if (++head > RX_NUM) head = 0;
rx_list[head] = i;
rx_head = head;
}


int usb_rawhid_recv(void *buffer, uint32_t timeout)
{
uint32_t wait_begin_at = systick_millis_count;
uint32_t tail = rx_tail;
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;
if (tail != rx_head) break;
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);
// digitalWriteFast(0, LOW);
if (++tail > RX_NUM) tail = 0;
uint32_t i = rx_list[tail];
rx_tail = tail;

memcpy(buffer, rx_buffer + i * RAWHID_RX_SIZE, RAWHID_RX_SIZE);
rx_queue_transfer(i);
//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;
}

@@ -98,6 +136,7 @@ int usb_rawhid_send(const void *buffer, uint32_t timeout)
}
uint8_t *txdata = txbuffer + (tx_head * RAWHID_TX_SIZE);
memcpy(txdata, buffer, RAWHID_TX_SIZE);
arm_dcache_flush_delete(txdata, SEREMU_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;
@@ -107,7 +146,8 @@ int usb_rawhid_send(const void *buffer, uint32_t timeout)
int usb_rawhid_available(void)
{
if (!usb_configuration) return 0;
if (!(usb_transfer_status(rx_transfer) & 0x80)) return RAWHID_RX_SIZE;
if (rx_head != rx_tail) return RAWHID_RX_SIZE;
//if (!(usb_transfer_status(rx_transfer) & 0x80)) return RAWHID_RX_SIZE;
return 0;
}


Loading…
취소
저장