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

📄 i2s_codec.c

📁 STM32XXXX usb从设备和住设备的应用源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
* Input          : - CodecPowerDown_Mode: could be CodecPowerDown_SW for power down
*                :   after communication, CodecPowerDown_HW simply shut down the codec
* Output         : None
* Return         : None
*******************************************************************************/
void I2S_CODEC_PowerDown(u32 CodecPowerDown_Mode)
{
  if (CodecPowerDown_Mode == CodecPowerDown_SW)
  {
    /* Power down the DAC and the speaker (PMDAC and PMSPK bits)*/
    (void)CODEC_WriteRegister(0x00, 0x40);
    /* Power down the VCOM*/
    (void)CODEC_WriteRegister(0x00, 0x00);
  }
  else /* CodecPowerDown_HW */
  {
    /* Power Down the Codec */
    GPIO_ResetBits(Codec_PDN_GPIO, Codec_PDN_Pin);
  }
}

/*******************************************************************************
* Function Name  : I2S_CODEC_Reset
* Description    : Reset the Audio Codec.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void I2S_CODEC_Reset(void)
{
  /* Power Down the Codec */
  GPIO_ResetBits(Codec_PDN_GPIO, Codec_PDN_Pin);

  /* wait for a delay to allow registers erasing */
  delay(0xFF);

  /* Power On the Codec after the power off => all registers are reinitialized*/
  GPIO_SetBits(Codec_PDN_GPIO, Codec_PDN_Pin);

}

/*******************************************************************************
* Function Name  : I2S_CODEC_SpeakerHeadphoneSwap
* Description    : Configure the Audio Codec output to Speaker or Headphone while
*                : the audio wave is Paused or stopped.
* Input          : - OutputDevice: could be OutputDevice_Speaker or OutputDevice_Headphone
*                     or OutputDevice_Both .
*                  - Address: Specifies the audio file location in the memory.
* Output         : None.
* Return         : - 0: Operation done without failures.
*                  - 1: Memory failure occured.
*                  - 2: Audio file inialization failure occured
*                  - 3: I2C communication failure occured
*******************************************************************************/
u32 I2S_CODEC_SpeakerHeadphoneSwap(u32 OutputDevice, u32 Address)
{
  u32 tmp_pointer = 0, err = 0;

  /* Reset all Codec Registers */
  I2S_CODEC_Reset();

  /* Save the current position */
  tmp_pointer = GetVar_AudioDataIndex();

  /* Reinitialize the CODEC with the new configured parameters */
  err = I2S_CODEC_Init(OutputDevice, Address);

  if (err != 0)
  {
    return err;
  }

  /* Restore the last pointer position */
  AudioDataIndex = tmp_pointer;

  /* Restore the last volume level */
  I2S_CODEC_ControlVolume(VolumeDirection_LEVEL, CurrentVolume);

  /* Play from current position */
  I2S_CODEC_Play(tmp_pointer);

  return 0;
}

/*******************************************************************************
* Function Name  : I2S_CODEC_UpdateStatus
* Description    : Check if STOP or PAUSE command are generated and performs the
*                :  relative action (STOP or PAUSE playing)
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void I2S_CODEC_UpdateStatus(void)
{
  /* STOP command is generated => Stop playing the audio file */
  if ((GetVar_AudioPlayStatus() == AudioPlayStatus_STOPPED) && (SPI_I2S_GetFlagStatus(SPI2, I2S_FLAG_CHSIDE) == SET))
  {
    I2S_CODEC_Stop();
  }

  /* PAUSE Command is generated => PAUSE playing the audio File */
  if ((GetVar_AudioPlayStatus() == AudioPlayStatus_PAUSED) && (SPI_I2S_GetFlagStatus(SPI2, I2S_FLAG_CHSIDE) == SET))
  {
    I2S_CODEC_Pause();
  }
}

/*******************************************************************************
* Function Name  : GetVar_DataStartAddr
* Description    : returns DataStartAddr variable value (used by stm32f10x_it.c file).
* Input          : None
* Output         : None
* Return         : AudioDataIndex
*******************************************************************************/
u32 GetVar_DataStartAddr(void)
{
  return DataStartAddr;
}


/*******************************************************************************
* Function Name  : GetVar_CurrentVolume
* Description    : returns CurrentVolume variable value (used by extern files).
* Input          : None
* Output         : None
* Return         : CurrentVolume
*******************************************************************************/
u8 GetVar_CurrentVolume(void)
{
  return CurrentVolume;
}

/*******************************************************************************
* Function Name  : I2S_CODEC_WaveParsing
* Description    : Checks the format of the .WAV file and gets information about
*                  the audio format. This is done by reading the value of a
*                  number of parameters stored in the file header and comparing
*                  these to the values expected authenticates the format of a
*                  standard .WAV  file (44 bytes will be read). If  it is a valid
*                  .WAV file format, it continues reading the header to determine
*                  the  audio format such as the sample rate and the sampled data
*                  size. If the audio format is supported by this application,
*                  it retrieves the audio format in WAVE_Format structure and
*                  returns a zero value. Otherwise the function fails and the
*                  return value is nonzero.In this case, the return value specifies
*                  the cause of  the function fails. The error codes that can be
*                  returned by this function are declared in the header file.
* Input          : None
* Output         : None
* Return         : Zero value if the function succeed, otherwise it return
*                  a nonzero value which specifies the error code.
*******************************************************************************/
ErrorCode I2S_CODEC_WaveParsing(u8* HeaderTab)
{
  u32 Temp = 0x00;
  u32 ExtraFormatBytes = 0;

  /* Initialize the HeaderTabIndex variable */
  HeaderTabIndex = 0;

  /* Read chunkID, must be 'RIFF' ----------------------------------------------*/
  Temp = ReadUnit(4, BigEndian);
  if (Temp != ChunkID)
  {
    return(Unvalid_RIFF_ID);
  }
  /* Read the file length ----------------------------------------------------*/
  WAVE_Format.RIFFchunksize = ReadUnit(4, LittleEndian);

  /* Read the file format, must be 'WAVE' ------------------------------------*/
  Temp = ReadUnit(4, BigEndian);
  if (Temp != FileFormat)
  {
    return(Unvalid_WAVE_Format);
  }
  /* Read the format chunk, must be 'fmt ' -----------------------------------*/
  Temp = ReadUnit(4, BigEndian);
  if (Temp != FormatID)
  {
    return(Unvalid_FormatChunk_ID);
  }
  /* Read the length of the 'fmt' data, must be 0x10 -------------------------*/
  Temp = ReadUnit(4, LittleEndian);
  if (Temp != 0x10)
  {
    ExtraFormatBytes = 1;
  }
  /* Read the audio format, must be 0x01 (PCM) -------------------------------*/
  WAVE_Format.FormatTag = ReadUnit(2, LittleEndian);
  if (WAVE_Format.FormatTag != WAVE_FORMAT_PCM)
  {
    return(Unsupporetd_FormatTag);
  }
  /* Read the number of channels: 0x02->Stereo 0x01->Mono --------------------*/
  WAVE_Format.NumChannels = ReadUnit(2, LittleEndian);

  /* Read the Sample Rate ----------------------------------------------------*/
  WAVE_Format.SampleRate = ReadUnit(4, LittleEndian);

  /* Update the I2S_AudioFreq value according to the .WAV file Sample Rate */
  switch (WAVE_Format.SampleRate)
  {
    case SampleRate_8000 :
      i2saudiofreq = I2S_AudioFreq_8k;
      break;
    case SampleRate_16000:
      i2saudiofreq = I2S_AudioFreq_16k;
      break;
    case SampleRate_22050:
      i2saudiofreq = I2S_AudioFreq_22k;
      break;
    case SampleRate_44100:
      i2saudiofreq = I2S_AudioFreq_44k;
      break;
    case SampleRate_48000:
      i2saudiofreq = I2S_AudioFreq_48k;
      break;
    default:
      return(Unsupporetd_Sample_Rate);
  }
  /* Read the Byte Rate ------------------------------------------------------*/
  WAVE_Format.ByteRate = ReadUnit(4, LittleEndian);

  /* Read the block alignment ------------------------------------------------*/
  WAVE_Format.BlockAlign = ReadUnit(2, LittleEndian);

  /* Read the number of bits per sample --------------------------------------*/
  WAVE_Format.BitsPerSample = ReadUnit(2, LittleEndian);
  if (WAVE_Format.BitsPerSample != Bits_Per_Sample_16)
  {
    return(Unsupporetd_Bits_Per_Sample);
  }
  /* If there are Extra format bytes, these bytes will be defined in "Fact Chunk" */
  if (ExtraFormatBytes == 1)
  {
    /* Read th Extra format bytes, must be 0x00 ------------------------------*/
    Temp = ReadUnit(2, LittleEndian);
    if (Temp != 0x00)
    {
      return(Unsupporetd_ExtraFormatBytes);
    }
    /* Read the Fact chunk, must be 'fact' -----------------------------------*/
    Temp = ReadUnit(4, BigEndian);
    if (Temp != FactID)
    {
      return(Unvalid_FactChunk_ID);
    }
    /* Read Fact chunk data Size ---------------------------------------------*/
    Temp = ReadUnit(4, LittleEndian);

    /* Set the index to start reading just after the header end */
    HeaderTabIndex += Temp;
  }
  /* Read the Data chunk, must be 'data' -------------------------------------*/
  Temp = ReadUnit(4, BigEndian);
  if (Temp != DataID)
  {
    return(Unvalid_DataChunk_ID);
  }
  /* Read the number of sample data ------------------------------------------*/
  WAVE_Format.DataSize = ReadUnit(4, LittleEndian);

  /* Set the data pointer at the beginning of the effective audio data */
  DataStartAddr += HeaderTabIndex;

  return(Valid_WAVE_File);
}

