Smartphones are ubiquitous, and the computing power and sensors available in a smartphone continue to increase. It is now practical to use that phone as the interface to other devices. For example, the USB port on an Android smartphone can communicate with slave devices without a computer.
System
The USB port on the smartphone is crucial to this application. When the smartphone uses a USB on-the-go (OTG) transceiver, then USB host functionality is possible with slave devices such as mice, flash drives, keyboards, or the Thermochron datalogger. Recent versions of the Android API support USB host mode at the application level. This lets end users install applications that talk with USB peripherals without installing special drivers on the user’s smartphone.
Fig. 1: The system uses a DS9490R 1-Wire adapter to interface between the smartphone and the application device.
Normally, the Android smartphone is connected to a computer and acts as a USB slave, but the USB OTG transceiver (see Fig. 1 ) allows the phone to become a USB master. This role also requires a special OTG cable.
In this application the Android smartphone is the master and a Thermochron (DS1921G-F5#) is the slave. The Thermochron iButton is a digital thermometer with ±1.0°C accuracy, an RTC and timer, and measurement intervals from 1 to 255 minutes. The application uses a USB to 1-Wire/iButton adapter to bridge the smartphone with the datalogger. A network cable/socket serves as the 1-Wire bus.
Role of the 1-Wire bus
The 1-Wire outputs are open drain, operating with a pullup resistor similar to I2 C. Each 1-Wire slave also has a factory-lasered, unique 64-bit registration number.
A 1-Wire transaction sequence (see Fig. 2 ) starts with a reset pulse (trst) sent to the slave. The reset pulse puts all the slaves into a known state by holding down the 1-Wire bus for a predefined period of time. Next, the slaves acknowledge the master with a presence-detect pulse (tpd ) that pulls down the bus after the master releases it.
Fig. 2: 1-Wire timing pulses.
With a temperature logger as the slave, the master’s commands could involve writing or reading to its scratchpad or memory, or converting a temperature. The 1-Wire interface has no clock line, so communications are split into time slots (tslot), each carrying one information bit. At the beginning of a time slot, the master briefly pulls down the bus to indicate the start of a bit. When transferring a zero, the master or slave continues to hold the bus low; with a one, the master or slave will release the bus. The master or slave reads the bus at a defined time (tsample ) after the master indicates the start of the time slot.
The 1-Wire adapter
The DS9490R 1-Wire-to-USB adapter has four USB endpoints: control, interrupt, bulk input (epIN), and bulk output (epOUT). Generally, the control endpoint is used to send commands to the 1-Wire adapter and to set up the type of transfer. The bulk input/output is used for data transfers, and the interrupt endpoint receives time-sensitive information such as status registers and return messages.
Using the Android as a USB host
There is precedent for the design presented here. Manuel Di Cerbo connected an Arduino board with an Android phone over USB. The Android API, starting from version 3.1, supports USB host mode. Our application extends DiCerbo’s basic concepts to the USB-to-1-Wire adapter and uses the 1-Wire adapter instead of an MCU. The code examples demonstrate how Android sets up communication with a USB peripheral and provides the USB endpoints.
Here we demonstrate how to use the Android to perform a temperature conversion on the Thermochron and to read the temperature result. Each transaction step (see Table 1 ) begins with a 1-Wire Reset, followed by Match ROM to select the slave device, then followed by a final device-specific command.
1-Wire Reset | Match ROM | Convert Temperature |
1-Wire Reset | Match ROM | Read Memory/Register |
Table 1: Commands set by the 1-Wire master to the Thermochron datalogger.
The 1-Wire reset is performed by a USB control transfer. The control transfer function prototype from the Android API initiates 1-Wire Reset, Match ROM, or Block I/O.
// Performs a control transaction on endpoint zero for this device.
int controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int length, int timeout)
A bulk transfer is used for Match ROM and read/write memory. Here the endpoint would be either epIN or epOUT, depending on whether we are reading or writing data. The endpoint buffer either stores data to be sent, or is empty for storing the received data. The length will be the number of bytes received or sent.
// Performs a bulk transaction on the given endpoint.
int bulkTransfer( UsbEndpoint endpoint, byte[] buffer, int length, int timeout)
The code to send the convert temperature command (0x44) to the Thermochron is shown here. In line 1, the 1-Wire reset is sent by a control transfer (see Table 1).
// 1-Wire Reset
1 conn.controlTransfer(0x40, 0x01, 0x0C4B, 0x0001, null, 0x0000, 0);
// Match ROM, where romid is the iButton’s registration number
2 romid = new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3 conn.bulkTransfer(epOUT, romid, 8, 0);
4 conn.controlTransfer(0x40, 0x01, 0x0065, 0x55, null, 0, 0);
// Convert Temperature for DS1921G
5 data = new byte[]{0x44};
6 conn.bulkTransfer(epOUT, data, data.length, 0);
7 conn.controlTransfer(0x40, 0x01, 0x1075, data.length, null, 0, 0);
In line 4 the match access control transfer sends a 1-Wire Reset, match access ROM command on the 1-Wire bus, given by the index parameter of 0x55. This is followed by the desired slave’s ROM registration number, which is preloaded to epOUT in lines 2 and 3.
The Thermochron datasheet identifies 0x44 as the code for temperature conversion (see Table 2). The convert temperature command is performed by writing 0x44 to epOUT using a block I/O operation (line 6). The control transfer in line 7 executes the block I/O command.
Read Memory | 0xF0 | Read the data from the internal register. Follow the command by the register address, with LSb first. Continue with 0xff dummy data for each byte read. |
Convert Temperature | 0x44 | Begins the conversion. |
Table 2: Thermochron memory and control commands (using bulk I/O)
The code below shows the sequence for reading the temperature register data with a bulk I/O transfer. The Thermochron’s read memory command code is 0xF0 (see Table 2 ). This is followed by the target register address (TA) of 0x0211 and is split into two bytes (line 8). The master then writes dummy data (0xff) to the bus to receive the data. The slave will respond and override the 0xff data because the 1-Wire is an open-drain bus. The net effect is the ANDing of the data with 0xff.
These series of commands are put onto epOUT and the control transfer executes the command by sending it to the 1-Wire bus (lines 9 and 10). The read back data will be in the USB endpoint, epIN. The raw temperature code is stored in tempdata and converted to a temperature value (lines 12 and 13).
// 1-Wire Reset and Match ROM
// (omitted) …
// Read Temperature Register/Memory Command
// Read Memory, TA2, TA1, dummy data
8 command = new byte[] {(byte)0xf0, 0x11, 0x02, (byte)0xff, (byte)0xff};
9 conn.bulkTransfer(epOUT, command, command.length, 0);
10 conn.controlTransfer(0x40, 0x01, 0x1075, command.length, null, 0, 0);
// Return Data from input endpoint
11 byte[] tempdata = new byte[5];
12 conn.bulkTransfer(epIN, tempdata, 5, 0);
// Temperature calculation
13 temperature = (int)(tempdata[4] & 0xff)/2.0 – 40;
Our sample Android application
Figure 3 shows the sample Android application. When the user runs the program and presses the Enumerate button, a screen is displayed asking for permission to access the USB device. Once the user taps OK, the application performs a search and lists all the slaves’ 64-bit registration numbers in the dropdown menu. When the user selects a specific registration number, the application will perform the routine detailed above and display the real-time temperature from the Thermochron.
In the final application the code is abstracted into general 1-Wire operations: reset, read, write, and others. These commands together can be further abstracted into iButton functions such as convertTemperature() and readMemory(). This allows an AsyncTask thread to call the correct function.
Fig. 3: The Android application, USB permissions (left). Temperature measurement for a specific Thermochron iButton is identified by its registration number (right).
The sample application contains the UI code for linking the user interactions, such as pressing a button, into executing a command to the 1-Wire slave. Besides the DS1921G Thermochron, the application supports the iButton temperature logger (DS1922) and iButton Hygrochron temperature and humidity logger (DS1923). The registration number for each device is separated into three fields: family code, serial number, and CRC (see Fig. 3 ). The family code can be read from each registration number to determine the exact device model.
Learn more about Maxim Integrated