From d036f62b0ec72a2241c0b6075140e89bf6e45309 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 20 Nov 2018 17:25:41 +0700 Subject: samd51 fix stable issue with dcd --- src/device/dcd.h | 12 ++++++--- src/device/usbd.c | 19 +++++++++++--- src/device/usbd_control.c | 8 +++--- src/portable/microchip/samd51/dcd_samd51.c | 40 +++++++++++++----------------- 4 files changed, 44 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/device/dcd.h b/src/device/dcd.h index bd0ea852e..f07bda2d3 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -124,10 +124,14 @@ void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_ /*------------------------------------------------------------------*/ /* Endpoint API - * Note: - * - Address of control endpoint OUT is 0x00, In is 0x80 - * - When stalling control endpoint both control OUT and IN must be stalled - * (according to USB spec, stalled control is only recovered with setup token) + * - open : Configure endpoint's registers + * - xfer : Submit a transfer. When complete dcd_event_xfer_complete + * must be called to notify the stack + * - busy : Check if endpoint transferring is complete (TODO remove) + * - stall : stall ep. When control endpoint (addr = 0) is stalled, + * both direction (IN & OUT) of control ep must be stalled. + * - clear_stall : clear stall + * - stalled : check if stalled ( TODO remove ) *------------------------------------------------------------------*/ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc); bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes); diff --git a/src/device/usbd.c b/src/device/usbd.c index eac1a6a20..f39605987 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -174,7 +174,7 @@ static bool process_set_config(uint8_t rhport, uint8_t config_number); static void const* get_descriptor(tusb_control_request_t const * p_request, uint16_t* desc_len); void usbd_control_reset (uint8_t rhport); -tusb_error_t usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, tusb_event_t event, uint32_t xferred_bytes); +bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, tusb_event_t event, uint32_t xferred_bytes); void usbd_control_set_complete_callback( bool (*fp) (uint8_t, tusb_control_request_t const * ) ); //--------------------------------------------------------------------+ @@ -338,7 +338,10 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const switch ( p_request->bRequest ) { case TUSB_REQ_SET_ADDRESS: + // response with status first before changing device address + usbd_control_status(rhport, p_request); dcd_set_address(rhport, (uint8_t) p_request->wValue); + return true; // skip the rest break; case TUSB_REQ_GET_CONFIGURATION: @@ -362,7 +365,9 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const if ( data_buf == NULL || data_len == 0 ) return false; break; - default: return false; + default: + TU_BREAKPOINT(); + return false; } usbd_control_xfer(rhport, p_request, data_buf, data_len); @@ -405,12 +410,15 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const usbd_control_status(rhport, p_request); break; - default: return false; + default: + TU_BREAKPOINT(); + return false; } } else { //------------- Unsupported Request -------------// + TU_BREAKPOINT(); return false; } @@ -549,10 +557,13 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr) { case DCD_EVENT_BUS_RESET: case DCD_EVENT_UNPLUGGED: - case DCD_EVENT_SOF: osal_queue_send(_usbd_q, event, in_isr); break; + case DCD_EVENT_SOF: + // nothing to do now + break; + case DCD_EVENT_SUSPENDED: // TODO support suspended break; diff --git a/src/device/usbd_control.c b/src/device/usbd_control.c index a9687a3e6..8564d1ae9 100644 --- a/src/device/usbd_control.c +++ b/src/device/usbd_control.c @@ -126,7 +126,7 @@ bool usbd_control_xfer(uint8_t rhport, tusb_control_request_t const * request, v } // callback when a transaction complete on DATA stage of control endpoint -tusb_error_t usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, tusb_event_t event, uint32_t xferred_bytes) +bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, tusb_event_t event, uint32_t xferred_bytes) { if ( _control_state.request.bmRequestType_bit.direction == TUSB_DIR_OUT ) { @@ -151,7 +151,7 @@ tusb_error_t usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, tusb_event_t if ( is_ok ) { // Send status - TU_ASSERT( usbd_control_status(rhport, &_control_state.request), TUSB_ERROR_FAILED ); + TU_ASSERT( usbd_control_status(rhport, &_control_state.request) ); }else { // stall due to callback @@ -161,10 +161,10 @@ tusb_error_t usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, tusb_event_t else { // More data to transfer - TU_ASSERT(start_control_data_xact(rhport), TUSB_ERROR_FAILED); + TU_ASSERT( start_control_data_xact(rhport) ); } - return TUSB_ERROR_NONE; + return true; } #endif diff --git a/src/portable/microchip/samd51/dcd_samd51.c b/src/portable/microchip/samd51/dcd_samd51.c index e67fbd295..971210464 100644 --- a/src/portable/microchip/samd51/dcd_samd51.c +++ b/src/portable/microchip/samd51/dcd_samd51.c @@ -56,9 +56,8 @@ enum MAX_PACKET_SIZE = 64, }; -UsbDeviceDescBank sram_registers[8][2]; -ATTR_ALIGNED(4) uint8_t control_out_buffer[64]; -ATTR_ALIGNED(4) uint8_t control_in_buffer[64]; +static UsbDeviceDescBank sram_registers[8][2]; +static ATTR_ALIGNED(4) uint8_t setup_packet[8]; volatile uint32_t setup_count = 0; @@ -74,7 +73,8 @@ static void bus_reset(void) { 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; - dcd_edpt_xfer(0, 0, control_out_buffer, 64); + // Prepare for setup packet + dcd_edpt_xfer(0, 0, setup_packet, sizeof(setup_packet)); setup_count = 0; } @@ -105,7 +105,7 @@ void dcd_disconnect (uint8_t rhport) void dcd_set_address (uint8_t rhport, uint8_t dev_addr) { (void) rhport; - dcd_edpt_xfer (0, TUSB_DIR_IN_MASK, NULL, 0); + // Wait for EP0 to finish before switching the address. while (USB->DEVICE.DeviceEndpoint[0].EPSTATUS.bit.BK1RDY == 1) {} USB->DEVICE.DADD.reg = USB_DEVICE_DADD_DADD(dev_addr) | USB_DEVICE_DADD_ADDEN; @@ -119,20 +119,9 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num) } /*------------------------------------------------------------------*/ -/* Control +/* DCD Endpoint *------------------------------------------------------------------*/ -bool dcd_control_xfer (uint8_t rhport, uint8_t dir, uint8_t * buffer, uint16_t length) -{ - (void) rhport; - uint8_t ep_addr = 0; - if (dir == TUSB_DIR_IN) { - ep_addr |= TUSB_DIR_IN_MASK; - } - - return dcd_edpt_xfer (rhport, ep_addr, buffer, length); -} - bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) { (void) rhport; @@ -161,7 +150,6 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) ep->EPCFG.bit.EPTYPE1 = desc_edpt->bmAttributes.xfer + 1; ep->EPINTENSET.bit.TRCPT1 = true; } - __ISB(); __DSB(); return true; } @@ -219,9 +207,12 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1; } else { ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0; - } - __ISB(); __DSB(); + // for control, stall both IN & OUT + if (ep_addr == 0) { + ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1; + } + } } void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) @@ -260,12 +251,12 @@ static bool maybe_handle_setup_packet(void) { if (USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.RXSTP) { USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_RXSTP; + // uint8_t* buf = (uint8_t*) sram_registers[0][0].ADDR.reg; // // if (buf[6] == 0x12) asm("bkpt"); // This copies the data elsewhere so we can reuse the buffer. dcd_event_setup_received(0, (uint8_t*) sram_registers[0][0].ADDR.reg, true); - dcd_edpt_xfer(0, 0, control_out_buffer, 64); setup_count += 1; return true; } @@ -330,9 +321,12 @@ void transfer_complete(uint8_t direction) { ep_addr |= TUSB_DIR_IN_MASK; } dcd_event_xfer_complete(0, ep_addr, total_transfer_size, DCD_XFER_SUCCESS, true); - if (epnum == 0 && direction == TUSB_DIR_OUT) { - dcd_edpt_xfer(0, 0, control_out_buffer, 64); + + // just finished status stage (total size = 0), prepare for next setup packet + if (epnum == 0 && total_transfer_size == 0) { + dcd_edpt_xfer(0, 0, setup_packet, sizeof(setup_packet)); } + if (direction == TUSB_DIR_IN) { ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1; } else { -- cgit v1.3.1 From 155edc7b006d24f15b7a74e9e323c9d34f0651e7 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 21 Nov 2018 12:36:28 +0700 Subject: fix ses irq vector --- .../ses/ses_samd21/ATSAMD21G18A_Vectors.s | 250 ++++++++++----------- src/portable/microchip/samd21/hal_samd21.c | 6 - 2 files changed, 125 insertions(+), 131 deletions(-) (limited to 'src') diff --git a/examples/device/device_composite/ses/ses_samd21/ATSAMD21G18A_Vectors.s b/examples/device/device_composite/ses/ses_samd21/ATSAMD21G18A_Vectors.s index 5844d4990..5cecbd23b 100644 --- a/examples/device/device_composite/ses/ses_samd21/ATSAMD21G18A_Vectors.s +++ b/examples/device/device_composite/ses/ses_samd21/ATSAMD21G18A_Vectors.s @@ -60,206 +60,206 @@ Dummy_Handler: #if defined(__OPTIMIZATION_SMALL) - .weak PM_IRQHandler - .thumb_set PM_IRQHandler,Dummy_Handler + .weak PM_Handler + .thumb_set PM_Handler,Dummy_Handler - .weak SYSCTRL_IRQHandler - .thumb_set SYSCTRL_IRQHandler,Dummy_Handler + .weak SYSCTRL_Handler + .thumb_set SYSCTRL_Handler,Dummy_Handler - .weak WDT_IRQHandler - .thumb_set WDT_IRQHandler,Dummy_Handler + .weak WDT_Handler + .thumb_set WDT_Handler,Dummy_Handler - .weak RTC_IRQHandler - .thumb_set RTC_IRQHandler,Dummy_Handler + .weak RTC_Handler + .thumb_set RTC_Handler,Dummy_Handler - .weak EIC_IRQHandler - .thumb_set EIC_IRQHandler,Dummy_Handler + .weak EIC_Handler + .thumb_set EIC_Handler,Dummy_Handler - .weak NVMCTRL_IRQHandler - .thumb_set NVMCTRL_IRQHandler,Dummy_Handler + .weak NVMCTRL_Handler + .thumb_set NVMCTRL_Handler,Dummy_Handler - .weak DMAC_IRQHandler - .thumb_set DMAC_IRQHandler,Dummy_Handler + .weak DMAC_Handler + .thumb_set DMAC_Handler,Dummy_Handler - .weak USB_IRQHandler - .thumb_set USB_IRQHandler,Dummy_Handler + .weak USB_Handler + .thumb_set USB_Handler,Dummy_Handler - .weak EVSYS_IRQHandler - .thumb_set EVSYS_IRQHandler,Dummy_Handler + .weak EVSYS_Handler + .thumb_set EVSYS_Handler,Dummy_Handler - .weak SERCOM0_IRQHandler - .thumb_set SERCOM0_IRQHandler,Dummy_Handler + .weak SERCOM0_Handler + .thumb_set SERCOM0_Handler,Dummy_Handler - .weak SERCOM1_IRQHandler - .thumb_set SERCOM1_IRQHandler,Dummy_Handler + .weak SERCOM1_Handler + .thumb_set SERCOM1_Handler,Dummy_Handler - .weak SERCOM2_IRQHandler - .thumb_set SERCOM2_IRQHandler,Dummy_Handler + .weak SERCOM2_Handler + .thumb_set SERCOM2_Handler,Dummy_Handler - .weak SERCOM3_IRQHandler - .thumb_set SERCOM3_IRQHandler,Dummy_Handler + .weak SERCOM3_Handler + .thumb_set SERCOM3_Handler,Dummy_Handler - .weak SERCOM4_IRQHandler - .thumb_set SERCOM4_IRQHandler,Dummy_Handler + .weak SERCOM4_Handler + .thumb_set SERCOM4_Handler,Dummy_Handler - .weak SERCOM5_IRQHandler - .thumb_set SERCOM5_IRQHandler,Dummy_Handler + .weak SERCOM5_Handler + .thumb_set SERCOM5_Handler,Dummy_Handler - .weak TCC0_IRQHandler - .thumb_set TCC0_IRQHandler,Dummy_Handler + .weak TCC0_Handler + .thumb_set TCC0_Handler,Dummy_Handler - .weak TCC1_IRQHandler - .thumb_set TCC1_IRQHandler,Dummy_Handler + .weak TCC1_Handler + .thumb_set TCC1_Handler,Dummy_Handler - .weak TCC2_IRQHandler - .thumb_set TCC2_IRQHandler,Dummy_Handler + .weak TCC2_Handler + .thumb_set TCC2_Handler,Dummy_Handler - .weak TC3_IRQHandler - .thumb_set TC3_IRQHandler,Dummy_Handler + .weak TC3_Handler + .thumb_set TC3_Handler,Dummy_Handler - .weak TC4_IRQHandler - .thumb_set TC4_IRQHandler,Dummy_Handler + .weak TC4_Handler + .thumb_set TC4_Handler,Dummy_Handler - .weak TC5_IRQHandler - .thumb_set TC5_IRQHandler,Dummy_Handler + .weak TC5_Handler + .thumb_set TC5_Handler,Dummy_Handler - .weak ADC_IRQHandler - .thumb_set ADC_IRQHandler,Dummy_Handler + .weak ADC_Handler + .thumb_set ADC_Handler,Dummy_Handler - .weak AC_IRQHandler - .thumb_set AC_IRQHandler,Dummy_Handler + .weak AC_Handler + .thumb_set AC_Handler,Dummy_Handler - .weak DAC_IRQHandler - .thumb_set DAC_IRQHandler,Dummy_Handler + .weak DAC_Handler + .thumb_set DAC_Handler,Dummy_Handler - .weak I2S_IRQHandler - .thumb_set I2S_IRQHandler,Dummy_Handler + .weak I2S_Handler + .thumb_set I2S_Handler,Dummy_Handler #else .thumb_func - .weak PM_IRQHandler -PM_IRQHandler: + .weak PM_Handler +PM_Handler: b . .thumb_func - .weak SYSCTRL_IRQHandler -SYSCTRL_IRQHandler: + .weak SYSCTRL_Handler +SYSCTRL_Handler: b . .thumb_func - .weak WDT_IRQHandler -WDT_IRQHandler: + .weak WDT_Handler +WDT_Handler: b . .thumb_func - .weak RTC_IRQHandler -RTC_IRQHandler: + .weak RTC_Handler +RTC_Handler: b . .thumb_func - .weak EIC_IRQHandler -EIC_IRQHandler: + .weak EIC_Handler +EIC_Handler: b . .thumb_func - .weak NVMCTRL_IRQHandler -NVMCTRL_IRQHandler: + .weak NVMCTRL_Handler +NVMCTRL_Handler: b . .thumb_func - .weak DMAC_IRQHandler -DMAC_IRQHandler: + .weak DMAC_Handler +DMAC_Handler: b . .thumb_func - .weak USB_IRQHandler -USB_IRQHandler: + .weak USB_Handler +USB_Handler: b . .thumb_func - .weak EVSYS_IRQHandler -EVSYS_IRQHandler: + .weak EVSYS_Handler +EVSYS_Handler: b . .thumb_func - .weak SERCOM0_IRQHandler -SERCOM0_IRQHandler: + .weak SERCOM0_Handler +SERCOM0_Handler: b . .thumb_func - .weak SERCOM1_IRQHandler -SERCOM1_IRQHandler: + .weak SERCOM1_Handler +SERCOM1_Handler: b . .thumb_func - .weak SERCOM2_IRQHandler -SERCOM2_IRQHandler: + .weak SERCOM2_Handler +SERCOM2_Handler: b . .thumb_func - .weak SERCOM3_IRQHandler -SERCOM3_IRQHandler: + .weak SERCOM3_Handler +SERCOM3_Handler: b . .thumb_func - .weak SERCOM4_IRQHandler -SERCOM4_IRQHandler: + .weak SERCOM4_Handler +SERCOM4_Handler: b . .thumb_func - .weak SERCOM5_IRQHandler -SERCOM5_IRQHandler: + .weak SERCOM5_Handler +SERCOM5_Handler: b . .thumb_func - .weak TCC0_IRQHandler -TCC0_IRQHandler: + .weak TCC0_Handler +TCC0_Handler: b . .thumb_func - .weak TCC1_IRQHandler -TCC1_IRQHandler: + .weak TCC1_Handler +TCC1_Handler: b . .thumb_func - .weak TCC2_IRQHandler -TCC2_IRQHandler: + .weak TCC2_Handler +TCC2_Handler: b . .thumb_func - .weak TC3_IRQHandler -TC3_IRQHandler: + .weak TC3_Handler +TC3_Handler: b . .thumb_func - .weak TC4_IRQHandler -TC4_IRQHandler: + .weak TC4_Handler +TC4_Handler: b . .thumb_func - .weak TC5_IRQHandler -TC5_IRQHandler: + .weak TC5_Handler +TC5_Handler: b . .thumb_func - .weak ADC_IRQHandler -ADC_IRQHandler: + .weak ADC_Handler +ADC_Handler: b . .thumb_func - .weak AC_IRQHandler -AC_IRQHandler: + .weak AC_Handler +AC_Handler: b . .thumb_func - .weak DAC_IRQHandler -DAC_IRQHandler: + .weak DAC_Handler +DAC_Handler: b . .thumb_func - .weak I2S_IRQHandler -I2S_IRQHandler: + .weak I2S_Handler +I2S_Handler: b . #endif @@ -291,34 +291,34 @@ _vectors: .word 0 /* Reserved */ .word PendSV_Handler .word SysTick_Handler - .word PM_IRQHandler - .word SYSCTRL_IRQHandler - .word WDT_IRQHandler - .word RTC_IRQHandler - .word EIC_IRQHandler - .word NVMCTRL_IRQHandler - .word DMAC_IRQHandler - .word USB_IRQHandler - .word EVSYS_IRQHandler - .word SERCOM0_IRQHandler - .word SERCOM1_IRQHandler - .word SERCOM2_IRQHandler - .word SERCOM3_IRQHandler - .word SERCOM4_IRQHandler - .word SERCOM5_IRQHandler - .word TCC0_IRQHandler - .word TCC1_IRQHandler - .word TCC2_IRQHandler - .word TC3_IRQHandler - .word TC4_IRQHandler - .word TC5_IRQHandler + .word PM_Handler + .word SYSCTRL_Handler + .word WDT_Handler + .word RTC_Handler + .word EIC_Handler + .word NVMCTRL_Handler + .word DMAC_Handler + .word USB_Handler + .word EVSYS_Handler + .word SERCOM0_Handler + .word SERCOM1_Handler + .word SERCOM2_Handler + .word SERCOM3_Handler + .word SERCOM4_Handler + .word SERCOM5_Handler + .word TCC0_Handler + .word TCC1_Handler + .word TCC2_Handler + .word TC3_Handler + .word TC4_Handler + .word TC5_Handler .word Dummy_Handler /* Reserved */ .word Dummy_Handler /* Reserved */ - .word ADC_IRQHandler - .word AC_IRQHandler - .word DAC_IRQHandler + .word ADC_Handler + .word AC_Handler + .word DAC_Handler .word Dummy_Handler /* Reserved */ - .word I2S_IRQHandler + .word I2S_Handler _vectors_end: #ifdef VECTORS_IN_RAM diff --git a/src/portable/microchip/samd21/hal_samd21.c b/src/portable/microchip/samd21/hal_samd21.c index 524840be2..0df8cfb9d 100644 --- a/src/portable/microchip/samd21/hal_samd21.c +++ b/src/portable/microchip/samd21/hal_samd21.c @@ -44,12 +44,6 @@ #include "tusb_hal.h" -/*------------------------------------------------------------------*/ -/* MACRO TYPEDEF CONSTANT ENUM - *------------------------------------------------------------------*/ -#define USB_NVIC_PRIO 7 - -void tusb_hal_nrf_power_event(uint32_t event); /*------------------------------------------------------------------*/ /* TUSB HAL -- cgit v1.3.1 From a0ce92bcfdf48fce71c5cb4682dfe9915945ee26 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 21 Nov 2018 13:01:39 +0700 Subject: update dcd samd21 --- src/portable/microchip/samd21/dcd_samd21.c | 48 +++++++++++++----------------- 1 file changed, 20 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/portable/microchip/samd21/dcd_samd21.c b/src/portable/microchip/samd21/dcd_samd21.c index 33cbc8d92..81e37abcc 100644 --- a/src/portable/microchip/samd21/dcd_samd21.c +++ b/src/portable/microchip/samd21/dcd_samd21.c @@ -45,8 +45,6 @@ #include "device/usbd.h" #include "device/usbd_pvt.h" // to use defer function helper -#include "class/msc/msc_device.h" - #include "sam.h" /*------------------------------------------------------------------*/ @@ -58,9 +56,8 @@ enum MAX_PACKET_SIZE = 64, }; -UsbDeviceDescBank sram_registers[8][2]; -ATTR_ALIGNED(4) uint8_t control_out_buffer[64]; -ATTR_ALIGNED(4) uint8_t control_in_buffer[64]; +static UsbDeviceDescBank sram_registers[8][2]; +static ATTR_ALIGNED(4) uint8_t _setup_packet[8]; volatile uint32_t setup_count = 0; @@ -76,7 +73,8 @@ static void bus_reset(void) { 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; - dcd_edpt_xfer(0, 0, control_out_buffer, 64); + // Prepare for setup packet + dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet)); setup_count = 0; } @@ -107,7 +105,7 @@ void dcd_disconnect (uint8_t rhport) void dcd_set_address (uint8_t rhport, uint8_t dev_addr) { (void) rhport; - dcd_edpt_xfer (0, TUSB_DIR_IN_MASK, NULL, 0); + // Wait for EP0 to finish before switching the address. while (USB->DEVICE.DeviceEndpoint[0].EPSTATUS.bit.BK1RDY == 1) {} USB->DEVICE.DADD.reg = USB_DEVICE_DADD_DADD(dev_addr) | USB_DEVICE_DADD_ADDEN; @@ -121,20 +119,9 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num) } /*------------------------------------------------------------------*/ -/* Control +/* DCD Endpoint port *------------------------------------------------------------------*/ -bool dcd_control_xfer (uint8_t rhport, uint8_t dir, uint8_t * buffer, uint16_t length) -{ - (void) rhport; - uint8_t ep_addr = 0; - if (dir == TUSB_DIR_IN) { - ep_addr |= TUSB_DIR_IN_MASK; - } - - return dcd_edpt_xfer (rhport, ep_addr, buffer, length); -} - bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) { (void) rhport; @@ -163,7 +150,6 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) ep->EPCFG.bit.EPTYPE1 = desc_edpt->bmAttributes.xfer + 1; ep->EPINTENSET.bit.TRCPT1 = true; } - __ISB(); __DSB(); return true; } @@ -222,9 +208,12 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1; } else { ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0; - } - __ISB(); __DSB(); + // for control, stall both IN & OUT + if (ep_addr == 0) { + ep->EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ1; + } + } } void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) @@ -266,7 +255,6 @@ static bool maybe_handle_setup_packet(void) { // This copies the data elsewhere so we can reuse the buffer. dcd_event_setup_received(0, (uint8_t*) sram_registers[0][0].ADDR.reg, true); - dcd_edpt_xfer(0, 0, control_out_buffer, 64); setup_count += 1; return true; } @@ -287,12 +275,14 @@ void maybe_transfer_complete(void) { uint32_t epintflag = ep->EPINTFLAG.reg; + uint16_t total_transfer_size; + // Handle IN completions if ((epintflag & USB_DEVICE_EPINTFLAG_TRCPT1) != 0) { ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1; UsbDeviceDescBank* bank = &sram_registers[epnum][TUSB_DIR_IN]; - uint16_t total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT; + total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT; uint8_t ep_addr = epnum | TUSB_DIR_IN_MASK; dcd_event_xfer_complete(0, ep_addr, total_transfer_size, DCD_XFER_SUCCESS, true); @@ -303,13 +293,15 @@ void maybe_transfer_complete(void) { ep->EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0; UsbDeviceDescBank* bank = &sram_registers[epnum][TUSB_DIR_OUT]; - uint16_t total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT; + total_transfer_size = bank->PCKSIZE.bit.BYTE_COUNT; uint8_t ep_addr = epnum; dcd_event_xfer_complete(0, ep_addr, total_transfer_size, DCD_XFER_SUCCESS, true); - if (epnum == 0) { - dcd_edpt_xfer(0, 0, control_out_buffer, 64); - } + } + + // just finished status stage (total size = 0), prepare for next setup packet + if (epnum == 0 && total_transfer_size == 0) { + dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet)); } } } -- cgit v1.3.1 From a30dfa33244c3db1a3987dc9f75e4aae9b192d63 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 21 Nov 2018 13:11:19 +0700 Subject: clean up --- hw/bsp/metro_m4_express/board_metro_m4_express.c | 6 ++---- src/portable/microchip/samd51/dcd_samd51.c | 18 ++++-------------- 2 files changed, 6 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/hw/bsp/metro_m4_express/board_metro_m4_express.c b/hw/bsp/metro_m4_express/board_metro_m4_express.c index ef8516741..4e55a9201 100644 --- a/hw/bsp/metro_m4_express/board_metro_m4_express.c +++ b/hw/bsp/metro_m4_express/board_metro_m4_express.c @@ -43,8 +43,6 @@ #include "hal/include/hal_init.h" #include "hpl/gclk/hpl_gclk_base.h" #include "hpl_mclk_config.h" -#include "peripheral_clk_config.h" - //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION @@ -60,7 +58,7 @@ void board_init(void) { // Clock init ( follow hpl_init.c ) - // hri_nvmctrl_set_CTRLA_RWS_bf(NVMCTRL, CONF_NVM_WAIT_STATE); + hri_nvmctrl_set_CTRLA_RWS_bf(NVMCTRL, 0); _osc32kctrl_init_sources(); _oscctrl_init_sources(); @@ -84,7 +82,7 @@ void board_init(void) /* USB Clock init * The USB module requires a GCLK_USB of 48 MHz ~ 0.25% clock * for low speed and full speed operation. */ - hri_gclk_write_PCHCTRL_reg(GCLK, USB_GCLK_ID, CONF_GCLK_USB_SRC | GCLK_PCHCTRL_CHEN); + hri_gclk_write_PCHCTRL_reg(GCLK, USB_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK1_Val | GCLK_PCHCTRL_CHEN); hri_mclk_set_AHBMASK_USB_bit(MCLK); hri_mclk_set_APBBMASK_USB_bit(MCLK); diff --git a/src/portable/microchip/samd51/dcd_samd51.c b/src/portable/microchip/samd51/dcd_samd51.c index 971210464..023a706ea 100644 --- a/src/portable/microchip/samd51/dcd_samd51.c +++ b/src/portable/microchip/samd51/dcd_samd51.c @@ -57,9 +57,7 @@ enum }; static UsbDeviceDescBank sram_registers[8][2]; -static ATTR_ALIGNED(4) uint8_t setup_packet[8]; - -volatile uint32_t setup_count = 0; +static ATTR_ALIGNED(4) uint8_t _setup_packet[8]; // Setup the control endpoint 0. static void bus_reset(void) { @@ -74,8 +72,7 @@ static void bus_reset(void) { 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)); - setup_count = 0; + dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet)); } @@ -119,7 +116,7 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num) } /*------------------------------------------------------------------*/ -/* DCD Endpoint +/* DCD Endpoint port *------------------------------------------------------------------*/ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) @@ -252,12 +249,8 @@ static bool maybe_handle_setup_packet(void) { { USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_RXSTP; - // uint8_t* buf = (uint8_t*) sram_registers[0][0].ADDR.reg; - // - // if (buf[6] == 0x12) asm("bkpt"); // This copies the data elsewhere so we can reuse the buffer. dcd_event_setup_received(0, (uint8_t*) sram_registers[0][0].ADDR.reg, true); - setup_count += 1; return true; } return false; @@ -299,9 +292,6 @@ void USB_1_Handler(void) { } void transfer_complete(uint8_t direction) { - // uint8_t* buf = (uint8_t*) sram_registers[0][0].ADDR.reg; - // - // if (buf[6] == 0x12 || setup_count == 2) asm("bkpt"); uint32_t epints = USB->DEVICE.EPINTSMRY.reg; for (uint8_t epnum = 0; epnum < USB_EPT_NUM; epnum++) { if ((epints & (1 << epnum)) == 0) { @@ -324,7 +314,7 @@ void transfer_complete(uint8_t direction) { // just finished status stage (total size = 0), prepare for next setup packet if (epnum == 0 && total_transfer_size == 0) { - dcd_edpt_xfer(0, 0, setup_packet, sizeof(setup_packet)); + dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet)); } if (direction == TUSB_DIR_IN) { -- cgit v1.3.1 From 3cad1d11341bb70612ce94559bd93c21bc43b498 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 21 Nov 2018 15:00:36 +0700 Subject: samd21 work well with cdc --- examples/device/device_composite/ses/device_composite.emProject | 2 +- hw/bsp/metro_m0_express/board_metro_m0_express.c | 2 -- src/portable/microchip/samd21/dcd_samd21.c | 6 +----- 3 files changed, 2 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/examples/device/device_composite/ses/device_composite.emProject b/examples/device/device_composite/ses/device_composite.emProject index 7011fb979..f8168b7d3 100644 --- a/examples/device/device_composite/ses/device_composite.emProject +++ b/examples/device/device_composite/ses/device_composite.emProject @@ -214,7 +214,7 @@ arm_target_device_name="ATSAMD21G18A" arm_target_interface_type="SWD" build_treat_warnings_as_errors="Yes" - c_preprocessor_definitions="__SAMD21G18A__;__SAMD21_FAMILY;__SAM_D21_SUBFAMILY;ARM_MATH_CM0PLUS;FLASH_PLACEMENT=1;USE_SIMPLE_ASSERT;CONF_XOSC32K_CONFIG=1;BOARD_METRO_M0_EXPRESS;CFG_TUSB_MCU=OPT_MCU_SAMD21" + c_preprocessor_definitions="__SAMD21G18A__;__SAMD21_FAMILY;__SAM_D21_SUBFAMILY;ARM_MATH_CM0PLUS;FLASH_PLACEMENT=1;USE_SIMPLE_ASSERT;CONF_XOSC32K_CONFIG=1;CONF_OSC32K_ENABLE=1;CONF_OSC32K_EN32K=1;CONF_XOSC32K_STARTUP=CONF_XOSC32K_STARTUP_TIME_2000092MCS;CONF_DFLL_ONDEMAND=0;CONF_DFLL_OVERWRITE_CALIBRATION=0;BOARD_METRO_M0_EXPRESS;CFG_TUSB_MCU=OPT_MCU_SAMD21" c_user_include_directories="../src;$(rootDir)/hw/cmsis/Include;$(rootDir)/hw;$(rootDir)/src;$(asf4Dir);$(asf4Dir)/include;$(asf4Dir)/config;$(asf4Dir)/hri;$(asf4Dir)/hal/include;$(asf4Dir)/hal/utils/include;$(asf4Dir)/hpl/port;$(asf4Dir)/hpl/gclk;$(asf4Dir)/hpl/pm" debug_register_definition_file="ses_samd21/ATSAMD21G18A_Registers.xml" debug_target_connection="J-Link" diff --git a/hw/bsp/metro_m0_express/board_metro_m0_express.c b/hw/bsp/metro_m0_express/board_metro_m0_express.c index 9f1ed4fe4..6b4f185ac 100644 --- a/hw/bsp/metro_m0_express/board_metro_m0_express.c +++ b/hw/bsp/metro_m0_express/board_metro_m0_express.c @@ -46,8 +46,6 @@ #include "hpl/gclk/hpl_gclk_base.h" #include "hpl_pm_config.h" #include "hpl/pm/hpl_pm_base.h" -#include "peripheral_clk_config.h" - //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION diff --git a/src/portable/microchip/samd21/dcd_samd21.c b/src/portable/microchip/samd21/dcd_samd21.c index 81e37abcc..0102685d2 100644 --- a/src/portable/microchip/samd21/dcd_samd21.c +++ b/src/portable/microchip/samd21/dcd_samd21.c @@ -56,11 +56,9 @@ enum MAX_PACKET_SIZE = 64, }; -static UsbDeviceDescBank sram_registers[8][2]; +static ATTR_ALIGNED(4) UsbDeviceDescBank sram_registers[8][2]; static ATTR_ALIGNED(4) uint8_t _setup_packet[8]; -volatile uint32_t setup_count = 0; - // Setup the control endpoint 0. static void bus_reset(void) { // Max size of packets is 64 bytes. @@ -75,7 +73,6 @@ static void bus_reset(void) { // Prepare for setup packet dcd_edpt_xfer(0, 0, _setup_packet, sizeof(_setup_packet)); - setup_count = 0; } @@ -255,7 +252,6 @@ static bool maybe_handle_setup_packet(void) { // This copies the data elsewhere so we can reuse the buffer. dcd_event_setup_received(0, (uint8_t*) sram_registers[0][0].ADDR.reg, true); - setup_count += 1; return true; } return false; -- cgit v1.3.1 From 1d6fc49fa974903ca14af6454671ea816fb2d6dd Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 21 Nov 2018 17:03:39 +0700 Subject: clean up --- hw/bsp/metro_m0_express/board_metro_m0_express.c | 2 +- hw/bsp/metro_m4_express/board_metro_m4_express.c | 2 +- src/portable/microchip/samd21/dcd_samd21.c | 14 ++++---------- src/portable/microchip/samd51/dcd_samd51.c | 14 ++++---------- 4 files changed, 10 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/hw/bsp/metro_m0_express/board_metro_m0_express.c b/hw/bsp/metro_m0_express/board_metro_m0_express.c index 6b4f185ac..471aa4b25 100644 --- a/hw/bsp/metro_m0_express/board_metro_m0_express.c +++ b/hw/bsp/metro_m0_express/board_metro_m0_express.c @@ -76,7 +76,7 @@ void board_init(void) gpio_set_pin_level(BOARD_LED0, 1-LED_STATE_ON); #if CFG_TUSB_OS == OPT_OS_NONE - // Tick init + // Tick init, samd SystemCoreClock may not correct SysTick_Config(SystemCoreClock/1000); #endif diff --git a/hw/bsp/metro_m4_express/board_metro_m4_express.c b/hw/bsp/metro_m4_express/board_metro_m4_express.c index 4e55a9201..9d2bc2b78 100644 --- a/hw/bsp/metro_m4_express/board_metro_m4_express.c +++ b/hw/bsp/metro_m4_express/board_metro_m4_express.c @@ -75,7 +75,7 @@ void board_init(void) // Systick init #if CFG_TUSB_OS == OPT_OS_NONE - // Tick init + // Tick init, samd SystemCoreClock may not correct SysTick_Config(SystemCoreClock / 1000); #endif diff --git a/src/portable/microchip/samd21/dcd_samd21.c b/src/portable/microchip/samd21/dcd_samd21.c index 0102685d2..b31db547d 100644 --- a/src/portable/microchip/samd21/dcd_samd21.c +++ b/src/portable/microchip/samd21/dcd_samd21.c @@ -41,21 +41,11 @@ #if TUSB_OPT_DEVICE_ENABLED && CFG_TUSB_MCU == OPT_MCU_SAMD21 #include "device/dcd.h" - -#include "device/usbd.h" -#include "device/usbd_pvt.h" // to use defer function helper - #include "sam.h" /*------------------------------------------------------------------*/ /* MACRO TYPEDEF CONSTANT ENUM *------------------------------------------------------------------*/ -enum -{ - // Max allowed by USB specs - MAX_PACKET_SIZE = 64, -}; - static ATTR_ALIGNED(4) UsbDeviceDescBank sram_registers[8][2]; static ATTR_ALIGNED(4) uint8_t _setup_packet[8]; @@ -134,6 +124,10 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) } size_value++; } + + // unsupported endpoint size + if ( size_value == 7 && desc_edpt->wMaxPacketSize.size != 1023 ) return false; + bank->PCKSIZE.bit.SIZE = size_value; UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; diff --git a/src/portable/microchip/samd51/dcd_samd51.c b/src/portable/microchip/samd51/dcd_samd51.c index 023a706ea..6fff12ba7 100644 --- a/src/portable/microchip/samd51/dcd_samd51.c +++ b/src/portable/microchip/samd51/dcd_samd51.c @@ -41,21 +41,11 @@ #if TUSB_OPT_DEVICE_ENABLED && CFG_TUSB_MCU == OPT_MCU_SAMD51 #include "device/dcd.h" - -#include "device/usbd.h" -#include "device/usbd_pvt.h" // to use defer function helper - #include "sam.h" /*------------------------------------------------------------------*/ /* MACRO TYPEDEF CONSTANT ENUM *------------------------------------------------------------------*/ -enum -{ - // Max allowed by USB specs - MAX_PACKET_SIZE = 64, -}; - static UsbDeviceDescBank sram_registers[8][2]; static ATTR_ALIGNED(4) uint8_t _setup_packet[8]; @@ -134,6 +124,10 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) } size_value++; } + + // unsupported endpoint size + if ( size_value == 7 && desc_edpt->wMaxPacketSize.size != 1023 ) return false; + bank->PCKSIZE.bit.SIZE = size_value; UsbDeviceEndpoint* ep = &USB->DEVICE.DeviceEndpoint[epnum]; -- cgit v1.3.1