Both_Way_Comm_SY8833/TP3310_Demo.si4project/Backup/uart(4598).c

331 lines
7.1 KiB
C
Raw Permalink 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
*
*
* @version 1.0
* @date 2022/12/14 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 2022/12/14 Alex build this file
******************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "uart.h"
#include "system.h"
#include "vox_module.h"
#if UART0_ENABLE
/******************************************************************************\
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 //定义校验位
#define Baud_Rate0 115200//19200//38400//93750
#define S0REL_VALUE (1024-375000/Baud_Rate0)
/******************************************************************************\
Variables definitions
\******************************************************************************/
bit busy0;
#if 0
uint8_t RX0_Buffer[COM0_Data_Lenth] = 0; //接收缓冲
uint8_t Rx0Status = 0;
uint8_t RX0_Cnt = 0;
uint8_t RX0DataLen = 0;
#endif
#endif
#ifdef TEST_MODE
uint8_t gFun_Select_Type = 0; //功能选择位8个bit表示8个子功能的使能配置位1Enable0Disable。bit7:188数码管显示bit6透传功能bit5休眠唤醒功能bit4PWM功能
//bit3VOX双向通信uart模式bit2Vox双向通信Reg模式RX功能使能bit1边充边放功能
#endif
#ifdef _DEBUG_ALL
char putchar(char c)
{
/*
while(!TI0);
TI0 = 0;
return (S0BUF = c);
*/
Uart0SendData(c);
return(c);
}
#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;
SFRDATA &= ~0x0F;
SFRDATA |= 0x05; //P01 as UART's TX ;P00 as UART's RX
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 = 0xD0; //SCON: MODE 3, 9-bit UART, RI enable, EVEN Parity
TI0 = 1;
RI0 = 1;
ES0 = 1; //Enable UART0 IRQ
}
#endif
#ifdef _DEBUG_ALL
/*
*******************************************************************************
* void Uart0SendData(uint8_t Txdata)
*
* Description : UART0 Send Data
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void Uart0SendData(uint8_t Txdata)
{
while(busy0);
ACC = Txdata;
if (P) //根据P来设置校验位
{
#if (PARITYBIT == ODD_PARITY)
TB80 = 0; //设置校验位为0
#elif (PARITYBIT == EVEN_PARITY)
TB80 = 1; //设置校验位为1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
TB80 = 1; //设置校验位为1
#elif (PARITYBIT == EVEN_PARITY)
TB80 = 0; //设置校验位为0
#endif
}
busy0 = 1;
S0BUF = ACC;
}
#endif
#if 0
/*
*******************************************************************************
* void Uart0SendPacket(uint8_t Length,uint8_t *TransBuf)
*
* Description : UART0 Send Packet
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void Uart0SendPacket(uint8_t Length,uint8_t *TransBuf)
{
uint8_t i = 0;
for(i=0;i<Length;i++)
{
Uart0SendData(*TransBuf++);
}
}
/*
*******************************************************************************
* void Uart0SendString(char *s)
*
* Description : UART0 Send String
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void Uart0SendString(char *s)
{
while(*s) //检测字符串结束标志
{
Uart0SendData(*s++); //发送当前字符
}
}
#endif
#ifdef TEST_MODE
/*字符串转换为16进制
ASCII中的0-9对应<E5AFB9>?0x30-0x39
ASCII中的A-F对应<E5AFB9>?0x41-0x46
ASCII中的a-f对应<E5AFB9>?0x61-0x66
判断这个字符是否大于0x39
如果不是,直接取这个字符的低四位当作结果
如果是则为字母将他的低四位加<EFBFBD>?9即为<E58DB3>?<3F>?结果
*/
void from_hex(char *s, int l, char *d)
{
while(l--)
{
(*d++) = ( (*s > '9' ? (*s++)+9 : (*s++)) << 4 )
| ( (*s > '9' ? (*s++)+9 : (*s++)) & 0x0F );
}
}
/*
*******************************************************************************
* void Uart0_RX_Handle(uint8_t *RxBuff,uint8_t Rx_Len)
*
* Description : UART1接收处理函数。<E695B0>?<3F>过解析字符串处理相关参数<E58F82>??
*
*
* Arguments : uint8_t *RxBuff,uint8_t Rx_Len
* Returns :
*
* Notes :
*
*******************************************************************************
*/
void Uart0_RX_Handle(uint8_t *RxBuff)
{
char *Token = NULL;
const char delim[5] = "\r\n";
char PMU_Addr = 0;
char RX_data = 0;
/* 获取第一个子字符<E5AD97>? */
Token = strtok((char *)RxBuff, delim); //通过字符<E5AD97>?"\r\n"分解uart结束到的数据
/* 继续获取其他的子字符<E5AD97>? */
while( Token != NULL )
{
// printf( "* %s *\r\n", Token );
if(strncmp(Token,"Config Set:",strlen("Config Set:")) == 0)
{
from_hex(&Token[strlen("Config Set:0x")],2,&RX_data); //获取字符串中的Config命令RX_data的8个bit位表示8个子功能的配置位。
if(RX_data & 0x80) //bit7:188数码管显示使能动态显示几个数字
{
gFun_Select_Type |= 0x80;
}
if(RX_data & 0x40) //bit6:透传功能使能
{
gFun_Select_Type |= 0x40;
}
if(RX_data & 0x20) //bit5:休眠唤醒功能使能
{
gFun_Select_Type |= 0x20;
}
if(RX_data & 0x10) //bit4:PWM功能使能
{
gFun_Select_Type |= 0x10;
}
if(RX_data & 0x08) //bit3:VOX 双向通信(Uart)功能使能
{
gFun_Select_Type |= 0x08;
}
if(RX_data & 0x04) //bit2:VOX 双向通信Reg模式RX功能使能
{
gFun_Select_Type |= 0x04;
}
if(RX_data & 0x02) //bit1:边充边放功能使能
{
gFun_Select_Type |= 0x02;
}
if(RX_data & 0x01) //
{
gFun_Select_Type |= 0x01;
}
}
Token = strtok(NULL, delim);
}
}
#endif
#if UART0_ENABLE
/*uart0中断处理函数*/
void uart0_Interrupt(void) interrupt Interrupt_Vector_RI_TI
{
uint16_t CrcCheckSum = 0;
/*处理UART0发送中断*/
if(TI0)
{
TI0 = 0;
busy0 = 0;
}
/*处理UART0接收中断透传开关功能协议监听。*/
if(RI0)
{
RI0 = 0;
}
}
#endif