AnsweredAssumed Answered

SPI communication problem

Question asked by grossi.marco on Jul 6, 2015
Latest reply on Jul 6, 2015 by kirem
I am trying to use the SPI interface with a SPI temperature sensor and the STM32L Discovery board. I want to make a very basic read operation. I have set SPI_CLK to PA5 , MISO to PA11 and MOSI to PA12. The CS is controlled with the Digital Output Pin PA4.
However, after configuration, when I call the read procedure nothing works and only the CS pin works correctly (the other MISO MOSI CLK pin remain low).
Following are the procedures used to initialize SPI and call a read operation:

void SPI_initialization(void)
{
   RCC_APB2PeriphClockLPModeCmd(RCC_APB2Periph_SPI1, ENABLE);


   SPI_InitTypeDef SPI_InitTypeDefStruct;


   SPI_InitTypeDefStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
   SPI_InitTypeDefStruct.SPI_Mode = SPI_Mode_Master;
   SPI_InitTypeDefStruct.SPI_DataSize = SPI_DataSize_8b;
   SPI_InitTypeDefStruct.SPI_CPOL = SPI_CPOL_High;
   SPI_InitTypeDefStruct.SPI_CPHA = SPI_CPHA_2Edge;
   SPI_InitTypeDefStruct.SPI_NSS = SPI_NSS_Soft;
   SPI_InitTypeDefStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
   SPI_InitTypeDefStruct.SPI_FirstBit = SPI_FirstBit_MSB;


   SPI_Init(SPI1, &SPI_InitTypeDefStruct);


   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);


   GPIO_InitTypeDef gpioStructure;


   gpioStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_11 | GPIO_Pin_12;
   gpioStructure.GPIO_Mode = GPIO_Mode_AF;
   gpioStructure.GPIO_OType = GPIO_OType_PP;
   gpioStructure.GPIO_Speed = GPIO_Speed_40MHz;
   GPIO_Init(GPIOA, &gpioStructure);


   gpioStructure.GPIO_Pin = GPIO_Pin_4;
   gpioStructure.GPIO_Mode = GPIO_Mode_OUT;
   gpioStructure.GPIO_OType = GPIO_OType_PP;
   gpioStructure.GPIO_PuPd = GPIO_PuPd_UP;
   gpioStructure.GPIO_Speed = GPIO_Speed_40MHz;
   GPIO_Init(GPIOA, &gpioStructure);


   GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_SPI1);
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_SPI1);


   GPIO_SetBits(GPIOA, GPIO_Pin_4);


   SPI_Cmd(SPI1, ENABLE);


}


uint16_t mySPI_GetTemp(void){


uint8_t high8b_temp, low8b_temp;
uint16_t my16b_temp;


GPIO_ResetBits(GPIOA, GPIO_Pin_4);


uint8_t adress = 0b01010000;


while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, adress);
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));
SPI_I2S_ReceiveData(SPI1); //Clear RXNE bit


while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, 0x00); //Dummy byte to generate clock
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));


high8b_temp = SPI_I2S_ReceiveData(SPI1);


while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, 0x00); //Dummy byte to generate clock
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));


low8b_temp = SPI_I2S_ReceiveData(SPI1);


GPIO_SetBits(GPIOA, GPIO_Pin_4);


my16b_temp = (uint16_t)(high8b_temp<<8);
my16b_temp = my16b_temp | low8b_temp;


return  my16b_temp;
}

Anyone can help?
Thanks. 

Outcomes