1 #include <twr_onewire_ds2484.h>
2 #include <twr_system.h>
7 static bool _twr_onewire_ds2484_init(
void *ctx);
8 static bool _twr_onewire_ds2484_enable(
void *ctx);
9 static bool _twr_onewire_ds2484_disable(
void *ctx);
10 static bool _twr_onewire_ds2484_reset(
void *ctx);
11 static void _twr_onewire_ds2484_write_bit(
void *ctx, uint8_t bit);
12 static uint8_t _twr_onewire_ds2484_read_bit(
void *ctx);
13 static void _twr_onewire_ds2484_write_byte(
void *ctx, uint8_t
byte);
14 static uint8_t _twr_onewire_ds2484_read_byte(
void *ctx);
15 static bool _twr_onewire_ds2484_search_next(
void *ctx,
twr_onewire_t *onewire, uint64_t *device_number);
17 static const twr_onewire_driver_t _twr_onewire_ds2484_driver =
19 .init = _twr_onewire_ds2484_init,
20 .enable = _twr_onewire_ds2484_enable,
21 .disable = _twr_onewire_ds2484_disable,
22 .reset = _twr_onewire_ds2484_reset,
23 .write_bit = _twr_onewire_ds2484_write_bit,
24 .read_bit = _twr_onewire_ds2484_read_bit,
25 .write_byte = _twr_onewire_ds2484_write_byte,
26 .read_byte = _twr_onewire_ds2484_read_byte,
27 .search_next = _twr_onewire_ds2484_search_next
35 const twr_onewire_driver_t *twr_onewire_det_driver(
void)
37 return &_twr_onewire_ds2484_driver;
40 static bool _twr_onewire_ds2484_init(
void *ctx)
45 static bool _twr_onewire_ds2484_enable(
void *ctx)
51 static bool _twr_onewire_ds2484_disable(
void *ctx)
57 static bool _twr_onewire_ds2484_reset(
void *ctx)
62 static void _twr_onewire_ds2484_write_bit(
void *ctx, uint8_t bit)
69 static uint8_t _twr_onewire_ds2484_read_bit(
void *ctx)
76 static void _twr_onewire_ds2484_write_byte(
void *ctx, uint8_t
byte)
81 static uint8_t _twr_onewire_ds2484_read_byte(
void *ctx)
88 static bool _twr_onewire_ds2484_search_next(
void *ctx,
twr_onewire_t *onewire, uint64_t *device_number)
100 if (!twr_ds2484_write_byte(ds2484, 0xf0))
106 uint8_t id_bit_number = 1;
107 uint8_t rom_byte_number = 0;
108 uint8_t rom_byte_mask = 1;
109 uint8_t last_zero = 0;
118 if (id_bit_number < onewire->_last_discrepancy)
120 direction = onewire->_last_rom_no[rom_byte_number] & rom_byte_mask;
124 direction = id_bit_number == onewire->_last_discrepancy;
127 if (!twr_ds2484_triplet(ds2484, direction))
139 status = twr_ds2484_status_get(ds2484);
141 id = status & TWR_DS2484_STATUS_SBR;
142 comp_id = status & TWR_DS2484_STATUS_TSB;
143 direction = status & TWR_DS2484_STATUS_DIR;
151 if (!
id && !comp_id && !direction)
153 last_zero = id_bit_number;
158 onewire->_last_rom_no[rom_byte_number] |= rom_byte_mask;
162 onewire->_last_rom_no[rom_byte_number] &= ~rom_byte_mask;
168 if (rom_byte_mask == 0)
174 }
while (rom_byte_number < 8);
176 onewire->_last_device_flag = last_zero == 0;
178 onewire->_last_discrepancy = last_zero;
180 if (
twr_onewire_crc8(onewire->_last_rom_no,
sizeof(onewire->_last_rom_no), 0) != 0)
185 memcpy(device_number, onewire->_last_rom_no,
sizeof(onewire->_last_rom_no));
bool twr_ds2484_reset(twr_ds2484_t *self)
Reset the 1-Wire bus and return the presence of any device.
bool twr_ds2484_init(twr_ds2484_t *self, twr_i2c_channel_t i2c_channel)
Initialize DS2484.
void twr_ds2484_enable(twr_ds2484_t *self)
Enable DS2484.
bool twr_ds2484_busy_wait(twr_ds2484_t *self)
Wait until not busy.
void twr_ds2484_disable(twr_ds2484_t *self)
Disable DS2484.
struct twr_ds2484_t twr_ds2484_t
TMP112 instance.
@ TWR_I2C_I2C0
I2C channel I2C0.
void void void void twr_log_warning(const char *format,...) __attribute__((format(printf
Log WARNING message (annotated in log as <W>)
struct twr_onewire_t twr_onewire_t
1-Wire instance
bool twr_onewire_init(twr_onewire_t *self, const twr_onewire_driver_t *driver, void *driver_ctx)
Initialize 1-Wire.
uint8_t twr_onewire_crc8(const void *buffer, size_t length, uint8_t crc)
Calculate 8-bit CRC.
void twr_onewire_ds2484_init(twr_onewire_t *onewire, twr_ds2484_t *twr_ds2484)
Initialize 1-Wire.