(home)

Project Log : Arduino USB

Description

Project log for developing USB expansion shield for Arduino and associated code.

See also: Learning About Arduino and AVR-USB

. See also . : Featured as a chapter called Virtual USB Keyboard in the new book Practical Arduino by Jon Oxer complete with readable schematics and all.

Code

Notes

USB lines D+ and D- MUST be wired to the same I/O port. We recommend that D+
triggers the interrupt (best achieved by using INT0 for D+), but it is also
possible to trigger the interrupt from D-. If D- is used, interrupts are also
triggered by SOF packets. D- requires a pullup of 1.5k to +3.5V (and the device
must be powered at 3.5V) to identify as low-speed USB device. A pullup of
1M SHOULD be connected from D+ to +3.5V to prevent interference when no USB
master is connected. We use D+ as interrupt source and not D- because it
does not trigger on keep-alive and RESET states.
Mar  7 04:52:38 localhost kernel: [685132.128973] usb 1-2: new low speed USB device using uhci_hcd and address 9
Mar  7 04:52:38 localhost kernel: [685132.252884] usb 1-2: device descriptor read/64, error -71
Mar  7 04:52:38 localhost kernel: [685132.480711] usb 1-2: device descriptor read/64, error -71
Mar  7 04:52:39 localhost kernel: [685132.696583] usb 1-2: new low speed USB device using uhci_hcd and address 10
Mar  7 04:52:39 localhost kernel: [685132.816506] usb 1-2: device descriptor read/64, error -71
Mar  7 04:52:39 localhost kernel: [685133.040335] usb 1-2: device descriptor read/64, error -71
Mar  7 04:52:39 localhost kernel: [685133.256203] usb 1-2: new low speed USB device using uhci_hcd and address 11
Mar  7 04:52:40 localhost kernel: [685133.663904] usb 1-2: device not accepting address 11, error -71
Mar  7 04:52:40 localhost kernel: [685133.775828] usb 1-2: new low speed USB device using uhci_hcd and address 12
Mar  7 04:52:40 localhost kernel: [685134.183550] usb 1-2: device not accepting address 12, error -71
hardware/tools/avr/bin/avrdude -Chardware/tools/avr/etc/avrdude.conf -v -v -v -v -pm168 -cstk500v1 -P/dev/tty.usbserial-<id> -b19200 -D -Uflash:w:<path>main.hex:i
Bus 001 Device 040: ID 16c0:05dc  
  iManufacturer           1 www.obdev.at
  iProduct                2 PowerSwitch

hardware/tools/avr/bin/avr-g++  -Wall -Os -I. -DUSB_CFG_CLOCK_KHZ=16000  -mmcu=atmega168  -c usbdrvasm.S
hardware/tools/avr/bin/avr-g++  -Wall -Os -I. -DUSB_CFG_CLOCK_KHZ=16000  -mmcu=atmega168  -c usbdrv.c
sudo cat /dev/input/by-path/pci-0000\:00\:14.2-usb-0\:2\:1.0-event- | hexdump
0000000 86c7 47de 05cc 000b 0001 002a 0001 0000
0000010 86c7 47de 05d7 000b 0001 001e 0001 0000
0000020 86c7 47de 05d9 000b 0000 0000 0000 0000
0000030 86c7 47de da7f 000c 0001 002a 0000 0000
0000040 86c7 47de da89 000c 0001 001e 0000 0000
0000050 86c7 47de da8c 000c 0000 0000 0000 0000
0000000 88e5 47de a4c7 000b 0001 002a 0001 0000
0000010 88e5 47de a4d0 000b 0001 0030 0001 0000
0000020 88e5 47de a4d2 000b 0000 0000 0000 0000
0000030 88e5 47de 9ec5 000c 0001 002a 0000 0000
0000040 88e5 47de 9ece 000c 0001 0030 0000 0000
0000050 88e5 47de 9ed1 000c 0000 0000 0000 0000

 	// disable timer 0 overflow interrupt (used for millis)
	TIMSK0&=!(1<<TOIE0);
void delayMs(unsigned int ms) {
   /*
  */ 
  for (int i = 0; i < ms; i++) {
    delayMicroseconds(1000);
  }
}

( 18 February 2009 )

( 23 February 2009 )

--- arduinousb_release_001/libraries/UsbKeyboard/usbconfig.h	2008-08-12 13:38:53.000000000 +1200
+++ arduino-0012/hardware/libraries/UsbKeyboard/usbconfig.h	2009-02-05 17:19:58.000000000 +1300
@@ -27,7 +27,8 @@
 /* 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      3
+#define USB_CFG_DMINUS_BIT      4
+//#define USB_CFG_DMINUS_BIT      3
 /* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.
  * This may be any bit in the port.
  */
@@ -39,13 +40,13 @@

 /* ----------------------- Optional Hardware Config ------------------------ */

-/* #define USB_CFG_PULLUP_IOPORTNAME   D */
+#define USB_CFG_PULLUP_IOPORTNAME   D
 /* If you connect the 1.5k pullup resistor from D- to a port pin instead of
  * V+, you can connect and disconnect the device from firmware by calling
  * the macros usbDeviceConnect() and usbDeviceDisconnect() (see usbdrv.h).
  * This constant defines the port on which the pullup resistor is connected.
  */
-/* #define USB_CFG_PULLUP_BIT          4 */
+#define USB_CFG_PULLUP_BIT          5
 /* This constant defines the bit number in USB_CFG_PULLUP_IOPORT (defined
  * above) where the 1.5k pullup resistor is connected. See description
  * above for details.
--- arduinousb_release_001/libraries/UsbKeyboard/UsbKeyboard.h	2008-08-12 13:38:53.000000000 +1200
+++ arduino-0012/hardware/libraries/UsbKeyboard/UsbKeyboard.h	2009-02-05 14:48:47.000000000 +1300
@@ -7,9 +7,18 @@
 #define __UsbKeyboard_h__

 #include <avr/pgmspace.h>
+#include <avr/interrupt.h>
+#include <string.h>

 #include "usbdrv.h"

+// TODO: Work around Arduino 12 issues better.
+//#include <WConstants.h>
+//#undef int()
+
+typedef uint8_t byte;
+
+
 #define BUFFER_SIZE 4 // Minimum of 2: 1 for modifiers + 1 for keystroke 

 
@@ -122,6 +131,11 @@
     PORTD = 0; // TODO: Only for USB pins?
     DDRD |= ~USBMASK;

+    cli();
+    usbDeviceDisconnect();
+    usbDeviceConnect();
+
+
     usbInit();

     sei();
--- arduinousb_release_001/examples/UsbKeyboardDemo1/UsbKeyboardDemo1.pde	2008-08-12 13:54:45.000000000 +1200
+++ arduinousb_release_001/examples/UsbKeyboardDemo1/UsbKeyboardDemo1.pde	2009-02-23 03:25:29.000000000 +1300
@@ -5,12 +5,25 @@
 void setup() {
   pinMode(BUTTON_PIN, INPUT);
   digitalWrite(BUTTON_PIN, HIGH);
+  
+  	// disable timer 0 overflow interrupt (used for millis)
+	TIMSK0&=!(1<<TOIE0); // ++
+}
+
+void delayMs(unsigned int ms) {
+   /*
+  */ 
+  for (int i = 0; i < ms; i++) {
+    delayMicroseconds(1000);
+  }
 }

 void loop() {

   UsbKeyboard.update();

+  digitalWrite(13, !digitalRead(13));
+
   if (digitalRead(BUTTON_PIN) == 0) {

     //UsbKeyboard.sendKeyStroke(KEY_B, MOD_GUI_LEFT);
@@ -32,7 +45,7 @@

     UsbKeyboard.sendKeyStroke(KEY_ENTER);

-    delay(200);
+    delayMs(200);
    }

 }

( 23 May 2009 )

( 24 May 2009 )

May 24 01:31:54 ComputationDevice kernel[0]: USBF:	118274.167	[0x44d7400] The IOUSBFamily is having trouble enumerating a USB device that has been plugged in.  It will keep retrying.  (Port 2 of hub @ location: 0x1a000000)
May 24 01:32:01 ComputationDevice kernel[0]: USBF:	118280.814	AppleUSBUHCI[0x421a000]::Found a transaction which hasn't moved in 5 seconds on bus 0x1a, timing out! (Addr: 0, EP: 0)
May 24 01:32:05 ComputationDevice kernel[0]: USBF:	118285. 74	[0x44d7400] The IOUSBFamily was not able to enumerate a device.

( 13 September 2009 )

( 16 October 2009 )

( 17 October 2009 )

( 19 October 2009 )

( 20 October 2009 )

( 22 October 2009 )

from arduino.usbdevice import ArduinoUsbDevice

theDevice = ArduinoUsbDevice(idVendor=0x16c0, idProduct=0x05df)
theDevice.write(0x01)
print theDevice.read()
#include <UsbStream.h>
void setup() {
    UsbStream.begin();
    UsbStream.write(0xff);
}

void loop() {
  UsbStream.refresh();

  if (UsbStream.available() > 0) {
    int data = UsbStream.read();
  }
}

( 24 October 2009 )

( 27 October 2009 )

( 6 April 2010 )

( 28 July 2010 )

#ifdef __cplusplus
extern "C"{
#endif
...
#ifdef __cplusplus
} // extern "C"                                                                                                             
#endif

And then from http://forums.obdev.at/viewtopic.php?f=8&t=4022 add this to "usbconfig.h":

#define USB_INTR_VECTOR INT0_vect
code@rancidbacon.com