AnsweredAssumed Answered

Communication between two stm32l031k6 (lib STM32cubemx)

Question asked by ENEDIS.ibrahim on Jul 7, 2016
Latest reply on Jul 8, 2016 by rozenko.serge
Hello xD,
I want to communicate with two STM32L031K6 in SPI with the library HAL for STM32CubeMX.
I configure a Master with emission interruption and the other in slave reception with interruption. and I connect the two STM32 with son
(PA5) MOSI ---  (PA5) MOSI,
(PB4) MISO --- MISO
(PB5) SCK --- SCK.
But I can not communicate or to receive the data. I do not know why. That is my program. Thank you very much.

spi.c
/* Includes ------------------------------------------------------------------*/
#include "spi.h"
#include "gpio.h"
 
extern __IO uint8_t data_get[1];
SPI_HandleTypeDef hspi1;
 
/* SPI1 init function */
void MX_SPI1_Init(void)
{
 
  hspi1.Instance = SPI1;
 /* master mode for stm32 send data */
  hspi1.Init.Mode = SPI_MODE_MASTER;
  /* slave mode for stm32 receive data */
  //hspi1.Init.Mode = SPI_MODE_SLAVE;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 7;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
}
 
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{
 
  GPIO_InitTypeDef GPIO_InitStruct;
  if(spiHandle->Instance==SPI1)
  {
    /* Peripheral clock enable */
    __HAL_RCC_SPI1_CLK_ENABLE();
   
    /**SPI1 GPIO Configuration   
    PA5     ------> SPI1_SCK
    PB4     ------> SPI1_MISO
    PB5     ------> SPI1_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
  }
}
 
void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
{
 
  if(spiHandle->Instance==SPI1)
  {
    /* Peripheral clock disable */
    __HAL_RCC_SPI1_CLK_DISABLE();
   
    /**SPI1 GPIO Configuration   
    PA5     ------> SPI1_SCK
    PB4     ------> SPI1_MISO
    PB5     ------> SPI1_MOSI
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5);
 
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_4|GPIO_PIN_5);
 
  }
}

main.c 
/* Includes ------------------------------------------------------------------*/
#include "stm32l0xx_hal.h"
#include <..\MDK-ARM\Hardware.h>
#include "usart.h"
#include "gpio.h"
#include "tim.h"
#include "spi.h"
 
#define BUFFERSIZE  100
 
volatile uint8_t data_get[1];
volatile uint8_t Save_data[1];
 
volatile int Index_BufferRx_SPI = 0;
int count_data = 0;
volatile uint8_t SPIStatus;
volatile uint8_t aTxBuffer[BUFFERSIZE] = "SPI Master/Slave : Communication between two SPI using Interrupts";
volatile uint8_t aRxbuffer[BUFFERSIZE];
 
 
int main(void)
{
    /* initialization UART, SPI, TIMER and GPIO */
    HW_Init();
     
    #if 0
    /* Enable flag interrupt emission */
    __HAL_SPI_ENABLE_IT(&hspi1, (SPI_IT_TXE));
    HAL_SPI_Transmit_IT(&hspi1, (uint8_t *) aTxBuffer, 100);
    #endif
     
    #if 1
    /* Enable flag interrupt recption */
    __HAL_SPI_ENABLE_IT(&hspi1, (SPI_IT_RXNE));
    HAL_SPI_Receive_IT(&hspi1, (uint8_t *) data_get, 1);
    #endif
     
    while(1)
    {
    }
}
 
 
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
{
    if(hspi->Instance == hspi1.Instance)
    {
        /* Save data and filling the table aRxbuffer */
        Save_data[0] = data_get[0];
        aRxbuffer[Index_BufferRx_SPI] = Save_data[0];
        Index_BufferRx_SPI++;
    }  
    /* it's ok to receive anathor data */
    SPIStatus =  HAL_SPI_Receive_IT(&hspi1, (uint8_t *) data_get, 1);
    while(SPIStatus != HAL_OK)
    {
        HAL_SPI_Receive_IT(&hspi1, (uint8_t *) data_get, 1);   
    }  
    Index_BufferRx_SPI = 0;
}
 
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
{
    if(hspi->Instance == hspi1.Instance)
    {
        SPIStatus =  HAL_SPI_Transmit_IT(&hspi1, (uint8_t *) aTxBuffer, 100);
        while(SPIStatus != HAL_OK)
        {
            HAL_SPI_Transmit_IT(&hspi1, (uint8_t *) aTxBuffer, 100);   
        }
    }
}


Outcomes