STM32F10x_StdPeriph_Examples/SPI/CRC/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    SPI/CRC/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 SPI_CRC
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00034 
00035 /* Private define ------------------------------------------------------------*/
00036 #define BufferSize  32
00037 
00038 /* Private macro -------------------------------------------------------------*/
00039 /* Private variables ---------------------------------------------------------*/
00040 SPI_InitTypeDef  SPI_InitStructure;
00041 uint16_t SPI1_Buffer_Tx[BufferSize] = {0x0102, 0x0304, 0x0506, 0x0708, 0x090A, 0x0B0C,
00042                                   0x0D0E, 0x0F10, 0x1112, 0x1314, 0x1516, 0x1718,
00043                                   0x191A, 0x1B1C, 0x1D1E, 0x1F20, 0x2122, 0x2324,
00044                                   0x2526, 0x2728, 0x292A, 0x2B2C, 0x2D2E, 0x2F30,
00045                                   0x3132, 0x3334, 0x3536, 0x3738, 0x393A, 0x3B3C,
00046                                   0x3D3E, 0x3F40};
00047 uint16_t SPI2_Buffer_Tx[BufferSize] = {0x5152, 0x5354, 0x5556, 0x5758, 0x595A, 0x5B5C,
00048                                   0x5D5E, 0x5F60, 0x6162, 0x6364, 0x6566, 0x6768,
00049                                   0x696A, 0x6B6C, 0x6D6E, 0x6F70, 0x7172, 0x7374,
00050                                   0x7576, 0x7778, 0x797A, 0x7B7C, 0x7D7E, 0x7F80,
00051                                   0x8182, 0x8384, 0x8586, 0x8788, 0x898A, 0x8B8C,
00052                                   0x8D8E, 0x8F90};
00053 uint16_t SPI1_Buffer_Rx[BufferSize], SPI2_Buffer_Rx[BufferSize];
00054 uint32_t TxIdx = 0, RxIdx = 0;
00055 __IO uint16_t CRC1Value = 0, CRC2Value = 0;
00056 volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
00057 
00058 /* Private functions ---------------------------------------------------------*/
00059 void RCC_Configuration(void);
00060 void GPIO_Configuration(void);
00061 TestStatus Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength);
00062 
00063 /**
00064   * @brief  Main program
00065   * @param  None
00066   * @retval None
00067   */
00068 int main(void)
00069 {
00070   /*!< At this stage the microcontroller clock setting is already configured, 
00071        this is done through SystemInit() function which is called from startup
00072        file (startup_stm32f10x_xx.s) before to branch to application main.
00073        To reconfigure the default setting of SystemInit() function, refer to
00074        system_stm32f10x.c file
00075      */     
00076        
00077   /* System clocks configuration ---------------------------------------------*/
00078   RCC_Configuration();
00079 
00080   /* GPIO configuration ------------------------------------------------------*/
00081   GPIO_Configuration();
00082 
00083   /* SPI1 configuration ------------------------------------------------------*/
00084   SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
00085   SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
00086   SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
00087   SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
00088   SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
00089   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
00090   SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
00091   SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
00092   SPI_InitStructure.SPI_CRCPolynomial = 7;
00093   SPI_Init(SPI1, &SPI_InitStructure);
00094 
00095   /* SPI2 configuration ------------------------------------------------------*/
00096   SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
00097   SPI_Init(SPI2, &SPI_InitStructure);
00098 
00099   /* Enable SPI1 CRC calculation */
00100   SPI_CalculateCRC(SPI1, ENABLE);
00101   /* Enable SPI2 CRC calculation */
00102   SPI_CalculateCRC(SPI2, ENABLE);
00103 
00104   /* Enable SPI1 */
00105   SPI_Cmd(SPI1, ENABLE);
00106   /* Enable SPI2 */
00107   SPI_Cmd(SPI2, ENABLE);
00108 
00109   /* Transfer procedure */
00110   while (TxIdx < BufferSize - 1)
00111   {
00112     /* Wait for SPI1 Tx buffer empty */
00113     while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
00114     /* Send SPI2 data */
00115     SPI_I2S_SendData(SPI2, SPI2_Buffer_Tx[TxIdx]);
00116     /* Send SPI1 data */
00117     SPI_I2S_SendData(SPI1, SPI1_Buffer_Tx[TxIdx++]);
00118     /* Wait for SPI2 data reception */
00119     while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
00120     /* Read SPI2 received data */
00121     SPI2_Buffer_Rx[RxIdx] = SPI_I2S_ReceiveData(SPI2);
00122     /* Wait for SPI1 data reception */
00123     while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
00124     /* Read SPI1 received data */
00125     SPI1_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI1);
00126   }
00127 
00128   /* Wait for SPI1 Tx buffer empty */
00129   while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
00130   /* Wait for SPI2 Tx buffer empty */
00131   while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
00132 
00133   /* Send last SPI2_Buffer_Tx data */
00134   SPI_I2S_SendData(SPI2, SPI2_Buffer_Tx[TxIdx]);
00135   /* Enable SPI2 CRC transmission */
00136   SPI_TransmitCRC(SPI2);
00137   /* Send last SPI1_Buffer_Tx data */
00138   SPI_I2S_SendData(SPI1, SPI1_Buffer_Tx[TxIdx]);
00139   /* Enable SPI1 CRC transmission */
00140   SPI_TransmitCRC(SPI1);
00141 
00142   /* Wait for SPI1 last data reception */
00143   while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
00144   /* Read SPI1 last received data */
00145   SPI1_Buffer_Rx[RxIdx] = SPI_I2S_ReceiveData(SPI1);
00146 
00147   /* Wait for SPI2 last data reception */
00148   while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
00149   /* Read SPI2 last received data */
00150   SPI2_Buffer_Rx[RxIdx] = SPI_I2S_ReceiveData(SPI2);
00151 
00152   /* Wait for SPI1 data reception: CRC transmitted by SPI2 */
00153   while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
00154   /* Wait for SPI2 data reception: CRC transmitted by SPI1 */
00155   while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
00156 
00157   /* Check the received data with the send ones */
00158   TransferStatus1 = Buffercmp(SPI2_Buffer_Rx, SPI1_Buffer_Tx, BufferSize);
00159   TransferStatus2 = Buffercmp(SPI1_Buffer_Rx, SPI2_Buffer_Tx, BufferSize);
00160   /* TransferStatus1, TransferStatus2 = PASSED, if the data transmitted and received
00161      are correct */
00162   /* TransferStatus1, TransferStatus2 = FAILED, if the data transmitted and received
00163      are different */
00164 
00165   /* Test on the SPI1 CRC Error flag */
00166   if ((SPI_I2S_GetFlagStatus(SPI1, SPI_FLAG_CRCERR)) == SET)
00167   {
00168     TransferStatus2 = FAILED;
00169   }
00170 
00171   /* Test on the SPI2 CRC Error flag */
00172   if ((SPI_I2S_GetFlagStatus(SPI2, SPI_FLAG_CRCERR)) == SET)
00173   {
00174     TransferStatus1 = FAILED;
00175   }
00176 
00177   /* Read SPI1 received CRC value */
00178   CRC1Value = SPI_I2S_ReceiveData(SPI1);
00179   /* Read SPI2 received CRC value */
00180   CRC2Value = SPI_I2S_ReceiveData(SPI2);
00181 
00182   while (1)
00183   {}
00184 }
00185 
00186 /**
00187   * @brief  Configures the different system clocks.
00188   * @param  None
00189   * @retval None
00190   */
00191 void RCC_Configuration(void)
00192 {
00193   /* PCLK2 = HCLK/2 */
00194   RCC_PCLK2Config(RCC_HCLK_Div2);
00195 
00196   /* Enable peripheral clocks --------------------------------------------------*/
00197   /* GPIOA, GPIOB and SPI1 clock enable */
00198   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
00199                          RCC_APB2Periph_SPI1, ENABLE);
00200 
00201   /* SPI2 Periph clock enable */
00202   RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
00203 }
00204 
00205 /**
00206   * @brief  Configures the different GPIO ports.
00207   * @param  None
00208   * @retval None
00209   */
00210 void GPIO_Configuration(void)
00211 {
00212   GPIO_InitTypeDef GPIO_InitStructure;
00213 
00214   /* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/
00215   /* Confugure SCK and MOSI pins as Alternate Function Push Pull */
00216   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
00217   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00218   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00219   GPIO_Init(GPIOA, &GPIO_InitStructure);
00220   /* Confugure MISO pin as Input Floating  */
00221   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
00222   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00223   GPIO_Init(GPIOA, &GPIO_InitStructure);
00224   
00225   /* Configure SPI2 pins: SCK, MISO and MOSI ---------------------------------*/
00226   /* Confugure SCK and MOSI pins as Input Floating */
00227   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
00228   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00229   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00230   GPIO_Init(GPIOB, &GPIO_InitStructure);
00231   /* Confugure MISO pin as Alternate Function Push Pull */
00232   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
00233   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00234   GPIO_Init(GPIOB, &GPIO_InitStructure);
00235  
00236 }
00237 
00238 /**
00239   * @brief  Compares two buffers.
00240   * @param  pBuffer1, pBuffer2: buffers to be compared.
00241   * @param  BufferLength: buffer's length
00242   * @retval PASSED: pBuffer1 identical to pBuffer2
00243   *   FAILED: pBuffer1 differs from pBuffer2
00244   */
00245 TestStatus Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength)
00246 {
00247   while (BufferLength--)
00248   {
00249     if (*pBuffer1 != *pBuffer2)
00250     {
00251       return FAILED;
00252     }
00253 
00254     pBuffer1++;
00255     pBuffer2++;
00256   }
00257 
00258   return PASSED;
00259 }
00260 
00261 #ifdef  USE_FULL_ASSERT
00262 
00263 /**
00264   * @brief  Reports the name of the source file and the source line number
00265   *         where the assert_param error has occurred.
00266   * @param  file: pointer to the source file name
00267   * @param  line: assert_param error line source number
00268   * @retval None
00269   */
00270 void assert_failed(uint8_t* file, uint32_t line)
00271 {
00272   /* User can add his own implementation to report the file name and line number,
00273      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00274 
00275   /* Infinite loop */
00276   while (1)
00277   {}
00278 }
00279 #endif
00280 
00281 /**
00282   * @}
00283   */ 
00284 
00285 /**
00286   * @}
00287   */ 
00288 
00289 /******************* (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