cancel
Showing results for 
Search instead for 
Did you mean: 

How to make official spi examples master and slave in one board.(slave can not work)

yuming
Associate II

hi, I use the official example to change, the example need two board(one master and one slave), I am tring to make spi master and slave communication in one board, My board stm32f411ve. I use PortB13~15(spi2) and3~5(spi3), when I set both of spi2 and spi3 to be master, and MOSI connect to itselfs MISO, its work, and the value is right. When I set one of them to slave, MOSI(PB15)-MOSI(PB5) MISO(PB14)-MISO(PB4) SCK(PB3)-SCK(PB13)and it will time out error. this is my code.

#define MASTER_BOARD

/* Private variables ---------------------------------------------------------*/

/* SPI handler declaration */

SPI_HandleTypeDef SpiHandle;

SPI_HandleTypeDef SpiHandle2;

/* Buffer used for transmission */

uint8_t aTxBuffer[] = "****SPI - Two Boards communication based on Polling **** SPI Message ******** SPI Message ******** SPI Message ****";

/* Buffer used for reception */

uint8_t aRxBuffer[BUFFERSIZE];

/* Private function prototypes -----------------------------------------------*/

static void SystemClock_Config(void);

static void Error_Handler(void);

static void Timeout_Error_Handler(void);

static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);

/* Private functions ---------------------------------------------------------*/

/**

 * @brief Main program

 * @param None

 * @retval None

 */

int main(void)

{

 /* STM32F4xx HAL library initialization:

    - Configure the Flash prefetch, instruction and Data caches

    - Configure the Systick to generate an interrupt each 1 msec

    - Set NVIC Group Priority to 4

    - Global MSP (MCU Support Package) initialization

   */

 HAL_Init();

 /* Configure LED3, LED4, LED5 and LED6 */

 BSP_LED_Init(LED3);

 BSP_LED_Init(LED4);

 BSP_LED_Init(LED5);

 BSP_LED_Init(LED6);

 /* Configure the system clock to 100 MHz */

 SystemClock_Config();

  

 /*##-1- Configure the SPI peripheral #######################################*/

 /* Set the SPI parameters */

 SpiHandle.Instance        = SPIx;

  

 SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;

 SpiHandle.Init.Direction     = SPI_DIRECTION_2LINES;

 SpiHandle.Init.CLKPhase     = SPI_PHASE_1EDGE;

 SpiHandle.Init.CLKPolarity    = SPI_POLARITY_HIGH;

 SpiHandle.Init.CRCCalculation  = SPI_CRCCALCULATION_DISABLE;

 SpiHandle.Init.CRCPolynomial   = 7;

 SpiHandle.Init.DataSize     = SPI_DATASIZE_8BIT;

 SpiHandle.Init.FirstBit     = SPI_FIRSTBIT_MSB;

 SpiHandle.Init.NSS        = SPI_NSS_SOFT;

 SpiHandle.Init.TIMode      = SPI_TIMODE_DISABLE;

  

#ifdef MASTER_BOARD

 SpiHandle.Init.Mode = SPI_MODE_MASTER;

//SpiHandle.Init.Mode = SPI_MODE_SLAVE;

#else

 SpiHandle.Init.Mode = SPI_MODE_SLAVE;

#endif /* MASTER_BOARD */

 if(HAL_SPI_Init(&SpiHandle) != HAL_OK)

 {

  /* Initialization Error */

  Error_Handler();

 }

 SpiHandle2.Instance        = SPI3;

  

 SpiHandle2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;

 SpiHandle2.Init.Direction     = SPI_DIRECTION_2LINES;

 SpiHandle2.Init.CLKPhase     = SPI_PHASE_1EDGE;

 SpiHandle2.Init.CLKPolarity    = SPI_POLARITY_HIGH;

 SpiHandle2.Init.CRCCalculation  = SPI_CRCCALCULATION_DISABLE;

 SpiHandle2.Init.CRCPolynomial   = 7;

 SpiHandle2.Init.DataSize     = SPI_DATASIZE_8BIT;

 SpiHandle2.Init.FirstBit     = SPI_FIRSTBIT_MSB;

 SpiHandle2.Init.NSS        = SPI_NSS_SOFT;

 SpiHandle2.Init.TIMode      = SPI_TIMODE_DISABLE;  

#ifdef MASTER_BOARD 

SpiHandle2.Init.Mode = SPI_MODE_SLAVE;

//SpiHandle2.Init.Mode = SPI_MODE_MASTER;

#else

  SpiHandle2.Init.Mode = SPI_MODE_MASTER;

#endif /* MASTER_BOARD */

if(HAL_SPI_Init(&SpiHandle2) != HAL_OK)

 {

  /* Initialization Error */

  Error_Handler();

 }

#ifdef MASTER_BOARD

 /* Configure Tamper push button */

 BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);

 /* Wait for Tamper Button press before starting the Communication */

 while (BSP_PB_GetState(BUTTON_KEY) != 1)

 {

  BSP_LED_Toggle(LED3);

  HAL_Delay(40);

 }

  

  BSP_LED_Off(LED3);

#endif /* MASTER_BOARD */ 

 /*##-2- Start the Full Duplex Communication process ########################*/  

 /* While the SPI in TransmitReceive process, user can transmit data through 

   "aTxBuffer" buffer & receive data through "aRxBuffer" */

 /* Timeout is set to 5S */

  

switch(HAL_SPI_TransmitReceive(&SpiHandle2, (uint8_t*)aTxBuffer, (uint8_t *)aRxBuffer, BUFFERSIZE, 5000))

 {

 case HAL_OK:  

  /* Communication is completed_____________________________________________*/

  /* Compare the sent and received buffers */

  if(Buffercmp((uint8_t*)aTxBuffer, (uint8_t*)aRxBuffer, BUFFERSIZE))

  {

   /* Transfer error in transmission process */

   Error_Handler();   

  }

   

  /* Turn LED4 on: Transfer in transmission process is correct */

  // BSP_LED_On(LED4);

  /* Turn LED6 on: Transfer in reception process is correct */

  BSP_LED_On(LED6);

  break;  

   

 case HAL_TIMEOUT:

  /* A Timeout occurred______________________________________________________*/

  /* Call Timeout Handler */

  Timeout_Error_Handler();  

  break;  

   

  /* An Error occurred_______________________________________________________*/

 case HAL_ERROR:

  /* Call Timeout Handler */

  Error_Handler();  

  break;

  

 default:

  break;

 }

switch(HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t*)aTxBuffer, (uint8_t *)aRxBuffer, BUFFERSIZE, 5000))

 {

 case HAL_OK:  

  /* Communication is completed_____________________________________________*/

  /* Compare the sent and received buffers */

  if(Buffercmp((uint8_t*)aTxBuffer, (uint8_t*)aRxBuffer, BUFFERSIZE))

  {

   /* Transfer error in transmission process */

   Error_Handler();   

  }

   

  /* Turn LED4 on: Transfer in transmission process is correct */

  BSP_LED_On(LED4);

  /* Turn LED6 on: Transfer in reception process is correct */

 // BSP_LED_On(LED6);

  break;  

   

 case HAL_TIMEOUT:

  /* A Timeout occurred______________________________________________________*/

  /* Call Timeout Handler */

  Timeout_Error_Handler();  

  break;  

   

  /* An Error occurred_______________________________________________________*/

 case HAL_ERROR:

  /* Call Timeout Handler */

  Error_Handler();  

  break;

  

 default:

  break;

 }

 /* Infinite loop */

 while (1)

 {

 }

}

THANKS

0 REPLIES 0