From a89f877252f3e5d945fc0cd877026dc19d015cd1 Mon Sep 17 00:00:00 2001 From: qitas Date: Wed, 17 Oct 2018 16:44:45 +0800 Subject: [PATCH] add examples --- README.md | 4 + examples/ADC/ADC.C | 108 +++ examples/ADC/ADC.H | 34 + examples/ADC/Main.C | 52 + examples/CH554.uvproj | 374 ++++++++ examples/DataFlash/DataFlash.C | 65 ++ examples/DataFlash/DataFlash.H | 18 + examples/DataFlash/Main.C | 44 + examples/GPIO/GPIO.C | 118 +++ examples/GPIO/GPIO.H | 25 + examples/GPIO/Main.C | 39 + examples/IAP/CH554IAPDemo.C | 44 + examples/PWM/Main.C | 56 ++ examples/PWM/PWM.C | 42 + examples/PWM/PWM.H | 27 + examples/Public/CH554.H | 1217 ++++++++++++++++++++++++ examples/Public/Debug.C | 271 ++++++ examples/Public/Debug.H | 46 + examples/SPI/MainMaster.C | 60 ++ examples/SPI/MainSlave.C | 57 ++ examples/SPI/SPI.C | 141 +++ examples/SPI/SPI.H | 67 ++ examples/Timer/Main.C | 76 ++ examples/Timer/Timer.C | 153 +++ examples/Timer/Timer.H | 92 ++ examples/TouchKey/Main.C | 59 ++ examples/TouchKey/TouchKey.C | 120 +++ examples/TouchKey/TouchKey.H | 49 + examples/USB/Device/CompatibilityHID.C | 450 +++++++++ examples/USB/Device/CompositeKM.C | 514 ++++++++++ examples/USB/Device/VendorDefinedDev.C | 335 +++++++ examples/USB/Host/USBHostHUB_KM.C | 256 +++++ .gitignore => keil/.gitignore | 0 {usr => keil/usr}/CH554.h | 0 {usr => keil/usr}/GPIO.c | 0 {usr => keil/usr}/GPIO.h | 0 {usr => keil/usr}/LCD.c | 0 {usr => keil/usr}/LCD.h | 0 {usr => keil/usr}/STARTUP.A51 | 0 {usr => keil/usr}/Timer.c | 0 {usr => keil/usr}/Timer.h | 0 {usr => keil/usr}/USB.c | 0 {usr => keil/usr}/USB.h | 0 {usr => keil/usr}/common.c | 0 {usr => keil/usr}/common.h | 0 {usr => keil/usr}/font.c | 0 {usr => keil/usr}/font.h | 0 {usr => keil/usr}/main.c | 0 48 files changed, 5013 insertions(+) create mode 100644 examples/ADC/ADC.C create mode 100644 examples/ADC/ADC.H create mode 100644 examples/ADC/Main.C create mode 100644 examples/CH554.uvproj create mode 100644 examples/DataFlash/DataFlash.C create mode 100644 examples/DataFlash/DataFlash.H create mode 100644 examples/DataFlash/Main.C create mode 100644 examples/GPIO/GPIO.C create mode 100644 examples/GPIO/GPIO.H create mode 100644 examples/GPIO/Main.C create mode 100644 examples/IAP/CH554IAPDemo.C create mode 100644 examples/PWM/Main.C create mode 100644 examples/PWM/PWM.C create mode 100644 examples/PWM/PWM.H create mode 100644 examples/Public/CH554.H create mode 100644 examples/Public/Debug.C create mode 100644 examples/Public/Debug.H create mode 100644 examples/SPI/MainMaster.C create mode 100644 examples/SPI/MainSlave.C create mode 100644 examples/SPI/SPI.C create mode 100644 examples/SPI/SPI.H create mode 100644 examples/Timer/Main.C create mode 100644 examples/Timer/Timer.C create mode 100644 examples/Timer/Timer.H create mode 100644 examples/TouchKey/Main.C create mode 100644 examples/TouchKey/TouchKey.C create mode 100644 examples/TouchKey/TouchKey.H create mode 100644 examples/USB/Device/CompatibilityHID.C create mode 100644 examples/USB/Device/CompositeKM.C create mode 100644 examples/USB/Device/VendorDefinedDev.C create mode 100644 examples/USB/Host/USBHostHUB_KM.C rename .gitignore => keil/.gitignore (100%) rename {usr => keil/usr}/CH554.h (100%) rename {usr => keil/usr}/GPIO.c (100%) rename {usr => keil/usr}/GPIO.h (100%) rename {usr => keil/usr}/LCD.c (100%) rename {usr => keil/usr}/LCD.h (100%) rename {usr => keil/usr}/STARTUP.A51 (100%) rename {usr => keil/usr}/Timer.c (100%) rename {usr => keil/usr}/Timer.h (100%) rename {usr => keil/usr}/USB.c (100%) rename {usr => keil/usr}/USB.h (100%) rename {usr => keil/usr}/common.c (100%) rename {usr => keil/usr}/common.h (100%) rename {usr => keil/usr}/font.c (100%) rename {usr => keil/usr}/font.h (100%) rename {usr => keil/usr}/main.c (100%) diff --git a/README.md b/README.md index 63474f3..0e3a9a4 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,10 @@ 包括芯片手册资料等 +- examples + +同CH554的官方例程 + --- diff --git a/examples/ADC/ADC.C b/examples/ADC/ADC.C new file mode 100644 index 0000000..1785392 --- /dev/null +++ b/examples/ADC/ADC.C @@ -0,0 +1,108 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : ADC.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554 ADCʱãADCͨúѹȽģʽ +*******************************************************************************/ + +#include "..\Public\CH554.H" +#include "..\Public\Debug.H" +#include "ADC.H" +#include "stdio.h" + +#pragma NOAREGS + +#define ADC_INTERRUPT 1 + +/******************************************************************************* +* Function Name : ADCInit(UINT8 div) +* Description : ADCʱ,ģ鿪жϿ +* Input : UINT8 div ʱ + 1 384Fosc + 0 96Fosc +* Output : None +* Return : None +*******************************************************************************/ +void ADCInit(UINT8 div) +{ + ADC_CFG &= ~bADC_CLK | div; + ADC_CFG |= bADC_EN; //ADCԴʹ +#if ADC_INTERRUPT + ADC_IF = 0; //ж + IE_ADC = 1; //ʹADCж +#endif +} + +/******************************************************************************* +* Function Name : ADC_ChannelSelect(UINT8 ch) +* Description : ADC +* Input : UINT8 ch ͨ +* Output : None +* Return : ɹ SUCCESS + ʧ FAIL +*******************************************************************************/ +UINT8 ADC_ChannelSelect(UINT8 ch) +{ + if(ch == 0){ADC_CHAN1 =0;ADC_CHAN0=0;P1_DIR_PU &= ~bAIN0;} //AIN0 + else if(ch == 1){ADC_CHAN1 =0;ADC_CHAN0=1;P1_DIR_PU &= ~bAIN1;} //AIN1 + else if(ch == 2){ADC_CHAN1 =1;ADC_CHAN0=0;P1_DIR_PU &= ~bAIN2;} //AIN2 + else if(ch == 3){ADC_CHAN1 =1;ADC_CHAN0=1;P3_DIR_PU &= ~bAIN3;} //AIN3 + else return FAIL; + return SUCCESS; +} + +/******************************************************************************* +* Function Name : VoltageCMPModeInit() +* Description : ѹȽģʽʼ +* Input : UINT8 fo ˿ 0\1\2\3 + UINT8 re ˿ 1\3 +* Output : None +* Return : ɹ SUCCESS + ʧ FAIL +*******************************************************************************/ +UINT8 VoltageCMPModeInit(UINT8 fo,UINT8 re) +{ + ADC_CFG |= bCMP_EN; //ƽȽϵԴʹ + if(re == 1){ + if(fo == 0) {ADC_CHAN1 =0;ADC_CHAN0=0;CMP_CHAN =0;} //AIN0AIN1 + else if(fo == 2) {ADC_CHAN1 =1;ADC_CHAN0=0;CMP_CHAN =0;} //AIN2AIN1 + else if(fo == 3) {ADC_CHAN1 =1;ADC_CHAN0=1;CMP_CHAN =0; } //AIN3AIN1 + else return FAIL; + } + else if(re == 3){ + if(fo == 0) {ADC_CHAN1 =0;ADC_CHAN0=0;CMP_CHAN =0;} //AIN0AIN1 + else if(fo == 1) {ADC_CHAN1 =0;ADC_CHAN0=1;CMP_CHAN =0;} //AIN1AIN1 + else if(fo == 2) {ADC_CHAN1 =1;ADC_CHAN0=0;CMP_CHAN =0;} //AIN2AIN1 + else return FAIL; + } + else return FAIL; +#if ADC_INTERRUPT + CMP_IF = 0; //ж + IE_ADC = 1; //ʹADCж +#endif + + return SUCCESS; +} + +#if ADC_INTERRUPT +/******************************************************************************* +* Function Name : ADCInterrupt(void) +* Description : ADC жϷ +*******************************************************************************/ +void ADCInterrupt( void ) interrupt INT_NO_ADC using 1 //ADCжϷ,ʹüĴ1 +{ + if(ADC_IF == 1) //ADCж + { +// UserData = ADC_DATA; //ȡADC + ADC_IF = 0; //ADCжϱ־ + } + if(CMP_IF == 1) //ѹȽж + { +// UserData = ADC_CTRL&0x80 >> 7); //Ƚ + CMP_IF = 0; //ձȽж + } +} +#endif + diff --git a/examples/ADC/ADC.H b/examples/ADC/ADC.H new file mode 100644 index 0000000..780eb4a --- /dev/null +++ b/examples/ADC/ADC.H @@ -0,0 +1,34 @@ + +#define ADC_INTERRUPT 1 + +/******************************************************************************* +* Function Name : ADCClkSet(UINT8 div) +* Description : ADCʱ,ģ鿪жϿ +* Input : UINT8 div ʱ + 0 384Fosc + 1 96Fosc +* Output : None +* Return : None +*******************************************************************************/ +void ADCInit(UINT8 div); + +/******************************************************************************* +* Function Name : ADC_ChannelSelect(UINT8 ch) +* Description : ADCͨ +* Input : UINT8 ch ͨ0-3 +* Output : None +* Return : ɹ SUCCESS + ʧ FAIL ͨóΧ +*******************************************************************************/ +UINT8 ADC_ChannelSelect(UINT8 ch); + +/******************************************************************************* +* Function Name : VoltageCMPModeInit() +* Description : ѹȽģʽʼ +* Input : UINT8 fo ˿ 0\1\2\3 + UINT8 re ˿ 1\3 +* Output : None +* Return : ɹ SUCCESS + ʧ FAIL +*******************************************************************************/ +UINT8 VoltageCMPModeInit(UINT8 fo,UINT8 re); diff --git a/examples/ADC/Main.C b/examples/ADC/Main.C new file mode 100644 index 0000000..7b30e7a --- /dev/null +++ b/examples/ADC/Main.C @@ -0,0 +1,52 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : Main.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH544 ADCʼADCжϺͲѯʽɼʾʾ +*******************************************************************************/ +#include "..\Public\CH554.H" +#include "..\Public\Debug.H" +#include "ADC.H" +#include "stdio.h" +#include + +#pragma NOAREGS + +void main( ) +{ + UINT16 i; + UINT16 j = 0; + CfgFsys( ); //CH554ʱѡ + mDelaymS(20); + mInitSTDIO( ); //0ʼ + printf("start ...\n"); + + ADCInit( 0 ); //ADCʱã0(96clk) 1(384clk)ADCģ鿪 + +#if ADC_INTERRUPT //ADCжϷʽ + EA = 1; + while(1) + { + for(i=0;i<4;i++){ + printf("AIN%02x ",(UINT16)i); //ADCͨӡʾ + ADC_ChannelSelect( i ); //ADCԴͨãi(0-3)ʾͨ + ADC_START = 1; //ʼɽж + mDelayuS(30); //ȴɼɲлһͨ + } + } +#else + while(1) //ADC ѯʽ + { + for(i=0;i<4;i++){ + printf("AIN%02x ",(UINT16)i); + ADC_ChannelSelect( i ); //ADCʼ + ADC_START = 1; //ʼɽж + while(ADC_START); //ADC_STARTΪ0ʱʾ + printf("DATA: %02x\n",(UINT16)ADC_DATA); + mDelaymS(100); //ģⵥƬ + } + } +#endif +} \ No newline at end of file diff --git a/examples/CH554.uvproj b/examples/CH554.uvproj new file mode 100644 index 0000000..1b50b7f --- /dev/null +++ b/examples/CH554.uvproj @@ -0,0 +1,374 @@ + + + + 1.1 + +
### uVision Project, (C) Keil Software
+ + + + Target 1 + 0x0 + MCS-51 + + + SST89C54 + SST + IRAM(0-0xFF) IROM(0-0x3FFF) CLOCK(33000000) + + "LIB\STARTUP.A51" ("Standard 8051 Startup Code") + + 2932 + REG52.H + + + + + + + + + + + 0 + + + + + + + 0 + 0 + 0 + 0 + 1 + + .\ + CH554DEMO + 1 + 0 + 1 + 1 + 1 + .\ + 0 + 0 + 0 + + 0 + 0 + + + 0 + 0 + 0 + 0 + + + 0 + 0 + + + 0 + 0 + + + 0 + 0 + + + 0 + 0 + + 0 + + + + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 3 + + + 65535 + + + S8051.DLL + + DP51.DLL + -pSST5X + S8051.DLL + + TP51.DLL + -pSST5X + + + + 0 + 0 + 0 + 0 + 16 + + + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 0 + + + 0 + 1 + 0 + 1 + 1 + 1 + 0 + 1 + 0 + + 0 + -1 + + + + + + + + + + + + + + + + + + + 0 + 0 + 0 + 0 + 0 + -1 + + + "" () + + + + + 2 + 0 + 2 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + + + 0 + 0x0 + 0xffff + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 1 + 0x0 + 0x4000 + + + 0 + 0x0 + 0x100 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + + + 0 + 0 + 1 + 0 + 1 + 3 + 8 + 2 + 1 + 1 + 0 + 0 + + + + + + + + + 0 + 1 + 0 + 0 + + + + + + + + + 0 + 0 + 1 + 0 + 2 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Source Group 1 + + + Debug.C + 1 + .\Public\Debug.C + + + USBHOST.C + 1 + .\USB\USB_LIB\USBHOST.C + + + EXAM1.C + 1 + .\USB\U_DISK\EXAM1\EXAM1.C + + + + + + + +
diff --git a/examples/DataFlash/DataFlash.C b/examples/DataFlash/DataFlash.C new file mode 100644 index 0000000..0887e62 --- /dev/null +++ b/examples/DataFlash/DataFlash.C @@ -0,0 +1,65 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : DataFlash.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554 DataFlashֽڶд +*******************************************************************************/ + +#include "..\Public\CH554.H" +#include "..\Public\Debug.H" +#include "DataFlash.H" + +/******************************************************************************* +* Function Name : WriteDataFlash(UINT8 Addr,PUINT8 buf,UINT8 len) +* Description : DataFlashд +* Input : UINT8 AddrPUINT16 buf,UINT8 len +* Output : None +* Return : UINT8 i д볤 +*******************************************************************************/ +UINT8 WriteDataFlash(UINT8 Addr,PUINT8 buf,UINT8 len) +{ + UINT8 i; + SAFE_MOD = 0x55; + SAFE_MOD = 0xAA; //밲ȫģʽ + GLOBAL_CFG |= bDATA_WE; //ʹDataFlashд + SAFE_MOD = 0; //˳ȫģʽ + ROM_ADDR_H = DATA_FLASH_ADDR >> 8; + Addr <<= 1; + for(i=0;i 0) return i; // ״̬,0x00=success, 0x02=unknown command(bROM_CMD_ERR) + } + SAFE_MOD = 0x55; + SAFE_MOD = 0xAA; //밲ȫģʽ + GLOBAL_CFG &= ~bDATA_WE; //DataFlashд + SAFE_MOD = 0; //˳ȫģʽ + return i; +} + +/******************************************************************************* +* Function Name : ReadDataFlash(UINT8 Addr,UINT8 len,PUINT8 buf) +* Description : DataFlash +* Input : UINT8 Addr UINT8 len PUINT8 buf +* Output : None +* Return : UINT8 i д볤 +*******************************************************************************/ +UINT8 ReadDataFlash(UINT8 Addr,UINT8 len,PUINT8 buf) +{ + UINT8 i; + ROM_ADDR_H = DATA_FLASH_ADDR >> 8; + Addr <<= 1; + for(i=0;i + +void main( ) +{ + UINT8 i,m,len; + UINT16 j = 0; + CfgFsys( ); //CH554ʱѡ + mDelaymS(20); + mInitSTDIO( ); //0ʼ + printf("start ...\n"); + + while(1){ + for(i=0;i<128;i++){ //ѭд128ֽ + len = WriteDataFlash(i,&i,1); //DataFlashƫƵַiдi + if(len != 1){ + printf("Write Err = %02x,m = %02x\n",j,(UINT16)m); //дӡ + } + } + for(i=0;i<128;i++){ //DataFlashƫƵַiУ + len = ReadDataFlash(i,1,&m); + if((len != 1) ||(m != i)){ + printf("Read Err = %02x, = %02x,addr =%02x ,ֵ= %02x\n",j,(UINT16)(i*2),(UINT16)ROM_DATA_L,(UINT16)m); + } //Уӡ + } + printf("$$OK %02x \n",j); + j++; + mDelaymS(100); + } +} \ No newline at end of file diff --git a/examples/GPIO/GPIO.C b/examples/GPIO/GPIO.C new file mode 100644 index 0000000..a5d5ded --- /dev/null +++ b/examples/GPIO/GPIO.C @@ -0,0 +1,118 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : GPIO.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554 IO ýӿںGPIOжϺ +*******************************************************************************/ + +#include "..\Public\CH554.H" +#include "..\Public\Debug.H" +#include "GPIO.H" +#include "stdio.h" + +#pragma NOAREGS + +/******************************************************************************* +* Function Name : Port1Cfg() +* Description : ˿1 +* Input : Mode 0 = 룬 + 1 = + 2 = © + 3 = 51ģʽ©ڲ·Լɵ͵ߵĵƽ + ,UINT8 Pin (0-7) +* Output : None +* Return : None +*******************************************************************************/ +void Port1Cfg(UINT8 Mode,UINT8 Pin) +{ + switch(Mode){ + case 0: + P1_MOD_OC = P1_MOD_OC & ~(1< + +#pragma NOAREGS + +void main( ) +{ + UINT16 j = 0; + CfgFsys( ); //CH554ʱѡ + mDelaymS(20); + mInitSTDIO( ); //0ʼ + printf("start ...\n"); + +#if GPIO_INTERRUPT + GPIOInterruptCfg(); //GPIOжú + EA = 1; + printf("Run"); + while(1){ + printf("."); + mDelaymS(50); + } +#endif + Port1Cfg(3,4); //P14ó51˫ģʽ + while(1){ + SCS = ~SCS; + mDelaymS(50); + } +} \ No newline at end of file diff --git a/examples/IAP/CH554IAPDemo.C b/examples/IAP/CH554IAPDemo.C new file mode 100644 index 0000000..78bb53a --- /dev/null +++ b/examples/IAP/CH554IAPDemo.C @@ -0,0 +1,44 @@ + +/********************************** (C) COPYRIGHT ****************************** +* File Name : CH554IAPDemo.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : ϵкP17LED˸⡰EnableIAPΪ͵ƽ󣬽ûתBOOTͨBOOTû +*******************************************************************************/ +#include "./Public/CH554.H" +#include "./Public/Debug.H" + +sbit EnableIAP = P1^6; +#define BOOT_ADDR 0x3800 + +#pragma NOAREGS + +/******************************************************************************* +* Function Name : main +* Description : +* +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +typedef void( *pTaskFn)( void ); + +pTaskFn tasksArr[1]; + +void main( void ) +{ + UINT16 i=0; + while(1){ + SCK = ~SCK; //P17˸ + mDelaymS(50); + if(EnableIAP == 0){ //P16ż⵽͵ƽת + break; + } + } + EA = 0; //رжϣؼ + tasksArr[0] = BOOT_ADDR; + mDelaymS( 100 ); + (tasksArr[0])( ); //BOOT,ʹISP + while(1); +} diff --git a/examples/PWM/Main.C b/examples/PWM/Main.C new file mode 100644 index 0000000..1180fd7 --- /dev/null +++ b/examples/PWM/Main.C @@ -0,0 +1,56 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : Main.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554 PWMʼռձãPWMĬϵƽ + ֧жϷʽ޸PWMռձ +*******************************************************************************/ + +#include "..\Public\CH554.H" +#include "..\Public\Debug.H" +#include "PWM.H" +#include "stdio.h" + +#pragma NOAREGS + +main( ) +{ + CfgFsys( ); //CH554ʱѡ + mDelaymS(5); //ʱӺ󣬽ʱȶʱ + mInitSTDIO( ); //0ʼ + printf("start ...\n"); + + P1_MOD_OC &= ~(bPWM1 | bPWM2); //PWMΪ + P1_DIR_PU |= bPWM1 | bPWM2; + + SetPWMClk(4); //PWMʱ 4Ƶ + ForceClearPWMFIFO( ); //ǿPWM FIFOCOUNT + CancleClearPWMFIFO( ); //ȡPWM FIFOCOUNT + PWM1OutEnable( ); //PWM1 + PWM2OutEnable( ); //PWM2 + + PWM1OutPolarHighAct( ); //PWM1ĬϵͣЧ + PWM2OutPolarLowAct( ); //PWM2ĬϸߣЧ + +#if PWM_INTERRUPT + PWMInterruptEnable(); + EA = 1; + SetPWM1Dat(0x10); + SetPWM2Dat(0x40); + while(1); +#endif + SetPWM1Dat(0x10); //ռձ0x10/256 + SetPWM2Dat(0x40); + while(1){ + if(PWM_CTRL&bPWM_IF_END){ + PWM_CTRL |= bPWM_IF_END; //PWMж + SetPWM1Dat(0x10); + SetPWM2Dat(0x40); +#ifdef DE_PRINTF + printf("PWM_CYC_END %02X\n",(UINT16)PWM_CTRL); +#endif + } + } +} \ No newline at end of file diff --git a/examples/PWM/PWM.C b/examples/PWM/PWM.C new file mode 100644 index 0000000..59dd134 --- /dev/null +++ b/examples/PWM/PWM.C @@ -0,0 +1,42 @@ +/********************************** (C) COPYRIGHT ******************************* +* File Name : PWM.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554 PWMжʹܺжϴ +*******************************************************************************/ + +#include "..\Public\CH554.H" +#include "..\Public\Debug.H" +#include "PWM.H" +#include "stdio.h" + +#pragma NOAREGS + +#if PWM_INTERRUPT +/******************************************************************************* +* Function Name : PWMInterruptEnable() +* Description : PWMжʹ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void PWMInterruptEnable() +{ + PWM_CTRL |= bPWM_IF_END | bPWM_IE_END; //PWMжϣʹPWMж + IE_PWMX = 1; +} + +/******************************************************************************* +* Function Name : PWMInterrupt(void) +* Description : PWMжϷ +*******************************************************************************/ +void PWMInterrupt( void ) interrupt INT_NO_PWMX using 1 //PWM1&2жϷ,ʹüĴ1 +{ + PWM_CTRL |= bPWM_IF_END; //PWMж + SetPWM1Dat(0x10); + SetPWM2Dat(0x40); +// printf("PWM_CTRL %02X\n",(UINT16)PWM_CTRL); //ڲ鿴Ƿж +} +#endif + diff --git a/examples/PWM/PWM.H b/examples/PWM/PWM.H new file mode 100644 index 0000000..6e1ec3f --- /dev/null +++ b/examples/PWM/PWM.H @@ -0,0 +1,27 @@ + +#define PWM_INTERRUPT 1 + +#if PWM_INTERRUPT +extern void PWMInterruptEnable(); //PWMжʹ +#endif + +#define SetPWMClk(CK_SE) (PWM_CK_SE = CK_SE) //Ƶ,ĬʱFsys + +#define SetPWM1Dat(dat) (PWM_DATA1 = dat) //PWMռձ +#define SetPWM2Dat(dat) (PWM_DATA2 = dat) + +#define PWM1PINAlter( ) {PIN_FUNC |= bPWM1_PIN_X;} //PWMӳP30 +#define PWM2PINAlter( ) {PIN_FUNC |= bPWM2_PIN_X;} //PWMӳP31 + +#define ForceClearPWMFIFO( ) {PWM_CTRL |= bPWM_CLR_ALL;} //ǿPWM FIFOCOUNT +#define CancleClearPWMFIFO( ) {PWM_CTRL &= ~bPWM_CLR_ALL;} //ȡPWM FIFOCOUNT + +#define PWM1OutEnable() (PWM_CTRL |= bPWM1_OUT_EN) //PWM1 +#define PWM2OutEnable() (PWM_CTRL |= bPWM2_OUT_EN) //PWM2 +#define DsiablePWM1Out() (PWM_CTRL &= ~bPWM1_OUT_EN) //رPWM1 +#define DisablePWM2Out() (PWM_CTRL &= ~bPWM2_OUT_EN) //رPWM2 + +#define PWM1OutPolarHighAct()(PWM_CTRL &= ~bPWM1_POLAR) //PWM1ĬϵͣЧ +#define PWM2OutPolarHighAct()(PWM_CTRL &= ~bPWM2_POLAR) //PWM2ĬϵͣЧ +#define PWM1OutPolarLowAct() (PWM_CTRL |= bPWM1_POLAR) //PWM1ĬϸߣЧ +#define PWM2OutPolarLowAct() (PWM_CTRL |= bPWM2_POLAR) //PWM2ĬϸߣЧ \ No newline at end of file diff --git a/examples/Public/CH554.H b/examples/Public/CH554.H new file mode 100644 index 0000000..ab29ec5 --- /dev/null +++ b/examples/Public/CH554.H @@ -0,0 +1,1217 @@ +/*-------------------------------------------------------------------------- +CH554.H +Header file for CH554 microcontrollers. +**************************************** +** Copyright (C) W.ch 1999-2014 ** +** Web: http://wch.cn ** +**************************************** +--------------------------------------------------------------------------*/ + +// 3 blocks: __BASE_TYPE__, __CH554_H__, __USB_DEF__ + +#ifndef __BASE_TYPE__ +#define __BASE_TYPE__ + +#ifdef __cplusplus +extern "C" { +#endif + +/*----- constant and type define -----------------------------------------*/ + +#ifndef TRUE +#define TRUE 1 +#define FALSE 0 +#endif +#ifndef NULL +#define NULL 0 +#endif + +#ifndef BOOL +typedef bit BOOL; +#endif +#ifndef UINT8 +typedef unsigned char UINT8; +#endif +#ifndef UINT16 +typedef unsigned short UINT16; +#endif +#ifndef UINT32 +typedef unsigned long UINT32; +#endif +#ifndef UINT8D +typedef unsigned char data UINT8D; +#endif +#ifndef UINT16D +typedef unsigned short data UINT16D; +#endif +#ifndef UINT32D +typedef unsigned long data UINT32D; +#endif +#ifndef UINT8I +typedef unsigned char idata UINT8I; +#endif +#ifndef UINT16I +typedef unsigned short idata UINT16I; +#endif +#ifndef UINT32I +typedef unsigned long idata UINT32I; +#endif +#ifndef UINT8X +typedef unsigned char xdata UINT8X; +#endif +#ifndef UINT16X +typedef unsigned short xdata UINT16X; +#endif +#ifndef UINT32X +typedef unsigned long xdata UINT32X; +#endif +#ifndef UINT8V +typedef unsigned char volatile UINT8V; +#endif +#ifndef UINT8DV +typedef unsigned char volatile data UINT8DV; +#endif +#ifndef UINT8XV +typedef unsigned char volatile xdata UINT8XV; +#endif +#ifndef UINT8PV +typedef unsigned char volatile pdata UINT8PV; +#endif +#ifndef UINT8C +typedef const unsigned char code UINT8C; +#endif +#ifndef PUINT8 +typedef unsigned char *PUINT8; +#endif +#ifndef PUINT16 +typedef unsigned short *PUINT16; +#endif +#ifndef PUINT32 +typedef unsigned long *PUINT32; +#endif +#ifndef PUINT8I +typedef unsigned char idata *PUINT8I; +#endif +#ifndef PUINT16I +typedef unsigned short idata *PUINT16I; +#endif +#ifndef PUINT32I +typedef unsigned long idata *PUINT32I; +#endif +#ifndef PUINT8X +typedef unsigned char xdata *PUINT8X; +#endif +#ifndef PUINT16X +typedef unsigned short xdata *PUINT16X; +#endif +#ifndef PUINT32X +typedef unsigned long xdata *PUINT32X; +#endif +#ifndef PUINT8V +typedef unsigned char volatile *PUINT8V; +#endif +#ifndef PUINT8DV +typedef unsigned char volatile data *PUINT8DV; +#endif +#ifndef PUINT8XV +typedef unsigned char volatile xdata *PUINT8XV; +#endif +#ifndef PUINT8PV +typedef unsigned char volatile pdata *PUINT8PV; +#endif +#ifndef PUINT8C +typedef const unsigned char code *PUINT8C; +#endif + +#ifndef STRUCT_OFFSET +#define STRUCT_OFFSET( s, m ) ( (UINT8)( & (((s) *)0) -> (m) ) ) /* get the offset address for a member of a structure */ +#endif + +#ifdef __cplusplus +} +#endif + +#endif // __BASE_TYPE__ + +#ifndef __CH554_H__ +#define __CH554_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/*----- SFR --------------------------------------------------------------*/ +/* sbit are bit addressable, others are byte addressable */ + +/* System Registers */ +sfr PSW = 0xD0; // program status word + sbit CY = PSW^7; // carry flag + sbit AC = PSW^6; // auxiliary carry flag + sbit F0 = PSW^5; // bit addressable general purpose flag 0 + sbit RS1 = PSW^4; // register R0-R7 bank selection high bit + sbit RS0 = PSW^3; // register R0-R7 bank selection low bit +#define MASK_PSW_RS 0x18 // bit mask of register R0-R7 bank selection +// RS1 & RS0: register R0-R7 bank selection +// 00 - bank 0, R0-R7 @ address 0x00-0x07 +// 01 - bank 1, R0-R7 @ address 0x08-0x0F +// 10 - bank 2, R0-R7 @ address 0x10-0x17 +// 11 - bank 3, R0-R7 @ address 0x18-0x1F + sbit OV = PSW^2; // overflow flag + sbit F1 = PSW^1; // bit addressable general purpose flag 1 + sbit P = PSW^0; // ReadOnly: parity flag +sfr ACC = 0xE0; // accumulator +sfr B = 0xF0; // general purpose register B +sfr SP = 0x81; // stack pointer +//sfr16 DPTR = 0x82; // DPTR pointer, little-endian +sfr DPL = 0x82; // data pointer low +sfr DPH = 0x83; // data pointer high +sfr SAFE_MOD = 0xA1; // WriteOnly: writing safe mode +//sfr CHIP_ID = 0xA1; // ReadOnly: reading chip ID +#define CHIP_ID SAFE_MOD +sfr GLOBAL_CFG = 0xB1; // global config, Write@SafeMode +#define bBOOT_LOAD 0x20 // ReadOnly: boot loader status for discriminating BootLoader or Application: set 1 by power on reset, clear 0 by software reset +#define bSW_RESET 0x10 // software reset bit, auto clear by hardware +#define bCODE_WE 0x08 // enable flash-ROM (include code & Data-Flash) being program or erasing: 0=writing protect, 1=enable program and erase +#define bDATA_WE 0x04 // enable Data-Flash (flash-ROM data area) being program or erasing: 0=writing protect, 1=enable program and erase +#define bLDO3V3_OFF 0x02 // disable 5V->3.3V LDO: 0=enable LDO for USB and internal oscillator under 5V power, 1=disable LDO, V33 pin input external 3.3V power +#define bWDOG_EN 0x01 // enable watch-dog reset if watch-dog timer overflow: 0=as timer only, 1=enable reset if timer overflow + +/* Clock and Sleep and Power Registers */ +sfr PCON = 0x87; // power control and reset flag +#define SMOD 0x80 // baud rate selection for UART0 mode 1/2/3: 0=slow(Fsys/128 @mode2, TF1/32 @mode1/3, no effect for TF2), + // 1=fast(Fsys/32 @mode2, TF1/16 @mode1/3, no effect for TF2) +#define bRST_FLAG1 0x20 // ReadOnly: recent reset flag high bit +#define bRST_FLAG0 0x10 // ReadOnly: recent reset flag low bit +#define MASK_RST_FLAG 0x30 // ReadOnly: bit mask of recent reset flag +#define RST_FLAG_SW 0x00 +#define RST_FLAG_POR 0x10 +#define RST_FLAG_WDOG 0x20 +#define RST_FLAG_PIN 0x30 +// bPC_RST_FLAG1 & bPC_RST_FLAG0: recent reset flag +// 00 - software reset, by bSW_RESET=1 @(bBOOT_LOAD=0 or bWDOG_EN=1) +// 01 - power on reset +// 10 - watch-dog timer overflow reset +// 11 - external input manual reset by RST pin +#define GF1 0x08 // general purpose flag bit 1 +#define GF0 0x04 // general purpose flag bit 0 +#define PD 0x02 // power-down enable bit, auto clear by wake-up hardware +sfr CLOCK_CFG = 0xB9; // system clock config: lower 3 bits for system clock Fsys, Write@SafeMode +#define bOSC_EN_INT 0x80 // internal oscillator enable and original clock selection: 1=enable & select internal clock, 0=disable & select external clock +#define bOSC_EN_XT 0x40 // external oscillator enable, need quartz crystal or ceramic resonator between XI and XO pins +#define bWDOG_IF_TO 0x20 // ReadOnly: watch-dog timer overflow interrupt flag, cleared by reload watch-dog count or auto cleared when MCU enter interrupt routine +#define bROM_CLK_FAST 0x10 // flash-ROM clock frequency selection: 0=normal(for Fosc>=16MHz), 1=fast(for Fosc<16MHz) +#define bRST 0x08 // ReadOnly: pin RST input +#define bT2EX_ 0x08 // alternate pin for T2EX +#define bCAP2_ 0x08 // alternate pin for CAP2 +#define MASK_SYS_CK_SEL 0x07 // bit mask of system clock Fsys selection +/* + Fxt = 24MHz(8MHz~25MHz for non-USB application), from external oscillator @XI&XO + Fosc = bOSC_EN_INT ? 24MHz : Fxt + Fpll = Fosc * 4 => 96MHz (32MHz~100MHz for non-USB application) + Fusb4x = Fpll / 2 => 48MHz (Fixed) + MASK_SYS_CK_SEL[2] [1] [0] + Fsys = Fpll/3 = 32MHz: 1 1 1 + Fsys = Fpll/4 = 24MHz: 1 1 0 + Fsys = Fpll/6 = 16MHz: 1 0 1 + Fsys = Fpll/8 = 12MHz: 1 0 0 + Fsys = Fpll/16 = 6MHz: 0 1 1 + Fsys = Fpll/32 = 3MHz: 0 1 0 + Fsys = Fpll/128 = 750KHz: 0 0 1 + Fsys = Fpll/512 =187.5KHz: 0 0 0 +*/ +sfr WAKE_CTRL = 0xA9; // wake-up control, Write@SafeMode +#define bWAK_BY_USB 0x80 // enable wake-up by USB event +#define bWAK_RXD1_LO 0x40 // enable wake-up by RXD1 low level +#define bWAK_P1_5_LO 0x20 // enable wake-up by pin P1.5 low level +#define bWAK_P1_4_LO 0x10 // enable wake-up by pin P1.4 low level +#define bWAK_P1_3_LO 0x08 // enable wake-up by pin P1.3 low level +#define bWAK_RST_HI 0x04 // enable wake-up by pin RST high level +#define bWAK_P3_2E_3L 0x02 // enable wake-up by pin P3.2 (INT0) edge or pin P3.3 (INT1) low level +#define bWAK_RXD0_LO 0x01 // enable wake-up by RXD0 low level +sfr RESET_KEEP = 0xFE; // value keeper during reset +sfr WDOG_COUNT = 0xFF; // watch-dog count, count by clock frequency Fsys/65536 + +/* Interrupt Registers */ +sfr IE = 0xA8; // interrupt enable + sbit EA = IE^7; // enable global interrupts: 0=disable, 1=enable if E_DIS=0 + sbit E_DIS = IE^6; // disable global interrupts, intend to inhibit interrupt during some flash-ROM operation: 0=enable if EA=1, 1=disable + sbit ET2 = IE^5; // enable timer2 interrupt + sbit ES = IE^4; // enable UART0 interrupt + sbit ET1 = IE^3; // enable timer1 interrupt + sbit EX1 = IE^2; // enable external interrupt INT1 + sbit ET0 = IE^1; // enable timer0 interrupt + sbit EX0 = IE^0; // enable external interrupt INT0 +sfr IP = 0xB8; // interrupt priority and current priority + sbit PH_FLAG = IP^7; // ReadOnly: high level priority action flag + sbit PL_FLAG = IP^6; // ReadOnly: low level priority action flag +// PH_FLAG & PL_FLAG: current interrupt priority +// 00 - no interrupt now +// 01 - low level priority interrupt action now +// 10 - high level priority interrupt action now +// 11 - unknown error + sbit PT2 = IP^5; // timer2 interrupt priority level + sbit PS = IP^4; // UART0 interrupt priority level + sbit PT1 = IP^3; // timer1 interrupt priority level + sbit PX1 = IP^2; // external interrupt INT1 priority level + sbit PT0 = IP^1; // timer0 interrupt priority level + sbit PX0 = IP^0; // external interrupt INT0 priority level +sfr IE_EX = 0xE8; // extend interrupt enable + sbit IE_WDOG = IE_EX^7; // enable watch-dog timer interrupt + sbit IE_GPIO = IE_EX^6; // enable GPIO input interrupt + sbit IE_PWMX = IE_EX^5; // enable PWM1/2 interrupt + sbit IE_UART1 = IE_EX^4; // enable UART1 interrupt + sbit IE_ADC = IE_EX^3; // enable ADC interrupt + sbit IE_USB = IE_EX^2; // enable USB interrupt + sbit IE_TKEY = IE_EX^1; // enable touch-key timer interrupt + sbit IE_SPI0 = IE_EX^0; // enable SPI0 interrupt +sfr IP_EX = 0xE9; // extend interrupt priority +#define bIP_LEVEL 0x80 // ReadOnly: current interrupt nested level: 0=no interrupt or two levels, 1=one level +#define bIP_GPIO 0x40 // GPIO input interrupt priority level +#define bIP_PWMX 0x20 // PWM1/2 interrupt priority level +#define bIP_UART1 0x10 // UART1 interrupt priority level +#define bIP_ADC 0x08 // ADC interrupt priority level +#define bIP_USB 0x04 // USB interrupt priority level +#define bIP_TKEY 0x02 // touch-key timer interrupt priority level +#define bIP_SPI0 0x01 // SPI0 interrupt priority level +sfr GPIO_IE = 0xC7; // GPIO interrupt enable +#define bIE_IO_EDGE 0x80 // enable GPIO edge interrupt: 0=low/high level, 1=falling/rising edge +#define bIE_RXD1_LO 0x40 // enable interrupt by RXD1 low level / falling edge +#define bIE_P1_5_LO 0x20 // enable interrupt by pin P1.5 low level / falling edge +#define bIE_P1_4_LO 0x10 // enable interrupt by pin P1.4 low level / falling edge +#define bIE_P1_3_LO 0x08 // enable interrupt by pin P1.3 low level / falling edge +#define bIE_RST_HI 0x04 // enable interrupt by pin RST high level / rising edge +#define bIE_P3_1_LO 0x02 // enable interrupt by pin P3.1 low level / falling edge +#define bIE_RXD0_LO 0x01 // enable interrupt by RXD0 low level / falling edge + +/* FlashROM and Data-Flash Registers */ +sfr16 ROM_ADDR = 0x84; // address for flash-ROM, little-endian +sfr ROM_ADDR_L = 0x84; // address low byte for flash-ROM +sfr ROM_ADDR_H = 0x85; // address high byte for flash-ROM +sfr16 ROM_DATA = 0x8E; // data for flash-ROM writing, little-endian +sfr ROM_DATA_L = 0x8E; // data low byte for flash-ROM writing, data byte for Data-Flash reading/writing +sfr ROM_DATA_H = 0x8F; // data high byte for flash-ROM writing +sfr ROM_CTRL = 0x86; // WriteOnly: flash-ROM control +#define ROM_CMD_WRITE 0x9A // WriteOnly: flash-ROM word or Data-Flash byte write operation command +#define ROM_CMD_READ 0x8E // WriteOnly: Data-Flash byte read operation command +//sfr ROM_STATUS = 0x86; // ReadOnly: flash-ROM status +#define ROM_STATUS ROM_CTRL +#define bROM_ADDR_OK 0x40 // ReadOnly: flash-ROM writing operation address valid flag, can be reviewed before or after operation: 0=invalid parameter, 1=address valid +#define bROM_CMD_ERR 0x02 // ReadOnly: flash-ROM operation command error flag: 0=command accepted, 1=unknown command + +/* Port Registers */ +sfr P1 = 0x90; // port 1 input & output + sbit SCK = P1^7; // serial clock for SPI0 + sbit TXD1 = P1^7; // TXD output for UART1 + sbit TIN5 = P1^7; // TIN5 for Touch-Key + sbit MISO = P1^6; // master serial data input or slave serial data output for SPI0 + sbit RXD1 = P1^6; // RXD input for UART1 + sbit TIN4 = P1^6; // TIN4 for Touch-Key + sbit MOSI = P1^5; // master serial data output or slave serial data input for SPI0 + sbit PWM1 = P1^5; // PWM output for PWM1 + sbit TIN3 = P1^5; // TIN3 for Touch-Key + sbit UCC2 = P1^5; // CC2 for USB type-C + sbit AIN2 = P1^5; // AIN2 for ADC + sbit T2_ = P1^4; // alternate pin for T2 + sbit CAP1_ = P1^4; // alternate pin for CAP1 + sbit SCS = P1^4; // slave chip-selection input for SPI0 + sbit TIN2 = P1^4; // TIN2 for Touch-Key + sbit UCC1 = P1^4; // CC1 for USB type-C + sbit AIN1 = P1^4; // AIN1 for ADC + sbit TXD_ = P1^3; // alternate pin for TXD of UART0 + sbit RXD_ = P1^2; // alternate pin for RXD of UART0 + sbit T2EX = P1^1; // external trigger input for timer2 reload & capture + sbit CAP2 = P1^1; // capture2 input for timer2 + sbit TIN1 = P1^1; // TIN1 for Touch-Key + sbit VBUS2 = P1^1; // VBUS2 for USB type-C + sbit AIN0 = P1^1; // AIN0 for ADC + sbit T2 = P1^0; // external count input + sbit CAP1 = P1^0; // capture1 input for timer2 + sbit TIN0 = P1^0; // TIN0 for Touch-Key +sfr P1_MOD_OC = 0x92; // port 1 output mode: 0=push-pull, 1=open-drain +sfr P1_DIR_PU = 0x93; // port 1 direction for push-pull or pullup enable for open-drain +// Pn_MOD_OC & Pn_DIR_PU: pin input & output configuration for Pn (n=1/3) +// 0 0: float input only, without pullup resistance +// 0 1: push-pull output, strong driving high level and low level +// 1 0: open-drain output and input without pullup resistance +// 1 1: quasi-bidirectional (standard 8051 mode), open-drain output and input with pullup resistance, just driving high level strongly for 2 clocks if turning output level from low to high +#define bSCK 0x80 // serial clock for SPI0 +#define bTXD1 0x80 // TXD output for UART1 +#define bMISO 0x40 // master serial data input or slave serial data output for SPI0 +#define bRXD1 0x40 // RXD input for UART1 +#define bMOSI 0x20 // master serial data output or slave serial data input for SPI0 +#define bPWM1 0x20 // PWM output for PWM1 +#define bUCC2 0x20 // CC2 for USB type-C +#define bAIN2 0x20 // AIN2 for ADC +#define bT2_ 0x10 // alternate pin for T2 +#define bCAP1_ 0x10 // alternate pin for CAP1 +#define bSCS 0x10 // slave chip-selection input for SPI0 +#define bUCC1 0x10 // CC1 for USB type-C +#define bAIN1 0x10 // AIN1 for ADC +#define bTXD_ 0x08 // alternate pin for TXD of UART0 +#define bRXD_ 0x04 // alternate pin for RXD of UART0 +#define bT2EX 0x02 // external trigger input for timer2 reload & capture +#define bCAP2 bT2EX // capture2 input for timer2 +#define bVBUS2 0x02 // VBUS2 for USB type-C +#define bAIN0 0x02 // AIN0 for ADC +#define bT2 0x01 // external count input or clock output for timer2 +#define bCAP1 bT2 // capture1 input for timer2 +sfr P2 = 0xA0; // port 2 +sfr P3 = 0xB0; // port 3 input & output + sbit UDM = P3^7; // ReadOnly: pin UDM input + sbit UDP = P3^6; // ReadOnly: pin UDP input + sbit T1 = P3^5; // external count input for timer1 + sbit PWM2 = P3^4; // PWM output for PWM2 + sbit RXD1_ = P3^4; // alternate pin for RXD1 + sbit T0 = P3^4; // external count input for timer0 + sbit INT1 = P3^3; // external interrupt 1 input + sbit TXD1_ = P3^2; // alternate pin for TXD1 + sbit INT0 = P3^2; // external interrupt 0 input + sbit VBUS1 = P3^2; // VBUS1 for USB type-C + sbit AIN3 = P3^2; // AIN3 for ADC + sbit PWM2_ = P3^1; // alternate pin for PWM2 + sbit TXD = P3^1; // TXD output for UART0 + sbit PWM1_ = P3^0; // alternate pin for PWM1 + sbit RXD = P3^0; // RXD input for UART0 +sfr P3_MOD_OC = 0x96; // port 3 output mode: 0=push-pull, 1=open-drain +sfr P3_DIR_PU = 0x97; // port 3 direction for push-pull or pullup enable for open-drain +#define bUDM 0x80 // ReadOnly: pin UDM input +#define bUDP 0x40 // ReadOnly: pin UDP input +#define bT1 0x20 // external count input for timer1 +#define bPWM2 0x10 // PWM output for PWM2 +#define bRXD1_ 0x10 // alternate pin for RXD1 +#define bT0 0x10 // external count input for timer0 +#define bINT1 0x08 // external interrupt 1 input +#define bTXD1_ 0x04 // alternate pin for TXD1 +#define bINT0 0x04 // external interrupt 0 input +#define bVBUS1 0x04 // VBUS1 for USB type-C +#define bAIN3 0x04 // AIN3 for ADC +#define bPWM2_ 0x02 // alternate pin for PWM2 +#define bTXD 0x02 // TXD output for UART0 +#define bPWM1_ 0x01 // alternate pin for PWM1 +#define bRXD 0x01 // RXD input for UART0 +sfr PIN_FUNC = 0xC6; // pin function selection +#define bUSB_IO_EN 0x80 // USB UDP/UDM I/O pin enable: 0=P3.6/P3.7 as GPIO, 1=P3.6/P3.7 as USB +#define bIO_INT_ACT 0x40 // ReadOnly: GPIO interrupt request action status +#define bUART1_PIN_X 0x20 // UART1 alternate pin enable: 0=RXD1/TXD1 on P1.6/P1.7, 1=RXD1/TXD1 on P3.4/P3.2 +#define bUART0_PIN_X 0x10 // UART0 alternate pin enable: 0=RXD0/TXD0 on P3.0/P3.1, 1=RXD0/TXD0 on P1.2/P1.3 +#define bPWM2_PIN_X 0x08 // PWM2 alternate pin enable: 0=PWM2 on P3.4, 1=PWM2 on P3.1 +#define bPWM1_PIN_X 0x04 // PWM1 alternate pin enable: 0=PWM1 on P1.5, 1=PWM1 on P3.0 +#define bT2EX_PIN_X 0x02 // T2EX/CAP2 alternate pin enable: 0=T2EX/CAP2 on P1.1, 1=T2EX/CAP2 on RST +#define bT2_PIN_X 0x01 // T2/CAP1 alternate pin enable: 0=T2/CAP1 on P1.1, 1=T2/CAP1 on P1.4 +sfr XBUS_AUX = 0xA2; // xBUS auxiliary setting +#define bUART0_TX 0x80 // ReadOnly: indicate UART0 transmittal status +#define bUART0_RX 0x40 // ReadOnly: indicate UART0 receiving status +#define bSAFE_MOD_ACT 0x20 // ReadOnly: safe mode action status +#define GF2 0x08 // general purpose flag bit 2 +#define bDPTR_AUTO_INC 0x04 // enable DPTR auto increase if finished MOVX_@DPTR instruction +#define DPS 0x01 // dual DPTR selection: 0=DPTR0 selected, 1=DPTR1 selected + +/* Timer0/1 Registers */ +sfr TCON = 0x88; // timer 0/1 control and external interrupt control + sbit TF1 = TCON^7; // timer1 overflow & interrupt flag, auto cleared when MCU enter interrupt routine + sbit TR1 = TCON^6; // timer1 run enable + sbit TF0 = TCON^5; // timer0 overflow & interrupt flag, auto cleared when MCU enter interrupt routine + sbit TR0 = TCON^4; // timer0 run enable + sbit IE1 = TCON^3; // INT1 interrupt flag, auto cleared when MCU enter interrupt routine + sbit IT1 = TCON^2; // INT1 interrupt type: 0=low level action, 1=falling edge action + sbit IE0 = TCON^1; // INT0 interrupt flag, auto cleared when MCU enter interrupt routine + sbit IT0 = TCON^0; // INT0 interrupt type: 0=low level action, 1=falling edge action +sfr TMOD = 0x89; // timer 0/1 mode +#define bT1_GATE 0x80 // gate control of timer1: 0=timer1 run enable while TR1=1, 1=timer1 run enable while P3.3 (INT1) pin is high and TR1=1 +#define bT1_CT 0x40 // counter or timer mode selection for timer1: 0=timer, use internal clock, 1=counter, use P3.5 (T1) pin falling edge as clock +#define bT1_M1 0x20 // timer1 mode high bit +#define bT1_M0 0x10 // timer1 mode low bit +#define MASK_T1_MOD 0x30 // bit mask of timer1 mode +// bT1_M1 & bT1_M0: timer1 mode +// 00: mode 0, 13-bit timer or counter by cascaded TH1 and lower 5 bits of TL1, the upper 3 bits of TL1 are ignored +// 01: mode 1, 16-bit timer or counter by cascaded TH1 and TL1 +// 10: mode 2, TL1 operates as 8-bit timer or counter, and TH1 provide initial value for TL1 auto-reload +// 11: mode 3, stop timer1 +#define bT0_GATE 0x08 // gate control of timer0: 0=timer0 run enable while TR0=1, 1=timer0 run enable while P3.2 (INT0) pin is high and TR0=1 +#define bT0_CT 0x04 // counter or timer mode selection for timer0: 0=timer, use internal clock, 1=counter, use P3.4 (T0) pin falling edge as clock +#define bT0_M1 0x02 // timer0 mode high bit +#define bT0_M0 0x01 // timer0 mode low bit +#define MASK_T0_MOD 0x03 // bit mask of timer0 mode +// bT0_M1 & bT0_M0: timer0 mode +// 00: mode 0, 13-bit timer or counter by cascaded TH0 and lower 5 bits of TL0, the upper 3 bits of TL0 are ignored +// 01: mode 1, 16-bit timer or counter by cascaded TH0 and TL0 +// 10: mode 2, TL0 operates as 8-bit timer or counter, and TH0 provide initial value for TL0 auto-reload +// 11: mode 3, TL0 is 8-bit timer or counter controlled by standard timer0 bits, TH0 is 8-bit timer using TF1 and controlled by TR1, timer1 run enable if it is not mode 3 +sfr TL0 = 0x8A; // low byte of timer 0 count +sfr TL1 = 0x8B; // low byte of timer 1 count +sfr TH0 = 0x8C; // high byte of timer 0 count +sfr TH1 = 0x8D; // high byte of timer 1 count + +/* UART0 Registers */ +sfr SCON = 0x98; // UART0 control (serial port control) + sbit SM0 = SCON^7; // UART0 mode bit0, selection data bit: 0=8 bits data, 1=9 bits data + sbit SM1 = SCON^6; // UART0 mode bit1, selection baud rate: 0=fixed, 1=variable +// SM0 & SM1: UART0 mode +// 00 - mode 0, shift Register, baud rate fixed at: Fsys/12 +// 01 - mode 1, 8-bit UART, baud rate = variable by timer1 or timer2 overflow rate +// 10 - mode 2, 9-bit UART, baud rate fixed at: Fsys/128@SMOD=0, Fsys/32@SMOD=1 +// 11 - mode 3, 9-bit UART, baud rate = variable by timer1 or timer2 overflow rate + sbit SM2 = SCON^5; // enable multi-device communication in mode 2/3 +#define MASK_UART0_MOD 0xE0 // bit mask of UART0 mode + sbit REN = SCON^4; // enable UART0 receiving + sbit TB8 = SCON^3; // the 9th transmitted data bit in mode 2/3 + sbit RB8 = SCON^2; // 9th data bit received in mode 2/3, or stop bit received for mode 1 + sbit TI = SCON^1; // transmit interrupt flag, set by hardware after completion of a serial transmittal, need software clear + sbit RI = SCON^0; // receive interrupt flag, set by hardware after completion of a serial receiving, need software clear +sfr SBUF = 0x99; // UART0 data buffer: reading for receiving, writing for transmittal + +/* Timer2/Capture2 Registers */ +sfr T2CON = 0xC8; // timer 2 control + sbit TF2 = T2CON^7; // timer2 overflow & interrupt flag, need software clear, the flag will not be set when either RCLK=1 or TCLK=1 + sbit CAP1F = T2CON^7; // timer2 capture 1 interrupt flag, set by T2 edge trigger if bT2_CAP1_EN=1, need software clear + sbit EXF2 = T2CON^6; // timer2 external flag, set by T2EX edge trigger if EXEN2=1, need software clear + sbit RCLK = T2CON^5; // selection UART0 receiving clock: 0=timer1 overflow pulse, 1=timer2 overflow pulse + sbit TCLK = T2CON^4; // selection UART0 transmittal clock: 0=timer1 overflow pulse, 1=timer2 overflow pulse + sbit EXEN2 = T2CON^3; // enable T2EX trigger function: 0=ignore T2EX, 1=trigger reload or capture by T2EX edge + sbit TR2 = T2CON^2; // timer2 run enable + sbit C_T2 = T2CON^1; // timer2 clock source selection: 0=timer base internal clock, 1=external edge counter base T2 falling edge + sbit CP_RL2 = T2CON^0; // timer2 function selection (force 0 if RCLK=1 or TCLK=1): 0=timer and auto reload if count overflow or T2EX edge, 1=capture by T2EX edge +sfr T2MOD = 0xC9; // timer 2 mode and timer 0/1/2 clock mode +#define bTMR_CLK 0x80 // fastest internal clock mode for timer 0/1/2 under faster clock mode: 0=use divided clock, 1=use original Fsys as clock without dividing +#define bT2_CLK 0x40 // timer2 internal clock frequency selection: 0=standard clock, Fsys/12 for timer mode, Fsys/4 for UART0 clock mode, + // 1=faster clock, Fsys/4 @bTMR_CLK=0 or Fsys @bTMR_CLK=1 for timer mode, Fsys/2 @bTMR_CLK=0 or Fsys @bTMR_CLK=1 for UART0 clock mode +#define bT1_CLK 0x20 // timer1 internal clock frequency selection: 0=standard clock, Fsys/12, 1=faster clock, Fsys/4 if bTMR_CLK=0 or Fsys if bTMR_CLK=1 +#define bT0_CLK 0x10 // timer0 internal clock frequency selection: 0=standard clock, Fsys/12, 1=faster clock, Fsys/4 if bTMR_CLK=0 or Fsys if bTMR_CLK=1 +#define bT2_CAP_M1 0x08 // timer2 capture mode high bit +#define bT2_CAP_M0 0x04 // timer2 capture mode low bit +// bT2_CAP_M1 & bT2_CAP_M0: timer2 capture point selection +// x0: from falling edge to falling edge +// 01: from any edge to any edge (level changing) +// 11: from rising edge to rising edge +#define T2OE 0x02 // enable timer2 generated clock output: 0=disable output, 1=enable clock output at T2 pin, frequency = TF2/2 +#define bT2_CAP1_EN 0x01 // enable T2 trigger function for capture 1 of timer2 if RCLK=0 & TCLK=0 & CP_RL2=1 & C_T2=0 & T2OE=0 +sfr16 RCAP2 = 0xCA; // reload & capture value, little-endian +sfr RCAP2L = 0xCA; // low byte of reload & capture value +sfr RCAP2H = 0xCB; // high byte of reload & capture value +sfr16 T2COUNT = 0xCC; // counter, little-endian +sfr TL2 = 0xCC; // low byte of timer 2 count +sfr TH2 = 0xCD; // high byte of timer 2 count +sfr16 T2CAP1 = 0xCE; // ReadOnly: capture 1 value for timer2 +sfr T2CAP1L = 0xCE; // ReadOnly: capture 1 value low byte for timer2 +sfr T2CAP1H = 0xCF; // ReadOnly: capture 1 value high byte for timer2 + +/* PWM1/2 Registers */ +sfr PWM_DATA2 = 0x9B; // PWM data for PWM2 +sfr PWM_DATA1 = 0x9C; // PWM data for PWM1 +sfr PWM_CTRL = 0x9D; // PWM 1/2 control +#define bPWM_IE_END 0x80 // enable interrupt for PWM mode cycle end +#define bPWM2_POLAR 0x40 // PWM2 output polarity: 0=default low and high action, 1=default high and low action +#define bPWM1_POLAR 0x20 // PWM1 output polarity: 0=default low and high action, 1=default high and low action +#define bPWM_IF_END 0x10 // interrupt flag for cycle end, write 1 to clear or write PWM_CYCLE or load new data to clear +#define bPWM2_OUT_EN 0x08 // PWM2 output enable +#define bPWM1_OUT_EN 0x04 // PWM1 output enable +#define bPWM_CLR_ALL 0x02 // force clear FIFO and count of PWM1/2 +sfr PWM_CK_SE = 0x9E; // clock divisor setting + +/* SPI0/Master0/Slave Registers */ +sfr SPI0_STAT = 0xF8; // SPI 0 status + sbit S0_FST_ACT = SPI0_STAT^7; // ReadOnly: indicate first byte received status for SPI0 + sbit S0_IF_OV = SPI0_STAT^6; // interrupt flag for slave mode FIFO overflow, direct bit address clear or write 1 to clear + sbit S0_IF_FIRST = SPI0_STAT^5; // interrupt flag for first byte received, direct bit address clear or write 1 to clear + sbit S0_IF_BYTE = SPI0_STAT^4; // interrupt flag for a byte data exchanged, direct bit address clear or write 1 to clear or accessing FIFO to clear if bS0_AUTO_IF=1 + sbit S0_FREE = SPI0_STAT^3; // ReadOnly: SPI0 free status + sbit S0_T_FIFO = SPI0_STAT^2; // ReadOnly: tx FIFO count for SPI0 + sbit S0_R_FIFO = SPI0_STAT^0; // ReadOnly: rx FIFO count for SPI0 +sfr SPI0_DATA = 0xF9; // FIFO data port: reading for receiving, writing for transmittal +sfr SPI0_CTRL = 0xFA; // SPI 0 control +#define bS0_MISO_OE 0x80 // SPI0 MISO output enable +#define bS0_MOSI_OE 0x40 // SPI0 MOSI output enable +#define bS0_SCK_OE 0x20 // SPI0 SCK output enable +#define bS0_DATA_DIR 0x10 // SPI0 data direction: 0=out(master_write), 1=in(master_read) +#define bS0_MST_CLK 0x08 // SPI0 master clock mode: 0=mode 0 with default low, 1=mode 3 with default high +#define bS0_2_WIRE 0x04 // enable SPI0 two wire mode: 0=3 wire (SCK+MOSI+MISO), 1=2 wire (SCK+MISO) +#define bS0_CLR_ALL 0x02 // force clear FIFO and count of SPI0 +#define bS0_AUTO_IF 0x01 // enable FIFO accessing to auto clear S0_IF_BYTE interrupt flag +sfr SPI0_CK_SE = 0xFB; // clock divisor setting +//sfr SPI0_S_PRE = 0xFB; // preset value for SPI slave +#define SPI0_S_PRE SPI0_CK_SE +sfr SPI0_SETUP = 0xFC; // SPI 0 setup +#define bS0_MODE_SLV 0x80 // SPI0 slave mode: 0=master, 1=slave +#define bS0_IE_FIFO_OV 0x40 // enable interrupt for slave mode FIFO overflow +#define bS0_IE_FIRST 0x20 // enable interrupt for first byte received for SPI0 slave mode +#define bS0_IE_BYTE 0x10 // enable interrupt for a byte received +#define bS0_BIT_ORDER 0x08 // SPI0 bit data order: 0=MSB first, 1=LSB first +#define bS0_SLV_SELT 0x02 // ReadOnly: SPI0 slave mode chip selected status: 0=unselected, 1=selected +#define bS0_SLV_PRELOAD 0x01 // ReadOnly: SPI0 slave mode data pre-loading status just after chip-selection + +/* UART1 Registers */ +sfr SCON1 = 0xC0; // UART1 control (serial port control) + sbit U1SM0 = SCON1^7; // UART1 mode, selection data bit: 0=8 bits data, 1=9 bits data + sbit U1SMOD = SCON1^5; // UART1 2X baud rate selection: 0=slow(Fsys/32/(256-SBAUD1)), 1=fast(Fsys/16/(256-SBAUD1)) + sbit U1REN = SCON1^4; // enable UART1 receiving + sbit U1TB8 = SCON1^3; // the 9th transmitted data bit in 9 bits data mode + sbit U1RB8 = SCON1^2; // 9th data bit received in 9 bits data mode, or stop bit received for 8 bits data mode + sbit U1TI = SCON1^1; // transmit interrupt flag, set by hardware after completion of a serial transmittal, need software clear + sbit U1RI = SCON1^0; // receive interrupt flag, set by hardware after completion of a serial receiving, need software clear +sfr SBUF1 = 0xC1; // UART1 data buffer: reading for receiving, writing for transmittal +sfr SBAUD1 = 0xC2; // UART1 baud rate setting + +/* ADC and comparator Registers */ +sfr ADC_CTRL = 0x80; // ADC control + sbit CMPO = ADC_CTRL^7; // ReadOnly: comparator result input + sbit CMP_IF = ADC_CTRL^6; // flag for comparator result changed, direct bit address clear + sbit ADC_IF = ADC_CTRL^5; // interrupt flag for ADC finished, direct bit address clear + sbit ADC_START = ADC_CTRL^4; // set 1 to start ADC, auto cleared when ADC finished + sbit CMP_CHAN = ADC_CTRL^3; // comparator IN- input channel selection: 0=AIN1, 1=AIN3 + sbit ADC_CHAN1 = ADC_CTRL^1; // ADC/comparator IN+ channel selection high bit + sbit ADC_CHAN0 = ADC_CTRL^0; // ADC/comparator IN+ channel selection low bit +// ADC_CHAN1 & ADC_CHAN0: ADC/comparator IN+ channel selection +// 00: AIN0(P1.1) +// 01: AIN1(P1.4) +// 10: AIN2(P1.5) +// 11: AIN3(P3.2) +sfr ADC_CFG = 0x9A; // ADC config +#define bADC_EN 0x08 // control ADC power: 0=shut down ADC, 1=enable power for ADC +#define bCMP_EN 0x04 // control comparator power: 0=shut down comparator, 1=enable power for comparator +#define bADC_CLK 0x01 // ADC clock frequency selection: 0=slow clock, 384 Fosc cycles for each ADC, 1=fast clock, 96 Fosc cycles for each ADC +sfr ADC_DATA = 0x9F; // ReadOnly: ADC data + +/* Touch-key timer Registers */ +sfr TKEY_CTRL = 0xC3; // touch-key control +#define bTKC_IF 0x80 // ReadOnly: interrupt flag for touch-key timer, cleared by writing touch-key control or auto cleared when start touch-key checking +#define bTKC_2MS 0x10 // touch-key timer cycle selection: 0=1mS, 1=2mS +#define bTKC_CHAN2 0x04 // touch-key channel selection high bit +#define bTKC_CHAN1 0x02 // touch-key channel selection middle bit +#define bTKC_CHAN0 0x01 // touch-key channel selection low bit +// bTKC_CHAN2 & bTKC_CHAN1 & bTKC_CHAN0: touch-key channel selection +// 000: disable touch-key +// 001: TIN0(P1.0) +// 010: TIN1(P1.1) +// 011: TIN2(P1.4) +// 100: TIN3(P1.5) +// 101: TIN4(P1.6) +// 110: TIN5(P1.7) +// 111: enable touch-key but disable all channel +sfr16 TKEY_DAT = 0xC4; // ReadOnly: touch-key data, little-endian +sfr TKEY_DATL = 0xC4; // ReadOnly: low byte of touch-key data +sfr TKEY_DATH = 0xC5; // ReadOnly: high byte of touch-key data +#define bTKD_CHG 0x80 // ReadOnly: indicate control changed, current data maybe invalid + +/* USB/Host/Device Registers */ +sfr USB_C_CTRL = 0x91; // USB type-C control +#define bVBUS2_PD_EN 0x80 // USB VBUS2 10K pulldown resistance: 0=disable, 1=enable pullup +#define bUCC2_PD_EN 0x40 // USB CC2 5.1K pulldown resistance: 0=disable, 1=enable pulldown +#define bUCC2_PU1_EN 0x20 // USB CC2 pullup resistance control high bit +#define bUCC2_PU0_EN 0x10 // USB CC2 pullup resistance control low bit +#define bVBUS1_PD_EN 0x08 // USB VBUS1 10K pulldown resistance: 0=disable, 1=enable pullup +#define bUCC1_PD_EN 0x04 // USB CC1 5.1K pulldown resistance: 0=disable, 1=enable pulldown +#define bUCC1_PU1_EN 0x02 // USB CC1 pullup resistance control high bit +#define bUCC1_PU0_EN 0x01 // USB CC1 pullup resistance control low bit +// bUCC?_PU1_EN & bUCC?_PU0_EN: USB CC pullup resistance selection +// 00: disable pullup resistance +// 01: enable 56K pullup resistance for default USB power +// 10: enable 22K pullup resistance for 1.5A USB power +// 11: enable 10K pullup resistance for 3A USB power +sfr UDEV_CTRL = 0xD1; // USB device physical port control +#define bUD_PD_DIS 0x80 // disable USB UDP/UDM pulldown resistance: 0=enable pulldown, 1=disable +#define bUD_DP_PIN 0x20 // ReadOnly: indicate current UDP pin level +#define bUD_DM_PIN 0x10 // ReadOnly: indicate current UDM pin level +#define bUD_LOW_SPEED 0x04 // enable USB physical port low speed: 0=full speed, 1=low speed +#define bUD_GP_BIT 0x02 // general purpose bit +#define bUD_PORT_EN 0x01 // enable USB physical port I/O: 0=disable, 1=enable +//sfr UHOST_CTRL = 0xD1; // USB host physical port control +#define UHOST_CTRL UDEV_CTRL +#define bUH_PD_DIS 0x80 // disable USB UDP/UDM pulldown resistance: 0=enable pulldown, 1=disable +#define bUH_DP_PIN 0x20 // ReadOnly: indicate current UDP pin level +#define bUH_DM_PIN 0x10 // ReadOnly: indicate current UDM pin level +#define bUH_LOW_SPEED 0x04 // enable USB port low speed: 0=full speed, 1=low speed +#define bUH_BUS_RESET 0x02 // control USB bus reset: 0=normal, 1=force bus reset +#define bUH_PORT_EN 0x01 // enable USB port: 0=disable, 1=enable port, automatic disabled if USB device detached +sfr UEP1_CTRL = 0xD2; // endpoint 1 control +#define bUEP_R_TOG 0x80 // expected data toggle flag of USB endpoint X receiving (OUT): 0=DATA0, 1=DATA1 +#define bUEP_T_TOG 0x40 // prepared data toggle flag of USB endpoint X transmittal (IN): 0=DATA0, 1=DATA1 +#define bUEP_AUTO_TOG 0x10 // enable automatic toggle after successful transfer completion on endpoint 1/2/3: 0=manual toggle, 1=automatic toggle +#define bUEP_R_RES1 0x08 // handshake response type high bit for USB endpoint X receiving (OUT) +#define bUEP_R_RES0 0x04 // handshake response type low bit for USB endpoint X receiving (OUT) +#define MASK_UEP_R_RES 0x0C // bit mask of handshake response type for USB endpoint X receiving (OUT) +#define UEP_R_RES_ACK 0x00 +#define UEP_R_RES_TOUT 0x04 +#define UEP_R_RES_NAK 0x08 +#define UEP_R_RES_STALL 0x0C +// bUEP_R_RES1 & bUEP_R_RES0: handshake response type for USB endpoint X receiving (OUT) +// 00: ACK (ready) +// 01: no response, time out to host, for non-zero endpoint isochronous transactions +// 10: NAK (busy) +// 11: STALL (error) +#define bUEP_T_RES1 0x02 // handshake response type high bit for USB endpoint X transmittal (IN) +#define bUEP_T_RES0 0x01 // handshake response type low bit for USB endpoint X transmittal (IN) +#define MASK_UEP_T_RES 0x03 // bit mask of handshake response type for USB endpoint X transmittal (IN) +#define UEP_T_RES_ACK 0x00 +#define UEP_T_RES_TOUT 0x01 +#define UEP_T_RES_NAK 0x02 +#define UEP_T_RES_STALL 0x03 +// bUEP_T_RES1 & bUEP_T_RES0: handshake response type for USB endpoint X transmittal (IN) +// 00: DATA0 or DATA1 then expecting ACK (ready) +// 01: DATA0 or DATA1 then expecting no response, time out from host, for non-zero endpoint isochronous transactions +// 10: NAK (busy) +// 11: STALL (error) +sfr UEP1_T_LEN = 0xD3; // endpoint 1 transmittal length +sfr UEP2_CTRL = 0xD4; // endpoint 2 control +sfr UEP2_T_LEN = 0xD5; // endpoint 2 transmittal length +sfr UEP3_CTRL = 0xD6; // endpoint 3 control +sfr UEP3_T_LEN = 0xD7; // endpoint 3 transmittal length +sfr USB_INT_FG = 0xD8; // USB interrupt flag + sbit U_IS_NAK = USB_INT_FG^7; // ReadOnly: indicate current USB transfer is NAK received + sbit U_TOG_OK = USB_INT_FG^6; // ReadOnly: indicate current USB transfer toggle is OK + sbit U_SIE_FREE = USB_INT_FG^5; // ReadOnly: indicate USB SIE free status + sbit UIF_FIFO_OV = USB_INT_FG^4; // FIFO overflow interrupt flag for USB, direct bit address clear or write 1 to clear + sbit UIF_HST_SOF = USB_INT_FG^3; // host SOF timer interrupt flag for USB host, direct bit address clear or write 1 to clear + sbit UIF_SUSPEND = USB_INT_FG^2; // USB suspend or resume event interrupt flag, direct bit address clear or write 1 to clear + sbit UIF_TRANSFER = USB_INT_FG^1; // USB transfer completion interrupt flag, direct bit address clear or write 1 to clear + sbit UIF_DETECT = USB_INT_FG^0; // device detected event interrupt flag for USB host mode, direct bit address clear or write 1 to clear + sbit UIF_BUS_RST = USB_INT_FG^0; // bus reset event interrupt flag for USB device mode, direct bit address clear or write 1 to clear +sfr USB_INT_ST = 0xD9; // ReadOnly: USB interrupt status +#define bUIS_IS_NAK 0x80 // ReadOnly: indicate current USB transfer is NAK received for USB device mode +#define bUIS_TOG_OK 0x40 // ReadOnly: indicate current USB transfer toggle is OK +#define bUIS_TOKEN1 0x20 // ReadOnly: current token PID code bit 1 received for USB device mode +#define bUIS_TOKEN0 0x10 // ReadOnly: current token PID code bit 0 received for USB device mode +#define MASK_UIS_TOKEN 0x30 // ReadOnly: bit mask of current token PID code received for USB device mode +#define UIS_TOKEN_OUT 0x00 +#define UIS_TOKEN_SOF 0x10 +#define UIS_TOKEN_IN 0x20 +#define UIS_TOKEN_SETUP 0x30 +// bUIS_TOKEN1 & bUIS_TOKEN0: current token PID code received for USB device mode +// 00: OUT token PID received +// 01: SOF token PID received +// 10: IN token PID received +// 11: SETUP token PID received +#define MASK_UIS_ENDP 0x0F // ReadOnly: bit mask of current transfer endpoint number for USB device mode +#define MASK_UIS_H_RES 0x0F // ReadOnly: bit mask of current transfer handshake response for USB host mode: 0000=no response, time out from device, others=handshake response PID received +sfr USB_MIS_ST = 0xDA; // ReadOnly: USB miscellaneous status +#define bUMS_SOF_PRES 0x80 // ReadOnly: indicate host SOF timer presage status +#define bUMS_SOF_ACT 0x40 // ReadOnly: indicate host SOF timer action status for USB host +#define bUMS_SIE_FREE 0x20 // ReadOnly: indicate USB SIE free status +#define bUMS_R_FIFO_RDY 0x10 // ReadOnly: indicate USB receiving FIFO ready status (not empty) +#define bUMS_BUS_RESET 0x08 // ReadOnly: indicate USB bus reset status +#define bUMS_SUSPEND 0x04 // ReadOnly: indicate USB suspend status +#define bUMS_DM_LEVEL 0x02 // ReadOnly: indicate UDM level saved at device attached to USB host +#define bUMS_DEV_ATTACH 0x01 // ReadOnly: indicate device attached status on USB host +sfr USB_RX_LEN = 0xDB; // ReadOnly: USB receiving length +sfr UEP0_CTRL = 0xDC; // endpoint 0 control +sfr UEP0_T_LEN = 0xDD; // endpoint 0 transmittal length +sfr UEP4_CTRL = 0xDE; // endpoint 4 control +sfr UEP4_T_LEN = 0xDF; // endpoint 4 transmittal length +sfr USB_INT_EN = 0xE1; // USB interrupt enable +#define bUIE_DEV_SOF 0x80 // enable interrupt for SOF received for USB device mode +#define bUIE_DEV_NAK 0x40 // enable interrupt for NAK responded for USB device mode +#define bUIE_FIFO_OV 0x10 // enable interrupt for FIFO overflow +#define bUIE_HST_SOF 0x08 // enable interrupt for host SOF timer action for USB host mode +#define bUIE_SUSPEND 0x04 // enable interrupt for USB suspend or resume event +#define bUIE_TRANSFER 0x02 // enable interrupt for USB transfer completion +#define bUIE_DETECT 0x01 // enable interrupt for USB device detected event for USB host mode +#define bUIE_BUS_RST 0x01 // enable interrupt for USB bus reset event for USB device mode +sfr USB_CTRL = 0xE2; // USB base control +#define bUC_HOST_MODE 0x80 // enable USB host mode: 0=device mode, 1=host mode +#define bUC_LOW_SPEED 0x40 // enable USB low speed: 0=full speed, 1=low speed +#define bUC_DEV_PU_EN 0x20 // USB device enable and internal pullup resistance enable +#define bUC_SYS_CTRL1 0x20 // USB system control high bit +#define bUC_SYS_CTRL0 0x10 // USB system control low bit +#define MASK_UC_SYS_CTRL 0x30 // bit mask of USB system control +// bUC_HOST_MODE & bUC_SYS_CTRL1 & bUC_SYS_CTRL0: USB system control +// 0 00: disable USB device and disable internal pullup resistance +// 0 01: enable USB device and disable internal pullup resistance, need external pullup resistance +// 0 1x: enable USB device and enable internal pullup resistance +// 1 00: enable USB host and normal status +// 1 01: enable USB host and force UDP/UDM output SE0 state +// 1 10: enable USB host and force UDP/UDM output J state +// 1 11: enable USB host and force UDP/UDM output resume or K state +#define bUC_INT_BUSY 0x08 // enable automatic responding busy for device mode or automatic pause for host mode during interrupt flag UIF_TRANSFER valid +#define bUC_RESET_SIE 0x04 // force reset USB SIE, need software clear +#define bUC_CLR_ALL 0x02 // force clear FIFO and count of USB +#define bUC_DMA_EN 0x01 // DMA enable and DMA interrupt enable for USB +sfr USB_DEV_AD = 0xE3; // USB device address, lower 7 bits for USB device address +#define bUDA_GP_BIT 0x80 // general purpose bit +#define MASK_USB_ADDR 0x7F // bit mask for USB device address +sfr16 UEP2_DMA = 0xE4; // endpoint 2 buffer start address, little-endian +sfr UEP2_DMA_L = 0xE4; // endpoint 2 buffer start address low byte +sfr UEP2_DMA_H = 0xE5; // endpoint 2 buffer start address high byte +sfr16 UEP3_DMA = 0xE6; // endpoint 3 buffer start address, little-endian +sfr UEP3_DMA_L = 0xE6; // endpoint 3 buffer start address low byte +sfr UEP3_DMA_H = 0xE7; // endpoint 3 buffer start address high byte +sfr UEP4_1_MOD = 0xEA; // endpoint 4/1 mode +#define bUEP1_RX_EN 0x80 // enable USB endpoint 1 receiving (OUT) +#define bUEP1_TX_EN 0x40 // enable USB endpoint 1 transmittal (IN) +#define bUEP1_BUF_MOD 0x10 // buffer mode of USB endpoint 1 +// bUEPn_RX_EN & bUEPn_TX_EN & bUEPn_BUF_MOD: USB endpoint 1/2/3 buffer mode, buffer start address is UEPn_DMA +// 0 0 x: disable endpoint and disable buffer +// 1 0 0: 64 bytes buffer for receiving (OUT endpoint) +// 1 0 1: dual 64 bytes buffer by toggle bit bUEP_R_TOG selection for receiving (OUT endpoint), total=128bytes +// 0 1 0: 64 bytes buffer for transmittal (IN endpoint) +// 0 1 1: dual 64 bytes buffer by toggle bit bUEP_T_TOG selection for transmittal (IN endpoint), total=128bytes +// 1 1 0: 64 bytes buffer for receiving (OUT endpoint) + 64 bytes buffer for transmittal (IN endpoint), total=128bytes +// 1 1 1: dual 64 bytes buffer by bUEP_R_TOG selection for receiving (OUT endpoint) + dual 64 bytes buffer by bUEP_T_TOG selection for transmittal (IN endpoint), total=256bytes +#define bUEP4_RX_EN 0x08 // enable USB endpoint 4 receiving (OUT) +#define bUEP4_TX_EN 0x04 // enable USB endpoint 4 transmittal (IN) +// bUEP4_RX_EN & bUEP4_TX_EN: USB endpoint 4 buffer mode, buffer start address is UEP0_DMA +// 0 0: single 64 bytes buffer for endpoint 0 receiving & transmittal (OUT & IN endpoint) +// 1 0: single 64 bytes buffer for endpoint 0 receiving & transmittal (OUT & IN endpoint) + 64 bytes buffer for endpoint 4 receiving (OUT endpoint), total=128bytes +// 0 1: single 64 bytes buffer for endpoint 0 receiving & transmittal (OUT & IN endpoint) + 64 bytes buffer for endpoint 4 transmittal (IN endpoint), total=128bytes +// 1 1: single 64 bytes buffer for endpoint 0 receiving & transmittal (OUT & IN endpoint) +// + 64 bytes buffer for endpoint 4 receiving (OUT endpoint) + 64 bytes buffer for endpoint 4 transmittal (IN endpoint), total=192bytes +sfr UEP2_3_MOD = 0xEB; // endpoint 2/3 mode +#define bUEP3_RX_EN 0x80 // enable USB endpoint 3 receiving (OUT) +#define bUEP3_TX_EN 0x40 // enable USB endpoint 3 transmittal (IN) +#define bUEP3_BUF_MOD 0x10 // buffer mode of USB endpoint 3 +#define bUEP2_RX_EN 0x08 // enable USB endpoint 2 receiving (OUT) +#define bUEP2_TX_EN 0x04 // enable USB endpoint 2 transmittal (IN) +#define bUEP2_BUF_MOD 0x01 // buffer mode of USB endpoint 2 +sfr16 UEP0_DMA = 0xEC; // endpoint 0 buffer start address, little-endian +sfr UEP0_DMA_L = 0xEC; // endpoint 0 buffer start address low byte +sfr UEP0_DMA_H = 0xED; // endpoint 0 buffer start address high byte +sfr16 UEP1_DMA = 0xEE; // endpoint 1 buffer start address, little-endian +sfr UEP1_DMA_L = 0xEE; // endpoint 1 buffer start address low byte +sfr UEP1_DMA_H = 0xEF; // endpoint 1 buffer start address high byte +//sfr UH_SETUP = 0xD2; // host aux setup +#define UH_SETUP UEP1_CTRL +#define bUH_PRE_PID_EN 0x80 // USB host PRE PID enable for low speed device via hub +#define bUH_SOF_EN 0x40 // USB host automatic SOF enable +//sfr UH_RX_CTRL = 0xD4; // host receiver endpoint control +#define UH_RX_CTRL UEP2_CTRL +#define bUH_R_TOG 0x80 // expected data toggle flag of host receiving (IN): 0=DATA0, 1=DATA1 +#define bUH_R_AUTO_TOG 0x10 // enable automatic toggle after successful transfer completion: 0=manual toggle, 1=automatic toggle +#define bUH_R_RES 0x04 // prepared handshake response type for host receiving (IN): 0=ACK (ready), 1=no response, time out to device, for isochronous transactions +//sfr UH_EP_PID = 0xD5; // host endpoint and token PID, lower 4 bits for endpoint number, upper 4 bits for token PID +#define UH_EP_PID UEP2_T_LEN +#define MASK_UH_TOKEN 0xF0 // bit mask of token PID for USB host transfer +#define MASK_UH_ENDP 0x0F // bit mask of endpoint number for USB host transfer +//sfr UH_TX_CTRL = 0xD6; // host transmittal endpoint control +#define UH_TX_CTRL UEP3_CTRL +#define bUH_T_TOG 0x40 // prepared data toggle flag of host transmittal (SETUP/OUT): 0=DATA0, 1=DATA1 +#define bUH_T_AUTO_TOG 0x10 // enable automatic toggle after successful transfer completion: 0=manual toggle, 1=automatic toggle +#define bUH_T_RES 0x01 // expected handshake response type for host transmittal (SETUP/OUT): 0=ACK (ready), 1=no response, time out from device, for isochronous transactions +//sfr UH_TX_LEN = 0xD7; // host transmittal endpoint transmittal length +#define UH_TX_LEN UEP3_T_LEN +//sfr UH_EP_MOD = 0xEB; // host endpoint mode +#define UH_EP_MOD UEP2_3_MOD +#define bUH_EP_TX_EN 0x40 // enable USB host OUT endpoint transmittal +#define bUH_EP_TBUF_MOD 0x10 // buffer mode of USB host OUT endpoint +// bUH_EP_TX_EN & bUH_EP_TBUF_MOD: USB host OUT endpoint buffer mode, buffer start address is UH_TX_DMA +// 0 x: disable endpoint and disable buffer +// 1 0: 64 bytes buffer for transmittal (OUT endpoint) +// 1 1: dual 64 bytes buffer by toggle bit bUH_T_TOG selection for transmittal (OUT endpoint), total=128bytes +#define bUH_EP_RX_EN 0x08 // enable USB host IN endpoint receiving +#define bUH_EP_RBUF_MOD 0x01 // buffer mode of USB host IN endpoint +// bUH_EP_RX_EN & bUH_EP_RBUF_MOD: USB host IN endpoint buffer mode, buffer start address is UH_RX_DMA +// 0 x: disable endpoint and disable buffer +// 1 0: 64 bytes buffer for receiving (IN endpoint) +// 1 1: dual 64 bytes buffer by toggle bit bUH_R_TOG selection for receiving (IN endpoint), total=128bytes +//sfr16 UH_RX_DMA = 0xE4; // host rx endpoint buffer start address, little-endian +#define UH_RX_DMA UEP2_DMA +//sfr UH_RX_DMA_L = 0xE4; // host rx endpoint buffer start address low byte +#define UH_RX_DMA_L UEP2_DMA_L +//sfr UH_RX_DMA_H = 0xE5; // host rx endpoint buffer start address high byte +#define UH_RX_DMA_H UEP2_DMA_H +//sfr16 UH_TX_DMA = 0xE6; // host tx endpoint buffer start address, little-endian +#define UH_TX_DMA UEP3_DMA +//sfr UH_TX_DMA_L = 0xE6; // host tx endpoint buffer start address low byte +#define UH_TX_DMA_L UEP3_DMA_L +//sfr UH_TX_DMA_H = 0xE7; // host tx endpoint buffer start address high byte +#define UH_TX_DMA_H UEP3_DMA_H + +/*----- XDATA: xRAM ------------------------------------------*/ + +#define XDATA_RAM_SIZE 0x0400 // size of expanded xRAM, xdata SRAM embedded chip + +/*----- Reference Information --------------------------------------------*/ +#define ID_CH554 0x54 // chip ID + +/* Interrupt routine address and interrupt number */ +#define INT_ADDR_INT0 0x0003 // interrupt vector address for INT0 +#define INT_ADDR_TMR0 0x000B // interrupt vector address for timer0 +#define INT_ADDR_INT1 0x0013 // interrupt vector address for INT1 +#define INT_ADDR_TMR1 0x001B // interrupt vector address for timer1 +#define INT_ADDR_UART0 0x0023 // interrupt vector address for UART0 +#define INT_ADDR_TMR2 0x002B // interrupt vector address for timer2 +#define INT_ADDR_SPI0 0x0033 // interrupt vector address for SPI0 +#define INT_ADDR_TKEY 0x003B // interrupt vector address for touch-key timer +#define INT_ADDR_USB 0x0043 // interrupt vector address for USB +#define INT_ADDR_ADC 0x004B // interrupt vector address for ADC +#define INT_ADDR_UART1 0x0053 // interrupt vector address for UART1 +#define INT_ADDR_PWMX 0x005B // interrupt vector address for PWM1/2 +#define INT_ADDR_GPIO 0x0063 // interrupt vector address for GPIO +#define INT_ADDR_WDOG 0x006B // interrupt vector address for watch-dog timer +#define INT_NO_INT0 0 // interrupt number for INT0 +#define INT_NO_TMR0 1 // interrupt number for timer0 +#define INT_NO_INT1 2 // interrupt number for INT1 +#define INT_NO_TMR1 3 // interrupt number for timer1 +#define INT_NO_UART0 4 // interrupt number for UART0 +#define INT_NO_TMR2 5 // interrupt number for timer2 +#define INT_NO_SPI0 6 // interrupt number for SPI0 +#define INT_NO_TKEY 7 // interrupt number for touch-key timer +#define INT_NO_USB 8 // interrupt number for USB +#define INT_NO_ADC 9 // interrupt number for ADC +#define INT_NO_UART1 10 // interrupt number for UART1 +#define INT_NO_PWMX 11 // interrupt number for PWM1/2 +#define INT_NO_GPIO 12 // interrupt number for GPIO +#define INT_NO_WDOG 13 // interrupt number for watch-dog timer + +/* Special Program Space */ +#define DATA_FLASH_ADDR 0xC000 // start address of Data-Flash +#define BOOT_LOAD_ADDR 0x3800 // start address of boot loader program +#define ROM_CFG_ADDR 0x3FF8 // chip configuration information address +#define ROM_CHIP_ID_HX 0x3FFA // chip ID number highest byte (only low byte valid) +#define ROM_CHIP_ID_LO 0x3FFC // chip ID number low word +#define ROM_CHIP_ID_HI 0x3FFE // chip ID number high word + +/* +New Instruction: MOVX @DPTR1,A +Instruction Code: 0xA5 +Instruction Cycle: 1 +Instruction Operation: + step-1. write ACC @DPTR1 into xdata SRAM embedded chip + step-2. increase DPTR1 +ASM example: + INC XBUS_AUX + MOV DPTR,#TARGET_ADDR ;DPTR1 + DEC XBUS_AUX + MOV DPTR,#SOURCE_ADDR ;DPTR0 + MOV R7,#xxH + LOOP: MOVX A,@DPTR ;DPTR0 + INC DPTR ;DPTR0, if need + DB 0A5H ;MOVX @DPTR1,A & INC DPTR1 + DJNZ R7,LOOP +*/ + +#ifdef __cplusplus +} +#endif + +#endif // __CH554_H__ + +#ifndef __USB_DEF__ +#define __USB_DEF__ + +#ifdef __cplusplus +extern "C" { +#endif + +/*----- USB constant and structure define --------------------------------*/ + +/* USB PID */ +#ifndef USB_PID_SETUP +#define USB_PID_NULL 0x00 /* reserved PID */ +#define USB_PID_SOF 0x05 +#define USB_PID_SETUP 0x0D +#define USB_PID_IN 0x09 +#define USB_PID_OUT 0x01 +#define USB_PID_ACK 0x02 +#define USB_PID_NAK 0x0A +#define USB_PID_STALL 0x0E +#define USB_PID_DATA0 0x03 +#define USB_PID_DATA1 0x0B +#define USB_PID_PRE 0x0C +#endif + +/* USB standard device request code */ +#ifndef USB_GET_DESCRIPTOR +#define USB_GET_STATUS 0x00 +#define USB_CLEAR_FEATURE 0x01 +#define USB_SET_FEATURE 0x03 +#define USB_SET_ADDRESS 0x05 +#define USB_GET_DESCRIPTOR 0x06 +#define USB_SET_DESCRIPTOR 0x07 +#define USB_GET_CONFIGURATION 0x08 +#define USB_SET_CONFIGURATION 0x09 +#define USB_GET_INTERFACE 0x0A +#define USB_SET_INTERFACE 0x0B +#define USB_SYNCH_FRAME 0x0C +#endif + +/* USB hub class request code */ +#ifndef HUB_GET_DESCRIPTOR +#define HUB_GET_STATUS 0x00 +#define HUB_CLEAR_FEATURE 0x01 +#define HUB_GET_STATE 0x02 +#define HUB_SET_FEATURE 0x03 +#define HUB_GET_DESCRIPTOR 0x06 +#define HUB_SET_DESCRIPTOR 0x07 +#endif + +/* USB HID class request code */ +#ifndef HID_GET_REPORT +#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 +#endif + +/* Bit define for USB request type */ +#ifndef USB_REQ_TYP_MASK +#define USB_REQ_TYP_IN 0x80 /* control IN, device to host */ +#define USB_REQ_TYP_OUT 0x00 /* control OUT, host to device */ +#define USB_REQ_TYP_READ 0x80 /* control read, device to host */ +#define USB_REQ_TYP_WRITE 0x00 /* control write, host to device */ +#define USB_REQ_TYP_MASK 0x60 /* bit mask of request type */ +#define USB_REQ_TYP_STANDARD 0x00 +#define USB_REQ_TYP_CLASS 0x20 +#define USB_REQ_TYP_VENDOR 0x40 +#define USB_REQ_TYP_RESERVED 0x60 +#define USB_REQ_RECIP_MASK 0x1F /* bit mask of request recipient */ +#define USB_REQ_RECIP_DEVICE 0x00 +#define USB_REQ_RECIP_INTERF 0x01 +#define USB_REQ_RECIP_ENDP 0x02 +#define USB_REQ_RECIP_OTHER 0x03 +#endif + +/* USB request type for hub class request */ +#ifndef HUB_GET_HUB_DESCRIPTOR +#define HUB_CLEAR_HUB_FEATURE 0x20 +#define HUB_CLEAR_PORT_FEATURE 0x23 +#define HUB_GET_BUS_STATE 0xA3 +#define HUB_GET_HUB_DESCRIPTOR 0xA0 +#define HUB_GET_HUB_STATUS 0xA0 +#define HUB_GET_PORT_STATUS 0xA3 +#define HUB_SET_HUB_DESCRIPTOR 0x20 +#define HUB_SET_HUB_FEATURE 0x20 +#define HUB_SET_PORT_FEATURE 0x23 +#endif + +/* Hub class feature selectors */ +#ifndef HUB_PORT_RESET +#define HUB_C_HUB_LOCAL_POWER 0 +#define HUB_C_HUB_OVER_CURRENT 1 +#define HUB_PORT_CONNECTION 0 +#define HUB_PORT_ENABLE 1 +#define HUB_PORT_SUSPEND 2 +#define HUB_PORT_OVER_CURRENT 3 +#define HUB_PORT_RESET 4 +#define HUB_PORT_POWER 8 +#define HUB_PORT_LOW_SPEED 9 +#define HUB_C_PORT_CONNECTION 16 +#define HUB_C_PORT_ENABLE 17 +#define HUB_C_PORT_SUSPEND 18 +#define HUB_C_PORT_OVER_CURRENT 19 +#define HUB_C_PORT_RESET 20 +#endif + +/* USB descriptor type */ +#ifndef USB_DESCR_TYP_DEVICE +#define USB_DESCR_TYP_DEVICE 0x01 +#define USB_DESCR_TYP_CONFIG 0x02 +#define USB_DESCR_TYP_STRING 0x03 +#define USB_DESCR_TYP_INTERF 0x04 +#define USB_DESCR_TYP_ENDP 0x05 +#define USB_DESCR_TYP_QUALIF 0x06 +#define USB_DESCR_TYP_SPEED 0x07 +#define USB_DESCR_TYP_OTG 0x09 +#define USB_DESCR_TYP_HID 0x21 +#define USB_DESCR_TYP_REPORT 0x22 +#define USB_DESCR_TYP_PHYSIC 0x23 +#define USB_DESCR_TYP_CS_INTF 0x24 +#define USB_DESCR_TYP_CS_ENDP 0x25 +#define USB_DESCR_TYP_HUB 0x29 +#endif + +/* USB device class */ +#ifndef USB_DEV_CLASS_HUB +#define USB_DEV_CLASS_RESERVED 0x00 +#define USB_DEV_CLASS_AUDIO 0x01 +#define USB_DEV_CLASS_COMMUNIC 0x02 +#define USB_DEV_CLASS_HID 0x03 +#define USB_DEV_CLASS_MONITOR 0x04 +#define USB_DEV_CLASS_PHYSIC_IF 0x05 +#define USB_DEV_CLASS_POWER 0x06 +#define USB_DEV_CLASS_PRINTER 0x07 +#define USB_DEV_CLASS_STORAGE 0x08 +#define USB_DEV_CLASS_HUB 0x09 +#define USB_DEV_CLASS_VEN_SPEC 0xFF +#endif + +/* USB endpoint type and attributes */ +#ifndef USB_ENDP_TYPE_MASK +#define USB_ENDP_DIR_MASK 0x80 +#define USB_ENDP_ADDR_MASK 0x0F +#define USB_ENDP_TYPE_MASK 0x03 +#define USB_ENDP_TYPE_CTRL 0x00 +#define USB_ENDP_TYPE_ISOCH 0x01 +#define USB_ENDP_TYPE_BULK 0x02 +#define USB_ENDP_TYPE_INTER 0x03 +#endif + +#ifndef USB_DEVICE_ADDR +#define USB_DEVICE_ADDR 0x02 /* ĬϵUSB豸ַ */ +#endif +#ifndef DEFAULT_ENDP0_SIZE +#define DEFAULT_ENDP0_SIZE 8 /* default maximum packet size for endpoint 0 */ +#endif +#ifndef MAX_PACKET_SIZE +#define MAX_PACKET_SIZE 64 /* maximum packet size */ +#endif +#ifndef USB_BO_CBW_SIZE +#define USB_BO_CBW_SIZE 0x1F /* CBWܳ */ +#define USB_BO_CSW_SIZE 0x0D /* ״̬CSWܳ */ +#endif +#ifndef USB_BO_CBW_SIG0 +#define USB_BO_CBW_SIG0 0x55 /* CBWʶ־'USBC' */ +#define USB_BO_CBW_SIG1 0x53 +#define USB_BO_CBW_SIG2 0x42 +#define USB_BO_CBW_SIG3 0x43 +#define USB_BO_CSW_SIG0 0x55 /* ״̬CSWʶ־'USBS' */ +#define USB_BO_CSW_SIG1 0x53 +#define USB_BO_CSW_SIG2 0x42 +#define USB_BO_CSW_SIG3 0x53 +#endif + +typedef struct _USB_SETUP_REQ { + UINT8 bRequestType; + UINT8 bRequest; + UINT8 wValueL; + UINT8 wValueH; + UINT8 wIndexL; + UINT8 wIndexH; + UINT8 wLengthL; + UINT8 wLengthH; +} USB_SETUP_REQ, *PUSB_SETUP_REQ; + +typedef USB_SETUP_REQ xdata *PXUSB_SETUP_REQ; + +typedef struct _USB_DEVICE_DESCR { + UINT8 bLength; + UINT8 bDescriptorType; + UINT8 bcdUSBL; + UINT8 bcdUSBH; + UINT8 bDeviceClass; + UINT8 bDeviceSubClass; + UINT8 bDeviceProtocol; + UINT8 bMaxPacketSize0; + UINT8 idVendorL; + UINT8 idVendorH; + UINT8 idProductL; + UINT8 idProductH; + UINT8 bcdDeviceL; + UINT8 bcdDeviceH; + UINT8 iManufacturer; + UINT8 iProduct; + UINT8 iSerialNumber; + UINT8 bNumConfigurations; +} USB_DEV_DESCR, *PUSB_DEV_DESCR; + +typedef USB_DEV_DESCR xdata *PXUSB_DEV_DESCR; + +typedef struct _USB_CONFIG_DESCR { + UINT8 bLength; + UINT8 bDescriptorType; + UINT8 wTotalLengthL; + UINT8 wTotalLengthH; + UINT8 bNumInterfaces; + UINT8 bConfigurationValue; + UINT8 iConfiguration; + UINT8 bmAttributes; + UINT8 MaxPower; +} USB_CFG_DESCR, *PUSB_CFG_DESCR; + +typedef USB_CFG_DESCR xdata *PXUSB_CFG_DESCR; + +typedef struct _USB_INTERF_DESCR { + UINT8 bLength; + UINT8 bDescriptorType; + UINT8 bInterfaceNumber; + UINT8 bAlternateSetting; + UINT8 bNumEndpoints; + UINT8 bInterfaceClass; + UINT8 bInterfaceSubClass; + UINT8 bInterfaceProtocol; + UINT8 iInterface; +} USB_ITF_DESCR, *PUSB_ITF_DESCR; + +typedef USB_ITF_DESCR xdata *PXUSB_ITF_DESCR; + +typedef struct _USB_ENDPOINT_DESCR { + UINT8 bLength; + UINT8 bDescriptorType; + UINT8 bEndpointAddress; + UINT8 bmAttributes; + UINT8 wMaxPacketSizeL; + UINT8 wMaxPacketSizeH; + UINT8 bInterval; +} USB_ENDP_DESCR, *PUSB_ENDP_DESCR; + +typedef USB_ENDP_DESCR xdata *PXUSB_ENDP_DESCR; + +typedef struct _USB_CONFIG_DESCR_LONG { + USB_CFG_DESCR cfg_descr; + USB_ITF_DESCR itf_descr; + USB_ENDP_DESCR endp_descr[1]; +} USB_CFG_DESCR_LONG, *PUSB_CFG_DESCR_LONG; + +typedef USB_CFG_DESCR_LONG xdata *PXUSB_CFG_DESCR_LONG; + +typedef struct _USB_HUB_DESCR { + UINT8 bDescLength; + UINT8 bDescriptorType; + UINT8 bNbrPorts; + UINT8 wHubCharacteristicsL; + UINT8 wHubCharacteristicsH; + UINT8 bPwrOn2PwrGood; + UINT8 bHubContrCurrent; + UINT8 DeviceRemovable; + UINT8 PortPwrCtrlMask; +} USB_HUB_DESCR, *PUSB_HUB_DESCR; + +typedef USB_HUB_DESCR xdata *PXUSB_HUB_DESCR; + +typedef struct _USB_HID_DESCR { + UINT8 bLength; + UINT8 bDescriptorType; + UINT8 bcdHIDL; + UINT8 bcdHIDH; + UINT8 bCountryCode; + UINT8 bNumDescriptors; + UINT8 bDescriptorTypeX; + UINT8 wDescriptorLengthL; + UINT8 wDescriptorLengthH; +} USB_HID_DESCR, *PUSB_HID_DESCR; + +typedef USB_HID_DESCR xdata *PXUSB_HID_DESCR; + +typedef struct _UDISK_BOC_CBW { /* command of BulkOnly USB-FlashDisk */ + UINT8 mCBW_Sig0; + UINT8 mCBW_Sig1; + UINT8 mCBW_Sig2; + UINT8 mCBW_Sig3; + UINT8 mCBW_Tag0; + UINT8 mCBW_Tag1; + UINT8 mCBW_Tag2; + UINT8 mCBW_Tag3; + UINT8 mCBW_DataLen0; + UINT8 mCBW_DataLen1; + UINT8 mCBW_DataLen2; + UINT8 mCBW_DataLen3; /* uppest byte of data length, always is 0 */ + UINT8 mCBW_Flag; /* transfer direction and etc. */ + UINT8 mCBW_LUN; + UINT8 mCBW_CB_Len; /* length of command block */ + UINT8 mCBW_CB_Buf[16]; /* command block buffer */ +} UDISK_BOC_CBW, *PUDISK_BOC_CBW; + +typedef UDISK_BOC_CBW xdata *PXUDISK_BOC_CBW; + +typedef struct _UDISK_BOC_CSW { /* status of BulkOnly USB-FlashDisk */ + UINT8 mCSW_Sig0; + UINT8 mCSW_Sig1; + UINT8 mCSW_Sig2; + UINT8 mCSW_Sig3; + UINT8 mCSW_Tag0; + UINT8 mCSW_Tag1; + UINT8 mCSW_Tag2; + UINT8 mCSW_Tag3; + UINT8 mCSW_Residue0; /* return: remainder bytes */ + UINT8 mCSW_Residue1; + UINT8 mCSW_Residue2; + UINT8 mCSW_Residue3; /* uppest byte of remainder length, always is 0 */ + UINT8 mCSW_Status; /* return: result status */ +} UDISK_BOC_CSW, *PUDISK_BOC_CSW; + +typedef UDISK_BOC_CSW xdata *PXUDISK_BOC_CSW; + +#ifdef __cplusplus +} +#endif + +#endif // __USB_DEF__ diff --git a/examples/Public/Debug.C b/examples/Public/Debug.C new file mode 100644 index 0000000..acf28ed --- /dev/null +++ b/examples/Public/Debug.C @@ -0,0 +1,271 @@ +/********************************** (C) COPYRIGHT ******************************* +* File Name : Debug.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554 DEBUG Interface + CH554Ƶ޸ġʱ + 0ʹ1ʼ + 0ʹ1շӺ + Źʼ +*******************************************************************************/ + +#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 : CH5540ӳ,ӳ䵽P0.2P0.3 +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void CH554UART0Alter() +{ + PIN_FUNC |= bUART0_PIN_X; //ӳ䵽P1.2P1.3 +} + +/******************************************************************************* +* Function Name : mInitSTDIO() +* Description : CH5540ʼ,ĬʹT1UART0IJʷ,Ҳʹ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 : CH5541ʼ +* 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; //Źֵ +} \ No newline at end of file diff --git a/examples/Public/Debug.H b/examples/Public/Debug.H new file mode 100644 index 0000000..bad3446 --- /dev/null +++ b/examples/Public/Debug.H @@ -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(); //CH5540ӳ䵽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 diff --git a/examples/SPI/MainMaster.C b/examples/SPI/MainMaster.C new file mode 100644 index 0000000..03c6924 --- /dev/null +++ b/examples/SPI/MainMaster.C @@ -0,0 +1,60 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : MainMaster.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554 SPICH376ͨѶCH376CH376ȡ +*******************************************************************************/ +#include "..\Public\CH554.H" +#include "..\Public\Debug.H" +#include "SPI.H" +#include "stdio.h" +#include + +/*Ӳӿڶ*/ +/****************************************************************************** +ʹCH554 ӲSPIӿ + CH554 DIR + P1.4 <==> SCS + P1.5 <==> MOSI + P1.6 <==> MISO + P1.7 <==> SCK +*******************************************************************************/ + + +void main() +{ + UINT8 ret,i=0; + CfgFsys( ); + mDelaymS(5); //ƵԼʱȴڲʱȶ + mInitSTDIO( ); //0ʼ + printf("start ...\n"); + + SPIMasterModeSet(3); //SPIģʽãģʽ3 + SPI_CK_SET(2); //2Ƶ +// #ifdef SPI0Interrupt +// CH554SPIInterruptInit(); //SPIжϳʼ +// EA = 1; //ʹȫж +// #endif + while(1) + { + SCS = 0; + CH554SPIMasterWrite(0x06); //CH554CH376ͨѶCH376 + mDelayuS(2); //յCH376ȡص + CH554SPIMasterWrite(i); + mDelayuS(2); + ret = CH554SPIMasterRead(); //CH376ص + SCS = 1; + if(ret != (i^0xff)) + { + printf("Err: %02X %02X \n",(UINT16)i,(UINT16)ret); //ڷݵȡӡϢ + } + mDelaymS(50); + i = i+1; + if((i%40)==0) + { + printf("success %02x\n",(UINT16)i); //ÿɹ40δӡһ + } + } +} \ No newline at end of file diff --git a/examples/SPI/MainSlave.C b/examples/SPI/MainSlave.C new file mode 100644 index 0000000..114dd45 --- /dev/null +++ b/examples/SPI/MainSlave.C @@ -0,0 +1,57 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : MainSlave.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554 SPI豸ʾSPIշȡ豸ȡ + Ȼ͸豸 +*******************************************************************************/ +#include "..\Public\CH554.H" +#include "..\Public\Debug.H" +#include "SPI.H" +#include "stdio.h" +#include "string.h" + +/*Ӳӿڶ*/ +/****************************************************************************** +ʹCH554 ӲSPIӿ + CH554 DIR + P1.4 <==> SCS + P1.5 <==> MOSI + P1.6 <==> MISO + P1.7 <==> SCK +*******************************************************************************/ + + +void main() +{ + UINT8 ret,i=0; + CfgFsys( ); + mDelaymS(5); //޸ϵͳƵԼʱȴƵȶ + + mInitSTDIO( ); //0ʼ + printf("start ...\n"); + + SPISlvModeSet( ); //SPIӻģʽ +// #if SPI0Interrupt +// CH554SPIInterruptInit(); //SPIжϳʼ +// EA = 1; //ʹȫж +// #endif + while(1) + { + CH554SPISlvWrite(i); //SPIȴȡ + mDelayuS(2); + ret = CH554SPISlvRead(); //ȡSPI + if(ret != (i^0xff)) + { + printf("Err: %02X %02X \n",(UINT16)i,(UINT16)ret); //ȡأֵ󣬴ӡ + } + mDelaymS(10); + i = i+1; + if((i%40)==0) //ÿɹ40δӡһ + { + printf("success %02x\n",(UINT16)i); + } + } +} \ No newline at end of file diff --git a/examples/SPI/SPI.C b/examples/SPI/SPI.C new file mode 100644 index 0000000..84ce6c5 --- /dev/null +++ b/examples/SPI/SPI.C @@ -0,0 +1,141 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : SPI.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554 SPIģʽӿں +עƬѡЧʱӻԶSPI0_S_PREԤֵλÿƬѡ +ЧǰSPI0_S_PREĴдԤֵ˶׸ֽڣʱע +ȡSPI0_S_PREֵһS0_IF_BYTEжϡ +ƬѡЧЧӻȽз͵ĻðֽڷŵSPI0_S_PREĴУ +ѾƬѡЧĻʹSPI0_DATAͿ +*******************************************************************************/ + +#include "..\Public\CH554.H" //Ϣӡ +#include "..\Public\Debug.H" +#include "SPI.H" + +#pragma NOAREGS + +/******************************************************************************* +* Function Name : SPIMasterModeSet( UINT8 mode ) +* Description : SPIģʽʼ +* Input : UINT8 mode +* Output : None +* Return : None +*******************************************************************************/ +void SPIMasterModeSet(UINT8 mode) +{ + SPI0_SETUP &= ~bS0_MODE_SLV; //Masterģʽ + if(mode == 0){ + SPI0_CTRL = SPI0_CTRL & ~(bS0_MST_CLK | bS0_CLR_ALL) | bS0_MOSI_OE | bS0_SCK_OE;//ģʽ0 + } + else if(mode == 3){ + SPI0_CTRL = SPI0_CTRL & ~bS0_CLR_ALL | bS0_MST_CLK | bS0_MOSI_OE | bS0_SCK_OE;//ģʽ3 + } + SPI0_CTRL &= ~bS0_MISO_OE; + P1_MOD_OC &= 0x0F; + P1_DIR_PU |= 0xB0; //SCS,MOSI,SCK + P1_DIR_PU &= 0xBF; //MISO踡 +} + +/******************************************************************************* +* Function Name : CH554SPIInterruptInit() +* Description : CH554SPIжϳʼ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void CH554SPIInterruptInit() +{ + //IP_EX |= bIP_SPI0; //SPI0жȼ + SPI0_SETUP |= bS0_IE_FIFO_OV | bS0_IE_BYTE; //ʹܽ1ֽжϣʹFIFOж + SPI0_CTRL |= bS0_AUTO_IF; //ԶS0_IF_BYTEжϱ־ + SPI0_STAT |= 0xff; //SPI0жϱ־ +#ifdef SPI_Interrupt + IE_SPI0 = 1; //ʹSPI0ж +#endif +} + +/******************************************************************************* +* Function Name : CH554SPIMasterWrite(UINT8 dat) +* Description : CH554ӲSPIд,ģʽ +* Input : UINT8 dat +* Output : None +* Return : None +*******************************************************************************/ +void CH554SPIMasterWrite(UINT8 dat) +{ + SPI0_DATA = dat; + while(S0_FREE == 0); //ȴ +//bS0_DATA_DIRΪ1˴ֱӶȡһֽڵڿٶд +} + +/******************************************************************************* +* Function Name : CH554SPIMasterRead( ) +* Description : CH554ӲSPI0ݣģʽ +* Input : None +* Output : None +* Return : UINT8 ret +*******************************************************************************/ +UINT8 CH554SPIMasterRead() +{ + SPI0_DATA = 0xff; + while(S0_FREE == 0); + return SPI0_DATA; +} + +/******************************************************************************* +* Function Name : SPISlvModeSet( ) +* Description : SPIӻģʽʼ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void SPISlvModeSet( ) +{ + SPI0_SETUP |= bS0_MODE_SLV; //Slvģʽ + SPI0_CTRL = SPI0_CTRL & ~( bS0_CLR_ALL | bS0_MOSI_OE | bS0_MISO_OE | bS0_SCK_OE) | bS0_AUTO_IF;//дFIFO,ԶS0_IF_BYTE־ + P1_MOD_OC &= 0x0F; + P1_DIR_PU &= 0x0F; //SCS,MOSI,SCK,MISOȫø +} + +/******************************************************************************* +* Function Name : CH554SPISlvWrite(UINT8 dat) +* Description : CH554ӲSPIдݣӻģʽ +* Input : UINT8 dat +* Output : None +* Return : None +*******************************************************************************/ +void CH554SPISlvWrite(UINT8 dat) +{ + while(S0_IF_BYTE); + S0_IF_BYTE = 0; + SPI0_DATA = dat; +} + +/******************************************************************************* +* Function Name : CH554SPISlvRead( ) +* Description : CH554ӲSPI0ݣӻģʽ +* Input : None +* Output : None +* Return : UINT8 ret +*******************************************************************************/ +UINT8 CH554SPISlvRead() +{ + while(S0_IF_BYTE); + S0_IF_BYTE = 0; + return SPI0_DATA; +} + +#if SPI_Interrupt +/******************************************************************************* +* Function Name : ADCInterrupt(void) +* Description : ADC жϷ +*******************************************************************************/ +void SPIInterrupt( void ) interrupt INT_NO_SPI0 using 1 //SPIжϷ,ʹüĴ1 +{ +} +#endif + diff --git a/examples/SPI/SPI.H b/examples/SPI/SPI.H new file mode 100644 index 0000000..ee4ba9a --- /dev/null +++ b/examples/SPI/SPI.H @@ -0,0 +1,67 @@ + +// #define SPI_INTERRUPT 1 + +#define SPI_CK_SET( n ) (SPI0_CK_SE = n) //SPIʱú + +/******************************************************************************* +* Function Name : SPIMasterModeSet( UINT8 mode ) +* Description : SPIģʽʼ +* Input : UINT8 mode +* Output : None +* Return : None +*******************************************************************************/ +void SPIMasterModeSet(UINT8 mode); + +/******************************************************************************* +* Function Name : CH554SPIInterruptInit() +* Description : CH554SPIжϳʼ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void CH554SPIInterruptInit(); + +/******************************************************************************* +* Function Name : CH554SPIMasterWrite(UINT8 dat) +* Description : CH554ӲSPIдݣģʽ +* Input : UINT8 dat +* Output : None +* Return : None +*******************************************************************************/ +void CH554SPIMasterWrite(UINT8 dat); + +/******************************************************************************* +* Function Name : CH554SPIMasterRead( ) +* Description : CH554ӲSPI0ݣģʽ +* Input : None +* Output : None +* Return : UINT8 ret +*******************************************************************************/ +UINT8 CH554SPIMasterRead(); + +/******************************************************************************* +* Function Name : SPISlvModeSet( ) +* Description : SPIӻģʽʼ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void SPISlvModeSet( ); + +/******************************************************************************* +* Function Name : CH554SPISlvWrite(UINT8 dat) +* Description : CH554ӲSPIдݣӻģʽ +* Input : UINT8 dat +* Output : None +* Return : None +*******************************************************************************/ +void CH554SPISlvWrite(UINT8 dat); + +/******************************************************************************* +* Function Name : CH554SPISlvRead( ) +* Description : CH554ӲSPI0ݣӻģʽ +* Input : None +* Output : None +* Return : UINT8 ret +*******************************************************************************/ +UINT8 CH554SPISlvRead(); \ No newline at end of file diff --git a/examples/Timer/Main.C b/examples/Timer/Main.C new file mode 100644 index 0000000..37405d7 --- /dev/null +++ b/examples/Timer/Main.C @@ -0,0 +1,76 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : Main.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554 Time ʼʱֵT2׽ܵ + ʱжϴ +*******************************************************************************/ +#include "..\Public\CH554.H" +#include "..\Public\Debug.H" +#include "Timer.H" +#include "stdio.h" + +#pragma NOAREGS + +UINT8 FLAG; +UINT16 Cap[8] = {0}; + +main( ) +{ + CfgFsys( ); //CH554ʱѡ + mDelaymS(5); //޸ƵԼʱȴƵȶ + mInitSTDIO( ); //ڳʼ + printf("start ...\n"); + + +#ifdef T0_INT + printf("T0 Test ...\n"); + mTimer0Clk12DivFsys(); //T0ʱʱ + mTimer_x_ModInit(0,2); //T0 ʱģʽ + mTimer_x_SetData(0,0x5555); //T0ʱֵ + mTimer0RunCTL(1); //T0ʱ + ET0 = 1; //T0ʱжϿ + EA = 1; + while(1); +#endif + +#ifdef T1_INT + printf("T1 Test ...\n"); + mTimer1Clk12DivFsys(); //T1ʱʱ + mTimer_x_ModInit(1,2); //T1 ʱģʽ + mTimer_x_SetData(1,0xEEEE); //T1ʱֵ + mTimer1RunCTL(1); //T1ʱ + ET1 = 1; //T1ʱжϿ + EA = 1; + while(1); +#endif + + +#ifdef T2_INT + printf("T2 Test ...\n"); + mTimer2ClkFsys(); //T2ʱʱ + mTimer_x_ModInit(2,0); //T2 ʱģʽ + mTimer_x_SetData(2,0xAAAA); //T2ʱֵ + mTimer2RunCTL(1); //T2ʱ + ET2 = 1; //T2ʱжϿ + EA = 1; + while(1); +#endif + + +#ifdef T2_CAP + printf("T2_CAP Test ...\n"); + mTimer2ClkFsys(); //T2ʱʱ + mTimer_x_SetData(2,0); //T2 ʱģʽò׽ģʽ + CAP2Init(1); //T2 CAP2ãز׽ + CAP1Init(1); //T2 CAP2ãز׽ + mTimer2RunCTL(1); //T2ʱ + ET2 = 1; //T2ʱжϿ + EA = 1; + while(1); +#endif + + while(1); +} \ No newline at end of file diff --git a/examples/Timer/Timer.C b/examples/Timer/Timer.C new file mode 100644 index 0000000..e6e5c6e --- /dev/null +++ b/examples/Timer/Timer.C @@ -0,0 +1,153 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : Timer.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554 Time ʼʱֵT2׽ܿ + ʱжϺ +*******************************************************************************/ +#include "..\Public\CH554.H" +#include "..\Public\Debug.H" +#include "Timer.H" +#include "stdio.h" + +#pragma NOAREGS + +/******************************************************************************* +* Function Name : mTimer_x_ModInit(UINT8 x ,UINT8 mode) +* Description : CH554ʱxģʽ +* Input : UINT8 mode,Timerģʽѡ + 0ģʽ013λʱTLnĸ3λЧ + 1ģʽ116λʱ + 2ģʽ28λԶװʱ + 3ģʽ38λʱ Timer0 + 3ģʽ3Timer1ֹͣ +* Output : None +* Return : ɹ SUCCESS + ʧ FAIL +*******************************************************************************/ +UINT8 mTimer_x_ModInit(UINT8 x ,UINT8 mode) +{ + if(x == 0){TMOD = TMOD & 0xf0 | mode;} + else if(x == 1){TMOD = TMOD & 0x0f | (mode<<4);} + else if(x == 2){RCLK = 0;TCLK = 0;CP_RL2 = 0;} //16λԶضʱ + else return FAIL; + return SUCCESS; +} + +/******************************************************************************* +* Function Name : mTimer_x_SetData(UINT8 x,UINT16 dat) +* Description : CH554Timer0 TH0TL0ֵ +* Input : UINT16 dat;ʱֵ +* Output : None +* Return : None +*******************************************************************************/ +void mTimer_x_SetData(UINT8 x,UINT16 dat) +{ + UINT16 tmp; + tmp = 65536 - dat; + if(x == 0){TL0 = tmp & 0xff;TH0 = (tmp>>8) & 0xff;} + else if(x == 1){TL1 = tmp & 0xff;TH1 = (tmp>>8) & 0xff;} + else if(x == 2){ + RCAP2L = TL2 = tmp & 0xff; //16λԶضʱ + RCAP2H = TH2 = (tmp>>8) & 0xff; + } +} + +/******************************************************************************* +* Function Name : CAP2Init(UINT8 mode) +* Description : CH554ʱ2 T2EXŲ׽ܳʼ + UINT8 mode,ز׽ģʽѡ + 0:T2ex½صһ½ + 1:T2ex֮ + 3:T2exصһ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void CAP2Init(UINT8 mode) +{ + RCLK = 0; + TCLK = 0; + C_T2 = 0; + EXEN2 = 1; + CP_RL2 = 1; //T2exIJ׽ + T2MOD |= mode << 2; //ز׽ģʽѡ +} + +/******************************************************************************* +* Function Name : CAP1Init(UINT8 mode) +* Description : CH554ʱ2 T2Ų׽ܳʼT2 + UINT8 mode,ز׽ģʽѡ + 0:T2ex½صһ½ + 1:T2ex֮ + 3:T2exصһ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void CAP1Init(UINT8 mode) +{ + RCLK = 0; + TCLK = 0; + CP_RL2 = 1; + C_T2 = 0; + T2MOD = T2MOD & ~T2OE | (mode << 2) | bT2_CAP1_EN; //ʹT2Ų׽,ز׽ģʽѡ +} + +#ifdef T0_INT +/******************************************************************************* +* Function Name : mTimer0Interrupt() +* Description : CH554ʱ0ʱжϴ +*******************************************************************************/ +void mTimer0Interrupt( void ) interrupt INT_NO_TMR0 using 1 //timer0жϷ,ʹüĴ1 +{ //ʽ3ʱTH0ʹTimer1жԴ + SCK = ~SCK; +// mTimer_x_SetData(0,0x0000); //Զطʽ¸TH0TL0ֵ +} +#endif + +#ifdef T1_INT +/******************************************************************************* +* Function Name : mTimer1Interrupt() +* Description : CH554ʱ0ʱжϴ +*******************************************************************************/ +void mTimer1Interrupt( void ) interrupt INT_NO_TMR1 using 2 //timer1жϷ,ʹüĴ2 +{ //ʽ3ʱTimer1ֹͣ + SCK = ~SCK; +// mTimer_x_SetData(1,0x0000); //Զطʽ¸TH1TL1ֵ +} +#endif + +#ifdef T2_INT +/******************************************************************************* +* Function Name : mTimer2Interrupt() +* Description : CH554ʱ0ʱжϴ +*******************************************************************************/ +void mTimer2Interrupt( void ) interrupt INT_NO_TMR2 using 3 //timer2жϷ,ʹüĴ3 +{ + mTimer2RunCTL( 0 ); //ضʱ +#ifdef T2_CAP + if(EXF2) //T2exƽ仯жжϱ־ + { + SCK = ~SCK; //P17ƽָʾ + Cap[FLAG++] = RCAP2; //T2EX + printf("RCAP2 %04x \n",RCAP2); + EXF2 = 0; //T2ex׽жϱ־ + } + if(CAP1F) //T2ƽ׽жϱ־ + { + Cap[FLAG++] = T2CAP1; //T2; + printf("T2CAP1 %04x \n",T2CAP1); + CAP1F = 0; //T2׽жϱ־ + } +#endif + if(TF2) + { + TF2 = 0; //նʱ2ж + SCK = ~SCK; //P17ƽָʾ + } + mTimer2RunCTL( 1 ); //ʱ +} +#endif diff --git a/examples/Timer/Timer.H b/examples/Timer/Timer.H new file mode 100644 index 0000000..ea84fc7 --- /dev/null +++ b/examples/Timer/Timer.H @@ -0,0 +1,92 @@ + + +// #define T0_INT 1 //TжϿ +#define T1_INT 1 +// #define T2_INT 1 +// #define T2_CAP 1 + +extern UINT8 FLAG; +extern UINT16 Cap[8]; + +//CH554 Timer0ʱѡ +//bTMR_CLKͬʱӰTimer0&1&2,ʹʱҪע (ʱʹñ׼ʱ) +#define mTimer0Clk12DivFsys( ) (T2MOD &= ~bT0_CLK) //ʱ,ʱ=Fsys/12 T0׼ʱ +#define mTimer0ClkFsys( ) (T2MOD |= bTMR_CLK | bT0_CLK) //ʱ,ʱ=Fsys +#define mTimer0Clk4DivFsys( ) (T2MOD &= ~bTMR_CLK;T2MOD |= bT0_CLK) //ʱ,ʱ=Fsys/4 +#define mTimer0CountClk( ) (TMOD |= bT0_CT) //,T0ŵ½Ч + +//CH554 Timer0 ʼ(SS=1)/(SS=0) +#define mTimer0RunCTL( SS ) (TR0 = SS ? START : STOP) + + +#define mTimer1Clk12DivFsys( ) (T2MOD &= ~bT1_CLK) //ʱ,ʱ=Fsys/12 T1׼ʱ +#define mTimer1ClkFsys( ) (T2MOD |= bTMR_CLK | bT1_CLK) //ʱ,ʱ=Fsys +#define mTimer1Clk4DivFsys( ) (T2MOD &= ~bTMR_CLK;T2MOD |= bT1_CLK) //ʱ,ʱ=Fsys/4 +#define mTimer1CountClk( ) (TMOD |= bT1_CT) //,T0ŵ½Ч + +//CH554 Timer1 ʼ(SS=1)/(SS=0) +#define mTimer1RunCTL( SS ) (TR1 = SS ? START : STOP) + + +#define mTimer2Clk12DivFsys( ) {T2MOD &= ~(bTMR_CLK | bT2_CLK);C_T2 = 0;} //ʱ,ʱ=Fsys/12 T2׼ʱ +#define mTimer2ClkFsys( ) {T2MOD |= (bTMR_CLK | bT2_CLK);C_T2=0;} //ʱ,ʱ=Fsys +#define mTimer2Clk4DivFsys( ) {T2MOD &= ~bTMR_CLK;T2MOD |= bT2_CLK;C_T2 = 0;}//ʱ,ʱ=Fsys/4 +#define mTimer2CountClk( ) {C_T2 = 1;} //,T2ŵ½Ч + +//CH554 Timer2 ʼ(SS=1)/(SS=0) +#define mTimer2RunCTL( SS ) {TR2 = SS ? START : STOP;} +#define mTimer2OutCTL( ) (T2MOD |= T2OE) //T2 ƵTF2/2 +#define CAP1Alter( ) (PIN_FUNC |= bT2_PIN_X;) //CAP1P10 ӳ䵽P14 +#define CAP2Alter( ) (PIN_FUNC |= bT2EX_PIN_X;) //CAP2P11 ӳRST + +/******************************************************************************* +* Function Name : mTimer_x_ModInit(UINT8 x ,UINT8 mode) +* Description : CH554ʱxģʽ +* Input : UINT8 mode,Timerģʽѡ + 0ģʽ013λʱTLnĸ3λЧ + 1ģʽ116λʱ + 2ģʽ28λԶװʱ + 3ģʽ38λʱ Timer0 + 3ģʽ3Timer1ֹͣ + UINT8 x ʱ 0 1 2 +* Output : None +* Return : ɹ SUCCESS + ʧ FAIL +*******************************************************************************/ +UINT8 mTimer_x_ModInit(UINT8 x ,UINT8 mode); + +/******************************************************************************* +* Function Name : mTimer_x_SetData(UINT8 x,UINT16 dat) +* Description : CH554Timer +* Input : UINT16 dat;ʱֵ + UINT8 x ʱ 0 1 2 +* Output : None +* Return : None +*******************************************************************************/ +void mTimer_x_SetData(UINT8 x,UINT16 dat); + +/******************************************************************************* +* Function Name : CAP2Init(UINT8 mode) +* Description : CH554ʱ2 T2EXŲ׽ܳʼ + UINT8 mode,ز׽ģʽѡ + 0:T2ex½صһ½ + 1:T2ex֮ + 3:T2exصһ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void CAP2Init(UINT8 mode); + +/******************************************************************************* +* Function Name : CAP1Init(UINT8 mode) +* Description : CH554ʱ2 T2Ų׽ܳʼT2 + UINT8 mode,ز׽ģʽѡ + 0:T2ex½صһ½ + 1:T2ex֮ + 3:T2exصһ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void CAP1Init(UINT8 mode); \ No newline at end of file diff --git a/examples/TouchKey/Main.C b/examples/TouchKey/Main.C new file mode 100644 index 0000000..0928a4e --- /dev/null +++ b/examples/TouchKey/Main.C @@ -0,0 +1,59 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : Main.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554 жϺͲѯʽвɼ浱ǰͨ״̬ʼͰʾ +*******************************************************************************/ +#include "..\Public\CH554.H" +#include "..\Public\Debug.H" +#include "TouchKey.H" +#include "stdio.h" + +#pragma NOAREGS + +main( ) +{ + UINT8 i; + CfgFsys( ); //CH554ʱѡ + mDelaymS(5); //޸ƵԼʱȴоƬȶ + mInitSTDIO( ); //0ʼ + printf("start ...\n"); + + P1_DIR_PU &= 0x0C; //дͨΪ룬òͨԲ + TouchKeyQueryCyl2ms(); //TouchKeyѯ2ms + GetTouckKeyFreeBuf(); //ȡ׼ֵ +#if DE_PRINTF + for(i=KEY_FIRST;i<(KEY_LAST+1);i++) //ӡ׼ֵ + { + printf("Channel %02x base sample %04x\n",(UINT16)i,KeyFree[i]); + } +#endif + +#if INTERRUPT_TouchKey + EA = 1; + while(1) + { + if(KeyBuf) //key_buf0ʾ⵽ + { + printf("INT TouchKey Channel %02x \n",(UINT16)KeyBuf); //ӡǰ״̬ͨ + KeyBuf = 0; //±־ + mDelaymS(100); //ʱ壬ģⵥƬ + } + mDelaymS(100); //ʱ壬ģⵥƬ + } +#else + while(1) + { + TouchKeyChannelQuery(); //ѯ״̬ + if(KeyBuf) //key_buf0ʾ⵽ + { + printf("Query TouchKey Channel %02x \n",(UINT16)KeyBuf); //ӡǰ״̬ͨ + KeyBuf = 0; //±־ + mDelaymS(100); //ʱ壬ģⵥƬ + } + mDelaymS(100); //ʱ壬ģⵥƬ + } +#endif +} \ No newline at end of file diff --git a/examples/TouchKey/TouchKey.C b/examples/TouchKey/TouchKey.C new file mode 100644 index 0000000..cae6a45 --- /dev/null +++ b/examples/TouchKey/TouchKey.C @@ -0,0 +1,120 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : TouchKey.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554 áͨѡлжϴ +*******************************************************************************/ + +#include "..\Public\CH554.H" +#include "..\Public\Debug.H" +#include "TouchKey.H" +#include "stdio.h" + +#pragma NOAREGS + +UINT16 KeyFree[KEY_LAST-KEY_FIRST+1]; //ֵ洢ڱȽϰ״ֵ̬Сڻ׼ֵ +UINT8V KeyBuf; //״̬Ϊ0ʾް0ʾǰⰴ + +/******************************************************************************* +* Function Name : GetTouckKeyFreeBuf() +* Description : ȡռ״ֵ̬ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void GetTouckKeyFreeBuf() +{ + UINT8 i,j; + UINT8 TmpSum = 0; + KeyBuf = 0; //ʼΪް״̬ + for(i=KEY_FIRST;i<(KEY_LAST+1);i++) + { + j = KEY_BASE_SAMPLE_TIME; //ɶƽֵΪο + TKEY_CTRL = (TKEY_CTRL & 0xF8 | i)+1; //òͨ + while(j--) + { + while((TKEY_CTRL&bTKC_IF) == 0); //bTKC_IFΪ1ʱڲ + TmpSum += TKEY_DAT&0x0F; //ֵȶȡ4λ͹ + } + KeyFree[i] = TKEY_DAT&0x07F0 + TmpSum/5; //ֵ + } +#if INTERRUPT_TouchKey + IE_TKEY = 1; //ʹTouch_Keyж +#endif +} + +/******************************************************************************* +* Function Name : TouchKeyChannelSelect(UINT8 ch) +* Description : ͨѡ +* Input : UINT8 ch ͨ + 0~5 ֱͨ +* Output : None +* Return : ɹ SUCCESS + ʧ FAIL ֵ֧ͨ +*******************************************************************************/ +UINT8 TouchKeyChannelSelect(UINT8 ch) +{ + if(ch < 6) + { + TKEY_CTRL = (TKEY_CTRL & 0xF8 | ch)+1; + return SUCCESS; + } + return FAIL; +} + +#if INTERRUPT_TouchKey +/******************************************************************************* +* Function Name : TouchKeyInterrupt(void) +* Description : Touch_Key жϷ +*******************************************************************************/ +void TouchKeyInterrupt( void ) interrupt INT_NO_TKEY using 1 //Touch_KeyжϷ,ʹüĴ1 +{ + UINT8 ch; + UINT16 KeyData; + + KeyData = TKEY_DAT; //87us,ȡ + ch = TKEY_CTRL&7; //ȡǰͨ + if ( ch > KEY_LAST ){ + TKEY_CTRL = TKEY_CTRL & 0xF8 | KEY_FIRST; //ͨʼ + } + else + { + TKEY_CTRL ++; //лһͨ + } + if ( KeyData < (KeyFree[ch-KEY_FIRST] - KEY_ACT) ) //㣬 + { + KeyBuf=ch; //ڴ˴аñ־֪ͨmainд + } +} +#else +/******************************************************************************* +* Function Name : TouchKeyChannelQuery() +* Description : ͨ״̬ѯ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void TouchKeyChannelQuery() +{ + UINT8 ch; + UINT16 KeyData; + + while((TKEY_CTRL&bTKC_IF) == 0); //bTKC_IFΪ1ʱڲ + KeyData = TKEY_DAT; //87us,ȡ + ch = TKEY_CTRL&7; //ȡǰͨ + if ( ch > KEY_LAST ){ + TKEY_CTRL = TKEY_CTRL & 0xF8 | KEY_FIRST; //ͨʼ + } + else + { + TKEY_CTRL ++; //лһͨ + } + if ( KeyData < (KeyFree[ch-KEY_FIRST] - KEY_ACT) ) //㣬 + { + KeyBuf=ch; //ڴ˴аñ־֪ͨmainд + } +} +#endif + diff --git a/examples/TouchKey/TouchKey.H b/examples/TouchKey/TouchKey.H new file mode 100644 index 0000000..350da23 --- /dev/null +++ b/examples/TouchKey/TouchKey.H @@ -0,0 +1,49 @@ + +#define INTERRUPT_TouchKey 0 //TouchKeyжϷʽ + +#define KEY_FIRST 0 //ʼͨ +#define KEY_LAST 3 //ͨ +#define KEY_ACT 20 //£ֵͨСֵСȸߣֵȵ +#define KEY_BASE_SAMPLE_TIME 5 //׼ֵΪȡȶͨ׼ֵ + +extern UINT16 KeyFree[KEY_LAST-KEY_FIRST+1]; //ֵ洢ڱȽϰ״ֵ̬Сڻ׼ֵ +extern UINT8V KeyBuf; //״̬Ϊ0ʾްΪ1ʾǰⰴ + +#define TouchKeyOFF() {TKEY_CTRL &= 0xF8;} //رյݼ⣬1ms2msʱж +#define TouchKeyON_NoChannel() {TKEY_CTRL = TKEY_CTRL & 0xF8 | 7;} //ݼ⣬Dzͨ +#define TouchKeyQueryCyl1ms() {TKEY_CTRL &= ~bTKC_2MS;} //1ms +#define TouchKeyQueryCyl2ms() {TKEY_CTRL |= bTKC_2MS;} //2ms + +/******************************************************************************* +* Function Name : TouchKeyChannelSelect(UINT8 ch) +* Description : ͨѡ +* Input : UINT8 ch ͨ + 0: رյݼ⣬1ms2msʱж + 1~6 ֱͨ + 7: ݼ⣬Dzͨ +* Output : None +* Return : ɹ SUCCESS + ʧ FAIL +*******************************************************************************/ +UINT8 TouchKeyChannelSelect(UINT8 ch); + +/******************************************************************************* +* Function Name : GetTouckKeyFreeBuf() +* Description : ȡռ״ֵ̬ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void GetTouckKeyFreeBuf(); + +#if !INTERRUPT_TouchKey +/******************************************************************************* +* Function Name : TouchKeyChannelQuery() +* Description : ͨ״̬ѯ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void TouchKeyChannelQuery(); +#endif + diff --git a/examples/USB/Device/CompatibilityHID.C b/examples/USB/Device/CompatibilityHID.C new file mode 100644 index 0000000..61ba81a --- /dev/null +++ b/examples/USB/Device/CompatibilityHID.C @@ -0,0 +1,450 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name :CompatibilityHID.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554ģHID豸֧ж´֧ȫ٣ +*******************************************************************************/ + +#include "./Public/CH554.H" +#include "./Public/Debug.H" +#include +#include + +#define Fullspeed 1 +#ifdef Fullspeed +#define THIS_ENDP0_SIZE 64 +#else +#define THIS_ENDP0_SIZE DEFAULT_ENDP0_SIZE +#endif +UINT8X Ep0Buffer[8<(THIS_ENDP0_SIZE+2)?8:(THIS_ENDP0_SIZE+2)] _at_ 0x0000; //˵0 OUT&INżַ +UINT8X Ep2Buffer[128<(2*MAX_PACKET_SIZE+4)?128:(2*MAX_PACKET_SIZE+4)] _at_ 0x0044;//˵2 IN&OUT,żַ +UINT8 SetupReq,SetupLen,Ready,Count,FLAG,UsbConfig; +PUINT8 pDescr; //USBñ־ +USB_SETUP_REQ SetupReqBuf; //ݴSetup +#define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer) + +sbit Ep2InKey = P1^5; //K1 +#pragma NOAREGS +/*豸*/ +UINT8C DevDesc[18] = {0x12,0x01,0x10,0x01,0x00,0x00,0x00,THIS_ENDP0_SIZE, + 0x31,0x51,0x07,0x20,0x00,0x00,0x00,0x00, + 0x00,0x01 + }; +UINT8C CfgDesc[41] = +{ + 0x09,0x02,0x29,0x00,0x01,0x01,0x04,0xA0,0x23, // + 0x09,0x04,0x00,0x00,0x02,0x03,0x00,0x00,0x05, //ӿ + 0x09,0x21,0x00,0x01,0x00,0x01,0x22,0x22,0x00, //HID + 0x07,0x05,0x82,0x03,THIS_ENDP0_SIZE,0x00,0x18, //˵ + 0x07,0x05,0x02,0x03,THIS_ENDP0_SIZE,0x00,0x18, //˵ +}; +/*ַ */ + +/*HID౨*/ +UINT8C HIDRepDesc[ ] = +{ + 0x06, 0x00,0xff, + 0x09, 0x01, + 0xa1, 0x01, //Ͽʼ + 0x09, 0x02, //Usage Page ÷ + 0x15, 0x00, //Logical Minimun + 0x26, 0x00,0xff, //Logical Maximun + 0x75, 0x08, //Report Size + 0x95, THIS_ENDP0_SIZE, //Report Counet + 0x81, 0x06, //Input + 0x09, 0x02, //Usage Page ÷ + 0x15, 0x00, //Logical Minimun + 0x26, 0x00,0xff, //Logical Maximun + 0x75, 0x08, //Report Size + 0x95, THIS_ENDP0_SIZE, //Report Counet + 0x91, 0x06, //Output + 0xC0 +}; +// unsigned char code LangDes[]={0x04,0x03,0x09,0x04}; // +// unsigned char code SerDes[]={ +// 0x28,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +// 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +// 0x00,0x00,0x00,0x00,0x00,0x49,0x00,0x43,0x00,0x42, +// 0x00,0x43,0x00,0x31,0x00,0x00,0x00,0x00,0x00,0x00 +// }; //ַ + +UINT8X UserEp2Buf[64]; //ûݶ + +/******************************************************************************* +* Function Name : USBDeviceInit() +* Description : USB豸ģʽ,豸ģʽշ˵ãжϿ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void USBDeviceInit() +{ + IE_USB = 0; + USB_CTRL = 0x00; // 趨USB豸ģʽ +#ifndef Fullspeed + UDEV_CTRL |= bUD_LOW_SPEED; //ѡ1.5Mģʽ +#else + UDEV_CTRL &= ~bUD_LOW_SPEED; //ѡȫ12MģʽĬϷʽ +#endif + UEP2_DMA = Ep2Buffer; //˵2ݴַ + UEP2_3_MOD |= bUEP2_TX_EN; //˵2ʹ + UEP2_3_MOD |= bUEP2_RX_EN; //˵2ʹ + UEP2_3_MOD &= ~bUEP2_BUF_MOD; //˵2շ64ֽڻ + UEP2_CTRL = bUEP_AUTO_TOG | UEP_T_RES_NAK | UEP_R_RES_ACK; //˵2Զתͬ־λIN񷵻NAKOUTACK + UEP0_DMA = Ep0Buffer; //˵0ݴַ + UEP4_1_MOD &= ~(bUEP4_RX_EN | bUEP4_TX_EN); //˵064ֽշ + UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK; //OUT񷵻ACKIN񷵻NAK + + USB_DEV_AD = 0x00; + UDEV_CTRL = bUD_PD_DIS; // ֹDP/DM + USB_CTRL = bUC_DEV_PU_EN | bUC_INT_BUSY | bUC_DMA_EN; // USB豸DMAжڼжϱ־δǰԶNAK + UDEV_CTRL |= bUD_PORT_EN; // USB˿ + USB_INT_FG = 0xFF; // жϱ־ + USB_INT_EN = bUIE_SUSPEND | bUIE_TRANSFER | bUIE_BUS_RST; + IE_USB = 1; +} + +/******************************************************************************* +* Function Name : Enp2BlukIn() +* Description : USB豸ģʽ˵2ϴ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Enp2BlukIn( ) +{ + memcpy( Ep2Buffer+MAX_PACKET_SIZE, UserEp2Buf, sizeof(UserEp2Buf)); //ϴ + UEP2_T_LEN = THIS_ENDP0_SIZE; //ϴ + UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //ʱϴݲӦACK + while(UEP2_CTRL&UEP_T_RES_ACK); //ȴ +} + +/******************************************************************************* +* Function Name : DeviceInterrupt() +* Description : CH559USBжϴ +*******************************************************************************/ +void DeviceInterrupt( void ) interrupt INT_NO_USB using 1 //USBжϷ,ʹüĴ1 +{ + UINT8 len,i; + if(UIF_TRANSFER) //USBɱ־ + { + switch (USB_INT_ST & (MASK_UIS_TOKEN | MASK_UIS_ENDP)) + { + case UIS_TOKEN_IN | 2: //endpoint 2# ˵ϴ + UEP2_T_LEN = 0; //Ԥʹ÷ͳһҪ +// UEP1_CTRL ^= bUEP_T_TOG; //ԶתҪֶת + UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; //ĬӦNAK + break; + case UIS_TOKEN_OUT | 2: //endpoint 2# ˵´ + if ( U_TOG_OK ) // ͬݰ + { + len = USB_RX_LEN; //ݳȣݴEp2Buffer׵ַʼ + for ( i = 0; i < len; i ++ ) + { + Ep2Buffer[MAX_PACKET_SIZE+i] = Ep2Buffer[i] ^ 0xFF; // OUTȡINɼ֤ + } + UEP2_T_LEN = len; + UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; // ϴ + } + break; + case UIS_TOKEN_SETUP | 0: //SETUP + len = USB_RX_LEN; + if(len == (sizeof(USB_SETUP_REQ))) + { + SetupLen = UsbSetupBuf->wLengthL; + if(UsbSetupBuf->wLengthH || SetupLen > 0x7F ) + { + SetupLen = 0x7F; // ܳ + } + len = 0; // ĬΪɹϴ0 + SetupReq = UsbSetupBuf->bRequest; + if ( ( UsbSetupBuf->bRequestType & USB_REQ_TYP_MASK ) != USB_REQ_TYP_STANDARD )/*HID*/ + { + switch( SetupReq ) + { + case 0x01: //GetReport + break; + case 0x02: //GetIdle + break; + case 0x03: //GetProtocol + break; + case 0x09: //SetReport + break; + case 0x0A: //SetIdle + break; + case 0x0B: //SetProtocol + break; + default: + len = 0xFF; /*֧*/ + break; + } + } + else //׼ + { + switch(SetupReq) // + { + case USB_GET_DESCRIPTOR: + switch(UsbSetupBuf->wValueH) + { + case 1: //豸 + pDescr = DevDesc; //豸͵Ҫ͵Ļ + len = sizeof(DevDesc); + break; + case 2: // + pDescr = CfgDesc; //豸͵Ҫ͵Ļ + len = sizeof(CfgDesc); + break; + case 0x22: // + pDescr = HIDRepDesc; //׼ϴ + len = sizeof(HIDRepDesc); + Ready = 1; //иӿڣñ׼λӦһӿɺЧ + break; + default: + len = 0xff; //ֵ֧߳ + break; + } + if ( SetupLen > len ) + { + SetupLen = len; //ܳ + } + len = SetupLen >= THIS_ENDP0_SIZE ? THIS_ENDP0_SIZE : SetupLen;//δ䳤 + memcpy(Ep0Buffer,pDescr,len); //ϴ + SetupLen -= len; + pDescr += len; + break; + case USB_SET_ADDRESS: + SetupLen = UsbSetupBuf->wValueL; //ݴUSB豸ַ + 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 0x02: + UEP2_CTRL = UEP2_CTRL & ~ ( bUEP_R_TOG | MASK_UEP_R_RES ) | UEP_R_RES_ACK; + break; + default: + len = 0xFF; // ֵ֧Ķ˵ + break; + } + } + else + { + len = 0xFF; // Ƕ˵㲻֧ + } + break; + case USB_SET_FEATURE: /* Set Feature */ + if( ( UsbSetupBuf->bRequestType & 0x1F ) == 0x00 ) /* 豸 */ + { + if( ( ( ( UINT16 )UsbSetupBuf->wValueH << 8 ) | UsbSetupBuf->wValueL ) == 0x01 ) + { + if( CfgDesc[ 7 ] & 0x20 ) + { + /* ûʹܱ־ */ + } + else + { + len = 0xFF; /* ʧ */ + } + } + else + { + len = 0xFF; /* ʧ */ + } + } + else if( ( UsbSetupBuf->bRequestType & 0x1F ) == 0x02 ) /* ö˵ */ + { + 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;/* ö˵2 IN STALL */ + break; + case 0x02: + UEP2_CTRL = UEP2_CTRL & (~bUEP_R_TOG) | UEP_R_RES_STALL;/* ö˵2 OUT Stall */ + break; + case 0x81: + UEP1_CTRL = UEP1_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL;/* ö˵1 IN STALL */ + break; + default: + len = 0xFF; /* ʧ */ + break; + } + } + else + { + len = 0xFF; /* ʧ */ + } + } + else + { + len = 0xFF; /* ʧ */ + } + break; + case USB_GET_STATUS: + Ep0Buffer[0] = 0x00; + Ep0Buffer[1] = 0x00; + if ( SetupLen >= 2 ) + { + len = 2; + } + else + { + len = SetupLen; + } + break; + default: + len = 0xff; //ʧ + break; + } + } + } + else + { + len = 0xff; //ȴ + } + if(len == 0xff) + { + SetupReq = 0xFF; + UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_STALL | UEP_T_RES_STALL;//STALL + } + else if(len <= THIS_ENDP0_SIZE) //ϴݻ״̬׶η0Ȱ + { + UEP0_T_LEN = len; + UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK;//ĬݰDATA1ӦACK + } + else + { + UEP0_T_LEN = 0; //Ȼδ״̬׶ΣǰԤϴ0ݰԷǰ״̬׶ + UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK;//ĬݰDATA1,ӦACK + } + break; + case UIS_TOKEN_IN | 0: //endpoint0 IN + switch(SetupReq) + { + case USB_GET_DESCRIPTOR: + len = SetupLen >= THIS_ENDP0_SIZE ? THIS_ENDP0_SIZE : SetupLen; //δ䳤 + memcpy( Ep0Buffer, pDescr, len ); //ϴ + SetupLen -= len; + pDescr += len; + UEP0_T_LEN = len; + UEP0_CTRL ^= bUEP_T_TOG; //ͬ־λת + 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; //״̬׶жϻǿϴ0ݰƴ + UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK; + break; + } + break; + case UIS_TOKEN_OUT | 0: // endpoint0 OUT + len = USB_RX_LEN; + if(SetupReq == 0x09) + { + if(Ep0Buffer[0]) + { + printf("Light on Num Lock LED!\n"); + } + else if(Ep0Buffer[0] == 0) + { + printf("Light off Num Lock LED!\n"); + } + } + UEP0_T_LEN = 0; //Ȼδ״̬׶ΣǰԤϴ0ݰԷǰ״̬׶ + UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_ACK;//ĬݰDATA0,ӦACK + break; + default: + break; + } + UIF_TRANSFER = 0; //д0ж + } + if(UIF_BUS_RST) //豸ģʽUSB߸λж + { + 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; //жϱ־ + } + if (UIF_SUSPEND) //USB߹/ + { + UIF_SUSPEND = 0; + if ( USB_MIS_ST & bUMS_SUSPEND ) // + { +#ifdef DE_PRINTF + printf( "zz" ); //˯״̬ +#endif + while ( XBUS_AUX & bUART0_TX ) + { + ; //ȴ + } + SAFE_MOD = 0x55; + SAFE_MOD = 0xAA; + WAKE_CTRL = bWAK_BY_USB | bWAK_RXD0_LO; //USBRXD0źʱɱ + PCON |= PD; //˯ + SAFE_MOD = 0x55; + SAFE_MOD = 0xAA; + WAKE_CTRL = 0x00; + } + } + else { //ж,ܷ + USB_INT_FG = 0xFF; //жϱ־ +// printf("UnknownInt N"); + } +} + +main() +{ + UINT8 i; + CfgFsys( ); //CH559ʱѡ + mDelaymS(5); //޸Ƶȴڲȶ,ؼ + mInitSTDIO( ); //0ʼ +#ifdef DE_PRINTF + printf("start ...\n"); +#endif + for(i=0; i<64; i++) //׼ʾ + { + UserEp2Buf[i] = i; + } + USBDeviceInit(); //USB豸ģʽʼ + EA = 1; //Ƭж + UEP1_T_LEN = 0; //Ԥʹ÷ͳһҪ + UEP2_T_LEN = 0; //Ԥʹ÷ͳһҪ + FLAG = 0; + Ready = 0; + while(1) + { + if(Ready&& (Ep2InKey==0)) + { + Enp2BlukIn( ); + mDelaymS( 100 ); + } + mDelaymS( 100 ); //ģⵥƬ + } +} diff --git a/examples/USB/Device/CompositeKM.C b/examples/USB/Device/CompositeKM.C new file mode 100644 index 0000000..d574e7b --- /dev/null +++ b/examples/USB/Device/CompositeKM.C @@ -0,0 +1,514 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name :CompositeKM.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH559ģUSB豸֧ +*******************************************************************************/ + +#include "./Public/CH554.H" +#include "./Public/Debug.H" +#include +#include + +#define THIS_ENDP0_SIZE DEFAULT_ENDP0_SIZE + +UINT8X Ep0Buffer[8<(THIS_ENDP0_SIZE+2)?8:(THIS_ENDP0_SIZE+2)] _at_ 0x0000; //˵0 OUT&INżַ +UINT8X Ep1Buffer[64<(MAX_PACKET_SIZE+2)?64:(MAX_PACKET_SIZE+2)] _at_ 0x000a; //˵1 IN,żַ +UINT8X Ep2Buffer[64<(MAX_PACKET_SIZE+2)?64:(MAX_PACKET_SIZE+2)] _at_ 0x0050; //˵2 IN,żַ +UINT8 SetupReq,SetupLen,Ready,Count,FLAG,UsbConfig; +PUINT8 pDescr; //USBñ־ +USB_SETUP_REQ SetupReqBuf; //ݴSetup +sbit Ep2InKey = P1^5; +#define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer) +#define DEBUG 0 +#pragma NOAREGS +/*豸*/ +UINT8C DevDesc[18] = {0x12,0x01,0x10,0x01,0x00,0x00,0x00,0x08, + 0x3d,0x41,0x07,0x21,0x00,0x00,0x00,0x00, + 0x00,0x01 + }; +UINT8C CfgDesc[59] = +{ + 0x09,0x02,0x3b,0x00,0x02,0x01,0x00,0xA0,0x32, // + 0x09,0x04,0x00,0x00,0x01,0x03,0x01,0x01,0x00, //ӿ, + 0x09,0x21,0x11,0x01,0x00,0x01,0x22,0x3e,0x00, //HID + 0x07,0x05,0x81,0x03,0x08,0x00,0x0a, //˵ + 0x09,0x04,0x01,0x00,0x01,0x03,0x01,0x02,0x00, //ӿ, + 0x09,0x21,0x10,0x01,0x00,0x01,0x22,0x34,0x00, //HID + 0x07,0x05,0x82,0x03,0x04,0x00,0x0a //˵ +}; +/*ַ*/ +/*HID౨*/ +UINT8C KeyRepDesc[62] = +{ + 0x05,0x01,0x09,0x06,0xA1,0x01,0x05,0x07, + 0x19,0xe0,0x29,0xe7,0x15,0x00,0x25,0x01, + 0x75,0x01,0x95,0x08,0x81,0x02,0x95,0x01, + 0x75,0x08,0x81,0x01,0x95,0x03,0x75,0x01, + 0x05,0x08,0x19,0x01,0x29,0x03,0x91,0x02, + 0x95,0x05,0x75,0x01,0x91,0x01,0x95,0x06, + 0x75,0x08,0x26,0xff,0x00,0x05,0x07,0x19, + 0x00,0x29,0x91,0x81,0x00,0xC0 +}; +UINT8C MouseRepDesc[52] = +{ + 0x05,0x01,0x09,0x02,0xA1,0x01,0x09,0x01, + 0xA1,0x00,0x05,0x09,0x19,0x01,0x29,0x03, + 0x15,0x00,0x25,0x01,0x75,0x01,0x95,0x03, + 0x81,0x02,0x75,0x05,0x95,0x01,0x81,0x01, + 0x05,0x01,0x09,0x30,0x09,0x31,0x09,0x38, + 0x15,0x81,0x25,0x7f,0x75,0x08,0x95,0x03, + 0x81,0x06,0xC0,0xC0 +}; +/**/ +UINT8 HIDMouse[4] = {0x0,0x0,0x0,0x0}; +/**/ +UINT8 HIDKey[8] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}; + +/******************************************************************************* +* Function Name : USBDeviceInit() +* Description : USB豸ģʽ,豸ģʽշ˵ãжϿ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void USBDeviceInit() +{ + IE_USB = 0; + USB_CTRL = 0x00; // 趨USB豸ģʽ + UEP2_DMA = Ep2Buffer; //˵2ݴַ + UEP2_3_MOD = UEP2_3_MOD & ~bUEP2_BUF_MOD | bUEP2_TX_EN; //˵2ʹ 64ֽڻ + UEP2_CTRL = bUEP_AUTO_TOG | UEP_T_RES_NAK; //˵2Զתͬ־λIN񷵻NAK + UEP0_DMA = Ep0Buffer; //˵0ݴַ + UEP4_1_MOD &= ~(bUEP4_RX_EN | bUEP4_TX_EN); //˵064ֽշ + UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK; //OUT񷵻ACKIN񷵻NAK + UEP1_DMA = Ep1Buffer; //˵1ݴַ + UEP4_1_MOD = UEP4_1_MOD & ~bUEP1_BUF_MOD | bUEP1_TX_EN; //˵1ʹ 64ֽڻ + UEP1_CTRL = bUEP_AUTO_TOG | UEP_T_RES_NAK; //˵1Զתͬ־λIN񷵻NAK + + USB_DEV_AD = 0x00; + UDEV_CTRL = bUD_PD_DIS; // ֹDP/DM + USB_CTRL = bUC_DEV_PU_EN | bUC_INT_BUSY | bUC_DMA_EN; // USB豸DMAжڼжϱ־δǰԶNAK + UDEV_CTRL |= bUD_PORT_EN; // USB˿ + USB_INT_FG = 0xFF; // жϱ־ + USB_INT_EN = bUIE_SUSPEND | bUIE_TRANSFER | bUIE_BUS_RST; + IE_USB = 1; +} +/******************************************************************************* +* Function Name : Enp1IntIn() +* Description : USB豸ģʽ˵1жϴ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Enp1IntIn( ) +{ + memcpy( Ep1Buffer, HIDKey, sizeof(HIDKey)); //ϴ + UEP1_T_LEN = sizeof(HIDKey); //ϴݳ + UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //ʱϴݲӦACK +} +/******************************************************************************* +* Function Name : Enp2IntIn() +* Description : USB豸ģʽ˵2жϴ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void Enp2IntIn( ) +{ + memcpy( Ep2Buffer, HIDMouse, sizeof(HIDMouse)); //ϴ + UEP2_T_LEN = sizeof(HIDMouse); //ϴݳ + UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //ʱϴݲӦACK +} +/******************************************************************************* +* Function Name : DeviceInterrupt() +* Description : CH559USBжϴ +*******************************************************************************/ +void DeviceInterrupt( void ) interrupt INT_NO_USB using 1 //USBжϷ,ʹüĴ1 +{ + UINT8 len; + if(UIF_TRANSFER) //USBɱ־ + { + switch (USB_INT_ST & (MASK_UIS_TOKEN | MASK_UIS_ENDP)) + { + case UIS_TOKEN_IN | 2: //endpoint 2# ж϶˵ϴ + UEP2_T_LEN = 0; //Ԥʹ÷ͳһҪ +// UEP1_CTRL ^= bUEP_T_TOG; //ԶתҪֶת + UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; //ĬӦNAK + break; + case UIS_TOKEN_IN | 1: //endpoint 1# ж϶˵ϴ + UEP1_T_LEN = 0; //Ԥʹ÷ͳһҪ +// UEP2_CTRL ^= bUEP_T_TOG; //ԶתҪֶת + UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; //ĬӦNAK + FLAG = 1; /*ɱ־*/ + break; + case UIS_TOKEN_SETUP | 0: //SETUP + len = USB_RX_LEN; + if(len == (sizeof(USB_SETUP_REQ))) + { + SetupLen = UsbSetupBuf->wLengthL; + if(UsbSetupBuf->wLengthH || SetupLen > 0x7F ) + { + SetupLen = 0x7F; // ܳ + } + len = 0; // ĬΪɹϴ0 + SetupReq = UsbSetupBuf->bRequest; + if ( ( UsbSetupBuf->bRequestType & USB_REQ_TYP_MASK ) != USB_REQ_TYP_STANDARD )/* HID */ + { + switch( SetupReq ) + { + case 0x01://GetReport + break; + case 0x02://GetIdle + break; + case 0x03://GetProtocol + break; + case 0x09://SetReport + break; + case 0x0A://SetIdle + break; + case 0x0B://SetProtocol + break; + default: + len = 0xFF; /*֧*/ + break; + } + } + else + {//׼ + switch(SetupReq) // + { + case USB_GET_DESCRIPTOR: + switch(UsbSetupBuf->wValueH) + { + case 1: //豸 + pDescr = DevDesc; //豸͵Ҫ͵Ļ + len = sizeof(DevDesc); + break; + case 2: // + pDescr = CfgDesc; //豸͵Ҫ͵Ļ + len = sizeof(CfgDesc); + break; + case 0x22: // + if(UsbSetupBuf->wIndexL == 0) //ӿ0 + { + pDescr = KeyRepDesc; //׼ϴ + len = sizeof(KeyRepDesc); + } + else if(UsbSetupBuf->wIndexL == 1) //ӿ1 + { + pDescr = MouseRepDesc; //׼ϴ + len = sizeof(MouseRepDesc); + Ready = 1; //иӿڣñ׼λӦһӿɺЧ + } + else + { + len = 0xff; //ֻ2ӿڣ仰ִ + } + break; + default: + len = 0xff; //ֵ֧߳ + break; + } + if ( SetupLen > len ) + { + SetupLen = len; //ܳ + } + len = SetupLen >= 8 ? 8 : SetupLen; //δ䳤 + memcpy(Ep0Buffer,pDescr,len); //ϴ + SetupLen -= len; + pDescr += len; + break; + case USB_SET_ADDRESS: + SetupLen = UsbSetupBuf->wValueL; //ݴUSB豸ַ + 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; // ֵ֧Ķ˵ + break; + } + } + else + { + len = 0xFF; // Ƕ˵㲻֧ + } + break; + case USB_SET_FEATURE: /* Set Feature */ + if( ( UsbSetupBuf->bRequestType & 0x1F ) == 0x00 ) /* 豸 */ + { + if( ( ( ( UINT16 )UsbSetupBuf->wValueH << 8 ) | UsbSetupBuf->wValueL ) == 0x01 ) + { + if( CfgDesc[ 7 ] & 0x20 ) + { + /* ûʹܱ־ */ + } + else + { + len = 0xFF; /* ʧ */ + } + } + else + { + len = 0xFF; /* ʧ */ + } + } + else if( ( UsbSetupBuf->bRequestType & 0x1F ) == 0x02 ) /* ö˵ */ + { + 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;/* ö˵2 IN STALL */ + break; + case 0x02: + UEP2_CTRL = UEP2_CTRL & (~bUEP_R_TOG) | UEP_R_RES_STALL;/* ö˵2 OUT Stall */ + break; + case 0x81: + UEP1_CTRL = UEP1_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL;/* ö˵1 IN STALL */ + break; + default: + len = 0xFF; //ʧ + break; + } + } + else + { + len = 0xFF; //ʧ + } + } + else + { + len = 0xFF; //ʧ + } + break; + case USB_GET_STATUS: + Ep0Buffer[0] = 0x00; + Ep0Buffer[1] = 0x00; + if ( SetupLen >= 2 ) + { + len = 2; + } + else + { + len = SetupLen; + } + break; + default: + len = 0xff; //ʧ + break; + } + } + } + else + { + len = 0xff; //ȴ + } + if(len == 0xff) + { + SetupReq = 0xFF; + UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_STALL | UEP_T_RES_STALL;//STALL + } + else if(len <= 8) //ϴݻ״̬׶η0Ȱ + { + UEP0_T_LEN = len; + UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK;//ĬݰDATA1ӦACK + } + else + { + UEP0_T_LEN = 0; //Ȼδ״̬׶ΣǰԤϴ0ݰԷǰ״̬׶ + UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK;//ĬݰDATA1,ӦACK + } + break; + case UIS_TOKEN_IN | 0: //endpoint0 IN + switch(SetupReq) + { + case USB_GET_DESCRIPTOR: + len = SetupLen >= 8 ? 8 : SetupLen; //δ䳤 + memcpy( Ep0Buffer, pDescr, len ); //ϴ + SetupLen -= len; + pDescr += len; + UEP0_T_LEN = len; + UEP0_CTRL ^= bUEP_T_TOG; //ͬ־λת + 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; //״̬׶жϻǿϴ0ݰƴ + UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK; + break; + } + break; + case UIS_TOKEN_OUT | 0: // endpoint0 OUT + len = USB_RX_LEN; + if(SetupReq == 0x09) + { + if(Ep0Buffer[0]) + { + printf("Light on Num Lock LED!\n"); + } + else if(Ep0Buffer[0] == 0) + { + printf("Light off Num Lock LED!\n"); + } + } + UEP0_T_LEN = 0; //Ȼδ״̬׶ΣǰԤϴ0ݰԷǰ״̬׶ + UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_ACK;//ĬݰDATA0,ӦACK + break; + default: + break; + } + UIF_TRANSFER = 0; //д0ж + } + if(UIF_BUS_RST) //豸ģʽUSB߸λж + { + 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; //жϱ־ + } + if (UIF_SUSPEND) //USB߹/ + { + UIF_SUSPEND = 0; + if ( USB_MIS_ST & bUMS_SUSPEND ) // + { +#if DEBUG + printf( "zz" ); //˯״̬ +#endif + while ( XBUS_AUX & bUART0_TX ); //ȴ + SAFE_MOD = 0x55; + SAFE_MOD = 0xAA; + WAKE_CTRL = bWAK_BY_USB | bWAK_RXD0_LO; //USBRXD0źʱɱ + PCON |= PD; //˯ + SAFE_MOD = 0x55; + SAFE_MOD = 0xAA; + WAKE_CTRL = 0x00; + } + } + else { //ж,ܷ + USB_INT_FG = 0xFF; //жϱ־ +// printf("UnknownInt N"); + } +} +void HIDValueHandle() +{ + UINT8 i; + i = getkey( ); + printf( "%c", (UINT8)i ); + switch(i) + { +//ϴʾ + case 'L': // + HIDMouse[0] = 0x01; + Enp2IntIn(); + HIDMouse[0] = 0; + break; + case 'R': //Ҽ + HIDMouse[0] = 0x02; + Enp2IntIn(); + HIDMouse[0] = 0; + break; +//ϴʾ + case 'A': //A + FLAG = 0; + HIDKey[2] = 0x04; //ʼ + Enp1IntIn(); + HIDKey[2] = 0; // + while(FLAG == 0) + { + ; /*ȴһ*/ + } + Enp1IntIn(); + break; + case 'P': //P + FLAG = 0; + HIDKey[2] = 0x13; + Enp1IntIn(); + HIDKey[2] = 0; // + while(FLAG == 0) + { + ; /*ȴһ*/ + } + Enp1IntIn(); + break; + case 'Q': //Num Lock + FLAG = 0; + HIDKey[2] = 0x53; + Enp1IntIn(); + HIDKey[2] = 0; // + while(FLAG == 0) + { + ; /*ȴһ*/ + } + Enp1IntIn(); + break; + default: // + UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; //ĬӦNAK + UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; //ĬӦNAK + break; + } +} + +main() +{ + CfgFsys( ); //CH559ʱѡ + mDelaymS(5); //޸Ƶȴڲȶ,ؼ + mInitSTDIO( ); //0ʼ +#ifdef DE_PRINTF + printf("start ...\n"); +#endif + USBDeviceInit(); //USB豸ģʽʼ + EA = 1; //Ƭж + UEP1_T_LEN = 0; //Ԥʹ÷ͳһҪ + UEP2_T_LEN = 0; //Ԥʹ÷ͳһҪ + FLAG = 0; + Ready = 0; + while(1) + { + if(Ready) + { + HIDValueHandle(); + } + if(Ready&&(Ep2InKey == 0)){ +#ifdef DE_PRINTF //ȡоƬID + printf("ID0 = %02x %02x \n",(UINT16)*(PUINT8C)(0x3FFA),(UINT16)*(PUINT8C)(0x3FFB)); + printf("ID1 = %02x %02x \n",(UINT16)*(PUINT8C)(0x3FFC),(UINT16)*(PUINT8C)(0x3FFD)); + printf("ID2 = %02x %02x \n",(UINT16)*(PUINT8C)(0x3FFE),(UINT16)*(PUINT8C)(0x3FFF)); +#endif + mDelaymS( 100 ); + } + mDelaymS( 100 ); //ģⵥƬ + } +} diff --git a/examples/USB/Device/VendorDefinedDev.C b/examples/USB/Device/VendorDefinedDev.C new file mode 100644 index 0000000..8fbf683 --- /dev/null +++ b/examples/USB/Device/VendorDefinedDev.C @@ -0,0 +1,335 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : VendorDefinedDev.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : CH554ģUSB Module(CH554),Զӿ豸Ҫװ +CH37XDRV߰װISPToolԶװ豸豸˿ƴ⣬ֱ˵2´Ͷ˵1 +жϴͨ372DEBUG.EXEȡUSBԹ߽շʾ + +*******************************************************************************/ + +#include "./Public/CH554.H" +#include "./Public/Debug.H" +#include +#include + +#define THIS_ENDP0_SIZE DEFAULT_ENDP0_SIZE + +// 豸 +UINT8C MyDevDescr[] = { 0x12, 0x01, 0x10, 0x01, + 0xFF, 0x80, 0x55, THIS_ENDP0_SIZE, + 0x48, 0x43, 0x37, 0x55, // IDͲƷID + 0x00, 0x01, 0x01, 0x02, + 0x00, 0x01 }; +// +UINT8C MyCfgDescr[] = { 0x09, 0x02, 0x27, 0x00, 0x01, 0x01, 0x00, 0x80, 0x32, + 0x09, 0x04, 0x00, 0x00, 0x03, 0xFF, 0x80, 0x55, 0x00, + 0x07, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, + 0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, + 0x07, 0x05, 0x81, 0x03, 0x40, 0x00, 0x00 }; +// +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 }; + +UINT8 UsbConfig = 0; // USBñ־ + +UINT8X Ep0Buffer[THIS_ENDP0_SIZE+2 >= MAX_PACKET_SIZE ? MAX_PACKET_SIZE : THIS_ENDP0_SIZE+2]; // OUT&IN +UINT8X Ep1Buffer[MAX_PACKET_SIZE]; // IN +UINT8X Ep2Buffer[2*MAX_PACKET_SIZE]; // OUT+IN + +#define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer) + +#pragma NOAREGS + +void USB_DeviceInterrupt( void ) interrupt INT_NO_USB using 1 /* USBжϷ,ʹüĴ1 */ +{ + UINT8 i, len; + static UINT8 SetupReqCode, SetupLen; + static PUINT8 pDescr; + if ( UIF_TRANSFER ) { // USB + if ( U_IS_NAK ) { // not enable for this example +// switch ( USB_INT_ST & ( MASK_UIS_TOKEN | MASK_UIS_ENDP ) ) { // ƺͶ˵ +// case UIS_TOKEN_OUT | 2: // endpoint 2# ˵´ +// break; +// case UIS_TOKEN_IN | 2: // endpoint 2# ˵ϴ +// break; +// case UIS_TOKEN_IN | 1: // endpoint 1# ж϶˵ϴ +// break; +// default: +// break; +// } + } + else { + switch ( USB_INT_ST & ( MASK_UIS_TOKEN | MASK_UIS_ENDP ) ) { // ƺͶ˵ + case UIS_TOKEN_OUT | 2: // endpoint 2# ˵´ + if ( U_TOG_OK ) { // ͬݰ +// UEP2_CTRL ^= bUEP_R_TOG; // Զת + len = USB_RX_LEN; + for ( i = 0; i < len; i ++ ) { + Ep2Buffer[MAX_PACKET_SIZE+i] = Ep2Buffer[i] ^ 0xFF; // OUTȡINɼ֤ + } + UEP2_T_LEN = len; + UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK;// ϴ + } + break; + case UIS_TOKEN_IN | 2: // endpoint 2# ˵ϴ +// UEP2_CTRL ^= bUEP_T_TOG; // Զת + UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; // ͣϴ + break; + case UIS_TOKEN_IN | 1: // endpoint 1# ж϶˵ϴ +// UEP1_CTRL ^= bUEP_T_TOG; // Զת + UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; // ͣϴ + break; + case UIS_TOKEN_SETUP | 0: // endpoint 0# SETUP + len = USB_RX_LEN; + if ( len == sizeof( USB_SETUP_REQ ) ) { // SETUP + SetupLen = UsbSetupBuf->wLengthL; + if ( UsbSetupBuf->wLengthH || SetupLen > 0x7F ) SetupLen = 0x7F;// ܳ + len = 0; // ĬΪɹϴ0 + SetupReqCode = UsbSetupBuf->bRequest; + if ( ( UsbSetupBuf->bRequestType & USB_REQ_TYP_MASK ) != USB_REQ_TYP_STANDARD ) { /* DZ׼ */ + len = 0xFF; + } + else { // ׼ + switch( SetupReqCode ) { // + case USB_GET_DESCRIPTOR: + switch( UsbSetupBuf->wValueH ) { + case 1: // 豸 + pDescr = (PUINT8)( &MyDevDescr[0] ); + len = sizeof( MyDevDescr ); + break; + case 2: // + pDescr = (PUINT8)( &MyCfgDescr[0] ); + len = sizeof( MyCfgDescr ); + 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; + default: + len = 0xFF; // ֵ֧ + break; + } + if ( SetupLen > len ) SetupLen = len; // ܳ + len = SetupLen >= THIS_ENDP0_SIZE ? THIS_ENDP0_SIZE : SetupLen; // δ䳤 + memcpy( Ep0Buffer, pDescr, len ); /* ϴ */ + SetupLen -= len; + pDescr += len; + break; + case USB_SET_ADDRESS: + SetupLen = UsbSetupBuf->wValueL; // ݴUSB豸ַ + break; + case USB_GET_CONFIGURATION: + Ep0Buffer[0] = UsbConfig; + if ( SetupLen >= 1 ) len = 1; + break; + case USB_SET_CONFIGURATION: + UsbConfig = UsbSetupBuf->wValueL; + break; + case USB_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 0x02: + UEP2_CTRL = UEP2_CTRL & ~ ( bUEP_R_TOG | MASK_UEP_R_RES ) | UEP_R_RES_ACK; + 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; // ֵ֧Ķ˵ + break; + } + } + else len = 0xFF; // Ƕ˵㲻֧ + break; + case USB_GET_INTERFACE: + Ep0Buffer[0] = 0x00; + if ( SetupLen >= 1 ) len = 1; + break; + case USB_GET_STATUS: + Ep0Buffer[0] = 0x00; + Ep0Buffer[1] = 0x00; + if ( SetupLen >= 2 ) len = 2; + else len = SetupLen; + break; + default: + len = 0xFF; // ʧ +#ifdef DE_PRINTF + printf("ErrEp0ReqCode=%02X\n",(UINT16)SetupReqCode); +#endif + break; + } + } + } + else { + len = 0xFF; // SETUPȴ +#ifdef DE_PRINTF + printf("ErrEp0ReqSize\n"); +#endif + } + if ( len == 0xFF ) { // ʧ + SetupReqCode = 0xFF; + UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_STALL | UEP_T_RES_STALL; // STALL + } + else if ( len <= THIS_ENDP0_SIZE ) { // ϴݻ״̬׶η0Ȱ + UEP0_T_LEN = len; + UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK; // ĬݰDATA1 + } + else { // ´ݻ + UEP0_T_LEN = 0; // Ȼδ״̬׶ΣǰԤϴ0ݰԷǰ״̬׶ + UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK; // ĬݰDATA1 + } + break; + case UIS_TOKEN_IN | 0: // endpoint 0# IN + switch( SetupReqCode ) { + case USB_GET_DESCRIPTOR: + len = SetupLen >= THIS_ENDP0_SIZE ? THIS_ENDP0_SIZE : SetupLen; // δ䳤 + memcpy( Ep0Buffer, pDescr, len ); /* ϴ */ + SetupLen -= len; + pDescr += len; + UEP0_T_LEN = len; + UEP0_CTRL ^= bUEP_T_TOG; // ת + 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; // ״̬׶жϻǿϴ0ݰƴ + UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK; + break; + } + break; + case UIS_TOKEN_OUT | 0: // endpoint 0# OUT + switch( SetupReqCode ) { +// case download: +// if ( U_TOG_OK ) { // ͬݰ +// UEP0_CTRL ^= bUEP_R_TOG; // ת +// //ȡ´; +// //UEP0_CTRL = UEP0_CTRL & bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK; // Ԥϴ0ݰDATA1Էǰ״̬׶ +// } +// break; + case USB_GET_DESCRIPTOR: + default: + if ( U_TOG_OK ) { // ͬݰ +// if ( USB_RX_LEN ) control_status_error; +// else control_ok; // յ0Ȱʾƶ/ϴOK + } +// else control_status_error; + UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK; // ׼һƴ + break; + } + break; + default: + break; + } + } + UIF_TRANSFER = 0; // жϱ־ + } + else if ( UIF_BUS_RST ) { // USB߸λ + 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; // жϱ־ + } + else if ( UIF_SUSPEND ) { // USB߹/ + UIF_SUSPEND = 0; + if ( USB_MIS_ST & bUMS_SUSPEND ) { // +#ifdef DE_PRINTF + printf( "zz" ); // ˯״̬ +#endif + while ( XBUS_AUX & bUART0_TX ); // ȴ + SAFE_MOD = 0x55; + SAFE_MOD = 0xAA; + WAKE_CTRL = bWAK_BY_USB | bWAK_RXD0_LO; // USBRXD0źʱɱ + PCON |= PD; // ˯ + SAFE_MOD = 0x55; + SAFE_MOD = 0xAA; + WAKE_CTRL = 0x00; + } + else { // + } + } + else { // ж,ܷ + + USB_INT_FG = 0xFF; // жϱ־ + } +} + +/******************************************************************************* +* Function Name : InitUSB_Device() +* Description : USB豸ģʽ,豸ģʽշ˵ãжϿ +* Input : None +* Output : None +* Return : None +*******************************************************************************/ +void InitUSB_Device( void ) // ʼUSB豸 +{ + IE_USB = 0; + USB_CTRL = 0x00; // 趨ģʽ + UEP4_1_MOD = bUEP1_TX_EN; // ˵1ϴIN + UEP2_3_MOD = bUEP2_RX_EN | bUEP2_TX_EN; // ˵2´OUTϴIN + UEP0_DMA = Ep0Buffer; + UEP1_DMA = Ep1Buffer; + UEP2_DMA = Ep2Buffer; + 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; + UDEV_CTRL = bUD_PD_DIS; // ֹDP/DM + USB_CTRL = bUC_DEV_PU_EN | bUC_INT_BUSY | bUC_DMA_EN; // USB豸DMAжڼжϱ־δǰԶNAK + UDEV_CTRL |= bUD_PORT_EN; // USB˿ + USB_INT_FG = 0xFF; // жϱ־ + USB_INT_EN = bUIE_SUSPEND | bUIE_TRANSFER | bUIE_BUS_RST; + IE_USB = 1; +} + +main( ) { + UINT8 i; + CfgFsys(); + mDelaymS(5); //޸ƵԼʱȴƵȶ + mInitSTDIO( ); /* ʼ0Ϊüͨڼʾ */ +#ifdef DE_PRINTF + printf( "Start @ChipID=%02X\n", (UINT16)CHIP_ID ); +#endif + InitUSB_Device( ); + EA = 1; + while ( 1 ) { + i = getkey( ); + printf( "%c", (UINT8)i ); + if ( i >= '0' && i <= 'z' ) { + memcpy( Ep1Buffer, (PUINT8C)(i-'0'), MAX_PACKET_SIZE ); /* ϴ */ + UEP1_T_LEN = i-'0' > 8 ? 8 : i-'0'; + UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; + } + } +} diff --git a/examples/USB/Host/USBHostHUB_KM.C b/examples/USB/Host/USBHostHUB_KM.C new file mode 100644 index 0000000..89b6d29 --- /dev/null +++ b/examples/USB/Host/USBHostHUB_KM.C @@ -0,0 +1,256 @@ + +/********************************** (C) COPYRIGHT ******************************* +* File Name : USBHostHUB_KM.C +* Author : WCH +* Version : V1.0 +* Date : 2017/01/20 +* Description : + USB host example for CH554, start USB device under DP/DM and HP/HM port + USBӦ,ʼöUSB˿ӵ豸,ͬʱ֧1USB豸,֧һⲿHUB, + ԲUSBHUB,HID + ҪỤοU_DISKļµ + ּ֧USBӡûдUSBӡ +*******************************************************************************/ + +#include "..\..\Public\CH554.H" +#include "..\..\Public\Debug.H" +#include "..\USB_LIB\USBHOST.H" +#include +#include + +#pragma NOAREGS + +/*ȡ豸*/ +UINT8C SetupGetDevDescr[] = { USB_REQ_TYP_IN, USB_GET_DESCRIPTOR, 0x00, USB_DESCR_TYP_DEVICE, 0x00, 0x00, sizeof( USB_DEV_DESCR ), 0x00 }; + +/*ȡ*/ +UINT8C SetupGetCfgDescr[] = { USB_REQ_TYP_IN, USB_GET_DESCRIPTOR, 0x00, USB_DESCR_TYP_CONFIG, 0x00, 0x00, 0x04, 0x00 }; + +/*USBַ*/ +UINT8C SetupSetUsbAddr[] = { USB_REQ_TYP_OUT, USB_SET_ADDRESS, USB_DEVICE_ADDR, 0x00, 0x00, 0x00, 0x00, 0x00 }; + +/*USB*/ +UINT8C SetupSetUsbConfig[] = { USB_REQ_TYP_OUT, USB_SET_CONFIGURATION, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + +/*USBӿ*/ +UINT8C SetupSetUsbInterface[] = { USB_REQ_RECIP_INTERF, USB_SET_INTERFACE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + +/*˵STALL*/ +UINT8C SetupClrEndpStall[] = { USB_REQ_TYP_OUT | USB_REQ_RECIP_ENDP, USB_CLEAR_FEATURE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + +/*ȡHUB*/ +UINT8C SetupGetHubDescr[] = { HUB_GET_HUB_DESCRIPTOR, HUB_GET_DESCRIPTOR, 0x00, USB_DESCR_TYP_HUB, 0x00, 0x00, sizeof( USB_HUB_DESCR ), 0x00 }; + +/*ȡHID豸*/ +UINT8C SetupGetHIDDevReport[] = { 0x81, USB_GET_DESCRIPTOR, 0x00, USB_DESCR_TYP_REPORT, 0x00, 0x00, 0x41, 0x00 }; + +/*ӡ*/ +UINT8C XPrinterReport[] = { 0xA1, 0, 0x00, 0, 0x00, 0x00, 0xF1, 0x03 }; + +UINT8X UsbDevEndp0Size; //* USB豸Ķ˵0ߴ */ +UINT8X RxBuffer[ MAX_PACKET_SIZE ] _at_ 0x0000 ; // IN, must even address +UINT8X TxBuffer[ MAX_PACKET_SIZE ] _at_ 0x0040 ; // OUT, must even address + +UINT8 Set_Port = 0; + +_RootHubDev xdata ThisUsbDev; //ROOT +_DevOnHubPort xdata DevOnHubPort[HUB_MAX_PORTS]; // ٶ:1ⲿHUB,ÿⲿHUBHUB_MAX_PORTS˿(˲) + +bit RootHubId; // ǰڲroot-hub˿ں:0=HUB0,1=HUB1 +bit FoundNewDev; + +main( ) +{ + UINT8 i, s, len, endp; + UINT16 loc; + CfgFsys( ); + mDelaymS(5); //޸Ƶȴʱȶ + mInitSTDIO( ); //Ϊüͨڼʾ + printf( "Start @ChipID=%02X\n", (UINT16)CHIP_ID ); + InitUSB_Host( ); + FoundNewDev = 0; + printf( "Wait Device In\n" ); + while ( 1 ) + { + s = ERR_SUCCESS; + if ( UIF_DETECT ){ // USBж + UIF_DETECT = 0; // жϱ־ + s = AnalyzeRootHub( ); // ROOT-HUB״̬ + if ( s == ERR_USB_CONNECT ) FoundNewDev = 1; + } + if ( FoundNewDev ){ // µUSB豸 + FoundNewDev = 0; + mDelaymS( 200 ); // USB豸ղδȶ,ʵȴUSB豸ٺ,ζ + s = EnumAllRootDevice( ); // öROOT-HUB˿ڵUSB豸 + if ( s != ERR_SUCCESS ){ + printf( "EnumAllRootDev err = %02X\n", (UINT16)s ); + } + } + if ( RI == 0 ) continue; + i = getkey( ); + printf( "%c", (UINT8)i ); + if ( i == 'E'){ // ÿһʱ,ͨ100mS1000mS,ⲿHUBĶ˿ڽһö,Ƭпʱ + s = EnumAllHubPort( ); // öROOT-HUB˿ⲿHUBĶUSB豸 + if ( s != ERR_SUCCESS ){ // HUBϿ + printf( "EnumAllHubPort err = %02X\n", (UINT16)s ); + } + } + switch( i ){ // ģ,ijUSB豸в + case 'M': // öʱģ,Ҫ + loc = SearchTypeDevice( DEV_TYPE_MOUSE ); // ROOT-HUBԼⲿHUB˿ָ͵豸ڵĶ˿ں + if ( loc != 0xFFFF ){ // ҵ,MOUSEδ? + printf( "Query Mouse @%04X\n", loc ); + i = (UINT8)( loc >> 8 ); + len = (UINT8)loc; + SelectHubPort( len ); // ѡָROOT-HUB˿,õǰUSBٶԼ豸USBַ + endp = len ? DevOnHubPort[len-1].GpVar : ThisUsbDev.GpVar; // ж϶˵ĵַ,λ7ͬ־λ + if ( endp & USB_ENDP_ADDR_MASK ){ // ˵Ч + s = USBHostTransact( USB_PID_IN << 4 | endp & 0x7F, endp & 0x80 ? bUH_R_TOG | bUH_T_TOG : 0, 0 );// CH554,ȡ,NAK + if ( s == ERR_SUCCESS ){ + endp ^= 0x80; // ͬ־ת + if ( len ) DevOnHubPort[len-1].GpVar = endp; // ͬ־λ + else ThisUsbDev.GpVar = endp; + len = USB_RX_LEN; // յݳ + if ( len ) { + printf("Mouse data: "); + for ( i = 0; i < len; i ++ ){ + printf("x%02X ",(UINT16)(RxBuffer[i]) ); + } + printf("\n"); + } + } + else if ( s != ( USB_PID_NAK | ERR_USB_TRANSFER ) ) { + printf("Mouse error %02x\n",(UINT16)s); // ǶϿ + } + } + else { + printf("Mouse no interrupt endpoint\n"); + } + SetUsbSpeed( 1 ); // ĬΪȫ + } + break; + case 'K': // öʱģ,Ҫ + loc = SearchTypeDevice( DEV_TYPE_KEYBOARD ); // ROOT-HUBԼⲿHUB˿ָ͵豸ڵĶ˿ں + if ( loc != 0xFFFF ){ // ҵ,KeyBoardδ? + printf( "Query Keyboard @%04X\n", loc ); + i = (UINT8)( loc >> 8 ); + len = (UINT8)loc; + SelectHubPort( len ); // ѡָROOT-HUB˿,õǰUSBٶԼ豸USBַ + endp = len ? DevOnHubPort[len-1].GpVar : ThisUsbDev.GpVar; // ж϶˵ĵַ,λ7ͬ־λ + printf("%02X ",endp); + if ( endp & USB_ENDP_ADDR_MASK ){ // ˵Ч + s = USBHostTransact( USB_PID_IN << 4 | endp & 0x7F, endp & 0x80 ? bUH_R_TOG | bUH_T_TOG : 0, 0 );// CH554,ȡ,NAK + if ( s == ERR_SUCCESS ){ + endp ^= 0x80; // ͬ־ת + if ( len ) DevOnHubPort[len-1].GpVar = endp; // ͬ־λ + else ThisUsbDev.GpVar = endp; + len = USB_RX_LEN; // յݳ + if ( len ){ + SETorOFFNumLock(RxBuffer); + printf("keyboard data: "); + for ( i = 0; i < len; i ++ ){ + printf("x%02X ",(UINT16)(RxBuffer[i]) ); + } + printf("\n"); + } + } + else if ( s != ( USB_PID_NAK | ERR_USB_TRANSFER ) ){ + printf("keyboard error %02x\n",(UINT16)s); // ǶϿ + } + } + else{ + printf("keyboard no interrupt endpoint\n"); + } + SetUsbSpeed( 1 ); // ĬΪȫ + } + break; + case 'H': //HUB + loc = SearchTypeDevice( DEV_TYPE_KEYBOARD ); // ROOT-HUBԼⲿHUB˿ָ͵豸ڵĶ˿ں + if ( loc != 0xFFFF ){ // ҵ,KeyBoardδ? + printf( "Query Keyboard @%04X\n", loc ); + i = (UINT8)( loc >> 8 ); + len = (UINT8)loc; + SelectHubPort( len ); // ѡָROOT-HUB˿,õǰUSBٶԼ豸USBַ + endp = len ? DevOnHubPort[len-1].GpVar : ThisUsbDev.GpVar; // ж϶˵ĵַ,λ7ͬ־λ + printf("%02X ",endp); + if ( endp & USB_ENDP_ADDR_MASK ){ // ˵Ч + s = USBHostTransact( USB_PID_IN << 4 | endp & 0x7F, endp & 0x80 ? bUH_R_TOG | bUH_T_TOG : 0, 0 );// CH554,ȡ,NAK + if ( s == ERR_SUCCESS ){ + endp ^= 0x80; // ͬ־ת + if ( len ) DevOnHubPort[len-1].GpVar = endp; // ͬ־λ + else ThisUsbDev.GpVar = endp; + len = USB_RX_LEN; // յݳ + if ( len ) { + //SETorOFFNumLock(RxBuffer); + printf("keyboard data: "); + for ( i = 0; i < len; i ++ ){ + printf("x%02X ",(UINT16)(RxBuffer[i]) ); + } + printf("\n"); + } + } + else if ( s != ( USB_PID_NAK | ERR_USB_TRANSFER ) ){ + printf("keyboard error %02x\n",(UINT16)s); // ǶϿ + } + } + else printf("keyboard no interrupt endpoint\n"); + } + for(i=0; i<2; i++) { + if((ThisUsbDev.DeviceStatus == ROOT_DEV_SUCCESS)&&(ThisUsbDev.DeviceType == USB_DEV_CLASS_HUB)){ + SelectHubPort( 0 ); // ѡָROOT-HUB˿,õǰUSBٶԼ豸USBַ + endp = ThisUsbDev.GpVar1; // ж϶˵ĵַ,λ7ͬ־λ + if ( endp & USB_ENDP_ADDR_MASK ){ // ˵Ч + s = USBHostTransact( USB_PID_IN << 4 | endp & 0x7F, endp & 0x80 ? bUH_R_TOG | bUH_T_TOG : 0, 0 );// CH554,ȡ,NAK + if ( s == ERR_SUCCESS ) { + endp ^= 0x80; // ͬ־ת + ThisUsbDev.GpVar1 = endp; // ͬ־λ + len = USB_RX_LEN; // յݳ + if ( len ){ + EnumHubPort(); + for ( i = 0; i < len; i ++ ){ + printf("x%02X ",(UINT16)(RxBuffer[i]) ); + } + printf("\n"); + } + } + else if ( s != ( USB_PID_NAK | ERR_USB_TRANSFER ) ) { + printf("HUB error %02x\n",(UINT16)s); // ǶϿ + } + } + else printf("HUB %02d no interrupt endpoint\n",i); + } + else printf("ROOTHUB %02d not HUB\n",i); + } + break; + case 'P': //USBӡ + if(TIN0 == 0){ //P10Ϊͣʼӡ + memset(TxBuffer,0,sizeof(TxBuffer)); + TxBuffer[0]=0x1B;TxBuffer[1]=0x40;TxBuffer[2]=0x1D;TxBuffer[3]=0x55;TxBuffer[4]=0x42;TxBuffer[5]=0x02;TxBuffer[6]=0x18;TxBuffer[7]=0x1D; + TxBuffer[8]=0x76;TxBuffer[9]=0x30;TxBuffer[10]=0x00;TxBuffer[11]=0x30;TxBuffer[12]=0x00;TxBuffer[13]=0x18; + loc = SearchTypeDevice( USB_DEV_CLASS_PRINTER ); // ROOT-HUBԼⲿHUB˿ָ͵豸ڵĶ˿ں + if ( loc != 0xFFFF ){ // ҵ,ӡδ? + printf( "Query Printer @%04X\n", loc ); + i = (UINT8)( loc >> 8 ); + len = (UINT8)loc; + SelectHubPort( len ); // ѡָROOT-HUB˿,õǰUSBٶԼ豸USBַ + endp = len ? DevOnHubPort[len-1].GpVar : ThisUsbDev.GpVar; // ˵ĵַ,λ7ͬ־λ + printf("%02X ",endp); + if ( endp & USB_ENDP_ADDR_MASK ){ // ˵Ч + UH_TX_LEN = 64; // Ĭݹ״̬׶ΪIN + s = USBHostTransact( USB_PID_OUT << 4 | endp & 0x7F, endp & 0x80 ? bUH_R_TOG | bUH_T_TOG : 0, 0xffff );// CH554,ȡ,NAK + if ( s == ERR_SUCCESS ){ + endp ^= 0x80; // ͬ־ת + memset(TxBuffer,0,sizeof(TxBuffer)); + UH_TX_LEN = 64; // Ĭݹ״̬׶ΪIN + s = USBHostTransact( USB_PID_OUT << 4 | endp & 0x7F, endp & 0x80 ? bUH_R_TOG | bUH_T_TOG : 0, 0xffff );// CH554,ȡ,NAK + } + else if ( s != ( USB_PID_NAK | ERR_USB_TRANSFER ) ) printf("Printer error %02x\n",(UINT16)s); // ǶϿ + } + } + } + break; + default: + break; + } + } +} diff --git a/.gitignore b/keil/.gitignore similarity index 100% rename from .gitignore rename to keil/.gitignore diff --git a/usr/CH554.h b/keil/usr/CH554.h similarity index 100% rename from usr/CH554.h rename to keil/usr/CH554.h diff --git a/usr/GPIO.c b/keil/usr/GPIO.c similarity index 100% rename from usr/GPIO.c rename to keil/usr/GPIO.c diff --git a/usr/GPIO.h b/keil/usr/GPIO.h similarity index 100% rename from usr/GPIO.h rename to keil/usr/GPIO.h diff --git a/usr/LCD.c b/keil/usr/LCD.c similarity index 100% rename from usr/LCD.c rename to keil/usr/LCD.c diff --git a/usr/LCD.h b/keil/usr/LCD.h similarity index 100% rename from usr/LCD.h rename to keil/usr/LCD.h diff --git a/usr/STARTUP.A51 b/keil/usr/STARTUP.A51 similarity index 100% rename from usr/STARTUP.A51 rename to keil/usr/STARTUP.A51 diff --git a/usr/Timer.c b/keil/usr/Timer.c similarity index 100% rename from usr/Timer.c rename to keil/usr/Timer.c diff --git a/usr/Timer.h b/keil/usr/Timer.h similarity index 100% rename from usr/Timer.h rename to keil/usr/Timer.h diff --git a/usr/USB.c b/keil/usr/USB.c similarity index 100% rename from usr/USB.c rename to keil/usr/USB.c diff --git a/usr/USB.h b/keil/usr/USB.h similarity index 100% rename from usr/USB.h rename to keil/usr/USB.h diff --git a/usr/common.c b/keil/usr/common.c similarity index 100% rename from usr/common.c rename to keil/usr/common.c diff --git a/usr/common.h b/keil/usr/common.h similarity index 100% rename from usr/common.h rename to keil/usr/common.h diff --git a/usr/font.c b/keil/usr/font.c similarity index 100% rename from usr/font.c rename to keil/usr/font.c diff --git a/usr/font.h b/keil/usr/font.h similarity index 100% rename from usr/font.h rename to keil/usr/font.h diff --git a/usr/main.c b/keil/usr/main.c similarity index 100% rename from usr/main.c rename to keil/usr/main.c