cancel
Showing results for 
Search instead for 
Did you mean: 

SPI communication problem MISO

bolognino
Associate
Posted on February 11, 2015 at 11:11

Hello,

I am trying

to interface the

board

STM32F4

discovery with

communication chip

SPIRIT1

.

When

SPI communication

,

MOSI

is ok but MISO

always sends 0xFF.

I send you

my code

Please help !

thank you in advance,

Bolognino Antonin CODE: //Fonction Main :

#include <
stm32f4xx.h
>
#include <
stm32f4xx_gpio.h
>
#include <
stm32f4xx_spi.h
>
#include <
stm32f4xx_rcc.h
>
#include <
SPIRIT_Commands.h
>
#include <
MCU_Interface.h
>
int main (void)
{
SdkEvalSpiInit();
uint8_t Reponse[1];
SdkEvalSpiReadRegisters(ANA_FUNC_CONF0_BASE, 1, Reponse);
while(1);
return 0;
}
//////////////////////////////////////////////////////////////////////////////////
//Librairie SPI :
/* Includes ------------------------------------------------------------------*/
#include <
stm32f4xx.h
>
#include <
stm32f4xx_gpio.h
>
#include <
MCU_Interface.h
>
#include <
stm32f4xx_rcc.h
>
#include <
stm32f4xx_spi.h
>
#define CS_TO_SCLK_DELAY 0x0001
#define CLK_TO_CS_DELAY 0x0001
#define HEADER_WRITE_MASK 0x00 /*!< Write mask for header byte*/
#define HEADER_READ_MASK 0x01 /*!< Read mask for header byte*/
#define HEADER_ADDRESS_MASK 0x00 /*!< Address mask for header byte*/
#define HEADER_COMMAND_MASK 0x80 /*!< Command mask for header byte*/
#define LINEAR_FIFO_ADDRESS 0xFF /*!< Linear FIFO address*/
#define BUILT_HEADER(add_comm, w_r) (add_comm | w_r) /*!< macro to build the header byte*/
#define WRITE_HEADER BUILT_HEADER(HEADER_ADDRESS_MASK, HEADER_WRITE_MASK) /*!< macro to build the write header byte*/
#define READ_HEADER BUILT_HEADER(HEADER_ADDRESS_MASK, HEADER_READ_MASK) /*!< macro to build the read header byte*/
#define COMMAND_HEADER BUILT_HEADER(HEADER_COMMAND_MASK, HEADER_WRITE_MASK) /*!< macro to build the command header byte*/
#define SPICSLow() GPIO_ResetBits(GPIOA, GPIO_Pin_4);
#define SPICSHigh() GPIO_SetBits(GPIOA, GPIO_Pin_4);
//INIT SPI //////////////////////////
void SdkEvalSpiInit(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;
//Common settings for all pins
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
//Enable clock for GPIOA
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//Pinspack nr. 1 SCK MISO MOSI
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
//Enable clock for SPI1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
SPI_StructInit(&SPI_InitStruct);
//Définition des valeurs d'initialisation du SPI
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
//Désactivation du SPI1
SPI_Cmd(SPI1, DISABLE);
//Mettre les valeurs de reset
SPI_DeInit(SPI1);
//Initialiser le SPI1 avec les valeurs définies
SPI_Init(SPI1, &SPI_InitStruct);
//Activation du SPI
SPI_Cmd(SPI1, ENABLE);
/* Configure SPI pin: CS */
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
//
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOA, &GPIO_InitStruct);
SPICSHigh();
}
StatusBytes SdkEvalSpiReadRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer)
{
uint16_t tmpstatus = 0x0000;
StatusBytes *status=(StatusBytes *)&tmpstatus;
uint8_t header[2];
uint8_t dummy=0xFF;
/* Built the header bytes */
header[0]=READ_HEADER;
header[1]=cRegAddress;
uint16_t i =0;
/* Put the SPI chip select low to start the transaction */
SPICSLow();
for(i=0;i<CS_TO_SCLK_DELAY;i++);
/* Write the header bytes and read the STS2001 status bytes */
for(i=0; i<2; i++)
{ SPI_SendData(SPI1, header[i]);
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_BSY) == SET);
tmpstatus += ((uint16_t)(SPI_ReceiveData(SPI1)))<<((1-i)*8);
}
uint16_t index = 0;
/* Read the registers according to the number of bytes */
for(index=0; index<cNbBytes; index++)
{
SPI_SendData(SPI1, dummy);
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_BSY) == SET);
*pcBuffer = SPI_ReceiveData(SPI1);
pcBuffer++;
}
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
/* Put the SPI chip select high to end the transaction */
SPICSHigh();
return *status;
}

1 REPLY 1
igal
Associate II
Posted on February 12, 2015 at 13:35

Since the ST is the master I'm assuming the MISO problem is what the Slave sends, so you need to check the code on the other side, not the ST.

In any case I would modify the code like this:

 

  for(i=0;i<CS_TO_SCLK_DELAY;i++);

 

  /* Write the header bytes and read the STS2001 status bytes */

  for(i=0; i<2; i++)

  {

        while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);

        SPI_I2S_SendData(SPI1, header[i]);

        while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);

        tmpstatus += SPI_I2S_ReceiveData(SPI1);

  }

  uint16_t index = 0;

  /* Read the registers according to the number of bytes */

  for(index=0; index<cNbBytes; index++)

  {

        while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);

        SPI_I2S_SendData(SPI1, dummy);

        while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);

        *pcBuffer = SPI_I2S_ReceiveData(SPI1);

    pcBuffer++;

  }

  while (SPI_GetFlagStatus(SPI1, SPI_FLAG_BSY) == SET);

 

  /* Put the SPI chip select high to end the transaction */

  SPICSHigh();