Пример #1
0
/**
  * @brief  Initializes the Audio Codec audio interface (I2S)
  * @note   This function assumes that the I2S input clock (through PLL_R in 
  *         Devices RevA/Z and through dedicated PLLI2S_R in Devices RevB/Y)
  *         is already configured and ready to be used.    
  * @param  AudioFreq: Audio frequency to be configured for the I2S peripheral. 
  * @retval None.
  */
static void Codec_AudioInterface_Init(uint32_t AudioFreq)
{
  /* Enable the CODEC_I2S peripheral clock */
  RCC_APB1PeriphClockCmd(CODEC_I2S_CLK, ENABLE);
  RCC->APB1ENR |= RCC_APB1Periph_SPI2 | RCC_APB1Periph_SPI3;

  /* CODEC_I2S peripheral configuration */
  SPI_I2S_DeInit(CODEC_I2S);
  I2S_InitStructure.I2S_AudioFreq = AudioFreq;
  I2S_InitStructure.I2S_Standard = I2S_STANDARD;
  I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
  I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
  I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
#ifdef CODEC_MCLK_ENABLED
  I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Enable;
#elif defined(CODEC_MCLK_DISABLED)
  I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
#else
 #error "No selection for the MCLK output has been defined !"
#endif /* CODEC_MCLK_ENABLED */

  /* Initialize the I2S peripheral with the structure above */
  I2S_Init(CODEC_I2S, &I2S_InitStructure);

  /* Enable the I2S DMA TX request */
  SPI_I2S_DMACmd(CODEC_I2S, SPI_I2S_DMAReq_Tx, ENABLE);

  /* The I2S peripheral will be enabled only in the EVAL_AUDIO_Play() function 
       or by user functions if DMA mode not enabled */  
}
Пример #2
0
/*******************************************************************************
* Function Name  : I2S_Config
* Description    : Configure the I2S Peripheral.
* Input          :  - Standard: I2S_Standard_Phillips, I2S_Standard_MSB or I2S_Standard_LSB
*                   - MCLKOutput: I2S_MCLKOutput_Enable or I2S_MCLKOutput_Disable
*                   - AudioFreq: I2S_AudioFreq_8K, I2S_AudioFreq_16K, I2S_AudioFreq_22K,
*                                I2S_AudioFreq_44K or I2S_AudioFreq_48K
* Output         : None
* Return         : None
*******************************************************************************/
static void I2S_Config(uint16_t Standard, uint16_t MCLKOutput, uint16_t AudioFreq)
{
    I2S_InitTypeDef I2S_InitStructure;

    /* Enable I2S2 APB1 clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

    /* Deinitialize SPI2 peripheral */
    SPI_I2S_DeInit(SPI2);

    /* I2S2 peripheral configuration */
    I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
    I2S_InitStructure.I2S_Standard = Standard;
    I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
    I2S_InitStructure.I2S_MCLKOutput = MCLKOutput;
    I2S_InitStructure.I2S_AudioFreq = AudioFreq;
    I2S_InitStructure.I2S_CPOL = I2S_CPOL_High;
    I2S_Init(SPI2, &I2S_InitStructure);

    /* Disable the I2S2 TXE Interrupt */
    SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, DISABLE);

    /* Enable the SPI2/I2S2 peripheral */
    I2S_Cmd(SPI2, ENABLE);
    printf("\n\r I2S peripheral Config finished");
}
Пример #3
0
/**
  * @brief  Initializes the Audio Codec audio interface (I2S)
  * @note   This function assumes that the I2S input clock (through PLL_R in 
  *         Devices RevA/Z and through dedicated PLLI2S_R in Devices RevB/Y)
  *         is already configured and ready to be used.    
  * @param  AudioFreq: Audio frequency to be configured for the I2S peripheral. 
  * @retval None
  */
static void Codec_AudioInterface_Init(uint32_t AudioFreq)
{
  I2S_InitTypeDef I2S_InitStructure;
  DAC_InitTypeDef  DAC_InitStructure;

  /* Enable the CODEC_I2S peripheral clock */
  RCC_APB1PeriphClockCmd(CODEC_I2S_CLK, ENABLE);
  
  /* CODEC_I2S peripheral configuration */
  SPI_I2S_DeInit(CODEC_I2S);
  I2S_InitStructure.I2S_AudioFreq = AudioFreq;
  I2S_InitStructure.I2S_Standard = I2S_STANDARD;
  I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
  I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
#ifdef DAC_USE_I2S_DMA
  if (CurrAudioInterface == AUDIO_INTERFACE_DAC)
  {  
    I2S_InitStructure.I2S_Mode = I2S_Mode_MasterRx;
  }
  else
  {
#else
   I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
#endif
#ifdef DAC_USE_I2S_DMA
  }
#endif /* DAC_USE_I2S_DMA */
#ifdef CODEC_MCLK_ENABLED
  I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Enable;
#elif defined(CODEC_MCLK_DISABLED)
  I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
#else
#error "No selection for the MCLK output has been defined !"
#endif /* CODEC_MCLK_ENABLED */
  
  /* Initialize the I2S peripheral with the structure above */
  I2S_Init(CODEC_I2S, &I2S_InitStructure);


  /* Configure the DAC interface */
  if (CurrAudioInterface == AUDIO_INTERFACE_DAC)
  {    
    /* DAC Periph clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
    
    /* DAC channel1 Configuration */
    DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
    DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
    DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
    DAC_Init(AUDIO_DAC_CHANNEL, &DAC_InitStructure);
    
    /* Enable DAC Channel1 */
    DAC_Cmd(AUDIO_DAC_CHANNEL, ENABLE);  
  }
  
  /* The I2S peripheral will be enabled only in the EVAL_AUDIO_Play() function 
       or by user functions if DMA mode not enabled */  
}
Пример #4
0
/*******************************************************************************
* Function Name: I2S_Start
********************************************************************************
*
* Summary:
*  Starts the I2S interface.
*
* Parameters:
*  None.
*
* Return:
*  None.
*
* Global Variables:
*  I2S_initVar - used to check initial configuration, modified on
*  first function call.
*
* Reentrant:
*  No.
*
*******************************************************************************/
void I2S_Start(void) 
{
    /* Writes Tx and/or Rx interrupt source values provided for customizer */
    if(0u == I2S_initVar)
    {
        I2S_Init();
        I2S_initVar = 1u;
    }

    I2S_Enable();
}
Пример #5
0
/*******************************************************************************
* Function Name  : I2S_Config
* Description    : Initializes the I2S peripheral.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
static void I2S_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  I2S_InitTypeDef I2S_InitStructure;  

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

  /* I2S2 SD, CK and WS pins configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* I2S2 MCK pin configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  
  /* Enable I2S2 APB1 clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

  /* Deinitialize SPI2 peripheral */
  SPI_I2S_DeInit(SPI2);
  /* I2S2 peripheral configuration */
  I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
  I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
  I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
  I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Enable;
#ifdef AUDIO_FREQ_96K
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_96k;
#elif defined (AUDIO_FREQ_48K)
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_48k;
#elif defined (AUDIO_FREQ_44K) 
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_44k;
#elif defined (AUDIO_FREQ_32K) 
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_32k;
#elif defined (AUDIO_FREQ_22K)  
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_22k;
#elif defined  (AUDIO_FREQ_16K) 
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_16k;
#elif defined  (AUDIO_FREQ_11K) 
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_11k;
#elif defined  (AUDIO_FREQ_8K) 
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_8k;  
#endif /* AUDIO_FREQ_96K */  
  I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
  I2S_Init(SPI2, &I2S_InitStructure);
  
  /* Enable the SPI2/I2S2 peripheral */
  I2S_Cmd(SPI2, ENABLE);  
  
}
Пример #6
0
/**
  * @brief  I2S_Mode_Config 配置IIS总线的工作模式(默认采样频率)
  * @param  none
  * @retval none
  */
static void I2S_Mode_Config(void)
{
	I2S_InitTypeDef I2S_InitStructure; 
			
	/* I2S2 外设配置 */
	I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;					// 配置I2S工作模式 
	I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;				// 接口标准 
	I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;			// 数据格式,16bit
	I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Enable;		// 主时钟模式 
	I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_44k;			// 音频采样频率
	I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;  					// 默认为低	
	I2S_Init(SPI2, &I2S_InitStructure);
}
Пример #7
0
/**
  * @brief  I2S_Freq_Config 根据采样频率配置iis总线,在播放音频文件时可从文件中解码获取
  * @param  SampleFreq 采样频率
  * @retval none
  */
void I2S_Freq_Config(uint16_t SampleFreq)
{
	I2S_InitTypeDef I2S_InitStructure;

	I2S_Stop();
	I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;						// 配置I2S工作模式
	I2S_InitStructure.I2S_Standard = I2S_Standard_LSB;				// 接口标准 
	I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;		// 数据格式,16bit 
	I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Enable;	// 主时钟模式 
	I2S_InitStructure.I2S_AudioFreq = SampleFreq;							// 音频采样频率 
	I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;  			
	I2S_Init(SPI2, &I2S_InitStructure);
	
	I2S_Cmd(SPI2, ENABLE);																		//使能iis总线
}
Пример #8
0
static void I2S_Configuration(uint32_t I2S_AudioFreq)
{
    I2S_InitTypeDef I2S_InitStructure;

    /* I2S peripheral configuration */
    I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
    I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
    I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
    I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq;
    I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;

    /* I2S2 configuration */
#if CODEC_MASTER_MODE
    I2S_InitStructure.I2S_Mode = I2S_Mode_SlaveTx;
#else
    I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
#endif
    I2S_Init(CODEC_I2S_PORT, &I2S_InitStructure);
}
Пример #9
0
void startI2S(uint32_t AudioFreq,uint32_t AudioBit)
{
  I2S_InitTypeDef I2S_InitStructure;
  /* Enable the CODEC_I2S peripheral clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
  /* CODEC_I2S peripheral configuration */
  SPI_I2S_DeInit(SPI3);
  I2S_InitStructure.I2S_AudioFreq = AudioFreq;
  I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
  if (AudioBit == 16) {
    I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
  } else {
    I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_24b;
  }
//  I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_32b;
  I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
  I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
  I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Enable;
  I2S_Init(SPI3, &I2S_InitStructure);
}
Пример #10
0
/**
  * @brief  Initialize SPI peripheral.
  * @param  Freq :Audio frequency
  * @retval None
  */
static void WaveRecorder_SPI_Init()
{
  I2S_InitTypeDef I2S_InitStructure;

  /* Enable the SPI clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
  
  /* SPI configuration */
  SPI_I2S_DeInit(SPI2);
  I2S_InitStructure.I2S_AudioFreq = 32000;
  I2S_InitStructure.I2S_Standard = I2S_Standard_LSB;
  I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
  I2S_InitStructure.I2S_CPOL = I2S_CPOL_High;
  I2S_InitStructure.I2S_Mode = I2S_Mode_MasterRx;
  I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  /* Initialize the I2S peripheral with the structure above */
  I2S_Init(SPI2, &I2S_InitStructure);

  /* Enable the Rx buffer not empty interrupt */
  SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
}
Пример #11
0
//参数I2S_Standard:  @ref SPI_I2S_Standard  I2S标准,
//I2S_Standard_Phillips,飞利浦标准;
//I2S_Standard_MSB,MSB对齐标准(右对齐);
//I2S_Standard_LSB,LSB对齐标准(左对齐);
//I2S_Standard_PCMShort,I2S_Standard_PCMLong:PCM标准
//参数I2S_Mode:  @ref SPI_I2S_Mode  I2S_Mode_SlaveTx:从机发送;I2S_Mode_SlaveRx:从机接收;I2S_Mode_MasterTx:主机发送;I2S_Mode_MasterRx:主机接收;
//参数I2S_Clock_Polarity   @ref SPI_I2S_Clock_Polarity:  I2S_CPOL_Low,时钟低电平有效;I2S_CPOL_High,时钟高电平有效
//参数I2S_DataFormat: @ref SPI_I2S_Data_Format :数据长度,I2S_DataFormat_16b,16位标准;I2S_DataFormat_16bextended,16位扩展(frame=32bit);I2S_DataFormat_24b,24位;I2S_DataFormat_32b,32位.
void I2S2_Init(u16 I2S_Standard,u16 I2S_Mode,u16 I2S_Clock_Polarity,u16 I2S_DataFormat)
{ 
  I2S_InitTypeDef I2S_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);//使能SPI2时钟
	
	RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,ENABLE); //复位SPI2
	RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,DISABLE);//结束复位
  
	I2S_InitStructure.I2S_Mode=I2S_Mode;//IIS模式
	I2S_InitStructure.I2S_Standard=I2S_Standard;//IIS标准
	I2S_InitStructure.I2S_DataFormat=I2S_DataFormat;//IIS数据长度
	I2S_InitStructure.I2S_MCLKOutput=I2S_MCLKOutput_Disable;//主时钟输出禁止
	I2S_InitStructure.I2S_AudioFreq=I2S_AudioFreq_Default;//IIS频率设置
	I2S_InitStructure.I2S_CPOL=I2S_Clock_Polarity;//空闲状态时钟电平
	I2S_Init(SPI2,&I2S_InitStructure);//初始化IIS

 
	SPI_I2S_DMACmd(SPI2,SPI_I2S_DMAReq_Tx,ENABLE);//SPI2 TX DMA请求使能.
  I2S_Cmd(SPI2,ENABLE);//SPI2 I2S EN使能.	
} 
Пример #12
0
int main(void)
{
    int i;
    DelayInit();
    GPIO_QuickInit(HW_GPIOE, 6, kGPIO_Mode_OPP);
    UART_QuickInit(UART0_RX_PA01_TX_PA02, 115200);
    
     /** print message before mode change*/
    printf("i2s MAPSK64 test\r\n");
    I2C_QuickInit(I2C0_SCL_PE24_SDA_PE25, 100*1000);
   // I2C_Scan(I2C0_SCL_PE24_SDA_PE25);
    wm8960_init(0);
    wm8960_format_config(44100, 16);
   // wm8960_set_volume(kWolfsonModuleHP, 0x2FFF);
    
    
    I2S_InitTypeDef Init;
    Init.instance = 0;
    Init.isStereo = true;
    Init.isMaster = true;
    Init.protocol = kSaiBusI2SLeft;
    Init.sampleBit = 16;
    Init.sampleRate = 44100;
    Init.chl = 0;
    I2S_Init(&Init);
    
    /* pinmux */
    PORT_PinMuxConfig(HW_GPIOE, 6, kPinAlt4);
    PORT_PinMuxConfig(HW_GPIOE, 7, kPinAlt4);
    PORT_PinMuxConfig(HW_GPIOE, 12, kPinAlt4); 
    PORT_PinMuxConfig(HW_GPIOE, 11, kPinAlt4); 
    PORT_PinMuxConfig(HW_GPIOE, 10, kPinAlt4); 
    
    while(1)
    {
        I2S_SendData(0, 16, 0, (uint8_t*)music_44100_16_2, sizeof(music_44100_16_2));
        printf("complete\r\n");
      //  DelayMs(500);
    }
}
Пример #13
0
void audioInit()
{
    I2S_InitTypeDef I2S_InitStructure;

    Audio_GPIO_Init();

    /*CONFIG the I2S_RCC ,MUST before enabling the I2S APB clock*/
    //PLLI2SN 302,PLLI2SR 2,I2SDIV 53,I2SODD 1,FS 44.1KHZ,16bit,Error 0.0011%
    RCC_PLLI2SConfig(302,2);
    RCC_PLLI2SCmd(ENABLE);
    RCC_I2SCLKConfig(RCC_I2S2CLKSource_PLLI2S);

    /* Enable the CODEC_I2S peripheral clock */
    RCC_APB1PeriphClockCmd(CODEC_I2S_CLK, ENABLE);
    /* CODEC_I2S peripheral configuration */
    SPI_I2S_DeInit(CODEC_I2S);
    I2S_InitStructure.I2S_AudioFreq = AudioFreq;
    I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
    I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
    I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;//clk 0 when idle state
    I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
#ifdef CODEC_MCLK_ENABLED
    I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Enable;
#elif defined(CODEC_MCLK_DISABLED)
    I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
#else
    #error "No selection for the MCLK output has been defined !"
#endif /* CODEC_MCLK_ENABLED */

    /* Initialize the I2S peripheral with the structure above */
    I2S_Init(CODEC_I2S, &I2S_InitStructure);
    I2S_Cmd(CODEC_I2S, ENABLE);
    //interrupt
#ifdef CODEC_USE_INT
    SPI_I2S_ITConfig(CODEC_I2S,SPI_I2S_IT_TXE,ENABLE);
#elif defined(CODEC_USE_DMA)
#error "DMA is not initialized"
#endif
}
Пример #14
0
void Codec_AudioInterface_Init(uint32_t AudioFreq)
{
	//Setup I2C comms to codec
	Codec_GPIO_Init();
	I2C_Cntrl_Init();

	//Bring Up Codec via the I2C comms
	Codec_Init();

	//Invoke I2S Pin connections
	I2S_GPIO_Init();

	//Now, get down to defining I2S, Full Duplex, Codec Master Configuration
	I2S_InitTypeDef I2S_InitStructure;

	/* Enable the CODEC_I2S peripheral clock */
	RCC_APB1PeriphClockCmd(CODEC_I2S_CLK, ENABLE);

	/* CODEC_I2S peripheral configuration */
	I2S_InitStructure.I2S_AudioFreq = AudioFreq;
	I2S_InitStructure.I2S_Standard = I2S_Standard_MSB;
	I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
	I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
	I2S_InitStructure.I2S_Mode = I2S_Mode_SlaveTx;

	I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;

	/* Initialize the I2S peripheral with the structure above */
	I2S_Init(CODEC_I2S, &I2S_InitStructure);
	I2S_FullDuplexConfig(I2S3ext, &I2S_InitStructure);

	//Turn on the I2S interface
	I2S_Cmd(CODEC_I2S, ENABLE);
	I2S_Cmd(I2S3ext, ENABLE);

}
Пример #15
0
/*********************************************************************//**
 * @brief		c_entry: Main program body
 * @param[in]	None
 * @return 		int
 **********************************************************************/
int c_entry (void) {                       /* Main Program */
	I2S_MODEConf_Type I2S_ClkConfig;
	I2S_CFG_Type I2S_ConfigStruct;
	PINSEL_CFG_Type PinCfg;

	uint32_t i;
	/* Initialize debug via UART0
	 * – 115200bps
	 * – 8 data bit
	 * – No parity
	 * – 1 stop bit
	 * – No flow control
	 */
	debug_frmwrk_init();

	//print menu screen
	print_menu();
/* Initialize I2S peripheral ------------------------------------*/
	/* Pin configuration:
	 * Assign: 	- P0.4 as I2SRX_CLK
	 * 			- P0.5 as I2SRX_WS
	 * 			- P0.6 as I2SRX_SDA
	 * 			- P0.7 as I2STX_CLK
	 * 			- P0.8 as I2STX_WS
	 * 			- P0.9 as I2STX_SDA
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Pinnum = 4;
	PinCfg.Portnum = 0;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 5;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 6;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 7;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 8;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 9;
	PINSEL_ConfigPin(&PinCfg);

	Buffer_Init();

	I2S_Init(LPC_I2S);

	/* setup:
	 * 		- wordwidth: 16 bits
	 * 		- stereo mode
	 * 		- master mode for I2S_TX and slave for I2S_RX
	 * 		- ws_halfperiod is 31
	 * 		- not use mute mode
	 * 		- use reset and stop mode
	 * 		- select the fractional rate divider clock output as the source,
	 * 		- disable 4-pin mode
	 * 		- MCLK ouput is disable
	 * 		- Frequency = 44.1 kHz
	 * Because we use mode I2STXMODE[3:0]= 0000, I2SDAO[5]=0 and
	 * I2SRX[3:0]=0000, I2SDAI[5] = 1. So we have I2SRX_CLK = I2STX_CLK
	 * --> I2SRXBITRATE = 1 (not divide TXCLK to produce RXCLK)
	 */

	/* Audio Config*/
	I2S_ConfigStruct.wordwidth = I2S_WORDWIDTH_16;
	I2S_ConfigStruct.mono = I2S_STEREO;
	I2S_ConfigStruct.stop = I2S_STOP_ENABLE;
	I2S_ConfigStruct.reset = I2S_RESET_ENABLE;
	I2S_ConfigStruct.ws_sel = I2S_MASTER_MODE;
	I2S_ConfigStruct.mute = I2S_MUTE_DISABLE;
	I2S_Config(LPC_I2S,I2S_TX_MODE,&I2S_ConfigStruct);

	I2S_ConfigStruct.ws_sel = I2S_SLAVE_MODE;
	I2S_Config(LPC_I2S,I2S_RX_MODE,&I2S_ConfigStruct);

	/* Clock Mode Config*/
	I2S_ClkConfig.clksel = I2S_CLKSEL_FRDCLK;
	I2S_ClkConfig.fpin = I2S_4PIN_DISABLE;
	I2S_ClkConfig.mcena = I2S_MCLK_DISABLE;
	I2S_ModeConfig(LPC_I2S,&I2S_ClkConfig,I2S_TX_MODE);
	I2S_ModeConfig(LPC_I2S,&I2S_ClkConfig,I2S_RX_MODE);

	I2S_FreqConfig(LPC_I2S, 44100, I2S_TX_MODE);
	I2S_SetBitRate(LPC_I2S, 0, I2S_RX_MODE);

	I2S_Stop(LPC_I2S, I2S_TX_MODE);
	I2S_Stop(LPC_I2S, I2S_RX_MODE);

	NVIC_EnableIRQ(I2S_IRQn);

	/* RX FIFO depth is 1, TX FIFO depth is 8. */
	I2S_IRQConfig(LPC_I2S,I2S_TX_MODE,8);
	I2S_IRQConfig(LPC_I2S,I2S_RX_MODE,1);
	I2S_IRQCmd(LPC_I2S,I2S_RX_MODE,ENABLE);
	I2S_Start(LPC_I2S);

/* I2S transmit ---------------------------------------------------*/
	while ( I2SWriteLength < BUFFER_SIZE )
	{
		while(I2S_GetLevel(LPC_I2S, I2S_TX_MODE)==TXFIFO_FULL);
		I2S_Send(LPC_I2S, I2STXBuffer[I2SWriteLength++]);
	}

	I2STXDone = 1;

	/* Wait for transmit/receive complete */
	while ( !I2SRXDone || !I2STXDone );
	for(i=0;i<BUFFER_SIZE;i++)
	{
		_DBH32(I2SRXBuffer[i]);_DBG_("");
	}
	/* Verify RX and TX Buffer */
	if(Buffer_Verify())
	{
		_DBG_("Verify Buffer: OK...");
	}
	else
	{
		_DBG_("Verify Buffer: ERROR...");
	}


	return 0;
}
Пример #16
0
void I2S_Test(void)
{
	u32 uCountFunc=0;
	s32 iSel=0;
	double dTemp;
	AUDIO_PORT eSelPort;
	u32 uLoopBackMode = 0;

	//TestSFR();
	eSelPort = AUDIO_PORT0;

	I2S_Init(AUDIO_PORT0);
	I2S_InitPort(AUDIO_PORT0);
	
	I2S_Init(AUDIO_PORT1);
	I2S_InitPort(AUDIO_PORT1);

	while(1)
	{	
		UART_Printf("================================================================================\n");
		if ( uLoopBackMode == 1) 
				UART_Printf("Loop Back Mode");
		for (uCountFunc = 0; uCountFunc < I2S_CONNUM; uCountFunc++)
		{
			
			UART_Printf("Port : %d\t",uCountFunc);			
			
			UART_Printf("Operation Mode : ");
			if ( g_oI2SInfor[uCountFunc].m_eOpMode ==  Master)
				UART_Printf("Master\t");
			else 
				UART_Printf("Slave\t");
			
			UART_Printf("PCM Word Length : ");
			if ( g_oI2SInfor[uCountFunc].m_eWordLength ==  Word8)
				UART_Printf("8bit\n");
			else if ( g_oI2SInfor[uCountFunc].m_eWordLength ==  Word16)
				UART_Printf("16bit\n");
			else 
				UART_Printf("24bit\n");

			UART_Printf("Interface Data Format : ");
			if ( g_oI2SInfor[uCountFunc].m_eDataFormat==  I2SFormat)
				UART_Printf("I2S Format\t");
			else if ( g_oI2SInfor[uCountFunc].m_eDataFormat==  MSBJustified)
				UART_Printf("MSBJustified(Left)\t");
			else if ( g_oI2SInfor[uCountFunc].m_eDataFormat==  LSBJustified)
				UART_Printf("LSBJustified(Right)\t");

			
			UART_Printf("LRCLK Polarity : ");
			if ( g_oI2SInfor[uCountFunc].m_eLRCLKPolarity==  LeftHigh)
				UART_Printf("Left High\n");
			else if ( g_oI2SInfor[uCountFunc].m_eLRCLKPolarity==  RightHigh)
				UART_Printf("Right High\n");	

			UART_Printf("I2S Clock Source : ");
			if ( g_oI2SInfor[uCountFunc].m_CLKSRC==  I2S_MOUT_EPLL)
				UART_Printf("EPLL Out\t");
			else if ( g_oI2SInfor[uCountFunc].m_CLKSRC==  I2S_PCLK)
				UART_Printf("PCLK\t");
			else if ( g_oI2SInfor[uCountFunc].m_CLKSRC==  I2S_DOUT_MPLL)
				UART_Printf("MPLL Out\t");
			else if ( g_oI2SInfor[uCountFunc].m_CLKSRC==  I2S_FIN)
				UART_Printf("System External CLK\t");
			else if ( g_oI2SInfor[uCountFunc].m_CLKSRC==  I2S_EXTERNALCDCLK)
				UART_Printf("I2S External CLK\t");
			dTemp = (double)(g_oI2SInfor[uCountFunc].m_dSamplingFreq)/1000;
			UART_Printf("Sampling Frequency : %3.1fKHz\n",dTemp);
			UART_Printf("RFS : ");
			if ( g_oI2SInfor[uCountFunc].m_eRFS==  RFS_256fs)
				UART_Printf("256fs\t");
			else if ( g_oI2SInfor[uCountFunc].m_eRFS==  RFS_512fs)
				UART_Printf("512fs\t");
			else if ( g_oI2SInfor[uCountFunc].m_eRFS==  RFS_384fs)
				UART_Printf("384fs\t");
			else if ( g_oI2SInfor[uCountFunc].m_eRFS==  RFS_768fs)
				UART_Printf("768fs\t");	

			UART_Printf("BFS : ");
			if ( g_oI2SInfor[uCountFunc].m_eBFS==  BFS_32fs)
				UART_Printf("32fs\t");
			else if ( g_oI2SInfor[uCountFunc].m_eBFS==  BFS_48fs)
				UART_Printf("48fs\t");
			else if ( g_oI2SInfor[uCountFunc].m_eBFS==  BFS_16fs)
				UART_Printf("16fs\t");
			else if ( g_oI2SInfor[uCountFunc].m_eBFS==  BFS_24fs)
				UART_Printf("24fs\t");
			UART_Printf("\n\n");
			
		}
				
		UART_Printf("====================== I2S Function Test =======================================\n\n");
		UART_Printf("Current Control Port : Port %d\n",eSelPort);
		for (uCountFunc=0; (u32)(g_aI2STestFunc[uCountFunc].desc)!=0; uCountFunc++)
			UART_Printf("%2d: %s\n", uCountFunc, g_aI2STestFunc[uCountFunc].desc);	
		UART_Printf("\n%2d: Change Port\n",uCountFunc++);		
		UART_Printf("================================================================================\n");			
		UART_Printf("\nSelect the function to test : ");
		
		iSel =UART_GetIntNum();		
		UART_Printf("\n");
		
		if(iSel == 0) 
			break;
		if (iSel>0 && iSel<(sizeof(g_aI2STestFunc)/8-1))
			(g_aI2STestFunc[iSel].func) (eSelPort);
		else if ( iSel == (sizeof(g_aI2STestFunc)/8-1))			
			eSelPort = I2S_ChangePort();	
	}	
	I2S_ReturnPort(AUDIO_PORT0);
	I2S_ReturnPort(AUDIO_PORT1);	
}
Пример #17
0
/*********************************************************************//**
 * @brief		c_entry: Main I2S program body
 * @param[in]	None
 * @return 		int
 **********************************************************************/
int c_entry (void) {
	uint32_t i;
	uint32_t dummy=0;
	I2S_MODEConf_Type I2S_ClkConfig;
	I2S_CFG_Type I2S_ConfigStruct;
	PINSEL_CFG_Type PinCfg;

	/* Initialize debug via UART0
	 * – 115200bps
	 * – 8 data bit
	 * – No parity
	 * – 1 stop bit
	 * – No flow control
	 */
	debug_frmwrk_init();

	//print menu screen
	print_menu();

	Buffer_Init();

	/* Pin configuration:
	 * Assign: 	- P0.4 as I2SRX_CLK
	 * 			- P0.5 as I2SRX_WS
	 * 			- P0.6 as I2SRX_SDA
	 * 			- P0.7 as I2STX_CLK
	 * 			- P0.8 as I2STX_WS
	 * 			- P0.9 as I2STX_SDA
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Pinnum = 4;
	PinCfg.Portnum = 0;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 5;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 6;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 7;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 8;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 9;
	PINSEL_ConfigPin(&PinCfg);

	I2S_Init(LPC_I2S);

	//Setup for I2S: RX is similar with TX
	/* setup:
	 * 		- wordwidth: 16 bits
	 * 		- stereo mode
	 * 		- master mode for I2S_TX and slave for I2S_RX
	 * 		- ws_halfperiod is 31
	 * 		- not use mute mode
	 * 		- use reset and stop mode
	 * 		- select the fractional rate divider clock output as the source,
	 * 		- disable 4-pin mode
	 * 		- MCLK ouput is disable
	 * 		- Frequency = 44.1 kHz (x=8,y=51 - automatic setting)
	 * Because we use mode I2STXMODE[3:0]= 0000, I2SDAO[5]=0 and
	 * I2SRX[3:0]=0000, I2SDAI[5] = 1. So we have I2SRX_CLK = I2STX_CLK
	 * --> I2SRXBITRATE = 1 (not divide TXCLK to produce RXCLK)
	 */

	/* Audio Config*/
	I2S_ConfigStruct.wordwidth = I2S_WORDWIDTH_16;
	I2S_ConfigStruct.mono = I2S_STEREO;
	I2S_ConfigStruct.stop = I2S_STOP_ENABLE;
	I2S_ConfigStruct.reset = I2S_RESET_ENABLE;
	I2S_ConfigStruct.ws_sel = I2S_MASTER_MODE;
	I2S_ConfigStruct.mute = I2S_MUTE_DISABLE;
	I2S_Config(LPC_I2S,I2S_TX_MODE,&I2S_ConfigStruct);

	I2S_ConfigStruct.ws_sel = I2S_SLAVE_MODE;
	I2S_Config(LPC_I2S,I2S_RX_MODE,&I2S_ConfigStruct);

	/* Clock Mode Config*/
	I2S_ClkConfig.clksel = I2S_CLKSEL_FRDCLK;
	I2S_ClkConfig.fpin = I2S_4PIN_DISABLE;
	I2S_ClkConfig.mcena = I2S_MCLK_DISABLE;
	I2S_ModeConfig(LPC_I2S,&I2S_ClkConfig,I2S_TX_MODE);
	I2S_ModeConfig(LPC_I2S,&I2S_ClkConfig,I2S_RX_MODE);

	/* Set up frequency and bit rate*/
	I2S_FreqConfig(LPC_I2S, 44100, I2S_TX_MODE);
	I2S_SetBitRate(LPC_I2S, 0, I2S_RX_MODE);

	I2S_Start(LPC_I2S);
	 while(I2STXDone == 0||I2SRXDone == 0){
		if(I2STXDone ==0){
			 I2S_Send(LPC_I2S,I2STXBuffer[I2SWriteLength]);
			 I2SWriteLength +=1;
			 if(I2SWriteLength == BUFFER_SIZE) I2STXDone = 1;
		}
		if(I2SRXDone == 0)
		{
			while(I2S_GetLevel(LPC_I2S,I2S_RX_MODE)==0x00);
			if(dummy == 0) //dummy receive
			{
				i = I2S_Receive(LPC_I2S);
				dummy = 1;
			}
			else
			{
				*(uint32_t *)(&I2SRXBuffer[I2SReadLength]) = I2S_Receive(LPC_I2S);
				I2SReadLength +=1;
			}
			if(I2SReadLength == BUFFER_SIZE) I2SRXDone = 1;
		}
	 }

	 /* print received data */
	 _DBG_("Receive Buffer data: ...");
	 for(i=0;i<BUFFER_SIZE;i++)
	 {
		 _DBH32(I2SRXBuffer[i]);_DBG_("");
	 }

	 /* Validate received data */
	 if(Buffer_Verify())
	 {
		 _DBG_("Verify Buffer: OK...");
	 }
	 else
	 {
		 _DBG_("Verify Buffer: ERROR...");
	 }
	 I2S_DeInit(LPC_I2S);
	 while(1);
}
Пример #18
0
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
       
  /* System clocks configuration ---------------------------------------------*/
  RCC_Configuration();

  /* GPIO configuration ------------------------------------------------------*/
  GPIO_Configuration();

  /* I2S peripheral configuration */
  I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
  I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16bextended;
  I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_48k;
  I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;

  /* I2S3 Master Transmitter to I2S2 Slave Receiver communication ------------*/
  /* I2S3 configuration */
  I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
  I2S_Init(SPI3, &I2S_InitStructure);

  /* I2S2 configuration */
  I2S_InitStructure.I2S_Mode = I2S_Mode_SlaveRx;
  I2S_Init(SPI2, &I2S_InitStructure);

  /* Enable the I2S2 */
  I2S_Cmd(SPI2, ENABLE);

  /* Enable the I2S3 */
  I2S_Cmd(SPI3, ENABLE);

  /* Begin the communication in I2S mode */
  while (RxIdx < BufferSize)
  {
    /* Wait the Tx buffer to be empty */
    while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET)
    {}
    /* Send a data from I2S3 */
    SPI_I2S_SendData(SPI3, I2S3_Buffer_Tx[TxIdx++]);

    /* Wait the Rx buffer to be full */
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET)
    {}
    /* Store the I2S2 received data in the relative data table */
    I2S2_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI2);
  }

  TransferStatus1 = Buffercmp(I2S2_Buffer_Rx, I2S3_Buffer_Tx, BufferSize);
  /* TransferStatus1 = PASSED, if the data transmitted from I2S3 and received by
                               I2S2 are the same 
     TransferStatus1 = FAILED, if the data transmitted from I2S3 and received by
                               I2S2 are different */

  /* Reset TxIdx, RxIdx indexes */
  TxIdx = 0;
  RxIdx = 0;
  
  /* Switch to SPI mode communication ----------------------------------------*/
  /* SPI3 configuration */
  SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI3, &SPI_InitStructure);

  /* SPI2 configuration ------------------------------------------------------*/
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_RxOnly;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
  SPI_Init(SPI2, &SPI_InitStructure);

  /* Enable SPI2 */
  SPI_Cmd(SPI2, ENABLE);
  /* Enable SPI3 */
  SPI_Cmd(SPI3, ENABLE);

  /* Begin the communication in SPI mode */
  while (RxIdx < BufferSize)
  {
    /* Wait the Tx buffer to be empty */
    while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET)
    {}
    /* Send a data from SPI3 */
    SPI_I2S_SendData(SPI3, SPI3_Buffer_Tx[TxIdx++]);

    /* Wait the Rx buffer to be full */
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET)
    {}
    /* Store the SPI2 received data in the relative data table */
    SPI2_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI2);
  }

  TransferStatus2 = Buffercmp(SPI2_Buffer_Rx, SPI3_Buffer_Tx, BufferSize);
  /* TransferStatus2 = PASSED, if the data transmitted from SPI3 and received by
                               SPI2 are the same
     TransferStatus2 = FAILED, if the data transmitted from SPI3 and received by
                               SPI2 are different */

  /* Reset TxIdx, RxIdx indexes and receive table values */
  for (TxIdx = 0; TxIdx < BufferSize; TxIdx++)
  {
    I2S2_Buffer_Rx[TxIdx] = 0;
  }
  
  TxIdx = 0;
  RxIdx = 0;
      
  /* I2S3 Slave Transmitter to I2S2 Master Receiver communication ------------*/
  /* I2S3 configuration */
  I2S_InitStructure.I2S_Mode = I2S_Mode_SlaveTx;
  I2S_Init(SPI3, &I2S_InitStructure);

  /* I2S2 configuration */
  I2S_InitStructure.I2S_Mode = I2S_Mode_MasterRx;
  I2S_Init(SPI2, &I2S_InitStructure);

  /* Wait the Tx buffer to be empty */
  while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET)
  {}
  /* Prepare the first data to be sent from the slave */
  SPI_I2S_SendData(SPI3, I2S3_Buffer_Tx[TxIdx++]);

  /* Enable the I2S3 */
  I2S_Cmd(SPI3, ENABLE);

  /* Enable the I2S2 */
  I2S_Cmd(SPI2, ENABLE);

  /* Begin the communication in I2S mode */
  while (RxIdx < BufferSize)
  {
    /* Wait the Rx buffer to be full */
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET)
    {}
    /* Store the I2S2 received data in the relative data table */
    I2S2_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI2);

    /* Wait the Tx buffer to be empty */
    while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET)
    {}
    /* Send a data from I2S3 */
    SPI_I2S_SendData(SPI3, I2S3_Buffer_Tx[TxIdx++]);
  }

  TransferStatus3 = Buffercmp(I2S2_Buffer_Rx, I2S3_Buffer_Tx, BufferSize);
  /* TransferStatus3 = PASSED, if the data transmitted from I2S3 and received by
                               I2S2 are the same
     TransferStatus3 = FAILED, if the data transmitted from I2S3 and received by
                               I2S2 are different */

  while (1)
  {}
}
Пример #19
0
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* System clocks configuration ---------------------------------------------*/
  RCC_Configuration();

  /* NVIC configuration ------------------------------------------------------*/
  NVIC_Configuration();

  /* GPIO configuration ------------------------------------------------------*/
  GPIO_Configuration();

  SPI_I2S_DeInit(SPI3);
  SPI_I2S_DeInit(SPI2);
  
  /* I2S peripheral configuration */
  I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
  I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16bextended;
  I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_48k;
  I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;

  /* I2S3 Master Transmitter to I2S2 Slave Receiver communication -----------*/
  /* I2S3 configuration */
  I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
  I2S_Init(SPI3, &I2S_InitStructure);

  /* I2S2 configuration */
  I2S_InitStructure.I2S_Mode = I2S_Mode_SlaveRx;
  I2S_Init(SPI2, &I2S_InitStructure);

  /* Enable the I2S3 TxE interrupt */
  SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_TXE, ENABLE);

  /* Enable the I2S2 RxNE interrupt */
  SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);

  /* Enable the I2S2 */
  I2S_Cmd(SPI2, ENABLE);

  /* Enable the I2S3 */
  I2S_Cmd(SPI3, ENABLE);

  /* Wait the end of communication */
  while (RxIdx < 32)
  {}

  TransferStatus1 = Buffercmp(I2S2_Buffer_Rx, (uint16_t*)I2S3_Buffer_Tx, 32);
  /* TransferStatus1 = PASSED, if the data transmitted from I2S3 and received by
                               I2S2 are the same
     TransferStatus1 = FAILED, if the data transmitted from I2S3 and received by
                               I2S2 are different */
  
  /* Reinitialize the buffers */
  for (RxIdx = 0; RxIdx < 32; RxIdx++)
  {
    I2S2_Buffer_Rx[RxIdx] = 0;
  }
  TxIdx = 0;
  RxIdx = 0;

  SPI_I2S_DeInit(SPI3);
  SPI_I2S_DeInit(SPI2);  
  
  /* I2S peripheral configuration */
  I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
  I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_24b;
  I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_16k;
  I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;

  /* I2S3 Master Transmitter to I2S2 Slave Receiver communication -----------*/
  /* I2S3 configuration */
  I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
  I2S_Init(SPI3, &I2S_InitStructure);

  /* I2S2 configuration */
  I2S_InitStructure.I2S_Mode = I2S_Mode_SlaveRx;
  I2S_Init(SPI2, &I2S_InitStructure);

  /* Enable the I2S3 TxE interrupt */
  SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_TXE, ENABLE);

  /* Enable the I2S2 RxNE interrupt */
  SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);

  /* Enable the I2S2 */
  I2S_Cmd(SPI2, ENABLE);

  /* Enable the I2S3 */
  I2S_Cmd(SPI3, ENABLE);

  /* Wait the end of communication */
  while (RxIdx < 32)
  {
  }

  TransferStatus2 = Buffercmp24bits(I2S2_Buffer_Rx, (uint16_t*)I2S3_Buffer_Tx, 32);
  /* TransferStatus2 = PASSED, if the data transmitted from I2S3 and received by
                               I2S2 are the same
     TransferStatus2 = FAILED, if the data transmitted from I2S3 and received by
                               I2S2 are different */     

  while (1)
  {
  }
}
Пример #20
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s)
       before to branch to application main.
     */

  /* I2S configuration -------------------------------------------------------*/
  I2S_Config();

  /* SysTick configuration ---------------------------------------------------*/
  SysTickConfig();

  /* LEDs configuration ------------------------------------------------------*/
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);

#ifdef I2S_MASTER
  /* Master full Duplex configuration ----------------------------------------*/
  /* Clear the Rx Master Buffer */
  Fill_Buffer((uint8_t*)aRxMasterBuffer, (TX_MASTER_BUFFERSIZE*2));

  /* Configure I2Sx in Master Transmitter Mode */
  I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
  I2S_Init(I2Sx, &I2S_InitStructure);

  /* Configure the I2Sx_ext (the second instance) in Slave Receiver Mode */
  I2S_FullDuplexConfig(I2Sxext, &I2S_InitStructure);

  /* Enable the I2Sx peripheral */
  I2S_Cmd(I2Sx, ENABLE);
  /* Enable the I2Sx_ext peripheral for Full Duplex mode */
  I2S_Cmd(I2Sxext, ENABLE);

  /* Master full Duplex Communication ----------------------------------------*/
  /* Communication Full Duplex Started */
  ubBufferCounter = 0;
  while ((ubBufferCounter != TX_MASTER_BUFFERSIZE))
  {
    /* Data to transmitted through I2Sx SD pin */
    while (SPI_I2S_GetFlagStatus(I2Sx, SPI_I2S_FLAG_TXE ) != SET);
    SPI_I2S_SendData(I2Sx, aTxMasterBuffer[ubBufferCounter]);

    /* Data Received through I2Sx_ext SD pin */
    while (SPI_I2S_GetFlagStatus(I2Sxext, SPI_I2S_FLAG_RXNE ) != SET);
    aRxMasterBuffer[ubBufferCounter] = SPI_I2S_ReceiveData(I2Sxext);
    ubBufferCounter++;

  }
  /* Communication Full Duplex Finished */
  I2S_Cmd(I2Sx, DISABLE);
  I2S_Cmd(I2Sxext, DISABLE);

  /* Check Communication Result ----------------------------------------------*/
  if (Buffercmp((uint8_t*)aRxMasterBuffer, (uint8_t*)aTxSlaveBuffer, (TX_SLAVE_BUFFERSIZE*2)) != FAILED)
  {
    /* Turn ON LED2 and LED4 */
    STM_EVAL_LEDOn(LED2);
    STM_EVAL_LEDOn(LED4);
  }
#endif /* I2S_MASTER */

#ifdef I2S_SLAVE
  /* Slave full Duplex configuration ----------------------------------------*/
  /* Clear the RxBuffer */
  Fill_Buffer((uint8_t*)aRxSlaveBuffer, (TX_SLAVE_BUFFERSIZE*2));

  /* Configure I2Sx in Slave Receiver Mode */
  I2S_InitStructure.I2S_Mode = I2S_Mode_SlaveRx;
  I2S_Init(I2Sx, &I2S_InitStructure);

  /* Configure the I2Sx_ext (the second instance) in Slave Transmitter Mode */
  I2S_FullDuplexConfig(I2Sxext, &I2S_InitStructure);

  /* Enable the I2Sx_ext peripheral for Full Duplex mode */
  I2S_Cmd(I2Sxext, ENABLE);
  /* Enable the I2Sx peripheral */
  I2S_Cmd(I2Sx, ENABLE);

  /* Slave full Duplex Communication -----------------------------------------*/
  /* Communication Full Duplex Started */
  ubBufferCounter = 0;
  while ((ubBufferCounter != TX_SLAVE_BUFFERSIZE) )
  {
    /* Data to transmitted through I2Sx_ext SD pin */
    while (SPI_I2S_GetFlagStatus(I2Sxext, SPI_I2S_FLAG_TXE ) != SET);
    SPI_I2S_SendData(I2Sxext, aTxSlaveBuffer[ubBufferCounter]);

    /* Data Received through I2Sx SD pin */
    while (SPI_I2S_GetFlagStatus(I2Sx, SPI_I2S_FLAG_RXNE ) != SET);
    aRxSlaveBuffer[ubBufferCounter] = SPI_I2S_ReceiveData(I2Sx);
    ubBufferCounter++;

  }
  /* Communication full duplex Finished */
  I2S_Cmd(I2Sx, DISABLE);
  I2S_Cmd(I2Sxext, DISABLE);

  /* Check Communication Results ---------------------------------------------*/
  if (Buffercmp((uint8_t*)aRxSlaveBuffer, (uint8_t*)aTxMasterBuffer, (TX_SLAVE_BUFFERSIZE*2)) != FAILED)
  {
    /* Turn ON LED3 */
    STM_EVAL_LEDOn(LED3);
  }
#endif /* I2S_SLAVE */

  while(1)
  {
  }
}
Пример #21
0
/*********************************************************************//**
 * @brief		c_entry: Main program body
 * @param[in]	None
 * @return 		int
 **********************************************************************/
int c_entry(void)
{
	uint32_t i;
	GPDMA_Channel_CFG_Type GPDMACfg;
	I2S_MODEConf_Type I2S_ClkConfig;
	I2S_CFG_Type I2S_ConfigStruct;
	I2S_DMAConf_Type I2S_DMAStruct;
	PINSEL_CFG_Type PinCfg;

	/* Initialize debug via UART0
	 * – 115200bps
	 * – 8 data bit
	 * – No parity
	 * – 1 stop bit
	 * – No flow control
	 */
	debug_frmwrk_init();

	//print menu screen
	print_menu();

	//Initialize buffer
	Buffer_Init();

	_DBG_("Press '1' to initialize buffer...");
	while(_DG !='1');
	_DBG_("Transmit Buffer init: ...");
	for(i=0;i<BUFFER_SIZE;i++)
	{
		_DBH32(I2STXBuffer[i]);_DBG_("");
	}
	_DBG_("Receive Buffer init: ...");
	for(i=0;i<BUFFER_SIZE;i++)
	{
		_DBH32(I2SRXBuffer[i]);_DBG_("");
	}

	/* Pin configuration:
	 * Assign: 	- P0.4 as I2SRX_CLK
	 * 			- P0.5 as I2SRX_WS
	 * 			- P0.6 as I2SRX_SDA
	 * 			- P0.7 as I2STX_CLK
	 * 			- P0.8 as I2STX_WS
	 * 			- P0.9 as I2STX_SDA
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Pinnum = 4;
	PinCfg.Portnum = 0;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 5;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 6;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 7;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 8;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 9;
	PINSEL_ConfigPin(&PinCfg);

	/* Initialize I2S */
	I2S_Init(LPC_I2S);

	//Setup for I2S: RX is similar with TX
	/* setup:
	 * 		- wordwidth: 16 bits
	 * 		- stereo mode
	 * 		- master mode for I2S_TX and slave for I2S_RX
	 * 		- ws_halfperiod is 31
	 * 		- not use mute mode
	 * 		- use reset and stop mode
	 * 		- select the fractional rate divider clock output as the source,
	 * 		- disable 4-pin mode
	 * 		- MCLK ouput is disable
	 * 		- Frequency = 44.1 kHz
	 * Because we use mode I2STXMODE[3:0]= 0000, I2SDAO[5]=0 and
	 * I2SRX[3:0]=0000, I2SDAI[5] = 1. So we have I2SRX_CLK = I2STX_CLK
	 * --> I2SRXBITRATE = 1 (not divide TXCLK to produce RXCLK)
	 */

	/* Audio Config*/
	I2S_ConfigStruct.wordwidth = I2S_WORDWIDTH_16;
	I2S_ConfigStruct.mono = I2S_STEREO;
	I2S_ConfigStruct.stop = I2S_STOP_ENABLE;
	I2S_ConfigStruct.reset = I2S_RESET_ENABLE;
	I2S_ConfigStruct.ws_sel = I2S_MASTER_MODE;
	I2S_ConfigStruct.mute = I2S_MUTE_DISABLE;
	I2S_Config(LPC_I2S,I2S_TX_MODE,&I2S_ConfigStruct);

	I2S_ConfigStruct.ws_sel = I2S_SLAVE_MODE;
	I2S_Config(LPC_I2S,I2S_RX_MODE,&I2S_ConfigStruct);

	/* Clock Mode Config*/
	I2S_ClkConfig.clksel = I2S_CLKSEL_FRDCLK;
	I2S_ClkConfig.fpin = I2S_4PIN_DISABLE;
	I2S_ClkConfig.mcena = I2S_MCLK_DISABLE;
	I2S_ModeConfig(LPC_I2S,&I2S_ClkConfig,I2S_TX_MODE);
	I2S_ModeConfig(LPC_I2S,&I2S_ClkConfig,I2S_RX_MODE);

	/* Set up frequency and bit rate*/
	I2S_FreqConfig(LPC_I2S, 44100, I2S_TX_MODE);
	I2S_SetBitRate(LPC_I2S, 0, I2S_RX_MODE);
	_DBG_("Press '2' to initialize DMA...");
	while(_DG !='2');
	  /* GPDMA Interrupt configuration section ------------------------------------------------- */

	 /* Initialize GPDMA controller */
	 GPDMA_Init();
	 LPC_GPDMA->DMACConfig = 0x01;

	 /* Setting GPDMA interrupt */
     // Disable interrupt for DMA
     NVIC_DisableIRQ (DMA_IRQn);
     /* preemption = 1, sub-priority = 1 */
     NVIC_SetPriority(DMA_IRQn, ((0x01<<3)|0x01));

	/*
	 * Configure GPDMA channel 0 -------------------------------------------------------------
	 * Used for I2S Transmit
	 */
	// Setup GPDMA channel --------------------------------
	// channel 0
	GPDMACfg.ChannelNum = 0;
	// Source memory
	GPDMACfg.SrcMemAddr = DMA_SRC;
	// Destination memory
	GPDMACfg.DstMemAddr = 0;
	// Transfer size
	GPDMACfg.TransferSize = BUFFER_SIZE;
	// Transfer width - unused
	GPDMACfg.TransferWidth = 0;
	// Transfer type
	GPDMACfg.TransferType = GPDMA_TRANSFERTYPE_M2P;
	// Source connection
	GPDMACfg.SrcConn = 0;
	// Destination connection - unused
	GPDMACfg.DstConn = GPDMA_CONN_I2S_Channel_0;
	// Linker List Item - unused
	GPDMACfg.DMALLI = 0;
	GPDMA_Setup(&GPDMACfg);
	_DBG_("DMA Channel 0 setting finised...");
	/* Reset terminal counter */
	Channel0_TC = 0;
	/* Reset Error counter */
	Channel0_Err = 0;

	/*
	* Configure GPDMA channel 1 -------------------------------------------------------------
	* Used for UART0 Receive
	*/
	// Setup GPDMA channel --------------------------------
	// channel 1
	GPDMACfg.ChannelNum = 1;
	// Source memory - unused
	GPDMACfg.SrcMemAddr = 0;
	// Destination memory
	GPDMACfg.DstMemAddr = DMA_DST;
	// Transfer size
	GPDMACfg.TransferSize = BUFFER_SIZE+1;
	// Transfer width - unused
	GPDMACfg.TransferWidth = 0;
	// Transfer type
	GPDMACfg.TransferType = GPDMA_TRANSFERTYPE_P2M;
	// Source connection - unused
	GPDMACfg.SrcConn = GPDMA_CONN_I2S_Channel_1;
	// Destination connection
	GPDMACfg.DstConn = 0;
	// Linker List Item - unused
	GPDMACfg.DMALLI = 0;
	GPDMA_Setup(&GPDMACfg);
	_DBG_("DMA Channel 1 setting finised...");
	/* Reset terminal counter */
	Channel1_TC = 0;
	/* Reset Error counter */
	Channel1_Err = 0;

	// Enable GPDMA channel 0 & 1
	GPDMA_ChannelCmd(0, ENABLE);
	GPDMA_ChannelCmd(1, ENABLE);

	// Enable interrupt for DMA
	NVIC_EnableIRQ (DMA_IRQn);
	_DBG_("Press '3' to start I2S transfer process...");
	while(_DG !='3');
	_DBG_("I2S Start...");

	I2S_DMAStruct.DMAIndex = I2S_DMA_2;
	I2S_DMAStruct.depth = 8;
	I2S_DMAConfig(LPC_I2S, &I2S_DMAStruct, I2S_RX_MODE);
	I2S_DMAStruct.DMAIndex = I2S_DMA_1;
	I2S_DMAStruct.depth = 1;
	I2S_DMAConfig(LPC_I2S, &I2S_DMAStruct, I2S_TX_MODE);

	I2S_Start(LPC_I2S);

	I2S_DMACmd(LPC_I2S, I2S_DMA_2, I2S_RX_MODE, ENABLE);
	I2S_DMACmd(LPC_I2S, I2S_DMA_1, I2S_TX_MODE, ENABLE);

	while ((Channel0_TC == 0)||(Channel1_TC == 0) );

	_DBG_("I2S Finish...");
	_DBG_("Receive Buffer data: ...");
	for(i=0;i<BUFFER_SIZE+1;i++)
	{
	 _DBH32(I2SRXBuffer[i]);
	 if(I2SRXBuffer[i]==0)
	 {
		 _DBG_(" ->Dummy data");
	 }
	 else _DBG_("");
	}
	I2S_DeInit(LPC_I2S);
	while(1);
	return 1;
}
Пример #22
0
void initTX(unsigned int freq, uint32_t txblock, uint32_t rxblock) {

	//Set I2S pins
	PINSEL_CFG_Type PinCfg;
	I2S_CFG_Type I2S_ConfigStruct;
	I2S_MODEConf_Type I2S_ClkConfig;

	PinCfg.Portnum = PINSEL_PORT_0;
	PinCfg.Funcnum = PINSEL_FUNC_1;
	PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
	PinCfg.Pinmode = PINSEL_PINMODE_TRISTATE;

	PinCfg.Pinnum = PINSEL_PIN_4; // P0.4 as I2SRX_CLK
	PINSEL_ConfigPin(&PinCfg);

	PinCfg.Pinnum = PINSEL_PIN_5; // P0.5 as I2SRX_WS
	PINSEL_ConfigPin(&PinCfg);

	PinCfg.Pinnum = PINSEL_PIN_6; // P0.6 as I2SRX_SDA
	PINSEL_ConfigPin(&PinCfg);

	PinCfg.Pinnum = PINSEL_PIN_7; // P0.7 as I2STX_CLK
	PINSEL_ConfigPin(&PinCfg);

	PinCfg.Pinnum = PINSEL_PIN_8; // P0.8 as I2STX_WS
	PINSEL_ConfigPin(&PinCfg);

	PinCfg.Pinnum = PINSEL_PIN_9; // P0.9 as I2STX_SDA
	PINSEL_ConfigPin(&PinCfg);

	PinCfg.Portnum = PINSEL_PORT_4;
	PinCfg.Pinnum = PINSEL_PIN_29;
	PINSEL_ConfigPin(&PinCfg);

	/* I2S init */

	I2S_Init(LPC_I2S);

	I2S_ConfigStruct.wordwidth = I2S_WORDWIDTH_16;
	I2S_ConfigStruct.mono = I2S_STEREO;
	I2S_ConfigStruct.stop = I2S_STOP_DISABLE;
	I2S_ConfigStruct.reset = I2S_RESET_ENABLE;
	I2S_ConfigStruct.ws_sel = I2S_MASTER_MODE;
	I2S_ConfigStruct.mute = I2S_MUTE_DISABLE;
	I2S_Config(LPC_I2S, I2S_TX_MODE, &I2S_ConfigStruct);
	I2S_Config(LPC_I2S, I2S_RX_MODE, &I2S_ConfigStruct);

	I2S_ClkConfig.clksel = I2S_CLKSEL_FRDCLK;
	I2S_ClkConfig.fpin = I2S_4PIN_DISABLE;
	I2S_ClkConfig.mcena = I2S_MCLK_ENABLE;
	I2S_ModeConfig(LPC_I2S, &I2S_ClkConfig, I2S_TX_MODE);

	I2S_ClkConfig.clksel = I2S_CLKSEL_MCLK;
	I2S_ModeConfig(LPC_I2S, &I2S_ClkConfig, I2S_RX_MODE);


	//Setup I2S clocks
	uint8_t x = 14;
	uint8_t y = 31;
	uint8_t divider = 2;

	LPC_SC->PCONP |= (0x01 << 27); // turn on I2S peripheral

	LPC_SC->PCLKSEL1 &= (~(0x03 << 22));

	if (divider == 1)
		LPC_SC->PCLKSEL1 |= (0x01 << 22);
	else if (divider == 2)
		LPC_SC->PCLKSEL1 |= (0x02 << 22);
	else if (divider == 4)
		LPC_SC->PCLKSEL1 |= (0x00 << 22);
	else if (divider == 8)
		LPC_SC->PCLKSEL1 |= (0x03 << 22);

	LPC_I2S->I2STXRATE = (x << 8) | y;
	LPC_I2S->I2SRXRATE = (x << 8) | y;

	uint32_t peripheralClock = SystemCoreClock / divider;
	uint32_t masterClock = (x * peripheralClock) / (2 * y);

	uint32_t bitClock = 16 * 44100 * 2;
	uint8_t bitDivider = masterClock / bitClock;

	LPC_I2S->I2STXBITRATE = bitDivider - 1;
	LPC_I2S->I2SRXBITRATE = bitDivider - 1;

	initI2SDMA(txblock, rxblock);

	I2S_Start(LPC_I2S);

}
Пример #23
0
/*********************************************************************//**
 * @brief	Main I2S program body
 **********************************************************************/
int c_entry (void) {                       /* Main Program */
	uint32_t i;
	uint8_t ch;
	uint8_t dummy=0;
	I2S_MODEConf_Type I2S_ClkConfig;
	I2S_CFG_Type I2S_ConfigStruct;
	I2S_PinCFG_Type I2S_PinStruct;

	// DeInit NVIC and SCBNVIC
	NVIC_DeInit();
	NVIC_SCBDeInit();

	/* Configure the NVIC Preemption Priority Bits:
	 * two (2) bits of preemption priority, six (6) bits of sub-priority.
	 * Since the Number of Bits used for Priority Levels is five (5), so the
	 * actual bit number of sub-priority is three (3)
	 */
	NVIC_SetPriorityGrouping(0x05);

	//  Set Vector table offset value
#if (__RAM_MODE__==1)
	NVIC_SetVTOR(0x10000000);
#else
	NVIC_SetVTOR(0x00000000);
#endif
	 NVIC_SetPriorityGrouping(0x06);

	debug_frmwrk_init();
	print_menu();

	_DBG_("Press '1' to initialize buffer...");
	while(_DG !='1');
	Buffer_Init();
	_DBG_("Transmit Buffer init: ...");
	for(i=0;i<BUFFER_SIZE;i++)
	{
		_DBH32(I2STXBuffer[i]);_DBG_("");
	}
	_DBG_("Receive Buffer init: ...");
	for(i=0;i<BUFFER_SIZE;i++)
	{
		_DBH32(I2SRXBuffer[i]);_DBG_("");
	}

	/* Initializes pin corresponding to I2S function */
	I2S_PinStruct.CLK_Pin=I2S_STX_CLK_P0_7;
	I2S_PinStruct.WS_Pin=I2S_STX_WS_P0_8;
	I2S_PinStruct.SDA_Pin=I2S_STX_SDA_P0_9;
	I2S_PinStruct.MCLK_Pin=I2S_TX_MCLK_P4_29;

	PINSEL_ConfigPin((PINSEL_CFG_Type *) (&i2s_stx_clk_pin[I2S_PinStruct.CLK_Pin]));
	PINSEL_ConfigPin((PINSEL_CFG_Type *) (&i2s_stx_ws_pin[I2S_PinStruct.WS_Pin]));
	PINSEL_ConfigPin((PINSEL_CFG_Type *) (&i2s_stx_sda_pin[I2S_PinStruct.SDA_Pin]));
	PINSEL_ConfigPin((PINSEL_CFG_Type *) (&i2s_tx_mclk_pin[I2S_PinStruct.MCLK_Pin]));

	// Configure pinsel for I2S_RX
	I2S_PinStruct.CLK_Pin=I2S_SRX_CLK_P0_4;
	I2S_PinStruct.WS_Pin=I2S_SRX_WS_P0_5;
	I2S_PinStruct.SDA_Pin=I2S_SRX_SDA_P0_6;
	I2S_PinStruct.MCLK_Pin=I2S_RX_MCLK_P4_28;

	PINSEL_ConfigPin((PINSEL_CFG_Type *) (&i2s_srx_clk_pin[I2S_PinStruct.CLK_Pin]));
	PINSEL_ConfigPin((PINSEL_CFG_Type *) (&i2s_srx_ws_pin[I2S_PinStruct.WS_Pin]));
	PINSEL_ConfigPin((PINSEL_CFG_Type *) (&i2s_srx_sda_pin[I2S_PinStruct.SDA_Pin]));
	PINSEL_ConfigPin((PINSEL_CFG_Type *) (&i2s_rx_mclk_pin[I2S_PinStruct.MCLK_Pin]));

	I2S_Init(LPC_I2S);

	//Setup for I2S: RX is similar with TX
	/* setup:
	 * 		- wordwidth: 16 bits
	 * 		- stereo mode
	 * 		- master mode for I2S_TX and slave for I2S_RX
	 * 		- ws_halfperiod is 31
	 * 		- not use mute mode
	 * 		- use reset and stop mode
	 * 		- select the fractional rate divider clock output as the source,
	 * 		- disable 4-pin mode
	 * 		- MCLK ouput is disable
	 * 		- Frequency = 44.1 kHz (x=8,y=51 - automatic setting)
	 * Because we use mode I2STXMODE[3:0]= 0000, I2SDAO[5]=0 and
	 * I2SRX[3:0]=0000, I2SDAI[5] = 1. So we have I2SRX_CLK = I2STX_CLK
	 * --> I2SRXBITRATE = 1 (not divide TXCLK to produce RXCLK)
	 */

	/* Audio Config*/
	I2S_ConfigStruct.wordwidth = I2S_WORDWIDTH_16;
	I2S_ConfigStruct.mono = I2S_STEREO;
	I2S_ConfigStruct.stop = I2S_STOP_ENABLE;
	I2S_ConfigStruct.reset = I2S_RESET_ENABLE;
	I2S_ConfigStruct.ws_sel = I2S_MASTER_MODE;
	I2S_ConfigStruct.mute = I2S_MUTE_DISABLE;
	I2S_Config(LPC_I2S,I2S_TX_MODE,&I2S_ConfigStruct);

	I2S_ConfigStruct.ws_sel = I2S_SLAVE_MODE;
	I2S_Config(LPC_I2S,I2S_RX_MODE,&I2S_ConfigStruct);

	/* Clock Mode Config*/
	I2S_ClkConfig.clksel = I2S_CLKSEL_0;
	I2S_ClkConfig.fpin = I2S_4PIN_DISABLE;
	I2S_ClkConfig.mcena = I2S_MCLK_DISABLE;
	I2S_ModeConfig(LPC_I2S,&I2S_ClkConfig,I2S_TX_MODE);
	I2S_ClkConfig.fpin = I2S_4PIN_ENABLE;
	I2S_ModeConfig(LPC_I2S,&I2S_ClkConfig,I2S_RX_MODE);

	/* Set up frequency and bit rate*/
	I2S_FreqConfig(LPC_I2S, 44100, I2S_TX_MODE);
//	I2S_SetBitRate(I2S, 1, I2S_RX_MODE);

	 I2S_Start(LPC_I2S);
	 _DBG_("Press '2' to start I2S transfer process...");
	 while(_DG !='2');
	 _DBG_("I2S Start ...");
	 while(I2STXDone == 0||I2SRXDone == 0){
		 if(I2STXDone ==0){
			 while (I2S_GetLevel(LPC_I2S,I2S_TX_MODE)!=0x00);
			 I2S_Send(LPC_I2S,I2STXBuffer[I2SWriteLength]);
			 I2SWriteLength +=1;
			 if(I2SWriteLength == BUFFER_SIZE) I2STXDone = 1;

		}
		if(I2SRXDone == 0)
		{
			while(I2S_GetLevel(LPC_I2S,I2S_RX_MODE)==0x00);
			if(dummy == 0) //dummy receive
			{
				i = I2S_Receive(LPC_I2S);
				if(i!=0)
				{
					*(uint32_t *)(&I2SRXBuffer[I2SReadLength]) = i;
					I2SReadLength +=1;
					dummy = 1;
				}
			}
			else
			{
				*(uint32_t *)(&I2SRXBuffer[I2SReadLength]) = I2S_Receive(LPC_I2S);
				I2SReadLength +=1;
			}
			if(I2SReadLength == BUFFER_SIZE) I2SRXDone = 1;
		}
	 }
	 _DBG_("I2S Finish...");

	 _DBG_("Receive Buffer data: ...");
	 for(i=0;i<BUFFER_SIZE;i++)
	 {
		 _DBH32(I2SRXBuffer[i]);_DBG_("");
	 }
	 if(Buffer_Verify())
	 {
		 _DBG_("Verify Buffer: OK...");
	 }
	 else
	 {
		 _DBG_("Verify Buffer: ERROR...");
	 }
	 while(1);
}
/**
  * @brief  Configures I2S peripheral.
  * @param  None
  * @retval None
  */
