⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2s_codec.c

📁 stm32.rar
💻 C
📖 第 1 页 / 共 4 页
字号:
  /* 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  : Media_ReadHalfWord
* 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)
{ 
  GPIO_InitTypeDef GPIO_InitStructure; 

  /* 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);

      /* Enable FSMC Bank1_NOR Bank */
      FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE);

      /* NE2 configuration */
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
      GPIO_Init(GPIOG, &GPIO_InitStructure);
      
      tmpvar =  (*(vu16 *) (AudioFileAddress + Offset));

      /* Disable FSMC Bank1_NOR Bank */
      FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, DISABLE);

      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
      GPIO_Init(GPIOG, &GPIO_InitStructure);
      GPIO_SetBits(GPIOG, GPIO_Pin_9);
       
      /* 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  : Media_ReadByte
* 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;
  GPIO_InitTypeDef GPIO_InitStructure; 
    
  /* Enable the FSMC that share a pin w/ I2C1 (LBAR) */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);

  /* Enable FSMC Bank1_NOR Bank */
  FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE);

  /* NE2 configuration */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_Init(GPIOG, &GPIO_InitStructure);

  /* Read data from the specified location */
  tmp =  (*(vu8 *) (AudioFileAddress + Offset));

  /* Disable FSMC Bank1_NOR Bank */
  FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, DISABLE);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOG, &GPIO_InitStructure);
  GPIO_SetBits(GPIOG, GPIO_Pin_9);
       
  /* 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)
{ 
#if STM3210E
  GPIO_InitTypeDef GPIO_InitStructure; 

  /* Enable the FSMC that share a pin w/ I2C1 (LBAR) */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);

  /* Initialize the FSMC NOR Flash Interface */
  FSMC_NOR_Init();

  /* Set the NOR read modes */  
  FSMC_NOR_ReturnToReadMode();

  /* Disable FSMC Bank1_NOR Bank */
  FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, DISABLE);

  /* NE2 configuration */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_Init(GPIOG, &GPIO_InitStructure);
  GPIO_SetBits(GPIOG, GPIO_Pin_9);
       
  /* Disable the FSMC that share a pin w/ I2C1 (LBAR) */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
#endif

  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)
{ 
#if STM3210E
  GPIO_InitTypeDef GPIO_InitStructure; 
    
  /* Enable the FSMC that share a pin w/ I2C1 (LBAR) */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);

  /* Enable FSMC Bank1_NOR Bank */
  FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE);

  /* NE2 configuration */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_Init(GPIOG, &GPIO_InitStructure);

  /* Read the data */
  while(NumByteToRead--)
  {
    *pBuffer++ = *(vu8 *)ReadAddr++;
  }

  /* Disable FSMC Bank1_NOR Bank */
  FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, DISABLE);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOG, &GPIO_InitStructure);
  GPIO_SetBits(GPIOG, GPIO_Pin_9);
       
  /* Disable the FSMC that share a pin w/ I2C1 (LBAR) */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
#else
  SPI_FLASH_BufferRead(pBuffer, ReadAddr, NumByteToRead);
#endif
}

/*******************************************************************************
* 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--);
}

/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -