[NUC472/M453] Support USB device

pull/3365/head
ccli8 2016-11-30 09:19:53 +08:00
parent 1fd2402a76
commit 7f4881fbb2
10 changed files with 1411 additions and 54 deletions

View File

@ -53,6 +53,10 @@ typedef enum {
#include "USBEndpoints_Maxim.h"
#elif defined(TARGET_EFM32GG_STK3700) || defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32HG_STK3400)
#include "USBEndpoints_EFM32.h"
#elif defined(TARGET_NUMAKER_PFM_NUC472)
#include "USBEndpoints_NUC472.h"
#elif defined(TARGET_NUMAKER_PFM_M453)
#include "USBEndpoints_M453.h"
#else
#error "Unknown target type"
#endif

View File

@ -0,0 +1,65 @@
#define NU_MAX_EPX_BUFSIZE 4096
#define NU_EP2EPL(ep) ((ep) >> 1)
#define NU_EP2EPH(ep) (((ep) >> 1) + 1)
#define NU_EPL2EPH(ep) ((ep) + 1)
#define NU_EPH2EPL(ep) ((ep) - 1)
#define NU_EP_DIR_Pos 0
#define NU_EP_DIR_Msk (1 << NU_EP_DIR_Pos)
#define NU_EP_DIR_OUT 0
#define NU_EP_DIR_IN 1
#define NU_EP_TYPE(ep) (((ep) & NU_EP_TYPE_Msk) >> NU_EP_TYPE_Pos)
#define NU_EP_NUM(ep) (((ep) & NU_EP_NUM_Msk) >> NU_EP_NUM_Pos)
#define NU_EP_DIR(ep) (((ep) & NU_EP_DIR_Msk) >> NU_EP_DIR_Pos)
#define NU_EP_NUM_DIR(ep) ((NU_EP_NUM(ep) << 1) | NU_EP_DIR(ep))
#define NUMBER_OF_PHYSICAL_ENDPOINTS 8
#define EP0OUT (0)
#define EP0IN (1)
#define EP1OUT (2)
#define EP1IN (3)
#define EP2OUT (4)
#define EP2IN (5)
#define EP3OUT (6)
#define EP3IN (7)
#define EP4OUT (8)
#define EP4IN (9)
#define EP5OUT (10)
#define EP5IN (11)
#define EP6OUT (12)
#define EP6IN (13)
/* Maximum Packet sizes */
#define MAX_PACKET_SIZE_EP0 64
#define MAX_PACKET_SIZE_EP1 64
#define MAX_PACKET_SIZE_EP2 64
#define MAX_PACKET_SIZE_EP3 0x60
#define MAX_PACKET_SIZE_EP4 64
#define MAX_PACKET_SIZE_EP5 64
#define MAX_PACKET_SIZE_EP6 64
#define MAX_PACKET_SIZE_EP7 64
/* Generic endpoints - intended to be portable accross devices */
/* and be suitable for simple USB devices. */
/* Bulk endpoints */
#define EPBULK_OUT EP5OUT
#define EPBULK_IN EP6IN
#define EPBULK_OUT_callback EP5_OUT_callback
#define EPBULK_IN_callback EP6_IN_callback
/* Interrupt endpoints */
#define EPINT_OUT EP1OUT
#define EPINT_IN EP2IN
#define EPINT_OUT_callback EP1_OUT_callback
#define EPINT_IN_callback EP2_IN_callback
/* Isochronous endpoints */
#define EPISO_OUT EP3OUT
#define EPISO_IN EP4IN
#define EPISO_OUT_callback EP3_OUT_callback
#define EPISO_IN_callback EP4_IN_callback
#define MAX_PACKET_SIZE_EPBULK 64
#define MAX_PACKET_SIZE_EPINT 64
#define MAX_PACKET_SIZE_EPISO 1023

View File

@ -0,0 +1,75 @@
#define NU_MAX_EPX_BUFSIZE 4096
#define NU_EP2EPL(ep) ((ep) >> 1)
#define NU_EP2EPH(ep) (((ep) >> 1) - 1)
#define NU_EPX2EP(ep) ((ep == CEP) ? EP0OUT : ((ep) - EPA + EP1OUT))
#define NU_EPL2EPH(ep) ((ep) - 1)
#define NU_EPH2EPL(ep) ((ep) + 1)
#define NU_EP_DIR_Pos 0
#define NU_EP_DIR_Msk (1 << NU_EP_DIR_Pos)
#define NU_EP_DIR_OUT 0
#define NU_EP_DIR_IN 1
#define NU_EP_TYPE(ep) (((ep) & NU_EP_TYPE_Msk) >> NU_EP_TYPE_Pos)
#define NU_EP_NUM(ep) (((ep) & NU_EP_NUM_Msk) >> NU_EP_NUM_Pos)
#define NU_EP_DIR(ep) (((ep) & NU_EP_DIR_Msk) >> NU_EP_DIR_Pos)
#define NU_EP_NUM_DIR(ep) ((NU_EP_NUM(ep) << 1) | NU_EP_DIR(ep))
#define NUMBER_OF_PHYSICAL_ENDPOINTS 12
#define EP0OUT (0)
#define EP0IN (1)
#define EP1OUT (2)
#define EP1IN (3)
#define EP2OUT (4)
#define EP2IN (5)
#define EP3OUT (6)
#define EP3IN (7)
#define EP4OUT (8)
#define EP4IN (9)
#define EP5OUT (10)
#define EP5IN (11)
#define EP6OUT (12)
#define EP6IN (13)
/* Maximum Packet sizes */
#define MAX_PACKET_SIZE_EP0 64
#define MAX_PACKET_SIZE_EP1 64
#define MAX_PACKET_SIZE_EP2 64
#define MAX_PACKET_SIZE_EP3 0x60
#define MAX_PACKET_SIZE_EP4 64
#define MAX_PACKET_SIZE_EP5 64
#define MAX_PACKET_SIZE_EP6 64
#define MAX_PACKET_SIZE_EP7 64
#define MAX_PACKET_SIZE_EP8 64
#define MAX_PACKET_SIZE_EP9 64
#define MAX_PACKET_SIZE_EP10 64
#define MAX_PACKET_SIZE_EP11 64
/* Generic endpoints - intended to be portable accross devices */
/* and be suitable for simple USB devices. */
/* Bulk endpoints */
#define EPBULK_OUT EP5OUT
#define EPBULK_IN EP6IN
#define EPBULK_OUT_callback EP5_OUT_callback
#define EPBULK_IN_callback EP6_IN_callback
/* Interrupt endpoints */
#define EPINT_OUT EP1OUT
#define EPINT_IN EP2IN
#define EPINT_OUT_callback EP1_OUT_callback
#define EPINT_IN_callback EP2_IN_callback
/* Isochronous endpoints */
#define EPISO_OUT EP3OUT
#define EPISO_IN EP4IN
#define EPISO_OUT_callback EP3_OUT_callback
#define EPISO_IN_callback EP4_IN_callback
#define MAX_PACKET_SIZE_EPBULK 64
#define MAX_PACKET_SIZE_EPINT 64
#define MAX_PACKET_SIZE_EPISO 1023
#define USBD_GET_EP_MAX_PAYLOAD(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EPAMPS + (uint32_t)((ep)*0x28))))
#define USBD_GET_EP_DATA_COUNT(ep) ((*((__IO uint32_t *) ((uint32_t)&USBD->EPADATCNT + (uint32_t)((ep)*0x28)))) & 0xFFFFF)
#define USBD_SET_EP_SHORT_PACKET(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EPARSPCTL + (uint32_t)((ep)*0x28))) = (*((__IO uint32_t *) ((uint32_t)&USBD->EPARSPCTL + (uint32_t)((ep)*0x28)))) & 0x10 | 0x40)
#define USBD_GET_EP_INT_EN(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EPAINTEN + (uint32_t)((ep)*0x28))))

View File

@ -68,6 +68,23 @@ protected:
virtual void suspendStateChanged(unsigned int suspended){};
virtual void SOF(int frameNumber){};
#if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M453)
// NUC472/M453 USB doesn't support configuration of the same EP number for IN/OUT simultaneously.
virtual bool EP1_OUT_callback(){return false;};
virtual bool EP2_IN_callback(){return false;};
virtual bool EP3_OUT_callback(){return false;};
virtual bool EP4_IN_callback(){return false;};
virtual bool EP5_OUT_callback(){return false;};
virtual bool EP6_IN_callback(){return false;};
#if ! (defined(TARGET_NUMAKER_PFM_M453))
virtual bool EP7_OUT_callback(){return false;};
virtual bool EP8_IN_callback(){return false;};
virtual bool EP9_OUT_callback(){return false;};
virtual bool EP10_IN_callback(){return false;};
virtual bool EP11_OUT_callback(){return false;};
virtual bool EP12_IN_callback(){return false;};
#endif
#else
virtual bool EP1_OUT_callback(){return false;};
virtual bool EP1_IN_callback(){return false;};
virtual bool EP2_OUT_callback(){return false;};
@ -102,6 +119,7 @@ protected:
virtual bool EP15_IN_callback(){return false;};
#endif
#endif
#endif
private:
void usbisr(void);
@ -110,10 +128,12 @@ private:
#if defined(TARGET_LPC11UXX) || defined(TARGET_LPC11U6X) || defined(TARGET_LPC1347) || defined(TARGET_LPC1549)
bool (USBHAL::*epCallback[10 - 2])(void);
#elif defined(TARGET_STM32F4) && !defined(USB_STM_HAL)
#elif (defined(TARGET_STM32F4) && !defined(USB_STM_HAL)) || defined(TARGET_NUMAKER_PFM_M453)
bool (USBHAL::*epCallback[8 - 2])(void);
#elif defined(TARGET_STM32F4) || defined(TARGET_STM32F3) || defined (TARGET_STM32F2)|| defined(TARGET_STM32L4) || defined(TARGET_STM32F7)
PCD_HandleTypeDef hpcd;
#elif defined(TARGET_NUMAKER_PFM_NUC472)
bool (USBHAL::*epCallback[14 - 2])(void);
#else
bool (USBHAL::*epCallback[32 - 2])(void);
#endif

View File

@ -0,0 +1,464 @@
/* mbed Microcontroller Library
* Copyright (c) 2015-2016 Nuvoton
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if defined(TARGET_NUMAKER_PFM_M453)
#include "USBHAL.h"
#include "M451Series.h"
#include "pinmap.h"
/**
* EP: mbed USBD defined endpoint, e.g. EP0OUT/IN, EP1OUT/IN, EP2OUT/IN.
* EPX: BSP defined endpoint, e.g. CEP, EPA, EPB, EPC.
*/
USBHAL * USBHAL::instance;
/* Global variables for Control Pipe */
extern uint8_t g_usbd_SetupPacket[]; /*!< Setup packet buffer */
static volatile uint32_t s_ep_compl = 0;
static volatile uint32_t s_ep_buf_ind = 8;
static volatile uint8_t s_usb_addr = 0;
static volatile uint8_t s_ep_data_bit[NUMBER_OF_PHYSICAL_ENDPOINTS] = {1};
static volatile uint8_t s_ep_mxp[NUMBER_OF_PHYSICAL_ENDPOINTS] = {0};
extern volatile uint8_t *g_usbd_CtrlInPointer;
extern volatile uint32_t g_usbd_CtrlInSize;
extern volatile uint8_t *g_usbd_CtrlOutPointer;
extern volatile uint32_t g_usbd_CtrlOutSize;
extern volatile uint32_t g_usbd_CtrlOutSizeLimit;
extern volatile uint32_t g_usbd_UsbConfig;
extern volatile uint32_t g_usbd_CtrlMaxPktSize;
extern volatile uint32_t g_usbd_UsbAltInterface;
volatile uint32_t g_usbd_CepTransferLen = 0;
volatile uint32_t frame_cnt = 0;
USBHAL::USBHAL(void)
{
SYS_UnlockReg();
s_ep_buf_ind = 8;
memset(epCallback, 0x00, sizeof (epCallback));
epCallback[0] = &USBHAL::EP1_OUT_callback;
epCallback[1] = &USBHAL::EP2_IN_callback;
epCallback[2] = &USBHAL::EP3_OUT_callback;
epCallback[3] = &USBHAL::EP4_IN_callback;
epCallback[4] = &USBHAL::EP5_OUT_callback;
epCallback[5] = &USBHAL::EP6_IN_callback;
instance = this;
/* Enable USBD module clock */
CLK_EnableModuleClock(USBD_MODULE);
CLK_SetModuleClock(USBD_MODULE, 0, CLK_CLKDIV0_USB(3));
/* Enable USB LDO33 */
SYS->USBPHY = SYS_USBPHY_LDO33EN_Msk;
/* Initial USB engine */
USBD->ATTR = 0x7D0;
/* Set SE0 (disconnect) */
USBD_SET_SE0();
//NVIC_SetVector(OTG_FS_IRQn, (uint32_t) &_usbisr);
NVIC_SetVector(USBD_IRQn, (uint32_t) &_usbisr);
NVIC_EnableIRQ(USBD_IRQn);
}
USBHAL::~USBHAL(void)
{
NVIC_DisableIRQ(USBD_IRQn);
USBD_SET_SE0();
USBD_DISABLE_PHY();
}
void USBHAL::connect(void)
{
USBD->STBUFSEG = 0;
frame_cnt = 0;
/* EP0 ==> control IN endpoint, address 0 */
USBD_CONFIG_EP(EP0, USBD_CFG_CSTALL | USBD_CFG_EPMODE_IN | 0);
/* Buffer range for EP0 */
USBD_SET_EP_BUF_ADDR(EP0, s_ep_buf_ind);
/* EP1 ==> control OUT endpoint, address 0 */
USBD_CONFIG_EP(EP1, USBD_CFG_CSTALL | USBD_CFG_EPMODE_OUT | 0);
/* Buffer range for EP1 */
USBD_SET_EP_BUF_ADDR(EP1, s_ep_buf_ind);
s_ep_buf_ind += MAX_PACKET_SIZE_EP0;
/* Disable software-disconnect function */
USBD_CLR_SE0();
/* Clear USB-related interrupts before enable interrupt */
USBD_CLR_INT_FLAG(USBD_INT_BUS | USBD_INT_USB | USBD_INT_FLDET | USBD_INT_WAKEUP);
/* Enable USB-related interrupts. */
USBD_ENABLE_INT(USBD_INT_BUS | USBD_INT_USB | USBD_INT_FLDET | USBD_INT_WAKEUP);
}
void USBHAL::disconnect(void)
{
/* Set SE0 (disconnect) */
USBD_SET_SE0();
}
void USBHAL::configureDevice(void)
{
/**
* In USBDevice.cpp > USBDevice::requestSetConfiguration, configureDevice() is called after realiseEndpoint() (in USBCallback_setConfiguration()).
* So we have the following USB buffer management policy:
* 1. Allocate for CEP on connect().
* 2. Allocate for EPX in realiseEndpoint().
* 3. Deallocate all except for CEP in unconfigureDevice().
*/
}
void USBHAL::unconfigureDevice(void)
{
s_ep_buf_ind = 8;
}
void USBHAL::setAddress(uint8_t address)
{
// NOTE: Delay address setting; otherwise, USB controller won't ack.
s_usb_addr = address;
}
void USBHAL::remoteWakeup(void)
{
#if 0
USBD->OPER |= USBD_OPER_RESUMEEN_Msk;
#endif
}
bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options)
{
uint32_t ep_type = 0;
uint32_t ep_hw_index = NU_EP2EPH(endpoint);
uint32_t ep_logic_index = NU_EP2EPL(endpoint);
uint32_t ep_dir = (NU_EP_DIR(endpoint) == NU_EP_DIR_IN) ? USBD_CFG_EPMODE_IN : USBD_CFG_EPMODE_OUT;
if(ep_logic_index == 3 || ep_logic_index == 4)
ep_type = USBD_CFG_TYPE_ISO;
USBD_CONFIG_EP(ep_hw_index, ep_dir | ep_type | ep_logic_index);
/* Buffer range */
USBD_SET_EP_BUF_ADDR(ep_hw_index, s_ep_buf_ind);
if(ep_dir == USBD_CFG_EPMODE_OUT)
USBD_SET_PAYLOAD_LEN(ep_hw_index, maxPacket);
s_ep_mxp[ep_logic_index] = maxPacket;
s_ep_buf_ind += maxPacket;
return true;
}
void USBHAL::EP0setup(uint8_t *buffer)
{
uint32_t sz;
endpointReadResult(EP0OUT, buffer, &sz);
}
void USBHAL::EP0read(void)
{
}
void USBHAL::EP0readStage(void)
{
// N/A
USBD_PrepareCtrlOut(0,0);
}
uint32_t USBHAL::EP0getReadResult(uint8_t *buffer)
{
uint32_t i;
uint8_t *buf = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP1));
uint32_t ceprxcnt = USBD_GET_PAYLOAD_LEN(EP1);
for (i = 0; i < ceprxcnt; i ++)
buffer[i] = buf[i];
USBD_SET_PAYLOAD_LEN(EP1, MAX_PACKET_SIZE_EP0);
return ceprxcnt;
}
void USBHAL::EP0write(uint8_t *buffer, uint32_t size)
{
if (buffer && size)
{
if(s_ep_data_bit[0] & 1)
USBD_SET_DATA1(EP0);
else
USBD_SET_DATA0(EP0);
s_ep_data_bit[0]++;
USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0), buffer, size);
USBD_SET_PAYLOAD_LEN(EP0, size);
if(size < MAX_PACKET_SIZE_EP0)
s_ep_data_bit[0] = 1;
}
else
{
if(g_usbd_SetupPacket[0] & 0x80) //Device to Host
{
// Status stage
// USBD_PrepareCtrlOut(0,0);
}else
{
USBD_SET_DATA1(EP0);
USBD_SET_PAYLOAD_LEN(EP0, 0);
}
}
}
void USBHAL::EP0getWriteResult(void)
{
// N/A
}
void USBHAL::EP0stall(void)
{
stallEndpoint(EP0OUT);
}
EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize)
{
return EP_PENDING;
}
EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) //spcheng
{
if(endpoint == EP0OUT)
{
USBD_MemCopy(g_usbd_SetupPacket, (uint8_t *)USBD_BUF_BASE, 8);
if (buffer) {
USBD_MemCopy(buffer, g_usbd_SetupPacket, 8);
}
USBD_SET_PAYLOAD_LEN(EP1, MAX_PACKET_SIZE_EP0);
}
else
{
uint32_t i;
uint8_t *buf = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(NU_EP2EPH(endpoint)));
uint32_t eprxcnt = USBD_GET_PAYLOAD_LEN(NU_EP2EPH(endpoint));
for (i = 0; i < eprxcnt; i ++)
buffer[i] = buf[i];
*bytesRead = eprxcnt;
USBD_SET_PAYLOAD_LEN(NU_EP2EPH(endpoint),s_ep_mxp[NU_EPH2EPL(NU_EP2EPL(endpoint))]);
}
return EP_COMPLETED;
}
uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer)
{
return 0;
}
EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size)
{
uint32_t ep_logic_index = NU_EP2EPL(endpoint);
if(ep_logic_index == 0)
return EP_INVALID;
else
{
uint8_t *buf;
uint32_t i=0;
uint32_t ep_hw_index = NU_EP2EPH(endpoint);
s_ep_compl |= (1 << ep_logic_index);
buf = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(ep_hw_index));
for(i=0;i<size;i++)
buf[i] = data[i];
/* Set transfer length and trigger IN transfer */
USBD_SET_PAYLOAD_LEN(ep_hw_index, size);
}
return EP_PENDING;
}
EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint)
{
if (!(s_ep_compl & (1 << NU_EP2EPL(endpoint))))
return EP_COMPLETED;
return EP_PENDING;
}
void USBHAL::stallEndpoint(uint8_t endpoint)
{
uint32_t ep_hw_index = NU_EP2EPH(endpoint);
if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS)
return;
USBD_SetStall(NU_EPH2EPL(ep_hw_index));
}
void USBHAL::unstallEndpoint(uint8_t endpoint)
{
uint32_t ep_hw_index = NU_EP2EPH(endpoint);
if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS)
return;
USBD_ClearStall(NU_EPH2EPL(ep_hw_index));
}
bool USBHAL::getEndpointStallState(uint8_t endpoint)
{
uint32_t ep_hw_index = NU_EP2EPH(endpoint);
if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS)
return false;
return USBD_GetStall(NU_EPH2EPL(ep_hw_index)) ? 1 : 0;
}
void USBHAL::_usbisr(void)
{
MBED_ASSERT(instance);
instance->usbisr();
}
void USBHAL::usbisr(void)
{
uint32_t u32IntSts = USBD_GET_INT_FLAG();
uint32_t u32State = USBD_GET_BUS_STATE();
//------------------------------------------------------------------
if(u32IntSts & USBD_INTSTS_VBDETIF_Msk)
{
// Floating detect
USBD_CLR_INT_FLAG(USBD_INTSTS_VBDETIF_Msk);
if(USBD_IS_ATTACHED())
{
/* USB Plug In */
USBD_ENABLE_USB();
}
else
{
/* USB Un-plug */
USBD_DISABLE_USB();
}
}
//------------------------------------------------------------------
if(u32IntSts & USBD_INTSTS_BUSIF_Msk)
{
/* Clear event flag */
USBD_CLR_INT_FLAG(USBD_INTSTS_BUSIF_Msk);
if(u32State & USBD_ATTR_USBRST_Msk)
{
/* Bus reset */
USBD_ENABLE_USB();
USBD_SwReset();
}
if(u32State & USBD_ATTR_SUSPEND_Msk)
{
/* Enable USB but disable PHY */
USBD_DISABLE_PHY();
}
if(u32State & USBD_ATTR_RESUME_Msk)
{
/* Enable USB and enable PHY */
USBD_ENABLE_USB();
}
}
if(u32IntSts & USBD_INTSTS_USBIF_Msk)
{
// USB event
if(u32IntSts & USBD_INTSTS_SETUP_Msk)
{
// Setup packet
/* Clear event flag */
USBD_CLR_INT_FLAG(USBD_INTSTS_SETUP_Msk);
/* Clear the data IN/OUT ready flag of control end-points */
USBD_STOP_TRANSACTION(EP0);
USBD_STOP_TRANSACTION(EP1);
EP0setupCallback();
}
// EP events
if(u32IntSts & USBD_INTSTS_EP0)
{
/* Clear event flag */
USBD_CLR_INT_FLAG(USBD_INTSTS_EP0);
// control IN
EP0in();
// In ACK for Set address
if((g_usbd_SetupPacket[0] == REQ_STANDARD) && (g_usbd_SetupPacket[1] == USBD_SET_ADDRESS))
{
if((USBD_GET_ADDR() != s_usb_addr) && (USBD_GET_ADDR() == 0))
{
USBD_SET_ADDR(s_usb_addr);
}
}
}
if(u32IntSts & USBD_INTSTS_EP1)
{
/* Clear event flag */
USBD_CLR_INT_FLAG(USBD_INTSTS_EP1);
// control OUT
EP0out();
}
uint32_t gintsts_epx = (u32IntSts >> 18) & 0x3F;
uint32_t ep_hw_index = 2;
while (gintsts_epx) {
if(gintsts_epx & 0x01)
{
uint32_t ep_status = (USBD_GET_EP_FLAG() >> (ep_hw_index * 3 + 8)) & 0x7;
/* Clear event flag */
USBD_CLR_INT_FLAG(1 << (ep_hw_index + 16));
if(ep_status == 0x02 || ep_status == 0x06 || (ep_status == 0x07 && NU_EPH2EPL(ep_hw_index) == 3)) //RX
{
if(ep_status == 0x07)
SOF(frame_cnt++);
if ((instance->*(epCallback[ep_hw_index-2]))())
{
}
USBD_SET_PAYLOAD_LEN(ep_hw_index,s_ep_mxp[NU_EPH2EPL(ep_hw_index)]);
}
else if(ep_status == 0x00 || ep_status == 0x07) //TX
{
s_ep_compl &= ~(1 << (NU_EPH2EPL(ep_hw_index)));
if ((instance->*(epCallback[ep_hw_index-2]))())
{
}
}
}
gintsts_epx = gintsts_epx >> 1;
ep_hw_index++;
}
}
}
#endif

View File

@ -0,0 +1,729 @@
/* mbed Microcontroller Library
* Copyright (c) 2015-2016 Nuvoton
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if defined(TARGET_NUMAKER_PFM_NUC472)
#include "USBHAL.h"
#include "NUC472_442.h"
#include "pinmap.h"
/**
* EP: mbed USBD defined endpoint, e.g. EP0OUT/IN, EP1OUT/IN, EP2OUT/IN.
* EPX: BSP defined endpoint, e.g. CEP, EPA, EPB, EPC.
*/
USBHAL * USBHAL::instance;
static volatile uint32_t s_ep_compl = 0;
static volatile uint32_t s_ep_buf_ind = 0;
static volatile uint8_t s_usb_addr = 0;
static volatile S_USBD_CMD_T s_setup;
static volatile uint16_t s_ctrlin_packetsize;
static uint8_t *g_usbd_CtrlInPointer = 0;
static uint32_t g_usbd_CtrlMaxPktSize = 64;
static uint32_t g_usbd_ShortPkt = 0;
static uint32_t gEpRead = 0;
static uint32_t gEpReadCnt = 0;
void USBD_CtrlInput(void)
{
int volatile i;
uint32_t volatile count;
// Process remained data
if(g_usbd_CtrlInSize >= g_usbd_CtrlMaxPktSize)
{
// Data size > MXPLD
for (i=0; i<(g_usbd_CtrlMaxPktSize >> 2); i++, g_usbd_CtrlInPointer+=4)
USBD->CEPDAT = *(uint32_t *)g_usbd_CtrlInPointer;
USBD_START_CEP_IN(g_usbd_CtrlMaxPktSize);
g_usbd_CtrlInSize -= g_usbd_CtrlMaxPktSize;
}
else
{
// Data size <= MXPLD
for (i=0; i<(g_usbd_CtrlInSize >> 2); i++, g_usbd_CtrlInPointer+=4)
USBD->CEPDAT = *(uint32_t *)g_usbd_CtrlInPointer;
count = g_usbd_CtrlInSize % 4;
for (i=0; i<count; i++)
USBD->CEPDAT_BYTE = *(uint8_t *)(g_usbd_CtrlInPointer + i);
USBD_START_CEP_IN(g_usbd_CtrlInSize);
g_usbd_CtrlInPointer = 0;
g_usbd_CtrlInSize = 0;
}
}
USBHAL::USBHAL(void)
{
SYS_UnlockReg();
s_ep_buf_ind = 0;
memset(epCallback, 0x00, sizeof (epCallback));
epCallback[0] = &USBHAL::EP1_OUT_callback;
epCallback[1] = &USBHAL::EP2_IN_callback;
epCallback[2] = &USBHAL::EP3_OUT_callback;
epCallback[3] = &USBHAL::EP4_IN_callback;
epCallback[4] = &USBHAL::EP5_OUT_callback;
epCallback[5] = &USBHAL::EP6_IN_callback;
epCallback[6] = &USBHAL::EP7_OUT_callback;
epCallback[7] = &USBHAL::EP8_IN_callback;
epCallback[8] = &USBHAL::EP9_OUT_callback;
epCallback[9] = &USBHAL::EP10_IN_callback;
epCallback[10] = &USBHAL::EP11_OUT_callback;
epCallback[11] = &USBHAL::EP12_IN_callback;
instance = this;
/* Enable USBD module clock */
CLK_EnableModuleClock(USBD_MODULE);
/* Enable USB PHY's LDO33. Run as USB device. */
SYS->USBPHY = SYS_USBPHY_USBROLE_OTG_V33_EN | SYS_USBPHY_USBROLE_STD_USBD;
/* Enable USB PHY and wait for it ready */
USBD_ENABLE_PHY();
while (1)
{
USBD->EPAMPS = 0x20;
if (USBD->EPAMPS == 0x20)
break;
}
/* Force to full-speed */
USBD->OPER = 0;//USBD_OPER_HISPDEN_Msk;
/* Set SE0 (disconnect) */
USBD_SET_SE0();
NVIC_SetVector(USBD_IRQn, (uint32_t) &_usbisr);
NVIC_EnableIRQ(USBD_IRQn);
}
USBHAL::~USBHAL(void)
{
NVIC_DisableIRQ(USBD_IRQn);
USBD_SET_SE0();
USBD_DISABLE_PHY();
}
void USBHAL::connect(void)
{
USBD_ResetDMA();
USBD_SET_ADDR(0);
/**
* Control Transfer Packet Size Constraints
* low-speed: 8
* full-speed: 8, 16, 32, 64
* high-speed: 64
*/
/* Control endpoint */
USBD_SetEpBufAddr(CEP, s_ep_buf_ind, MAX_PACKET_SIZE_EP0);
s_ep_buf_ind = MAX_PACKET_SIZE_EP0;
/* Enable USB/CEP interrupt */
USBD_ENABLE_USB_INT(USBD_GINTEN_USBIE_Msk | USBD_GINTEN_CEPIE_Msk);
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_SETUPPKIEN_Msk|USBD_CEPINTEN_STSDONEIEN_Msk);
/* Enable BUS interrupt */
USBD_ENABLE_BUS_INT(
USBD_BUSINTEN_DMADONEIEN_Msk |
USBD_BUSINTEN_RESUMEIEN_Msk |
USBD_BUSINTEN_RSTIEN_Msk |
USBD_BUSINTEN_VBUSDETIEN_Msk |
USBD_BUSINTEN_SOFIEN_Msk
);
/* Clear SE0 (connect) */
USBD_CLR_SE0();
}
void USBHAL::disconnect(void)
{
/* Set SE0 (disconnect) */
USBD_SET_SE0();
}
void USBHAL::configureDevice(void)
{
/**
* In USBDevice.cpp > USBDevice::requestSetConfiguration, configureDevice() is called after realiseEndpoint() (in USBCallback_setConfiguration()).
* So we have the following USB buffer management policy:
* 1. Allocate for CEP on connect().
* 2. Allocate for EPX in realiseEndpoint().
* 3. Deallocate all except for CEP in unconfigureDevice().
*/
}
void USBHAL::unconfigureDevice(void)
{
s_ep_buf_ind = MAX_PACKET_SIZE_EP0;
}
void USBHAL::setAddress(uint8_t address)
{
// NOTE: Delay address setting; otherwise, USB controller won't ack.
s_usb_addr = address;
}
void USBHAL::remoteWakeup(void)
{
USBD->OPER |= USBD_OPER_RESUMEEN_Msk;
}
bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options)
{
uint32_t ep_type;
uint32_t ep_hw_index = NU_EP2EPH(endpoint);
USBD_SetEpBufAddr(ep_hw_index, s_ep_buf_ind, maxPacket);
s_ep_buf_ind += maxPacket;
USBD_SET_MAX_PAYLOAD(ep_hw_index, maxPacket);
switch (NU_EP2EPL(endpoint))
{
case 1:case 2:
ep_type = USB_EP_CFG_TYPE_INT;
break;
case 3:case 4:
ep_type = USB_EP_CFG_TYPE_ISO;
break;
default:
ep_type = USB_EP_CFG_TYPE_BULK;
}
uint32_t ep_dir = (NU_EP_DIR(endpoint) == NU_EP_DIR_IN) ? USB_EP_CFG_DIR_IN : USB_EP_CFG_DIR_OUT;
USBD_ConfigEp(ep_hw_index, NU_EP2EPL(endpoint), ep_type, ep_dir);
/* Enable USB/EPX interrupt */
// NOTE: Require USBD_GINTEN_EPAIE_Pos, USBD_GINTEN_EPBIE_Pos, ... USBD_GINTEN_EPLIE_Pos to be consecutive.
USBD_ENABLE_USB_INT(USBD->GINTEN | USBD_GINTEN_USBIE_Msk |
USBD_GINTEN_CEPIE_Msk |
1 << (ep_hw_index + USBD_GINTEN_EPAIE_Pos)); // Added USB/EPX interrupt
if (ep_dir == 0)
USBD_ENABLE_EP_INT(ep_hw_index, USBD_EPINTEN_RXPKIEN_Msk);
else
USBD_ENABLE_EP_INT(ep_hw_index, USBD_EPINTEN_TXPKIEN_Msk);
return true;
}
void USBHAL::EP0setup(uint8_t *buffer)
{
uint32_t sz;
endpointReadResult(EP0OUT, buffer, &sz);
}
void USBHAL::EP0read(void)
{
if (s_setup.wLength && ! (s_setup.bmRequestType & 0x80))
{
// Control OUT
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_SETUPPKIEN_Msk | USBD_CEPINTEN_RXPKIEN_Msk);
}
else
{
// Status stage
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk);
USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR);
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
}
}
void USBHAL::EP0readStage(void)
{
// N/A
}
uint32_t USBHAL::EP0getReadResult(uint8_t *buffer)
{
uint32_t i;
uint32_t ceprxcnt = USBD->CEPRXCNT;
for (i = 0; i < ceprxcnt; i ++)
*buffer ++ = USBD->CEPDAT_BYTE;
return ceprxcnt;
}
void USBHAL::EP0write(uint8_t *buffer, uint32_t size)
{
if (buffer && size)
{
g_usbd_CtrlInPointer = buffer;
g_usbd_CtrlInSize = size;
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_INTKIF_Msk);
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_INTKIEN_Msk);
}
else
{
/* Status stage */
s_ctrlin_packetsize = 0;
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk);
USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR);
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
}
}
void USBHAL::EP0getWriteResult(void)
{
// N/A
}
void USBHAL::EP0stall(void)
{
stallEndpoint(EP0OUT);
}
EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize)
{
return EP_PENDING;
}
EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) //spcheng
{
if(endpoint == EP0OUT)
{
if (buffer) {
*((uint16_t *) (buffer + 0)) = (uint16_t) USBD->SETUP1_0;
*((uint16_t *) (buffer + 2)) = (uint16_t) USBD->SETUP3_2;
*((uint16_t *) (buffer + 4)) = (uint16_t) USBD->SETUP5_4;
*((uint16_t *) (buffer + 6)) = (uint16_t) USBD->SETUP7_6;
}
s_setup.bmRequestType = (uint8_t) (USBD->SETUP1_0 & 0xff);
s_setup.bRequest = (int8_t) (USBD->SETUP1_0 >> 8) & 0xff;
s_setup.wValue = (uint16_t) USBD->SETUP3_2;
s_setup.wIndex = (uint16_t) USBD->SETUP5_4;
s_setup.wLength = (uint16_t) USBD->SETUP7_6;
}
else
{
if (!(s_ep_compl & (1 << NU_EP2EPL(endpoint))))
{
while(1)
{
if (!(USBD->DMACTL & USBD_DMACTL_DMAEN_Msk))
break;
else
if (!USBD_IS_ATTACHED())
break;
}
gEpReadCnt = USBD_GET_EP_DATA_COUNT(NU_EP2EPH(endpoint));
if(gEpReadCnt == 0)
{
*bytesRead = 0;
return EP_COMPLETED;
}
s_ep_compl |= (1 << NU_EP2EPL(endpoint));
USBD_SET_DMA_LEN(gEpReadCnt);
USBD_SET_DMA_ADDR((uint32_t)buffer);
USBD_SET_DMA_WRITE(NU_EP2EPL(endpoint));
USBD_ENABLE_DMA();
return EP_PENDING;;
}
else
{
if ((USBD->DMACTL & USBD_DMACTL_DMAEN_Msk))
return EP_PENDING;;
USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_DMADONEIF_Msk);
s_ep_compl &= ~(1 << NU_EP2EPL(endpoint));
*bytesRead = gEpReadCnt;
}
}
return EP_COMPLETED;
}
uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer)
{
return 0;
}
EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size)
{
uint32_t ep_logic_index = NU_EP2EPL(endpoint);
if(ep_logic_index == 0)
return EP_INVALID;
else
{
uint32_t ep_hw_index = NU_EP2EPH(endpoint);
uint32_t mps = USBD_GET_EP_MAX_PAYLOAD(ep_hw_index);
if (size > mps) {
return EP_INVALID;
}
if(size < mps)
g_usbd_ShortPkt = 1;
if (!(s_ep_compl & (1 << NU_EP2EPL(endpoint))))
{
s_ep_compl |= (1 << ep_logic_index);
while(1)
{
if (!(USBD->DMACTL & USBD_DMACTL_DMAEN_Msk))
break;
else
if (!USBD_IS_ATTACHED())
break;
}
USBD_SET_DMA_LEN(size);
USBD_SET_DMA_ADDR((uint32_t)data);
USBD_SET_DMA_READ(ep_logic_index);
USBD_ENABLE_DMA();
}
}
return EP_PENDING;
}
EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint)
{
if (!(s_ep_compl & (1 << NU_EP2EPL(endpoint))))
return EP_COMPLETED;
else
{
if((USBD_GET_EP_DATA_COUNT(NU_EP2EPH(endpoint))) == 0 && !(USBD->DMACTL & USBD_DMACTL_DMAEN_Msk))
{
s_ep_compl &= ~(s_ep_compl & (1 << NU_EP2EPL(endpoint)));
return EP_COMPLETED;
}
}
return EP_PENDING;
}
void USBHAL::stallEndpoint(uint8_t endpoint)
{
uint32_t ep_hw_index = NU_EP2EPH(endpoint);
if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS)
return;
USBD_SetStall(ep_hw_index);
}
void USBHAL::unstallEndpoint(uint8_t endpoint)
{
uint32_t ep_hw_index = NU_EP2EPH(endpoint);
if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS)
return;
USBD_ClearStall(ep_hw_index);
}
bool USBHAL::getEndpointStallState(uint8_t endpoint)
{
uint32_t ep_hw_index = NU_EP2EPH(endpoint);
if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS)
return false;
return USBD_GetStall(ep_hw_index) ? 1 : 0;
}
void USBHAL::_usbisr(void)
{
MBED_ASSERT(instance);
instance->usbisr();
}
void USBHAL::usbisr(void)
{
uint32_t gintsts = USBD->GINTSTS & USBD->GINTEN;
if (! gintsts)
return;
if (gintsts & USBD_GINTSTS_USBIF_Msk)
{
uint32_t busintsts = USBD->BUSINTSTS & USBD->BUSINTEN;
/* SOF */
if (busintsts & USBD_BUSINTSTS_SOFIF_Msk)
{
USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_SOFIF_Msk);
// TODO
SOF(USBD->FRAMECNT >> 3);
}
/* Reset */
if (busintsts & USBD_BUSINTSTS_RSTIF_Msk)
{
connect();
USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_RSTIF_Msk);
USBD_CLR_CEP_INT_FLAG(0x1ffc);
}
/* Resume */
if (busintsts & USBD_BUSINTSTS_RESUMEIF_Msk)
{
USBD_ENABLE_BUS_INT(USBD_BUSINTEN_RSTIEN_Msk|USBD_BUSINTEN_SUSPENDIEN_Msk | USBD_BUSINTEN_SOFIEN_Msk | USBD_BUSINTEN_SOFIEN_Msk);
USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_RESUMEIF_Msk);
}
/* Suspend */
if (busintsts & USBD_BUSINTSTS_SUSPENDIF_Msk)
{
USBD_ENABLE_BUS_INT(USBD_BUSINTEN_RSTIEN_Msk | USBD_BUSINTEN_RESUMEIEN_Msk |USBD_BUSINTEN_SOFIEN_Msk);
USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_SUSPENDIF_Msk);
}
/* High-speed */
if (busintsts & USBD_BUSINTSTS_HISPDIF_Msk)
{
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_SETUPPKIEN_Msk);
USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_HISPDIF_Msk);
}
/* DMA */
if (busintsts & USBD_BUSINTSTS_DMADONEIF_Msk)
{
if(USBD->DMACTL & 0x10) /* IN - Read */
{
if(g_usbd_ShortPkt)
{
uint32_t ep_hw_index = NU_EPL2EPH((USBD->DMACTL & 0xF));
USBD_SET_EP_SHORT_PACKET(ep_hw_index);
g_usbd_ShortPkt = 0;
}
}
USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_DMADONEIF_Msk);
}
/* PHY clock available */
if (busintsts & USBD_BUSINTSTS_PHYCLKVLDIF_Msk)
{
USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_PHYCLKVLDIF_Msk);
}
/* VBUS plug-in */
if (busintsts & USBD_BUSINTSTS_VBUSDETIF_Msk)
{
if (USBD_IS_ATTACHED())
{
// USB plug-in
USBD_ENABLE_USB();
}
else
{
// USB unplug-out
USBD_DISABLE_USB();
}
USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_VBUSDETIF_Msk);
}
}
/* CEP interrupts */
if (gintsts & USBD_GINTSTS_CEPIF_Msk)
{
uint32_t cepintsts = USBD->CEPINTSTS & USBD->CEPINTEN;
/* SETUP token packet */
if (cepintsts & USBD_CEPINTSTS_SETUPTKIF_Msk)
{
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_SETUPTKIF_Msk);
return;
}
/* SETUP transaction */
if (cepintsts & USBD_CEPINTSTS_SETUPPKIF_Msk)
{
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_SETUPPKIF_Msk);
EP0setupCallback();
return;
}
/* OUT token packet */
if (cepintsts & USBD_CEPINTSTS_OUTTKIF_Msk)
{
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_OUTTKIF_Msk);
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
return;
}
/* IN token packet */
if (cepintsts & USBD_CEPINTSTS_INTKIF_Msk)
{
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_INTKIF_Msk);
if (!(cepintsts & USBD_CEPINTSTS_STSDONEIF_Msk))
{
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_TXPKIF_Msk);
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_TXPKIEN_Msk);
USBD_CtrlInput();
}
else
{
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_TXPKIF_Msk);
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_TXPKIEN_Msk|USBD_CEPINTEN_STSDONEIEN_Msk);
}
return;
}
/* PING packet */
if (cepintsts & USBD_CEPINTSTS_PINGIF_Msk)
{
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_PINGIF_Msk);
return;
}
/* IN transaction */
if (cepintsts & USBD_CEPINTSTS_TXPKIF_Msk)
{
EP0in();
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_TXPKIF_Msk);
return;
}
/* OUT transaction */
if (cepintsts & USBD_CEPINTSTS_RXPKIF_Msk)
{
EP0out();
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_RXPKIF_Msk);
return;
}
/* NAK handshake packet */
if (cepintsts & USBD_CEPINTSTS_NAKIF_Msk)
{
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_NAKIF_Msk);
return;
}
/* STALL handshake packet */
if (cepintsts & USBD_CEPINTSTS_STALLIF_Msk)
{
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STALLIF_Msk);
return;
}
/* ERR special packet */
if (cepintsts & USBD_CEPINTSTS_ERRIF_Msk)
{
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_ERRIF_Msk);
return;
}
/* Status stage transaction */
if (cepintsts & USBD_CEPINTSTS_STSDONEIF_Msk)
{
if (s_usb_addr)
{
USBD_SET_ADDR(s_usb_addr);
s_usb_addr = 0;
}
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk);
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_SETUPPKIEN_Msk);
return;
}
/* Buffer Full */
if (cepintsts & USBD_CEPINTSTS_BUFFULLIF_Msk)
{
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_BUFFULLIF_Msk);
return;
}
/* Buffer Empty */
if (cepintsts & USBD_CEPINTSTS_BUFEMPTYIF_Msk)
{
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_BUFEMPTYIF_Msk);
return;
}
}
/* EPA, EPB, EPC, ... EPL interrupts */
uint32_t gintsts_epx = gintsts >> 2;
uint32_t ep_hw_index = 0;
while (gintsts_epx) {
if(gintsts_epx & 0x01)
{
uint32_t epxintsts = USBD_GET_EP_INT_FLAG(ep_hw_index) & USBD_GET_EP_INT_EN(ep_hw_index);
USBD_CLR_EP_INT_FLAG(ep_hw_index, epxintsts);
/* Buffer Full */
if (epxintsts & USBD_EPINTSTS_BUFFULLIF_Msk)
{
}
/* Buffer Empty */
if (epxintsts & USBD_EPINTSTS_BUFEMPTYIF_Msk)
{
}
/* Short Packet Transferred */
if (epxintsts & USBD_EPINTSTS_SHORTTXIF_Msk)
{
}
/* Data Packet Transmitted */
if (epxintsts & USBD_EPINTSTS_TXPKIF_Msk)
{
s_ep_compl &= ~(1 << (NU_EPH2EPL(ep_hw_index)));
if ((instance->*(epCallback[ep_hw_index]))())
{
}
}
/* Data Packet Received */
if (epxintsts & USBD_EPINTSTS_RXPKIF_Msk)
{
if ((instance->*(epCallback[ep_hw_index]))())
{
}
}
/* OUT token packet */
if (epxintsts & USBD_EPINTSTS_OUTTKIF_Msk)
{
}
/* IN token packet */
if (epxintsts & USBD_EPINTSTS_INTKIF_Msk)
{
}
/* PING packet */
if (epxintsts & USBD_EPINTSTS_PINGIF_Msk)
{
}
/* NAK handshake packet sent to Host */
if (epxintsts & USBD_EPINTSTS_NAKIF_Msk)
{
}
/* STALL handshake packet sent to Host */
if (epxintsts & USBD_EPINTSTS_STALLIF_Msk)
{
}
/* NYET handshake packet sent to Host */
if (epxintsts & USBD_EPINTSTS_NYETIF_Msk)
{
}
/* ERR packet sent to Host */
if (epxintsts & USBD_EPINTSTS_ERRIF_Msk)
{
}
/* Bulk Out Short Packet Received */
if (epxintsts & USBD_EPINTSTS_SHORTRXIF_Msk)
{
}
}
gintsts_epx = gintsts_epx >> 1;
ep_hw_index++;
}
}
#endif

