⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fesc_5554lib.c

📁 MPC5554处理器的初始化例程
💻 C
字号:
/* system clock freq is 120MHz */





/*====================================================================*/
/*   @  */
/*       ################################
         ##                            ##
         ##                            ##
         ##                            ##
         ################################        */



void set_sys_tick(uint16_t cnt)
{   EnterCritical();
	  sys_tick = cnt;
	  ExitCritical();
}

void set_temp_tick(uint16_t cnt)
{   EnterCritical();
	  temp_tick = cnt;
	  ExitCritical();
}


/*====================================================================*/
/*   @  */
/*       ################################
         ##                            ##
         ##     initial                ##
         ##                            ##
         ################################        */
void FESC_5554_init(void)
{   init_EMIOS();
    init_ESCI();
    init_FPGA_SPI();
    init_SXC();
    init_IRQ();

    init_EMIOS_GEN_SSP_LOOP_IRQ();
    init_EMIOS_GEN_SPI_master_DAEMON_IRQ();
    init_FPGA_GEN_MF_SPI_slave_DAEMON_IRQ();
    init_FPGA_GEN_MF_SPI_ALL_DONE_IRQ();

    init_GloVar();
    //EnableIRQ();   //enable interrupt in main loop
}





/*====================================================================*/
/*   @  */
/*       ################################
         ##                            ##
         ##                            ##
         ##                            ##
         ################################        */
void ioc_err_cnt_handle(uint8_t brd_no)
{
	if(SBP_Buf[brd_no].returnStat==SBP_RTSTUS_OK)
	{  if(ioc_err_cnt[brd_no] !=0)
		 { ioc_err_cnt[brd_no]--;
		 }	
	}
	else
	{ 	if(ioc_err_cnt[brd_no] < 0xE0)
			{	ioc_err_cnt[brd_no]+=0x08;
			}
	}		

  if(ioc_err_cnt[brd_no] > 0xD0) ioc_fail[brd_no]=1;
}
/*====================================================================*/
/*   @  */
/*       ################################
         ##                            ##
         ##                            ##
         ##                            ##
         ################################        */
void  ioc_fail_check(void)
{
	 if( (ioc_fail[B_OL]==1) && (ioc_fail[B_OR]==1) ) 
	 {   printp(send_c_ESCIA_dpb,">>>> BOTH O-Board fail, System DOWN! <<<<");
	     for(;;){}
	 }
   if( (ioc_fail[B_IL]==1) && (ioc_fail[B_IR]==1) ) 
	 {   printp(send_c_ESCIA_dpb,">>>> BOTH I-Board fail, System DOWN! <<<<");
	     for(;;){}
	 }
   if( (ioc_fail[B_CL]==1) && (ioc_fail[B_CR]==1) ) 
	 {   printp(send_c_ESCIA_dpb,">>>> BOTH C-Board fail, System DOWN! <<<<");
	     for(;;){}
	 }
}


/*====================================================================*/
/*   @  */
/*       ################################
         ##                            ##
         ##                            ##
         ##                            ##
         ################################        */
void mate_board_write(void)
{
	  	  Quene_SPIMaster_Send(B_MT,'w',Self_CB.lid,((uint8_t*)(&Self_CB)),sizeof(MBCtrlBlock_tag));

	  	  while(SBP_Buf[B_MT].isFinished == 0){}
	  	  SXC.CH[B_MT].SXC_CR.B.self_packet_done = 1;
	  	  set_temp_tick(10);
	  	  while((SXC.CH[B_MT].SXC_SR.B.both_packet_done ==0)&&(temp_tick)){} 
	  
	      if((SBP_debug_info[B_MT]&SBP_GENERAL_INFO_MASK)!=0) 
	      	    dump_SBP_lastPack(B_MT); 	  
	      if((SBP_debug_info[B_MT]&SXC_ENABLE_MASK) != 0 )           
	      	   SXC_master_verify(B_MT);	
	  
}

