📄 i2s.c
字号:
/*****************************************************************************
* i2s.c: - for NXP LPC23xx/24xx Family Microprocessors
* I2S Audio demo with UDA1341
* this file contains configuration and control files for i2s interface
* on LPC23xx
*
* Copyright(C) 2007, NXP Semiconductor
* All rights reserved.
*
* History
* 2007.02.20 ver 1.00 Prelimnary version, first Release
*
******************************************************************************/
#include "LPC23xx.h" /* LPC23xx/24xx definitions */
#include "type.h" /* LPC23xx/24xx definitions */
#include "irq.h" /* LPC23xx/24xx definitions */
#include "timer.h" /* LPC23xx/24xx definitions */
extern void IRQ_I2S_service(void);
/* functions in this file - table of contents/prototypes */
int i2s_init(void);
void i2s_start_clock(void);
void i2s_stop_clock(void);
int i2s_config_rxrate(unsigned int bitrate, unsigned int pclk_i2s_freq);
int i2s_config_txrate(unsigned int bitrate, unsigned int pclk_i2s_freq);
void i2s_enable_tx_irq(int flag);
void i2s_enable_rx_irq(int flag);
void i2s_set_tx_depth_irq(int depth);
void i2s_set_rx_depth_irq(int depth);
void I2S_Enable_DAIO(int flag);
void I2S_Enable_DAI(int flag);
void I2S_Enable_DAO(int flag);
void I2S_Mute_DAO(int flag);
void I2S_Config_DAI(unsigned int wordwidth, unsigned int mono,
unsigned int ws_sel, unsigned int ws_halfperiod);
void I2S_Config_DAO(unsigned int wordwidth, unsigned int mono,
unsigned int ws_sel, unsigned int ws_halfperiod);
__irq void IRQ_I2S_Handler(void);
/*******************************************************************************
* Function: init_i2s
*
*******************************************************************************/
int i2s_init(void)
{
/*set (pclk_i2s pclk = cclk) = 32.7619Mhz */
PCLKSEL1 &= ~(0x03 << 22); /* clear I2S clock register */
PCLKSEL1 |= (0x01 << 22); /* set I2S clock register */
/*enable I2S in the PCONP register. I2S is disabled on reset*/
PCONP |= (1 << 27);
/*connect the I2S sigals to port pins(P0.4-P0.9)*/
// PINSEL0 &= ~0x00055500; /* clear port pins */
PINSEL0 |= 0x00055500; /* set port pins */
return(1);
}
/*******************************************************************************
* Function: i2s_start_clock
*
*******************************************************************************/
void i2s_start_clock(void)
{
/*enable I2S in the PCONP register. I2S is disabled on reset*/
PCONP |= (1 << 27);
}
/*******************************************************************************
* Function: i2s_stop_clock
*
*******************************************************************************/
void i2s_stop_clock(void)
{
/*enable I2S in the PCONP register. I2S is disabled on reset*/
PCONP &= ~(1 << 27);
}
/*******************************************************************************
* Function: i2s_config_rxrate
*
*******************************************************************************/
int i2s_config_rxrate(unsigned int bitrate, unsigned int pclk_i2s_freq)
{
if(pclk_i2s_freq <= bitrate)
{
I2S_RXRATE = 0;
}
else
{
I2S_RXRATE = (pclk_i2s_freq / bitrate)-1 ;
// I2S_RXRATE = (pclk_i2s_freq / bitrate) ;
}
return (1);
}
/*******************************************************************************
* Function: i2s_config_txrate
*
*******************************************************************************/
int i2s_config_txrate(unsigned int bitrate, unsigned int pclk_i2s_freq)
{
if(pclk_i2s_freq <= bitrate)
{
I2S_TXRATE = 0;
}
else
{
I2S_TXRATE = (pclk_i2s_freq / bitrate) -1;
}
return (1);
}
/*******************************************************************************
* Function: i2s_enable_tx_irq
*
*******************************************************************************/
void i2s_enable_tx_irq(int flag)
{
if(flag == 0)
{
I2S_IRQ &= ~2;
}
else
{
I2S_IRQ |= 2;
}
}
/*******************************************************************************
* Function: i2s_enable_rx_irq
*
*******************************************************************************/
void i2s_enable_rx_irq(int flag)
{
if(flag == 0)
{
I2S_IRQ &= ~1;
}
else
{
I2S_IRQ |= 1;
}
}
/*******************************************************************************
* Function: i2s_set_tx_depth_irq
*
*******************************************************************************/
void i2s_set_tx_depth_irq(int depth)
{
I2S_IRQ &= ~(0xFF << 16);
I2S_IRQ |= (depth << 16);
}
/*******************************************************************************
* Function: i2s_set_rx_depth_irq
*
*******************************************************************************/
void i2s_set_rx_depth_irq(int depth)
{
depth &= 0xFF;
I2S_IRQ &= ~(0xFF << 8);
I2S_IRQ |= (depth << 8);
}
/*******************************************************************************
* Function: I2S_Enable_DAIO
*
*******************************************************************************/
void I2S_Enable_DAIO(int flag)
{
if(flag == 0)
{
I2S_DAI |= 0x18; /* stop and reset */
I2S_DAO |= 0x18;
I2S_DAO &= ~0x10;
I2S_DAI &= ~0x10; /* release reset */
}
else
{
I2S_DAO &= ~0x18;
I2S_DAI &= ~0x18; /* go and release reset */
}
}
/*******************************************************************************
* Function: I2S_Enable_DAI
*
*******************************************************************************/
void I2S_Enable_DAI(int flag)
{
if(flag == 0)
{
I2S_DAI |= 0x18; /* stop and reset */
I2S_DAI &= ~0x10; /* release reset */
}
else
{
I2S_DAI &= ~0x18; /* go and release reset */
}
}
/*******************************************************************************
* Function: I2S_Enable_DAO
*
*******************************************************************************/
void I2S_Enable_DAO(int flag)
{
if(flag == 0)
{
I2S_DAO |= 0x18; /* stop and reset */
I2S_DAO &= ~0x10; /* release reset */
}
else
{
I2S_DAO &= ~0x18; /* go and release reset */
}
}
/*******************************************************************************
* Function: I2S_Mute_DAO
*
*******************************************************************************/
void I2S_Mute_DAO(int flag)
{
if(flag == 0)
{
I2S_DAO &= ~(0x8000);
}
else
{
I2S_DAO |= 0x8000;
}
}
/*******************************************************************************
* Function: config_i2s_dai
*
*******************************************************************************/
void I2S_Config_DAI(unsigned int wordwidth, unsigned int mono,
unsigned int ws_sel, unsigned int ws_halfperiod)
{
I2S_DAI = 0x18 | wordwidth | (mono << 2) | (ws_sel << 5) | (ws_halfperiod << 6);
/* 0x18 = stop and reset(async) Rx channel & FIFO */
/* ws_sel determines master/slave operation */
}
/*******************************************************************************
* Function: I2S_Config_DAO
*
*******************************************************************************/
void I2S_Config_DAO(unsigned int wordwidth, unsigned int mono,
unsigned int ws_sel, unsigned int ws_halfperiod)
{
I2S_DAO = 0x18 | wordwidth | (mono << 2) | (ws_sel << 5) | (ws_halfperiod << 6);
/* 0x18 = stop and reset(async) Tx channel & FIFO */
/* ws_sel determines master/slave operation */
}
/*******************************************************************************
* Function: i2s_IRQ_Handler
*
*******************************************************************************/
__irq void IRQ_I2S_Handler(void)
{
IRQ_I2S_service();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -