2020-01-16 06:59:15 +01:00
/*
2019-03-20 10:11:42 +01:00
* The MIT License ( MIT )
*
2019-05-14 06:48:05 +02:00
* Copyright ( c ) 2019 Ha Thach ( tinyusb . org )
2019-03-20 10:11:42 +01:00
*
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the " Software " ) , to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software .
*
* THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE .
*
* This file is part of the TinyUSB stack .
*/
2018-12-04 06:47:58 +01:00
# include "tusb_option.h"
2021-02-22 12:02:52 +01:00
# if TUSB_OPT_DEVICE_ENABLED && \
( CFG_TUSB_MCU = = OPT_MCU_LPC18XX | | CFG_TUSB_MCU = = OPT_MCU_LPC43XX | | CFG_TUSB_MCU = = OPT_MCU_MIMXRT10XX )
2018-12-04 06:47:58 +01:00
2021-12-01 06:14:44 +01:00
# warning "transdimenion is renamed to chipidea (portable / chipidea / ci_hs) to match other opensource naming convention such as linux. This file will be removed in the future, please update your makefile accordingly"
2018-12-04 06:47:58 +01:00
//--------------------------------------------------------------------+
// INCLUDE
//--------------------------------------------------------------------+
2019-11-22 11:40:12 +01:00
# if CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
2019-11-21 10:08:08 +01:00
# include "fsl_device_registers.h"
2021-04-24 07:19:13 +02:00
# define INCLUDE_FSL_DEVICE_REGISTERS
2019-11-21 10:08:08 +01:00
# else
2020-01-17 06:59:00 +01:00
// LPCOpen for 18xx & 43xx
2019-11-21 10:08:08 +01:00
# include "chip.h"
2020-01-17 11:09:23 +01:00
# endif
2021-02-22 19:27:52 +01:00
# include "common/tusb_common.h"
# include "device/dcd.h"
# include "common_transdimension.h"
2020-01-17 11:09:23 +01:00
# if defined(__CORTEX_M) && __CORTEX_M == 7 && __DCACHE_PRESENT == 1
# define CleanInvalidateDCache_by_Addr SCB_CleanInvalidateDCache_by_Addr
# else
# define CleanInvalidateDCache_by_Addr(_addr, _dsize)
2019-11-21 10:08:08 +01:00
# endif
2018-12-04 06:47:58 +01:00
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
2018-12-04 10:06:50 +01:00
2019-11-22 06:14:55 +01:00
// ENDPTCTRL
enum {
ENDPTCTRL_STALL = TU_BIT ( 0 ) ,
2021-09-11 16:29:04 +02:00
ENDPTCTRL_TOGGLE_INHIBIT = TU_BIT ( 5 ) , // used for test only
2019-11-22 06:14:55 +01:00
ENDPTCTRL_TOGGLE_RESET = TU_BIT ( 6 ) ,
ENDPTCTRL_ENABLE = TU_BIT ( 7 )
} ;
2021-09-11 16:29:04 +02:00
enum {
ENDPTCTRL_TYPE_POS = 2 , // Endpoint type is 2-bit field
} ;
2019-11-22 06:14:55 +01:00
// USBSTS, USBINTR
enum {
INTR_USB = TU_BIT ( 0 ) ,
INTR_ERROR = TU_BIT ( 1 ) ,
INTR_PORT_CHANGE = TU_BIT ( 2 ) ,
INTR_RESET = TU_BIT ( 6 ) ,
INTR_SOF = TU_BIT ( 7 ) ,
INTR_SUSPEND = TU_BIT ( 8 ) ,
INTR_NAK = TU_BIT ( 16 )
} ;
// Queue Transfer Descriptor
2019-11-21 18:38:22 +01:00
typedef struct
{
// Word 0: Next QTD Pointer
uint32_t next ; ///< Next link pointer This field contains the physical memory address of the next dTD to be processed
// Word 1: qTQ Token
uint32_t : 3 ;
volatile uint32_t xact_err : 1 ;
uint32_t : 1 ;
volatile uint32_t buffer_err : 1 ;
volatile uint32_t halted : 1 ;
volatile uint32_t active : 1 ;
uint32_t : 2 ;
uint32_t iso_mult_override : 2 ; ///< This field can be used for transmit ISOs to override the MULT field in the dQH. This field must be zero for all packet types that are not transmit-ISO.
uint32_t : 3 ;
uint32_t int_on_complete : 1 ;
volatile uint32_t total_bytes : 15 ;
2021-09-11 14:27:37 +02:00
uint32_t : 1 ;
2019-11-21 18:38:22 +01:00
// Word 2-6: Buffer Page Pointer List, Each element in the list is a 4K page aligned, physical memory address. The lower 12 bits in each pointer are reserved (except for the first one) as each memory pointer must reference the start of a 4K page
uint32_t buffer [ 5 ] ; ///< buffer1 has frame_n for TODO Isochronous
2021-09-11 14:27:37 +02:00
//--------------------------------------------------------------------+
// TD is 32 bytes aligned but occupies only 28 bytes
// Therefore there are 4 bytes padding that we can use.
//--------------------------------------------------------------------+
2019-11-21 18:38:22 +01:00
uint16_t expected_bytes ;
uint8_t reserved [ 2 ] ;
} dcd_qtd_t ;
TU_VERIFY_STATIC ( sizeof ( dcd_qtd_t ) = = 32 , " size is not correct " ) ;
2019-11-22 06:14:55 +01:00
// Queue Head
2019-11-21 18:38:22 +01:00
typedef struct
{
// Word 0: Capabilities and Characteristics
uint32_t : 15 ; ///< Number of packets executed per transaction descriptor 00 - Execute N transactions as demonstrated by the USB variable length protocol where N is computed using Max_packet_length and the Total_bytes field in the dTD. 01 - Execute one transaction 10 - Execute two transactions 11 - Execute three transactions Remark: Non-isochronous endpoints must set MULT = 00. Remark: Isochronous endpoints must set MULT = 01, 10, or 11 as needed.
uint32_t int_on_setup : 1 ; ///< Interrupt on setup This bit is used on control type endpoints to indicate if USBINT is set in response to a setup being received.
2021-09-11 14:27:37 +02:00
uint32_t max_packet_size : 11 ; ///< Endpoint's wMaxPacketSize
2019-11-21 18:38:22 +01:00
uint32_t : 2 ;
uint32_t zero_length_termination : 1 ; ///< This bit is used for non-isochronous endpoints to indicate when a zero-length packet is received to terminate transfers in case the total transfer length is “multiple”. 0 - Enable zero-length packet to terminate transfers equal to a multiple of Max_packet_length (default). 1 - Disable zero-length packet on transfers that are equal in length to a multiple Max_packet_length.
uint32_t iso_mult : 2 ; ///<
// Word 1: Current qTD Pointer
2020-01-17 06:59:00 +01:00
volatile uint32_t qtd_addr ;
2019-11-21 18:38:22 +01:00
2020-01-17 06:59:00 +01:00
// Word 2-9: Transfer Overlay
volatile dcd_qtd_t qtd_overlay ;
2019-11-21 18:38:22 +01:00
2020-01-17 06:59:00 +01:00
// Word 10-11: Setup request (control OUT only)
volatile tusb_control_request_t setup_request ;
2019-11-21 18:38:22 +01:00
2020-01-17 06:59:00 +01:00
//--------------------------------------------------------------------+
2021-09-11 14:27:37 +02:00
// QHD is 64 bytes aligned but occupies only 48 bytes
// Therefore there are 16 bytes padding that we can use.
2019-11-21 18:38:22 +01:00
//--------------------------------------------------------------------+
2021-06-26 19:53:57 +02:00
tu_fifo_t * ff ;
uint8_t reserved [ 12 ] ;
2020-01-17 11:09:23 +01:00
} dcd_qhd_t ;
2019-11-21 18:38:22 +01:00
TU_VERIFY_STATIC ( sizeof ( dcd_qhd_t ) = = 64 , " size is not correct " ) ;
//--------------------------------------------------------------------+
// Variables
//--------------------------------------------------------------------+
2020-01-17 11:09:23 +01:00
typedef struct
{
dcd_registers_t * regs ; // registers
const IRQn_Type irqnum ; // IRQ number
2020-05-26 17:15:38 +02:00
const uint8_t ep_count ; // Max bi-directional Endpoints
2020-01-17 11:09:23 +01:00
} dcd_controller_t ;
# if CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
2020-05-26 17:15:38 +02:00
static const dcd_controller_t _dcd_controller [ ] =
2020-01-17 11:09:23 +01:00
{
// RT1010 and RT1020 only has 1 USB controller
# if FSL_FEATURE_SOC_USBHS_COUNT == 1
{ . regs = ( dcd_registers_t * ) USB_BASE , . irqnum = USB_OTG1_IRQn , . ep_count = 8 }
# else
{ . regs = ( dcd_registers_t * ) USB1_BASE , . irqnum = USB_OTG1_IRQn , . ep_count = 8 } ,
{ . regs = ( dcd_registers_t * ) USB2_BASE , . irqnum = USB_OTG2_IRQn , . ep_count = 8 }
# endif
} ;
# else
2020-05-26 17:15:38 +02:00
static const dcd_controller_t _dcd_controller [ ] =
2020-01-17 11:09:23 +01:00
{
{ . regs = ( dcd_registers_t * ) LPC_USB0_BASE , . irqnum = USB0_IRQn , . ep_count = 6 } ,
{ . regs = ( dcd_registers_t * ) LPC_USB1_BASE , . irqnum = USB1_IRQn , . ep_count = 4 }
} ;
# endif
2018-12-04 10:06:50 +01:00
# define QTD_NEXT_INVALID 0x01
2018-12-04 06:47:58 +01:00
typedef struct {
2018-12-04 10:06:50 +01:00
// Must be at 2K alignment
2021-09-09 15:26:03 +02:00
// Each endpoint with direction (IN/OUT) occupies a queue head
// for portability, TinyUSB only queue 1 TD for each Qhd
dcd_qhd_t qhd [ DCD_ATTR_ENDPOINT_MAX ] [ 2 ] TU_ATTR_ALIGNED ( 64 ) ;
dcd_qtd_t qtd [ DCD_ATTR_ENDPOINT_MAX ] [ 2 ] TU_ATTR_ALIGNED ( 32 ) ;
2018-12-04 06:47:58 +01:00
} dcd_data_t ;
2020-05-17 09:24:15 +02:00
CFG_TUSB_MEM_SECTION TU_ATTR_ALIGNED ( 2048 )
static dcd_data_t _dcd_data ;
2020-01-17 06:59:00 +01:00
2018-12-04 06:47:58 +01:00
//--------------------------------------------------------------------+
2021-02-22 19:27:52 +01:00
// Controller API
2018-12-04 06:47:58 +01:00
//--------------------------------------------------------------------+
/// follows LPC43xx User Manual 23.10.3
static void bus_reset ( uint8_t rhport )
{
2020-01-17 11:09:23 +01:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
2018-12-04 06:47:58 +01:00
// The reset value for all endpoint types is the control endpoint. If one endpoint
// direction is enabled and the paired endpoint of opposite direction is disabled, then the
2020-01-17 11:09:23 +01:00
// endpoint type of the unused direction must be changed from the control type to any other
// type (e.g. bulk). Leaving an un-configured endpoint control will cause undefined behavior
2018-12-04 06:47:58 +01:00
// for the data PID tracking on the active endpoint.
2021-09-11 16:29:04 +02:00
for ( uint8_t i = 1 ; i < _dcd_controller [ rhport ] . ep_count ; i + + )
2018-12-04 06:47:58 +01:00
{
2021-09-11 16:29:04 +02:00
dcd_reg - > ENDPTCTRL [ i ] = ( TUSB_XFER_BULK < < ENDPTCTRL_TYPE_POS ) | ( TUSB_XFER_BULK < < ( 16 + ENDPTCTRL_TYPE_POS ) ) ;
2018-12-04 06:47:58 +01:00
}
//------------- Clear All Registers -------------//
2019-11-22 06:16:47 +01:00
dcd_reg - > ENDPTNAK = dcd_reg - > ENDPTNAK ;
dcd_reg - > ENDPTNAKEN = 0 ;
dcd_reg - > USBSTS = dcd_reg - > USBSTS ;
dcd_reg - > ENDPTSETUPSTAT = dcd_reg - > ENDPTSETUPSTAT ;
dcd_reg - > ENDPTCOMPLETE = dcd_reg - > ENDPTCOMPLETE ;
2018-12-04 06:47:58 +01:00
2020-01-17 06:06:59 +01:00
while ( dcd_reg - > ENDPTPRIME ) { }
2019-11-22 06:16:47 +01:00
dcd_reg - > ENDPTFLUSH = 0xFFFFFFFF ;
2020-01-17 06:06:59 +01:00
while ( dcd_reg - > ENDPTFLUSH ) { }
2018-12-04 06:47:58 +01:00
// read reset bit in portsc
//------------- Queue Head & Queue TD -------------//
2019-11-21 18:58:18 +01:00
tu_memclr ( & _dcd_data , sizeof ( dcd_data_t ) ) ;
2018-12-04 06:47:58 +01:00
//------------- Set up Control Endpoints (0 OUT, 1 IN) -------------//
2021-09-09 15:26:03 +02:00
_dcd_data . qhd [ 0 ] [ 0 ] . zero_length_termination = _dcd_data . qhd [ 0 ] [ 1 ] . zero_length_termination = 1 ;
2021-09-11 14:27:37 +02:00
_dcd_data . qhd [ 0 ] [ 0 ] . max_packet_size = _dcd_data . qhd [ 0 ] [ 1 ] . max_packet_size = CFG_TUD_ENDPOINT0_SIZE ;
2021-09-09 15:26:03 +02:00
_dcd_data . qhd [ 0 ] [ 0 ] . qtd_overlay . next = _dcd_data . qhd [ 0 ] [ 1 ] . qtd_overlay . next = QTD_NEXT_INVALID ;
2018-12-04 06:47:58 +01:00
2021-09-09 15:26:03 +02:00
_dcd_data . qhd [ 0 ] [ 0 ] . int_on_setup = 1 ; // OUT only
2018-12-04 06:47:58 +01:00
}
2019-03-29 10:23:00 +01:00
void dcd_init ( uint8_t rhport )
2018-12-04 06:47:58 +01:00
{
2019-11-22 08:20:10 +01:00
tu_memclr ( & _dcd_data , sizeof ( dcd_data_t ) ) ;
2020-04-16 11:56:16 +02:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
2018-12-04 06:47:58 +01:00
2019-11-22 08:20:10 +01:00
// Reset controller
dcd_reg - > USBCMD | = USBCMD_RESET ;
while ( dcd_reg - > USBCMD & USBCMD_RESET ) { }
// Set mode to device, must be set immediately after reset
dcd_reg - > USBMODE = USBMODE_CM_DEVICE ;
dcd_reg - > OTGSC = OTGSC_VBUS_DISCHARGE | OTGSC_OTG_TERMINATION ;
2021-06-26 23:13:01 +02:00
# if !TUD_OPT_HIGH_SPEED
dcd_reg - > PORTSC1 = PORTSC1_FORCE_FULL_SPEED ;
# endif
2018-12-04 06:47:58 +01:00
2020-01-17 06:59:00 +01:00
CleanInvalidateDCache_by_Addr ( ( uint32_t * ) & _dcd_data , sizeof ( dcd_data_t ) ) ;
2020-01-16 06:59:15 +01:00
2019-11-22 06:16:47 +01:00
dcd_reg - > ENDPTLISTADDR = ( uint32_t ) _dcd_data . qhd ; // Endpoint List Address has to be 2K alignment
dcd_reg - > USBSTS = dcd_reg - > USBSTS ;
2021-09-09 12:04:04 +02:00
dcd_reg - > USBINTR = INTR_USB | INTR_ERROR | INTR_PORT_CHANGE | INTR_SUSPEND ;
2018-12-04 06:47:58 +01:00
2020-08-01 15:14:58 +02:00
dcd_reg - > USBCMD & = ~ 0x00FF0000 ; // Interrupt Threshold Interval = 0
dcd_reg - > USBCMD | = USBCMD_RUN_STOP ; // Connect
2018-12-05 07:20:25 +01:00
}
void dcd_int_enable ( uint8_t rhport )
{
2020-01-17 11:09:23 +01:00
NVIC_EnableIRQ ( _dcd_controller [ rhport ] . irqnum ) ;
2018-12-05 07:20:25 +01:00
}
2018-12-04 06:47:58 +01:00
2018-12-05 07:20:25 +01:00
void dcd_int_disable ( uint8_t rhport )
{
2020-01-17 11:09:23 +01:00
NVIC_DisableIRQ ( _dcd_controller [ rhport ] . irqnum ) ;
2018-12-04 06:47:58 +01:00
}
2018-12-17 06:14:11 +01:00
void dcd_set_address ( uint8_t rhport , uint8_t dev_addr )
{
2019-03-13 17:14:48 +01:00
// Response with status first before changing device address
dcd_edpt_xfer ( rhport , tu_edpt_addr ( 0 , TUSB_DIR_IN ) , NULL , 0 ) ;
2020-01-17 11:09:23 +01:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
dcd_reg - > DEVICEADDR = ( dev_addr < < 25 ) | TU_BIT ( 24 ) ;
2018-12-17 06:14:11 +01:00
}
2019-03-30 08:47:11 +01:00
void dcd_remote_wakeup ( uint8_t rhport )
{
2021-09-09 11:01:05 +02:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
2021-09-09 12:04:04 +02:00
dcd_reg - > PORTSC1 | = PORTSC1_FORCE_PORT_RESUME ;
2019-03-30 08:47:11 +01:00
}
2020-04-16 11:56:16 +02:00
void dcd_connect ( uint8_t rhport )
{
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
dcd_reg - > USBCMD | = USBCMD_RUN_STOP ;
}
void dcd_disconnect ( uint8_t rhport )
{
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
dcd_reg - > USBCMD & = ~ USBCMD_RUN_STOP ;
}
2018-12-04 06:47:58 +01:00
//--------------------------------------------------------------------+
// HELPER
//--------------------------------------------------------------------+
static void qtd_init ( dcd_qtd_t * p_qtd , void * data_ptr , uint16_t total_bytes )
{
2021-09-12 19:45:15 +02:00
// Force the CPU to flush the buffer. We increase the size by 31 because the call aligns the
// address to 32-byte boundaries. Buffer must be word aligned
CleanInvalidateDCache_by_Addr ( ( uint32_t * ) tu_align ( ( uint32_t ) data_ptr , 4 ) , total_bytes + 31 ) ;
2018-12-04 06:47:58 +01:00
tu_memclr ( p_qtd , sizeof ( dcd_qtd_t ) ) ;
2021-09-12 19:45:15 +02:00
p_qtd - > next = QTD_NEXT_INVALID ;
p_qtd - > active = 1 ;
p_qtd - > total_bytes = p_qtd - > expected_bytes = total_bytes ;
p_qtd - > int_on_complete = true ;
2018-12-04 06:47:58 +01:00
if ( data_ptr ! = NULL )
{
2021-09-12 19:45:15 +02:00
p_qtd - > buffer [ 0 ] = ( uint32_t ) data_ptr ;
uint32_t const bufend = p_qtd - > buffer [ 0 ] + total_bytes ;
2018-12-04 06:47:58 +01:00
for ( uint8_t i = 1 ; i < 5 ; i + + )
{
2021-09-12 19:45:15 +02:00
uint32_t const next_page = tu_align4k ( p_qtd - > buffer [ i - 1 ] ) + 4096 ;
if ( bufend < = next_page ) break ;
2021-06-26 19:53:57 +02:00
2021-09-12 19:45:15 +02:00
p_qtd - > buffer [ i ] = next_page ;
2021-06-26 19:53:57 +02:00
2021-09-12 19:45:15 +02:00
// TODO page[1] FRAME_N for ISO transfer
2021-06-26 23:13:01 +02:00
}
2021-06-26 19:53:57 +02:00
}
}
2018-12-04 06:47:58 +01:00
//--------------------------------------------------------------------+
// DCD Endpoint Port
//--------------------------------------------------------------------+
void dcd_edpt_stall ( uint8_t rhport , uint8_t ep_addr )
{
2018-12-12 05:51:31 +01:00
uint8_t const epnum = tu_edpt_number ( ep_addr ) ;
uint8_t const dir = tu_edpt_dir ( ep_addr ) ;
2018-12-04 06:47:58 +01:00
2020-01-17 11:09:23 +01:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
dcd_reg - > ENDPTCTRL [ epnum ] | = ENDPTCTRL_STALL < < ( dir ? 16 : 0 ) ;
2021-09-09 15:26:03 +02:00
// flush to abort any primed buffer
dcd_reg - > ENDPTFLUSH = TU_BIT ( epnum + ( dir ? 16 : 0 ) ) ;
2018-12-04 06:47:58 +01:00
}
void dcd_edpt_clear_stall ( uint8_t rhport , uint8_t ep_addr )
{
2021-09-09 15:26:03 +02:00
uint8_t const epnum = tu_edpt_number ( ep_addr ) ;
uint8_t const dir = tu_edpt_dir ( ep_addr ) ;
2018-12-04 06:47:58 +01:00
// data toggle also need to be reset
2020-01-17 11:09:23 +01:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
dcd_reg - > ENDPTCTRL [ epnum ] | = ENDPTCTRL_TOGGLE_RESET < < ( dir ? 16 : 0 ) ;
dcd_reg - > ENDPTCTRL [ epnum ] & = ~ ( ENDPTCTRL_STALL < < ( dir ? 16 : 0 ) ) ;
2018-12-04 06:47:58 +01:00
}
bool dcd_edpt_open ( uint8_t rhport , tusb_desc_endpoint_t const * p_endpoint_desc )
{
2021-09-09 15:26:03 +02:00
uint8_t const epnum = tu_edpt_number ( p_endpoint_desc - > bEndpointAddress ) ;
uint8_t const dir = tu_edpt_dir ( p_endpoint_desc - > bEndpointAddress ) ;
2018-12-04 06:47:58 +01:00
2020-01-17 11:09:23 +01:00
// Must not exceed max endpoint number
TU_ASSERT ( epnum < _dcd_controller [ rhport ] . ep_count ) ;
2018-12-04 10:06:50 +01:00
2018-12-04 06:47:58 +01:00
//------------- Prepare Queue Head -------------//
2021-09-09 15:26:03 +02:00
dcd_qhd_t * p_qhd = & _dcd_data . qhd [ epnum ] [ dir ] ;
2018-12-04 06:47:58 +01:00
tu_memclr ( p_qhd , sizeof ( dcd_qhd_t ) ) ;
p_qhd - > zero_length_termination = 1 ;
2021-10-24 08:11:21 +02:00
p_qhd - > max_packet_size = tu_edpt_packet_size ( p_endpoint_desc ) ;
2021-06-26 23:13:01 +02:00
if ( p_endpoint_desc - > bmAttributes . xfer = = TUSB_XFER_ISOCHRONOUS )
{
p_qhd - > iso_mult = 1 ;
}
2018-12-04 06:47:58 +01:00
p_qhd - > qtd_overlay . next = QTD_NEXT_INVALID ;
2020-01-17 02:17:49 +01:00
2020-01-17 06:59:00 +01:00
CleanInvalidateDCache_by_Addr ( ( uint32_t * ) & _dcd_data , sizeof ( dcd_data_t ) ) ;
2018-12-04 06:47:58 +01:00
// Enable EP Control
2020-01-17 11:09:23 +01:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
2021-06-26 23:13:01 +02:00
2021-09-12 12:39:32 +02:00
uint32_t const epctrl = ( p_endpoint_desc - > bmAttributes . xfer < < ENDPTCTRL_TYPE_POS ) | ENDPTCTRL_ENABLE | ENDPTCTRL_TOGGLE_RESET ;
2021-06-26 23:13:01 +02:00
2021-09-12 12:39:32 +02:00
if ( dir = = TUSB_DIR_OUT )
{
dcd_reg - > ENDPTCTRL [ epnum ] = ( dcd_reg - > ENDPTCTRL [ epnum ] & 0xFFFF0000u ) | epctrl ;
} else
{
dcd_reg - > ENDPTCTRL [ epnum ] = ( dcd_reg - > ENDPTCTRL [ epnum ] & 0x0000FFFFu ) | ( epctrl < < 16 ) ;
}
2018-12-04 06:47:58 +01:00
return true ;
}
2021-08-26 12:02:24 +02:00
void dcd_edpt_close_all ( uint8_t rhport )
{
2021-09-11 16:29:04 +02:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
// Disable all non-control endpoints
for ( uint8_t epnum = 1 ; epnum < _dcd_controller [ rhport ] . ep_count ; epnum + + )
{
_dcd_data . qhd [ epnum ] [ TUSB_DIR_OUT ] . qtd_overlay . halted = 1 ;
_dcd_data . qhd [ epnum ] [ TUSB_DIR_IN ] . qtd_overlay . halted = 1 ;
dcd_reg - > ENDPTFLUSH = TU_BIT ( epnum ) | TU_BIT ( epnum + 16 ) ;
dcd_reg - > ENDPTCTRL [ epnum ] = ( TUSB_XFER_BULK < < ENDPTCTRL_TYPE_POS ) | ( TUSB_XFER_BULK < < ( 16 + ENDPTCTRL_TYPE_POS ) ) ;
}
2021-08-26 12:02:24 +02:00
}
2021-06-26 23:13:01 +02:00
void dcd_edpt_close ( uint8_t rhport , uint8_t ep_addr )
{
uint8_t const epnum = tu_edpt_number ( ep_addr ) ;
uint8_t const dir = tu_edpt_dir ( ep_addr ) ;
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
2021-09-12 12:39:32 +02:00
_dcd_data . qhd [ epnum ] [ dir ] . qtd_overlay . halted = 1 ;
2021-06-26 23:13:01 +02:00
// Flush EP
2021-09-12 12:39:32 +02:00
uint32_t const flush_mask = TU_BIT ( epnum + ( dir ? 16 : 0 ) ) ;
2021-06-26 23:13:01 +02:00
dcd_reg - > ENDPTFLUSH = flush_mask ;
while ( dcd_reg - > ENDPTFLUSH & flush_mask ) ;
// Clear EP enable
dcd_reg - > ENDPTCTRL [ epnum ] & = ~ ( ENDPTCTRL_ENABLE < < ( dir ? 16 : 0 ) ) ;
}
2021-09-12 19:45:15 +02:00
static void qhd_start_xfer ( uint8_t rhport , uint8_t epnum , uint8_t dir )
2018-12-04 06:47:58 +01:00
{
2020-01-17 11:09:23 +01:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
2021-09-12 19:45:15 +02:00
dcd_qhd_t * p_qhd = & _dcd_data . qhd [ epnum ] [ dir ] ;
dcd_qtd_t * p_qtd = & _dcd_data . qtd [ epnum ] [ dir ] ;
p_qhd - > qtd_overlay . halted = false ; // clear any previous error
p_qhd - > qtd_overlay . next = ( uint32_t ) p_qtd ; // link qtd to qhd
// flush cache
CleanInvalidateDCache_by_Addr ( ( uint32_t * ) & _dcd_data , sizeof ( dcd_data_t ) ) ;
2018-12-04 06:47:58 +01:00
if ( epnum = = 0 )
{
// follows UM 24.10.8.1.1 Setup packet handling using setup lockout mechanism
// wait until ENDPTSETUPSTAT before priming data/status in response TODO add time out
2020-01-17 11:09:23 +01:00
while ( dcd_reg - > ENDPTSETUPSTAT & TU_BIT ( 0 ) ) { }
2018-12-04 06:47:58 +01:00
}
2021-09-12 19:45:15 +02:00
// start transfer
dcd_reg - > ENDPTPRIME = TU_BIT ( epnum + ( dir ? 16 : 0 ) ) ;
}
bool dcd_edpt_xfer ( uint8_t rhport , uint8_t ep_addr , uint8_t * buffer , uint16_t total_bytes )
{
uint8_t const epnum = tu_edpt_number ( ep_addr ) ;
uint8_t const dir = tu_edpt_dir ( ep_addr ) ;
2018-12-04 06:47:58 +01:00
2021-09-12 19:45:15 +02:00
dcd_qhd_t * p_qhd = & _dcd_data . qhd [ epnum ] [ dir ] ;
dcd_qtd_t * p_qtd = & _dcd_data . qtd [ epnum ] [ dir ] ;
2020-01-16 06:59:15 +01:00
2021-09-12 19:45:15 +02:00
// Prepare qtd
2018-12-04 06:47:58 +01:00
qtd_init ( p_qtd , buffer , total_bytes ) ;
2021-09-11 14:27:37 +02:00
2021-09-12 19:45:15 +02:00
// Start qhd transfer
2021-09-12 12:39:32 +02:00
p_qhd - > ff = NULL ;
2021-09-12 19:45:15 +02:00
qhd_start_xfer ( rhport , epnum , dir ) ;
2018-12-04 06:47:58 +01:00
return true ;
}
2021-06-26 19:53:57 +02:00
// fifo has to be aligned to 4k boundary
2021-06-26 23:13:01 +02:00
bool dcd_edpt_xfer_fifo ( uint8_t rhport , uint8_t ep_addr , tu_fifo_t * ff , uint16_t total_bytes )
2021-06-26 19:53:57 +02:00
{
uint8_t const epnum = tu_edpt_number ( ep_addr ) ;
uint8_t const dir = tu_edpt_dir ( ep_addr ) ;
2021-09-12 12:39:32 +02:00
dcd_qhd_t * p_qhd = & _dcd_data . qhd [ epnum ] [ dir ] ;
dcd_qtd_t * p_qtd = & _dcd_data . qtd [ epnum ] [ dir ] ;
2021-06-26 19:53:57 +02:00
tu_fifo_buffer_info_t fifo_info ;
2021-06-26 23:13:01 +02:00
if ( dir )
2021-06-26 19:53:57 +02:00
{
tu_fifo_get_read_info ( ff , & fifo_info ) ;
2021-06-26 23:13:01 +02:00
} else
2021-06-26 19:53:57 +02:00
{
tu_fifo_get_write_info ( ff , & fifo_info ) ;
}
2021-06-26 23:13:01 +02:00
2021-09-12 19:45:15 +02:00
if ( fifo_info . len_lin > = total_bytes )
2021-06-26 23:13:01 +02:00
{
2021-09-12 19:45:15 +02:00
// Linear length is enough for this transfer
qtd_init ( p_qtd , fifo_info . ptr_lin , total_bytes ) ;
2021-06-26 23:13:01 +02:00
}
2021-09-09 16:45:18 +02:00
else
{
2021-09-12 19:45:15 +02:00
// linear part is not enough
2021-09-09 16:45:18 +02:00
2021-09-12 19:45:15 +02:00
// prepare TD up to linear length
qtd_init ( p_qtd , fifo_info . ptr_lin , fifo_info . len_lin ) ;
2021-06-26 19:53:57 +02:00
2021-09-12 19:45:15 +02:00
if ( ! tu_offset4k ( ( uint32_t ) fifo_info . ptr_wrap ) & & ! tu_offset4k ( tu_fifo_depth ( ff ) ) )
{
// If buffer is aligned to 4K & buffer size is multiple of 4K
// We can make use of buffer page array to also combine the linear + wrapped length
p_qtd - > total_bytes = p_qtd - > expected_bytes = total_bytes ;
for ( uint8_t i = 1 , page = 0 ; i < 5 ; i + + )
{
// pick up buffer array where linear ends
if ( p_qtd - > buffer [ i ] = = 0 )
{
p_qtd - > buffer [ i ] = ( uint32_t ) fifo_info . ptr_wrap + 4096 * page ;
page + + ;
}
}
CleanInvalidateDCache_by_Addr ( ( uint32_t * ) tu_align ( ( uint32_t ) fifo_info . ptr_wrap , 4 ) , total_bytes - fifo_info . len_wrap + 31 ) ;
}
else
{
2021-09-13 08:09:38 +02:00
// TODO we may need to carry the wrapped length after the linear part complete
// for now only transfer up to linear part
2021-09-12 19:45:15 +02:00
}
}
2021-06-26 19:53:57 +02:00
2021-09-12 19:45:15 +02:00
// Start qhd transfer
p_qhd - > ff = ff ;
qhd_start_xfer ( rhport , epnum , dir ) ;
2021-06-26 19:53:57 +02:00
return true ;
}
2018-12-04 06:47:58 +01:00
//--------------------------------------------------------------------+
// ISR
//--------------------------------------------------------------------+
2021-09-09 15:26:03 +02:00
2021-09-11 14:27:37 +02:00
static void process_edpt_complete_isr ( uint8_t rhport , uint8_t epnum , uint8_t dir )
2021-09-09 15:26:03 +02:00
{
2021-09-12 12:39:32 +02:00
dcd_qhd_t * p_qhd = & _dcd_data . qhd [ epnum ] [ dir ] ;
2021-09-11 14:27:37 +02:00
dcd_qtd_t * p_qtd = & _dcd_data . qtd [ epnum ] [ dir ] ;
2021-09-09 15:26:03 +02:00
uint8_t result = p_qtd - > halted ? XFER_RESULT_STALLED :
( p_qtd - > xact_err | | p_qtd - > buffer_err ) ? XFER_RESULT_FAILED : XFER_RESULT_SUCCESS ;
2021-09-11 14:27:37 +02:00
if ( result ! = XFER_RESULT_SUCCESS )
{
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
// flush to abort error buffer
dcd_reg - > ENDPTFLUSH = TU_BIT ( epnum + ( dir ? 16 : 0 ) ) ;
}
2021-09-12 12:39:32 +02:00
uint16_t const xferred_bytes = p_qtd - > expected_bytes - p_qtd - > total_bytes ;
if ( p_qhd - > ff )
{
if ( dir = = TUSB_DIR_IN )
{
tu_fifo_advance_read_pointer ( p_qhd - > ff , xferred_bytes ) ;
} else
{
tu_fifo_advance_write_pointer ( p_qhd - > ff , xferred_bytes ) ;
}
}
2021-09-09 15:26:03 +02:00
// only number of bytes in the IOC qtd
2021-09-12 12:39:32 +02:00
dcd_event_xfer_complete ( rhport , tu_edpt_addr ( epnum , dir ) , xferred_bytes , result , true ) ;
2021-09-09 15:26:03 +02:00
}
2020-04-17 07:27:53 +02:00
void dcd_int_handler ( uint8_t rhport )
2018-12-04 06:47:58 +01:00
{
2021-09-09 11:01:05 +02:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
2018-12-04 06:47:58 +01:00
2019-11-22 06:16:47 +01:00
uint32_t const int_enable = dcd_reg - > USBINTR ;
uint32_t const int_status = dcd_reg - > USBSTS & int_enable ;
dcd_reg - > USBSTS = int_status ; // Acknowledge handled interrupt
2018-12-04 06:47:58 +01:00
2019-11-22 06:16:47 +01:00
// disabled interrupt sources
if ( int_status = = 0 ) return ;
2018-12-04 06:47:58 +01:00
2021-09-09 12:04:04 +02:00
// Set if the port controller enters the full or high-speed operational state.
// either from Bus Reset or Suspended state
if ( int_status & INTR_PORT_CHANGE )
{
// TU_LOG2("PortChange %08lx\r\n", dcd_reg->PORTSC1);
// Reset interrupt is not enabled, we manually check if Port Change is due
// to connection / disconnection
if ( dcd_reg - > USBSTS & INTR_RESET )
{
dcd_reg - > USBSTS = INTR_RESET ;
if ( dcd_reg - > PORTSC1 & PORTSC1_CURRENT_CONNECT_STATUS )
{
uint32_t const speed = ( dcd_reg - > PORTSC1 & PORTSC1_PORT_SPEED ) > > PORTSC1_PORT_SPEED_POS ;
bus_reset ( rhport ) ;
dcd_event_bus_reset ( rhport , ( tusb_speed_t ) speed , true ) ;
} else
{
dcd_event_bus_signal ( rhport , DCD_EVENT_UNPLUGGED , true ) ;
}
}
else
{
// Triggered by resuming from suspended state
if ( ! ( dcd_reg - > PORTSC1 & PORTSC1_SUSPEND ) )
{
dcd_event_bus_signal ( rhport , DCD_EVENT_RESUME , true ) ;
}
}
}
2018-12-04 06:47:58 +01:00
2019-11-22 06:14:55 +01:00
if ( int_status & INTR_SUSPEND )
2018-12-04 06:47:58 +01:00
{
2021-09-09 12:04:04 +02:00
// TU_LOG2("Suspend %08lx\r\n", dcd_reg->PORTSC1);
2019-11-22 08:20:10 +01:00
if ( dcd_reg - > PORTSC1 & PORTSC1_SUSPEND )
2018-12-04 06:47:58 +01:00
{
// Note: Host may delay more than 3 ms before and/or after bus reset before doing enumeration.
2021-09-09 11:01:05 +02:00
// Skip suspend event if we are not addressed
2019-11-22 06:16:47 +01:00
if ( ( dcd_reg - > DEVICEADDR > > 25 ) & 0x0f )
2018-12-04 06:47:58 +01:00
{
2019-03-28 19:34:53 +01:00
dcd_event_bus_signal ( rhport , DCD_EVENT_SUSPEND , true ) ;
2018-12-04 06:47:58 +01:00
}
}
}
2019-11-22 06:14:55 +01:00
if ( int_status & INTR_USB )
2018-12-04 06:47:58 +01:00
{
2021-09-11 14:27:37 +02:00
// Make sure we read the latest version of _dcd_data.
CleanInvalidateDCache_by_Addr ( ( uint32_t * ) & _dcd_data , sizeof ( dcd_data_t ) ) ;
2019-11-22 06:16:47 +01:00
uint32_t const edpt_complete = dcd_reg - > ENDPTCOMPLETE ;
dcd_reg - > ENDPTCOMPLETE = edpt_complete ; // acknowledge
2018-12-04 06:47:58 +01:00
2019-11-22 06:16:47 +01:00
if ( dcd_reg - > ENDPTSETUPSTAT )
2018-12-04 06:47:58 +01:00
{
//------------- Set up Received -------------//
// 23.10.10.2 Operational model for setup transfers
2021-09-09 15:26:03 +02:00
dcd_reg - > ENDPTSETUPSTAT = dcd_reg - > ENDPTSETUPSTAT ;
2018-12-04 06:47:58 +01:00
2021-10-17 11:26:27 +02:00
dcd_event_setup_received ( rhport , ( uint8_t * ) ( uintptr_t ) & _dcd_data . qhd [ 0 ] [ 0 ] . setup_request , true ) ;
2021-09-09 15:26:03 +02:00
}
// 23.10.12.3 Failed QTD also get ENDPTCOMPLETE set
2021-09-11 14:27:37 +02:00
// nothing to do, we will submit xfer as error to usbd
// if (int_status & INTR_ERROR) { }
2018-12-04 06:47:58 +01:00
if ( edpt_complete )
{
2021-09-11 14:27:37 +02:00
for ( uint8_t epnum = 0 ; epnum < DCD_ATTR_ENDPOINT_MAX ; epnum + + )
2018-12-04 06:47:58 +01:00
{
2021-09-11 14:27:37 +02:00
if ( tu_bit_test ( edpt_complete , epnum ) ) process_edpt_complete_isr ( rhport , epnum , TUSB_DIR_OUT ) ;
if ( tu_bit_test ( edpt_complete , epnum + 16 ) ) process_edpt_complete_isr ( rhport , epnum , TUSB_DIR_IN ) ;
2018-12-04 06:47:58 +01:00
}
}
}
2019-11-22 06:14:55 +01:00
if ( int_status & INTR_SOF )
2018-12-04 06:47:58 +01:00
{
dcd_event_bus_signal ( rhport , DCD_EVENT_SOF , true ) ;
}
}
# endif