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

Non-blocking DS2431 1 kbit EEPROM 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 DS2431_FAMILY_CODE   0x2D
 DS2431 1-Wire family code (LSB of the 64-bit ROM address).
#define DS2431_BITS_PER_BYTE   8
 Standard 8 bits per byte.
#define DS2431_ROM_BYTES   8
 Number of bytes in a device ROM address.
#define DS2431_EEPROM_SIZE   128
 DS2431 EEPROM size in bytes (1 kbit).
#define DS2431_PAGE_SIZE   32
 DS2431 EEPROM page size in bytes (4 pages of 32 bytes).
#define DS2431_PAGE_COUNT   (DS2431_EEPROM_SIZE / DS2431_PAGE_SIZE)
 Number of EEPROM pages.
#define DS2431_SCRATCH_SIZE   8
 DS2431 scratchpad size in bytes (one EEPROM block).
#define DS2431_SCRATCH_FRAME   (DS2431_SCRATCH_SIZE + 3)
 Maximum length of the Read Scratchpad reply frame in bytes.
#define DS2431_SCRATCH_FRAME_CRC   (DS2431_SCRATCH_FRAME + 2)
 Maximum wire length of the Read Scratchpad reply in bytes, CRC included.
#define DS2431_READ_MEMORY   0xF0
 DS2431 Read Memory command.
#define DS2431_WRITE_SCRATCHPAD   0x0F
 DS2431 Write Scratchpad command.
#define DS2431_READ_SCRATCHPAD   0xAA
 DS2431 Read Scratchpad command.
#define DS2431_COPY_SCRATCHPAD   0x55
 DS2431 Copy Scratchpad command.
#define DS2431_MATCH_ROM   0x55
 DS2431 Match ROM command (ROM-level addressing prefix).
#define DS2431_SKIP_ROM   0xCC
 DS2431 Skip ROM command (ROM-level broadcast prefix).
#define DS2431_TPROG_US   11000
 Copy Scratchpad EEPROM programming hold-off in microseconds.
#define DS2431_READ_PASS   8
 Maximum EEPROM bytes read by one Read Memory bus transaction.

Functions

void ds2431_init (void)
 Initialize DS2431 driver hardware and peripherals.
void ds2431_select (const uint8_t *rom)
 Select which DS2431 device to address by its ROM address.
void ds2431_set_parasite (uint8_t parasite)
 Declare the bus as parasite-powered.
uint8_t ds2431_parasite_mode (void)
 Current parasite-power configuration of the driver.
uint8_t ds2431_last_command_ok (void)
 Result of the last completed DS2431 operation.
void ds2431_read_memory (uint8_t *dst, uint16_t addr, uint8_t len)
 Read up to DS2431_READ_PASS bytes of EEPROM (non-blocking).
uint8_t ds2431_read_memory_poll (void)
 Advance the non-blocking Read Memory operation.
uint8_t ds2431_write_scratchpad (uint16_t addr, const uint8_t *data, uint8_t len)
 Write up to 8 bytes into the scratchpad (non-blocking).
uint8_t ds2431_write_scratchpad_poll (void)
 Advance the non-blocking Write Scratchpad operation.
void ds2431_read_scratchpad (uint8_t *buf)
 Read the scratchpad frame (non-blocking).
uint8_t ds2431_read_scratchpad_poll (void)
 Advance the non-blocking scratchpad frame read.
void ds2431_copy_scratchpad (void)
 Copy the scratchpad into the EEPROM (non-blocking).
uint8_t ds2431_copy_scratchpad_poll (void)
 Advance the non-blocking Copy Scratchpad operation.

Detailed Description

Non-blocking DS2431 1 kbit EEPROM driver for STM32.

This driver implements a strictly non-blocking interface for the DS2431 EEPROM using hardware timers and DMA on STM32 microcontrollers, on top of the public 1-Wire layer primitives (onewire_reset() / onewire_write_slots() / onewire_read_data()). It is a self-contained second driver: it does not touch the DS18B20 driver or the 1-Wire layer internals, and is split into a core TU (src/ds2431.c, owns the context and the public start/poll API) and an engine TU (src/ds2431_txn.c, reset -> write -> read/wait on the caller-owned context via src/ds2431_internal.h).

Key features:

  • Pure bare-metal, register-level programming
  • No interrupts; no software delays or busy-waits in the measurement path
  • Non-blocking state machine architecture (start/poll pairs)
  • Read Memory (0xF0) in passes of up to 8 bytes per bus transaction
  • Full write cycle: Write Scratchpad (0x0F) -> Read Scratchpad (0xAA, TA/ES echo and written-data read-back verified) -> Copy Scratchpad (0x55)
  • Parasite-power support: the strong pull-up is engaged for the t_PROG programming window of Copy Scratchpad (ds2431_set_parasite(1))
  • Optional Match ROM (0x55 + 8-byte ROM) addressing from a bus search; Skip ROM (broadcast) is the default

Device model (DS2431 datasheet): 128 bytes of EEPROM = 4 pages x 32 bytes, written through an 8-byte scratchpad in page-aligned 8-byte blocks. The scratchpad holds the block being written plus the latched target address and the end-offset status byte ES = (address + length - 1) & 0x07. The Read Scratchpad reply ends with an inverted CRC-16 over the command, the target address (TA1/TA2), the E/S byte and the 8 scratchpad data bytes; the driver reads and verifies it (onewire_crc16()) and additionally compares the TA1/TA2/ES echo against what the last Write Scratchpad latched. Because the device computes that CRC over what it actually stored, the read-back data bytes are also compared with the payload that was sent: a byte corrupted on the way to the device during Write Scratchpad passes the CRC and the echo, so only this comparison catches it before Copy Scratchpad commits it.

Usage:

  1. Call ds2431_init() once at startup. The DS18B20, DS2431 and DS2413 drivers may all be initialized before the first bus operation, but only one of them may own the bus at a time.
  2. (Optional) Run the non-blocking device search (onewire_search_start() with family code DS2431_FAMILY_CODE) to get a ROM address, then call ds2431_select() so subsequent operations are addressed to that device
  3. Read back memory: ds2431_read_memory() + ds2431_read_memory_poll()
  4. Program a page: ds2431_write_scratchpad() (8-byte block) + poll, then ds2431_read_scratchpad() + poll (verify the TA/ES echo and the written data via ds2431_last_command_ok()), then ds2431_copy_scratchpad() + poll
  5. Partial block writes are read-modify-write at the application level: read the 8-byte block, modify the bytes of interest, write the whole block back