185 lines
4.0 KiB
C
185 lines
4.0 KiB
C
/*
|
||
******************************************************************************
|
||
*
|
||
* @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 2400//115200//19200//38400//93750
|
||
#define S0REL_VALUE (1024-375000/Baud_Rate0)
|
||
/******************************************************************************\
|
||
Variables definitions
|
||
\******************************************************************************/
|
||
|
||
bit busy0;
|
||
|
||
#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 &= ~0x0C;
|
||
SFRDATA |= 0x04; //P01 as UART's TX
|
||
|
||
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
|
||
}
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* 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;
|
||
}
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* 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++);
|
||
}
|
||
}
|
||
|
||
#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
|
||
|
||
|