/*******************************************************************************
* Function Name  : GetVar_AudioDataIndex
* Description    : returns AudioDataIndex variable value (used by stm32f10x_it.c file).
* Input          : None
* Output         : None
* Return         : AudioDataIndex
*******************************************************************************/
u32 GetVar_AudioDataIndex(void)
{
  return AudioDataIndex;
}

/*******************************************************************************
* Function Name  : SetVar_AudioDataIndex
* Description    : Sets AudioDataIndex variable value (used by stm32f10x_it.c file).
* Input          : None
* Output         : None
* Return         : AudioDataIndex
*******************************************************************************/
void SetVar_AudioDataIndex(u32 value)
{
  AudioDataIndex = value;
}

/*******************************************************************************
* Function Name  : IncrementVar_AudioDataIndex
* Description    : Increment the AudioDataIndex variable.
* Input          : - IncrementNumber: number of incrementations.
* Output         : None
* Return         : None
*******************************************************************************/
void IncrementVar_AudioDataIndex(u32 IncrementNumber)
{
  AudioDataIndex += (u32)IncrementNumber;

  if (AudioDataIndex >= AudioDataLength)
  {
    ResetVar_AudioDataIndex();
    Decrement_AudioReplay();
  }
}

/*******************************************************************************
* Function Name  : DecrementVar_AudioDataIndex
* Description    : Set the AudioDataIndex variable to 1.
* Input          : None
* Output         : None
* Return         : None.
*******************************************************************************/
void DecrementVar_AudioDataIndex(u32 DecrementNumber)
{
  if (DecrementNumber >= AudioDataIndex)
  {
    ResetVar_AudioDataIndex();
  }
  else
  {
    AudioDataIndex -= (u32)DecrementNumber;
  }
}

/*******************************************************************************
* Function Name  : ResetVar_AudioDataIndex
* Description    : Reset the AudioDataIndex variable.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void ResetVar_AudioDataIndex(void)
{
  AudioDataIndex = DataStartAddr;

  /* Send the read command to the media */
  Media_StartReadSequence(AudioFileAddress + DataStartAddr + 1);
}

/*******************************************************************************
* Function Name  : GetVar_SendDummyData
* Description    : returns SendDummyData variable value (used by stm32f10x_it.c file).
* Input          : None
* Output         : None
* Return         : SendDummyData
*******************************************************************************/
u32 GetVar_SendDummyData(void)
{
  return SendDummyData;
}

/*******************************************************************************
* Function Name  : SetVar_SendDummyData
* Description    : Set the SendDummyData variable to 1.
* Input          : None
* Output         : None
* Return         : SendDummyData
*******************************************************************************/
u32 SetVar_SendDummyData(void)
{
  SendDummyData = (u32)0x1;

  return SendDummyData;
}

/*******************************************************************************
* Function Name  : ResetVar_SendDummyData
* Description    : Reset the SendDummyData variable to 0.
* Input          : None
* Output         : None
* Return         : SendDummyData
*******************************************************************************/
u32 ResetVar_SendDummyData(void)
{
  SendDummyData = (u32)0;

  return SendDummyData;
}

/*******************************************************************************
* Function Name  : GetVar_AudioPlayStatus
* Description    : returns AudioPlayStatus variable value (used by stm32f10x_it.c file).
* Input          : None
* Output         : None
* Return         : AudioPlayStatus value.
*******************************************************************************/

⌨️ 快捷键说明

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