initial USB CDC+HID vendor code merge

merged in example USB CDC + HID code, but removed any comms with actual USART peripheral.

this code is not yet tested. once tested and USB enumerates, work can proceed.
This commit is contained in:
true
2024-11-06 19:58:23 -08:00
parent a903464666
commit 243ae309cf
32 changed files with 5566 additions and 48 deletions

View File

@@ -0,0 +1,954 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_core.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : Standard protocol processing (USB v2.0)
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "../../driver/inc/usb_lib.h"
/* Global define */
#define ValBit(VAR,Place) (VAR & (1 << Place))
#define SetBit(VAR,Place) (VAR |= (1 << Place))
#define ClrBit(VAR,Place) (VAR &= ((1 << Place) ^ 255))
#define Send0LengthData() { _SetEPTxCount(ENDP0, 0); \
vSetEPTxStatus(EP_TX_VALID); \
}
#define vSetEPRxStatus(st) (SaveRState = st)
#define vSetEPTxStatus(st) (SaveTState = st)
#define USB_StatusIn() Send0LengthData()
#define USB_StatusOut() vSetEPRxStatus(EP_RX_VALID)
#define StatusInfo0 StatusInfo.bw.bb1
#define StatusInfo1 StatusInfo.bw.bb0
uint16_t_uint8_t StatusInfo;
bool Data_Mul_MaxPacketSize = FALSE;
static void DataStageOut(void);
static void DataStageIn(void);
static void NoData_Setup0(void);
static void Data_Setup0(void);
/*********************************************************************
* @fn Standard_GetConfiguration
*
* @brief Return the current configuration variable address.
*
* @param Length - How many bytes are needed.
*
* @return Return 1 - if the request is invalid when "Length" is 0.
* Return "Buffer" if the "Length" is not 0.
*/
uint8_t *Standard_GetConfiguration(uint16_t Length)
{
if (Length == 0)
{
pInformation->Ctrl_Info.Usb_wLength = sizeof(pInformation->Current_Configuration);
return 0;
}
pUser_Standard_Requests->User_GetConfiguration();
return (uint8_t *)&pInformation->Current_Configuration;
}
/*********************************************************************
* @fn Standard_SetConfiguration
*
* @brief This routine is called to set the configuration value
* Then each class should configure device itself.
*
* @param None.
*
* @return Return USB_SUCCESS - if the request is performed.
* Return USB_UNSUPPORT - if the request is invalid.
*/
RESULT Standard_SetConfiguration(void)
{
if ((pInformation->USBwValue0 <=
Device_Table.Total_Configuration) && (pInformation->USBwValue1 == 0)
&& (pInformation->USBwIndex == 0))
{
pInformation->Current_Configuration = pInformation->USBwValue0;
pUser_Standard_Requests->User_SetConfiguration();
return USB_SUCCESS;
}
else
{
return USB_UNSUPPORT;
}
}
/*********************************************************************
* @fn Standard_GetInterface
*
* @brief Return the Alternate Setting of the current interface.
*
* @param Length - How many bytes are needed.
*
* @return Return 0 - if the request is invalid when "Length" is 0.
* Return "Buffer" if the "Length" is not 0.
*/
uint8_t *Standard_GetInterface(uint16_t Length)
{
if (Length == 0)
{
pInformation->Ctrl_Info.Usb_wLength = sizeof(pInformation->Current_AlternateSetting);
return 0;
}
pUser_Standard_Requests->User_GetInterface();
return (uint8_t *)&pInformation->Current_AlternateSetting;
}
/*********************************************************************
* @fn Standard_SetInterface
*
* @brief This routine is called to set the interface.
* Then each class should configure the interface them self.
*
* @param None
*
* @return Return USB_SUCCESS - if the request is performed.
* Return USB_UNSUPPORT - if the request is invalid.
*/
RESULT Standard_SetInterface(void)
{
RESULT Re;
Re = (*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, pInformation->USBwValue0);
if (pInformation->Current_Configuration != 0)
{
if ((Re != USB_SUCCESS) || (pInformation->USBwIndex1 != 0)
|| (pInformation->USBwValue1 != 0))
{
return USB_UNSUPPORT;
}
else if (Re == USB_SUCCESS)
{
pUser_Standard_Requests->User_SetInterface();
pInformation->Current_Interface = pInformation->USBwIndex0;
pInformation->Current_AlternateSetting = pInformation->USBwValue0;
return USB_SUCCESS;
}
}
return USB_UNSUPPORT;
}
/*********************************************************************
* @fn Standard_GetStatus
*
* @brief Copy the device request data to "StatusInfo buffer".
*
* @param Length - How many bytes are needed.
*
* @return Return 0 - if the request is at end of data block,
* or is invalid when "Length" is 0.
*/
uint8_t *Standard_GetStatus(uint16_t Length)
{
if (Length == 0)
{
pInformation->Ctrl_Info.Usb_wLength = 2;
return 0;
}
StatusInfo.w = 0;
if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
{
uint8_t Feature = pInformation->Current_Feature;
if (ValBit(Feature, 5))
{
SetBit(StatusInfo0, 1);
}
else
{
ClrBit(StatusInfo0, 1);
}
if (ValBit(Feature, 6))
{
SetBit(StatusInfo0, 0);
}
else
{
ClrBit(StatusInfo0, 0);
}
}
else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
{
return (uint8_t *)&StatusInfo;
}
else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT))
{
uint8_t Related_Endpoint;
uint8_t wIndex0 = pInformation->USBwIndex0;
Related_Endpoint = (wIndex0 & 0x0f);
if (ValBit(wIndex0, 7))
{
if (_GetTxStallStatus(Related_Endpoint))
{
SetBit(StatusInfo0, 0);
}
}
else
{
if (_GetRxStallStatus(Related_Endpoint))
{
SetBit(StatusInfo0, 0);
}
}
}
else
{
return NULL;
}
pUser_Standard_Requests->User_GetStatus();
return (uint8_t *)&StatusInfo;
}
/*********************************************************************
* @fn Standard_ClearFeature
*
* @brief Clear or disable a specific feature.
*
* @return Return USB_SUCCESS - if the request is performed.
* Return USB_UNSUPPORT - if the request is invalid.
*/
RESULT Standard_ClearFeature(void)
{
uint32_t Type_Rec = Type_Recipient;
uint32_t Status;
if (Type_Rec == (STANDARD_REQUEST | DEVICE_RECIPIENT))
{
ClrBit(pInformation->Current_Feature, 5);
return USB_SUCCESS;
}
else if (Type_Rec == (STANDARD_REQUEST | ENDPOINT_RECIPIENT))
{
DEVICE* pDev;
uint32_t Related_Endpoint;
uint32_t wIndex0;
uint32_t rEP;
if ((pInformation->USBwValue != ENDPOINT_STALL)
|| (pInformation->USBwIndex1 != 0))
{
return USB_UNSUPPORT;
}
pDev = &Device_Table;
wIndex0 = pInformation->USBwIndex0;
rEP = wIndex0 & ~0x80;
Related_Endpoint = ENDP0 + rEP;
if (ValBit(pInformation->USBwIndex0, 7))
{
Status = _GetEPTxStatus(Related_Endpoint);
}
else
{
Status = _GetEPRxStatus(Related_Endpoint);
}
if ((rEP >= pDev->Total_Endpoint) || (Status == 0)
|| (pInformation->Current_Configuration == 0))
{
return USB_UNSUPPORT;
}
if (wIndex0 & 0x80)
{
if (_GetTxStallStatus(Related_Endpoint ))
{
ClearDTOG_TX(Related_Endpoint);
SetEPTxStatus(Related_Endpoint, EP_TX_VALID);
}
}
else
{
if (_GetRxStallStatus(Related_Endpoint))
{
if (Related_Endpoint == ENDP0)
{
SetEPRxCount(Related_Endpoint, Device_Property.MaxPacketSize);
_SetEPRxStatus(Related_Endpoint, EP_RX_VALID);
}
else
{
ClearDTOG_RX(Related_Endpoint);
_SetEPRxStatus(Related_Endpoint, EP_RX_VALID);
}
}
}
pUser_Standard_Requests->User_ClearFeature();
return USB_SUCCESS;
}
return USB_UNSUPPORT;
}
/*********************************************************************
* @fn Standard_SetEndPointFeature
*
* @brief Set or enable a specific feature of EndPoint
*
* @return Return USB_SUCCESS - if the request is performed.
* Return USB_UNSUPPORT - if the request is invalid.
*/
RESULT Standard_SetEndPointFeature(void)
{
uint32_t wIndex0;
uint32_t Related_Endpoint;
uint32_t rEP;
uint32_t Status;
wIndex0 = pInformation->USBwIndex0;
rEP = wIndex0 & ~0x80;
Related_Endpoint = ENDP0 + rEP;
if (ValBit(pInformation->USBwIndex0, 7))
{
Status = _GetEPTxStatus(Related_Endpoint);
}
else
{
Status = _GetEPRxStatus(Related_Endpoint);
}
if (Related_Endpoint >= Device_Table.Total_Endpoint
|| pInformation->USBwValue != 0 || Status == 0
|| pInformation->Current_Configuration == 0)
{
return USB_UNSUPPORT;
}
else
{
if (wIndex0 & 0x80)
{
_SetEPTxStatus(Related_Endpoint, EP_TX_STALL);
}
else
{
_SetEPRxStatus(Related_Endpoint, EP_RX_STALL);
}
}
pUser_Standard_Requests->User_SetEndPointFeature();
return USB_SUCCESS;
}
/*********************************************************************
* @fn Standard_SetDeviceFeature
*
* @brief Set or enable a specific feature of Device.
*
* @return Return USB_SUCCESS - if the request is performed.
* Return USB_UNSUPPORT - if the request is invalid.
*/
RESULT Standard_SetDeviceFeature(void)
{
SetBit(pInformation->Current_Feature, 5);
pUser_Standard_Requests->User_SetDeviceFeature();
return USB_SUCCESS;
}
/*********************************************************************
* @fn Standard_GetDescriptorData
*
* @brief This routine is used for the descriptors resident in Flash
* or RAM pDesc can be in either Flash or RAM
* The purpose of this routine is to have a versatile way to
* response descriptors request. It allows user to generate
* certain descriptors with software or read descriptors from
* external storage part by part.
*
* @param Length - Length of the data in this transfer.
* pDesc - A pointer points to descriptor struct.
* The structure gives the initial address of the descriptor and
* its original size.
*
* @return Address of a part of the descriptor pointed by the Usb_
* wOffset The buffer pointed by this address contains at least
* Length bytes.
*/
uint8_t *Standard_GetDescriptorData(uint16_t Length, ONE_DESCRIPTOR *pDesc)
{
uint32_t wOffset;
wOffset = pInformation->Ctrl_Info.Usb_wOffset;
if (Length == 0)
{
pInformation->Ctrl_Info.Usb_wLength = pDesc->Descriptor_Size - wOffset;
return 0;
}
return pDesc->Descriptor + wOffset;
}
/*********************************************************************
* @fn DataStageOut
*
* @brief Data stage of a Control Write Transfer.
*
* @return none
*/
void DataStageOut(void)
{
ENDPOINT_INFO *pEPinfo = &pInformation->Ctrl_Info;
uint32_t save_rLength;
save_rLength = pEPinfo->Usb_rLength;
if (pEPinfo->CopyData && save_rLength)
{
uint8_t *Buffer;
uint32_t Length;
Length = pEPinfo->PacketSize;
if (Length > save_rLength)
{
Length = save_rLength;
}
Buffer = (*pEPinfo->CopyData)(Length);
pEPinfo->Usb_rLength -= Length;
pEPinfo->Usb_rOffset += Length;
PMAToUserBufferCopy(Buffer, GetEPRxAddr(ENDP0), Length);
}
if (pEPinfo->Usb_rLength != 0)
{
vSetEPRxStatus(EP_RX_VALID);
SetEPTxCount(ENDP0, 0);
vSetEPTxStatus(EP_TX_VALID);
}
if (pEPinfo->Usb_rLength >= pEPinfo->PacketSize)
{
pInformation->ControlState = OUT_DATA;
}
else
{
if (pEPinfo->Usb_rLength > 0)
{
pInformation->ControlState = LAST_OUT_DATA;
}
else if (pEPinfo->Usb_rLength == 0)
{
pInformation->ControlState = WAIT_STATUS_IN;
USB_StatusIn();
}
}
}
/*********************************************************************
* @fn DataStageIn
*
* @brief Data stage of a Control Read Transfer.
*
* @return none
*/
void DataStageIn(void)
{
ENDPOINT_INFO *pEPinfo = &pInformation->Ctrl_Info;
uint32_t save_wLength = pEPinfo->Usb_wLength;
uint32_t ControlState = pInformation->ControlState;
uint8_t *DataBuffer;
uint32_t Length;
if ((save_wLength == 0) && (ControlState == LAST_IN_DATA))
{
if(Data_Mul_MaxPacketSize == TRUE)
{
Send0LengthData();
ControlState = LAST_IN_DATA;
Data_Mul_MaxPacketSize = FALSE;
}
else
{
ControlState = WAIT_STATUS_OUT;
vSetEPTxStatus(EP_TX_STALL);
}
goto Expect_Status_Out;
}
Length = pEPinfo->PacketSize;
ControlState = (save_wLength <= Length) ? LAST_IN_DATA : IN_DATA;
if (Length > save_wLength)
{
Length = save_wLength;
}
DataBuffer = (*pEPinfo->CopyData)(Length);
UserToPMABufferCopy(DataBuffer, GetEPTxAddr(ENDP0), Length);
SetEPTxCount(ENDP0, Length);
pEPinfo->Usb_wLength -= Length;
pEPinfo->Usb_wOffset += Length;
vSetEPTxStatus(EP_TX_VALID);
USB_StatusOut();
Expect_Status_Out:
pInformation->ControlState = ControlState;
}
/*********************************************************************
* @fn NoData_Setup0
*
* @brief Proceed the processing of setup request without data stage.
*
* @return none
*/
void NoData_Setup0(void)
{
RESULT Result = USB_UNSUPPORT;
uint32_t RequestNo = pInformation->USBbRequest;
uint32_t ControlState;
if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
{
if (RequestNo == SET_CONFIGURATION)
{
Result = Standard_SetConfiguration();
}
else if (RequestNo == SET_ADDRESS)
{
if ((pInformation->USBwValue0 > 127) || (pInformation->USBwValue1 != 0)
|| (pInformation->USBwIndex != 0)
|| (pInformation->Current_Configuration != 0))
{
ControlState = STALLED;
goto exit_NoData_Setup0;
}
else
{
Result = USB_SUCCESS;
}
}
else if (RequestNo == SET_FEATURE)
{
if ((pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP) \
&& (pInformation->USBwIndex == 0))
{
Result = Standard_SetDeviceFeature();
}
else
{
Result = USB_UNSUPPORT;
}
}
else if (RequestNo == CLEAR_FEATURE)
{
if (pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP
&& pInformation->USBwIndex == 0
&& ValBit(pInformation->Current_Feature, 5))
{
Result = Standard_ClearFeature();
}
else
{
Result = USB_UNSUPPORT;
}
}
}
else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
{
if (RequestNo == SET_INTERFACE)
{
Result = Standard_SetInterface();
}
}
else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT))
{
if (RequestNo == CLEAR_FEATURE)
{
Result = Standard_ClearFeature();
}
else if (RequestNo == SET_FEATURE)
{
Result = Standard_SetEndPointFeature();
}
}
else
{
Result = USB_UNSUPPORT;
}
if (Result != USB_SUCCESS)
{
Result = (*pProperty->Class_NoData_Setup)(RequestNo);
if (Result == USB_NOT_READY)
{
ControlState = PAUSE;
goto exit_NoData_Setup0;
}
}
if (Result != USB_SUCCESS)
{
ControlState = STALLED;
goto exit_NoData_Setup0;
}
ControlState = WAIT_STATUS_IN;
USB_StatusIn();
exit_NoData_Setup0:
pInformation->ControlState = ControlState;
return;
}
/*********************************************************************
* @fn Data_Setup0
*
* @brief Proceed the processing of setup request with data stage.
*
* @return none
*/
void Data_Setup0(void)
{
uint8_t *(*CopyRoutine)(uint16_t);
RESULT Result;
uint32_t Request_No = pInformation->USBbRequest;
uint32_t Related_Endpoint, Reserved;
uint32_t wOffset, Status;
CopyRoutine = NULL;
wOffset = 0;
if (Request_No == GET_DESCRIPTOR)
{
if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
{
uint8_t wValue1 = pInformation->USBwValue1;
if (wValue1 == DEVICE_DESCRIPTOR)
{
CopyRoutine = pProperty->GetDeviceDescriptor;
}
else if (wValue1 == CONFIG_DESCRIPTOR)
{
CopyRoutine = pProperty->GetConfigDescriptor;
}
else if (wValue1 == STRING_DESCRIPTOR)
{
CopyRoutine = pProperty->GetStringDescriptor;
}
}
}
else if ((Request_No == GET_STATUS) && (pInformation->USBwValue == 0)
&& (pInformation->USBwLength == 0x0002)
&& (pInformation->USBwIndex1 == 0))
{
if ((Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
&& (pInformation->USBwIndex == 0))
{
CopyRoutine = Standard_GetStatus;
}
else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
{
if (((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == USB_SUCCESS)
&& (pInformation->Current_Configuration != 0))
{
CopyRoutine = Standard_GetStatus;
}
}
else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT))
{
Related_Endpoint = (pInformation->USBwIndex0 & 0x0f);
Reserved = pInformation->USBwIndex0 & 0x70;
if (ValBit(pInformation->USBwIndex0, 7))
{
Status = _GetEPTxStatus(Related_Endpoint);
}
else
{
Status = _GetEPRxStatus(Related_Endpoint);
}
if ((Related_Endpoint < Device_Table.Total_Endpoint) && (Reserved == 0)
&& (Status != 0))
{
CopyRoutine = Standard_GetStatus;
}
}
}
else if (Request_No == GET_CONFIGURATION)
{
if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
{
CopyRoutine = Standard_GetConfiguration;
}
}
else if (Request_No == GET_INTERFACE)
{
if ((Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
&& (pInformation->Current_Configuration != 0) && (pInformation->USBwValue == 0)
&& (pInformation->USBwIndex1 == 0) && (pInformation->USBwLength == 0x0001)
&& ((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == USB_SUCCESS))
{
CopyRoutine = Standard_GetInterface;
}
}
if (CopyRoutine)
{
pInformation->Ctrl_Info.Usb_wOffset = wOffset;
pInformation->Ctrl_Info.CopyData = CopyRoutine;
(*CopyRoutine)(0);
Result = USB_SUCCESS;
}
else
{
Result = (*pProperty->Class_Data_Setup)(pInformation->USBbRequest);
if (Result == USB_NOT_READY)
{
pInformation->ControlState = PAUSE;
return;
}
}
if (pInformation->Ctrl_Info.Usb_wLength == 0xFFFF)
{
pInformation->ControlState = PAUSE;
return;
}
if ((Result == USB_UNSUPPORT) || (pInformation->Ctrl_Info.Usb_wLength == 0))
{
pInformation->ControlState = STALLED;
return;
}
if (ValBit(pInformation->USBbmRequestType, 7))
{
__IO uint32_t wLength = pInformation->USBwLength;
if (pInformation->Ctrl_Info.Usb_wLength > wLength)
{
pInformation->Ctrl_Info.Usb_wLength = wLength;
}
else if (pInformation->Ctrl_Info.Usb_wLength < pInformation->USBwLength)
{
if (pInformation->Ctrl_Info.Usb_wLength < pProperty->MaxPacketSize)
{
Data_Mul_MaxPacketSize = FALSE;
}
else if ((pInformation->Ctrl_Info.Usb_wLength % pProperty->MaxPacketSize) == 0)
{
Data_Mul_MaxPacketSize = TRUE;
}
}
pInformation->Ctrl_Info.PacketSize = pProperty->MaxPacketSize;
DataStageIn();
}
else
{
pInformation->ControlState = OUT_DATA;
vSetEPRxStatus(EP_RX_VALID);
}
return;
}
/*********************************************************************
* @fn Setup0_Process
*
* @brief Get the device request data and dispatch to individual process.
*
* @return Post0_Process.
*/
uint8_t Setup0_Process(void)
{
union
{
uint8_t* b;
uint16_t* w;
} pBuf;
uint16_t offset = 1;
pBuf.b = PMAAddr + (uint8_t *)(_GetEPRxAddr(ENDP0) * 2); /* *2 for 32 bits addr */
if (pInformation->ControlState != PAUSE)
{
pInformation->USBbmRequestType = *pBuf.b++; /* bmRequestType */
pInformation->USBbRequest = *pBuf.b++; /* bRequest */
pBuf.w += offset; /* word not accessed because of 32 bits addressing */
pInformation->USBwValue = ByteSwap(*pBuf.w++); /* wValue */
pBuf.w += offset; /* word not accessed because of 32 bits addressing */
pInformation->USBwIndex = ByteSwap(*pBuf.w++); /* wIndex */
pBuf.w += offset; /* word not accessed because of 32 bits addressing */
pInformation->USBwLength = *pBuf.w; /* wLength */
}
pInformation->ControlState = SETTING_UP;
if (pInformation->USBwLength == 0)
{
NoData_Setup0();
}
else
{
Data_Setup0();
}
return Post0_Process();
}
/*********************************************************************
* @fn In0_Process
*
* @brief Process the IN token on all default endpoint.
*
* @return none
*/
uint8_t In0_Process(void)
{
uint32_t ControlState = pInformation->ControlState;
if ((ControlState == IN_DATA) || (ControlState == LAST_IN_DATA))
{
DataStageIn();
ControlState = pInformation->ControlState;
}
else if (ControlState == WAIT_STATUS_IN)
{
if ((pInformation->USBbRequest == SET_ADDRESS) &&
(Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)))
{
SetDeviceAddress(pInformation->USBwValue0);
pUser_Standard_Requests->User_SetDeviceAddress();
}
(*pProperty->Process_Status_IN)();
ControlState = STALLED;
}
else
{
ControlState = STALLED;
}
pInformation->ControlState = ControlState;
return Post0_Process();
}
/*********************************************************************
* @fn Out0_Process
*
* @brief Process the OUT token on all default endpoint.
*
* @return none
*/
uint8_t Out0_Process(void)
{
uint32_t ControlState = pInformation->ControlState;
if ((ControlState == IN_DATA) || (ControlState == LAST_IN_DATA))
{
ControlState = STALLED;
}
else if ((ControlState == OUT_DATA) || (ControlState == LAST_OUT_DATA))
{
DataStageOut();
ControlState = pInformation->ControlState;
}
else if (ControlState == WAIT_STATUS_OUT)
{
(*pProperty->Process_Status_OUT)();
ControlState = STALLED;
}
else
{
ControlState = STALLED;
}
pInformation->ControlState = ControlState;
return Post0_Process();
}
/*********************************************************************
* @fn Post0_Process
*
* @brief Stall the Endpoint 0 in case of error.
*
* @return 0 - if the control State is in PAUSE
* 1 - if not.
*/
uint8_t Post0_Process(void)
{
SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
if (pInformation->ControlState == STALLED)
{
vSetEPRxStatus(EP_RX_STALL);
vSetEPTxStatus(EP_TX_STALL);
}
return (pInformation->ControlState == PAUSE);
}
/*********************************************************************
* @fn SetDeviceAddress
*
* @brief Set the device and all the used Endpoints addresses.
*
* @param Val - device address.
*
* @return none
*/
void SetDeviceAddress(uint8_t Val)
{
uint32_t i;
uint32_t nEP = Device_Table.Total_Endpoint;
for (i = 0; i < nEP; i++)
{
_SetEPAddress((uint8_t)i, (uint8_t)i);
}
_SetDADDR(Val | DADDR_EF);
}
/*********************************************************************
* @fn NOP_Process
*
* @brief No operation function.
*
* @return none
*/
void NOP_Process(void)
{
}

View File

@@ -0,0 +1,42 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_init.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : Initialization routines & global variables
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "usb_lib.h"
uint8_t EPindex;
DEVICE_INFO *pInformation;
DEVICE_PROP *pProperty;
uint16_t SaveState ;
uint16_t wInterrupt_Mask;
DEVICE_INFO Device_Info;
USER_STANDARD_REQUESTS *pUser_Standard_Requests;
/*******************************************************************************
* @fn USB_Init
*
* @brief USB system initialization
*
* @return None.
*
*/
void USB_Init(void)
{
pInformation = &Device_Info;
pInformation->ControlState = 2;
pProperty = &Device_Property;
pUser_Standard_Requests = &User_Standard_Requests;
pProperty->Init();
}

View File

@@ -0,0 +1,133 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_int.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : Endpoint CTR (Low and High) interrupt's service routines
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "../../driver/inc/usb_lib.h"
/* Private variables */
__IO uint16_t SaveRState;
__IO uint16_t SaveTState;
/* Extern variables */
extern void (*pEpInt_IN[7])(void); /* Handles IN interrupts */
extern void (*pEpInt_OUT[7])(void); /* Handles OUT interrupts */
/*******************************************************************************
* @fn CTR_LP.
*
* @brief Low priority Endpoint Correct Transfer interrupt's service
* routine.
*
* @return None.
*/
void CTR_LP(void)
{
__IO uint16_t wEPVal = 0;
while (((wIstr = _GetISTR()) & ISTR_CTR) != 0)
{
EPindex = (uint8_t)(wIstr & ISTR_EP_ID);
if (EPindex == 0)
{
SaveRState = _GetENDPOINT(ENDP0);
SaveTState = SaveRState & EPTX_STAT;
SaveRState &= EPRX_STAT;
_SetEPRxTxStatus(ENDP0,EP_RX_NAK,EP_TX_NAK);
if ((wIstr & ISTR_DIR) == 0)
{
_ClearEP_CTR_TX(ENDP0);
In0_Process();
_SetEPRxTxStatus(ENDP0,SaveRState,SaveTState);
return;
}
else
{
wEPVal = _GetENDPOINT(ENDP0);
if ((wEPVal &EP_SETUP) != 0)
{
_ClearEP_CTR_RX(ENDP0);
Setup0_Process();
_SetEPRxTxStatus(ENDP0,SaveRState,SaveTState);
return;
}
else if ((wEPVal & EP_CTR_RX) != 0)
{
_ClearEP_CTR_RX(ENDP0);
Out0_Process();
_SetEPRxTxStatus(ENDP0,SaveRState,SaveTState);
return;
}
}
}
else
{
wEPVal = _GetENDPOINT(EPindex);
if ((wEPVal & EP_CTR_RX) != 0)
{
_ClearEP_CTR_RX(EPindex);
(*pEpInt_OUT[EPindex-1])();
}
if ((wEPVal & EP_CTR_TX) != 0)
{
_ClearEP_CTR_TX(EPindex);
(*pEpInt_IN[EPindex-1])();
}
}
}
}
/*******************************************************************************
* @fn CTR_HP.
*
* @brief High Priority Endpoint Correct Transfer interrupt's service
* routine.
*
* @return None.
*/
void CTR_HP(void)
{
uint32_t wEPVal = 0;
while (((wIstr = _GetISTR()) & ISTR_CTR) != 0)
{
_SetISTR((uint16_t)CLR_CTR);
EPindex = (uint8_t)(wIstr & ISTR_EP_ID);
wEPVal = _GetENDPOINT(EPindex);
if ((wEPVal & EP_CTR_RX) != 0)
{
_ClearEP_CTR_RX(EPindex);
(*pEpInt_OUT[EPindex-1])();
}
else if ((wEPVal & EP_CTR_TX) != 0)
{
_ClearEP_CTR_TX(EPindex);
(*pEpInt_IN[EPindex-1])();
}
}
}

View File

@@ -0,0 +1,74 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_mem.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : Utility functions for memory transfers to/from PMA
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "../../driver/inc/usb_lib.h"
/*******************************************************************************
* @fn UserToPMABufferCopy
*
* @brief Copy a buffer from user memory area to packet memory area (PMA)
*
* @param pbUsrBuf: pointer to user memory area.
* wPMABufAddr: address into PMA.
* wNBytes: no. of bytes to be copied.
*
* @param None .
*/
void UserToPMABufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
{
uint32_t n = (wNBytes + 1) >> 1;
uint32_t i, temp1, temp2;
uint16_t *pdwVal;
pdwVal = (uint16_t *)(wPMABufAddr * 2 + PMAAddr);
for (i = n; i != 0; i--)
{
temp1 = (uint16_t) * pbUsrBuf;
pbUsrBuf++;
temp2 = temp1 | (uint16_t) * pbUsrBuf << 8;
*pdwVal++ = temp2;
pdwVal++;
pbUsrBuf++;
}
}
/*******************************************************************************
* @fn PMAToUserBufferCopy
*
* @brief Copy a buffer from user memory area to packet memory area (PMA)
*
* @param pbUsrBuf: pointer to user memory area.
* wPMABufAddr: address into PMA.
* wNBytes: no. of bytes to be copied.
*
* @param None.
*/
void PMAToUserBufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
{
uint32_t n = (wNBytes + 1) >> 1;
uint32_t i;
uint32_t *pdwVal;
pdwVal = (uint32_t *)(wPMABufAddr * 2 + PMAAddr);
for (i = n; i != 0; i--)
{
*(uint16_t*)pbUsrBuf++ = *pdwVal++;
pbUsrBuf++;
}
}

View File

@@ -0,0 +1,852 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_regs.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : Interface functions to USB cell registers
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "../../driver/inc/usb_lib.h"
/*******************************************************************************
* @fn SetCNTR.
*
* @brief Set the CNTR register value.
*
* @param wRegValue: new register value.
*
* @return None.
*/
void SetCNTR(uint16_t wRegValue)
{
_SetCNTR(wRegValue);
}
/*******************************************************************************
* @fn GetCNTR.
*
* @brief returns the CNTR register value.
*
* @param None.
*
* @return CNTR register Value.
*/
uint16_t GetCNTR(void)
{
return(_GetCNTR());
}
/*******************************************************************************
* @fn SetISTR.
*
* @brief Set the ISTR register value.
*
* @param wRegValue: new register value.
*
* @return None.
*/
void SetISTR(uint16_t wRegValue)
{
_SetISTR(wRegValue);
}
/*******************************************************************************
* @fn GetISTR
*
* @brief Returns the ISTR register value.
*
* @param None.
*
* @return ISTR register Value
*/
uint16_t GetISTR(void)
{
return(_GetISTR());
}
/*******************************************************************************
* @fn GetFNR
*
* @brief Returns the FNR register value.
*
* @param None.
*
* @return FNR register Value
*/
uint16_t GetFNR(void)
{
return(_GetFNR());
}
/*******************************************************************************
* @fn SetDADDR
*
* @brief Set the DADDR register value.
*
* @param wRegValue: new register value.
*
* @return None.
*/
void SetDADDR(uint16_t wRegValue)
{
_SetDADDR(wRegValue);
}
/*******************************************************************************
* @fn GetDADDR
*
* @brief Returns the DADDR register value.
*
* @return DADDR register Value
*
*/
uint16_t GetDADDR(void)
{
return(_GetDADDR());
}
/*******************************************************************************
* @fn SetBTABLE
*
* @brief Set the BTABLE.
*
* @param wRegValue: New register value.
*
* @return None.
*/
void SetBTABLE(uint16_t wRegValue)
{
_SetBTABLE(wRegValue);
}
/*******************************************************************************
* @fn GetBTABLE.
*
* @param Returns the BTABLE register value.
*
* @return BTABLE address.
*/
uint16_t GetBTABLE(void)
{
return(_GetBTABLE());
}
/*******************************************************************************
* @fn SetENDPOINT
*
* @brief Set the Endpoint register value.
*
* @param bEpNum: Endpoint Number.
* wRegValue.
*
* @return None.
*/
void SetENDPOINT(uint8_t bEpNum, uint16_t wRegValue)
{
_SetENDPOINT(bEpNum, wRegValue);
}
/*******************************************************************************
* @fn GetENDPOINT
*
* @brief Return the Endpoint register value.
*
* @param bEpNum: Endpoint Number.
*
* @return Endpoint register value.
*/
uint16_t GetENDPOINT(uint8_t bEpNum)
{
return(_GetENDPOINT(bEpNum));
}
/*******************************************************************************
* @fn SetEPType
*
* @brief sets the type in the endpoint register.
*
* @param bEpNum: Endpoint Number.
* wType: type definition.
*
* @return None.
*/
void SetEPType(uint8_t bEpNum, uint16_t wType)
{
_SetEPType(bEpNum, wType);
}
/*******************************************************************************
* @fn GetEPType
*
* @brief Returns the endpoint type.
*
* @param bEpNum: Endpoint Number.
*
* @return Endpoint Type
*/
uint16_t GetEPType(uint8_t bEpNum)
{
return(_GetEPType(bEpNum));
}
/*******************************************************************************
* @fn SetEPTxStatus
*
* @brief Set the status of Tx endpoint.
*
* @param bEpNum: Endpoint Number.
* wState: new state.
*
* @return None.
*/
void SetEPTxStatus(uint8_t bEpNum, uint16_t wState)
{
_SetEPTxStatus(bEpNum, wState);
}
/*******************************************************************************
* @fn SetEPRxStatus
*
* @brief Set the status of Rx endpoint.
*
* @param bEpNum: Endpoint Number.
* wState: new state.
*
* @return None.
*/
void SetEPRxStatus(uint8_t bEpNum, uint16_t wState)
{
_SetEPRxStatus(bEpNum, wState);
}
/*******************************************************************************
* @fn SetDouBleBuffEPStall
*
* @brief sets the status for Double Buffer Endpoint to STALL
*
* @param bEpNum: Endpoint Number.
* bDir: Endpoint direction.
*
* @return None.
*/
void SetDouBleBuffEPStall(uint8_t bEpNum, uint8_t bDir)
{
uint16_t Endpoint_DTOG_Status;
Endpoint_DTOG_Status = GetENDPOINT(bEpNum);
if (bDir == EP_DBUF_OUT)
{
_SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPRX_DTOG1);
}
else if (bDir == EP_DBUF_IN)
{
_SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPTX_DTOG1);
}
}
/*******************************************************************************
* @fn GetEPTxStatus
*
* @brief Returns the endpoint Tx status.
*
* @param bEpNum: Endpoint Number.
*
* @return Endpoint TX Status
*/
uint16_t GetEPTxStatus(uint8_t bEpNum)
{
return(_GetEPTxStatus(bEpNum));
}
/*******************************************************************************
* @fn GetEPRxStatus
*
* @brief Returns the endpoint Rx status.
*
* @param bEpNum: Endpoint Number.
*
* @return Endpoint RX Status
*/
uint16_t GetEPRxStatus(uint8_t bEpNum)
{
return(_GetEPRxStatus(bEpNum));
}
/*******************************************************************************
* @fn SetEPTxValid
*
* @brief Valid the endpoint Tx Status.
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void SetEPTxValid(uint8_t bEpNum)
{
_SetEPTxStatus(bEpNum, EP_TX_VALID);
}
/*******************************************************************************
* @fn SetEPRxValid
*
* @brief Valid the endpoint Rx Status.
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void SetEPRxValid(uint8_t bEpNum)
{
_SetEPRxStatus(bEpNum, EP_RX_VALID);
}
/*******************************************************************************
* @fn SetEP_KIND
*
* @brief Clear the EP_KIND bit.
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void SetEP_KIND(uint8_t bEpNum)
{
_SetEP_KIND(bEpNum);
}
/*******************************************************************************
* @fn ClearEP_KIND
*
* @brief set the EP_KIND bit.
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void ClearEP_KIND(uint8_t bEpNum)
{
_ClearEP_KIND(bEpNum);
}
/*******************************************************************************
* @fn Clear_Status_Out
*
* @brief Clear the Status Out of the related Endpoint
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void Clear_Status_Out(uint8_t bEpNum)
{
_ClearEP_KIND(bEpNum);
}
/*******************************************************************************
* @fn Set_Status_Out
*
* @brief Set the Status Out of the related Endpoint
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void Set_Status_Out(uint8_t bEpNum)
{
_SetEP_KIND(bEpNum);
}
/*******************************************************************************
* @fn SetEPDoubleBuff
*
* @brief Enable the double buffer feature for the endpoint.
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void SetEPDoubleBuff(uint8_t bEpNum)
{
_SetEP_KIND(bEpNum);
}
/*******************************************************************************
* @fn ClearEPDoubleBuff
*
* @brief Disable the double buffer feature for the endpoint.
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void ClearEPDoubleBuff(uint8_t bEpNum)
{
_ClearEP_KIND(bEpNum);
}
/*******************************************************************************
* @fn GetTxStallStatus
*
* @brief Returns the Stall status of the Tx endpoint.
*
* @param bEpNum: Endpoint Number.
*
* @return Tx Stall status.
*/
uint16_t GetTxStallStatus(uint8_t bEpNum)
{
return(_GetTxStallStatus(bEpNum));
}
/*******************************************************************************
* @fn GetRxStallStatus
*
* @brief Returns the Stall status of the Rx endpoint.
*
* @param bEpNum: Endpoint Number.
*
* @return Rx Stall status.
*/
uint16_t GetRxStallStatus(uint8_t bEpNum)
{
return(_GetRxStallStatus(bEpNum));
}
/*******************************************************************************
* @fn ClearEP_CTR_RX
*
* @brief Clear the CTR_RX bit.
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void ClearEP_CTR_RX(uint8_t bEpNum)
{
_ClearEP_CTR_RX(bEpNum);
}
/*******************************************************************************
* @fn ClearEP_CTR_TX
*
* @brief Clear the CTR_TX bit.
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void ClearEP_CTR_TX(uint8_t bEpNum)
{
_ClearEP_CTR_TX(bEpNum);
}
/*******************************************************************************
* @fn ToggleDTOG_RX
*
* @brief Toggle the DTOG_RX bit.
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void ToggleDTOG_RX(uint8_t bEpNum)
{
_ToggleDTOG_RX(bEpNum);
}
/*******************************************************************************
* @fn ToggleDTOG_TX
*
* @brief Toggle the DTOG_TX bit.
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void ToggleDTOG_TX(uint8_t bEpNum)
{
_ToggleDTOG_TX(bEpNum);
}
/*******************************************************************************
* @fn ClearDTOG_RX.
*
* @brief Clear the DTOG_RX bit.
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void ClearDTOG_RX(uint8_t bEpNum)
{
_ClearDTOG_RX(bEpNum);
}
/*******************************************************************************
* @fn ClearDTOG_TX.
*
* @brief Clear the DTOG_TX bit.
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
void ClearDTOG_TX(uint8_t bEpNum)
{
_ClearDTOG_TX(bEpNum);
}
/*******************************************************************************
* @fn SetEPAddress
*
* @brief Set the endpoint address.
*
* @param bEpNum: Endpoint Number.
* bAddr: New endpoint address.
*
* @return None.
*/
void SetEPAddress(uint8_t bEpNum, uint8_t bAddr)
{
_SetEPAddress(bEpNum, bAddr);
}
/*******************************************************************************
* @fn GetEPAddress
*
* @brief Get the endpoint address.
*
* @param bEpNum: Endpoint Number.
*
* @return Endpoint address.
*/
uint8_t GetEPAddress(uint8_t bEpNum)
{
return(_GetEPAddress(bEpNum));
}
/*******************************************************************************
* @fn SetEPTxAddr
*
* @brief Set the endpoint Tx buffer address.
*
* @param bEpNum: Endpoint Number.
* wAddr: new address.
*
* @return None.
*/
void SetEPTxAddr(uint8_t bEpNum, uint16_t wAddr)
{
_SetEPTxAddr(bEpNum, wAddr);
}
/*******************************************************************************
* @fn SetEPRxAddr
*
* @brief Set the endpoint Rx buffer address.
*
* @param bEpNum: Endpoint Number.
* wAddr: new address.
*
* @return None.
*/
void SetEPRxAddr(uint8_t bEpNum, uint16_t wAddr)
{
_SetEPRxAddr(bEpNum, wAddr);
}
/*******************************************************************************
* @fn GetEPTxAddr
*
* @brief Returns the endpoint Tx buffer address.
*
* @param bEpNum: Endpoint Number.
*
* @return Rx buffer address.
*/
uint16_t GetEPTxAddr(uint8_t bEpNum)
{
return(_GetEPTxAddr(bEpNum));
}
/*******************************************************************************
* @fn GetEPRxAddr.
*
* @brief Returns the endpoint Rx buffer address.
*
* @param bEpNum: Endpoint Number.
*
* @returnRx buffer address.
*/
uint16_t GetEPRxAddr(uint8_t bEpNum)
{
return(_GetEPRxAddr(bEpNum));
}
/*******************************************************************************
* @fn SetEPTxCount.
*
* @brief Set the Tx count.
*
* @param bEpNum: Endpoint Number.
* wCount: new count value.
*
* @return None.
*/
void SetEPTxCount(uint8_t bEpNum, uint16_t wCount)
{
_SetEPTxCount(bEpNum, wCount);
}
/*******************************************************************************
* @fn SetEPCountRxReg.
*
* @brief Set the Count Rx Register value.
*
* @param *pdwReg: point to the register.
* wCount: the new register value.
*
* @return None.
*/
void SetEPCountRxReg(uint32_t *pdwReg, uint16_t wCount)
{
_SetEPCountRxReg(dwReg, wCount);
}
/*******************************************************************************
* @fn SetEPRxCount
*
* @brief Set the Rx count.
*
* @param bEpNum: Endpoint Number.
* wCount: the new count value.
*
* @return None.
*/
void SetEPRxCount(uint8_t bEpNum, uint16_t wCount)
{
_SetEPRxCount(bEpNum, wCount);
}
/*******************************************************************************
* @fn GetEPTxCount
*
* @brief Get the Tx count.
*
* @param bEpNum: Endpoint Number.
*
* @return Tx count value.
*/
uint16_t GetEPTxCount(uint8_t bEpNum)
{
return(_GetEPTxCount(bEpNum));
}
/*******************************************************************************
* @fn GetEPRxCount
*
* @brief Get the Rx count.
*
* @param bEpNum: Endpoint Number.
*
* @return Rx count value.
*/
uint16_t GetEPRxCount(uint8_t bEpNum)
{
return(_GetEPRxCount(bEpNum));
}
/*******************************************************************************
* @fn SetEPDblBuffAddr
*
* @brief Set the addresses of the buffer 0 and 1.
*
* @param bEpNum: Endpoint Number.
* wBuf0Addr: new address of buffer 0.
* wBuf1Addr: new address of buffer 1.
*
* @return None.
*/
void SetEPDblBuffAddr(uint8_t bEpNum, uint16_t wBuf0Addr, uint16_t wBuf1Addr)
{
_SetEPDblBuffAddr(bEpNum, wBuf0Addr, wBuf1Addr);
}
/*******************************************************************************
* @fn SetEPDblBuf0Addr
*
* @brief Set the Buffer 1 address.
*
* @param bEpNum: Endpoint Number
* wBuf0Addr: new address.
*
* @return None.
*/
void SetEPDblBuf0Addr(uint8_t bEpNum, uint16_t wBuf0Addr)
{
_SetEPDblBuf0Addr(bEpNum, wBuf0Addr);
}
/*******************************************************************************
* @fn SetEPDblBuf1Addr
*
* @brief Set the Buffer 1 address.
*
* @param bEpNum: Endpoint Number
* wBuf1Addr: new address.
*
* @return None.
*/
void SetEPDblBuf1Addr(uint8_t bEpNum, uint16_t wBuf1Addr)
{
_SetEPDblBuf1Addr(bEpNum, wBuf1Addr);
}
/*******************************************************************************
* @fn GetEPDblBuf0Addr
*
* @brief Returns the address of the Buffer 0.
*
* @param bEpNum: Endpoint Number.
*
* @return None.
*/
uint16_t GetEPDblBuf0Addr(uint8_t bEpNum)
{
return(_GetEPDblBuf0Addr(bEpNum));
}
/*******************************************************************************
* @fn GetEPDblBuf1Addr
*
* @brief Returns the address of the Buffer 1.
*
* @param bEpNum: Endpoint Number.
*
* @return Address of the Buffer 1.
*/
uint16_t GetEPDblBuf1Addr(uint8_t bEpNum)
{
return(_GetEPDblBuf1Addr(bEpNum));
}
/*******************************************************************************
* @fn SetEPDblBuffCount
*
* @brief Set the number of bytes for a double Buffer
* endpoint.
*
* @param bEpNum,bDir, wCount
*
* @return None.
*/
void SetEPDblBuffCount(uint8_t bEpNum, uint8_t bDir, uint16_t wCount)
{
_SetEPDblBuffCount(bEpNum, bDir, wCount);
}
/*******************************************************************************
* @fn SetEPDblBuf0Count
*
* @brief Set the number of bytes in the buffer 0 of a double Buffer
* endpoint.
*
* @param bEpNum, bDir, wCount
*
* @return None.
*/
void SetEPDblBuf0Count(uint8_t bEpNum, uint8_t bDir, uint16_t wCount)
{
_SetEPDblBuf0Count(bEpNum, bDir, wCount);
}
/*******************************************************************************
* @fn SetEPDblBuf1Count
*
* @brief Set the number of bytes in the buffer 0 of a double Buffer
* endpoint.
*
* @param bEpNum, bDir, wCount
*
* @return None.
*/
void SetEPDblBuf1Count(uint8_t bEpNum, uint8_t bDir, uint16_t wCount)
{
_SetEPDblBuf1Count(bEpNum, bDir, wCount);
}
/*******************************************************************************
* @fn GetEPDblBuf0Count
*
* @brief Returns the number of byte received in the buffer 0 of a double
* Buffer endpoint.
*
* @param bEpNum: Endpoint Number.
*
* @return Endpoint Buffer 0 count
*/
uint16_t GetEPDblBuf0Count(uint8_t bEpNum)
{
return(_GetEPDblBuf0Count(bEpNum));
}
/*******************************************************************************
* @fn GetEPDblBuf1Count
*
* @brief Returns the number of data received in the buffer 1 of a double
* Buffer endpoint.
*
* @param bEpNum: Endpoint Number.
*
* @return Endpoint Buffer 1 count.
*/
uint16_t GetEPDblBuf1Count(uint8_t bEpNum)
{
return(_GetEPDblBuf1Count(bEpNum));
}
/*******************************************************************************
* @fn GetEPDblBufDir
*
* @brief gets direction of the double buffered endpoint
*
* @param bEpNum: Endpoint Number.
*
* @return EP_DBUF_OUT, EP_DBUF_IN,
* EP_DBUF_ERR if the endpoint counter not yet programmed.
*/
EP_DBUF_DIR GetEPDblBufDir(uint8_t bEpNum)
{
if ((uint16_t)(*_pEPRxCount(bEpNum) & 0xFC00) != 0)
return(EP_DBUF_OUT);
else if (((uint16_t)(*_pEPTxCount(bEpNum)) & 0x03FF) != 0)
return(EP_DBUF_IN);
else
return(EP_DBUF_ERR);
}
/*******************************************************************************
* @fn FreeUserBuffer
*
* @brief free buffer used from the application realizing it to the line
toggles bit SW_BUF in the double buffered endpoint register
*
* @param bEpNum, bDir
*
* @return None.
*/
void FreeUserBuffer(uint8_t bEpNum, uint8_t bDir)
{
if (bDir == EP_DBUF_OUT)
{
_ToggleDTOG_TX(bEpNum);
}
else if (bDir == EP_DBUF_IN)
{
_ToggleDTOG_RX(bEpNum);
}
}
/*******************************************************************************
* @fn ToWord
*
* @brief merge two byte in a word.
*
* @param bh: byte high, bl: bytes low.
*
* @return resulted word.
*/
uint16_t ToWord(uint8_t bh, uint8_t bl)
{
uint16_t wRet;
wRet = (uint16_t)bl | ((uint16_t)bh << 8);
return(wRet);
}
/*******************************************************************************
* @fn ByteSwap
*
* @brief Swap two byte in a word.
*
* @param wSwW: word to Swap.
*
* @return resulted word.
*/
uint16_t ByteSwap(uint16_t wSwW)
{
uint8_t bTemp;
uint16_t wRet;
bTemp = (uint8_t)(wSwW & 0xff);
wRet = (wSwW >> 8) | ((uint16_t)bTemp << 8);
return(wRet);
}

View File

@@ -0,0 +1,77 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_sil.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : Simplified Interface Layer for Global Initialization and
* Endpoint Rea/Write operations.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "../../driver/inc/usb_lib.h"
/*******************************************************************************
* @fn USB_SIL_Init
*
* @brief Initialize the USB Device IP and the Endpoint 0.
*
* @return Status.
*/
uint32_t USB_SIL_Init(void)
{
_SetISTR(0);
wInterrupt_Mask = IMR_MSK;
_SetCNTR(wInterrupt_Mask);
return 0;
}
/*******************************************************************************
* @fn USB_SIL_Write
*
* @brief Write a buffer of data to a selected endpoint.
*
* @param bEpAddr: The address of the non control endpoint.
* pBufferPointer: The pointer to the buffer of data to be written
* to the endpoint.
* wBufferSize: Number of data to be written (in bytes).
*
* @return Status.
*/
uint32_t USB_SIL_Write(uint8_t bEpAddr, uint8_t* pBufferPointer, uint32_t wBufferSize)
{
UserToPMABufferCopy(pBufferPointer, GetEPTxAddr(bEpAddr & 0x7F), wBufferSize);
SetEPTxCount((bEpAddr & 0x7F), wBufferSize);
return 0;
}
/*******************************************************************************
* @fn USB_SIL_Read
*
* @brief Write a buffer of data to a selected endpoint.
*
* @param bEpAddr: The address of the non control endpoint.
* pBufferPointer: The pointer to which will be saved the
* received data buffer.
*
* @return Number of received data (in Bytes).
*/
uint32_t USB_SIL_Read(uint8_t bEpAddr, uint8_t* pBufferPointer)
{
uint32_t DataLength = 0;
DataLength = GetEPRxCount(bEpAddr & 0x7F);
PMAToUserBufferCopy(pBufferPointer, GetEPRxAddr(bEpAddr & 0x7F), DataLength);
return DataLength;
}