add ch552

This commit is contained in:
unknown 2018-10-30 00:17:17 +08:00
parent 87b0f7b8d5
commit e09f959da4
10 changed files with 2913 additions and 0 deletions

405
HID/HID.uvproj Normal file
View File

@ -0,0 +1,405 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="project_proj.xsd">
<SchemaVersion>1.1</SchemaVersion>
<Header>### uVision Project, (C) Keil Software</Header>
<Targets>
<Target>
<TargetName>QITAS</TargetName>
<ToolsetNumber>0x0</ToolsetNumber>
<ToolsetName>MCS-51</ToolsetName>
<uAC6>0</uAC6>
<TargetOption>
<TargetCommonOption>
<Device>AT89C52</Device>
<Vendor>Atmel</Vendor>
<Cpu>IRAM(0-0xFF) IROM(0-0x1FFF) CLOCK(24000000)</Cpu>
<FlashUtilSpec></FlashUtilSpec>
<StartupFile>"LIB\STARTUP.A51" ("Standard 8051 Startup Code")</StartupFile>
<FlashDriverDll></FlashDriverDll>
<DeviceId>2980</DeviceId>
<RegisterFile>REGX52.H</RegisterFile>
<MemoryEnv></MemoryEnv>
<Cmp></Cmp>
<Asm></Asm>
<Linker></Linker>
<OHString></OHString>
<InfinionOptionDll></InfinionOptionDll>
<SLE66CMisc></SLE66CMisc>
<SLE66AMisc></SLE66AMisc>
<SLE66LinkerMisc></SLE66LinkerMisc>
<SFDFile></SFDFile>
<bCustSvd>0</bCustSvd>
<UseEnv>0</UseEnv>
<BinPath></BinPath>
<IncludePath></IncludePath>
<LibPath></LibPath>
<RegisterFilePath>Atmel\</RegisterFilePath>
<DBRegisterFilePath>Atmel\</DBRegisterFilePath>
<TargetStatus>
<Error>0</Error>
<ExitCodeStop>0</ExitCodeStop>
<ButtonStop>0</ButtonStop>
<NotGenerated>0</NotGenerated>
<InvalidFlash>1</InvalidFlash>
</TargetStatus>
<OutputDirectory>.\obj\</OutputDirectory>
<OutputName>VCK1</OutputName>
<CreateExecutable>1</CreateExecutable>
<CreateLib>0</CreateLib>
<CreateHexFile>1</CreateHexFile>
<DebugInformation>1</DebugInformation>
<BrowseInformation>1</BrowseInformation>
<ListingPath>.\list\</ListingPath>
<HexFormatSelection>0</HexFormatSelection>
<Merge32K>0</Merge32K>
<CreateBatchFile>0</CreateBatchFile>
<BeforeCompile>
<RunUserProg1>0</RunUserProg1>
<RunUserProg2>0</RunUserProg2>
<UserProg1Name></UserProg1Name>
<UserProg2Name></UserProg2Name>
<UserProg1Dos16Mode>0</UserProg1Dos16Mode>
<UserProg2Dos16Mode>0</UserProg2Dos16Mode>
<nStopU1X>0</nStopU1X>
<nStopU2X>0</nStopU2X>
</BeforeCompile>
<BeforeMake>
<RunUserProg1>0</RunUserProg1>
<RunUserProg2>0</RunUserProg2>
<UserProg1Name></UserProg1Name>
<UserProg2Name></UserProg2Name>
<UserProg1Dos16Mode>0</UserProg1Dos16Mode>
<UserProg2Dos16Mode>0</UserProg2Dos16Mode>
<nStopB1X>0</nStopB1X>
<nStopB2X>0</nStopB2X>
</BeforeMake>
<AfterMake>
<RunUserProg1>0</RunUserProg1>
<RunUserProg2>0</RunUserProg2>
<UserProg1Name></UserProg1Name>
<UserProg2Name></UserProg2Name>
<UserProg1Dos16Mode>0</UserProg1Dos16Mode>
<UserProg2Dos16Mode>0</UserProg2Dos16Mode>
<nStopA1X>0</nStopA1X>
<nStopA2X>0</nStopA2X>
</AfterMake>
<SelectedForBatchBuild>0</SelectedForBatchBuild>
<SVCSIdString></SVCSIdString>
</TargetCommonOption>
<CommonProperty>
<UseCPPCompiler>0</UseCPPCompiler>
<RVCTCodeConst>0</RVCTCodeConst>
<RVCTZI>0</RVCTZI>
<RVCTOtherData>0</RVCTOtherData>
<ModuleSelection>0</ModuleSelection>
<IncludeInBuild>1</IncludeInBuild>
<AlwaysBuild>0</AlwaysBuild>
<GenerateAssemblyFile>0</GenerateAssemblyFile>
<AssembleAssemblyFile>0</AssembleAssemblyFile>
<PublicsOnly>0</PublicsOnly>
<StopOnExitCode>3</StopOnExitCode>
<CustomArgument></CustomArgument>
<IncludeLibraryModules></IncludeLibraryModules>
<ComprImg>1</ComprImg>
<BankNo>65535</BankNo>
</CommonProperty>
<DllOption>
<SimDllName>S8051.DLL</SimDllName>
<SimDllArguments></SimDllArguments>
<SimDlgDll>DP51.DLL</SimDlgDll>
<SimDlgDllArguments>-p52</SimDlgDllArguments>
<TargetDllName>S8051.DLL</TargetDllName>
<TargetDllArguments></TargetDllArguments>
<TargetDlgDll>TP51.DLL</TargetDlgDll>
<TargetDlgDllArguments>-p52</TargetDlgDllArguments>
</DllOption>
<DebugOption>
<OPTHX>
<HexSelection>0</HexSelection>
<HexRangeLowAddress>0</HexRangeLowAddress>
<HexRangeHighAddress>0</HexRangeHighAddress>
<HexOffset>0</HexOffset>
<Oh166RecLen>16</Oh166RecLen>
</OPTHX>
<Simulator>
<UseSimulator>1</UseSimulator>
<LoadApplicationAtStartup>1</LoadApplicationAtStartup>
<RunToMain>1</RunToMain>
<RestoreBreakpoints>1</RestoreBreakpoints>
<RestoreWatchpoints>1</RestoreWatchpoints>
<RestoreMemoryDisplay>1</RestoreMemoryDisplay>
<RestoreFunctions>1</RestoreFunctions>
<RestoreToolbox>1</RestoreToolbox>
<LimitSpeedToRealTime>0</LimitSpeedToRealTime>
<RestoreSysVw>1</RestoreSysVw>
</Simulator>
<Target>
<UseTarget>0</UseTarget>
<LoadApplicationAtStartup>1</LoadApplicationAtStartup>
<RunToMain>0</RunToMain>
<RestoreBreakpoints>1</RestoreBreakpoints>
<RestoreWatchpoints>1</RestoreWatchpoints>
<RestoreMemoryDisplay>1</RestoreMemoryDisplay>
<RestoreFunctions>0</RestoreFunctions>
<RestoreToolbox>1</RestoreToolbox>
<RestoreTracepoints>0</RestoreTracepoints>
<RestoreSysVw>1</RestoreSysVw>
</Target>
<RunDebugAfterBuild>0</RunDebugAfterBuild>
<TargetSelection>-1</TargetSelection>
<SimDlls>
<CpuDll></CpuDll>
<CpuDllArguments></CpuDllArguments>
<PeripheralDll></PeripheralDll>
<PeripheralDllArguments></PeripheralDllArguments>
<InitializationFile></InitializationFile>
</SimDlls>
<TargetDlls>
<CpuDll></CpuDll>
<CpuDllArguments></CpuDllArguments>
<PeripheralDll></PeripheralDll>
<PeripheralDllArguments></PeripheralDllArguments>
<InitializationFile></InitializationFile>
<Driver></Driver>
</TargetDlls>
</DebugOption>
<Utilities>
<Flash1>
<UseTargetDll>0</UseTargetDll>
<UseExternalTool>0</UseExternalTool>
<RunIndependent>0</RunIndependent>
<UpdateFlashBeforeDebugging>0</UpdateFlashBeforeDebugging>
<Capability>0</Capability>
<DriverSelection>-1</DriverSelection>
</Flash1>
<bUseTDR>0</bUseTDR>
<Flash2></Flash2>
<Flash3>"" ()</Flash3>
<Flash4></Flash4>
<pFcarmOut></pFcarmOut>
<pFcarmGrp></pFcarmGrp>
<pFcArmRoot></pFcArmRoot>
<FcArmLst>0</FcArmLst>
</Utilities>
<Target51>
<Target51Misc>
<MemoryModel>2</MemoryModel>
<RTOS>0</RTOS>
<RomSize>2</RomSize>
<DataHold>0</DataHold>
<XDataHold>0</XDataHold>
<UseOnchipRom>0</UseOnchipRom>
<UseOnchipArithmetic>0</UseOnchipArithmetic>
<UseMultipleDPTR>0</UseMultipleDPTR>
<UseOnchipXram>0</UseOnchipXram>
<HadIRAM>1</HadIRAM>
<HadXRAM>0</HadXRAM>
<HadIROM>1</HadIROM>
<Moda2>0</Moda2>
<Moddp2>0</Moddp2>
<Modp2>0</Modp2>
<Mod517dp>0</Mod517dp>
<Mod517au>0</Mod517au>
<Mode2>0</Mode2>
<useCB>0</useCB>
<useXB>0</useXB>
<useL251>0</useL251>
<useA251>0</useA251>
<Mx51>0</Mx51>
<ModC812>0</ModC812>
<ModCont>0</ModCont>
<Lp51>0</Lp51>
<useXBS>0</useXBS>
<ModDA>0</ModDA>
<ModAB2>0</ModAB2>
<Mx51P>0</Mx51P>
<hadXRAM2>0</hadXRAM2>
<uocXram2>0</uocXram2>
<hadXRAM3>0</hadXRAM3>
<ModC2>0</ModC2>
<ModH2>0</ModH2>
<Mdu_R515>0</Mdu_R515>
<Mdu_F120>0</Mdu_F120>
<Psoc>0</Psoc>
<hadIROM2>0</hadIROM2>
<hadIROM3>0</hadIROM3>
<ModSmx2>0</ModSmx2>
<cBanks>0</cBanks>
<xBanks>0</xBanks>
<OnChipMemories>
<RCB>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0xffff</Size>
</RCB>
<RXB>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</RXB>
<Ocm1>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</Ocm1>
<Ocm2>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</Ocm2>
<Ocm3>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</Ocm3>
<Ocr1>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</Ocr1>
<Ocr2>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</Ocr2>
<Ocr3>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</Ocr3>
<IRO>
<Type>1</Type>
<StartAddress>0x0</StartAddress>
<Size>0x2000</Size>
</IRO>
<IRA>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x100</Size>
</IRA>
<XRA>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</XRA>
<XRA512>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</XRA512>
<IROM512>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</IROM512>
<XRA513>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</XRA513>
<IROM513>
<Type>0</Type>
<StartAddress>0x0</StartAddress>
<Size>0x0</Size>
</IROM513>
</OnChipMemories>
</Target51Misc>
<C51>
<RegisterColoring>0</RegisterColoring>
<VariablesInOrder>0</VariablesInOrder>
<IntegerPromotion>1</IntegerPromotion>
<uAregs>0</uAregs>
<UseInterruptVector>1</UseInterruptVector>
<Fuzzy>3</Fuzzy>
<Optimize>6</Optimize>
<WarningLevel>2</WarningLevel>
<SizeSpeed>1</SizeSpeed>
<ObjectExtend>1</ObjectExtend>
<ACallAJmp>0</ACallAJmp>
<InterruptVectorAddress>0</InterruptVectorAddress>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
<Undefine></Undefine>
<IncludePath></IncludePath>
</VariousControls>
</C51>
<Ax51>
<UseMpl>0</UseMpl>
<UseStandard>1</UseStandard>
<UseCase>0</UseCase>
<UseMod51>0</UseMod51>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
<Undefine></Undefine>
<IncludePath></IncludePath>
</VariousControls>
</Ax51>
<Lx51>
<useFile>0</useFile>
<linkonly>0</linkonly>
<UseMemoryFromTarget>1</UseMemoryFromTarget>
<CaseSensitiveSymbols>0</CaseSensitiveSymbols>
<WarningLevel>2</WarningLevel>
<DataOverlaying>1</DataOverlaying>
<OverlayString></OverlayString>
<MiscControls></MiscControls>
<DisableWarningNumbers>16</DisableWarningNumbers>
<LinkerCmdFile></LinkerCmdFile>
<Assign></Assign>
<ReserveString></ReserveString>
<CClasses></CClasses>
<UserClasses></UserClasses>
<CSection></CSection>
<UserSection></UserSection>
<CodeBaseAddress></CodeBaseAddress>
<XDataBaseAddress></XDataBaseAddress>
<PDataBaseAddress></PDataBaseAddress>
<BitBaseAddress></BitBaseAddress>
<DataBaseAddress></DataBaseAddress>
<IDataBaseAddress></IDataBaseAddress>
<Precede></Precede>
<Stack></Stack>
<CodeSegmentName></CodeSegmentName>
<XDataSegmentName></XDataSegmentName>
<BitSegmentName></BitSegmentName>
<DataSegmentName></DataSegmentName>
<IDataSegmentName></IDataSegmentName>
</Lx51>
</Target51>
</TargetOption>
<Groups>
<Group>
<GroupName>HID</GroupName>
<Files>
<File>
<FileName>main.c</FileName>
<FileType>1</FileType>
<FilePath>.\usr\main.c</FilePath>
</File>
<File>
<FileName>Key.c</FileName>
<FileType>1</FileType>
<FilePath>.\usr\Key.c</FilePath>
</File>
<File>
<FileName>USB.c</FileName>
<FileType>1</FileType>
<FilePath>.\usr\USB.c</FilePath>
</File>
<File>
<FileName>DEBUG.C</FileName>
<FileType>1</FileType>
<FilePath>.\usr\DEBUG.C</FilePath>
</File>
</Files>
</Group>
</Groups>
</Target>
</Targets>
</Project>

1217
HID/usr/CH554.H Normal file

File diff suppressed because it is too large Load Diff

271
HID/usr/DEBUG.C Normal file
View File

@ -0,0 +1,271 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : DEBUG.C
* Author : WCH
* Version : V1.0
* Date : 2017/01/20
* Description : CH554 DEBUG Interface
CH554主频修改
01
01
*******************************************************************************/
#include "CH554.H"
#include "DEBUG.H"
/*******************************************************************************
* Function Name : CfgFsys( )
* Description : CH554时钟选择和配置函数,使Fsys 6MHzFREQ_SYS可以通过
CLOCK_CFG配置得到
Fsys = (Fosc * 4/(CLOCK_CFG & MASK_SYS_CK_SEL);
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CfgFsys( )
{
// SAFE_MOD = 0x55;
// SAFE_MOD = 0xAA;
// CLOCK_CFG |= bOSC_EN_XT; //使能外部晶振
// CLOCK_CFG &= ~bOSC_EN_INT; //关闭内部晶振
SAFE_MOD = 0x55;
SAFE_MOD = 0xAA;
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x07; // 32MHz
CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x06; // 24MHz
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x05; // 16MHz
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x04; // 12MHz
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x03; // 6MHz
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x02; // 3MHz
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x01; // 750KHz
// CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x00; // 187.5MHz
SAFE_MOD = 0x00;
}
/*******************************************************************************
* Function Name : mDelayus(UNIT16 n)
* Description : us延时函数
* Input : UNIT16 n
* Output : None
* Return : None
*******************************************************************************/
void mDelayuS( UINT16 n ) // 以uS为单位延时
{
#ifdef FREQ_SYS
#if FREQ_SYS <= 6000000
n >>= 2;
#endif
#if FREQ_SYS <= 3000000
n >>= 2;
#endif
#if FREQ_SYS <= 750000
n >>= 4;
#endif
#endif
while ( n ) { // total = 12~13 Fsys cycles, 1uS @Fsys=12MHz
++ SAFE_MOD; // 2 Fsys cycles, for higher Fsys, add operation here
#ifdef FREQ_SYS
#if FREQ_SYS >= 14000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 16000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 18000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 20000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 22000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 24000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 26000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 28000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 30000000
++ SAFE_MOD;
#endif
#if FREQ_SYS >= 32000000
++ SAFE_MOD;
#endif
#endif
-- n;
}
}
/*******************************************************************************
* Function Name : mDelayms(UNIT16 n)
* Description : ms延时函数
* Input : UNIT16 n
* Output : None
* Return : None
*******************************************************************************/
void mDelaymS( UINT16 n ) // 以mS为单位延时
{
while ( n ) {
#ifdef DELAY_MS_HW
while ( ( TKEY_CTRL & bTKC_IF ) == 0 );
while ( TKEY_CTRL & bTKC_IF );
#else
mDelayuS( 1000 );
#endif
-- n;
}
}
/*******************************************************************************
* Function Name : CH554UART0Alter()
* Description : CH554串口0引脚映射,P0.2P0.3
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CH554UART0Alter()
{
PIN_FUNC |= bUART0_PIN_X; //串口映射到P1.2和P1.3
}
/*******************************************************************************
* Function Name : mInitSTDIO()
* Description : CH554串口0初始化,使T1作UART0的波特率发生器,使T2
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void mInitSTDIO( )
{
UINT32 x;
UINT8 x2;
SM0 = 0;
SM1 = 1;
SM2 = 0; //串口0使用模式1
//使用Timer1作为波特率发生器
RCLK = 0; //UART0接收时钟
TCLK = 0; //UART0发送时钟
PCON |= SMOD;
x = 10 * FREQ_SYS / UART0_BUAD / 16; //如果更改主频注意x的值不要溢出
x2 = x % 10;
x /= 10;
if ( x2 >= 5 ) x ++; //四舍五入
TMOD = TMOD & ~ bT1_GATE & ~ bT1_CT & ~ MASK_T1_MOD | bT1_M1; //0X20Timer1作为8位自动重载定时器
T2MOD = T2MOD | bTMR_CLK | bT1_CLK; //Timer1时钟选择
TH1 = 0-x; //12MHz晶振,buad/12为实际需设置波特率
TR1 = 1; //启动定时器1
TI = 1;
REN = 1; //串口0接收使能
}
/*******************************************************************************
* Function Name : CH554UART0RcvByte()
* Description : CH554UART0接收一个字节
* Input : None
* Output : None
* Return : SBUF
*******************************************************************************/
UINT8 CH554UART0RcvByte( )
{
while(RI == 0); //查询接收,中断方式可不用
RI = 0;
return SBUF;
}
/*******************************************************************************
* Function Name : CH554UART0SendByte(UINT8 SendDat)
* Description : CH554UART0发送一个字节
* Input : UINT8 SendDat
* Output : None
* Return : None
*******************************************************************************/
void CH554UART0SendByte(UINT8 SendDat)
{
SBUF = SendDat; //查询发送中断方式可不用下面2条语句,但发送前需TI=0
while(TI ==0);
TI = 0;
}
/*******************************************************************************
* Function Name : UART1Setup()
* Description : CH554串口1初始化
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void UART1Setup( )
{
U1SM0 = 0; //UART1选择8位数据位
U1SMOD = 1; //快速模式
U1REN = 1; //使能接收
SBAUD1 = 0 - FREQ_SYS/16/UART1_BUAD;
}
/*******************************************************************************
* Function Name : CH554UART1RcvByte()
* Description : CH554UART1接收一个字节
* Input : None
* Output : None
* Return : SBUF
*******************************************************************************/
UINT8 CH554UART1RcvByte( )
{
while(U1RI == 0); //查询接收,中断方式可不用
U1RI = 0;
return SBUF1;
}
/*******************************************************************************
* Function Name : CH554UART1SendByte(UINT8 SendDat)
* Description : CH554UART1发送一个字节
* Input : UINT8 SendDat
* Output : None
* Return : None
*******************************************************************************/
void CH554UART1SendByte(UINT8 SendDat)
{
SBUF1 = SendDat; //查询发送中断方式可不用下面2条语句,但发送前需TI=0
while(U1TI ==0);
U1TI = 1;
}
/*******************************************************************************
* Function Name : CH554WDTModeSelect(UINT8 mode)
* Description : CH554看门狗模式选择
* Input : UINT8 mode
0 timer
1 watchDog
* Output : None
* Return : None
*******************************************************************************/
void CH554WDTModeSelect(UINT8 mode)
{
SAFE_MOD = 0x55;
SAFE_MOD = 0xaa; //进入安全模式
if(mode){
GLOBAL_CFG |= bWDOG_EN; //启动看门狗复位
}
else GLOBAL_CFG &= ~bWDOG_EN; //启动看门狗仅仅作为定时器
SAFE_MOD = 0x00; //退出安全模式
WDOG_COUNT = 0; //看门狗赋初值
}
/*******************************************************************************
* Function Name : CH554WDTFeed(UINT8 tim)
* Description : CH554看门狗定时时间设置
* Input : UINT8 tim
00H(6MHz)=2.8s
80H(6MHz)=1.4s
* Output : None
* Return : None
*******************************************************************************/
void CH554WDTFeed(UINT8 tim)
{
WDOG_COUNT = tim; //看门狗计数器赋值
}

