• Hi

    I am trying to use Espruino Pico to extract data from the IMU(LSM6DSL). In order to configure the IMU, I need to configure by writing and reading the value to the register. However, from the espruino library, I am not very certain how to write and read the value to the IMU registers. I am wondering if anyone has done something similar? Thank you very much for your help.

  • In most modules, there is a r and w function for low level access to the device. I think you can use those.

  • Hi AkosLukacs,

    Thank you for your help.
    Sorry that I did not make the question very clear, I am trying to use SPI to be the communication protocol.
    and I am having some issue using the Espruino Javascript library to read and write the register.

    Thank you

  • Ooops, missed the SPI part, sorry.
    If you search for SPI in the docs, you get all the modules that use SPI, and you can look at the code.

    For example from the LIS2DH12 that has SPI interface as well (at the bottom of the file):

    exports.connectSPI = function(spi,cs,options) {
      if ("function"==typeof options)
        throw new Error("Use require(LIS2DH12).connectSPI(..., {callback:function() { ... }} instead");
      return new LIS2DH12(function(reg,len) { // read
        return spi.send([reg|0xC0,new Uint8Array(len)], cs).slice(1);
      }, function(reg,data) { // write
        return spi.write(reg, data, cs);
      },options);
    };
    

    The interesting part:

    // reg - the register address
    // len - how many bytes to read
    function read(reg,len) {
        return spi.send([reg|0xC0,new Uint8Array(len)], cs).slice(1);
     }
    
    // reg - the register address
    // data - the data you want to send
    function write(reg,data) {
        return spi.write(reg, data, cs);
      }
    
  • I think @AkosLukacs has hit it on the head. SPI should be pretty straight forward. The main gotchas are:

    • The register is usually sent as the first byte
    • When you read, you need to send the register and then another byte (usually 0 or 255). The first byte you get back is then basically garbage and what comes after is the data.
    • Often devices set the top bit of the register to indicate a read or write (so reg|0x80 - the code above uses 0xC0 which I think is specific to the LIS2DH12. You'd have to check your chip's datasheet.

    If you post the code you have up here we might be able to help you a bit more.

  • Hi all,

    I am fairly new to javascript and Espruino.

    I just started to implement the SPI, but the code does not seem to recognize my SPI. write function. I think it was because some header files were missing, but I am not sure what and how to include them.

    Except for the SPI, I am having some trouble finding these function's library

    function to convert decimal to binary
    fopen,fclose -- (file open, file write, file close)
    getTime

    Thank you very much for your time


    3 Attachments

    • Screen Shot 2019-10-13 at 1.01.22 AM.png
    • Screen Shot 2019-10-13 at 1.01.39 AM.png
    • Screen Shot 2019-10-13 at 1.01.48 AM.png
  • You have a typo in the first line: it should be var start = new Date(), not new Data() :)

  • Sun 2019.10.13

    for @AkosLukacs 'You have a typo in the first line:' Good catch but;

    Should also be var d = new Date() in order for second line to be valid ;) ;)


    @user101931 See: http://www.espruino.com/Pico+Clock#line=­3,11
    or any of the examples beneath the Date constructor topic in the 'Reference' page link that follows

    This will be of help using the Date functions:

    http://www.espruino.com/Reference#Date
    http://www.espruino.com/Reference#l_Date­_now

    Binary to Decimal, can't go wrong with the tutorials at W3Schools:

    https://www.w3schools.com/js/
    https://www.w3schools.com/js/tryit.asp?f­ilename=tryjs_bitwise_convert


    'the code does not seem to recognize my SPI. write function'

    Is it possible that SPI is not being used correctly concept-wise? (for read write)

    http://www.espruino.com/SPI
    http://www.espruino.com/Reference#SPI

     

    'I think it was because some header files were missing, but I am not sure what and how to include them'

    Locate the heading 'constructor SPI' from the 'Reference' page link above and click on the right facing arrow adjacent to that heading. This will link to:

    https://github.com/espruino/Espruino/blo­b/master/src/jswrap_spi_i2c.c#L55

    showing that SPI is part of the firmware build and no other 'includes' are needed.

    Would you provide the link to the source code or tutorial that is being worked from please.


    *If* this is the correct data sheet:

    https://www.st.com/resource/en/datasheet­/lsm6dsl.pdf

    p.40 p.42 it appears the address and data are required to be sent simultaneously as 16 bits.

    Data byte missing?

    Chip Select 'Actually' going lo and not inadvertently inverted?

    Does the Pico MOSI actually go to the inertial device MOSI and not flipped with MISO?


    It appears the inertial module is programmed very similar to the GPS device. This link:

    http://forum.espruino.com/conversations/­332295/#comment14688770

    might provide some insight on how I solved communication using the FX2 Logic Analyzer. Saved a ton of time rather than just guessing. . . .

  • Hi - I think it might be best to start out from some existing SPI code like http://www.espruino.com/SPI and work from there...

    The code you've written seems to have a few issues (like :integer= not being valid JS) and I think you'll be banging your head against a wall for quite some time just trying to stop it erroring.

    For SPI, the issue is you're using SPI which is the class name for an SPI peripheral, but isn't an SPI peripheral itself. Try changing it for SPI1 - the first SPI peripheral - and it should be fine.

    In terms of your other stuff - try using the search box at the top right of Espruino.com and specifically the reference at http://www.espruino.com/Reference:

    function to convert decimal to binary

    If you want to convert a number to a binary string, just use ...toString(2) - eg (42).toString(2)

    ... but you don't need to convert anything to binary to send it over SPI

    fopen,fclose -- (file open, file write, file close)

    http://www.espruino.com/Reference#fs

    http://www.espruino.com/File+IO

    Or since you don't have an SD card, http://www.espruino.com/Reference#Storag­e

    getTime

    It's literally a function called 'getTime()' - or you can use Date.now() - http://www.espruino.com/Reference#l__glo­bal_getTime

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

How to use spi to write and read some specfic register address

Posted by Avatar for user101931 @user101931

Actions