Files
templates/c/ds18b20-ds2480/ds2480.h

86 lines
3.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* UART-мост DS2480B для операций 1-Wire: сброс, обмен битами и байтами, поиск устройств и
* управление питанием. После ошибки связи состояние готовности сбрасывается, чтобы следующий
* обмен не продолжался в неизвестном режиме моста.
*/
#ifndef DS2480_H
#define DS2480_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
DS2480_OK = 0, DS2480_DONE, DS2480_NO_PRESENCE, DS2480_SHORT,
DS2480_IO, DS2480_PROTOCOL, DS2480_CRC, DS2480_ARGUMENT,
DS2480_NOT_READY, DS2480_DATA, DS2480_BUSY
} ds2480_status;
/** Blocking UART callbacks: 0 = success, nonzero = error/timeout.
* prepare resets the bridge (BREAK >= 2 ms or hardware reset), sets UART
* to 9600 8N1, waits >= 2 ms after reset, clears RX/errors before returning.
* write waits for physical TX completion; read receives exactly size bytes.
* Both enforce timeout_ms. RX must remain enabled while transmitting.
* delay_ms waits AT LEAST the requested duration. No callback may reenter.
*/
typedef struct {
void *user;
int (*prepare)(void *user);
int (*write)(void *user, const uint8_t *data, uint32_t size, uint32_t timeout_ms);
int (*read)(void *user, uint8_t *data, uint32_t size, uint32_t timeout_ms);
void (*delay_ms)(void *user, uint32_t ms);
} ds2480_port;
/** One instance per UART/bridge; serialize access for the whole operation. */
typedef struct {
ds2480_port port;
uint32_t timeout_ms;
uint8_t ready;
uint8_t power_active;
uint8_t data_mode;
} ds2480;
/** Independent ROM search cursor. Zero-initialize before each enumeration. */
typedef struct {
uint8_t rom[8];
uint8_t discrepancy;
uint8_t done;
} ds2480_search;
/** Reset/calibrate DS2480B and verify configuration. Empty bus is allowed.
* On IO/protocol errors call init again before further transactions.
*/
ds2480_status ds2480_init(ds2480 *bus, const ds2480_port *port, uint32_t timeout_ms);
/** Standard-speed reset; distinguish short, empty bus and transport failure. */
ds2480_status ds2480_reset(ds2480 *bus);
/** Exchange one slot (write 1 to read); received must be non-NULL. */
ds2480_status ds2480_bit(ds2480 *bus, uint8_t bit, uint8_t *received);
/** Exchange a byte in Data Mode; escapes E3 and preserves one reply per byte. */
ds2480_status ds2480_byte(ds2480 *bus, uint8_t value, uint8_t *received);
/** Write a byte and start strong pullup immediately after its last slot.
* Blocks for hold_ms (1..1000), then terminates the pulse and consumes reply.
*/
ds2480_status ds2480_power_byte(ds2480 *bus, uint8_t value, uint32_t hold_ms);
/** Start indefinite strong pullup after the final bit; returns immediately
* after UART exchange. Until power_end, other bus operations return BUSY.
* Application MUST call power_end after the sensor's required hold time,
* or init to cancel/recover. No internal timer/interrupt releases the pulse.
*/
ds2480_status ds2480_power_begin(ds2480 *bus, uint8_t value);
/** Release strong pullup, consume its reply. Idempotent when ready/idle. */
ds2480_status ds2480_power_end(ds2480 *bus);
/** CRC8 Dallas/Maxim. data must address size bytes (NULL allowed for size=0). */
uint8_t ds2480_crc8(const uint8_t *data, uint32_t size);
/** Search ALL families. OK yields ROM with valid CRC; DONE ends enumeration.
* Cursor changes only on success. After an error retry or zero it to restart.
*/
ds2480_status ds2480_search_next(ds2480 *bus, ds2480_search *search);
#ifdef __cplusplus
}
#endif
#endif