Skip to main content

Subsection 12.2.7 Part 7: The Accelerometer’s Device Driver

Writing a device driver follows five steps, the same steps you followed for the display. Figure 10.3.29 is the map: the driver reaches the device only through the I2C library, and the application reaches the device only through the driver.
  1. From the datasheet, understand the basic I2C transfers for reading and writing for the device you want to talk to.
  2. From the datasheet, #define the device’s addresses and commands.
  3. Define functions for the actions you need from the device.
  4. Write those functions using the i2c.c library functions.
  5. Devise unit tests to confirm the driver works.
Step 2 is already done, and here is where it came from. Open the LSM303AGR datasheet to §7, Table 26 (Register address map, p. 43), and put lsm303agr.h beside it: the header is that table, transcribed into #defines. Here is the top of its register block:
// I2C address for the sensor
#define LSM303_ADDRESS_ACCEL    (0x32 >> 1)         // 0011001x

// ACCELEROMETER SECTION
// Accelerometer registers                // DEFAULT    TYPE
#define LSM303_WHO_AM_I_A          0x0F   // 00110011   r
#define LSM303_CTRL_REG1_A         0x20   // 00000111   rw
#define LSM303_CTRL_REG2_A         0x21   // 00000000   rw
#define LSM303_CTRL_REG3_A         0x22   // 00000000   rw
#define LSM303_CTRL_REG4_A         0x23   // 00000000   rw
#define LSM303_CTRL_REG5_A         0x24   // 00000000   rw
#define LSM303_CTRL_REG6_A         0x25   // 00000000   rw
#define LSM303_REFERENCE_A         0x26   // 00000000   rw
#define LSM303_STATUS_REG_A        0x27   // 00000000   r
#define LSM303_OUT_X_L_A           0x28
#define LSM303_OUT_X_H_A           0x29
#define LSM303_OUT_Y_L_A           0x2A
#define LSM303_OUT_Y_H_A           0x2B
#define LSM303_OUT_Z_L_A           0x2C
#define LSM303_OUT_Z_H_A           0x2D
The real file keeps going (FIFO, interrupt, and click registers up to address 0x3D), and we leave all of those at their defaults. The _A in every name marks the accelerometer’s half of the register map; the magnetometer has its own, which we don’t touch. Notice the comment columns: for each control and status register, the header records the power-on default from the datasheet’s table. You saw one of those defaults already if you did the wrong-register capture.

Activity 12.2.9. Check the header against the datasheet.

(a)

Download lsm303agr.h from the book’s starter files and put it in your mylib folder.

(b)

In the datasheet’s Table 26 (p. 43), find the rows for WHO_AM_I_A and CTRL_REG1_A and check them against the header’s #defines, the address and the default value of each.
Step 3 is also done: the header declares four functions, and they are the interface we’ll build on for the rest of the week.
// The accelerometer has several gain and update rate settings.  These are described
// in the datasheet and are set up in the AccelInit function.
uint8_t lsm303_AccelInit();

// The AccelRegisterWrite function should take a register address and a command byte
// to be written into the register.
void lsm303_AccelRegisterWrite(uint8_t registerAddress, uint8_t data);

// The AccelRegisterRead function should take a register address and return a data byte
// read from the register.
uint8_t lsm303_AccelRegisterRead(uint8_t registerAddress);

// Here is the higher-level function to read the acceleration vector.  Output struct
// contains raw (unscaled) acceleration values.
void lsm303_AccelReadRaw(lsm303AccelData_s * const result);
One of the four is already running on your board. We used lsm303_AccelRegisterRead() to read the WHO_AM_I register. Writing lsm303_AccelRegisterWrite() is tonight’s homework.