Page 1 of 2

Back to back SPI transfers

Posted: Wed Oct 10, 2012 10:35 am
by talsit
Hi there!

I'm running on a STM32F4, first on the discovery board, then on a modified FEZ40 (SPI pins brought out), and I'm trying to understand a couple of things with ChibiOS, since I'm still a total newbie on this.

I'm trying to do back to back SPI transfers, so start a new one as soon as one finishes. Note, this has little practical use, I'm just trying to gauge how things work.

In a thread:

Code: Select all

   while (TRUE) 
   {
      palSetPad(LED2_PORT, LED2_PAD);
      spiAcquireBus(spid);
      spiSelectI(spid);
      spiStartSendI(spid, sizeof(buf), buf);
   }


In the SPI configurations, there's a callback:

Code: Select all

   
   chSysLockFromIsr();
   spiUnselectI(spip);
   spiReleaseBus(spip);      
   chSysUnlockFromIsr();


And it crashes in spiReleaseBus(), in chMtxUnlock(), ending in a _unhandled_exception.

I imagine that this is not the way to do it, and wanted feedback on what the best way to go around it. I imagine that a better solution would be to call spiPolledExchange(), but it's more of a question on how I can reasonably manage 2 threads, to get one to start an SPI transfer once another finishes...

Re: Back to back SPI transfers

Posted: Wed Oct 10, 2012 11:05 am
by Giovanni
Hi,

From interrupts you can only call I-class functions, spiReleaseBus() is not of that kind.

Note that spiAcquireBus() and spiReleaseBus() are only required for mutual exclusion if you have multiple threads accessing the SPI bus, if this is not the case just remove them.

If you have multiple threads then consider using synchronous functions without callbacks.

Giovanni

Re: Back to back SPI transfers

Posted: Wed Oct 10, 2012 11:27 am
by talsit
Ok, thanks. I think things are starting to fall in to place. (And thanks for the incredibly quick answer)

Are all callbacks interrupts?

Also, from what I'm seeing, all 3 SPI's on the STM32F4 are 100% independent, correct? I just tried doing interleaved transfers on SPI1 and SPI2, and didn't see any CLK/MOSI/MISO traffic of SPI1 on the SPI2 bus. Does this mean that the STM32F4's don't support a single SPI bus (SCK, MOSI, MISO) but with different CS pins for different devices? I suppose that I could just change the "ssport" and "sspad" on the SPIConfig struct every time I need to address a different device. But what happens if my 2 devices need different polarity or phase?

Re: Back to back SPI transfers

Posted: Wed Oct 10, 2012 11:41 am
by Giovanni
All callbacks are called from ISR context.

The 3 SPIs are absolutely independent.

If you need to share a single SPI for multiple peripherals just create a configuration structure for each peripheral (with a different CS pin and SPI parameters) and call spiStart() when you need to change mode. Note that in order to call spiStart() you don't need to call spiStop() first, it just reconfigures the SPI parameters in that case.

spiStart(&SPID1, &config1);
spiSelect(&SPID1);
....
spiUnselect(&SPID1);
spiStart(&SPID1, &config2);
spiSelect(&SPID1);
....
spiUnselect(&SPID1);

etc

Giovanni

Re: Back to back SPI transfers

Posted: Wed Oct 10, 2012 2:38 pm
by talsit
Oh.... that's way more useful that I originally had thought it would be!

Re: Back to back SPI transfers

Posted: Thu Oct 11, 2012 3:58 pm
by talsit
I've gotten a bit more of a hang of this, I'm transferring data correctly, it's all going great, but after a bit - anywhere between 30 seconds and 2 minutes - I get an "_unhandled_exception". I know I get that because my LED's stop blinking, and if I break the debugger, that's where PC points to. The callstack is also empty.

I would like to know what would be the typical way of debugging where this unhandled is coming from? I'm using IAR starter. Putting a breakpoint on that does nothing.

Any hints or tricks I should try out?

Re: Back to back SPI transfers

Posted: Thu Oct 11, 2012 4:09 pm
by Giovanni
Run with debug options eabled, probably something would be trapped.

I would bet on a stack overflow in some thread or in the IRQ stack. Try to increase sizes.

Giovanni

Re: Back to back SPI transfers

Posted: Thu Oct 11, 2012 4:47 pm
by talsit
By "something would be trapped", what do you mean? I've tried putting a breakpoint in port_halt(), but it never hits it.

BUT, just digging through, you were right! I was reading 20 bytes into a 16 byte buffer.

Thank you!!!

Re: Back to back SPI transfers

Posted: Tue Mar 19, 2013 10:15 am
by talsit
Giovanni wrote:All callbacks are called from ISR context.

The 3 SPIs are absolutely independent.

If you need to share a single SPI for multiple peripherals just create a configuration structure for each peripheral (with a different CS pin and SPI parameters) and call spiStart() when you need to change mode. Note that in order to call spiStart() you don't need to call spiStop() first, it just reconfigures the SPI parameters in that case.

spiStart(&SPID1, &config1);
spiSelect(&SPID1);
....
spiUnselect(&SPID1);
spiStart(&SPID1, &config2);
spiSelect(&SPID1);
....
spiUnselect(&SPID1);

etc

Giovanni


Sorry to dig this up, but I've only recently come back to the project.

I only need to call spiStart(); if the config is different, correct?
I can test for it like this:

Code: Select all

if ( SPID1.config != &config1 )   
   spiStart(&SPID1, &config1);
spiSelect(&SPID1);
// transfer to device #1
spiUnselect(&SPID1);

// new peripheral
if ( SPID1.config != &config2 )   
   spiStart(&SPID1, &config2);
spiSelect(&SPID1);
// transfer to device #2
spiUnselect(&SPID1);


The reason I'm asking is because I'm seeing a little "glitch" on MOSI just before a transfer (about 1.4us) of bytes (green is MOSI, yellow is SCK):
Image

And I'm wondering if the re-configuring of the SPI could be triggering that. I understand that if the peripheral is correctly implemented, it shouldn't respond to things that are outside the SCK, but I know for a fact that this device I'm interfacing to is dodgy as hell. I have noticed that I get some bad state happening "near" these MOSI glitches.

Re: Back to back SPI transfers

Posted: Tue Mar 19, 2013 10:20 am
by Giovanni
Is the glitch caused by the repeated spiStart() or by the transfer itself?

Anyway, a glitch with CS not asserted should not cause problems.

Giovanni