4 // Macros for access to USB registers of Atmels AT90USB microcontrollers
5 // This file contains low level register stuff as described in
6 // Atmels AT90USB datasheet 7593D-AVR-07/06
7 // S. Salewski 21-MAR-2007
8 // B. Sauter (modified for usb host stack) 2007
11 // A few macros for bit fiddling
12 #define SetBit(adr, bit) (adr |= (1<<bit))
13 #define ClearBit(adr, bit) (adr &= ~(1<<bit))
14 #define BitIsSet(adr, bit) (adr & (1<<bit))
15 #define BitIsClear(adr, bit) (!(adr & (1<<bit)))
19 // PLL clock for USB interface, section 6.11, page 50
20 // PLLCSR (PLL Control and Status Register)
21 // set PLL prescaler according to XTAL crystal frequency
22 //#define UsbXTALFrequencyIs2MHz() PLLCSR = (PLLCSR & ~(15<<1))
23 //#define UsbXTALFrequencyIs4MHz() PLLCSR = (PLLCSR & ~(15<<1)) | (1<<2)
24 //#define UsbXTALFrequencyIs6MHz() PLLCSR = (PLLCSR & ~(15<<1)) | (2<<2)
25 //#define UsbXTALFrequencyIs8MHz() PLLCSR = (PLLCSR & ~(15<<1)) | (3<<2)
26 //#define UsbXTALFrequencyIs12MHz() PLLCSR = (PLLCSR & ~(15<<1)) | (4<<2)
27 //#define UsbXTALFrequencyIs16MHz() PLLCSR = (PLLCSR & ~(15<<1)) | (5<<2)
28 #if (F_CPU == 2000000)
30 #elif (F_CPU == 4000000)
32 #elif (F_CPU == 6000000)
34 #elif (F_CPU == 8000000)
36 #elif (F_CPU == 12000000)
38 #elif (F_CPU == 16000000)
41 #error "XTAL-Frequency has to be 2, 4, 6, 8, 12 or 16 MHz for USB devices!"
43 #define UsbSetPLL_CPU_Frequency() PLLCSR = (_pre_<<2)
44 #define UsbEnablePLL() SetBit(PLLCSR, PLLE)
45 #define UsbDisablePLL() ClearBit(PLLCSR, PLLE)
46 #define UsbIsPLL_Locked() BitIsSet(PLLCSR, PLOCK)
47 #define UsbWaitPLL_Locked() while (!(PLLCSR & (1<<PLOCK)));
48 #define UsbEnableClock() ClearBit(USBCON, FRZCLK)
50 //USB general registers, section 21.12.1, page 263 of datasheet
51 // UHWCON (UsbHardWareCONfiguration)
52 #define UsbSetDeviceMode() SetBit(UHWCON, UIMOD) // select host or device mode manually
53 #define UsbSetHostMode() ClearBit(UHWCON, UIMOD)
54 #define UsbEnableUID_ModeSelection() SetBit(UHWCON, UIDE) // enable mode selection by UID pin
55 #define UsbDisableUID_ModeSelection() ClearBit(UHWCON, UIDE)
56 #define UsbEnableUVCON_PinControl() SetBit(UHWCON, UVCONE) // enable UVCON pin control, figure 21-7
57 #define UsbDisableUVCON_PinControl() ClearBit(UHWCON, UVCONE)
58 #define UsbEnablePadsRegulator() SetBit(UHWCON, UVREGE) // USB pads (D+, D-) supply
59 #define UsbDisablePadsRegulator() ClearBit(UHWCON, UVREGE)
61 // USBCON (USB CONfiguration)
62 #define UsbEnableController() SetBit(USBCON, USBE) // USB controller enable
63 #define UsbDisableController() ClearBit(USBCON, USBE) // reset and disable controller
64 #define UsbIsControllerEnabled() BitIsSet(USBCON, USBE)
65 #define UsbSetHostModeReg() SetBit(USBCON, HOST) // select multiplexed controller registers
66 #define UsbSetDeviceModeReg() ClearBit(USBCON, HOST) //
67 #define UsbFreezeClock() SetBit(USBCON, FRZCLK) // reduce power consumption
68 #define UsbEnableClock() ClearBit(USBCON, FRZCLK)
69 #define UsbIsClockFreezed() BitIsSet(USBCON, FRZCLK)
70 #define UsbEnableOTG_Pad() SetBit(USBCON, OTGPADE) // ??? is this the UID pad?
71 #define UsbDisableOTG_Pad() ClearBit(USBCON, OTGPADE)
72 #define UsbEnableID_TransitionInt() SetBit(USBCON, IDTE) // enable ID transition interrupt generation
73 #define UsbDisableID_TransitionInt() ClearBit(USBCON, IDTE)
74 #define UsbEnableVBUS_TransitionInt() SetBit(USBCON, VBUSTE) // enable VBUS transition interrupt
75 #define UsbDisableVBUS_TransitionInt() ClearBit(USBCON, VBUSTE)
77 // USBSTA (USBSTAtus, read only)
78 #define UsbIsFullSpeedMode() BitIsSet(USBSTA, SPEED) // set by hardware if controller is in fullspeed mode,
79 #define UsbIsLowSpeedMode() BitIsClear(USBSTA, SPEED) // use in host mode only, indeterminate in device mode
80 #define UsbIsUID_PinHigh() BitIsSet(USBSTA, ID) // query UID pad/pin
81 #define UsbIsVBUS_PinHigh() BitIsSet(USBSTA, VBUS) // query VBUS pad/pin
83 // USBINT (USBINTerrupt)
84 #define UsbIsIDTI_FlagSet() BitIsSet(USBINT, IDTI) // set by hardware if ID pin transition detected
85 #define UsbClearIDTI_Flag() ClearBit(USBINT, IDTI) // shall be cleared by software
86 #define UsbIsVBUSTI_FlagSet() BitIsSet(USBINT, VBUSTI) // set by hardware if transition on VBUS pad is detected
87 #define UsbClearVBUSTI_Flag() ClearBit(USBINT, VBUSTI) // shall be cleared by software
89 #endif /* _AT90USB_H */