各位大神,小弟最近刚学STM32,关于SPI基于DMA方式的通信,遇到了一个问题,SPI1作主机发送20个数据,可为毛SPI2作从机收到的全是0x00 啊?求解啊... 
 
main.c文件的程序: 
 
/* Includes ------------------------------------------------------------------*/ 
#include "stm32f10x.h" 
#include "stm32f10x_gpio.h" 
#include "stm32f10x_rcc.h" 
#include "stm32f10x_spi.h" 
#include "stm32f10x_dma.h" 
#include "platform_config.h" 
 
#include "bsp_led.h" 
#include "bsp_usart.h" 
 
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; 
 
#define BufferSize       32 
 
SPI_InitTypeDef  SPI_InitStructure; 
DMA_InitTypeDef  DMA_InitStructure; 
GPIO_InitTypeDef GPIO_InitStructure; 
 
uint8_t SPI_MASTER_Buffer_Tx[BufferSize] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 
                                            0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 
                                            0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 
                                            0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 
                                            0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 
                                            0x1F, 0x20}; 
uint8_t SPI_SLAVE_Buffer_Rx[BufferSize]; 
__IO uint8_t TxIdx = 0; 
volatile TestStatus TransferStatus = FAILED; 
 
void RCC_Configuration(void); 
void GPIO_Configuration(void); 
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength); 
 
int main(void) 
{ 
        uint32_t num;   
         
        LED_GPIO_Config(); 
        USARTx_Config(); 
 
  RCC_Configuration(); 
 
  GPIO_Configuration(); 
 
  DMA_DeInit(SPI_SLAVE_Rx_DMA_Channel); 
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPI_SLAVE_DR_Base; 
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI_SLAVE_Buffer_Rx; 
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; 
  DMA_InitStructure.DMA_BufferSize = BufferSize; 
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; 
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; 
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; 
  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; 
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 
  DMA_Init(SPI_SLAVE_Rx_DMA_Channel, &DMA_InitStructure); 
 
  /* SPI_MASTER configuration ------------------------------------------------------*/ 
  SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; 
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master; 
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; 
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; 
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; 
  SPI_InitStructure.SPI_NSS = SPI_NSS_Hard; 
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; 
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; 
  SPI_InitStructure.SPI_CRCPolynomial = 7; 
  SPI_Init(SPI_MASTER, &SPI_InitStructure); 
 
  /* SPI_SLAVE configuration ------------------------------------------------------*/ 
  SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx; 
  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; 
  SPI_Init(SPI_SLAVE, &SPI_InitStructure); 
 
  /* Enable SPI_MASTER NSS output for master mode */ 
  SPI_SSOutputCmd(SPI_MASTER, ENABLE); 
 
  /* Enable SPI_SLAVE Rx request */ 
  SPI_I2S_DMACmd(SPI_SLAVE, SPI_I2S_DMAReq_Rx, ENABLE); 
 
  /* Enable SPI_SLAVE */ 
  SPI_Cmd(SPI_SLAVE, ENABLE); 
  /* Enable SPI_MASTER */ 
  SPI_Cmd(SPI_MASTER, ENABLE); 
         
        printf("\r\n 写入的数据为: \r\n"); 
        for(num = 0; num < BufferSize; num++) 
        { 
                printf(&quot; 0x%02X &quot;, SPI_MASTER_Buffer_Tx[num]); 
                if(SPI_MASTER_Buffer_Tx[num] % 8 == 0) 
                        printf(&quot;\r\n&quot;); 
        } 
 
  /* Enable DMA1 Channel4 */ 
  DMA_Cmd(SPI_SLAVE_Rx_DMA_Channel, ENABLE); 
         
  /* Transfer procedure */ 
  while (TxIdx < BufferSize) 
  { 
    /* Wait for SPI_MASTER Tx buffer empty */ 
    while (SPI_I2S_GetFlagStatus(SPI_MASTER, SPI_I2S_FLAG_TXE) == RESET); 
    /* Send SPI_MASTER data */ 
    SPI_I2S_SendData(SPI_MASTER, SPI_MASTER_Buffer_Tx[TxIdx++]); 
  } 
 
  /* Wait for DMA1 channel4 transfer complete */ 
  while (!DMA_GetFlagStatus(SPI_SLAVE_Rx_DMA_FLAG)); 
 
  /* Check the correctness of written data */ 
  TransferStatus = Buffercmp(SPI_SLAVE_Buffer_Rx, SPI_MASTER_Buffer_Tx, BufferSize); 
  /* TransferStatus = PASSED, if the transmitted and received data 
     are equal */ 
  /* TransferStatus = FAILED, if the transmitted and received data 
     are different */ 
         
        printf(&quot; 读出的数据为: \r\n&quot;); 
        for(num = 0; num < BufferSize; num++) 
        { 
                printf(&quot; 0x%02X &quot;, SPI_SLAVE_Buffer_Rx[num]); 
                if(SPI_SLAVE_Buffer_Rx[num] % 8 == 0) 
                        printf(&quot;\r\n&quot;); 
        } 
         
        if(TransferStatus == PASSED) 
                for(num = 0; num < 0xFFFF; num++) 
                        LED_CIRCLE(num); 
        else 
                LEDx_TOGGLE(GPIO_Pin_5); 
 
  while (1) 
  {} 
} 
 
/** 
  * @brief  Configures the different system clocks. 
  * @param  None 
  * @retval None 
  */ 
void RCC_Configuration(void) 
{ 
  /* PCLK2 = HCLK/2 */ 
//  RCC_PCLK2Config(RCC_HCLK_Div2);  
 
  /* Enable peripheral clocks --------------------------------------------------*/ 
  /* Enable SPI_SLAVE DMA clock */ 
  RCC_AHBPeriphClockCmd(SPI_SLAVE_DMA_CLK, ENABLE); 
 
  /* Enable SPI_MASTER clock and GPIO clock for SPI_MASTER and SPI_SLAVE */ 
  RCC_APB2PeriphClockCmd(SPI_MASTER_GPIO_CLK | SPI_SLAVE_GPIO_CLK | 
                         SPI_MASTER_CLK, ENABLE); 
 
  /* Enable SPI_SLAVE Periph clock */ 
  RCC_APB1PeriphClockCmd(SPI_SLAVE_CLK, ENABLE); 
} 
 
/** 
  * @brief  Configures the different GPIO ports. 
  * @param  None 
  * @retval None 
  */ 
void GPIO_Configuration(void) 
{ 
  GPIO_InitTypeDef GPIO_InitStructure; 
 
  /* Enable SPI3 Pins Software Remapping */ 
 
  /* Configure SPI_MASTER pins: SCK and MOSI */ 
  GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_SCK | SPI_MASTER_PIN_MOSI; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
  GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure); 
 
  /* Configure SPI_MASTER NSS pin */ 
  GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_NSS; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
  GPIO_Init(SPI_MASTER_GPIO_NSS, &GPIO_InitStructure); 
 
  /* Configure SPI_SLAVE pins: NSS, SCK and MISO*/ 
  GPIO_InitStructure.GPIO_Pin = SPI_SLAVE_PIN_NSS | SPI_SLAVE_PIN_SCK; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
  GPIO_Init(SPI_SLAVE_GPIO, &GPIO_InitStructure); 
 
  GPIO_InitStructure.GPIO_Pin =  SPI_SLAVE_PIN_MISO; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
  GPIO_Init(SPI_SLAVE_GPIO, &GPIO_InitStructure); 
 
} 
 
/** 
  * @brief  Compares two buffers. 
  * @param  pBuffer1, pBuffer2: buffers to be compared. 
  * @param  BufferLength: buffer's length 
  * @retval PASSED: pBuffer1 identical to pBuffer2 
  *         FAILED: pBuffer1 differs from pBuffer2 
  */ 
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength) 
{ 
  while (BufferLength--) 
  { 
    if (*pBuffer1 != *pBuffer2) 
    { 
      return FAILED; 
    } 
 
    pBuffer1++; 
    pBuffer2++; 
  } 
 
  return PASSED; 
} 
 
#ifdef  USE_FULL_ASSERT 
 
/** 
  * @brief  Reports the name of the source file and the source line number 
  *         where the assert_param error has occurred. 
  * @param  file: pointer to the source file name 
  * @param  line: assert_param error line source number 
  * @retval None 
  */ 
void assert_failed(uint8_t* file, uint32_t line) 
{ 
  /* User can add his own implementation to report the file name and line number, 
     ex: printf(&quot;Wrong parameters value: file %s on line %d\r\n&quot;, file, line) */ 
 
  /* Infinite loop */ 
  while (1) 
  {} 
} 
 
#endif 
 
/** 
  * @} 
  */  
 
/** 
  * @} 
  */  
 
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 
 
 
 
 
platform_config.h文件: 
 
/* Define to prevent recursive inclusion -------------------------------------*/ 
#ifndef __PLATFORM_CONFIG_H 
#define __PLATFORM_CONFIG_H 
 
/* Includes ------------------------------------------------------------------*/ 
 
/* Exported types ------------------------------------------------------------*/ 
/* Exported constants --------------------------------------------------------*/ 
/* Uncomment the line corresponding to the STMicroelectronics evaluation board 
   used to run the example */ 
 
  #define SPI_MASTER                   SPI1 
  #define SPI_MASTER_CLK               RCC_APB2Periph_SPI1 
  #define SPI_MASTER_GPIO              GPIOA 
  #define SPI_MASTER_GPIO_NSS                                         GPIOC 
  #define SPI_MASTER_GPIO_CLK          RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC 
  #define SPI_MASTER_PIN_NSS           GPIO_Pin_0 
  #define SPI_MASTER_PIN_SCK           GPIO_Pin_5 
  #define SPI_MASTER_PIN_MISO          GPIO_Pin_6 
  #define SPI_MASTER_PIN_MOSI          GPIO_Pin_7 
 
  #define SPI_SLAVE                    SPI2 
  #define SPI_SLAVE_CLK                RCC_APB1Periph_SPI2 
  #define SPI_SLAVE_GPIO               GPIOB 
  #define SPI_SLAVE_GPIO_CLK           RCC_APB2Periph_GPIOB  
  #define SPI_SLAVE_PIN_NSS            GPIO_Pin_12 
  #define SPI_SLAVE_PIN_SCK            GPIO_Pin_13 
  #define SPI_SLAVE_PIN_MISO           GPIO_Pin_14 
  #define SPI_SLAVE_PIN_MOSI           GPIO_Pin_15  
  #define SPI_SLAVE_DMA                DMA1 
  #define SPI_SLAVE_DMA_CLK            RCC_AHBPeriph_DMA1   
  #define SPI_SLAVE_Rx_DMA_Channel     DMA1_Channel4 
  #define SPI_SLAVE_Rx_DMA_FLAG        DMA1_FLAG_TC4 
//  #define SPI_SLAVE_Tx_DMA_Channel     DMA1_Channel5 
//  #define SPI_SLAVE_Tx_DMA_FLAG        DMA1_FLAG_TC5   
  #define SPI_SLAVE_DR_Base            0x4000380C 
 
/* Exported macro ------------------------------------------------------------*/ 
/* Exported functions ------------------------------------------------------- */ 
 
#endif /* __PLATFORM_CONFIG_H */ 
 
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ |   
     
  
 |