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,162 @@
/********************************** (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 "hw_config.h"
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include "../driver/inc/usb_lib.h"
#include "usb_desc.h"
#include "usb_istr.h"
#include "usb_prop.h"
#include "usb_pwr.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 usb_conf_clksource(void)
{
switch (SystemCoreClock) {
case 144000000: RCC_USBCLKConfig( RCC_USBCLKSource_PLLCLK_Div3 ); break;
case 96000000: RCC_USBCLKConfig( RCC_USBCLKSource_PLLCLK_Div2 ); break;
case 48000000: RCC_USBCLKConfig( RCC_USBCLKSource_PLLCLK_Div1 ); break;
}
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_intr_init(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;
}

View File

@@ -0,0 +1,41 @@
/********************************** (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 <ch32v20x.h>
#include "usb_type.h"
#ifdef USE_PRINTF
#define printf_usb(X...) printf(X)
#else
#define printf_usb(X...)
#endif
void usb_conf_clksource(void);
void usb_intr_init(void);
void Enter_LowPowerMode(void);
void Leave_LowPowerMode(void);
void USB_Port_Set(FunctionalState NewState, FunctionalState Pin_In_IPU);
#endif /* __HW_CONFIG_H */

View File

@@ -0,0 +1,78 @@
/********************************** (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 (15)
/* 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_RXADDR (ENDP1_TXADDR + 0x40)
#define ENDP3_TXADDR (ENDP2_RXADDR + 0x40)
#define ENDP4_TXADDR (ENDP3_TXADDR + 0x40)
#define ENDP4_RXADDR (ENDP4_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 */

View File

