Multiple cleanups/tweaks for onewire driver

Use onewire_addr_t for onewire addresses
Move internal defines out of onewire.h
Remove global variables for search state
use taskENTER_CRITICAL instead of portDISABLE_INTERRUPTS
remove unnecessary onewire_init function
Remove unnecessary critical sections
Use GPIO_OUT_OPEN_DRAIN
reformat/style cleanup
This commit is contained in:
Alex Stewart 2016-03-14 21:59:39 -07:00
parent 02c35d8a71
commit a2b9d688ea
4 changed files with 304 additions and 359 deletions

View file

@ -29,75 +29,72 @@
#define ONEWIRE_CRC8_TABLE 0
#endif
// Platform specific I/O definitions
#define noInterrupts portDISABLE_INTERRUPTS
#define interrupts portENABLE_INTERRUPTS
#define delayMicroseconds sdk_os_delay_us
typedef uint64_t onewire_addr_t;
#define DIRECT_READ(pin) gpio_read(pin)
#define DIRECT_MODE_INPUT(pin) gpio_enable(pin, GPIO_INPUT)
#define DIRECT_MODE_OUTPUT(pin) gpio_enable(pin, GPIO_OUTPUT)
#define DIRECT_WRITE_LOW(pin) gpio_write(pin, 0)
#define DIRECT_WRITE_HIGH(pin) gpio_write(pin, 1)
typedef struct {
uint8_t rom_no[8];
uint8_t last_discrepancy;
bool last_device_found;
} onewire_search_t;
void onewire_init(uint8_t pin);
// The following is an invalid ROM address that will never occur in a device
// (CRC mismatch), and so can be useful as an indicator for "no-such-device",
// etc.
#define ONEWIRE_NONE ((onewire_addr_t)(0xffffffffffffffffLL))
// 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
uint8_t onewire_reset(uint8_t pin);
bool onewire_reset(int pin);
// Issue a 1-Wire rom select command, you do the reset first.
void onewire_select(uint8_t pin, const uint8_t rom[8]);
void onewire_select(int pin, const onewire_addr_t rom);
// Issue a 1-Wire rom skip command, to address all on bus.
void onewire_skip(uint8_t pin);
void onewire_skip_rom(int pin);
// 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 depower() or doing
// another read or write.
void onewire_write(uint8_t pin, uint8_t v, uint8_t power);
// Write a byte. The writing code uses open-drain mode and expects the pullup
// resistor to pull the line high when not driven low. If you need strong
// power after the write (e.g. DS18B20 in parasite power mode) then call
// onewire_power() after this is complete to actively drive the line high.
void onewire_write(int pin, uint8_t v);
void onewire_write_bytes(uint8_t pin, const uint8_t *buf, uint16_t count, bool power);
void onewire_write_bytes(int pin, const uint8_t *buf, size_t count);
// Read a byte.
uint8_t onewire_read(uint8_t pin);
uint8_t onewire_read(int pin);
void onewire_read_bytes(uint8_t pin, uint8_t *buf, uint16_t count);
void onewire_read_bytes(int pin, uint8_t *buf, size_t count);
// Write a bit. The bus is always left powered at the end, see
// note in write() about that.
// void onewire_write_bit(uint8_t pin, uint8_t v);
// Read a bit.
// uint8_t onewire_read_bit(uint8_t pin);
// Actively drive the bus high to provide extra power for certain operations of
// parasitically-powered devices.
void onewire_power(int pin);
// Stop forcing power onto the bus. You only need to do this if
// you used the 'power' flag to write() or used a 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.
void onewire_depower(uint8_t pin);
// you previously called onewire_power() to drive the bus high and now want to
// allow it to float instead. Note that onewire_reset() will also
// automatically depower the bus first, so you do not need to call this first
// if you just want to start a new operation.
void onewire_depower(int pin);
// Clear the search state so that if will start from the beginning again.
void onewire_reset_search(uint8_t pin);
void onewire_search_start(onewire_search_t *search);
// Setup the search to find the device type 'family_code' on the next call
// to search(*newAddr) if it is present.
void onewire_target_search(uint8_t pin, uint8_t family_code);
void onewire_search_prefix(onewire_search_t *search, uint8_t family_code);
// 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.
uint8_t onewire_search(uint8_t pin, uint8_t *newAddr);
// Look for the next device. Returns the address of the next device on the bus,
// or ONEWIRE_NONE if there is no next address. ONEWIRE_NONE 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.
onewire_addr_t onewire_search_next(onewire_search_t *search, int pin);
// Compute a Dallas Semiconductor 8 bit CRC, these are used in the
// ROM and scratchpad registers.
uint8_t onewire_crc8(const uint8_t *addr, uint8_t len);
uint8_t onewire_crc8(const uint8_t *data, uint8_t len);
// Compute the 1-Wire CRC16 and compare it against the received CRC.
// Example usage (reading a DS2408):
@ -117,9 +114,9 @@ uint8_t onewire_crc8(const uint8_t *addr, uint8_t len);
// @param inverted_crc - The two CRC16 bytes in the received data.
// This should just point into the received data,
// *not* at a 16-bit integer.
// @param crc - The crc starting value (optional)
// @param crc_iv - The crc starting value (optional)
// @return True, iff the CRC matches.
bool onewire_check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc);
bool onewire_check_crc16(const uint8_t* input, size_t len, const uint8_t* inverted_crc, uint16_t crc_iv);
// 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
@ -131,8 +128,8 @@ bool onewire_check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inve
// byte order than the two bytes you get from 1-Wire.
// @param input - Array of bytes to checksum.
// @param len - How many bytes to use.
// @param crc - The crc starting value (optional)
// @param crc_iv - The crc starting value (optional)
// @return The CRC16, as defined by Dallas Semiconductor.
uint16_t onewire_crc16(const uint8_t* input, uint16_t len, uint16_t crc);
uint16_t onewire_crc16(const uint8_t* input, size_t len, uint16_t crc_iv);
#endif