Subsection12.2.7Part 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.
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:
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.
Open the accelerometer datasheet to §7, Table 26 (the register address map), and put lsm303agr.h beside it: the header is that table, transcribed into #defines.
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.
// 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.
// 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. We used RegisterRead to read the WHO_AM_I register. RegisterWrite is tonight’s homework, on paper.