📄 wm2wirelink.c
字号:
/*-----------------------------------------------------------------------------
* Copyright (c) Wolfson Microelectronics plc. All rights reserved.
*
* This software as well as any related documentation is furnished under
* license and may only be used or copied in accordance with the terms of the
* license. The information in this file is furnished for informational use
* only, is subject to change without notice, and should not be construed as
* a commitment by Wolfson Microelectronics plc. Wolfson Microelectronics plc
* assumes no responsibility or liability for any errors or inaccuracies that
* may appear in this document or any software that may be provided in
* association with this document.
*
* Except as permitted by such license, no part of this document may be
* reproduced, stored in a retrieval system, or transmitted in any form or by
* any means without the express written consent of Wolfson Microelectronics plc.
*
* $Id: WM2WireLink.c 2169 2005-09-20 14:14:47Z ib $
*
* This file encapsulates the 2-wire operations required by drivers for Wolfson
* chips. It assumes the interface in WMPlatform2Wire.h is provided for the
* specific platform/operating system.
*
* Warning:
* This driver is specifically written for Wolfson Codecs. It is not a
* general CODEC device driver.
*
* -----------------------------------------------------------------------------*/
/*
* IMPORTANT
*
* The functions in this file must obey two rules:
*
* 1. They do not use any memory other than stack space and parameters passed in.
* 2. They do not make system calls, unless WMSystemCallsAllowed returns TRUE.
*
* All functions take a context parameter, which passes in pointers to the virtual
* addresses of memory-mapped registers, and stores what type of device is being
* accessed.
*
* Some client functions provide shared memory via the v_pWMData member of the
* context structure. This cannot be relied on.
*/
/*
* Include files
*/
#include "WMCommon.h"
#include "WMDevice.h"
#include "WMControlLink.h"
#include "WMDeviceContext.h"
#include "WM2WireLink.h"
#include "WMPlatform2Wire.h"
#if WM_I2S
/*
* Global definitions
*/
#define WRITE_ADDR(_addr) (((_addr)<<1)|0)
#define READ_ADDR(_addr) (((_addr)<<1)|1)
/*
* Convert to/from network byte order (MSB first).
*/
#ifndef htons
# define htons(_v) ((_v & 0xFF00) >> 8) | ((_v & 0x00FF) << 8)
# define ntohs(_v) htons(_v)
#endif
/*
* Private data
*/
/*
* Function prototypes
*/
/*-----------------------------------------------------------------------------
* Function: WM2WireWrite
*
* Write the value to the given register.
*
* Parameters:
* hDevice handle to the device (from WMOpenDevice)
* reg register to write to
* value value to write
*
* Returns: WMSTATUS
* See WMStatus.h
*---------------------------------------------------------------------------*/
WMSTATUS WM2WireWrite( WM_DEVICE_HANDLE hDevice,
WM_REGTYPE reg,
WM_REGVAL value
)
{
WM_DEVICE_CONTEXT *pDeviceContext = WMHANDLE_TO_DEVICE( hDevice );
const WM_CHIPDEF *pChipDef;
WMSTATUS status;
unsigned short data;
WM_ASSERT( hDevice, pDeviceContext );
/*
* Look up our chipdef.
*/
pChipDef = WMGetChipDef( hDevice );
WM_ASSERT( hDevice, pChipDef );
if ( !pChipDef )
{
status = WMS_NO_SUPPORTED_DEVICE;
goto error;
}
/* Make sure the fields have been set up */
WM_ASSERT( hDevice, 0 != pChipDef->regAddrMask );
WM_ASSERT( hDevice, 0 != pChipDef->regDataMask );
/* Make sure the write fits in our 16-bit data variable */
WM_ASSERT( hDevice, (reg << pChipDef->regAddrShift) <= 0xFFFF );
/* At the moment we assume the address goes in the top */
WM_ASSERT( hDevice, 0 != pChipDef->regAddrShift );
WM_ASSERT( hDevice, pChipDef->regAddrMask > pChipDef->regDataMask );
/*
* Build up the register/data value.
* It's made up of combined address and data - the widths of the two fields are
* described in the chipdef. For example a 7-bit address/9-bit data write would
* be made up as follows:
* 15 9 8 0
* +------+--------+
* | reg | data |
* +------+--------+
*/
data = ( (reg << pChipDef->regAddrShift) & pChipDef->regAddrMask ) | (value & pChipDef->regDataMask);
/*
* Now convert to network byte order (MSB first), since that's what
* the 2-wire link uses.
*/
data = htons( data );
/*
* And do the write.
*/
status = WMPlatform2WireWrite( hDevice,
(unsigned char) WRITE_ADDR(pDeviceContext->codecAddress),
(unsigned char *) &data,
sizeof( data )
);
if ( WM_ERROR( status ) )
{
goto error;
}
/*
* We're done.
*/
return WMS_SUCCESS;
error:
return status;
}
/*-----------------------------------------------------------------------------
* Function: WM2WireReset
*
* Reset the registers to default settings.
*
* Parameters:
* hDevice handle to the device (from WMOpenDevice)
*
* Returns: WMSTATUS
* See WMStatus.h.
*---------------------------------------------------------------------------*/
WMSTATUS WM2WireReset( WM_DEVICE_HANDLE hDevice )
{
WMSTATUS status;
const WM_CHIPDEF *pChipDef;
WM_REGTYPE resetReg;
pChipDef = WMGetChipDef( hDevice );
if ( !pChipDef )
{
status = WMS_NO_SUPPORTED_DEVICE;
goto finish;
}
resetReg = pChipDef->resetReg;
status = WMWrite( hDevice, resetReg, 0 );
finish:
return status;
}
#endif /* WM_I2S */
/*------------------------------ END OF FILE ---------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -