Skip to main content

Subsection 12.2.6 Part 6: Break It on Purpose

Activity 12.2.7. Break it: a wrong device address.

A NACK is easier to recognize the first time if you produce one on purpose, on a bus you know is properly configured.
Suppose you change the 0x32 in LSM303_ADDRESS_ACCEL to 0x30. Predict: what will CoolTerm show, and what will the logic analyzer show?

Activity 12.2.8. Break it: try it.

(a)

Change the 0x32 in LSM303_ADDRESS_ACCEL to 0x30, rebuild and upload; then take a single sweep on the analyzer. The program attempts the read once a second, so the analyzer captures one within a second of being armed. Read the decoded row: what happened after the address byte?

(b)

Save an image of the capture (a screenshot of the Waveforms window will do), so that you can look at it again later.

(c)

Change the address back to 0x32, rebuild, and confirm the accelerometer responds once again.

(d)

If you finish early: explore what happens when you read a wrong register. Call lsm303_AccelRegisterRead(0x20) instead of reading the WHO_AM_I address. Predict first: will the transactions complete this time? What value comes back? Then capture it, and check the value you got against the LSM303AGR datasheet, §8.6 (CTRL_REG1_A).

(e)

One more prediction: suppose the program read register 0x0F from the display instead, at its address 0x70. Would the display acknowledge its address? And could the byte that comes back confirm that you reached the right device, the way 0x33 does for the accelerometer?
Figure 12.2.23. The same read attempted at the wrong device address. One transaction: h18 WR goes out, no device answers, the acknowledge bit stays HIGH (the decoder prints NACK), and a STOP ends the transaction.
CoolTerm prints Could not connect to accelerometer once a second. After the NACK, i2c1_memRead() skips the read, so data keeps its starting value of 0, the WHO_AM_I check fails, and the loop carries on.
Figure 12.2.25. The right device address, the wrong register address: reading register 0x20 instead of WHO_AM_I. Both transactions complete (every acknowledge the accelerometer owes is an ACK, and the read ends with the controller’s own NACK, exactly as the WHO_AM_I read did), and the value that comes back is h07: CTRL_REG1_A’s power-on default, 0b00000111 (LSM303AGR datasheet §8.6).
Figure 12.2.26. The same wrong-register capture, close up: the write transaction delivering register address 0x20 (top), then the read transaction returning 0x07 (bottom).