📄 waveplayer.c
字号:
/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* File Name : waveplayer.c
* Author : MCD Application Team
* Version : V1.1
* Date : 11/26/2007
* Description : This file includes the wave player driver for the
* STM3210B-EVAL demo.
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private typedef -----------------------------------------------------------*/
typedef enum
{
LittleEndian,
BigEndian
}Endianness;
/* Private define ------------------------------------------------------------*/
#define ChunkID 0x52494646 /* correspond to the letters 'RIFF' */
#define FileFormat 0x57415645 /* correspond to the letters 'WAVE' */
#define FormatID 0x666D7420 /* correspond to the letters 'fmt ' */
#define DataID 0x64617461 /* correspond to the letters 'data' */
#define FactID 0x66616374 /* correspond to the letters 'fact' */
#define WAVE_FORMAT_PCM 0x01
#define FormatChunkSize 0x10
#define Channel_Mono 0x01
#define SampleRate_8000 8000
#define SampleRate_11025 11025
#define SampleRate_22050 22050
#define SampleRate_44100 44100
#define Bits_Per_Sample_8 8
#define Dummy_Byte 0xA5
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static WAVE_FormatTypeDef WAVE_Format;
static volatile ErrorCode WaveFileStatus = Unvalid_RIFF_ID;
static vu16 TIM2ARRValue = 0x00;
static vu32 WaveDataLength;
static vu32 SpeechDataOffset = 0x00;
static u32 ReadAddr = 0x00, PauseReadAddr = 0x00;
/* Private function prototypes -----------------------------------------------*/
static u32 ReadUnit(u8 NbrOfBytes, Endianness BytesFormat);
static ErrorCode WavePlayer_WaveParsing(u32 ReadAddress);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : WavePlay_Init
* Description : Wave player Initialization
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void WavePlayer_Init(void)
{
/* Peripherals InitStructure define */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIOB_InitStructure;
TIM_OCStructInit(&TIM_OCInitStructure);
/* Read the Speech wave file status */
WaveFileStatus = WavePlayer_WaveParsing(SpeechReadAddr);
if(WaveFileStatus == Valid_WAVE_File) /* the .WAV file is valid */
{
/* Set WaveDataLenght to the Speech wave length */
WaveDataLength = WAVE_Format.DataSize;
}
/* TIM4 and TIM2 configuration is used also for voice recorder application */
/* Configure PB.08 as alternate function (TIM4_OC3) */
GPIOB_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIOB_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIOB_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIOB_InitStructure);
/* TIM4 used for PWM genration */
TIM_TimeBaseStructure.TIM_Prescaler = 0x00; /* TIM4CLK = 72 MHz */
TIM_TimeBaseStructure.TIM_Period = 0xFF; /* PWM frequency : 281.250KHz*/
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* TIM4's Channel3 in PWM1 mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_Channel = TIM_Channel_3;
TIM_OCInitStructure.TIM_Pulse = 0x7F; /* Duty cycle: 50%*/
TIM_OCInit(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
/* 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);
/* Output Compare Inactive Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;
TIM_OCInitStructure.TIM_Channel = TIM_Channel_1;
TIM_OCInitStructure.TIM_Pulse = 0x0;
TIM_OCInit(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
}
/*******************************************************************************
* Function Name : Get_WaveFileStatus
* Description : Returns the Wave file status.
* Input : None
* Output : None
* Return : Wave file status.
*******************************************************************************/
ErrorCode Get_WaveFileStatus(void)
{
return (WaveFileStatus);
}
/*******************************************************************************
* Function Name : WavePlayer_Start
* Description : Start wave playing
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void WavePlayer_Start(void)
{
/* Set WaveDataLenght to the Speech wave length */
WaveDataLength = WAVE_Format.DataSize;
TIM_SetAutoreload(TIM2, TIM2ARRValue);
/* Start TIM4 */
TIM_Cmd(TIM4, ENABLE);
/* Start TIM2 */
TIM_Cmd(TIM2, ENABLE);
/* Set SPI Flash start read address to the Speech wave data */
ReadAddr = SpeechReadAddr + SpeechDataOffset;
}
/*******************************************************************************
* Function Name : WavePlayer_Stop
* Description : Stop wave playing
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void WavePlayer_Stop(void)
{
/* Disable TIM2 update interrupt */
TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
/* Disable TIM4 */
TIM_Cmd(TIM4, DISABLE);
/* Disable TIM2 */
TIM_Cmd(TIM2, DISABLE);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
}
/*******************************************************************************
* Function Name : WavePlayer_RePlay
* Description : Pause wave playing
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void WavePlayer_RePlay(void)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
/* Enable TIM2 update interrupt */
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* set Speech SPI flash wave address */
SPI_FLASH_StartReadSequence(PauseReadAddr);
}
/*******************************************************************************
* Function Name : WavePlayer_Pause
* Description : Pause wave playing
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void WavePlayer_Pause(void)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
/* Disable TIM2 update interrupt */
TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
PauseReadAddr = WAVE_Format.DataSize - WaveDataLength;
PauseReadAddr += ReadAddr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -