Firmware SDK
twr_onewire_ds2484.c
1 #include <twr_onewire_ds2484.h>
2 #include <twr_system.h>
3 #include <twr_timer.h>
4 #include <twr_i2c.h>
5 #include <twr_log.h>
6 
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);
16 
17 static const twr_onewire_driver_t _twr_onewire_ds2484_driver =
18 {
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
28 };
29 
31 {
32  twr_onewire_init(onewire, &_twr_onewire_ds2484_driver, twr_ds2484);
33 }
34 
35 const twr_onewire_driver_t *twr_onewire_det_driver(void)
36 {
37  return &_twr_onewire_ds2484_driver;
38 }
39 
40 static bool _twr_onewire_ds2484_init(void *ctx)
41 {
42  return twr_ds2484_init((twr_ds2484_t *) ctx, TWR_I2C_I2C0);
43 }
44 
45 static bool _twr_onewire_ds2484_enable(void *ctx)
46 {
48  return true;
49 }
50 
51 static bool _twr_onewire_ds2484_disable(void *ctx)
52 {
54  return true;
55 }
56 
57 static bool _twr_onewire_ds2484_reset(void *ctx)
58 {
59  return twr_ds2484_reset((twr_ds2484_t *) ctx);
60 }
61 
62 static void _twr_onewire_ds2484_write_bit(void *ctx, uint8_t bit)
63 {
64  (void) ctx;
65  (void) bit;
66  twr_log_warning("onewire_ds2484_write_bit not implemented");
67 }
68 
69 static uint8_t _twr_onewire_ds2484_read_bit(void *ctx)
70 {
71  uint8_t bit = 0;
72  twr_ds2484_read_bit((twr_ds2484_t *) ctx, &bit);
73  return bit;
74 }
75 
76 static void _twr_onewire_ds2484_write_byte(void *ctx, uint8_t byte)
77 {
78  twr_ds2484_write_byte((twr_ds2484_t *) ctx, byte);
79 }
80 
81 static uint8_t _twr_onewire_ds2484_read_byte(void *ctx)
82 {
83  uint8_t byte = 0;
84  twr_ds2484_read_byte((twr_ds2484_t *) ctx, &byte);
85  return byte;
86 }
87 
88 static bool _twr_onewire_ds2484_search_next(void *ctx, twr_onewire_t *onewire, uint64_t *device_number)
89 {
90  twr_ds2484_t *ds2484 = (twr_ds2484_t *) ctx;
91 
92  *device_number = 0;
93 
94  if (!twr_ds2484_reset(ds2484))
95  {
96  twr_log_warning("ds2484_reset");
97  return false;
98  }
99 
100  if (!twr_ds2484_write_byte(ds2484, 0xf0)) // search command
101  {
102  twr_log_warning("ds2484_write_byte(0xf0)");
103  return false;
104  }
105 
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;
110 
111  do
112  {
113  uint8_t status;
114  uint8_t id;
115  uint8_t comp_id;
116  uint8_t direction;
117 
118  if (id_bit_number < onewire->_last_discrepancy)
119  {
120  direction = onewire->_last_rom_no[rom_byte_number] & rom_byte_mask;
121  }
122  else
123  {
124  direction = id_bit_number == onewire->_last_discrepancy;
125  }
126 
127  if (!twr_ds2484_triplet(ds2484, direction))
128  {
129  twr_log_warning("ds2484_triplet");
130  return false;
131  }
132 
133  if (!twr_ds2484_busy_wait(ds2484))
134  {
135  twr_log_warning("ds2484_busy_wait");
136  return false;
137  }
138 
139  status = twr_ds2484_status_get(ds2484);
140 
141  id = status & TWR_DS2484_STATUS_SBR;
142  comp_id = status & TWR_DS2484_STATUS_TSB;
143  direction = status & TWR_DS2484_STATUS_DIR;
144 
145  if (id && comp_id)
146  {
147  twr_log_warning("id && comp_id");
148  return false;
149  }
150 
151  if (!id && !comp_id && !direction)
152  {
153  last_zero = id_bit_number;
154  }
155 
156  if (direction)
157  {
158  onewire->_last_rom_no[rom_byte_number] |= rom_byte_mask;
159  }
160  else
161  {
162  onewire->_last_rom_no[rom_byte_number] &= ~rom_byte_mask;
163  }
164 
165  id_bit_number++;
166  rom_byte_mask <<= 1;
167 
168  if (rom_byte_mask == 0)
169  {
170  rom_byte_number++;
171  rom_byte_mask = 1;
172  }
173 
174  } while (rom_byte_number < 8);
175 
176  onewire->_last_device_flag = last_zero == 0;
177 
178  onewire->_last_discrepancy = last_zero;
179 
180  if (twr_onewire_crc8(onewire->_last_rom_no, sizeof(onewire->_last_rom_no), 0) != 0)
181  {
182  return false;
183  }
184 
185  memcpy(device_number, onewire->_last_rom_no, sizeof(onewire->_last_rom_no));
186 
187  return true;
188 }
bool twr_ds2484_reset(twr_ds2484_t *self)
Reset the 1-Wire bus and return the presence of any device.
Definition: twr_ds2484.c:102
bool twr_ds2484_init(twr_ds2484_t *self, twr_i2c_channel_t i2c_channel)
Initialize DS2484.
Definition: twr_ds2484.c:40
void twr_ds2484_enable(twr_ds2484_t *self)
Enable DS2484.
Definition: twr_ds2484.c:59
bool twr_ds2484_busy_wait(twr_ds2484_t *self)
Wait until not busy.
Definition: twr_ds2484.c:125
void twr_ds2484_disable(twr_ds2484_t *self)
Disable DS2484.
Definition: twr_ds2484.c:90
struct twr_ds2484_t twr_ds2484_t
TMP112 instance.
Definition: twr_ds2484.h:23
@ TWR_I2C_I2C0
I2C channel I2C0.
Definition: twr_i2c.h:18
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
Definition: twr_onewire.h:14
bool twr_onewire_init(twr_onewire_t *self, const twr_onewire_driver_t *driver, void *driver_ctx)
Initialize 1-Wire.
Definition: twr_onewire.c:12
uint8_t twr_onewire_crc8(const void *buffer, size_t length, uint8_t crc)
Calculate 8-bit CRC.
Definition: twr_onewire.c:165
void twr_onewire_ds2484_init(twr_onewire_t *onewire, twr_ds2484_t *twr_ds2484)
Initialize 1-Wire.