Merge pull request #121 from foogod/ds18b20-updates

DS18B20 API Improvements
This commit is contained in:
Angus Gratton 2016-04-07 08:42:02 +10:00
commit 83c5f91bc0
6 changed files with 830 additions and 483 deletions

View file

@ -19,15 +19,14 @@
// DS18B20 driver // DS18B20 driver
#include "ds18b20/ds18b20.h" #include "ds18b20/ds18b20.h"
// Onewire init
#include "onewire/onewire.h"
void broadcast_temperature(void *pvParameters) void broadcast_temperature(void *pvParameters)
{ {
uint8_t amount = 0; uint8_t amount = 0;
uint8_t sensors = 2; uint8_t sensors = 1;
ds_sensor_t t[sensors]; ds18b20_addr_t addrs[sensors];
float results[sensors];
// Use GPIO 13 as one wire pin. // Use GPIO 13 as one wire pin.
uint8_t GPIO_FOR_ONE_WIRE = 13; uint8_t GPIO_FOR_ONE_WIRE = 13;
@ -36,8 +35,6 @@ void broadcast_temperature(void *pvParameters)
// Broadcaster part // Broadcaster part
err_t err; err_t err;
// Initialize one wire bus.
onewire_init(GPIO_FOR_ONE_WIRE);
while(1) { while(1) {
@ -66,18 +63,17 @@ void broadcast_temperature(void *pvParameters)
for(;;) { for(;;) {
// Search all DS18B20, return its amount and feed 't' structure with result data. // Search all DS18B20, return its amount and feed 't' structure with result data.
amount = ds18b20_read_all(GPIO_FOR_ONE_WIRE, t); amount = ds18b20_scan_devices(GPIO_FOR_ONE_WIRE, addrs, sensors);
if (amount < sensors){ if (amount < sensors){
printf("Something is wrong, I expect to see %d sensors \nbut just %d was detected!\n", sensors, amount); printf("Something is wrong, I expect to see %d sensors \nbut just %d was detected!\n", sensors, amount);
} }
for (int i = 0; i < amount; ++i) ds18b20_measure_and_read_multi(GPIO_FOR_ONE_WIRE, addrs, sensors, results);
for (int i = 0; i < sensors; ++i)
{ {
int intpart = (int)t[i].value; // ("\xC2\xB0" is the degree character (U+00B0) in UTF-8)
int fraction = (int)((t[i].value - intpart) * 100); sprintf(msg, "Sensor %08x%08x reports: %f \xC2\xB0""C\n", (uint32_t)(addrs[i] >> 32), (uint32_t)addrs[i], results[i]);
// Multiple "" here is just to satisfy compiler and don`t raise 'hex escape sequence out of range' warning.
sprintf(msg, "Sensor %d report: %d.%02d ""\xC2""\xB0""C\n",t[i].id, intpart, fraction);
printf("%s", msg); printf("%s", msg);
struct netbuf* buf = netbuf_new(); struct netbuf* buf = netbuf_new();

View file

@ -1,59 +1,78 @@
/* ds18b20 - Retrieves temperature from ds18b20 sensors and print it out. /* ds18b20_onewire.c - Retrieves readings from one or more DS18B20 temperature
* sensors, and prints the results to stdout.
* *
* This sample code is in the public domain., * This sample code is in the public domain.,
*/ */
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
#include "timers.h" #include "esp/uart.h"
#include "queue.h"
// DS18B20 driver
#include "ds18b20/ds18b20.h" #include "ds18b20/ds18b20.h"
// Onewire init
#include "onewire/onewire.h"
void print_temperature(void *pvParameters) #define SENSOR_GPIO 13
{ #define MAX_SENSORS 8
int delay = 500; #define RESCAN_INTERVAL 8
uint8_t amount = 0; #define LOOP_DELAY_MS 250
// Declare amount of sensors
uint8_t sensors = 2; void print_temperature(void *pvParameters) {
ds_sensor_t t[sensors]; ds18b20_addr_t addrs[MAX_SENSORS];
float temps[MAX_SENSORS];
// Use GPIO 13 as one wire pin. int sensor_count;
uint8_t GPIO_FOR_ONE_WIRE = 13;
onewire_init(GPIO_FOR_ONE_WIRE);
// There is no special initialization required before using the ds18b20
// routines. However, we make sure that the internal pull-up resistor is
// enabled on the GPIO pin so that one can connect up a sensor without
// needing an external pull-up (Note: The internal (~47k) pull-ups of the
// ESP8266 do appear to work, at least for simple setups (one or two sensors
// connected with short leads), but do not technically meet the pull-up
// requirements from the DS18B20 datasheet and may not always be reliable.
// For a real application, a proper 4.7k external pull-up resistor is
// recommended instead!)
gpio_set_pullup(SENSOR_GPIO, true, true);
while(1) { while(1) {
// Search all DS18B20, return its amount and feed 't' structure with result data. // Every RESCAN_INTERVAL samples, check to see if the sensors connected
amount = ds18b20_read_all(GPIO_FOR_ONE_WIRE, t); // to our bus have changed.
sensor_count = ds18b20_scan_devices(SENSOR_GPIO, addrs, MAX_SENSORS);
if (amount < sensors){ if (sensor_count < 1) {
printf("Something is wrong, I expect to see %d sensors \nbut just %d was detected!\n", sensors, amount); printf("\nNo sensors detected!\n");
} } else {
printf("\n%d sensors detected:\n", sensor_count);
// If there were more sensors found than we have space to handle,
// just report the first MAX_SENSORS..
if (sensor_count > MAX_SENSORS) sensor_count = MAX_SENSORS;
for (int i = 0; i < amount; ++i) // Do a number of temperature samples, and print the results.
{ for (int i = 0; i < RESCAN_INTERVAL; i++) {
int intpart = (int)t[i].value; ds18b20_measure_and_read_multi(SENSOR_GPIO, addrs, sensor_count, temps);
int fraction = (int)((t[i].value - intpart) * 100); for (int j = 0; j < sensor_count; j++) {
// Multiple "" here is just to satisfy compiler and don`t raise 'hex escape sequence out of range' warning. // The DS18B20 address is a 64-bit integer, but newlib-nano
printf("Sensor %d report: %d.%02d ""\xC2""\xB0""C\n",t[i].id, intpart, fraction); // printf does not support printing 64-bit values, so we
// split it up into two 32-bit integers and print them
// back-to-back to make it look like one big hex number.
uint32_t addr0 = addrs[j] >> 32;
uint32_t addr1 = addrs[j];
float temp_c = temps[j];
float temp_f = (temp_c * 1.8) + 32;
printf(" Sensor %08x%08x reports %f deg C (%f deg F)\n", addr0, addr1, temp_c, temp_f);
}
printf("\n");
// Wait for a little bit between each sample (note that the
// ds18b20_measure_and_read_multi operation already takes at
// least 750ms to run, so this is on top of that delay).
vTaskDelay(LOOP_DELAY_MS / portTICK_RATE_MS);
}
} }
printf("\n");
vTaskDelay(delay / portTICK_RATE_MS);
} }
} }
void user_init(void) void user_init(void) {
{
uart_set_baud(0, 115200); uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
xTaskCreate(&print_temperature, (signed char *)"print_temperature", 256, NULL, 2, NULL); xTaskCreate(&print_temperature, (signed char *)"print_temperature", 256, NULL, 2, NULL);
} }

View file

@ -1,43 +1,47 @@
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
#include "math.h"
#include "onewire/onewire.h"
#include "ds18b20.h" #include "ds18b20.h"
#define DS1820_WRITE_SCRATCHPAD 0x4E #define DS18B20_WRITE_SCRATCHPAD 0x4E
#define DS1820_READ_SCRATCHPAD 0xBE #define DS18B20_READ_SCRATCHPAD 0xBE
#define DS1820_COPY_SCRATCHPAD 0x48 #define DS18B20_COPY_SCRATCHPAD 0x48
#define DS1820_READ_EEPROM 0xB8 #define DS18B20_READ_EEPROM 0xB8
#define DS1820_READ_PWRSUPPLY 0xB4 #define DS18B20_READ_PWRSUPPLY 0xB4
#define DS1820_SEARCHROM 0xF0 #define DS18B20_SEARCHROM 0xF0
#define DS1820_SKIP_ROM 0xCC #define DS18B20_SKIP_ROM 0xCC
#define DS1820_READROM 0x33 #define DS18B20_READROM 0x33
#define DS1820_MATCHROM 0x55 #define DS18B20_MATCHROM 0x55
#define DS1820_ALARMSEARCH 0xEC #define DS18B20_ALARMSEARCH 0xEC
#define DS1820_CONVERT_T 0x44 #define DS18B20_CONVERT_T 0x44
#define os_sleep_ms(x) vTaskDelay(((x) + portTICK_RATE_MS - 1) / portTICK_RATE_MS)
uint8_t ds18b20_read_all(uint8_t pin, ds_sensor_t *result) { uint8_t ds18b20_read_all(uint8_t pin, ds_sensor_t *result) {
onewire_addr_t addr;
uint8_t addr[8]; onewire_search_t search;
uint8_t sensor_id = 0; uint8_t sensor_id = 0;
onewire_reset_search(pin);
onewire_search_start(&search);
while(onewire_search(pin, addr)){ while ((addr = onewire_search_next(&search, pin)) != ONEWIRE_NONE) {
uint8_t crc = onewire_crc8(addr, 7); uint8_t crc = onewire_crc8((uint8_t *)&addr, 7);
if (crc != addr[7]){ if (crc != (addr >> 56)){
printf("CRC check failed: %02X %02X\n", addr[7], crc); printf("CRC check failed: %02X %02X\n", (unsigned)(addr >> 56), crc);
return 0; return 0;
} }
onewire_reset(pin); onewire_reset(pin);
onewire_select(pin, addr); onewire_select(pin, addr);
onewire_write(pin, DS1820_CONVERT_T, ONEWIRE_DEFAULT_POWER); onewire_write(pin, DS18B20_CONVERT_T);
onewire_power(pin);
vTaskDelay(750 / portTICK_RATE_MS); vTaskDelay(750 / portTICK_RATE_MS);
onewire_reset(pin); onewire_reset(pin);
onewire_select(pin, addr); onewire_select(pin, addr);
onewire_write(pin, DS1820_READ_SCRATCHPAD, ONEWIRE_DEFAULT_POWER); onewire_write(pin, DS18B20_READ_SCRATCHPAD);
uint8_t get[10]; uint8_t get[10];
@ -71,15 +75,15 @@ uint8_t ds18b20_read_all(uint8_t pin, ds_sensor_t *result) {
float ds18b20_read_single(uint8_t pin) { float ds18b20_read_single(uint8_t pin) {
onewire_reset(pin); onewire_reset(pin);
onewire_skip_rom(pin);
onewire_write(pin, DS18B20_CONVERT_T);
onewire_write(pin, DS1820_SKIP_ROM, ONEWIRE_DEFAULT_POWER); onewire_power(pin);
onewire_write(pin, DS1820_CONVERT_T, ONEWIRE_DEFAULT_POWER);
vTaskDelay(750 / portTICK_RATE_MS); vTaskDelay(750 / portTICK_RATE_MS);
onewire_reset(pin); onewire_reset(pin);
onewire_write(pin, DS1820_SKIP_ROM, ONEWIRE_DEFAULT_POWER); onewire_skip_rom(pin);
onewire_write(pin, DS1820_READ_SCRATCHPAD, ONEWIRE_DEFAULT_POWER); onewire_write(pin, DS18B20_READ_SCRATCHPAD);
uint8_t get[10]; uint8_t get[10];
@ -106,3 +110,114 @@ float ds18b20_read_single(uint8_t pin) {
return temperature; return temperature;
//printf("Got a DS18B20 Reading: %d.%02d\n", (int)temperature, (int)(temperature - (int)temperature) * 100); //printf("Got a DS18B20 Reading: %d.%02d\n", (int)temperature, (int)(temperature - (int)temperature) * 100);
} }
bool ds18b20_measure(int pin, ds18b20_addr_t addr, bool wait) {
if (!onewire_reset(pin)) {
return false;
}
if (addr == DS18B20_ANY) {
onewire_skip_rom(pin);
} else {
onewire_select(pin, addr);
}
taskENTER_CRITICAL();
onewire_write(pin, DS18B20_CONVERT_T);
// For parasitic devices, power must be applied within 10us after issuing
// the convert command.
onewire_power(pin);
taskEXIT_CRITICAL();
if (wait) {
os_sleep_ms(750);
onewire_depower(pin);
}
return true;
}
bool ds18b20_read_scratchpad(int pin, ds18b20_addr_t addr, uint8_t *buffer) {
uint8_t crc;
uint8_t expected_crc;
if (!onewire_reset(pin)) {
return false;
}
if (addr == DS18B20_ANY) {
onewire_skip_rom(pin);
} else {
onewire_select(pin, addr);
}
onewire_write(pin, DS18B20_READ_SCRATCHPAD);
for (int i = 0; i < 8; i++) {
buffer[i] = onewire_read(pin);
}
crc = onewire_read(pin);
expected_crc = onewire_crc8(buffer, 8);
if (crc != expected_crc) {
printf("CRC check failed reading scratchpad: %02x %02x %02x %02x %02x %02x %02x %02x : %02x (expected %02x)\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], crc, expected_crc);
return false;
}
return true;
}
float ds18b20_read_temperature(int pin, ds18b20_addr_t addr) {
uint8_t scratchpad[8];
int temp;
if (!ds18b20_read_scratchpad(pin, addr, scratchpad)) {
return NAN;
}
temp = scratchpad[1] << 8 | scratchpad[0];
return ((float)temp * 625.0)/10000;
}
float ds18b20_measure_and_read(int pin, ds18b20_addr_t addr) {
if (!ds18b20_measure(pin, addr, true)) {
return NAN;
}
return ds18b20_read_temperature(pin, addr);
}
bool ds18b20_measure_and_read_multi(int pin, ds18b20_addr_t *addr_list, int addr_count, float *result_list) {
if (!ds18b20_measure(pin, DS18B20_ANY, true)) {
for (int i=0; i < addr_count; i++) {
result_list[i] = NAN;
}
return false;
}
return ds18b20_read_temp_multi(pin, addr_list, addr_count, result_list);
}
int ds18b20_scan_devices(int pin, ds18b20_addr_t *addr_list, int addr_count) {
onewire_search_t search;
onewire_addr_t addr;
int found = 0;
onewire_search_start(&search);
while ((addr = onewire_search_next(&search, pin)) != ONEWIRE_NONE) {
if (found < addr_count) {
addr_list[found] = addr;
}
found++;
}
return found;
}
bool ds18b20_read_temp_multi(int pin, ds18b20_addr_t *addr_list, int addr_count, float *result_list) {
bool result = true;
for (int i = 0; i < addr_count; i++) {
result_list[i] = ds18b20_read_temperature(pin, addr_list[i]);
if (isnan(result_list[i])) {
result = false;
}
}
return result;
}

View file

@ -1,6 +1,139 @@
#ifndef DRIVER_DS18B20_H_ #ifndef DRIVER_DS18B20_H_
#define DRIVER_DS18B20_H_ #define DRIVER_DS18B20_H_
#include "onewire/onewire.h"
/** @file ds18b20.h
*
* Communicate with the DS18B20 family of one-wire temperature sensor ICs.
*
*/
typedef onewire_addr_t ds18b20_addr_t;
/** An address value which can be used to indicate "any device on the bus" */
#define DS18B20_ANY ONEWIRE_NONE
/** Find the addresses of all DS18B20 devices on the bus.
*
* Scans the bus for all devices and places their addresses in the supplied
* array. If there are more than `addr_count` devices on the bus, only the
* first `addr_count` are recorded.
*
* @param pin The GPIO pin connected to the DS18B20 bus
* @param addr_list A pointer to an array of ds18b20_addr_t values. This
* will be populated with the addresses of the found
* devices.
* @param addr_count Number of slots in the `addr_list` array. At most this
* many addresses will be returned.
*
* @returns The number of devices found. Note that this may be less than,
* equal to, or more than `addr_count`, depending on how many DS18B20 devices
* are attached to the bus.
*/
int ds18b20_scan_devices(int pin, ds18b20_addr_t *addr_list, int addr_count);
/** Tell one or more sensors to perform a temperature measurement and
* conversion (CONVERT_T) operation. This operation can take up to 750ms to
* complete.
*
* If `wait=true`, this routine will automatically drive the pin high for the
* necessary 750ms after issuing the command to ensure parasitically-powered
* devices have enough power to perform the conversion operation (for
* non-parasitically-powered devices, this is not necessary but does not
* hurt). If `wait=false`, this routine will drive the pin high, but will
* then return immediately. It is up to the caller to wait the requisite time
* and then depower the bus using onewire_depower() or by issuing another
* command once conversion is done.
*
* @param pin The GPIO pin connected to the DS18B20 device
* @param addr The 64-bit address of the device on the bus. This can be set
* to ::DS18B20_ANY to send the command to all devices on the bus
* at the same time.
* @param wait Whether to wait for the necessary 750ms for the DS18B20 to
* finish performing the conversion before returning to the
* caller (You will normally want to do this).
*
* @returns `true` if the command was successfully issued, or `false` on error.
*/
bool ds18b20_measure(int pin, ds18b20_addr_t addr, bool wait);
/** Read the value from the last CONVERT_T operation.
*
* This should be called after ds18b20_measure() to fetch the result of the
* temperature measurement.
*
* @param pin The GPIO pin connected to the DS18B20 device
* @param addr The 64-bit address of the device to read. This can be set
* to ::DS18B20_ANY to read any device on the bus (but note
* that this will only work if there is exactly one device
* connected, or they will corrupt each others' transmissions)
*
* @returns The temperature in degrees Celsius, or NaN if there was an error.
*/
float ds18b20_read_temperature(int pin, ds18b20_addr_t addr);
/** Read the value from the last CONVERT_T operation for multiple devices.
*
* This should be called after ds18b20_measure() to fetch the result of the
* temperature measurement.
*
* @param pin The GPIO pin connected to the DS18B20 bus
* @param addr_list A list of addresses for devices to read.
* @param addr_count The number of entries in `addr_list`.
* @param result_list An array of floats to hold the returned temperature
* values. It should have at least `addr_count` entries.
*
* @returns `true` if all temperatures were fetched successfully, or `false`
* if one or more had errors (the temperature for erroring devices will be
* returned as NaN).
*/
bool ds18b20_read_temp_multi(int pin, ds18b20_addr_t *addr_list, int addr_count, float *result_list);
/** Perform a ds18b20_measure() followed by ds18b20_read_temperature()
*
* @param pin The GPIO pin connected to the DS18B20 device
* @param addr The 64-bit address of the device to read. This can be set
* to ::DS18B20_ANY to read any device on the bus (but note
* that this will only work if there is exactly one device
* connected, or they will corrupt each others' transmissions)
*
* @returns The temperature in degrees Celsius, or NaN if there was an error.
*/
float ds18b20_measure_and_read(int pin, ds18b20_addr_t addr);
/** Perform a ds18b20_measure() followed by ds18b20_read_temp_multi()
*
* @param pin The GPIO pin connected to the DS18B20 bus
* @param addr_list A list of addresses for devices to read.
* @param addr_count The number of entries in `addr_list`.
* @param result_list An array of floats to hold the returned temperature
* values. It should have at least `addr_count` entries.
*
* @returns `true` if all temperatures were fetched successfully, or `false`
* if one or more had errors (the temperature for erroring devices will be
* returned as NaN).
*/
bool ds18b20_measure_and_read_multi(int pin, ds18b20_addr_t *addr_list, int addr_count, float *result_list);
/** Read the scratchpad data for a particular DS18B20 device.
*
* This is not generally necessary to do directly. It is done automatically
* as part of ds18b20_read_temperature().
*
* @param pin The GPIO pin connected to the DS18B20 device
* @param addr The 64-bit address of the device to read. This can be set
* to ::DS18B20_ANY to read any device on the bus (but note
* that this will only work if there is exactly one device
* connected, or they will corrupt each others' transmissions)
* @param buffer An 8-byte buffer to hold the read data.
*
* @returns `true` if the data was read successfully, or `false` on error.
*/
bool ds18b20_read_scratchpad(int pin, ds18b20_addr_t addr, uint8_t *buffer);
// The following are obsolete/deprecated APIs
typedef struct { typedef struct {
uint8_t id; uint8_t id;
float value; float value;

View file

@ -1,206 +1,207 @@
#include "onewire.h" #include "onewire.h"
#include "string.h"
#include "task.h"
#include "esp/gpio.h"
// global search state #define ONEWIRE_SELECT_ROM 0x55
static unsigned char ROM_NO[ONEWIRE_NUM][8]; #define ONEWIRE_SKIP_ROM 0xcc
static uint8_t LastDiscrepancy[ONEWIRE_NUM]; #define ONEWIRE_SEARCH 0xf0
static uint8_t LastFamilyDiscrepancy[ONEWIRE_NUM];
static uint8_t LastDeviceFlag[ONEWIRE_NUM];
void onewire_init(uint8_t pin) // Waits up to `max_wait` microseconds for the specified pin to go high.
{ // Returns true if successful, false if the bus never comes high (likely
gpio_enable(pin, GPIO_INPUT); // shorted).
onewire_reset_search(pin); static inline bool _onewire_wait_for_bus(int pin, int max_wait) {
bool state;
for (int i = 0; i < ((max_wait + 4) / 5); i++) {
if (gpio_read(pin)) break;
sdk_os_delay_us(5);
}
state = gpio_read(pin);
// Wait an extra 1us to make sure the devices have an adequate recovery
// time before we drive things low again.
sdk_os_delay_us(1);
return state;
} }
// Perform the onewire reset function. We will wait up to 250uS for // Perform the onewire reset function. We will wait up to 250uS for
// the bus to come high, if it doesn't then it is broken or shorted // the bus to come high, if it doesn't then it is broken or shorted
// and we return a 0; // and we return false;
// //
// Returns 1 if a device asserted a presence pulse, 0 otherwise. // Returns true if a device asserted a presence pulse, false otherwise.
// //
uint8_t onewire_reset(uint8_t pin) bool onewire_reset(int pin) {
{ bool r;
uint8_t r;
uint8_t retries = 125;
noInterrupts(); gpio_enable(pin, GPIO_OUT_OPEN_DRAIN);
DIRECT_MODE_INPUT(pin); gpio_write(pin, 1);
interrupts(); // wait until the wire is high... just in case
// wait until the wire is high... just in case if (!_onewire_wait_for_bus(pin, 250)) return false;
do {
if (--retries == 0) return 0;
delayMicroseconds(2);
} while ( !DIRECT_READ(pin));
noInterrupts(); gpio_write(pin, 0);
DIRECT_WRITE_LOW(pin); sdk_os_delay_us(480);
DIRECT_MODE_OUTPUT(pin); // drive output low
interrupts(); taskENTER_CRITICAL();
delayMicroseconds(480); gpio_write(pin, 1); // allow it to float
noInterrupts(); sdk_os_delay_us(70);
DIRECT_MODE_INPUT(pin); // allow it to float r = !gpio_read(pin);
delayMicroseconds(70); taskEXIT_CRITICAL();
r = !DIRECT_READ(pin);
interrupts(); // Wait for all devices to finish pulling the bus low before returning
delayMicroseconds(410); if (!_onewire_wait_for_bus(pin, 410)) return false;
return r;
return r;
} }
// Write a bit. Port and bit is used to cut lookup time and provide static bool _onewire_write_bit(int pin, bool v) {
// more certain timing. if (!_onewire_wait_for_bus(pin, 10)) return false;
// if (v) {
static void onewire_write_bit(uint8_t pin, uint8_t v) taskENTER_CRITICAL();
{ gpio_write(pin, 0); // drive output low
if (v & 1) { sdk_os_delay_us(10);
noInterrupts(); gpio_write(pin, 1); // allow output high
DIRECT_WRITE_LOW(pin); taskEXIT_CRITICAL();
DIRECT_MODE_OUTPUT(pin); // drive output low sdk_os_delay_us(55);
delayMicroseconds(10); } else {
DIRECT_WRITE_HIGH(pin); // drive output high taskENTER_CRITICAL();
interrupts(); gpio_write(pin, 0); // drive output low
delayMicroseconds(55); sdk_os_delay_us(65);
} else { gpio_write(pin, 1); // allow output high
noInterrupts(); taskEXIT_CRITICAL();
DIRECT_WRITE_LOW(pin); }
DIRECT_MODE_OUTPUT(pin); // drive output low sdk_os_delay_us(1);
delayMicroseconds(65);
DIRECT_WRITE_HIGH(pin); // drive output high return true;
interrupts();
delayMicroseconds(5);
}
} }
// Read a bit. Port and bit is used to cut lookup time and provide static int _onewire_read_bit(int pin) {
// more certain timing. int r;
//
static uint8_t onewire_read_bit(uint8_t pin)
{
uint8_t r;
noInterrupts(); if (!_onewire_wait_for_bus(pin, 10)) return -1;
DIRECT_MODE_OUTPUT(pin); taskENTER_CRITICAL();
DIRECT_WRITE_LOW(pin); gpio_write(pin, 0);
delayMicroseconds(3); sdk_os_delay_us(2);
DIRECT_MODE_INPUT(pin); // let pin float, pull up will raise gpio_write(pin, 1); // let pin float, pull up will raise
delayMicroseconds(10); sdk_os_delay_us(11);
r = DIRECT_READ(pin); r = gpio_read(pin); // Must sample within 15us of start
interrupts(); taskEXIT_CRITICAL();
delayMicroseconds(53); sdk_os_delay_us(48);
return r;
return r;
} }
// Write a byte. The writing code uses the active drivers to raise the // Write a byte. The writing code uses open-drain mode and expects the pullup
// pin high, if you need power after the write (e.g. DS18S20 in // resistor to pull the line high when not driven low. If you need strong
// parasite power mode) then set 'power' to 1, otherwise the pin will // power after the write (e.g. DS18B20 in parasite power mode) then call
// go tri-state at the end of the write to avoid heating in a short or // onewire_power() after this is complete to actively drive the line high.
// other mishap.
// //
void onewire_write(uint8_t pin, uint8_t v, uint8_t power /* = 0 */) { bool onewire_write(int pin, uint8_t v) {
uint8_t bitMask; uint8_t bitMask;
for (bitMask = 0x01; bitMask; bitMask <<= 1) { for (bitMask = 0x01; bitMask; bitMask <<= 1) {
onewire_write_bit(pin, (bitMask & v)?1:0); if (!_onewire_write_bit(pin, (bitMask & v))) {
} return false;
if ( !power) { }
noInterrupts(); }
DIRECT_MODE_INPUT(pin); return true;
DIRECT_WRITE_LOW(pin);
interrupts();
}
} }
void onewire_write_bytes(uint8_t pin, const uint8_t *buf, uint16_t count, bool power /* = 0 */) { bool onewire_write_bytes(int pin, const uint8_t *buf, size_t count) {
uint16_t i; size_t i;
for (i = 0 ; i < count ; i++)
onewire_write(pin, buf[i], ONEWIRE_DEFAULT_POWER); for (i = 0 ; i < count ; i++) {
if (!power) { if (!onewire_write(pin, buf[i])) {
noInterrupts(); return false;
DIRECT_MODE_INPUT(pin); }
DIRECT_WRITE_LOW(pin); }
interrupts(); return true;
}
} }
// Read a byte // Read a byte
// //
uint8_t onewire_read(uint8_t pin) { int onewire_read(int pin) {
uint8_t bitMask; uint8_t bitMask;
uint8_t r = 0; int r = 0;
int bit;
for (bitMask = 0x01; bitMask; bitMask <<= 1) { for (bitMask = 0x01; bitMask; bitMask <<= 1) {
if (onewire_read_bit(pin)) r |= bitMask; bit = _onewire_read_bit(pin);
} if (bit < 0) {
return r; return -1;
} else if (bit) {
r |= bitMask;
}
}
return r;
} }
void onewire_read_bytes(uint8_t pin, uint8_t *buf, uint16_t count) { bool onewire_read_bytes(int pin, uint8_t *buf, size_t count) {
uint16_t i; size_t i;
for (i = 0 ; i < count ; i++) int b;
buf[i] = onewire_read(pin);
for (i = 0 ; i < count ; i++) {
b = onewire_read(pin);
if (b < 0) return false;
buf[i] = b;
}
return true;
} }
// Do a ROM select bool onewire_select(int pin, onewire_addr_t addr) {
//
void onewire_select(uint8_t pin, const uint8_t rom[8])
{
uint8_t i; uint8_t i;
onewire_write(pin, 0x55, ONEWIRE_DEFAULT_POWER); // Choose ROM if (!onewire_write(pin, ONEWIRE_SELECT_ROM)) {
return false;
}
for (i = 0; i < 8; i++) onewire_write(pin, rom[i], ONEWIRE_DEFAULT_POWER); for (i = 0; i < 8; i++) {
if (!onewire_write(pin, addr & 0xff)) {
return false;
}
addr >>= 8;
}
return true;
} }
// Do a ROM skip bool onewire_skip_rom(int pin) {
// return onewire_write(pin, ONEWIRE_SKIP_ROM);
void onewire_skip(uint8_t pin)
{
onewire_write(pin, 0xCC, ONEWIRE_DEFAULT_POWER); // Skip ROM
} }
void onewire_depower(uint8_t pin) bool onewire_power(int pin) {
{ // Make sure the bus is not being held low before driving it high, or we
noInterrupts(); // may end up shorting ourselves out.
DIRECT_MODE_INPUT(pin); if (!_onewire_wait_for_bus(pin, 10)) return false;
interrupts();
gpio_enable(pin, GPIO_OUTPUT);
gpio_write(pin, 1);
return true;
} }
// You need to use this function to start a search again from the beginning. void onewire_depower(int pin) {
// You do not need to do it for the first search, though you could. gpio_enable(pin, GPIO_OUT_OPEN_DRAIN);
//
void onewire_reset_search(uint8_t pin)
{
// reset the search state
LastDiscrepancy[pin] = 0;
LastDeviceFlag[pin] = 0;
LastFamilyDiscrepancy[pin] = 0;
int i;
for(i = 7; ; i--) {
ROM_NO[pin][i] = 0;
if ( i == 0) break;
}
} }
// Setup the search to find the device type 'family_code' on the next call void onewire_search_start(onewire_search_t *search) {
// to search(*newAddr) if it is present. // reset the search state
// memset(search, 0, sizeof(*search));
void onewire_target_search(uint8_t pin, uint8_t family_code)
{
// set the search state to find SearchFamily type devices
ROM_NO[pin][0] = family_code;
uint8_t i;
for (i = 1; i < 8; i++)
ROM_NO[pin][i] = 0;
LastDiscrepancy[pin] = 64;
LastFamilyDiscrepancy[pin] = 0;
LastDeviceFlag[pin] = 0;
} }
// Perform a search. If this function returns a '1' then it has void onewire_search_prefix(onewire_search_t *search, uint8_t family_code) {
// enumerated the next device and you may retrieve the ROM from the uint8_t i;
// OneWire::address variable. If there are no devices, no further
search->rom_no[0] = family_code;
for (i = 1; i < 8; i++) {
search->rom_no[i] = 0;
}
search->last_discrepancy = 64;
search->last_device_found = false;
}
// Perform a search. If the next device has been successfully enumerated, its
// ROM address will be returned. If there are no devices, no further
// devices, or something horrible happens in the middle of the // devices, or something horrible happens in the middle of the
// enumeration then a 0 is returned. If a new device is found then // enumeration then ONEWIRE_NONE is returned. Use OneWire::reset_search() to
// its address is copied to newAddr. Use OneWire::reset_search() to
// start over. // start over.
// //
// --- Replaced by the one from the Dallas Semiconductor web site --- // --- Replaced by the one from the Dallas Semiconductor web site ---
@ -210,129 +211,119 @@ void onewire_target_search(uint8_t pin, uint8_t family_code)
// Return 1 : device found, ROM number in ROM_NO buffer // Return 1 : device found, ROM number in ROM_NO buffer
// 0 : device not found, end of search // 0 : device not found, end of search
// //
uint8_t onewire_search(uint8_t pin, uint8_t *newAddr) onewire_addr_t onewire_search_next(onewire_search_t *search, int pin) {
{ //TODO: add more checking for read/write errors
uint8_t id_bit_number; uint8_t id_bit_number;
uint8_t last_zero, rom_byte_number, search_result; uint8_t last_zero, search_result;
uint8_t id_bit, cmp_id_bit; int rom_byte_number;
uint8_t id_bit, cmp_id_bit;
onewire_addr_t addr;
unsigned char rom_byte_mask;
bool search_direction;
unsigned char rom_byte_mask, search_direction; // initialize for search
id_bit_number = 1;
// initialize for search last_zero = 0;
id_bit_number = 1; rom_byte_number = 0;
last_zero = 0; rom_byte_mask = 1;
rom_byte_number = 0; search_result = 0;
rom_byte_mask = 1;
search_result = 0;
// if the last call was not the last one // if the last call was not the last one
if (!LastDeviceFlag[pin]) if (!search->last_device_found) {
{ // 1-Wire reset
// 1-Wire reset if (!onewire_reset(pin)) {
if (!onewire_reset(pin)) // reset the search
{ search->last_discrepancy = 0;
// reset the search search->last_device_found = false;
LastDiscrepancy[pin] = 0; return ONEWIRE_NONE;
LastDeviceFlag[pin] = 0; }
LastFamilyDiscrepancy[pin] = 0;
return 0;
}
// issue the search command // issue the search command
onewire_write(pin, 0xF0, ONEWIRE_DEFAULT_POWER); onewire_write(pin, ONEWIRE_SEARCH);
// loop to do the search // loop to do the search
do do {
{ // read a bit and its complement
// read a bit and its complement id_bit = _onewire_read_bit(pin);
id_bit = onewire_read_bit(pin); cmp_id_bit = _onewire_read_bit(pin);
cmp_id_bit = onewire_read_bit(pin);
// check for no devices on 1-wire // check for no devices on 1-wire
if ((id_bit == 1) && (cmp_id_bit == 1)) if ((id_bit < 0) || (cmp_id_bit < 0)) {
break; // Read error
else break;
{ } else if ((id_bit == 1) && (cmp_id_bit == 1)) {
// all devices coupled have 0 or 1 break;
if (id_bit != cmp_id_bit) } else {
search_direction = id_bit; // bit write value for search // all devices coupled have 0 or 1
else if (id_bit != cmp_id_bit) {
{ search_direction = id_bit; // bit write value for search
// if this discrepancy if before the Last Discrepancy } else {
// on a previous next then pick the same as last time // if this discrepancy if before the Last Discrepancy
if (id_bit_number < LastDiscrepancy[pin]) // on a previous next then pick the same as last time
search_direction = ((ROM_NO[pin][rom_byte_number] & rom_byte_mask) > 0); if (id_bit_number < search->last_discrepancy) {
else search_direction = ((search->rom_no[rom_byte_number] & rom_byte_mask) > 0);
// if equal to last pick 1, if not then pick 0 } else {
search_direction = (id_bit_number == LastDiscrepancy[pin]); // if equal to last pick 1, if not then pick 0
search_direction = (id_bit_number == search->last_discrepancy);
}
// if 0 was picked then record its position in LastZero // if 0 was picked then record its position in LastZero
if (search_direction == 0) if (!search_direction) {
{ last_zero = id_bit_number;
last_zero = id_bit_number; }
}
// check for Last discrepancy in family // set or clear the bit in the ROM byte rom_byte_number
if (last_zero < 9) // with mask rom_byte_mask
LastFamilyDiscrepancy[pin] = last_zero; if (search_direction) {
} search->rom_no[rom_byte_number] |= rom_byte_mask;
} else {
search->rom_no[rom_byte_number] &= ~rom_byte_mask;
}
// serial number search direction write bit
_onewire_write_bit(pin, search_direction);
// increment the byte counter id_bit_number
// and shift the mask rom_byte_mask
id_bit_number++;
rom_byte_mask <<= 1;
// if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
if (rom_byte_mask == 0) {
rom_byte_number++;
rom_byte_mask = 1;
}
}
} while (rom_byte_number < 8); // loop until through all ROM bytes 0-7
// if the search was successful then
if (!(id_bit_number < 65)) {
// search successful so set last_discrepancy,last_device_found,search_result
search->last_discrepancy = last_zero;
// check for last device
if (search->last_discrepancy == 0) {
search->last_device_found = true;
} }
// set or clear the bit in the ROM byte rom_byte_number search_result = 1;
// with mask rom_byte_mask }
if (search_direction == 1) }
ROM_NO[pin][rom_byte_number] |= rom_byte_mask;
else
ROM_NO[pin][rom_byte_number] &= ~rom_byte_mask;
// serial number search direction write bit // if no device found then reset counters so next 'search' will be like a first
onewire_write_bit(pin, search_direction); if (!search_result || !search->rom_no[0]) {
search->last_discrepancy = 0;
// increment the byte counter id_bit_number search->last_device_found = false;
// and shift the mask rom_byte_mask return ONEWIRE_NONE;
id_bit_number++; } else {
rom_byte_mask <<= 1; addr = 0;
for (rom_byte_number = 7; rom_byte_number >= 0; rom_byte_number--) {
// if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask addr = (addr << 8) | search->rom_no[rom_byte_number];
if (rom_byte_mask == 0) }
{ //printf("Ok I found something at %08x%08x...\n", (uint32_t)(addr >> 32), (uint32_t)addr);
rom_byte_number++; }
rom_byte_mask = 1; return addr;
}
}
}
while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
// if the search was successful then
if (!(id_bit_number < 65))
{
// search successful so set LastDiscrepancy,LastDeviceFlag,search_result
LastDiscrepancy[pin] = last_zero;
// check for last device
if (LastDiscrepancy[pin] == 0)
LastDeviceFlag[pin] = 1;
search_result = 1;
}
}
// if no device found then reset counters so next 'search' will be like a first
if (!search_result || !ROM_NO[pin][0])
{
LastDiscrepancy[pin] = 0;
LastDeviceFlag[pin] = 0;
LastFamilyDiscrepancy[pin] = 0;
search_result = 0;
}
else
{
for (rom_byte_number = 0; rom_byte_number < 8; rom_byte_number++)
{
newAddr[rom_byte_number] = ROM_NO[pin][rom_byte_number];
//printf("Ok I found something at %d - %x...\n",rom_byte_number, newAddr[rom_byte_number]);
}
}
return search_result;
} }
// The 1-Wire CRC scheme is described in Maxim Application Note 27: // The 1-Wire CRC scheme is described in Maxim Application Note 27:
@ -371,41 +362,38 @@ static const uint8_t dscrc_table[] = {
// compared to all those delayMicrosecond() calls. But I got // compared to all those delayMicrosecond() calls. But I got
// confused, so I use this table from the examples.) // confused, so I use this table from the examples.)
// //
uint8_t onewire_crc8(const uint8_t *addr, uint8_t len) uint8_t onewire_crc8(const uint8_t *data, uint8_t len) {
{ uint8_t crc = 0;
uint8_t crc = 0;
while (len--) { while (len--) {
crc = pgm_read_byte(dscrc_table + (crc ^ *addr++)); crc = pgm_read_byte(dscrc_table + (crc ^ *data++));
} }
return crc; return crc;
} }
#else #else
// //
// Compute a Dallas Semiconductor 8 bit CRC directly. // Compute a Dallas Semiconductor 8 bit CRC directly.
// this is much slower, but much smaller, than the lookup table. // this is much slower, but much smaller, than the lookup table.
// //
uint8_t onewire_crc8(const uint8_t *addr, uint8_t len) uint8_t onewire_crc8(const uint8_t *data, uint8_t len) {
{ uint8_t crc = 0;
uint8_t crc = 0;
while (len--) {
while (len--) { uint8_t inbyte = *data++;
uint8_t inbyte = *addr++; for (int i = 8; i; i--) {
uint8_t i; uint8_t mix = (crc ^ inbyte) & 0x01;
for (i = 8; i; i--) { crc >>= 1;
uint8_t mix = (crc ^ inbyte) & 0x01; if (mix) crc ^= 0x8C;
crc >>= 1; inbyte >>= 1;
if (mix) crc ^= 0x8C; }
inbyte >>= 1; }
} return crc;
}
return crc;
} }
#endif #endif
// Compute the 1-Wire CRC16 and compare it against the received CRC. // Compute the 1-Wire CRC16 and compare it against the received CRC.
// Example usage (reading a DS2408): // Example usage (reading a DS2408):
// // Put everything in a buffer so we can compute the CRC easily. // // Put everything in a buffer so we can compute the CRC easily.
// uint8_t buf[13]; // uint8_t buf[13];
// buf[0] = 0xF0; // Read PIO Registers // buf[0] = 0xF0; // Read PIO Registers
// buf[1] = 0x88; // LSB address // buf[1] = 0x88; // LSB address
@ -423,9 +411,8 @@ uint8_t onewire_crc8(const uint8_t *addr, uint8_t len)
// *not* at a 16-bit integer. // *not* at a 16-bit integer.
// @param crc - The crc starting value (optional) // @param crc - The crc starting value (optional)
// @return 1, iff the CRC matches. // @return 1, 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) {
{ uint16_t crc = ~onewire_crc16(input, len, crc_iv);
crc = ~onewire_crc16(input, len, crc);
return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1]; return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1];
} }
@ -441,8 +428,8 @@ bool onewire_check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inve
// @param len - How many bytes to use. // @param len - How many bytes to use.
// @param crc - The crc starting value (optional) // @param crc - The crc starting value (optional)
// @return The CRC16, as defined by Dallas Semiconductor. // @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) {
{ uint16_t crc = crc_iv;
static const uint8_t oddparity[16] = static const uint8_t oddparity[16] =
{ 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 }; { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
@ -463,4 +450,4 @@ uint16_t onewire_crc16(const uint8_t* input, uint16_t len, uint16_t crc)
crc ^= cdata; crc ^= cdata;
} }
return crc; return crc;
} }

View file

@ -4,135 +4,232 @@
#include <espressif/esp_misc.h> // sdk_os_delay_us #include <espressif/esp_misc.h> // sdk_os_delay_us
#include "FreeRTOS.h" #include "FreeRTOS.h"
// 1 for keeping the parasitic power on H /** @file onewire.h
#define ONEWIRE_DEFAULT_POWER 1 *
* Routines to access devices using the Dallas Semiconductor 1-Wire(tm)
* protocol.
*/
// Maximum number of devices. /** Select the table-lookup method of computing the 8-bit CRC
#define ONEWIRE_NUM 20 * by setting this to 1 during compilation. The lookup table enlarges code
* size by about 250 bytes. By default, a slower but very compact algorithm
// You can exclude certain features from OneWire. In theory, this * is used.
// might save some space. In practice, the compiler automatically */
// removes unused code (technically, the linker, using -fdata-sections
// and -ffunction-sections when compiling, and Wl,--gc-sections
// when linking), so most of these will not result in any code size
// reduction. Well, unless you try to use the missing features
// and redesign your program to not need them! ONEWIRE_CRC8_TABLE
// is the exception, because it selects a fast but large algorithm
// or a small but slow algorithm.
// Select the table-lookup method of computing the 8-bit CRC
// by setting this to 1. The lookup table enlarges code size by
// about 250 bytes. It does NOT consume RAM (but did in very
// old versions of OneWire). If you disable this, a slower
// but very compact algorithm is used.
#ifndef ONEWIRE_CRC8_TABLE #ifndef ONEWIRE_CRC8_TABLE
#define ONEWIRE_CRC8_TABLE 0 #define ONEWIRE_CRC8_TABLE 0
#endif #endif
// Platform specific I/O definitions /** Type used to hold all 1-Wire device ROM addresses (64-bit) */
#define noInterrupts portDISABLE_INTERRUPTS typedef uint64_t onewire_addr_t;
#define interrupts portENABLE_INTERRUPTS
#define delayMicroseconds sdk_os_delay_us
#define DIRECT_READ(pin) gpio_read(pin) /** Structure to contain the current state for onewire_search_next(), etc */
#define DIRECT_MODE_INPUT(pin) gpio_enable(pin, GPIO_INPUT) typedef struct {
#define DIRECT_MODE_OUTPUT(pin) gpio_enable(pin, GPIO_OUTPUT) uint8_t rom_no[8];
#define DIRECT_WRITE_LOW(pin) gpio_write(pin, 0) uint8_t last_discrepancy;
#define DIRECT_WRITE_HIGH(pin) gpio_write(pin, 1) bool last_device_found;
} onewire_search_t;
void onewire_init(uint8_t pin); /** ::ONEWIRE_NONE 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 /** Perform a 1-Wire reset cycle.
// with a presence pulse. Returns 0 if there is no device or the *
// bus is shorted or otherwise held low for more than 250uS * @param pin The GPIO pin connected to the 1-Wire bus.
uint8_t onewire_reset(uint8_t pin); *
* @returns `true` if at least one device responds with a presence pulse,
* `false` if no devices were detected (or the bus is shorted, etc)
*/
bool onewire_reset(int pin);
// Issue a 1-Wire rom select command, you do the reset first. /** Issue a 1-Wire rom select command to select a particular device.
void onewire_select(uint8_t pin, const uint8_t rom[8]); *
* It is necessary to call onewire_reset() before calling this function.
*
* @param pin The GPIO pin connected to the 1-Wire bus.
* @param addr The ROM address of the device to select
*
* @returns `true` if the "ROM select" command could be succesfully issued,
* `false` if there was an error.
*/
bool onewire_select(int pin, const onewire_addr_t addr);
// Issue a 1-Wire rom skip command, to address all on bus. /** Issue a 1-Wire "skip ROM" command to select *all* devices on the bus.
void onewire_skip(uint8_t pin); *
* It is necessary to call onewire_reset() before calling this function.
*
* @param pin The GPIO pin connected to the 1-Wire bus.
*
* @returns `true` if the "skip ROM" command could be succesfully issued,
* `false` if there was an error.
*/
bool onewire_skip_rom(int pin);
// Write a byte. If 'power' is one then the wire is held high at /** Write a byte on the onewire bus.
// the end for parasitically powered devices. You are responsible *
// for eventually depowering it by calling depower() or doing * The writing code uses open-drain mode and expects the pullup resistor to
// another read or write. * pull the line high when not driven low. If you need strong power after the
void onewire_write(uint8_t pin, uint8_t v, uint8_t power); * write (e.g. DS18B20 in parasite power mode) then call onewire_power() after
* this is complete to actively drive the line high.
*
* @param pin The GPIO pin connected to the 1-Wire bus.
* @param v The byte value to write
*
* @returns `true` if successful, `false` on error.
*/
bool onewire_write(int pin, uint8_t v);
void onewire_write_bytes(uint8_t pin, const uint8_t *buf, uint16_t count, bool power); /** Write multiple bytes on the 1-Wire bus.
*
* See onewire_write() for more info.
*
* @param pin The GPIO pin connected to the 1-Wire bus.
* @param buf A pointer to the buffer of bytes to be written
* @param count Number of bytes to write
*
* @returns `true` if all bytes written successfully, `false` on error.
*/
bool onewire_write_bytes(int pin, const uint8_t *buf, size_t count);
// Read a byte. /** Read a byte from a 1-Wire device.
uint8_t onewire_read(uint8_t pin); *
* @param pin The GPIO pin connected to the 1-Wire bus.
*
* @returns the read byte on success, negative value on error.
*/
int onewire_read(int pin);
void onewire_read_bytes(uint8_t pin, uint8_t *buf, uint16_t count); /** Read multiple bytes from a 1-Wire device.
*
* @param pin The GPIO pin connected to the 1-Wire bus.
* @param buf A pointer to the buffer to contain the read bytes
* @param count Number of bytes to read
*
* @returns `true` on success, `false` on error.
*/
bool onewire_read_bytes(int pin, uint8_t *buf, size_t count);
// Write a bit. The bus is always left powered at the end, see /** Actively drive the bus high to provide extra power for certain operations
// note in write() about that. * of parasitically-powered devices.
// void onewire_write_bit(uint8_t pin, uint8_t v); *
* For parasitically-powered devices which need more power than can be
* provided via the normal pull-up resistor, it may be necessary for some
* operations to drive the bus actively high. This function can be used to
* perform that operation.
*
* The bus can be depowered once it is no longer needed by calling
* onewire_depower(), or it will be depowered automatically the next time
* onewire_reset() is called to start another command.
*
* Note: Make sure the device(s) you are powering will not pull more current
* than the ESP8266 is able to supply via its GPIO pins (this is especially
* important when multiple devices are on the same bus and they are all
* performing a power-intensive operation at the same time (i.e. multiple
* DS18B20 sensors, which have all been given a "convert T" operation by using
* onewire_skip_rom())).
*
* Note: This routine will check to make sure that the bus is already high
* before driving it, to make sure it doesn't attempt to drive it high while
* something else is pulling it low (which could cause a reset or damage the
* ESP8266).
*
* @param pin The GPIO pin connected to the 1-Wire bus.
*
* @returns `true` on success, `false` on error.
*/
bool onewire_power(int pin);
// Read a bit. /** Stop forcing power onto the bus.
// uint8_t onewire_read_bit(uint8_t pin); *
* You only need to do this if 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.
*
* @param pin The GPIO pin connected to the 1-Wire bus.
*/
void onewire_depower(int pin);
// Stop forcing power onto the bus. You only need to do this if /** Clear the search state so that it will start from the beginning on the next
// you used the 'power' flag to write() or used a write_bit() call * call to onewire_search_next().
// 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 * @param search The onewire_search_t structure to reset.
// someone shorts your bus. */
void onewire_depower(uint8_t pin); void onewire_search_start(onewire_search_t *search);
// Clear the search state so that if will start from the beginning again. /** Setup the search to search for devices with the specified "family code".
void onewire_reset_search(uint8_t pin); *
* @param search The onewire_search_t structure to update.
* @param family_code The "family code" to search for.
*/
void onewire_search_prefix(onewire_search_t *search, uint8_t family_code);
// Setup the search to find the device type 'family_code' on the next call /** Search for the next device on the bus.
// to search(*newAddr) if it is present. *
void onewire_target_search(uint8_t pin, uint8_t family_code); * The order of returned device addresses is deterministic. You will always
* get the same devices in the same order.
*
* @returns the address of the next device on the bus, or ::ONEWIRE_NONE if
* there is no next address. ::ONEWIRE_NONE might also 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.
*/
onewire_addr_t onewire_search_next(onewire_search_t *search, int pin);
// Look for the next device. Returns 1 if a new address has been /** Compute a Dallas Semiconductor 8 bit CRC.
// returned. A zero might mean that the bus is shorted, there are *
// no devices, or you have already retrieved all of them. It * These are used in the ROM address and scratchpad registers to verify the
// might be a good idea to check the CRC to make sure you didn't * transmitted data is correct.
// get garbage. The order is deterministic. You will always get */
// the same devices in the same order. uint8_t onewire_crc8(const uint8_t *data, uint8_t len);
uint8_t onewire_search(uint8_t pin, uint8_t *newAddr);
// Compute a Dallas Semiconductor 8 bit CRC, these are used in the /** Compute the 1-Wire CRC16 and compare it against the received CRC.
// ROM and scratchpad registers. *
uint8_t onewire_crc8(const uint8_t *addr, uint8_t len); * Example usage (reading a DS2408):
* @code
* // 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
* onewire_write_bytes(pin, buf, 3); // Write 3 cmd bytes
* onewire_read_bytes(pin, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16
* if (!onewire_check_crc16(buf, 11, &buf[11])) {
* // TODO: Handle error.
* }
* @endcode
*
* @param input Array of bytes to checksum.
* @param len Number of bytes in `input`
* @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_iv The crc starting value (optional)
*
* @returns `true` if the CRC matches, `false` otherwise.
*/
bool onewire_check_crc16(const uint8_t* input, size_t len, const uint8_t* inverted_crc, uint16_t crc_iv);
// Compute the 1-Wire CRC16 and compare it against the received CRC. /** Compute a Dallas Semiconductor 16 bit CRC.
// Example usage (reading a DS2408): *
// // Put everything in a buffer so we can compute the CRC easily. * This is required to check the integrity of data received from many 1-Wire
// uint8_t buf[13]; * devices. Note that the CRC computed here is *not* what you'll get from the
// buf[0] = 0xF0; // Read PIO Registers * 1-Wire network, for two reasons:
// buf[1] = 0x88; // LSB address * 1. The CRC is transmitted bitwise inverted.
// buf[2] = 0x00; // MSB address * 2. Depending on the endian-ness of your processor, the binary
// WriteBytes(net, buf, 3); // Write 3 cmd bytes * representation of the two-byte return value may have a different
// ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16 * byte order than the two bytes you get from 1-Wire.
// if (!CheckCRC16(buf, 11, &buf[11])) { *
// // Handle error. * @param input Array of bytes to checksum.
// } * @param len How many bytes are in `input`.
// * @param crc_iv The crc starting value (optional)
// @param input - Array of bytes to checksum. *
// @param len - How many bytes to use. * @returns the CRC16, as defined by Dallas Semiconductor.
// @param inverted_crc - The two CRC16 bytes in the received data. */
// This should just point into the received data, uint16_t onewire_crc16(const uint8_t* input, size_t len, uint16_t crc_iv);
// *not* at a 16-bit integer.
// @param crc - 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);
// 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.
// @param input - Array of bytes to checksum.
// @param len - How many bytes to use.
// @param crc - 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);
#endif #endif