Code:
/**
Bibliothek für RS232-Kommuniktion mit dem AT90USB1287
Folgende Funktionen müssen/können aufgerufen werden:
USB_initTask (); einmalig beim start aufrufen
USB_task (); regelmäßig im Hauptprogramm aufrufen
bool USB_testUARTreceived (void);
char USB_getUARTchar (void);
bool USB_sendReadyUART (void);
void USB_sendUARTchar (char senData);
void USB_sendUARTbuffer (U8 *buffer, U8 nb_data);
nützliches zur Abfrage:
bool USB_connected; Flag, ob USB eingesteckt ist
bool USB_isDeviceEnumerated Flag, ob USB am PC angemeldet ist
Folgende Sachen können noch konfiguriert werden:
**/
#define USB_deviceBusPowered 0x00
#define USB_deviceSelfPowered 0x01
#define USB_deviceRemoteWakeUp 0x02
#define USB_deviceBatteryPowered 0x04
#define USB_devicePower USB_deviceBusPowered
#define USB_maxPower 250 // 500 mA (50 = 100mA)
//------------------------------------------------------------------------------------------------------------------------------
#include <compiler.h>
#include <avr/io.h>
#include <avr/power.h>
#define USB_vidATMEL 0x03EB
#define USB_pidMegaCDC 0x2018
#define USB_endpointBulk 0x02
#define USB_endpointInterrupt 0x03
#define USB_endpointIn 0x80
#define USB_CDCglobClass 0x02
#define USB_CDCcommClass 0x02
#define USB_CDCdataClass 0x0A
#define USB_CDCcommSubClass 0x02
#define USB_CDCdataSubClass 0x00
#define USB_CDCcommProtocol 0x01
#define USB_CDCdataProtocol 0x00
#define USB_specification 0x0200
#define USB_deviceSubClass 0 // each configuration has its own sub-class
#define USB_deviceProtocol 0 // each configuration has its own protocol
#define USB_EPcontrolLength 64
#define USB_releaseNumber 0x1000
#define USB_manIndex 0x00
#define USB_prodIndex 0x00
#define USB_SNindex 0x00 // No serial number field
#define USB_NBconfiguration 1
#define USB_endpointTX 0x01
#define USB_endpointRX 0x02
#define USB_endpointInterface 0x03
#define USB_interfaceCount 2 // Number of interfaces
#define USB_confNB 1
#define USB_confINDEX 0
#define USB_confAttributesReserved 0x80
#define USB_confBusPowered (USB_confAttributesReserved)
#define USB_interface0number 0
#define USB_interface0alternate 0
#define USB_interface0endpoint 1
#define USB_interface0class USB_CDCcommClass // CDC ACM Com
#define USB_interface0subClass USB_CDCcommSubClass
#define USB_interface0protocol USB_CDCcommProtocol
#define USB_interface0index 0
#define USB_interface1number 1
#define USB_interface1alternate 0
#define USB_interface1endpoint 2
#define USB_interface1class USB_CDCdataClass // CDC ACM Data
#define USB_interface1subClass USB_CDCdataSubClass
#define USB_interface1protocol USB_CDCdataProtocol
#define USB_interface1index 0
#define USB_interfaceEPsize 0x20
#define USB_endpoint3number USB_endpointIn | USB_endpointInterface
#define USB_endpoint3attributes USB_endpointInterrupt
#define USB_endpoint3size USB_interfaceEPsize
#define USB_endpoint3interval 0xFF //ms interrupt pooling from host
#define USB_interfaceTXEPsize 0x40
#define USB_endpoint1number USB_endpointIn | USB_endpointTX
#define USB_endpoint1attributes USB_endpointBulk
#define USB_endpoint1size USB_interfaceTXEPsize
#define USB_endpoint1interval 0x00
#define USB_interfaceRXEPsize 0x40
#define USB_endpoint2number USB_endpointTX
#define USB_endpoint2attributes USB_endpointBulk
#define USB_endpoint2size USB_interfaceRXEPsize
#define USB_endpoint2interval 0x00
#define USB_descriptorLangID 0x00
#define USB_languageID 0x0409
#define USB_endpointCount 7
#define USB_endpointControl 0
#define USB_setupRecipientDevice (0)
#define USB_setupRecipientInterface (1)
#define USB_setupRecipientEndpoint (2)
#define USB_descriptorDevice 0x01
#define USB_descriptorConfiguration 0x02
#define USB_descriptorString 0x03
#define USB_descriptorInterface 0x04
#define USB_descriptorEndpoint 0x05
#define USB_writeWordEnumStruc(x) (x)
typedef struct
{
U8 bLength; // Size of this descriptor in bytes
U8 bDescriptorType; // DEVICE descriptor type
U16 bscUSB; // Binay Coded Decimal Spec. release
U8 bDeviceClass; // Class code assigned by the USB
U8 bDeviceSubClass; // Sub-class code assigned by the USB
U8 bDeviceProtocol; // Protocol code assigned by the USB
U8 bMaxPacketSize0; // Max packet size for EP0
U16 idVendor; // Vendor ID. ATMEL = 0x03EB
U16 idProduct; // Product ID assigned by the manufacturer
U16 bcdDevice; // Device release number
U8 iManufacturer; // Index of manu. string descriptor
U8 iProduct; // Index of prod. string descriptor
U8 iSerialNumber; // Index of S.N. string descriptor
U8 bNumConfigurations; // Number of possible configurations
} USB_structDeviceDescriptor;
typedef struct
{
U8 bLength; // size of this descriptor in bytes
U8 bDescriptorType; // CONFIGURATION descriptor type
U16 wTotalLength; // total length of data returned
U8 bNumInterfaces; // number of interfaces for this conf.
U8 bConfigurationValue; // value for SetConfiguration resquest
U8 iConfiguration; // index of string descriptor
U8 bmAttibutes; // Configuration characteristics
U8 MaxPower; // maximum power consumption
} USB_structConfigurationDescriptor;
typedef struct
{
U8 bLength; // size of this descriptor in bytes
U8 bDescriptorType; // INTERFACE descriptor type
U8 bInterfaceNumber; // Number of interface
U8 bAlternateSetting; // value to select alternate setting
U8 bNumEndpoints; // Number of EP except EP 0
U8 bInterfaceClass; // Class code assigned by the USB
U8 bInterfaceSubClass; // Sub-class code assigned by the USB
U8 bInterfaceProtocol; // Protocol code assigned by the USB
U8 iInterface; // Index of string descriptor
} USB_structInterfaceDescriptor;
typedef struct
{
U8 bLength; // Size of this descriptor in bytes
U8 bDescriptorType; // ENDPOINT descriptor type
U8 bEndpointAddress; // Address of the endpoint
U8 bmAttributes; // Endpoint's attributes
U16 wMaxPacketSize; // Maximum packet size for this EP
U8 bInterval; // Interval for polling EP in ms
} USB_structEndpointDescritptor;
typedef struct
{
U8 bLength; // size of this descriptor in bytes
U8 bDescriptorType; // STRING descriptor type
U16 wlangid; // language id
} USB_structLanguageID;
typedef struct
{
USB_structConfigurationDescriptor cfg;
USB_structInterfaceDescriptor ifc0;
U8 CS_INTERFACE[19];
USB_structEndpointDescritptor ep3;
USB_structInterfaceDescriptor ifc1;
USB_structEndpointDescritptor ep1;
USB_structEndpointDescritptor ep2;
} USB_structUserConfigurationDescriptor;
typedef struct
{
U32 dwDTERate;
U8 bCharFormat;
U8 bParityType;
U8 bDataBits;
} USB_structLineCoding;
typedef union
{
U8 all;
struct { U8 DTR: 1;
U8 RTS: 1;
U8 unused: 6;
};
} USB_structLineStatus;
code USB_structDeviceDescriptor USB_deviceDescriptor = { sizeof (USB_deviceDescriptor),
USB_descriptorDevice,
USB_writeWordEnumStruc (USB_specification),
USB_CDCglobClass,
USB_deviceSubClass,
USB_deviceProtocol,
USB_EPcontrolLength,
USB_writeWordEnumStruc (USB_vidATMEL),
USB_writeWordEnumStruc (USB_pidMegaCDC),
USB_writeWordEnumStruc (USB_releaseNumber),
USB_manIndex,
USB_prodIndex,
USB_SNindex,
USB_NBconfiguration};
code USB_structUserConfigurationDescriptor USB_configurationDescriptor = { { sizeof (USB_structConfigurationDescriptor),
USB_descriptorConfiguration,
0x0043,
USB_interfaceCount,
USB_confNB,
USB_confINDEX,
USB_confBusPowered,
USB_maxPower},
{ sizeof (USB_structInterfaceDescriptor),
USB_descriptorInterface,
USB_interface0number,
USB_interface0alternate,
USB_interface0endpoint,
USB_interface0class,
USB_interface0subClass,
USB_interface0protocol,
USB_interface0index},
{ 0x05, 0x24, 0x00, 0x10, 0x01, 0x05, 0x24, 0x01, 0x03, 0x01,
0x04, 0x24, 0x02, 0x06,0x05, 0x24, 0x06, 0x00, 0x01},
{ sizeof (USB_structEndpointDescritptor),
USB_descriptorEndpoint,
USB_endpoint3number,
USB_endpoint3attributes,
USB_writeWordEnumStruc (USB_endpoint3size),
USB_endpoint3interval},
{ sizeof (USB_structInterfaceDescriptor),
USB_descriptorInterface,
USB_interface1number,
USB_interface1alternate,
USB_interface1endpoint,
USB_interface1class,
USB_interface1subClass,
USB_interface1protocol,
USB_interface1index},
{ sizeof (USB_structEndpointDescritptor),
USB_descriptorEndpoint,
USB_endpoint1number,
USB_endpoint1attributes,
USB_writeWordEnumStruc (USB_endpoint1size),
USB_endpoint1interval},
{ sizeof (USB_structEndpointDescritptor),
USB_descriptorEndpoint,
USB_endpoint2number,
USB_endpoint2attributes,
USB_writeWordEnumStruc (USB_endpoint2size),
USB_endpoint2interval}
};
code USB_structLanguageID USB_userLanguageID = { sizeof (USB_userLanguageID),
USB_descriptorString,
USB_writeWordEnumStruc (USB_languageID)};
PGM_VOID_P USB_pbuffer;
USB_structLineCoding USB_lineCoding;
USB_structLineStatus USB_lineStatus;
volatile U8 USB_requestBreak = FALSE;
U8 USB_endpointStatus[USB_endpointCount];
U8 USB_dataToTransfer;
volatile U8 USB_configurationNumber;
volatile U8 USB_remoteWakeUpFeature = DISABLE;
static U8 USB_deviceStatus = USB_devicePower;
bool USB_connected = FALSE;
volatile U16 USB_gEvent = 0;
U8 USB_RXcounter = 0;
void USB_initTask (void);
void USB_task (void);
U8 USB_configEndpoint (U8 config0, U8 config1);
U8 USB_initDevice (void);
void USB_processRequest (void);
static bool USB_getDescriptor (void);
static void USB_setAddress (void);
static bool USB_setConfiguration (void);
static void USB_getConfiguration (void);
static bool USB_getStatus (U8 bmRequestType);
static bool USB_setFeature (U8 bmRequestType);
static bool USB_clearFeature (U8 bmRequestType);
static bool USB_getInterface (void);
static bool USB_setInterface (void);
bool USB_readUserRequest (U8 type, U8 request);
void USB_initUserEndpoint (U8 conf_nb);
U8 USB_getUserInterface (U16 wInterface);
void USB_resetUserInterface (U16 wInterface, U8 alternate_setting);
bool USB_getUserDescriptor (U8 type, U8 string);
void USB_CDCgetLineCoding (void);
void USB_CDCsetLineCoding (void);
void USB_CDCsetControlLineState (U16 state);
void USB_CDCsendBreak (U16 break_duration);
void USB_initDeviceTask (void);
void USB_startDevice (void);
void USB_deviceTask (void);
void USB_initUART (void);
bool USB_testUARTreceived (void);
char USB_getUARTchar (void);
bool USB_sendReadyUART (void);
void USB_sendUARTchar (char sendData);
void USB_sendUARTbuffer (U8 *buffer, U8 nb_data);
#define USB_sendEvent(x) (USB_gEvent |= (1<<x))
#define USB_ackEvent(x) (USB_gEvent &= ~(1<<x))
#define USB_isEvent(x) ((USB_gEvent & (1<<x)) ? TRUE : FALSE)
#define USB_eventPowered 1 // USB plugged
#define USB_eventUnPowered 2 // USB un-plugged
#define USB_eventSuspend 5 // USB suspend
#define USB_eventWakeUp 6 // USB wake up
#define USB_eventResume 7 // USB resume
#define USB_eventReset 8 // USB reset
#define USB_maskEndpointDir 0x7F
#define USB_maskUADD 0x7F
#define USB_typeControl 0
#define USB_typeIsochronous 1
#define USB_typeBulk 2
#define USB_typeInterrupt 3
#define USB_directionOut 0
#define USB_directionIn 1
#define USB_size8 0
#define USB_size16 1
#define USB_size32 2
#define USB_size64 3
#define USB_size128 4
#define USB_size256 5
#define USB_size512 6
#define USB_size1024 7
#define USB_bankCount1 0
#define USB_bankCount2 1
#define USB_NYETenabled 0
#define USB_NYETdisabled 1
#define USB_selectEndpoint(ep) (UENUM = (U8)ep)
#define USB_resetEndpoint(ep) (UERST = 1 << (U8)ep, UERST = 0)
#define USB_enableEndpoint (UECONX |= (1<<EPEN))
#define USB_enableStallHandshake (UECONX |= (1<<STALLRQ))
#define USB_resetDataToggle (UECONX |= (1<<RSTDT))
#define USB_disableStallHandshake (UECONX |= (1<<STALLRQC))
#define USB_isEndpointEnabled ((UECONX & (1<<EPEN)) ? TRUE : FALSE)
#define USB_allocateMemory (UECFG1X |= (1<<ALLOC))
#define USB_buildEndpoint0config(type, dir, nyet) ((type<<6) | (nyet<<1) | (dir))
#define USB_buildEndpoint1config(size, bank) ((size<<4) | (bank<<2))
#define USB_buildEndpointConfiguration(num, type, dir, size, bank, nyet) (USB_selectEndpoint(num), USB_configEndpoint(USB_buildEndpoint0config(type, dir, nyet), USB_buildEndpoint1config(size, bank)))
#define USB_enableRegulator (UHWCON |= (1<<UVREGE))
#define USB_disableUIDpin (UHWCON &= ~(1<<UIDE))
#define USB_forceDeviceMode (USB_disableUIDpin, UHWCON |= (1<<UIMOD))
#define USB_enable (USBCON |= ((1<<USBE) | (1<<OTGPADE)))
#define USB_disable (USBCON &= ~((1<<USBE) | (1<<OTGPADE)))
#define USB_selectDevice (USBCON &= ~(1<<HOST))
#define USB_freezeClock (USBCON |= (1<<FRZCLK))
#define USB_unfreezeClock (USBCON &= ~(1<<FRZCLK))
#define USB_isIDdevice ((USBSTA & (1<<ID)) ? TRUE : FALSE)
#define USB_isVBUShigh ((USBSTA & (1<<VBUS)) ? TRUE : FALSE)
#define USB_isVBUSlow ((USBSTA & (1<<VBUS)) ? FALSE : TRUE)
#define USB_detach (UDCON |= (1<<DETACH))
#define USB_attach (UDCON &= ~(1<<DETACH))
#define USB_disableResumeInterrupt (UDIEN &= ~(1<<EORSME))
#define USB_isResumeInterruptEnabled ((UDIEN & (1<<EORSME)) ? TRUE : FALSE)
#define USB_ackResume (UDINT = ~(1<<EORSMI))
#define USB_isResume ((UDINT & (1<<EORSMI)) ? TRUE : FALSE)
#define USB_enableWakeUpInterrupt (UDIEN |= (1<<WAKEUPE))
#define USB_disableWakeUpInterrupt (UDIEN &= ~(1<<WAKEUPE))
#define USB_isWakeUpInterruptEnabled ((UDIEN & (1<<WAKEUPE)) ? TRUE : FALSE)
#define USB_ackWakeUp (UDINT = ~(1<<WAKEUPI))
#define USB_isWakeUp ((UDINT & (1<<WAKEUPI)) ? TRUE : FALSE)
#define USB_enableResetInterrupt (UDIEN |= (1<<EORSTE))
#define USB_isResetInterruptEnabled ((UDIEN & (1<<EORSTE)) ? TRUE : FALSE)
#define USB_ackReset (UDINT = ~(1<<EORSTI))
#define USB_isReset ((UDINT & (1<<EORSTI)) ? TRUE : FALSE)
#define USB_isSOFinterruptEnabled ((UDIEN & (1<<SOFE)) ? TRUE : FALSE)
#define USB_ackSOF (UDINT = ~(1<<SOFI))
#define USB_isSOF ((UDINT & (1<<SOFI)) ? TRUE : FALSE)
#define USB_enableSuspendInterrupt (UDIEN |= (1<<SUSPE))
#define USB_disableSuspendInterrupt (UDIEN &= ~(1<<SUSPE))
#define USB_isSuspendInterruptEnabled ((UDIEN & (1<<SUSPE)) ? TRUE : FALSE)
#define USB_ackSuspend (UDINT = ~(1<<SUSPI))
#define USB_isSuspend ((UDINT & (1<<SUSPI)) ? TRUE : FALSE)
#define USB_enableAddress (UDADDR |= (1<<ADDEN))
#define USB_configAddress(addr) (UDADDR = (UDADDR & (1<<ADDEN)) | ((U8)addr & USB_maskUADD))
#define USB_isEndpointConfigured ((UESTA0X & (1<<CFGOK)) ? TRUE : FALSE)
#define USB_ackFiFoCon (UEINTX &= ~(1<<FIFOCON))
#define USB_ackNakOut (UEINTX &= ~(1<<NAKOUTI))
#define USB_ackReceiveSetup (UEINTX &= ~(1<<RXSTPI))
#define USB_ackReceiveOut (UEINTX &= ~(1<<RXOUTI), USB_ackFiFoCon)
#define USB_ackInReady (UEINTX &= ~(1<<TXINI), USB_ackFiFoCon)
#define USB_isWriteEnabled (UEINTX&(1<<RWAL))
#define USB_isReadControlEnabled (UEINTX&(1<<TXINI))
#define USB_isNakOutSent (UEINTX&(1<<NAKOUTI))
#define USB_isReceiveSetup (UEINTX&(1<<RXSTPI))
#define USB_isReceiveOut (UEINTX&(1<<RXOUTI))
#define USB_isInReady (UEINTX&(1<<TXINI))
#define USB_sendControlIn (UEINTX &= ~(1<<TXINI))
#define USB_ackControlOut (UEINTX &= ~(1<<RXOUTI))
#define USB_readByte (UEDATX)
#define USB_writeByte(byte) (UEDATX = (U8)byte)
#define USB_writePGMbyte(byte) (USB_writeByte (pgm_read_byte_near ((unsigned int)byte)))
#define USB_byteCounter ((((U16)UEBCHX) << 8) | (UEBCLX))
#define USB_getDeviceDescriptorPointer (&(USB_deviceDescriptor.bLength))
#define USB_getDeviceDescriptorLength (sizeof (USB_deviceDescriptor))
#define USB_getConfigDesciptorPointer (&(USB_configurationDescriptor.cfg.bLength))
#define USB_getConfigDesciptorLength (sizeof (USB_configurationDescriptor))
#define USB_setupDirHostToDevice (0<<7)
#define USB_setupDirDeviceToHost (1<<7)
#define USB_setupTypeStandard (0<<5)
#define USB_setupTypeClass (1<<5)
#define USB_setupSetStandardDevice (USB_setupDirHostToDevice | USB_setupTypeStandard | USB_setupRecipientDevice) // 0x00
#define USB_setupGetStandardDevice (USB_setupDirDeviceToHost | USB_setupTypeStandard | USB_setupRecipientDevice) // 0x80
#define USB_setupSetStandardInterface (USB_setupDirHostToDevice | USB_setupTypeStandard | USB_setupRecipientInterface) // 0x01
#define USB_setupGetStandardInterface (USB_setupDirDeviceToHost | USB_setupTypeStandard | USB_setupRecipientInterface) // 0x81
#define USB_setupSetStandardEndpoint (USB_setupDirHostToDevice | USB_setupTypeStandard | USB_setupRecipientEndpoint) // 0x02
#define USB_setupGetStandardEndpoint (USB_setupDirDeviceToHost | USB_setupTypeStandard | USB_setupRecipientEndpoint) // 0x82
#define USB_setupSetClassInterface (USB_setupDirHostToDevice | USB_setupTypeClass | USB_setupRecipientInterface) // 0x21
#define USB_setupGetClassInterface (USB_setupDirDeviceToHost | USB_setupTypeClass | USB_setupRecipientInterface) // 0xA1
#define USB_setupGetStatus 0x00
#define USB_setupGetDevice 0x01
#define USB_setupClearFeature 0x01
#define USB_setupGetString 0x03
#define USB_setupSetFeature 0x03
#define USB_setupSetAddress 0x05
#define USB_setupGetDescriptor 0x06
#define USB_setupSetDescriptor 0x07
#define USB_setupGetConfiguration 0x08
#define USB_setupSetConfiguration 0x09
#define USB_setupGetInterface 0x0A
#define USB_setupSetInterface 0x0B
#define USB_setupSynchFrame 0x0C
#define USB_featureDeviceRemoteWakeUp 0x01
#define USB_featureEndpointHalt 0x00
#define USB_CDCsetupSetLineCoding 0x20
#define USB_CDCsetupGetLineCoding 0x21
#define USB_CDCsetupSetControlLineState 0x22
#define USB_CDCsetupSendBreak 0x23
#define USB_remoteWakeUp 1
#define USB_isDeviceEnumerated ((USB_configurationNumber != 0) ? TRUE : FALSE)
#if (defined (__AVR_AT90USB1287__) || defined (__AVR_AT90USB1286__))
#define USB_PLLx03 ((1<<PLLP2) | (0<<PLLP1) | (1<<PLLP0))
#elif (defined (__AVR_AT90USB647__) || defined (__AVR_AT90USB646__) || defined (__AVR_ATmega32U6__))
#define USB_PLLx03 ((1<<PLLP2) | (1<<PLLP1) | (0<<PLLP0))
#endif
#define USB_PLLx06 ((0<<PLLP2) | (1<<PLLP1) | (1<<PLLP0))
#define USB_startPLL(clockfactor) (PLLCSR = (clockfactor | (1<<PLLE)))
#define USB_isPLLready (PLLCSR & (1<<PLOCK))
#define USB_waitPLLready while (!(PLLCSR & (1<<PLOCK)))
#define USB_stopPLL (PLLCSR &= (~(1<<PLLE)), PLLCSR = 0)
#if (F_CPU == 8000000)
#define USB_autoStartPLL USB_startPLL (USB_PLLx06)
#elif (F_CPU == 16000000)
#define USB_autoStartPLL USB_startPLL (USB_PLLx03)
#else
#error "F_CPU should be defined with 8000000 or 16000000"
#endif
Lesezeichen