view usb.h @ 35:fed32b382de2

Tidy up, hide details behind macros to make it more obvious what we talk to do do things. Convert constants to my preferred format.
author darius
date Tue, 23 Oct 2007 10:54:01 +0930
parents 4e417d84365e
children
line wrap: on
line source

/*
 * Copyright (c) 2003 Bernd Walter
 * All rights reserved.
 */

/*  Example Source Code for USB Enumeration using a PDIUSBD11 connected to a PIC16F87x
 *  Copyright 2001 Craig Peacock, Craig.Peacock@beyondlogic.org
 *  31th December 2001 http://www.beyondlogic.org            				      
 */

typedef struct {
    uint8_t bmRequestType;
    uint8_t bRequest;
    uint16_t wValue;
    uint16_t wIndex;
    uint16_t wLength;
}  __attribute__ ((packed)) USB_SETUP_REQUEST;

typedef struct {
    uint8_t bLength;
    uint8_t bDescriptorType;
    uint16_t bcdUSB;
    uint8_t bDeviceClass;
    uint8_t bDeviceSubClass;
    uint8_t bDeviceProtocol;
    uint8_t bMaxPacketSize0;
    uint16_t idVendor;
    uint16_t idProduct;
    uint16_t bcdDevice;
    uint8_t iManufacturer;
    uint8_t iProduct;
    uint8_t iSerialNumber;
    uint8_t bNumConfigurations;
} __attribute__ ((packed))  USB_DEVICE_DESCRIPTOR;

typedef struct {
    uint8_t bLength;
    uint8_t bDescriptorType;
    uint8_t bEndpointAddress;
    uint8_t bmAttributes;
    uint16_t wMaxPacketSize;
    uint8_t bInterval;
} __attribute__ ((packed))  USB_ENDPOINT_DESCRIPTOR;

typedef struct {
    uint8_t bLength;
    uint8_t bDescriptorType;
    uint16_t wTotalLength;
    uint8_t bNumInterfaces;
    uint8_t bConfigurationValue;
    uint8_t iConfiguration;
    uint8_t bmAttributes;
    uint8_t MaxPower;
} __attribute__ ((packed))  USB_CONFIGURATION_DESCRIPTOR;

typedef struct {
    uint8_t bLength;
    uint8_t bDescriptorType;
    uint8_t bInterfaceNumber;
    uint8_t bAlternateSetting;
    uint8_t bNumEndpoints;
    uint8_t bInterfaceClass;
    uint8_t bInterfaceSubClass;
    uint8_t bInterfaceProtocol;
    uint8_t iInterface;
} __attribute__ ((packed))  USB_INTERFACE_DESCRIPTOR;

typedef struct {
    uint8_t bLength;
    uint8_t bDescriptorType;
    uint16_t wHIDClassSpecComp;
    uint8_t bCountry;
    uint8_t bNumDescriptors;
    uint8_t b1stDescType;
    uint16_t w1stDescLength;
} __attribute__ ((packed))  USB_HID_DESCRIPTOR;

typedef struct {
    USB_CONFIGURATION_DESCRIPTOR ConfigDescriptor;
    USB_INTERFACE_DESCRIPTOR InterfaceDescriptor0;
    USB_ENDPOINT_DESCRIPTOR EndpointDescriptor00;
    USB_ENDPOINT_DESCRIPTOR EndpointDescriptor01;
    USB_INTERFACE_DESCRIPTOR InterfaceDescriptor1;
    USB_ENDPOINT_DESCRIPTOR EndpointDescriptor10;
    USB_ENDPOINT_DESCRIPTOR EndpointDescriptor11;
} __attribute__ ((packed))  USB_CONFIG_DATA;

typedef struct {
    uint8_t bLength;
    uint8_t bDescriptorType;
    char     bString[];
} __attribute__ ((packed))  STRING_DESCRIPTOR;

typedef struct {
    uint8_t bLength;
    uint8_t bDescriptorType;
    uint16_t wLANGID0;
} __attribute__ ((packed))  LANGID_DESCRIPTOR;

#define D12_SET_ADDRESS_ENABLE		0xd0
#define D12_SET_ENDPOINT_ENABLE  	0xd8
#define D12_SET_MODE			0xf3
#define D12_SET_DMA			0xfb
#define D12_READ_INTERRUPT_REGISTER 	0xf4
#define D12_READ_BUFFER			0xf0
#define D12_WRITE_BUFFER		0xf0
#define D12_ACK_SETUP			0xf2
#define D12_CLEAR_BUFFER		0xf2
#define D12_VALIDATE_BUFFER		0xfa
#define D12_READ_LAST_TRANSACTION	0x40
#define D12_SET_ENDPOINT_STATUS		0x40
#define D12_READ_ENDPOINT_STATUS	0x80
#define D12_READ_CHIP_ID		0xfd

