#ifndef __USBAPI__
#define __USBAPI__
#include <inttypes.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <util/delay.h>
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned long u32;
#include "Arduino.h"
#if defined(USBCON)
#include "USBDesc.h"
#include "USBCore.h"
//================================================================================
//================================================================================
// USB
class USBDevice_
{
public:
USBDevice_();
bool configured();
void attach();
void detach(); // Serial port goes down too...
void poll();
};
extern USBDevice_ USBDevice;
I am trying to create my own USBDevice subclass so I can use the USBAPI functions like, USB_Send as well as the HID functions defined in HID.cpp. But no matter what I do it wont let me call the class USBDevice and I think it's because I need to define USBCON as 'something'... but I have no idea what.
Here is my current .ino but ive tried so many different things to get access to the USBAPI. I also Have my HID report file below.
I don't know anything about usbapi. Is google broken in your country ? A 2 minute consultation with Dr Google seems to suggest that there are quite a few examples out there.
The code you posted seems to be invalid. There is no #endif matching the #if defined(USBCON)
The exact purpose of that USBCON eludes me.
However, as a general concept, it isn't necessarily necessary to give these #define'd parameters an actual value. Merely adding the statement
#define USBCON
will cause that parameter to be defined, and then the following code will be included. It doesn't actually have to have a "value".
I have already tried the adding the obvious #define USBCON to my .ino but to no avail. One of the reasons I was thinking that this function needs to be defined as 'something' is because it isnt the usual #ifdef function... That kind of threw me off even though everything I've read says that the two are basically the same function. THe other reason I think that USBCON needs to be defined as something is because when I trace the definition ('right click' > 'go to definition' via the STINO plugin with simple text 3) it takes me to the file iom16u2.h. Which I have no idea wtf that header is about. And in that header is defined:
The other reason I think USBCON needs to be defined as something is because some of the functions that I want to use, which are defined in the USBCore.cpp file, like:
int USB_Send(u8 ep, const void* d, int len)
{
if (!_usbConfiguration)
return -1;
int r = len;
const u8* data = (const u8*)d;
u8 timeout = 250; // 250ms timeout on send? TODO
while (len)
{
u8 n = USB_SendSpace(ep);
if (n == 0)
{
if (!(--timeout))
return -1;
delay(1);
continue;
}
if (n > len)
n = len;
{
LockEP lock(ep);
// Frame may have been released by the SOF interrupt handler
if (!ReadWriteAllowed())
continue;
len -= n;
if (ep & TRANSFER_ZERO)
{
while (n--)
Send8(0);
}
else if (ep & TRANSFER_PGM)
{
while (n--)
Send8(pgm_read_byte(data++));
}
else
{
while (n--)
Send8(*data++);
}
if (!ReadWriteAllowed() || ((len == 0) && (ep & TRANSFER_RELEASE))) // Release full buffer
ReleaseTX();
}
}
TXLED1; // light the TX LED
TxLEDPulse = TX_RX_LED_PULSE_MS;
return r;
}
extern const u8 _initEndpoints[] PROGMEM;
const u8 _initEndpoints[] =
{
0,
#ifdef CDC_ENABLED
EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
#endif
#ifdef HID_ENABLED
EP_TYPE_INTERRUPT_IN // HID_ENDPOINT_INT
#endif
};
#define EP_SINGLE_64 0x32 // EP0
#define EP_DOUBLE_64 0x36 // Other endpoints
static
void InitEP(u8 index, u8 type, u8 size)
{
UENUM = index;
UECONX = 1;
UECFG0X = type;
UECFG1X = size;
}
static
void InitEndpoints()
{
for (u8 i = 1; i < sizeof(_initEndpoints); i++)
{
UENUM = i;
UECONX = 1;
UECFG0X = pgm_read_byte(_initEndpoints+i);
UECFG1X = EP_DOUBLE_64;
}
UERST = 0x7E; // And reset them
UERST = 0;
}
// Handle CLASS_INTERFACE requests
static
bool ClassInterfaceRequest(Setup& setup)
{
u8 i = setup.wIndex;
#ifdef CDC_ENABLED
if (CDC_ACM_INTERFACE == i)
return CDC_Setup(setup);
#endif
#ifdef HID_ENABLED
if (HID_INTERFACE == i)
return HID_Setup(setup);
#endif
return false;
}
int _cmark;
int _cend;
call and declare variables which are inside that iom16u2.h file, the purpose of which confuses me.
USBCON will be defined in the mcu header file if the mcu has USB hardware. It is a register to configure the USB hardware, the definition is the register address. Without the mcu hardware I doubt the USB API will be of much use.
I suggest get started by trying out in the Arduino IDE, under File, Examples, 09.USB there are several sketches which use USB. Then you can build on that.
A typical processor for those programs in Arduino land is the ATmega32U4 which is on board the Arduino Leonardo, Pro Micro, Micro, and a few other models. I have heard trying to implement USB on a processor without native USB hardware is difficult.