cdc.c

来自「一款车载DVD的车机源程序(正在生成中的哦)」· C语言 代码 · 共 849 行 · 第 1/2 页

C
849
字号
     		case C_RANOFF:
     		case C_SCNOFF:
        		CtxBuffer[1] = 0x40;
        		break;
     		case C_RANDOM:
        		CtxBuffer[1] = 0x40;
        		CtxBuffer[2] = 0x20;            /* shuffle */
        		break;
     		case C_DSCRAN:
        		CtxBuffer[1] = 0x40;
        		CtxBuffer[2] = 0x10;            /* disc shuffle */
        		break;
     		case C_SCAN:  
        		CtxBuffer[1] = 0x40;
        		CtxBuffer[2] = 0x80;            /* scan */
        		break;
     		case C_DSCSCN:
        		CtxBuffer[1] = 0x40;
        		CtxBuffer[2] = 0x04;            /* disc scan */
        		break;
  	}
  	ctxcntr = 12;                         /* 12 bits */  
  	iCdc = CtxBuffer[0] & 0x0F;	//Check the OP code then can find out the bits
  	if ((iCdc == 0x01) || (iCdc == 0x02))
  		{
     		ctxcntr = 32;	//The OP code is 0001 or 0010,so the bit is 32 bits
     		if ((CtxBuffer[1] & 0xF0) == 0x10) //Check the Address then can find out the bits 
     			ctxcntr = 24;	//The Address is 0001,so the command memory is "CD control command",so the bit is 24 bits     
  		}
  	jCdc = 0;                             /* make parity */
  	for (iCdc = 0; iCdc < (ctxcntr/4)-1; iCdc++)
  		{
     		if (iCdc % 2)  
     			jCdc ^= (CtxBuffer[iCdc/2] & 0x0F);
     		else  
     			jCdc ^= ((CtxBuffer[iCdc/2] >> 4) & 0x0F);
     		jCdc &= 0x0F;
  		}
  	jCdc = (jCdc +1) & 0x0F;              /* parity nibble */
  	iCdc = ctxcntr / 8;
  	if (ctxcntr % 8)
  		{
     		CtxBuffer[iCdc]  = 0;
     		CtxBuffer[iCdc] += (jCdc << 4);
  		}
  	else
  		{
     		CtxBuffer[iCdc-1] &= 0xF0;
     		CtxBuffer[iCdc-1] += jCdc;
  		}  

  	ctbit = 0;
  	ctbyte = 0;
  	CtxState = CT_LOW;
  	PIN_CdcOut();
  	SetCdcFlg(RdyCtx);
}


/*--------------------------------------------------------------------------
Routine : Cdc1Try
Input   : 
Output  : 
Description 
        - Connect Check Command Handling Routine of CD Changer
--------------------------------------------------------------------------*/ 
static void Cdc1Try (void)
{
	if (!GetCdcFlg1(ConChk) || (TxCntr == 0))
  		{
     		ClrCdcFlg1(ConChk);
     		CdcState = CD_IDLE;
     		return;
  		}
  	if (CdcDelay != 0)  return;
  	if (GetCdcFlg(RdyCtx))  return;
  	if (Cd1Tout != 0)  return;

  	TxCntr--;
     	SetCdcFlg(CdcTx);
     	ctbit = 0;
     	ctbyte = 0;
     	ctxcntr = 12;
     	CtxState = CT_LOW;
     	PIN_CdcOut();
     	SetCdcFlg(RdyCtx);
     	CdcDelay = 2;                  /* Some Delay */
}

/*--------------------------------------------------------------------------
Routine : Cdc1Try1
Input   : 
Output  : 
Description 
        - Back Up Command Handling Routine of CD Changer
--------------------------------------------------------------------------*/ 
static void Cdc1Try1 (void)
{
  	if (!GetCdcFlg1(BckUp) || (TxCntr == 0))
  		{
     		ClrCdcFlg1(BckUp);
     		CdcState = CD_IDLE;
     		return;
  		}
  	if (CdcDelay != 0)  return;
  	if (GetCdcFlg(RdyCtx))  return;
  	if (Cd1Tout != 0)  return;

  	TxCntr--;
     	SetCdcFlg(CdcTx);
     	ctbit = 0;
     	ctbyte = 0;
     	ctxcntr = 12;
     	CtxState = CT_LOW;
     	PIN_CdcOut();
     	SetCdcFlg(RdyCtx);
     	CdcDelay = 2;                  /* Some Delay */
}

/*--------------------------------------------------------------------------
Routine : Cdc1Stop
Input   : 
Output  : 
Description 
        - 'Stop afte Stop' Handling Routine of CD Changer
--------------------------------------------------------------------------*/ 
static void Cdc1Stop (void)
{
	if (GetCdcFlg(ReqCtx))
  		{
     		CdcState = CD_IDLE;
     		return;
  		}
  	if (CdcDelay != 0)  return;
  
  	CdcState = CD_IDLE;
  	if (GetCdcFlg(CdcStop))  
  		return;
  	CdcCmd = C_STOP;
  	SetCdcFlg(ReqCtx);
}

/*--------------------------------------------------------------------------
Routine : ReadCdc1
Input   : 
Output  : 
Description 
        - Read SANYO CD Changer Status
--------------------------------------------------------------------------*/ 
void ReadCdc1 (void)
{ 
	if ((CdcTout == 0) && !GetCdcStat1(NoMgzn))  
  		SetCdcFlg(NoCdc);
  	if (GetCdcFlg(RdyCrx))	//Check new data
  		{
     		Cd1Delay = 130;                 /* //980709 2 sec delay for check command */     
     		jCdc = 0;     
     		for (iCdc = 0; iCdc < ((Crxbits / 4)-1); iCdc++)  //980709
     			{
        		if (iCdc % 2)  
        			jCdc ^= (CrxBuffer[iCdc/2] & 0x0F);
        		else  
        			jCdc ^= ((CrxBuffer[iCdc/2] >> 4) & 0x0F);
        		jCdc &= 0x0F;
     			}
     		jCdc = (jCdc + 1) & 0x0F;
     		if (Crxbits % 8)  
     			iCdc = (CrxBuffer[Crxbits/8]) & 0x0F;
     		else              
     			iCdc = CrxBuffer[(Crxbits / 8) -1] & 0x0F;
     
     		if (jCdc != iCdc)                     /* parity error */
     			{
        		ClrCdcFlg(RdyCrx); 
        		if(GetCdcStat(fplay))
        			{
           			SetCdcFlg(ReqCtx);       // Florent - uncommented
           			CdcCmd = C_PLAY;
        			}   
        		return;
     			}
         
     		switch (CrxBuffer[0] & 0x0F)
     			{
        		case CS_CDC:
           			if (GetCdcFlg1(SusTm))
           				{
              			ClrCdcFlg1(SusTm);
              			break;
           				}
           			CdcStatus = 0;
           			if (CrxBuffer[6] & 0x80)  
           				SetCdcStat(fscn);
           			if (CrxBuffer[6] & 0x04)  
           				SetCdcStat(fdscn);
           			if (CrxBuffer[7] & 0x10)  
           				SetCdcStat(fplay);
           			if (CrxBuffer[7] & 0x20)  
           				SetCdcStat(fpaus);
           			if (CrxBuffer[7] & 0x40)  
           				SetCdcFlg(CdcStop);           
           			if (CrxBuffer[5] & 0x04)  
           				SetCdcStat(frpt);
           			if (CrxBuffer[5] & 0x08)  
           				SetCdcStat(fdrpt);
           			if (CrxBuffer[6] & 0x20)  
           				SetCdcStat(frdm);
           			if (CrxBuffer[6] & 0x10)  
           				SetCdcStat(fdrdm);
           			CurDisc  = (CrxBuffer[3] >> 4) & 0x0F;
           			CdcTrack = (CrxBuffer[1] << 4) & 0xF0;
           			CdcTrack += ((CrxBuffer[2] >> 4) & 0x0F);
           
//++Nov.10 cdc time info adding
           			CdcMin = (CrxBuffer[3] << 4) & 0xF0;
           			CdcMin += ((CrxBuffer[4] >> 4) & 0x0F);
           			CdcSec = (CrxBuffer[4] << 4) & 0xF0;
           			CdcSec += ((CrxBuffer[5] >> 4) & 0x0F);
//++Nov.10 cdc time info adding  
          
           			if ((CurDisc != 0) && (CdcTrack != 0))
           				{
              			Err4Cnt = 0;
           				}
           			ClrCdcFlg(NoCdc);
           			break;
        		case CS_MGZ:
           			ClrCdcStat1(NoDsk);
           			CdcDiscs = 0;
           			if (CrxBuffer[6] & 0x80)  
           				SetBit(CdcDiscs,0);
           			if (CrxBuffer[6] & 0x40)  
           				SetBit(CdcDiscs,1);
           			if (CrxBuffer[6] & 0x20)  
           				SetBit(CdcDiscs,2);
           			if (CrxBuffer[6] & 0x10)  
           				SetBit(CdcDiscs,3);
           			if (CrxBuffer[6] & 0x08)  
           				SetBit(CdcDiscs,4);
           			if (CrxBuffer[6] & 0x04)  
           				SetBit(CdcDiscs,5);
           			if (CrxBuffer[6] & 0x02)  
           				SetBit(CdcDiscs,6);
           			if (CrxBuffer[6] & 0x01)  
           				SetBit(CdcDiscs,7);
           			if (CrxBuffer[7] & 0x80)  
           				SetBit(CdcDiscs,8);
           			if (CrxBuffer[7] & 0x40)  
           				SetBit(CdcDiscs,9);
           			if (CdcDiscs == 0)           
           				{
              			if (!GetCdcStat1(DscChn)) 
              				SetCdcStat1(NoDsk);
           				}
           			DiscNo = 0;
           			for (iCdc = 0; iCdc < 10; iCdc++)
           				if (ValBit(CdcDiscs, iCdc)) 
           					DiscNo++;           
           			ClrCdcFlg1(BckUp);
           			break;
        		case CS_CON:
           			ClrCdcFlg1(ConChk);
           			break;
        		case CS_TOC:
           			StaMusic = (CrxBuffer[1] << 4) & 0xF0;
           			StaMusic += ((CrxBuffer[2] >> 4) & 0x0F);
           			EndMusic = (CrxBuffer[2] << 4) & 0xF0;
           			EndMusic += ((CrxBuffer[3] >> 4) & 0x0F);
           			break;
        		case CS_DSC:
           			ClrCdcStat1(NoMgzn);
           			ClrCdcStat1(DscChn);
           			iCdc = CrxBuffer[3] & 0x10;
           			if (iCdc != 0)   
           				SetCdcStat1(DscChn);
                                        /* Under Disc Change ? */
           			iCdc = CrxBuffer[3] & 0xA0;
           			if (iCdc != 0)
           				{
              			SetCdcStat1(NoMgzn);      /* No Magazine ? */
              			ClrCdcStat1(NoDsk);              
           				}
           			break;
        		case CS_ERR:
           			CdcErr = 0;
           			iCdc = CrxBuffer[1];
           			if (iCdc == 0x01)  
           				CdcErr = 1;
           			if (iCdc == 0x30)  
           				CdcErr = 2;
///           if ((iCdc == 0x07) && (CdcDiscs != 0))
           			if (iCdc == 0x07)  CdcErr = 4;
        		default:
           			break;
     		}
     		ClrCdcFlg(RdyCrx);
     		ClrCdcFlg(NoCdc);
     		CdcTout = 430;                 /* 4.3 sec delay for CDC Time Out */
               
     		if (GetCdcFlg(CdcStop))  return;
     		if (!BitVal(PinAUD_TelDet))
     			{
         		ClrCdcFlg1(DspLtr);
         		return;
     			}
     		if (GetCdcFlg1(DspLtr))
     			{
         		ClrCdcFlg1(DspLtr);
     			}
     		}
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Routine : Get/Set/ClrCdcStat
Description
        - Get/Set/Clr the flag in CdcStatus
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 
unsigned char GetCdcStat (unsigned char thebit)
{
  return(ValBit(CdcStatus, thebit));
}
void SetCdcStat (unsigned char thebit)
{
  SetBit(CdcStatus, thebit);
}
void ClrCdcStat (unsigned char thebit)
{
  ClrBit(CdcStatus, thebit);
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Routine : Get/Set/ClrCdcStat1
Description
        - Get/Set/Clr the flag in CdcStatus1
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 
unsigned char GetCdcStat1 (unsigned char thebit)
{
  return(ValBit(CdcStatus1, thebit));
}
void SetCdcStat1 (unsigned char thebit)
{
  SetBit(CdcStatus1, thebit);
}
void ClrCdcStat1 (unsigned char thebit)
{
  ClrBit(CdcStatus1, thebit);
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Routine : Get/Set/ClrCdcFlg
Description
        - Get/Set/Clr the flag in CdcFlag
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 
unsigned char GetCdcFlg (unsigned char thebit)
{
  return(ValBit(CdcFlag, thebit));
}
void SetCdcFlg (unsigned char thebit)
{
  SetBit(CdcFlag, thebit);
}
void ClrCdcFlg (unsigned char thebit)
{
  ClrBit(CdcFlag, thebit);
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Routine : Get/Set/ClrCdcFlg1
Description
        - Get/Set/Clr the flag in CdcFlag1
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 
unsigned char GetCdcFlg1 (unsigned char thebit)
{
  return(ValBit(CdcFlag1, thebit));
}
void SetCdcFlg1 (unsigned char thebit)
{
  SetBit(CdcFlag1, thebit);
}
void ClrCdcFlg1 (unsigned char thebit)
{
  ClrBit(CdcFlag1, thebit);
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : PIN_CdcOut
INPUT/OUTPUT : None  
DESCRIPTION  : CD Changer data line change to output open drain.
COMMENTS     : I/O configured always as Input unless data has to be sent.
-----------------------------------------------------------------------------*/ 
void PIN_CdcOut(void)
{
  SetBit(PB_DDR,CDC_BUS);                /* Configure CDC data pin (PB4) to out OD.*/
  ClrBit(PB_OR,CDC_BUS);                
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : PIN_CdcIn
INPUT/OUTPUT : None  
DESCRIPTION  : CD Changer data line change to input w/ interrupt and pull-up.
COMMENTS     : I/O configured always as Input unless data has to be sent.
-----------------------------------------------------------------------------*/ 
void PIN_CdcIn(void)
{
  ClrBit(PB_DDR,CDC_BUS);      /* Configure CDC data pin (PB4) to input w/ PU & IT */ 
  SetBit(PB_OR,CDC_BUS);      
}
/******************************* End of File *******************************/



















⌨️ 快捷键说明

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