• Thr 2019.08.01

    Seeking technique suggestions here. Individually, manual toggle of lines allows correct result from one pass of setWatch().

    Continuation of threads:

    Espruino Wifi as USB Master, Pico as USB slave - is that possible?
    Does pinMode 'input' state place pin in tri-state mode

    Goal: After CS Chip Select goes low, on rising edge of SCK clock line, read state of MOSI

    Setup: Espruino MDBT42Q as master sending software SPI to Espruino WiFi as slave

    Data sent inside a setInterval() creating a CS pulse Low of ~200msec surounding 4 x 8bits of ~300usec clocked at 3usec:

    Limited code for brevity

    spi.setup( { mosi:D27, miso:D26, sck:D25, baud:100000 } );
      var val = spi.send([0xC0,0xF4,3,4]);

    Verified: Using logic analyzer and protocol decoder am able to see and verify CS, SCK, MOSI, MISO and data as placed on MOSI

    Individually, the setWatch() definitions execute as expected.

    // WiFi
    const MOSI = B5;  // pin 17
    const MISO = B4;  // pin 16
    const SCK  = B3;  // pin 15
    const CS   = B1;  // pin  7
    setWatch(function(e) { 
      // watchdog to prevent runaway
      if( countWatch >= 10 ) clearWatch();
      }, B3, { repeat:true, edge:'falling' }
    read:  0.00000286102
    calc:  SPI 100000 1usec pulse 2usec  500KHz

    The idea is to only allow the second setWatch() to run after tripped by the setWatch() monitoring CS

    var state;
    //Start first: setw()
    //Chip Select detect
    function setcs() {
      setWatch(function(e) { 
        // new attempt to signal 2nd setWatch()
        state = 0;
    //ABANDONED as second setWatch() always returns 0's
    //    var state = digitalRead( CS );
    //    if( !state )
    //      fetchBit();
        }, B1, { repeat:true, edge:'falling' }
    //    }, B1, { repeat:true, edge:'rising' }
    // Monitor clock line
    function setw() {
      setWatch(function(e) {
        // watchdog to prevent runaway
        if( countWatch >= 10 ) clearWatch();
    //  console.log(e.time-e.lastTime);
    //    var state = digitalRead( CS );
        if( !state )
    //    }, B3, { repeat:true, edge:'falling' }
        }, B3, { repeat:true, edge:'rising' }

    When I manually set the state of MOSI using

    function fetchBit() {
      var val = 0x00;
      val = digitalRead( MOSI );  // B5
      aryByteCmd[ elemBits++ ] = val;

    function fetchBit() reads and stores manually, correctly.

    Suspicion: A timing issue, starting technique or a different means to trigger the second setWatch() is needed.

  • Aside: You can pass in the CS pin to the send command like var val = spi.send([0xC0,0xF4,3,4], PIN_CS);

    Also, the setWatch docs say you can ask for the state of another pin, so you don't have to read it (might matter, as you don't have to spend the time to read it):

    // Advanced: If specified, the given pin will be read whenever the
    watch is called // and the state will be included as a 'data' field
    in the callback data : pin

    I don't completely understand the snippets, for example, you don't call setw from B1's setWatch, but that call should be there, right?

    Why not just set up one setWatch once on the SCK pin, and ignore if the CS is not low? Ok, depends on how many other slaves are there, and how much CPU the watch uses...

    So I guess you could

    • use 1 setwatch on the SCK pin, pass the MOSI in as data
    • in the watch, check the state of CS, if not 0, just exit
    • process the data as you wish :)

Avatar for AkosLukacs @AkosLukacs started