Motion_EC_HC32_archived/Libraries/HC32L073_StdPeriph_Driver/src/i2s.c

322 lines
12 KiB
C
Raw Normal View History

2022-01-10 16:20:39 +08:00
/******************************************************************************
* Copyright (C) 2019, Huada Semiconductor Co.,Ltd All rights reserved.
*
* This software is owned and published by:
* Huada Semiconductor Co.,Ltd ("HDSC").
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with HDSC
* components. This software is licensed by HDSC to be adapted only
* for use in systems utilizing HDSC components. HDSC shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. HDSC is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* Disclaimer:
* HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
* REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
* ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
* WARRANTY OF NONINFRINGEMENT.
* HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
* NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
* LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
* LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
* INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
* SAVINGS OR PROFITS,
* EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
* INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
* FROM, THE SOFTWARE.
*
* This software may be replicated in part or whole for the licensed use,
* with the restriction that this Disclaimer and Copyright notice must be
* included with each copy of this software, whether used in part or whole,
* at all times.
*/
/******************************************************************************/
/** \file i2s.c
**
** I2S driver API.
**
** - 2019-07-05 lsq First Version
**
******************************************************************************/
/******************************************************************************
* Include files
******************************************************************************/
#include "i2s.h"
/**
******************************************************************************
** \addtogroup AdcGroup
******************************************************************************/
//@{
/******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
/******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
/******************************************************************************
* Local function prototypes ('static')
******************************************************************************/
/******************************************************************************
* Local variable definitions ('static')
******************************************************************************/
/*****************************************************************************
* Function implementation - global ('extern') and local ('static')
*****************************************************************************/
/**
******************************************************************************
* @brief 使I2Sx
* @param i2sx: M0P_I2S0M0P_I2S1
* @param i2s_it:
* @arg I2S_IT_TXE: 使
* @arg I2S_IT_RXNEIE:使
* @arg I2S_IT_ERRIE: 使
* @param NewState: =ENALE DISABLE
* @retval None
**
******************************************************************************/
void I2S_ConfIt(M0P_I2S_TypeDef *i2sx, uint8_t i2s_it, en_en_state_t NewState)
{
uint32_t itmark=0;
itmark=1<<i2s_it;
if(NewState == ENABLE) //使能中断
{
i2sx->CR |= itmark;
}
else if(NewState == DISABLE) //禁止中断
{
i2sx->CR &= ~itmark;
}
else
{
;
}
}
/**
******************************************************************************
** \brief I2SDMA使
**
* @param i2sx: M0P_I2S0M0P_I2S1
* @param i2s_it:
* @arg I2S_RDMA_EN: DMA使
* @arg I2S_LDMA_EN: DMA使
* @param NewState: =ENALE DISABLE
** \retval none
**
******************************************************************************/
void I2s_ConfDma(M0P_I2S_TypeDef *i2sx, uint8_t rl_dma_en, en_en_state_t NewState)
{
uint32_t itmark=0;
itmark = 1<<rl_dma_en;
if(NewState == ENABLE) //使能左声道或右声道DMA
{
i2sx->CR |= itmark;
}
else if(NewState == DISABLE) //禁止左声道或右声道DMA
{
i2sx->CR &= ~itmark;
}
else
{
;
}
}
/**
******************************************************************************
** \brief I2S
**
* @param i2sx: M0P_I2S0M0P_I2S1
* @param i2s_status:
* @arg I2S_RXNE_L:
* @arg I2S_TXE_L:
* @arg I2S_UDR_L:
* @arg I2S_UDR_R:
* @arg I2S_OVR_L:
* @arg I2S_BSY:
* @arg I2S_FRE:
* @arg I2S_OVR_R:
* @arg I2S_RXNE_R:
* @arg I2S_TXE_R:
** \retval RESET SET
**
******************************************************************************/
en_flag_status_t I2s_GetStatus(M0P_I2S_TypeDef *i2sx, uint8_t i2s_status)
{
uint16_t itmark=0;
en_flag_status_t bitstatus;
itmark=(uint16_t)1<<i2s_status;
if(!(i2sx->SR & itmark))
{
bitstatus = RESET;
}
else
{
bitstatus = SET;
}
return bitstatus;
}
/**
******************************************************************************
** \brief I2S
**
* @param i2sx: M0P_I2S0M0P_I2S1
* @param i2s_status:
* @arg I2S_FLAG_UDF:
* @arg I2S_FLAG_OVR:
* @arg I2S_FLAG_FRE:
** \retval none
**
******************************************************************************/
void I2s_ClearITPendingBit(M0P_I2S_TypeDef *i2sx, uint8_t i2s_it_flag)
{
uint32_t bitstatus;
bitstatus = 1<<i2s_it_flag;
i2sx->ICR &= ~bitstatus;
}
/**
******************************************************************************
** \brief DRL
**
* @param i2sx: M0P_I2S0M0P_I2S1
* @param Data: 16
** \retval none
**
******************************************************************************/
void I2s_SendDataL(M0P_I2S_TypeDef *i2sx, uint16_t Data)
{
i2sx->DRL_f.DRL = Data;
}
/**
******************************************************************************
** \brief DRR
**
* @param i2sx: M0P_I2S0M0P_I2S1
* @param Data: 16
** \retval none
**
******************************************************************************/
void I2s_SendDataR(M0P_I2S_TypeDef *i2sx, uint16_t Data)
{
i2sx->DRR_f.DRR = Data;
}
/**
******************************************************************************
** \brief DRL
**
* @param i2sx: M0P_I2S0M0P_I2S1
** \retval
**
******************************************************************************/
uint16_t I2s_ReceiveDataL(M0P_I2S_TypeDef *i2sx)
{
uint16_t data;
data = (uint16_t)i2sx->DRL;
return data;
}
/**
******************************************************************************
** \brief DRR
**
* @param i2sx: M0P_I2S0M0P_I2S1
** \retval
**
******************************************************************************/
uint16_t I2s_ReceiveDataR(M0P_I2S_TypeDef *i2sx)
{
uint16_t data;
data = (uint16_t)i2sx->DRR;
return data;
}
/**
******************************************************************************
** \brief I2Sx
** ()FRACT=0:
** (1)(MCKOE=1)
** 16=I2SxCLK/[(16*2)*(2*I2SDIV+ODD)*8]
** 32=I2SxCLK/[(32*2)*(2*I2SDIV+ODD)*4]
** (2)(MCKOE=0)
** 16=I2SxCLK/[(16*2)*(2*I2SDIV+ODD)]
** 32=I2SxCLK/[(32*2)*(2*I2SDIV+ODD)]
** ()FRACT=0:
** (1)(MCKOE=1)
** 16=I2SxCLK/[(16*2)*(2*(I2SDIV+FRACT/64))*8]
** 32=I2SxCLK/[(32*2)*(2*(I2SDIV+FRACT/64))*4]
** (2)(MCKOE=0)
** 16=I2SxCLK/[(16*2)*(2*(I2SDIV+FRACT/64))]
** 32=I2SxCLK/[(32*2)*(2*(I2SDIV+FRACT/64))]
** I2SDIVFRACTI2SODD
* @param i2sx: M0P_I2S0M0P_I2S1
* @param i2s_conf
** \retval
**
******************************************************************************/
void I2s_Init(M0P_I2S_TypeDef *i2sx, stc_i2s_config_t *i2s_conf)
{
i2sx->CFGR_f.CFG = i2s_conf->i2s_Mode; //设置模式
i2sx->CFGR_f.PCMSYNC = i2s_conf->i2s_PcmSync; //PCM帧同步位 只有在I2SSTD=3的情况下该位才有意义
i2sx->CFGR_f.CKSEL = i2s_conf->i2s_Cksel; //主模式下I2S始终选择0PCLK 1:HCLK
i2sx->CFGR_f.STD = i2s_conf->i2s_Std; //标准选择 0I2S Philips 1:MAS左对齐 2LSB右对齐 3PCM标准
i2sx->CFGR_f.DATLEN = i2s_conf->i2s_Datalen; //要传输的数据长度
i2sx->CFGR_f.CHIEN = i2s_conf->i2s_Chlen; //每个音频通道的位数016位 132位
i2sx->PR_f.MCKOE = i2s_conf->i2s_Mckoe; //主时钟MCK输出使能
i2sx->PR_f.I2SDIV = i2s_conf->i2s_Div;
i2sx->PR_f.FRACT = i2s_conf->i2s_Fract;
i2sx->PR_f.ODD = i2s_conf->i2s_Odd;
}
/**
******************************************************************************
** \brief 使I2Sx
**
* @param i2sx: M0P_I2S0M0P_I2S1
**
* @param NewState :EANBLE DISABLE
** \retval
**
******************************************************************************/
void I2S_Cmd(M0P_I2S_TypeDef *i2sx, en_en_state_t NewState)
{
i2sx->CFGR_f.E = NewState;
}
/******************************************************************************
* EOF (not truncated)
******************************************************************************/