Working versione for L-tek FF1705
Embed:
(wiki syntax)
Show/hide line numbers
OneWire.h
00001 #ifndef OneWire_h 00002 #define OneWire_h 00003 00004 #include <inttypes.h> 00005 #include <mbed.h> 00006 00007 #if defined(TARGET_STM) 00008 #define MODE() output(); \ 00009 mode(OpenDrain) 00010 #define INPUT() (*gpio.reg_set = gpio.mask) // write 1 to open drain 00011 #define OUTPUT() // configured as output in the constructor and stays like that forever 00012 #define READ() ((*gpio.reg_in & gpio.mask) != 0) 00013 #define WRITE(x) write(x) 00014 #else 00015 #define MODE() mode(PullUp) 00016 #define INPUT() input() 00017 #define OUTPUT() output() 00018 #define READ() read() 00019 #define WRITE(x) write(x) 00020 #endif 00021 00022 #ifdef TARGET_NORDIC 00023 //NORDIC targets (NRF) use software delays since their ticker uses a 32kHz clock 00024 static uint32_t loops_per_us = 0; 00025 00026 #define INIT_WAIT init_soft_delay() 00027 #define WAIT_US(x) for(int cnt = 0; cnt < (x * loops_per_us) >> 5; cnt++) {__NOP(); __NOP(); __NOP();} 00028 00029 void init_soft_delay( void ) { 00030 if (loops_per_us == 0) { 00031 loops_per_us = 1; 00032 Timer timey; 00033 timey.start(); 00034 ONEWIRE_DELAY_US(320000); 00035 timey.stop(); 00036 loops_per_us = (320000 + timey.read_us() / 2) / timey.read_us(); 00037 } 00038 } 00039 #else 00040 #define INIT_WAIT 00041 #define WAIT_US(x) wait_us(x) 00042 #endif 00043 00044 // You can exclude certain features from OneWire. In theory, this 00045 // might save some space. In practice, the compiler automatically 00046 // removes unused code (technically, the linker, using -fdata-sections 00047 // and -ffunction-sections when compiling, and Wl,--gc-sections 00048 // when linking), so most of these will not result in any code size 00049 // reduction. Well, unless you try to use the missing features 00050 // and redesign your program to not need them! ONEWIRE_CRC8_TABLE 00051 // is the exception, because it selects a fast but large algorithm 00052 // or a small but slow algorithm. 00053 00054 // you can exclude onewire_search by defining that to 0 00055 #ifndef ONEWIRE_SEARCH 00056 #define ONEWIRE_SEARCH 1 00057 #endif 00058 00059 // You can exclude CRC checks altogether by defining this to 0 00060 #ifndef ONEWIRE_CRC 00061 #define ONEWIRE_CRC 1 00062 #endif 00063 00064 class OneWire : public DigitalInOut 00065 { 00066 Timer timer; 00067 00068 #if ONEWIRE_SEARCH 00069 // global search state 00070 unsigned char ROM_NO[8]; 00071 uint8_t LastDiscrepancy; 00072 uint8_t LastFamilyDiscrepancy; 00073 uint8_t LastDeviceFlag; 00074 #endif 00075 00076 public: 00077 OneWire(PinName pin); 00078 00079 // Perform a 1-Wire reset cycle. Returns 1 if a device responds 00080 // with a presence pulse. Returns 0 if there is no device or the 00081 // bus is shorted or otherwise held low for more than 250uS 00082 uint8_t reset(void); 00083 00084 // Issue a 1-Wire rom select command, you do the reset first. 00085 void select(const uint8_t rom[8]); 00086 00087 // Issue a 1-Wire rom skip command, to address all on bus. 00088 void skip(void); 00089 00090 // Write a byte. If 'power' is one then the wire is held high at 00091 // the end for parasitically powered devices. You are responsible 00092 // for eventually depowering it by calling depower() or doing 00093 // another read or write. 00094 void write_byte(uint8_t v, uint8_t power = 0); 00095 00096 void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0); 00097 00098 // Read a byte. 00099 uint8_t read_byte(void); 00100 00101 void read_bytes(uint8_t *buf, uint16_t count); 00102 00103 // Write a bit. The bus is always left powered at the end, see 00104 // note in write() about that. 00105 void write_bit(uint8_t v); 00106 00107 // Read a bit. 00108 uint8_t read_bit(void); 00109 00110 // Stop forcing power onto the bus. You only need to do this if 00111 // you used the 'power' flag to write() or used a write_bit() call 00112 // and aren't about to do another read or write. You would rather 00113 // not leave this powered if you don't have to, just in case 00114 // someone shorts your bus. 00115 void depower(void); 00116 00117 #if ONEWIRE_SEARCH 00118 // Clear the search state so that if will start from the beginning again. 00119 void reset_search(); 00120 00121 // Setup the search to find the device type 'family_code' on the next call 00122 // to search(*newAddr) if it is present. 00123 void target_search(uint8_t family_code); 00124 00125 // Look for the next device. Returns 1 if a new address has been 00126 // returned. A zero might mean that the bus is shorted, there are 00127 // no devices, or you have already retrieved all of them. It 00128 // might be a good idea to check the CRC to make sure you didn't 00129 // get garbage. The order is deterministic. You will always get 00130 // the same devices in the same order. 00131 uint8_t search(uint8_t *newAddr); 00132 #endif 00133 00134 #if ONEWIRE_CRC 00135 // Compute a Dallas Semiconductor 8 bit CRC, these are used in the 00136 // ROM and scratchpad registers. 00137 static uint8_t crc8(const uint8_t *addr, uint8_t len); 00138 00139 #if ONEWIRE_CRC16 00140 // Compute the 1-Wire CRC16 and compare it against the received CRC. 00141 // Example usage (reading a DS2408): 00142 // // Put everything in a buffer so we can compute the CRC easily. 00143 // uint8_t buf[13]; 00144 // buf[0] = 0xF0; // Read PIO Registers 00145 // buf[1] = 0x88; // LSB address 00146 // buf[2] = 0x00; // MSB address 00147 // WriteBytes(net, buf, 3); // Write 3 cmd bytes 00148 // ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16 00149 // if (!CheckCRC16(buf, 11, &buf[11])) { 00150 // // Handle error. 00151 // } 00152 // 00153 // @param input - Array of bytes to checksum. 00154 // @param len - How many bytes to use. 00155 // @param inverted_crc - The two CRC16 bytes in the received data. 00156 // This should just point into the received data, 00157 // *not* at a 16-bit integer. 00158 // @param crc - The crc starting value (optional) 00159 // @return True, iff the CRC matches. 00160 static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0); 00161 00162 // Compute a Dallas Semiconductor 16 bit CRC. This is required to check 00163 // the integrity of data received from many 1-Wire devices. Note that the 00164 // CRC computed here is *not* what you'll get from the 1-Wire network, 00165 // for two reasons: 00166 // 1) The CRC is transmitted bitwise inverted. 00167 // 2) Depending on the endian-ness of your processor, the binary 00168 // representation of the two-byte return value may have a different 00169 // byte order than the two bytes you get from 1-Wire. 00170 // @param input - Array of bytes to checksum. 00171 // @param len - How many bytes to use. 00172 // @param crc - The crc starting value (optional) 00173 // @return The CRC16, as defined by Dallas Semiconductor. 00174 static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0); 00175 #endif 00176 #endif 00177 }; 00178 00179 #endif 00180 00181
Generated on Wed Jul 27 2022 22:37:19 by
1.7.2