USB: use central user input storage

This commit is contained in:
King Kévin 2018-01-24 22:18:58 +01:00
parent 7657812ead
commit 9b025c743e
2 changed files with 4 additions and 37 deletions

View File

@ -231,16 +231,11 @@ static const char *usb_strings[] = {
"CuVoodoo DFU bootloader (runtime mode)",
};
/* input and output ring buffer, indexes, and available memory */
static uint8_t rx_buffer[CDCACM_BUFFER] = {0}; /**< ring buffer for received data */
static volatile uint8_t rx_i = 0; /**< current position of read received data */
static volatile uint8_t rx_used = 0; /**< how much data has been received and not red */
mutex_t rx_lock = MUTEX_UNLOCKED; /**< lock to update rx_i or rx_used */
static uint8_t tx_buffer[CDCACM_BUFFER] = {0}; /**< ring buffer for data to transmit */
/* output ring buffer, index, and available memory */
static uint8_t tx_buffer[64] = {0}; /**< ring buffer for data to transmit */
static volatile uint8_t tx_i = 0; /**< current position if transmitted data */
static volatile uint8_t tx_used = 0; /**< how much data needs to be transmitted */
mutex_t tx_lock = MUTEX_UNLOCKED; /**< lock to update tx_i or tx_used */
volatile uint8_t usb_cdcacm_received = 0; // same as rx_used, but since the user can write this variable we don't rely on it
static bool connected = false; /**< is the USB device is connected to a host */
/** disconnect USB by pulling down D+ to for re-enumerate */
@ -382,11 +377,9 @@ static void usb_cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
/* receive data */
usb_length = usbd_ep_read_packet(usbd_dev, 0x02, usb_data, sizeof(usb_data));
if (usb_length) { // copy received data
for (uint16_t i=0; i<usb_length && rx_used<LENGTH(rx_buffer); i++) { // only until buffer is full
rx_buffer[(rx_i+rx_used)%LENGTH(rx_buffer)] = usb_data[i]; // put character in buffer
rx_used++; // update used buffer
for (uint16_t i=0; i<usb_length; i++) { // only until buffer is full
user_input_store(usb_data[i]); // store user data
}
usb_cdcacm_received = rx_used; // update available data
}
}
@ -444,27 +437,11 @@ void usb_cdcacm_setup(void)
nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ); // enable interrupts (to not have to poll all the time)
// reset buffer states
rx_i = 0;
rx_used = 0;
mutex_unlock(&rx_lock);
usb_cdcacm_received = 0;
tx_i = 0;
tx_used = 0;
mutex_unlock(&tx_lock);
}
char usb_cdcacm_getchar(void)
{
while (!rx_used) { // idle until data is available
__WFI(); // sleep until interrupt (not sure if it's a good idea here)
}
char to_return = rx_buffer[rx_i]; // get the next available character
rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer
rx_used--; // update used buffer
usb_cdcacm_received = rx_used; // update available data
return to_return;
}
void usb_cdcacm_putchar(char c)
{
if (!usb_device || !connected) {

View File

@ -19,18 +19,8 @@
*/
#pragma once
/** transmit and receive buffer sizes */
#define CDCACM_BUFFER 64
/** how many bytes available in the received buffer since last read */
extern volatile uint8_t usb_cdcacm_received;
/** setup USB CDC ACM peripheral */
void usb_cdcacm_setup(void);
/** get character received over USB (blocking)
* @return character received over USB
* @note blocks until character is received over USB when received buffer is empty
*/
char usb_cdcacm_getchar(void);
/** send character over USB (non-blocking)
* @param[in] c character to send
* @note blocks if transmit buffer is full, else puts in buffer and returns