Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a problem with the SPI2 on stm32f4vg board , I have a code that is working perfectly for spi1 , but when I change the code to run on spi2, the code doesn't respond here is the original code which I found on this web site http://easystm32.ru/interfaces/45-spi-interface-part-2

 #include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx.h"
#include "stm32f4xx_it.h"
#include "stm32f4xx_spi.h"

#define LED_PORT GPIOD

#define LED_GREEN (1 << 12)

#define LED_RED (1 << 14)

int main (void) {

GPIO_InitTypeDef GPIO_InitStructure;

SPI_InitTypeDef SPI_InitStructure;


// Timing port with LEDs

RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOD, ENABLE);


GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;



// Pins with LEDs are configured as outputs

GPIO_InitStructure.GPIO_Pin = (LED_GREEN | LED_RED);

GPIO_Init (LED_PORT, & GPIO_InitStructure);



// Timing SPI1 module and port A

RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1, ENABLE);
RCC_APB2PeriphClockCmd (RCC_APB1Periph_SPI2, ENABLE);

RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOB, ENABLE);


// Set SPI1 legs for running alternate function

GPIO_PinAFConfig (GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

GPIO_PinAFConfig (GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);

GPIO_PinAFConfig (GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_InitStructure.GPIO_Pin =( GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5);

GPIO_Init (GPIOA, & GPIO_InitStructure);



// fills the structure with the parameters SPI module



SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // full duplex

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // pass 8 bits

SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // polarity and

SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; // clock phase

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // control the state of NSS software

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; // prescaler SCK

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // The first significant bit is sent

SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // mode - master
*/
SPI_Init (SPI1, & SPI_InitStructure); // Set up the SPI1

SPI_Cmd (SPI1, ENABLE);

// Since the NSS signal is controlled by the software, install it in the unit

// If you reset it to zero, our SPI module will think

// we multimaster topology, and was stripped of his powers the wizard.

SPI_NSSInternalSoftwareConfig (SPI1, SPI_NSSInternalSoft_Set);


uint16_t data = 0;

while (1) {



SPI_I2S_SendData (SPI1, 0x93); // 0x93 bytes transferred through SPI1

while (SPI_I2S_GetFlagStatus (SPI1, SPI_I2S_FLAG_BSY) == SET) // The transmitter is busy?
; // means doing nothing



if (SPI_I2S_GetFlagStatus (SPI1, SPI_I2S_FLAG_BSY) == SET) 
{// If the data came


  data = SPI_I2S_ReceiveData (SPI1); // Read the received data
  if (data == 0x93) 
    {// Came correct data?
    GPIO_Write (LED_PORT, LED_GREEN);
    } 
  else 
    {
    GPIO_Write (LED_PORT, LED_RED);
    }
}
}
} 

Now the moment that I change spi1 with SPI2 , (the port mapping and pins also have to be changed as follows :

int main (void) {

    GPIO_InitTypeDef GPIO_InitStructure;

    SPI_InitTypeDef SPI_InitStructure;


    // Timing port with LEDs

    RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOD, ENABLE);


    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;



    // Pins with LEDs are configured as outputs

    GPIO_InitStructure.GPIO_Pin = (LED_GREEN | LED_RED);

    GPIO_Init (LED_PORT, & GPIO_InitStructure);



    // Timing SPI2 module and port B


    RCC_APB2PeriphClockCmd (RCC_APB1Periph_SPI2, ENABLE);


    RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOB, ENABLE);


    // Set SPI1 legs for running alternate function

    GPIO_PinAFConfig (GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);

    GPIO_PinAFConfig (GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);

    GPIO_PinAFConfig (GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_InitStructure.GPIO_Pin =( GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);

    GPIO_Init (GPIOB, & GPIO_InitStructure);



    // fills the structure with the parameters SPI module



    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // full duplex

    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // pass 8 bits

    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // polarity and

    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; // clock phase

    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // control the state of NSS software

    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; // prescaler SCK

    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // The first significant bit is sent

    SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // mode - master
    */
    SPI_Init (SPI2, & SPI_InitStructure); // Set up the SPI2

    SPI_Cmd (SPI2, ENABLE);

    // Since the NSS signal is controlled by the software, install it in the unit

    // If you reset it to zero, our SPI module will think

    // we multimaster topology, and was stripped of his powers the wizard.

    SPI_NSSInternalSoftwareConfig (SPI2, SPI_NSSInternalSoft_Set);


    uint16_t data = 0;

    while (1) {



    SPI_I2S_SendData (SPI2, 0x93); // 0x93 bytes transferred through SPI2

    while (SPI_I2S_GetFlagStatus (SPI2, SPI_I2S_FLAG_BSY) == SET) // The transmitter is busy?
    ; // means doing nothing



    if (SPI_I2S_GetFlagStatus (SPI2, SPI_I2S_FLAG_BSY) == SET) 
    {// If the data came


      data = SPI_I2S_ReceiveData (SPI2); // Read the received data
      if (data == 0x93) 
        {// Came correct data?
        GPIO_Write (LED_PORT, LED_GREEN);
        } 
      else 
        {
        GPIO_Write (LED_PORT, LED_RED);
        }
    }
    }
    } 

Please can any one help me understand what is the problem

share|improve this question
    
Double check that you are enabling the right peripheral busses, clocks, and GPIOs. With the way the STM32 libraries are typically written, you can write the right data to the wrong bus and get a valid compile, but code that won't do what is intended. – Chris Stratton Nov 23 '15 at 1:27
    
Please edit your question and fix the indention. – Lundin Nov 24 '15 at 15:21
up vote 3 down vote accepted
    RCC_APB2PeriphClockCmd (RCC_APB1Periph_SPI2, ENABLE);
        ^^^^                    ^^^^

See the mismatch here?

SPI2 is an APB1 peripheral, so you need to enable it using RCC_APB1PeriphClockCmd().

share|improve this answer
    
thank you so much , you spotted it right – werber bang Nov 23 '15 at 19:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.