Match ROM: The Match ROM command is represented by 55h. It is used to select individual 1-Wire devices once their 64-bit ID code is known. A common situation in which this might be used is when there are numerous devices on the bus, all of their device ID codes are known (having been previously found with a Search ROM command and remembered by the application), and the bus master wants to communicate with one of them. Here, the bus master issues a reset, waits for the presence pulse, issues the Match ROM command, then writes the 64-bit ID code of the device it is interested in. By issuing the Match ROM command, the bus master is telling all of the devices on the bus to listen to (read) the next 64 bits. Any device that does not match the 64-bit ID code will be deselected and will do nothing else but wait for the next reset pulse from the bus master. The one device that matches the 64-bit ID code (there can be only one, since they are all unique) will now be put into a state in which is it is waiting for additional commands that are specific to its function. A more common situation is the case where there are multiple devices on the bus, but the bus master isn’t maintaining their ID codes in memory. It simply knows that one of them is of a specific type it wants to communicate with—a thermometer, for example—on a bus with several other types of devices such as switches and A/D converters. In this situation, the bus master issues a reset, waits for the presence pulses, issues the Search ROM, and determines the device IDs for everything on the bus. The first 8 bits of the device ID is the family code, which differentiates the thermometer from the switches and the A/D converters. The bus master takes the device ID for the thermometer, issues a reset, waits for the presence pulse, issues a Match ROM, writes the thermometer’s device ID, and then writes thermometer- specific commands to the bus and retrieves the desired temperature data.
The Skip ROM command is represented by CCh. It’s used when you know there is only one device on the bus, you know what it is, and you want to select it without going through the process of giving it the 64-bit ID code. To communicate with a 1- Wire device in this fashion, the bus master issues a reset pulse, waits for the presence pulse, and then issues the Skip ROM command. Then, the one device on the bus is selected and awaiting further device-specific commands. We mentioned earlier that the only two functions that the DS2401 Silicon Serial Number uses are the Read ROM and Search ROM command. We’ve also mentioned that the Match ROM and Skip ROM are common to all 1-Wire devices. That contradiction is easily explained. Match ROM and Skip ROM aren’t relevant to the DS2401 because the DS2401 doesn’t have any other functions beyond merely responding with its 64-bit ID code. Both Match ROM and Skip ROM are functions that involve selecting a device with the purpose of issuing further commands. The DS2401 ignores Match ROM and Skip ROM. In addition to Read ROM, Search ROM, Match ROM, and Skip ROM, some 1-Wire devices have additional ROM commands that allow more options when it comes to selecting them. An example is the Alarm Search ROM found in the DS1920 thermometer iButton. It functions much like a Search ROM, with the exception that only thermometer iButtons that have experienced a temperature alarm will actually respond.
We’ve mentioned that the Match ROM command and the Skip ROM command select individual 1-Wire devices and put them in a state where they are ready to read additional commands from the 1-Wire bus. These additional, function-specific commands are frequently referred to as Memory commands on the Dallas Semiconductor data sheets. We’re not going to go into them in detail here. Instead, we’ll discuss them when we cover some of the 1-Wire devices in more detail. For the time being, just be aware that there are additional commands known as memory commands that work generally the same way as the ROM commands.
Cyclic Redundancy Check (CRC)
The CRC, or cyclic redundancy check, is a method of verifying the integrity of data transfers. This verification comes in the form of a CRC byte, which is the result of a logical operation on a stream of bits. In 1-Wire devices there are two different CRC types. One CRC is an 8-bit type and is stored in the most significant byte of the 64- bit ROM. The other CRC is a 16-bit type. While the CRC8 is used in a number of places in 1-Wire data transfers, a good example of it can be found in the 64-bit ID code that every 1-Wire device has. The last 8 bits of this ID code is a CRC8 byte. That byte is computed using the first 56 bits of the ROM ID according to the following formula:
CRC8 = X8 + X5 + X4 + 1
The CRC16 is generated according to the standardized CRC16-polynomial. A CRC16 is used for fast verification of a data transfer when writing memory devices. That byte is computed according to the following formula:
CRC16 = X16 + X15 + X2 + 1
For the CRC8, the process starts with the 8-bit CRC “shift register” initialized to all zeros. (Although we are calling it a shift register, it’s important to remember that it’s really an algorithm being implemented in software by the bus master. We’re simply using the shift register to visualize the process.) Once the shift register is initialized to all zeros, the bus master takes the ROM ID, one bit at a time, starting with the least significant bit and working up, and inserts it into the CRC shift register.
1) The current input bit and the previous value of the least significant shift register bit, Bit 0, are subjected to a logical exclusive or (XOR). XOR is another way of saying, “one or the other, but not both.” If both are zero, the output is zero. If both are one, the output is zero. If one is zero and the other is one, the output is one. The result of this XOR is the feedback bit that gets fed back into several other registers.
2) Bits 1, 2, 5, 6, and 7 get the value of their higher-order neighbor (bits 2, 3, 6, 7, and 8 respectively) shifted into them unmodified.
3) Bit 3 gets the value of the feedback bit XOR bit 4.
4) Bit 4 gets the value of the feedback bit XOR bit 5.
5) Bit 8 takes on the value of the feedback bit.
Then, another input bit is taken from the ROM ID and put into this algorithm. Once 56 bits have been taken from the ROM ID and put into the shift register computation, the value of the 8-bit CRC shift register should equal the CRC byte. If you continue to perform the shifting algorithm on the remaining 8-bits of the ROM ID—that is, shift in the CRC byte into the CRC register—the contents should go to zero.
Figure 10-12: CRC example, table with all 64 cycles
Now that we’ve looked at what the CRC is and how it works, there are a couple of additional points. The cyclic redundancy check is fundamentally the responsibility of the bus master. While the 1-Wire slave devices may send CRC bytes along with their data, the bus master doesn’t actually have to pay any attention to them or act upon them. The CRC is merely a capability that allows the bus master to determine whether or not the data transfer was good, but the existence of this capability doesn’t imply automatic error checking. The bus master has to first use the CRC byte to identify a communication problem, and then act on that problem by repeating the communication. Neither of those actions is mandatory or automatic. Although the example we’ve looked at focuses on the ROM ID, many other data transfers from the 1-Wire devices also incorporate CRC8 or CRC16 bytes. The specific data sheets for each of the 1-Wire devices explain where and when they get sent. One last comment about CRC. If you are wondering how you would go about writing programs that verified the accuracy of CRC bytes, the 1-Wire Java class libraries have CRC classes in them that provide objects and methods for handling CRC checking. We took a look at the DS2401 Silicon Serial Number in our discussion about the Read ROM and Search ROM commands. Now, let’s take a look at some other common 1-Wire devices: the DS2405 Addressable Switch and the DS1920 Temperature iButton.
1-Wire Device Example: the DS2405 Addressable Switch
This is one of the most useful 1-Wire devices, and it lends itself well to a whole range of applications with respect to Internet-enabled devices. The DS2405, as its name implies, is a 1-Wire, selectable switch. It has an output, which can be thought of as a switchable pin, a 1-Wire bus pin, and a ground. It comes in a variety of packages similar to the DS2401.
1. All voltages referenced to ground.
2. Vpullup is the external pullup voltage.
3. Input load is to ground.
4. VIH is a function of the external pullup resistor and the VCC supply.
5. VIH for the PIO pin should always be greater than or equal to Vpullup-0.3 volts.
6. Input resistance is to ground.
7. Under certain low voltage conditions VILMAX may have to be reduced to as much as 0.5 volts to always guarentee a presence pulse
The switch pin can be used to pull a node (PIO on the pinout above) to ground, sinking up to 4 mA in the process. Since the device doesn’t connect to a positive supply, it can’t drive the node high; it is either pulling the node low or off, and allowed to be pulled high. You can give the circuit controlled by the PIO pin its own positive supply that acts as that pullup. You can also query the DS2405 to find out whether or not it is pulling the node low. Thus, it can be used to control LEDs, trigger logic, read and control the state of relays, etc.
DS2405 Addressable Switch ROM commands
The DS2405 has the standard four ROM commands that we’ve learned about: Read ROM, Search ROM, Match ROM, and Skip ROM. It also has an additional ROM command, Active Only Search ROM.
Table 10-3: Table of DS2405 ROM commands