Commit e0af6014 authored by Dean Camera's avatar Dean Camera
Browse files

Added incomplete PrinterHost demo application.

Seperated out Lib components of the incomplete BluetoothHost demo application out into a seperate Lib subfolder.

Changed F_CLOCK entries in project makefiles to alias to F_CPU by default, as this is the most common case.
parent b462f2d4
......@@ -101,7 +101,7 @@ F_CPU = 8000000
#
# If no clock division is performed on the input clock inside the AVR (via the
# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
F_CLOCK = 8000000
F_CLOCK = $(F_CPU)
# Output format. (can be srec, ihex, binary)
......@@ -133,9 +133,9 @@ LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"
SRC = $(TARGET).c \
DeviceDescriptor.c \
ConfigDescriptor.c \
BluetoothStack.c \
BluetoothHCICommands.c \
BluetoothACLPackets.c \
Lib/BluetoothStack.c \
Lib/BluetoothHCICommands.c \
Lib/BluetoothACLPackets.c \
$(LUFA_PATH)/LUFA/Drivers/Peripheral/SerialStream.c \
$(LUFA_PATH)/LUFA/Drivers/Peripheral/Serial.c \
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/DevChapter9.c \
......
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
#include "ConfigDescriptor.h"
uint8_t ProcessConfigurationDescriptor(void)
{
uint8_t* ConfigDescriptorData;
uint16_t ConfigDescriptorSize;
uint8_t ErrorCode;
uint8_t FoundEndpoints = 0;
uint8_t FoundEndpointMask;
/* Get Configuration Descriptor size from the device */
if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
return ControlError;
/* Ensure that the Configuration Descriptor isn't too large */
if (ConfigDescriptorSize > MAX_CONFIG_DESCRIPTOR_SIZE)
return DescriptorTooLarge;
/* Allocate enough memory for the entire config descriptor */
ConfigDescriptorData = alloca(ConfigDescriptorSize);
/* Retrieve the entire configuration descriptor into the allocated buffer */
USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData);
/* Validate returned data - ensure first entry is a configuration header descriptor */
if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
return InvalidConfigDataReturned;
/* Get the printer interface from the configuration descriptor */
if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
NextPrinterInterface)))
{
/* Descriptor not found, error out */
return NoInterfaceFound;
}
/* Get the printer's communication protocol */
PrinterProtocol = DESCRIPTOR_CAST(ConfigDescriptorData, USB_Descriptor_Interface_t).Protocol;
/* Determine what endpoints to look for from the protocol */
switch (PrinterProtocol)
{
case PROTOCOL_UNIDIRECTIONAL:
FoundEndpointMask = (1 << PRINTER_DATA_OUT_PIPE);
break;
case PROTOCOL_BIDIRECTIONAL:
case PROTOCOL_IEEE1284:
FoundEndpointMask = ((1 << PRINTER_DATA_OUT_PIPE) | (1 << PRINTER_DATA_IN_PIPE));
break;
default:
return NoInterfaceFound;
}
/* Get the IN and OUT data endpoints for the mass storage interface */
while (FoundEndpoints != FoundEndpointMask)
{
/* Fetch the next bulk endpoint from the current printer interface */
if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
NextInterfaceBulkDataEndpoint)))
{
/* Descriptor not found, error out */
return NoEndpointFound;
}
USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
/* Check if the endpoint is a bulk IN or bulk OUT endpoint, set appropriate globals */
if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
{
/* Configure the data IN pipe */
Pipe_ConfigurePipe(PRINTER_DATA_IN_PIPE, EP_TYPE_BULK, PIPE_TOKEN_IN,
EndpointData->EndpointAddress, EndpointData->EndpointSize,
PIPE_BANK_SINGLE);
Pipe_SetInfiniteINRequests();
/* Set the flag indicating that the data IN pipe has been found */
FoundEndpoints |= (1 << PRINTER_DATA_IN_PIPE);
}
else
{
/* Configure the data OUT pipe */
Pipe_ConfigurePipe(PRINTER_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,
EndpointData->EndpointAddress, EndpointData->EndpointSize,
PIPE_BANK_SINGLE);
/* Set the flag indicating that the data OUT pipe has been found */
FoundEndpoints |= (1 << PRINTER_DATA_OUT_PIPE);
}
}
/* Valid data found, return success */
return SuccessfulConfigRead;
}
uint8_t NextPrinterInterface(void* CurrentDescriptor)
{
/* PURPOSE: Find next mass storage class interface descriptor */
if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
{
/* Check the descriptor class and protocol, break out if correct class/protocol interface found */
if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == PRINTER_CLASS) &&
(DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == PRINTER_SUBCLASS))
{
return DESCRIPTOR_SEARCH_Found;
}
}
return DESCRIPTOR_SEARCH_NotFound;
}
uint8_t NextInterfaceBulkDataEndpoint(void* CurrentDescriptor)
{
/* PURPOSE: Find next interface bulk endpoint descriptor before next interface descriptor */
if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
{
uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
/* Check the endpoint type, break out if correct BULK type endpoint found */
if (EndpointType == EP_TYPE_BULK)
return DESCRIPTOR_SEARCH_Found;
}
else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
{
return DESCRIPTOR_SEARCH_Fail;
}
return DESCRIPTOR_SEARCH_NotFound;
}
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
#ifndef _CONFIGDESCRIPTOR_H_
#define _CONFIGDESCRIPTOR_H_
/* Includes: */
#include <LUFA/Drivers/USB/USB.h>
#include "PrinterHost.h"
/* Macros: */
#define PRINTER_CLASS 0x07
#define PRINTER_SUBCLASS 0x01
#define PRINTER_DATA_OUT_PIPE 1
#define PRINTER_DATA_IN_PIPE 2
#define MAX_CONFIG_DESCRIPTOR_SIZE 512
/* Enums: */
enum PrinterHost_GetConfigDescriptorDataCodes_t
{
SuccessfulConfigRead = 0,
ControlError = 1,
DescriptorTooLarge = 2,
InvalidConfigDataReturned = 3,
NoInterfaceFound = 4,
NoEndpointFound = 5,
};
/* Function Prototypes: */
uint8_t ProcessConfigurationDescriptor(void);
uint8_t NextPrinterInterface(void* CurrentDescriptor);
uint8_t NextInterfaceBulkDataEndpoint(void* CurrentDescriptor);
#endif
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/*
USB Printer host demo application.
** NOT CURRENTLY FUNCTIONAL - DO NOT USE **
*/
#include "PrinterHost.h"
/* Globals */
uint8_t PrinterProtocol;
int main(void)
{
SetupHardware();
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY
"Printer Host Demo running.\r\n" ESC_INVERSE_OFF));
for (;;)
{
USB_Printer_Host();
USB_USBTask();
}
}
void SetupHardware(void)
{
/* Disable watchdog if enabled by bootloader/fuses */
MCUSR &= ~(1 << WDRF);
wdt_disable();
/* Disable clock division */
clock_prescale_set(clock_div_1);
/* Hardware Initialization */
SerialStream_Init(9600, false);
LEDs_Init();
USB_Init();
}
void EVENT_USB_DeviceAttached(void)
{
puts_P(PSTR("Device Attached.\r\n"));
LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
}
void EVENT_USB_DeviceUnattached(void)
{
puts_P(PSTR("\r\nDevice Unattached.\r\n"));
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
}
void EVENT_USB_HostError(uint8_t ErrorCode)
{
USB_ShutDown();
puts_P(PSTR(ESC_BG_RED "Host Mode Error\r\n"));
printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
for(;;);
}
void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode, uint8_t SubErrorCode)
{
puts_P(PSTR(ESC_BG_RED "Dev Enum Error\r\n"));
printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);
printf_P(PSTR(" -- In State %d\r\n"), USB_HostState);
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
}
void EVENT_USB_DeviceEnumerationComplete(void)
{
LEDs_SetAllLEDs(LEDMASK_USB_READY);
}
void USB_Printer_Host(void)
{
uint8_t ErrorCode;
switch (USB_HostState)
{
case HOST_STATE_Addressed:
/* Standard request to set the device configuration to configuration 1 */
USB_ControlRequest = (USB_Request_Header_t)
{
bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
bRequest: REQ_SetConfiguration,
wValue: 1,
wIndex: 0,
wLength: 0,
};
/* Send the request, display error and wait for device detatch if request fails */
if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
{
puts_P(PSTR("Control Error (Set Configuration).\r\n"));
printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
/* Indicate error via status LEDs */
LEDs_SetAllLEDs(LEDS_LED1);
/* Wait until USB device disconnected */
while (USB_IsConnected);
break;
}
USB_HostState = HOST_STATE_Configured;
break;
case HOST_STATE_Configured:
puts_P(PSTR("Getting Config Data.\r\n"));
/* Get and process the configuration descriptor data */
if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
{
if (ErrorCode == ControlError)
puts_P(PSTR("Control Error (Get Configuration).\r\n"));
else
puts_P(PSTR("Invalid Device.\r\n"));
printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
/* Indicate error via status LEDs */
LEDs_SetAllLEDs(LEDS_LED1);
/* Wait until USB device disconnected */
while (USB_IsConnected);
break;
}
puts_P(PSTR("Printer Enumerated.\r\n"));
USB_HostState = HOST_STATE_Ready;
break;
case HOST_STATE_Ready:
/* Indicate device busy via the status LEDs */
LEDs_SetAllLEDs(LEDS_LED3 | LEDS_LED4);
if (!(GetDeviceID()))
{
/* Indicate error via status LEDs */
LEDs_SetAllLEDs(LEDS_LED1);
/* Wait until USB device disconnected */
while (USB_IsConnected);
break;
}
/* Indicate device no longer busy */
LEDs_SetAllLEDs(LEDS_LED4);
/* Wait until USB device disconnected */
while (USB_IsConnected);
break;
}
}
bool GetDeviceID(void)
{
Device_ID_String_t DeviceIDString;
/* Request to retrieve the device ID string */
USB_ControlRequest = (USB_Request_Header_t)
{
bmRequestType: (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
bRequest: GET_DEVICE_ID,
wValue: 0,
wIndex: 0,
wLength: sizeof(DeviceIDString),
};
printf("Error Code: %d", USB_Host_SendControlRequest(&DeviceIDString));
/* Send the request, display error and wait for device detatch if request fails */
if (USB_Host_SendControlRequest(&DeviceIDString) != HOST_SENDCONTROL_Successful)
return false;
/* Reverse the order of the string length as it is sent in big-endian format */
DeviceIDString.Length = SwapEndian_16(DeviceIDString.Length);
printf("%s", DeviceIDString.String);
return true;
}
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
#ifndef _MASS_STORE_HOST_H_
#define _MASS_STORE_HOST_H_
/* Includes: */
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include <avr/power.h>
#include <stdio.h>
#include "ConfigDescriptor.h"
#include <LUFA/Version.h>
#include <LUFA/Drivers/Misc/TerminalCodes.h>
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Drivers/Peripheral/SerialStream.h>
#include <LUFA/Drivers/Board/LEDs.h>
/* Macros: */
/** LED mask for the library LED driver, to indicate that the USB interface is not ready. */
#define LEDMASK_USB_NOTREADY LEDS_LED1
/** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */
#define LEDMASK_USB_ENUMERATING (LEDS_LED2 | LEDS_LED3)
/** LED mask for the library LED driver, to indicate that the USB interface is ready. */
#define LEDMASK_USB_READY (LEDS_LED2 | LEDS_LED4)
/** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */
#define LEDMASK_USB_ERROR (LEDS_LED1 | LEDS_LED3)
#define PROTOCOL_UNIDIRECTIONAL 0x01
#define PROTOCOL_BIDIRECTIONAL 0x02
#define PROTOCOL_IEEE1284 0x03
#define GET_DEVICE_ID 0
/* Type Defines: */
typedef struct
{
uint16_t Length;
uint8_t String[128];
} Device_ID_String_t;
/* External Variables: */
extern uint8_t PrinterProtocol;
/* Function Prototypes: */
void EVENT_USB_DeviceAttached(void);
void EVENT_USB_DeviceUnattached(void);
void EVENT_USB_DeviceEnumerationComplete(void);
void EVENT_USB_HostError(uint8_t ErrorCode);
void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode, uint8_t SubErrorCode);
void SetupHardware(void);
void USB_Printer_Host(void);
bool GetDeviceID(void);
#endif
This diff is collapsed.
......@@ -101,7 +101,7 @@ F_CPU = 8000000
#
# If no clock division is performed on the input clock inside the AVR (via the
# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
F_CLOCK = 8000000
F_CLOCK = $(F_CPU)
# Output format. (can be srec, ihex, binary)
......
......@@ -101,7 +101,7 @@ F_CPU = 8000000
#
# If no clock division is performed on the input clock inside the AVR (via the
# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
F_CLOCK = 8000000
F_CLOCK = $(F_CPU)
# Output format. (can be srec, ihex, binary)
......
......@@ -101,7 +101,7 @@ F_CPU = 8000000
#
# If no clock division is performed on the input clock inside the AVR (via the
# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
F_CLOCK = 8000000
F_CLOCK = $(F_CPU)
# Output format. (can be srec, ihex, binary)
......
......@@ -101,7 +101,7 @@ F_CPU = 8000000
#
# If no clock division is performed on the input clock inside the AVR (via the
# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
F_CLOCK = 8000000
F_CLOCK = $(F_CPU)
# Output format. (can be srec, ihex, binary)
......