|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
-
-
- #include "usb_dev.h"
- #include "usb_rawhid.h"
- #include "core_pins.h"
- #include <string.h> // for memcpy()
-
-
- #ifdef RAWHID_INTERFACE
- #if F_CPU >= 20000000
-
- int usb_rawhid_recv(void *buffer, uint32_t timeout)
- {
- usb_packet_t *rx_packet;
- uint32_t begin = millis();
-
- while (1) {
- if (!usb_configuration) return -1;
- rx_packet = usb_rx(RAWHID_RX_ENDPOINT);
- if (rx_packet) break;
- if (millis() - begin > timeout || !timeout) return 0;
- yield();
- }
- memcpy(buffer, rx_packet->buf, RAWHID_RX_SIZE);
- usb_free(rx_packet);
- return RAWHID_RX_SIZE;
- }
-
- int usb_rawhid_available(void)
- {
- uint32_t count;
-
- if (!usb_configuration) return 0;
- count = usb_rx_byte_count(RAWHID_RX_ENDPOINT);
- return count;
- }
-
-
- #define TX_PACKET_LIMIT 4
-
- int usb_rawhid_send(const void *buffer, uint32_t timeout)
- {
- usb_packet_t *tx_packet;
- uint32_t begin = millis();
-
- while (1) {
- if (!usb_configuration) return -1;
- if (usb_tx_packet_count(RAWHID_TX_ENDPOINT) < TX_PACKET_LIMIT) {
- tx_packet = usb_malloc();
- if (tx_packet) break;
- }
- if (millis() - begin > timeout) return 0;
- yield();
- }
- memcpy(tx_packet->buf, buffer, RAWHID_TX_SIZE);
- tx_packet->len = RAWHID_TX_SIZE;
- usb_tx(RAWHID_TX_ENDPOINT, tx_packet);
- return RAWHID_TX_SIZE;
- }
-
- #endif
- #endif
|