STM32F10x_StdPeriph_Examples/SPI/FullDuplex_SoftNSS/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    SPI/FullDuplex_SoftNSS/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 #include "platform_config.h"
00024 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup SPI_FullDuplex_SoftNSS
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00035 
00036 /* Private define ------------------------------------------------------------*/
00037 #define BufferSize 32
00038 
00039 /* Private macro -------------------------------------------------------------*/
00040 /* Private variables ---------------------------------------------------------*/
00041 SPI_InitTypeDef   SPI_InitStructure;
00042 uint8_t SPIy_Buffer_Tx[BufferSize] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
00043                                       0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
00044                                       0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
00045                                       0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
00046                                       0x1D, 0x1E, 0x1F, 0x20};
00047 uint8_t SPIz_Buffer_Tx[BufferSize] = {0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
00048                                       0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E,
00049                                       0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 
00050                                       0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C,
00051                                       0x6D, 0x6E, 0x6F, 0x70};
00052 uint8_t SPIy_Buffer_Rx[BufferSize], SPIz_Buffer_Rx[BufferSize];
00053 __IO uint8_t TxIdx = 0, RxIdx = 0, k = 0;
00054 volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
00055 volatile TestStatus TransferStatus3 = FAILED, TransferStatus4 = FAILED;
00056 
00057 /* Private functions ---------------------------------------------------------*/
00058 void RCC_Configuration(void);
00059 void GPIO_Configuration(uint16_t SPIy_Mode, uint16_t SPIz_Mode);
00060 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00061 
00062 /**
00063   * @brief   Main program
00064   * @param  None
00065   * @retval None
00066   */
00067 int main(void)
00068 {
00069   /*!< At this stage the microcontroller clock setting is already configured, 
00070        this is done through SystemInit() function which is called from startup
00071        file (startup_stm32f10x_xx.s) before to branch to application main.
00072        To reconfigure the default setting of SystemInit() function, refer to
00073        system_stm32f10x.c file
00074      */     
00075        
00076   /* System clocks configuration ---------------------------------------------*/
00077   RCC_Configuration();
00078 
00079   /* 1st phase: SPIy Master and SPIz Slave */
00080   /* GPIO configuration ------------------------------------------------------*/
00081   GPIO_Configuration(SPI_Mode_Master, SPI_Mode_Slave);
00082   
00083   /* SPIy Config -------------------------------------------------------------*/
00084   SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
00085   SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
00086   SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
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_4;
00091   SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
00092   SPI_InitStructure.SPI_CRCPolynomial = 7;
00093   SPI_Init(SPIy, &SPI_InitStructure);
00094 
00095   /* SPIz Config -------------------------------------------------------------*/
00096   SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
00097   SPI_Init(SPIz, &SPI_InitStructure);
00098 
00099   /* Enable SPIy */
00100   SPI_Cmd(SPIy, ENABLE);
00101   /* Enable SPIz */
00102   SPI_Cmd(SPIz, ENABLE);
00103 
00104   /* Transfer procedure */
00105   while (TxIdx < BufferSize)
00106   {
00107     /* Wait for SPIy Tx buffer empty */
00108     while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE) == RESET);
00109     /* Send SPIz data */
00110     SPI_I2S_SendData(SPIz, SPIz_Buffer_Tx[TxIdx]);
00111     /* Send SPIy data */
00112     SPI_I2S_SendData(SPIy, SPIy_Buffer_Tx[TxIdx++]);
00113     /* Wait for SPIz data reception */
00114     while (SPI_I2S_GetFlagStatus(SPIz, SPI_I2S_FLAG_RXNE) == RESET);
00115     /* Read SPIz received data */
00116     SPIz_Buffer_Rx[RxIdx] = SPI_I2S_ReceiveData(SPIz);
00117     /* Wait for SPIy data reception */
00118     while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_RXNE) == RESET);
00119     /* Read SPIy received data */
00120     SPIy_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPIy);
00121   }
00122 
00123   /* Check the corectness of written dada */
00124   TransferStatus1 = Buffercmp(SPIz_Buffer_Rx, SPIy_Buffer_Tx, BufferSize);
00125   TransferStatus2 = Buffercmp(SPIy_Buffer_Rx, SPIz_Buffer_Tx, BufferSize);
00126   /* TransferStatus1, TransferStatus2 = PASSED, if the transmitted and received data
00127      are equal */
00128   /* TransferStatus1, TransferStatus2 = FAILED, if the transmitted and received data
00129      are different */
00130 
00131   /* 2nd phase: SPIy Slave and SPIz Master */
00132   /* GPIO configuration ------------------------------------------------------*/
00133   GPIO_Configuration(SPI_Mode_Slave , SPI_Mode_Master);
00134   
00135   /* SPIy Re-configuration ---------------------------------------------------*/
00136   SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
00137   SPI_Init(SPIy, &SPI_InitStructure);
00138 
00139   /* SPIz Re-configuration ---------------------------------------------------*/
00140   SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
00141   SPI_Init(SPIz, &SPI_InitStructure);
00142 
00143   /* Reset TxIdx, RxIdx indexes and receive tables values */
00144   TxIdx = 0;
00145   RxIdx = 0;
00146   for (k = 0; k < BufferSize; k++)  SPIz_Buffer_Rx[k] = 0;
00147   for (k = 0; k < BufferSize; k++)  SPIy_Buffer_Rx[k] = 0;
00148 
00149   /* Transfer procedure */
00150   while (TxIdx < BufferSize)
00151   {
00152     /* Wait for SPIz Tx buffer empty */
00153     while (SPI_I2S_GetFlagStatus(SPIz, SPI_I2S_FLAG_TXE) == RESET);
00154     /* Send SPIy data */
00155     SPI_I2S_SendData(SPIy, SPIy_Buffer_Tx[TxIdx]);
00156     /* Send SPIz data */
00157     SPI_I2S_SendData(SPIz, SPIz_Buffer_Tx[TxIdx++]);
00158     /* Wait for SPIy data reception */
00159     while (SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_RXNE) == RESET);
00160     /* Read SPIy received data */
00161     SPIy_Buffer_Rx[RxIdx] = SPI_I2S_ReceiveData(SPIy);
00162     /* Wait for SPIz data reception */
00163     while (SPI_I2S_GetFlagStatus(SPIz, SPI_I2S_FLAG_RXNE) == RESET);
00164     /* Read SPIz received data */
00165     SPIz_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPIz);
00166   }
00167 
00168   /* Check the corectness of written dada */
00169   TransferStatus3 = Buffercmp(SPIz_Buffer_Rx, SPIy_Buffer_Tx, BufferSize);
00170   TransferStatus4 = Buffercmp(SPIy_Buffer_Rx, SPIz_Buffer_Tx, BufferSize);
00171   /* TransferStatus3, TransferStatus4 = PASSED, if the transmitted and received data
00172      are equal */
00173   /* TransferStatus3, TransferStatus4 = FAILED, if the transmitted and received data
00174      are different */
00175 
00176   while (1)
00177   {}
00178 }
00179 
00180 /**
00181   * @brief  Configures the different system clocks.
00182   * @param  None
00183   * @retval None
00184   */
00185 void RCC_Configuration(void)
00186 {
00187   /* PCLK2 = HCLK/2 */
00188   RCC_PCLK2Config(RCC_HCLK_Div2); 
00189 
00190 /* Enable peripheral clocks --------------------------------------------------*/
00191 #ifdef USE_STM3210C_EVAL
00192   /* Enable GPIO clock for SPIy and SPIz */
00193   RCC_APB2PeriphClockCmd(SPIy_GPIO_CLK | SPIz_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
00194 
00195   /* Enable SPIy Periph clock */
00196   RCC_APB1PeriphClockCmd(SPIy_CLK, ENABLE);
00197                            
00198 #else
00199   /* Enable SPIy clock and GPIO clock for SPIy and SPIz */
00200   RCC_APB2PeriphClockCmd(SPIy_GPIO_CLK | SPIz_GPIO_CLK | SPIy_CLK, ENABLE);
00201 #endif
00202   /* Enable SPIz Periph clock */
00203   RCC_APB1PeriphClockCmd(SPIz_CLK, ENABLE);
00204 }
00205 
00206 /**
00207   * @brief  Configures the different SPIy and SPIz GPIO ports.
00208   * @param  SPIy_Mode: Specifies the SPIy operating mode. 
00209   *            This parameter can be:
00210   *              -  SPIy_Mode_Master
00211   *              -  SPIy_Mode_Slave                 
00212   * @param  SPIz_Mode: Specifies the SPIz operating mode. 
00213   *            This parameter can be:
00214   *              -  SPIz_Mode_Master
00215   *              -  SPIz_Mode_Slave 
00216   * @retval None
00217   */
00218 void GPIO_Configuration(uint16_t SPIy_Mode, uint16_t SPIz_Mode)
00219 {
00220   GPIO_InitTypeDef GPIO_InitStructure;
00221 
00222 #ifdef USE_STM3210C_EVAL
00223   /* Enable SPI3 Pins Software Remapping */
00224   GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);
00225 #endif
00226 
00227   /* Configure SPIy pins: SCK, MISO and MOSI ---------------------------------*/
00228   GPIO_InitStructure.GPIO_Pin = SPIy_PIN_SCK | SPIy_PIN_MOSI;
00229   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00230 
00231   if(SPIy_Mode == SPI_Mode_Master)
00232   {
00233     /* Confugure SCK and MOSI pins as Alternate Function Push Pull */
00234     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00235   }
00236   else
00237   {
00238     /* Confugure SCK and MOSI pins as Input Floating */
00239     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00240   }
00241   GPIO_Init(SPIy_GPIO, &GPIO_InitStructure);
00242 
00243   GPIO_InitStructure.GPIO_Pin = SPIy_PIN_MISO;
00244 
00245   if(SPIy_Mode == SPI_Mode_Master)
00246   {
00247     /* Confugure MISO pin as Input Floating  */
00248     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00249   }
00250   else
00251   {
00252     /* Confugure MISO pin as Alternate Function Push Pull */
00253     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00254   }
00255   GPIO_Init(SPIy_GPIO, &GPIO_InitStructure);
00256   
00257   /* Configure SPIz pins: SCK, MISO and MOSI ---------------------------------*/
00258   GPIO_InitStructure.GPIO_Pin = SPIz_PIN_SCK | SPIz_PIN_MOSI;
00259 
00260   if(SPIz_Mode == SPI_Mode_Slave)
00261   {
00262     /* Confugure SCK and MOSI pins as Input Floating */
00263     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00264   }
00265   else
00266   { 
00267     /* Confugure SCK and MOSI pins as Alternate Function Push Pull */
00268     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00269   }
00270   GPIO_Init(SPIz_GPIO, &GPIO_InitStructure);
00271 
00272   GPIO_InitStructure.GPIO_Pin = SPIz_PIN_MISO;
00273   if(SPIz_Mode == SPI_Mode_Slave)
00274   {
00275     /* Confugure MISO pin as Alternate Function Push Pull */
00276     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00277   }
00278   else
00279   { /* Confugure MISO pin as Input Floating  */
00280     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00281   }
00282   GPIO_Init(SPIz_GPIO, &GPIO_InitStructure);
00283 }
00284 /**
00285   * @brief  Compares two buffers.
00286   * @param  pBuffer1, pBuffer2: buffers to be compared.
00287   * @param  BufferLength: buffer's length
00288   * @retval PASSED: pBuffer1 identical to pBuffer2
00289   *         FAILED: pBuffer1 differs from pBuffer2
00290   */
00291 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00292 {
00293   while (BufferLength--)
00294   {
00295     if (*pBuffer1 != *pBuffer2)
00296     {
00297       return FAILED;
00298     }
00299 
00300     pBuffer1++;
00301     pBuffer2++;
00302   }
00303 
00304   return PASSED;
00305 }
00306 
00307 #ifdef  USE_FULL_ASSERT
00308 
00309 /**
00310   * @brief  Reports the name of the source file and the source line number
00311   *         where the assert_param error has occurred.
00312   * @param  file: pointer to the source file name
00313   * @param  line: assert_param error line source number
00314   * @retval None
00315   */
00316 void assert_failed(uint8_t* file, uint32_t line)
00317 {
00318   /* User can add his own implementation to report the file name and line number,
00319      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00320 
00321   /* Infinite loop */
00322   while (1)
00323   {}
00324 }
00325 #endif
00326 /**
00327   * @}
00328   */ 
00329 
00330 /**
00331   * @}
00332   */ 
00333 
00334 /******************* (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