AnsweredAssumed Answered

Problem with SPI

Question asked by Deer90 on Mar 20, 2013
Latest reply on Mar 22, 2013 by Deer90
Hello, I´m using STM32F303 and I´m trying to communicate via SPI (simplex SPI1-SPI2 using interrupts). It couldn´t be easier but it doesn´t work and I can´t find mistake. I connect PB3 (SPI1_SCK) with PB13(SPI2_SCK) and PB5(SPI1_MOSI) with PB14(SPI2_MISO) but it doesn´t generate interrupts. Here is my code. Thanks a lot for your help.

/* Private variables ---------------------------------------------------------*/
NVIC_InitTypeDef      NVIC_InitStructure;
GPIO_InitTypeDef      GPIO_InitStructure;
__IO uint32_t TimDelay = 0;
uint8_t SPI_MASTER_Buffer_Tx[]={1};
uint8_t ReceivedData;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void SPI_Ini(void);

int main(void)
{

    SPI_Ini();

    /* GPIOE Periph clock enable */
   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
    
   /* Configure PE.9 (LED) */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_Init(GPIOE, &GPIO_InitStructure);
    
    while(1){
    
        /* Send SPI_MASTER data */
       SPI_SendData8(SPI1, SPI_MASTER_Buffer_Tx[0]);
        
       if(ReceivedData!=0)GPIOE->BSRR = 0x200; // LED on
        
       for(TimDelay=0; TimDelay < 100000; TimDelay++);
        
  }
}

void SPI_Ini(void)
{
    
    SPI_InitTypeDef  SPI_InitStructure;
    
    /* Enable GPIOB for SCK, MISO, MOSI GPIO clocks */
   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    
    /* Enable the SPI1 periph */
   RCC_APB1PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
    
    /* Enable the SPI2 periph */
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
 
    /*----------------------Inicialization SPI master------------------------------------*/
    
    /* SPI1 SCK pin configuration: AF PP output 50MHz  */
   GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_5);  // PB3: SPI1 SCK
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;            // alternate mode
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    // 50MHz
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;         // push pull output
   //GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
   GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    /* SPI1  MOSI pin configuration: AF PP output 50MHz */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_5); // PB5: SPI1 MOSI
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;                 // alternate mode
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         // 50MHz
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;              // push pull output
    //GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* SPI1 configuration -------------------------------------------------------*/
  SPI_I2S_DeInit(SPI1);
  SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_Init(SPI1, &SPI_InitStructure);
    
    /*----------------------Inicialization SPI slave------------------------------------*/
 
    /* SPI2 SCK pin configuration: input mode 50MHz */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;             // input mode
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    // 50MHz
    //GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    /* SPI2 MISO pin configuration: AF PP output 50MHz */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_5); // PB14: SPI2 MISO
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;            // alternative mode
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    // 50MHz
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;         // push pull output
    //GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    /* SPI2 configuration -------------------------------------------------------*/
    SPI_I2S_DeInit(SPI2);
    SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
    SPI_Init(SPI2, &SPI_InitStructure);
    
    /* Enable the Rx buffer not empty interrupt */
    SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
    
    /* Configure the SPI interrupt priority */
   NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);
    
    /* Enable the SPI peripheral */
   SPI_Cmd(SPI2, ENABLE);
    
    /* Enable the SPI peripheral */
   SPI_Cmd(SPI1, ENABLE);
 
}

void SPI2_IRQHandler(void)
{
    /* SPI in Slave Receiver mode--------------------------------------- */
   if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET)
  {
        ReceivedData = SPI_ReceiveData8(SPI2);
  }
}

 

Outcomes