Forum Rule: Always post complete source code & details to reproduce any issue!
Page 4 of 4 FirstFirst ... 2 3 4
Results 76 to 93 of 93

Thread: SPISlave_T4

  1. #76
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    4,074
    2 slaves? use 1 or 2 SPI ports on master should work, the slave is fixed only to one bus

  2. #77
    Sorry, was unclear, I need the teensy to act as SPI slave to two different masters!

  3. #78
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    4,074
    that wouldnt be possible, u cant have 2 masters talking to one slave,

  4. #79
    as in you can't set up multiple LPSPIs as slaves? or just your library doesn't support it?

  5. #80
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,810
    SPI protocol lacks multi-master arbitration or other sharing mechanisms. How do you imagine 2 masters could possibly work on any SPI bus?

  6. #81
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    4,074
    this library setups only one LPSPI as slave. Even if you managed to setup 2 or more slave on LPSPI you'd need to be able to manage the 2 ports simuletaneously, and if interrupts are involved, good luck

  7. #82
    Quote Originally Posted by PaulStoffregen View Post
    SPI protocol lacks multi-master arbitration or other sharing mechanisms. How do you imagine 2 masters could possibly work on any SPI bus?
    Two seperate SPI busses, with the Teensy behaving as slave on both of them.


    Quote Originally Posted by tonton81 View Post
    this library setups only one LPSPI as slave. Even if you managed to setup 2 or more slave on LPSPI you'd need to be able to manage the 2 ports simuletaneously, and if interrupts are involved, good luck
    Don't most processors have some form of interrupt scheduling / something to manage two interrupts happing at the same time or close proximity?

    I did a timing check running one SPI Slave, and you could easily handle 4 SPI slaves theoretically within the time between the clock line going H->L and the time it begins to output the first bit from the byte.
    set output high as interrupt starts, then low as interrupt finishes, and it looks like below:
    Click image for larger version. 

Name:	Y6ou1F8.png 
Views:	2 
Size:	7.7 KB 
ID:	29119

    Code:
    void myFunc() {
      noInterrupts();
      digitalWrite(5, HIGH);
      //while ( mySPI.active() ) {
        if (mySPI.available()) {
          mySPI.pushr(l<<1);
        }
      //}
      digitalWrite(5, LOW);
      interrupts();
    }

  8. #83
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    4,074
    it all depends on how long your interrupts are, and the speeds of the SPI obviously especially when pushing data. if the slave interrupt is not ready to receive because of another interrupt you may lose data. Your example shows a single data transfer, if your purpose is to test a single byte/word vs arrays of data and processing without including other running code while 2 SPI are transferring streams of data, then this could be fine, otherwise something might happen, data may corrupt, and then people might complain about libraries not working without considering this

  9. #84
    Quote Originally Posted by tonton81 View Post
    it all depends on how long your interrupts are, and the speeds of the SPI obviously especially when pushing data. if the slave interrupt is not ready to receive because of another interrupt you may lose data. Your example shows a single data transfer, if your purpose is to test a single byte/word vs arrays of data and processing without including other running code while 2 SPI are transferring streams of data, then this could be fine, otherwise something might happen, data may corrupt, and then people might complain about libraries not working without considering this
    Well in this case, I'm only doing single data packet (26bit) transfers in response to a non-continuous clock signal. Basically using it to fake being a 24bit SSI encoder.

  10. #85
    Junior Member
    Join Date
    Feb 2019
    Posts
    11
    Quote Originally Posted by TCWilliamson View Post
    Sorry, was unclear, I need the teensy to act as SPI slave to two different masters!
    it's a curious system but I feel it should be possible. You'd need to refer to the datasheet and make your own copy of the library and adjust to use one of the other SPI peripherals.

    I copied the library to make some changes, a brief note about it here:

    https://forum.pjrc.com/threads/66389...l=1#post308305

    Look into the information about word and frame size. It is possible to trigger the interrupt only after the transaction is fully complete and the data is ready in the buffer. I don't see why it couldn't work with two masters because the buffer will be there and the interrupts will be serviced in order. However, this does require a deep dive into the datasheet and careful reading about the pin mux, etc.

    I'd be interested just out of curiosity what kind of system you are building that requires two masters connected to one peripheral system.

  11. #86
    Basically I'm not using the SPI as SPI, but faking the SSI protocol. for an 24 bit SSI comms, it turns out if you set the SPI bus to 26 bits (n+2) and then shift the data 1 to the left (data<<1) it basically works as SSI encoder protocol.

    I'm using the teensy as a master to control two servo drives that are e-camming to the SSI encoder input on their second encoder port. It works *really well* compared to step and direction, allowing you to do the slow movements, and also do high speed movements, without the issue of pulse frequency limits (very high resolution means slow movement limit, or if you want high speed with pulse/direction you need to decrease the resolution)

    an SSI encoder recieves pulse and then outputs data in sequence with it.
    https://en.wikipedia.org/wiki/Synchr...rial_Interface

    I wonder if there is a better implementation method... clock frequency is up to 1.5MHz in my case, though it only requests position update at 10-20kHz. Still, that's close to the PWM frequency of a servo drive, so why does it need to update faster then that, haha.

  12. #87
    Junior Member
    Join Date
    Feb 2019
    Posts
    11
    interesting but I don't know if I fully understand. is the servo drive the master for two SSI channels and it thinks there are 2 encoders (slave) ?

    but you are doing something where there are not encoders but you want to create the signals for the servo drive - to get it to do what you want?

  13. #88
    I'll go through the whole thing, might be a lot of stuff you already know, but clarity is nice for other readers:

    So basically a Position Loop of a servo drive has two numbers it cares about [Actual Position] and [Target Position]
    It runs a whole load of maths and moves the motor, optimizing to make the [Actual Position] as close to [Target Position] as possible.

    the [Actual Position] is typically read by an encoder on the motor / load, e.g. Quadrature Incremental, Sin-Cos Analog or Absolute (SSI, BiSS-C, even SPI) to name a few.

    the [Target Position] can be set by a huge range of ways:
    many servo drives support writing programs to change the target position based upon timing or i/o.
    sometimes you want to use an analog input, and scale / reference the position to say +/-10V = +/-1m from reference. (wouldn't personally recommend this for noise reasons - generally works better in the velocity loop with position fed back to the 'controller'
    other times you command the position over fieldbus, like EtherCAT or CANOpen, Ethernet/IP or other such ones.

    You can also link some servo drives (such as the ones I have) to follow an encoder input, so you could time one conveyor belt, or a rotating blade to a conveyor belt for example.

    In my case, as a low latancy, fast way to link a servo drive to a low cost controller (Teensy) my plan is to make the Teensy pretend to be the encoder, which, in this case, runs an SSI protocol.
    The great thing about this, is as opposed to step/direction, you can't loose steps, or find youself not able to generate them fast enough (also very computationally intensive to calculate it out and output the Pulse steps)

    as a result I'm able to get the [Target Position] in at upwards of 10-20kHz, far, FAR faster then any fieldbus, and with far less computational resources. You'd just have to add a small bit of I/O to handle drive enable, homing etc. (or use a weak implementation of the fieldbus for that)

  14. #89
    Junior Member
    Join Date
    Aug 2022
    Location
    Seoul
    Posts
    3
    I'm trying to test with Teensy 4.1 SPI Master and Teensy 4.1 SPI Slave communication, but SPI communication is not working.

    Environment is
    OS: Windows 11
    Arduino 1.8.19
    Library: Teensyduino 1.56

    After downloading as zip from https://github.com/tonton81/SPI_MSTransfer_T4.git, I added the library in Arduino.

    In Master Monitor of Teensy 4.1 one

    4784114
    State: 1

    Detected slaves:
    No slaves detected, check connections.

    4785116

    SPI Master Teensy 4.1 is output on the serial monitor as above.
    Slave Teensy 4.1 prints millis:925709 on the serial monitor.

    The SPI pins were connected 1:1 between the #1 Teensy 4.1 board and #2 Teensy 4.1 board.

    Master to Slave
    Teensy 4.1 to Teensy 4.1
    CS - 10 to CS - 10
    MOSI-11 to MOSI-11
    MISO - 12 to MISO - 12
    SCK - 13 to SCK - 13

    The example is the same as git repository, so I didn't upload it.

    Could you please confirm? and give me the solution?

  15. #90
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    4,074
    can you try SPI_MSTransfer_T4 library example, that should work just for sanity check, because if it doesn't, it's most likely a connection or cross-wire issue.
    check if the demo works on both your T4's and let me know

  16. #91
    Junior Member
    Join Date
    Aug 2022
    Location
    Seoul
    Posts
    3
    I have tried SPI_MSTransfer_T4 library example, checked the sanity check, and checked the connections.

    When I check it with an oscilloscope, it shows the waveform keeps disapearing(gone) and appearing repeatedly with a period of 1 second according to the source code.

    Test Code as below:
    same as git of SPI_MSTransfer_T4 library example


    <MASTER.ino>
    #include "SPI.h"

    #include "SPI_MSTransfer_MASTER.h"
    SPI_MSTransfer_MASTER<&SPI, 10, 0x1234> mySPI1234;
    SPI_MSTransfer_MASTER<&SPI, 10, 0x4567> mySPI4567;

    void setup() {
    Serial.begin(115200);
    SPI.begin();
    pinMode(10, OUTPUT);
    digitalWrite(10, 1);
    mySPI1234.begin();
    mySPI4567.begin();
    }

    void loop() {

    static uint32_t t = millis();
    if ( millis() - t > 1000 ) {
    Serial.println(millis());

    uint16_t buf[5] = { 0xF1, 0xF2, 0xDEAD, 0xF4, 0xBEEF };
    uint16_t buf2[5] = { 0xBEEF, 0xF7, 0xF8, 0xF9, 0xDEAD };
    mySPI1234.transfer16(buf2, 5, random(0x1000, 0x8000));
    mySPI4567.transfer16(buf, 5, random(0x1000, 0x8000));

    static bool flip = 0;
    flip = !flip;
    mySPI1234.digitalWrite(6, flip);
    // mySPI1234.pinMode(5, INPUT);
    bool moo = mySPI1234.digitalRead(6);
    Serial.print("State: "); Serial.println(moo);
    mySPI1234.detectSlaves();

    mySPI1234.pinMode(5, INPUT);
    t = millis();
    }

    }


    SPI_MSTransfer_SLAVE

    #include "SPI_MSTransfer_T4.h"
    SPI_MSTransfer_T4<&SPI, 0x1234> mySPI;

    void setup() {
    Serial.begin(115200);
    mySPI.begin();
    mySPI.onTransfer(myCB);
    }

    void loop() {
    mySPI.events();
    static uint32_t t = millis();
    if ( millis() - t > 1000 ) {
    Serial.print("millis: "); Serial.println(millis());
    t = millis();
    }
    }

    void myCB(uint16_t *buffer, uint16_t length, AsyncMST info) {
    for ( int i = 0; i < length; i++ ) {
    Serial.print(buffer[i], HEX); Serial.print(" ");
    }
    Serial.print(" --> Length: "); Serial.print(length);
    Serial.print(" --> PacketID: "); Serial.println(info.packetID, HEX);
    }

    Master Serial Monitor

    1484378
    State: 1

    Detected slaves:
    No slaves detected, check connections.

    1485380
    State: 1

    Detected slaves:
    No slaves detected, check connections.


    Slave Serial Monitor

    millis: 1427713
    millis: 1428714
    millis: 1429715
    millis: 1430716
    millis: 1431717
    millis: 1432718
    millis: 1433719
    millis: 1434720
    millis: 1435721
    millis: 1436722
    millis: 1437723
    millis: 1438724
    millis: 1439725
    millis: 1440726
    millis: 1441727
    millis: 1442728
    millis: 1443729


    Connections

    Master to Slave
    Teensy 4.1 to Teensy 4.1
    CS - 10 to CS - 10
    MOSI-11 to MOSI-11
    MISO - 12 to MISO - 12
    SCK - 13 to SCK - 13
    GND to GND

    <picture>
    Attachment 29261

    <oscilloscope>
    Ch1(CS)_Ch2(MOSI)_Ch3(MISO)_Ch4(SCK)
    Attachment 29263

    the waveform in detail
    Ch1(CS)_Ch2(MOSI)_Ch3(MISO)_Ch4(SCK)
    Attachment 29262

    Click image for larger version. 

Name:	Ch1(CS)_Ch2(MOSI)_Ch3(MISO)_Ch4(SCK)_2.png 
Views:	2 
Size:	35.8 KB 
ID:	29264
    Last edited by ysd375; 09-01-2022 at 12:30 AM.

  17. #92
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    4,074
    have you tried swapping around the MISO and MOSI wires?

  18. #93
    Junior Member
    Join Date
    Aug 2022
    Location
    Seoul
    Posts
    3
    Yes I tried swapping around the MISO and MOSI wires, too.
    MOSI-11 to MISO - 12
    MISO - 12 to MOSI-11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •