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

📄 hdlc.c

📁 这是单板上DPRAM的驱动程序
💻 C
字号:
#include <string.h>
#include "board.h"
#include "sdev.h"
#include "hdlc.h"

#ifdef param
#undef param
#endif
#define param(ch) (pda->pram[ch].scc.pscc.h)
#ifdef regs
#undef regs
#endif
#define regs(ch) (pda->Scc_regs[ch])

static void HdlcStopOp(HdlcCfgStruct *pCfg)
{
    PDA  *pda=(PDA *)(GetIMMR() & IO_MAP_MASK);
	CPM_CMD(GRACEFUL_STOP|((pCfg->ch)<<6));
    regs(pCfg->ch).scc_gsmra &= ~0x00000030;/*disable tx_rx*/
}

static void HdlcStartOp(HdlcCfgStruct *pCfg)
{
    PDA  *pda=(PDA *)(GetIMMR() & IO_MAP_MASK);
	CPM_CMD(INIT_RX_TX_PARAMS|((pCfg->ch)<<6));
    regs(pCfg->ch).scc_gsmra |= 0x00000030;/* enable tx&rx */
}

static void HdlcClearBuf(DataBufStruct *pData)
{
    int i;
    pData->CurRecvBD=pData->CurSendBD=0; 	
    for(i=0;i<pData->MaxRecvBD;i++)
	pData->pBaseRecvBD[i].status = BD_EMPTY|BD_INTR;
    if(i>0)pData->pBaseRecvBD[i-1].status |= BD_WRAP;
    for(i=0;i<pData->MaxSendBD;i++)
	pData->pBaseSendBD[i].status = BD_INTR|BD_LAST|HDLC_TX_CRC;
    if(i>0)pData->pBaseSendBD[i-1].status |= BD_WRAP;
}

static void HdlcIsr(void* pDataBuf)
{
    USHORT starBD,tempstate;
    PDA  *pda=(PDA *)(GetIMMR() & IO_MAP_MASK);
    DataBufStruct*  pData=(DataBufStruct *)pDataBuf;
    HdlcCfgStruct*  pCfg =(HdlcCfgStruct *)(pData+1);

    USHORT Events = regs(pCfg->ch).scc_scce;
    regs(pCfg->ch).scc_scce = Events;

    if((Events & HDLC_RX_FRAME) || (Events & HDLC_LACK_OF_BUF )
                                || (Events & HDLC_RX_BUF))
    {
        pData->DrvInfo.TotalRecvInt++;
        if( (pData->RecvEv!=NULL) && (pData->RecvTid!=NULL) )
        {	
            ev_send(pData->RecvTid,pData->RecvEv);
        }
        if(pData->RecvQID!=NULL)
        {    
            q_send(pData->RecvQID,pData->RecvMessage); 
        }
    }
    if (Events & HDLC_TX_BUF)
    {
        pData->DrvInfo.TotalSendInt++;
        if( (pData->SendEv!=NULL) && (pData->SendTid!=NULL) )
        {
            ev_send(pData->SendTid,pData->SendEv);
        }
        if(pData->SendQID!=NULL)
        {    
            q_send(pData->SendQID,pData->SendMessage); 
        }
    }
}/* End of HdlcIsr */

void HdlcInit(void *pDataBuf, HdlcCfgStruct *cfg )
{
    int i;
    USHORT temp_ushort;
    BuffDescType   *temp_bufdes;
    char *pRecvBuf,*pSendBuf;
    PDA  *pda = (PDA *)(GetIMMR() & IO_MAP_MASK);
    DataBufStruct*  pData=(DataBufStruct *)pDataBuf;
    HdlcCfgStruct*  pCfg =(HdlcCfgStruct *)(pData+1);

/*-------------------------<< Initial data >>-------------------------------*/
    memmove(pCfg,cfg,sizeof(*cfg));
    memset(pData, 0, sizeof(DataBufStruct));
    pCfg->ch=(pCfg->ch&0x3);
    pCfg->Clk=(pCfg->Clk&0x7);
    pCfg->TxClk=(pCfg->TxClk&0x7);
    pCfg->LoopClk=(pCfg->LoopClk&0x7);
    pData->MaxRecvBD=pCfg->MaxRecvBD;
    pData->MaxSendBD=pCfg->MaxSendBD;
    pData->MaxBufLen=pCfg->MaxBufLen;
    pData->RecvErrMask=(HDLC_DPLL_ERR|HDLC_GREATER_LEN|HDLC_NOT_ALIGNED
        |HDLC_ABORT_SEQ|HDLC_RX_CRC|HDLC_OVERRUN|HDLC_CD_LOST);
    pData->SendErrMask=(HDLC_UNDERRUN|HDLC_CTS_LOST);
    pData->UncachedBuffer=pRecvBuf=pCfg->UncachedBuffer;
    pSendBuf=(char*)(pRecvBuf+( pData->MaxRecvBD)*(pData->MaxBufLen));

/*-------------------------<< stop SCCx operation >>----------------------*/
    HdlcStopOp(pCfg);
/*------------------------<< init TDM >>---------------------------------*/
    /* InitTDM(); has be moved to board.c */
	SPLX(
    switch(pCfg->UseTDM)
    {
        case HDLC_TDMA:
            pda->si_sicr  &= ~(0xFF<<(pCfg->ch*8));/* use TDMA *//* SPLX done */
            pda->si_sicr  |= (0x40<<(pCfg->ch*8));/* SPLX done */
            pda->si_sigmr |= 0x04;	/* enable TDMA *//* SPLX done */
            break;
        case HDLC_TDMB:
            pda->si_sicr  &= ~(0xFF<<(pCfg->ch*8));/* use TDMB *//* SPLX done */
            pda->si_sicr  |= (0x40<<(pCfg->ch*8));/* SPLX done */
            pda->si_sigmr |= 0x08;	/* enable TDMB *//* SPLX done */
            break;
        case HDLC_NMSI:
        default:
         	pda->si_sicr  &= ~(0xFF<<(pCfg->ch*8)); /*use NMSI *//* SPLX done */
            if(pCfg->DiffClk!=0)pda->si_sicr  |= /* SPLX done */
                 ((((ULONG)pCfg->Clk<<3)+(ULONG)pCfg->TxClk)<<(pCfg->ch*8)); 
            else pda->si_sicr  |= /* SPLX done */
                 ((((ULONG)pCfg->Clk<<3)+(ULONG)pCfg->Clk)<<(pCfg->ch*8)); 
            break;
    }) /* End of SPLX */

/*----------------------<< init SCCx >>-----------------------------------*/
    param(pCfg->ch).c_mask=0xf0b8;
    param(pCfg->ch).c_pres=0xffff;
    param(pCfg->ch).disfc=
    param(pCfg->ch).crcec=
    param(pCfg->ch).abtsc=
    param(pCfg->ch).nmarc=
    param(pCfg->ch).retrc=0;
    param(pCfg->ch).rfthr=1; 	
    param(pCfg->ch).mrblr=pData->MaxBufLen;
    param(pCfg->ch).mflr =pData->MaxBufLen;
    param(pCfg->ch).hmask=pCfg->AddrMask;
    param(pCfg->ch).haddr1=pCfg->Addr1;
    param(pCfg->ch).haddr2=pCfg->Addr2;
    param(pCfg->ch).haddr3=pCfg->Addr3;
    param(pCfg->ch).haddr4=pCfg->Addr4;
    param(pCfg->ch).rfcr=0x18;
    param(pCfg->ch).tfcr=0x18;
	
    /* rx */
    temp_ushort=(unsigned short)dpram_alloc(0,pData->MaxRecvBD*BD_LEN);
    param(pCfg->ch).rbase  = temp_ushort-0X2000;
    temp_bufdes=pData->pBaseRecvBD=(BuffDescType *)((char *)pda + temp_ushort);
    for(i=0;i<pData->MaxRecvBD;i++)
    {
        temp_bufdes[i].status=BD_EMPTY|BD_INTR;
        temp_bufdes[i].length=0;
        temp_bufdes[i].address=pRecvBuf+i*pData->MaxBufLen;
    }
    if(i>0)temp_bufdes[i-1].status|=BD_WRAP;

    /* tx */
    temp_ushort=(unsigned short)dpram_alloc(0,pData->MaxSendBD*BD_LEN);
    param(pCfg->ch).tbase = temp_ushort-0X2000;
    temp_bufdes=pData->pBaseSendBD=(BuffDescType *)((char *)pda+temp_ushort);
    for(i=0;i<pData->MaxSendBD;i++)
    {
        temp_bufdes[i].status=BD_INTR|BD_LAST|HDLC_TX_CRC;
        temp_bufdes[i].length=pData->MaxBufLen-2;
        temp_bufdes[i].address=pSendBuf+i*pData->MaxBufLen;
    }
    if(i>0)temp_bufdes[i-1].status|=BD_WRAP;

    regs(pCfg->ch).scc_gsmrb=(pCfg->BusMode==0)?02:80;
    regs(pCfg->ch).scc_gsmra=0;
    regs(pCfg->ch).scc_scce=0xffff;
    regs(pCfg->ch).scc_sccm=0x000e; /* RXF & TXB */
    SPLX(pda->cpmi_cimr|=(EN_SCC1 >> pCfg->ch);)/* enable scc interrutp */
    SPLX(pda->cpmi_cipr|=(CLR_SCC1 >> pCfg->ch);)/* clear cpm int pending reg */
    SPLX(pda->cpmi_cicr|=CPM_EN_INTS|0x1b0000;)/* enable CPM Interrupt */
    regs(pCfg->ch).scc_dsr  =0x7e7e;
    regs(pCfg->ch).scc_psmr = (pCfg->BusMode==0)?0x2200:0x2220;
    SPLX(CpmIsrAddHandler(V_SCC1-pCfg->ch,HdlcIsr,(void *)pData); )

    /* SPLX(pda->siu_simask |= 0X40;)*/
    /* SPLX(pda->siu_sipend |= 0X40;)*/
    /* asm(" mtspr  80, 0 ");   */ /* Enable EE Bit in MSR  */
         
    HdlcStartOp(pCfg);
}

int HdlcRead(void *pDataBuf,void *pBuf, int MaxLen)         
{   
    int ret=BDRead(pDataBuf,pBuf,MaxLen);
    if(ret>2)return ret-2;    /* sub 2 bytes of CRC */
    else if(ret>0)return SDE_INVALID_PACKET;  
    else return ret;
}

int HdlcWrite(void *pDataBuf,void *pBuf, int MaxLen)         
{   
    return BDWrite(pDataBuf,pBuf,MaxLen);
}

int HdlcCntrl(void *pDataBuf, int cmd, void *pParam, int maxlen)
{
    int i;
    ULONG temp_ulong;
    PDA *pda= (PDA *)(GetIMMR() & IO_MAP_MASK);
    DataBufStruct *pData=(DataBufStruct *)pDataBuf;
    HdlcCfgStruct *pCfg =(HdlcCfgStruct *)(pData+1);
    int ret=BDCntrl(pDataBuf, cmd, pParam, maxlen);
    if(ret!=SDE_UNKNOW_CMD)return ret;
    switch(cmd)
    {
        case SDC_REINIT:
            HdlcStopOp(pCfg);
            dpram_dealloc(0,(char*)pData->pBaseRecvBD,BD_LEN*pData->MaxRecvBD);
            dpram_dealloc(0,(char*)pData->pBaseSendBD,BD_LEN*pData->MaxSendBD);
            HdlcInit(pDataBuf,pCfg);
            HdlcStartOp(pCfg);          
            break;

        case SDC_CONFIG_CD:
            if(pParam==NULL || maxlen!=sizeof(USHORT)*6)
                return SDE_INVALID_ARG;
            pCfg->Clk=(*(USHORT*)pParam); 
	    pCfg->AddrMask=*(1+(USHORT*)pParam);
	    pCfg->Addr1=*(2+(USHORT*)pParam);
	    pCfg->Addr2=*(3+(USHORT*)pParam);
	    pCfg->Addr3=*(4+(USHORT*)pParam);
	    pCfg->Addr4=*(5+(USHORT*)pParam);
            HdlcStopOp(pCfg);
            dpram_dealloc(0,(char*)pData->pBaseRecvBD,BD_LEN*pData->MaxRecvBD);
            dpram_dealloc(0,(char*)pData->pBaseSendBD,BD_LEN*pData->MaxSendBD);
            HdlcInit(pDataBuf,pCfg);
            HdlcStartOp(pCfg);          
            break;

        case SDC_SET_LOOPBACK_MODE:
            HdlcStopOp(pCfg);

			SPLX(\
            temp_ulong = pda->si_sicr;
            temp_ulong &=(~(0xff<<(pCfg->ch*8)));
            if(pCfg->IsValidLoopClk==0)temp_ulong |= (0x1b<<(pCfg->ch*8));
            else  temp_ulong |= 
               ((((ULONG)pCfg->LoopClk<<3)+(ULONG)pCfg->LoopClk)<<(pCfg->ch*8));
            pda->si_sicr = temp_ulong;
			) /* End of splx */
            temp_ulong = regs(pCfg->ch).scc_gsmra;
            temp_ulong &= ~0x000000c0;
            temp_ulong |=  0x00000040; /* scc internal loopback mode */
            regs(pCfg->ch).scc_gsmra = temp_ulong;
            /*pda->si_simode |= 0x0c000000;  debug mode */
            pda->brgc4=(0x00010050); 

            HdlcClearBuf(pData);
            HdlcStartOp(pCfg);          
            break;

        case SDC_SET_TSA_LOOPBACK_MODE:
            if(pCfg->UseTDM==0)return SDE_UNKNOW_CMD;
            HdlcStopOp(pCfg);

			SPLX( \
            temp_ulong = pda->si_sicr;
            temp_ulong &=(~(0xffL<<(pCfg->ch*8)));
            temp_ulong |=(0x40L<<(pCfg->ch*8));
            pda->si_sicr = temp_ulong;
            if(pCfg->UseTDM==HDLC_TDMB)
                pda->si_simode |= 0x0c000000;/* debug mode */
            else if(pCfg->UseTDM==HDLC_TDMA)   
                pda->si_simode |= 0x00000c00;/* debug mode */
			) /* End of SPLX */
            HdlcClearBuf(pData);
            HdlcStartOp(pCfg);          
            break;

        case SDC_SET_NORMAL_MODE:
            HdlcStopOp(pCfg);

			SPLX( \
            temp_ulong = pda->si_sicr;
            temp_ulong &=(~(0xffL<<(pCfg->ch*8)));
            if(pCfg->UseTDM!=0)temp_ulong |=(0x40L<<(pCfg->ch*8));
            else if(pCfg->DiffClk!=0)temp_ulong |= 
                ((((ULONG)pCfg->Clk<<3)+(ULONG)pCfg->TxClk)<<(pCfg->ch*8)); 
            else temp_ulong |= 
                ((((ULONG)pCfg->Clk<<3)+(ULONG)pCfg->Clk)<<(pCfg->ch*8)); 
            pda->si_sicr = temp_ulong;
            if(pCfg->UseTDM==HDLC_TDMB)
                pda->si_simode &=~(0x0c000000);/* debug mode */
            else if(pCfg->UseTDM==HDLC_TDMA)   
                pda->si_simode &=~(0x00000c00);/* debug mode */
			) /* End of SPLX */
            temp_ulong = regs(pCfg->ch).scc_gsmra;
            temp_ulong &= ~0x000000c0L;
            regs(pCfg->ch).scc_gsmra = temp_ulong;
            
            HdlcClearBuf(pData);
            HdlcStartOp(pCfg);          
            break;

        case SDC_SET_TX_NO_CRC_MODE:
            HdlcStopOp(pCfg);
            HdlcClearBuf(pData);
            for(i=0;i<pData->MaxSendBD;i++)
                pData->pBaseSendBD[i].status &= (~HDLC_TX_CRC);
            HdlcStartOp(pCfg);          
            break;
            
        default: 
            return SDE_UNKNOW_CMD;
    }/* end of switch(cmd) */
    return 0;
}


char *HdlcBspInit(int DEV, char *FreeMemPtr, HdlcCfgStruct *cfg )
{
    int sz=(cfg->MaxRecvBD+cfg->MaxSendBD)*cfg->MaxBufLen;
    cfg->UncachedBuffer=AllocUncachedBuffer(sz,4);
    InstallSD(DEV, HdlcRead, HdlcWrite, HdlcCntrl, FreeMemPtr);
    HdlcInit(FreeMemPtr, cfg ); 
    FreeMemPtr += sizeof(DataBufStruct)+sizeof(HdlcCfgStruct);
    memcpy(FreeMemPtr,"*HdlcDat",8);
    FreeMemPtr += 8;
    return FreeMemPtr;
}

⌨️ 快捷键说明

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