/* ****************************************************************************** * * @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" #include "led.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 uint8_t RX0_Buffer[COM0_Data_Lenth]; //接收缓冲 bit Uart0_RX_Finish_Flag; #endif #if UART1_ENABLE uint8_t RX1_Buffer[COM0_Data_Lenth]; //接收缓冲 bit Uart1_RX_Finish_Flag; #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; #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 TI0 = 1; RI0 = 1; 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; SFRADDR = P0_OE; //Set P02 And P03 As Output. SFRDATA |= 0x0C; SFRADDR = P0_IE; //Set P02 And P03 As Input. SFRDATA &= ~0x0C; for(i=0; i COM0_Data_Lenth) ) //当接收到的data个数等于RX0DataLen,这结束本次传输。 { RX0_Cnt = 0; Rx0Status = 0; Uart0_RX_Finish_Flag = TRUE; } } #else switch(Rx0Status) { case 0: if( S0BUF == EARPHONE_HEADER_HIGH ) { Rx0Status = 1; RX0_Cnt = 0; RX0_Buffer[RX0_Cnt++] = S0BUF; } else { Rx0Status = 0; RX0_Cnt = 0; } break; case 1: if( S0BUF == EARPHONE_HEADER_LOW ) { Rx0Status = 2; RX0_Buffer[RX0_Cnt++] = S0BUF; } else { Rx0Status = 0; RX0_Cnt = 0; } break; case 2: { RX0_Buffer[RX0_Cnt++] = S0BUF; #if JIELI if( ( RX0_Cnt >= ( RX0_Buffer[2] + Vox_Comm_JL_HEADER_DATA_LEN ) ) || ( RX0_Cnt > COM0_Data_Lenth ) ) #elif ZHONGKE if ( ( RX0_Cnt >= ( RX0_Buffer[4] + Vox_Comm_ZK_HEADER_DATA_LEN ) ) || ( RX0_Cnt > COM0_Data_Lenth ) ) #endif { RX0_Cnt = 0; Rx0Status = 0; Uart0_RX_Finish_Flag = TRUE; } } break; default: break; } #endif #endif } } #endif #endif #if UART1_ENABLE bit busy1; /* ******************************************************************************* * 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