Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_ds28e17.c
1 #include <bc_ds28e17.h>
2 #include <bc_tick.h>
3 #include <bc_log.h>
4 
5 static const bc_gpio_channel_t _bc_ds28e17_set_speed_lut[2] =
6 {
7  [BC_I2C_SPEED_100_KHZ] = 0x00,
8  [BC_I2C_SPEED_400_KHZ] = 0x01
9 };
10 
11 static bool _bc_ds28e17_write(bc_ds28e17_t *self, uint8_t *head, size_t head_length, void *buffer, size_t length);
12 static bool _bc_ds28e17_read(bc_ds28e17_t *self, uint8_t *head, size_t head_length, void *buffer, size_t length);
13 
14 void bc_ds28e17_init(bc_ds28e17_t *self, bc_gpio_channel_t channel, uint64_t device_number)
15 {
16  memset(self, 0, sizeof(*self));
17 
18  self->_device_number = device_number;
19 
20  self->_channel = channel;
21 
22  bc_onewire_init(self->_channel);
23 
25 }
26 
28 {
29  (void) self;
30 }
31 
33 {
34  return self->_device_number;
35 }
36 
38 {
39  bc_onewire_transaction_start(self->_channel);
40 
41  if (!bc_onewire_reset(self->_channel))
42  {
43  bc_onewire_transaction_stop(self->_channel);
44 
45  return false;
46  }
47 
48  bc_onewire_select(self->_channel, &self->_device_number);
49 
50  uint8_t buffer[2];
51 
52  buffer[0] = 0xD2;
53  buffer[1] = _bc_ds28e17_set_speed_lut[speed];
54 
55  bc_onewire_write(self->_channel, buffer, sizeof(buffer));
56 
57  bc_onewire_transaction_stop(self->_channel);
58 
59  return true;
60 }
61 
62 bool bc_ds28e17_write(bc_ds28e17_t *self, const bc_i2c_transfer_t *transfer)
63 {
64  uint8_t head[3];
65 
66  head[0] = 0x4B;
67  head[1] = transfer->device_address << 1;
68  head[2] = (uint8_t) transfer->length;
69 
70  return _bc_ds28e17_write(self, head, sizeof(head), transfer->buffer, transfer->length);
71 }
72 
73 bool bc_ds28e17_read(bc_ds28e17_t *self, const bc_i2c_transfer_t *transfer)
74 {
75  uint8_t head[3];
76 
77  head[0] = 0x87;
78  head[1] = (transfer->device_address << 1) | 0x01;
79  head[2] = (uint8_t) transfer->length;
80 
81  return _bc_ds28e17_read(self, head, sizeof(head), transfer->buffer, transfer->length);
82 }
83 
85 {
86  size_t head_length;
87  uint8_t head[5];
88 
89  head[0] = 0x4B;
90  head[1] = transfer->device_address << 1;
91 
92  if ((transfer->memory_address & BC_I2C_MEMORY_ADDRESS_16_BIT) != 0)
93  {
94  head_length = 5;
95  head[2] = (uint8_t) transfer->length + 2;
96  head[3] = transfer->memory_address >> 8;
97  head[4] = transfer->memory_address;
98  }
99  else
100  {
101  head_length = 4;
102  head[2] = (uint8_t) transfer->length + 1;
103  head[3] = (uint8_t) transfer->memory_address;
104  }
105 
106  return _bc_ds28e17_write(self, head, head_length, transfer->buffer, transfer->length);
107 }
108 
110 {
111  size_t head_length;
112  uint8_t head[6];
113 
114  head[0] = 0x2D;
115  head[1] = transfer->device_address << 1;
116 
117  if ((transfer->memory_address & BC_I2C_MEMORY_ADDRESS_16_BIT) != 0)
118  {
119  head_length = 6;
120  head[2] = 2;
121  head[3] = transfer->memory_address >> 8;
122  head[4] = transfer->memory_address;
123  head[5] = transfer->length;
124  }
125  else
126  {
127  head_length = 5;
128  head[2] = 1;
129  head[3] = transfer->memory_address;
130  head[4] = transfer->length;
131  }
132 
133  return _bc_ds28e17_read(self, head, head_length, transfer->buffer, transfer->length);
134 }
135 
136 static bool _bc_ds28e17_write(bc_ds28e17_t *self, uint8_t *head, size_t head_length, void *buffer, size_t length)
137 {
138  bc_onewire_transaction_start(self->_channel);
139 
140  bc_onewire_reset(self->_channel);
141 
142  if (!bc_onewire_reset(self->_channel))
143  {
144  bc_onewire_transaction_stop(self->_channel);
145 
146  return false;
147  }
148 
149  uint16_t crc16 = bc_onewire_crc16(head, head_length, 0x00);
150 
151  crc16 = bc_onewire_crc16(buffer, length, crc16);
152 
153  bc_onewire_select(self->_channel, &self->_device_number);
154 
155  bc_onewire_write(self->_channel, head, head_length);
156 
157  bc_onewire_write(self->_channel, buffer, length);
158 
159  crc16 = ~crc16;
160 
161  bc_onewire_write(self->_channel, &crc16, sizeof(crc16));
162 
163  bc_tick_t timeout = bc_tick_get() + 50;
164 
165  while (bc_onewire_read_bit(self->_channel) != 0)
166  {
167  if (timeout < bc_tick_get())
168  {
169  bc_onewire_transaction_stop(self->_channel);
170 
171  return false;
172  }
173 
174  continue;
175  }
176 
177  uint8_t status = bc_onewire_read_8b(self->_channel);
178 
179  uint8_t write_status = bc_onewire_read_8b(self->_channel);
180 
181  bc_onewire_transaction_stop(self->_channel);
182 
183  return (status == 0x00) && (write_status == 0x00);
184 }
185 
186 static bool _bc_ds28e17_read(bc_ds28e17_t *self, uint8_t *head, size_t head_length, void *buffer, size_t length)
187 {
188  bc_onewire_transaction_start(self->_channel);
189 
190  bc_onewire_reset(self->_channel);
191 
192  if (!bc_onewire_reset(self->_channel))
193  {
194  bc_onewire_transaction_stop(self->_channel);
195 
196  return false;
197  }
198 
199  uint16_t crc16 = bc_onewire_crc16(head, head_length, 0x00);
200 
201  bc_onewire_select(self->_channel, &self->_device_number);
202 
203  bc_onewire_write(self->_channel, head, head_length);
204 
205  crc16 = ~crc16;
206 
207  bc_onewire_write(self->_channel, &crc16, sizeof(crc16));
208 
209  bc_tick_t timeout = bc_tick_get() + 50;
210 
211  while (bc_onewire_read_bit(self->_channel) != 0)
212  {
213  if (timeout < bc_tick_get())
214  {
215  bc_onewire_transaction_stop(self->_channel);
216 
217  return false;
218  }
219 
220  continue;
221  }
222 
223  uint8_t status = bc_onewire_read_8b(self->_channel);
224 
225  uint8_t write_status = head[0] == 0x87 ? 0 : bc_onewire_read_8b(self->_channel);
226 
227  if ((status != 0x00) || (write_status != 0x00))
228  {
229  bc_onewire_transaction_stop(self->_channel);
230 
231  return false;
232  }
233 
234  bc_onewire_read(self->_channel, buffer, length);
235 
236  bc_onewire_transaction_stop(self->_channel);
237 
238  return true;
239 }
void * buffer
Pointer to buffer which is being written or read.
Definition: bc_i2c.h:66
I2C transfer parameters.
Definition: bc_i2c.h:42
uint64_t bc_tick_t
Timestamp data type.
Definition: bc_tick.h:16
I2C communication speed is 400 kHz.
Definition: bc_i2c.h:36
void bc_ds28e17_deinit(bc_ds28e17_t *self)
Deinitialize DS28E17.
Definition: bc_ds28e17.c:27
bool bc_ds28e17_set_speed(bc_ds28e17_t *self, bc_i2c_speed_t speed)
Set I2C speed.
Definition: bc_ds28e17.c:37
int bc_onewire_read_bit(bc_gpio_channel_t channel)
Select device.
Definition: bc_onewire.c:174
uint8_t device_address
7-bit I2C device address
Definition: bc_i2c.h:45
bool bc_ds28e17_read(bc_ds28e17_t *self, const bc_i2c_transfer_t *transfer)
Read from I2C.
Definition: bc_ds28e17.c:73
bool bc_onewire_transaction_stop(bc_gpio_channel_t channel)
Stop transaction.
Definition: bc_onewire.c:67
uint64_t bc_ds28e17_get_device_number(bc_ds28e17_t *self)
Get Device number.
Definition: bc_ds28e17.c:32
void bc_onewire_auto_ds28e17_sleep_mode(bool on)
Enable call sleep mode for all ds28e17 after transaction.
Definition: bc_onewire.c:545
I2C communication speed is 100 kHz.
Definition: bc_i2c.h:33
bc_tick_t bc_tick_get(void)
Get absolute timestamp since start of program.
Definition: bc_tick.c:7
void bc_ds28e17_init(bc_ds28e17_t *self, bc_gpio_channel_t channel, uint64_t device_number)
Initialize DS28E17.
Definition: bc_ds28e17.c:14
void * buffer
Pointer to buffer which is being written or read.
Definition: bc_i2c.h:48
uint32_t memory_address
8-bit I2C memory address (it can be extended to 16-bit format if OR-ed with BC_I2C_MEMORY_ADDRESS_16_...
Definition: bc_i2c.h:63
uint8_t device_address
7-bit I2C device address
Definition: bc_i2c.h:60
DS28E17 instance.
Definition: bc_ds28e17.h:14
bool bc_onewire_reset(bc_gpio_channel_t channel)
Reset the 1-Wire bus and return the presence of any device.
Definition: bc_onewire.c:91
void bc_onewire_init(bc_gpio_channel_t channel)
Initialize 1-Wire.
Definition: bc_onewire.c:35
I2C memory transfer parameters.
Definition: bc_i2c.h:57
void bc_onewire_select(bc_gpio_channel_t channel, uint64_t *device_number)
Select device.
Definition: bc_onewire.c:104
bool bc_onewire_transaction_start(bc_gpio_channel_t channel)
Start transaction, enable pll and run timer.
Definition: bc_onewire.c:51
size_t length
Length of buffer which is being written or read.
Definition: bc_i2c.h:51
uint16_t bc_onewire_crc16(const void *buffer, size_t length, uint16_t crc)
Calculate 16-bit CRC, polynomial 0x8005.
Definition: bc_onewire.c:224
bool bc_ds28e17_write(bc_ds28e17_t *self, const bc_i2c_transfer_t *transfer)
Write to I2C.
Definition: bc_ds28e17.c:62
bc_gpio_channel_t
GPIO channels.
Definition: bc_gpio.h:12
void bc_onewire_write(bc_gpio_channel_t channel, const void *buffer, size_t length)
Select device.
Definition: bc_onewire.c:132
bc_i2c_speed_t
I2C communication speed.
Definition: bc_i2c.h:30
bool bc_ds28e17_memory_write(bc_ds28e17_t *self, const bc_i2c_memory_transfer_t *transfer)
Memory write to I2C.
Definition: bc_ds28e17.c:84
size_t length
Length of buffer which is being written or read.
Definition: bc_i2c.h:69
void bc_onewire_read(bc_gpio_channel_t channel, void *buffer, size_t length)
Select device.
Definition: bc_onewire.c:142
#define BC_I2C_MEMORY_ADDRESS_16_BIT
This flag extends I2C memory transfer address from 8-bit to 16-bit.
Definition: bc_i2c.h:11
uint8_t bc_onewire_read_8b(bc_gpio_channel_t channel)
Select device.
Definition: bc_onewire.c:159
bool bc_ds28e17_memory_read(bc_ds28e17_t *self, const bc_i2c_memory_transfer_t *transfer)
Memory read from I2C.
Definition: bc_ds28e17.c:109