Skip to content

Commit

Permalink
Added CDC functional descriptor structs to the Low Level CDC demos an…
Browse files Browse the repository at this point in the history
…d CDC class bootloader, to improve the readability of the descriptors.

Fixed BootloaderCDC project failing on some operating systems due to removed Line Encoding options (thanks to Alexey Belyaev).

git-svn-id: http://lufa-lib.googlecode.com/svn@1498 d5102386-fcda-11dd-9fdb-3debd5008f28
  • Loading branch information
Dean authored and Dean committed Sep 28, 2010
1 parent ae4e44b commit fb55198
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 144 deletions.
42 changes: 42 additions & 0 deletions trunk/Bootloaders/CDC/BootloaderCDC.c
Expand Up @@ -36,6 +36,14 @@
#define INCLUDE_FROM_BOOTLOADERCDC_C
#include "BootloaderCDC.h"

/** Contains the current baud rate and other settings of the first virtual serial port. This must be retained as some
* operating systems will not open the port unless the settings can be set successfully.
*/
CDC_Line_Coding_t LineEncoding = { .BaudRateBPS = 0,
.CharFormat = OneStopBit,
.ParityType = Parity_None,
.DataBits = 8 };

/** Current address counter. This stores the current address of the FLASH or EEPROM as set by the host,
* and is used when reading or writing to the AVRs memory (either FLASH or EEPROM depending on the issued
* command.)
Expand Down Expand Up @@ -113,6 +121,40 @@ void EVENT_USB_Device_ConfigurationChanged(void)
ENDPOINT_BANK_SINGLE);
}

/** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific
* control requests that are not handled internally by the USB library (including the CDC control commands,
* which are all issued via the control endpoint), so that they can be handled appropriately for the application.
*/
void EVENT_USB_Device_UnhandledControlRequest(void)
{
/* Process CDC specific control requests */
switch (USB_ControlRequest.bRequest)
{
case REQ_GetLineEncoding:
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
{
Endpoint_ClearSETUP();

/* Write the line coding data to the control endpoint */
Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_Line_Coding_t));
Endpoint_ClearOUT();
}

break;
case REQ_SetLineEncoding:
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
{
Endpoint_ClearSETUP();

/* Read the line coding data in from the host into the global struct */
Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_Line_Coding_t));
Endpoint_ClearIN();
}

break;
}
}

/** Reads or writes a block of EEPROM or FLASH memory to or from the appropriate CDC data endpoint, depending
* on the AVR910 protocol command issued.
*
Expand Down
42 changes: 41 additions & 1 deletion trunk/Bootloaders/CDC/BootloaderCDC.h
Expand Up @@ -49,7 +49,7 @@

#include <LUFA/Drivers/USB/USB.h>

/* Macros: */
/* Macros: */
/** Version major of the CDC bootloader. */
#define BOOTLOADER_VERSION_MAJOR 0x01

Expand All @@ -65,10 +65,50 @@
/** Eight character bootloader firmware identifier reported to the host when requested */
#define SOFTWARE_IDENTIFIER "LUFACDC"

/** CDC Class specific request to get the current virtual serial port configuration settings. */
#define REQ_GetLineEncoding 0x21

/** CDC Class specific request to set the current virtual serial port configuration settings. */
#define REQ_SetLineEncoding 0x20

/* Type Defines: */
/** Type define for the virtual serial port line encoding settings, for storing the current USART configuration
* as set by the host via a class specific request.
*/
typedef struct
{
uint32_t BaudRateBPS; /**< Baud rate of the virtual serial port, in bits per second */
uint8_t CharFormat; /**< Character format of the virtual serial port, a value from the
* CDCDevice_CDC_LineCodingFormats_t enum
*/
uint8_t ParityType; /**< Parity setting of the virtual serial port, a value from the
* CDCDevice_LineCodingParity_t enum
*/
uint8_t DataBits; /**< Bits of data per character of the virtual serial port */
} CDC_Line_Coding_t;

/** Type define for a non-returning pointer to the start of the loaded application in flash memory. */
typedef void (*AppPtr_t)(void) ATTR_NO_RETURN;

/* Enums: */
/** Enum for the possible line encoding formats of a virtual serial port. */
enum CDCDevice_CDC_LineCodingFormats_t
{
OneStopBit = 0, /**< Each frame contains one stop bit */
OneAndAHalfStopBits = 1, /**< Each frame contains one and a half stop bits */
TwoStopBits = 2, /**< Each frame contains two stop bits */
};

/** Enum for the possible line encoding parity settings of a virtual serial port. */
enum CDCDevice_LineCodingParity_t
{
Parity_None = 0, /**< No parity bit mode on each frame */
Parity_Odd = 1, /**< Odd parity bit mode on each frame */
Parity_Even = 2, /**< Even parity bit mode on each frame */
Parity_Mark = 3, /**< Mark parity bit mode on each frame */
Parity_Space = 4, /**< Space parity bit mode on each frame */
};

/* Function Prototypes: */
void CDC_Task(void);
void SetupHardware(void);
Expand Down
27 changes: 14 additions & 13 deletions trunk/Bootloaders/CDC/Descriptors.c
Expand Up @@ -55,7 +55,7 @@ USB_Descriptor_Device_t DeviceDescriptor =

.VendorID = 0x03EB,
.ProductID = 0x204A,
.ReleaseNumber = 0x0000,
.ReleaseNumber = 0x0002,

.ManufacturerStrIndex = NO_DESCRIPTOR,
.ProductStrIndex = 0x01,
Expand Down Expand Up @@ -102,29 +102,30 @@ USB_Descriptor_Configuration_t ConfigurationDescriptor =
.InterfaceStrIndex = NO_DESCRIPTOR
},

.CDC_Functional_IntHeader =
.CDC_Functional_Header =
{
.Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24},
.SubType = 0x00,
.Header = {.Size = sizeof(USB_Descriptor_CDC_FunctionalHeader_t), .Type = DTYPE_CSInterface},
.Subtype = 0x00,

.Data = {0x10, 0x01}
.CDCSpecification = VERSION_BCD(01.10),
},

.CDC_Functional_AbstractControlManagement =
.CDC_Functional_ACM =
{
.Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(1)), .Type = 0x24},
.SubType = 0x02,
.Header = {.Size = sizeof(USB_Descriptor_CDC_FunctionalACM_t), .Type = DTYPE_CSInterface},
.Subtype = 0x02,

.Data = {0x06}
.Capabilities = 0x04,
},

.CDC_Functional_Union =
{
.Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24},
.SubType = 0x06,
.Header = {.Size = sizeof(USB_Descriptor_CDC_FunctionalUnion_t), .Type = DTYPE_CSInterface},
.Subtype = 0x06,

.Data = {0x00, 0x01}
},
.MasterInterfaceNumber = 0,
.SlaveInterfaceNumber = 1,
},

.CDC_NotificationEndpoint =
{
Expand Down
54 changes: 37 additions & 17 deletions trunk/Bootloaders/CDC/Descriptors.h
Expand Up @@ -92,20 +92,6 @@
#error The selected AVR part is not currently supported by this bootloader.
#endif

/** Structure for a CDC class Functional descriptor, with a given data size. This is used instead of a
* type define so that the same macro can be used for functional descriptors of varying data lengths,
* while allowing the sizeof() operator to return correct results.
*
* \param[in] DataSize Size of the functional descriptor's data payload, in bytes
*/
#define CDC_FUNCTIONAL_DESCRIPTOR(DataSize) \
struct \
{ \
USB_Descriptor_Header_t Header; \
uint8_t SubType; \
uint8_t Data[DataSize]; \
}

/** Endpoint number for the CDC control interface event notification endpoint. */
#define CDC_NOTIFICATION_EPNUM 3

Expand All @@ -122,6 +108,40 @@
#define CDC_TXRX_EPSIZE 16

/* Type Defines: */
/** Type define for a CDC class-specific functional header descriptor. This indicates to the host that the device
* contains one or more CDC functional data descriptors, which give the CDC interface's capabilities and configuration.
* See the CDC class specification for more details.
*/
typedef struct
{
USB_Descriptor_Header_t Header; /**< Regular descriptor header containing the descriptor's type and length. */
uint8_t Subtype; /**< Sub type value used to distinguish between CDC class-specific descriptors. */
uint16_t CDCSpecification; /**< Version number of the CDC specification implemented by the device,
* encoded in BCD format.
*/
} USB_Descriptor_CDC_FunctionalHeader_t;

/** Type define for a CDC class-specific functional ACM descriptor. This indicates to the host that the CDC interface
* supports the CDC ACM subclass of the CDC specification. See the CDC class specification for more details.
*/
typedef struct
{
USB_Descriptor_Header_t Header; /**< Regular descriptor header containing the descriptor's type and length. */
uint8_t Subtype; /**< Sub type value used to distinguish between CDC class-specific descriptors. */
uint8_t Capabilities; /**< Capabilities of the ACM interface, given as a bit mask. */
} USB_Descriptor_CDC_FunctionalACM_t;

/** Type define for a CDC class-specific functional Union descriptor. This indicates to the host that specific
* CDC control and data interfaces are related. See the CDC class specification for more details.
*/
typedef struct
{
USB_Descriptor_Header_t Header; /**< Regular descriptor header containing the descriptor's type and length. */
uint8_t Subtype; /**< Sub type value used to distinguish between CDC class-specific descriptors. */
uint8_t MasterInterfaceNumber; /**< Interface number of the CDC Control interface. */
uint8_t SlaveInterfaceNumber; /**< Interface number of the CDC Data interface. */
} USB_Descriptor_CDC_FunctionalUnion_t;

/** Type define for the device configuration descriptor structure. This must be defined in the
* application code, as the configuration descriptor contains several sub-descriptors which
* vary between devices, and which describe the device's usage to the host.
Expand All @@ -130,9 +150,9 @@
{
USB_Descriptor_Configuration_Header_t Config;
USB_Descriptor_Interface_t CDC_CCI_Interface;
CDC_FUNCTIONAL_DESCRIPTOR(2) CDC_Functional_IntHeader;
CDC_FUNCTIONAL_DESCRIPTOR(1) CDC_Functional_AbstractControlManagement;
CDC_FUNCTIONAL_DESCRIPTOR(2) CDC_Functional_Union;
USB_Descriptor_CDC_FunctionalHeader_t CDC_Functional_Header;
USB_Descriptor_CDC_FunctionalACM_t CDC_Functional_ACM;
USB_Descriptor_CDC_FunctionalUnion_t CDC_Functional_Union;
USB_Descriptor_Endpoint_t CDC_NotificationEndpoint;
USB_Descriptor_Interface_t CDC_DCI_Interface;
USB_Descriptor_Endpoint_t CDC_DataOutEndpoint;
Expand Down
2 changes: 1 addition & 1 deletion trunk/Bootloaders/DFU/Descriptors.c
Expand Up @@ -104,7 +104,7 @@ USB_Descriptor_Configuration_t ConfigurationDescriptor =

.DFU_Functional =
{
.Header = {.Size = sizeof(USB_DFU_Functional_Descriptor_t), .Type = DTYPE_DFUFunctional},
.Header = {.Size = sizeof(USB_Descriptor_DFU_Functional_t), .Type = DTYPE_DFUFunctional},

.Attributes = (ATTR_CAN_UPLOAD | ATTR_CAN_DOWNLOAD),

Expand Down
22 changes: 11 additions & 11 deletions trunk/Bootloaders/DFU/Descriptors.h
Expand Up @@ -140,19 +140,19 @@
USB_Descriptor_Header_t Header; /**< Standard descriptor header structure */

uint8_t Attributes; /**< DFU device attributes, a mask comprising of the
* ATTR_* macros listed in this source file
*/
* ATTR_* macros listed in this source file
*/
uint16_t DetachTimeout; /**< Timeout in milliseconds between a USB_DETACH
* command being issued and the device detaching
* from the USB bus
*/
* command being issued and the device detaching
* from the USB bus
*/
uint16_t TransferSize; /**< Maximum number of bytes the DFU device can accept
* from the host in a transaction
*/
* from the host in a transaction
*/
uint16_t DFUSpecification; /**< BCD packed DFU specification number this DFU
* device complies with
*/
} USB_DFU_Functional_Descriptor_t;
* device complies with
*/
} USB_Descriptor_DFU_Functional_t;

