Compare commits

..

No commits in common. "8cead7ac350ca24f4e701b75087c872f47228db7" and "1264fc0d5a7e9a1501cae81a0f1472ca3ff7344a" have entirely different histories.

4 changed files with 125 additions and 82 deletions

5
.gitignore vendored
View File

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

View File

@ -29,16 +29,35 @@ 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
HAL_StatusTypeDef ds2482_init(ds2482_t* const ds2482) { static I2C_HandleTypeDef* hi2c;
HAL_StatusTypeDef status = ds2482_reset(ds2482); #endif
#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(ds2482, DS2482_CONFIG_APU); status = ds2482_write_config(DEV_ARG DS2482_CONFIG_APU);
if (status != HAL_OK) { if (status != HAL_OK) {
return status; return status;
} }
@ -54,29 +73,29 @@ HAL_StatusTypeDef ds2482_init(ds2482_t* const ds2482) {
return HAL_OK; return HAL_OK;
} }
HAL_StatusTypeDef ds2482_reset(ds2482_t* const ds2482) { HAL_StatusTypeDef ds2482_reset(DEV_ARG_ONE_DEF) {
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(ds2482_t * const ds2482, uint8_t config) { HAL_StatusTypeDef ds2482_write_config(DEV_ARG_DEF 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(ds2482_t * const ds2482, uint8_t read_ptr) { HAL_StatusTypeDef ds2482_set_read_ptr(DEV_ARG_DEF 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(ds2482_t * const ds2482, bool* presence) { HAL_StatusTypeDef ds2482_1w_reset(DEV_ARG_DEF 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};
uint32_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W; uint16_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) {
@ -91,14 +110,12 @@ HAL_StatusTypeDef ds2482_1w_reset(ds2482_t * const ds2482, 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(ds2482_t * const ds2482, uint8_t byte) { HAL_StatusTypeDef ds2482_1w_write_byte(DEV_ARG_DEF 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) {
@ -106,7 +123,7 @@ HAL_StatusTypeDef ds2482_1w_write_byte(ds2482_t * const ds2482, uint8_t byte) {
} }
DS2482_Status status_reg = {.BUSY = 1}; DS2482_Status status_reg = {.BUSY = 1};
uint32_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W; uint16_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) {
@ -120,7 +137,7 @@ HAL_StatusTypeDef ds2482_1w_write_byte(ds2482_t * const ds2482, uint8_t byte) {
return HAL_OK; return HAL_OK;
} }
HAL_StatusTypeDef ds2482_1w_read_byte(ds2482_t * const ds2482, uint8_t* byte) { HAL_StatusTypeDef ds2482_1w_read_byte(DEV_ARG_DEF 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) {
@ -128,7 +145,7 @@ HAL_StatusTypeDef ds2482_1w_read_byte(ds2482_t * const ds2482, uint8_t* byte) {
} }
DS2482_Status status_reg = {.BUSY = 1}; DS2482_Status status_reg = {.BUSY = 1};
uint32_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W; uint16_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) {
@ -139,7 +156,7 @@ HAL_StatusTypeDef ds2482_1w_read_byte(ds2482_t * const ds2482, uint8_t* byte) {
} }
} while (status_reg.BUSY); } while (status_reg.BUSY);
status = ds2482_set_read_ptr(ds2482, DS2482_READ_DATA); status = ds2482_set_read_ptr(DEV_ARG DS2482_READ_DATA);
if (status != HAL_OK) { if (status != HAL_OK) {
return status; return status;
} }
@ -147,12 +164,12 @@ HAL_StatusTypeDef ds2482_1w_read_byte(ds2482_t * const ds2482, 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(ds2482_t * const ds2482, uint8_t dir) { HAL_StatusTypeDef ds2482_1w_triplet(DEV_ARG_DEF 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(ds2482_t * const ds2482, bool bit) { HAL_StatusTypeDef ds2482_1w_write_bit(DEV_ARG_DEF 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) {
@ -160,7 +177,7 @@ HAL_StatusTypeDef ds2482_1w_write_bit(ds2482_t * const ds2482, bool bit) {
} }
DS2482_Status status_reg = {.BUSY = 1}; DS2482_Status status_reg = {.BUSY = 1};
uint32_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W; uint16_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) {
@ -174,7 +191,7 @@ HAL_StatusTypeDef ds2482_1w_write_bit(ds2482_t * const ds2482, bool bit) {
return HAL_OK; return HAL_OK;
} }
HAL_StatusTypeDef ds2482_1w_read_bit(ds2482_t * const ds2482, bool* bit) { HAL_StatusTypeDef ds2482_1w_read_bit(DEV_ARG_DEF 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) {
@ -182,7 +199,7 @@ HAL_StatusTypeDef ds2482_1w_read_bit(ds2482_t * const ds2482, bool* bit) {
} }
DS2482_Status status_reg = {.BUSY = 1}; DS2482_Status status_reg = {.BUSY = 1};
uint32_t timeout = HAL_GetTick() + DS2482_TIMEOUT_1W; uint16_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) {
@ -251,30 +268,30 @@ static uint8_t crc8;
// Return true : device present // Return true : device present
// false : no device present // false : no device present
// //
static bool OWReset(ds2482_t* const ds2482) { static bool OWReset(DEV_ARG_ONE_DEF) {
bool presence; bool presence;
(void)ds2482_1w_reset(ds2482, &presence); (void)ds2482_1w_reset(DEV_ARG &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(ds2482_t * const ds2482, uint8_t byte_value) { (void)ds2482_1w_write_byte(ds2482, byte_value); } static inline void OWWriteByte(DEV_ARG_DEF uint8_t byte_value) { (void)ds2482_1w_write_byte(DEV_ARG 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(ds2482_t * const ds2482, uint8_t bit_value) { (void)ds2482_1w_write_bit(ds2482, bit_value); } static inline void OWWriteBit(DEV_ARG_DEF uint8_t bit_value) { (void)ds2482_1w_write_bit(DEV_ARG 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(ds2482_t* const ds2482) { static inline uint8_t OWReadBit(DEV_ARG_ONE_DEF) {
bool bit; bool bit;
(void)ds2482_1w_read_bit(ds2482, &bit); (void)ds2482_1w_read_bit(DEV_ARG &bit);
return bit; return bit;
} }
@ -309,7 +326,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(ds2482_t* const ds2482) { static bool OWSearch(DEV_ARG_ONE_DEF) {
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;
@ -326,7 +343,7 @@ static bool OWSearch(ds2482_t* const ds2482) {
// 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(ds2482)) { if (!OWReset(DEV_ARG_ONE)) {
// reset the search // reset the search
LastDiscrepancy = 0; LastDiscrepancy = 0;
LastDeviceFlag = false; LastDeviceFlag = false;
@ -335,13 +352,13 @@ static bool OWSearch(ds2482_t* const ds2482) {
} }
// issue the search command // issue the search command
OWWriteByte(ds2482, 0xF0); OWWriteByte(DEV_ARG 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(ds2482); id_bit = OWReadBit(DEV_ARG_ONE);
cmp_id_bit = OWReadBit(ds2482); cmp_id_bit = OWReadBit(DEV_ARG_ONE);
// 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))
@ -377,7 +394,7 @@ static bool OWSearch(ds2482_t* const ds2482) {
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(ds2482, search_direction); OWWriteBit(DEV_ARG 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
@ -422,13 +439,13 @@ static bool OWSearch(ds2482_t* const ds2482) {
// 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(ds2482_t* const ds2482) { static bool OWFirst(DEV_ARG_ONE_DEF) {
// reset the search state // reset the search state
LastDiscrepancy = 0; LastDiscrepancy = 0;
LastDeviceFlag = false; LastDeviceFlag = false;
LastFamilyDiscrepancy = 0; LastFamilyDiscrepancy = 0;
return OWSearch(ds2482); return OWSearch(DEV_ARG_ONE);
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
@ -436,10 +453,10 @@ static bool OWFirst(ds2482_t* const ds2482) {
// 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(ds2482_t* const ds2482) { return OWSearch(ds2482); } static inline bool OWNext(DEV_ARG_ONE_DEF) { return OWSearch(DEV_ARG_ONE); }
// 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(ds2482_t * const ds2482, uint64_t device, bool* present) { HAL_StatusTypeDef ds2482_1w_verify_device(DEV_ARG_DEF uint64_t device, bool* present) {
uint8_t rom_backup[8]; uint8_t rom_backup[8];
int i, rslt; int i, rslt;
@ -452,7 +469,7 @@ HAL_StatusTypeDef ds2482_1w_verify_device(ds2482_t * const ds2482, uint64_t devi
LastDiscrepancy = 64; LastDiscrepancy = 64;
LastDeviceFlag = false; LastDeviceFlag = false;
if (OWSearch(ds2482)) { if (OWSearch(DEV_ARG_ONE)) {
// 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++) {
@ -470,8 +487,8 @@ HAL_StatusTypeDef ds2482_1w_verify_device(ds2482_t * const ds2482, uint64_t devi
HAL_StatusTypeDef ds2482_1w_search(ds2482_t * const ds2482, uint16_t max_devices, uint64_t devices[static max_devices]) { HAL_StatusTypeDef ds2482_1w_search(DEV_ARG_DEF uint16_t max_devices, uint64_t devices[static max_devices]) {
OWFirst(ds2482); OWFirst(DEV_ARG_ONE);
uint16_t count = 0; uint16_t count = 0;
do { do {
uint64_t device = 0; uint64_t device = 0;
@ -479,7 +496,7 @@ HAL_StatusTypeDef ds2482_1w_search(ds2482_t * const ds2482, uint16_t max_devices
device |= (uint64_t)ROM_NO[i] << (i * 8); device |= (uint64_t)ROM_NO[i] << (i * 8);
} }
devices[count++] = device; devices[count++] = device;
} while (OWNext(ds2482) && count < max_devices); } while (OWNext(DEV_ARG_ONE) && count < max_devices);
return HAL_OK; return HAL_OK;
} }

View File

@ -3,9 +3,18 @@
#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
@ -16,6 +25,10 @@
#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");
@ -23,67 +36,74 @@ 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!")]]
typedef struct { #if MULTIPLE_DEVICES
I2C_HandleTypeDef* hi2c; typedef struct {
uint16_t address; I2C_HandleTypeDef* hi2c;
} ds2482_t; uint16_t address;
} ds2482_t;
#define ds2482_create(hi2c, address) ((constexpr ds2482_t){.hi2c = hi2c, .address = address}) static inline ds2482_t ds2482_create(I2C_HandleTypeDef* hi2c, uint16_t address) {
return (ds2482_t){.hi2c = hi2c, .address = address};
}
nag_attr HAL_StatusTypeDef ds2482_init(ds2482_t * const ds2482); #define DEV_ARG ds2482,
nag_attr HAL_StatusTypeDef ds2482_reset(ds2482_t * const ds2482); #define DEV_ARG_DEF const ds2482_t * const ds2482,
nag_attr HAL_StatusTypeDef ds2482_write_config(ds2482_t * const ds2482, uint8_t config); #define DEV_ARG_ONE ds2482
nag_attr HAL_StatusTypeDef ds2482_set_read_ptr(ds2482_t * const ds2482, uint8_t read_ptr); #define DEV_ARG_ONE_DEF const ds2482_t* const ds2482
nag_attr HAL_StatusTypeDef ds2482_1w_reset(ds2482_t * const ds2482, bool * const presence); nag_attr HAL_StatusTypeDef ds2482_init(const ds2482_t * const ds2482);
nag_attr HAL_StatusTypeDef ds2482_1w_write_byte(ds2482_t * const ds2482, uint8_t byte); #else
nag_attr HAL_StatusTypeDef ds2482_1w_read_byte(ds2482_t * const ds2482, uint8_t * const byte); #define DEV_ARG
nag_attr HAL_StatusTypeDef ds2482_1w_read_bit(ds2482_t * const ds2482, bool * const bit); #define DEV_ARG_DEF
nag_attr HAL_StatusTypeDef ds2482_1w_write_bit(ds2482_t * const ds2482, bool bit); #define DEV_ARG_ONE
nag_attr HAL_StatusTypeDef ds2482_1w_triplet(ds2482_t * const ds2482, uint8_t dir); #define DEV_ARG_ONE_DEF
nag_attr HAL_StatusTypeDef ds2482_init(I2C_HandleTypeDef* hi2c);
#endif
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_reset(DEV_ARG_ONE_DEF);
nag_attr HAL_StatusTypeDef ds2482_1w_verify_device(ds2482_t * const ds2482, uint64_t device, bool * const present); 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_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(ds2482_t * const ds2482, uint64_t device) { nag_attr static inline HAL_StatusTypeDef ds2482_1w_select_device(DEV_ARG_DEF uint64_t device) {
bool present; bool present;
HAL_StatusTypeDef status = ds2482_1w_reset(ds2482, &present); HAL_StatusTypeDef status = ds2482_1w_reset(DEV_ARG &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(ds2482, DS2482_1W_ROM_MATCH); status = ds2482_1w_write_byte(DEV_ARG 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(ds2482, (device >> (i * 8)) & 0xFF); status = ds2482_1w_write_byte(DEV_ARG (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(ds2482_t * const ds2482) { nag_attr static inline HAL_StatusTypeDef ds2482_1w_select_all(DEV_ARG_ONE_DEF) {
bool present; bool present;
HAL_StatusTypeDef status = ds2482_1w_reset(ds2482, &present); HAL_StatusTypeDef status = ds2482_1w_reset(DEV_ARG &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(ds2482, DS2482_1W_ROM_SKIP); return ds2482_1w_write_byte(DEV_ARG 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,14 +27,25 @@ 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
``` ```
To use the DS2482: For multiple DS2482 devices on one chip:
- 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.