• How to Bobble the Bits in the Registers

    The Vogan poetry in the datasheet for the LSM9DS1 can be found here:
    http://www.st.com/content/ccc/resource/t­echnical/document/datasheet/1e/3f/2a/d6/­25/eb/48/46/DM00103319.pdf/files/DM00103­319.pdf/jcr:content/translations/en.DM00­103319.pdf
    In section six the register mapping is described. This lets us write a table to reference the registers in the code. For example the gyro and accelerometer registers are defined:

    var Regs={
    ACT_THS: 04, ACT_DUR: 05, INT_GEN_CFG_XL: 06, INT_GEN_THS_X_XL: 07, INT_GEN_THS_Y_XL: 08,
    INT_GEN_THS_Z_XL: 09, INT_GEN_DUR_XL: 0x0A, REFERENCE_G: 0x0B, INT1_CTRL: 0x0C,
    INT2_CTRL: 0x0D, //Reserved -- 0E -- -- Reserved
    WHO_AM_I: 0x0F, CTRL_REG1_G: 0x10, CTRL_REG2_G: 0x11, CTRL_REG3_G: 0x12,
    ORIENT_CFG_G: 0x13, INT_GEN_SRC_G: 0x14, OUT_TEMP_L: 0x15, OUT_TEMP_H: 0x16,
    STATUS_REG1: 0x17, OUT_X_L_G: 0x18, OUT_X_H_G: 0x19, OUT_Y_L_G: 0x1A,
    OUT_Y_H_G: 0x1B, OUT_Z_L_G: 0x1C, OUT_Z_H_G: 0x1D, 
    CTRL_REG4: 0x1E, CTRL_REG5_XL: 0x1F, CTRL_REG6_XL: 0x20, CTRL_REG7_XL: 0x21,
    CTRL_REG8: 0x22, CTRL_REG9: 0x23, CTRL_REG10:0x24, //Reserved -- 25 -- -- Reserved
    INT_GEN_SRC_XL: 0x26, STATUS_REG2: 0x27,
    OUT_X_L_XL: 0x28, OUT_X_H_XL: 0x29, OUT_Y_L_XL: 0x2A, 
    OUT_Y_H_XL: 0x2B,  OUT_Z_L_XL: 0x2C, OUT_Z_H_XL: 0x2D,
    FIFO_CTRL: 0x2E, FIFO_SRC: 0x2F,
    INT_GEN_CFG_G: 0x30, 
    INT_GEN_THS_XH_G: 0x31, INT_GEN_THS_XL_G: 0x32, INT_GEN_THS_YH_G: 0x33,
    INT_GEN_THS_YL_G: 0x34, INT_GEN_THS_ZH_G: 0x35, INT_GEN_THS_ZL_G: 0x36,
    INT_GEN_DUR_G: 0x37,
    //Reserved r 38-7F
    };
    

    In Section7 of the datasheet the bits in the Accelerometer and gyroscope registers are described.
    For example:
    7.12 CTRL_REG1_G (10h) Angular rate sensor Control Register 1.
    | Bit7 | Bit6| Bit5| Bit4| Bit3| Bit2| Bit1| Bit0|
    | --- | --- | --- | --- | --- | --- | --- | --- |
    |ODR_G2 |ODR_G1 |ODR_G0 |FS_G1 |FS_G0 |0(1)| BW_G1 |BW_G0 |

    1. This bit must be set to ‘0’ for the correct operation of the device. BW_G1 BW_G0
      Bits 5, 6, and 7 control the output data rate of the gyro.
      Bits 3, and 4 select the full scale of the gyro.
      Bit 2 is always 0, and
      Bits 0 and 1 select the gyro bandwidth.

    Setting up a data structure to bobble the bits

    var REGval={
    /*
    CTRL_REG1_G (10h)
    Angular rate sensor Control Register
    ODR_G2, ODR_G1, ODR_G0, FS_G1, FS_G0, 0,BW_G1, BW_G0
    */
      ODR_G:{reg:Regs.CTRL_REG1_G,shift:5,mask­:7},
      FS_G:{reg:Regs.CTRL_REG1_G,shift:3,mask:­3},
      BW_G:{reg:Regs.CTRL_REG1_G,shift:0,mask:­3},
    //more stuff
    

    And setup functions to read or write the bits

    /** Use the datastructure to write bits to AG register */
    LSM9DS1.prototype.AGwrite=function(R,val­ue){
    var A=this.xgReadByte(R.reg);
    //console.log(A,value,R,R.mask,R.shift);­
    var v=value&R.mask;
    //  console.log("v= "+v);
     A=A&(~(R.mask<<R.shift));
     A=A|(v<<R.shift);
    // console.log("A= "+A.toString(2));
     this.xgWriteByte(R.reg,A);
    };//end AGwrite
    
    /** Use the data structure to read bit settings from AG register*/
    LSM9DS1.prototype.AGread=function(R){
     var A=this.xgReadByte(R.reg);
     A=A>>R.shift;
     A=A&R.mask;
    // console.log("A= "+A.toString(2));
     return A;
    };//end AGread
    

    The following tests to see if the communication is working, performs a reset, writes and then reads a value to the ODR bits and then iterates thru the data structure to read and display all the bits.

    /** 'run() initializes the LSM9DS1' */
    LSM9DS1.prototype.run=function(){
     var i;
    var xgTest = this.xgReadByte(Regs.WHO_AM_I_XG);
     console.log("XG replied "+xgTest);
    
    console.log("do a reset");
    this.AGwrite(REGval.SW_RESET,1);
    console.log("SWreset= "+this.AGread(REGval.SW_RESET));
    //
    console.log("try writing to the ODR_G bits");
    this.AGwrite(REGval.ODR_G,7);
      console.log("ODR_G= "+this.AGread(REGval.ODR_G));
    console.log("iterate thru all the AG settings");
    for (var x in REGval)
      console.log(x,this.AGread(REGval[x]));
    
      return 1;
    };//end run
    

    The output looks like this:

    1v92 Copyright 2016 G.Williams
    >
    =undefined
    XG replied 104
    do a reset
    SWreset= 0
    try writing to the ODR_G bits
    ODR_G= 7
    iterate thru all the AG settings
    ODR_G 7
    FS_G 0
    BW_G 0
    G_INT_SEL 0
    G_OUT_SEL 0
    G_LP_mode 0
    G_HP_EN 0
    HPCF_G 0
    SignX_G 0
    SignY_G 0
    SignZ_G 0
    Orient_G 0
    Dec_XL 0
    Zen_XL 1
    Yen_XL 1
    Xen_XL 1
    ODR_XL 0
    FS_XL 0
    BW_SCAL_ODR_XL 0
    BW_XL 0
    HR_XL 0
    DCF_XL 0
    FDS_XL 0
    HPIS1_XL 0
    BOOT 0
    BDU 0
    H_LACTIVE 0
    PP_OD 0
    SIM 0
    IF_ADD_INC 1
    BLE 0
    SW_RESET 0
    SLEEPG 0
    FIFO_TEMP_EN 0
    DRDY_mask_bit 0
    I2C_DISABLE 0
    FIFO_EN 0
    STOP_ON_FTH 0
    ST_G 0
    ST_XL 0
    FMODE 0
    FTH 0
    >
    

    1 Attachment

About