cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4XX SPI3 Data register is always 0xff and TXE is 1

Reema O
Associate II
Posted on January 22, 2018 at 19:55

I am trying to interface SPI3 of STM32F4with external SPI F-RAm memory chip. But after writing dummy data(0x00) in data register, data register is set to 0xff and it doest not change and my TXE bit is always set. Have i not initialize my GPIO port and SPI properly?

Her is my initialization

int main(void)

{

uint8_t flagstatus=255;

uint8_t last_position[4]; // tseting last saved position

uint8_t du_current_location=0;

init_USART(9600, PARITY_NONE);

USART_puts(USART1, '\n\r USART Initialize complete\n\r');

// GPIO initialization for SPI3

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

GPIO_InitTypeDef GPIO_InitStruct;

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOC,&GPIO_InitStruct);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;

GPIO_Init(GPIOC,&GPIO_InitStruct);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource10,GPIO_AF_SPI3);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource11,GPIO_AF_SPI3);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource12,GPIO_AF_SPI3);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;

GPIO_Init(GPIOA, &GPIO_InitStruct);

// SPI 3 initialization

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE);

SPI_InitTypeDef SPI_InitStruct;

SPI_InitStruct.SPI_DataSize=SPI_DataSize_8b;

SPI_InitStruct.SPI_CPOL=SPI_CPOL_High;

SPI_InitStruct.SPI_CPHA=SPI_CPHA_2Edge;

SPI_InitStruct.SPI_Mode=SPI_Mode_Master;

SPI_InitStruct.SPI_NSS=SPI_NSS_Soft;

SPI_InitStruct.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_64;

SPI_InitStruct.SPI_FirstBit=SPI_FirstBit_MSB;

SPI_InitStruct.SPI_Direction=SPI_Direction_2Lines_FullDuplex;

SPI_InitStruct.SPI_CRCPolynomial=7; // This need to check I dont need it

SPI_Init(SPI3, &SPI_InitStruct);

GPIO_SetBits(GPIOA, GPIO_Pin_15);

SPI_Cmd(SPI3,ENABLE);

GPIO_ResetBits(GPIOA, GPIO_Pin_15);

SPI3->DR=0x00; // this is dummy variable to initiate SPI communication

while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE)==1);

while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE)==0);

flagstatus=SPI3->DR;

SPI3->DR=0x05;

while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE)==1);

while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE)==0);

flagstatus=SPI3->DR;

GPIO_SetBits(GPIOA, GPIO_Pin_15);

SPI_Cmd(SPI3,DISABLE);

}
5 REPLIES 5
Jan Waclawek
Senior II
Posted on January 23, 2018 at 10:11

There's no readback of what you've written to SPI_DR - you are reading back the Rx value.

How did you observe TXE? You can hardly do it in a debugger, it changes too fast. Observe the waveform on SPI pins using an oscilloscope or logic analyzer.

JW

Posted on January 24, 2018 at 00:25

I dont understand what do you by read back Rx value, I thought with '

flagstatus=SPI3->DR' I am reading my receive data value.

Also on o-scope I am not getting any data on MISO

Can you please check if my Port initialization and SPI3 is correct?

Posted on January 24, 2018 at 00:27

To add to it my TXE is always set and my RXNE is always reset. So i am guessing that I am not initializing my SPI properly 

Posted on January 24, 2018 at 21:47

I dont understand what do you by read back Rx value,

Sorry, I thought you expect that you'll find in DR the same value as you've written to.,

I am not getting any data on MISO

If it's always at log.1, then it's okay to read all FFs from DR. It's the memory which is supposed to drive data to MISO.

Observe the waveforms/levels on all the pins of the memory - and by that I mean to put the tip of the probe directly at the pins of the memory - if they are as expected (compared to datasheet of the momory).

If in doubts with using the SPI, you should try a loopback first: disconnect your memory and connect MISO to MOSI. Then you should read out the same data as you've sent.

JW

Posted on January 26, 2018 at 01:32

Thanks Jw for your help.

II was combination of mistakes. I was not initializing my data properly as well as not read back the data in proper manner required by memory chip.

Corrected it and now its working fine