void general_ioc_board_read(uint8_t brd_no)
{
	  //if(ioc_fail[brd_no]==1)
	  //{
	  //	printp(send_c_ESCIA_dpb,"B%d[%s] dead\n\r",brd_no,board_name[brd_no]);
	  //}
  	//else
	  {	  Quene_SPIMaster_Read(brd_no,'r',Self_CB.lid);
	  	  while(SBP_Buf[brd_no].isFinished == 0){}
	  	  SXC.CH[brd_no].SXC_CR.B.self_packet_done = 1;
	  	  set_temp_tick(10);
	  	  while((SXC.CH[brd_no].SXC_SR.B.both_packet_done ==0)&&(temp_tick)){} //得加上超时机制

	      if((SBP_debug_info[brd_no]&SBP_GENERAL_INFO_MASK)!=0) 
	      	    dump_SBP_lastPack(brd_no); 
	      if((SBP_debug_info[brd_no]&SXC_ENABLE_MASK) != 0 )           
	      	   SXC_master_verify(brd_no);	
	      ioc_err_cnt_handle(brd_no);
    }
     
}

void general_ioc_board_write(uint8_t brd_no,uint8_t* buf,uint8_t len)
{
	  //if(ioc_fail[brd_no]==1)
	  //{
	  //	printp(send_c_ESCIA_dpb,"B%d[%s] dead\n\r",brd_no,board_name[brd_no]);
	  //}
	  //else
	  {	  Quene_SPIMaster_Send(brd_no,'w',Self_CB.lid,buf,len);
	  	  while(SBP_Buf[brd_no].isFinished == 0){}
	  	  SXC.CH[brd_no].SXC_CR.B.self_packet_done = 1;
	  	  set_temp_tick(10);
	  	  while((SXC.CH[brd_no].SXC_SR.B.both_packet_done ==0)&&(temp_tick)){} //得加上超时机制
	  	  
	      if((SBP_debug_info[brd_no]&SBP_GENERAL_INFO_MASK)!=0)
	      	   	dump_SBP_lastPack(brd_no); 
	      if((SBP_debug_info[brd_no]&SXC_ENABLE_MASK) != 0 ) 
	      	    SXC_master_verify(brd_no);	
	      ioc_err_cnt_handle(brd_no);
    }	
}

      
      
      
/*====================================================================*/
/*   @  */
/*       ################################
         ##                            ##
         ##                            ##
         ##                            ##
         ################################        */
void init_GloVar(void)
{    uint32_t i;
   
     init_dpb(&ESCIA_DP_BUF,ESCIA_DP_BUF_MEM,ESCIA_DP_BUF_LEN);
     init_dpb(&SYS_ERROR_INFO_BUF,SYS_ERROR_INFO_BUF_MEM,SYS_ERROR_INFO_BUF_LEN);

     for(i=0;i<8;i++)
     {  init_SBP_Buf(i);
     	  SBP_debug_info[i]  = /* SBP_GENERAL_INFO_MASK | */ SXC_ENABLE_MASK;   //
     	  SPI_LED_Refresh[i] = 0;
     }
     
     SBP_Buf[7].isFinished = 0;  //SBP_Buf[7] is MF-SPI, slave mode
     sys_tick = 0;
     temp_tick = 0;
     
     for(i=0;i<6;i++)
     { ioc_err_cnt[i] = 0;
     	 ioc_fail[i] = 0;
     }

     init_MBCtrlBlock(&Self_CB);
     init_MBCtrlBlock(&Mate_CB);
}
/*====================================================================*/
/*   @  */
/*       ################################
         ##                            ##
         ##                            ##
         ##                            ##
         ################################        */
void refresh_SPI_Indicate_LED(void)
{    uint8_t i;
	   uint8_t led_array[8]={PIN_EMIOS21,PIN_EMIOS19,PIN_EMIOS17,
	   	                     PIN_EMIOS20,PIN_EMIOS18,PIN_EMIOS16,
	   	                     PIN_EMIOS23,PIN_EMIOS22};

   	 for(i=0;i<8;i++)
	   {  
	   	  if(SPI_LED_Refresh[i]) set_led(led_array[i],SSP_MB_LED_TOGGLE);
   	    else                   set_led(led_array[i],SSP_MB_LED_OFF); 	
	   	  SPI_LED_Refresh[i] = 0;	
	   }
	}
/*====================================================================*/
/*   @  */
/*       ################################
         ##                            ##
         ##                            ##
         ##                            ##
         ################################        */
//-------------------------------------
void init_MBCtrlBlock(MBCtrlBlock_tag* pt)
{
	pt -> role    = role_voting;
  pt -> alive   = 1;
  pt -> ssp_loop_start_flag  = 0;
  pt -> unused  = 0xA5;
	pt -> lid     = 0;//SXC.Test_Reg.B.B;
}
/******************************************/
/* @1                                     */
/*                                        */
/******************************************/
void dump_SBP_lastPack(uint8_t brd_no)
{   uint32_t i,j;
    uint8_t  returnStat;
    
    
    
    returnStat = SBP_Buf[brd_no].returnStat;
    
      printp(send_c_ESCIA_dpb,"B%d[%s]:",brd_no,board_name[brd_no]); 
      //Rx  -------
      if(SBP_Buf[brd_no].amIWriteData == 0)  //0 - Master Read
      {
        if(returnStat==SBP_RTSTUS_OK) printp(send_c_ESCIA_dpb,"Rx SBP_RTSTUS_OK["); 
        else               printp(send_c_ESCIA_dpb,"Rx ERROR %2d[",returnStat);   
        j= SBP_Buf[brd_no].iRxBufferLen;
        for(i=0;i<j;i++)  printp(send_c_ESCIA_dpb," %02X",SBP_Buf[brd_no].RxBuffer[i]);
        printp(send_c_ESCIA_dpb,"]\n\r");
      }
      //Tx -----
      else
      {
        if(returnStat==SBP_RTSTUS_OK)
        {	  printp(send_c_ESCIA_dpb,"Tx SBP_RTSTUS_OK["); 
		        j= SBP_Buf[brd_no].iTxBufferLen;
		        for(i=0;i<j;i++)  printp(send_c_ESCIA_dpb," %02X",SBP_Buf[brd_no].TxBuffer[i]);
		        printp(send_c_ESCIA_dpb,"]\n\r");
        }
        else
        	  printp(send_c_ESCIA_dpb,"Tx ERROR %2d\n\r",returnStat); 
      }    
}


/******************************************/
/* @1                                     */
/*                                        */
/******************************************/
void Quene_SPIMaster_Read(uint8_t brd_no,
                    uint8_t command,
                    uint16_t LID)
{
  uint32_t i;
  init_SBP_Buf(brd_no);
  SBP_Buf[brd_no].amIWriteData = 0;
  SBP_Buf[brd_no].InstrBuffer[0] = ESC;
  SBP_Buf[brd_no].InstrBuffer[1] = ESC;
  SBP_Buf[brd_no].InstrBuffer[2] = ESC;   
  SBP_Buf[brd_no].InstrBuffer[3] = 'F';  
  SBP_Buf[brd_no].InstrBuffer[4] = command;
  SBP_Buf[brd_no].InstrBuffer[5] = ((LID&0x3F80)>>7);
  SBP_Buf[brd_no].InstrBuffer[6] = ( LID&0x7F);
  SBP_Buf[brd_no].InstrBuffer[7] = 'B';  
  SBP_Buf[brd_no].iRxBufferLen   = 0;
  SBP_Buf[brd_no].isFinished     = 0;    //acctivate
  SXC.CH[brd_no].SXC_CR.B.PACKET_START   = 1;
}

/******************************************/
/* @1                                     */
/*                                        */
/******************************************/
void Quene_SPIMaster_Send(uint8_t brd_no,
                    uint8_t command,
                    uint16_t LID,
                    uint8_t* buf,
                    uint32_t len)
{
  uint32_t i;
  init_SBP_Buf(brd_no);
  SBP_Buf[brd_no].amIWriteData = 1;
  SBP_Buf[brd_no].InstrBuffer[0] = ESC;
  SBP_Buf[brd_no].InstrBuffer[1] = ESC;
  SBP_Buf[brd_no].InstrBuffer[2] = ESC;   
  SBP_Buf[brd_no].InstrBuffer[3] = 'F';  
  SBP_Buf[brd_no].InstrBuffer[4] = command;
  SBP_Buf[brd_no].InstrBuffer[5] = ((LID&0x3F80)>>7);
  SBP_Buf[brd_no].InstrBuffer[6] =  LID&0x7F;
  SBP_Buf[brd_no].InstrBuffer[7] = 'B';   
  for(i=0;i<len;i++)  
  SBP_Buf[brd_no].TxBuffer[i]    = buf[i];
  SBP_Buf[brd_no].iTxBufferLen   = len;
  SBP_Buf[brd_no].isFinished     = 0;    //acctivate
  SXC.CH[brd_no].SXC_CR.B.PACKET_START   = 1;
} 