46
HID/usr/DEBUG.H Normal file
View File

@ -0,0 +1,46 @@
/* 调试 */
/* 提供printf子程序和延时函数 */
#ifndef __DEBUG_H__
#define __DEBUG_H__
//定义函数返回值
#ifndef SUCCESS
#define SUCCESS 0
#endif
#ifndef FAIL
#define FAIL 0xFF
#endif
//定义定时器起始
#ifndef START
#define START 1
#endif
#ifndef STOP
#define STOP 0
#endif
#ifndef DE_PRINTF
#define DE_PRINTF 0
#endif
#define FREQ_SYS 12000000 //系统主频12MHz
#ifndef UART0_BUAD
#define UART0_BUAD 57600
#define UART1_BUAD 57600
#endif
void CfgFsys( ); //CH554时钟选择和配置
void mDelayuS( UINT16 n ); // 以uS为单位延时
void mDelaymS( UINT16 n ); // 以mS为单位延时
void CH554UART0Alter(); //CH554串口0引脚映射到P0.2/P0.3
void mInitSTDIO( ); //T1作为波特率发生器
UINT8 CH554UART0RcvByte( ); //CH554 UART0查询方式接收一个字节
void CH554UART0SendByte(UINT8 SendDat); //CH554UART0发送一个字节
void UART1Setup( ); //
UINT8 CH554UART1RcvByte( ); //CH554 UART1查询方式接收一个字节
void CH554UART1SendByte(UINT8 SendDat); //CH554UART1发送一个字节
void CH554WDTModeSelect(UINT8 mode); //CH554看门狗模式设置
void CH554WDTFeed(UINT8 tim); //CH554看门狗喂狗
#endif

80
HID/usr/Key.c Normal file
View File

@ -0,0 +1,80 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : Touch_Key.C
* Author : WCH
* Version : V1.0
* Date : 2017/01/20
* Description : CH554
*******************************************************************************/
#include "CH554.H"
#include "DEBUG.H"
#include "Key.h"
#include "stdio.h"
#pragma NOAREGS
/*******************************************************************************
* Function Name : TouchKeyQueryCylSet1Or2ms(UINT8 cyl)
* Description :
* Input : UINT8 cyl
1 2ms
0 1ms
* Output : None
* Return : None
*******************************************************************************/
void TouchKeyQueryCylSet1Or2ms(UINT8 cyl)
{
if(cyl)
{
TKEY_CTRL |= bTKC_2MS;
}
else
{
TKEY_CTRL &= ~bTKC_2MS;
}
}
/*******************************************************************************
* Function Name : TouchKeyChanelSelect(UINT8 ch)
* Description :
* Input : UINT8 ch
* Output : None
* Return : SUCCESS
FAIL
*******************************************************************************/
UINT8 TouchKeyChanelSelect(UINT8 ch)
{
TKEY_CTRL &= 0xF8;
if(ch == 0){;} //关闭电容检测仅作1ms或者2ms定时中断
else if(ch == 1){P1_DIR_PU &= 0xFE; TKEY_CTRL |= 0x01;} //TIN0(P10)
else if(ch == 2){P1_DIR_PU &= 0xFD; TKEY_CTRL |= 0x02;} //TIN0(P11)
else if(ch == 3){P1_DIR_PU &= 0xEF; TKEY_CTRL |= 0x03;} //TIN0(P14)
else if(ch == 4){P1_DIR_PU &= 0xDF; TKEY_CTRL |= 0x04;} //TIN0(P15)
else if(ch == 5){P1_DIR_PU &= 0xBF; TKEY_CTRL |= 0x05;} //TIN0(P16)
else if(ch == 6){P1_DIR_PU &= 0x7F; TKEY_CTRL |= 0x06;} //TIN0(P17)
else if(ch == 7){TKEY_CTRL |= 0x07;} //开启电容检测,但是不连接通道
else return FAIL;
#if INTERRUPT_TouchKey
IE_TKEY = 1; //使能Touch_Key中断
#endif
return SUCCESS;
}
#if INTERRUPT_TouchKey
/*******************************************************************************
* Function Name : Touch_KeyInterrupt(void)
* Description : Touch_Key
*******************************************************************************/
void Touch_KeyInterrupt( void ) interrupt INT_NO_TKEY using 1 //Touch_Key中断服务程序,使用寄存器组1
{
UINT16 KeyData;
IE_TKEY = 0; //关中断
KeyData = TKEY_DAT; //保持87us,尽快取走
#ifdef DE_PRINTF
printf("DATA: %04x\n",KeyData&0x7FFF);
#endif
}
#endif

39
HID/usr/Key.h Normal file
View File

@ -0,0 +1,39 @@
#define INTERRUPT_TouchKey 0 //开启Key中断方式
#define L_WIN 0X08
#define L_ALT 0X04
#define L_SHIFT 0X02
#define L_CTL 0X01
#define R_WIN 0X80
#define R_ALT 0X40
#define R_SHIFT 0X20
#define R_CTL 0X10
#define SPACE 0X2C
#define ENTER 0X28
#define MOUSE 0
/*******************************************************************************
* Function Name : TouchKeyQueryCylSet(UINT8 cyl)
* Description :
* Input : UINT8 cyl
1 2ms
0 1ms
* Output : None
* Return : None
*******************************************************************************/
void TouchKeyQueryCylSet1Or2ms(UINT8 cyl);
/*******************************************************************************
* Function Name : TouchKeyChanelSelect(UINT8 ch)
* Description :
* Input : UINT8 ch
* Output : None
* Return : SUCCESS
FAIL
*******************************************************************************/
UINT8 TouchKeyChanelSelect(UINT8 ch);

760
HID/usr/USB.c Normal file
View File

@ -0,0 +1,760 @@
/********************************** (C) COPYRIGHT ******************************
* File Name :USB.C
* Author :QITAS
* Version : V1.0
* Date : 2018/06/05
* Description : A demo for USB compound device created by CH554, support
keyboard , and HID-compliant device.
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CH554.H"
#include "DEBUG.H"
#include "USB.h"
#include "Key.h"
#pragma NOAREGS
#define THIS_ENDP0_SIZE DEFAULT_ENDP0_SIZE
#define BUFFER_SIZE 64
#define DUAL_BUFFER_SIZE 128
#define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer)
UINT8X Ep0Buffer[THIS_ENDP0_SIZE] _at_ 0x0000; // Endpoint 0, buffer OUT/OUTthe address must be even.
UINT8X Ep1Buffer[BUFFER_SIZE] _at_ 0x000A; // Endpoint 1, buffer INthe address must be even.
UINT8X Ep2Buffer[DUAL_BUFFER_SIZE] _at_ 0x0050; // Endpoint 2, buffer OUT[64]+IN[64]the address must be even.
/**************************** Global variable ********************************/
UINT8 volatile SetupReq, SetupLen, UsbConfig;
UINT8 volatile EnumOK, FLAG;
PUINT8 pDescr; // USB enumerate complete flag.
USB_SETUP_REQ SetupReqBuf; // A buffer for Setup package.
UINT8 HIDKey[8] = {0};
UINT8 TX[64] = {0};
UINT8 RX[64] = {0};
UINT8 TXflag = 0;
UINT8 RXflag = 0;
/**************************** Device Descriptor *************************************/
UINT8C DevDesc[18] = { // Device Descriptor
0x12,0x01,
0x10,0x01,
0x00,0x00,
0x00,0x08,
0x31,0x51, // Vendor ID | VID = 0X5131///413c
0x07,0x20, // Product ID | PID = 0X2007 /// 2105
// 0x00,0x01,
// 0x05,0x21,
0x00,0x11, // bcdDevice
0x00,0x00,
0x00,0x01
};
/**************************** HID Report Descriptor *********************************/
UINT8C KeyRepDesc[65] = // Report Descriptor, DELL Keyboard
{
0x05, 0x01, // Usage page Generatic Desktop
0x09, 0x06, // Usage keyboard
0xa1, 0x01, // Collation Application
0x05, 0x07, // Usafe page (key code)
0x19, 0xe0, // Usage Min ( E0 --> L_CTL)
0x29, 0xe7, // Usage MAX ( E7 --> R_GUI )
0x15, 0x00, // Logical min
0x25, 0x01, // Logical max
0x95, 0x08, // Report count ( 8 )
0x75, 0x01, // Report size ( 1 )
0x81, 0x02, // Input ( Data, Variable, Absolute )
0x95, 0x08, // Report count ( 8 )
0x75, 0x01, // Report size ( 1 )
0x81, 0x01, // Input ( const )
0x05, 0x08, // Usage page( LED )
0x19, 0x01, // Usage min ( 1 )
0x29, 0x03, // Usage max ( 3 )
0x95, 0x03, // Report count ( 3 )
0x75, 0x01, // Report size ( 1 )
0x91, 0x02, // Output ( Data, Variable, Absolute )
0x95, 0x01, // Report count ( 1 )
0x75, 0x05, // Report size ( 5 )
0x91, 0x01, // Output ( const )
0x05, 0x07, // Usage page ( key code )
0x19, 0x00, // Usage min ( 0H )
0x2a, 0xff, 0x00, // Usage max ( FFH )
0x15, 0x00, // Logical min ( 0H )
0x26, 0xff, 0x00, // Logical max ( FFH )
0x95, 0x06, // Report count ( 6 )
0x75, 0x08, // Report size ( 8 )
0x81, 0x00, // Input ( Data, Array, Absolute )
0xc0 // End collection
};
UINT8C ComRepDesc[34] = // Report Descriptor, Composite device
{
0x06, 0x00, 0xff, // Usage page Vendor defined
0x09, 0x01, // Usage keyboard
0xa1, 0x01, // Collation Application
0x09, 0x02, // Mouse
0x15, 0x00, // Logical min ( 0H )
0x26, 0x00, 0xff, // Logical max ( FFH )
0x75, 0x08, // Report size ( 08H )
0x95, 0x40, // Report count ( 40H )
0x81, 0x06, // Input ( Data, Relative, Wrap )
0x09, 0x02, // Mouse
0x15, 0x00, // Logical min ( 0H )
0x26, 0x00, 0xff, // Logical max ( FFH )
0x75, 0x08, // Report size ( 08H )
0x95, 0x40, // Report count ( 40H )
0x91, 0x06, // Output ( Data, Relative, Wrap )
0xc0,
};
// 配置描述符
UINT8C CfgDesc[9+9+9+7+9+9+7+7] =
{
0x09,0x02,0x42,0x00,0x02,0x01,0x00,0xA0,0x32, // Configure Descriptor
// blen type tlen interface
// 0x09,0x02,0x22,0x00,0x01,0x01,0x00,0xA0,0x32,
0x09,0x04,0x00,0x00,0x01,0x03,0x01,0x01,0x00, // Interface Descriptor, keyboard
0x09,0x21,0x11,0x01,0x00,0x01,0x22,sizeof(KeyRepDesc),0x00, // HID Descriptor
0x07,0x05,0x81,0x03,0x08,0x00,0x0a, // Endpoint Descriptor, 50ms
// 0x09,0x02,0x29,0x00,0x01,0x01,0x00,0xA0,0x32, // Configure Descriptor
// 0x09,0x04,0x00,0x00,0x02,0x03,0x00,0x00,0x00, // Interface Descriptor, Composite device 2 EPs
0x09,0x04,0x01,0x00,0x02,0x03,0x00,0x00,0x00, // Interface Descriptor, Composite device 2 EPs
0x09,0x21,0x10,0x01,0x21,0x01,0X22,sizeof(ComRepDesc),0x00, // HID Descriptor
0x07,0x05,0x82,0x03,0x40,0x00,0x32, // Endpoint Descriptor, 10ms, IN
0x07,0x05,0x02,0x03,0x40,0x00,0x0a // Endpoint Descriptor, 10ms, OUT
};
// 语言描述符
UINT8C MyLangDescr[] = { 0x04, 0x03, 0x09, 0x04 };
// 厂家信息
UINT8C MyManuInfo[] = { 0x0E, 0x03, 'w', 0, 'c', 0, 'h', 0, '.', 0, 'c', 0, 'n', 0 };
// 产品信息
UINT8C MyProdInfo[] = { 0x0C, 0x03, 'C', 0, 'H', 0, '5', 0, '5', 0, '4', 0 };
/*******************************************************************************
* Function Name : USBDeviceInit()
* Description : Configure USB mode USB device init configure.Configure tie Endpoint, compound device,
Endpoint 0 control trans, Endpoint 1/2 interrupt(IN).
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USBDeviceInit()
{
IE_USB = 0;
USB_CTRL = 0x00; // Device mode
UEP1_DMA = Ep1Buffer; // Endpoint 1 uploard address
UEP0_DMA = Ep0Buffer; // Endpoint 0 trans address
UEP2_DMA = Ep2Buffer; // Endpoint 2 uploard address
UEP4_1_MOD = UEP4_1_MOD | bUEP1_TX_EN & ~(bUEP1_RX_EN | bUEP1_BUF_MOD | bUEP4_RX_EN | bUEP4_TX_EN); // Endpoint 1 sigle 64 byte send buffer, Endpoint 4 disable
UEP1_CTRL = bUEP_AUTO_TOG | UEP_T_RES_NAK; // Endpoint 1 auto Toggle flag, In translation return NAK
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK; // Endpoint 0 OUT trans return ACK, IN trans return NAK
UEP2_3_MOD = UEP2_3_MOD | bUEP2_TX_EN | bUEP2_RX_EN & ~bUEP2_BUF_MOD; // Endpoint 2 sigle 64 byte send buffer OUT[64]+IN[64] (OUT first)
UEP2_CTRL = bUEP_AUTO_TOG | UEP_T_RES_NAK; // Endpoint 2 auto Toggle flag, IN trans return NAK
USB_DEV_AD = 0x00; // Initial the device address
UDEV_CTRL = bUD_PD_DIS; // Disable DM/PM PULL_DOWN.
USB_CTRL = bUC_DEV_PU_EN | bUC_INT_BUSY | bUC_DMA_EN; // Enable inter PULL_UP. Auto return NAK, during interrupt routing, if the interrupt flag.
UDEV_CTRL |= bUD_PORT_EN; // Enable USB port.
USB_INT_FG = 0xFF; // Clear interrupt flag.
USB_INT_EN = bUIE_SUSPEND | bUIE_TRANSFER | bUIE_BUS_RST; // Enable USB interrupt
IE_USB = 1; // Enable USB interrupt
}
/*******************************************************************************
* Function Name : Enp1IntIn()
* Description : USB Device mode Endpoint 1 upload data, Interrupt mode
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void Enp1IntIn( void )
{
memcpy( Ep1Buffer, HIDKey, sizeof(HIDKey)); // Upload data
UEP1_T_LEN = sizeof(HIDKey); // Upload length
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; // Return ACK
while(( UEP1_CTRL & MASK_UEP_T_RES ) == UEP_T_RES_ACK); // Waiting upload complete, avoid overwriting
}
/*******************************************************************************
* Function Name : Enp2TX()
* Description : USB Device mode Endpoint 2 upload data, Interrupt mode
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void Enp2TX( void )
{
memcpy( &Ep2Buffer[64], TX, sizeof(TX)); // Upload data
UEP2_T_LEN = sizeof(TX); // Upload length
UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; // Return ACK
while(( UEP2_CTRL & MASK_UEP_T_RES ) == UEP_T_RES_ACK); // Waiting upload complete, avoid overwriting
}
/*******************************************************************************
* Function Name : Enp2TX()
* Description : USB Device mode Endpoint 2 upload data, Interrupt mode
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void Enp2RX()
{
UINT8 len;
len = USB_RX_LEN;
if(len>3)
{
memcpy(RX,Ep2Buffer, sizeof(RX));
memcpy(TX,RX, sizeof(RX));
RXflag = len;
Enp2TX();
}
//UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_R_RES | UEP_R_RES_NAK;
}
/*******************************************************************************
* Function Name : Enp2TX()
* Description : USB Device mode Endpoint 2 upload data, Interrupt mode
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void USB_Setup()
{
}
/*******************************************************************************
* Function Name : DeviceInterrupt()
* Description : USB ISR
*******************************************************************************/
void DeviceInterrupt( void ) interrupt INT_NO_USB using 1 //USB ISR, Using register 1
{
UINT8 len;
if( UIF_TRANSFER ) //USB trans flag
{
switch (USB_INT_ST & (MASK_UIS_TOKEN | MASK_UIS_ENDP) )
{
case UIS_TOKEN_IN | 1: // Endpoint 1 upload
{
UEP1_T_LEN = 0; // Reset length
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; // Default return NAK
FLAG = 1; // Upload complete flag
break;
}
case UIS_TOKEN_IN | 2: // Endpoint 2 upload
{
UEP2_T_LEN = 0; // Reset length
UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; // Default return NAK
break;
}
case UIS_TOKEN_OUT | 2: // Eendpoint 2 download
{
Enp2RX();
UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_R_RES | UEP_T_RES_NAK; // Default return NAK
break;
}
case UIS_TOKEN_SETUP | 0: // SETUP packet
{
len = USB_RX_LEN;
if( len == (sizeof(USB_SETUP_REQ)) )
{
SetupLen = UsbSetupBuf->wLengthL;
if(UsbSetupBuf->wLengthH || SetupLen > 0x7F )
{
SetupLen = 0x7F; // Total length less than 0X7F
}
len = 0; // Default length is 0
if( (UsbSetupBuf->bRequestType & USB_REQ_TYP_MASK) != USB_REQ_TYP_STANDARD ) /* 非标准请求 */
{
if( (UsbSetupBuf->bRequestType == 0x21)&&(UsbSetupBuf->bRequest == HID_SET_REPORT) )//Init ok
{
EnumOK = 1; // Class require, SET REPORT, indicate the enumerate complete
}
if( (UsbSetupBuf->bRequestType == 0x21)&&(UsbSetupBuf->bRequest == HID_SET_IDLE) )
{
UEP0_T_LEN = 0; //Status stage complete, upload 0 data packet, end the control trans
// UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_STALL | UEP_T_RES_STALL;
}
len = 0; // Fail
}
else
{ // Deal with the standard require
SetupReq = UsbSetupBuf->bRequest;
switch( SetupReq ) // Require code
{
case USB_GET_DESCRIPTOR:
{
switch( UsbSetupBuf->wValueH )
{
case 1: // Device descriptor// 设备描述符
pDescr = DevDesc; // Write device descriptor to buffer
len = sizeof( DevDesc );
break;
case 2: // Configuration descriptor // 配置描述符
pDescr = CfgDesc; // Write configuration descriptor to buffer
len = sizeof( CfgDesc );
break;
case 3: // 字符串描述符
{
switch( UsbSetupBuf->wValueL )
{
case 1:
pDescr = (PUINT8)( &MyManuInfo[0] );
len = sizeof( MyManuInfo );
break;
case 2:
pDescr = (PUINT8)( &MyProdInfo[0] );
len = sizeof( MyProdInfo );
break;
case 0:
pDescr = (PUINT8)( &MyLangDescr[0] );
len = sizeof( MyLangDescr );
break;
default:
len = 0xFF; // 不支持的字符串描述符
break;
}
break;
}
case 0x22: // HID report descriptor
{
if( UsbSetupBuf->wIndexL == 0 ) // Interface index 0 HID report --> keyboard
{
pDescr = KeyRepDesc; // Write to buffer
len = sizeof( KeyRepDesc );
}
// if( UsbSetupBuf->wIndexL == 0 ) // Interface index 1 HID report --> Composite device
// {
// pDescr = ComRepDesc; // Write to buffer
// len = sizeof( ComRepDesc );
// }
else if(UsbSetupBuf->wIndexL == 1) // Interface index 1 HID report --> Composite device
{
pDescr = ComRepDesc; // Write to buffer
len = sizeof( ComRepDesc );
}
else
{
len = 0xff; // Add interface index, if required
}
break;
}
default:
len = 0xff; // Length equal 0 if code error
break;
}
if ( len == 0xff ){
break; // Error code, jump out the swith sentence
}
if ( SetupLen > len ){
SetupLen = len; // Limit the total length
}
len = SetupLen >= 8 ? 8 : SetupLen; // Upload this length
memcpy( Ep0Buffer, pDescr, len ); // Upload data
SetupLen -= len;
pDescr += len;
break;
}
case USB_SET_ADDRESS:
SetupLen = UsbSetupBuf->wValueL; // Save the device address
break;
case USB_GET_CONFIGURATION:
{
Ep0Buffer[0] = UsbConfig;
if ( SetupLen >= 1 )
{
len = 1;
}
break;
}
case USB_SET_CONFIGURATION:
{
UsbConfig = UsbSetupBuf->wValueL;
break;
}
case 0x0A:
break;
case USB_CLEAR_FEATURE: //Clear Feature
{
if ( (UsbSetupBuf->bRequestType & USB_REQ_RECIP_MASK) == USB_REQ_RECIP_ENDP )
{
switch( UsbSetupBuf->wIndexL )
{
case 0x82:
UEP2_CTRL = UEP2_CTRL & ~ (bUEP_T_TOG | MASK_UEP_T_RES) | UEP_T_RES_NAK;
break;
case 0x81:
UEP1_CTRL = UEP1_CTRL & ~ (bUEP_T_TOG | MASK_UEP_T_RES) | UEP_T_RES_NAK;
break;
case 0x01:
UEP1_CTRL = UEP1_CTRL & ~ (bUEP_R_TOG | MASK_UEP_R_RES) | UEP_R_RES_ACK;
break;
default:
len = 0xFF; // Error endpoint
break;
}
}
else
{
len = 0xFF; // Deal with the error endpoint
}
break;
}
case USB_SET_FEATURE: // Set Feature
{
if( (UsbSetupBuf->bRequestType & 0x1F) == 0x00 )
{
if( ( ((UINT16)UsbSetupBuf->wValueH << 8) | UsbSetupBuf->wValueL ) == 0x01 )
{
if( CfgDesc[ 7 ] & 0x20 )
{
/* Awake the device */
}
else
{
len = 0xFF; // Error routing
}
}
else
{
len = 0xFF; // Error routing
}
}
else
{
if( (UsbSetupBuf->bRequestType & 0x1F) == 0x02 ) // Set endpoint
{
if( ( ((UINT16)UsbSetupBuf->wValueH << 8) | UsbSetupBuf->wValueL ) == 0x00 )
{
switch( ((UINT16)UsbSetupBuf->wIndexH << 8) | UsbSetupBuf->wIndexL )
{
case 0x82:
UEP2_CTRL = UEP2_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL;/* Endpoint 2 IN STALL */
break;
case 0x02:
UEP2_CTRL = UEP2_CTRL & (~bUEP_R_TOG) | UEP_R_RES_STALL;/* Endpoint 2 OUT STALL */
break;
case 0x81:
UEP1_CTRL = UEP1_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL;/* Endpoint 1 IN STALL */
break;
default:
len = 0xFF; // Error routing
break;
}
}
else
{
len = 0xFF; // Error routing
}
}
else
{
len = 0xFF; // Error routing
}
}
break;
}
case USB_GET_STATUS: // Get status
{
Ep0Buffer[0] = 0x00;
Ep0Buffer[1] = 0x00;
if ( SetupLen >= 2 )
{
len = 2;
}
else
{
len = SetupLen;
}
break;
}
default:
len = 0xff; // Error routing
break;
}
}
}
else
{
len = 0xff; //Packet length error
}
if(len == 0xff)
{
SetupReq = 0xFF;
// UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_STALL | UEP_T_RES_STALL; // Control endpoint should not return STALL,
// if Send STALL, that means the device has no ability to communicate with the host
}
else if(len <= 8) // Upload the data or return the 0 data packet
{
UEP0_T_LEN = len;
UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK; // Default data packet is DATA1, and return ACK
}
else
{
UEP0_T_LEN = 0; // Upload 0 data packet, incase the host go to status stage ahead, and get fault code
UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK; // Default data packet is DATA1, and return ACK
}
break;
}
case UIS_TOKEN_IN | 0: // Endpoint 0 IN
{
switch(SetupReq)
{
case USB_GET_DESCRIPTOR:
len = SetupLen >= 8 ? 8 : SetupLen; // Current length
memcpy( Ep0Buffer, pDescr, len ); // Upload the data
SetupLen -= len;
pDescr += len;
UEP0_T_LEN = len;
UEP0_CTRL ^= bUEP_T_TOG; //Toggle Sysc flag
break;
case USB_SET_ADDRESS:
USB_DEV_AD = USB_DEV_AD & bUDA_GP_BIT | SetupLen;
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
break;
default:
// UEP0_T_LEN = 0; //Status stage complete, upload 0 data packet, end the control trans
// UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
break;
}
if(len == 0xff)
{
UEP0_T_LEN = 0; //Status stage complete, upload 0 data packet, end the control trans
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
}
break;
}
case UIS_TOKEN_OUT | 0: // Endpoint 0 OUT
{
len = USB_RX_LEN;
if( SetupReq == 0x09 )
{
if( Ep0Buffer[0] )
{
}
else if( Ep0Buffer[0] == 0)
{
}
}
UEP0_T_LEN = 0; // Upload 0 data packet, incase the host go to status stage ahead, and get fault code
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_ACK; // Default data packet is DATA1, and return ACK
break;
}
default:
break;
}
UIF_TRANSFER = 0; // Reset the trans interrupt
}// end if( UIF_TRANSFER )
if( UIF_BUS_RST ) // Device mode Bus reset
{
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
UEP1_CTRL = bUEP_AUTO_TOG | UEP_R_RES_ACK;
UEP2_CTRL = bUEP_AUTO_TOG | UEP_R_RES_ACK | UEP_T_RES_NAK;
USB_DEV_AD = 0x00;
UIF_SUSPEND = 0;
UIF_TRANSFER = 0;
UIF_BUS_RST = 0; // Reset Reset interrupt
}
if( UIF_SUSPEND ) // USB Suspended and Awake interrupt
{
UIF_SUSPEND = 0;
if ( USB_MIS_ST & bUMS_SUSPEND ) // Suspended
{
while ( XBUS_AUX & bUART0_TX ); // Waiting send complete ...
SAFE_MOD = 0x55;
SAFE_MOD = 0xAA;
WAKE_CTRL = bWAK_BY_USB | bWAK_RXD0_LO; // Action on USB Bus, or RXD0, will awake the device
PCON |= PD; // LPM
SAFE_MOD = 0x55;
SAFE_MOD = 0xAA;
WAKE_CTRL = 0x00;
}
}
else
{ // Unexpected interrupt
USB_INT_FG = 0xFF; // Reset the interrupt
}
}
/*******************************************************************************
* Function Name : static SendKey( char *p)
* Description : ASCII to key code
* Input : char *p
* Output : None
* Return : None
*******************************************************************************/
static void SendKey( char *p )
{
char c = *p;
if( (c >= 'a') && (c <= 'z' ))
{
c = c - 'a' + 'A';
}
if( (c >= 'A') && (c <= 'Z' ))
{
HIDKey[2] = c - 'A' + 4;
}
else
{
if( c >= '1' && c <= '9' ) HIDKey[2] = c - '1' + 0X1E;
else
{
switch ( c )
{
case '`' :
HIDKey[0] = 0X08;
HIDKey[2] = 0X15;
break;
case '\\':
HIDKey[2] = 0x31;
break;
case ' ':
HIDKey[2] = L_SHIFT;
break;
case '\r':
HIDKey[2] = ENTER;
break;
case ':':
HIDKey[0] = 0x02;
HIDKey[2] = 0x33;
break;
case '+':
HIDKey[0] = 0x000;
HIDKey[2] = 0x57;
break;
case '_':
HIDKey[0] = 0X02;
HIDKey[2] = 0X2D;
break;
case '/':
HIDKey[0] = L_CTL + L_ALT;
HIDKey[2] = 0X16;
break;
case '0':
HIDKey[2] = 0X27;
break;
case '.':
HIDKey[2] = 0X37;
break;
case '~':
HIDKey[0] = L_ALT;
HIDKey[2] = 0X05;
break;
case '!':
HIDKey[0] = L_ALT;
HIDKey[2] = 0X08;
break;
default:
break;
}
}
}
mDelaymS( 20 ); //emulate the press down and pop up
while(FLAG == 0); // Upload the key
Enp1IntIn();
while(FLAG == 0);
mDelaymS( 20 );
HIDKey[0] = 0X00;
HIDKey[2] = 0X00; // Reset the key
while(FLAG == 0);
Enp1IntIn();
while(FLAG == 0);
}
/*******************************************************************************
* Function Name : extern HIDValueHandle( void )
* Description : Upload the HID code
* Input : None
* Output : None
* Return : None
*******************************************************************************/
char code sPath[] = "` E:\\vck1\\hid_recv.exe \r "; // The path for the software. SPACE for delay
char *pStr = sPath;
char code s1Path[] = "` \r ";
char *p1Str = s1Path;
void TSTKey(void)
{
UINT16 KeyData;
if(TKEY_CTRL&bTKC_IF)
{
KeyData = TKEY_DAT;
if (KeyData < (TouchKeyButton-100)) //100 灵敏度调节
{
TXflag =0x04 ;
}
#ifdef DE_PRINTF
printf("B.= %04x\n",KeyData&0x7FFF);
#endif
}
}
void TXflagHandle(UINT8 val)
{
TSTKey();
if(TXflag & 0X01)
{
SendKey(pStr); // Upload path
pStr++;
if(*pStr == '\0')
{
SendKey( "~" ); // Upload ALT+B
mDelaymS( 200 );
TXflag &= 0xFE;
}
}
else if(TXflag & 0X02)
{
SendKey(p1Str); // Upload path
pStr++;
if(*p1Str == '\0')
{
SendKey( "~" ); // Upload ALT+B
mDelaymS( 200 );
TXflag &= 0xFE;
}
}
else if(TXflag & 0x04)
{
char btn[3];
btn[0]= val+ 0x30;
//SendKey(btn);
SendKey("` ");
//TX[0] = val;
//Enp2TX();
TXflag &= 0xFB;
}
else if(TXflag & 0x08)
{
SendKey("A");
//TX[0] = val;
//Enp2TX();
TXflag &= 0xFB;
}
}
/**************************** END *************************************/

25
HID/usr/USB.h Normal file
View File

@ -0,0 +1,25 @@
/********************************** (C) COPYRIGHT *******************************
* File Name :Compound_Dev.C *
* Author : WCH *
* Version : V1.2 *
* Date : 2017/02/24 *
* Description : A demo for USB compound device created by CH554, support *
keyboard , and HID-compliant device. *
********************************************************************************/
#ifndef __COMPOUND_H__
#define __COMPOUND_H__
extern UINT16I TouchKeyButton;
extern UINT8 TX[64];
extern UINT8 RX[64];
extern UINT8 TXflag;
extern UINT8 RXflag;
extern void USBDeviceInit();
extern void TXflagHandle(UINT8 val);
#endif
/**************************** END *************************************/

66
HID/usr/main.c Normal file
View File

@ -0,0 +1,66 @@
/********************************** (C) COPYRIGHT ******************************
* File Name : main.c
* Author : QITAS
* Version : V1.0
* Date : 2018/06/05
* Description : A demo for HID compatible device.
********************************************************************************/
#include "CH554.H"
#include "USB.h"
#include "debug.h"
#include "Key.h"
#include "stdio.h"
extern UINT8 FLAG; // Trans complete flag
extern UINT8 EnumOK; // Enum ok flag
extern UINT8 TX[64];
extern UINT8 RX[64];
UINT16I TouchKeyButton = 0;
//UINT8X TSTDATA[64] _at_ 0x00E0;
void CfgCLK(void)
{
SAFE_MOD = 0x55;
SAFE_MOD = 0xAA;
CLOCK_CFG = CLOCK_CFG & ~ MASK_SYS_CK_SEL | 0x06; // 24MHz
SAFE_MOD = 0x00;
}
void main( void )
{
UINT8 i;
CfgCLK(); //Configure sys
mDelaymS(10);
mInitSTDIO( ); // Init UART0
USBDeviceInit(); // Configure the USB module,the Endpoint and the USB Interrupt
EA = 1;
UEP1_T_LEN = 0; // Reset the trans length register
UEP2_T_LEN = 0;
FLAG = 0;
EnumOK = 0;
TouchKeyQueryCylSet1Or2ms(1); //set query cycle
TouchKeyChanelSelect(3); //set query chanel
for( i = 1; i < 64 ;i ++ )
{
TX[i]=i+1;
}
while(1)
{
if( EnumOK == 1 )
{
TXflagHandle(2);
}
else
{
if(TKEY_CTRL&bTKC_IF) //wait query finsh,max 2ms 获取键值基准值,多采样几次
{
TouchKeyButton = TKEY_DAT;
}
}
if(RXflag);
i++;
}
}
/**************************** END *************************************/

View File

@ -16,6 +16,10 @@
包括芯片手册资料等
- HID
包括一个HID通信的工程代码简介易用
- examples
同CH554的官方例程