📄 hwctxt.cpp
字号:
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function: Deinit()
Description: Deinitializest the hardware: disables DMA channel(s),
clears any pending interrupts, powers down the audio
codec chip, etc.
Returns: Boolean indicating success
-------------------------------------------------------------------*/
BOOL HardwareContext::Deinit()
{
//----- 1. Disable the input/output channels -----
// AUDIO_IN_DMA_DISABLE();
AUDIO_OUT_DMA_DISABLE();
//----- 2. Disable/clear DMA input/output interrupts -----
AUDIO_IN_CLEAR_INTERRUPTS();
AUDIO_OUT_CLEAR_INTERRUPTS();
//----- 3. Turn the audio hardware off -----
AudioMute(DMA_CH_OUT | DMA_CH_MIC, TRUE);
//----- 4. Unmap the control registers and DMA buffers -----
UnmapRegisters();
UnmapDMABuffers();
return TRUE;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function: UnmapRegisters()
Description: Unmaps the config registers used by both the SPI and
AC97 controllers.
Notes: The SPI and AC97 controllers both use the GPIO config
registers, so these MUST be deinitialized LAST.
Returns: Boolean indicating success
-------------------------------------------------------------------*/
BOOL HardwareContext::UnmapRegisters()
{
return TRUE;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function: MapDMABuffers()
Description: Maps the DMA buffers used for audio input/output
on the AC97 bus.
Returns: Boolean indicating success
-------------------------------------------------------------------*/
BOOL HardwareContext::MapDMABuffers()
{
PBYTE pVirtDMABufferAddr = NULL;
DMA_ADAPTER_OBJECT Adapter;
memset(&Adapter, 0, sizeof(DMA_ADAPTER_OBJECT));
Adapter.InterfaceType = Internal;
Adapter.ObjectSize = sizeof(DMA_ADAPTER_OBJECT);
// Allocate a block of virtual memory (physically contiguous) for the DMA buffers.
//
pVirtDMABufferAddr = (PBYTE)HalAllocateCommonBuffer(&Adapter, (AUDIO_DMA_PAGE_SIZE * 4), &g_PhysDMABufferAddr, FALSE);
if (pVirtDMABufferAddr == NULL)
{
RETAILMSG(TRUE, (TEXT("WAVEDEV.DLL:HardwareContext::MapDMABuffers() - Failed to allocate DMA buffer.\r\n")));
return(FALSE);
}
// Setup the DMA page pointers.
// NOTE: Currently, input and output each have two DMA pages: these pages are used in a round-robin
// fashion so that the OS can read/write one buffer while the audio codec chip read/writes the other buffer.
//
m_Output_pbDMA_PAGES[0] = pVirtDMABufferAddr;
m_Output_pbDMA_PAGES[1] = pVirtDMABufferAddr + AUDIO_DMA_PAGE_SIZE;
m_Input_pbDMA_PAGES[0] = pVirtDMABufferAddr + (2 * AUDIO_DMA_PAGE_SIZE);
m_Input_pbDMA_PAGES[1] = pVirtDMABufferAddr + (3 * AUDIO_DMA_PAGE_SIZE);
return(TRUE);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function: UnmapDMABuffers()
Description: Unmaps the DMA buffers used for audio input/output
on the AC97 bus.
Returns: Boolean indicating success
-------------------------------------------------------------------*/
BOOL HardwareContext::UnmapDMABuffers()
{
if(m_Output_pbDMA_PAGES[0])
{
VirtualFree((PVOID)m_Output_pbDMA_PAGES[0], 0, MEM_RELEASE);
}
return TRUE;
}
#if (AC97_READY_CHECK_METHOD == 1) // AC97 Interrupt Thread
HANDLE AC97_InterruptEvent = NULL;
HANDLE AC97_Thread = NULL;
DWORD g_AC97SysIntr = SYSINTR_UNDEFINED;
DWORD g_AC97Irq = IRQ_WDT_AC97;
DWORD AC97InterruptThread(void);
DWORD Codec_Ready_Irq = 0;
static DWORD AC97InterruptThread(void)
{
while(1)
{
WaitForSingleObject(AC97_InterruptEvent, INFINITE);
RETAILMSG(1,(_T("AC97InterruptThread() !!\r\n")));
if ( (v_pAC97regs->AC_GLBSTAT& 0x400000))
{
Codec_Ready_Irq=1;
RETAILMSG(1,(_T("Codec Ready!\r\n")));
v_pAC97regs->AC_GLBCTRL &= ~(0x400000); // codec ready interrupt disable
}
s2440INT->INTSUBMSK &= ~(1<< IRQ_SUB_AC97);
s2440INT->INTMSK &= ~(1 << IRQ_WDT_AC97);
}
}
static void AC97_InterruptThread_Initialize(void)
{
DWORD threadID;
if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &g_AC97Irq, sizeof(UINT32), &g_AC97SysIntr, sizeof(UINT32), NULL))
{
RETAILMSG(1, (TEXT("ERROR: AC97Irq: Failed to request sysintr value for AC97 interrupt.\r\n")));
}
if (AC97_InterruptEvent == NULL)
{
AC97_InterruptEvent = CreateEvent(NULL, FALSE, FALSE,NULL);
if (NULL == AC97_InterruptEvent) {
RETAILMSG(1,(TEXT("AC97 interrupt event Error1\r\n")));
}
// initialize the card insertion interrupt event
if (!InterruptInitialize (g_AC97SysIntr, AC97_InterruptEvent, 0, 0)) {
RETAILMSG(1,(TEXT("AC97 interrupt event Error2\r\n")));
}
}
if (AC97_Thread == NULL)
{
AC97_Thread = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)AC97InterruptThread,
0,
0,
&threadID);
if (NULL == AC97_Thread ) {
RETAILMSG(1,(TEXT("Create AC97 Interrupt Thread Fail\r\n")));
}
}
RETAILMSG(1,(TEXT("Use AC97 Int for initialization\r\n")));
}
#endif
// Kingfish2 AC97 Initialize function
BOOL HardwareContext::AC97_Init()
{
RETAILMSG(AC97_DEBUG,(_T("WAVDEV_AC97::AC97_Init()++\r\n")));
v_pCLKPWRreg->CLKCON |= AC97_INTERNAL_CLOCK_ENABLE; // Enable the CPU clock to the AC97 controller
// Write into the AC97 Global Control Register
//Cold Reset
v_pAC97regs->AC_GLBCTRL |= (0x1<<0);
Delay(10);
v_pAC97regs->AC_GLBCTRL &= ~(0x1<<0);
Delay(10);
//AC-link On
v_pAC97regs->AC_GLBCTRL |= (0x1<<2);
Delay(10);
//Warm Reset
v_pAC97regs->AC_GLBCTRL |= (0x1<<1);
Delay(10);
v_pAC97regs->AC_GLBCTRL &= ~(0x1<<1);
Delay(10);
//Cold Reset
v_pAC97regs->AC_GLBCTRL |= (0x1<<0);
Delay(10);
v_pAC97regs->AC_GLBCTRL &= ~(0x1<<0);
Delay(10);
//AC-link On
v_pAC97regs->AC_GLBCTRL |= (0x1<<2);
Delay(10);
//Transfer data enable using AC-link
v_pAC97regs->AC_GLBCTRL |= (0x1<<3);
Delay(10);
// Enable the Codec ready Interrupt
v_pAC97regs->AC_GLBCTRL |= (0x1<<22);
Delay(10);
while ( !(v_pAC97regs->AC_GLBSTAT & 0x400000) )
{
RETAILMSG(AC97_DEBUG,(_T("WAVEDEV_AC97::AC97 codec is not ready!\r\n")));
}
v_pAC97regs->AC_GLBCTRL &= ~(0x400000); // codec ready interrupt disable
Delay(10);
#if AC97_RECORD_MICIN
v_pAC97regs->AC_GLBCTRL = (v_pAC97regs->AC_GLBCTRL & ~(0x3f<<8)) | 0x2200; // PCM_OUT=DMA,PCM_IN=OFF,MIC=DMA;
#else
// v_pAC97regs->AC_GLBCTRL = (v_pAC97regs->AC_GLBCTRL & ~(0x3f<<10)) | 0x2800; // PCM_OUT=DMA,PCM_IN=DMA,MIC=OFF;
v_pAC97regs->AC_GLBCTRL = (v_pAC97regs->AC_GLBCTRL & ~(0x3f<<8)) | 0x2800; // PCM_OUT=DMA,PCM_IN=DMA,MIC=OFF;
#endif
RETAILMSG(AC97_DEBUG,(_T("WAVDEV_AC97::AC97_Init()--\r\n")));
return TRUE;
}
BOOL HardwareContext::Codec_channel()
{
/*
// Kingfish2 AC97
if( m_InputDMARunning & m_OutputDMARunning )
{
RETAILMSG(AC97_DEBUG1,(_T("Codec_channel() - In & Out\r\n")));
WriteCodecRegister(AC97_POWER_CONTROL, AC97_PWR_D0); // ADC, DAC power up
}
else if( m_InputDMARunning )
{
RETAILMSG(AC97_DEBUG1,(_T("Codec_channel() - In\r\n")));
WriteCodecRegister(AC97_POWER_CONTROL, AC97_PWR_PR1); // DAC power down
//WriteCodecRegister(AC97_POWER_DOWN1, 0xb9cf);
//WriteCodecRegister(AC97_POWER_DOWN2, 0xbff0);
}
else if( m_OutputDMARunning )
{
RETAILMSG(AC97_DEBUG1,(_T("Codec_channel() - Out\r\n")));
WriteCodecRegister(AC97_POWER_CONTROL, AC97_PWR_PR0); // ADC power down
}
else
{
RETAILMSG(AC97_DEBUG1,(_T("Codec_channel() - none\r\n")));
WriteCodecRegister(AC97_POWER_CONTROL, AC97_PWR_PR1|AC97_PWR_PR0); // ADC/DAC power down
}
*/
return(TRUE);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function: InitCodec()
Description: Initializes the audio codec chip.
Notes: The audio codec chip is intialized for output mode
but powered down. To conserve battery life, the chip
is only powered up when the user starts playing a
file.
Specifically, the powerup/powerdown logic is done
in the AudioMute() function. If either of the
audio channels are unmuted, then the chip is powered
up; otherwise the chip is powered own.
Returns: Boolean indicating success
-------------------------------------------------------------------*/
BOOL HardwareContext::InitCodec()
{
USHORT usVendorID1, usVendorID2;
RETAILMSG(AC97_DEBUG, (TEXT("+++InitCodec\r\n")));
// ----------------------------------------------------------------------------
// For WM9715L initial
// Added by DiaoLiangpeng 2009-04-15
#if 0
// Send reset command
// WriteCodecRegister(AC97_RESET,0xffff);
// Delay(10);
// Get the Vendor ID
usVendorID1 = ReadCodecRegister(AC97_VENDOR_ID1);
// Delay(10);
usVendorID2 = ReadCodecRegister(AC97_VENDOR_ID2);
// Delay(10);
if ( (0x574D == usVendorID1) && (0x4C12 == usVendorID2) )
{
RETAILMSG(AC97_DEBUG,(_T("Detect WM9715L 1: usVendorID1 = 0x%x, usVendorID2 = 0x%x.\r\n"), usVendorID1, usVendorID2));
}
else
{
RETAILMSG(AC97_DEBUG,(_T("Detect WM9715L 2: usVendorID1 = 0x%x, usVendorID2 = 0x%x.\r\n"), usVendorID1, usVendorID2));
return FALSE;
}
// WriteCodecRegister(AC97_POWER_CONTROL, 0x0000);
WriteCodecRegister(AC97_LINE_IN_VOLUME,0xe000);
WriteCodecRegister(AC97_PHONE_VOLUME,0xc000);
WriteCodecRegister(AC97_PC_BEEP_VOLUME,0xaaa0);
WriteCodecRegister(AC97_VIDEO_VOLUME,0xad00);
WriteCodecRegister(AC97_CD_VOLUME,0xaaa0);
WriteCodecRegister(AC97_MIC_VOLUME,0x6808);
WriteCodecRegister(AC97_RECORD_SELECT,0x3000);
// Set PCM Channel
//WriteCodecRegister(0x7, 0x7);
// Set stereo Channel
WriteCodecRegister(AC97_ALT_LINE_LEVEL_OUT_VOLUME,0x0c0c);
// Set mono Channel
WriteCodecRegister(AC97_MASTER_VOLUME_MONO,0x000c);
// Set speaker Channel
WriteCodecRegister(AC97_AUX_VOLUME,0x0000);
// WriteCodecRegister(AC97_AUX_VOLUME,(0x2<<9)|(0x1<<8));
WriteCodecRegister(AC97_MASTER_VOLUME,0x0c0c);
// WriteCodecRegister(AC97_MASTER_VOLUME, (0x3f<<8)|(0x3f<<0));
// WriteCodecRegister(AC97_ALT_LINE_LEVEL_OUT_VOLUME,(0x3f<<8)|(0x3f<<0));
#else
// WriteCodecRegister(0x02, 0x8242);
WriteCodecRegister(0x02, 0x0000);
// WriteCodecRegister(0x04, 0x8B0B);
WriteCodecRegister(0x04, 0x0000);
// WriteCodecRegister(0x06, 0x0000);
WriteCodecRegister(0x06, 0x8000);
WriteCodecRegister(0x08, 0x8303);
WriteCodecRegister(0x0C, 0xC008);
WriteCodecRegister(0x0E, 0xC803);
WriteCodecRegister(0x10, 0x0808);
WriteCodecRegister(0x14, 0x8C00);
WriteCodecRegister(0x16, 0x0100);
// WriteCodecRegister(0x18, 0xE808);
WriteCodecRegister(0x18, 0x0808);
WriteCodecRegister(0x1A, 0x3707);
WriteCodecRegister(0x1C, 0x6262);
WriteCodecRegister(0x26, 0x0000);
WriteCodecRegister(0x2A, 0x0411);
WriteCodecRegister(0x2C, 0xAC44);
WriteCodecRegister(0x2E, 0xAC44);
WriteCodecRegister(0x32, 0xAC44);
#endif
PrintCodecRegisters();
// ----------------------------------------------------------------------------
RETAILMSG(AC97_DEBUG, (TEXT("---InitCodec\r\n")));
return(TRUE);
}
MMRESULT HardwareContext::SetOutputGain (DWORD dwGain)
{
m_dwOutputGain = dwGain & 0xffff; // save off so we can return this from GetGain - but only MONO
// convert 16-bit gain to 5-bit attenuation
UCHAR ucGain;
if (m_dwOutputGain == 0) {
ucGain = 0x3F; // mute: set maximum attenuation
}
else {
ucGain = (UCHAR) ((0xffff - m_dwOutputGain) >> 11); // codec supports 64dB attenuation, we'll only use 32
}
ASSERT((ucGain & 0xC0) == 0); // bits 6,7 clear indicate DATA0 in Volume mode.
// Kingfish2. I can't find output gain of CODEC
// WriteCodecRegister( AC97_RECORD_GAIN, AC97_RECORD_GAIN_VAL );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -