cancel
Showing results for 
Search instead for 
Did you mean: 

SPI buffer zero reading : 1 line rx only in master mode

williamchevallier
Associate II
Posted on April 30, 2014 at 17:03

Hello all,

I am strugling with an spi communication between my ''stm32f3discovey '' and a resolver chip AS5311.

Took me some time but I managed to make the clock and the communication working has I can see on my scope. Only problem is despite good  transmission, when i read the buffer, it return only zeros.

Here is my program :

void init_spi_spi2(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

SPI_InitTypeDef SPI_InitStructure;

/* Enable SCK, MOSI, MISO and NSS GPIO clocks */

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

/* Enable the SPI periph */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

/* GPIO SPI port init structure */

/* SPI NSS pin configuration */

GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_5);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;

GPIO_Init(GPIOB, &GPIO_InitStructure);

/* SPI SCK pin configuration */

GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_5);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;

GPIO_Init(GPIOB, &GPIO_InitStructure);

/* SPI MISO pin configuration */

GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_5);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//GPIO_Mode_AF

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;//GPIO_OType_PP

GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;//GPIO_PuPd_UP

GPIO_Init(GPIOB, &GPIO_InitStructure);

/* SPI  MOSI pin configuration */

// GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_6);

// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;

// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

// GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;

// GPIO_Init(GPIOB, &GPIO_InitStructure);

/* SPI configuration */

SPI_I2S_DeInit(SPI2);

SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx;

SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;

SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStructure.SPI_CRCPolynomial = 1;

SPI_Init(SPI2, &SPI_InitStructure);

SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF);

SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);

/* Configure the SPI interrupt priority */

NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

void begin_spi_transmission(void)

{

/* Enable SPI com for data retrieval */

dataCounter = 0;

SPI_NSSInternalSoftwareConfig(SPI2, SPI_NSSInternalSoft_Set);

SPI_SSOutputCmd(SPI2, ENABLE);

SPI_Cmd(SPI2, ENABLE);

}

void SPI2_IRQHandler(void)

{

static uint8_t temp = 0;

/* SPI in Master Receiver mode */

temp = SPI_ReceiveData8(SPI2);

//temp = SPI_I2S_ReceiveData16(SPI2);

//temp = SPI2->DR;

printf(''%u\n'', temp);

/* The disable control must occur just between the sampling time of the first */

/* bit of the last received frame and first bit of next (unwanted dummy) data frame. */

if (dataCounter == (spiMessage2->numberOfdata-1))

SPI_Cmd(SPI2, DISABLE);

/* there is no more data to receive */

if (dataCounter >= spiMessage2->numberOfdata) {

STM_EVAL_LEDOff(LED7);

/* we need to shut down the SPI com */

SPI_NSSInternalSoftwareConfig(SPI2, SPI_NSSInternalSoft_Reset);

SPI_SSOutputCmd(SPI2, DISABLE);

//SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, DISABLE);

if (spiMessage2->spiSuccessCallback != NULL)

(*spiMessage2->spiSuccessCallback)();

del_voidCircularBuffer_bufferHead(spiBuffer);

}

}

Thanks for your time.

tags :

SPI_Direction_1Line_Rx

rxonly

interrupt

SPI_Mode_Master

zero

#spi-spi_direction_1line_rx
9 REPLIES 9
stm322399
Senior
Posted on April 30, 2014 at 17:42

Your MISO line mode must be AF and not IN. Normally no pull-up nor pull-down, unless strictly required by your SPI device.

Sorry, I missed that you were in bidirectional mode.

I don't know how it works, but I assume that you must communicate using MOSI (should be in AF mode anyway), and that you must use the function SPI_BiDirectionalLineConfig to turn MOSI as the input. Last I suppose that you still must write something to DR in order to activate data reception.

stm322399
Senior
Posted on April 30, 2014 at 17:45

Of course there is a piece of code you did show us that sends bytes over the SPI, otherwise you wouldn't have seen anything on your scope.

williamchevallier
Associate II
Posted on May 01, 2014 at 02:24

Hi Laurent, 

No there is no part of the code missing.

As described in the STM32F3 reference manual RM0316, page 724. SPI in simplex communications, RXONLY=1, in master mode MOSI output is disable.The clock signal is generated continuously as long as the SPI is enabled

Enabling the SPI (with is done in function : begin_spi_transmission) start the clock, with the combination of slave select pin activation, the slave starts to send data to the master. That much I can see on my scope, so I am sure of.

Also my interrupt activates only when Rx buffer is ''quater full'', and it does, so the RX buffer is been fill with data.

But for a reason I don't understand, when I try to retrieve data from buffer, all I get is 0x00.

Thanks

Posted on May 01, 2014 at 06:28

Well definitely want the AF mode for the pins.

Really not much in the way of examples for the 1-line modes, in this or comparable parts. I don't have any driver to manifest one.

Consider if you can test/evaluate in the 2-line mode, and pump the Tx side with junk data. Confirm if the Rx side works.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
williamchevallier
Associate II
Posted on May 01, 2014 at 12:04

Hi clive.

Yes, I too couldn't manage to find an example where stm32 in RXonly, 1 line, master.

I allready tried whis MISO pin in ''AF'' or ''IN'', also tried ''OD'' ''PP'', ''UP'' and ''DOWN'', doesn't seems to have any good effect.

You are right, I will a 2 line mode to make sure Rx works.

But ultimately I would really like to make 1 line RxOnly work.

Thanks

stm322399
Senior
Posted on May 01, 2014 at 16:52

Getting the interrupt is a very good sign.

If you constantly read 0x00 from FIFO, youmust focus to the*wiring* of the data line.

It uses MISO (I told you MOSI because it's true on F4, but it's wrong on F3), that must be configured AF (for sure), and probably PP (normally an input is not impacted by PP or OD), and NOPULL as well (generally there is no pull-up or pull-down design in SPI as it limits the speed). AF on AF_5 looks good, clocks looks good.

Last thing you can focus is the sampling, check (or try and test) values of CPHA and CPOL (read diagram from the SPI slave datasheet), bad configuration here is a frequent reason of non-working SPI.

williamchevallier
Associate II
Posted on May 01, 2014 at 19:14

I also tried AF_6, but same result.

I tried every combination of SPI_CPOL & SPI_CPHA, but none works. Also, even if there were wrong I would be getting something, not 0x00 right?

I stamble on something page 729 :

''These FIFOs are used in all SPI modes except for receiver-only mode (slave or master) with CRC calculation enabled''

Even if I thing CRC is disable by default, I tried  to ad the following to my configuration :

''SPI_CalculateCRC(SPI2, DISABLE);''

But It didn't made any change.

I really don't get whats wrong...

Still need to try clive idea to make sure there is no problem with the SPI.

Thanks

Posted on May 02, 2014 at 08:30

At this point, I would try to bit-bang...

wek

williamchevallier
Associate II
Posted on November 03, 2014 at 18:52

Hi,

I was off for some time.

Sorry I haven't the solution to this particular problem.

But I have managed to make my SPI working by changing ''SPI_Direction'', to :

''SPI_Direction_2Lines_RxOnly'' instead of ''SPI_Direction_1Line_Rx''

Thanks for all your help.

++