/**************************************************************************//**
 * @file     apu.h
 * @version  V1.00
 * $Revision: 1 $
 * $Date: 14/07/30 13:27p $
 * @brief    N572 Series APU Driver Header File
 *
 * @note
 * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
 *
 ******************************************************************************/
#ifndef __APU_H__
#define __APU_H__

#ifdef __cplusplus
extern "C"
{
#endif

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

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


/** @addtogroup N572_APU_EXPORTED_CONSTANTS APU Exported Constants
  @{
*/

#define APU_NONBYPASS_PA				(0x0UL << APU_CTL_BPPAM_Pos)			 /*!< SPK+/DAC pin is one of power amplifier output  \hideinitializer */
#define APU_BYPASS_PA					(0x1UL << APU_CTL_BPPAM_Pos)			 /*!< SPK+/DAC pin is current DAC output, no output on SPK-  \hideinitializer */

#define APU_INT_THD0				(0x0)				/*!< APU Interrupt Threshold 0  \hideinitializer */
#define APU_INT_THD1				(0x1)				/*!< APU Interrupt Threshold 0  \hideinitializer */
#define APU_INT_THD2				(0x2)				/*!< APU Interrupt Threshold 0  \hideinitializer */
#define APU_INT_THD3				(0x3)				/*!< APU Interrupt Threshold 0  \hideinitializer */
#define APU_INT_THD4				(0x4)				/*!< APU Interrupt Threshold 0  \hideinitializer */
#define APU_INT_THD5				(0x5)				/*!< APU Interrupt Threshold 0  \hideinitializer */
#define APU_INT_THD6				(0x6)				/*!< APU Interrupt Threshold 0  \hideinitializer */
#define APU_INT_THD7				(0x7)				/*!< APU Interrupt Threshold 0  \hideinitializer */

/*@}*/ /* end of group N572_APU_EXPORTED_CONSTANTS */


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

/**
  * @brief      Enable power amplifier function.
  * @param[in]  apu The base address of APU module
  * @return     None
  */
#define APU_ENABLE_PA(apu)		(apu->CTL |= APU_CTL_PAMPE_Msk)

/**
  * @brief      Disable power amplifier function.
  * @param[in]  apu The base address of APU module
  * @return     None
  */
#define APU_DISABLE_PA(apu)		(apu->CTL &= ~APU_CTL_PAMPE_Msk)

/**
  * @brief      Enable DAC function.
  * @param[in]  apu The base address of APU module
  * @return     None
  */
#define APU_ENABLE_DAC(apu)		(apu->CTL |= (APU_CTL_DACE_Msk|APU_CTL_DACGN_Msk))

/**
  * @brief      Disable power amplifier function.
  * @param[in]  apu The base address of APU module
  * @return     None
  */
#define APU_DISABLE_DAC(apu)	(apu->CTL &= ~(APU_CTL_DACE_Msk|APU_CTL_DACGN_Msk))

/**
  * @brief      Set APU output pin whether bypass power amplifier or not.
  * @param[in]  apu The base address of APU module
  * @param[in]  u32Bypass bypass power amplifier selection
  *             - \ ref APU_NONBYPASS_PA
  *             - \ ref APU_BYPASS_PA
  * @return     None
  */
#define APU_SET_BYPASSPA(apu, u32Bypass)	(apu->CTL = (apu->CTL&~APU_CTL_BPPAM_Msk)|u32Bypass)

/**
  * @brief     Enable threshold interrupt
  * @param[in] apu The base address of APU module
  * @return    None
  */
#define APU_ENABLE_INT(apu)		(apu->CTL |= APU_CTL_APUIE_Msk)

/**
  * @brief     Disable threshold interrupt
  * @param[in] apu The base address of APU module
  * @return    None
  */
#define APU_DISABLE_INT(apu)		(apu->CTL &= ~APU_CTL_APUIE_Msk)

/**
  * @brief     Clear interrupt status bits
  * @param[in] apu The base address of APU module
  * @return    None
  */
#define APU_CLR_INT_FLAG(apu) 	(apu->CTL &= ~APU_CTL_APUIS_Msk)

/**
  * @brief     Return the interrupt flags
  * @param[in] apu The base address of APU module
  * @return    interrupt flags. 1: APU threshold interrupt occur; 0: opposite.
  */
#define APU_GET_INT_FLAG(apu)		(apu->CTL & (APU_CTL_APUIS_Msk))

/**
  * @brief     Set APU Interrupt of buffer threshold
  * @param[in] apu The base address of APU module
  * @param[in] u8Thd APU interrupt threshold value
  *            - \ ref APU_INT_THD0
  *            - \ ref APU_INT_THD1
  *            - \ ref APU_INT_THD2
  *            - \ ref APU_INT_THD3
  *            - \ ref APU_INT_THD4
  *            - \ ref APU_INT_THD5
  *            - \ ref APU_INT_THD6
  *            - \ ref APU_INT_THD7
  * @return    None
  */
#define APU_SET_INT_THD(apu, u8Thd)		(apu->CTL = (apu->CTL&~APU_CTL_TSHD_Msk)|u8Thd)		

/**
  * @brief     Return the Interrupt threshold value
  * @param[in] apu The base address of APU module
  * @return    threshold value. 
  */
#define APU_GET_INT_THD(apu)		(apu->CTL & APU_CTL_TSHD_Msk)

/**
  * @brief     Set audio pcm data to APU channel0 buffer
  * @param[in] apu The base address of APU module
  * @param[in] u8BufIdx index number of PCM buffer (0~7, 8 level)
  * @param[in] i16Pcm pcm data (2' complement)
  * @return    None. 
  * @note      User needs to take care of the effective bit of PCM because the
  *            H/W Mixer output (channel0+channel1)is clipped to 13 bits automatically.
  */
#define APU_SET_CH0DATA(apu, u8BufIdx, i16Pcm)		(apu->CH0DAT[u8BufIdx] = (uint32_t)i16Pcm)

/**
  * @brief     Get audio pcm data to APU channel0 buffer
  * @param[in] apu The base address of APU module
  * @param[in] u8BufIdx index number of PCM buffer (0~7, 8 level)
  * @return    pcm data (2' complement). 
  */
#if (__CHIP_SERIES__ == __N572F072_SERIES__)
#define APU_GET_CH0DATA(apu, u8BufIdx)		((int16_t)(apu->CH0DAT[u8BufIdx]))
#else
#define APU_GET_CH0DATA(apu, u8BufIdx)		(((int16_t)((apu->CH0DAT[u8BufIdx]&APU_CH0DAT_PCM_Msk)<<3))>>3)
#endif
/**
  * @brief     Set audio pcm data and DC bias value to APU channel1 buffer
  * @param[in] apu The base address of APU module
  * @param[in] i16Pcm pcm data (2' complement), effective bit is 13.
  * @param[in] i16DCBias dc compensation data (2' complement), user can fill
  *            channel1 to compensate dc offset value. For example: channel0 source is
  *            from ADC.
  * @return    None. 
  * @note      Channel 1 has only one level buffer. No H/W timer is associated
  *						 with it. 
  */
#define APU_SET_CH1DATA(apu, i16Pcm, i16DCBias)		(apu->CH1DAT = (uint32_t)(i16Pcm+i16DCBias))

/**
  * @brief     Get audio pcm data to APU channel1 buffer
  * @param[in] apu The base address of APU module
  * @return    pcm data (2' complement). 
  */
#if (__CHIP_SERIES__ == __N572F072_SERIES__)
#define APU_GET_CH1DATA(apu)		((int16_t)(apu->CH1DAT))
#else
#define APU_GET_CH1DATA(apu)		(((int16_t)((apu->CH1DAT&APU_CH1DAT_PCM_Msk)<<3))>>3)
#endif
/**
  * @brief     APU start to play via timer0 sampling
  * @param[in] apu The base address of APU module
  * @return    None.
  */
#define APU_START_PLAY(apu)    TIMER_Start(TIMER0)

/**
  * @brief     APU stop to play 
  * @param[in] apu The base address of APU module
  * @return    None.
  */
#define APU_STOP_PLAY(apu)     TIMER_Stop(TIMER0)

void APU_Open(APU_T *apu); 
void APU_Close(APU_T *apu);
int32_t APU_SetVolGaindB(APU_T *apu, int32_t i32GainIndB);
int32_t APU_GetVolGaindB(APU_T *apu);
uint32_t APU_SetSampleRate(APU_T *apu, uint32_t u32SampleRate);
uint32_t APU_GetSampleRate(APU_T *apu);

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

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

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

#ifdef __cplusplus
}
#endif

#endif //__APU_H__

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

