/******************************************************************************* * Function Name : Speaker_Timer_Config * Description : Configure and enable the timer * Input : None. * Return : None. *******************************************************************************/ void Speaker_Config(void) { #ifdef USE_STM3210B_EVAL GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /* Configure PB.08 as alternate function (TIM4_OC3) */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); /* TIM4 configuration */ 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_Pulse = 0x7F; /* Duty cycle: 50%*/ TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; /* set high polarity */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OC3Init(TIM4, &TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); /* TIM2 configuration */ TIM_TimeBaseStructure.TIM_Period = TIM2ARRValue; TIM_TimeBaseStructure.TIM_Prescaler = 0x00; /* TIM2CLK = 72 MHz */ 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_Timing; TIM_OCInitStructure.TIM_Pulse = 0x0; TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); /* Start TIM4 */ TIM_Cmd(TIM4, ENABLE); /* Start TIM2 */ TIM_Cmd(TIM2, ENABLE); /* Enable TIM2 update interrupt */ TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); #else /* Configure the initializatin parameters */ I2S_GPIO_Config(); I2S_Config(I2S_Standard_Phillips, I2S_MCLKOutput_Enable, I2S_AudioFreq_22k); CODEC_Config(OutputDevice_SPEAKER, I2S_Standard_Phillips, I2S_MCLKOutput_Enable, 0x08); SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, ENABLE); #endif }
/** * @brief I2S_Bus_Init 初始化iis总线 * @param none * @retval none */ void I2S_Bus_Init(void) { /* 配置I2S GPIO用到的引脚 */ I2S_GPIO_Config(); /* 配置I2S工作模式 */ I2S_Mode_Config(); I2S_Cmd(SPI2, DISABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); }
/******************************************************************************* * Function Name : I2S_CODEC_Init * Description : Initializes the I2S audio codec according parameters configured * by I2S_CODEC_Config function. * Input : - OutputDevice: Could be OutoutDevice_SPEAKER or * OutoutDevice_HEADPHONE. * - Address: Specifies the location of the audio file in the memory. * Output : None * Return : - 0: if all initializations are OK. * - 1: if memory initialization failed (LD2 is turned on). * - 2: if audio file initialization failed (LD2 is turned on). * - 3: if Codec initialization failed (LD1 is turned on). *******************************************************************************/ uint32_t I2S_CODEC_Init(uint32_t OutputDevice, uint32_t Address) { uint32_t count = 0; /* Set the audio file address */ AudioFileAddress = (uint32_t) Address; /* Configure the I2S2, I2C1 and GPIOF pins */ I2S_GPIO_Config(); /* Read the Audio file to extract the audio data length and frequency */ errorcode = AudioFile_Init(); if (errorcode < 3) { /* Turn on LD2 connected to PF.07 */ return errorcode; } /* Configure the SPI2 peripheral in I2S mode */ I2S_Config(I2S_STANDARD, I2S_MCLKOUTPUT, i2saudiofreq); printf("\n\r i2saudiofreq: 0x%dHz ", i2saudiofreq); /* Set the current output device */ CurrentOutputDevice = OutputDevice; /* Codec Configuration via I2C interface */ count = CODEC_Config(OutputDevice, I2S_Standard_MSB, I2S_MCLKOUTPUT, DEFAULT_VOL); if (count != 0) { /* Turn on LD1 connected to PF.06 */ return 3; } /* Turn on LD4 connected to PF.09 */ return 0; /* Configuration is OK */ }