MD_MAX72xx using HW SPI on Teensy

Status
Not open for further replies.

SteveBar

Well-known member
Hi All,
I'm trying to get the MD_MAX72xx lib to use HW SPI2 on Teensy 4.1. (SPI1: MOSI1=26, SCK1=27, CS1=0 or 38)

Using the 5 param call results in a BIT Bangged "SPI" (not the goal) which works, but NOT via HW SPI...

Code:
    #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
    #define MAX_DEVICES	 4
    #define CLOCK_PIN       27
    #define MOSI_PIN         26
    #define CS_PIN_MATRIX  0 // or 38
    
    MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, MOSI_PIN, CLOCK_PIN, CS_PIN_MATRIX, MAX_DEVICES);

from MD_MAX72xx.cpp (line ~328)
Code:
else  { // not hardware SPI - bit bash it out
    for (uint16_t i = 0; i < SPI_DATA_SIZE; i++)
      shiftOut(_dataPin, _clkPin, MSBFIRST, _spiData[i]);
    }

using the 3 param call supposed to get HW SPI (is the goal) which does not work...

Code:
    #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
    #define MAX_DEVICES	 4
    //#define CLOCK_PIN       27
    //#define MOSI_PIN         26
    #define CS_PIN_MATRIX  0 // or 38
    
    MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN_MATRIX, MAX_DEVICES);

from MD_MAX72xx.cpp (line ~323)
Code:
  if (_hardwareSPI) {
    for (uint16_t i = 0; i < SPI_DATA_SIZE; i++)
      SPI.transfer(_spiData[i]);
  }

I added a few comments in the MD_Max code and SPI.cpp to monitor the type and use of the SPI. It shows (when using the 3 param call) that HW SPI is being used by the MD_Max lib but the SPI is using MISO: 12 MOSI: 11, SCK: 13.
MD_MAX72XX::begin _hardwareSPI=1
MD_MAX72XX::ARDUINO SPIbegin _hardwareSPI=1
SPIClass::SPI MISO: 12 MOSI: 11, SCK: 13

Questions:
1) How can I get the MD_Max and SPI libs to use the SPI2 of the T41?
2) Where/How does MD_Max and SPI libs "know" which MISO, MOSI, & SCK pins to use when I send it the CS pin only? I assume it knows that its a T41 etc...

Thanks
Steve
 
Can't seem to find the edit button...
I want to make clear I should have written "SPI1" (not SPI2), which is the 2nd SPI port. You'll can tell I'm an engineer (starts counting at 1), not a SW dev guy (starts counting at 0) ! :)
 
I'm not familiar with that library. Is it this one? (from a quick search)

https://github.com/MajicDesigns/MD_MAX72XX


ps: forum messages are editable for only 2 hours, mostly as an anti-spam measure, but also because we've had cases were people edited or deleted their messages after others replied, leaving a confusing mess for anyone who later finds the conversation looking for a solution to a similar problem.
 
This guy...
8x8x4LEDMatrix.jpeg
 
In Conclusion:
To use SPI1 in the MD_MAX72xx lib do this...
Search & Replace of "SPI." with "SPI1." in the file MD_MAX72xx.cpp.
I did it and got great results!
BTW the CS pin can be something other than 0 or 38 (I used 7, cause the HW board layout had it wired that way).
NOTE: this silly 4x 8x8 LED martix PCB needs to be rotated 180 degs to work! The input pins should be on the right side and silk screen text upside down!!! (YUP)

Marco Colli (lib author) also confirmed this strategy... (thanks Marco!)
>> Looks like the answer you got is pretty much the solution. It used
>> the SPI object from the library so the pin mapping is done outside
>> the library itself.

Cheers
Steve
 
Status
Not open for further replies.
Back
Top