271 lines
9.7 KiB
C++
271 lines
9.7 KiB
C++
|
/********************************** (C) COPYRIGHT *******************************
|
|||
|
* File Name : DEBUG.C
|
|||
|
* Author : WCH
|
|||
|
* Version : V1.0
|
|||
|
* Date : 2017/01/20
|
|||
|
* Description : CH554 DEBUG Interface
|
|||
|
CH554<EFBFBD><EFBFBD>Ƶ<EFBFBD>ġ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD>Ӻ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|||
|
*******************************************************************************/
|
|||
|
|
|||
|
#include "CH554.H"
|
|||
|
#include "DEBUG.H"
|
|||
|
|
|||
|
/*******************************************************************************
|
|||
|
* Function Name : CfgFsys( )
|
|||
|
* Description : CH554ʱ<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD>,Ĭ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>Fsys 6MHz<EFBFBD><EFBFBD>FREQ_SYS<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>
|
|||
|
CLOCK_CFG<EFBFBD><EFBFBD><EFBFBD>õõ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
|
|||
|
Fsys = (Fosc * 4/(CLOCK_CFG & MASK_SYS_CK_SEL);<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* Input : None
|
|||
|
* Output : None
|
|||
|
* Return : None
|
|||
|
*******************************************************************************/
|
|||
|
void CfgFsys( )
|
|||
|
{
|
|||
|
// SAFE_MOD = 0x55;
|
|||
|
// SAFE_MOD = 0xAA;
|
|||
|
// CLOCK_CFG |= bOSC_EN_XT; //ʹ<><CAB9><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD>
|
|||
|
// CLOCK_CFG &= ~bOSC_EN_INT; //<2F>ر<EFBFBD><D8B1>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* Input : UNIT16 n
|
|||
|
* Output : None
|
|||
|
* Return : None
|
|||
|
*******************************************************************************/
|
|||
|
void mDelayuS( UINT16 n ) // <20><>uSΪ<53><CEAA>λ<EFBFBD><CEBB>ʱ
|
|||
|
{
|
|||
|
#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<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* Input : UNIT16 n
|
|||
|
* Output : None
|
|||
|
* Return : None
|
|||
|
*******************************************************************************/
|
|||
|
void mDelaymS( UINT16 n ) // <20><>mSΪ<53><CEAA>λ<EFBFBD><CEBB>ʱ
|
|||
|
{
|
|||
|
while ( n ) {
|
|||
|
#ifdef DELAY_MS_HW
|
|||
|
while ( ( TKEY_CTRL & bTKC_IF ) == 0 );
|
|||
|
while ( TKEY_CTRL & bTKC_IF );
|
|||
|
#else
|
|||
|
mDelayuS( 1000 );
|
|||
|
#endif
|
|||
|
-- n;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/*******************************************************************************
|
|||
|
* Function Name : CH554UART0Alter()
|
|||
|
* Description : CH554<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>䵽P0.2<EFBFBD><EFBFBD>P0.3
|
|||
|
* Input : None
|
|||
|
* Output : None
|
|||
|
* Return : None
|
|||
|
*******************************************************************************/
|
|||
|
void CH554UART0Alter()
|
|||
|
{
|
|||
|
PIN_FUNC |= bUART0_PIN_X; //<2F><><EFBFBD><EFBFBD>ӳ<EFBFBD>䵽P1.2<EFBFBD><EFBFBD>P1.3
|
|||
|
}
|
|||
|
|
|||
|
/*******************************************************************************
|
|||
|
* Function Name : mInitSTDIO()
|
|||
|
* Description : CH554<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>,Ĭ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>T1<EFBFBD><EFBFBD>UART0<EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>T2
|
|||
|
<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* Input : None
|
|||
|
* Output : None
|
|||
|
* Return : None
|
|||
|
*******************************************************************************/
|
|||
|
void mInitSTDIO( )
|
|||
|
{
|
|||
|
UINT32 x;
|
|||
|
UINT8 x2;
|
|||
|
|
|||
|
SM0 = 0;
|
|||
|
SM1 = 1;
|
|||
|
SM2 = 0; //<2F><><EFBFBD><EFBFBD>0ʹ<30><CAB9>ģʽ1
|
|||
|
//ʹ<><CAB9>Timer1<72><31>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ʷ<EFBFBD><CAB7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
RCLK = 0; //UART0<54><30><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
TCLK = 0; //UART0<54><30><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
PCON |= SMOD;
|
|||
|
x = 10 * FREQ_SYS / UART0_BUAD / 16; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5>ע<EFBFBD><D7A2>x<EFBFBD><78>ֵ<EFBFBD><D6B5>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
|
|||
|
x2 = x % 10;
|
|||
|
x /= 10;
|
|||
|
if ( x2 >= 5 ) x ++; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
TMOD = TMOD & ~ bT1_GATE & ~ bT1_CT & ~ MASK_T1_MOD | bT1_M1; //0X20<32><30>Timer1<72><31>Ϊ8λ<38>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ض<EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
T2MOD = T2MOD | bTMR_CLK | bT1_CLK; //Timer1ʱ<31><CAB1>ѡ<EFBFBD><D1A1>
|
|||
|
TH1 = 0-x; //12MHz<48><7A><EFBFBD><EFBFBD>,buad/12Ϊʵ<CEAA><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
TR1 = 1; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>1
|
|||
|
TI = 1;
|
|||
|
REN = 1; //<2F><><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>
|
|||
|
}
|
|||
|
|
|||
|
/*******************************************************************************
|
|||
|
* Function Name : CH554UART0RcvByte()
|
|||
|
* Description : CH554UART0<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
|||
|
* Input : None
|
|||
|
* Output : None
|
|||
|
* Return : SBUF
|
|||
|
*******************************************************************************/
|
|||
|
UINT8 CH554UART0RcvByte( )
|
|||
|
{
|
|||
|
while(RI == 0); //<2F><>ѯ<EFBFBD><D1AF><EFBFBD>գ<EFBFBD><D5A3>жϷ<D0B6>ʽ<EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD>
|
|||
|
RI = 0;
|
|||
|
return SBUF;
|
|||
|
}
|
|||
|
|
|||
|
/*******************************************************************************
|
|||
|
* Function Name : CH554UART0SendByte(UINT8 SendDat)
|
|||
|
* Description : CH554UART0<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
|||
|
* Input : UINT8 SendDat<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* Output : None
|
|||
|
* Return : None
|
|||
|
*******************************************************************************/
|
|||
|
void CH554UART0SendByte(UINT8 SendDat)
|
|||
|
{
|
|||
|
SBUF = SendDat; //<2F><>ѯ<EFBFBD><D1AF><EFBFBD>ͣ<EFBFBD><CDA3>жϷ<D0B6>ʽ<EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>TI=0
|
|||
|
while(TI ==0);
|
|||
|
TI = 0;
|
|||
|
}
|
|||
|
|
|||
|
/*******************************************************************************
|
|||
|
* Function Name : UART1Setup()
|
|||
|
* Description : CH554<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|||
|
* Input : None
|
|||
|
* Output : None
|
|||
|
* Return : None
|
|||
|
*******************************************************************************/
|
|||
|
void UART1Setup( )
|
|||
|
{
|
|||
|
U1SM0 = 0; //UART1ѡ<31><D1A1>8λ<38><CEBB><EFBFBD><EFBFBD>λ
|
|||
|
U1SMOD = 1; //<2F><><EFBFBD><EFBFBD>ģʽ
|
|||
|
U1REN = 1; //ʹ<>ܽ<EFBFBD><DCBD><EFBFBD>
|
|||
|
SBAUD1 = 0 - FREQ_SYS/16/UART1_BUAD;
|
|||
|
}
|
|||
|
|
|||
|
/*******************************************************************************
|
|||
|
* Function Name : CH554UART1RcvByte()
|
|||
|
* Description : CH554UART1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
|||
|
* Input : None
|
|||
|
* Output : None
|
|||
|
* Return : SBUF
|
|||
|
*******************************************************************************/
|
|||
|
UINT8 CH554UART1RcvByte( )
|
|||
|
{
|
|||
|
while(U1RI == 0); //<2F><>ѯ<EFBFBD><D1AF><EFBFBD>գ<EFBFBD><D5A3>жϷ<D0B6>ʽ<EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD>
|
|||
|
U1RI = 0;
|
|||
|
return SBUF1;
|
|||
|
}
|
|||
|
|
|||
|
/*******************************************************************************
|
|||
|
* Function Name : CH554UART1SendByte(UINT8 SendDat)
|
|||
|
* Description : CH554UART1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
|||
|
* Input : UINT8 SendDat<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* Output : None
|
|||
|
* Return : None
|
|||
|
*******************************************************************************/
|
|||
|
void CH554UART1SendByte(UINT8 SendDat)
|
|||
|
{
|
|||
|
SBUF1 = SendDat; //<2F><>ѯ<EFBFBD><D1AF><EFBFBD>ͣ<EFBFBD><CDA3>жϷ<D0B6>ʽ<EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>TI=0
|
|||
|
while(U1TI ==0);
|
|||
|
U1TI = 1;
|
|||
|
}
|
|||
|
|
|||
|
/*******************************************************************************
|
|||
|
* Function Name : CH554WDTModeSelect(UINT8 mode)
|
|||
|
* Description : CH554<EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD>ģʽѡ<EFBFBD><EFBFBD>
|
|||
|
* Input : UINT8 mode
|
|||
|
0 timer
|
|||
|
1 watchDog
|
|||
|
* Output : None
|
|||
|
* Return : None
|
|||
|
*******************************************************************************/
|
|||
|
void CH554WDTModeSelect(UINT8 mode)
|
|||
|
{
|
|||
|
SAFE_MOD = 0x55;
|
|||
|
SAFE_MOD = 0xaa; //<2F><><EFBFBD>밲ȫģʽ
|
|||
|
if(mode){
|
|||
|
GLOBAL_CFG |= bWDOG_EN; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD><C5B9><EFBFBD>λ
|
|||
|
}
|
|||
|
else GLOBAL_CFG &= ~bWDOG_EN; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD><C5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʱ<EFBFBD><CAB1>
|
|||
|
SAFE_MOD = 0x00; //<2F>˳<EFBFBD><CBB3><EFBFBD>ȫģʽ
|
|||
|
WDOG_COUNT = 0; //<2F><><EFBFBD>Ź<EFBFBD><C5B9><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|||
|
}
|
|||
|
|
|||
|
/*******************************************************************************
|
|||
|
* Function Name : CH554WDTFeed(UINT8 tim)
|
|||
|
* Description : CH554<EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD><EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* Input : UINT8 tim <EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD><EFBFBD><EFBFBD>λʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
00H(6MHz)=2.8s
|
|||
|
80H(6MHz)=1.4s
|
|||
|
* Output : None
|
|||
|
* Return : None
|
|||
|
*******************************************************************************/
|
|||
|
void CH554WDTFeed(UINT8 tim)
|
|||
|
{
|
|||
|
WDOG_COUNT = tim; //<2F><><EFBFBD>Ź<EFBFBD><C5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|||
|
}
|