From befb7e006cd335315d7d8fa9d9826c58f18b2f8f Mon Sep 17 00:00:00 2001 From: kbracher Date: Sat, 29 Jun 2024 18:26:12 +0200 Subject: [PATCH] add 1-wire helper functions --- DS2482_HAL.h | 66 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 14 deletions(-) diff --git a/DS2482_HAL.h b/DS2482_HAL.h index 75d8d10..b974330 100644 --- a/DS2482_HAL.h +++ b/DS2482_HAL.h @@ -45,28 +45,66 @@ static_assert (DS2482_TIMEOUT_I2C > 0, "DS2482_TIMEOUT_I2C cannot be negative"); return (ds2482_t){.hi2c = hi2c, .address = address}; } - #define DEV_ARG const ds2482_t * const ds2482, + #define DEV_ARG ds2482, + #define DEV_ARG_DEF const ds2482_t * const ds2482, + #define DEV_ARG_ONE ds2482 + #define DEV_ARG_ONE_DEF 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 + #define DEV_ARG_DEF + #define DEV_ARG_ONE + #define DEV_ARG_ONE_DEF + nag_attr HAL_StatusTypeDef ds2482_init(I2C_HandleTypeDef* hi2c); #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_reset(DEV_ARG_ONE_DEF); +nag_attr HAL_StatusTypeDef ds2482_write_config(DEV_ARG_DEF uint8_t config); +nag_attr HAL_StatusTypeDef ds2482_set_read_ptr(DEV_ARG_DEF uint8_t read_ptr); +nag_attr HAL_StatusTypeDef ds2482_1w_reset(DEV_ARG_DEF bool* presence); +nag_attr HAL_StatusTypeDef ds2482_1w_write_byte(DEV_ARG_DEF uint8_t byte); +nag_attr HAL_StatusTypeDef ds2482_1w_read_byte(DEV_ARG_DEF uint8_t* byte); +nag_attr HAL_StatusTypeDef ds2482_1w_read_bit(DEV_ARG_DEF bool* bit); +nag_attr HAL_StatusTypeDef ds2482_1w_write_bit(DEV_ARG_DEF bool bit); +nag_attr HAL_StatusTypeDef ds2482_1w_triplet(DEV_ARG_DEF 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); +nag_attr HAL_StatusTypeDef ds2482_1w_search(DEV_ARG_DEF uint16_t max_devices, uint64_t devices[static max_devices]); +nag_attr HAL_StatusTypeDef ds2482_1w_verify_device(DEV_ARG_DEF uint64_t device, bool* present); + +//helper functions + +#define DS2482_1W_ROM_MATCH 0x55 +#define DS2482_1W_ROM_SKIP 0xCC + +nag_attr static inline HAL_StatusTypeDef ds2482_1w_select_device(DEV_ARG_DEF uint64_t device) { + bool present; + HAL_StatusTypeDef status = ds2482_1w_reset(DEV_ARG &present); + if (status != HAL_OK) return status; + if (!present) return HAL_ERROR; + status = ds2482_1w_write_byte(DEV_ARG DS2482_1W_ROM_MATCH); + if (status != HAL_OK) return status; + for (int i = 0; i < 8; i++) { + status = ds2482_1w_write_byte(DEV_ARG (device >> (i * 8)) & 0xFF); + if (status != HAL_OK) return status; + } + return HAL_OK; +} + +nag_attr static inline HAL_StatusTypeDef ds2482_1w_select_all(DEV_ARG_ONE_DEF) { + bool present; + HAL_StatusTypeDef status = ds2482_1w_reset(DEV_ARG &present); + if (status != HAL_OK) return status; + if (!present) return HAL_ERROR; + return ds2482_1w_write_byte(DEV_ARG DS2482_1W_ROM_SKIP); +} #undef nag_attr +#undef DEV_ARG_DEF #undef DEV_ARG +#undef DEV_ARG_ONE +#undef DEV_ARG_ONE_DEF + +#undef DS2482_1W_ROM_MATCH +#undef DS2482_1W_ROM_SKIP #endif // DS2482_HAL_H \ No newline at end of file