summaryrefslogtreecommitdiff
path: root/src/portable/microchip
diff options
context:
space:
mode:
authorhathach <[email protected]>2019-04-01 21:11:06 -0700
committerGitHub <[email protected]>2019-04-01 21:11:06 -0700
commit0848c462b3e431a9da42e96537d2b597a4579636 (patch)
tree746d6642e22e3ecf30d4f4c5f830c2a155bb28d7 /src/portable/microchip
parent86c24b310509a368cc6a9dfbec43ce531e71d598 (diff)
parent2c1072c0aba48a2e6cbe93118c286b7999b0db43 (diff)
Merge pull request #51 from hathach/develop
Add suspend, resume and remote wakeup support
Diffstat (limited to 'src/portable/microchip')
-rw-r--r--src/portable/microchip/samd21/dcd_samd21.c83
-rw-r--r--src/portable/microchip/samd51/dcd_samd51.c75
2 files changed, 119 insertions, 39 deletions
diff --git a/src/portable/microchip/samd21/dcd_samd21.c b/src/portable/microchip/samd21/dcd_samd21.c
index 723bd6c3a..8ecfd3668 100644
--- a/src/portable/microchip/samd21/dcd_samd21.c
+++ b/src/portable/microchip/samd21/dcd_samd21.c
@@ -38,26 +38,27 @@ static ATTR_ALIGNED(4) UsbDeviceDescBank sram_registers[8][2];
static ATTR_ALIGNED(4) uint8_t _setup_packet[8];
// Setup the control endpoint 0.
-static void bus_reset(void) {
- // Max size of packets is 64 bytes.
- UsbDeviceDescBank* bank_out = &sram_registers[0][TUSB_DIR_OUT];
- bank_out->PCKSIZE.bit.SIZE = 0x3;
- UsbDeviceDescBank* bank_in = &sram_registers[0][TUSB_DIR_IN];
- bank_in->PCKSIZE.bit.SIZE = 0x3;
+static void bus_reset(void)
+{
+ // Max size of packets is 64 bytes.
+ UsbDeviceDescBank* bank_out = &sram_registers[0][TUSB_DIR_OUT];
+ bank_out->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];
- 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;
+ UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[0];
+ 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;
- // Prepare for setup packet
- dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet));
+ // Prepare for setup packet
+ dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet));
}
/*------------------------------------------------------------------*/
/* Controller API
*------------------------------------------------------------------*/
-bool dcd_init (uint8_t rhport)
+void dcd_init (uint8_t rhport)
{
(void) rhport;
@@ -79,9 +80,8 @@ bool dcd_init (uint8_t rhport)
USB->DEVICE.CTRLA.reg = USB_CTRLA_MODE_DEVICE | USB_CTRLA_ENABLE | USB_CTRLA_RUNSTDBY;
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;
-
- return true;
}
void dcd_int_enable(uint8_t rhport)
@@ -105,6 +105,10 @@ void dcd_set_address (uint8_t rhport, uint8_t dev_addr)
while (USB->DEVICE.DeviceEndpoint[0].EPSTATUS.bit.BK1RDY == 1) {}
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)
@@ -112,6 +116,14 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num)
(void) rhport;
(void) config_num;
// Nothing to do
+
+}
+
+void dcd_remote_wakeup(uint8_t rhport)
+{
+ (void) rhport;
+
+ USB->DEVICE.CTRLB.bit.UPRSM = 1;
}
/*------------------------------------------------------------------*/
@@ -292,21 +304,46 @@ void maybe_transfer_complete(void) {
}
}
-void USB_Handler(void) {
- uint32_t int_status = USB->DEVICE.INTFLAG.reg;
+void USB_Handler(void)
+{
+ uint32_t int_status = USB->DEVICE.INTFLAG.reg & USB->DEVICE.INTENSET.reg;
+ USB->DEVICE.INTFLAG.reg = int_status; // clear interrupt
/*------------- Interrupt Processing -------------*/
- if ( int_status & USB_DEVICE_INTFLAG_EORST )
+ if ( int_status & USB_DEVICE_INTFLAG_SOF )
{
- USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTENCLR_EORST;
- bus_reset();
- dcd_event_bus_signal(0, DCD_EVENT_BUS_RESET, true);
+ dcd_event_bus_signal(0, DCD_EVENT_SOF, true);
}
- if ( int_status & USB_DEVICE_INTFLAG_SOF )
+ // 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_SOF;
- dcd_event_bus_signal(0, DCD_EVENT_SOF, true);
+ // 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 )
+ {
+ // 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);
}
// Setup packet received.
diff --git a/src/portable/microchip/samd51/dcd_samd51.c b/src/portable/microchip/samd51/dcd_samd51.c
index 9869956c7..d2e7aaf7d 100644
--- a/src/portable/microchip/samd51/dcd_samd51.c
+++ b/src/portable/microchip/samd51/dcd_samd51.c
@@ -38,26 +38,27 @@ static UsbDeviceDescBank sram_registers[8][2];
static ATTR_ALIGNED(4) uint8_t _setup_packet[8];
// Setup the control endpoint 0.
-static void bus_reset(void) {
- // Max size of packets is 64 bytes.
- UsbDeviceDescBank* bank_out = &sram_registers[0][TUSB_DIR_OUT];
- bank_out->PCKSIZE.bit.SIZE = 0x3;
- UsbDeviceDescBank* bank_in = &sram_registers[0][TUSB_DIR_IN];
- bank_in->PCKSIZE.bit.SIZE = 0x3;
+static void bus_reset(void)
+{
+ // Max size of packets is 64 bytes.
+ UsbDeviceDescBank* bank_out = &sram_registers[0][TUSB_DIR_OUT];
+ bank_out->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];
- 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;
+ UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[0];
+ 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;
- // Prepare for setup packet
- dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet));
+ // Prepare for setup packet
+ dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet));
}
/*------------------------------------------------------------------*/
/* Controller API
*------------------------------------------------------------------*/
-bool dcd_init (uint8_t rhport)
+void dcd_init (uint8_t rhport)
{
(void) rhport;
@@ -78,9 +79,9 @@ bool dcd_init (uint8_t rhport)
USB->DEVICE.CTRLB.reg = USB_DEVICE_CTRLB_SPDCONF_FS;
USB->DEVICE.CTRLA.reg = USB_CTRLA_MODE_DEVICE | USB_CTRLA_ENABLE | USB_CTRLA_RUNSTDBY;
while (USB->DEVICE.SYNCBUSY.bit.ENABLE == 1) {}
- USB->DEVICE.INTENSET.reg = USB_DEVICE_INTENSET_SOF | USB_DEVICE_INTENSET_EORST;
- return true;
+ USB->DEVICE.INTFLAG.reg |= USB->DEVICE.INTFLAG.reg; // clear pending
+ USB->DEVICE.INTENSET.reg = USB_DEVICE_INTENSET_SOF | USB_DEVICE_INTENSET_EORST;
}
void dcd_int_enable(uint8_t rhport)
@@ -110,6 +111,10 @@ void dcd_set_address (uint8_t rhport, uint8_t dev_addr)
while (USB->DEVICE.DeviceEndpoint[0].EPSTATUS.bit.BK1RDY == 1) {}
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)
@@ -119,6 +124,13 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num)
// Nothing to do
}
+void dcd_remote_wakeup(uint8_t rhport)
+{
+ (void) rhport;
+
+ USB->DEVICE.CTRLB.bit.UPRSM = 1;
+}
+
/*------------------------------------------------------------------*/
/* DCD Endpoint port
*------------------------------------------------------------------*/
@@ -267,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_6, USB_TRFAIL1_PERR_7, USB_UPRSM, USB_WAKEUP */
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 -------------*/
+ // 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 )
{
- 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();
dcd_event_bus_signal(0, DCD_EVENT_BUS_RESET, true);
}
@@ -280,6 +322,7 @@ void USB_0_Handler(void) {
// Setup packet received.
maybe_handle_setup_packet();
}
+
/* USB_SOF_HSOF */
void USB_1_Handler(void) {
USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_SOF;