📄 lcodu_main_080416.c
字号:
//*----------------------------------------------------------------------------
//* File Name : lcodu_main.c
//* Object : Main application written in C
//* Creation : wp 200710
//* Modif : wp 20071023
//*----------------------------------------------------------------------------
// Include Standard files
//#include<assert.h>
#include "Board.h"
#include "vars.h"
#include "usart.h"
#include "hardctrl.h"
#include "spi.h"
#define SENDIND_DATA(x) {if(x) send_msg_USART1();\
else send_msg_USART0(); }
extern void initiation(void);
//*--------------------------------------------------------------------------------------
//* Function Name : Main
//* Object : Software entry point
//* Input Parameters : none.
//* Output Parameters : none.
//*--------------------------------------------------------------------------------------
void Delay (unsigned int a) { while (--a!=0); } // 延时函数
unsigned char debug_msg[12]={0x03,0x09,0x0,0x04,0x00,0x00,0x11,0x00,0x01,0x02,0x03,0x04};
unsigned int oldalarm;
//unsigned char old_crc_counter;
//unsigned int alive_always_counter;
/*
函数名: odu_data_init
功能:参数初始化,从flash中读出各配置参数的值
作者:wp
*/
void odu_data_init(void)
{
//v_counter=0;
//old_crc_counter=0;
att_step_delay=400;
v_at1b_auto=AUTO;
v_rssi_deltar_t_auto=MANUAL;
v_txmute_auto=0;
adc_det_out=1;
v_sysmax_txpower=(config_b[2]>>16)&0x0ff;
v_c_m_tx=(config_b[2]>>8)&0x0ff;
v_c_m_rx=config_b[2]&0x00ff;
v_c_tx_if=(config_b[4]>>16)&0x0ffff;
v_c_tx_if*=1000;
v_c_rx_if=config_b[4]&0x00ffff;
v_c_rx_if*=1000;
syn_select=(config_b[5]>>24)&0x0ff;
v_at1d =(config_b[5]>>16)&0x0ff;
v_at1e =(config_b[5]>>8)&0x0ff;
v_txpower_max=config_b[6]&0x0ff;
v_gate_a=(config_b[7]>>24)&0x0ff;
v_gate_b=(config_b[7]>>16)&0x0ff;
v_gate_c=(config_b[7]>>8)&0x0ff;
v_tx_power=(v_txpower_max-v_att0)*10-diplexer_tx_loss;
v_tx_frequency=*(unsigned int *)EADDR_F_TX;
v_rx_frequency=*(unsigned int *)EADDR_F_RX;
v_rx_f_lo=(v_rx_frequency-v_c_rx_if)/v_c_m_rx;
v_tx_f_lo=(v_tx_frequency-v_c_tx_if)/v_c_m_tx;
Set_TX_RX_RF(TX_RF,(v_tx_f_lo/1000));
Set_TX_RX_RF(RX_RF,(v_rx_f_lo/1000));
}
/*
函数名: adc_find_item
功能:查表求rssi相对应的rssi0的大小(dbm)
说明:adc表第一项为最小功率所对应的电压值,逐项变大
作者:wp
*/
signed int adc_find_item(unsigned int adcresult)
{
signed int i;
unsigned int t;
for(i=0;i<70;i++)
{
t=(v_e_rssi[i][1]<<8)+v_e_rssi[i][0];
if(adcresult<t) break;
}
if(i==0) return -90;
if(i==70) return -20;
if((adcresult-((v_e_rssi[i-1][1]<<8)+v_e_rssi[i-1][0]))>(t-adcresult))
return i-90;
else return(i-91);
}
/*
函数名: pmax_find_item
功能:查表求det_out相对应的功率的大小(dbm)
说明:adc表第一项为最大功率所对应的电压值,逐项变小
作者:wp
*/
signed int pmax_find_item(unsigned int adcresult)
{
signed int i;
signed int t;
for(i=0;i<50;i++)
{
if(adcresult>v_e_pmax[i]) break;
}
t=v_txpower_max-(diplexer_tx_loss/10);
if(i==0) return (4+t);
if((adcresult-v_e_pmax[i])>(v_e_pmax[i-1]-adcresult))
return (3-i+t);
return (4-i+t);
}
/*
函数名: adc_status_collect
功能:求messurement参数,处理当其中有参数发生变化时的动作
说明:AT1B表第一项为最低温度及其相应的温度补偿值,逐项递增
作者:wp
*/
//*-----------------------------------------------------------------------------
//* Function Name : adc_status_collect
//* Object : check the changes of the 5 ADC channels' result,and refresh
//* the corresponding ADC result. And make it sure if any
//* alarms occur, and manage them. Then control AT1B.
//* At the same time,if a specifically time passed, report
//* the corresponding parameters to the IDU or Monitor.
//* autor : wang ping
//* Input Parameters : none
//* Output Parameters : none.
//*-----------------------------------------------------------------------------
void adc_status_collect()
{
unsigned char flag,i;
signed char tmp1,tmp2;
signed int tmp,ack;
unsigned int address;
flag=0;
if(adc_temp!=adc_result_buf0)
{
adc_temp=adc_result_buf0;
tmp=adc_result_buf0*33;
tmp/=128;
tmp-=69;
if(v_temp_value!=tmp)
{
v_temp_value=tmp;
if(v_temp_value>v_temp_max)
{ //v_temp_max=v_temp_value;
address=(unsigned int)&v_temp_max;
ltob.ul=v_temp_value;
ack=at91f_flash_write(address,4,ltob.ch);
if(ack!=0) { v_odu_alarm[1]|=ALARM_EEPROM; alarm_flag=1;}
else v_odu_alarm[1]&=NORMAL_EEPROM;
}
for(i=0;i<4;i++)
{
tmp1=v_e_at1b[i][0];
if(tmp<tmp1) break;
}
if(i>3) v_at1b_a=v_e_at1b[3][1];
else
{
v_at1b_a=v_e_at1b[i][1];
if(i>0)
{
tmp2=v_e_at1b[i-1][0];
if((tmp-tmp2)<(tmp1-tmp))
v_at1b_a=v_e_at1b[i-1][1];
}
}
tmp1=v_at1a+v_at1b_a+v_at1c+v_at1d+v_at1e;
if(tmp1<0) { v_at1b_a=-(v_at1a+v_at1c+v_at1d+v_at1e);}
if(tmp1>31){ v_at1b_a=31-(v_at1a+v_at1c+v_at1d+v_at1e);}
if(v_at1b_auto==AUTO)
{
/*for(i=0;i<4;i++)
{
tmp1=v_e_at1b[i][0];
if(tmp<tmp1) break;
}
v_at1b=v_e_at1b[i][1];
if(i>0)
{
tmp2=v_e_at1b[i-1][0];
if((tmp-tmp2)<(tmp1-tmp))
v_at1b=v_e_at1b[i-1][1];
}
tmp1=v_at1a+v_at1b+v_at1c+v_at1d;
if(tmp1<0) { v_at1b=-(v_at1a+v_at1c+v_at1d);tmp1=0;}
if(tmp1>31){ v_at1b=31-(v_at1a+v_at1c+v_at1d);tmp1=31;}
*/
v_at1b=v_at1b_a;
tmp1=v_at1a+v_at1b+v_at1c+v_at1d+v_at1e;
if(tmp1<0) tmp1=0;
if(tmp1>30) tmp1=30;
set_att(1,tmp1);
}
if(v_rssi_deltar_t_auto==AUTO)
{
for(i=0;i<4;i++)
{
tmp1=v_e_rssi_deltar_t[i][0];
if(tmp<tmp1) break;
}
v_rssi_deltar_t=v_e_rssi_deltar_t[i][1];
if(i>0)
{
tmp2=v_e_rssi_deltar_t[i-1][0];
if((tmp-tmp2)<(tmp1-tmp))
v_rssi_deltar_t=v_e_rssi_deltar_t[i-1][1];
}
}
flag=1;//indicate the temperature has changed
}
}
#if(1)
adc_rssi_1=adc_result_buf2;
adc_rssi_2=adc_result_buf1;
tmp=(adc_rssi_1+adc_rssi_2)/2;
if((adc_rssi!=tmp)||(flag))
{
if(adc_rssi!=tmp) { adc_rssi=tmp; v_rssi0=adc_find_item(adc_rssi);}
v_rssi=v_rssi0+v_rssi_deltar_t+(signed int)v_e_rssi_deltar;
v_vrssi=3.7+0.04*v_rssi;
v_dac_spi=(v_vrssi/3.3)*255;
dac_write(v_dac_spi);
}
#endif
if(adc_det_in!=adc_result_buf3) adc_det_in=adc_result_buf3;
if(adc_det_out!=adc_result_buf4)
{ adc_det_out=adc_result_buf4;
v_det_out=pmax_find_item(adc_det_out);
tmp=v_tx_power/10;
if(v_det_out<(tmp+v_gate_a))
{ v_odu_alarm[0]|=ALARM_TXPOWERALARM;
alarm_flag=1;
}
else
v_odu_alarm[0]&=NORMAL_TXPOWERALARM;
if((v_det_out<(tmp+v_gate_b))||(v_det_out>(tmp+v_gate_c)))
{ v_odu_alarm[0]|=ALARM_TX_CABLE;
alarm_flag=1;
}
else
v_odu_alarm[0]&=NORMAL_TX_CABLE;
}
if(timer0_flag) //rssi
{ timer0_flag=0;
report_status_pro(ITEM_RSL);
SENDIND_DATA(us_port);
}
#if(1)
if((v_att_counter_forsend+10)<AT91C_BASE_RTTC->RTTC_RTVR)
{
debug_report_pro(0x35);
debug_report_pro(0x56);
debug_report_pro(0x40);
debug_report_pro(0x41);
debug_report_pro(0x42);
debug_report_pro(0x43);
debug_report_pro(0x44);
debug_report_pro(0x47);
debug_report_pro(0x48);
debug_report_pro(0x49);
debug_report_pro(0x4a);
debug_report_pro(0x4b);
debug_report_pro(0x4c);
v_att_counter_forsend=AT91C_BASE_RTTC->RTTC_RTVR;
}
else if(v_att_counter_forsend>AT91C_BASE_RTTC->RTTC_RTVR)
v_att_counter_forsend=AT91C_BASE_RTTC->RTTC_RTVR;
if((v_temp_value_counter_forsend+TIME_120SECONDS)<AT91C_BASE_RTTC->RTTC_RTVR) //temperature
{
report_status_pro(ITEM_TEMP);
report_status_pro(ITEM_MAX_TEMP);
if(v_txmute==TX_NOTMUTED)
report_status_pro(ITEM_TX_PA);
else
{
v_tx_power=-600;
report_status_pro(ITEM_TX_PA);
}
SENDIND_DATA(us_port);
v_temp_value_counter_forsend=AT91C_BASE_RTTC->RTTC_RTVR;
}
else if(AT91C_BASE_RTTC->RTTC_RTVR<v_temp_value_counter_forsend)
v_temp_value_counter_forsend=AT91C_BASE_RTTC->RTTC_RTVR;
if((v_config_counter_forsend+TIME_121SECONDS)<AT91C_BASE_RTTC->RTTC_RTVR)
{
//report_config_infor_pro(0x0b);
//report_config_infor_pro(0x0c);
//report_config_infor_pro(0x0d);
//report_config_infor_pro(0x10);
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(0x01);
report_config_infor_pro(0x02);
SENDIND_DATA(us_port);
v_config_counter_forsend=AT91C_BASE_RTTC->RTTC_RTVR;
}
else if(AT91C_BASE_RTTC->RTTC_RTVR<v_temp_value_counter_forsend)
v_config_counter_forsend=AT91C_BASE_RTTC->RTTC_RTVR;
#endif
}
/*
函数名: alarm_status_collect
功能:检测是否有告警产生,如果有,立即上报
说明:alarm_flag:为1表示中断中(timer1,irq0、irq1)检测到有告警产生
作者:wp
*/
//*-----------------------------------------------------------------------------
//* Function Name : alarm_status_collect
//* Object : check the alarm input port and new-alarm-occur flag(alarm_flag).
//* Clear the corresponding bits of v_odu_alarm[],if any
//* alarms disappear. Set the corresponding bits of v_odu_alarm[],
//* if any alarms occur,and report it immediately.
//* autor : wang ping
//* Input Parameters : none
//* Output Parameters : none.
//*-----------------------------------------------------------------------------
void alarm_status_collect()
{
if((AT91C_BASE_PIOA->PIO_PDSR & BIT30 ) == BIT30 )
{ v_odu_alarm[0]&=NORMAL_RXSYNTHLOCK;
}
else
{ alarm_flag=1;
v_odu_alarm[0]|=ALARM_RXSYNTHLOCK;
}
if((AT91C_BASE_PIOA->PIO_PDSR & BIT20 ) == BIT20 )
{
v_odu_alarm[0]&=NORMAL_TXSYNTHLOCK;
}
else
{ alarm_flag=2;
v_odu_alarm[0]|=ALARM_TXSYNTHLOCK;
}
if((v_telemetry_counter+TIME_120SECONDS)<AT91C_BASE_RTTC->RTTC_RTVR)
{
v_odu_alarm[1]|=ALARM_TELEMETRY;
if(alarm_flag==0) alarm_flag=1;
}
else
{ v_odu_alarm[1]&=NORMAL_TELEMETRY;
}
if(alarm_flag>0)
{
if((alarm_flag==2)&&(v_txmute_auto))
{
v_txmute=1;
set_mute(1);
}
alarm_flag=0;
if(oldalarm!=(v_odu_alarm[1]<<8)+v_odu_alarm[0])
{ oldalarm=(v_odu_alarm[1]<<8)+v_odu_alarm[0];
report_alarm_pro();
SENDIND_DATA(us_port);
}
}
if((v_alarm_counter_forsend+ALARM_OVER_TIME)<AT91C_BASE_RTTC->RTTC_RTVR)
{ v_alarm_counter_forsend=AT91C_BASE_RTTC->RTTC_RTVR;
report_alarm_pro();
SENDIND_DATA(us_port);
if((v_odu_alarm[1])||(v_odu_alarm[0])) AT91C_BASE_PIOA->PIO_CODR= PA8_LED;
else AT91C_BASE_PIOA->PIO_SODR= PA8_LED;
}
// if(us_port==0)
{
if(v_alarm_counter_forsend>AT91C_BASE_RTTC->RTTC_RTVR)
v_alarm_counter_forsend=AT91C_BASE_RTTC->RTTC_RTVR;
}
}
//*-----------------------------------------------------------------------------
//* Function Name : alarm_status_collect
//* Object : including adc_status_collect,alarm_status_collect,
//* If TX RF is muted,then change the v_tx_power to -600,
//* means tx power is -60dbm.
//* autor : wang ping
//* Input Parameters : none
//* Output Parameters : none.
//*-----------------------------------------------------------------------------
void odu_status_collect()
{ //unsigned char tmp_msg[5];
signed int tsi;
//signed char tsc;
adc_status_collect();
alarm_status_collect();
if(v_txmute==TX_NOTMUTED)
{
tsi=(v_txpower_max-v_att0)*10-diplexer_tx_loss;
if(tsi!=v_tx_power)
{
v_tx_power=tsi;
report_status_pro(ITEM_TX_PA);
SENDIND_DATA(us_port);
}
}
else v_tx_power=-600;
}
__arm
void sw_reset(void)
{
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKDIS;
AT91C_BASE_AIC->AIC_IDCR = 0XFFFFFFFF;
AT91C_BASE_AIC->AIC_ICCR = 0XFFFFFFFF;
asm("B 4000");
}
void reset_pio()
{
unsigned int tmp;
//AT91C_BASE_PIOA->PIO_PDR = PA17_TEMP;
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA; // 允许PIO时钟
tmp=0;
tmp=0;
tmp=(BIT0|BIT1|BIT2|BIT4|BIT7|BIT16|BIT18|BIT19|BIT23|BIT24|BIT25|BIT26|PA12_MUTE|BIT28|PA15_LE1|PA8_LED|BIT11|BIT13|BIT14|BIT31|BIT3);
AT91C_BASE_PIOA->PIO_PER = tmp; // 配置PA0为输出->att0 and att1
AT91C_BASE_PIOA->PIO_OER = tmp;
AT91C_BASE_PIOA->PIO_SODR= tmp;
//AT91F_PIO_CfgOutput( AT91C_BASE_PIOA, tmp ) ;
}
//*-----------------------------------------------------------------------------
//* Function Name : main
//* Object : the main function.The entrance of the software.
//* autor : wang ping
//* Input Parameters : none
//* Output Parameters : none.
//*-----------------------------------------------------------------------------
int main()
{
initiation();
if((AT91F_PIO_GetInput(AT91C_BASE_PIOA) & PA27_MONITOR ) == PA27_MONITOR ) us_port=0; //select us0
else us_port=1;
syn_select=(config_b[5]>>24)&0x0ff;
odu_spi_init();
odu_data_init();
v_txmute=1;
set_mute(1);
report_odu_options();
Delay(8000000);
//sw_reset();
//reset_pio();
while(1)
{
odu_status_collect();
odu_to_idu_com();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -