#include "Scc1.h"
#include "CardMng.h"
#include "MasterOrSlave.h"
#include "Fhlog.h"
/*Scc1 用来和业务盘通信,512k 的HDLC 方式*/
unsigned int Scc1RecvIntCount=0;
unsigned int Scc1RecvCount=0;
unsigned int Scc1SendCount=0;
unsigned int SendToScc1QueueCount=0;
unsigned char Scc1TxBDIndex;
unsigned char Scc1RxBDIndex;
MSG_Q_ID Scc1MsgQId = NULL;
SEM_ID Scc1TxSem;
void InitScc1BD(void)
{
unsigned char i;
unsigned char *pBDBuffer = (unsigned char *)cacheDmaMalloc(2*SCC1_BD_NUM*SCC1_DATA_LEN);
for (i=0; i<SCC1_BD_NUM;i++)
{
RxTxBD->SCC1RxBD[i].pBuffer = pBDBuffer+i*SCC1_DATA_LEN;
RxTxBD->SCC1RxBD[i].DataLen = 0;
if(i != (SCC1_BD_NUM-1))
{
RxTxBD->SCC1RxBD[i].StaCtrl = 0x8000;
}
else
{
RxTxBD->SCC1RxBD[i].StaCtrl = 0xa000;
}
}
for (i=0; i<SCC1_BD_NUM; i++)
{
RxTxBD->SCC1TxBD[i].DataLen = 0;
RxTxBD->SCC1TxBD[i].pBuffer = pBDBuffer+(SCC1_BD_NUM+i)*SCC1_DATA_LEN;
if(i != (SCC1_BD_NUM-1))
{
RxTxBD->SCC1TxBD[i].StaCtrl = 0x0C00;
}
else
{
RxTxBD->SCC1TxBD[i].StaCtrl = 0x2C00;
}
}
Scc1TxBDIndex=0;
Scc1RxBDIndex=0;
}
void SCC1IntSrv(void)
{
unsigned short IntEvent;
unsigned char IntCount=0;
int key;
int errno;
/*Interrupt Suspend*/
immr->ic_sipnr_l |= 0x00800000;
key=intLock();
SetIntStatus(SCC1_INT);
IntEvent = immr->scc_regs[SCC1].scce;
/*-------------------------------------------*/
/* Clear SCC1 Event Register (by writing 1s) */
/*-------------------------------------------*/
immr->scc_regs[SCC1].scce=0xffff;
/*如果是收中断*/
if((IntEvent&HDLC_SCCE_RXF)==0x0008)
{
/*如果当前BD 表有数据,接收*/
/*2006.10.09 if --> while*/
while((RxTxBD->SCC1RxBD[Scc1RxBDIndex].StaCtrl&0x8000)==0x0000)
{
IntCount++;
if(IntCount>=2)
{
if(GetErrorLogSwitch() == ENABLE)
{
logMsg("scc1 Int while num=%d\n", IntCount);
}
}
Scc1RecvIntCount++;
if((RxTxBD->SCC1RxBD[Scc1RxBDIndex].DataLen-2) > 6)
{
errno = msgQSend(Scc1MsgQId, RxTxBD->SCC1RxBD[Scc1RxBDIndex].pBuffer, (RxTxBD->SCC1RxBD[Scc1RxBDIndex].DataLen-2), NO_WAIT, MSG_PRI_NORMAL);
if( errno== ERROR)
{
fhLog(FH_LOGMODULE_SCC1|FH_LOGLEVEL_ERROR,"Send to Scc1MsgQId Error errno-%d, msg num=%d,msg Len=%d\r\n", errnoGet(), msgQNumMsgs (Scc1MsgQId),(RxTxBD->SCC1RxBD[Scc1RxBDIndex].DataLen-2));
}
else
{
SendToScc1QueueCount++;
}
}
else
{
fhLog(FH_LOGMODULE_SCC1|FH_LOGLEVEL_ERROR,"Recv Short Frame! len=%d\r\n", (RxTxBD->SCC1RxBD[Scc1RxBDIndex].DataLen-2));
}
/*接收完成后,清空当前BD 表*/
if(Scc1RxBDIndex != (SCC1_BD_NUM-1))
{
RxTxBD->SCC1RxBD[Scc1RxBDIndex].StaCtrl = 0x8000;
Scc1RxBDIndex++;
}
else
{
RxTxBD->SCC1RxBD[Scc1RxBDIndex].StaCtrl = 0xa000;
Scc1RxBDIndex=0;
}
}
IntCount=0;
}
ClearIntStatus(SCC1_INT);
intUnlock(key);
}
void InitScc1Hardware(void)
{
unsigned int i=0;
/*设置SCC 的工作方式:HDLC, 初始化时*/
/*write gsmr_h1 23th 24th*/
/*
immr->scc_regs[SCC1].gsmr_h = 0x00000000;
*/
/*2006.11.21 for wangqing yiselie
#ifdef _YISELIE*/
/*以色列时序*/
immr->scc_regs[SCC1].gsmr_l |= 0x00000000;
/*PD31---SCC1 RX*/
immr->io_regs[PORT_D].ppar |= PPARD_31;
immr->io_regs[PORT_D].psor &= (~PSORD_31);
immr->io_regs[PORT_D].pdir &= (~PDIRD_31);
/*PD30---SCC1 TX*/
immr->io_regs[PORT_D].ppar |= PPARD_30;
immr->io_regs[PORT_D].psor |= PSORD_30;
immr->io_regs[PORT_D].pdir |= PDIRD_30;
/*PC29---SCC1 CLK---BRGC2*/
immr->io_regs[PORT_C].ppar |= PPARC_29;
immr->io_regs[PORT_C].psor &= (~PPARC_29);
immr->io_regs[PORT_C].pdir |= PPARC_29;
/* Enable BRG,User BRGCLK 1/660 of Main Frequency*/
immr->brgs_brgc2 = (0x00010000|((66)<<1));
/*
immr->brgs_brgc2 = (0x00010000|((660)<<1));
*/
/*sdc from 66 to 132 20060824*/
/*清除SCC1 原来的时钟配置*/
/*
immr->cpm_mux_cmxscr &=~(0x0000FF00);
*/
/*set scc1 CMXSCR R1CS and T1CS,the fourth step*/
immr->cpm_mux_cmxscr |= 0x09000000;
immr->cpm_mux_cmxscr &= 0x09ffffff;
/*Write RBASE and TBASE, the sixth and seventh step, according to mpc860*/
immr->pram.serials.scc_pram[SCC1].rbase = (UHWORD)((UWORD)&RxTxBD->SCC1RxBD[0]);
immr->pram.serials.scc_pram[SCC1].tbase = (UHWORD)((UWORD)&RxTxBD->SCC1TxBD[0]);
/*write RFCR TFCR the ninth step*/
immr->pram.serials.scc_pram[SCC1].rfcr = 0x10;
immr->pram.serials.scc_pram[SCC1].tfcr = 0x10;
/*write MRBLR the tenth step*/
immr->pram.serials.scc_pram[SCC1].mrblr = SCC1_DATA_LEN; /*should be defined*/
/*write C_mask and C_pres 11th and 12th*/
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.c_mask = HDLC_C_MASK;
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.c_pres = HDLC_C_PRES;
/*clear DISFC CRCEC ABTSC NMARC RETRC 13th*/
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.disfc = 0x0000;
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.crcec = 0x0000;
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.abtsc = 0x0000;
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.nmarc = 0x0000;
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.retrc = 0x0000;
/*write MFLR 14th 15th 16th*/
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.mflr = SCC1_DATA_LEN;
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.rfthr = 0x0001;
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.hmask = 0x0000;
/*clear HADDR1-HADDR4 17th*/
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.haddr1 = 0x0000;
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.haddr2 = 0x0000;
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.haddr3 = 0x0000;
immr->pram.serials.scc_pram[SCC1].SpecificProtocol.h.haddr4 = 0x0000;
/*initialize RXBD AND TXBD 18th 19th*/
/*write SCCE 20th*/
immr->scc_regs[SCC1].scce = 0xffff;
/*write SCCM 21th*/
immr->scc_regs[SCC1].sccm = 0x0008;
/*write siu's simr_l and sipnr_l 22th*/
immr->ic_simr_l |= 0x00800000;
/*
immr->ic_sipnr_l = 0xffffffff;
*/
/*write PSMR1 25th*/
immr->scc_regs[SCC1].psmr = HDLC_PSMR_NOF_0;
/*set cpcr for scc1 the eighth step*/
while ((immr->cpm_cpcr & CPCR_FLG) != READY_TO_RX_CMD);
/*-------------------------------------*/
/* ISSUE INIT TX/RX PARAMETERS COMMAND */
/*-------------------------------------*/
immr->cpm_cpcr = CPCR_INIT_TX_RX_PARAMS |
SCC1_PAGE_SUBBLOCK |
CPCR_FLG;
while ((immr->cpm_cpcr & CPCR_FLG) != READY_TO_RX_CMD);
/*初始化中断向量*/
intConnect(INUM_TO_IVEC(INUM_SCC1), (VOIDFUNCPTR)SCC1IntSrv, 0);
/*write GSMR_L1 26th*/
immr->scc_regs[SCC1].gsmr_l |= GSMR_L1_ENT|GSMR_L1_ENR/*|GSMR_L1_INT_LB*/;
}
void Scc1Send(unsigned char *pData, unsigned short Len)
{
unsigned char byCnt;
if(GetCardMasterOrSlave() != SYS_MASTER)
{
return;
}
if(Len>MAX_DATA_LEN)
{
if(GetScc1LogSwitch() == ENABLE)
{
printf("The packet is too long %d\n",Len);
}
return ;
}
if(GetScc1PollingLogSwitch() == ENABLE)
{
if((*(pData+1)==(CARD_TYPE_NOCARD>>8))
&&(*(pData+2)==(CARD_TYPE_NOCARD&0xff)))
{
printf("Scc1 Send Enable:\r\n");
for(byCnt=0;byCnt<Len;byCnt++)
{
printf("%3x,",*(pData+byCnt));
}
printf("\r\n");
}
}
if(GetScc1LogSwitch() == ENABLE)
{
if((*(pData+1)!=(CARD_TYPE_NOCARD>>8))
&&(*(pData+2)!=(CARD_TYPE_NOCARD&0xff)))
{
printf("Scc1Send:\r\n");
for(byCnt=0;byCnt<Len;byCnt++)
{
printf("%3x,",*(pData+byCnt));
}
printf("\r\n");
}
}
semTake(Scc1TxSem, WAIT_FOREVER);
if((RxTxBD->SCC1TxBD[Scc1TxBDIndex].StaCtrl&0x8000)==0x0000)
{
memcpy(RxTxBD->SCC1TxBD[Scc1TxBDIndex].pBuffer, pData, Len);
Scc1SendCount++;
RxTxBD->SCC1TxBD[Scc1TxBDIndex].DataLen=Len;
RxTxBD->SCC