Skip to content

nimaltd/ow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

67 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

⚑ Non-Blocking One-Wire (1-Wire) Library for STM32

A lightweight and efficient 1-Wire protocol library written in C for STM32 (HAL-based).

Unlike blocking implementations, this library uses asynchronous, non-blocking operation driven by a single timer interrupt.
It can run on any GPIO pins.

It supports a wide range of Maxim/Dallas devices, including:

  • 🌑️ DS18B20 – Temperature sensor
  • πŸ”‹ DS2431 – EEPROM
  • πŸ” DS28E17 – IΒ²C master bridge
  • πŸ”’ DS1990A iButton – Authentication token
  • ⚑ Any other standard 1-Wire device

The library is designed for:

  • Projects where CPU blocking must be avoided
  • Applications that need multi-device 1-Wire bus support
  • Easy portability across different STM32 families (F0/F1/F3/F4/F7/G0/G4/H7, etc.)

✨ Features

  • πŸ”Ή Single or multiple device support (OW_MAX_DEVICE)
  • πŸ”Ή Full STM32 HAL compatibility
  • πŸ”Ή ROM ID operations (read, match, skip, search)
  • πŸ”Ή Robust error handling (reset, bus, ROM, length)
  • πŸ”Ή Non-blocking operation via timer callbacks
  • πŸ”Ή Clean and modular API
  • πŸ”Ή Support Single and Dual pins (for insolation circuits)

βš™οΈ Installation

You can install in two ways:

1. Copy files directly

Add these files to your STM32 project:

  • ow.h
  • ow.c
  • ow_config.h

2. STM32Cube Pack Installer (Recommended)

Available in the official pack repo:
πŸ‘‰ STM32-PACK (Not Ready)


πŸ”§ Configuration (ow_config.h)

Defines library limits and timing values. Example:

#define OW_MAX_DEVICE     4      // Max number of devices
#define OW_MAX_DATA_LEN   32     // Max data length
#define OW_DUAL_PINS      0      // Enable if using dual pins(TX/RX) for isolation 

πŸ›  CubeMX Setup

  1. GPIO Pin

    • Configure as Output Open-Drain (e.g., PC8).
    • In dual pins mode, set TX pin as Output Push-PULL and set RX pin as ** INPUT ** (e.g., PC8, PC7).
  2. Timer

    • Use internal clock source.
    • Prescaler set for 1 Β΅s tick.
      • Example: 170 MHz bus β†’ Prescaler = 170 - 1.
    • Enable Timer NVIC interrupt.
    • In Project Manager β†’ Advanced Settings, enable Register Callback for the timer.
    • In Project Manager β†’ Code Generator, enable Generate Peripheral initialization as a pair ".c/.h" files per peripheral.

πŸš€ Quick Start

Include header

#include "ow.h"

Define a handle

ow_handle_t ds18;

Create a timer callback

void ds18_tim_cb(TIM_HandleTypeDef *htim)
{
    ow_callback(&ds18);
}

Optional Done Callback

void ds18_done_cb(ow_err_t error)
{
}

Initialize 1 pin mode in main.c

ow_init_t ow_init_struct;
ow_init_struct.tim_handle = &htim1;
ow_init_struct.gpio = GPIOC;
ow_init_struct.pin = GPIO_PIN_8;
ow_init_struct.tim_cb = ds18_tim_cb;
ow_init_struct.done_cb = ds18_done_cb;   // Optional: callback when transfer is done, or can use NULL
ow_init_struct.rom_id_filter = 0;        // 0 = Accept All, or use a value. (Available if OW_MAX_DEVICE > 1) 

ow_init(&ds18, &ow_init_struct);

Now the library is readyβ€”use any ow_* functions.

Example: Reading temperature from DS18B20:

uint8_t data[16];
ow_update_rom_id(&ds18);
while (ow_is_busy(&ds18));
HAL_Delay(10);
ow_xfer_by_id(&ds18, 0, 0x44, NULL, 0, 0);
HAL_Delay(1000);
ow_xfer_by_id(&ds18, 0, 0xBE, NULL, 0, 9);
while (ow_is_busy(&ds18));
ow_read_resp(&ds18, data, 16);

🧰 API Overview

Function Description
ow_init() Initialize one-wire handle
ow_callback() Timer callback (must be called in IRQ)
ow_crc() Calculate CRC
ow_is_busy() Check if bus is busy
ow_last_error() Get last error
ow_update_rom_id() Detect and update connected ROM IDs
ow_xfer() Write command + Read/Write data to/from the bus (no specific ROM ID)
ow_xfer_by_id() Write command + Read/Write data to/from the bus (selected ROM ID)
ow_devices() Get number of detected devices (only if multi-device enabled)
ow_read_resp() Copy response buffer to user data

πŸ’– Support

If you find this project useful, please ⭐ star the repo and consider supporting!

  • GitHub
  • YouTube
  • Instagram
  • LinkedIn
  • Email
  • Ko-fi

πŸ“œ License

Licensed under the terms in the LICENSE.


Releases

No releases published

Sponsor this project

  •  

Packages

No packages published

Languages