429 lines
7.8 KiB
C
429 lines
7.8 KiB
C
/*
|
||
******************************************************************************
|
||
*
|
||
* @file uart.c
|
||
* @brief uart module
|
||
* @ic sy8835
|
||
*
|
||
* @version 1.0
|
||
* @date 2024/11/01 15:00:40
|
||
* @author Alex Xu
|
||
*
|
||
* Copyright (c) 2013-2099,Tkplusemi Technology Co.,Ltd.
|
||
* All Rights Reserved
|
||
*
|
||
* History:
|
||
* Revision Date Author Desc
|
||
* 1.0.0 2024/11/01 Alex build this file
|
||
******************************************************************************/
|
||
/*_____ I N C L U D E S ____________________________________________________*/
|
||
#include "uart.h"
|
||
#include "system.h"
|
||
#include "vox_module.h"
|
||
#include "sleep.h"
|
||
|
||
/******************************************************************************\
|
||
Macro definitions
|
||
\******************************************************************************/
|
||
#define NONE_PARITY 0 //ÎÞУÑé
|
||
#define ODD_PARITY 1 //ÆæÐ£Ñé
|
||
#define EVEN_PARITY 2 //żУÑé
|
||
#define MARK_PARITY 3 //±ê¼ÇУÑé
|
||
#define SPACE_PARITY 4 //¿Õ°×УÑé
|
||
|
||
#define PARITYBIT EVEN_PARITY //¶¨ÒåУÑéλ
|
||
|
||
/******************************************************************************\
|
||
Variables definitions
|
||
\******************************************************************************/
|
||
|
||
bit busy0;
|
||
|
||
#ifdef VOX_RX
|
||
|
||
#if 0
|
||
|
||
idata uint8_t RX0_Buffer[COM0_Data_Lenth] = 0; //½ÓÊÕ»º³å
|
||
|
||
bit Uart0_RX_Finish_Flag = 0;
|
||
|
||
#endif
|
||
|
||
#if UART1_ENABLE
|
||
|
||
idata uint8_t RX1_Buffer[COM1_Data_Lenth] = 0; //½ÓÊÕ»º³å
|
||
|
||
bit Uart1_RX_Finish_Flag = 0;
|
||
|
||
#endif
|
||
|
||
#endif
|
||
|
||
#ifdef _DEBUG_ALL
|
||
|
||
char putchar(char c)
|
||
{
|
||
#if 0
|
||
|
||
while(!TI0);
|
||
TI0 = 0;
|
||
return (S0BUF = c);
|
||
|
||
#else
|
||
|
||
Uart0SendData(c);
|
||
|
||
return(c);
|
||
|
||
#endif
|
||
|
||
}
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void Uart1SendData(uint8_t Txdata)
|
||
*
|
||
* Description : UART1 Send Data
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
|
||
void Uart0SendData(uint8_t Txdata)
|
||
{
|
||
while(busy0);
|
||
|
||
busy0 = 1;
|
||
|
||
// SFRADDR = MFP_CTL0; //Set P01 As TX.
|
||
// SFRDATA &=~ 0x0C;
|
||
// SFRDATA |= 0x04;
|
||
|
||
S0BUF = Txdata;
|
||
|
||
// SFRADDR = MFP_CTL0; //Set P01 As RX.
|
||
// SFRDATA &=~ 0x0C;
|
||
// SFRDATA |= 0x0C;
|
||
}
|
||
|
||
#endif
|
||
|
||
#if UART0_ENABLE
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void UART0_Init(void)
|
||
*
|
||
* Description : UART0 Initialization UART0ÓÃÓÚdebug¡¢´òÓ¡¡¢Í¸´«¹¦ÄÜ
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
|
||
void UART0_Init(void)
|
||
{
|
||
|
||
SFRADDR = MFP_CTL0; //Set P01 As TX.
|
||
SFRDATA &=~ 0x0C;
|
||
SFRDATA |= 0x04;
|
||
|
||
BD = 1; //Select additional Baudrate generator
|
||
PCON |= 0x80; //Baudrate double enable
|
||
|
||
//baudrate set
|
||
S0RELH = HIBYTE(S0REL_VALUE);
|
||
S0RELL = LOBYTE(S0REL_VALUE); //s0rel=1023, baudrate = fclk/32 = 187.5k
|
||
|
||
S0CON = 0x52; //SCON: MODE 1, 8-bit UART, RI enable
|
||
|
||
ES0 = 1; //Enable UART0 IRQ
|
||
}
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void Uart0SendPacket(uint8_t Length,uint8_t *TransBuf)
|
||
*
|
||
* Description : UART0 Send Packet
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
#if 0
|
||
|
||
void Uart0SendPacket(uint16_t Length,uint8_t *TransBuf)
|
||
{
|
||
uint8_t i = 0;
|
||
|
||
for(i=0;i<Length;i++)
|
||
{
|
||
while(busy0);
|
||
busy0 = 1;
|
||
S0BUF = *TransBuf++;
|
||
}
|
||
}
|
||
#endif
|
||
|
||
/*uart0Öжϴ¦Àíº¯Êý*/
|
||
void uart0_Interrupt(void) interrupt Interrupt_Vector_RI_TI
|
||
{
|
||
|
||
/*´¦ÀíUART0·¢ËÍÖжÏ*/
|
||
if(TI0)
|
||
{
|
||
TI0 = 0;
|
||
busy0 = 0;
|
||
}
|
||
|
||
/*´¦ÀíUART0½ÓÊÕÖжÏ*/
|
||
if(RI0)
|
||
{
|
||
RI0 = 0;
|
||
}
|
||
}
|
||
|
||
#endif
|
||
|
||
#if UART1_ENABLE
|
||
|
||
bit busy1 = 0;
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void UART1_Init(void)
|
||
*
|
||
* Description : UART1 Initialization UART1ÓÃÓÚVOXͨѶ¹¦ÄÜ
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
void UART1_Init(void)
|
||
{
|
||
//baudrate set
|
||
S1RELH = HIBYTE(S1REL_VALUE);
|
||
S1RELL = LOBYTE(S1REL_VALUE);//s1rel=1023, baudrate = fclk/32 = 187.5k
|
||
|
||
S1CON = 0x92; //S1CON: 8-bit UART1,REN1 ¡¢ RI1 ¡¢TI1 enable
|
||
|
||
IEN2 |= 1; //Enable UART1 IRQ
|
||
}
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void Uart1SendPacket(uint16_t Length,uint8_t *TransBuf)
|
||
*
|
||
* Description : UART1 Send Packet
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
void Uart1SendPacket(uint16_t Length,uint8_t *TransBuf)
|
||
{
|
||
uint8_t i = 0;
|
||
|
||
for(i=0;i<Length;i++)
|
||
{
|
||
while(busy1);
|
||
|
||
busy1 = 1;
|
||
|
||
S1BUF = *TransBuf++;
|
||
}
|
||
}
|
||
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void Uart1SendString(char *s)
|
||
*
|
||
* Description : UART1 Send String
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
#if 0
|
||
void Uart1SendString(char *s)
|
||
{
|
||
while(*s) //¼ì²â×Ö·û´®½áÊø±êÖ¾
|
||
{
|
||
Uart1SendData(*s++); //·¢Ë͵±Ç°×Ö·û
|
||
}
|
||
}
|
||
#endif
|
||
|
||
uint8_t Rx1Status = 0;
|
||
|
||
uint8_t RX1_Cnt = 0;
|
||
|
||
#if XUANHENG
|
||
|
||
uint16_t RX1DataLen = 0;
|
||
|
||
#endif
|
||
|
||
#define Vox_Comm_JL_HEADER_DATA_LEN 4
|
||
|
||
#define Vox_Comm_ZK_HEADER_DATA_LEN 6
|
||
|
||
#define Vox_Comm_XH_HEADER_DATA_LEN 7
|
||
|
||
/*uart1Öжϴ¦Àíº¯Êý*/
|
||
void uart1_Interrupt(void) interrupt Interrupt_Vector_RI1_TI1
|
||
{
|
||
|
||
/*´¦ÀíUART1·¢ËÍÖжÏ*/
|
||
if(S1CON & 0x02) //TI1 = 1
|
||
{
|
||
S1CON &= ~0x02; //TI1 = 0
|
||
|
||
busy1 = 0;
|
||
}
|
||
|
||
/*´¦ÀíUART1½ÓÊÕÖжÏ*/
|
||
if(S1CON & 0x01) //RI1 = 1
|
||
{
|
||
S1CON &= ~0x01;
|
||
|
||
#if SLEEP_ENABLE
|
||
Enter_Sleep_Cnt_Restart_Flag = 1;
|
||
#endif
|
||
|
||
#ifdef VOX_RX
|
||
|
||
#if XUANHENG
|
||
|
||
switch(Rx1Status)
|
||
{
|
||
case 0:
|
||
|
||
if( S1BUF == BOXHEADER )
|
||
{
|
||
Rx1Status = 1;
|
||
|
||
RX1_Cnt = 0;
|
||
|
||
RX1_Buffer[RX1_Cnt++] = S1BUF;
|
||
}
|
||
else
|
||
{
|
||
Rx1Status = 0;
|
||
|
||
RX1_Cnt = 0;
|
||
}
|
||
break;
|
||
|
||
case 1:
|
||
|
||
RX1_Buffer[RX1_Cnt++] = S1BUF;
|
||
|
||
if( RX1_Cnt == Vox_Comm_XH_HEADER_DATA_LEN )
|
||
{
|
||
RX1DataLen = RX1_Buffer[5] | (RX1_Buffer[6] << 8) + 9; //»ñÈ¡Êý¾Ý³¤¶È£¬Headr(1Byte) + Trandsmit Dir(1Byte) + CMD(2Bytes) + datalen(2Bytes) + CRC16(2Bytes)
|
||
}
|
||
|
||
if( (RX1DataLen == RX1_Cnt) || (RX1_Cnt > COM1_Data_Lenth) ) //µ±½ÓÊÕµ½µÄdata¸öÊýµÈÓÚRX0DataLen£¬Õâ½áÊø±¾´Î´«Êä¡£
|
||
{
|
||
RX1_Cnt = 0;
|
||
|
||
Rx1Status = 0;
|
||
|
||
Uart1_RX_Finish_Flag = 1;
|
||
}
|
||
}
|
||
|
||
#else
|
||
|
||
switch(Rx1Status)
|
||
{
|
||
case 0:
|
||
if( S1BUF == EARPHONE_HEADER_HIGH )
|
||
{
|
||
Rx1Status = 1;
|
||
|
||
RX1_Cnt = 0;
|
||
|
||
RX1_Buffer[RX1_Cnt++] = S1BUF;
|
||
}
|
||
else
|
||
{
|
||
Rx1Status = 0;
|
||
|
||
RX1_Cnt = 0;
|
||
}
|
||
break;
|
||
|
||
case 1:
|
||
if( S1BUF == EARPHONE_HEADER_LOW )
|
||
{
|
||
Rx1Status = 2;
|
||
|
||
RX1_Buffer[RX1_Cnt++] = S1BUF;
|
||
}
|
||
else
|
||
{
|
||
Rx1Status = 0;
|
||
|
||
RX1_Cnt = 0;
|
||
}
|
||
break;
|
||
|
||
case 2:
|
||
{
|
||
RX1_Buffer[RX1_Cnt++] = S1BUF;
|
||
|
||
#if JIELI
|
||
if ( ( RX1_Cnt >= ( RX1_Buffer[2] + Vox_Comm_JL_HEADER_DATA_LEN ) ) || ( RX1_Cnt > COM1_Data_Lenth ) )
|
||
#elif ZHONGKE
|
||
if ( ( RX1_Cnt >= ( RX1_Buffer[4] + Vox_Comm_ZK_HEADER_DATA_LEN ) ) || ( RX1_Cnt > COM1_Data_Lenth ) )
|
||
#endif
|
||
{
|
||
RX1_Cnt = 0;
|
||
|
||
Rx1Status = 0;
|
||
|
||
Uart1_RX_Finish_Flag = 1;
|
||
}
|
||
}
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
#endif
|
||
|
||
#endif
|
||
|
||
}
|
||
}
|
||
#endif
|
||
|