STM32-DS2482-HAL/DS2482_HAL.h

72 lines
2.5 KiB
C

#pragma once
#include <stdint.h>
#ifndef DS2482_HAL_H
#define DS2482_HAL_H
#ifdef STM32F3
#include "stm32f3xx_hal.h"
#elifdef STM32F4
#include "stm32f4xx_hal.h"
#elifdef STM32H7
#include "stm32h7xx_hal.h"
#else
#error "No target defined"
#endif
#define MULTIPLE_DEVICES false //set to true if multiple DS2482 devices are connected
#if !MULTIPLE_DEVICES
#define DS2482_I2C_ADDR 0x00 //set to I2C address of the DS2482 device (if in single device mode)
#endif
#define DS2482_TIMEOUT_1W 100 //timeout in ms for 1-wire operations
#define DS2482_TIMEOUT_I2C 100 //timeout in ms for I2C operations
#if !MULTIPLE_DEVICES
static_assert(DS2482_I2C_ADDR != 0x00, "DS2482_I2C_ADDR must be set in single device mode!");
#endif
static_assert (DS2482_TIMEOUT_1W < UINT16_MAX, "DS2482_TIMEOUT_1W must be less than 65535");
static_assert (DS2482_TIMEOUT_I2C < UINT32_MAX, "DS2482_TIMEOUT_I2C must be less than 4294967295");
static_assert (DS2482_TIMEOUT_1W > 0, "DS2482_TIMEOUT_1W cannot be negative");
static_assert (DS2482_TIMEOUT_I2C > 0, "DS2482_TIMEOUT_I2C cannot be negative");
#define nag_attr [[nodiscard("Check status for errors!")]]
#if MULTIPLE_DEVICES
typedef struct {
I2C_HandleTypeDef* hi2c;
uint16_t address;
} ds2482_t;
static inline ds2482_t ds2482_create(I2C_HandleTypeDef* hi2c, uint16_t address) {
return (ds2482_t){.hi2c = hi2c, .address = address};
}
#define DEV_ARG const ds2482_t * const ds2482,
nag_attr HAL_StatusTypeDef ds2482_init(const ds2482_t * const ds2482);
nag_attr HAL_StatusTypeDef ds2482_reset(const ds2482_t * const ds2482);
#else
nag_attr HAL_StatusTypeDef ds2482_init(I2C_HandleTypeDef* hi2c);
nag_attr HAL_StatusTypeDef ds2482_reset();
#define DEV_ARG
#endif
nag_attr HAL_StatusTypeDef ds2482_write_config(DEV_ARG uint8_t config);
nag_attr HAL_StatusTypeDef ds2482_set_read_ptr(DEV_ARG uint8_t read_ptr);
nag_attr HAL_StatusTypeDef ds2482_1w_reset(DEV_ARG bool* presence);
nag_attr HAL_StatusTypeDef ds2482_1w_write_byte(DEV_ARG uint8_t byte);
nag_attr HAL_StatusTypeDef ds2482_1w_read_byte(DEV_ARG uint8_t* byte);
nag_attr HAL_StatusTypeDef ds2482_1w_read_bit(DEV_ARG bool* bit);
nag_attr HAL_StatusTypeDef ds2482_1w_write_bit(DEV_ARG bool bit);
nag_attr HAL_StatusTypeDef ds2482_1w_triplet(DEV_ARG uint8_t dir);
nag_attr HAL_StatusTypeDef ds2482_1w_search(DEV_ARG uint16_t max_devices, uint64_t devices[static max_devices]);
nag_attr HAL_StatusTypeDef ds2482_1w_verify_device(DEV_ARG uint64_t device, bool* present);
#undef nag_attr
#undef DEV_ARG
#endif // DS2482_HAL_H