add 1-wire helper functions
This commit is contained in:
parent
921fd4234a
commit
1264fc0d5a
87
DS2482_HAL.h
87
DS2482_HAL.h
@ -1,18 +1,9 @@
|
||||
#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
|
||||
|
||||
@ -24,6 +15,16 @@
|
||||
|
||||
|
||||
|
||||
#ifdef STM32F3
|
||||
#include "stm32f3xx_hal.h"
|
||||
#elifdef STM32F4
|
||||
#include "stm32f4xx_hal.h"
|
||||
#elifdef STM32H7
|
||||
#include "stm32h7xx_hal.h"
|
||||
#else
|
||||
#error "No target defined"
|
||||
#endif
|
||||
|
||||
#if !MULTIPLE_DEVICES
|
||||
static_assert(DS2482_I2C_ADDR != 0x00, "DS2482_I2C_ADDR must be set in single device mode!");
|
||||
#endif
|
||||
@ -45,28 +46,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
|
Loading…
x
Reference in New Issue
Block a user