📄 i2s_codec.c
字号:
/* Command the sending of dummy data */
SetVar_SendDummyData();
/* Enable the I2S2 TXE Interrupt => Generate the clocks*/
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, ENABLE);
/* ReSelect the MCKI frequency (FS0-1 bits): 256.Fs */
counter += CODEC_WriteRegister(0x05, 0x02 );
/* Set up the path "DAC->Speaker-Amp" with no power save (DACS and SPPSN bits) */
counter += CODEC_WriteRegister(0x02, 0x20 );
/* Speaker Gain (SPKG0-1 bits): Gain=+10.65dB(ALC off)/+12.65(ALC on) */
counter += CODEC_WriteRegister(0x03, 0x10);
/* Extra Configuration (of the ALC) */
counter += CODEC_WriteRegister(0x06, 0x3C );
counter += CODEC_WriteRegister(0x08, 0xE1 );
counter += CODEC_WriteRegister(0x0B, 0x00 );
counter += CODEC_WriteRegister(0x07, 0x20 );
counter += CODEC_WriteRegister(0x09, 0x91 );
counter += CODEC_WriteRegister(0x0C, 0x91 );
/* Left Channel Digital Volume control */
counter += CODEC_WriteRegister(0x0A, Volume);
/* Right Channel Digital Volume control */
counter += CODEC_WriteRegister(0x0D, Volume);
/* Power up Speaker and DAC (PMSPK and PMDAC bits)*/
counter += CODEC_WriteRegister(0x00, 0x54);
/* Set up the path "DAC -> Speaker-Amp" with no power save */
counter += CODEC_WriteRegister(0x02, 0xA0 /*0xA1*/);
}
/* Disable the I2S2 TXE Interrupt */
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, DISABLE);
/* Disable the sending of Dummy data */
ResetVar_SendDummyData();
/* Return the counter value */
return counter;
}
/*******************************************************************************
* Function Name : CODEC_WriteRegister
* Description : Writes a value in a register of the audio Codec through I2C.
* Input : - RegisterAddr: The target register adress (between 00x and 0x24)
* : - RegisterValue: The target register value to be written
* : - Verify: 0-> Don't verify the written data, 1-> Verify the written data
* Output : None
* Return : - 0 -> Correct write operation
* : - !0 -> Incorrect write operation
*******************************************************************************/
u32 CODEC_WriteRegister(u32 RegisterAddr, u32 RegisterValue)
{
u32 read_verif = 0;
/* Reset all I2C2 registers */
I2C_SoftwareResetCmd(I2C1, ENABLE);
I2C_SoftwareResetCmd(I2C1, DISABLE);
/* Enable the I2C1 peripheral */
I2C_Cmd(I2C1, ENABLE);
/* Configure the I2C peripheral */
I2C_Config();
/* Begin the config sequence */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
{}
/* Transmit the slave address and enable writing operation */
I2C_Send7bitAddress(I2C1, CODEC_ADDRESS, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{}
/* Transmit the first address for r/w operations */
I2C_SendData(I2C1, RegisterAddr);
/* Test on EV8 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{}
/* Prepare the register value to be sent */
I2C_SendData(I2C1, RegisterValue);
/* Test on EV8 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{}
/* End the configuration sequence */
I2C_GenerateSTOP(I2C1, ENABLE);
/* Verify (if needed) that the loaded data is correct */
#ifdef VERIFY_WRITTENDATA
/* Read the just written register*/
read_verif = CODEC_ReadRegister(RegisterAddr);
/* Load the register and verify its value */
if (read_verif != RegisterValue)
{
/* Control data wrongly tranfered */
read_verif = 1;
}
else
{
/* Control data correctly transfered */
read_verif = 0;
}
#endif
/* Return the verifying value: 0 (Passed) or 1 (Failed) */
return read_verif;
}
/*******************************************************************************
* Function Name : CODEC_ReadRegister
* Description : Reads a register of the audio Codec through I2C.
* Input : - RegisterAddr: The target register adress (between 00x and 0x24)
* Output : None
* Return : The value of the read register
*******************************************************************************/
u32 CODEC_ReadRegister(u32 RegisterAddr)
{
u32 tmp = 0;
/* Disable the I2C1 peripheral */
I2C_Cmd(I2C1, DISABLE);
/* Reset all I2C2 registers */
I2C_SoftwareResetCmd(I2C1, ENABLE);
I2C_SoftwareResetCmd(I2C1, DISABLE);
/* Configure the I2C peripheral */
I2C_Config();
/* Enable the I2C peripheral */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
{}
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C1, DISABLE);
/* Transmit the slave address and enable writing operation */
I2C_Send7bitAddress(I2C1, CODEC_ADDRESS, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{}
/* Transmit the first address for r/w operations */
I2C_SendData(I2C1, RegisterAddr);
/* Test on EV8 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{}
/* Regenerate a start condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
{}
/* Transmit the slave address and enable writing operation */
I2C_Send7bitAddress(I2C1, CODEC_ADDRESS, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{}
/* Test on EV7 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
{}
/* End the configuration sequence */
I2C_GenerateSTOP(I2C1, ENABLE);
/* Load the register value */
tmp = I2C_ReceiveData(I2C1);
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C1, ENABLE);
/* Return the read value */
return tmp;
}
/*******************************************************************************
* Function Name : ReadUnit
* Description : Reads a number of bytes from the SPI Flash and reorder them
* in Big or little endian.
* Input : - NbrOfBytes : number of bytes to read.
* This parameter must be a number between 1 and 4.
* - ReadAddr : external memory address to read from.
* - Endians : specifies the bytes endianness.
* This parameter can be one of the following values:
* - LittleEndian
* - BigEndian
* Output : None
* Return : Bytes read from the SPI Flash.
*******************************************************************************/
static u32 ReadUnit(u8 NbrOfBytes, Endianness BytesFormat)
{
u32 index = 0;
u32 Temp = 0;
if (BytesFormat == LittleEndian)
{
for (index = 0; index < NbrOfBytes; index++)
{
Temp |= AudioFileHeader[HeaderTabIndex++] << (index * 8);
}
}
else
{
for (index = NbrOfBytes; index != 0; index--)
{
Temp |= AudioFileHeader[HeaderTabIndex++] << ((index - 1) * 8);
}
}
return Temp;
}
/*******************************************************************************
* Function Name : I2S_CODEC_MediaReadHalfWord
* Description : Read one half word from the media (SPI_Flash/NOR/NAND memories..)
* Input : - Offset: the adress offset for read operation
* Output : None.
* Return : Data read from the media memory.
*******************************************************************************/
u16 Media_ReadHalfWord(u32 Offset)
{
/* Test if the left channel is to be sent */
if (monovar == 0)
{
/* Enable the FSMC that share a pin w/ I2C1 (LBAR) */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
tmpvar = (*(vu16 *) (AudioFileAddress + Offset));
/* Disable the FSMC that share a pin w/ I2C1 (LBAR) */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
/* Increment the mono variable only if the file is in mono format */
if (WAVE_Format.NumChannels == Channel_MONO)
{
/* Increment the monovar variable */
monovar++;
}
/* Return the read value */
return tmpvar;
}
/* Right channel to be sent in mono format */
else
{
/* Reset the monovar variable */
monovar = 0;
/* Return the previous read data in mono format */
return tmpvar;
}
}
/*******************************************************************************
* Function Name : I2S_CODEC_MediaReadByte
* Description : Read one byte from the media (SPI_Flash/NOR/NAND memories..)
* Input : - Offset: the adress offset for read operation
* Output : None.
* Return : Data read from the media memory.
*******************************************************************************/
u8 Media_ReadByte(u32 Offset)
{
u8 tmp = 0;
/* Enable the FSMC that share a pin w/ I2C1 (LBAR) */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
/* Read data from the specified location */
tmp = (*(vu8 *) (AudioFileAddress + Offset));
/* Disable the FSMC that share a pin w/ I2C1 (LBAR) */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
return tmp;
}
/*******************************************************************************
* Function Name : Media_Init
* Description : Read one byte from the media (SPI_Flash/NOR/NAND memories..)
* Input : - Offset: the adress offset for read operation
* Output : None.
* Return : - 0 if initialization is OK
* - 1 if initialization failed..
*******************************************************************************/
u32 Media_Init(void)
{
return 0;
}
/*******************************************************************************
* Function Name : Media_BufferRead
* Description : Read a buffer from the memory media
* Input : - pBuffer: Destination buffer address
* : - ReadAddr: start reading position
* : - NumByteToRead: size of the buffer to read
* Output : None.
* Return : None.
*******************************************************************************/
void Media_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)
{
/* Enable the FSMC that share a pin w/ I2C1 (LBAR) */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
/* Read the data */
while (NumByteToRead--)
{
*pBuffer++ = *(vu8 *)ReadAddr++;
}
/* Disable the FSMC that share a pin w/ I2C1 (LBAR) */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
}
/*******************************************************************************
* Function Name : Media_StartReadSequence
* Description : Initialize reading sequence on the media.
* Input : - ReadAddr: start reading position
* Output : None.
* Return : None.
*******************************************************************************/
void Media_StartReadSequence(u32 ReadAddr)
{
/* This function could be used for memories needing a start read sequence
like SPI_Flash memory */
}
/*******************************************************************************
* Function Name : I2S_CODEC_DataTransfer
* Description : Sends the audio data using the SPI2 peripheral and checks the
* : audio playing status (if a command (Pause/Stop) is pending
* : the playing status is updated). If the TXE flag interrupt
* : is used to synchronize data sending, this function should be
* : called in the SPI2 ISR.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void I2S_CODEC_DataTransfer(void)
{
/* Audio codec configuration section -------------------------------------*/
if (GetVar_SendDummyData() == 1)
{
/* Send a dummy data just to generate the I2S clock */
SPI_I2S_SendData(SPI2, DUMMY_DATA);
}
/* Audio codec communication section -------------------------------------*/
else
{
/* Send the data read from the memory */
SPI_I2S_SendData(SPI2, (Media_ReadHalfWord(AudioDataIndex)));
/* Increment the index */
IncrementVar_AudioDataIndex(WAVE_Format.NumChannels);
/* Check and update the stream playing status */
I2S_CODEC_UpdateStatus();
}
}
/*******************************************************************************
* Function Name : delay
* Description : Inserts a delay time.
* Input : nCount: specifies the delay time length
* Output : None
* Return : The length of the wave file read from the SPI_Flash
*******************************************************************************/
void delay(vu32 nCount)
{
for (; nCount != 0; nCount--);
}
#endif
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -