📄 lcodu_msg.c
字号:
//usart.c for lc ODU
//*----------------------------------------------------------------------------
//* File Name : lcodu_msg.c
//* Object : handle the idu to odu communication
//* Creation : wp 22/10/2007
//* Modif :
//*----------------------------------------------------------------------------
#include <stdio.h>
#include "usart.h"
//#include "AT91SAM7S128.h"
#include "board.h"
//#include "crc.h"
#include "vars.h"
#include <stdlib.h>
#include "hardctrl.h"
#include "spi.h"
//#define AT91C_MASTER_CLOCK 48000000
extern void AT91F_disable_interrupt(void);
extern void AT91F_enable_interrupt(void);
extern void odu_data_init(void);
extern void Delay (unsigned int a);
extern void sw_reset(void);
unsigned char rx_buf[256]; //接收缓冲区
unsigned char tx_buf[256]; //发送缓冲区
unsigned char infor_msg[256];
//unsigned char send_msg[256];
//static unsigned char rx_buf1[256]; //另一用途接收缓冲区
//unsigned char tx_buf1[128]; //另一用途发送缓冲区
unsigned int hd_flag=0;//接收数据阶段标志
unsigned int rx_index,rx_len,rx_flag,rx_index1; //接收、发送数组下标及长度
unsigned int rx_rd_pt;//receiving buffer reading pointer
unsigned int tx_wr_pt;//sending buffer writing pointer
unsigned int rx1_rd_pt;//the other receiving buffer reading pointer
unsigned int tx1_wr_pt;//the other sending buffer writing pointer
unsigned int send_point;//the pointer of the first byte to be send (US0)
unsigned int send_point1;//the pointer of the first byte to be send(US1)
unsigned char escape_flag=0;//*escape_flag=1:the previous byte received is an escape byte
AT91PS_USART u_pUSART0 = AT91C_BASE_US0;
AT91PS_USART u_pUSART1 = AT91C_BASE_US1;
#define ACK_MSG_HEAD(comp,sub,len) {tx_buf[0]=SENDERID;tx_buf[1]=0x00;tx_buf[2]=comp;tx_buf[3]=sub;tx_buf[4]=len;}
#define ODU_MSG_HEAD(cmd,sub,len) {tx_buf[0]=SENDERID;tx_buf[1]=cmd; tx_buf[2]=0x00;tx_buf[3]=sub;tx_buf[4]=len;}
#define ODU_MSG_DATA(lenth,y) {ack=lenth; for(i=0;i<lenth;i++) tx_buf[i+5]=y[i];}
#define ODU_MSG_INT_DATA(x) {ltob.ul=x; ack=4; for(i=0;i<4;i++) tx_buf[i+5]=ltob.ch[3-i];}
//unsigned char send_triger; //for debug^^^^^^^^^^^^^^^^^for debug
//*-----------------------------------------------------------------------------
//* Function Name : send_msg_USART0
//* Object : send the newly loaded messages stored in us0_send_dma[].
//* send_point is to memory the pointer of the first byte
//* to be sended. tx_wr_pt is to memory the writing pointer
//* of the us0_send_dma[]
//* autor : wang ping
//* Input Parameters : none
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void send_msg_USART0(){
if(tx_wr_pt!=send_point) {
//Alive_counter=AT91C_BASE_RTTC->RTTC_RTVR;
//v_counter=0;
if(send_point<tx_wr_pt){
while(u_pUSART0->US_TCR!=0){};
u_pUSART0->US_TPR=send_point+(unsigned int)us0_send_dma;
u_pUSART0->US_TCR=tx_wr_pt-send_point;
send_point=tx_wr_pt;
//send_triger=1;
}
else
{
while(u_pUSART0->US_TCR!=0){};
u_pUSART0->US_TPR=send_point+(unsigned int)us0_send_dma;
u_pUSART0->US_TCR=MAX_SEND_BUF-send_point;
send_point=0;
//send_triger=1;
while(u_pUSART0->US_TCR!=0){};
u_pUSART0->US_TPR=(unsigned int)us0_send_dma;
u_pUSART0->US_TCR=tx_wr_pt;
send_point=tx_wr_pt;
}
}
}
//*-----------------------------------------------------------------------------
//* Function Name : send_msg_USART1
//* Object : send the newly loaded messages stored in us1_send_dma[].
//* send_point1 is to memory the pointer of the first byte
//* to be sended. tx1_wr_pt is to memory the writing pointer
//* of the us1_send_dma[]
//* autor : wang ping
//* Input Parameters : none
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void send_msg_USART1(){
if(tx1_wr_pt!=send_point1) {
//Alive_counter=AT91C_BASE_RTTC->RTTC_RTVR;
//v_counter=0;
if(send_point1<tx1_wr_pt){
while(u_pUSART1->US_TCR!=0){};
u_pUSART1->US_TPR=send_point1+(unsigned int)us1_send_dma;
u_pUSART1->US_TCR=tx1_wr_pt-send_point1;
send_point1=tx1_wr_pt;
//send_triger=1;
}
else
{
while(u_pUSART1->US_TCR!=0){};
u_pUSART1->US_TPR=send_point1+(unsigned int)us1_send_dma;
u_pUSART1->US_TCR=MAX_SEND_BUF-send_point1;
send_point1=0;
//send_triger=1;
while(u_pUSART1->US_TCR!=0){};
u_pUSART1->US_TPR=(unsigned int)us1_send_dma;
u_pUSART1->US_TCR=tx1_wr_pt;
send_point1=tx1_wr_pt;
}
}
}
/*将需要传输的信息打包,组成一个应用层信息帧*/
/*void msg_pack(unsigned char type,unsigned char id,unsigned char ack,unsigned char exinfor) //application layer
{
}*/
/*计算校验和*/
//*-----------------------------------------------------------------------------
//* Function Name : crc_fun
//* Object : calculate the checksum.
//* autor : wang ping
//* Input Parameters : msg[]:the bytes used to calculate; len: the length of
//* of the message.
//* Output Parameters : crc_result:the checksum
//*-----------------------------------------------------------------------------
unsigned char crc_fun(unsigned char msg[],unsigned int len)
{
int i;
unsigned char crc_result;
crc_result=0;
for(i=0;i<len;i++)
crc_result+=msg[i];
return crc_result;
}
/*检验所收到的信息帧的校验和*/
//*-----------------------------------------------------------------------------
//* Function Name : check_crc
//* Object : check the checksum of the received message.
//* autor : wang ping
//* Input Parameters : len: the length of the message.
//* Output Parameters : 1:the checksum is correct; 0:the checksum is error
//*-----------------------------------------------------------------------------
unsigned int check_crc(unsigned int len) //检验校验和
{
//unsigned char type,id,ack,para_id;
unsigned char crc_cksum;
crc_cksum=infor_msg[len-1];
if(crc_cksum==crc_fun(infor_msg,len-1))
{ v_telemetry_counter=AT91C_BASE_RTTC->RTTC_RTVR;
hd_flag=0; //准备接收下一帧
return 1;
}
//v_counter++; //通信错误计数器加1
hd_flag=1; ////若收到非法帧,则将刚收到的0xf5作为起始字符。
rx_len=0;
rx_buf[0]=DELIMITER;
rx_index=1;
return 0;
}
/*打包静态参数的data域*/
//*-----------------------------------------------------------------------------
//* Function Name : pack_config_infor
//* Object : pack the config informations of the odu.
//* autor : wang ping
//* Input Parameters : subject:the subject number of the content to be packed
//* Output Parameters : ack:the lenth of the message package.
//* if ack==0,means the subject number is invalid.
//*-----------------------------------------------------------------------------
unsigned char pack_config_infor(unsigned char subject)
{
unsigned char i,ack;
ack=0;
switch(subject)
{
case 0x01:
ODU_MSG_DATA(5,rfu_boot_version)
break;
case 0x02:
ODU_MSG_DATA(5,rfu_sw_version)
break;
case 0x03:
ODU_MSG_DATA(5,odu_part_num)
break;
case 0x05:
ODU_MSG_DATA(3,odu_serial_num)
break;
case 0x8:
ODU_MSG_DATA(3,rfu_asm_date)
break;
case 0x0a:
break;
case 0x0b:
ODU_MSG_INT_DATA(v_tx_freqmin)
break;
case 0x0c:
ODU_MSG_INT_DATA(v_tx_freqmax)
break;
case 0x0d:
ODU_MSG_INT_DATA(v_rx_freqmin)
break;
case 0x10:
ODU_MSG_INT_DATA(v_rx_freqmax)
break;
case 0x11:
ODU_MSG_INT_DATA(v_c_step)
break;
case 0x12:
ODU_MSG_INT_DATA(v_tx_frequency)
break;
case 0x13:
ODU_MSG_INT_DATA(v_rx_frequency)
break;
case 0x14:
ODU_MSG_INT_DATA(diplexer_tx_loss)
break;
case 0x15:
ODU_MSG_INT_DATA(diplexer_rx_loss)
break;
case 0x96:
ODU_MSG_DATA(5,rfu_part_num)
break;
case 0x98:
ODU_MSG_DATA(3,rfu_serial_num)
break;
case 0x9a:
ODU_MSG_DATA(5,iftx_part_num)
break;
case 0x9b:
ODU_MSG_DATA(3,iftx_serial_num)
break;
case 0x9c:
ODU_MSG_DATA(5,ifrx_part_num)
break;
case 0x9d:
ODU_MSG_DATA(3,ifrx_serial_num)
break;
case 0x9e:
ODU_MSG_DATA(5,syntx_part_num)
break;
case 0x9f:
ODU_MSG_DATA(3,syntx_serial_num)
break;
case 0xa0:
ODU_MSG_DATA(5,synrx_part_num)
break;
case 0xa1:
ODU_MSG_DATA(3,synrx_serial_num)
break;
case 0xa2:
ODU_MSG_DATA(5,dcc_part_num)
break;
case 0xa3:
ODU_MSG_DATA(3,dcc_serial_num)
break;
case 0xa4:
ODU_MSG_DATA(5,bb_part_num)
break;
case 0xa5:
ODU_MSG_DATA(3,bb_serial_num)
break;
case 0xa6:
ODU_MSG_DATA(5,diplexer_part_num)
break;
case 0xa8:
ODU_MSG_DATA(3,diplexer_serial_num)
break;
case 0xaa:
ODU_MSG_DATA(5,trxtx_part_num)
break;
case 0xab:
ODU_MSG_DATA(3,trxtx_serial_num)
break;
case 0xac:
ODU_MSG_DATA(5,trxrx_part_num)
break;
case 0xad:
ODU_MSG_DATA(3,trxrx_serial_num)
break;
case 0xd0:
ack=5;
for(i=0;i<5;i++)
{
ltob.ul=config_b[i];
tx_buf[ack++]=ltob.ch[3];
tx_buf[ack++]=ltob.ch[2];
tx_buf[ack++]=ltob.ch[1];
tx_buf[ack++]=ltob.ch[0];
}
break;
default:
break;
}
return ack;
}
/*上报静态配置消息*/
//*-----------------------------------------------------------------------------
//* Function Name : report_config_infor_pro
//* Object : report the config informations of the odu.CommandID=0x03
//* autor : wang ping
//* Input Parameters : subject:the subject number of the content to be reported
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void report_config_infor_pro(unsigned char subject)
{ unsigned char len;
len=pack_config_infor(subject);
if(len!=0)
{ ODU_MSG_HEAD(0x03,subject,len);
len+=5;
load_msg(tx_buf,len);
}
}
/*上报ODU各属性*/
//*-----------------------------------------------------------------------------
//* Function Name : report_odu_options
//* Object : report all the options of odu.
//* report when the system initiated,and then 2.5seconds once.
//* autor : wang ping
//* Input Parameters : none
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void report_odu_options()
{ void report_all_debugs();
report_config_infor_pro(0x01);
report_config_infor_pro(0x02);
report_config_infor_pro(0x03);
report_config_infor_pro(0x05);
report_config_infor_pro(0x08);
report_config_infor_pro(0x0b);
report_config_infor_pro(0x0c);
report_config_infor_pro(0x0d);
report_config_infor_pro(0x10);
report_config_infor_pro(0x11);
report_config_infor_pro(0x12);
report_config_infor_pro(0x13);
report_config_infor_pro(0x14);
report_config_infor_pro(0x15);
report_config_infor_pro(0x96);
report_config_infor_pro(0x98);
report_config_infor_pro(0x9a);
report_config_infor_pro(0x9b);
report_config_infor_pro(0x9c);
report_config_infor_pro(0x9d);
report_config_infor_pro(0x9e);
report_config_infor_pro(0x9f);
report_config_infor_pro(0xa0);
report_config_infor_pro(0xa1);
report_config_infor_pro(0xa2);
report_config_infor_pro(0xa3);
report_config_infor_pro(0xa4);
report_config_infor_pro(0xa5);
report_config_infor_pro(0xa6);
report_config_infor_pro(0xa8);
report_config_infor_pro(0xaa);
report_config_infor_pro(0xab);
report_config_infor_pro(0xac);
report_config_infor_pro(0xad);
report_config_infor_pro(0xd0);
report_status_pro(0x21);
report_status_pro(0x22);
report_status_pro(0x23);
report_status_pro(0x24);
report_all_debugs();
//get_cmd_pro(0x35,0x04);
get_cmd_pro(0x30,0x04);
//get_cmd_pro(0x56,0x04);
}
/*上报动态参数上报消息*/
//*-----------------------------------------------------------------------------
//* Function Name : report_status_pro
//* Object : report the dynamic parameters of odu.
//* report in main loop
//* autor : wang ping
//* Input Parameters : subject:the subject number of the content to be reported
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void report_status_pro(unsigned char subject)
{
unsigned char ack,i;
ack=0;
switch(subject)
{
case 0x21:
ODU_MSG_INT_DATA(v_temp_value)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -