|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
-
-
- #include "usb_dev.h"
- #include "usb_rawhid.h"
- #include "core_pins.h"
- #include <string.h> // for memcpy()
-
-
- #include "debug/printf.h"
-
- #ifdef RAWHID_INTERFACE
-
- #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)
- {
- transfer_t *xfer = tx_transfer + tx_head;
- uint32_t wait_begin_at = systick_millis_count;
-
- while (1) {
- if (!usb_configuration) return -1;
- uint32_t status = usb_transfer_status(xfer);
- if (!(status & 0x80)) break;
- 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)
- {
- return 0;
- }
-
- int usb_rawhid_send(const void *buffer, uint32_t timeout)
- {
- return -1;
- }
-
- #endif
|