Using multiple spi hardware controllers on teensy

Status
Not open for further replies.

Welocs

Well-known member
Hi there!

I'd like to use more than one SPI hardware controller of the teensy (or even arduino), but i don't find any information about how to do it, or if it is implemented in the spi library.
AND - NO! I don't want to connect all my spi devices on one spi controller, i really want to use different controllers...
when searching 4 this, you only find titles like "how to use multiple slave on spi" or similar stuff, but i'd like, as my title says, to use multiple spi hardware controllers.
pls help =)

greetings,
welocs
 
I am assuming you mean you wish to have multiple SPI busses on the same board? Where you can put some of your devices on one bus and put some on another...

You can do this with several teensy boards. Summary of what each teensy has is on: https://www.pjrc.com/teensy/techspecs.html

Teensy LC - has 2 SPI busses, T3.5 and T3.6 have 3.

You can use these by connecting your devices up to the correct pins. Most of the pins are shown on cards that come with the teensy. https://www.pjrc.com/teensy/pinout.html

So for SPI1 you can find MOSI1, MISO1, and SCK1...

Software wise you can use the SPI library. To talk to the three busses on T3.6 for example, you talk to the three objects: SPI, SPI1, SPI2

The difficult part is most libraries that talk to SPI devices are hard coded to only talk to the SPI object. So you may need to edit some of these. With the last couple releases of Teensyduino, we have made the SPI objects all instances of one class, which is the same as has been done for Arduino Due/M0, so hopefully over time we can update libraries where you can do something like pass in a reference to which SPI object to use.
 
hi kurte,

thanks for your reply!

so, if i understand this correctly, i just can code like this?:
Code:
// begin, settings and transfer for SPI0
SPI.begin(SPISettings());
SPI.transfer();

// begin, settings and transfer for SPI1
SPI1.begin(SPI1Settings());
SPI1.transfer();
 
Yes... Almost....

That is, it is SPI.beginTransaction(SPISettings(...)) There is a separate SPI.begin() and SPI1.begin() which initializes the SPI setup and initializes the associated pins. the beginTransaction is used to then make sure you have the right settings (speed, mode, MSB/LSB) for the device you are about to talk to.... You then end that conversation to the device with the SPI.endTransaction()
 
so i don't need SPI.begin(), i just can use SPI.beginTransaction() for SPI0 and SPI1.beginTransaction() for SPI1, right?
 
Yes you need SPI.begin, which in most cases goes in your setup() function. The beginTransaction/endTransaction calls are typically used to bracket the spi transfer functions used to talk to a specific device. This is setup such that you can have multiple devices on the same buss, where each of the devices might have different settings, like what speed they can at.

You might try looking at some of the example programs, like: file->Examples->SPI->DigitalPotControl which shows how to talk to a device... Although I see a mistake in it!

That is in it's setup:
Code:
void setup() {
  // set the slaveSelectPin as an output:
  pinMode (slaveSelectPin, OUTPUT);
  pinMode (slaveSelectPin, HIGH);
  // initialize SPI:
  SPI.begin(); 
}
The second pinMode should be: digitalWrite...

Will check current sources and do Pull request
 
yes, i know the examples, but there's no example of using multiple SPI cores.

ok, thank you very much for your answer.
This seems to be beginner stuff, but when you are often on different systems and languages it's not the problem about the logic, it's more the problem about missing documentation or the missing knowledge of Syntax...
Sometimes it takes more time to find out how to write something than writing it by yourself...one of the best documentations i've ever seen is the one of php.net.
 
Most of my examples using multiple SPI ports are a little more involved. That is for example I have a version of the Teensyview (SSD1306) display driver code that allows me to specify which SPI bus a device is on... Actually it also can guess as you pass in the MOSI and SCK pins to constructor and from that it can deduce... I then have example program which allows up to one of these displays per buss. So on T3.6 or T3.5 I have had test setup to run 3 of them, and with this I am using the new SPI transfer code, which is in the very recent beta Teensyview release, which allows Asynchronous SPI calls, such that I have all three displays updating at the same time.

Edit: @Paul - I put in Pull request for bug in SPI example program
 
Status
Not open for further replies.
Back
Top