static void I2S_Config(void)
{
  I2S_InitTypeDef I2S_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable SPI1 APB clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

  /* Enable GPIOA, GPIOB clocks */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB, ENABLE);

   /* I2S1 Pins configuration */
#ifdef USE_STM320518_EVAL
  /* Configure pins as AF */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);   
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 |GPIO_Pin_5;
  GPIO_Init(GPIOB, &GPIO_InitStructure); 
  
  /* Connect pin to Periph */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_0); 
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_0);    
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_0); 
#else 
  /* I2S1 Pins configuration */
  /* Configure pins as AF */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7  ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);   
  
  /* Connect pin to Periph */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_0); 
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0);    
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0); 
#endif /* USE_STM320518_EVAL */

  /* I2S peripheral configuration */
  I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
  I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16bextended;
  I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_48k;
  I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;

#if defined (I2S_MASTER_TRANSMITTER)
  /* I2S Master Transmitter configuration */
  I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
#elif defined (I2S_SLAVE_RECEIVER)
  /* I2S Slave Receiver configuration */
  I2S_InitStructure.I2S_Mode = I2S_Mode_SlaveRx;
#endif
  I2S_Init(SPI1, &I2S_InitStructure);

  /* SPI1 IRQ Channel configuration */
  NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
Пример #25
0
void CS43L22_Config() {
	  // configure STM32F4Discovery I2S pins

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE);

	GPIO_InitTypeDef GPIO_InitStructure;
	  GPIO_StructInit(&GPIO_InitStructure);
	  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

	  GPIO_InitStructure.GPIO_Pin = STM32F4D_I2S_WS_PIN;
	  GPIO_Init(STM32F4D_I2S_WS_PORT, &GPIO_InitStructure);
	  GPIO_PinAFConfig(STM32F4D_I2S_WS_PORT, STM32F4D_I2S_WS_PINSRC, GPIO_AF_SPI3);

	  GPIO_InitStructure.GPIO_Pin = STM32F4D_I2S_CK_PIN;
	  GPIO_Init(STM32F4D_I2S_CK_PORT, &GPIO_InitStructure);
	  GPIO_PinAFConfig(STM32F4D_I2S_CK_PORT, STM32F4D_I2S_CK_PINSRC, GPIO_AF_SPI3);

	  GPIO_InitStructure.GPIO_Pin = STM32F4D_I2S_SD_PIN;
	  GPIO_Init(STM32F4D_I2S_SD_PORT, &GPIO_InitStructure);
	  GPIO_PinAFConfig(STM32F4D_I2S_SD_PORT, STM32F4D_I2S_SD_PINSRC, GPIO_AF_SPI3);

	  GPIO_InitStructure.GPIO_Pin = STM32F4D_I2S_MCLK_PIN;
	  GPIO_Init(STM32F4D_I2S_MCLK_PORT, &GPIO_InitStructure);
	  GPIO_PinAFConfig(STM32F4D_I2S_MCLK_PORT, STM32F4D_I2S_MCLK_PINSRC, GPIO_AF_SPI3);

	  // configure I2C pins to access the CS43L22 configuration registers

	  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
	  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

	  GPIO_InitStructure.GPIO_Pin = CODEC_I2C_SCL_PIN;
	  GPIO_Init(CODEC_I2C_SCL_PORT, &GPIO_InitStructure);
	  GPIO_PinAFConfig(CODEC_I2C_SCL_PORT, GPIO_PinSource6, GPIO_AF_I2C1);

	  GPIO_InitStructure.GPIO_Pin = CODEC_I2C_SDA_PIN;
	  GPIO_Init(CODEC_I2C_SDA_PORT, &GPIO_InitStructure);
	  GPIO_PinAFConfig(CODEC_I2C_SDA_PORT, GPIO_PinSource9, GPIO_AF_I2C1);

	  // CS43L22 reset pin
	  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	  GPIO_InitStructure.GPIO_Pin = CODEC_RESET_PIN;
	  GPIO_Init(CODEC_RESET_PORT, &GPIO_InitStructure);

	  GPIO_ResetBits(CODEC_RESET_PORT, CODEC_RESET_PIN); // activate reset

	  // I2S initialisation
	  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
	  RCC_PLLI2SCmd(ENABLE); // new for STM32F4: enable I2S PLL
	  SPI_I2S_DeInit(SPI3);
	  I2S_InitTypeDef I2S_InitStructure;
	  I2S_StructInit(&I2S_InitStructure);
	  I2S_InitStructure.I2S_Standard = STM32F4D_I2S_STANDARD;
	  I2S_InitStructure.I2S_DataFormat = STM32F4D_I2S_DATA_FORMAT;
	  I2S_InitStructure.I2S_MCLKOutput = STM32F4D_I2S_MCLK_ENABLE ? I2S_MCLKOutput_Enable : I2S_MCLKOutput_Disable;
	  I2S_InitStructure.I2S_AudioFreq  = (u16)(STM32F4D_I2S_AUDIO_FREQ);
	  I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low; // configuration required as well?
	  I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
	  I2S_Init(SPI3, &I2S_InitStructure);
	  I2S_Cmd(SPI3, ENABLE);

	  // DMA Configuration for SPI Tx Event
	  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);

	  DMA_InitTypeDef DMA_InitStructure;
	  DMA_StructInit(&DMA_InitStructure);

	  DMA_Cmd(DMA1_Stream5, DISABLE);
	  DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_TCIF5 | DMA_FLAG_TEIF5 | DMA_FLAG_HTIF5 | DMA_FLAG_FEIF5);
	  DMA_InitStructure.DMA_Channel = DMA_Channel_0;
	  DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&SPI3->DR;
	  //  DMA_InitStructure.DMA_MemoryBaseAddr = ...; // configured in CS43L22_Start
	  DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
	  //  DMA_InitStructure.DMA_BufferSize = ...; // configured in CS43L22_Start
	  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
	  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
	  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
	  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
	  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

	  DMA_Init(DMA1_Stream5, &DMA_InitStructure);
	  // DMA_Cmd(DMA1_Stream5, ENABLE); // done on CS43L22_Start

	  DMA_ITConfig(DMA1_Stream5, DMA_IT_TE | DMA_IT_FE, DISABLE);
	  // trigger interrupt when transfer half complete/complete
	  DMA_ITConfig(DMA1_Stream5, DMA_IT_HT | DMA_IT_TC, ENABLE);

	  // enable SPI interrupts to DMA
	  SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx, ENABLE);

	  // Configure and enable DMA interrupt
	  /* Configure the DMA IRQ handler priority */
	  NVIC_SetPriority(DMA1_Stream5_IRQn, 0x0);
	  NVIC_EnableIRQ(DMA1_Stream5_IRQn);

	  // configure I2C
	  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
	  I2C_DeInit(CODEC_I2C);
	  I2C_InitTypeDef I2C_InitStructure;
	  I2C_StructInit(&I2C_InitStructure);
	  I2C_InitStructure.I2C_ClockSpeed = 100000;
	  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
	  I2C_InitStructure.I2C_OwnAddress1 = CORE_I2C_ADDRESS;
	  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

	  I2C_Cmd(CODEC_I2C, ENABLE);
	  I2C_Init(CODEC_I2C, &I2C_InitStructure);

	  codec_init();
}
Пример #26
0
/*---------------------------------------------------------------------------------------------------------*/
int main (void)
{

    /* Init System, IP clock and multi-function I/O */
    SYS_Init();     

    /* Init UART0 for printf */
    UART0_Init();
    
    /* Init I2C */
    I2C_Init();

#if defined(DEMO_OPT_SLAVE) // I2S is slave     
    /* 
       I2S is initialized to be slave mode.
       I2S must output 6MHz MCLK to WAU8822.
       Init Codec WAU8822 to be in master mode
       WAU8822 will use MCLK of I2S to generate required 256fs(ex: 12.288MHz) and output BCLK, LR.
       Let WAU8822 be master could get more precise sampling rate.
    
       Supported sample rate are (Deviation dependent on codec):
            48kHz, 32kHz, 24kHz, 16kHz, 12kHz and 8kHz.

       Clock configuration summary: 
    
           XTAL = 12MHz
           PLL = According to HCLK (Independent to sampling rate)
           HCLK = User define (Independent to sampling rate)
           I2S_S = XTAL
           MCLK = 6MHz (XTAL / 2)
           BCLK = (From codec)
           fs   = 8000Hz, 12000Hz, ... 48000Hz
           
    */
    I2S_Init(0, 0); // I2S Slave Init, MCLK = 6MHz
    WAU8822_Init(WAU8822_INIT_MASTER | WAU8822_INIT_SR32000 | 
                 WAU8822_INIT_OUT_HP_L | WAU8822_INIT_OUT_HP_R | 
                 WAU8822_INIT_IN_MIC_L //| 
                 //WAU8822_INIT_IN_AUX_L | WAU8822_INIT_IN_AUX_R
                );
#elif defined(DEMO_OPT_MASTER_NOUSB) // I2S is master without USB applications                
    /* 
       I2S is initialized to be master mode.
       I2S must output 256*fs MCLK to WAU8822.
       Init Codec WAU8822 to be in slave mode.
       
       Supported sample rate are (Deviation for 48kHz, 32kHz,... are all 0.0186%):
       
            47991.071Hz, 31994.048Hz, 23995.536Hz, 15997.024Hz, 11997.768Hz, (7998.5119Hz)
            
       
       Clock configuration summary for 47991.071Hz, 31994.048Hz, 23995.536Hz, 15997.024Hz:
        
           XTAL = 12MHz
           PLL = 147428571.4Hz (PLLCON = SYSCLK_PLLCON_FOR_I2S)
           HCLK = PLL / 3 = 49142857.14Hz
           I2S_S = HCLK
           MCLK = 256*fs (MCLK DIV = 4, 6, 8, 12)
           BCLK = 8*fs (BCLK DIV = 32, 48, 64, 96)
           fs = 47991.071Hz, 31994.048Hz, 23995.536Hz, 15997.024Hz
           
       Clock configuration summary for 11997.768Hz, 7998.5119Hz:
        
           XTAL = 12MHz
           PLL = 147428571.4Hz (PLLCON = 0xA54)
           HCLK = PLL / 6 = 24571428.57Hz
           I2S_S = HCLK
           MCLK = 256*fs (MCLK DIV = 8, 12)
           BCLK = 8*fs (BCLK DIV = 64, 96)
           fs = 11997.768Hz, 7998.5119Hz
           
    */
    I2S_Init(1, 32000); // I2S Init to Master, MCLK = 256 * fs
    WAU8822_Init(WAU8822_INIT_SLAVE | WAU8822_INIT_SR32000 | 
                 WAU8822_INIT_OUT_HP_L | WAU8822_INIT_OUT_HP_R | 
                 WAU8822_INIT_IN_MIC_L //| 
                 //WAU8822_INIT_IN_AUX_L | WAU8822_INIT_IN_AUX_R
                );

#elif defined(DEMO_OPT_MASTER_USB) // I2S is master with USB applications
    /* 
       I2S is initialized to be master mode and work with USB (48MHz clock is necessary)
       I2S must output 256*fs MCLK to WAU8822.
       Init Codec WAU8822 to be in slave mode.
       
       Supported sample rate are (Deviation for 48kHz, 32kHz, ... are all 2.3438%):
       
            46875Hz, 31250Hz, 23437.5Hz, 15625Hz, 11718.75Hz, (7812.5Hz)
            
       
       Clock configuration summary for 46875Hz, 31250Hz, 23437.5Hz, 15625Hz:
        
           XTAL = 12MHz
           PLL = 48MHz (PLLCON = SYSCLK_PLLCON_48MHz_XTAL)
           HCLK = PLL / 1 = 48MHz
           I2S_S = HCLK
           MCLK = 256*fs (MCLK DIV = 4, 6, 8, 12)
           BCLK = 8*fs (BCLK DIV = 32, 48, 64, 96)
           fs = 46875Hz, 31250Hz, 23437.5Hz, 15625Hz
           
       Clock configuration summary for 11718.75Hz, 7812.5Hz:
        
           XTAL = 12MHz
           PLL = 48MHz (PLLCON = SYSCLK_PLLCON_48MHz_XTAL)
           HCLK = PLL / 2 = 24MHz
           I2S_S = HCLK
           MCLK = 256*fs (MCLK DIV = 8, 12)
           BCLK = 8*fs (BCLK DIV = 64, 96)
           fs = 11718.75Hz, 7812.5Hz
       
    */
    I2S_Init(1, 12000); // I2S Master Init, MCLK = 256*fs
    WAU8822_Init(WAU8822_INIT_SLAVE | WAU8822_INIT_SR12000 | 
                 WAU8822_INIT_OUT_HP_L | WAU8822_INIT_OUT_HP_R | 
                 WAU8822_INIT_IN_MIC_L //| 
                 //WAU8822_INIT_IN_AUX_L | WAU8822_INIT_IN_AUX_R
                );
#endif

    MUTE_OFF();

    printf("+----------------------------------------------+\n");
    printf("|        NUC200 I2S Driver Sample Code         |\n");
    printf("+----------------------------------------------+\n");
	printf("\n");
    printf("    Working ...\n");
    printf("    User can measure LRCLK to check sampling rate.\n");
    
    /* Toggle PC15 to indicate we are working. */
    while(1)
    {
        SYS_SysTickDelay(100000);
        PC15 ^= 1;
    }

}
Пример #27
0
void Audio_Init(uint32_t samplefreq)
{
	I2S_MODEConf_Type I2S_ClkConfig;
	I2S_CFG_Type I2S_ConfigStruct;

	Audio_Reset_Data_Buffer();
	NVIC_DisableIRQ(I2SIRQ);

	switch(samplefreq){
	case 11025:
	case 22050:
	case 44100:
		audio_buffer_size = 1764;
		break;
	case 8000:
	case 16000:
	case 32000:
	case 48000:
	default:
		audio_buffer_size = samplefreq * 4 * AUDIO_MAX_PC / 1000;
		break;
	}
	
	/* Reset UDA1380 on board Hitex */
//	scu_pinmux(8,2,MD_PUP, FUNC0);
//	GPIO_SetDir(4, 1<<2, 1);
//	GPIO_ClearValue(4, 1<<2);

/* Initialize I2S peripheral ------------------------------------*/
	/* Init I2C */
	I2C_Init(LPC_I2C, 100000);
	/* Enable Slave I2C operation */
	I2C_Cmd(LPC_I2C, ENABLE);
	/* Init UDA1380 CODEC */
	UDA1380_init();
/* Initialize I2S peripheral ------------------------------------*/
	scu_pinmux(6,0, MD_PUP, 4);			// I2S_RX_SCK
	scu_pinmux(6,1, MD_PUP, 3);			// I2S_RX_WS
	scu_pinmux(6,2, MD_PUP, 3);			// I2S_RX_SDA

	scu_pinmux(3,0, MD_PUP, 2);			// I2S_TX_SCK
	scu_pinmux(3,1, MD_PUP, 0);			// I2S_TX_WS
	scu_pinmux(3,2, MD_PUP, 0);			// I2S_TX_SDA

	I2S_Init(I2S);

	/* setup:
	 * 		- wordwidth: 16 bits
	 * 		- stereo mode
	 * 		- master mode for I2S_TX
	 * 		- Frequency = 44.1 kHz
	 */

	/* Audio Config*/
	I2S_ConfigStruct.wordwidth = I2S_WORDWIDTH_16;
	I2S_ConfigStruct.mono = I2S_STEREO;
	I2S_ConfigStruct.stop = I2S_STOP_ENABLE;
	I2S_ConfigStruct.reset = I2S_RESET_ENABLE;
	I2S_ConfigStruct.ws_sel = I2S_MASTER_MODE;
	I2S_ConfigStruct.mute = I2S_MUTE_DISABLE;
	I2S_Config(I2S,I2S_TX_MODE,&I2S_ConfigStruct);

	/* Clock Mode Config*/
	I2S_ClkConfig.clksel = I2S_CLKSEL_FRDCLK;
	I2S_ClkConfig.fpin = I2S_4PIN_DISABLE;
	I2S_ClkConfig.mcena = I2S_MCLK_DISABLE;
	I2S_ModeConfig(I2S,&I2S_ClkConfig,I2S_TX_MODE);

	I2S_FreqConfig(I2S, samplefreq, I2S_TX_MODE);

	I2S_Stop(I2S, I2S_TX_MODE);

	/* TX FIFO depth is 4 */
	I2S_IRQConfig(I2S,I2S_TX_MODE,I2S_TX_LEVEL);
	I2S_IRQCmd(I2S,I2S_TX_MODE,ENABLE);
	I2S_Start(I2S);
	NVIC_EnableIRQ(I2SIRQ);
}
Пример #28
0
void codec_init()
{
	GPIO_InitTypeDef PinInitStruct;
	GPIO_StructInit(&PinInitStruct);

	I2S_InitTypeDef I2S_InitType;

	I2C_InitTypeDef I2C_InitType;

	//Reset pin as GPIO
	PinInitStruct.GPIO_Pin = CODEC_RESET_PIN;
	PinInitStruct.GPIO_Mode = GPIO_Mode_OUT;
	PinInitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;
	PinInitStruct.GPIO_OType = GPIO_OType_PP;
	PinInitStruct.GPIO_Speed = GPIO_Speed_50MHz;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOB, ENABLE);

	GPIO_Init(GPIOD, &PinInitStruct);

	// I2C pins
	PinInitStruct.GPIO_Mode = GPIO_Mode_AF;
	PinInitStruct.GPIO_OType = GPIO_OType_OD;
	PinInitStruct.GPIO_Pin = I2C_SCL_PIN | I2C_SDA_PIN;
	PinInitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
	PinInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &PinInitStruct);

	GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);

	//enable I2S and I2C clocks
	//RCC_I2SCLKConfig(RCC_I2S2CLKSource_PLLI2S);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 | RCC_APB1Periph_SPI3, ENABLE);
	RCC_PLLI2SCmd(ENABLE);

	// I2S pins
	PinInitStruct.GPIO_OType = GPIO_OType_PP;
	PinInitStruct.GPIO_Pin = I2S3_SCLK_PIN | I2S3_SD_PIN | I2S3_MCLK_PIN;
	GPIO_Init(GPIOC, &PinInitStruct);

	PinInitStruct.GPIO_Pin = I2S3_WS_PIN;
	GPIO_Init(GPIOA, &PinInitStruct);

	//prepare output ports for alternate function
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI3);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_SPI3);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);


	//keep Codec off for now
	GPIO_ResetBits(GPIOD, CODEC_RESET_PIN);


	// configure I2S port
	SPI_I2S_DeInit(CODEC_I2S);
	I2S_InitType.I2S_AudioFreq = I2S_AudioFreq_11k;			// <-- tutaj zmienione
	I2S_InitType.I2S_MCLKOutput = I2S_MCLKOutput_Enable;
	I2S_InitType.I2S_DataFormat = I2S_DataFormat_16b;		// <-- nie zmienioone, ale dlatego nie trzeba zmniejszac wartosci na 12 bitow
	I2S_InitType.I2S_Mode = I2S_Mode_MasterTx;
	I2S_InitType.I2S_Standard = I2S_Standard_Phillips;
	I2S_InitType.I2S_CPOL = I2S_CPOL_Low;

	I2S_Init(CODEC_I2S, &I2S_InitType);
	I2S_Cmd(CODEC_I2S, ENABLE);


	// configure I2C port
	I2C_DeInit(CODEC_I2C);
	I2C_InitType.I2C_ClockSpeed = 100000;
	I2C_InitType.I2C_Mode = I2C_Mode_I2C;
	I2C_InitType.I2C_OwnAddress1 = CORE_I2C_ADDRESS;
	I2C_InitType.I2C_Ack = I2C_Ack_Enable;
	I2C_InitType.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitType.I2C_DutyCycle = I2C_DutyCycle_2;

	I2C_Cmd(CODEC_I2C, ENABLE);
	I2C_Init(CODEC_I2C, &I2C_InitType);
}