2019-03-20 16:11:42 +07:00
/*
* The MIT License ( MIT )
*
2019-05-14 11:48:05 +07:00
* Copyright ( c ) 2019 Ha Thach ( tinyusb . org )
2019-03-20 16:11:42 +07: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-05 08:51:11 +07:00
# include "tusb_option.h"
2019-09-01 16:44:40 +07:00
/* Since 2012 starting with LPC11uxx, NXP start to use common USB Device Controller with code name LPC IP3511
2019-09-01 00:51:58 +07:00
* for almost their new MCUs . Currently supported and tested families are
2019-10-04 13:13:51 +07:00
* - LPC11U68 , LPC11U37
* - LPC1347
* - LPC51U68
* - LPC54114
* - LPC55s69
2019-09-01 00:51:58 +07:00
*/
2022-02-23 21:46:40 +07:00
# if CFG_TUD_ENABLED && ( CFG_TUSB_MCU == OPT_MCU_LPC11UXX || \
2019-09-01 00:51:58 +07:00
CFG_TUSB_MCU = = OPT_MCU_LPC13XX | | \
2019-10-04 13:13:51 +07:00
CFG_TUSB_MCU = = OPT_MCU_LPC15XX | | \
2019-09-01 16:15:23 +07:00
CFG_TUSB_MCU = = OPT_MCU_LPC51UXX | | \
2019-09-05 16:12:13 +07:00
CFG_TUSB_MCU = = OPT_MCU_LPC54XXX | | \
CFG_TUSB_MCU = = OPT_MCU_LPC55XX )
2019-08-31 22:44:08 +07:00
2021-04-24 10:44:14 +07:00
//--------------------------------------------------------------------+
// INCLUDE
//--------------------------------------------------------------------+
2019-10-04 13:13:51 +07:00
# if CFG_TUSB_MCU == OPT_MCU_LPC11UXX || CFG_TUSB_MCU == OPT_MCU_LPC13XX || CFG_TUSB_MCU == OPT_MCU_LPC15XX
2021-04-24 10:44:14 +07:00
// LPCOpen
2019-08-31 22:44:08 +07:00
# include "chip.h"
2021-04-24 10:44:14 +07:00
# else
// SDK
2019-08-31 22:44:08 +07:00
# include "fsl_device_registers.h"
2021-04-24 10:44:14 +07:00
# define INCLUDE_FSL_DEVICE_REGISTERS
2019-08-31 22:44:08 +07:00
# endif
2018-12-05 08:51:11 +07:00
# include "device/dcd.h"
2021-04-24 11:30:14 +07:00
//--------------------------------------------------------------------+
// IP3511 Registers
//--------------------------------------------------------------------+
2021-04-24 10:44:14 +07:00
typedef struct {
__IO uint32_t DEVCMDSTAT ; // Device Command/Status register, offset: 0x0
__I uint32_t INFO ; // Info register, offset: 0x4
__IO uint32_t EPLISTSTART ; // EP Command/Status List start address, offset: 0x8
__IO uint32_t DATABUFSTART ; // Data buffer start address, offset: 0xC
__IO uint32_t LPM ; // Link Power Management register, offset: 0x10
__IO uint32_t EPSKIP ; // Endpoint skip, offset: 0x14
__IO uint32_t EPINUSE ; // Endpoint Buffer in use, offset: 0x18
__IO uint32_t EPBUFCFG ; // Endpoint Buffer Configuration register, offset: 0x1C
__IO uint32_t INTSTAT ; // interrupt status register, offset: 0x20
__IO uint32_t INTEN ; // interrupt enable register, offset: 0x24
__IO uint32_t INTSETSTAT ; // set interrupt status register, offset: 0x28
uint8_t RESERVED_0 [ 8 ] ;
__I uint32_t EPTOGGLE ; // Endpoint toggle register, offset: 0x34
} dcd_registers_t ;
2021-04-26 20:22:40 +07:00
// Max nbytes for each control/bulk/interrupt transfer
2018-12-05 08:51:11 +07:00
enum {
2022-10-11 09:59:28 -04:00
NBYTES_CBI_FS_ISO_MAX = 1023 , // FS ISO
NBYTES_CBI_FS_MAX = 64 , // FS control/bulk/interrupt
NBYTES_CBI_HS_MAX = 32767 // can be up to all 15-bit, but only tested with 4096
2018-12-05 08:51:11 +07:00
} ;
enum {
2018-12-14 15:28:38 +07:00
INT_SOF_MASK = TU_BIT ( 30 ) ,
INT_DEVICE_STATUS_MASK = TU_BIT ( 31 )
2018-12-05 08:51:11 +07:00
} ;
enum {
2018-12-14 15:28:38 +07:00
CMDSTAT_DEVICE_ADDR_MASK = TU_BIT ( 7 ) - 1 ,
CMDSTAT_DEVICE_ENABLE_MASK = TU_BIT ( 7 ) ,
CMDSTAT_SETUP_RECEIVED_MASK = TU_BIT ( 8 ) ,
2021-04-24 12:19:13 +07:00
CMDSTAT_DEVICE_CONNECT_MASK = TU_BIT ( 16 ) , // reflect the soft-connect only, does not reflect the actual attached state
2018-12-14 15:28:38 +07:00
CMDSTAT_DEVICE_SUSPEND_MASK = TU_BIT ( 17 ) ,
2021-04-24 12:19:13 +07:00
// 23-22 is link speed (only available for HighSpeed port)
2018-12-14 15:28:38 +07:00
CMDSTAT_CONNECT_CHANGE_MASK = TU_BIT ( 24 ) ,
CMDSTAT_SUSPEND_CHANGE_MASK = TU_BIT ( 25 ) ,
CMDSTAT_RESET_CHANGE_MASK = TU_BIT ( 26 ) ,
CMDSTAT_VBUS_DEBOUNCED_MASK = TU_BIT ( 28 ) ,
2018-12-05 08:51:11 +07:00
} ;
2021-04-24 12:19:13 +07:00
enum {
CMDSTAT_SPEED_SHIFT = 22
} ;
2021-04-24 11:30:14 +07:00
//--------------------------------------------------------------------+
// Endpoint Command/Status List
//--------------------------------------------------------------------+
2021-04-26 14:43:58 +07:00
// Endpoint Command/Status
typedef union TU_ATTR_PACKED
2018-12-05 08:51:11 +07:00
{
2021-04-26 14:43:58 +07:00
// Full and High speed has different bit layout for buffer_offset and nbytes
// Buffer (aligned 64) = DATABUFSTART [31:22] | buffer_offset [21:6]
volatile struct {
uint32_t offset : 16 ;
uint32_t nbytes : 10 ;
uint32_t TU_RESERVED : 6 ;
} buffer_fs ;
// Buffer (aligned 64) = USB_RAM [31:17] | buffer_offset [16:6]
volatile struct {
uint32_t offset : 11 ;
uint32_t nbytes : 15 ;
uint32_t TU_RESERVED : 6 ;
} buffer_hs ;
volatile struct {
uint32_t TU_RESERVED : 26 ;
uint32_t is_iso : 1 ;
uint32_t toggle_mode : 1 ;
uint32_t toggle_reset : 1 ;
uint32_t stall : 1 ;
uint32_t disable : 1 ;
uint32_t active : 1 ;
} ;
2018-12-05 08:51:11 +07:00
} ep_cmd_sts_t ;
TU_VERIFY_STATIC ( sizeof ( ep_cmd_sts_t ) = = 4 , " size is not correct " ) ;
2021-04-26 14:43:58 +07:00
// Software transfer management
2018-12-05 08:51:11 +07:00
typedef struct
{
uint16_t total_bytes ;
uint16_t xferred_bytes ;
uint16_t nbytes ;
2021-04-24 15:04:58 +07:00
// prevent unaligned access on Highspeed port on USB_SRAM
uint16_t TU_RESERVED ;
2018-12-05 08:51:11 +07:00
} xfer_dma_t ;
2021-04-24 12:19:13 +07:00
// Absolute max of endpoints pairs for all port
// - 11 13 15 51 54 has 5x2 endpoints
// - 55 usb0 (FS) has 5x2 endpoints, usb1 (HS) has 6x2 endpoints
# define MAX_EP_PAIRS 6
2018-12-05 08:51:11 +07:00
// NOTE data will be transferred as soon as dcd get request by dcd_pipe(_queue)_xfer using double buffering.
// current_td is used to keep track of number of remaining & xferred bytes of the current request.
typedef struct
{
// 256 byte aligned, 2 for double buffer (not used)
// Each cmd_sts can only transfer up to DMA_NBYTES_MAX bytes each
2021-04-24 11:30:14 +07:00
ep_cmd_sts_t ep [ 2 * MAX_EP_PAIRS ] [ 2 ] ;
xfer_dma_t dma [ 2 * MAX_EP_PAIRS ] ;
2021-04-24 12:19:13 +07:00
2019-06-06 10:39:37 +07:00
TU_ATTR_ALIGNED ( 64 ) uint8_t setup_packet [ 8 ] ;
2018-12-05 08:51:11 +07:00
} dcd_data_t ;
2021-04-24 11:30:14 +07:00
// EP list must be 256-byte aligned
2021-04-24 13:36:05 +07:00
// Some MCU controller may require this variable to be placed in specific SRAM region.
// For example: LPC55s69 port1 Highspeed must be USB_RAM (0x40100000)
// Use CFG_TUSB_MEM_SECTION to place it accordingly.
2021-04-24 11:30:14 +07:00
CFG_TUSB_MEM_SECTION TU_ATTR_ALIGNED ( 256 ) static dcd_data_t _dcd ;
2022-10-11 09:59:28 -04:00
CFG_TUSB_MEM_SECTION TU_ATTR_ALIGNED ( 256 ) static volatile uint8_t dummy [ 64 ] ; // TODO temp fix to prevent accidental overwrite due to ep[][].buffer_xx.offset being 0
2021-04-24 11:30:14 +07:00
2018-12-05 08:51:11 +07:00
//--------------------------------------------------------------------+
2021-04-24 11:30:14 +07:00
// Multiple Controllers
2018-12-05 08:51:11 +07:00
//--------------------------------------------------------------------+
2021-04-24 11:30:14 +07:00
typedef struct
{
2021-04-24 12:19:13 +07:00
dcd_registers_t * regs ; // registers
const tusb_speed_t max_speed ; // max link speed
const IRQn_Type irqnum ; // IRQ number
const uint8_t ep_pairs ; // Max bi-directional Endpoints
2021-04-24 11:30:14 +07:00
} dcd_controller_t ;
# ifdef INCLUDE_FSL_DEVICE_REGISTERS
2021-04-26 17:14:20 +07:00
static const dcd_controller_t _dcd_controller [ ] =
{
{ . regs = ( dcd_registers_t * ) USB0_BASE , . max_speed = TUSB_SPEED_FULL , . irqnum = USB0_IRQn , . ep_pairs = FSL_FEATURE_USB_EP_NUM } ,
# if defined(FSL_FEATURE_SOC_USBHSD_COUNT) && FSL_FEATURE_SOC_USBHSD_COUNT
{ . regs = ( dcd_registers_t * ) USBHSD_BASE , . max_speed = TUSB_SPEED_HIGH , . irqnum = USB1_IRQn , . ep_pairs = FSL_FEATURE_USBHSD_EP_NUM }
# endif
} ;
2021-04-24 11:30:14 +07:00
# else
2021-04-26 17:14:20 +07:00
static const dcd_controller_t _dcd_controller [ ] =
{
{ . regs = ( dcd_registers_t * ) LPC_USB0_BASE , . max_speed = TUSB_SPEED_FULL , . irqnum = USB0_IRQn , . ep_pairs = 5 } ,
} ;
2021-04-24 11:30:14 +07:00
# endif
//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
2018-12-05 08:51:11 +07:00
static inline uint16_t get_buf_offset ( void const * buffer )
{
uint32_t addr = ( uint32_t ) buffer ;
TU_ASSERT ( ( addr & 0x3f ) = = 0 , 0 ) ;
return ( ( addr > > 6 ) & 0xFFFFUL ) ;
}
2021-04-24 13:36:05 +07:00
static inline uint8_t ep_addr2id ( uint8_t ep_addr )
2018-12-05 08:51:11 +07:00
{
2021-04-24 13:36:05 +07:00
return 2 * ( ep_addr & 0x0F ) + ( ( ep_addr & TUSB_DIR_IN_MASK ) ? 1 : 0 ) ;
2018-12-05 08:51:11 +07:00
}
//--------------------------------------------------------------------+
// CONTROLLER API
//--------------------------------------------------------------------+
2022-10-11 09:59:28 -04:00
static void prepare_setup_packet ( uint8_t rhport )
{
if ( _dcd_controller [ rhport ] . max_speed = = TUSB_SPEED_FULL )
{
_dcd . ep [ 0 ] [ 1 ] . buffer_fs . offset = get_buf_offset ( _dcd . setup_packet ) ;
} else
{
_dcd . ep [ 0 ] [ 1 ] . buffer_hs . offset = get_buf_offset ( _dcd . setup_packet ) ;
}
}
static void edpt_reset ( uint8_t rhport , uint8_t ep_id )
{
2022-10-12 11:16:35 -04:00
const uint32_t offset = get_buf_offset ( ( void * ) ( uint32_t ) dummy ) ;
2022-10-11 09:59:28 -04:00
tu_memclr ( & _dcd . ep [ ep_id ] , sizeof ( _dcd . ep [ ep_id ] ) ) ;
if ( _dcd_controller [ rhport ] . max_speed = = TUSB_SPEED_FULL )
{
_dcd . ep [ ep_id ] [ 0 ] . buffer_fs . offset = _dcd . ep [ ep_id ] [ 1 ] . buffer_fs . offset = offset ;
}
else
{
_dcd . ep [ ep_id ] [ 0 ] . buffer_hs . offset = _dcd . ep [ ep_id ] [ 1 ] . buffer_hs . offset = offset ;
}
}
static void edpt_reset_all ( uint8_t rhport )
{
for ( uint8_t ep_id = 0 ; ep_id < 2 * _dcd_controller [ rhport ] . ep_pairs ; + + ep_id )
{
edpt_reset ( rhport , ep_id ) ;
}
prepare_setup_packet ( rhport ) ;
}
2019-03-30 14:47:11 +07:00
void dcd_init ( uint8_t rhport )
2018-12-05 08:51:11 +07:00
{
2022-10-11 09:59:28 -04:00
edpt_reset_all ( rhport ) ;
2021-04-24 10:44:14 +07:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
2019-03-30 14:47:11 +07:00
2021-04-24 10:44:14 +07:00
dcd_reg - > EPLISTSTART = ( uint32_t ) _dcd . ep ;
2021-04-26 14:43:58 +07:00
dcd_reg - > DATABUFSTART = tu_align ( ( uint32_t ) & _dcd , TU_BIT ( 22 ) ) ; // 22-bit alignment
dcd_reg - > INTSTAT | = dcd_reg - > INTSTAT ; // clear all pending interrupt
2021-04-24 10:44:14 +07:00
dcd_reg - > INTEN = INT_DEVICE_STATUS_MASK ;
dcd_reg - > DEVCMDSTAT | = CMDSTAT_DEVICE_ENABLE_MASK | CMDSTAT_DEVICE_CONNECT_MASK |
2019-10-04 13:13:51 +07:00
CMDSTAT_RESET_CHANGE_MASK | CMDSTAT_CONNECT_CHANGE_MASK | CMDSTAT_SUSPEND_CHANGE_MASK ;
2019-03-30 14:47:11 +07:00
2021-04-24 11:30:14 +07:00
NVIC_ClearPendingIRQ ( _dcd_controller [ rhport ] . irqnum ) ;
2018-12-05 08:51:11 +07:00
}
2019-03-30 14:47:11 +07:00
void dcd_int_enable ( uint8_t rhport )
2018-12-05 08:51:11 +07:00
{
2021-04-24 11:30:14 +07:00
NVIC_EnableIRQ ( _dcd_controller [ rhport ] . irqnum ) ;
2018-12-05 08:51:11 +07:00
}
2019-03-30 14:47:11 +07:00
void dcd_int_disable ( uint8_t rhport )
2018-12-05 08:51:11 +07:00
{
2021-04-24 11:30:14 +07:00
NVIC_DisableIRQ ( _dcd_controller [ rhport ] . irqnum ) ;
2018-12-05 08:51:11 +07:00
}
void dcd_set_address ( uint8_t rhport , uint8_t dev_addr )
{
2021-04-24 10:44:14 +07:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
2019-03-13 23:14:48 +07:00
// Response with status first before changing device address
dcd_edpt_xfer ( rhport , tu_edpt_addr ( 0 , TUSB_DIR_IN ) , NULL , 0 ) ;
2018-12-05 08:51:11 +07:00
2021-04-24 10:44:14 +07:00
dcd_reg - > DEVCMDSTAT & = ~ CMDSTAT_DEVICE_ADDR_MASK ;
dcd_reg - > DEVCMDSTAT | = dev_addr ;
2018-12-05 08:51:11 +07:00
}
2019-03-30 14:47:11 +07:00
void dcd_remote_wakeup ( uint8_t rhport )
{
( void ) rhport ;
2018-12-05 08:51:11 +07:00
}
2020-04-16 16:05:57 +07:00
void dcd_connect ( uint8_t rhport )
{
2021-04-24 10:44:14 +07:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
dcd_reg - > DEVCMDSTAT | = CMDSTAT_DEVICE_CONNECT_MASK ;
2020-04-16 16:05:57 +07:00
}
void dcd_disconnect ( uint8_t rhport )
{
2021-04-24 10:44:14 +07:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
dcd_reg - > DEVCMDSTAT & = ~ CMDSTAT_DEVICE_CONNECT_MASK ;
2020-04-16 16:05:57 +07:00
}
2022-03-07 23:05:05 +07:00
void dcd_sof_enable ( uint8_t rhport , bool en )
{
( void ) rhport ;
( void ) en ;
// TODO implement later
}
2018-12-05 08:51:11 +07:00
//--------------------------------------------------------------------+
// DCD Endpoint Port
//--------------------------------------------------------------------+
void dcd_edpt_stall ( uint8_t rhport , uint8_t ep_addr )
{
2018-12-07 14:49:55 +07:00
( void ) rhport ;
2019-03-13 23:14:48 +07:00
// TODO cannot able to STALL Control OUT endpoint !!!!! FIXME try some walk-around
uint8_t const ep_id = ep_addr2id ( ep_addr ) ;
_dcd . ep [ ep_id ] [ 0 ] . stall = 1 ;
2018-12-05 08:51:11 +07:00
}
2018-12-12 11:51:31 +07:00
void dcd_edpt_clear_stall ( uint8_t rhport , uint8_t ep_addr )
2018-12-05 08:51:11 +07:00
{
2018-12-07 14:49:55 +07:00
( void ) rhport ;
2018-12-12 11:51:31 +07:00
uint8_t const ep_id = ep_addr2id ( ep_addr ) ;
2018-12-05 08:51:11 +07:00
_dcd . ep [ ep_id ] [ 0 ] . stall = 0 ;
_dcd . ep [ ep_id ] [ 0 ] . toggle_reset = 1 ;
_dcd . ep [ ep_id ] [ 0 ] . toggle_mode = 0 ;
}
bool dcd_edpt_open ( uint8_t rhport , tusb_desc_endpoint_t const * p_endpoint_desc )
{
//------------- Prepare Queue Head -------------//
uint8_t ep_id = ep_addr2id ( p_endpoint_desc - > bEndpointAddress ) ;
// Check if endpoint is available
TU_ASSERT ( _dcd . ep [ ep_id ] [ 0 ] . disable & & _dcd . ep [ ep_id ] [ 1 ] . disable ) ;
2022-10-11 09:59:28 -04:00
edpt_reset ( rhport , ep_id ) ;
2018-12-05 08:51:11 +07:00
_dcd . ep [ ep_id ] [ 0 ] . is_iso = ( p_endpoint_desc - > bmAttributes . xfer = = TUSB_XFER_ISOCHRONOUS ) ;
// Enable EP interrupt
2021-04-24 10:44:14 +07:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
dcd_reg - > INTEN | = TU_BIT ( ep_id ) ;
2018-12-05 08:51:11 +07:00
return true ;
}
2021-08-26 17:02:24 +07:00
void dcd_edpt_close_all ( uint8_t rhport )
{
2022-10-11 09:59:28 -04:00
for ( uint8_t ep_id = 0 ; ep_id < 2 * _dcd_controller [ rhport ] . ep_pairs ; + + ep_id )
{
_dcd . ep [ ep_id ] [ 0 ] . active = _dcd . ep [ ep_id ] [ 0 ] . active = 0 ; // TODO proper way is to EPSKIP then wait ep[][].active then write ep[][].disable (see table 778 in LPC55S69 Use Manual)
_dcd . ep [ ep_id ] [ 0 ] . disable = _dcd . ep [ ep_id ] [ 1 ] . disable = 1 ;
}
2021-08-26 17:02:24 +07:00
}
2022-10-11 09:59:28 -04:00
void dcd_edpt_close ( uint8_t rhport , uint8_t ep_addr )
2021-04-26 14:43:58 +07:00
{
2022-10-13 11:48:30 -04:00
( void ) rhport ;
2022-10-11 09:59:28 -04:00
uint8_t ep_id = ep_addr2id ( ep_addr ) ;
_dcd . ep [ ep_id ] [ 0 ] . active = _dcd . ep [ ep_id ] [ 0 ] . active = 0 ; // TODO proper way is to EPSKIP then wait ep[][].active then write ep[][].disable (see table 778 in LPC55S69 Use Manual)
_dcd . ep [ ep_id ] [ 0 ] . disable = _dcd . ep [ ep_id ] [ 1 ] . disable = 1 ;
2021-04-26 14:43:58 +07:00
}
static void prepare_ep_xfer ( uint8_t rhport , uint8_t ep_id , uint16_t buf_offset , uint16_t total_bytes )
2018-12-05 08:51:11 +07:00
{
2021-04-26 20:22:40 +07:00
uint16_t nbytes ;
2018-12-05 08:51:11 +07:00
2021-04-26 14:43:58 +07:00
if ( _dcd_controller [ rhport ] . max_speed = = TUSB_SPEED_FULL )
{
2022-10-11 09:59:28 -04:00
nbytes = tu_min16 ( total_bytes , _dcd . ep [ ep_id ] [ 0 ] . is_iso ? NBYTES_CBI_FS_ISO_MAX : NBYTES_CBI_FS_MAX ) ;
2021-04-26 14:43:58 +07:00
_dcd . ep [ ep_id ] [ 0 ] . buffer_fs . offset = buf_offset ;
2021-04-26 20:22:40 +07:00
_dcd . ep [ ep_id ] [ 0 ] . buffer_fs . nbytes = nbytes ;
2021-04-26 14:43:58 +07:00
} else
{
2022-10-11 09:59:28 -04:00
nbytes = tu_min16 ( total_bytes , NBYTES_CBI_HS_MAX ) ;
2021-04-26 14:43:58 +07:00
_dcd . ep [ ep_id ] [ 0 ] . buffer_hs . offset = buf_offset ;
2021-04-26 20:22:40 +07:00
_dcd . ep [ ep_id ] [ 0 ] . buffer_hs . nbytes = nbytes ;
2021-04-26 14:43:58 +07:00
}
2021-04-26 20:22:40 +07:00
_dcd . dma [ ep_id ] . nbytes = nbytes ;
2021-04-26 14:43:58 +07:00
_dcd . ep [ ep_id ] [ 0 ] . active = 1 ;
2018-12-05 08:51:11 +07:00
}
bool dcd_edpt_xfer ( uint8_t rhport , uint8_t ep_addr , uint8_t * buffer , uint16_t total_bytes )
{
uint8_t const ep_id = ep_addr2id ( ep_addr ) ;
2021-04-24 15:04:58 +07:00
tu_memclr ( & _dcd . dma [ ep_id ] , sizeof ( xfer_dma_t ) ) ;
2018-12-05 08:51:11 +07:00
_dcd . dma [ ep_id ] . total_bytes = total_bytes ;
2022-10-12 11:16:35 -04:00
prepare_ep_xfer ( rhport , ep_id , buffer ? get_buf_offset ( buffer ) : get_buf_offset ( ( void * ) ( uint32_t ) dummy ) , total_bytes ) ;
2018-12-05 08:51:11 +07:00
2019-10-04 13:13:51 +07:00
return true ;
2018-12-05 08:51:11 +07:00
}
//--------------------------------------------------------------------+
// IRQ
//--------------------------------------------------------------------+
2021-04-24 10:44:14 +07:00
static void bus_reset ( uint8_t rhport )
2018-12-05 08:51:11 +07:00
{
tu_memclr ( & _dcd , sizeof ( dcd_data_t ) ) ;
2022-10-11 09:59:28 -04:00
edpt_reset_all ( rhport ) ;
2018-12-05 08:51:11 +07:00
// disable all non-control endpoints on bus reset
2021-04-24 11:30:14 +07:00
for ( uint8_t ep_id = 2 ; ep_id < 2 * MAX_EP_PAIRS ; ep_id + + )
2018-12-05 08:51:11 +07:00
{
_dcd . ep [ ep_id ] [ 0 ] . disable = _dcd . ep [ ep_id ] [ 1 ] . disable = 1 ;
}
2021-04-24 10:44:14 +07:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
dcd_reg - > EPINUSE = 0 ;
dcd_reg - > EPBUFCFG = 0 ;
dcd_reg - > EPSKIP = 0xFFFFFFFF ;
2018-12-05 08:51:11 +07:00
2021-04-24 10:44:14 +07:00
dcd_reg - > INTSTAT = dcd_reg - > INTSTAT ; // clear all pending interrupt
dcd_reg - > DEVCMDSTAT | = CMDSTAT_SETUP_RECEIVED_MASK ; // clear setup received interrupt
dcd_reg - > INTEN = INT_DEVICE_STATUS_MASK | TU_BIT ( 0 ) | TU_BIT ( 1 ) ; // enable device status & control endpoints
2018-12-05 08:51:11 +07:00
}
2021-04-24 11:30:14 +07:00
static void process_xfer_isr ( uint8_t rhport , uint32_t int_status )
2018-12-05 08:51:11 +07:00
{
2021-04-24 11:30:14 +07:00
uint8_t const max_ep = 2 * _dcd_controller [ rhport ] . ep_pairs ;
for ( uint8_t ep_id = 0 ; ep_id < max_ep ; ep_id + + )
2018-12-05 08:51:11 +07:00
{
2019-05-14 12:54:29 +07:00
if ( tu_bit_test ( int_status , ep_id ) )
2018-12-05 08:51:11 +07:00
{
ep_cmd_sts_t * ep_cs = & _dcd . ep [ ep_id ] [ 0 ] ;
xfer_dma_t * xfer_dma = & _dcd . dma [ ep_id ] ;
2021-04-26 14:43:58 +07:00
if ( ep_id = = 0 | | ep_id = = 1 )
{
// For control endpoint, we need to manually clear Active bit
ep_cs - > active = 0 ;
}
uint16_t buf_offset ;
uint16_t buf_nbytes ;
if ( _dcd_controller [ rhport ] . max_speed = = TUSB_SPEED_FULL )
{
buf_offset = ep_cs - > buffer_fs . offset ;
buf_nbytes = ep_cs - > buffer_fs . nbytes ;
} else
{
buf_offset = ep_cs - > buffer_hs . offset ;
buf_nbytes = ep_cs - > buffer_hs . nbytes ;
}
xfer_dma - > xferred_bytes + = xfer_dma - > nbytes - buf_nbytes ;
2018-12-05 08:51:11 +07:00
2021-04-26 14:43:58 +07:00
if ( ( buf_nbytes = = 0 ) & & ( xfer_dma - > total_bytes > xfer_dma - > xferred_bytes ) )
2018-12-05 08:51:11 +07:00
{
// There is more data to transfer
// buff_offset has been already increased by hw to correct value for next transfer
2021-04-26 14:43:58 +07:00
prepare_ep_xfer ( rhport , ep_id , buf_offset , xfer_dma - > total_bytes - xfer_dma - > xferred_bytes ) ;
2018-12-05 08:51:11 +07:00
}
else
{
2021-04-26 14:43:58 +07:00
// for detecting ZLP
2018-12-05 08:51:11 +07:00
xfer_dma - > total_bytes = xfer_dma - > xferred_bytes ;
2021-04-24 11:30:14 +07:00
uint8_t const ep_addr = tu_edpt_addr ( ep_id / 2 , ep_id & 0x01 ) ;
2018-12-05 08:51:11 +07:00
// TODO no way determine if the transfer is failed or not
2021-04-24 11:30:14 +07:00
dcd_event_xfer_complete ( rhport , ep_addr , xfer_dma - > xferred_bytes , XFER_RESULT_SUCCESS , true ) ;
2018-12-05 08:51:11 +07:00
}
}
}
}
2020-04-17 12:27:53 +07:00
void dcd_int_handler ( uint8_t rhport )
2018-12-05 08:51:11 +07:00
{
2021-04-24 10:44:14 +07:00
dcd_registers_t * dcd_reg = _dcd_controller [ rhport ] . regs ;
2020-04-06 18:00:37 +07:00
2021-04-24 10:44:14 +07:00
uint32_t const cmd_stat = dcd_reg - > DEVCMDSTAT ;
2018-12-05 08:51:11 +07:00
2021-04-24 10:44:14 +07:00
uint32_t int_status = dcd_reg - > INTSTAT & dcd_reg - > INTEN ;
dcd_reg - > INTSTAT = int_status ; // Acknowledge handled interrupt
2018-12-05 08:51:11 +07:00
if ( int_status = = 0 ) return ;
//------------- Device Status -------------//
if ( int_status & INT_DEVICE_STATUS_MASK )
{
2021-04-24 10:44:14 +07:00
dcd_reg - > DEVCMDSTAT | = CMDSTAT_RESET_CHANGE_MASK | CMDSTAT_CONNECT_CHANGE_MASK | CMDSTAT_SUSPEND_CHANGE_MASK ;
2021-04-26 14:43:58 +07:00
2019-10-04 13:13:51 +07:00
if ( cmd_stat & CMDSTAT_RESET_CHANGE_MASK ) // bus reset
2018-12-05 08:51:11 +07:00
{
2021-04-24 10:44:14 +07:00
bus_reset ( rhport ) ;
2021-04-24 12:19:13 +07:00
tusb_speed_t speed = TUSB_SPEED_FULL ;
if ( _dcd_controller [ rhport ] . max_speed = = TUSB_SPEED_HIGH )
{
// 0 : reserved, 1 : full, 2 : high, 3: super
if ( 2 = = ( ( cmd_stat > > CMDSTAT_SPEED_SHIFT ) & 0x3UL ) )
{
speed = TUSB_SPEED_HIGH ;
}
}
dcd_event_bus_reset ( rhport , speed , true ) ;
2018-12-05 08:51:11 +07:00
}
2019-10-04 13:13:51 +07:00
if ( cmd_stat & CMDSTAT_CONNECT_CHANGE_MASK )
2018-12-05 08:51:11 +07:00
{
// device disconnect
2019-10-04 13:13:51 +07:00
if ( cmd_stat & CMDSTAT_DEVICE_ADDR_MASK )
2018-12-05 08:51:11 +07:00
{
// debouncing as this can be set when device is powering
2021-04-24 11:30:14 +07:00
dcd_event_bus_signal ( rhport , DCD_EVENT_UNPLUGGED , true ) ;
2018-12-05 08:51:11 +07:00
}
}
2019-10-04 13:13:51 +07:00
if ( cmd_stat & CMDSTAT_SUSPEND_CHANGE_MASK )
2018-12-05 08:51:11 +07:00
{
2022-10-11 09:59:28 -04:00
// suspend signal, bus idle for more than 3ms
// Note: Host may delay more than 3 ms before and/or after bus reset before doing enumeration.
if ( cmd_stat & CMDSTAT_DEVICE_ADDR_MASK )
{
dcd_event_bus_signal ( rhport , ( cmd_stat & CMDSTAT_DEVICE_SUSPEND_MASK ) ? DCD_EVENT_SUSPEND : DCD_EVENT_RESUME , true ) ;
2018-12-05 08:51:11 +07:00
}
}
}
// Setup Receive
2019-10-04 13:13:51 +07:00
if ( tu_bit_test ( int_status , 0 ) & & ( cmd_stat & CMDSTAT_SETUP_RECEIVED_MASK ) )
2018-12-05 08:51:11 +07:00
{
// Follow UM flowchart to clear Active & Stall on both Control IN/OUT endpoints
_dcd . ep [ 0 ] [ 0 ] . active = _dcd . ep [ 1 ] [ 0 ] . active = 0 ;
_dcd . ep [ 0 ] [ 0 ] . stall = _dcd . ep [ 1 ] [ 0 ] . stall = 0 ;
2021-04-24 10:44:14 +07:00
dcd_reg - > DEVCMDSTAT | = CMDSTAT_SETUP_RECEIVED_MASK ;
2018-12-05 08:51:11 +07:00
2021-04-24 11:30:14 +07:00
dcd_event_setup_received ( rhport , _dcd . setup_packet , true ) ;
2018-12-05 08:51:11 +07:00
// keep waiting for next setup
2021-04-26 14:43:58 +07:00
prepare_setup_packet ( rhport ) ;
2018-12-05 08:51:11 +07:00
// clear bit0
2019-05-14 12:54:29 +07:00
int_status = tu_bit_clear ( int_status , 0 ) ;
2018-12-05 08:51:11 +07:00
}
// Endpoint transfer complete interrupt
2021-04-24 11:30:14 +07:00
process_xfer_isr ( rhport , int_status ) ;
2018-12-05 08:51:11 +07:00
}
# endif