View File

@ -43,15 +43,15 @@ volatile uint8_t g_usbd_RemoteWakeupEn = 0; /*!< Remote wake up function enable
/**
* @cond HIDDEN_SYMBOLS
*/
static volatile uint8_t *g_usbd_CtrlInPointer = 0;
static volatile uint32_t g_usbd_CtrlInSize = 0;
static volatile uint8_t *g_usbd_CtrlOutPointer = 0;
static volatile uint32_t g_usbd_CtrlOutSize = 0;
static volatile uint32_t g_usbd_CtrlOutSizeLimit = 0;
static volatile uint32_t g_usbd_UsbAddr = 0;
static volatile uint32_t g_usbd_UsbConfig = 0;
static volatile uint32_t g_usbd_CtrlMaxPktSize = 8;
static volatile uint32_t g_usbd_UsbAltInterface = 0;
volatile uint8_t *g_usbd_CtrlInPointer = 0;
volatile uint32_t g_usbd_CtrlInSize = 0;
volatile uint8_t *g_usbd_CtrlOutPointer = 0;
volatile uint32_t g_usbd_CtrlOutSize = 0;
volatile uint32_t g_usbd_CtrlOutSizeLimit = 0;
volatile uint32_t g_usbd_UsbAddr = 0;
volatile uint32_t g_usbd_UsbConfig = 0;
volatile uint32_t g_usbd_CtrlMaxPktSize = 64;
volatile uint32_t g_usbd_UsbAltInterface = 0;
/**
* @endcond
*/
@ -303,7 +303,7 @@ void USBD_StandardRequest(void)
// Device to host
switch(g_usbd_SetupPacket[1])
{
case GET_CONFIGURATION:
case USBD_GET_CONFIGURATION:
{
// Return current configuration setting
/* Data stage */
@ -317,13 +317,13 @@ void USBD_StandardRequest(void)
break;
}
case GET_DESCRIPTOR:
case USBD_GET_DESCRIPTOR:
{
USBD_GetDescriptor();
USBD_PrepareCtrlOut(0, 0); /* For status stage */
break;
}
case GET_INTERFACE:
case USBD_GET_INTERFACE:
{
// Return current interface setting
/* Data stage */
@ -337,7 +337,7 @@ void USBD_StandardRequest(void)
break;
}
case GET_STATUS:
case USBD_GET_STATUS:
{
// Device
if(g_usbd_SetupPacket[0] == 0x80)
@ -389,7 +389,7 @@ void USBD_StandardRequest(void)
// Host to device
switch(g_usbd_SetupPacket[1])
{
case CLEAR_FEATURE:
case USBD_CLEAR_FEATURE:
{
if(g_usbd_SetupPacket[2] == FEATURE_ENDPOINT_HALT)
{
@ -418,7 +418,7 @@ void USBD_StandardRequest(void)
break;
}
case SET_ADDRESS:
case USBD_SET_ADDRESS:
{
g_usbd_UsbAddr = g_usbd_SetupPacket[2];
DBG_PRINTF("Set addr to %d\n", g_usbd_UsbAddr);
@ -430,7 +430,7 @@ void USBD_StandardRequest(void)
break;
}
case SET_CONFIGURATION:
case USBD_SET_CONFIGURATION:
{
g_usbd_UsbConfig = g_usbd_SetupPacket[2];
@ -446,7 +446,7 @@ void USBD_StandardRequest(void)
break;
}
case SET_FEATURE:
case USBD_SET_FEATURE:
{
if(g_usbd_SetupPacket[2] == FEATURE_ENDPOINT_HALT)
{
@ -467,7 +467,7 @@ void USBD_StandardRequest(void)
break;
}
case SET_INTERFACE:
case USBD_SET_INTERFACE:
{
g_usbd_UsbAltInterface = g_usbd_SetupPacket[2];
if(g_usbd_pfnSetInterface != NULL)
@ -568,7 +568,7 @@ void USBD_CtrlIn(void)
else // No more data for IN token
{
// In ACK for Set address
if((g_usbd_SetupPacket[0] == REQ_STANDARD) && (g_usbd_SetupPacket[1] == SET_ADDRESS))
if((g_usbd_SetupPacket[0] == REQ_STANDARD) && (g_usbd_SetupPacket[1] == USBD_SET_ADDRESS))
{
if((USBD_GET_ADDR() != g_usbd_UsbAddr) && (USBD_GET_ADDR() == 0))
{

View File

@ -66,17 +66,17 @@ extern const S_USBD_INFO_T gsInfo;
#define REQ_VENDOR 0x40
/*!<USB Standard Request */
#define GET_STATUS 0x00
#define CLEAR_FEATURE 0x01
#define SET_FEATURE 0x03
#define SET_ADDRESS 0x05
#define GET_DESCRIPTOR 0x06
#define SET_DESCRIPTOR 0x07
#define GET_CONFIGURATION 0x08
#define SET_CONFIGURATION 0x09
#define GET_INTERFACE 0x0A
#define SET_INTERFACE 0x0B
#define SYNC_FRAME 0x0C
#define USBD_GET_STATUS 0x00
#define USBD_CLEAR_FEATURE 0x01
#define USBD_SET_FEATURE 0x03
#define USBD_SET_ADDRESS 0x05
#define USBD_GET_DESCRIPTOR 0x06
#define USBD_SET_DESCRIPTOR 0x07
#define USBD_GET_CONFIGURATION 0x08
#define USBD_SET_CONFIGURATION 0x09
#define USBD_GET_INTERFACE 0x0A
#define USBD_SET_INTERFACE 0x0B
#define USBD_SYNC_FRAME 0x0C
/*!<USB Descriptor Type */
#define DESC_DEVICE 0x01

View File

@ -303,7 +303,7 @@ void USBD_StandardRequest(void)
if (gUsbCmd.bmRequestType & 0x80) { /* request data transfer direction */
// Device to host
switch (gUsbCmd.bRequest) {
case GET_CONFIGURATION: {
case USBD_GET_CONFIGURATION: {
// Return current configuration setting
USBD_PrepareCtrlIn((uint8_t *)&g_usbd_UsbConfig, 1);
@ -311,14 +311,14 @@ void USBD_StandardRequest(void)
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_INTKIEN_Msk);
break;
}
case GET_DESCRIPTOR: {
case USBD_GET_DESCRIPTOR: {
if (!USBD_GetDescriptor()) {
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_INTKIF_Msk);
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_INTKIEN_Msk);
}
break;
}
case GET_INTERFACE: {
case USBD_GET_INTERFACE: {
// Return current interface setting
USBD_PrepareCtrlIn((uint8_t *)&g_usbd_UsbAltInterface, 1);
@ -326,7 +326,7 @@ void USBD_StandardRequest(void)
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_INTKIEN_Msk);
break;
}
case GET_STATUS: {
case USBD_GET_STATUS: {
// Device
if (gUsbCmd.bmRequestType == 0x80) {
if (g_usbd_sInfo->gu8ConfigDesc[7] & 0x40)
@ -357,14 +357,14 @@ void USBD_StandardRequest(void)
} else {
// Host to device
switch (gUsbCmd.bRequest) {
case CLEAR_FEATURE: {
case USBD_CLEAR_FEATURE: {
/* Status stage */
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk);
USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR);
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
break;
}
case SET_ADDRESS: {
case USBD_SET_ADDRESS: {
g_usbd_UsbAddr = (uint8_t)gUsbCmd.wValue;
// DATA IN for end of setup
@ -374,7 +374,7 @@ void USBD_StandardRequest(void)
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
break;
}
case SET_CONFIGURATION: {
case USBD_SET_CONFIGURATION: {
g_usbd_UsbConfig = (uint8_t)gUsbCmd.wValue;
g_usbd_Configured = 1;
// DATA IN for end of setup
@ -384,14 +384,14 @@ void USBD_StandardRequest(void)
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
break;
}
case SET_FEATURE: {
case USBD_SET_FEATURE: {
/* Status stage */
USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk);
USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR);
USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
break;
}
case SET_INTERFACE: {
case USBD_SET_INTERFACE: {
g_usbd_UsbAltInterface = (uint8_t)gUsbCmd.wValue;
if (g_usbd_pfnSetInterface != NULL)
g_usbd_pfnSetInterface(g_usbd_UsbAltInterface);
@ -422,16 +422,16 @@ void USBD_StandardRequest(void)
void USBD_UpdateDeviceState(void)
{
switch (gUsbCmd.bRequest) {
case SET_ADDRESS: {
case USBD_SET_ADDRESS: {
USBD_SET_ADDR(g_usbd_UsbAddr);
break;
}
case SET_FEATURE: {
case USBD_SET_FEATURE: {
if(gUsbCmd.wValue == FEATURE_ENDPOINT_HALT)
USBD_SetStall(gUsbCmd.wIndex & 0xF);
break;
}
case CLEAR_FEATURE: {
case USBD_CLEAR_FEATURE: {
if(gUsbCmd.wValue == FEATURE_ENDPOINT_HALT)
USBD_ClearStall(gUsbCmd.wIndex & 0xF);
break;

View File

@ -55,17 +55,17 @@ extern "C"
#define REQ_VENDOR 0x40
/* USB Standard Request */
#define GET_STATUS 0x00
#define CLEAR_FEATURE 0x01
#define SET_FEATURE 0x03
#define SET_ADDRESS 0x05
#define GET_DESCRIPTOR 0x06
#define SET_DESCRIPTOR 0x07
#define GET_CONFIGURATION 0x08
#define SET_CONFIGURATION 0x09
#define GET_INTERFACE 0x0A
#define SET_INTERFACE 0x0B
#define SYNC_FRAME 0x0C
#define USBD_GET_STATUS 0x00
#define USBD_CLEAR_FEATURE 0x01
#define USBD_SET_FEATURE 0x03
#define USBD_SET_ADDRESS 0x05
#define USBD_GET_DESCRIPTOR 0x06
#define USBD_SET_DESCRIPTOR 0x07
#define USBD_GET_CONFIGURATION 0x08
#define USBD_SET_CONFIGURATION 0x09
#define USBD_GET_INTERFACE 0x0A
#define USBD_SET_INTERFACE 0x0B
#define USBD_SYNC_FRAME 0x0C
/* USB Descriptor Type */
#define DESC_DEVICE 0x01