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

📄 hwctxt.cpp

📁 wince5.0 BSP包
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    DWORD Bits = dwDCSR & (DMA_DONEA|DMA_DONEB|DMA_BIU);

    switch (Bits)
    {
    case 0:
    case DMA_BIU:
        // No done bits set- must not be my interrupt
        return 0;
    case DMA_DONEA|DMA_DONEB|DMA_BIU:
        // Load B, then A
        BytesTransferred = TransferInputBuffer(IN_BUFFER_B);
        // fall through
    case DMA_DONEA: // This should never happen!
    case DMA_DONEA|DMA_BIU:
        // Load A
        BytesTransferred += TransferInputBuffer(IN_BUFFER_A);
        break;
    case DMA_DONEA|DMA_DONEB:
        // Load A, then B
        BytesTransferred = TransferInputBuffer(IN_BUFFER_A);
#if 0 //DMA_FLAG        
		BytesTransferred += TransferInputBuffer(IN_BUFFER_B);
		break;
#endif		
        // fall through
    case DMA_DONEB|DMA_BIU: // This should never happen!
    case DMA_DONEB:
        // Load B
        BytesTransferred += TransferInputBuffer(IN_BUFFER_B);
        break;
    }

    // If it was our interrupt, but we weren't able to transfer any bytes
    // (e.g. no empty buffers ready to be filled)
    // Then stop the input DMA
    if (BytesTransferred==0)
    {
        StopInputDMA();
    }
    return BytesTransferred;
}


void HardwareContext::InterruptThread()
{
    ULONG InputTransferred, OutputTransferred;
	BOOL dmaInterruptSource = 0;

    // Fast way to access embedded pointers in wave headers in other processes.
	SetProcPermissions((DWORD)-1);

    while(TRUE)
    {
		WaitForSingleObject(m_hAudioInterrupt, INFINITE);
		AC97MSG(1,(TEXT("0x%x\n"),s2440INT->rINTMSK));
		AC97MSG(1,(TEXT("start\n")));
		dmaInterruptSource = 0;
		//----- 1. Grab the lock -----
		Lock();

		__try
		{
			AC97MSG(1,(TEXT("try\n")));

			//----- 3. Determine the interrupt source (input DMA operation or output DMA operation?) -----
			//----- NOTE:	Often, platforms use two separate DMA channels for input/output operations but
			//				have the OAL return SYSINTR_AUDIO as the interrupt source.  If this is the case,
			//				then the interrupt source (input or output DMA channel) must be determined in
			//				this step.
			// charlie, determine the interrupt source
//			s2440INT->rSRCPND
			if( s2440INT->rINTMSK & BIT_DMA1 ){
				dmaInterruptSource |= DMA_CH_OUT;								// Output DMA is supported...
			}
			
			if( s2440INT->rINTMSK & BIT_DMA2 ){
				dmaInterruptSource |= DMA_CH_MIC;								// Input DMA is supported...
			}
						
			// For determine the interrupt source
			//----- 2. Acknowledge the DMA interrupt -----
			InterruptDone(m_IntrAudio);

			if ( m_Dx != D0 ) continue;

			//----- 4. Handle any interrupts on the input source -----
			//		   NOTE: The InterruptDone() call below automatically clears the interrupt.
//			if((m_InputDMARunning) && (dmaInterruptSource == DMA_CH_MIC))
			if((dmaInterruptSource == DMA_CH_MIC))
			{
				//----- Determine which buffer just completed the DMA transfer -----
				if(m_InputDMAStatus & DMA_BIU)
				{
					m_InputDMAStatus &= ~DMA_STRTB;							// Buffer B just completed...
					m_InputDMAStatus |= DMA_DONEB;

					m_InputDMAStatus &= ~DMA_BIU;							// Buffer A is in use
#if DMA_FLAG					
					SELECT_AUDIO_DMA_INPUT_BUFFER_B();
#else					
					SELECT_AUDIO_DMA_INPUT_BUFFER_A();
#endif
					AC97MSG(1,(TEXT("1\n")));
				}else
				{
					m_InputDMAStatus &= ~DMA_STRTA;							// Buffer A just completed...
					m_InputDMAStatus |= DMA_DONEA;

					m_InputDMAStatus |= DMA_BIU;							// Buffer B is in use
#if DMA_FLAG
					SELECT_AUDIO_DMA_INPUT_BUFFER_A();
#else										
					SELECT_AUDIO_DMA_INPUT_BUFFER_B();
#endif				
					AC97MSG(1,(TEXT("2\n")));
				}
#if !DMA_FLAG		
				//----- 5. Schedule the next DMA transfer -----
				AUDIO_IN_DMA_ENABLE();
#endif				
				
				//----- 6. Retrieve the next chunk of recorded data from the non-playing buffer -----
				InputTransferred = TransferInputBuffers(m_InputDMAStatus);			
			}

			//----- 7. Handle any interrupts on the output source -----
			//		   NOTE: The InterruptDone() call below automatically clears the interrupt.
//			if((m_OutputDMARunning) && (dmaInterruptSource == DMA_CH_OUT))
//			if((dmaInterruptSource == DMA_CH_OUT))
			else
			{	
				//----- Determine which buffer just completed the DMA transfer -----
				if(m_OutputDMAStatus & DMA_BIU)
				{
					//RETAILMSG(DMA_CHK,(_T("BB\r\n")));
					m_OutputDMAStatus &= ~DMA_STRTB;						// Buffer A just completed...
					m_OutputDMAStatus |= DMA_DONEB;
					m_OutputDMAStatus &= ~DMA_BIU;							// Buffer B is in use
					
					delay_count = 0;
				    while((v_pDMAregs->rDSTAT1&0xfffff)==0){
			        	RETAILMSG(1,(_T("3")));
#if WAIT_DMA_END
						Sleep(1);
#else
        				if( delay_count++ > DELAY_COUNT )	break;
#endif
	        		} 

#if DMA_FLAG		
					SELECT_AUDIO_DMA_OUTPUT_BUFFER_B();		// charlie. B => A
#else				
					SELECT_AUDIO_DMA_OUTPUT_BUFFER_A();		// charlie. B => A	
#endif					
				}else
				{
					//RETAILMSG(DMA_CHK,(_T("AA\r\n")));
					m_OutputDMAStatus &= ~DMA_STRTA;						// Buffer B just completed...
					m_OutputDMAStatus |= DMA_DONEA;
					m_OutputDMAStatus |= DMA_BIU;							// Buffer A is in use
					
					delay_count = 0;
					while((v_pDMAregs->rDSTAT1&0xfffff)==0)
					{
			        	RETAILMSG(1,(_T("4")));
#if WAIT_DMA_END
						Sleep(1);
#else
        				if( delay_count++ > DELAY_COUNT )	break;
#endif
					}
				
				
#if DMA_FLAG					
					SELECT_AUDIO_DMA_OUTPUT_BUFFER_A();		// charlie. B => A
#else				
					SELECT_AUDIO_DMA_OUTPUT_BUFFER_B();		// charlie. B => A	
#endif					
				}
#if !DMA_FLAG		
				//----- 8. Schedule the next DMA transfer -----
				AUDIO_OUT_DMA_ENABLE();
#endif
				//----- 9. Fill the non-playing buffer with the next chunk of audio data to play -----
				OutputTransferred = TransferOutputBuffers(m_OutputDMAStatus);			
			}

		}
		__except(EXCEPTION_EXECUTE_HANDLER) 
		{
			DEBUGMSG(ZONE_ERROR, (TEXT("WAVDEV2.DLL:InterruptThread() - EXCEPTION: %d"), GetExceptionCode()));
		}

		//----- 10. Give up the lock ----- 
		Unlock();
	}  
}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:		AudioMute()

Description:	Mutes/unmutes the specified audio channel.

Notes:			If both audio channels are MUTED, then the chip 
				is powered down to conserve battery life.  
				Alternatively, if either audio channel is unMUTED, 
				the chip is powered up.

Returns:		Boolean indicating success
-------------------------------------------------------------------*/
BOOL HardwareContext::AudioMute(DWORD channel, BOOL bMute)
{
	static DWORD dwActiveChannel = 0;
	USHORT volume;

	// 
	// Turn off/on mute bit in volume control register.
	// 
	if( (channel & DMA_CH_OUT ) && !m_OutputDMARunning )		// 030711
	{
		if(bMute)
		{
			volume = ReadCodecRegister(AC97_HEADPHONE_VOL);
     		WriteCodecRegister(AC97_HEADPHONE_VOL, volume | 0x8000);
		}
		else
		{
			volume = ReadCodecRegister(AC97_HEADPHONE_VOL);
     		WriteCodecRegister(AC97_HEADPHONE_VOL, volume & ~0x8000);
		}
	}		

	if( (channel & DMA_CH_MIC ) && !m_InputDMARunning )
	{
		if(bMute)
		{
			volume = ReadCodecRegister(AC97_MIC_VOL);
     		WriteCodecRegister(AC97_MIC_VOL, volume | 0x8000);
		}
		else
		{
			volume = ReadCodecRegister(AC97_MIC_VOL);
     		WriteCodecRegister(AC97_MIC_VOL, volume & ~0x8000);
		}
	}		
	
	return TRUE;
}


void CallInterruptThread(HardwareContext *pHWContext)
{
    pHWContext->InterruptThread();
}

DWORD 
HardwareContext::Open(
    void
    )
{
    DWORD mmErr = MMSYSERR_NOERROR;
    
    // Don't allow play when not on, if there is a power constraint upon us.
    if ( D0 != m_Dx )
    {
        // Tell the Power Manager we need to power up.
        // If there is a power constraint then fail.
        DWORD dwErr = DevicePowerNotify(_T("WAV1:"), D0, POWER_NAME);
        if ( ERROR_SUCCESS !=  dwErr ) {
            RETAILMSG(1, (TEXT("AC97::Open:DevicePowerNotify ERROR: %u\r\n"), dwErr ));
            mmErr = MMSYSERR_ERROR;
        }
    }
    
    return mmErr;
}


DWORD 
HardwareContext::Close(
    void
    )
{
    DWORD mmErr = MMSYSERR_NOERROR;
/*
    DWORD dwErr;
    
    // we are done so inform Power Manager to power us down, 030711
//    dwErr = DevicePowerNotify(_T("WAV1:"), (_CEDEVICE_POWER_STATE)D4, POWER_NAME);
    if ( ERROR_SUCCESS !=  dwErr ) {
        RETAILMSG(1, (TEXT("AC97::Close:DevicePowerNofify ERROR: %u\r\n"), dwErr ));
            mmErr = MMSYSERR_ERROR;
    }
*/
    
    return mmErr;
}


BOOL 
HardwareContext::IOControl( 
    DWORD  dwOpenData,
    DWORD  dwCode,
    PBYTE  pBufIn,
    DWORD  dwLenIn,
    PBYTE  pBufOut,
    DWORD  dwLenOut,
    PDWORD pdwActualOut)
{
    DWORD dwErr = ERROR_SUCCESS;    
    BOOL  bRc = TRUE;

    UNREFERENCED_PARAMETER(dwOpenData);
    
    switch (dwCode) {
        //
        // Power Management
        //
        case IOCTL_POWER_CAPABILITIES: 
        {
            PPOWER_CAPABILITIES ppc;
            RETAILMSG(1,(_T("IOCTL_POWER_CAPABILITIES++\r\n")));
            
            if ( !pdwActualOut || !pBufOut || (dwLenOut < sizeof(POWER_CAPABILITIES)) ) {
                bRc = FALSE;
                dwErr = ERROR_INVALID_PARAMETER;
                break;
            }
            
            ppc = (PPOWER_CAPABILITIES)pBufOut;
            
            memset(ppc, 0, sizeof(POWER_CAPABILITIES));

            // support D0, D4
            ppc->DeviceDx = 0x11;

            // no wake
            // no inrush

            // Report our nominal power consumption in uAmps rather than mWatts.
            ppc->Flags = POWER_CAP_PREFIX_MICRO | POWER_CAP_UNIT_AMPS;

            // REVIEW: Do we enable all these for normal playback?
            // D0: SPI + I2S + CODEC (Playback) + Headphone= 
            //     0.5 mA + 0.5 mA + (23 mW, into BUGBUG ohms ) + (30 mW, into 32 ohms)
            //     500 uA + 500 uA + 23000 uA + 32000 uA
            ppc->Power[D0] = 56000;
            
            *pdwActualOut = sizeof(POWER_CAPABILITIES);
            
            RETAILMSG(1,(_T("IOCTL_POWER_CAPABILITIES--\r\n")));
        } break;

        case IOCTL_POWER_SET: 

        	bRc = TRUE;
			break;

/*       	
        {
            CEDEVICE_POWER_STATE NewDx;

            RETAILMSG(1,(_T("IOCTL_POWER_SET++\r\n")));
            
            if ( !pdwActualOut || !pBufOut || (dwLenOut < sizeof(CEDEVICE_POWER_STATE)) ) {
                bRc = FALSE;
                dwErr = ERROR_INVALID_PARAMETER;
                break;
            }
            
            NewDx = *(PCEDEVICE_POWER_STATE)pBufOut;

            if ( VALID_DX(NewDx) ) {
                // grab the CS since the normal Xxx_PowerXxx can not.
                Lock();
                switch ( NewDx ) {
                    case D0:
                        if (m_Dx != D0) {
                            PowerUp();
                            m_Dx = D0;
                        }
                        break;

                    default:
                        if (m_Dx != (_CEDEVICE_POWER_STATE)D4) {
                            PowerDown();
                            m_Dx = (_CEDEVICE_POWER_STATE)D4;
                        }
                        break;
                }
                
                // return our state
                *(PCEDEVICE_POWER_STATE)pBufOut = m_Dx;
                
                RETAILMSG(1, (TEXT("AC97AUDIO: IOCTL_POWER_SET: D%u => D%u \r\n"), NewDx, m_Dx));

                Unlock();
                
                *pdwActualOut = sizeof(CEDEVICE_POWER_STATE);
            } else {
                bRc = FALSE;
                dwErr = ERROR_INVALID_PARAMETER;
            }

            RETAILMSG(1,(_T("IOCTL_POWER_SET--\r\n")));
            
        } break;
*/
        case IOCTL_POWER_GET: 

            RETAILMSG(1,(_T("IOCTL_POWER_GET++\r\n")));

            if ( !pdwActualOut || !pBufOut || (dwLenOut < sizeof(CEDEVICE_POWER_STATE)) ) {
                bRc = FALSE;
                dwErr = ERROR_INVALID_PARAMETER;
                break;
            }

            *(PCEDEVICE_POWER_STATE)pBufOut = m_Dx;

            RETAILMSG(1, (TEXT("AC97AUDIO: IOCTL_POWER_GET: D%u \r\n"), m_Dx));

            *pdwActualOut = sizeof(CEDEVICE_POWER_STATE);

            RETAILMSG(1,(_T("IOCTL_POWER_GET++\r\n")));
            break;
            
        default:
            bRc = FALSE;
            dwErr = ERROR_INVALID_FUNCTION;
            DEBUGMSG (ZONE_FUNCTION, (TEXT(" Unsupported ioctl 0x%X\r\n"), dwCode));
            break;            
    }
    
    if ( !bRc ) {
        SetLastError(dwErr);
    }
    
    return(bRc);
}

⌨️ 快捷键说明

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