1 #include <twr_onewire.h>
7 static void _twr_onewire_search_target_setup(
twr_onewire_t *
self, uint8_t family_code);
8 static int _twr_onewire_search_devices(
twr_onewire_t *
self, uint64_t *device_list,
size_t device_list_size);
10 #define _TWR_ONEWIRE_CHECK_CALLBACK(X) if (X == NULL) twr_error(TWR_ERROR_CALLBACK)
14 memset(
self, 0,
sizeof(*
self));
16 _TWR_ONEWIRE_CHECK_CALLBACK(driver->
init);
17 _TWR_ONEWIRE_CHECK_CALLBACK(driver->enable);
18 _TWR_ONEWIRE_CHECK_CALLBACK(driver->disable);
19 _TWR_ONEWIRE_CHECK_CALLBACK(driver->reset);
20 _TWR_ONEWIRE_CHECK_CALLBACK(driver->write_bit);
21 _TWR_ONEWIRE_CHECK_CALLBACK(driver->read_bit);
22 _TWR_ONEWIRE_CHECK_CALLBACK(driver->write_byte);
23 _TWR_ONEWIRE_CHECK_CALLBACK(driver->read_byte);
25 self->_driver = driver;
26 self->_driver_ctx = driver_ctx;
28 return self->_driver->
init(self->_driver_ctx);
33 if (self->_lock_count != 0)
38 _twr_onewire_lock(
self);
45 _twr_onewire_unlock(
self);
52 return self->_lock_count != 0;
59 _twr_onewire_lock(
self);
61 state =
self->_driver->reset(self->_driver_ctx);
63 _twr_onewire_unlock(
self);
70 _twr_onewire_lock(
self);
72 if (*device_number == TWR_ONEWIRE_DEVICE_NUMBER_SKIP_ROM)
74 self->_driver->write_byte(self->_driver_ctx, 0xCC);
78 self->_driver->write_byte(self->_driver_ctx, 0x55);
80 for (
size_t i = 0; i <
sizeof(uint64_t); i++)
82 self->_driver->write_byte(self->_driver_ctx, ((uint8_t *) device_number)[i]);
86 _twr_onewire_unlock(
self);
91 _twr_onewire_lock(
self);
92 self->_driver->write_byte(self->_driver_ctx, 0xCC);
93 _twr_onewire_unlock(
self);
98 _twr_onewire_lock(
self);
99 for (
size_t i = 0; i < length; i++)
101 self->_driver->write_byte(self->_driver_ctx, ((uint8_t *) buffer)[i]);
103 _twr_onewire_unlock(
self);
108 _twr_onewire_lock(
self);
109 for (
size_t i = 0; i < length; i++)
111 ((uint8_t *) buffer)[i] =
self->_driver->read_byte(self->_driver_ctx);
113 _twr_onewire_unlock(
self);
118 _twr_onewire_lock(
self);
119 self->_driver->write_byte(self->_driver_ctx,
byte);
120 _twr_onewire_unlock(
self);
125 _twr_onewire_lock(
self);
126 uint8_t data =
self->_driver->read_byte(self->_driver_ctx);
127 _twr_onewire_unlock(
self);
133 _twr_onewire_lock(
self);
134 self->_driver->write_bit(self->_driver_ctx, bit & 0x01);
135 _twr_onewire_unlock(
self);
140 _twr_onewire_lock(
self);
141 int bit =
self->_driver->read_bit(self->_driver_ctx);
142 _twr_onewire_unlock(
self);
148 _twr_onewire_search_reset(
self);
150 return _twr_onewire_search_devices(
self, device_list, device_list_size);
155 _twr_onewire_search_target_setup(
self, family_code);
157 return _twr_onewire_search_devices(
self, device_list, device_list_size);
162 self->_auto_ds28e17_sleep_mode = on;
167 uint8_t *_buffer = (uint8_t *) buffer;
176 if ((crc ^ inbyte) & 0x01)
194 static const uint8_t oddparity[16] =
195 { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
198 for (i = 0; i < length; i++)
200 uint16_t cdata = ((uint8_t *) buffer)[i];
201 cdata = (cdata ^ crc) & 0xff;
204 if (oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4]) crc ^= 0xC001;
216 if (family_code != 0)
218 _twr_onewire_search_target_setup(
self, family_code);
222 _twr_onewire_search_reset(
self);
228 if (self->_last_device_flag)
233 _twr_onewire_lock(
self);
234 bool search_result =
self->_driver->search_next(self->_driver_ctx,
self, device_number);
235 _twr_onewire_unlock(
self);
236 return search_result;
241 if ((self->_lock_count)++ == 0)
243 self->_driver->enable(self->_driver_ctx);
249 if (self->_lock_count < 1) twr_error(TWR_ERROR_ERROR_UNLOCK);
251 if (self->_lock_count == 1)
253 if (self->_auto_ds28e17_sleep_mode)
255 if (self->_driver->reset(self->_driver_ctx))
257 self->_driver->write_byte(self->_driver_ctx, 0xcc);
258 self->_driver->write_byte(self->_driver_ctx, 0x1e);
262 self->_driver->disable(self->_driver_ctx);
270 self->_last_discrepancy = 0;
271 self->_last_device_flag =
false;
272 self->_last_family_discrepancy = 0;
275 static void _twr_onewire_search_target_setup(
twr_onewire_t *
self, uint8_t family_code)
277 memset(self->_last_rom_no, 0,
sizeof(self->_last_rom_no));
278 self->_last_rom_no[0] = family_code;
279 self->_last_discrepancy = 64;
280 self->_last_family_discrepancy = 0;
281 self->_last_device_flag =
false;
284 static int _twr_onewire_search_devices(
twr_onewire_t *
self, uint64_t *device_list,
size_t device_list_size)
287 int max_devices = device_list_size /
sizeof(uint64_t);
void twr_onewire_write(twr_onewire_t *self, const void *buffer, size_t length)
Select device.
void twr_onewire_write_byte(twr_onewire_t *self, uint8_t byte)
Select device.
void twr_onewire_search_start(twr_onewire_t *self, uint8_t family_code)
Start of manual search, see also twr_onewire_search_next.
struct twr_onewire_t twr_onewire_t
1-Wire instance
void twr_onewire_skip_rom(twr_onewire_t *self)
Skip ROM.
bool twr_onewire_is_transaction(twr_onewire_t *self)
Is transaction run.
bool twr_onewire_transaction_start(twr_onewire_t *self)
Start transaction, enable pll and run timer.
bool twr_onewire_reset(twr_onewire_t *self)
Reset the 1-Wire bus and return the presence of any device.
int twr_onewire_search_all(twr_onewire_t *self, uint64_t *device_list, size_t device_list_size)
Search for all devices on 1-Wire.
bool twr_onewire_init(twr_onewire_t *self, const twr_onewire_driver_t *driver, void *driver_ctx)
Initialize 1-Wire.
int twr_onewire_search_family(twr_onewire_t *self, uint8_t family_code, uint64_t *device_list, size_t device_list_size)
Search for all devices on 1-Wire with family code.
void twr_onewire_read(twr_onewire_t *self, void *buffer, size_t length)
Select device.
uint8_t twr_onewire_crc8(const void *buffer, size_t length, uint8_t crc)
Calculate 8-bit CRC.
void twr_onewire_select(twr_onewire_t *self, uint64_t *device_number)
Select device.
uint8_t twr_onewire_read_byte(twr_onewire_t *self)
Select device.
uint16_t twr_onewire_crc16(const void *buffer, size_t length, uint16_t crc)
Calculate 16-bit CRC, polynomial 0x8005.
int twr_onewire_read_bit(twr_onewire_t *self)
Select device.
bool twr_onewire_transaction_stop(twr_onewire_t *self)
Stop transaction.
void twr_onewire_write_bit(twr_onewire_t *self, int bit)
Select device.
void twr_onewire_auto_ds28e17_sleep_mode(twr_onewire_t *self, bool on)
Enable call sleep mode for all ds28e17 after transaction.
bool twr_onewire_search_next(twr_onewire_t *self, uint64_t *device_number)
Manual search of next device.
bool(* init)(void)
Callback for initialization.