• 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.

  • 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