/*====================================================================*/
/*   @  */
/*       ################################
         ##                            ##
         ##        console             ##
         ##                            ##
         ################################        */
const uint8_t menu[] ={
"\
d:debug control\n\r\
i:SXC info\n\r\
s:read MCB\n\r\
V:Version\n\r\
Q:Quit\n\r\
"};
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
uint8_t command_line_interface()
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
{
  uint8_t       cmd;

  uint32_t      i,j;
  uint8_t       read_u8t_2;
  uint8_t       read_u8t;
  uint32_t      read_u32t;
  unsigned long read_long;

  printp(send_c_ESCIA_dpb,"\n\r%s",menu);
  while(1)
  {
      printp(send_c_ESCIA_dpb,"\n\rCMD-> ");
      cmd = get_c_ESCI_A();
      printp(send_c_ESCIA_dpb,"%c",cmd);
      //---------------------------------
      switch(cmd)
      {  case '@':  j=SYS_ERROR_INFO_BUF.n;
                   printp(send_c_ESCIA_dpb,"\n\r");
                   if(j==0)  printp(send_c_ESCIA_dpb,"     All runs fines");
                   for(i=0;i<j;i++)
                   {    pop_dpb_data(&SYS_ERROR_INFO_BUF,&read_u8t);
                        printp(send_c_ESCIA_dpb,"%2X ",read_u8t);
                        if((i & 0x0F) == 0x0F) printp(send_c_ESCIA_dpb,"\n\r");
                   }
                   break;
        case 'd':  printp(send_c_ESCIA_dpb,"\n\r which board(0-7) :  ");
                   read_long = TermGetNum();
                   read_u8t = (uint8_t)(read_long&0x07);
                   if(read_u8t > 7)  {printp(send_c_ESCIA_dpb,"\n\r8 boards only"); break;}
                   printp(send_c_ESCIA_dpb,"\n\r which level [%d]:  ",SBP_debug_info[read_u8t]);
                   read_long  = TermGetNum();
                   read_u8t_2 = (uint8_t)(read_long);	
        	         SBP_debug_info[read_u8t] = read_u8t_2;
        	         break;
        case 'i':
        	         printp(send_c_ESCIA_dpb,"\n\rchannel(0-7) :  ");
                   read_long = TermGetNum();
                   read_u8t = (uint8_t)(read_long&0x07);
                   printp(send_c_ESCIA_dpb,"\n\r");
	                 dump_SXC_CH_info(read_u8t);
        	         break;
        case 's':  printp(send_c_ESCIA_dpb,"\n\rSelf: %d(LID %d)",Self_CB.role,Self_CB.lid);
                   printp(send_c_ESCIA_dpb,"\n\rMate: %d(LID %d)",Mate_CB.role,Mate_CB.lid);
                   break;
        case '/':  printp(send_c_ESCIA_dpb,"\n\rhow many 1ms [%d] ?",ssp_delay_period);
                   read_long = TermGetNum();
                   ssp_delay_period = (uint16_t)(read_long&0xFFFF);
                   break;
        case 'v':
        case 'V':  printp(send_c_ESCIA_dpb,"\n\rBuild@  %s %s",__DATE__,__TIME__);
        	         break; 	         
        case '?':
        case 'h':
        case 'H':  printp(send_c_ESCIA_dpb,"\n\r%s",menu);
        	         break;
        case 'q':
        case 'Q':  printp(send_c_ESCIA_dpb,"\n\r");
        	         set_sys_tick(200);
        	         Mate_CB.alive = 0;
        	         Mate_CB.ssp_loop_start_flag=0;  
                   while((Mate_CB.ssp_loop_start_flag==0)&&(sys_tick)){}
        	         if(Mate_CB.alive && (Mate_CB.role == role_president))
        	         {	Self_CB.role = role_visepresident; 
        	         }
        	         else
        	         {  Self_CB.role = role_president; 
        	         }
                   return(0);
        default:   printp(send_c_ESCIA_dpb,"  What?");  break;
      }//switch
   }
}
/**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/**************************************************************************/













⌨️ 快捷键说明

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