Table of Contents

[hemmerling] AVR Microcontrollers Expertness 4/4 - USB Communications Device Class ( USB CDC )

Related pages:

1 Objective

2 Hardware and Software Requirements

2.1 Atmel Board

  1. Atmel AVR XMEGA-A3BU Xplained kit, with LCD ( 128×32 pixels ), light sensor, and ntc sensor and with ATxmega256A3BU 8-bit CPU ( 256 KBytes Flash, 16 Kbytes SRAM, 4 KBytes EEPROM ).
    • From point of view of the Atmel CPU, the interface is a built-in USB device.
    • The ASF software module “USB CDC ( Single Interface Device )” as communication interface.
    • The optional ASF software module “UTILITY - Unit test framework (Driver)” as unit testing framework.
  2. An in-circuit debugger ( e.g. JTAGICE 3 ) - just for software development and code upload, not necessary for execution.

2.2 Windows Host Computer

  1. A host computer, running WinXP, Vista, Win7.
  2. A Windows setup information file for a CDC device.
    • Get either:
      • “avr.inf”, a “Windows 2000, XP & Vista setup File for AVR CDC Device” → Supplied by Atmel on the “Atmel Technology on Tour 2011” workshop.
      • “XPLAINED_Virtual_Com_Port.inf” → Atmel AVR XMEGA-A3BU Xplained kit, “Related Document / download Xplained USB CDC driver”.
    • From point of view of the Windows host computer, the interface is a virtual serial port ( e.g. COM21: ).
      • A terminal software can be used as Human Machine Interface ( HMI ).
      • Alternatively, You can write individual software ( in Python, Perl, C/C++, Java,.. ) to communicate with the Atmel board.
  3. The free IDE Atmel AVR Studio 5, with GNU-C AVR compiler.
  4. Any Windows terminal software.

3 How to implement CDC Communications, in your Atmel C/C++ Application

  1. In Atmel Studio 5, create your Visual Studio solution ( a solution may consist of several projects ) and Visual Studio project for your application, plus an extra static library project “avrstdio” for the Stdio redirection functions.
  2. Select “File / New / Example Project... ”, “Show Projects = AVR XMEGA, 8-bit” and add the “C” demo project Atmel Corporation "USB Device CDC Example for XMEGA-A3BU Xplained / ASF USB Device CDC" to your solution, to have sample code and to learn which drivers and which initialisation code you need.
  3. As you can´t transform a “C” project in a “C++” project with Atmel AVR Studio 5, currrently, you must start from scratch to build your own C++ applications, by importing needed ASF drivers to your empty project.
  4. Load the ASF drivers into your application project.
    • “IOPORT - Input/Output Port Controller (Driver)”.
    • “UTILITY - Generic board support (Driver)”.
    • “GFX Monochrome - Monochrome Graphic Library (Service)”.
    • “GFX Monochrome - System Font (Service)”.
    • “GPIO - General purpose Input/Output (Service)”.
    • “USB CDC (Single Interface Device) (Service)”.
  5. Load the ASF drivers into your optional “avrstdio” library project.
    • “IOPORT - Input/Output Port Controller (Driver)”.
    • “UTILITY - Generic board support (Driver)”.
    • “GPIO - General purpose Input/Output (Service)”.
    • “USB CDC (Single Interface Device) (Service)”.
  6. In Atmel Studio 5, modify the project files “src / config / conf_clock.h”, by including the USB configuration definitions next to “CONFIGURATION WITH USB”, both of the application project and the “avrstdio” project
    // ***************************************************************
    // **                  CONFIGURATION WITH USB                   **
    // ***************************************************************
    //! The following clock configuraiton can be used for USB operation
    //! It allows to operate USB using On-Chip RC oscillator at 48MHz
    //! The RC oscillator is calibrated via USB Start Of Frame
    //! Clk USB     = 48MHz (used by USB)
    //! Clk sys     = 48MHz
    //! Clk cpu/per = 24MHz
    #define CONFIG_USBCLK_SOURCE     USBCLK_SRC_RCOSC
    #define CONFIG_OSC_RC32_CAL      48000000UL
    #define CONFIG_OSC_AUTOCAL          OSC_ID_RC32MHZ
    #define CONFIG_OSC_AUTOCAL_REF_OSC  OSC_ID_USBSOF
    #define CONFIG_SYSCLK_SOURCE     SYSCLK_SRC_RC32MHZ
    #define CONFIG_SYSCLK_PSADIV     SYSCLK_PSADIV_2
    #define CONFIG_SYSCLK_PSBCDIV    SYSCLK_PSBCDIV_1_1
  7. In Atmel Studio 5, modify the project files “src / config / conf_usb.h”, by including the USB configuration definitions next to “Device definition (mandatory)”, both of the application project and the “avrstdio” project
    //! Device definition (mandatory)
    #define  USB_DEVICE_VENDOR_ID             0x03EB
    #define  USB_DEVICE_PRODUCT_ID            0x2404

4 How to direct Stdin / Stdout Communications to CDC, in your Atmel C/C++ Application

4.1 Description

4.2 stdio_demo.h

/**
 * \file stdio_demo.h
 * \date 2011-11-13
 * \brief Stdin / Stdout Demo
 * \author Rolf Hemmerling, http://www.hemmerling.com
 */  
#ifndef STDIO_DEMO_H_
#define STDIO_DEMO_H_

// #define USE_EXTERNAL_STDIOLIB
#undef USE_EXTERNAL_STDIOLIB

#include <asf.h>
#include <stdio.h>
#include <sleepmgr.h>
#include <sysclk.h>

void system_init(void);
void graphics_welcome(void);
void graphics_print(uint32_t aValue);
void communication_welcome(void);
int communication_start(void);
void avrstdio_demo(void);
int main(void);

#endif /* STDIO_DEMO_H_ */

4.3 stdio_demo.c

/**
 * \file stdio_demo.h
 * \date 2011-11-13
 * \brief Stdin / Stdout Demo
 * \author Rolf Hemmerling, http://www.hemmerling.com
 */

#include "avrstdio.h"
#include "stdio_demo.h"

FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
FILE uart_str_external = FDEV_SETUP_STREAM(uart_putchar_external, uart_getchar_external, _FDEV_SETUP_RW);

/**
 * \brief General system initialization
 */
void system_init(void)
{
	/* Initialize the microcontroller board */
	board_init();
        /* Initialize and enable the ITs routine */
	irq_initialize_vectors();
	cpu_irq_enable();
	/* Initialize the sleep manager service */
	sleepmgr_init();
	/* Initialize the clock service */
	sysclk_init(); 
}

/**
 * \brief Print a welcome message on the graphics system of XMEGA-A3BU
 */
void graphics_welcome(void)
{
	/**
	 * After initialization the example will write the "Stdin / Stdout Demo"
	 * to position 0, 0 on the display.
	 * Use the system font sysfont. 
	 */
	gfx_mono_init();
        ioport_set_pin_high(NHD_C12832A1Z_BACKLIGHT);
	st7565r_set_contrast ( ST7565R_DISPLAY_CONTRAST_MIN );
	gfx_mono_draw_string("Stdin / Stdout Demo", 0, 0, &sysfont);
}

/**
 * \brief Print a number on the graphics system of XMEGA-A3BU
 */
void graphics_print(uint32_t aValue)
{
        char aStringBuf[15];
        snprintf(aStringBuf, sizeof(aStringBuf), "%12lu", aValue);
        gfx_mono_draw_string(aStringBuf, 8, 8, &sysfont);
}

/**
 * \brief Print a welcome message by Stdout
 */
void communication_welcome(void)
{
	printf("Stdin / Stdout Demo\n");	
}	
	
/**
 * \brief Start of the Stdin / Stdout communication 
 */
int communication_start(void)	
{
	int aKeyboardInput = 0;
	/* Wait for a single "blindly typed" keyboard input */
        while (aKeyboardInput == 0)
	{
	   aKeyboardInput = getchar();
	};
	return (aKeyboardInput);
}	

/**
 * \brief Demo of communication by Stdin / Stdout
 */
void avrstdio_demo(void)
{
        int aStartCommand;
	graphics_welcome();
#ifdef USE_EXTERNAL_STDIOLIB
	avrstdio_start_external();
	stdout = &uart_str_external; 
        stdin = &uart_str_external; 
#else
	avrstdio_start();
	stdout = &uart_str; 
        stdin = &uart_str; 
#endif
	aStartCommand = communication_start();
	communication_welcome();
	uint32_t aCounter = 0u;
	while (true) {
		graphics_print(aCounter);
		printf("-Hello World %12lu-", aCounter);
		aCounter++;
	}			
}
	
/**
 * \brief Main entry of example application
 */
int main(void)
{
	system_init();
	avrstdio_demo();
}

4.4 avrstdio.h

/**
 * \file avrstdio.h
 * \date 2011-11-13
 * \brief Stdin / Stdout for Atmel AVR XMEGA-A3BU Xplained kit", by CDC protocol via USB
 * \author Rolf Hemmerling, http://www.hemmerling.com
 *
 * \details Include these statements in your applicatation 
 * FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
 * stdout = &uart_str; 
 * stdin = &uart_str; 
 */  

#ifndef AVRSTDIO_H_
#define AVRSTDIO_H_

#include <asf.h>
#include <stdio.h>
#include <sleepmgr.h>
#include <sysclk.h>
#include <udc.h>

int uart_putchar(char c, FILE *stream);
int uart_getchar(FILE *stream);
void avrstdio_start(void);
void avrstdio_stop(void);

int uart_putchar_external(char c, FILE *stream);
int uart_getchar_external(FILE *stream);
void avrstdio_start_external(void);
void avrstdio_stop_external(void);

#endif /* AVRSTDIO_H_ */

4.5 avrstdio.c

/**
 * \file avrstdio.c
 * \date 2011-11-13
 * \brief Stdin / Stdout for Atmel AVR XMEGA-A3BU Xplained kit", by CDC protocol via USB
 * \author Rolf Hemmerling, http://www.hemmerling.com
 */ 

#include "avrstdio.h"

/**
  *  \brief Send character c to CDC
  */
int uart_putchar(char c, FILE *stream)
{
        if (c == '\n')
          uart_putchar('\r', stream);
        udi_cdc_putc(c);
        return 0;
}

/**
  *  \brief Receive a character by CDC.
  */
int uart_getchar(FILE *stream)
{
        uint8_t c;
        c = udi_cdc_getc();
        return c;
}

/**
  * \brief Initialization of the Stdio communication
  */
void avrstdio_start(void)
 {
	/* Initialize the microcontroller board */
	// board_init();
        /* Initialize and enable the ITs routine */
	// irq_initialize_vectors();
	// cpu_irq_enable();
	/* Initialize the sleep manager service */
	// sleepmgr_init();
	/* Initialize the clock service */
	// sysclk_init();	 
	/* Enable the USB device stack */
	udc_start();
	/* Attach the USB device to the host ( to start communications ) */
	udc_attach();
 }
 
 /**
   * \brief Stop of the Stdio communication
   */
void avrstdio_stop(void)
 {
	// By stopping USB communications during the application runtime, 
	// you risk that some previous output is not delivered :-(

	// If you don't delay the stop of the USB connection, there will be no output.
	// cdc_delay();
	/* Detach the USB device to the host ( to start communications ) */
	// udc_detach();
	/* Disable the USB device stack */
	// udc_stop();
	// cpu_irq_disable();
 }

5 How to configure the Windows Host for CDC Communications

5.1 Install the Windows setup information file for a CDC device

5.2 Confgure PuTTY to convert the CR output of a CDC Application to a CR, LF

5.3 How to start a CDC application on Windows Host with PuTTY & Atmel Target

6 Operation Instructions

7 Restrictions with CDC Communications

7.1 Please wait for a Keypress to start CDC Communications

7.2 Stopping at a Breakpoint breaks CDC Communications

7.3 No CDC Communications with a Windows host if Breakpoints are set

7.4 You can´t reopen CDC communications

Resources

Appropriate OpenDirectory Directory Pages


When this document changes ! Site Navigation ( My Business ! My Topics ! Imprint / Contact ! Privacy Policy ! Keyword Index ! ! Google+ Publisher "hemmerling" )