STM32F10x_StdPeriph_Examples/SPI/DMA/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    SPI/DMA/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 
00026 /** @addtogroup STM32F10x_StdPeriph_Examples
00027   * @{
00028   */
00029 
00030 /** @addtogroup SPI_DMA
00031   * @{
00032   */ 
00033 
00034 /* Private typedef -----------------------------------------------------------*/
00035 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00036 
00037 /* Private define ------------------------------------------------------------*/
00038 #define BufferSize       32
00039 
00040 /* Private macro -------------------------------------------------------------*/
00041 /* Private variables ---------------------------------------------------------*/
00042 SPI_InitTypeDef  SPI_InitStructure;
00043 DMA_InitTypeDef  DMA_InitStructure;
00044 GPIO_InitTypeDef GPIO_InitStructure;
00045 
00046 uint8_t SPI_MASTER_Buffer_Tx[BufferSize] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
00047                                             0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
00048                                             0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
00049                                             0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
00050                                             0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,
00051                                             0x1F, 0x20};
00052 uint8_t SPI_SLAVE_Buffer_Rx[BufferSize];
00053 __IO uint8_t TxIdx = 0;
00054 volatile TestStatus TransferStatus = FAILED;
00055 
00056 
00057 /* Private function prototypes -----------------------------------------------*/
00058 void RCC_Configuration(void);
00059 void GPIO_Configuration(void);
00060 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00061 
00062 /* Private functions ---------------------------------------------------------*/
00063 
00064 /**
00065   * @brief  Main program
00066   * @param  None
00067   * @retval None
00068   */
00069 int main(void)
00070 {
00071   /*!< At this stage the microcontroller clock setting is already configured, 
00072        this is done through SystemInit() function which is called from startup
00073        file (startup_stm32f10x_xx.s) before to branch to application main.
00074        To reconfigure the default setting of SystemInit() function, refer to
00075        system_stm32f10x.c file
00076      */     
00077        
00078   /* System clocks configuration ---------------------------------------------*/
00079   RCC_Configuration();
00080 
00081   /* GPIO configuration ------------------------------------------------------*/
00082   GPIO_Configuration();
00083 
00084   /* SPI_SLAVE_Rx_DMA_Channel configuration ---------------------------------------------*/
00085   DMA_DeInit(SPI_SLAVE_Rx_DMA_Channel);
00086   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPI_SLAVE_DR_Base;
00087   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI_SLAVE_Buffer_Rx;
00088   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
00089   DMA_InitStructure.DMA_BufferSize = BufferSize;
00090   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00091   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
00092   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
00093   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
00094   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
00095   DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
00096   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00097   DMA_Init(SPI_SLAVE_Rx_DMA_Channel, &DMA_InitStructure);
00098 
00099   /* SPI_MASTER configuration ------------------------------------------------------*/
00100   SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
00101   SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
00102   SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
00103   SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
00104   SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
00105   SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
00106   SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
00107   SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
00108   SPI_InitStructure.SPI_CRCPolynomial = 7;
00109   SPI_Init(SPI_MASTER, &SPI_InitStructure);
00110 
00111   /* SPI_SLAVE configuration ------------------------------------------------------*/
00112   SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx;
00113   SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
00114   SPI_Init(SPI_SLAVE, &SPI_InitStructure);
00115 
00116   /* Enable SPI_MASTER NSS output for master mode */
00117   SPI_SSOutputCmd(SPI_MASTER, ENABLE);
00118 
00119   /* Enable SPI_SLAVE Rx request */
00120   SPI_I2S_DMACmd(SPI_SLAVE, SPI_I2S_DMAReq_Rx, ENABLE);
00121 
00122   /* Enable SPI_SLAVE */
00123   SPI_Cmd(SPI_SLAVE, ENABLE);
00124   /* Enable SPI_MASTER */
00125   SPI_Cmd(SPI_MASTER, ENABLE);
00126 
00127   /* Enable DMA1 Channel4 */
00128   DMA_Cmd(SPI_SLAVE_Rx_DMA_Channel, ENABLE);
00129 
00130   /* Transfer procedure */
00131   while (TxIdx < BufferSize)
00132   {
00133     /* Wait for SPI_MASTER Tx buffer empty */
00134     while (SPI_I2S_GetFlagStatus(SPI_MASTER, SPI_I2S_FLAG_TXE) == RESET);
00135     /* Send SPI_MASTER data */
00136     SPI_I2S_SendData(SPI_MASTER, SPI_MASTER_Buffer_Tx[TxIdx++]);
00137   }
00138 
00139   /* Wait for DMA1 channel4 transfer complete */
00140   while (!DMA_GetFlagStatus(SPI_SLAVE_Rx_DMA_FLAG));
00141 
00142   /* Check the corectness of written data */
00143   TransferStatus = Buffercmp(SPI_SLAVE_Buffer_Rx, SPI_MASTER_Buffer_Tx, BufferSize);
00144   /* TransferStatus = PASSED, if the transmitted and received data
00145      are equal */
00146   /* TransferStatus = FAILED, if the transmitted and received data
00147      are different */
00148 
00149   while (1)
00150   {}
00151 }
00152 
00153 /**
00154   * @brief  Configures the different system clocks.
00155   * @param  None
00156   * @retval None
00157   */
00158 void RCC_Configuration(void)
00159 {
00160   /* PCLK2 = HCLK/2 */
00161   RCC_PCLK2Config(RCC_HCLK_Div2); 
00162 
00163   /* Enable peripheral clocks --------------------------------------------------*/
00164   /* Enable SPI_SLAVE DMA clock */
00165   RCC_AHBPeriphClockCmd(SPI_SLAVE_DMA_CLK, ENABLE);
00166 
00167 #ifdef USE_STM3210C_EVAL
00168   /* Enable GPIO clock for SPI_MASTER and SPI_SLAVE */
00169   RCC_APB2PeriphClockCmd(SPI_MASTER_GPIO_CLK | SPI_SLAVE_GPIO_CLK |
00170                          RCC_APB2Periph_AFIO, ENABLE);
00171 
00172   /* Enable SPI_MASTER Periph clock */
00173   RCC_APB1PeriphClockCmd(SPI_MASTER_CLK, ENABLE);
00174                            
00175 #else
00176   /* Enable SPI_MASTER clock and GPIO clock for SPI_MASTER and SPI_SLAVE */
00177   RCC_APB2PeriphClockCmd(SPI_MASTER_GPIO_CLK | SPI_SLAVE_GPIO_CLK |
00178                          SPI_MASTER_CLK, ENABLE);
00179 #endif
00180   /* Enable SPI_SLAVE Periph clock */
00181   RCC_APB1PeriphClockCmd(SPI_SLAVE_CLK, ENABLE);
00182 }
00183 
00184 /**
00185   * @brief  Configures the different GPIO ports.
00186   * @param  None
00187   * @retval None
00188   */
00189 void GPIO_Configuration(void)
00190 {
00191   GPIO_InitTypeDef GPIO_InitStructure;
00192 
00193 #ifdef USE_STM3210C_EVAL
00194   /* Enable SPI3 Pins Software Remapping */
00195   GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);
00196 
00197   /* Configure SPI_MASTER pins: SCK and MOSI */
00198   GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_SCK | SPI_MASTER_PIN_MOSI;
00199   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00200   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00201   GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure);
00202 
00203   /* Configure SPI_MASTER NSS pin */
00204   GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_NSS;
00205   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00206   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00207   GPIO_Init(SPI_MASTER_GPIO_NSS, &GPIO_InitStructure);
00208 
00209 #else
00210   /* Configure SPI_MASTER pins: NSS, SCK and MOSI */
00211   GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_NSS | SPI_MASTER_PIN_SCK | SPI_MASTER_PIN_MOSI;
00212   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00213   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00214   GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure);
00215 #endif
00216 
00217   /* Configure SPI_SLAVE pins: NSS, SCK and MISO*/
00218   GPIO_InitStructure.GPIO_Pin = SPI_SLAVE_PIN_NSS | SPI_SLAVE_PIN_SCK;
00219   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00220   GPIO_Init(SPI_SLAVE_GPIO, &GPIO_InitStructure);
00221 
00222   GPIO_InitStructure.GPIO_Pin =  SPI_SLAVE_PIN_MISO;
00223   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00224   GPIO_Init(SPI_SLAVE_GPIO, &GPIO_InitStructure);
00225   
00226 }
00227 
00228 /**
00229   * @brief  Compares two buffers.
00230   * @param  pBuffer1, pBuffer2: buffers to be compared.
00231   * @param  BufferLength: buffer's length
00232   * @retval PASSED: pBuffer1 identical to pBuffer2
00233   *   FAILED: pBuffer1 differs from pBuffer2
00234   */
00235 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00236 {
00237   while (BufferLength--)
00238   {
00239     if (*pBuffer1 != *pBuffer2)
00240     {
00241       return FAILED;
00242     }
00243 
00244     pBuffer1++;
00245     pBuffer2++;
00246   }
00247 
00248   return PASSED;
00249 }
00250 
00251 #ifdef  USE_FULL_ASSERT
00252 
00253 /**
00254   * @brief  Reports the name of the source file and the source line number
00255   *         where the assert_param error has occurred.
00256   * @param  file: pointer to the source file name
00257   * @param  line: assert_param error line source number
00258   * @retval None
00259   */
00260 void assert_failed(uint8_t* file, uint32_t line)
00261 {
00262   /* User can add his own implementation to report the file name and line number,
00263      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00264 
00265   /* Infinite loop */
00266   while (1)
00267   {}
00268 }
00269 #endif
00270 
00271 /**
00272   * @}
00273   */ 
00274 
00275 /**
00276   * @}
00277   */ 
00278 
00279 /******************* (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