#include #include "/home/jeblack3/libusblinux300/ownet.h" #define THERMOMETER 0x10 #define MATCH_ROM_CMD 0x55 #define SEARCH_ROM_CMD 0xF0 #define READ_POWER_CMD 0xB4 #define MAXDEVICES 256 int echoReadBit(); int echoWriteBit (int bit); void msg (char *s); void msgInt (const char *format, unsigned int n); int checkCRC8 (unsigned char *ptr, int len); int echoFindDevices (unsigned char SN[][8]); void doReadPower (unsigned char serialNum[8]); int deviceRW (unsigned char serialNum[8], unsigned char const *src, unsigned char const *isWriting, unsigned char *dest, int len); int portnum, doOutputToFile; FILE *outputFile; int main (int argc, char *argv[]) { char portname[] = "DS2490-1"; unsigned char SN[MAXDEVICES][8]; int numDevices, dev; // Acquire adapter portnum = owAcquireEx (portname); if (portnum < 0) { msg ("ERROR: Adapter not found\n"); return 0; } // Find Devices numDevices = echoFindDevices (SN); if (numDevices == 0) { return 0; } while (1) { // Read power supply status of thermometers for (dev = 0; dev < numDevices; dev++) { if (SN[dev][0] == THERMOMETER) { doReadPower (SN[dev]); } } } return 0; } int echoReadBit() { int bit; bit = owTouchBit (portnum, 1); msgInt ("%u", bit); return bit; } int echoWriteBit (int bit) { int bit2; msgInt ("%u", bit); bit2 = owTouchBit (portnum, bit); if (bit != bit2) { msgInt ("\nERROR: Wrote %u", bit); msgInt (" but read %u\n", bit2); return 0; } return 1; } void msg (char *s) { printf ("%s", s); if (doOutputToFile) { if ( fprintf(outputFile, "%s", s) < 0 ) { printf ("\nERROR: Could not write to file\n"); } fflush (outputFile); } return; } void msgInt (const char *format, unsigned int n) { printf (format, n); if (doOutputToFile) { if ( fprintf(outputFile, format, n) < 0 ) { printf ("\nERROR: Could not write to file\n"); } fflush (outputFile); } return; } int checkCRC8 (unsigned char *ptr, int len) { int crc8 = 0; int i; setcrc8 (portnum, 0); for (i = 0; i < len - 1; i++) { crc8 = docrc8 (portnum, ptr[i]); } if (ptr[len - 1] == crc8) { return 1; } else { msgInt ("ERROR: CRC received = %02X", ptr[len - 1]); msgInt (" but CRC calculated = %02X\n", crc8); return 0; } } int echoFindDevices (unsigned char SN[][8]) { int bit[64], branch0[64], branch1[64], i, j, dev, depth; unsigned char command; dev = 0; depth = 0; for (i = 0; i < 64; i++) { bit[i] = 0; } do { // Reset signal if ( !owTouchReset(portnum) ) { msg ("ERROR: No one-wire devices responded to reset signal\n"); return 0; } // SEARCH ROM command command = SEARCH_ROM_CMD; if ( !owBlock(portnum, FALSE, &command, 1) ) { msg ("ERROR: Could not write to devices\n"); return 0; } msgInt ("%02X\n", command); if (command != SEARCH_ROM_CMD) { msgInt ("ERROR: Wrote %02X", SEARCH_ROM_CMD); msgInt (" but read %02X from bus\n", command); return 0; } // Traverse tree for (i = 0; i < 64; i++) { if (i >= depth) { branch0[i] = !echoReadBit(); branch1[i] = !echoReadBit(); if (branch0[i]) { bit[i] = 0; } else if (branch1[i]) { bit[i] = 1; } else { msg ("\nERROR: During search, no devices found with either bit 0 or bit 1\n"); return 0; } } else { if ( branch0[i] != (!echoReadBit()) || branch1[i] != (!echoReadBit()) ) { msg ("\nERROR: ROM bits read on different runs inconsistent\n"); return 0; } } if ( !echoWriteBit(bit[i]) ) { return 0; } if (i % 8 == 7) { msg ("\n"); } else { msg (" "); } } for (i = 0; i < 8; i++) { SN[dev][i] = 0; for (j = 0; j < 8; j++) { SN[dev][i] |= (bit[8 * i + j] << j); } } if ( !checkCRC8(SN[dev], 8) ) { return 0; } for (i = 64 - 1; i >= 0; i--) { bit[i]++; if (bit[i] == 1 && branch1[i]) { depth = i + 1; break; } } dev++; } while (i >= 0); if (dev == 0) { msg ("ERROR: No devices found\n"); } return dev; } void doReadPower (unsigned char serialNum[8]) { unsigned char const readPower[2][2] = { {READ_POWER_CMD, // READ POWER command 0xFF}, // Read slots {1, 0} }; unsigned char buff[sizeof(readPower[0]) + 9]; int i; // Send command if ( !deviceRW (serialNum, readPower[0], readPower[1], buff, sizeof(readPower[0])) ) { for (i = 7; i >= 0; i--) { msgInt ("%02X", serialNum[i]); } msg (": could not read\n"); return; } // Print results for (i = 7; i >= 0; i--) { msgInt ("%02X", serialNum[i]); } if (buff[10] & 1) { msg (": external power\n"); } else { msg (": parasite power\n"); } return; } int deviceRW (unsigned char serialNum[8], unsigned char const *src, unsigned char const *isWriting, unsigned char *dest, int len) { int success = 1; unsigned char correct; int i; // MATCH ROM command dest[0] = MATCH_ROM_CMD; // Serial number for (i = 0; i < 8; i++) { dest[1 + i] = serialNum[i]; } // Data for device for (i = 0; i < len; i++) { dest[9 + i] = src[i]; } // Send reset signal if ( !owTouchReset(portnum) ) { msg ("ERROR: No one-wire devices responded to reset signal\n"); return 0; } // Read & Write if ( !owBlock(portnum, FALSE, dest, 9 + len) ) { msg ("ERROR: Could not write to device\n"); return 0; } // Print, check writes for (i = 0; i < 9 + len; i++) { msgInt ("%02X", dest[i]); if (i == 8 && len > 20) { msg ("\n "); } else if (i == 0 || i == 8) { msg (" "); } if (i < 9 || isWriting[i - 9]) { if (i >= 9) { correct = src[i - 9]; } else if (i >= 1) { correct = serialNum[i - 1]; } else { correct = MATCH_ROM_CMD; } if (dest[i] != correct) { msgInt ("\nERROR: Wrote %02X", correct); msgInt (" but read %02X from bus\n", dest[0]); success = 0; } } } msg ("\n"); return success; }