Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of OneWire by
OneWire.h
00001 #ifndef OneWire_h 00002 #define OneWire_h 00003 00004 #include <inttypes.h> 00005 #include <mbed.h> 00006 00007 // You can exclude certain features from OneWire. In theory, this 00008 // might save some space. In practice, the compiler automatically 00009 // removes unused code (technically, the linker, using -fdata-sections 00010 // and -ffunction-sections when compiling, and Wl,--gc-sections 00011 // when linking), so most of these will not result in any code size 00012 // reduction. Well, unless you try to use the missing features 00013 // and redesign your program to not need them! ONEWIRE_CRC8_TABLE 00014 // is the exception, because it selects a fast but large algorithm 00015 // or a small but slow algorithm. 00016 00017 // you can exclude onewire_search by defining that to 0 00018 #ifndef ONEWIRE_SEARCH 00019 #define ONEWIRE_SEARCH 1 00020 #endif 00021 00022 // You can exclude CRC checks altogether by defining this to 0 00023 #ifndef ONEWIRE_CRC 00024 #define ONEWIRE_CRC 1 00025 #endif 00026 00027 class OneWire 00028 { 00029 private: 00030 DigitalInOut wire; 00031 00032 #if ONEWIRE_SEARCH 00033 // global search state 00034 unsigned char ROM_NO[8]; 00035 uint8_t LastDiscrepancy; 00036 uint8_t LastFamilyDiscrepancy; 00037 uint8_t LastDeviceFlag; 00038 #endif 00039 00040 public: 00041 00042 enum ONEWIRE_RESULT 00043 { 00044 ONEWIRE_OK = 0, 00045 ONEWIRE_NO_RESPONSE, 00046 ONEWIRE_ERROR, 00047 ONEWIRE_ERROR_SHORT_CIRCUIT 00048 }; 00049 00050 OneWire(PinName pin); 00051 00052 00053 00054 // Perform a 1-Wire reset cycle. Returns ONEWIRE_OK if a device responds 00055 // with a presence pulse. Returns ONEWIRE_NO_RESPONCE if there is no device or the 00056 // bus is shorted or otherwise held low for more than 250uS 00057 00058 uint8_t init_sequence(); 00059 00060 uint8_t reset( bool force = true ); 00061 00062 00063 00064 00065 // Issue a 1-Wire rom select command, you do the reset first. 00066 void select(const uint8_t rom[8]); 00067 00068 // Issue a 1-Wire rom skip command, to address all on bus. 00069 void skip(void); 00070 00071 // Write a byte. If 'power' is one then the wire is held high at 00072 // the end for parasitically powered devices. You are responsible 00073 // for eventually depowering it by calling depower() or doing 00074 // another read or write. 00075 void write(uint8_t v, uint8_t power = 0); 00076 00077 void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0); 00078 00079 // Read a byte. 00080 uint8_t read(void); 00081 00082 void read_bytes(uint8_t *buf, uint16_t count); 00083 00084 // Write a bit. The bus is always left powered at the end, see 00085 // note in write() about that. 00086 void write_bit(uint8_t v); 00087 00088 // Read a bit. 00089 uint8_t read_bit(void); 00090 00091 // Stop forcing power onto the bus. You only need to do this if 00092 // you used the 'power' flag to write() or used a write_bit() call 00093 // and aren't about to do another read or write. You would rather 00094 // not leave this powered if you don't have to, just in case 00095 // someone shorts your bus. 00096 void depower(void); 00097 00098 #if ONEWIRE_SEARCH 00099 // Clear the search state so that if will start from the beginning again. 00100 void reset_search(); 00101 00102 // Setup the search to find the device type 'family_code' on the next call 00103 // to search(*newAddr) if it is present. 00104 void target_search(uint8_t family_code); 00105 00106 // Look for the next device. Returns 1 if a new address has been 00107 // returned. A zero might mean that the bus is shorted, there are 00108 // no devices, or you have already retrieved all of them. It 00109 // might be a good idea to check the CRC to make sure you didn't 00110 // get garbage. The order is deterministic. You will always get 00111 // the same devices in the same order. 00112 uint8_t search(uint8_t *newAddr); 00113 #endif 00114 00115 #if ONEWIRE_CRC 00116 // Compute a Dallas Semiconductor 8 bit CRC, these are used in the 00117 // ROM and scratchpad registers. 00118 static uint8_t crc8(const uint8_t *addr, uint8_t len); 00119 00120 #if ONEWIRE_CRC16 00121 // Compute the 1-Wire CRC16 and compare it against the received CRC. 00122 // Example usage (reading a DS2408): 00123 // // Put everything in a buffer so we can compute the CRC easily. 00124 // uint8_t buf[13]; 00125 // buf[0] = 0xF0; // Read PIO Registers 00126 // buf[1] = 0x88; // LSB address 00127 // buf[2] = 0x00; // MSB address 00128 // WriteBytes(net, buf, 3); // Write 3 cmd bytes 00129 // ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16 00130 // if (!CheckCRC16(buf, 11, &buf[11])) { 00131 // // Handle error. 00132 // } 00133 // 00134 // @param input - Array of bytes to checksum. 00135 // @param len - How many bytes to use. 00136 // @param inverted_crc - The two CRC16 bytes in the received data. 00137 // This should just point into the received data, 00138 // *not* at a 16-bit integer. 00139 // @param crc - The crc starting value (optional) 00140 // @return True, iff the CRC matches. 00141 static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0); 00142 00143 // Compute a Dallas Semiconductor 16 bit CRC. This is required to check 00144 // the integrity of data received from many 1-Wire devices. Note that the 00145 // CRC computed here is *not* what you'll get from the 1-Wire network, 00146 // for two reasons: 00147 // 1) The CRC is transmitted bitwise inverted. 00148 // 2) Depending on the endian-ness of your processor, the binary 00149 // representation of the two-byte return value may have a different 00150 // byte order than the two bytes you get from 1-Wire. 00151 // @param input - Array of bytes to checksum. 00152 // @param len - How many bytes to use. 00153 // @param crc - The crc starting value (optional) 00154 // @return The CRC16, as defined by Dallas Semiconductor. 00155 static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0); 00156 #endif 00157 #endif 00158 }; 00159 00160 #endif
Generated on Fri Jul 15 2022 06:24:29 by
1.7.2
