Adding SPI support to LIS3MDL

Posted on
  • My board has an LIS3MDL magnetometer on it, but uses SPI, rather than the I2C interface used in the existing LIS3MDL device code.

    Looking at the code, the IO interface is pretty well isolated from the LIS3MDL specific methods so I'm inclined to simply update that module so that, rather than requiring an I2C interface, the connect code accepts either an I2C or SPI instance and the read and write code simply tests for which interface is being used and calls the appropriate lower level IO function.

    The alternative is to create a redundant device called something like LIS3MDL-S or something, but I think that's unnecessary since the first approach is not unwieldy.


  • I'd suggest doing what's done here:Ā­/blob/master/devices/LIS2DH12.js#L135

    I've started using that pattern on new modules - basically having two connect... functions, and passing read and write functions into the constructor. I believe it's actually faster than the previous method as well.

    In that case I'd keep the I2C function called just connect so it doesn't break existing code, but you could change the implementation such that the optional address could be supplied - which is always a nice bonus.

  • Hmmmm, how very javascript-ish!
    I'll look into this. The callback feature is nice and efficient! I may want to expand on that for continuous reads, but I have some learning to do before I get into that.

    Thanks for the feedback.

  • Interesting approach on the LIS2DH12. I like the fact that you can pass configuration settings in the options 'option'. It can eliminate a lot of superfluous setup functions.

    Given this, if using SPI then cs could be a required option and the same connect function used - it's easy enough to distinguish whether the interface is SPI or I2C within the connect function. And, as you say, addr could be an option as you have it in LIS2DH12.

    I'll mess around with it and see what I can come up with...

  • I think I've got everything modified in LIS3MDL (except updating the documentation) but it is unclear to me how LIS2DH12.readXYZ is used. Can you give me an example?

  • I like the fact that you can pass configuration settings in the options 'option'.

    Yes - it's also a bit more verbose - nothing worse than connect(SPI,D3,true,false,true,true) :)

    Personally I'd stick with two connect functions for SPI and I2C. It could be merged into one as you say (and maybe should have originally) but it's nice to try and keep modules all working the same way so it's more familiar to move between them.

    LIS2DH12.readXYZ - Can you give me an example?

    Should just be:

    lis.readXYZ(function(v) {
      console.log(v.x, v.y, v.z);
    // prints:
    //   hello
    //   2,3,4
  • Just to close this out, I'm moving on to something else and won't be working on this for the foreseeable future.
    Thanks for your help.

  • Something else Espruino related, or something else completely?

  • Something else completely.

  • Shame - thanks for letting us know though!

  • I realized last night that I might have left the wrong impression with my previous comment. I'm merely shelving the Espruino work for the time being so I can do some other stuff.

    I'll be baaack! :-)

  • @TomWS, glad to get that confirmed... After reading between the lines of your conversations and comparing the last two with each other, I interpreted it your way... and I did not get disappointed. Thank you!

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

Adding SPI support to LIS3MDL

Posted by Avatar for TomWS @TomWS