STM32 SPI2 no responde correctamente

-1

Tengo un problema con el SPI2 en la placa stm32f4vg, tengo un código que funciona perfectamente para spi1, pero cuando cambio el código para que se ejecute en spi2, el código no responde Aquí está el código original que encontré en este sitio web. enlace

 #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);
    }
}
}
} 

Ahora, en el momento en que cambio spi1 con SPI2, (la asignación de puertos y los pines también deben cambiarse de la siguiente manera:

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);
        }
    }
    }
    } 

Por favor, ¿alguien puede ayudarme a entender cuál es el problema?

    
pregunta werber bang

1 respuesta

3
    RCC_APB2PeriphClockCmd (RCC_APB1Periph_SPI2, ENABLE);
        ^^^^                    ^^^^

¿Ves la discrepancia aquí?

SPI2 es un periférico APB1, por lo que necesita habilitarlo usando RCC_APB1PeriphClockCmd() .

    
respondido por el duskwuff

Lea otras preguntas en las etiquetas