• The Magnetometer Bits

    The Registers

    The way this was done is to pull up the datasheet PDF, highlight the relevant table, paste it into a new Wordpad document, edit search and replace and save it as a text document with .js extension.
    Here is a portion of the magnetometer address map. Referring to the test in quotes in the following.
    Search “ r/w “ and replace with “: 0x”. Other edits apply as well.

    Table 22. Magnetic sensor register address map
    Registers marked as Reserved must not be changed. Writing to those registers may cause
    permanent damage to the device.
    To guarantee proper behavior of the device, all registers addresses not listed in the above
    table must not be accessed and the content stored on those registers must not be changed.
    The content of the registers that are loaded at boot should not be changed. They contain the
    factory calibration values. Their content is automatically restored when the device is
    powered up.
    Name Type
    Register address
    Default Comment
    Hex Binary
    Reserved 00 - 04 -- -- Reserved
    OFFSET_X_REG_L_M r/w 05 00000000
    Offset in order to compensate
    environmental effects
    OFFSET_X_REG_H_M r/w 06 00000000
    OFFSET_Y_REG_L_M r/w 07 00000000
    OFFSET_Y_REG_H_M r/w 08 00000000
    OFFSET_Z_REG_L_M r/w 09 00000000
    OFFSET_Z_REG_H_M r/w 0A 00000000
    Reserved 0B - 0E -- -- Reserved
    WHO_AM_I_M r 0F 0000 1111 00111101 Magnetic Who I am ID
    Reserved 10 - 1F -- -- Reserved
    CTRL_REG1_M r/w 20 0010 0000 00010000
    

    Here is a portion of the edited text

    var MagRegs={
    //Reserved 00 - 04 -- -- Reserved
    OFFSET_X_REG_L_M:0x05,
    OFFSET_X_REG_H_M:0x06,
    OFFSET_Y_REG_L_M:0x07,
    OFFSET_Y_REG_H_M:0x08,
    OFFSET_Z_REG_L_M:0x09,
    OFFSET_Z_REG_H_M:0x0A,
    //Reserved 0B - 0E -- -- Reserved
    WHO_AM_I_M: 0x0F,
    //Reserved 10 - 1F -- -- Reserved
    CTRL_REG1_M:0x20,
    CTRL_REG2_M:0x21,
    CTRL_REG3_M:0x22,
    CTRL_REG4_M:0x23,
    CTRL_REG5_M:0x24,
    //Reserved 25 - 26 -- -- Reserved
    

    Creating the Data Structure

    A similar process is used to edit the pasted PDF text into the data structure used to bobble the bits.
    Each register description in individually cut and pasted to reduce the editing clutter, for example.

    8.5 CTRL_REG1_M (20h)
    Table 110. X and Y axes operative mode selection
    Table 111. Output data rate configuration
    Table 107. WHO_AM_I_M register
    0 0 1 1 1 1 0 1
    Table 108. CTRL_REG1_M register
    TEMP_
    COMP OM1 OM0 DO2 DO1 DO0 FAST_ODR ST
    
    8.6 CTRL_REG2_M (21h)
    8.7 CTRL_REG3_M (22h)
    Table 112. CTRL_REG2_M register
    0(1)
    1. These bits must be set to ‘0’ for the correct operation of the device.
    FS1 FS0 0(1) REBOOT SOFT_RST 0(1) 0(1)
    

    I used the bit labels and retained some information as a comment. A portion is shown here:

    /**Table 108. CTRL_REG1_M register
    TEMP_COMP, OM1 OM0, DO2 DO1 DO0, FAST_ODR ,ST */
    
    TEMP_COMP:{reg:MagRegs.CTRL_REG1_M,shift­:7,mask:1},
    OM:{reg:MagRegs.CTRL_REG1_M,shift:5,mask­:3},
    DO:reg:{MagRegs.CTRL_REG1_M,shift:2,mask­:7},
    FAST_ODR:{reg:MagRegs.CTRL_REG1_M,shift:­1,mask:1},
    ST:{reg:MagRegs.CTRL_REG1_M,shift:0,mask­:1},
    
    /** 8.6 CTRL_REG2_M (21h)
    0(1),FS1, FS0, 0(1), REBOOT, SOFT_RST, 0(1,) 0(1*/
    FS:{reg:MagRegs.CTRL_REG2_M,shift:5,mask­:3},
    REBOOT:{reg:MagRegs.CTRL_REG2_M,shift:3,­mask:1},
    SOFT_RST{reg:MagRegs.CTRL_REG2_M,shift:2­,mask:1},
    
    

    2 Attachments

About