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

📄 smc1.c

📁 ads mpc860上smc的驱动程序
💻 C
字号:
/*-------------------------------------------------------------------------
* FILENAME:  smc1.c
*
* DESCRIPTION:   
*
*   The code in this module provides echo capability on SMC1. It's 
*   intent is to provide the beginnings of a debug port that is 
*   mostly compiler independent. If an ASCII terminal is connected
*   at 9600,N,8,1 on Port 2 (PB3) on the ADS board with no hardware
*   control, characters typed on the keyboard will be received by
*   SMC1 and echoed back out the RS232 port to the ASCII terminal.
*   This function was designed and tested on an ADS860 
*   development board. Note that if a different baud rate is 
*   required, there is a header file on the netcomm website under 
*   the General Software category that is labelled "Asynchronous
*   Baud Rate Tables".
*
* REFERENCES:
*
*   1) MPC860 Users Manual
*   2) PowerPC Microprocessor Family: The Programming Environments for 
*      32-Bit Microprocessors
*
* HISTORY:
*
* 27 APR 98  jay    initial release
*
*-------------------------------------------------------------------------*/

#include "netcomm.h"
#include "mpc860.h"
#include "masks860.h"
#include "smc1.h"

int stack[100]=0;
int stackTop;

/***********************/
/* Global Declarations */
/***********************/

EPPC  *IMMR;      /* IMMR base pointer */
BDRINGS *RxTxBD;  /* buffer descriptors base pointer */
LB *SMC1Buffers;  /* SMC1 base pointers */

/*---------------------*/
/* Function Prototypes */
/*---------------------*/

void  startup(void);
void  SMC1Init(void);
void  SMC1PutChar(UBYTE);
UBYTE SMC1GetChar(void);
UBYTE SMC1Poll(void);
void  main(void);
void  InitBDs(void);
void  EchoChar(void);



void startup(void)
{
	 asm("     	.global	_start ");
	 asm("_start:	 ");
	 asm("		addis   r1,r0,stackTop@h");
	 asm("		ori     r1,r1,stackTop@l");

  main();
}

/*-----------------------------------------------------------------------------
*
* FUNCTION NAME:  main 
*
* DESCRIPTION:
*
*  This is the main function for the SMC1 code.
*
* EXTERNAL EFFECT: 
*                 
* PARAMETERS:  None
*
* RETURNS: None
*
*---------------------------------------------------------------------------*/

void main(void)

{

   /*------------------------*/
   /* Establish IMMR pointer */
   /*------------------------*/
      
   IMMR = (EPPC *)(GetIMMR() & 0xFFFF0000);  /* MPC8xx internal register
                                                map  */

   /*--------------------------------------------------------------------*/
   /* We add 64 bytes to the start of the buffer descriptors because     */
   /* this code was also tested on the monitor version of SDS debugger.  */
   /* The monitor program on our target uses most of the first 64 bytes  */
   /* for buffer descriptors. If you are not using the SDS monitor  with */
   /* Motorola's ADS development board, you can delete 64 below and      */
   /* start at the beginning of this particular block of Dual Port RAM.  */
   /*--------------------------------------------------------------------*/

   /*----------------------------------*/  
   /* Get pointer to BD area on DPRAM */
   /*----------------------------------*/  
      
   RxTxBD = (BDRINGS *)(IMMR->qcp_or_ud.ud.udata_bd + 64);  

   /*-------------------------------------------------------------------*/
   /* Establish the buffer pool in Dual Port RAM. We do this because the*/
   /* pool size is only 2 bytes (1 for Rx and 1 for Tx) and to avoid    */
   /* disabling data cache for the memory region where BufferPool would */
   /* reside. The CPM does not recognize data in buffer pools once it   */
   /* been cached. It's acesses are direct through DMA to external      */
   /* memory.                                                           */
   /*-------------------------------------------------------------------*/

   SMC1Buffers = (LB *)(IMMR->qcp_or_ud.ud.udata_bd + 80);

   /*----------------------------------------*/
   /* Initialize SMC1 and buffer descriptors */
   /*----------------------------------------*/

   SMC1Init();

   while (1)

   {
      /*--------------------------------------------------*/
      /* if there is a receive character echo it back out */
      /*--------------------------------------------------*/

      if (SMC1Poll())   /* Check BD status for Rx characters */

         EchoChar();
   }

} /* END main */



/*-----------------------------------------------------------------------------
*
* FUNCTION NAME:  SMC1Init 
*
* DESCRIPTION:
*
*  Initialize SMC1 as a UART at 9600 Baud 
*  with 24MHZ CPU clock.
*
* EXTERNAL EFFECT: 
*                 
* PARAMETERS:  None
*
* RETURNS: None
*
*---------------------------------------------------------------------------*/

void SMC1Init(void)

