first submission

This commit is contained in:
qiyongshuang
2023-02-01 14:19:13 +08:00
parent d534aad004
commit b79c6ac80d
3711 changed files with 745432 additions and 2 deletions
@@ -0,0 +1,170 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : hw_config.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : USB configuration file.
*********************************************************************************
* 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"
#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"
void USBWakeUp_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void USB_LP_CAN1_RX0_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
/*******************************************************************************
* @fn USBWakeUp_IRQHandler
*
* @brief This function handles USB wake up exception.
*
* @return None
*/
void USBWakeUp_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line18);
}
/*******************************************************************************
* @fn USB_LP_CAN1_RX0_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( )
{
if( SystemCoreClock == 144000000 )
{
RCC_USBCLKConfig( RCC_USBCLKSource_PLLCLK_Div3 );
}
else if( SystemCoreClock == 96000000 )
{
RCC_USBCLKConfig( RCC_USBCLKSource_PLLCLK_Div2 );
}
else if( SystemCoreClock == 48000000 )
{
RCC_USBCLKConfig( RCC_USBCLKSource_PLLCLK_Div1 );
}
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("usb 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|=0X00044000; //float
}
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,38 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : hw_config.h
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : Configuration file for USB.
*********************************************************************************
* 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.
*******************************************************************************/
#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,76 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_conf.h
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : This file contains all the functions prototypes for the
* USB configration firmware library.
*********************************************************************************
* 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.
*******************************************************************************/
#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 ENDP2_TXADDR (0xC0)
#define ENDP3_RXADDR (ENDP2_TXADDR + 0x40)
/* 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_CALLBAC K*/
/* #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,107 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_desc.c
* Author : WCH
* Version : V1.0.0
* Date : 2022/11/25
* Description : USB Descriptors.
*********************************************************************************
* 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"
#include "usb_desc.h"
/* USB Device Descriptors */
const uint8_t USBD_DeviceDescriptor[] =
{
0x12, // bLength
0x01, // bDescriptorType (Device)
0x00, 0x02, // bcdUSB 2.00
0x00, // bDeviceClass (Use class information in the Interface Descriptors)
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
0x40, // bMaxPacketSize0 64
0x86, 0x1A, // idVendor 0x1A86
0x11, 0xFE, // idProduct 0xFE11
DEF_PROG_VERSION, 0x00, // bcdDevice 0.00
0x01, // iManufacturer (String Index)
0x02, // iProduct (String Index)
0x00, // iSerialNumber (String Index)
0x01, // bNumConfigurations 1
};
/* USB Configration Descriptors */
const uint8_t USBD_ConfigDescriptor[] =
{
/* Configuration Descriptor */
0x09, // bLength
0x02, // bDescriptorType (Configuration)
0x20, 0x00, // wTotalLength 32
0x01, // bNumInterfaces 1
0x01, // bConfigurationValue
0x00, // iConfiguration (String Index)
0x80, // bmAttributes
0x32, // bMaxPower 100mA
/*****************************************************************/
/* Interface Descriptor(CDROM) */
0x09, // bLength
0x04, // bDescriptorType (Interface)
0x00, // bInterfaceNumber 0
0x00, // bAlternateSetting
0x02, // bNumEndpoints 2
0x08, // bInterfaceClass
0x06, // bInterfaceSubClass
0x50, // bInterfaceProtocol
0x00, // iInterface (String Index)
/* Endpoint Descriptor */
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x82, // bEndpointAddress (IN/D2H)
0x02, // bmAttributes (Bulk)
0x40, 0x00, // wMaxPacketSize 64
0x00, // bInterval 0 (unit depends on device speed)
/* Endpoint Descriptor */
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x03, // bEndpointAddress (OUT/H2D)
0x02, // bmAttributes (Bulk)
0x40, 0x00, // wMaxPacketSize 64
0x00, // bInterval 0 (unit depends on device speed)
};
/* 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,'.',0,'c',0,'n',0
};
/* USB Device String Product */
const uint8_t USBD_StringProduct[USBD_SIZE_STRING_PRODUCT] = {
USBD_SIZE_STRING_PRODUCT,
USB_STRING_DESCRIPTOR_TYPE,
'C',0,'H',0,'2',0,'0',0,'x',0,'C',0,'D',0,'-',0,'R',0,'O',0,'M',0,
};
/* USB Device String Serial */
const 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,51 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_desc.h
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : This file contains all the functions prototypes for the
* USB description firmware library.
*********************************************************************************
* 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.
*******************************************************************************/
#ifndef __USB_DESC_H
#define __USB_DESC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "ch32v20x.h"
#define DEF_PROG_VERSION 0x01
#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 DEF_USBD_UEP0_SIZE 64
#define DEF_USBD_MAX_PACK_SIZE 64
#define USBD_SIZE_DEVICE_DESC 18
#define USBD_SIZE_CONFIG_DESC 32
#define USBD_SIZE_STRING_LANGID 4
#define USBD_SIZE_STRING_VENDOR 14
#define USBD_SIZE_STRING_PRODUCT 24
#define USBD_SIZE_STRING_SERIAL 26
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 const uint8_t USBD_StringSerial [USBD_SIZE_STRING_SERIAL];
#ifdef __cplusplus
}
#endif
#endif /* __USB_DESC_H */
@@ -0,0 +1,48 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_endp.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : Endpoint 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 <SW_CDROM.h>
#include "usb_lib.h"
#include "usb_desc.h"
#include "usb_mem.h"
#include "hw_config.h"
#include "usb_istr.h"
#include "usb_pwr.h"
#include "usb_prop.h"
uint16_t USB_Rx_Cnt = 0;
__attribute__ ((aligned(4))) uint8_t CDROM_EP3_Pack_Buffer[DEF_CDROM_PACK_64];
/*********************************************************************
* @fn EP2_IN_Callback
*
* @brief Endpoint 2 IN.
*
* @return none
*/
void EP2_IN_Callback (void)
{
CDROM_In_EP_Deal( );
}
/*********************************************************************
* @fn EP3_OUT_Callback
*
* @brief Endpoint 3 OUT.
*
* @return none
*/
void EP3_OUT_Callback (void)
{
USB_Rx_Cnt = USB_SIL_Read(ENDP3, CDROM_EP3_Pack_Buffer);
CDROM_Out_EP_Deal( CDROM_EP3_Pack_Buffer, USB_Rx_Cnt );
SetEPRxValid(ENDP3);
}
@@ -0,0 +1,196 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_istr.c
* Author : WCH
* Version : V1.0.1
* Date : 2022/12/28
* Description : ISTR events interrupt 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 "usb_lib.h"
#include "usb_prop.h"
#include "usb_pwr.h"
#include "usb_istr.h"
uint16_t Ep0RxBlks;
/* 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];
if ((*_pEPRxCount(0) & 0xFC00 )!= Ep0RxBlks)
{
*_pEPRxCount(0) |= (Ep0RxBlks & 0xFC00);
}
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();
#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,77 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_istr.h
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : This file includes the peripherals header files in the
* user application.
*********************************************************************************
* 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.
*******************************************************************************/
#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,358 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_prop.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : USB Processing
*********************************************************************************
* 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"
#include "usb_conf.h"
#include "usb_prop.h"
#include "usb_desc.h"
#include "usb_pwr.h"
#include "hw_config.h"
uint8_t Request = 0;
volatile uint8_t CDROM_Max_Lun = 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,
DEF_USBD_UEP0_SIZE
};
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_SetDeviceFeature.
*
* @brief SetDeviceFeature Routine.
*
* @return none
*/
void USBD_SetDeviceFeature (void)
{
}
/*********************************************************************
* @fn USBD_ClearFeature.
*
* @brief ClearFeature Routine.
*
* @return none
*/
void USBD_ClearFeature(void)
{
}
/*********************************************************************
* @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;
USB_Port_Set(DISABLE, DISABLE);
Delay_Ms(20);
USB_Port_Set(ENABLE, ENABLE);
}
/*******************************************************************************
* @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(ENDP2, EP_BULK);
SetEPTxAddr(ENDP2, ENDP2_TXADDR);
SetEPTxStatus(ENDP2, EP_TX_NAK);
SetEPRxStatus(ENDP2,EP_RX_DIS);
_ClearDTOG_TX(ENDP2);
_ClearDTOG_RX(ENDP2);
SetEPType(ENDP3, EP_BULK);
SetEPTxStatus(ENDP3, EP_TX_DIS);
SetEPRxAddr(ENDP3, ENDP3_RXADDR);
SetEPRxCount(ENDP3, DEF_USBD_MAX_PACK_SIZE);
SetEPRxStatus(ENDP3, EP_RX_VALID );
_ClearDTOG_TX(ENDP3);
_ClearDTOG_RX(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 configuration 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 string descriptors.
*/
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 USB_UNSUPPORT or USB_SUCCESS.
*/
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)
{
uint32_t Request_No = pInformation->USBbRequest;
if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
{
return USB_UNSUPPORT;
}
else if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT) )
{
if (Request_No == 0xFE)
{
pInformation->Ctrl_Info.Usb_wLength = 1;
pInformation->Ctrl_Info.CopyData = &USBD_CDROM_Get_Max_Lun;
return USB_SUCCESS;
}
else
{
return USB_UNSUPPORT;
}
}
return USB_UNSUPPORT;
}
/*******************************************************************************
* @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_UNSUPPORT;
}
uint8_t *USBD_CDROM_Get_Max_Lun(uint16_t Length)
{
uint8_t wIndex0 = pInformation->USBwIndexs.bw.bb0;
if (wIndex0 > 1)
{
return NULL;
}
else
{
return (uint8_t*)&CDROM_Max_Lun;
}
}
@@ -0,0 +1,58 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_prop.h
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : All processing related to Virtual COM Port Demo (Endpoint 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.
*******************************************************************************/
#ifndef __usb_prop_H
#define __usb_prop_H
#ifdef __cplusplus
extern "C" {
#endif
#include "ch32v20x.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
void USBD_init(void);
void USBD_Reset(void);
void USBD_SetConfiguration(void);
void USBD_SetDeviceAddress (void);
void USBD_SetDeviceFeature (void);
void USBD_ClearFeature (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_CDROM_Get_Max_Lun(uint16_t);
#ifdef __cplusplus
}
#endif
#endif /* __usb_prop_H */
@@ -0,0 +1,215 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_pwr.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : Connection/disconnection & power management
*********************************************************************************
* 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"
#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;
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,62 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_pwr.h
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : Connection/disconnection & power management header
*********************************************************************************
* 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.
*******************************************************************************/
#ifndef __USB_PWR_H
#define __USB_PWR_H
#ifdef __cplusplus
extern "C" {
#endif
#include "ch32v20x.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*/