1 #include <twr_ds2484.h>
7 #define _TWR_DS2484_I2C_ADDRESS 0x18
10 #define _TWR_DS2484_CMD_DRST (0xF0)
11 #define _TWR_DS2484_CMD_SRP (0xE1)
12 #define _TWR_DS2484_CMD_WCFG (0xD2)
13 #define _TWR_DS2484_CMD_CHSL (0xC3)
14 #define _TWR_DS2484_CMD_1WRS (0xB4)
15 #define _TWR_DS2484_CMD_1WSB (0x87)
16 #define _TWR_DS2484_CMD_1WWB (0xA5)
17 #define _TWR_DS2484_CMD_1WRB (0x96)
18 #define _TWR_DS2484_CMD_1WT (0x78)
21 #define _TWR_DS2484_REG_ST (0xF0)
22 #define _TWR_DS2484_REG_DATA (0xE1)
23 #define _TWR_DS2484_REG_CFG (0xC3)
28 #define _TWR_DS2484_CFG_APU (1<<0)
29 #define _TWR_DS2484_CFG_SPU (1<<2)
30 #define _TWR_DS2484_CFG_1WS (1<<3)
32 #define _TWR_DS2484_CFG_DEFAULT (_TWR_DS2484_CFG_APU)
34 static bool _twr_ds2484_i2c_read_byte(
twr_ds2484_t *
self, uint8_t *b);
35 static bool _twr_ds2484_i2c_write_byte(
twr_ds2484_t *
self,
const uint8_t b);
36 static bool _twr_ds2484_device_reset(
twr_ds2484_t *
self);
37 static bool _twr_ds2484_write_config(
twr_ds2484_t *
self,
const uint8_t cfg);
38 static bool _twr_ds2484_set_reed_pointer(
twr_ds2484_t *
self, uint8_t srp);
42 memset(
self, 0,
sizeof(*
self));
44 self->_i2c_channel = i2c_channel;
53 void twr_ds2484_set_slpz_handler(
twr_ds2484_t *
self,
bool (*handler)(
void *,
bool),
void *handler_ctx)
55 self->_set_slpz = handler;
56 self->_set_slpz_ctx = handler_ctx;
63 if (self->_set_slpz != NULL)
65 if (!self->_set_slpz(self->_set_slpz_ctx, 1))
71 if (!_twr_ds2484_device_reset(
self))
78 if (!_twr_ds2484_write_config(
self, _TWR_DS2484_CFG_DEFAULT))
94 if (self->_set_slpz != NULL)
96 self->_set_slpz(self->_set_slpz_ctx, 0);
109 if (!_twr_ds2484_i2c_write_byte(
self, _TWR_DS2484_CMD_1WRS))
114 self->_srp = _TWR_DS2484_REG_ST;
122 return (self->_status & TWR_DS2484_STATUS_PPD) != 0;
134 if (!_twr_ds2484_set_reed_pointer(
self, _TWR_DS2484_REG_ST))
141 if (!_twr_ds2484_i2c_read_byte(
self, &self->_status))
157 bool twr_ds2484_write_byte(
twr_ds2484_t *
self,
const uint8_t
byte)
169 self->_srp = _TWR_DS2484_REG_ST;
174 bool twr_ds2484_read_byte(
twr_ds2484_t *
self, uint8_t *
byte)
181 if (!_twr_ds2484_i2c_write_byte(
self, _TWR_DS2484_CMD_1WRB))
186 self->_srp = _TWR_DS2484_REG_ST;
193 if (!_twr_ds2484_set_reed_pointer(
self, _TWR_DS2484_REG_DATA))
198 if (!_twr_ds2484_i2c_read_byte(
self,
byte))
206 bool twr_ds2484_read_bit(
twr_ds2484_t *
self, uint8_t *bit)
213 if (!_twr_ds2484_i2c_write_byte(
self, _TWR_DS2484_CMD_1WSB))
218 self->_srp = _TWR_DS2484_REG_ST;
225 if (!_twr_ds2484_i2c_read_byte(
self, bit))
230 *bit = *bit & 0x80 ? 1 : 0;
235 bool twr_ds2484_triplet(
twr_ds2484_t *
self,
const uint8_t direction)
242 if (!
twr_i2c_memory_write_8b(self->_i2c_channel, _TWR_DS2484_I2C_ADDRESS, _TWR_DS2484_CMD_1WT, direction ? 0x80 : 0))
247 self->_srp = _TWR_DS2484_REG_ST;
259 return self->_status;
264 return _twr_ds2484_device_reset(
self);
267 static bool _twr_ds2484_i2c_write_byte(
twr_ds2484_t *
self,
const uint8_t b)
280 static bool _twr_ds2484_i2c_read_byte(
twr_ds2484_t *
self, uint8_t *b)
291 static bool _twr_ds2484_device_reset(
twr_ds2484_t *
self)
293 if (!_twr_ds2484_i2c_write_byte(
self, _TWR_DS2484_CMD_DRST))
298 self->_srp = _TWR_DS2484_REG_ST;
305 static bool _twr_ds2484_write_config(
twr_ds2484_t *
self,
const uint8_t cfg)
309 if (!
twr_i2c_memory_write_8b(self->_i2c_channel, _TWR_DS2484_I2C_ADDRESS, _TWR_DS2484_CMD_WCFG, ((~cfg << 4) | cfg)))
314 self->_srp = _TWR_DS2484_REG_CFG;
316 if (!_twr_ds2484_i2c_read_byte(
self, &res))
326 self->_spu_on = (cfg & _TWR_DS2484_CFG_SPU) ?
true :
false;
331 static bool _twr_ds2484_set_reed_pointer(
twr_ds2484_t *
self, uint8_t srp)
333 if (self->_srp != srp)
void twr_delay_us(uint16_t microseconds)
Delay us.
bool twr_ds2484_reset(twr_ds2484_t *self)
Reset the 1-Wire bus and return the presence of any device.
#define TWR_DS2484_STATUS_1WB
Callback events.
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.
void twr_i2c_init(twr_i2c_channel_t channel, twr_i2c_speed_t speed)
Initialize I2C channel.
bool twr_i2c_read(twr_i2c_channel_t channel, const twr_i2c_transfer_t *transfer)
Read from I2C channel.
bool twr_i2c_memory_write_8b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t data)
Memory write 1 byte to I2C channel.
bool twr_i2c_write(twr_i2c_channel_t channel, const twr_i2c_transfer_t *transfer)
Write to I2C channel.
twr_i2c_channel_t
I2C channels.
@ TWR_I2C_SPEED_400_KHZ
I2C communication speed is 400 kHz.
void void void void void twr_log_error(const char *format,...) __attribute__((format(printf
Log ERROR message (annotated in log as <E>)
void * buffer
Pointer to buffer which is being written or read.
uint8_t device_address
7-bit I2C device address
size_t length
Length of buffer which is being written or read.