OneWire API

OneWire driver optimized for GSM module.

Header File

Source: include/driver/onewire.h

#include <driver/onewire.h>

Functions

int OneWire(uint8_t pin)

Register OneWire bus

Parameters:

pin – [in] IO pin number gpioname_e

Returns:

On success, returns handle to OneWire Bus object. 0 on error (no memory)

uint8_t ow_reset(int handle)

Perform a 1-Wire reset cycle. Returns 1 if a device responds with a presence pulse. Returns 0 if there is no device or the bus is shorted or otherwise held low for more than 250uS

Parameters:

handle – [in] OneWire bus handle

Returns:

void ow_select(int handle, const uint8_t rom[8])

Issue a 1-Wire ROM select command (0x55), you do the reset first.

Parameters:
  • handle – [in] OneWire bus handle

  • rom – [in] ROM address to select

void ow_skip(int handle)

Issue a 1-Wire ROM skip command (0xCC), to address all on bus.

Parameters:

handle – [in] OneWire bus handle

void ow_write(int handle, uint8_t val, uint8_t power)

Write a byte. If ‘power’ is one then the wire is held high at the end for parasitically powered devices. You are responsible for eventually depowering it by calling ow_depower() or doing another read or write.

Parameters:
  • handle – [in] OneWire bus handle

  • val – [in] Input byte

  • power – [in] Power state after write

void ow_write_bytes(int handle, const uint8_t *buf, uint16_t count, uint8_t power)

Write multiple bytes on OneWire bus

See also

ow_write()

Parameters:
  • handle – [in] OneWire bus handle

  • buf – [in] Buffer containing data to send

  • count – [in] Length of data in buffer

  • power – [in] Power state after write

uint8_t ow_read(int handle)

Read one byte from OneWire

Parameters:

handle – [in] OneWire bus handle

Returns:

Read byte

void ow_read_bytes(int handle, uint8_t *buf, uint16_t count)

Read multiple bytes from OneWire bus

Parameters:
  • handle – [in] OneWire bus handle

  • buf – [out] Buffer to store data

  • count – [in] length of data to read

void ow_write_bit(int handle, uint8_t v)

Write one bit on bus. The bus is always left powered at the end

See also

ow_write

Parameters:
  • handle – [in] OneWire bus handle

  • v – [in] Bit value

uint8_t ow_read_bit(int handle)

Read one bit from bus

Parameters:

handle – [in] OneWire bus handle

Returns:

Bit value read

void ow_depower(int handle)

Stop forcing power onto the bus. You only need to do this if you used the ‘power’ flag to ow_write() or used a ow_write_bit() call and aren’t about to do another read or write. You would rather not leave this powered if you don’t have to, just in case someone shorts your bus.

Parameters:

handle – [in] OneWire bus handle

void ow_reset_search(int handle)

Clear the search state so that if will start from the beginning again.

Parameters:

handle – [in] OneWire bus handle

void ow_target_search(int handle, uint8_t family_code)

Setup the search to find the device type ‘family_code’ on the next call to search(*newAddr) if it is present.

Parameters:
  • handle – [in] OneWire bus handle

  • family_code – [in] Family code to search

uint8_t ow_search(int handle, uint8_t *newAddr)

Perform a Normal search on OneWire bus (0xF0) Look for the next device. Returns 1 if a new address has been returned. A zero might mean that the bus is shorted, there are no devices, or you have already retrieved all of them. It might be a good idea to check the CRC to make sure you didn’t get garbage. The order is deterministic. You will always get the same devices in the same order.

Parameters:
  • handle – [in] OneWire bus handle

  • newAddr – [out] Buffer to store address of newly found device

Returns:

returns true if new address found, false if not found

uint8_t ow_search_cond(int handle, uint8_t *newAddr)

Perform a conditional search on OneWire bus (0xEC)

See also

ow_search

Parameters:
  • handle – [in] OneWire bus handle

  • newAddr – [out] Buffer to store address of newly found device

Returns:

returns true if new address found, false if not found

uint8_t ow_crc8(const uint8_t *addr, uint8_t len)

Compute a Dallas Semiconductor 8 bit CRC, these are used in the ROM and scratchpad registers.

Parameters:
  • addr – [in] Input buffer to calculate CRC on

  • len – [in] Length of input data

Returns:

CRC value

uint8_t ow_check_crc16(const uint8_t *input, uint16_t len, const uint8_t *inverted_crc, uint16_t crc)

Compute the 1-Wire CRC16 and compare it against the received CRC.

Example usage (reading a DS2408):

// Put everything in a buffer so we can compute the CRC easily. uint8_t buf[13]; buf[0] = 0xF0; // Read PIO Registers buf[1] = 0x88; // LSB address buf[2] = 0x00; // MSB address ow_write_bytes(handle, net, buf, 3); // Write 3 cmd bytes ow_read_bytes(handle net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16 if (!ow_check_crc16(buf, 11, &buf[11])) { // Handle error. }

Parameters:
  • input – [in] Array of bytes to checksum.

  • len – [in] How many bytes to use.

  • inverted_crc – [out] The two CRC16 bytes in the received data. This should just point into the received data, not at a 16-bit integer.

  • crc – [in] The crc starting value (0 if not used)

Returns:

True, if the CRC matches.

uint16_t ow_crc16(const uint8_t *input, uint16_t len, uint16_t crc)

Compute a Dallas Semiconductor 16 bit CRC. This is required to check the integrity of data received from many 1-Wire devices. Note that the CRC computed here is not what you’ll get from the 1-Wire network, for two reasons: 1) The CRC is transmitted bitwise inverted. 2) Depending on the endian-ness of your processor, the binary representation of the two-byte return value may have a different byte order than the two bytes you get from 1-Wire.

Parameters:
  • input – [in] Array of bytes to checksum.

  • len – [in] How many bytes to use.

  • crc – [in] The crc starting value (0 if not used)

Returns:

The CRC16, as defined by Dallas Semiconductor.

Macros

OW_ADDR_LEN