SY883x_For_Clients_ROCK-A/UsrSrc/uart/uart.c

429 lines
7.6 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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"
#include "sleep.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 //¶¨ÒåУÑéλ
/******************************************************************************\
Variables definitions
\******************************************************************************/
bit busy0;
idata uint8_t RX0_Buffer[COM0_Data_Lenth] = 0; //½ÓÊÕ»º³å
bit Uart0_RX_Finish_Flag = 0;
#endif
#ifdef _DEBUG_ALL
char putchar(char c)
{
while(!TI0);
TI0 = 0;
return (S0BUF = c);
}
#endif
#if UART0_ENABLE
/*
*******************************************************************************
* void Uart0SendPacket(uint8_t Length,uint8_t *TransBuf)
*
* Description : UART0 Send Packet
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
#ifdef VOX_TX
void Uart0SendPacket(uint8_t Length,uint8_t *TransBuf)
{
uint8_t i = 0;
for(i=0;i<Length;i++)
{
while(busy0);
ACC = *TransBuf++;
busy0 = 1;
S0BUF = ACC;
}
}
#endif
#endif
#if UART0_ENABLE
#ifdef VOX_TX
/*
*******************************************************************************
* uint8_t Crc8Maxim(u8 *buf, u8 length)
*
* Description : УÑéºÍµÄ¼ÆËã
*
*
* Arguments : u8 *buf: Êý¾Ý´æ·ÅµØÖ·
u8 length: Êý¾Ý³¤¶È, ÒÔbyteΪµ¥Î»
* Returns :
*
* Notes :
*
*******************************************************************************
*/
#if 1
uint32_t crc32Maxim(u8 *buf,uint8_t length)
{
uint32_t crc = 0xffffffff;
uint8_t i = 0,j = 0,byte = 0;;
while (length--)
{
byte = buf[i];
crc = crc ^ byte;
for ( j = 8; j > 0; --j )
{
crc = (crc >> 1) ^ (0xEDB88320 & (-(crc & 1)));
}
i++;
}
return crc ^ 0xffffffff;
}
#endif
/*
*******************************************************************************
* void HandleTxCommand(VOX_BES_COMMAND_E RxCommand, e_Vox_Chan nVox_Chn_Select)
*
* Description : ·¢ËÍÊý¾Ý°üµÄ·â×°£¬²¢´ò¿ªÏàÓ¦µÄË«ÏòͨÐÅͨµÀ
* Command payload
* Header checksum side cmd len Data
* 1bytes 1byte 1byte 1byte 1byte Len bytes
*
* Arguments : VOX_BES_COMMAND_E TxCommand: , e_Vox_Chan nVox_Chn_Select
* Returns :
*
* Notes :
*
*******************************************************************************
*/
void HandleTxCommand(VOX_FAC_COMMAND_E TxCommand)
{
uint8_t tx_pData[11] = 0;
uint8_t i = 0;
/*Êý¾Ý°üÍ·*/
tx_pData[0] = BOX_FAC_HEADER;
/*Ö¸Áî±àºÅ*/
tx_pData[2] = TxCommand;
switch(TxCommand)
{
case CMD_FAC_START:
break;
case CMD_FAC_HALL:
break;
case CMD_FAC_GUAGE:
break;
case CMD_FAC_LED:
break;
case CMD_FAC_SHUTDOWN:
break;
case CMD_FAC_GET_DAUGE:
break;
case CMD_FAC_GET_NTC:
break;
case CMD_FAC_GET_FWVER:
break;
case CMD_FAC_SET_VOX_5V:
break;
default:
break;
}
SFRADDR = MFP_CTL0;
SFRDATA |= 0x04; //P01 as UART's TX
#if UART0_ENABLE
Uart0SendPacket(tx_pData[4] + 6, tx_pData); //+6µÄÔ­Òò£º¶à¼ÓÒ»¸ö×Ö½Ú·¢ËÍ£¬ºóÃæÒªÉèÖÃTXΪGPIO£¬»áµ¼ÖÂ×îºóÒ»¸ö×Ö½ÚûÀ´µÃ¼°·¢³öÈ¥¡£
#endif
SFRADDR = MFP_CTL0; //Set P01 Pinmux As GPIO Function,Ô­Òò£ºTXºÍRXÁ¬ÔÚÒ»Æð£¬Òª±£Ö¤RXÄÜÕý³£½ÓÊÕµ½Êý¾Ý£¬TXÐèÉèÖÃΪGPIOģʽ£¬²¢¸¡¿Õ»òÊäÈëģʽ¡£
SFRDATA &= ~0x0C;
}
#endif
#ifdef VOX_RX
/*
*******************************************************************************
* void HandleRxCommand(ACK_TYPE_E RxCommand)
*
* Description : ¶ú»ú¶Ë·¢Ë͵ÄÊý¾Ý°üµÄ½âÎö¼°´¦Àí
* Ack payload:
* Header checksum side cmd len Data
* 1bytes 1byte 1byte 1byte 1byte Len bytes
*
* Arguments : ACK_TYPE_E RxCommand:
* Returns :
*
* Notes :
*
*******************************************************************************
*/
void HandleRxMsg(VOX_FAC_COMMAND_E RxCommand)
{
uint8_t i = 0;
switch(RxCommand)
{
case CMD_FAC_START:
break;
case CMD_FAC_HALL:
break;
case CMD_FAC_GUAGE:
break;
case CMD_FAC_LED:
break;
case CMD_FAC_SHUTDOWN:
break;
case CMD_FAC_GET_DAUGE:
break;
case CMD_FAC_GET_NTC:
break;
case CMD_FAC_GET_FWVER:
break;
case CMD_FAC_SET_VOX_5V:
break;
default:
break;
}
}
/*
*******************************************************************************
* void HandleVoxCommMsg(void)
*
* Description : Vox ͨѶ¹¦ÄÜ´¦Àíº¯Êý£¬100ms¶¨Ê±µ÷Óá£ÓÉÓںͶú»úͨÐÅǰÐèÒª´ò¿ª¶ú»ú¶ËµÄTRX¹¦ÄÜ£¬²¢ÇÒÿ´ÎͨÐÅÖ»ÄÜÒ»Ö»¶ú»ú¡£Í¨¹ýÇл»VOL/VORͨÐÅ¿ª¹Ø½øÐÐͨÐÅ
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void HandleVoxCommMsg(void)
{
uint8_t CrcCheckSum = 0;
uint8_t i = 0;
/*RXÊý¾Ý´¦Àí*/
if( Uart0_RX_Finish_Flag )
{
Uart0_RX_Finish_Flag = 0;
if( CrcCheckSum == RX0_Buffer[1] )
{
HandleRxMsg((VOX_FAC_COMMAND_E)RX0_Buffer[2]);
}
else
{
return;
}
}
}
#endif
#define Vox_Comm_Data_Len 5
#define Vox_R_W_FLAG 4
/*uart0Öжϴ¦Àíº¯Êý*/
void uart0_Interrupt(void) interrupt Interrupt_Vector_RI_TI
{
static uint8_t Rx0Status;
static uint8_t RX0_Cnt;
static uint8_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 == 0x04 )
{
Rx0Status = 1;
RX0_Buffer[0] = S0BUF;
}
else
{
Rx0Status = 0;
RX0_Cnt = 0;
}
break;
case 1:
if( S0BUF == 0xFB )
{
Rx0Status = 2;
RX0_Cnt = 1;
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_Buffer[3] == 1) || (RX0_Buffer[4] == 0) ) //¶Á²Ù×÷»òдÊý¾Ý³¤¶ÈΪ0£¬Ôò²»´ø4×Ö½ÚCRCУÑéºÍ
{
RX0_Cnt = 0;
Rx0Status = 0;
Uart0_RX_Finish_Flag = 1;
break;
}
if( RX0_Cnt == Vox_Comm_Data_Len )
{
RX0DataLen = RX0_Buffer[4] + 9; //»ñÈ¡Êý¾Ý³¤¶È:Header(2Bytes) + CMD(1Bytes) + W Flag(1Bytes) + Datalen(1Bytes) + Data + CRC(4Bytes)
}
if( (RX0DataLen == RX0_Cnt) || (RX0_Cnt > COM0_Data_Lenth) ) //µ±½ÓÊÕµ½µÄdata¸öÊýµÈÓÚRX0DataLen£¬Õâ½áÊø±¾´Î´«Êä¡£
{
RX0_Cnt = 0;
Rx0Status = 0;
Uart0_RX_Finish_Flag = 1;
}
}
#endif
}
}
#endif