first commit

This commit is contained in:
YXZhu
2022-08-06 13:35:32 +08:00
commit 2240710d39
1025 changed files with 472118 additions and 0 deletions
@@ -0,0 +1,154 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : hw_config.c
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* Description : USB configuration file.
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "usb_lib.h"
#include "usb_prop.h"
#include "usb_desc.h"
#include "usb_istr.h"
#include "hw_config.h"
#include "usb_pwr.h"
#include "string.h"
#include "stdarg.h"
#include "stdio.h"
/*********************************************************************
* @fn USBWakeUp_IRQHandler
*
* @brief This function handles USB wake up exception.
*
* @return none
*/
void USBWakeUp_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line18);
}
/*********************************************************************
* @fn USBWakeUp_IRQHandler
*
* @brief This function handles USB exception.
*
* @return none
*/
void USB_LP_CAN1_RX0_IRQHandler(void)
{
USB_Istr();
}
/*********************************************************************
* @fn Set_USBConfig
*
* @brief Set_USBConfig.
*
* @return none
*/
void Set_USBConfig( )
{
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
}
/*********************************************************************
* @fn Enter_LowPowerMode
*
* @brief Enter low power mode.
*
* @return none
*/
void Enter_LowPowerMode(void)
{
printf("usb enter low power mode\r\n");
bDeviceState=SUSPENDED;
}
/*********************************************************************
* @fn Leave_LowPowerMode
*
* @brief Leave low power mode.
*
* @return none
*/
void Leave_LowPowerMode(void)
{
DEVICE_INFO *pInfo=&Device_Info;
printf("leave low power mode\r\n");
if (pInfo->Current_Configuration!=0)bDeviceState=CONFIGURED;
else bDeviceState = ATTACHED;
}
/*********************************************************************
* @fn USB_Interrupts_Config
*
* @brief Configrate USB interrupt.
*
* @return none
*/
void USB_Interrupts_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_ClearITPendingBit(EXTI_Line18);
EXTI_InitStructure.EXTI_Line = EXTI_Line18;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_Init(&NVIC_InitStructure);
}
/*********************************************************************
* @fn USB_Port_Set
*
* @brief Set USB IO port.
*
* @param NewState - DISABLE or ENABLE.
* Pin_In_IPU - Enables or Disables intenal pull-up resistance.
*
* @return none
*/
void USB_Port_Set(FunctionalState NewState, FunctionalState Pin_In_IPU)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
if(NewState) {
_SetCNTR(_GetCNTR()&(~(1<<1)));
GPIOA->CFGHR&=0XFFF00FFF;
GPIOA->OUTDR&=~(3<<11); //PA11/12=0
GPIOA->CFGHR|=0X00088000; // IPD
}
else
{
_SetCNTR(_GetCNTR()|(1<<1));
GPIOA->CFGHR&=0XFFF00FFF;
GPIOA->OUTDR&=~(3<<11); //PA11/12=0
GPIOA->CFGHR|=0X00033000; // LOW
}
if(Pin_In_IPU) (EXTEN->EXTEN_CTR) |= EXTEN_USBD_PU_EN;
else (EXTEN->EXTEN_CTR) &= ~EXTEN_USBD_PU_EN;
}
@@ -0,0 +1,36 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : hw_config.h
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* Description : Configuration file for USB.
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#ifndef __HW_CONFIG_H
#define __HW_CONFIG_H
#include "usb_type.h"
#ifdef DEBUG
#define printf_usb(X...) printf(X)
#else
#define printf_usb(X...)
#endif
void Set_USBConfig(void);
void Enter_LowPowerMode(void);
void Leave_LowPowerMode(void);
void USB_Interrupts_Config(void);
void USB_Port_Set(FunctionalState NewState, FunctionalState Pin_In_IPU);
#endif /* __HW_CONFIG_H */
@@ -0,0 +1,75 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_conf.h
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* Description : This file contains all the functions prototypes for the
* USB configration firmware library.
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#ifndef __USB_CONF_H
#define __USB_CONF_H
#define EP_NUM (4)
/* Buffer Description Table */
/* buffer table base address */
/* buffer table base address */
#define BTABLE_ADDRESS (0x00)
/* EP0 */
/* rx/tx buffer base address */
#define ENDP0_RXADDR (0x40)
#define ENDP0_TXADDR (0x80)
/* EP1 */
/* tx buffer base address */
#define ENDP1_TXADDR (0xC0)
#define ENDP2_TXADDR (0x100)
#define ENDP2_RXADDR (0x110)
#define ENDP3_RXADDR (0x111)
/* ISTR events */
/* IMR_MSK */
/* mask defining which events has to be handled */
/* by the device application software */
#define IMR_MSK (CNTR_CTRM | CNTR_WKUPM | CNTR_SUSPM | CNTR_ERRM | CNTR_SOFM \
| CNTR_ESOFM | CNTR_RESETM )
/*#define CTR_CALLBACK*/
/*#define DOVR_CALLBACK*/
/*#define ERR_CALLBACK*/
/*#define WKUP_CALLBACK*/
/*#define SUSP_CALLBACK*/
/*#define RESET_CALLBACK*/
#define SOF_CALLBACK
/*#define ESOF_CALLBACK*/
/* CTR service routines */
/* associated to defined endpoints */
#define EP1_IN_Callback NOP_Process
/*#define EP2_IN_Callback NOP_Process*/
#define EP3_IN_Callback NOP_Process
#define EP4_IN_Callback NOP_Process
#define EP5_IN_Callback NOP_Process
#define EP6_IN_Callback NOP_Process
#define EP7_IN_Callback NOP_Process
#define EP1_OUT_Callback NOP_Process
/*#define EP2_OUT_Callback NOP_Process*/
#define EP3_OUT_Callback NOP_Process
#define EP4_OUT_Callback NOP_Process
#define EP5_OUT_Callback NOP_Process
#define EP6_OUT_Callback NOP_Process
#define EP7_OUT_Callback NOP_Process
#endif /* __USB_CONF_H */
@@ -0,0 +1,71 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_desc.c
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* Description : Descriptors for Virtual Com Port Demo.
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "usb_lib.h"
#include "usb_desc.h"
/* USB Device Descriptors */
const uint8_t USBD_DeviceDescriptor[] = {
0x12, 0x01, 0x10, 0x01,0xFF, 0x80, 0x55, 0x08,
0x48, 0x43, 0x37, 0x55,
0x00, 0x01, 0x01, 0x02, 0x03, 0x01,
};
/* USB Configration Descriptors */
const uint8_t USBD_ConfigDescriptor[] = {
0x09, 0x02, 0x4A, 0x00, 0x01, 0x01, 0x00, 0x80, 0x32,
0x09, 0x04, 0x00, 0x00, 0x07, 0xFF, 0x80, 0x55, 0x00,
0x07, 0x05, 0x84, 0x02, 0x40, 0x00, 0x00,
0x07, 0x05, 0x04, 0x02, 0x40, 0x00, 0x00,
0x07, 0x05, 0x83, 0x02, 0x40, 0x00, 0x00,
0x07, 0x05, 0x03, 0x02, 0x40, 0x00, 0x00,
0x07, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00,
0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00,
0x07, 0x05, 0x81, 0x02, 0x40, 0x00, 0x00,
0x07, 0x05, 0x01, 0x02, 0x40, 0x00, 0x00,
};
/* USB String Descriptors */
const uint8_t USBD_StringLangID[USBD_SIZE_STRING_LANGID] = {
USBD_SIZE_STRING_LANGID,
USB_STRING_DESCRIPTOR_TYPE,
0x09,
0x04
};
/* USB Device String Vendor */
const uint8_t USBD_StringVendor[USBD_SIZE_STRING_VENDOR] = {
USBD_SIZE_STRING_VENDOR,
USB_STRING_DESCRIPTOR_TYPE,
'W', 0, 'C', 0, 'H', 0, 'I', 0, 'C', 0, 'r', 0, 'o', 0, 'e', 0,
'l', 0, 'e', 0, 'c', 0, 't', 0, 'r', 0, 'o', 0, 'n', 0, 'i', 0,
'c', 0, 's', 0
};
/* USB Device String Product */
const uint8_t USBD_StringProduct[USBD_SIZE_STRING_PRODUCT] = {
USBD_SIZE_STRING_PRODUCT,
USB_STRING_DESCRIPTOR_TYPE,
'W', 0, 'C', 0, 'H', 0, '3', 0, '2', 0, ' ', 0, 'V', 0, 'i', 0,
'r', 0, 't', 0, 'u', 0, 'a', 0, 'l', 0, ' ', 0, 'C', 0, 'O', 0,
'M', 0, ' ', 0, 'P', 0, 'o', 0, 'r', 0, 't', 0, ' ', 0, ' ', 0
};
/* USB Device String Serial */
uint8_t USBD_StringSerial[USBD_SIZE_STRING_SERIAL] = {
USBD_SIZE_STRING_SERIAL,
USB_STRING_DESCRIPTOR_TYPE,
'W', 0, 'C', 0, 'H', 0, '3', 0, '2', 0
};
@@ -0,0 +1,52 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_desc.h
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* Description : This file contains all the functions prototypes for the
* USB description firmware library.
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#ifndef __USB_DESC_H
#define __USB_DESC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "ch32f10x.h"
#define USB_DEVICE_DESCRIPTOR_TYPE 0x01
#define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x02
#define USB_STRING_DESCRIPTOR_TYPE 0x03
#define USB_INTERFACE_DESCRIPTOR_TYPE 0x04
#define USB_ENDPOINT_DESCRIPTOR_TYPE 0x05
#define USBD_DATA_SIZE 64
#define USBD_INT_SIZE 8
#define USBD_SIZE_DEVICE_DESC 18
#define USBD_SIZE_CONFIG_DESC 74
#define USBD_SIZE_STRING_LANGID 4
#define USBD_SIZE_STRING_VENDOR 38
#define USBD_SIZE_STRING_PRODUCT 50
#define USBD_SIZE_STRING_SERIAL 26
#define STANDARD_ENDPOINT_DESC_SIZE 0x09
extern const uint8_t USBD_DeviceDescriptor[USBD_SIZE_DEVICE_DESC];
extern const uint8_t USBD_ConfigDescriptor[USBD_SIZE_CONFIG_DESC];
extern const uint8_t USBD_StringLangID [USBD_SIZE_STRING_LANGID];
extern const uint8_t USBD_StringVendor [USBD_SIZE_STRING_VENDOR];
extern const uint8_t USBD_StringProduct[USBD_SIZE_STRING_PRODUCT];
extern uint8_t USBD_StringSerial [USBD_SIZE_STRING_SERIAL];
#ifdef __cplusplus
}
#endif
#endif /* __USB_DESC_H */
@@ -0,0 +1,85 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_endp.c
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* Description : Endpoint routines
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "usb_lib.h"
#include "usb_desc.h"
#include "usb_mem.h"
#include "hw_config.h"
#include "usb_istr.h"
#include "usb_pwr.h"
/* Interval between sending IN packets in frame number (1 frame = 1ms) */
#define IN_FRAME_INTERVAL 5
/* Private variables */
uint8_t USB_Rx_Buffer[USBD_DATA_SIZE];
u16 USB_Rx_Cnt=0;
/*********************************************************************
* @fn EP2_IN_Callback
*
* @brief Endpoint 2 IN.
*
* @return none
*/
void EP2_IN_Callback (void)
{
UserToPMABufferCopy(USB_Rx_Buffer, ENDP2_TXADDR, USB_Rx_Cnt);
SetEPTxCount(ENDP2, USB_Rx_Cnt);
SetEPTxValid(ENDP2);
}
/*********************************************************************
* @fn EP2_OUT_Callback
*
* @brief Endpoint 2 IN.
*
* @return none
*/
void EP2_OUT_Callback(void)
{
uint8_t i;
USB_Rx_Cnt = USB_SIL_Read(EP2_OUT, USB_Rx_Buffer);
for(i=0;i<USB_Rx_Cnt;i++)
{
USB_Rx_Buffer[i] = ~USB_Rx_Buffer[i];
}
EP2_IN_Callback();
SetEPRxValid(ENDP2);
}
/*********************************************************************
* @fn SOF_Callback
*
* @brief SOF call back.
*
* @return none
*/
void SOF_Callback(void)
{
static uint32_t FrameCount = 0;
if(bDeviceState == CONFIGURED)
{
if (FrameCount++ == IN_FRAME_INTERVAL)
{
FrameCount = 0;
//Handle_USBAsynchXfer();
}
}
}
@@ -0,0 +1,189 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_istr.c
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* Description : ISTR events interrupt service routines
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "usb_lib.h"
#include "usb_prop.h"
#include "usb_pwr.h"
#include "usb_istr.h"
/* Private variables */
__IO uint16_t wIstr;
__IO uint8_t bIntPackSOF = 0;
__IO uint32_t esof_counter =0;
__IO uint32_t wCNTR=0;
/* function pointers to non-control endpoints service routines */
void (*pEpInt_IN[7])(void) ={
EP1_IN_Callback,
EP2_IN_Callback,
EP3_IN_Callback,
EP4_IN_Callback,
EP5_IN_Callback,
EP6_IN_Callback,
EP7_IN_Callback,
};
void (*pEpInt_OUT[7])(void) ={
EP1_OUT_Callback,
EP2_OUT_Callback,
EP3_OUT_Callback,
EP4_OUT_Callback,
EP5_OUT_Callback,
EP6_OUT_Callback,
EP7_OUT_Callback,
};
/*********************************************************************
* @fn USB_Istr
*
* @brief ISTR events interrupt service routine
*
* @return none
*/
void USB_Istr(void)
{
uint32_t i=0;
__IO uint32_t EP[8];
wIstr = _GetISTR();
#if (IMR_MSK & ISTR_SOF)
if (wIstr & ISTR_SOF & wInterrupt_Mask)
{
_SetISTR((uint16_t)CLR_SOF);
bIntPackSOF++;
#ifdef SOF_CALLBACK
SOF_Callback();
#endif
}
#endif
#if (IMR_MSK & ISTR_CTR)
if (wIstr & ISTR_CTR & wInterrupt_Mask)
{
CTR_LP();
#ifdef CTR_CALLBACK
CTR_Callback();
#endif
}
#endif
#if (IMR_MSK & ISTR_RESET)
if (wIstr & ISTR_RESET & wInterrupt_Mask)
{
_SetISTR((uint16_t)CLR_RESET);
Device_Property.Reset();
// SetDeviceAddress(0);
#ifdef RESET_CALLBACK
RESET_Callback();
#endif
}
#endif
#if (IMR_MSK & ISTR_DOVR)
if (wIstr & ISTR_DOVR & wInterrupt_Mask)
{
_SetISTR((uint16_t)CLR_DOVR);
#ifdef DOVR_CALLBACK
DOVR_Callback();
#endif
}
#endif
#if (IMR_MSK & ISTR_ERR)
if (wIstr & ISTR_ERR & wInterrupt_Mask)
{
_SetISTR((uint16_t)CLR_ERR);
#ifdef ERR_CALLBACK
ERR_Callback();
#endif
}
#endif
#if (IMR_MSK & ISTR_WKUP)
if (wIstr & ISTR_WKUP & wInterrupt_Mask)
{
_SetISTR((uint16_t)CLR_WKUP);
Resume(RESUME_EXTERNAL);
#ifdef WKUP_CALLBACK
WKUP_Callback();
#endif
}
#endif
#if (IMR_MSK & ISTR_SUSP)
if (wIstr & ISTR_SUSP & wInterrupt_Mask)
{
if (fSuspendEnabled)
{
Suspend();
}
else
{
Resume(RESUME_LATER);
}
_SetISTR((uint16_t)CLR_SUSP);
#ifdef SUSP_CALLBACK
SUSP_Callback();
#endif
}
#endif
#if (IMR_MSK & ISTR_ESOF)
if (wIstr & ISTR_ESOF & wInterrupt_Mask)
{
_SetISTR((uint16_t)CLR_ESOF);
if ((_GetFNR()&FNR_RXDP)!=0)
{
esof_counter ++;
if ((esof_counter >3)&&((_GetCNTR()&CNTR_FSUSP)==0))
{
wCNTR = _GetCNTR();
for (i=0;i<8;i++) EP[i] = _GetENDPOINT(i);
wCNTR|=CNTR_FRES;
_SetCNTR(wCNTR);
wCNTR&=~CNTR_FRES;
_SetCNTR(wCNTR);
while((_GetISTR()&ISTR_RESET) == 0);
_SetISTR((uint16_t)CLR_RESET);
for (i=0;i<8;i++)
_SetENDPOINT(i, EP[i]);
esof_counter = 0;
}
}
else
{
esof_counter = 0;
}
Resume(RESUME_ESOF);
#ifdef ESOF_CALLBACK
ESOF_Callback();
#endif
}
#endif
} /* USB_Istr */
@@ -0,0 +1,75 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_istr.h
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* Description : This file includes the peripherals header files in the
* user application.
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#ifndef __USB_ISTR_H
#define __USB_ISTR_H
#include "usb_conf.h"
void USB_Istr(void);
void EP1_IN_Callback(void);
void EP2_IN_Callback(void);
void EP3_IN_Callback(void);
void EP4_IN_Callback(void);
void EP5_IN_Callback(void);
void EP6_IN_Callback(void);
void EP7_IN_Callback(void);
void EP1_OUT_Callback(void);
void EP2_OUT_Callback(void);
void EP3_OUT_Callback(void);
void EP4_OUT_Callback(void);
void EP5_OUT_Callback(void);
void EP6_OUT_Callback(void);
void EP7_OUT_Callback(void);
#ifdef CTR_CALLBACK
void CTR_Callback(void);
#endif
#ifdef DOVR_CALLBACK
void DOVR_Callback(void);
#endif
#ifdef ERR_CALLBACK
void ERR_Callback(void);
#endif
#ifdef WKUP_CALLBACK
void WKUP_Callback(void);
#endif
#ifdef SUSP_CALLBACK
void SUSP_Callback(void);
#endif
#ifdef RESET_CALLBACK
void RESET_Callback(void);
#endif
#ifdef SOF_CALLBACK
void SOF_Callback(void);
#endif
#ifdef ESOF_CALLBACK
void ESOF_Callback(void);
#endif
#endif /*__USB_ISTR_H*/
@@ -0,0 +1,338 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_prop.c
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* Description : All processing related to Virtual Com Port Demo
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "usb_lib.h"
#include "usb_conf.h"
#include "usb_prop.h"
#include "usb_desc.h"
#include "usb_pwr.h"
#include "hw_config.h"
uint8_t Request = 0;
DEVICE Device_Table =
{
EP_NUM,
1
};
DEVICE_PROP Device_Property =
{
USBD_init,
USBD_Reset,
USBD_Status_In,
USBD_Status_Out,
USBD_Data_Setup,
USBD_NoData_Setup,
USBD_Get_Interface_Setting,
USBD_GetDeviceDescriptor,
USBD_GetConfigDescriptor,
USBD_GetStringDescriptor,
0,
0x08
};
USER_STANDARD_REQUESTS User_Standard_Requests =
{
USBD_GetConfiguration,
USBD_SetConfiguration,
USBD_GetInterface,
USBD_SetInterface,
USBD_GetStatus,
USBD_ClearFeature,
USBD_SetEndPointFeature,
USBD_SetDeviceFeature,
USBD_SetDeviceAddress
};
ONE_DESCRIPTOR Device_Descriptor =
{
(uint8_t*)USBD_DeviceDescriptor,
USBD_SIZE_DEVICE_DESC
};
ONE_DESCRIPTOR Config_Descriptor =
{
(uint8_t*)USBD_ConfigDescriptor,
USBD_SIZE_CONFIG_DESC
};
ONE_DESCRIPTOR String_Descriptor[4] =
{
{(uint8_t*)USBD_StringLangID, USBD_SIZE_STRING_LANGID},
{(uint8_t*)USBD_StringVendor, USBD_SIZE_STRING_VENDOR},
{(uint8_t*)USBD_StringProduct,USBD_SIZE_STRING_PRODUCT},
{(uint8_t*)USBD_StringSerial, USBD_SIZE_STRING_SERIAL}
};
/*********************************************************************
* @fn USBD_SetConfiguration.
*
* @brief Update the device state to configured.
*
* @return none
*/
void USBD_SetConfiguration(void)
{
DEVICE_INFO *pInfo = &Device_Info;
if (pInfo->Current_Configuration != 0)
{
bDeviceState = CONFIGURED;
}
}
/*********************************************************************
* @fn USBD_SetDeviceAddress.
*
* @brief Update the device state to addressed.
*
* @return none
*/
void USBD_SetDeviceAddress (void)
{
bDeviceState = ADDRESSED;
}
/*********************************************************************
* @fn USBD_Status_In.
*
* @brief USBD Status In Routine.
*
* @return none
*/
void USBD_Status_In(void)
{
}
/*********************************************************************
* @fn USBD_Status_Out
*
* @brief USBD Status OUT Routine.
*
* @return none
*/
void USBD_Status_Out(void)
{
}
/*********************************************************************
* @fn USB_init.
*
* @brief init routine.
*
* @return none
*/
void USBD_init(void)
{
uint8_t i;
pInformation->Current_Configuration = 0;
PowerOn();
for (i=0;i<8;i++) _SetENDPOINT(i,_GetENDPOINT(i) & 0x7F7F & EPREG_MASK);//all clear
_SetISTR((uint16_t)0x00FF);//all clear
USB_SIL_Init();
bDeviceState = UNCONNECTED;
}
/*********************************************************************
* @fn USBD_Reset
*
* @brief Virtual_Com_Port Mouse reset routine
*
* @return none
*/
void USBD_Reset(void)
{
pInformation->Current_Configuration = 0;
pInformation->Current_Feature = USBD_ConfigDescriptor[7];
pInformation->Current_Interface = 0;
SetBTABLE(BTABLE_ADDRESS);
SetEPType(ENDP0, EP_CONTROL);
SetEPTxStatus(ENDP0, EP_TX_STALL);
SetEPRxAddr(ENDP0, ENDP0_RXADDR);
SetEPTxAddr(ENDP0, ENDP0_TXADDR);
Clear_Status_Out(ENDP0);
SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
SetEPRxValid(ENDP0);
_ClearDTOG_RX(ENDP0);
_ClearDTOG_TX(ENDP0);
SetEPType(ENDP1, EP_BULK);
SetEPTxAddr(ENDP1, ENDP1_TXADDR);
SetEPTxStatus(ENDP1, EP_TX_NAK);
SetEPRxStatus(ENDP1, EP_RX_DIS);
_ClearDTOG_RX(ENDP1);
_ClearDTOG_TX(ENDP1);
SetEPType(ENDP2, EP_BULK);
SetEPTxAddr(ENDP2, ENDP2_TXADDR);
SetEPRxAddr(ENDP2, ENDP2_RXADDR);
SetEPRxCount(ENDP2, USBD_DATA_SIZE);
SetEPRxStatus(ENDP2, EP_RX_VALID);
SetEPTxStatus(ENDP2, EP_TX_NAK);
_ClearDTOG_RX(ENDP2);
_ClearDTOG_TX(ENDP2);
SetEPType(ENDP3, EP_INTERRUPT);
SetEPRxAddr(ENDP3, ENDP3_RXADDR);
SetEPRxCount(ENDP3, USBD_DATA_SIZE);
SetEPRxStatus(ENDP3, EP_RX_VALID);
SetEPTxStatus(ENDP3, EP_TX_DIS);
_ClearDTOG_RX(ENDP3);
_ClearDTOG_TX(ENDP3);
SetDeviceAddress(0);
bDeviceState = ATTACHED;
}
/*********************************************************************
* @fn USBD_GetDeviceDescriptor.
*
* @brief Gets the device descriptor.
*
* @param Length.
*
* @return The address of the device descriptor.
*/
uint8_t *USBD_GetDeviceDescriptor(uint16_t Length)
{
return Standard_GetDescriptorData(Length, &Device_Descriptor);
}
/*********************************************************************
* @fn USBD_GetConfigDescriptor.
*
* @brief get the configuration descriptor.
*
* @param Length.
*
* @return The address of the device descriptor.
*/
uint8_t *USBD_GetConfigDescriptor(uint16_t Length)
{
return Standard_GetDescriptorData(Length, &Config_Descriptor);
}
/*********************************************************************
* @fn USBD_GetStringDescriptor
*
* @brief Gets the string descriptors according to the needed index
*
* @param Length.
*
* @return The address of the device descriptor.
*/
uint8_t *USBD_GetStringDescriptor(uint16_t Length)
{
uint8_t wValue0 = pInformation->USBwValue0;
if (wValue0 > 4)
{
return NULL;
}
else
{
return Standard_GetDescriptorData(Length, &String_Descriptor[wValue0]);
}
}
/*********************************************************************
* @fn USBD_Get_Interface_Setting.
*
* @brief test the interface and the alternate setting according to the
* supported one.
*
* @param Interface - interface number.
* AlternateSetting - Alternate Setting number.
*
* @return The address of the device descriptor.
*/
RESULT USBD_Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting)
{
if (AlternateSetting > 0)
{
return USB_UNSUPPORT;
}
else if (Interface > 1)
{
return USB_UNSUPPORT;
}
return USB_SUCCESS;
}
/*********************************************************************
* @fn USBD_Data_Setup
*
* @brief handle the data class specific requests
*
* @param Request Nb.
*
* @return USB_UNSUPPORT or USB_SUCCESS.
*/
RESULT USBD_Data_Setup(uint8_t RequestNo)
{
return USB_SUCCESS;
}
/*********************************************************************
* @fn USBD_NoData_Setup.
*
* @brief handle the no data class specific requests.
*
* @param Request Nb.
*
* @return USB_UNSUPPORT or USB_SUCCESS.
*/
RESULT USBD_NoData_Setup(uint8_t RequestNo)
{
return USB_SUCCESS;
}
/*********************************************************************
* @fn Virtual_Com_Port_GetLineCoding.
*
* @brief send the linecoding structure to the PC host.
*
* @param Length.
*
* @return Linecoding structure base address.
*/
uint8_t *Virtual_Com_Port_GetLineCoding(uint16_t Length)
{
return 0;
}
/*********************************************************************
* @fn Virtual_Com_Port_SetLineCoding.
*
* @brief Set the linecoding structure fields.
*
* @param Length.
*
* @return Linecoding structure base address.
*/
uint8_t *Virtual_Com_Port_SetLineCoding(uint16_t Length)
{
return 0;
}
@@ -0,0 +1,67 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_prop.h
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* Description : All processing related to Virtual COM Port Demo (Endpoint 0)
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#ifndef __usb_prop_H
#define __usb_prop_H
#ifdef __cplusplus
extern "C" {
#endif
#include "ch32f10x.h"
#define USBD_GetConfiguration NOP_Process
//#define USBD_SetConfiguration NOP_Process
#define USBD_GetInterface NOP_Process
#define USBD_SetInterface NOP_Process
#define USBD_GetStatus NOP_Process
#define USBD_ClearFeature NOP_Process
#define USBD_SetEndPointFeature NOP_Process
#define USBD_SetDeviceFeature NOP_Process
//#define USBD_SetDeviceAddress NOP_Process
#define SEND_ENCAPSULATED_COMMAND 0x00
#define GET_ENCAPSULATED_RESPONSE 0x01
#define SET_COMM_FEATURE 0x02
#define GET_COMM_FEATURE 0x03
#define CLEAR_COMM_FEATURE 0x04
#define SET_LINE_CODING 0x20
#define GET_LINE_CODING 0x21
#define SET_CONTROL_LINE_STATE 0x22
#define SEND_BREAK 0x23
void USBD_init(void);
void USBD_Reset(void);
void USBD_SetConfiguration(void);
void USBD_SetDeviceAddress (void);
void USBD_Status_In (void);
void USBD_Status_Out (void);
RESULT USBD_Data_Setup(uint8_t);
RESULT USBD_NoData_Setup(uint8_t);
RESULT USBD_Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting);
uint8_t *USBD_GetDeviceDescriptor(uint16_t );
uint8_t *USBD_GetConfigDescriptor(uint16_t);
uint8_t *USBD_GetStringDescriptor(uint16_t);
uint8_t *USBD_GetLineCoding(uint16_t Length);
uint8_t *USBD_SetLineCoding(uint16_t Length);
#ifdef __cplusplus
}
#endif
#endif /* __usb_prop_H */
@@ -0,0 +1,214 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_pwr.c
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* Description : Connection/disconnection & power management
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "usb_lib.h"
#include "usb_conf.h"
#include "usb_pwr.h"
#include "hw_config.h"
__IO uint32_t bDeviceState = UNCONNECTED;
__IO bool fSuspendEnabled = TRUE;
__IO uint32_t EP[8];
struct
{
__IO RESUME_STATE eState;
__IO uint8_t bESOFcnt;
}
ResumeS;
__IO uint32_t remotewakeupon=0;
/*********************************************************************
* @fn PowerOn
*
* @brief Enable power on.
*
* @return USB_SUCCESS.
*/
RESULT PowerOn(void)
{
uint16_t wRegVal;
wRegVal = CNTR_FRES;
_SetCNTR(wRegVal);
wInterrupt_Mask = 0;
_SetCNTR(wInterrupt_Mask);
_SetISTR(0);
wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM;
_SetCNTR(wInterrupt_Mask);
return USB_SUCCESS;
}
/*********************************************************************
* @fn PowerOff
*
* @brief handles switch-off conditions
*
* @return USB_SUCCESS.
*/
RESULT PowerOff()
{
_SetCNTR(CNTR_FRES);
_SetISTR(0);
_SetCNTR(CNTR_FRES + CNTR_PDWN);
return USB_SUCCESS;
}
/*********************************************************************
* @fn Suspend
*
* @brief sets suspend mode operating conditions
*
* @return USB_SUCCESS.
*/
void Suspend(void)
{
uint32_t i =0;
uint16_t wCNTR;
__IO uint32_t savePWR_CR=0;
wCNTR = _GetCNTR();
for (i=0;i<8;i++) EP[i] = _GetENDPOINT(i);
wCNTR|=CNTR_RESETM;
_SetCNTR(wCNTR);
wCNTR|=CNTR_FRES;
_SetCNTR(wCNTR);
wCNTR&=~CNTR_FRES;
_SetCNTR(wCNTR);
while((_GetISTR()&ISTR_RESET) == 0);
_SetISTR((uint16_t)CLR_RESET);
for (i=0;i<8;i++)
_SetENDPOINT(i, EP[i]);
wCNTR |= CNTR_FSUSP;
_SetCNTR(wCNTR);
wCNTR = _GetCNTR();
wCNTR |= CNTR_LPMODE;
_SetCNTR(wCNTR);
Enter_LowPowerMode();
}
/*********************************************************************
* @fn Resume_Init
*
* @brief Handles wake-up restoring normal operations
*
* @return USB_SUCCESS.
*/
void Resume_Init(void)
{
uint16_t wCNTR;
wCNTR = _GetCNTR();
wCNTR &= (~CNTR_LPMODE);
_SetCNTR(wCNTR);
Leave_LowPowerMode();
_SetCNTR(IMR_MSK);
}
/*********************************************************************
* @fn Resume
*
* @brief This is the state machine handling resume operations and
* timing sequence. The control is based on the Resume structure
* variables and on the ESOF interrupt calling this subroutine
* without changing machine state.
*
* @param a state machine value (RESUME_STATE)
* RESUME_ESOF doesn't change ResumeS.eState allowing
* decrementing of the ESOF counter in different states.
*
* @return none
*/
void Resume(RESUME_STATE eResumeSetVal)
{
uint16_t wCNTR;
if (eResumeSetVal != RESUME_ESOF)
{
ResumeS.eState = eResumeSetVal;
}
switch (ResumeS.eState)
{
case RESUME_EXTERNAL:
if (remotewakeupon ==0)
{
Resume_Init();
ResumeS.eState = RESUME_OFF;
}
else
{
ResumeS.eState = RESUME_ON;
}
break;
case RESUME_INTERNAL:
Resume_Init();
ResumeS.eState = RESUME_START;
remotewakeupon = 1;
break;
case RESUME_LATER:
ResumeS.bESOFcnt = 2;
ResumeS.eState = RESUME_WAIT;
break;
case RESUME_WAIT:
ResumeS.bESOFcnt--;
if (ResumeS.bESOFcnt == 0)
ResumeS.eState = RESUME_START;
break;
case RESUME_START:
wCNTR = _GetCNTR();
wCNTR |= CNTR_RESUME;
_SetCNTR(wCNTR);
ResumeS.eState = RESUME_ON;
ResumeS.bESOFcnt = 10;
break;
case RESUME_ON:
ResumeS.bESOFcnt--;
if (ResumeS.bESOFcnt == 0)
{
wCNTR = _GetCNTR();
wCNTR &= (~CNTR_RESUME);
_SetCNTR(wCNTR);
ResumeS.eState = RESUME_OFF;
remotewakeupon = 0;
}
break;
case RESUME_OFF:
case RESUME_ESOF:
default:
ResumeS.eState = RESUME_OFF;
break;
}
}
@@ -0,0 +1,60 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_pwr.h
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* Description : Connection/disconnection & power management header
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#ifndef __USB_PWR_H
#define __USB_PWR_H
#ifdef __cplusplus
extern "C" {
#endif
#include "ch32f10x.h"
typedef enum _RESUME_STATE
{
RESUME_EXTERNAL,
RESUME_INTERNAL,
RESUME_LATER,
RESUME_WAIT,
RESUME_START,
RESUME_ON,
RESUME_OFF,
RESUME_ESOF
} RESUME_STATE;
typedef enum _DEVICE_STATE
{
UNCONNECTED,
ATTACHED,
POWERED,
SUSPENDED,
ADDRESSED,
CONFIGURED
} DEVICE_STATE;
void Suspend(void);
void Resume_Init(void);
void Resume(RESUME_STATE eResumeSetVal);
RESULT PowerOn(void);
RESULT PowerOff(void);
extern __IO uint32_t bDeviceState; /* USB device status */
extern __IO bool fSuspendEnabled; /* true when suspend is possible */
#ifdef __cplusplus
}
#endif
#endif /*__USB_PWR_H*/