Skip to main content

Subsection 12.5.3 Part 4: Complete the Driver

lsm303agr_partial.c is most of a driver. Of the four functions the header declares, lsm303_AccelRegisterRead() and lsm303_AccelReadRaw() are given whole: RegisterRead is the function that was included in whoami_test.c, and we went through ReadRaw yesterday. lsm303_AccelInit() is given except for the two register values, which you will fill in. Only lsm303_AccelRegisterWrite() is entirely yours to write:
/**************************************************************************
 * Write one byte to an accelerometer register (Datasheet, Table 20)
 */
void lsm303_AccelRegisterWrite(uint8_t registerAddress, uint8_t data) {
    // complete this function


}
/**************************************************************************
 * Read one byte from an accelerometer register (Datasheet, Table 22)
 */
uint8_t lsm303_AccelRegisterRead(uint8_t registerAddress) {
    uint8_t data = 0;
    i2c1_memRead(LSM303_ADDRESS_ACCEL, registerAddress, 1, &data);

    return data;
}

Activity 12.5.3. Write the register-write function.

Complete lsm303_AccelRegisterWrite(), on your own, from your paper draft, with the given lsm303_AccelRegisterRead() as the model. It takes one I2C library call.
On Tuesday we read from one of the accelerometer’s registers: the controller sent the accelerometer’s address, then the register’s sub-address, and the accelerometer sent the register’s contents back (Figure 12.5.10, Table 22). Writing to a register begins the same way: the address, then the sub-address, and then, instead of reading a byte back, we send the value we want written (Table 20).
Figure 12.5.10. Tuesday’s read beside today’s write, in the datasheet’s own tables (§6.1.1, p. 38). Table 22 is the single-register read that lsm303_AccelRegisterRead() makes; Table 20 is the single-register write that lsm303_AccelRegisterWrite() makes: the same device address and sub-address, and a data byte sent in place of the read-back.
lsm303_AccelInit() needs no new I2C work, only your two values in its blanks:
uint8_t lsm303_AccelInit() {
    // Check the WHOAMI register
    if (lsm303_AccelRegisterRead(LSM303_WHO_AM_I_A) != 0x33) {
        return 0;       // failure
    }
    else {
        /******* Control register 1 (Datasheet 8.6) *******/
        uint8_t ctrl_reg1 =      ; // normal power mode @ 400 Hz, all axes enabled
        lsm303_AccelRegisterWrite(LSM303_CTRL_REG1_A, ctrl_reg1);

        /******* Control register 4 (Datasheet 8.9) *******/
        uint8_t ctrl_reg4 =      ; // continuous update, 2g full scale, I2C
        lsm303_AccelRegisterWrite(LSM303_CTRL_REG4_A, ctrl_reg4);

        return 1;           // success
    }
}
The function begins with the WHO_AM_I check from Tuesday and returns 0 if the expected 0x33 does not come back, and 1 if initialization succeeded. That uint8_t return value is for the caller: a test program can ask whether the sensor is actually there before trusting any readings, and in a moment you will see accel_test.c use that answer.

Activity 12.5.4. Fill in the two settings.

Put your two values into the blanks in lsm303_AccelInit() and build. Fix any compile errors, but do not run anything yet; the test program comes next.