{

unsigned long *bcsr1; 
   
	/*-----------------------*/
	/* Allow SMC1 TX, RX out */
	/*-----------------------*/

   IMMR->pip_pbpar |= (0x00C0);     
   IMMR->pip_pbdir &= 0xFF7F; //pbpar设置端口线的传输模式:0,通用I/O;1,外设专用;                              //pbdir设置端口线的传输方式:0,输入;1,输出。

   /*------------------------------------------------*/
   /* Set Baud Rate to 9600 for 24MHz System Clock.  */
   /* Enable BRG Count.									     */
   /*------------------------------------------------*/

   IMMR->brgc1 = (0x0013E | 0x10000);//看不懂什么意思 

   IMMR->si_simode &= ~(0x0000F000);    /* SMC1:  NMSI mode */
   IMMR->si_simode |= 0x00000000;       /* SMC1:  Tx/Rx Clocks are BRG1 */


   /*----------------------------------------*/
	/* Set RXBD table start at Dual Port +800 */
	/*----------------------------------------*/

   IMMR->PRAM[PAGE3].pg.other.smc_dsp1.psmc1.u1.rbase = 	//PAGE4 is used by scc4,smc2. PAGE3 is used by scc3,smc1.
    
   (UHWORD)&RxTxBD->RxBD;

   /*----------------------------------------*/
	/* Set TXBD table start at Dual Port +808 */
	/*----------------------------------------*/

   IMMR->PRAM[PAGE3].pg.other.smc_dsp1.psmc1.u1.tbase =
    
   (UHWORD)&RxTxBD->TxBD;	    	

   /*---------------------------------------*/
   /* Initialize Rx and Tx Params for SMC1: */
   /* Spin until cpcr flag is cleared		  */
   /*---------------------------------------*/

   for(IMMR->cp_cr = 0x0091; IMMR->cp_cr & 0x0001;) ;//cp_cr中的CH_HUM = 1101 指令在SMC1/DSP2/PIP 中运行
                                                     // CH_HUM = 1001 指令在SMC1/DSP1 中运行

   
	/*--------------------------------------*/
   /* Set RFCR,TFCR -- Rx,Tx Function Code */
   /* Normal Operation and Motorola byte   */
   /* ordering                             */
	/*--------------------------------------*/

   IMMR->PRAM[PAGE3].pg.other.smc_dsp1.psmc1.u1.rfcr = 0x18; //big-endian 数据 
                                                               

   IMMR->PRAM[PAGE3].pg.other.smc_dsp1.psmc1.u1.tfcr = 0x18;  

   /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
   /* Protocol Specific Parameters */
   /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

   /*---------------------------*/
   /* MRBLR = MAX buffer length */
   /*---------------------------*/

   IMMR->PRAM[PAGE3].pg.other.smc_dsp1.psmc1.u1.mrblr = 1;    

   /*------------------------------------*/
   /* MAX_IDL = Disable MAX Idle Feature */
   /*------------------------------------*/

   IMMR->PRAM[PAGE3].pg.other.smc_dsp1.psmc1.u1.max_idl = 0;  

   /*-------------------------------------*/
   /* BRKEC = No break condition occurred */
   /*-------------------------------------*/

   IMMR->PRAM[PAGE3].pg.other.smc_dsp1.psmc1.u1.brkec = 0;    

   /*---------------------------------------*/
   /* BRKCR = 1 break char sent on top XMIT */
   /*---------------------------------------*/

   IMMR->PRAM[PAGE3].pg.other.smc_dsp1.psmc1.u1.brkcr = 1;    


   /*--------------------*/
   /* Initialize the BDs */
   /*--------------------*/

   InitBDs();

   IMMR->smc_regs[SMC1_REG].smc_smce = 0xFF;  /* Clear any pending events */

   /*--------------------------------------------------*/
   /* SMC_SMCM = Mask all interrupts, use polling mode */
   /*--------------------------------------------------*/

   IMMR->smc_regs[SMC1_REG].smc_smcm = 0;     

   IMMR->cpmi_cicr = 0;           /* Disable all CPM interrups */
   IMMR->cpmi_cipr = 0xFFFFFFFF;  /* Clear all pending interrupt events */
   IMMR->cpmi_cimr = 0;           /* Mask all event interrupts */

   /*-----------------------------------------------*/
   /* Enable RS232 interface on ADS board via BCSR1 */
   /* Get the base address of BCSR                  */ 
   /*-----------------------------------------------*/

   bcsr1 = (UWORD *) ((IMMR->memc_br1 & 0xFFFFFFFE) + 4); //看不懂   
   *bcsr1 &= 0xFFFB0000;  /* Assert the RS232EN* bit */

   /*------------------------------------*/
   /* 8-bit mode,  no parity, 1 stop-bit */
   /* UART SMC Mode							  */
   /* Normal operation (no loopback),	  */
   /* SMC Transmitter/Receiver Enabled	  */
   /*------------------------------------*/

   IMMR->smc_regs[SMC1_REG].smc_smcmr = 0x4823;//SMC模式寄存器,包括字符长度、协议、诊断模式、发送和接收使能 

} /* END SMC1Init */



