STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/I2S/I2S_DataExchangeInterrupt/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file I2S/I2S_DataExchangeInterrupt/main.c 00004 * @author MCD Application Team 00005 * @version V1.4.0 00006 * @date 24-July-2014 00007 * @brief Main program body 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 /* Includes ------------------------------------------------------------------*/ 00029 #include "main.h" 00030 00031 /** @addtogroup STM32F0xx_StdPeriph_Examples 00032 * @{ 00033 */ 00034 00035 /** @addtogroup I2S_DataExchangeInterrupt 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; 00041 /* Private define ------------------------------------------------------------*/ 00042 /* Private macro -------------------------------------------------------------*/ 00043 /* Private variables ---------------------------------------------------------*/ 00044 const uint16_t I2S_Buffer_Tx[32] = {0x0102, 0x0304, 0x0506, 0x0708, 0x090A, 0x0B0C, 00045 0x0D0E, 0x0F10, 0x1112, 0x1314, 0x1516, 0x1718, 00046 0x191A, 0x1B1C, 0x1D1E, 0x1F20, 0x2122, 0x2324, 00047 0x2526, 0x2728, 0x292A, 0x2B2C, 0x2D2E, 0x2F30, 00048 0x3132, 0x3334, 0x3536, 0x3738, 0x393A, 0x3B3C, 00049 0x3D3E, 0x3F40}; 00050 __IO uint16_t TxIdx = 0, RxIdx = 0; 00051 uint16_t I2S_Buffer_Rx[32]={0}; 00052 TestStatus TransferStatus = FAILED; 00053 /* Private function prototypes -----------------------------------------------*/ 00054 #if defined (I2S_SLAVE_RECEIVER) 00055 static TestStatus Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength); 00056 #endif 00057 static void I2S_Config(void); 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_stm32f0xx.s) before to branch to application main. 00070 To reconfigure the default setting of SystemInit() function, refer to 00071 system_stm32f0xx.c file 00072 */ 00073 /* I2S peripheral Configuration */ 00074 I2S_Config(); 00075 00076 /* Enable the Tamper button */ 00077 STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_GPIO); 00078 00079 /* Initialize the LEDs */ 00080 STM_EVAL_LEDInit(LED3); 00081 STM_EVAL_LEDInit(LED1); 00082 00083 #if defined (I2S_MASTER_TRANSMITTER) 00084 while (STM_EVAL_PBGetState(BUTTON_TAMPER) != RESET) 00085 {} 00086 00087 /* Enable the I2S1 TX Interrupt */ 00088 SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE); 00089 00090 /* Enable the I2S */ 00091 I2S_Cmd(SPI1, ENABLE); 00092 00093 while(TxIdx < 32); 00094 00095 #elif defined (I2S_SLAVE_RECEIVER) 00096 /* Enable the I2S1 RXNE Interrupt */ 00097 SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE); 00098 /* Enable the I2S */ 00099 I2S_Cmd(SPI1, ENABLE); 00100 /* Wait the end of communication */ 00101 while (RxIdx < 32); 00102 00103 /* Check if the data transmitted from Master Board and received by 00104 Slave Board are the same */ 00105 TransferStatus = Buffercmp(I2S_Buffer_Rx, (uint16_t*)I2S_Buffer_Tx, 32); 00106 00107 if (TransferStatus == PASSED) /* successful transfer */ 00108 { 00109 /* Green Led On */ 00110 STM_EVAL_LEDOn(LED1); 00111 STM_EVAL_LEDOff(LED3); 00112 } 00113 else /* unsuccessful transfer */ 00114 { 00115 /* Red Led On */ 00116 STM_EVAL_LEDOn(LED3); 00117 STM_EVAL_LEDOff(LED1); 00118 } 00119 #endif 00120 00121 /* Infinite loop */ 00122 while (1) 00123 {} 00124 } 00125 00126 /** 00127 * @brief Configures I2S peripheral. 00128 * @param None 00129 * @retval None 00130 */ 00131 static void I2S_Config(void) 00132 { 00133 I2S_InitTypeDef I2S_InitStructure; 00134 GPIO_InitTypeDef GPIO_InitStructure; 00135 NVIC_InitTypeDef NVIC_InitStructure; 00136 00137 /* Enable SPI1 APB clocks */ 00138 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); 00139 00140 /* Enable GPIOA, GPIOB clocks */ 00141 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB, ENABLE); 00142 00143 /* I2S1 Pins configuration */ 00144 #ifdef USE_STM320518_EVAL 00145 /* Configure pins as AF */ 00146 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 ; 00147 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 00148 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 00149 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 00150 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 00151 GPIO_Init(GPIOA, &GPIO_InitStructure); 00152 00153 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 |GPIO_Pin_5; 00154 GPIO_Init(GPIOB, &GPIO_InitStructure); 00155 00156 /* Connect pin to Periph */ 00157 GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_0); 00158 GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_0); 00159 GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_0); 00160 #else 00161 /* I2S1 Pins configuration */ 00162 /* Configure pins as AF */ 00163 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7 ; 00164 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 00165 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 00166 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 00167 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 00168 GPIO_Init(GPIOA, &GPIO_InitStructure); 00169 00170 /* Connect pin to Periph */ 00171 GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_0); 00172 GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0); 00173 GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0); 00174 #endif /* USE_STM320518_EVAL */ 00175 00176 /* I2S peripheral configuration */ 00177 I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips; 00178 I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16bextended; 00179 I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable; 00180 I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_48k; 00181 I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low; 00182 00183 #if defined (I2S_MASTER_TRANSMITTER) 00184 /* I2S Master Transmitter configuration */ 00185 I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx; 00186 #elif defined (I2S_SLAVE_RECEIVER) 00187 /* I2S Slave Receiver configuration */ 00188 I2S_InitStructure.I2S_Mode = I2S_Mode_SlaveRx; 00189 #endif 00190 I2S_Init(SPI1, &I2S_InitStructure); 00191 00192 /* SPI1 IRQ Channel configuration */ 00193 NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn; 00194 NVIC_InitStructure.NVIC_IRQChannelPriority = 0; 00195 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 00196 NVIC_Init(&NVIC_InitStructure); 00197 } 00198 00199 #if defined (I2S_SLAVE_RECEIVER) 00200 /** 00201 * @brief Compares two buffers. 00202 * @param pBuffer1, pBuffer2: buffers to be compared. 00203 * @param BufferLength: buffer's length 00204 * @retval PASSED: pBuffer1 identical to pBuffer2 00205 * FAILED: pBuffer1 differs from pBuffer2 00206 */ 00207 static TestStatus Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength) 00208 { 00209 while (BufferLength--) 00210 { 00211 if (*pBuffer1 != *pBuffer2) 00212 { 00213 return FAILED; 00214 } 00215 00216 pBuffer1++; 00217 pBuffer2++; 00218 } 00219 00220 return PASSED; 00221 } 00222 #endif 00223 00224 #ifdef USE_FULL_ASSERT 00225 00226 /** 00227 * @brief Reports the name of the source file and the source line number 00228 * where the assert_param error has occurred. 00229 * @param file: pointer to the source file name 00230 * @param line: assert_param error line source number 00231 * @retval None 00232 */ 00233 void assert_failed(uint8_t* file, uint32_t line) 00234 { 00235 /* User can add his own implementation to report the file name and line number, 00236 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00237 00238 /* Infinite loop */ 00239 while (1) 00240 { 00241 } 00242 } 00243 #endif 00244 00245 /** 00246 * @} 00247 */ 00248 00249 /** 00250 * @} 00251 */ 00252 00253 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/