JRDavisUF

Simultaneous SPI and SPI1

8 posts in this topic

I'm trying to get access to the second SPI bus/module in order to use both bus 0 and 1 at the same time on a MSP432 (Energia 18 / Code Composer) and ran into an issue with the best way to achieve this. Just to give a little background, I've got an SD card on bus 0 (Mode 0 SPI) and I'd like to put a ADC breakout on bus 1 (Mode 1 SPI) such that I don't need to worry the SD card clogging up the SPI lines when the 512 byte buffer gets written.

I am able to use:  SPI.setModule(1)  

to get access to the second SPI bus just fine...Should I be putting a set module command before the chunks of code which talk to SD or ADC drivers?  This didn't seem very elegant, but I think it might work.  Something like:

                 loop {

                  SPI.setModule(0)

                 Write to SD Card

                 SPI.setModule(1)

                 Get ADC conversion

                }

What I was hoping to do is use "SPI." (set to use bus 0) for the SD card libraries and "SPI1.*" (set to use bus 1) for the ADC libraries, however, SPI1 doesn't work.  What seems to be the problem with SPI1 is the following code in SPI.cpp:

/* C type function */
void spiTransferCallback(SPI_Handle spi, SPI_Transaction * transaction)
{
    SPI.transferComplete = 1;
}

Note how this code refers to "SPI.*" regardless of whether I'm using SPI.* or SPI1.*   What seems to be needed is some logic that executes SPI.transferComplete if the module is 0 and SPI1.transferComplete if the module is 1.  I tried changing this code to run SPI1.transferComplete, then atleast the SPI1.* stuff works...although I'm guessing SPI.* won't work at this point.  Is there a way to get this callback access to the value of spiModule?

any advice would be appreciated,

jrd

PS I'll also note that I'm using the original energia libraries (black), but the new boards (red).

 

Share this post


Link to post
Share on other sites

Just a quick thought to fix the SPI1 issue, maybe in SPI.begin something like

if spiModule==0

     params.transferCallbackFxn = spiTransferCallback0;

else

    params.transferCallbackFxn = spiTransferCallback1;

 

and then just have two transfer callback functions.  .I hate having to modify the core libraries, but I could see something like this working.

Share this post


Link to post
Share on other sites

This is definitely an issue and I would classify it as a bug / oversight. I'll ask the msp432 maintainer to take a look and report back.

Share this post


Link to post
Share on other sites

Please try the patch below for SPI.cpp. SPI.h remains unchanged.


--- SPI.cpp	2017-01-09 14:11:47.000000000 -0800
+++ SPI.cpp	2017-10-04 12:13:48.000000000 -0700
@@ -197,6 +197,7 @@
     transaction.txBuf = buffer;
     transaction.rxBuf = buffer;
     transaction.count = size;
+    transaction.arg = (void *)this;
     transferComplete = 0;
 
     /* kick off the SPI transaction */
@@ -263,6 +264,7 @@
     transaction.txBuf = &data_out;
     transaction.rxBuf = &data_in;
     transaction.count = 1;
+    transaction.arg = (void *)this;
     transferComplete = 0;
 
     /* kick off the SPI transaction */
@@ -324,5 +326,7 @@
 /* C type function */
 void spiTransferCallback(SPI_Handle spi, SPI_Transaction * transaction)
 {
-    SPI.transferComplete = 1;
+    SPIClass *c = static_cast<SPIClass*>(transaction->arg);
+
+    c->transferComplete = 1;
 }

 

Share this post


Link to post
Share on other sites

Thanks for the patch.   SPI1 now works standalone.

I'll try simultaneously SPI and SPI1 next.

Share this post


Link to post
Share on other sites

Thanks for the patch.   SPI1 now works standalone.

I'll try simultaneously SPI and SPI1 next.

Share this post


Link to post
Share on other sites

Simultaneous SPI and SPI1 works!  I've an SD card on SPI and a ADC breakout board on SPI1.  I've a small code to stream data from the ADC to the SD card and all looks good.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now