📄 lcodu_hardctrl.c
字号:
//*----------------------------------------------------------------------------
//* File Name : loodu_hardctrl.c
//* Object : hardware actions (set attenuation,mute ect. )
//* Creation : wp 22/10/2007
//* Modif :
//*----------------------------------------------------------------------------
//#include "AT91SAM7S128.h"
#include "vars.h"
#include "board.h"
extern void Delay (unsigned int a);
extern void report_status_pro(unsigned char subject);
AT91PS_PIO m_pPio = AT91C_BASE_PIOA; // 定义 PIOA 端口
const unsigned int C_ATT0[32]={
0x0000,0x0001,0x0002,0x0003,0x0004,0x0005,0x0006,0x0007,
0x0010,0x0011,0x0012,0x0013,0x0014,0x0015,0x0016,0x0017,
0x0080,0x0081,0x0082,0x0083,0x0084,0x0085,0x0086,0x0087,
0x0090,0x0091,0x0092,0x0093,0x0094,0x0095,0x0096,0x0097
};
const unsigned int C_ATT1[32]={
0x0000000,0x0010000,0x0800000,0x0810000,0x1000000,0x1010000,0x1800000,0x1810000,
0x2000000,0x2010000,0x2800000,0x2810000,0x3000000,0x3010000,0x3800000,0x3810000,
0x4000000,0x4010000,0x4800000,0x4810000,0x5000000,0x5010000,0x5800000,0x5810000,
0x6000000,0x6010000,0x6800000,0x6810000,0x7000000,0x7010000,0x7800000,0x7810000
};
#define FLASH_PAGE_SIZE_BYTE 256
#define FLASH_PAGE_SIZE_LONG 32
#define FLASH_LOCK_BITS_SECTOR 16
#define FLASH_SECTOR_PAGE 32
/* 16 lock bits, each protecting 16 sectors of 32 pages*/
#define FLASH_LOCK_BITS 16
#define FLASH_BASE_ADDRESS 0x00100000
#define FLASH_END_ADDRESS 0x0011ffff
#define MAX_ONE_FRAME_LENTH 93
extern void AT91F_disable_interrupt(void);
extern void AT91F_enable_interrupt(void);
__ramfunc void reset_pro(unsigned char c);
//extern void sw_reset(void);
//unsigned int flash_counter;
unsigned int max_flash_counter;
//*-----------------------------------------------------------------------------
//* Function Name : AT91F_Flash_Init
//* Object : flash initiation
//* autor : wang ping
//* Input Parameters : none
//* Output Parameters : none.
//*-----------------------------------------------------------------------------
__ramfunc void at91f_flash_init (void)
{
//* Set number of Flash Waite sate
// SAM7S64 features Single Cycle Access at Up to 30 MHz
// if MCK = 47923200, 72 Cycles for 1 econde ( field MC_FMR->FMCN)
AT91C_BASE_MC->MC_FMR = ((AT91C_MC_FMCN)&(72 <<16)) | AT91C_MC_FWS_1FWS ;
}
//*-----------------------------------------------------------------------------
//* Function Name : at91f_flash_ready
//* Object : Wait the flash ready.Reset flash if the waiting time
//* exceding the deadline.
//* autor : wang ping
//* Input Parameters : none
//* Output Parameters : status:the status of the flash.
//*-----------------------------------------------------------------------------
__ramfunc int at91f_flash_ready (void)
{
unsigned int status,i;
status = 0;
i=0;
//flash_counter=0;
//* Wait the end of command
while ((status & AT91C_MC_FRDY) != AT91C_MC_FRDY )
{
status = AT91C_BASE_MC->MC_FSR;
if(i++>500000)
{
reset_pro(1);
break;
}
// flash_counter++;
}
if(max_flash_counter<i)
max_flash_counter=i;
//status = AT91C_BASE_MC->MC_FSR;
return status;
}
//*-----------------------------------------------------------------------------
//* Function Name : at91f_flash_write
//* Object : Write in one or two Flash page located in AT91C_IFLASH,
//* size in 32 bits
//* autor : wang ping
//* Input Parameters : Flash_Address: start at 0x0010 0000;
//* size: in byte,less than 256 bytes
//* buff:the bytes to be write to flash
//* Output Parameters : ture:write correctly; false:error happens.
//*-----------------------------------------------------------------------------
__ramfunc int at91f_flash_write( unsigned int Flash_Address ,int size ,unsigned char * buff)
{
//* set the Flash controller base address
AT91PS_MC ptMC = AT91C_BASE_MC;
unsigned int i, page, status;
unsigned int pb_addr,index;
unsigned int flashbuf[128];
unsigned char *bufch;
unsigned int * Flash;
//* init flash pointer
/*if the address is invalid,then the command won't be executed*/
pb_addr=Flash_Address&0xffffff00;
if((Flash_Address<FLASH_BASE_ADDRESS)||((Flash_Address+size)>FLASH_END_ADDRESS))
return ACK_CMDNOTEXE;
if(size>MAX_ONE_FRAME_LENTH) return false;
Flash = (unsigned int *) pb_addr;
/*backup the current flash data*/
for(i=0;i<64;i++) flashbuf[i]=Flash[i];
index=Flash_Address&0x00ff;
bufch=(unsigned char *)flashbuf;
for(i=0;i<size;i++)
{
bufch[index+i]=buff[i];
}
at91f_flash_init();
//* Get the Flash page number
page = ((Flash_Address - (unsigned int)AT91C_IFLASH ) /AT91C_IFLASH_PAGE_SIZE);
//unlock page
// ptMC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_UNLOCK | (AT91C_MC_PAGEN & (page <<8)) ;
//* copy the new value
for(i=0;i<64;i++,Flash++) *Flash=flashbuf[i];
//* Protect
AT91F_disable_interrupt();
//* Write the write page command
ptMC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_START_PROG | (AT91C_MC_PAGEN & (page <<8)) ;
// ptMC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_PROG_AND_LOCK | (AT91C_MC_PAGEN & (page <<8)) ;
//* Wait the end of command
status = at91f_flash_ready();
//* Protect
AT91F_enable_interrupt();
#if(1)
/*------------------------------------------------------------------------*/
/*if datas must be stored in two different pages,then write the other page*/
/*------------------------------------------------------------------------*/
if((size+index)>FLASH_PAGE_SIZE_BYTE)
{
pb_addr+=FLASH_PAGE_SIZE_BYTE;
Flash=(unsigned int *) pb_addr;
page++;
//for (i=0; (i < FLASH_PAGE_SIZE_BYTE) & (size > 0) ;i+=4, Flash++,buff++,size-=4 ){
//* copy the flash to the write buffer ensuring code generation
// *Flash=*buff;
//}
// ptMC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_UNLOCK | (AT91C_MC_PAGEN & (page <<8)) ;
for(i=0;i<64;i++) flashbuf[i]=Flash[i];
size+=index;
size-=256;
index=256-index;
//bufch=(unsigned char *)flashbuf;
for(i=0;i<size;i++)
{
bufch[i]=buff[index+i];
}
for(i=0;i<64;i++,Flash++) *Flash=flashbuf[i];
AT91F_disable_interrupt();
//* Write the write page command
ptMC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_START_PROG | (AT91C_MC_PAGEN & (page <<8)) ;
// ptMC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_PROG_AND_LOCK | (AT91C_MC_PAGEN & (page <<8)) ;
//* Wait the end of command
status |= at91f_flash_ready();
//* Protect
AT91F_enable_interrupt();
}
#endif
//* Check the result
if ( (status & ( AT91C_MC_PROGE | AT91C_MC_LOCKE ))!=0) return false;
return true;
}
//*-----------------------------------------------------------------------------
//* Function Name : at91f_flash_read
//* Object : Read data located in AT91C_IFLASH, size in 8 bits
//* autor : wang ping
//* Input Parameters : Flash_Address: start at 0x0010 0000;
//* size: in byte,less than 256 bytes
//* buff:the bytes got from the flash
//* Output Parameters : TRUE.
//*-----------------------------------------------------------------------------
__ramfunc int at91f_flash_read( unsigned int Flash_Address ,int size ,unsigned char * buff)
{
//* set the Flash controller base address
//AT91PS_MC ptMC = AT91C_BASE_MC;
unsigned int i;
unsigned char * Flash;
//* init flash pointer
Flash = (unsigned char *) Flash_Address;
for (i=0; (i < FLASH_PAGE_SIZE_BYTE) & (size > 0) ;i++, Flash++,buff++,size-- ){
//* copy the flash to the write buffer ensuring code generation
*buff=*Flash;
}
return true;
}
#if(0)
__arm
unsigned char switch_flash_region(unsigned char c)
{
//AT91PS_MC reg_MC=AT91C_BASE_MC;
/*for adding.............................*/
if(c==0)
asm("B 0");
else if(c==1)
asm("B 108000h");
else
asm("B 110000h");
return 0;
}
#endif
//*-----------------------------------------------------------------------------
//* Function Name : reset_pro
//* Object : reset the flash
//* autor : wang ping
//* Input Parameters : c=1:software reset; c=0:hardware reset
//* Output Parameters : none.
//*-----------------------------------------------------------------------------
__ramfunc void reset_pro(unsigned char c) //c=1:reset
{ AT91PS_RSTC a_pRSTC = AT91C_BASE_RSTC;
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;
if(c==1) a_pRSTC->RSTC_RCR=0xa5000000+AT91C_RSTC_PROCRST;
else a_pRSTC->RSTC_RCR=0xa5000000;
}
//*-----------------------------------------------------------------------------
//* Function Name : set_att
//* Object : set the attenuation
//* autor : wang ping
//* Input Parameters : chn=1:set att1; chn=0:set att0
//* : att(0~31),the attenuation value
//* Output Parameters : 0:the attenuation was set correctly;
//* others:something unexpected taken place.
//*-----------------------------------------------------------------------------
unsigned char set_att(unsigned char chn,unsigned char att) //att0 控制功率; att1作温度补偿
{
unsigned int tmp0;
unsigned int tmp;
if(att>31) return ACK_VALINVALID;
if(chn==0) {
if(v_att0>31) v_att0=31;
/*disable tc0/tc1/adc interrupt*/
/*..............................*/
AT91F_AIC_ClearIt(AT91C_BASE_AIC, AT91C_ID_ADC);
AT91F_AIC_ClearIt(AT91C_BASE_AIC, AT91C_ID_TC1);
AT91F_AIC_ClearIt(AT91C_BASE_AIC, AT91C_ID_TC0);
do{
if(v_att0<att) v_att0++;
else if(v_att0>att) v_att0--;
tmp0=C_ATT0[v_att0];
tmp=C_ATT0[31];
m_pPio->PIO_CODR = tmp;
m_pPio->PIO_SODR = tmp0;
Delay(att_step_delay);
}while(v_att0!=att);
/*enable tc0/tc1/adc interrupt*/
/*..............................*/
AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_ADC);
AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC1);
AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC0);
}
else {
if(v_att1>31) v_att1=31;
/*disable tc0/tc1/adc interrupt*/
/*..............................*/
AT91F_AIC_ClearIt(AT91C_BASE_AIC, AT91C_ID_ADC);
AT91F_AIC_ClearIt(AT91C_BASE_AIC, AT91C_ID_TC1);
AT91F_AIC_ClearIt(AT91C_BASE_AIC, AT91C_ID_TC0);
do{
if(v_att1<att) v_att1++;
else if(v_att1>att) v_att1--;
tmp0=C_ATT1[v_att1];
tmp=C_ATT1[31];
m_pPio->PIO_CODR = tmp;
m_pPio->PIO_SODR = tmp0;
Delay(att_step_delay);
}while(v_att1!=att);
/*enable tc0/tc1/adc interrupt*/
/*..............................*/
AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_ADC);
AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC1);
AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC0);
}
if(chn==0) m_pPio->PIO_SODR=PA15_LE1;
else m_pPio->PIO_SODR=PA19_LE2;
return 0;
}
//*-----------------------------------------------------------------------------
//* Function Name : set_mute
//* Object : set the tx rf muted or unmuted
//* autor : wang ping
//* Input Parameters : v=1(TX_MUTED),mute the tx rf
//* : v=0(TX_NOTMUTED),unmute the tx rf
//* Output Parameters : 0:the attenuation was set correctly;
//* others:v is invalidity
//*-----------------------------------------------------------------------------
unsigned char set_mute(unsigned char v)
{
#if(__NOT_DEBUG__)
if((v_odu_alarm[0] & ALARM_TXSYNTHLOCK)==ALARM_TXSYNTHLOCK)
{
v_txmute=TX_MUTED;
if(v==TX_NOTMUTED) return ACK_CMDNOTEXE;
}
#endif
if(v==TX_NOTMUTED) //un mute
{ // m_pPio->PIO_CODR = MUTE;
AT91F_PIO_ClearOutput( AT91C_BASE_PIOA, PA12_MUTE);
if(v_e_att0>300)
set_att(0,30);
else
set_att(0,v_e_att0/10);
return 0;
}
if(v==TX_MUTED)//muted
{ //m_pPio->PIO_SODR = MUTE;
AT91F_PIO_SetOutput( AT91C_BASE_PIOA, PA12_MUTE);
v_tx_power=-600;
report_status_pro(ITEM_TX_PA);
return 0;
}
return ACK_VALINVALID;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -