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