@@ -0,0 +1,190 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_desc.c
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* 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_desc.h"
#include "../driver/inc/usb_lib.h"
/* USB Device Descriptors */
const uint8_t USBD_DeviceDescriptor[] = {
USBD_SIZE_DEVICE_DESC, // bLength
0x01, // bDescriptorType
0x10, 0x01, // bcdUSB
0x00, // bDeviceClass
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
DEF_USBD_UEP0_SIZE, // bMaxPacketSize0
0x86, 0x1A, // idVendor
0x0C, 0xFE, // idProduct
0x00, 0x01, // bcdDevice
0x01, // iManufacturer
0x02, // iProduct
0x00, // iSerialNumber
0x01, // bNumConfigurations
};
/* USB Configration Descriptors */
const uint8_t USBD_ConfigDescriptor[] = {
/* Configuration Descriptor */
0x09, // bLength
0x02, // bDescriptorType
USBD_SIZE_CONFIG_DESC & 0xFF, USBD_SIZE_CONFIG_DESC >> 8, // wTotalLength
0x03, // bNumInterfaces
0x01, // bConfigurationValue
0x00, // iConfiguration
0x80, // bmAttributes: Bus Powered; Remote Wakeup
0x32, // MaxPower: 100mA
/* IAD Descriptor(interface 0/1)*/
0x08, 0x0B, 0x00, 0x02, 0x02, 0x02, 0x01, 0x00,
/* Interface 0 (CDC) descriptor */
0x09, // bLength
0x04, // bDescriptorType (Interface)
0x00, // bInterfaceNumber 0
0x00, // bAlternateSetting
0x01, // bNumEndpoints 1
0x02, // bInterfaceClass
0x02, // bInterfaceSubClass
0x01, // bInterfaceProtocol
0x00, // iInterface (String Index)
/* Functional Descriptors */
0x05,0x24,0x00, 0x10, 0x01,
/* Length/management descriptor (data class interface 1) */
0x05, 0x24, 0x01, 0x00, 0x01,
0x04, 0x24, 0x02, 0x02,
0x05, 0x24, 0x06, 0x00, 0x01,
/* Interrupt upload endpoint descriptor */
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x81, // bEndpointAddress (IN/D2H)
0x03, // bmAttributes (Interrupt)
0x40, 0x00, // wMaxPacketSize 64
0x01, // bInterval 1 (unit depends on device speed)
/* Interface 1 (data interface) descriptor */
0x09, // bLength
0x04, // bDescriptorType (Interface)
0x01, // bInterfaceNumber 1
0x00, // bAlternateSetting
0x02, // bNumEndpoints 2
0x0A, // bInterfaceClass
0x00, // bInterfaceSubClass
0x00, // bInterfaceProtocol
0x00, // iInterface (String Index)
/* Endpoint descriptor */
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x02, // bEndpointAddress (OUT/H2D)
0x02, // bmAttributes (Bulk)
0x40, 0x00, // wMaxPacketSize 64
0x00, // bInterval 0 (unit depends on device speed)
/* Endpoint descriptor */
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x83, // bEndpointAddress (IN/D2H)
0x02, // bmAttributes (Bulk)
0x40, 0x00, // wMaxPacketSize 64
0x00, // bInterval 0 (unit depends on device speed)
/* interface 2 (HID interface) descriptor */
0x09, // bLength
0x04, // bDescriptorType (Interface)
0x02, // bInterfaceNumber 2
0x00, // bAlternateSetting
0x02, // bNumEndpoints 2
0x03, // bInterfaceClass
0x00, // bInterfaceSubClass
0x00, // bInterfaceProtocol
0x00, // iInterface (String Index)
/* interface 2 HID descriptor */
0x09, // bLength
0x21, // bDescriptorType
0x11, 0x01, // bcdHID
0x00, // bCountryCode
0x01, // bNumDescriptors
0x22, // bDescriptorType
USBD_SIZE_REPORT_DESC & 0xFF, USBD_SIZE_REPORT_DESC >> 8, // wDescriptorLength
/* interface 2 endpoint descriptor*/
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x84, // bEndpointAddress (IN/D2H)
0x03, // bmAttributes (Interrupt)
0x40, 0x00, // wMaxPacketSize 64
0x01, // bInterval 1 (unit depends on device speed)
/* interface 2 endpoint descriptor */
0x07, // bLength
0x05, // bDescriptorType (Endpoint)
0x04, // bEndpointAddress (OUT/H2D)
0x03, // bmAttributes (Interrupt)
0x40, 0x00, // wMaxPacketSize 64
0x01, // bInterval 1 (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,
'U', 0, 'S', 0, 'B', 0, ' ', 0, 'S', 0, 'e', 0, 'r', 0, 'i', 0, 'a', 0, 'l', 0};
/* USB Device String Serial */
uint8_t USBD_StringSerial[USBD_SIZE_STRING_SERIAL] = {
USBD_SIZE_STRING_SERIAL,
USB_STRING_DESCRIPTOR_TYPE,
'0', 0, '1', 0, '2', 0, '3', 0, '4', 0, '5', 0 , '6', 0, '7', 0, '8', 0, '9', 0
};
/* HID Report Descriptor */
const uint8_t USBD_HidRepDesc[USBD_SIZE_REPORT_DESC] =
{
0x06, 0x00, 0xFF, // Usage Page (Vendor Defined 0xFF00)
0x09, 0x01, // Usage (0x01)
0xA1, 0x01, // Collection (Application)
0x09, 0x02, // Usage (0x02)
0x26, 0xFF, 0x00, // Logical Maximum (255)
0x75, 0x08, // Report Size (8)
0x15, 0x00, // Logical Minimum (0)
0x95, 0x40, // Report Count (64)
0x81, 0x06, // Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
0x09, 0x02, // Usage (0x02)
0x15, 0x00, // Logical Minimum (0)
0x26, 0xFF, 0x00, // Logical Maximum (255)
0x75, 0x08, // Report Size (8)
0x95, 0x40, // Report Count (64)
0x91, 0x06, // Output (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0xC0, // End Collection
};

View File

@@ -0,0 +1,52 @@
/********************************** (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 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 107
#define USBD_SIZE_REPORT_DESC 34
#define USBD_SIZE_STRING_LANGID 4
#define USBD_SIZE_STRING_VENDOR 14
#define USBD_SIZE_STRING_PRODUCT 22
#define USBD_SIZE_STRING_SERIAL 22
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_HidRepDesc[USBD_SIZE_REPORT_DESC];
extern uint8_t USBD_StringSerial [USBD_SIZE_STRING_SERIAL];
#ifdef __cplusplus
}
#endif
#endif /* __USB_DESC_H */

View File

@@ -0,0 +1,148 @@
/********************************** (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 "usb_lib.h"
#include "usb_mem.h"
#include "hw_config.h"
#include "usb_desc.h"
#include "usb_istr.h"
#include "usb_prop.h"
#include "usb_pwr.h"
#include "../../user/usb/cdc.h"
uint8_t USBD_Endp3_Busy;
uint16_t USB_Rx_Cnt = 0;
uint8_t HID_Buffer[DEF_USBD_MAX_PACK_SIZE];
/*********************************************************************
* @fn EP1_IN_Callback
*
* @brief Endpoint 1 IN.
*
* @return none
*/
void EP1_IN_Callback (void)
{
}
/*********************************************************************
* @fn EP2_OUT_Callback
*
* @brief Endpoint 2 OUT.
*
* @return none
*/
void EP2_OUT_Callback (void)
{
uint32_t len;
len = GetEPRxCount( EP2_OUT & 0x7F );
PMAToUserBufferCopy( &CDC_Tx_Buf[ ( Cdc.Tx_LoadNum * DEF_USB_FS_PACK_LEN ) ], GetEPRxAddr( EP2_OUT & 0x7F ), len );
Cdc.Tx_PackLen[ Cdc.Tx_LoadNum ] = len;
Cdc.Tx_LoadNum++;
if( Cdc.Tx_LoadNum >= DEF_CDC_TX_BUF_NUM_MAX )
{
Cdc.Tx_LoadNum = 0x00;
}
Cdc.Tx_RemainNum++;
if( Cdc.Tx_RemainNum >= ( DEF_CDC_TX_BUF_NUM_MAX - 2 ) )
{
Cdc.USB_Down_StopFlag = 0x01;
}
else
{
SetEPRxValid( ENDP2 );
}
}
/*********************************************************************
* @fn EP3_IN_Callback
*
* @brief Endpoint 3 IN.
*
* @return none
*/
void EP3_IN_Callback (void)
{
USBD_Endp3_Busy = 0;
Cdc.USB_Up_IngFlag = 0x00;
}
/*********************************************************************
* @fn EP4_IN_Callback
*
* @brief Endpoint 4 IN.
*
* @return none
*/
void EP4_IN_Callback (void)
{
}
/*********************************************************************
* @fn EP4_OUT_Callback
*
* @brief Endpoint 4 OUT.
*
* @return none
*/
void EP4_OUT_Callback (void)
{
uint16_t i;
USB_Rx_Cnt = USB_SIL_Read(EP4_IN,HID_Buffer);
for ( i = 0; i < USB_Rx_Cnt; i++)
{
HID_Buffer[i] = ~HID_Buffer[i];
}
USB_SIL_Write( EP4_OUT, HID_Buffer, USB_Rx_Cnt );
SetEPTxValid( ENDP4 );
SetEPRxValid( ENDP4 );
}
/*********************************************************************
* @fn USBD_ENDPx_DataUp
*
* @brief USBD ENDPx DataUp Function
*
* @param endp - endpoint num.
* *pbuf - A pointer points to data.
* len - data length to transmit.
*
* @return data up status.
*/
uint8_t USBD_ENDPx_DataUp( uint8_t endp, uint8_t *pbuf, uint16_t len )
{
if( endp == ENDP3 )
{
if (USBD_Endp3_Busy)
{
return USB_ERROR;
}
USB_SIL_Write( EP3_IN, pbuf, len );
USBD_Endp3_Busy = 1;
SetEPTxStatus( ENDP3, EP_TX_VALID );
}
else
{
return USB_ERROR;
}
return USB_SUCCESS;
}

View File

@@ -0,0 +1,201 @@
/********************************** (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_istr.h"
#include "../driver/inc/usb_lib.h"
#include "usb_prop.h"
#include "usb_pwr.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 */

View File

@@ -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*/

View File

@@ -0,0 +1,503 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : usb_prop.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/08/08
* Description : All processing related to Virtual Com Port Demo
*********************************************************************************
* 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_prop.h"
#include "hw_config.h"
#include "usb_conf.h"
#include "usb_desc.h"
#include "usb_pwr.h"
#include "../../user/usb/cdc.h"
uint8_t Request = 0;
extern uint8_t USBD_Endp3_Busy;
volatile uint8_t HID_Idle_Value[2] = {0};
volatile uint8_t HID_Protocol_Value[2] = {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}
};
ONE_DESCRIPTOR Report_Descriptor[1] =
{
{(uint8_t*)USBD_HidRepDesc, USBD_SIZE_REPORT_DESC},
};
ONE_DESCRIPTOR Hid_Descriptor[1] =
{
{(uint8_t*)&USBD_ConfigDescriptor[84], 0x09},
};
/*********************************************************************
* @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)
{
uint32_t Request_No = pInformation->USBbRequest;
if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
{
if (Request_No == CDC_SET_LINE_CODING)
{
CDC_USB_Init();
}
}
}
/*******************************************************************************
* @fn USBD_Status_Out
*
* @brief USBD Status OUT Routine.
*
* @return None.
*/
void USBD_Status_Out(void)
{
}
/*******************************************************************************
* @fn USBD_init.
*
* @brief init routine.
*
* @return None.
*/
void USBD_init(void)
{
uint8_t i;
uint32_t timeout;
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);
for (timeout = SystemCoreClock >> 5; timeout; timeout--);
USB_Port_Set(ENABLE, ENABLE);
}
/*******************************************************************************
* @fn USBD_Reset
*
* @brief USBD 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_INTERRUPT);
SetEPTxStatus(ENDP1, EP_TX_NAK);
SetEPTxAddr(ENDP1, ENDP1_TXADDR);
SetEPRxStatus(ENDP1, EP_RX_DIS);
_ClearDTOG_TX(ENDP1);
_ClearDTOG_RX(ENDP1);
SetEPType(ENDP2, EP_BULK);
SetEPTxStatus(ENDP2, EP_TX_DIS);
SetEPRxAddr(ENDP2, ENDP2_RXADDR);
SetEPRxCount(ENDP2, DEF_USBD_MAX_PACK_SIZE);
SetEPRxStatus(ENDP2,EP_RX_VALID);
_ClearDTOG_RX(ENDP2);
_ClearDTOG_TX(ENDP2);
SetEPType(ENDP3, EP_BULK);
SetEPTxStatus(ENDP3, EP_TX_NAK);
SetEPTxAddr(ENDP3, ENDP3_TXADDR);
SetEPRxStatus(ENDP3, EP_RX_DIS);
_ClearDTOG_TX(ENDP3);
_ClearDTOG_RX(ENDP3);
USBD_Endp3_Busy = 0;
SetEPType(ENDP4, EP_INTERRUPT);
SetEPTxStatus(ENDP4, EP_TX_NAK);
SetEPTxAddr(ENDP4, ENDP4_TXADDR);
SetEPRxAddr(ENDP4, ENDP4_RXADDR);
SetEPRxCount(ENDP4, DEF_USBD_MAX_PACK_SIZE);
SetEPRxStatus(ENDP4, EP_RX_VALID );
_ClearDTOG_TX(ENDP4);
_ClearDTOG_RX(ENDP4);
SetDeviceAddress(0);
USBD_Endp3_Busy = 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_GetReportDescriptor
*
* @brief Gets the report descriptors according to the needed index
*
* @param Length.
*
* @return The address of the device descriptor.
*/
uint8_t *USBD_GetReportDescriptor(uint16_t Length)
{
return Standard_GetDescriptorData(Length, &Report_Descriptor[0]);
}
/*********************************************************************
* @fn USBD_GetHidDescriptor
*
* @brief Gets the report descriptors according to the needed index
*
* @param Length.
*
* @return The address of the device descriptor.
*/
uint8_t *USBD_GetHidDescriptor(uint16_t Length)
{
return Standard_GetDescriptorData(Length, &Hid_Descriptor[0]);
}
/*********************************************************************
* @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 USB_CDC_GetLineCoding.
*
* @brief send the linecoding structure to the PC host.
*
* @param Length
*
* @return Inecoding structure base address.
*/
uint8_t *USB_CDC_GetLineCoding( uint16_t Length )
{
if( Length == 0 ) {
pInformation->Ctrl_Info.Usb_wLength = 7;
return( NULL );
}
return (uint8_t *)&Cdc.Com_Cfg[ 0 ];
}
/*********************************************************************
* @fn USB_CDC_SetLineCoding.
*
* @brief Set the linecoding structure fields.
*
* @param Length
*
* @return Inecoding structure base address.
*/
uint8_t *USB_CDC_SetLineCoding( uint16_t Length )
{
if( Length == 0 ) {
pInformation->Ctrl_Info.Usb_wLength = 7;
return( NULL );
}
return(uint8_t *)&Cdc.Com_Cfg[ 0 ];
}
/*********************************************************************
* @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;
uint8_t *(*CopyRoutine)(uint16_t);
uint8_t wValue1;
Request_No = pInformation->USBbRequest;
CopyRoutine = NULL;
wValue1 = pInformation->USBwValue1;
if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) {
if (wValue1 == HID_REPORT_DESCRIPTOR) {
CopyRoutine = USBD_GetReportDescriptor;
}
else if (wValue1 == HID_DESCRIPTOR) {
CopyRoutine = USBD_GetHidDescriptor;
}
if (CopyRoutine) {
pInformation->Ctrl_Info.CopyData = CopyRoutine;
(*CopyRoutine)(0);
}
else {
return USB_UNSUPPORT;
}
}
else if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) {
if (Request_No == CDC_GET_LINE_CODING) {
CopyRoutine = &USB_CDC_GetLineCoding;
}
else if (Request_No == CDC_SET_LINE_CODING) {
CopyRoutine = &USB_CDC_SetLineCoding;
}
else {
return USB_UNSUPPORT;
}
}
if (CopyRoutine) {
pInformation->Ctrl_Info.CopyData = CopyRoutine;
pInformation->Ctrl_Info.Usb_wOffset = 0;
(*CopyRoutine)( 0 );
}
else {
return( USB_UNSUPPORT );
}
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)
{
uint32_t Request_No = pInformation->USBbRequest;
if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) {
if (Request_No == CDC_SET_LINE_CTLSTE) {
}
else if (Request_No == CDC_SEND_BREAK) {
}
else {
return USB_UNSUPPORT;
}
}
return USB_SUCCESS;
}

View File

@@ -0,0 +1,85 @@
/********************************** (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>
#include "usb_lib.h"
#define CDC_GET_LINE_CODING 0x21 /* This request allows the host to find out the currently configured line coding */
#define CDC_SET_LINE_CODING 0x20 /* Configures DTE rate, stop-bits, parity, and number-of-character */
#define CDC_SET_LINE_CTLSTE 0x22 /* This request generates RS-232/V.24 style control signals */
#define CDC_SEND_BREAK 0x23
#define HID_GET_REPORT 0x01
#define HID_GET_IDLE 0x02
#define HID_GET_PROTOCOL 0x03
#define HID_SET_REPORT 0x09
#define HID_SET_IDLE 0x0A
#define HID_SET_PROTOCOL 0x0B
#define HID_DESCRIPTOR 0x21
#define HID_REPORT_DESCRIPTOR 0x22
#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_ENDPx_DataUp( uint8_t endp, uint8_t *pbuf, uint16_t len );
#ifdef __cplusplus
}
#endif
#endif /* __usb_prop_H */

View File

@@ -0,0 +1,221 @@
/********************************** (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_pwr.h"
#include "usb_lib.h"
#include "hw_config.h"
#include "usb_conf.h"
__IO uint32_t bDeviceState = UNCONNECTED;
__IO uint8_t 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(void)
{
_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;
}
}
}

View File

@@ -0,0 +1,75 @@
/********************************** (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"
#include "usb_lib.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;
extern __IO uint32_t bDeviceState; /* USB device status */
extern __IO uint8_t fSuspendEnabled; /* true when suspend is possible */
void Suspend(void);
void Resume_Init(void);
void Resume(RESUME_STATE eResumeSetVal);
RESULT PowerOn(void);
RESULT PowerOff(void);
#ifdef __cplusplus
}
#endif
#endif /*__USB_PWR_H*/