stm32-async-1wire 3.2.0
Non-blocking 1-Wire master for STM32 with DS18B20 temperature and DS2431 EEPROM drivers
Loading...
Searching...
No Matches
ds18b20.h File Reference

Non-blocking DS18B20 temperature sensor driver for STM32. More...

#include "ow_config.h"
#include "ow_version.h"
#include <stdint.h>

Go to the source code of this file.

Macros

#define DS18B20_TEMP_ERROR_GENERIC   INT16_MIN
 Special error values (0.1°C units, outside -550..1250 range).
#define DS18B20_TEMP_ERROR_NO_SENSOR   (INT16_MIN + 1)
#define DS18B20_TEMP_ERROR_CRC_FAIL   (INT16_MIN + 2)
#define DS18B20_FAMILY_CODE   0x28
 DS18B20 1-Wire family code (LSB of the 64-bit ROM address).
#define DS18B20_BITS_PER_BYTE   8
 Standard 8 bits per byte.
#define DS18B20_ROM_BYTES   8
 Number of bytes in a device ROM address.
#define DS18B20_SEARCH_ROM   0xF0
 DS18B20 Search ROM command.
#define DS18B20_ALARM_SEARCH   0xEC
 DS18B20 Alarm Search ROM command.
#define DS18B20_MATCH_ROM   0x55
 DS18B20 Match ROM command.
#define DS18B20_CONVERT_T   0x44
 DS18B20 Convert T command.
#define DS18B20_READ_ROM   0x33
 DS18B20 Read ROM command.
#define DS18B20_READ_SCRATCHPAD   0xBE
 DS18B20 Read Scratchpad command.
#define DS18B20_WRITE_SCRATCHPAD   0x4E
 DS18B20 Write Scratchpad command.
#define DS18B20_COPY_SCRATCHPAD   0x48
 DS18B20 Copy Scratchpad command.
#define DS18B20_RECALL_EEPROM   0xB8
 DS18B20 Recall EEPROM command.
#define DS18B20_READ_POWER_SUPPLY   0xB4
 DS18B20 Read Power Supply command.
#define DS18B20_RES_MIN   9
 Minimum supported temperature resolution in bits.
#define DS18B20_RES_MAX   12
 Maximum supported temperature resolution in bits.
#define DS18B20_RES_DEFAULT   12
 Default temperature resolution in bits (power-on default of DS18B20).

Typedefs

typedef uint8_t(* ds18b20_search_sink_t) (const uint8_t *rom)
 Callback invoked for every DS18B20 found by the search.

Enumerations

enum  ds18b20_state_t {
  DS18B20_ST_IDLE = 0 , DS18B20_ST_START , DS18B20_ST_CONVERT , DS18B20_ST_WAIT ,
  DS18B20_ST_CONTINUE , DS18B20_ST_REQUEST , DS18B20_ST_READ , DS18B20_ST_DECODE
}
 DS18B20 driver state machine states. More...

Functions

void ds18b20_search_start (ds18b20_search_sink_t sink, uint8_t max_devices)
 Start a non-blocking device search.
uint8_t ds18b20_search_poll (void)
 Advance the non-blocking device search by one hardware operation.
uint8_t ds18b20_search_count (void)
 Number of DS18B20 devices found (valid once the search finished).
void ds18b20_alarm_search_start (ds18b20_search_sink_t sink, uint8_t max_devices)
 Start a non-blocking alarm search.
uint8_t ds18b20_alarm_search_poll (void)
 Advance the non-blocking alarm search by one hardware operation.
uint8_t ds18b20_alarm_search_count (void)
 Number of DS18B20 devices found in alarm (valid once finished).
void ds18b20_set_resolution (uint8_t bits)
 Start a non-blocking resolution change.
uint8_t ds18b20_set_resolution_poll (void)
 Advance the non-blocking resolution change by one hardware operation.
uint8_t ds18b20_get_resolution (void)
 Current conversion resolution in bits.
void ds18b20_read_rom (uint8_t *rom)
 Read the 64-bit ROM of the (only) DS18B20 on the bus.
uint8_t ds18b20_read_rom_poll (void)
 Advance the non-blocking Read ROM transaction.
void ds18b20_set_alarm_thresholds (uint8_t th, uint8_t tl)
 Configure the alarm trigger thresholds TH and TL.
uint8_t ds18b20_set_alarm_thresholds_poll (void)
 Advance the non-blocking alarm threshold write.
void ds18b20_read_scratchpad (uint8_t *buf)
 Read the 9-byte scratchpad (raw; includes TH, TL and the CRC).
uint8_t ds18b20_read_scratchpad_poll (void)
 Advance the non-blocking raw scratchpad read.
void ds18b20_copy_scratchpad (void)
 Copy the scratchpad into the EEPROM (non-volatile).
uint8_t ds18b20_copy_scratchpad_poll (void)
 Advance the non-blocking Copy Scratchpad transaction.
void ds18b20_recall_eeprom (void)
 Recall the EEPROM contents into the scratchpad.
uint8_t ds18b20_recall_eeprom_poll (void)
 Advance the non-blocking Recall EEPROM transaction.
void ds18b20_set_parasite (uint8_t parasite)
 Declare the bus as parasite-powered.
void ds18b20_detect_parasite (void)
 Detect the bus wiring and configure parasite mode automatically.
uint8_t ds18b20_detect_parasite_poll (void)
 Advance the non-blocking parasite-mode detection.
uint8_t ds18b20_parasite_mode (void)
 Current parasite-power configuration of the driver.
uint8_t ds18b20_last_command_ok (void)
 Result of the last completed command transaction.
void ds18b20_scan_start (void)
 Arm simultaneous conversion of every discovered device.
uint8_t ds18b20_device_count (void)
 Number of DS18B20 devices stored by the driver.
const uint8_t * ds18b20_device_rom (uint8_t index)
 ROM address of a discovered device.
uint8_t ds18b20_scan_index (void)
 Index of the device whose result ds18b20_complete() just reported.
uint8_t ds18b20_crc8 (const uint8_t *data, uint8_t len)
 Calculate Dallas/Maxim CRC-8 over a byte buffer.
void ds18b20_init (void)
 Initialize DS18B20 driver hardware and peripherals.
void ds18b20_poll (void)
 Advance the state machine (non-blocking).
void ds18b20_start_measure (void)
 Start one measurement cycle (non-blocking).
void ds18b20_select (const uint8_t *rom)
 Select which DS18B20 device to measure by its ROM address.
void ds18b20_busy (unsigned action)
 Busy indicator callback (weak).
void ds18b20_complete (int16_t temp)
 Measurement complete callback (weak).

Detailed Description

Non-blocking DS18B20 temperature sensor driver for STM32.

This driver implements a strictly non-blocking interface for the DS18B20 temperature sensor using hardware timers and DMA on STM32 microcontrollers.

Key features:

  • Pure bare-metal, register-level programming
  • No interrupts; no software delays or busy-waits in the measurement path
  • Hardware timer-based timing with DMA for data capture
  • Non-blocking state machine architecture
  • Weak function callbacks for customization
  • Built-in non-blocking device search (ds18b20_search_*) for multi-sensor buses, with zero busy-waits

Measurement model: the driver measures when it is asked to. It runs exactly one conversion + scratchpad-read cycle per ds18b20_start_measure() call, reports the result through ds18b20_complete() and then parks at DS18B20_ST_IDLE. It never starts a cycle on its own, so the measurement cadence, the retry policy and any idle interval belong to the application.

Usage:

  1. Call ds18b20_init() once at startup
  2. Call ds18b20_start_measure() to request a measurement cycle
  3. Call ds18b20_poll() repeatedly from main loop until ds18b20_complete() reports the result
  4. Repeat steps 2-3 at whatever rate the application wants to measure
  5. (Optional) Run the non-blocking device search (ds18b20_search_*) to get the ROM addresses to pass to ds18b20_select() before measuring
  6. Implement weak callbacks ds18b20_busy() and ds18b20_complete() to handle status indication and temperature results

Enumeration Type Documentation

◆ ds18b20_state_t

DS18B20 driver state machine states.

Enumerator
DS18B20_ST_IDLE 

Parked between cycles; ds18b20_start_measure() leaves it

DS18B20_ST_START 

Begin measurement, reset bus

DS18B20_ST_CONVERT 

Check presence, send Convert T command

DS18B20_ST_WAIT 

Wait for conversion to complete (93.75ms @ 9-bit .. 750ms @ 12-bit)

DS18B20_ST_CONTINUE 

Second bus reset before read

DS18B20_ST_REQUEST 

Check presence, send Read Scratchpad command

DS18B20_ST_READ 

Read 72 bits of scratchpad data

DS18B20_ST_DECODE 

Decode data, validate CRC, report result