STM32F10x_StdPeriph_Examples/I2S/Interrupt/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    I2S/Interrupt/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f10x.h"
00023 
00024 /** @addtogroup STM32F10x_StdPeriph_Examples
00025   * @{
00026   */
00027 
00028 /** @addtogroup I2S_Interrupt
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00034 
00035 /* Private define ------------------------------------------------------------*/
00036 /* Private macro -------------------------------------------------------------*/
00037 /* Private variables ---------------------------------------------------------*/
00038 I2S_InitTypeDef I2S_InitStructure;
00039 const uint16_t I2S3_Buffer_Tx[32] = {0x0102, 0x0304, 0x0506, 0x0708, 0x090A, 0x0B0C,
00040                                      0x0D0E, 0x0F10, 0x1112, 0x1314, 0x1516, 0x1718,
00041                                      0x191A, 0x1B1C, 0x1D1E, 0x1F20, 0x2122, 0x2324,
00042                                      0x2526, 0x2728, 0x292A, 0x2B2C, 0x2D2E, 0x2F30,
00043                                      0x3132, 0x3334, 0x3536, 0x3738, 0x393A, 0x3B3C,
00044                                      0x3D3E, 0x3F40};
00045 
00046 uint16_t I2S2_Buffer_Rx[32];
00047 __IO uint32_t TxIdx = 0, RxIdx = 0;
00048 TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
00049 ErrorStatus HSEStartUpStatus;
00050 
00051 /* Private function prototypes -----------------------------------------------*/
00052 void RCC_Configuration(void);
00053 void GPIO_Configuration(void);
00054 void NVIC_Configuration(void);
00055 TestStatus Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength);
00056 TestStatus Buffercmp24bits(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength);
00057 
00058 /* Private functions ---------------------------------------------------------*/
00059 
00060 /**
00061   * @brief   Main program
00062   * @param  None
00063   * @retval None
00064   */
00065 int main(void)
00066 {
00067   /*!< At this stage the microcontroller clock setting is already configured, 
00068        this is done through SystemInit() function which is called from startup
00069        file (startup_stm32f10x_xx.s) before to branch to application main.
00070        To reconfigure the default setting of SystemInit() function, refer to
00071        system_stm32f10x.c file
00072      */     
00073        
00074   /* System clocks configuration ---------------------------------------------*/
00075   RCC_Configuration();
00076 
00077   /* NVIC configuration ------------------------------------------------------*/
00078   NVIC_Configuration();
00079 
00080   /* GPIO configuration ------------------------------------------------------*/
00081   GPIO_Configuration();
00082 
00083   SPI_I2S_DeInit(SPI3);
00084   SPI_I2S_DeInit(SPI2);
00085   
00086   /* I2S peripheral configuration */
00087   I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
00088   I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16bextended;
00089   I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
00090   I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_48k;
00091   I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
00092 
00093   /* I2S3 Master Transmitter to I2S2 Slave Receiver communication -----------*/
00094   /* I2S3 configuration */
00095   I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
00096   I2S_Init(SPI3, &I2S_InitStructure);
00097 
00098   /* I2S2 configuration */
00099   I2S_InitStructure.I2S_Mode = I2S_Mode_SlaveRx;
00100   I2S_Init(SPI2, &I2S_InitStructure);
00101 
00102   /* Enable the I2S3 TxE interrupt */
00103   SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_TXE, ENABLE);
00104 
00105   /* Enable the I2S2 RxNE interrupt */
00106   SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
00107 
00108   /* Enable the I2S2 */
00109   I2S_Cmd(SPI2, ENABLE);
00110 
00111   /* Enable the I2S3 */
00112   I2S_Cmd(SPI3, ENABLE);
00113 
00114   /* Wait the end of communication */
00115   while (RxIdx < 32)
00116   {}
00117 
00118   TransferStatus1 = Buffercmp(I2S2_Buffer_Rx, (uint16_t*)I2S3_Buffer_Tx, 32);
00119   /* TransferStatus1 = PASSED, if the data transmitted from I2S3 and received by
00120                                I2S2 are the same
00121      TransferStatus1 = FAILED, if the data transmitted from I2S3 and received by
00122                                I2S2 are different */
00123   
00124   /* Reinitialize the buffers */
00125   for (RxIdx = 0; RxIdx < 32; RxIdx++)
00126   {
00127     I2S2_Buffer_Rx[RxIdx] = 0;
00128   }
00129   TxIdx = 0;
00130   RxIdx = 0;
00131 
00132   SPI_I2S_DeInit(SPI3);
00133   SPI_I2S_DeInit(SPI2);  
00134   
00135   /* I2S peripheral configuration */
00136   I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
00137   I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_24b;
00138   I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
00139   I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_16k;
00140   I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
00141 
00142   /* I2S3 Master Transmitter to I2S2 Slave Receiver communication -----------*/
00143   /* I2S3 configuration */
00144   I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
00145   I2S_Init(SPI3, &I2S_InitStructure);
00146 
00147   /* I2S2 configuration */
00148   I2S_InitStructure.I2S_Mode = I2S_Mode_SlaveRx;
00149   I2S_Init(SPI2, &I2S_InitStructure);
00150 
00151   /* Enable the I2S3 TxE interrupt */
00152   SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_TXE, ENABLE);
00153 
00154   /* Enable the I2S2 RxNE interrupt */
00155   SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
00156 
00157   /* Enable the I2S2 */
00158   I2S_Cmd(SPI2, ENABLE);
00159 
00160   /* Enable the I2S3 */
00161   I2S_Cmd(SPI3, ENABLE);
00162 
00163   /* Wait the end of communication */
00164   while (RxIdx < 32)
00165   {
00166   }
00167 
00168   TransferStatus2 = Buffercmp24bits(I2S2_Buffer_Rx, (uint16_t*)I2S3_Buffer_Tx, 32);
00169   /* TransferStatus2 = PASSED, if the data transmitted from I2S3 and received by
00170                                I2S2 are the same
00171      TransferStatus2 = FAILED, if the data transmitted from I2S3 and received by
00172                                I2S2 are different */     
00173 
00174   while (1)
00175   {
00176   }
00177 }
00178 
00179 /**
00180   * @brief  Configures the different system clocks.
00181   * @param  None
00182   * @retval None
00183   */
00184 void RCC_Configuration(void)
00185 {
00186   /* RCC system reset(for debug purpose) */
00187   RCC_DeInit();
00188 
00189   /* Enable HSE */
00190   RCC_HSEConfig(RCC_HSE_ON);
00191 
00192   /* Wait till HSE is ready */
00193   HSEStartUpStatus = RCC_WaitForHSEStartUp();
00194 
00195   if(HSEStartUpStatus == SUCCESS)
00196   {
00197     /* Enable Prefetch Buffer */
00198     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
00199 
00200     /* Flash 2 wait state */
00201     FLASH_SetLatency(FLASH_Latency_2);
00202         
00203     /* HCLK = SYSCLK */
00204     RCC_HCLKConfig(RCC_SYSCLK_Div1); 
00205   
00206     /* PCLK2 = HCLK */
00207     RCC_PCLK2Config(RCC_HCLK_Div1); 
00208 
00209     /* PCLK1 = HCLK/2 */
00210     RCC_PCLK1Config(RCC_HCLK_Div2);
00211 
00212     /* ADCCLK = PCLK2/4 */
00213     RCC_ADCCLKConfig(RCC_PCLK2_Div4); 
00214   
00215 #ifndef STM32F10X_CL  
00216     /* PLLCLK = 8MHz * 9 = 72 MHz */
00217     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
00218 
00219 #else
00220     /* Configure PLLs *********************************************************/
00221     /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
00222     RCC_PREDIV2Config(RCC_PREDIV2_Div5);
00223     RCC_PLL2Config(RCC_PLL2Mul_8);
00224 
00225     /* Enable PLL2 */
00226     RCC_PLL2Cmd(ENABLE);
00227 
00228     /* Wait till PLL2 is ready */
00229     while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET)
00230     {}
00231 
00232     /* PLL configuration: PLLCLK = (PLL2 / 5) * 9 = 72 MHz */ 
00233     RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);
00234     RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);
00235 
00236     /* PLL3 configuration: PLL3CLK = (HSE / 5) * 11 => PLL3_VCO = 110 MHz */
00237     RCC_PLL3Config(RCC_PLL3Mul_11);
00238     /* Enable PLL3 */
00239     RCC_PLL3Cmd(ENABLE);    
00240     /* Wait till PLL3 is ready */
00241     while (RCC_GetFlagStatus(RCC_FLAG_PLL3RDY) == RESET)
00242     {}
00243 
00244     /* Configure I2S clock source: On Connectivity-Line Devices, the I2S can be 
00245         clocked by PLL3 VCO instead of SYS_CLK in order to guarantee higher 
00246         precision */
00247     RCC_I2S3CLKConfig(RCC_I2S3CLKSource_PLL3_VCO);
00248     RCC_I2S2CLKConfig(RCC_I2S2CLKSource_PLL3_VCO);  
00249 #endif
00250 
00251     /* Enable PLL */ 
00252     RCC_PLLCmd(ENABLE);
00253 
00254     /* Wait till PLL is ready */
00255     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
00256     {
00257     }
00258 
00259     /* Select PLL as system clock source */
00260     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
00261 
00262     /* Wait till PLL is used as system clock source */
00263     while(RCC_GetSYSCLKSource() != 0x08)
00264     {
00265     }
00266   }  
00267   
00268   /* Enable peripheral clocks ------------------------------------------------*/
00269   /* GPIOA, GPIOB and AFIO clocks enable */
00270   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
00271                          RCC_APB2Periph_AFIO, ENABLE);
00272 
00273 #ifdef USE_STM3210C_EVAL 
00274   /* GPIOC Clock enable (for the SPI3 remapped pins)  */
00275   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);  
00276 #endif /* USE_STM3210C_EVAL */
00277   
00278   /* SPI3 and SPI2 clocks enable */
00279   RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3 | RCC_APB1Periph_SPI2, ENABLE);
00280 }
00281 
00282 /**
00283   * @brief  Configures the different GPIO ports.
00284   * @param  None
00285   * @retval None
00286   */
00287 void GPIO_Configuration(void)
00288 {
00289   GPIO_InitTypeDef GPIO_InitStructure;
00290 
00291 #ifdef USE_STM3210E_EVAL
00292   /* Disable the JTAG interface and enable the SWJ interface
00293       This operation is not necessary for Connectivity-Line devices since
00294       SPI3 I/Os can be remapped on other GPIO pins */
00295   GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
00296 #endif /* USE_STM3210E_EVAL */ 
00297 
00298   /* Configure SPI2 pins: CK, WS and SD ---------------------------------*/
00299   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15;
00300   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00301   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00302   GPIO_Init(GPIOB, &GPIO_InitStructure);
00303 
00304 #ifdef USE_STM3210C_EVAL
00305   
00306   /* Remap SPI3 on PC10-PC11-PC12-PA4 GPIO pins ------------------------*/
00307   GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);
00308   
00309   /* Configure SPI3 pins: CK and SD ------------------------------------*/
00310   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_12;
00311   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00312   GPIO_Init(GPIOC, &GPIO_InitStructure);  
00313 
00314   /* Configure SPI3 pins: WS -------------------------------------------*/
00315   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
00316   GPIO_Init(GPIOA, &GPIO_InitStructure);
00317   
00318 #elif defined (USE_STM3210E_EVAL)
00319   
00320   /* Configure SPI3 pins: CK and SD ------------------------------------*/
00321   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_5;
00322   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00323   GPIO_Init(GPIOB, &GPIO_InitStructure);
00324 
00325   /* Configure SPI3 pins: WS -------------------------------------------*/
00326   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
00327   GPIO_Init(GPIOA, &GPIO_InitStructure);
00328   
00329 #endif /* USE_STM3210C_EVAL */  
00330 }
00331 
00332 /**
00333   * @brief  Configure the nested vectored interrupt controller.
00334   * @param  None
00335   * @retval None
00336   */
00337 void NVIC_Configuration(void)
00338 {
00339   NVIC_InitTypeDef NVIC_InitStructure;
00340 
00341   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
00342 
00343   /* SPI3 IRQ Channel configuration */
00344   NVIC_InitStructure.NVIC_IRQChannel = SPI3_IRQn;
00345   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
00346   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00347   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00348   NVIC_Init(&NVIC_InitStructure);
00349 
00350   /* SPI2 IRQ channel configuration */
00351   NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
00352   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00353   NVIC_Init(&NVIC_InitStructure);
00354 }
00355 
00356 /**
00357   * @brief  Compares two buffers.
00358   * @param  pBuffer1, pBuffer2: buffers to be compared.
00359   *   : - BufferLength: buffer's length
00360   * @retval PASSED: pBuffer1 identical to pBuffer2
00361   *   FAILED: pBuffer1 differs from pBuffer2
00362   */
00363 TestStatus Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength)
00364 {
00365   while (BufferLength--)
00366   {
00367     if (*pBuffer1 != *pBuffer2)
00368     {
00369       return FAILED;
00370     }
00371 
00372     pBuffer1++;
00373     pBuffer2++;
00374   }
00375 
00376   return PASSED;
00377 }
00378 
00379 /**
00380   * @brief  Compares two buffers in 24 bits data format.
00381   * @param  pBuffer1, pBuffer2: buffers to be compared.
00382   * @param  BufferLength: buffer's length
00383   * @retval PASSED: pBuffer1 identical to pBuffer2
00384   *   FAILED: pBuffer1 differs from pBuffer2
00385   */
00386 TestStatus Buffercmp24bits(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength)
00387 {
00388   while (BufferLength--)
00389   {
00390     if (*pBuffer1 != *pBuffer2)
00391     {
00392       if (*pBuffer1 != (*pBuffer2 & 0xFF00))
00393       {
00394         return FAILED;
00395       }
00396     }
00397 
00398     pBuffer1++;
00399     pBuffer2++;
00400   }
00401 
00402   return PASSED;
00403 }
00404 
00405 #ifdef  USE_FULL_ASSERT
00406 /**
00407   * @brief  Reports the name of the source file and the source line number
00408   *         where the assert_param error has occurred.
00409   * @param  file: pointer to the source file name
00410   * @param  line: assert_param error line source number
00411   * @retval None
00412   */
00413 void assert_failed(uint8_t* file, uint32_t line)
00414 {
00415   /* User can add his own implementation to report the file name and line number,
00416      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00417 
00418   /* Infinite loop */
00419   while (1)
00420   {}
00421 }
00422 #endif
00423 
00424 /**
00425   * @}
00426   */ 
00427 
00428 /**
00429   * @}
00430   */
00431 
00432 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32