/** Type define for the device configuration descriptor structure. This must be defined in the
* application code, as the configuration descriptor contains several sub-descriptors which
Expand All @@ -162,7 +162,7 @@
{
USB_Descriptor_Configuration_Header_t Config;
USB_Descriptor_Interface_t DFU_Interface;
USB_DFU_Functional_Descriptor_t DFU_Functional;
USB_Descriptor_DFU_Functional_t DFU_Functional;
} USB_Descriptor_Configuration_t;

/* Function Prototypes: */
Expand Down
46 changes: 24 additions & 22 deletions trunk/Demos/Device/LowLevel/DualVirtualSerial/Descriptors.c
Expand Up @@ -128,28 +128,29 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.InterfaceStrIndex = NO_DESCRIPTOR
},

.CDC1_Functional_IntHeader =
.CDC1_Functional_Header =
{
.Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24},
.SubType = 0x00,
.Header = {.Size = sizeof(USB_Descriptor_CDC_FunctionalHeader_t), .Type = DTYPE_CSInterface},
.Subtype = 0x00,

.Data = {0x01, 0x10}
.CDCSpecification = VERSION_BCD(01.10),
},

.CDC1_Functional_AbstractControlManagement =
.CDC1_Functional_ACM =
{
.Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(1)), .Type = 0x24},
.SubType = 0x02,
.Header = {.Size = sizeof(USB_Descriptor_CDC_FunctionalACM_t), .Type = DTYPE_CSInterface},
.Subtype = 0x02,

.Data = {0x06}
.Capabilities = 0x06,
},

.CDC1_Functional_Union =
{
.Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24},
.SubType = 0x06,
.Header = {.Size = sizeof(USB_Descriptor_CDC_FunctionalUnion_t), .Type = DTYPE_CSInterface},
.Subtype = 0x06,

.Data = {0x00, 0x01}
.MasterInterfaceNumber = 0,
.SlaveInterfaceNumber = 1,
},

.CDC1_ManagementEndpoint =
Expand Down Expand Up @@ -228,28 +229,29 @@ USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.InterfaceStrIndex = NO_DESCRIPTOR
},

.CDC2_Functional_IntHeader =
.CDC2_Functional_Header =
{
.Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24},
.SubType = 0x00,
.Header = {.Size = sizeof(USB_Descriptor_CDC_FunctionalHeader_t), .Type = DTYPE_CSInterface},
.Subtype = 0x00,

.Data = {0x01, 0x10}
.CDCSpecification = VERSION_BCD(01.10),
},

.CDC2_Functional_AbstractControlManagement =
.CDC2_Functional_ACM =
{
.Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(1)), .Type = 0x24},
.SubType = 0x02,
.Header = {.Size = sizeof(USB_Descriptor_CDC_FunctionalACM_t), .Type = DTYPE_CSInterface},
.Subtype = 0x02,

.Data = {0x06}
.Capabilities = 0x06,
},

.CDC2_Functional_Union =
{
.Header = {.Size = sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)), .Type = 0x24},
.SubType = 0x06,
.Header = {.Size = sizeof(USB_Descriptor_CDC_FunctionalUnion_t), .Type = DTYPE_CSInterface},
.Subtype = 0x06,

.Data = {0x02, 0x03}
.MasterInterfaceNumber = 2,
.SlaveInterfaceNumber = 3,
},

.CDC2_ManagementEndpoint =
Expand Down

0 comments on commit fb55198

Please sign in to comment.