• var fs=require("fs");
    
    digitalWrite(A13,0);
    var g=require("PCD8544").connect(SPI2,C4,C5,A3, function() {
            g.clear();
            g.drawString("LCD OK",0,0);
            g.flip();
    });
    

    Note that I did not do anything to the SPI2 setup anywhere, under the reasoning that, since the SD card works after doing var fs=require("fs"), doing that must perform any necessary setup.

    The LCD works. The SD card does not:

    >fs.appendFile("asdasd.sad","Veni, vidi, vino");
    ERROR: Unable to write file : DISK_ERR
    =false
    

    If I don't set up the LCD, it works.

    I was under the impression that SPI interfaces could be shared, as long as each device had it's own pin to select that device (which is lowered to select that device, ie, nss_pin in spi.send()) - is this incorrect?

  • Yes, you're right - you should be able to share SPI. The only think I can think is that perhaps the SD card's CS pin gets left asserted, and so any further writes end up confusing it.

    What happens if you try accessing the SD card first with fs.readdir()? That should set up SPI as required for the card. Doing require('fs') doesn't automatically set anything up.

    Perhaps, if you don't set up SPI then it gets automatically set up in some way that it shouldn't be, and the fs module doesn't correct it.

    I've made a bug for this here: https://github.com/espruino/Espruino/issues/276

    As always, I've got a million things to do though so any help you could give that would help track this down would be good.

  • I've played with this a bit more....

    echo(1);
    
    var fs=require("fs");
    
    fs.readdir();
    
    fs.appendFile("before.txt","testtext");
    
    setTimeout("lcd();",1000);
    
    setTimeout('fs.appendFile("after.txt","testtext");',5000);
    
    var g=0;
    
    function lcd() {
      g=require('PCD8544').connect(SPI2,C4,C5,A3, function(){g.clear();g.drawString('LCD OK',0,0);g.flip();});
    }
    
    

    In this case, before.txt will be created just fine, and the file will have the correct content, and fs.appendFile() returned success - great.

    But the call after lcd() will fail - the file after.txt is created, but is empty.

    A few other things I've found:

    If you try to call appendFile() after setting up the LCD, on the same file as you just wrote to before setting up the LCD, it will report success, but nothing is appended to the file.

    You mentioned that the the CS pin might get left on, which I take to mean that the pin is left grounded when it shouldn't be (it goes low to select that device, right?). I assumed that the CS pin for SD card was D2, and tried digitalWrite(D2,1); before the call to lcd(); - but it didn't make any difference. Of course, I'm not certain I was doing the right thing here anyway.

  • Thanks - strange that "after.txt" actually gets created. I guess it's possible it's the other way around...

    Can you try your code, but then physically remove the LCD after it's been initialised and see if that changes anything? Not sure, but worth a try :)

    I actually just added software SPI to the latest Espruino (in Git - will be 1v60), so at least if this is blocking you, you can now run the PCD8544 off of any pins:

    var s = new SPI();
    s.setup({mosi: ... , sck: ... });
    g=require('PCD8544').connect(s, ... );
    
  • Woah! That software SPI sounds awesome! I assume the software SPI comes with a performance penalty?

    In any event, this wasn't a show stopper for me, at least not yet. I'm not sure I'll need the micro sd card for this project.

    I'll test whether it happens if I disconnect the LCD tonight.

  • Well, at the moment the penalty is surprisingly small - as the SPI.send command waits for a response anyway it doesn't really matter whether it's done by hardware or software.

    The only bad point is there's no baud rate support yet - it just goes as fast as it can, which appears to be good enough for the PCD8544 at least :)

  • I think I fixed this now. It was a bug in SPI - it'll be in 1v60

  • Yup, seems to be working now :-)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

SDCard fails to write if Nokia5110 is on SPI2 - expected or no?

Posted by Avatar for DrAzzy @DrAzzy

Actions