SAM3S8/SD8 Software Package: libraries/libboard_sam3s-ek2/source/wm8731.c Source File

SAM3S8/SD8 Software Package 1.2

# wm8731.c

Go to the documentation of this file.

    00001 /* ----------------------------------------------------------------------------
    00002  *         ATMEL Microcontroller Software Support
    00003  * ----------------------------------------------------------------------------
    00004  * Copyright (c) 2009, Atmel Corporation
    00005  *
    00006  * All rights reserved.
    00007  *
    00008  * Redistribution and use in source and binary forms, with or without
    00009  * modification, are permitted provided that the following conditions are met:
    00010  *
    00011  * - Redistributions of source code must retain the above copyright notice,
    00012  * this list of conditions and the disclaimer below.
    00013  *
    00014  * Atmel's name may not be used to endorse or promote products derived from
    00015  * this software without specific prior written permission.
    00016  *
    00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
    00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
    00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
    00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00027  * ----------------------------------------------------------------------------
    00028  */
    00029
    00030
    00031 /**
    00032   * \file
    00033   *
    00034   * Implementation WM8731 driver.
    00035   *
    00036   */
    00037
    00038 /*----------------------------------------------------------------------------
    00039  *        Headers
    00040  *----------------------------------------------------------------------------*/
    00041
    00042 #include "board.h"
    00043
    00044 /*----------------------------------------------------------------------------
    00045  *        Exported functions
    00046  *----------------------------------------------------------------------------*/
    00047
    00048 /**
    00049  * \brief Read data from WM8731 Register.
    00050  *
    00051  * \param pTwid   Pointer to twi driver structure
    00052  * \param device  Twi slave address.
    00053  * \param regAddr Register address to read.
    00054  * \return value in the given register.
    00055  */
    00056 uint16_t WM8731_Read(Twid *pTwid,
    00057                      uint32_t device,
    00058                      uint32_t regAddr)
    00059 {
    00060     uint16_t bitsDataRegister;
    00061     uint8_t Tdata[2]={0,0};
    00062
    00063     TWID_Read(pTwid, device, regAddr, 0, Tdata, 2, 0);
    00064     bitsDataRegister = (Tdata[0] << 8) | Tdata[1];
    00065     return bitsDataRegister;
    00066 }
    00067
    00068 /**
    00069  * \brief  Write data to WM8731 Register.
    00070  *
    00071  * \param pTwid   Pointer to twi driver structure
    00072  * \param device  Twi slave address.
    00073  * \param regAddr Register address to read.
    00074  * \param data    Data to write
    00075  */
    00076 void WM8731_Write(Twid *pTwid,
    00077                   uint32_t device,
    00078                   uint32_t regAddr,
    00079                   uint16_t data)
    00080 {
    00081     uint8_t tmpData[2];
    00082     uint16_t tmp;
    00083     tmp = ((regAddr & 0x7f) << 9) | (data & 0x1ff);
    00084
    00085     tmpData[0] = (tmp & 0xff00) >> 8;
    00086     tmpData[1] = tmp & 0xff;
    00087     TWID_Write(pTwid, device, regAddr, 0, tmpData, 2, 0);
    00088 }
    00089
    00090 /**
    00091  * \brief  Init WM8731 to DAC mode.
    00092  *
    00093  * \param pTwid   Pointer to twi driver structure
    00094  * \param device  Twi slave address.
    00095  * \return 0.
    00096  */
    00097 uint8_t WM8731_DAC_Init(Twid *pTwid,
    00098                    uint32_t device)
    00099 {
    00100     /* reset */
    00101     WM8731_Write(pTwid, device, WM8731_REG_RESET, 0);
    00102
    00103     /* analogue audio path control */
    00104     WM8731_Write(pTwid, device, WM8731_REG_ANALOGUE_PATH_CTRL, 0x12);
    00105
    00106     /* digital audio path control*/
    00107     WM8731_Write(pTwid, device, WM8731_REG_DIGITAL_PATH_CTRL, 0x00);
    00108
    00109     /* power down control */
    00110     WM8731_Write(pTwid, device, WM8731_REG_PWDOWN_CTRL, 0x7);
    00111
    00112     /* Active control*/
    00113     WM8731_Write(pTwid, device, WM8731_REG_ACTIVE_CTRL, 0x01);
    00114
    00115     return 0;
    00116 }
    00117
    00118 /**
    00119  * \brief  Set WM8731 volume
    00120  *
    00121  * \param pTwid   Pointer to twi driver structure
    00122  * \param device  Twi slave address.
    00123  * \param value Register value, valid value is between 0x30 to 0x7f
    00124  * \return 0.
    00125  */
    00126 uint8_t WM8731_VolumeSet(Twid *pTwid,
    00127                    uint32_t device,
    00128                    uint16_t value)
    00129 {
    00130     uint16_t regValue;
    00131
    00132     value &= WM8731_LHPVOL_BITS;
    00133     regValue = WM8731_LRHPBOTH_BIT | WM8731_LZCEN_BIT | value;
    00134     WM8731_Write(pTwid, device, WM8731_REG_LEFT_HPOUT, regValue);
    00135     regValue = WM8731_RZCEN_BIT | WM8731_RLHPBOTH_BIT | value;
    00136     WM8731_Write(pTwid, device, WM8731_REG_RIGHT_HPOUT, value);
    00137     return 0;
    00138 }
    00139

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines