Compare commits

...

2 Commits

Author SHA1 Message Date
8cead7ac35
add helper to send command to all devices 2024-08-23 17:37:23 +02:00
8e06b991ac
remove single-device mode
it made the code more confusing than it needed to be
2024-08-23 17:27:50 +02:00
4 changed files with 82 additions and 125 deletions

5
.gitignore vendored
View File

@ -52,3 +52,8 @@ Module.symvers
Mkfile.old Mkfile.old
dkms.conf dkms.conf
.gitignore
Drivers/
.cache/
.clangd
compile_commands.json

View File

@ -29,35 +29,16 @@ typedef struct {
uint8_t DIR : 1; uint8_t DIR : 1;
} DS2482_Status; } DS2482_Status;
#if MULTIPLE_DEVICES
#define hi2c ds2482->hi2c #define hi2c ds2482->hi2c
#define DS2482_I2C_ADDR ds2482->address #define DS2482_I2C_ADDR ds2482->address
#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
#else
#define DEV_ARG
#define DEV_ARG_DEF
#define DEV_ARG_ONE
#define DEV_ARG_ONE_DEF
static I2C_HandleTypeDef* hi2c; HAL_StatusTypeDef ds2482_init(ds2482_t* const ds2482) {
#endif HAL_StatusTypeDef status = ds2482_reset(ds2482);
#if MULTIPLE_DEVICES
HAL_StatusTypeDef ds2482_init(DEV_ARG_ONE_DEF) {
#else
HAL_StatusTypeDef ds2482_init(I2C_HandleTypeDef* handle) {
hi2c = handle;
#endif
HAL_StatusTypeDef status = ds2482_reset(DEV_ARG_ONE);
if (status != HAL_OK) { if (status != HAL_OK) {
return status; return status;
} }
status = ds2482_write_config(DEV_ARG DS2482_CONFIG_APU); status = ds2482_write_config(ds2482, DS2482_CONFIG_APU);
if (status != HAL_OK) { if (status != HAL_OK) {
return status; return status;
} }
@ -73,29 +54,29 @@ HAL_StatusTypeDef ds2482_init(I2C_HandleTypeDef* handle) {
return HAL_OK; return HAL_OK;
} }
HAL_StatusTypeDef ds2482_reset(DEV_ARG_ONE_DEF) { HAL_StatusTypeDef ds2482_reset(ds2482_t* const ds2482) {
uint8_t data[1] = {DS2482_CMD_RESET}; uint8_t data[1] = {DS2482_CMD_RESET};
return HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C); return HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C);
} }
HAL_StatusTypeDef ds2482_write_config(DEV_ARG_DEF uint8_t config) { HAL_StatusTypeDef ds2482_write_config(ds2482_t * const ds2482, uint8_t config) {
uint8_t data[2] = {DS2482_CMD_WRITE_CONFIG, config | (~config << 4)}; uint8_t data[2] = {DS2482_CMD_WRITE_CONFIG, config | (~config << 4)};
return HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C); return HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C);
} }
HAL_StatusTypeDef ds2482_set_read_ptr(DEV_ARG_DEF uint8_t read_ptr) { HAL_StatusTypeDef ds2482_set_read_ptr(ds2482_t * const ds2482, uint8_t read_ptr) {
uint8_t data[2] = {DS2482_CMD_SET_READ_PTR, read_ptr}; uint8_t data[2] = {DS2482_CMD_SET_READ_PTR, read_ptr};
return HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C); return HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C);
} }
HAL_StatusTypeDef ds2482_1w_reset(DEV_ARG_DEF bool* presence) { HAL_StatusTypeDef ds2482_1w_reset(ds2482_t * const ds2482, bool* presence) {
uint8_t data[1] = {DS2482_CMD_1W_RESET}; uint8_t data[1] = {DS2482_CMD_1W_RESET};
HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C); HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C);
if (status != HAL_OK) { if (status != HAL_OK) {
return status; return status;
} }
DS2482_Status status_reg = {.BUSY = 1}; DS2482_Status status_reg = {.BUSY = 1};
uint16_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W; uint32_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W;
do { do {
status = HAL_I2C_Master_Receive(hi2c, DS2482_I2C_ADDR, (uint8_t*)&status_reg, 1, DS2482_TIMEOUT_I2C); status = HAL_I2C_Master_Receive(hi2c, DS2482_I2C_ADDR, (uint8_t*)&status_reg, 1, DS2482_TIMEOUT_I2C);
if (status != HAL_OK) { if (status != HAL_OK) {
@ -110,12 +91,14 @@ HAL_StatusTypeDef ds2482_1w_reset(DEV_ARG_DEF bool* presence) {
return HAL_ERROR; return HAL_ERROR;
} }
if (presence != NULL) {
*presence = status_reg.PPD; *presence = status_reg.PPD;
}
return HAL_OK; return HAL_OK;
} }
HAL_StatusTypeDef ds2482_1w_write_byte(DEV_ARG_DEF uint8_t byte) { HAL_StatusTypeDef ds2482_1w_write_byte(ds2482_t * const ds2482, uint8_t byte) {
uint8_t data[2] = {DS2482_CMD_1W_WRITE_BYTE, byte}; uint8_t data[2] = {DS2482_CMD_1W_WRITE_BYTE, byte};
HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C); HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C);
if (status != HAL_OK) { if (status != HAL_OK) {
@ -123,7 +106,7 @@ HAL_StatusTypeDef ds2482_1w_write_byte(DEV_ARG_DEF uint8_t byte) {
} }
DS2482_Status status_reg = {.BUSY = 1}; DS2482_Status status_reg = {.BUSY = 1};
uint16_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W; uint32_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W;
do { do {
status = HAL_I2C_Master_Receive(hi2c, DS2482_I2C_ADDR, (uint8_t*)&status_reg, 1, DS2482_TIMEOUT_I2C); status = HAL_I2C_Master_Receive(hi2c, DS2482_I2C_ADDR, (uint8_t*)&status_reg, 1, DS2482_TIMEOUT_I2C);
if (status != HAL_OK) { if (status != HAL_OK) {
@ -137,7 +120,7 @@ HAL_StatusTypeDef ds2482_1w_write_byte(DEV_ARG_DEF uint8_t byte) {
return HAL_OK; return HAL_OK;
} }
HAL_StatusTypeDef ds2482_1w_read_byte(DEV_ARG_DEF uint8_t* byte) { HAL_StatusTypeDef ds2482_1w_read_byte(ds2482_t * const ds2482, uint8_t* byte) {
uint8_t data[1] = {DS2482_CMD_1W_READ_BYTE}; uint8_t data[1] = {DS2482_CMD_1W_READ_BYTE};
HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C); HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C);
if (status != HAL_OK) { if (status != HAL_OK) {
@ -145,7 +128,7 @@ HAL_StatusTypeDef ds2482_1w_read_byte(DEV_ARG_DEF uint8_t* byte) {
} }
DS2482_Status status_reg = {.BUSY = 1}; DS2482_Status status_reg = {.BUSY = 1};
uint16_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W; uint32_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W;
do { do {
status = HAL_I2C_Master_Receive(hi2c, DS2482_I2C_ADDR, (uint8_t*)&status_reg, 1, DS2482_TIMEOUT_I2C); status = HAL_I2C_Master_Receive(hi2c, DS2482_I2C_ADDR, (uint8_t*)&status_reg, 1, DS2482_TIMEOUT_I2C);
if (status != HAL_OK) { if (status != HAL_OK) {
@ -156,7 +139,7 @@ HAL_StatusTypeDef ds2482_1w_read_byte(DEV_ARG_DEF uint8_t* byte) {
} }
} while (status_reg.BUSY); } while (status_reg.BUSY);
status = ds2482_set_read_ptr(DEV_ARG DS2482_READ_DATA); status = ds2482_set_read_ptr(ds2482, DS2482_READ_DATA);
if (status != HAL_OK) { if (status != HAL_OK) {
return status; return status;
} }
@ -164,12 +147,12 @@ HAL_StatusTypeDef ds2482_1w_read_byte(DEV_ARG_DEF uint8_t* byte) {
return HAL_I2C_Master_Receive(hi2c, DS2482_I2C_ADDR, byte, 1, DS2482_TIMEOUT_I2C); return HAL_I2C_Master_Receive(hi2c, DS2482_I2C_ADDR, byte, 1, DS2482_TIMEOUT_I2C);
} }
HAL_StatusTypeDef ds2482_1w_triplet(DEV_ARG_DEF uint8_t dir) { HAL_StatusTypeDef ds2482_1w_triplet(ds2482_t * const ds2482, uint8_t dir) {
uint8_t data[2] = {DS2482_CMD_1W_TRIPLET, dir ? 0xFF : 0x00}; uint8_t data[2] = {DS2482_CMD_1W_TRIPLET, dir ? 0xFF : 0x00};
return HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C); return HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C);
} }
HAL_StatusTypeDef ds2482_1w_write_bit(DEV_ARG_DEF bool bit) { HAL_StatusTypeDef ds2482_1w_write_bit(ds2482_t * const ds2482, bool bit) {
uint8_t data[2] = {DS2482_CMD_1W_SINGLE_BIT, bit ? 0xFF : 0x00}; uint8_t data[2] = {DS2482_CMD_1W_SINGLE_BIT, bit ? 0xFF : 0x00};
HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C); HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C);
if (status != HAL_OK) { if (status != HAL_OK) {
@ -177,7 +160,7 @@ HAL_StatusTypeDef ds2482_1w_write_bit(DEV_ARG_DEF bool bit) {
} }
DS2482_Status status_reg = {.BUSY = 1}; DS2482_Status status_reg = {.BUSY = 1};
uint16_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W; uint32_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W;
do { do {
status = HAL_I2C_Master_Receive(hi2c, DS2482_I2C_ADDR, (uint8_t*)&status_reg, 1, DS2482_TIMEOUT_I2C); status = HAL_I2C_Master_Receive(hi2c, DS2482_I2C_ADDR, (uint8_t*)&status_reg, 1, DS2482_TIMEOUT_I2C);
if (status != HAL_OK) { if (status != HAL_OK) {
@ -191,7 +174,7 @@ HAL_StatusTypeDef ds2482_1w_write_bit(DEV_ARG_DEF bool bit) {
return HAL_OK; return HAL_OK;
} }
HAL_StatusTypeDef ds2482_1w_read_bit(DEV_ARG_DEF bool* bit) { HAL_StatusTypeDef ds2482_1w_read_bit(ds2482_t * const ds2482, bool* bit) {
uint8_t data[2] = {DS2482_CMD_1W_SINGLE_BIT, 0xFF}; uint8_t data[2] = {DS2482_CMD_1W_SINGLE_BIT, 0xFF};
HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C); HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(hi2c, DS2482_I2C_ADDR, data, sizeof(data), DS2482_TIMEOUT_I2C);
if (status != HAL_OK) { if (status != HAL_OK) {
@ -199,7 +182,7 @@ HAL_StatusTypeDef ds2482_1w_read_bit(DEV_ARG_DEF bool* bit) {
} }
DS2482_Status status_reg = {.BUSY = 1}; DS2482_Status status_reg = {.BUSY = 1};
uint16_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W; uint32_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W;
do { do {
status = HAL_I2C_Master_Receive(hi2c, DS2482_I2C_ADDR, (uint8_t*)&status_reg, 1, DS2482_TIMEOUT_I2C); status = HAL_I2C_Master_Receive(hi2c, DS2482_I2C_ADDR, (uint8_t*)&status_reg, 1, DS2482_TIMEOUT_I2C);
if (status != HAL_OK) { if (status != HAL_OK) {
@ -268,30 +251,30 @@ static uint8_t crc8;
// Return true : device present // Return true : device present
// false : no device present // false : no device present
// //
static bool OWReset(DEV_ARG_ONE_DEF) { static bool OWReset(ds2482_t* const ds2482) {
bool presence; bool presence;
(void)ds2482_1w_reset(DEV_ARG &presence); (void)ds2482_1w_reset(ds2482, &presence);
return presence; return presence;
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// Send 8 bits of data to the 1-Wire bus // Send 8 bits of data to the 1-Wire bus
// //
static inline void OWWriteByte(DEV_ARG_DEF uint8_t byte_value) { (void)ds2482_1w_write_byte(DEV_ARG byte_value); } static inline void OWWriteByte(ds2482_t * const ds2482, uint8_t byte_value) { (void)ds2482_1w_write_byte(ds2482, byte_value); }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// Send 1 bit of data to teh 1-Wire bus // Send 1 bit of data to teh 1-Wire bus
// //
static inline void OWWriteBit(DEV_ARG_DEF uint8_t bit_value) { (void)ds2482_1w_write_bit(DEV_ARG bit_value); } static inline void OWWriteBit(ds2482_t * const ds2482, uint8_t bit_value) { (void)ds2482_1w_write_bit(ds2482, bit_value); }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// Read 1 bit of data from the 1-Wire bus // Read 1 bit of data from the 1-Wire bus
// Return 1 : bit read is 1 // Return 1 : bit read is 1
// 0 : bit read is 0 // 0 : bit read is 0
// //
static inline uint8_t OWReadBit(DEV_ARG_ONE_DEF) { static inline uint8_t OWReadBit(ds2482_t* const ds2482) {
bool bit; bool bit;
(void)ds2482_1w_read_bit(DEV_ARG &bit); (void)ds2482_1w_read_bit(ds2482, &bit);
return bit; return bit;
} }
@ -326,7 +309,7 @@ static uint8_t docrc8(uint8_t value) {
// Return true : device found, ROM number in ROM_NO buffer // Return true : device found, ROM number in ROM_NO buffer
// false : device not found, end of search // false : device not found, end of search
// //
static bool OWSearch(DEV_ARG_ONE_DEF) { static bool OWSearch(ds2482_t* const ds2482) {
int id_bit_number; int id_bit_number;
int last_zero, rom_byte_number, search_result; int last_zero, rom_byte_number, search_result;
int id_bit, cmp_id_bit; int id_bit, cmp_id_bit;
@ -343,7 +326,7 @@ static bool OWSearch(DEV_ARG_ONE_DEF) {
// if the last call was not the last one // if the last call was not the last one
if (!LastDeviceFlag) { if (!LastDeviceFlag) {
// 1-Wire reset // 1-Wire reset
if (!OWReset(DEV_ARG_ONE)) { if (!OWReset(ds2482)) {
// reset the search // reset the search
LastDiscrepancy = 0; LastDiscrepancy = 0;
LastDeviceFlag = false; LastDeviceFlag = false;
@ -352,13 +335,13 @@ static bool OWSearch(DEV_ARG_ONE_DEF) {
} }
// issue the search command // issue the search command
OWWriteByte(DEV_ARG 0xF0); OWWriteByte(ds2482, 0xF0);
// loop to do the search // loop to do the search
do { do {
// read a bit and its complement // read a bit and its complement
id_bit = OWReadBit(DEV_ARG_ONE); id_bit = OWReadBit(ds2482);
cmp_id_bit = OWReadBit(DEV_ARG_ONE); cmp_id_bit = OWReadBit(ds2482);
// check for no devices on 1-wire // check for no devices on 1-wire
if ((id_bit == 1) && (cmp_id_bit == 1)) if ((id_bit == 1) && (cmp_id_bit == 1))
@ -394,7 +377,7 @@ static bool OWSearch(DEV_ARG_ONE_DEF) {
ROM_NO[rom_byte_number] &= ~rom_byte_mask; ROM_NO[rom_byte_number] &= ~rom_byte_mask;
// serial number search direction write bit // serial number search direction write bit
OWWriteBit(DEV_ARG search_direction); OWWriteBit(ds2482, search_direction);
// increment the byte counter id_bit_number // increment the byte counter id_bit_number
// and shift the mask rom_byte_mask // and shift the mask rom_byte_mask
@ -439,13 +422,13 @@ static bool OWSearch(DEV_ARG_ONE_DEF) {
// Return true : device found, ROM number in ROM_NO buffer // Return true : device found, ROM number in ROM_NO buffer
// false : no device present // false : no device present
// //
static bool OWFirst(DEV_ARG_ONE_DEF) { static bool OWFirst(ds2482_t* const ds2482) {
// reset the search state // reset the search state
LastDiscrepancy = 0; LastDiscrepancy = 0;
LastDeviceFlag = false; LastDeviceFlag = false;
LastFamilyDiscrepancy = 0; LastFamilyDiscrepancy = 0;
return OWSearch(DEV_ARG_ONE); return OWSearch(ds2482);
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
@ -453,10 +436,10 @@ static bool OWFirst(DEV_ARG_ONE_DEF) {
// Return true : device found, ROM number in ROM_NO buffer // Return true : device found, ROM number in ROM_NO buffer
// false : device not found, end of search // false : device not found, end of search
// //
static inline bool OWNext(DEV_ARG_ONE_DEF) { return OWSearch(DEV_ARG_ONE); } static inline bool OWNext(ds2482_t* const ds2482) { return OWSearch(ds2482); }
// Verify the device with the ROM number in ROM_NO buffer is present. // Verify the device with the ROM number in ROM_NO buffer is present.
HAL_StatusTypeDef ds2482_1w_verify_device(DEV_ARG_DEF uint64_t device, bool* present) { HAL_StatusTypeDef ds2482_1w_verify_device(ds2482_t * const ds2482, uint64_t device, bool* present) {
uint8_t rom_backup[8]; uint8_t rom_backup[8];
int i, rslt; int i, rslt;
@ -469,7 +452,7 @@ HAL_StatusTypeDef ds2482_1w_verify_device(DEV_ARG_DEF uint64_t device, bool* pre
LastDiscrepancy = 64; LastDiscrepancy = 64;
LastDeviceFlag = false; LastDeviceFlag = false;
if (OWSearch(DEV_ARG_ONE)) { if (OWSearch(ds2482)) {
// check if same device found // check if same device found
rslt = true; rslt = true;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
@ -487,8 +470,8 @@ HAL_StatusTypeDef ds2482_1w_verify_device(DEV_ARG_DEF uint64_t device, bool* pre
HAL_StatusTypeDef ds2482_1w_search(DEV_ARG_DEF uint16_t max_devices, uint64_t devices[static max_devices]) { HAL_StatusTypeDef ds2482_1w_search(ds2482_t * const ds2482, uint16_t max_devices, uint64_t devices[static max_devices]) {
OWFirst(DEV_ARG_ONE); OWFirst(ds2482);
uint16_t count = 0; uint16_t count = 0;
do { do {
uint64_t device = 0; uint64_t device = 0;
@ -496,7 +479,7 @@ HAL_StatusTypeDef ds2482_1w_search(DEV_ARG_DEF uint16_t max_devices, uint64_t de
device |= (uint64_t)ROM_NO[i] << (i * 8); device |= (uint64_t)ROM_NO[i] << (i * 8);
} }
devices[count++] = device; devices[count++] = device;
} while (OWNext(DEV_ARG_ONE) && count < max_devices); } while (OWNext(ds2482) && count < max_devices);
return HAL_OK; return HAL_OK;
} }

View File

@ -3,18 +3,9 @@
#ifndef DS2482_HAL_H #ifndef DS2482_HAL_H
#define DS2482_HAL_H #define DS2482_HAL_H
#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_1W 100 //timeout in ms for 1-wire operations
#define DS2482_TIMEOUT_I2C 100 //timeout in ms for I2C operations #define DS2482_TIMEOUT_I2C 100 //timeout in ms for I2C operations
#ifdef STM32F3 #ifdef STM32F3
#include "stm32f3xx_hal.h" #include "stm32f3xx_hal.h"
#elifdef STM32F4 #elifdef STM32F4
@ -25,10 +16,6 @@
#error "No target defined" #error "No target defined"
#endif #endif
#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_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_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_1W > 0, "DS2482_TIMEOUT_1W cannot be negative");
@ -36,74 +23,67 @@ static_assert (DS2482_TIMEOUT_I2C > 0, "DS2482_TIMEOUT_I2C cannot be negative");
#define nag_attr [[nodiscard("Check status for errors!")]] #define nag_attr [[nodiscard("Check status for errors!")]]
#if MULTIPLE_DEVICES typedef struct {
typedef struct { I2C_HandleTypeDef* hi2c;
I2C_HandleTypeDef* hi2c; uint16_t address;
uint16_t address; } ds2482_t;
} ds2482_t;
static inline ds2482_t ds2482_create(I2C_HandleTypeDef* hi2c, uint16_t address) { #define ds2482_create(hi2c, address) ((constexpr ds2482_t){.hi2c = hi2c, .address = address})
return (ds2482_t){.hi2c = hi2c, .address = address};
}
#define DEV_ARG ds2482, nag_attr HAL_StatusTypeDef ds2482_init(ds2482_t * const ds2482);
#define DEV_ARG_DEF const ds2482_t * const ds2482, nag_attr HAL_StatusTypeDef ds2482_reset(ds2482_t * const ds2482);
#define DEV_ARG_ONE ds2482 nag_attr HAL_StatusTypeDef ds2482_write_config(ds2482_t * const ds2482, uint8_t config);
#define DEV_ARG_ONE_DEF const ds2482_t* const ds2482 nag_attr HAL_StatusTypeDef ds2482_set_read_ptr(ds2482_t * const ds2482, uint8_t read_ptr);
nag_attr HAL_StatusTypeDef ds2482_init(const ds2482_t * const ds2482); nag_attr HAL_StatusTypeDef ds2482_1w_reset(ds2482_t * const ds2482, bool * const presence);
#else nag_attr HAL_StatusTypeDef ds2482_1w_write_byte(ds2482_t * const ds2482, uint8_t byte);
#define DEV_ARG nag_attr HAL_StatusTypeDef ds2482_1w_read_byte(ds2482_t * const ds2482, uint8_t * const byte);
#define DEV_ARG_DEF nag_attr HAL_StatusTypeDef ds2482_1w_read_bit(ds2482_t * const ds2482, bool * const bit);
#define DEV_ARG_ONE nag_attr HAL_StatusTypeDef ds2482_1w_write_bit(ds2482_t * const ds2482, bool bit);
#define DEV_ARG_ONE_DEF nag_attr HAL_StatusTypeDef ds2482_1w_triplet(ds2482_t * const ds2482, uint8_t dir);
nag_attr HAL_StatusTypeDef ds2482_init(I2C_HandleTypeDef* hi2c);
#endif
nag_attr HAL_StatusTypeDef ds2482_reset(DEV_ARG_ONE_DEF); nag_attr HAL_StatusTypeDef ds2482_1w_search(ds2482_t * const ds2482, uint16_t max_devices, uint64_t devices[static max_devices]);
nag_attr HAL_StatusTypeDef ds2482_write_config(DEV_ARG_DEF uint8_t config); nag_attr HAL_StatusTypeDef ds2482_1w_verify_device(ds2482_t * const ds2482, uint64_t device, bool * const present);
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_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 //helper functions
#define DS2482_1W_ROM_MATCH 0x55 #define DS2482_1W_ROM_MATCH 0x55
#define DS2482_1W_ROM_SKIP 0xCC #define DS2482_1W_ROM_SKIP 0xCC
nag_attr static inline HAL_StatusTypeDef ds2482_1w_select_device(DEV_ARG_DEF uint64_t device) { nag_attr static inline HAL_StatusTypeDef ds2482_1w_select_device(ds2482_t * const ds2482, uint64_t device) {
bool present; bool present;
HAL_StatusTypeDef status = ds2482_1w_reset(DEV_ARG &present); HAL_StatusTypeDef status = ds2482_1w_reset(ds2482, &present);
if (status != HAL_OK) return status; if (status != HAL_OK) return status;
if (!present) return HAL_ERROR; if (!present) return HAL_ERROR;
status = ds2482_1w_write_byte(DEV_ARG DS2482_1W_ROM_MATCH); status = ds2482_1w_write_byte(ds2482, DS2482_1W_ROM_MATCH);
if (status != HAL_OK) return status; if (status != HAL_OK) return status;
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
status = ds2482_1w_write_byte(DEV_ARG (device >> (i * 8)) & 0xFF); status = ds2482_1w_write_byte(ds2482, (device >> (i * 8)) & 0xFF);
if (status != HAL_OK) return status; if (status != HAL_OK) return status;
} }
return HAL_OK; return HAL_OK;
} }
nag_attr static inline HAL_StatusTypeDef ds2482_1w_select_all(DEV_ARG_ONE_DEF) { nag_attr static inline HAL_StatusTypeDef ds2482_1w_select_all(ds2482_t * const ds2482) {
bool present; bool present;
HAL_StatusTypeDef status = ds2482_1w_reset(DEV_ARG &present); HAL_StatusTypeDef status = ds2482_1w_reset(ds2482, &present);
if (status != HAL_OK) return status; if (status != HAL_OK) return status;
if (!present) return HAL_ERROR; if (!present) return HAL_ERROR;
return ds2482_1w_write_byte(DEV_ARG DS2482_1W_ROM_SKIP); return ds2482_1w_write_byte(ds2482, DS2482_1W_ROM_SKIP);
}
nag_attr static inline HAL_StatusTypeDef ds2482_1w_send_command(ds2482_t * const ds2482, uint64_t device, uint8_t command) {
HAL_StatusTypeDef status = ds2482_1w_select_device(ds2482, device);
if (status != HAL_OK) return status;
return ds2482_1w_write_byte(ds2482, command);
}
nag_attr static inline HAL_StatusTypeDef ds2482_1w_send_command_all(ds2482_t * const ds2482, uint8_t command) {
HAL_StatusTypeDef status = ds2482_1w_select_all(ds2482);
if (status != HAL_OK) return status;
return ds2482_1w_write_byte(ds2482, command);
} }
#undef nag_attr #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_MATCH
#undef DS2482_1W_ROM_SKIP #undef DS2482_1W_ROM_SKIP

View File

@ -27,25 +27,14 @@ Then, configure the HAL:
```c ```c
// (in DS2482_HAL.h) // (in DS2482_HAL.h)
#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_1W 100 //timeout in ms for 1-wire operations
#define DS2482_TIMEOUT_I2C 100 //timeout in ms for I2C operations #define DS2482_TIMEOUT_I2C 100 //timeout in ms for I2C operations
``` ```
For multiple DS2482 devices on one chip: To use the DS2482:
- Set `MULTIPLE_DEVICES` to `true`
- Call `ds2482_create()` with the I2C handle and address - Call `ds2482_create()` with the I2C handle and address
- Call `ds2482_init()` with the handle - Call `ds2482_init()` with the handle
- The HAL is ready - The HAL is ready
For just one DS2482:
- Set `DS2482_I2C_ADDR` to the I2C address
- Call `ds2482_init()` with the I2C handle
- The HAL is ready
Be sure to check the return values for error handling. Be sure to check the return values for error handling.