/*-----------------------------------------------------------------------------
*
* FUNCTION NAME:  InitBDs
*
* DESCRIPTION: This function initializes the Tx and Rx Buffer Descriptors.
*
* EXTERNAL EFFECT: RxTxBD
*                 
* PARAMETERS:  None
*
* RETURNS: None
*
*---------------------------------------------------------------------------*/

void InitBDs(void)

{
   /*-----------------------------------*/
   /* Setup Receiver Buffer Descriptors */
   /*-----------------------------------*/
   
   RxTxBD->RxBD.bd_cstatus = 0xA000;  //和mpc850 的RxBD不一样 E=1 ,W=1        /* Enable, Last BD */
   RxTxBD->RxBD.bd_length = 1;
   RxTxBD->RxBD.bd_addr = &(SMC1Buffers->RxBuffer);


   /*--------------------------------------*/
   /* Setup Transmitter Buffer Descriptors */
   /*--------------------------------------*/

   RxTxBD->TxBD.bd_cstatus = 0x2000;//和mpc850 的TxBD不一样 W=1    /* Buffer not yet ready; Last BD */
   RxTxBD->TxBD.bd_length = 1;
   RxTxBD->TxBD.bd_addr = &(SMC1Buffers->TxBuffer);


} /* END InitBDs */



/*-----------------------------------------------------------------------------
*
* FUNCTION NAME:  EchoChar
*
* DESCRIPTION: This function facilitates the echoing of a received character.
*
* EXTERNAL EFFECT: RxTxBD
*                 
* PARAMETERS:  None
*
* RETURNS: None
*
*---------------------------------------------------------------------------*/

void  EchoChar(void)

{

    UBYTE mych;

   mych = SMC1GetChar();
   SMC1PutChar(mych); 

} /* end EchoChar */


/*-----------------------------------------------------------------------------
*
* FUNCTION NAME:  SMC1PutChar
*
* DESCRIPTION: Output a character to SMC1
*
* EXTERNAL EFFECT: RxTxBD
*                 
* PARAMETERS:  ch - input character
*
* RETURNS: None
*
*---------------------------------------------------------------------------*/

void SMC1PutChar(UBYTE ch)

{
   /*-----------------------------------*/
	/* Loop until transmission completed */
	/*-----------------------------------*/
   
   while (RxTxBD->TxBD.bd_cstatus  &  0x8000);    
   
   /*------------*/ 
	/* Store data */
	/*------------*/

   *(RxTxBD->TxBD.bd_addr) = ch;                  
   RxTxBD->TxBD.bd_length = 1;

   /*---------------*/
   /* Set Ready bit */
 	/*---------------*/

   RxTxBD->TxBD.bd_cstatus |= 0x8000;              


} /* END SMC1PutChar */



/*-----------------------------------------------------------------------------
*
* FUNCTION NAME:  SMC1GetChar
*
* DESCRIPTION: Get a character from SMC1
*
* EXTERNAL EFFECT: RxTxBD
*                 
* PARAMETERS:  NONE
*
* RETURNS: A character from SMC1
*
*---------------------------------------------------------------------------*/

UBYTE SMC1GetChar(void)

{

UBYTE ch;   /* output character from SMC1 */
   
	/*--------------------*/
	/* Loop if RxBD empty */
	/*--------------------*/

   while (RxTxBD->RxBD.bd_cstatus  &  0x8000);      

	/*--------------*/
   /* Receive data */
	/*--------------*/

   ch = *(RxTxBD->RxBD.bd_addr);   
                   
	/*----------------------*/
   /* Set Buffer Empty bit */
	/*----------------------*/

   RxTxBD->RxBD.bd_cstatus |= 0x8000;   

   return ch;

} /* END SMC1GetChar */



/*-----------------------------------------------------------------------------
*
* FUNCTION NAME:  SMC1Poll
*
* DESCRIPTION: Poll SMC1 RxBD and check to see if a character was received
*
* EXTERNAL EFFECT: NONE
*                 
* PARAMETERS:  NONE
*
* RETURNS: A one if there is a character available in the receive buffer,
*          else zero.
*
*---------------------------------------------------------------------------*/

UBYTE SMC1Poll(void)

{
      
   if(RxTxBD->RxBD.bd_cstatus & 0x8000) 
   
   {
       return 0;  /*  character NOT available */
   } 
   
   else
   
   {
       return 1;  /*  character IS available */
   }

} /* END SMC1Poll */



/*-----------------------------------------------------------------------------
*
* FUNCTION NAME:  GetIMMR
*
* DESCRIPTION: Returns current value in the IMMR register.
*
* EXTERNAL EFFECT: NONE
*                 
* PARAMETERS:  NONE
*
* RETURNS: The IMMR value in r3. The compiler uses r3 as the register 
*          containing the return value.
*
*---------------------------------------------------------------------------*/

GetIMMR()

{
    asm(" mfspr  r3,638 ");        /* IMMR is spr #638 */
}



⌨️ 快捷键说明

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