mirror of
https://github.com/hathach/tinyusb.git
synced 2025-01-31 05:52:55 +08:00
samd support suspend and resume. Though cannot distinguish between Suspend and Disconnect
should work with #47
This commit is contained in:
parent
491b5936d5
commit
dd9c441a61
@ -71,6 +71,11 @@
|
|||||||
<file file_name="../../../../../hw/mcu/microchip/samd/asf4/samd51/hpl/gclk/hpl_gclk.c" />
|
<file file_name="../../../../../hw/mcu/microchip/samd/asf4/samd51/hpl/gclk/hpl_gclk.c" />
|
||||||
</folder>
|
</folder>
|
||||||
</folder>
|
</folder>
|
||||||
|
<folder Name="hal">
|
||||||
|
<folder Name="src">
|
||||||
|
<file file_name="../../../../../hw/mcu/microchip/samd/asf4/samd51/hal/src/hal_atomic.c" />
|
||||||
|
</folder>
|
||||||
|
</folder>
|
||||||
</folder>
|
</folder>
|
||||||
</folder>
|
</folder>
|
||||||
</folder>
|
</folder>
|
||||||
|
@ -38,19 +38,20 @@ static ATTR_ALIGNED(4) UsbDeviceDescBank sram_registers[8][2];
|
|||||||
static ATTR_ALIGNED(4) uint8_t _setup_packet[8];
|
static ATTR_ALIGNED(4) uint8_t _setup_packet[8];
|
||||||
|
|
||||||
// Setup the control endpoint 0.
|
// Setup the control endpoint 0.
|
||||||
static void bus_reset(void) {
|
static void bus_reset(void)
|
||||||
// Max size of packets is 64 bytes.
|
{
|
||||||
UsbDeviceDescBank* bank_out = &sram_registers[0][TUSB_DIR_OUT];
|
// Max size of packets is 64 bytes.
|
||||||
bank_out->PCKSIZE.bit.SIZE = 0x3;
|
UsbDeviceDescBank* bank_out = &sram_registers[0][TUSB_DIR_OUT];
|
||||||
UsbDeviceDescBank* bank_in = &sram_registers[0][TUSB_DIR_IN];
|
bank_out->PCKSIZE.bit.SIZE = 0x3;
|
||||||
bank_in->PCKSIZE.bit.SIZE = 0x3;
|
UsbDeviceDescBank* bank_in = &sram_registers[0][TUSB_DIR_IN];
|
||||||
|
bank_in->PCKSIZE.bit.SIZE = 0x3;
|
||||||
|
|
||||||
UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[0];
|
UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[0];
|
||||||
ep->EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(0x1) | USB_DEVICE_EPCFG_EPTYPE1(0x1);
|
ep->EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(0x1) | USB_DEVICE_EPCFG_EPTYPE1(0x1);
|
||||||
ep->EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRCPT0 | USB_DEVICE_EPINTENSET_TRCPT1 | USB_DEVICE_EPINTENSET_RXSTP;
|
ep->EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRCPT0 | USB_DEVICE_EPINTENSET_TRCPT1 | USB_DEVICE_EPINTENSET_RXSTP;
|
||||||
|
|
||||||
// Prepare for setup packet
|
// Prepare for setup packet
|
||||||
dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet));
|
dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -79,6 +80,7 @@ void dcd_init (uint8_t rhport)
|
|||||||
USB->DEVICE.CTRLA.reg = USB_CTRLA_MODE_DEVICE | USB_CTRLA_ENABLE | USB_CTRLA_RUNSTDBY;
|
USB->DEVICE.CTRLA.reg = USB_CTRLA_MODE_DEVICE | USB_CTRLA_ENABLE | USB_CTRLA_RUNSTDBY;
|
||||||
while (USB->DEVICE.SYNCBUSY.bit.ENABLE == 1) {}
|
while (USB->DEVICE.SYNCBUSY.bit.ENABLE == 1) {}
|
||||||
|
|
||||||
|
USB->DEVICE.INTFLAG.reg |= USB->DEVICE.INTFLAG.reg; // clear pending
|
||||||
USB->DEVICE.INTENSET.reg = USB_DEVICE_INTENSET_SOF | USB_DEVICE_INTENSET_EORST;
|
USB->DEVICE.INTENSET.reg = USB_DEVICE_INTENSET_SOF | USB_DEVICE_INTENSET_EORST;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,6 +105,10 @@ void dcd_set_address (uint8_t rhport, uint8_t dev_addr)
|
|||||||
while (USB->DEVICE.DeviceEndpoint[0].EPSTATUS.bit.BK1RDY == 1) {}
|
while (USB->DEVICE.DeviceEndpoint[0].EPSTATUS.bit.BK1RDY == 1) {}
|
||||||
|
|
||||||
USB->DEVICE.DADD.reg = USB_DEVICE_DADD_DADD(dev_addr) | USB_DEVICE_DADD_ADDEN;
|
USB->DEVICE.DADD.reg = USB_DEVICE_DADD_DADD(dev_addr) | USB_DEVICE_DADD_ADDEN;
|
||||||
|
|
||||||
|
// Enable SUSPEND interrupt since the bus signal D+/D- are stable now.
|
||||||
|
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTENCLR_SUSPEND; // clear pending
|
||||||
|
USB->DEVICE.INTENSET.reg = USB_DEVICE_INTENSET_SUSPEND;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dcd_set_config (uint8_t rhport, uint8_t config_num)
|
void dcd_set_config (uint8_t rhport, uint8_t config_num)
|
||||||
@ -111,17 +117,13 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num)
|
|||||||
(void) config_num;
|
(void) config_num;
|
||||||
// Nothing to do
|
// Nothing to do
|
||||||
|
|
||||||
#if 0
|
|
||||||
// Enable suspend to detect disconnection
|
|
||||||
// TODO need to distinguish Suspend vs Disconnect
|
|
||||||
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTENCLR_SUSPEND;
|
|
||||||
USB->DEVICE.INTENSET.reg |= USB_DEVICE_INTENSET_SUSPEND;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void dcd_remote_wakeup(uint8_t rhport)
|
void dcd_remote_wakeup(uint8_t rhport)
|
||||||
{
|
{
|
||||||
(void) rhport;
|
(void) rhport;
|
||||||
|
|
||||||
|
USB->DEVICE.CTRLB.bit.UPRSM = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------------------------*/
|
/*------------------------------------------------------------------*/
|
||||||
@ -305,35 +307,44 @@ void maybe_transfer_complete(void) {
|
|||||||
void USB_Handler(void)
|
void USB_Handler(void)
|
||||||
{
|
{
|
||||||
uint32_t int_status = USB->DEVICE.INTFLAG.reg & USB->DEVICE.INTENSET.reg;
|
uint32_t int_status = USB->DEVICE.INTFLAG.reg & USB->DEVICE.INTENSET.reg;
|
||||||
|
USB->DEVICE.INTFLAG.reg = int_status; // clear interrupt
|
||||||
|
|
||||||
/*------------- Interrupt Processing -------------*/
|
/*------------- Interrupt Processing -------------*/
|
||||||
if ( int_status & USB_DEVICE_INTFLAG_EORST )
|
|
||||||
{
|
|
||||||
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_EORST;
|
|
||||||
bus_reset();
|
|
||||||
dcd_event_bus_signal(0, DCD_EVENT_BUS_RESET, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( int_status & USB_DEVICE_INTFLAG_SOF )
|
if ( int_status & USB_DEVICE_INTFLAG_SOF )
|
||||||
{
|
{
|
||||||
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_SOF;
|
|
||||||
dcd_event_bus_signal(0, DCD_EVENT_SOF, true);
|
dcd_event_bus_signal(0, DCD_EVENT_SOF, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
// SAMD doesn't distinguish between Suspend and Disconnect state.
|
||||||
|
// Both condition will cause SUSPEND interrupt triggered.
|
||||||
|
// To prevent being triggered when D+/D- are not stable, SUSPEND interrupt is only
|
||||||
|
// enabled when we received SET_ADDRESS request and cleared on Bus Reset
|
||||||
if ( int_status & USB_DEVICE_INTFLAG_SUSPEND )
|
if ( int_status & USB_DEVICE_INTFLAG_SUSPEND )
|
||||||
{
|
{
|
||||||
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_SUSPEND;
|
// Enable wakeup interrupt
|
||||||
|
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_WAKEUP; // clear pending
|
||||||
|
USB->DEVICE.INTENSET.reg = USB_DEVICE_INTFLAG_WAKEUP;
|
||||||
|
|
||||||
// disable interrupt to prevent it continue to trigger
|
dcd_event_bus_signal(0, DCD_EVENT_SUSPEND, true);
|
||||||
USB->DEVICE.INTENCLR.reg = USB_DEVICE_INTENCLR_SUSPEND;
|
}
|
||||||
|
|
||||||
// TODO need to distinguish Suspend vs Disconnect
|
// Wakeup interrupt is only enabled when we got suspended.
|
||||||
// reset address to 0, force host to re-enumerate after resume
|
// Wakeup interrupt will disable itself
|
||||||
USB->DEVICE.DADD.reg = 0;
|
if ( int_status & USB_DEVICE_INTFLAG_WAKEUP )
|
||||||
dcd_event_bus_signal(0, DCD_EVENT_UNPLUGGED, true);
|
{
|
||||||
|
// disable wakeup interrupt itself
|
||||||
|
USB->DEVICE.INTENCLR.reg = USB_DEVICE_INTFLAG_WAKEUP;
|
||||||
|
dcd_event_bus_signal(0, DCD_EVENT_RESUME, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( int_status & USB_DEVICE_INTFLAG_EORST )
|
||||||
|
{
|
||||||
|
// Disable both suspend and wakeup interrupt
|
||||||
|
USB->DEVICE.INTENCLR.reg = USB_DEVICE_INTFLAG_WAKEUP | USB_DEVICE_INTFLAG_SUSPEND;
|
||||||
|
|
||||||
|
bus_reset();
|
||||||
|
dcd_event_bus_signal(0, DCD_EVENT_BUS_RESET, true);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
// Setup packet received.
|
// Setup packet received.
|
||||||
maybe_handle_setup_packet();
|
maybe_handle_setup_packet();
|
||||||
|
@ -38,19 +38,20 @@ static UsbDeviceDescBank sram_registers[8][2];
|
|||||||
static ATTR_ALIGNED(4) uint8_t _setup_packet[8];
|
static ATTR_ALIGNED(4) uint8_t _setup_packet[8];
|
||||||
|
|
||||||
// Setup the control endpoint 0.
|
// Setup the control endpoint 0.
|
||||||
static void bus_reset(void) {
|
static void bus_reset(void)
|
||||||
// Max size of packets is 64 bytes.
|
{
|
||||||
UsbDeviceDescBank* bank_out = &sram_registers[0][TUSB_DIR_OUT];
|
// Max size of packets is 64 bytes.
|
||||||
bank_out->PCKSIZE.bit.SIZE = 0x3;
|
UsbDeviceDescBank* bank_out = &sram_registers[0][TUSB_DIR_OUT];
|
||||||
UsbDeviceDescBank* bank_in = &sram_registers[0][TUSB_DIR_IN];
|
bank_out->PCKSIZE.bit.SIZE = 0x3;
|
||||||
bank_in->PCKSIZE.bit.SIZE = 0x3;
|
UsbDeviceDescBank* bank_in = &sram_registers[0][TUSB_DIR_IN];
|
||||||
|
bank_in->PCKSIZE.bit.SIZE = 0x3;
|
||||||
|
|
||||||
UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[0];
|
UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[0];
|
||||||
ep->EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(0x1) | USB_DEVICE_EPCFG_EPTYPE1(0x1);
|
ep->EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(0x1) | USB_DEVICE_EPCFG_EPTYPE1(0x1);
|
||||||
ep->EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRCPT0 | USB_DEVICE_EPINTENSET_TRCPT1 | USB_DEVICE_EPINTENSET_RXSTP;
|
ep->EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRCPT0 | USB_DEVICE_EPINTENSET_TRCPT1 | USB_DEVICE_EPINTENSET_RXSTP;
|
||||||
|
|
||||||
// Prepare for setup packet
|
// Prepare for setup packet
|
||||||
dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet));
|
dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -78,6 +79,8 @@ void dcd_init (uint8_t rhport)
|
|||||||
USB->DEVICE.CTRLB.reg = USB_DEVICE_CTRLB_SPDCONF_FS;
|
USB->DEVICE.CTRLB.reg = USB_DEVICE_CTRLB_SPDCONF_FS;
|
||||||
USB->DEVICE.CTRLA.reg = USB_CTRLA_MODE_DEVICE | USB_CTRLA_ENABLE | USB_CTRLA_RUNSTDBY;
|
USB->DEVICE.CTRLA.reg = USB_CTRLA_MODE_DEVICE | USB_CTRLA_ENABLE | USB_CTRLA_RUNSTDBY;
|
||||||
while (USB->DEVICE.SYNCBUSY.bit.ENABLE == 1) {}
|
while (USB->DEVICE.SYNCBUSY.bit.ENABLE == 1) {}
|
||||||
|
|
||||||
|
USB->DEVICE.INTFLAG.reg |= USB->DEVICE.INTFLAG.reg; // clear pending
|
||||||
USB->DEVICE.INTENSET.reg = USB_DEVICE_INTENSET_SOF | USB_DEVICE_INTENSET_EORST;
|
USB->DEVICE.INTENSET.reg = USB_DEVICE_INTENSET_SOF | USB_DEVICE_INTENSET_EORST;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,6 +111,10 @@ void dcd_set_address (uint8_t rhport, uint8_t dev_addr)
|
|||||||
while (USB->DEVICE.DeviceEndpoint[0].EPSTATUS.bit.BK1RDY == 1) {}
|
while (USB->DEVICE.DeviceEndpoint[0].EPSTATUS.bit.BK1RDY == 1) {}
|
||||||
|
|
||||||
USB->DEVICE.DADD.reg = USB_DEVICE_DADD_DADD(dev_addr) | USB_DEVICE_DADD_ADDEN;
|
USB->DEVICE.DADD.reg = USB_DEVICE_DADD_DADD(dev_addr) | USB_DEVICE_DADD_ADDEN;
|
||||||
|
|
||||||
|
// Enable SUSPEND interrupt since the bus signal D+/D- are stable now.
|
||||||
|
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTENCLR_SUSPEND; // clear pending
|
||||||
|
USB->DEVICE.INTENSET.reg = USB_DEVICE_INTENSET_SUSPEND;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dcd_set_config (uint8_t rhport, uint8_t config_num)
|
void dcd_set_config (uint8_t rhport, uint8_t config_num)
|
||||||
@ -120,6 +127,8 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num)
|
|||||||
void dcd_remote_wakeup(uint8_t rhport)
|
void dcd_remote_wakeup(uint8_t rhport)
|
||||||
{
|
{
|
||||||
(void) rhport;
|
(void) rhport;
|
||||||
|
|
||||||
|
USB->DEVICE.CTRLB.bit.UPRSM = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------------------------*/
|
/*------------------------------------------------------------------*/
|
||||||
@ -270,12 +279,42 @@ USB_TRFAIL1_PERR_0, USB_TRFAIL1_PERR_1, USB_TRFAIL1_PERR_2,
|
|||||||
USB_TRFAIL1_PERR_3, USB_TRFAIL1_PERR_4, USB_TRFAIL1_PERR_5,
|
USB_TRFAIL1_PERR_3, USB_TRFAIL1_PERR_4, USB_TRFAIL1_PERR_5,
|
||||||
USB_TRFAIL1_PERR_6, USB_TRFAIL1_PERR_7, USB_UPRSM, USB_WAKEUP */
|
USB_TRFAIL1_PERR_6, USB_TRFAIL1_PERR_7, USB_UPRSM, USB_WAKEUP */
|
||||||
void USB_0_Handler(void) {
|
void USB_0_Handler(void) {
|
||||||
uint32_t int_status = USB->DEVICE.INTFLAG.reg;
|
uint32_t int_status = USB->DEVICE.INTFLAG.reg & USB->DEVICE.INTENSET.reg;
|
||||||
|
|
||||||
/*------------- Interrupt Processing -------------*/
|
/*------------- Interrupt Processing -------------*/
|
||||||
|
// SAMD doesn't distinguish between Suspend and Disconnect state.
|
||||||
|
// Both condition will cause SUSPEND interrupt triggered.
|
||||||
|
// To prevent being triggered when D+/D- are not stable, SUSPEND interrupt is only
|
||||||
|
// enabled when we received SET_ADDRESS request and cleared on Bus Reset
|
||||||
|
if ( int_status & USB_DEVICE_INTFLAG_SUSPEND )
|
||||||
|
{
|
||||||
|
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_SUSPEND;
|
||||||
|
|
||||||
|
// Enable wakeup interrupt
|
||||||
|
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_WAKEUP; // clear pending
|
||||||
|
USB->DEVICE.INTENSET.reg = USB_DEVICE_INTFLAG_WAKEUP;
|
||||||
|
|
||||||
|
dcd_event_bus_signal(0, DCD_EVENT_SUSPEND, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wakeup interrupt is only enabled when we got suspended.
|
||||||
|
// Wakeup interrupt will disable itself
|
||||||
|
if ( int_status & USB_DEVICE_INTFLAG_WAKEUP )
|
||||||
|
{
|
||||||
|
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_WAKEUP;
|
||||||
|
|
||||||
|
// disable wakeup interrupt itself
|
||||||
|
USB->DEVICE.INTENCLR.reg = USB_DEVICE_INTFLAG_WAKEUP;
|
||||||
|
dcd_event_bus_signal(0, DCD_EVENT_RESUME, true);
|
||||||
|
}
|
||||||
|
|
||||||
if ( int_status & USB_DEVICE_INTFLAG_EORST )
|
if ( int_status & USB_DEVICE_INTFLAG_EORST )
|
||||||
{
|
{
|
||||||
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTENCLR_EORST;
|
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_EORST;
|
||||||
|
|
||||||
|
// Disable both suspend and wakeup interrupt
|
||||||
|
USB->DEVICE.INTENCLR.reg = USB_DEVICE_INTFLAG_WAKEUP | USB_DEVICE_INTFLAG_SUSPEND;
|
||||||
|
|
||||||
bus_reset();
|
bus_reset();
|
||||||
dcd_event_bus_signal(0, DCD_EVENT_BUS_RESET, true);
|
dcd_event_bus_signal(0, DCD_EVENT_BUS_RESET, true);
|
||||||
}
|
}
|
||||||
@ -283,6 +322,7 @@ void USB_0_Handler(void) {
|
|||||||
// Setup packet received.
|
// Setup packet received.
|
||||||
maybe_handle_setup_packet();
|
maybe_handle_setup_packet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* USB_SOF_HSOF */
|
/* USB_SOF_HSOF */
|
||||||
void USB_1_Handler(void) {
|
void USB_1_Handler(void) {
|
||||||
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_SOF;
|
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_SOF;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user