USB Serial firmware for ATmega32U4


This post shows how to create USB Serial firmware for the ATmega32U4 found on Adrafruit’s excellent breakout board, using Atmel Studio 7. The design of the breakout board is available at GitHub, so is the Fritzing part used in the figure below. The source code of the USB Serial firmware discussed here can also be forked at GitHub.

Screen Shot 2016-10-28 at 10.02.24.png

Source code

Creation of source code with Atmel Studio 7 is described in post Arduino USB Serial firmware from scratch. Choose adafruit_u4 as the board for LUFA Board Support (driver).

The code has been adapted to blink the same LED when receiving and transmitting data, because the breakout board has just one programmable LED.

Flash using JTAGICE3

See the wiring diagram above to see how JTAGICE3 can be wired to the ICSP header on the breakout board. The Device Programming dialog can then be used to program flash memory on the MCU as shown below.

screen-shot-2016-10-28-at-10-27-19.png

Test

The converter can be tested by using another USB Serial converter connected to RX and TX wires shown in the wiring diagram. Note that the wire ending with TX should be connected to RX on the other converter, similarly the wire ending with RX should be connected to TX.

Troubleshooting tips

The firmware requires that the host send SetLineCoding request to set the baud rate, as described in Universal Serial Bus Communications Class Subclass Specification for PSTN Devices. If the host fails to do that, the serial port will not get initialized, and data cannot be received from or sent to the host.

The breakout board ships from Adafruit in USB powered mode. That makes it ideal as a USB Serial adapter because it can be powered from the PC it is plugged into. If your USB host device does not provide enough current on VBUS, you can cut the VCC solder jumper on the other side, and provide 3.3V at the VCC header pin.

Screen Shot 2016-11-14 at 12.11.11.png

Atmel has a detailed application note on USB that has recommendations you should take into consideration in your designs.

Arduino USB Serial firmware from scratch


I have need to change the USB descriptors of the Arduino Uno R3, for a project that will have it acting as a peripheral to another device.

This post documents how you can make from scratch the USB Serial firmware for ATmega16U2 on the Arduino Uno R3, using Atmel Studio 7 and its LUFA Library extension. It can be flashed quite easily using the built-in DFU firmware and Atmel’s FLIP tool. Source code is available at GitHub.

Install LUFA Library extension if not already installed (Tools -> Extensions and Updates…).

lufa-install

Source files that perform the actual USB/serial proxying are reused from USB to Serial Converter example project for at90usb1287 (File -> New -> Example Project…). Go ahead and create that project and grab the files. Some additions to the USB/serial code such as allowing a sketch to be uploaded from the Arduino IDE, have been taken from the official source code at GitHub.

usb-serial-lufa.png

Create a new project for the ATmega16U2 (File -> New -> Project…).

usb-serial-project.png

usb-serial-project-board.png

Use ASF Wizard (ASF -> ASF Wizard) to add LUFA modules and remove the Generic board support (driver) module.

usb-serial-asf-wizard.png

Configure GCC symbols (Project -> Properties). Change BOARD to BOARD_UNO, and add F_CPU=16000000UL, and F_USB=16000000UL.

usb-serial-symbols.png

Resolve build errors by mostly eliminating code. Source code that builds without errors is available for comparison at GitHub. Program (Tools -> Device Programming) the firmware using the FLIP tool.

usb-serial-flip

Reboot the Arduino.

Arduino USB DFU firmware from scratch


The Device Firmware Upgrade (DFU) firmware for the ATmega16U2 on Arduino UNO R3 is used to flash the USB Serial firmware, among others, using Atmel’s FLIP tool.

This post documents how you can build from scratch DFU firmware using Atmel Studio 7 and its LUFA Library extension. You’ll need an ISP/ICSP programmer to program the ATmega16U2.

You can install the LUFA extension from the Extensions and Updates dialog (Tools -> Extensions and Updates).

lufa-install.png

Create a new project based on the DFU Bootloader example (File -> New -> Example Project…).

dfu-lufa-example.png

Change the compiler optimization setting to -Os in toolchain properties (Project -> Properties).

dfu-optimize-for-size.png

Build the solution and flash (Tools -> Device Programming) using an ISP/ICSP programmer connected to the ICSP2 header.

dfu-jtagice3.png

 

USB Descriptors of Arduino UNO


I’ve been studying the USB interface of Arduino UNO so that I can interface it to an embedded host. It appears as a serial port on Linux, OS X, and Windows 10, without need for custom drivers. The Arduino IDE can reprogram the device over the serial port. Makers also use it to output debug information.

uno.png

 

Here’s the detailed device descriptor as seen on Linux with lsusb -v

Bus 001 Device 005: ID 2341:0043 Arduino SA Uno R3 (CDC ACM)
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass            2 Communications
  bDeviceSubClass         0
  bDeviceProtocol         0
  bMaxPacketSize0         8
  idVendor           0x2341 Arduino SA
  idProduct          0x0043 Uno R3 (CDC ACM)
  bcdDevice            0.01
  iManufacturer           1
  iProduct                2
  iSerial               220
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           62
    bNumInterfaces          2
    bConfigurationValue     1
    iConfiguration          0
    bmAttributes         0xc0
      Self Powered
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         2 Communications
      bInterfaceSubClass      2 Abstract (modem)
      bInterfaceProtocol      1 AT-commands (v.25ter)
      iInterface              0
      CDC Header:
        bcdCDC               10.01
      CDC ACM:
        bmCapabilities       0x06
          sends break
          line coding and serial state
      CDC Union:
        bMasterInterface        0
        bSlaveInterface         1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0008  1x 8 bytes
        bInterval             255
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass        10 CDC Data
      bInterfaceSubClass      0 Unused
      bInterfaceProtocol      0
      iInterface              0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x04  EP 4 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               1

Interface descriptor with class Data Interface Class (bInterfaceClass=0x0A) is used for serial communications. Endpoint with bEndpointAddress 0x03 is used for input, and bEndpointAddress 0x04 for output.

Arduino Uno bootloader programming using JTAGICE mkII


Arduino Uno comes with an ATmega328 microcontroller. Like all AVR MCUs it can be programmed using an in-system programming interface (ISP or ICSP). This can be useful to upgrade the bootloader, or to completely replace it and use the full program space on the MCU.

I have a JTAGICE mkII at work that I use to program AVR32 MCUs. It supports  programming the Arduino Uno using the wiring shown below.

Screen Shot 2016-10-28 at 09.25.38.png

The ATmega16U2 that drives the USB to serial interface – within orange rectangle in the figure above – can also be reprogrammed using the ICSP2 header.

The Arduino can also be programmed using the more affordable JTAGICE3.

Getting Started with the ESP-03


The ESP-03 is a very affordable Wi-Fi module built around the ESP8266EX chip by Espressif. The ESP8266 has become very popular among makers who want to add wireless smarts to things at home and work.

The ESP-03 has two useful modes of operation that can be initiated by controlling its GPIO pins – normal mode and flash mode.

Normal mode wiring is show in the figure below. ESP-03 shown here is powered using SparkFun’s FTDI Basic Breakout – 3.3V – USB to serial module.

Normal Mode

In this mode, ESP-03 executes firmware programmed to the SPI Flash. SPI Flash is an external NOR Flash chip where program instructions are stored, and retrieved during execution.

The ESP-03 has a 4 Mbit 25Q40BT part which allows for 512 KB of program space. Of that, about 423 KB is available for your own programs.

Serial Flash Chip

In flash mode, new program instructions can be flashed to SPI Flash, using tools such as the ESP Flash Download Tool. The wiring is similar to that for normal mode, with the addition of GPIO0 connected to GND.

Flash Mode

ESP8266 can be programmed using an SDK distributed by Espressif. Popular embedded development platforms such as the Arduino IDE, MicroPython, and Lua can also be used.

Adafruit provides instructions for configuring the Arduino IDE for ESP8266 development. Here’re the settings I use with the Arduino IDE – under Tools menu.

Arduino Settings

Try the example project under File -> Examples -> ESP8266WiFi. With it, you’ll be controlling a GPIO pin on the ESP-03, over your Wi-Fi network, in no time at all.

wifi-web-server.gif
Wi-Fi Web Server

Programming an Arduino using BlocklyDuino


Blockly is an experimental drag-and-drop programming environment not unlike Scratch. BlocklyDuino is based on Blockly and provides an open source drag-and-drop programming editor for Arduino. It doesn’t yet build the resulting code, but you can copy-paste it into the Arduino IDE.

Here’s something quick I built to try it out. If an input digital pin is logic high, it prints text to the serial port.

BlocklyDuino Editor

Try and make it yourself, or open the following XML using BlocklyDuino’s Load XML feature.

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="controls_if" inline="false" x="59" y="70">
    <value name="IF0">
      <block type="logic_compare" inline="true">
        <title name="OP">EQ</title>
        <value name="A">
          <block type="inout_digital_read">
            <title name="PIN">2</title>
          </block>
        </value>
        <value name="B">
          <block type="inout_highlow">
            <title name="BOOL">HIGH</title>
          </block>
        </value>
      </block>
    </value>
    <statement name="DO0">
      <block type="serial_print" inline="false">
        <value name="CONTENT">
          <block type="text">
            <title name="TEXT">Hello, World!</title>
          </block>
        </value>
      </block>
    </statement>
  </block>
</xml>

You can copy-paste code from BlocklyDuino’s Arduino tab into the Arduino IDE.

BlocklyDuino Arduino Tab

You can then use Arduino IDE to verify it, and upload.

Arduino IDE with code copied from BlocklyDuino

Toggle Pin 2 between 3.3 (Arduino Pro) or 5 volts and GND with a wire. You should see text appear in the serial port output when the pin is high.