#define D12_ENDPOINT_EP0_OUT 		0x00
#define D12_ENDPOINT_EP0_IN 		0x01
#define D12_ENDPOINT_EP1_OUT 		0x02
#define D12_ENDPOINT_EP1_IN 		0x03
#define D12_ENDPOINT_EP2_OUT 		0x04
#define D12_ENDPOINT_EP2_IN 		0x05

#define D12_INT_EP0_OUT			0x01
#define D12_INT_EP0_IN			0x02
#define D12_INT_EP1_OUT			0x04
#define D12_INT_EP1_IN			0x08
#define D12_INT_EP2_OUT			0x10
#define D12_INT_EP2_IN			0x20
#define D12_INT_BUS_RESET		0x40
#define D12_INT_SUSPEND			0x80

#define D12_LAST_TRAN_OK		0x01
#define D12_LAST_TRAN_ERRMSK		0x1e
#define D12_LAST_TRAN_SETUP		0x20
#define D12_LAST_TRAN_DATA		0x40
#define D12_LAST_TRAN_OFLOW		0x80

#define STANDARD_DEVICE_REQUEST		0x00
#define STANDARD_INTERFACE_REQUEST	0x01
#define STANDARD_ENDPOINT_REQUEST	0x02
#define VENDOR_DEVICE_REQUEST		0x40
#define VENDOR_INTERFACE_REQUEST	0x41
#define VENDOR_ENDPOINT_REQUEST		0x42

#define GET_STATUS  			0
#define CLEAR_FEATURE     		1
#define SET_FEATURE                 	3
#define SET_ADDRESS                 	5
#define GET_DESCRIPTOR              	6
#define SET_DESCRIPTOR              	7
#define GET_CONFIGURATION           	8
#define SET_CONFIGURATION           	9
#define GET_INTERFACE               	10
#define SET_INTERFACE               	11
#define SYNCH_FRAME                 	12

#define VENDOR_DOTEMP			125
#define VENDOR_UPDATE			126
#define VENDOR_RESET			127

#define	ENDPOINT_HALT			0

#define TYPE_DEVICE_DESCRIPTOR          1
#define TYPE_CONFIGURATION_DESCRIPTOR   2
#define TYPE_STRING_DESCRIPTOR          3
#define TYPE_INTERFACE_DESCRIPTOR       4
#define TYPE_ENDPOINT_DESCRIPTOR        5
#define TYPE_HID_DESCRIPTOR		0x21

#define USB_ENDPOINT_TYPE_CONTROL	0x00
#define USB_ENDPOINT_TYPE_ISOCHRONOUS	0x01
#define USB_ENDPOINT_TYPE_BULK		0x02
#define USB_ENDPOINT_TYPE_INTERRUPT	0x03

/* Function prototypes */
uint8_t		d12_get_data(void);
void		d12_set_data(uint8_t data);
void		d12_set_cmd(uint8_t cmd);
void		d12_write_cmd(uint8_t command, const uint8_t *buffer, uint8_t count);
void		d12_read_cmd(uint8_t command, uint8_t *buffer, uint8_t count);
uint8_t		d12_read_endpt(uint8_t endpt, uint8_t *buffer);

void		d12_ep0_irq(void);
void		d12_handle_setup(void);
void		d12_getdescriptor(USB_SETUP_REQUEST *setuppkt);
void		d12_stallendpt(uint8_t ep);
uint8_t		d12_read_endpt(uint8_t endpt, uint8_t *buffer);
void		d12_write_endpt(uint8_t endpt, const uint8_t *buffer, uint8_t bytes);
void		d12_send_data_ep0(void);
void		d12_receive_data_ep1(void);
void		d12_send_data_ep2(void);
void		d12_receive_data_ep2(void);

void 		reset(void);

void		usb_init(void);
void		usb_intr(void);
void		usb_gendata(void);

/*
 * The PDIUSBD12 is wired up like so
 *
 *  PDI		     AVR	
 * ======================
 * D7:0     <=>     PA7:0	   
 * RD_N     <=      PB0
 * INT_N     =>     PB1
 * WR_N     <=      PB2
 * A0       <=      PB3 (0 = data, 1 = cmd)
 * SUSPEND  <=>     PB4
 */

#define PDIPORT	PORTA
#define PDIDDR	PORTA
#define PDICTL	PORTB
#define PDIRD	PB0
#define PDIINT	PB1
#define PDIWR	PB2
#define PDIA0	PB3
#define PDISUSP	PB4