Here is the usbdrv library and an example sketch
http://www.railways-in-miniature.co.uk/electronics/arduino/V-USB%20on%20Arduino.zip
Some notes:
-
I redownloaded the usbdrv files and deleted the ones I modified before - the way I changed them was wrong.
-
I realised that you don't need to rename the files to .cpp, you just have to include the header file as if it were a c type file:
extern "C"{
#include "usbdrv.h"
#if USE_INCLUDE
#include "usbdrv.c"
#endif
}
- There is a difference between the way C and C++ use pointer - specifically the C++ specification doesn't allow for the implicit conversion of (void*) pointers to type pointers (such as (usbRequest_t *)). These must be cast correctly. So in any of the examples where you find anything like:
usbRequest_t *rq = (void*)data;
//or
usbMsgPtr = (void*)&reportBuffer;
It needs to be changed to have the correct cast. In the two cases above it becomes:
usbRequest_t *rq = (usbRequest_t *)data;
//or
usbMsgPtr = (unsigned char *)&reportBuffer;
-
Any implementations the the libraries extern functions must be written within the extern "C" {} block to specify they should be treated as C functions. (See the example sketch)
-
You have to ensure your wiring is correct - USB runs at 3.3v level, which means your IC needs to run at 3.3v, or you need 3.6v zener diodes between D+ and GND, and between D- and GND. In the file above I have drawn the way I wired the board. In my case as I didn't have an zeners to hand, I used a pair of 1N4148 diodes in series with the +5v from the USB port and the arduino mega +5v rail. This forces the mega to run at just under 3.5V which is acceptable.
-
If you use a different IC to the Mega, such as a Tiny chip or an Uno, you will need to use the corrent pins. D+ should be on the pin connected to INT0. D- doesn't matter which pin it is connected to. You also need to specify the port and bits you have chosen in the usbconfig.h file (under the heading "Hardware Config"):
/* ---------------------------- Hardware Config ---------------------------- */
#define USB_CFG_IOPORTNAME D
/* This is the port where the USB bus is connected. When you configure it to
* "B", the registers PORTB, PINB and DDRB will be used.
*/
#define USB_CFG_DMINUS_BIT 1
/* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.
* This may be any bit in the port.
*/
#define USB_CFG_DPLUS_BIT 0
/* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected.
* This may be any bit in the port. Please note that D+ must also be connected
* to interrupt pin INT0! [You can also use other interrupts, see section
* "Optional MCU Description" below, or you can connect D- to the interrupt, as
* it is required if you use the USB_COUNT_SOF feature. If you use D- for the
* interrupt, the USB interrupt will also be triggered at Start-Of-Frame
* markers every millisecond.]
- Then example sketch is a bit of fun. If all goes well when you plug it in, after a couple of seconds you should see the mouse moving round in circles. It may have to install the drivers first (generic USB mouse driver).