cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Communication between two STM32F4 DISCOVERY boards

alanrouse91
Associate
Posted on February 22, 2015 at 17:49

Hi, I'm currently trying to send data from one STM32F4 board as a master to the other STM32F4 board which is the slave via SPI. 

However recever data are incorect.

//************************Code Master************************************

void spi_init() 

{

  GPIO_InitTypeDef gpio;

  SPI_InitTypeDef spi1;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); 

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);  

  GPIO_StructInit(&gpio);

 

  gpio.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;

  gpio.GPIO_Mode = GPIO_Mode_AF;

  gpio.GPIO_Speed = GPIO_Speed_50MHz;

  gpio.GPIO_OType = GPIO_OType_PP;

  gpio.GPIO_PuPd = GPIO_PuPd_DOWN;

  GPIO_Init(GPIOA,&gpio);

 

  GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1);

  GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1);

  GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1);

 

  SPI_I2S_DeInit(SPI1); 

  SPI_StructInit(&spi1);

 

  spi1.SPI_Mode = SPI_Mode_Master;

  spi1.SPI_DataSize = SPI_DataSize_16b;

  spi1.SPI_NSS = SPI_NSS_Soft;

  SPI_Init(SPI1,&spi1);

  SPI_Cmd(SPI1,ENABLE);

}

void spi_send(uint16_t data) 

{

  SPI_I2S_SendData(SPI1,data);

  while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) == RESET);  

}

int main() 

{

 

spi_init();

  STM_EVAL_LEDInit(LED3);

  STM_EVAL_LEDOn(LED3);

  

  while(1) 

 

{

spi_send(0x0001);

  }

  return 0;

}

//******************************Code Slave****************************************

uint16_t data = 0x0000;

void spi_init() 

{

  GPIO_InitTypeDef gpio;

  SPI_InitTypeDef spi2;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);

  GPIO_StructInit(&gpio);

 

  gpio.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

  gpio.GPIO_Mode = GPIO_Mode_AF;

  gpio.GPIO_Speed = GPIO_Speed_50MHz;

  gpio.GPIO_OType = GPIO_OType_PP;

  gpio.GPIO_PuPd = GPIO_PuPd_DOWN;

  GPIO_Init(GPIOB,&gpio);

 

  GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_SPI2);

  GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_SPI2);

  GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_SPI2);

 

  SPI_I2S_DeInit(SPI2);

  SPI_StructInit(&spi2);

 

  spi2.SPI_Mode = SPI_Mode_Slave;

  spi2.SPI_DataSize = SPI_DataSize_16b;

  spi2.SPI_NSS = SPI_NSS_Soft;

  SPI_Init(SPI2,&spi2);

  SPI_Cmd(SPI2,ENABLE);

}

 

uint16_t spi_receve() 

{

  uint16_t received ;

  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);  

  received = SPI_I2S_ReceiveData(SPI2);

  return received;

}

int main() 

{

  spi_init();

  STM_EVAL_LEDInit(LED3);

  while(1) 

{

data = spi_receve();  //

data are correct

0x0800....0x0008....0x0000

if (data == 0x0001)

STM_EVAL_LEDOn(LED3);

  }

  return 0;

}

///////////////////////////////

Help me please.....thanks

#stm32f4-discovery
0 REPLIES 0