|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
-
-
- #include "usb_dev.h"
- #include "usb_joystick.h"
- #include "core_pins.h"
- #include <string.h> // for memcpy()
- #include "avr/pgmspace.h"
- #include "debug/printf.h"
- #include "core_pins.h"
-
-
- #ifdef JOYSTICK_INTERFACE
-
-
- uint32_t usb_joystick_data[(JOYSTICK_SIZE+3)/4];
-
- static uint8_t transmit_previous_timeout=0;
-
-
- #define TX_TIMEOUT_MSEC 30
-
- #define TX_NUM 4
- #if JOYSTICK_SIZE <= 32
- #define TX_BUFSIZE 32
- #else
- #define TX_BUFSIZE 64
- #endif
- static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
- DMAMEM static uint8_t txbuffer[TX_NUM * TX_BUFSIZE] __attribute__ ((aligned(32)));
- static uint8_t tx_head=0;
- #if JOYSTICK_SIZE > TX_BUFSIZE
- #error "Internal error, transmit buffer size is too small for joystick endpoint"
- #endif
-
-
- void usb_joystick_configure(void)
- {
- memset(tx_transfer, 0, sizeof(tx_transfer));
- tx_head = 0;
- usb_config_tx(JOYSTICK_ENDPOINT, JOYSTICK_SIZE, 0, NULL);
- }
-
-
- int usb_joystick_send()
- {
- if (!usb_configuration) return -1;
- uint32_t head = tx_head;
- transfer_t *xfer = tx_transfer + head;
- uint32_t wait_begin_at = systick_millis_count;
- while (1) {
- uint32_t status = usb_transfer_status(xfer);
- if (!(status & 0x80)) {
- if (status & 0x68) {
-
- printf("ERROR status = %x, i=%d, ms=%u\n",
- status, tx_head, systick_millis_count);
- }
- transmit_previous_timeout = 0;
- break;
- }
- if (transmit_previous_timeout) return -1;
- if (systick_millis_count - wait_begin_at > TX_TIMEOUT_MSEC) {
-
- transmit_previous_timeout = 1;
- return -1;
- }
- if (!usb_configuration) return -1;
- yield();
- }
- delayNanoseconds(30);
- uint8_t *buffer = txbuffer + head * TX_BUFSIZE;
- memcpy(buffer, usb_joystick_data, JOYSTICK_SIZE);
- usb_prepare_transfer(xfer, buffer, JOYSTICK_SIZE, 0);
- arm_dcache_flush_delete(buffer, TX_BUFSIZE);
- usb_transmit(JOYSTICK_ENDPOINT, xfer);
- if (++head >= TX_NUM) head = 0;
- tx_head = head;
- return 0;
- }
-
-
- #endif
|