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

📄 dac_codec.c

📁 stm32.rar
💻 C
📖 第 1 页 / 共 3 页
字号:
*******************************************************************************/
u32 GetVar_AudioReplay(void)
{ 
  return AudioReplay;
}

/*******************************************************************************
* Function Name  : SetVar_AudioReplay
* Description    : Decrement the AudioReplayCount variable if AudioReplay is different
*                :  from zero (infinite replaying).
* Input          : None.
* Output         : None
* Return         : AudioPlayStatus value.
*******************************************************************************/
void Decrement_AudioReplay(void)
{ 
  if (AudioReplay != 0)
  {
    AudioReplayCount--;

		if (AudioReplayCount == 0)
	  {
	    /* Command the Stop of the audio playing */ 
	    SetVar_AudioPlayStatus(AudioPlayStatus_STOPPED);
	    
	    /* Reset the counter */
	    AudioReplayCount = AudioReplay;
	  }
  }
}

/*******************************************************************************
* Function Name  : GetVar_AudioDataLength
* Description    : Get the current audio file data length .
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
u32 GetVar_AudioDataLength(void)
{ 
  return AudioDataLength;
}

/*******************************************************************************
* Function Name  : GetVar_i2saudiofreq
* Description    : Get the current audio frequency .
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
u16 GetVar_i2saudiofreq(void)
{ 
  return dacaudiofreq;
}

/*******************************************************************************
* Function Name  : AudioFile_Init
* Description    : Initializes the SPI_Flash and returns the Wavadatalength variable.
* Input          : None
* Output         : None
* Return         : - The length of the wave file read from the SPI_Flash
*                  - 1 if an error occured when initializing the memory.
*                  - 2 if an error occured on the audio file intialization.
*******************************************************************************/
u32 AudioFile_Init(void)
{
  u32 err = 0;

  /* Initialize the media support */
  err = Media_Init();

  /* Check if Memory initialization is OK */
  if (err != 0)
  {
    return 1;
  }

  /* Read a Byte buffer and store it in the Header table*/
  Media_BufferRead(AudioFileHeader, AudioFileAddress, HEADER_SIZE);

  /* Read and check the audio file Header */
	WaveFileStatus = DAC_CODEC_WaveParsing(AudioFileHeader);

	/* Check if the selected file is a correct wave file */
  if(WaveFileStatus == Valid_WAVE_File)
  {
	 	/* Read and set the audio data length (/!\ data are counted as BYTES /!\) */ 
	  AudioDataLength = WAVE_Format.DataSize ;  
	  
	  /* Read and set the audio frequency */
	  dacaudiofreq = (u16)WAVE_Format.SampleRate;

    /* Return the audio file length */
		return AudioDataLength;
  }
	else /* Wrong wave file */
  {
   	return 2;
  }
}

/*******************************************************************************
* Function Name  : NVIC_Config
* Description    : Configure the TIM2s NVIC channel.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
static void NVIC_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

	/* TIM2 IRQ Channel configuration */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/*******************************************************************************
* Function Name  : DAC_GPIO_Config
* Description    : Initializes the GPIO pins used by the codec application.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
static void DAC_GPIO_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable GPIOA, GPIOB and AFIO clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | 
                         RCC_APB2Periph_AFIO, ENABLE);

  /* DAC pin configuration (PA4) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* LEDs pins configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8  | GPIO_Pin_9  | GPIO_Pin_10 | GPIO_Pin_11 |
                                GPIO_Pin_12 | GPIO_Pin_13 ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

	/* Enable the DAC APB1 clock */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);

	/* Turn Off All LEDs */
  GPIO_ResetBits(GPIOB, GPIO_Pin_8  | GPIO_Pin_9  | GPIO_Pin_10 | GPIO_Pin_11 |
                        GPIO_Pin_12 | GPIO_Pin_13 );
}

/*******************************************************************************
* Function Name  : DAC_Config
* Description    : Configure the DAC Peripheral.
* Input          :  - AudioFreq: AudioFreq_8K, AudioFreq_16K, AudioFreq_22K,
*                                AudioFreq_44K or AudioFreq_48K
* Output         : None
* Return         : None
*******************************************************************************/
static void DAC_Config(u16 AudioFreq)
{
  DAC_InitTypeDef          DAC_InitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef        TIM_OCInitStructure;

	/* Enable DAC APB1 clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);

  DAC_StructInit(&DAC_InitStructure);
  DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
  DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
  DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
  DAC_Init(DAC_Channel_1, &DAC_InitStructure);
  DAC_Cmd (DAC_Channel_1, ENABLE);

	/* Enable TIM2 APB1 clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  TIM_OCStructInit(&TIM_OCInitStructure);

  /* TIM2 used for timing, the timing period depends on wav file sample rate */
  TIM_TimeBaseStructure.TIM_Prescaler = 0x00;    /* TIM2CLK = 72 MHz */
  TIM_TimeBaseStructure.TIM_Period = TIM2ARRValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  /* Channel 1 Configuration in Timing mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
  TIM_OCInitStructure.TIM_Pulse = 0x0;
  TIM_OC1Init(TIM2, &TIM_OCInitStructure); 
}

/*******************************************************************************
* 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)
{ 
  vu16 tmp;
  Offset <<= 1;

  Media_BufferRead((u8*)(&tmp), AudioFileAddress + Offset, 2);  

  /* Return the read value */
  return tmp;
}

/*******************************************************************************
* Function Name  : Media_Init
* Description    : Initialize media (SPI_Flash/NOR/NAND memories..)
* Input          : None.
* Output         : None.
* Return         : - 0 if initialization is OK
*                  - 1 if initialization failed..
*******************************************************************************/
u32 Media_Init(void)
{ 
  /* Initialize the SPI FLASH driver */
  SPI_FLASH_Init();

  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)
{ 
  SPI_FLASH_BufferRead(pBuffer, ReadAddr, NumByteToRead);
}

/*******************************************************************************
* 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  : DAC_CODEC_DataTransfer
* Description    : Sets the audio data using DAC peripheral and checks the 
*                :   audio playing status (if a command (Pause/Stop) is pending 
*                :   the playing status is updated). If the interrupt 
*                :   is used to synchronize data sending, this function should be 
*                :   called in the TIM2 ISR.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void DAC_CODEC_DataTransfer(void)
{
  static int data = 0;

  DAC_SetChannel1Data (DAC_Align_12b_L, data);

  /* Increment the index */
  IncrementVar_AudioDataIndex(WAVE_Format.NumChannels << 1); 

  Media_BufferRead((u8*)(&data), AudioFileAddress + AudioDataIndex, 2);  

  /* Offset data for half of DAC area */
  data +=  1 << 15;

  /* Check and update the stream playing status */
  DAC_CODEC_UpdateStatus();
}

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

⌨️ 快捷键说明

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