Exemplo n.º 1
0
void udd_enable(void)
{
	uint8_t i;
	irqflags_t flags;

	// Sanity check Silicon revision
#if AVR8_PART_IS_DEFINED(ATxmega128A1U)
	// The part ATxmega128A1U Rev. J is not supported, please use new silicon revision.
	Assert(!(MCU_REVID < 0x0A));
#endif

#ifdef CONFIG_OSC_AUTOCAL_RC32MHZ_REF_OSC
# if CONFIG_OSC_AUTOCAL_RC32MHZ_REF_OSC == OSC_ID_USBSOF
	// RC oscillator calibration via USB Start Of Frame is not available
	// in low speed mode.
	// Thus, the calibration is disabled
	// when USB interface start in low speed mode
	DFLLRC32M.CTRL = 0;
# endif
#endif

#ifdef USB_DEVICE_LOW_SPEED
	// The USB hardware need of 6MHz in low speed mode
	sysclk_enable_usb(6);
	udd_set_low_speed();
#else
	// The USB hardware need of 48MHz in full speed mode
	sysclk_enable_usb(48);
	udd_set_full_speed();
#endif

// The XMEGA_A1U does not support the RC calibration through Keepalive (Low speed).
#if (!defined USB_DEVICE_LOW_SPEED) || (!XMEGA_A1U)
# ifdef CONFIG_OSC_AUTOCAL_RC32MHZ_REF_OSC
#   if CONFIG_OSC_AUTOCAL_RC32MHZ_REF_OSC == OSC_ID_USBSOF
	// The SOF calibration can be enabled
	DFLLRC32M.CTRL = DFLL_ENABLE_bm;
#   endif
# endif
#endif

	flags = cpu_irq_save();

	// Reset endpoints table
	for (i = 0; i < ((USB_DEVICE_MAX_EP + 1) * 2); i++) {
		udd_sram.ep_ctrl[i].CTRL = 0;
	}
#if (0!=USB_DEVICE_MAX_EP)
	// Reset internal variables
	for (i = 0; i < (USB_DEVICE_MAX_EP * 2); i++) {
		udd_ep_job[i].busy = false;
	}
#endif

	//** Enable USB hardware
	usb_pad_init();
	udd_set_nb_max_ep(USB_DEVICE_MAX_EP);
	udd_enable_interface();
	udd_enable_store_frame_number();
#if XMEGA_A1U
	Assert(((uint16_t)(&udd_sram) & 0x0F) == 0); /* check align on 16bit */
#else
	Assert(((uint16_t)(&udd_sram) & 0x01) == 0); /* check align on WORD */
#endif
	udd_set_ep_table_addr(udd_sram.ep_ctrl);
	// Enable TC fifo management
	udd_enable_fifo();
	udd_reset_fifo();
	// Enable Interrupt USB Device
	udd_enable_interrupt(UDD_USB_INT_LEVEL);

#ifndef UDD_NO_SLEEP_MGR
	// Initialize the sleep mode authorized for the USB suspend mode
	udd_b_idle = false;
	sleepmgr_lock_mode(USBC_SLEEP_MODE_USB_SUSPEND);
#endif

#ifndef USB_DEVICE_ATTACH_AUTO_DISABLE
	udd_attach();
#endif
	cpu_irq_restore(flags);
}
Exemplo n.º 2
0
void udd_enable(void)
{
	uint8_t i;
	irqflags_t flags;

	// Sanity check Silicon revision
#if AVR8_PART_IS_DEFINED(ATxmega128A1U)
	// The part ATxmega128A1U Rev. J is not supported, please use new silicon revision.
	Assert(!(MCU_REVID < 0x0A));
#endif

    uint8_t dfll_save = DFLLRC32M.CTRL;
    DFLLRC32M.CTRL = 0;

#ifdef USB_DEVICE_LOW_SPEED
	// The USB hardware need of 6MHz in low speed mode
	//sysclk_enable_usb(6);
	udd_set_low_speed();
#else
	// The USB hardware need of 48MHz in full speed mode
	//sysclk_enable_usb(48);
	udd_set_full_speed();
#endif

    DFLLRC32M.CTRL = dfll_save;

	flags = cpu_irq_save();

	// Reset endpoints table
	for (i = 0; i < ((USB_DEVICE_MAX_EP + 1) * 2); i++) {
		udd_sram.ep_ctrl[i].CTRL = 0;
	}
#if (0!=USB_DEVICE_MAX_EP)
	// Reset internal variables
	for (i = 0; i < (USB_DEVICE_MAX_EP * 2); i++) {
		udd_ep_job[i].busy = false;
	}
#endif

	//** Enable USB hardware
	usb_pad_init();
	udd_set_nb_max_ep(USB_DEVICE_MAX_EP);
	udd_enable_interface();
	udd_enable_store_frame_number();
#if XMEGA_A1U
	Assert(((uint16_t)(&udd_sram) & 0x0F) == 0); /* check align on 16bit */
#else
	Assert(((uint16_t)(&udd_sram) & 0x01) == 0); /* check align on WORD */
#endif
	udd_set_ep_table_addr(udd_sram.ep_ctrl);
	// Enable TC fifo management
	udd_enable_fifo();
	udd_reset_fifo();
	// Enable Interrupt USB Device
	udd_enable_interrupt(UDD_USB_INT_LEVEL);

#ifndef UDD_NO_SLEEP_MGR
	// Initialize the sleep mode authorized for the USB suspend mode
	udd_b_idle = false;
	sleepmgr_lock_mode(USBC_SLEEP_MODE_USB_SUSPEND);
#endif

#ifndef USB_DEVICE_ATTACH_AUTO_DISABLE
	udd_attach();
#endif
	cpu_irq_restore(flags);
}