esp32-warm-water
esp32 based project for the control of a heating element based on temperature
Macros | Functions
ds18b20.c File Reference
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_system.h"
#include "esp_log.h"
#include "ds18b20.h"
#include "owb.h"
Include dependency graph for ds18b20.c:

Go to the source code of this file.

Macros

#define DS18B20_FUNCTION_TEMP_CONVERT   0x44
 Initiate a single temperature conversion. More...
 
#define DS18B20_FUNCTION_SCRATCHPAD_WRITE   0x4E
 Write 3 bytes of data to the device scratchpad at positions 2, 3 and 4. More...
 
#define DS18B20_FUNCTION_SCRATCHPAD_READ   0xBE
 Read 9 bytes of data (including CRC) from the device scratchpad. More...
 
#define DS18B20_FUNCTION_SCRATCHPAD_COPY   0x48
 Copy the contents of the scratchpad to the device EEPROM. More...
 
#define DS18B20_FUNCTION_EEPROM_RECALL   0xB8
 Restore alarm trigger values and configuration data from EEPROM to the scratchpad. More...
 
#define DS18B20_FUNCTION_POWER_SUPPLY_READ   0xB4
 Determine if a device is using parasitic power. More...
 

Functions

DS18B20_Infods18b20_malloc (void)
 Construct a new device info instance. New instance should be initialised before calling other functions. More...
 
void ds18b20_free (DS18B20_Info **ds18b20_info)
 Delete an existing device info instance. More...
 
void ds18b20_init (DS18B20_Info *ds18b20_info, const OneWireBus *bus, OneWireBus_ROMCode rom_code)
 Initialise a device info instance with the specified GPIO. More...
 
void ds18b20_init_solo (DS18B20_Info *ds18b20_info, const OneWireBus *bus)
 Initialise a device info instance as a solo device on the bus. More...
 
void ds18b20_use_crc (DS18B20_Info *ds18b20_info, bool use_crc)
 Enable or disable use of CRC checks on device communications. More...
 
bool ds18b20_set_resolution (DS18B20_Info *ds18b20_info, DS18B20_RESOLUTION resolution)
 Set temperature measurement resolution. More...
 
DS18B20_RESOLUTION ds18b20_read_resolution (DS18B20_Info *ds18b20_info)
 Update and return the current temperature measurement resolution from the device. More...
 
bool ds18b20_convert (const DS18B20_Info *ds18b20_info)
 Start a temperature measurement conversion on a single device. More...
 
void ds18b20_convert_all (const OneWireBus *bus)
 Start temperature conversion on all connected devices. More...
 
float ds18b20_wait_for_conversion (const DS18B20_Info *ds18b20_info)
 Wait for the maximum conversion time according to the current resolution of the device. In external power mode, the device or devices can signal when conversion has completed. In parasitic power mode, this is not possible, so a pre-calculated delay is performed. More...
 
DS18B20_ERROR ds18b20_read_temp (const DS18B20_Info *ds18b20_info, float *value)
 Read last temperature measurement from device. More...
 
DS18B20_ERROR ds18b20_convert_and_read_temp (const DS18B20_Info *ds18b20_info, float *value)
 Convert, wait and read current temperature from device. More...
 
DS18B20_ERROR ds18b20_check_for_parasite_power (const OneWireBus *bus, bool *present)
 Check OneWire bus for presence of parasitic-powered devices. More...
 

Detailed Description

Resolution is cached in the DS18B20_Info object to avoid querying the hardware every time a temperature conversion is required. However this can result in the cached value becoming inconsistent with the hardware value, so care must be taken.

Definition in file ds18b20.c.

Macro Definition Documentation

◆ DS18B20_FUNCTION_EEPROM_RECALL

#define DS18B20_FUNCTION_EEPROM_RECALL   0xB8

Restore alarm trigger values and configuration data from EEPROM to the scratchpad.

Definition at line 58 of file ds18b20.c.

◆ DS18B20_FUNCTION_POWER_SUPPLY_READ

#define DS18B20_FUNCTION_POWER_SUPPLY_READ   0xB4

Determine if a device is using parasitic power.

Definition at line 59 of file ds18b20.c.

◆ DS18B20_FUNCTION_SCRATCHPAD_COPY

#define DS18B20_FUNCTION_SCRATCHPAD_COPY   0x48

Copy the contents of the scratchpad to the device EEPROM.

Definition at line 57 of file ds18b20.c.

◆ DS18B20_FUNCTION_SCRATCHPAD_READ

#define DS18B20_FUNCTION_SCRATCHPAD_READ   0xBE

Read 9 bytes of data (including CRC) from the device scratchpad.

Definition at line 56 of file ds18b20.c.

◆ DS18B20_FUNCTION_SCRATCHPAD_WRITE

#define DS18B20_FUNCTION_SCRATCHPAD_WRITE   0x4E

Write 3 bytes of data to the device scratchpad at positions 2, 3 and 4.

Definition at line 55 of file ds18b20.c.

◆ DS18B20_FUNCTION_TEMP_CONVERT

#define DS18B20_FUNCTION_TEMP_CONVERT   0x44

Initiate a single temperature conversion.

Definition at line 54 of file ds18b20.c.

Function Documentation

◆ ds18b20_check_for_parasite_power()

DS18B20_ERROR ds18b20_check_for_parasite_power ( const OneWireBus bus,
bool *  present 
)

Check OneWire bus for presence of parasitic-powered devices.

Parameters
[in]busPointer to initialised bus instance.
[out]presentResult value, true if a parasitic-powered device was detected.
Returns
DS18B20_OK if check is successful, otherwise error.

Definition at line 579 of file ds18b20.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ds18b20_convert()

bool ds18b20_convert ( const DS18B20_Info ds18b20_info)

Start a temperature measurement conversion on a single device.

Parameters
[in]ds18b20_infoPointer to device info instance.

Definition at line 467 of file ds18b20.c.

◆ ds18b20_convert_all()

void ds18b20_convert_all ( const OneWireBus bus)

Start temperature conversion on all connected devices.

This should be followed by a sufficient delay to ensure all devices complete their conversion before the measurements are read.

Parameters
[in]busPointer to initialised bus instance.

Definition at line 487 of file ds18b20.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ds18b20_convert_and_read_temp()

DS18B20_ERROR ds18b20_convert_and_read_temp ( const DS18B20_Info ds18b20_info,
float *  value 
)

Convert, wait and read current temperature from device.

Parameters
[in]ds18b20_infoPointer to device info instance. Must be initialised first.
[out]valuePointer to the measurement value returned by the device, in degrees Celsius.
Returns
DS18B20_OK if read is successful, otherwise error.

Definition at line 555 of file ds18b20.c.

◆ ds18b20_free()

void ds18b20_free ( DS18B20_Info **  ds18b20_info)

Delete an existing device info instance.

Parameters
[in]ds18b20_infoPointer to device info instance.
[in,out]ds18b20_infoPointer to device info instance that will be freed and set to NULL.

Definition at line 352 of file ds18b20.c.

Here is the caller graph for this function:

◆ ds18b20_init()

void ds18b20_init ( DS18B20_Info ds18b20_info,
const OneWireBus bus,
OneWireBus_ROMCode  rom_code 
)

Initialise a device info instance with the specified GPIO.

Parameters
[in]ds18b20_infoPointer to device info instance.
[in]busPointer to initialised 1-Wire bus instance.
[in]rom_codeDevice-specific ROM code to identify a device on the bus.

Definition at line 362 of file ds18b20.c.

Here is the caller graph for this function:

◆ ds18b20_init_solo()

void ds18b20_init_solo ( DS18B20_Info ds18b20_info,
const OneWireBus bus 
)

Initialise a device info instance as a solo device on the bus.

This is subject to the requirement that this device is the ONLY device on the bus. This allows for faster commands to be used without ROM code addressing.

NOTE: if additional devices are added to the bus, operation will cease to work correctly.

Parameters
[in]ds18b20_infoPointer to device info instance.
[in]busPointer to initialised 1-Wire bus instance.

Definition at line 378 of file ds18b20.c.

Here is the caller graph for this function:

◆ ds18b20_malloc()

DS18B20_Info* ds18b20_malloc ( void  )

Construct a new device info instance. New instance should be initialised before calling other functions.

Returns
Pointer to new device info instance, or NULL if it cannot be created.

Definition at line 336 of file ds18b20.c.

Here is the caller graph for this function:

◆ ds18b20_read_resolution()

DS18B20_RESOLUTION ds18b20_read_resolution ( DS18B20_Info ds18b20_info)

Update and return the current temperature measurement resolution from the device.

Parameters
[in]ds18b20_infoPointer to device info instance.
Returns
The currently configured temperature measurement resolution.

Definition at line 443 of file ds18b20.c.

◆ ds18b20_read_temp()

DS18B20_ERROR ds18b20_read_temp ( const DS18B20_Info ds18b20_info,
float *  value 
)

Read last temperature measurement from device.

This is typically called after ds18b20_start_mass_conversion(), provided enough time has elapsed to ensure that all devices have completed their conversions.

Parameters
[in]ds18b20_infoPointer to device info instance. Must be initialised first.
[out]valuePointer to the measurement value returned by the device, in degrees Celsius.
Returns
DS18B20_OK if read is successful, otherwise error.

Definition at line 523 of file ds18b20.c.

Here is the caller graph for this function:

◆ ds18b20_set_resolution()

bool ds18b20_set_resolution ( DS18B20_Info ds18b20_info,
DS18B20_RESOLUTION  resolution 
)

Set temperature measurement resolution.

This programs the hardware to the specified resolution and sets the cached value to be the same. If the program fails, the value currently in hardware is used to refresh the cache.

Parameters
[in]ds18b20_infoPointer to device info instance.
[in]resolutionSelected resolution.
Returns
True if successful, otherwise false.

Definition at line 404 of file ds18b20.c.

Here is the caller graph for this function:

◆ ds18b20_use_crc()

void ds18b20_use_crc ( DS18B20_Info ds18b20_info,
bool  use_crc 
)

Enable or disable use of CRC checks on device communications.

Parameters
[in]ds18b20_infoPointer to device info instance.
[in]use_crcTrue to enable CRC checks, false to disable.

Definition at line 395 of file ds18b20.c.

Here is the caller graph for this function:

◆ ds18b20_wait_for_conversion()

float ds18b20_wait_for_conversion ( const DS18B20_Info ds18b20_info)

Wait for the maximum conversion time according to the current resolution of the device. In external power mode, the device or devices can signal when conversion has completed. In parasitic power mode, this is not possible, so a pre-calculated delay is performed.

Parameters
[in]ds18b20_infoPointer to device info instance.
Returns
An estimate of the time elapsed, in milliseconds. Actual elapsed time may be greater.

Definition at line 503 of file ds18b20.c.

Here is the caller graph for this function: