/* ****************************************************************************** * * @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 UART0_ENABLE idata uint8_t RX0_Buffer[COM0_Data_Lenth] = 0; //接收缓冲 bit Uart0_RX_Finish_Flag = 0; #endif #if UART1_ENABLE idata uint8_t RX1_Buffer[COM0_Data_Lenth] = 0; //接收缓冲 bit Uart1_RX_Finish_Flag = 0; #endif #endif #if UART0_ENABLE /* ******************************************************************************* * void UART0_Init(void) * * Description : UART0 Initialization UART0用于debug、打印、透传功能 * * Arguments : NONE * Returns : NONE * Notes : NONE * ******************************************************************************* */ void UART0_Init(void) { #if 1 /* vor、vol都使用uart0进行分时通讯。 */ SFRADDR = P0_OE; //Set P02 And P03 As Output. SFRDATA |= 0x0C; SFRADDR = P0_IE; //Set P02 And P03 As Input. SFRDATA |= 0x0C; // SFRADDR = P0_PU; //Set P02 And P03 30K Pullup. // SFRDATA |= 0x0C; SFRADDR = MFP_CTL0; //VOR、VOL都使用UART0进行分时通讯。 SFRDATA &=~ 0x0F; SFRDATA |= 0x09; #else /* vor、vol使用不同uart进行通讯 */ SFRADDR = P0_OE; //Set P02 As Output. SFRDATA |= 0x04; SFRADDR = P0_IE; //Set P02 As Input. SFRDATA |= 0x04; SFRADDR = MFP_CTL0; //VOR使用UART0进行通讯。 SFRDATA &=~ 0x03; SFRDATA |= 0x01; #endif 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 * ******************************************************************************* */ #ifdef VOX_TX void Uart0SendPacket(uint16_t Length,uint8_t *TransBuf) { uint8_t i = 0; S0CON &=~ UART_REC_ENABLE; //Disable UART0's RX for(i=0;i> 8) ) { Rx0Status = 1; RX0_Cnt = 0; RX0_Buffer[RX0_Cnt] = S0BUF; RX0_Cnt++; } else { Rx0Status = 0; RX0_Cnt = 0; } break; case 1: if(S0BUF == BOXHEADER) { Rx0Status = 2; RX0_Buffer[RX0_Cnt] = S0BUF; RX0_Cnt++; } else { Rx0Status = 0; RX0_Cnt = 0; } break; case 2: RX0_Buffer[RX0_Cnt] = S0BUF; RX0_Cnt++; if( RX0_Cnt == Vox_Comm_Data_Len ) { RX0DataLen = RX0_Buffer[2] + 4; //获取数据长度 } if( (RX0DataLen == RX0_Cnt) || (RX0_Cnt > COM0_Data_Lenth) ) //当接收到的data个数等于RX0DataLen,这结束本次传输。 { RX0_Cnt = 0; Rx0Status = 0; Uart0_RX_Finish_Flag = 1; } } #endif } } #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) { SFRADDR = P0_OE; //Set P03 As Output. SFRDATA |= 0x08; SFRADDR = P0_IE; //Set P03 As Input. SFRDATA |= 0x08; SFRADDR = MFP_CTL0; //VOL使用UART1进行通讯。 SFRDATA &=~ 0x0C; SFRDATA |= 0x04; //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; S1CON &=~ UART_REC_ENABLE; //Disable UART0's RX for(i=0;i COM0_Data_Lenth) ) //当接收到的data个数等于RX0DataLen,这结束本次传输。 { RX1_Cnt = 0; Rx1Status = 0; Uart1_RX_Finish_Flag = 1; } } #endif } } #endif #if UART2_ENABLE bit busy2 = 0; #ifdef _DEBUG_ALL char putchar(char c) { #if 0 while(!TI0); TI0 = 0; return (S0BUF = c); #else Uart2SendData(c); return(c); #endif } #endif /* ******************************************************************************* * void UART2_Init(void) * * Description : UART2 Initialization UART1用于VOX通讯功能 * * Arguments : NONE * Returns : NONE * Notes : NONE * ******************************************************************************* */ void UART2_Init(void) { S2IOCFG = 0x0A; //baudrate set S2RELH = HIBYTE(S2REL_VALUE); S2RELL = LOBYTE(S2REL_VALUE);//s1rel=1023, baudrate = fclk/32 = 187.5k S2CON = 0x92; //S1CON: 8-bit UART1,REN1 、 RI1 、TI1 enable IEN2 = 0x02; //Enable UART2 IRQ } /* ******************************************************************************* * void Uart2SendData(uint8_t Txdata) * * Description : UART2 Send Data * * Arguments : NONE * Returns : NONE * Notes : NONE * ******************************************************************************* */ void Uart2SendData(uint8_t Txdata) { while(busy2); busy2 = 1; S2BUF = Txdata; } #if 0 /* ******************************************************************************* * void Uart2SendPacket(uint16_t Length,uint8_t *TransBuf) * * Description : UART2 Send Packet * * Arguments : NONE * Returns : NONE * Notes : NONE * ******************************************************************************* */ void Uart2SendPacket(uint16_t Length,uint8_t *TransBuf) { uint16_t i = 0; for(i=0;i