/**************************************************************************//**
 * @file     apu.c
 * @version  V1.00
 * $Revision: 3 $
 * $Date: 14/07/30 13:27p $
 * @brief    N572 APU driver source file
 *
 * @note
 * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/

#include  "N572.h"

/** @addtogroup N572_Device_Driver N572 Device Driver
  @{
*/

/** @addtogroup N572_APU_Driver APU Driver
  @{
*/


/** @addtogroup N572_APU_EXPORTED_FUNCTIONS APU Exported Functions
  @{
*/

/**
  * @brief      APU open function
  * @param[in]  apu Base address of APU module
  * @return     None
  * @details    
  */
void APU_Open(APU_T *apu)
{
#if (__CHIP_SERIES__ == __N572F072_SERIES__)
	APU_SET_CH1DATA(apu, 0, 0);
#else
	int32_t i32DacOfs;
	#define IMGMAP0  *((uint32_t *)0x500000FC)
	
	i32DacOfs = IMGMAP0 << 13;
	i32DacOfs >>= 19;
	
	APU_SET_CH1DATA(apu, 0, i32DacOfs);
#endif
}

/**
  * @brief      APU close function
  * @param[in]  apu Base address of APU module
  * @return     None
  * @details   
  */
void APU_Close(APU_T *apu)
{   
      
}

/**
  * @brief      Set APU volume gain
  * @param[in]  apu Base address of APU module
  * @param[in]  i32GainIndB gain value
  * \n          i32GainIndB = VOLUM*-3 
  * \n          VOLUM    mapping dB:                
  *              0       ->  0 dB(MAX);
  *              1       -> -3 dB;               
  *              2       -> -6 dB;               
  *              6       -> -18 dB(MIN;
  *              
  *
  * @return     Real gain value
  * @details    Range is from 0dB to -18dB in -3dB step size.
  *             VOLUM (0) is largest gain setting at 0dB and VOLUM (6) is lowest gain at -18dB.
  */
int32_t APU_SetVolGaindB(APU_T *apu, int32_t i32GainIndB)
{
	if(i32GainIndB < -18)
		i32GainIndB = -18;
	else if(i32GainIndB > 0)
		i32GainIndB = 0;
	apu->VM = i32GainIndB/-3;
	
	return ((int32_t )apu->VM*-3);
}

/**
  * @brief      Get APU volume gain
  * @param[in]  apu Base address of APU module
  * \n          i32GainIndB = VOLUM*-3 
  * \n          VOLUM    mapping dB:                
  *              0       ->  0 dB(MAX);
  *              1       -> -3 dB;               
  *              2       -> -6 dB;               
  *              6       -> -18 dB(MIN; 
  *              
  *
  * @return     Real gain value
  * @details    Range is from 0dB to -18dB in -3dB step size.
  *             VOLUM (0) is largest gain setting at 0dB and VOLUM (6) is lowest gain at -18dB.
  */
int32_t APU_GetVolGaindB(APU_T *apu)
{
	return ((int32_t )apu->VM*-3);
}

/**
  * @brief      Set APU sampleing rate
  * @param[in]  apu Base address of APU module
  * @param[in]  u32SampleRate desired sampling rate
  * @return     Real sample rate value
  * @details    APU use timer0 to control sample rate, so user needs
  *             to enable timer0 APB bus clcok.
  */
uint32_t APU_SetSampleRate(APU_T *apu, uint32_t u32SampleRate)
{
	uint32_t u32Clk;
	u32Clk = TIMER_GetModuleClock(TIMER0);
	TIMER_SET_PRESCALE_VALUE(TIMER0, 25-1);
	TIMER_SET_CMP_VALUE(TIMER0, (u32Clk/25/u32SampleRate));
	TIMER0->CTL = (TIMER0->CTL&~TMR_CTL_OPMODE_Msk)|TIMER_PERIODIC_MODE;
	
	return (u32Clk/25/TIMER0->CMP);
}

/**
  * @brief      Get APU sampleing rate
  * @param[in]  apu Base address of APU module
  * @return     Real sample rate value
   */
uint32_t APU_GetSampleRate(APU_T *apu)
{
	uint32_t u32Clk;
	u32Clk = TIMER_GetModuleClock(TIMER0);
	
	return (u32Clk/25/TIMER0->CMP);
}

/*@}*/ /* end of group N572_APU_EXPORTED_FUNCTIONS */

/*@}*/ /* end of group N572_APU_Driver */

/*@}*/ /* end of group N572_Device_Driver */

/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
