cancel
Showing results for 
Search instead for 
Did you mean: 

SPI communication problem

marcogrossi89
Associate II
Posted on July 06, 2015 at 18:18

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. 
1 REPLY 1
kirem
Associate II
Posted on July 06, 2015 at 19:01

It looks like you have only enabled the Low Power Mode clock in the RCC domain.

RCC_APB2PeriphClockLPModeCmd(RCC_APB2Periph_SPI1, ENABLE);

add or change this too:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

-Kirem