test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Committer:
ruslanbredun
Date:
Mon Dec 14 14:13:35 2020 +0000
Revision:
16:82251ada9b04
Parent:
11:32eeb052cda5
tester

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ommpy 11:32eeb052cda5 1 #ifndef OneWire_h
ommpy 11:32eeb052cda5 2 #define OneWire_h
ommpy 11:32eeb052cda5 3
ommpy 11:32eeb052cda5 4 #include <inttypes.h>
ommpy 11:32eeb052cda5 5 #include <mbed.h>
ommpy 11:32eeb052cda5 6
ommpy 11:32eeb052cda5 7 #if defined(TARGET_STM)
ommpy 11:32eeb052cda5 8 #define MODE() output(); \
ommpy 11:32eeb052cda5 9 mode(OpenDrain)
ommpy 11:32eeb052cda5 10 #define OUTPUT() // configured as output in the constructor and stays like that forever
ommpy 11:32eeb052cda5 11 #if defined(TARGET_STM32L072xx)
ommpy 11:32eeb052cda5 12 #define PORT ((GPIO_TypeDef *)(GPIOA_BASE + 0x0400 * STM_PORT(gpio.pin)))
ommpy 11:32eeb052cda5 13 #define PINMASK (1 << STM_PIN(gpio.pin))
ommpy 11:32eeb052cda5 14 #define INPUT() (PORT->MODER &= ~(GPIO_MODER_MODE0_0 << (STM_PIN(gpio.pin) * 2)))
ommpy 11:32eeb052cda5 15 #define READ() ((PORT->IDR & gpio.mask) != 0)
ommpy 11:32eeb052cda5 16 #define WRITE(x) (x == 1 ? PORT->BSRR = PINMASK : PORT->BRR = PINMASK)
ommpy 11:32eeb052cda5 17 #else
ommpy 11:32eeb052cda5 18 #define INPUT() (*gpio.reg_set = gpio.mask) // write 1 to open drain
ommpy 11:32eeb052cda5 19 #define READ() ((*gpio.reg_in & gpio.mask) != 0)
ommpy 11:32eeb052cda5 20 #define WRITE(x) write(x)
ommpy 11:32eeb052cda5 21 #endif
ommpy 11:32eeb052cda5 22 #else
ommpy 11:32eeb052cda5 23 #define MODE() mode(PullUp)
ommpy 11:32eeb052cda5 24 #define INPUT() input()
ommpy 11:32eeb052cda5 25 #define OUTPUT() output()
ommpy 11:32eeb052cda5 26 #define READ() read()
ommpy 11:32eeb052cda5 27 #define WRITE(x) write(x)
ommpy 11:32eeb052cda5 28 #endif
ommpy 11:32eeb052cda5 29
ommpy 11:32eeb052cda5 30 #ifdef TARGET_NORDIC
ommpy 11:32eeb052cda5 31 //NORDIC targets (NRF) use software delays since their ticker uses a 32kHz clock
ommpy 11:32eeb052cda5 32 static uint32_t loops_per_us = 0;
ommpy 11:32eeb052cda5 33
ommpy 11:32eeb052cda5 34 #define INIT_WAIT init_soft_delay()
ommpy 11:32eeb052cda5 35 #define WAIT_US(x) for(int cnt = 0; cnt < (x * loops_per_us) >> 5; cnt++) {__NOP(); __NOP(); __NOP();}
ommpy 11:32eeb052cda5 36
ommpy 11:32eeb052cda5 37 void init_soft_delay( void ) {
ommpy 11:32eeb052cda5 38 if (loops_per_us == 0) {
ommpy 11:32eeb052cda5 39 loops_per_us = 1;
ommpy 11:32eeb052cda5 40 Timer timey;
ommpy 11:32eeb052cda5 41 timey.start();
ommpy 11:32eeb052cda5 42 ONEWIRE_DELAY_US(320000);
ommpy 11:32eeb052cda5 43 timey.stop();
ommpy 11:32eeb052cda5 44 loops_per_us = (320000 + timey.read_us() / 2) / timey.read_us();
ommpy 11:32eeb052cda5 45 }
ommpy 11:32eeb052cda5 46 }
ommpy 11:32eeb052cda5 47 #else
ommpy 11:32eeb052cda5 48 #define INIT_WAIT
ommpy 11:32eeb052cda5 49 #define WAIT_US(x) wait_us(x)
ommpy 11:32eeb052cda5 50 #endif
ommpy 11:32eeb052cda5 51
ommpy 11:32eeb052cda5 52 // You can exclude certain features from OneWire. In theory, this
ommpy 11:32eeb052cda5 53 // might save some space. In practice, the compiler automatically
ommpy 11:32eeb052cda5 54 // removes unused code (technically, the linker, using -fdata-sections
ommpy 11:32eeb052cda5 55 // and -ffunction-sections when compiling, and Wl,--gc-sections
ommpy 11:32eeb052cda5 56 // when linking), so most of these will not result in any code size
ommpy 11:32eeb052cda5 57 // reduction. Well, unless you try to use the missing features
ommpy 11:32eeb052cda5 58 // and redesign your program to not need them! ONEWIRE_CRC8_TABLE
ommpy 11:32eeb052cda5 59 // is the exception, because it selects a fast but large algorithm
ommpy 11:32eeb052cda5 60 // or a small but slow algorithm.
ommpy 11:32eeb052cda5 61
ommpy 11:32eeb052cda5 62 // you can exclude onewire_search by defining that to 0
ommpy 11:32eeb052cda5 63 #ifndef ONEWIRE_SEARCH
ommpy 11:32eeb052cda5 64 #define ONEWIRE_SEARCH 1
ommpy 11:32eeb052cda5 65 #endif
ommpy 11:32eeb052cda5 66
ommpy 11:32eeb052cda5 67 // You can exclude CRC checks altogether by defining this to 0
ommpy 11:32eeb052cda5 68 #ifndef ONEWIRE_CRC
ommpy 11:32eeb052cda5 69 #define ONEWIRE_CRC 1
ommpy 11:32eeb052cda5 70 #endif
ommpy 11:32eeb052cda5 71
ommpy 11:32eeb052cda5 72 class OneWire : public DigitalInOut
ommpy 11:32eeb052cda5 73 {
ommpy 11:32eeb052cda5 74 int _sample_point_us;
ommpy 11:32eeb052cda5 75 int _out_to_in_transition_us;
ommpy 11:32eeb052cda5 76
ommpy 11:32eeb052cda5 77 #if ONEWIRE_SEARCH
ommpy 11:32eeb052cda5 78 // global search state
ommpy 11:32eeb052cda5 79 unsigned char ROM_NO[8];
ommpy 11:32eeb052cda5 80 uint8_t LastDiscrepancy;
ommpy 11:32eeb052cda5 81 uint8_t LastFamilyDiscrepancy;
ommpy 11:32eeb052cda5 82 uint8_t LastDeviceFlag;
ommpy 11:32eeb052cda5 83 #endif
ommpy 11:32eeb052cda5 84
ommpy 11:32eeb052cda5 85 public:
ommpy 11:32eeb052cda5 86 OneWire(PinName pin, int sample_point_us = 13);
ommpy 11:32eeb052cda5 87
ommpy 11:32eeb052cda5 88 // Perform a 1-Wire reset cycle. Returns 1 if a device responds
ommpy 11:32eeb052cda5 89 // with a presence pulse. Returns 0 if there is no device or the
ommpy 11:32eeb052cda5 90 // bus is shorted or otherwise held low for more than 250uS
ommpy 11:32eeb052cda5 91 uint8_t reset(void);
ommpy 11:32eeb052cda5 92
ommpy 11:32eeb052cda5 93 // Issue a 1-Wire rom select command, you do the reset first.
ommpy 11:32eeb052cda5 94 void select(const uint8_t rom[8]);
ommpy 11:32eeb052cda5 95
ommpy 11:32eeb052cda5 96 // Issue a 1-Wire rom skip command, to address all on bus.
ommpy 11:32eeb052cda5 97 void skip(void);
ommpy 11:32eeb052cda5 98
ommpy 11:32eeb052cda5 99 // Write a byte. If 'power' is one then the wire is held high at
ommpy 11:32eeb052cda5 100 // the end for parasitically powered devices. You are responsible
ommpy 11:32eeb052cda5 101 // for eventually depowering it by calling depower() or doing
ommpy 11:32eeb052cda5 102 // another read or write.
ommpy 11:32eeb052cda5 103 void write_byte(uint8_t v, uint8_t power = 0);
ommpy 11:32eeb052cda5 104
ommpy 11:32eeb052cda5 105 void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0);
ommpy 11:32eeb052cda5 106
ommpy 11:32eeb052cda5 107 // Read a byte.
ommpy 11:32eeb052cda5 108 uint8_t read_byte(void);
ommpy 11:32eeb052cda5 109
ommpy 11:32eeb052cda5 110 void read_bytes(uint8_t *buf, uint16_t count);
ommpy 11:32eeb052cda5 111
ommpy 11:32eeb052cda5 112 // Write a bit. The bus is always left powered at the end, see
ommpy 11:32eeb052cda5 113 // note in write() about that.
ommpy 11:32eeb052cda5 114 void write_bit(uint8_t v);
ommpy 11:32eeb052cda5 115
ommpy 11:32eeb052cda5 116 // Read a bit.
ommpy 11:32eeb052cda5 117 uint8_t read_bit(void);
ommpy 11:32eeb052cda5 118
ommpy 11:32eeb052cda5 119 // Stop forcing power onto the bus. You only need to do this if
ommpy 11:32eeb052cda5 120 // you used the 'power' flag to write() or used a write_bit() call
ommpy 11:32eeb052cda5 121 // and aren't about to do another read or write. You would rather
ommpy 11:32eeb052cda5 122 // not leave this powered if you don't have to, just in case
ommpy 11:32eeb052cda5 123 // someone shorts your bus.
ommpy 11:32eeb052cda5 124 void depower(void);
ommpy 11:32eeb052cda5 125
ommpy 11:32eeb052cda5 126 #if ONEWIRE_SEARCH
ommpy 11:32eeb052cda5 127 // Clear the search state so that if will start from the beginning again.
ommpy 11:32eeb052cda5 128 void reset_search();
ommpy 11:32eeb052cda5 129
ommpy 11:32eeb052cda5 130 // Setup the search to find the device type 'family_code' on the next call
ommpy 11:32eeb052cda5 131 // to search(*newAddr) if it is present.
ommpy 11:32eeb052cda5 132 void target_search(uint8_t family_code);
ommpy 11:32eeb052cda5 133
ommpy 11:32eeb052cda5 134 // Look for the next device. Returns 1 if a new address has been
ommpy 11:32eeb052cda5 135 // returned. A zero might mean that the bus is shorted, there are
ommpy 11:32eeb052cda5 136 // no devices, or you have already retrieved all of them. It
ommpy 11:32eeb052cda5 137 // might be a good idea to check the CRC to make sure you didn't
ommpy 11:32eeb052cda5 138 // get garbage. The order is deterministic. You will always get
ommpy 11:32eeb052cda5 139 // the same devices in the same order.
ommpy 11:32eeb052cda5 140 uint8_t search(uint8_t *newAddr);
ommpy 11:32eeb052cda5 141 #endif
ommpy 11:32eeb052cda5 142
ommpy 11:32eeb052cda5 143 #if ONEWIRE_CRC
ommpy 11:32eeb052cda5 144 // Compute a Dallas Semiconductor 8 bit CRC, these are used in the
ommpy 11:32eeb052cda5 145 // ROM and scratchpad registers.
ommpy 11:32eeb052cda5 146 static uint8_t crc8(const uint8_t *addr, uint8_t len);
ommpy 11:32eeb052cda5 147
ommpy 11:32eeb052cda5 148 #if ONEWIRE_CRC16
ommpy 11:32eeb052cda5 149 // Compute the 1-Wire CRC16 and compare it against the received CRC.
ommpy 11:32eeb052cda5 150 // Example usage (reading a DS2408):
ommpy 11:32eeb052cda5 151 // // Put everything in a buffer so we can compute the CRC easily.
ommpy 11:32eeb052cda5 152 // uint8_t buf[13];
ommpy 11:32eeb052cda5 153 // buf[0] = 0xF0; // Read PIO Registers
ommpy 11:32eeb052cda5 154 // buf[1] = 0x88; // LSB address
ommpy 11:32eeb052cda5 155 // buf[2] = 0x00; // MSB address
ommpy 11:32eeb052cda5 156 // WriteBytes(net, buf, 3); // Write 3 cmd bytes
ommpy 11:32eeb052cda5 157 // ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16
ommpy 11:32eeb052cda5 158 // if (!CheckCRC16(buf, 11, &buf[11])) {
ommpy 11:32eeb052cda5 159 // // Handle error.
ommpy 11:32eeb052cda5 160 // }
ommpy 11:32eeb052cda5 161 //
ommpy 11:32eeb052cda5 162 // @param input - Array of bytes to checksum.
ommpy 11:32eeb052cda5 163 // @param len - How many bytes to use.
ommpy 11:32eeb052cda5 164 // @param inverted_crc - The two CRC16 bytes in the received data.
ommpy 11:32eeb052cda5 165 // This should just point into the received data,
ommpy 11:32eeb052cda5 166 // *not* at a 16-bit integer.
ommpy 11:32eeb052cda5 167 // @param crc - The crc starting value (optional)
ommpy 11:32eeb052cda5 168 // @return True, iff the CRC matches.
ommpy 11:32eeb052cda5 169 static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0);
ommpy 11:32eeb052cda5 170
ommpy 11:32eeb052cda5 171 // Compute a Dallas Semiconductor 16 bit CRC. This is required to check
ommpy 11:32eeb052cda5 172 // the integrity of data received from many 1-Wire devices. Note that the
ommpy 11:32eeb052cda5 173 // CRC computed here is *not* what you'll get from the 1-Wire network,
ommpy 11:32eeb052cda5 174 // for two reasons:
ommpy 11:32eeb052cda5 175 // 1) The CRC is transmitted bitwise inverted.
ommpy 11:32eeb052cda5 176 // 2) Depending on the endian-ness of your processor, the binary
ommpy 11:32eeb052cda5 177 // representation of the two-byte return value may have a different
ommpy 11:32eeb052cda5 178 // byte order than the two bytes you get from 1-Wire.
ommpy 11:32eeb052cda5 179 // @param input - Array of bytes to checksum.
ommpy 11:32eeb052cda5 180 // @param len - How many bytes to use.
ommpy 11:32eeb052cda5 181 // @param crc - The crc starting value (optional)
ommpy 11:32eeb052cda5 182 // @return The CRC16, as defined by Dallas Semiconductor.
ommpy 11:32eeb052cda5 183 static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0);
ommpy 11:32eeb052cda5 184 #endif
ommpy 11:32eeb052cda5 185 #endif
ommpy 11:32eeb052cda5 186 };
ommpy 11:32eeb052cda5 187
ommpy 11:32eeb052cda5 188 #endif
ommpy 11:32eeb052cda5 189
ommpy 11:32eeb052cda5 190