SY8835_For_Demo_Ourself/UsrSrc/uart/uart.c

549 lines
10 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
******************************************************************************
*
* @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<Length;i++)
{
while(busy0);
// ACC = *TransBuf++;
busy0 = 1;
S0BUF = *TransBuf++;
}
S0CON |= UART_REC_ENABLE; //Enabel UART0's RX
}
#endif
#define Vox_Comm_Data_Len 7
/*uart0中断处理函数*/
void uart0_Interrupt(void) interrupt Interrupt_Vector_RI_TI
{
static uint8_t Rx0Status;
static uint16_t RX0_Cnt;
static uint16_t RX0DataLen;
/*处理UART0发送中断*/
if(TI0)
{
TI0 = 0;
busy0 = 0;
}
/*处理UART0接收中断*/
if(RI0)
{
RI0 = 0;
#if SLEEP_ENABLE
Enter_Sleep_Cnt_Restart_Flag = 1;
#endif
#ifdef VOX_RX
switch(Rx0Status)
{
case 0:
if( S0BUF == (BOXHEADER >> 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<Length;i++)
{
while(busy1);
//ACC = *TransBuf++;
busy1 = 1;
S1BUF = *TransBuf++;
}
S1CON |= UART_REC_ENABLE; //Enabel UART0's RX
}
/*
*******************************************************************************
* void Uart1SendString(char *s)
*
* Description : UART1 Send String
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void Uart1SendString(char *s)
{
while(*s) //检测字符串结束标志
{
Uart1SendData(*s++); //发送当前字符
}
}
/*uart1中断处理函数*/
void uart1_Interrupt(void) interrupt Interrupt_Vector_RI1_TI1
{
static uint8_t Rx1Status;
static uint16_t RX1_Cnt;
static uint16_t RX1DataLen;
/*处理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
switch(Rx1Status)
{
case 0:
if(S1BUF == 0xAA)
{
Rx1Status = 1;
RX1_Cnt = 0;
RX1_Buffer[RX1_Cnt] = S1BUF;
RX1_Cnt++;
}
else
{
Rx1Status = 0;
RX1_Cnt = 0;
}
break;
case 1:
RX1_Buffer[RX1_Cnt] = S1BUF;
RX1_Cnt++;
if( RX1_Cnt == Vox_Comm_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 > 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<Length;i++)
{
Uart2SendData(*TransBuf++);
}
}
/*
*******************************************************************************
* void Uart2SendString(char *s)
*
* Description : UART2 Send String
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void Uart2SendString(char *s)
{
while(*s) //检测字符串结束标志
{
Uart2SendData(*s++); //发送当前字符
}
}
#endif
/*uart2中断处理函数*/
void uart2_Interrupt(void) interrupt Interrupt_Vector_IE8
{
/*处理UART1发送中断*/
if(S2CON & 0x02) //TI2 = 1
{
S2CON &= ~0x02; //TI2 = 0
busy2 = 0;
}
/*处理UART1接收中断*/
if(S2CON & 0x01) //RI2 = 1
{
S2CON &= ~0x01;
}
}
#endif