/*
* Monolithic Arduino VUSB CDC implementation
* Combines VUSB_CDC, ringBuffer8, and usbdrv functionality.
* Includes V-USB core definitions inline.
*/
// Includes for Arduino compatibility
#include "Arduino.h"
/* VUSB Core Definitions - usbdrv.h */
/* Minimal USB driver header based on V-USB */
#define USB_PUBLIC static
#define usbMsgPtr_t unsigned char*
// Device request codes
#define USBRQ_TYPE_MASK 0x60
#define USBRQ_TYPE_STANDARD (0x00 << 5)
#define USBRQ_TYPE_CLASS (0x01 << 5)
#define USBRQ_TYPE_VENDOR (0x02 << 5)
#define USBRQ_TYPE_RESERVED (0x03 << 5)
#define USBRQ_DIR_MASK 0x80
#define USBRQ_DIR_HOST_TO_DEVICE 0x00
#define USBRQ_DIR_DEVICE_TO_HOST 0x80
// Declare USB functions (simulate usbdrv.h and usbdrv.c behavior)
void usbInit() {
// Initialize USB hardware (stub for simplicity)
}
void usbPoll() {
// Poll USB events (stub for simplicity)
}
// USB functions used in the CDC implementation
#define usbFunctionSetup(data) 0 // Stub
#define usbFunctionRead() 0 // Stub
#define usbFunctionWrite() 0 // Stub
/* CDC Implementation - VUSB_CDC.h */
#define HW_CDC_BUF_SIZE 24
#define HW_CDC_BULK_OUT_SIZE 8
#define HW_CDC_BULK_IN_SIZE 8
// Ring buffer implementation
class RingBuffer_t {
uint8_t buffer[HW_CDC_BUF_SIZE];
uint8_t head;
uint8_t tail;
public:
RingBuffer_t() : head(0), tail(0) {}
void put(uint8_t data) {
uint8_t next = (head + 1) % HW_CDC_BUF_SIZE;
if (next != tail) { // Prevent overwriting
buffer[head] = data;
head = next;
}
}
int16_t get() {
if (head == tail) return -1; // Empty
uint8_t value = buffer[tail];
tail = (tail + 1) % HW_CDC_BUF_SIZE;
return value;
}
bool isEmpty() {
return head == tail;
}
};
static RingBuffer_t rxBuf;
static RingBuffer_t txBuf;
// VUSB CDC class
class VUSB_CDCDevice : public Stream {
public:
void begin() {
usbInit();
usbPoll();
}
void end() {}
void refresh() {
usbPoll();
}
virtual int available() {
return !rxBuf.isEmpty();
}
virtual int peek() {
// No direct support; user should implement this if needed
return -1;
}
virtual int read() {
return rxBuf.get();
}
virtual void flush() {}
virtual size_t write(uint8_t c) {
txBuf.put(c);
return 1;
}
size_t write1(uint8_t c) {
return write(c);
}
uint8_t WaitTxEmpty() {
// Simulate waiting for TX buffer
while (!txBuf.isEmpty()) {}
return 0;
}
using Print::write;
};
VUSB_CDCDevice Serial_VUSB;
/* Main Arduino Sketch */
void setup() {
Serial_VUSB.begin();
}
void loop() {
Serial_VUSB.refresh();
if (Serial_VUSB.available()) {
int c = Serial_VUSB.read();
Serial_VUSB.write(c); // Echo back received data
}
}