cmdrun.c

来自「卫星接受机控制卫星和LCD显示」· C语言 代码 · 共 333 行

C
333
字号
#include <stdio.h>
#include <stdlib.h>
#include "AT91SAM7S128.h"
#include "usart.h"
#include "vars.h"
#define __inline inline
#include "lib_at91sam7s128.h"


#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 true	-1
#define false	0

extern  void AT91F_disable_interrupt(void);
extern  void AT91F_enable_interrupt(void);


//*----------------------------------------------------------------------------
//* \fn    AT91F_Flash_Init
//* \brief Flash init
//*----------------------------------------------------------------------------
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 ;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_Flash_Ready
//* \brief Wait the flash ready
//*----------------------------------------------------------------------------
__ramfunc int at91f_flash_ready (void)
{
    unsigned int status,i;
    status = 0;

    //* Wait the end of command
        while ((status & AT91C_MC_FRDY) != AT91C_MC_FRDY )
        {
          status = AT91C_BASE_MC->MC_FSR;
          if(i++>10000) break;
        }
        return status;
}


/*erase all*/
unsigned char erase_flash_region(unsigned char c)
{
  AT91PS_MC reg_MC=AT91C_BASE_MC;
  reg_MC->MC_FCR=(0x05a<<24)+(FLASH_REGION1_PAGE<<17)+AT91C_MC_FCMD_START_PROG;
  return 0;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_Flash_Write
//* \brief Write in one Flash page located in AT91C_IFLASH,  size in 32 bits
//* \input Flash_Address: start at 0x0010 0000 size: in byte
//*----------------------------------------------------------------------------
__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 char * Flash;
    //* init flash pointer
        Flash = (unsigned char *) Flash_Address;

        at91f_flash_init();
   //* Get the Flash page number
        page = ((Flash_Address - (unsigned int)AT91C_IFLASH ) /AT91C_IFLASH_PAGE_SIZE);
   //* copy the new value
	for (i=0; (i < FLASH_PAGE_SIZE_BYTE) & (size > 0) ;i++, Flash++,buff++,size-- ){
	//* copy the flash to the write buffer ensuring code generation
	    *Flash=*buff;
	}
	//* 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)) ;
    //* Wait the end of command
        status = at91f_flash_ready();
    //* Protect
		AT91F_enable_interrupt();

    //* Check the result
    if ( (status & ( AT91C_MC_PROGE | AT91C_MC_LOCKE ))!=0) return false;
  return 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, status;
    unsigned char * Flash;
    //* init flash pointer
        Flash = (unsigned char *) Flash_Address;

        at91f_flash_init();
   //* Get the Flash page number
        //page = ((Flash_Address - (unsigned int)AT91C_IFLASH ) /AT91C_IFLASH_PAGE_SIZE);
   //* read the flash address,and save to buff. the max lenth is 256 bytes
	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;
	}
	//* 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)) ;
    //* Wait the end of command
        status = at91f_flash_ready();
    //* Protect
		//AT91F_enable_interrupt();

    //* Check the result
    if ( (status & ( AT91C_MC_PROGE | AT91C_MC_LOCKE ))!=0) return false;
  return true;
}


unsigned char switch_flash_region(unsigned char c)
{
  //AT91PS_MC reg_MC=AT91C_BASE_MC;
  
  /*for adding.............................*/
  
  return 0;
}

void reset_pro(unsigned char c)                        //c=1:reset
{  AT91PS_RSTC   a_pRSTC  = AT91C_BASE_RSTC;

   if(c==1) a_pRSTC->RSTC_RCR=0xa5000000+AT91C_RSTC_PROCRST;
   else a_pRSTC->RSTC_RCR=0xa5000000;
}


unsigned char config_pro(unsigned char msg[],unsigned int len)
{
  unsigned char type,ack,config_no;
  unsigned char i;

  type=msg[0];
  config_no=msg[2];
  ack=0;
  
 // tmp_buf[0]=msg[0];
 // tmp_buf[1]=msg[1];
  
 // for(i=2;i<len;i++)
 //   tmp_buf[i+1]=msg[i];
  
  if(type==0x03)//command message
  {
    switch(config_no){
    case 0x00:
      
      break;
    case 0x01:
      v_opmode=msg[3];
      v_cfg_data[1].value[0]=msg[3];
      v_cfg_data[1].len=1;
      break;
    case 0x04:
      v_txmute=msg[3];  
      v_cfg_data[2].value[0]=msg[3];
      v_cfg_data[2].len=1;
      break;
    case 0x06:
      v_atpcon=msg[3];
      v_cfg_data[3].value[0]=msg[3];
      v_cfg_data[3].len=1;
      break;
    case 0x07:
      v_offtxpower=msg[4]<<8+msg[3];
      v_cfg_data[4].value[0]=msg[3];
      v_cfg_data[4].value[1]=msg[4];
      v_cfg_data[4].len=2;
      break;
    case 0x0a:
      ltob.ch[0]=msg[3]; ltob.ch[1]=msg[4];
      ltob.ch[2]=msg[5]; ltob.ch[3]=msg[6];
      v_tx_frequency=ltob.ul;
      ltob.ch[0]=msg[7]; ltob.ch[1]=msg[8];
      ltob.ch[2]=msg[9]; ltob.ch[3]=msg[10];
      v_rx_frequency=ltob.ul;
      for(i=0;i<8;i++)
        v_cfg_data[5].value[i]=msg[3+i];
      v_cfg_data[5].len=8;
      break;
    case 0x12:
      v_bandwidth=msg[3];
      v_cfg_data[6].value[0]=msg[3];
      v_cfg_data[6].len=1;
      break;
    case 0x13:
      v_qam=msg[3];
      v_cfg_data[7].value[0]=msg[3];
      v_cfg_data[7].len=1;
      break;
    /*case 0x20:
      v_rfband=msg[3];
      break;
    case 0x21:
      v_rfband=msg[3];
      break;
    case 0x23:
      v_txpower_min=msg[4]<<8+msg[3];
      break;
    case 0x25:
      v_txpower_max=msg[4]<<8+msg[3];
      break;
    case 0x29:
      ltob.ch[0]=msg[3]; ltob.ch[1]=msg[4];
      ltob.ch[2]=msg[5]; ltob.ch[3]=msg[6];
      v_rx_freqmin=ltob.ul;
      break;
    case 0x2d:
      ltob.ch[0]=msg[3]; ltob.ch[1]=msg[4];
      ltob.ch[2]=msg[5]; ltob.ch[3]=msg[6];
      v_rx_freqmax=ltob.ul;
      break;
    case 0x31:
      ltob.ch[0]=msg[3]; ltob.ch[1]=msg[4];
      ltob.ch[2]=msg[5]; ltob.ch[3]=msg[6];
      v_tx_freqmin=ltob.ul;
      break;
    case 0x35:
      ltob.ch[0]=msg[3]; ltob.ch[1]=msg[4];
      ltob.ch[2]=msg[5]; ltob.ch[3]=msg[6];
      v_tx_freqmax=ltob.ul;
      break;
      */
    default:
      ack=ACK_VALINVALID;
      break;
    }
  }
  //msg[2]=ack;
  //load_msg(tmp_buf,len+1);   //数据链路层打包,并写入待发送存储区,
  return ack;
}

unsigned char ftrans_pro(unsigned char *msg,unsigned int len){  //file translate program
  unsigned char ack=0;
  unsigned int i;
  unsigned char st,fg;
  unsigned char *tmp_buf;
  /*文件传输命令*/
  tmp_buf=malloc(len);
  st=msg[2];
  fg=msg[3];
  //msg[len]='/0';
  if(st=='s')
  {
    if(fg=='7')
      erase_flash_region(1);  //erase inactive flash region
    else if(fg=='0')
    {
      for(i=0;i<len-4;len++)
        tmp_buf[i]=msg[i+4];
    }
    else if(fg=='1')
    {
      //fprintf(tmp_buf,"%s",msg+4);
      for(i=0;i<len-4;len++)
        tmp_buf[i]=msg[i+4];
    }
    else if(fg=='5')
      switch_flash_region(1);
    else if(fg=='4')
      reset_pro(1);
  }
  return ack;
}

unsigned char flashacc_pro(unsigned char *msg,unsigned int len){
  unsigned char ack,serial_num,lenth,type;
  unsigned int i,flash_addr;
  unsigned char tmp_buf[128],temp[128];
  ack=0;
  
  type=msg[0];                //命令类型
  serial_num=msg[2];          
  lenth=msg[3];
  flash_addr=((unsigned int)msg[5])<<8+msg[4]+((unsigned int)msg[6]<<16)+((unsigned int)msg[7]<<24);
  tmp_buf[0]=0x01;                  /*type byte:response*/
  tmp_buf[1]=0x09;                  /*id byte: flash access*/
  for(i=3;i<len+1;i++)
    tmp_buf[i]=msg[i-1];
    
  /*对flash进行读写*/
  if(type==0x02)  /*若为request信息,则进行读操作*/
  {
    ack=at91f_flash_read( flash_addr,(int)lenth,temp);
    for(i=0;i<lenth;i++) tmp_buf[i+9]=temp[i];
    tmp_buf[2]=ack;
    load_msg(tmp_buf,9+lenth);
  }
  else if(type==0x03)/*若为cmd信息,则进行写操作*/
  { for(i=0;i<lenth;i++) temp[i]=msg[i+8];
    ack=at91f_flash_write(flash_addr,(int)lenth,temp);
    tmp_buf[2]=ack;
    load_msg(tmp_buf,9+lenth);
  }
    
  /******************/
  //load_msg(tmp_buf,i+lenth);
  
  return ack;
}

unsigned char swversion_pro(unsigned char *msg,unsigned int len){
 // unsigned char ack;
  //unsigned int i;
  //unsigned char tmp_buf[64];
  
  return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?