-
Notifications
You must be signed in to change notification settings - Fork 19
/
DS2431.cpp
339 lines (284 loc) · 8.35 KB
/
DS2431.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright (c) 2022 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
* see accompanying file LICENSE for details.
*
* This software is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the License for more information.
*/
/**
* DS2431 EEPROM usage example (Pico SDK).
*
* Required configuration:
* - @c CONFIG_SEARCH_ENABLED,
* - @c CONFIG_CRC16_ENABLED,
* - @c CONFIG_MAX_SEARCH_FILTERS >= 1,
* - @c CONFIG_OVERDRIVE_ENABLED if @c USE_OD_MODE is configured.
*/
#include <stdio.h>
#include <string.h>
#include "pico/stdio.h"
#include "OneWireNg_CurrentPlatform.h"
#include "platform/Platform_Delay.h"
/*
* 1-wire bus pin number.
*/
#ifndef OW_PIN
# define OW_PIN 13
#endif
/*
* If defined: use overdrive mode.
*/
//#define USE_OD_MODE
/*
* If defined: enable write demo.
*/
//#define WRITE_DEMO
/* DS2431 family code */
#define DS2431 0x2D
/* memory function commands */
#define CMD_WRITE_SCRATCHPAD 0x0F
#define CMD_COPY_SCRATCHPAD 0x55
#define CMD_READ_SCRATCHPAD 0xAA
#define CMD_READ_MEMORY 0xF0
/* EEPROM row size */
#define DS2431_ROW_SIZE 8
/* EEPROM page size */
#define DS2431_PAGE_SIZE (4 * DS2431_ROW_SIZE)
/* DS2431 memory size */
#define DS2431_MEM_SIZE (18 * DS2431_ROW_SIZE)
#if !CONFIG_SEARCH_ENABLED
# error "CONFIG_SEARCH_ENABLED is required"
#endif
#if !CONFIG_CRC16_ENABLED
# error "CONFIG_CRC16_ENABLED is required"
#endif
#if (CONFIG_MAX_SEARCH_FILTERS < 1)
# error "CONFIG_MAX_SEARCH_FILTERS >= 1 is required"
#endif
#if defined(USE_OD_MODE) && !CONFIG_OVERDRIVE_ENABLED
# error "CONFIG_OVERDRIVE_ENABLED is required if USE_OD_MODE is configured"
#endif
static OneWireNg *ow = NULL;
static void printId(const OneWireNg::Id& id)
{
for (size_t i = 0; i < sizeof(OneWireNg::Id); i++)
printf("%s%02X", (!i ? "" : ":"), id[i]);
printf("\n");
}
/**
* Prints device EEPROM memory on serial. The memory is ready via READ_MEMORY
* (0xF0) command which doesn't incorporate CRC protection. Therefore usage of
* the function is problematic on transmission error vulnerable environments
* (including OD mode).
*
* If @c id is NULL the routine resumes communication with lastly addressed
* device.
*/
static void printMem(const OneWireNg::Id *id)
{
uint8_t cmd[DS2431_MEM_SIZE + 3];
cmd[0] = CMD_READ_MEMORY;
/* start reading from 0x0000 */
cmd[1] = 0x00; /* TA1 (LSB) */
cmd[2] = 0x00; /* TA2 (MSB) */
/* read memory will be placed here */
uint8_t *mem = &cmd[3];
memset(mem, 0xff, DS2431_MEM_SIZE);
if (id) {
ow->addressSingle(*id);
} else {
ow->resume();
}
ow->touchBytes(cmd, DS2431_MEM_SIZE + 3);
for (int i = 0; i < DS2431_MEM_SIZE; i++)
{
if (!(i % DS2431_ROW_SIZE)) {
printf("%02X ", (uint8_t)i);
}
printf("%02X", mem[i]);
if (((i+1) % DS2431_ROW_SIZE) != 0) {
printf(":");
} else {
switch (i/DS2431_ROW_SIZE)
{
case 0:
printf(" Data Memory Page 0\n");
break;
case 4:
printf(" Data Memory Page 1\n");
break;
case 8:
printf(" Data Memory Page 2\n");
break;
case 12:
printf(" Data Memory Page 3\n");
break;
case 16:
printf(" Control Bytes: PCB0:PCB1:PCB2:PCB3:CPB:FACT:USR1:USR2\n");
break;
case 17:
printf(" Reserved\n");
break;
default:
printf("\n");
break;
}
}
}
}
#ifdef WRITE_DEMO
/**
* Write a single EEPROM row passed in @c rowData. Row address (0-17) passed
* by @c rowAddr.
*
* If @c checkDataIntegr is @c true scratchpad data are verified against
* the data being set (@c rowData) if both are the same. This parameter shall
* be set to @c false for EPROM mode (logical AND performed by write).
*
* If @c id is NULL the routine resumes communication with lastly addressed
* device.
*
* On success the routine returns @c true.
*/
static bool writeRow(const OneWireNg::Id *id,
unsigned rowAddr, const uint8_t rowData[DS2431_ROW_SIZE],
bool checkDataIntegr)
{
if (rowAddr > 17) return false;
uint8_t cmd[DS2431_ROW_SIZE + 6];
uint8_t ta1 = (rowAddr * DS2431_ROW_SIZE); /* TA1 (LSB) */
uint8_t ta2 = 0x00; /* TA2 (MSB) */
/* STEP 1: write row data into scratchpad
*/
cmd[0] = CMD_WRITE_SCRATCHPAD;
cmd[1] = ta1;
cmd[2] = ta2;
memcpy(&cmd[3], rowData, DS2431_ROW_SIZE);
/* inverted CRC-16 will be placed here */
uint8_t *crc16 = &cmd[DS2431_ROW_SIZE + 3];
crc16[0] = 0xff;
crc16[1] = 0xff;
if (id) {
ow->addressSingle(*id);
} else {
ow->resume();
}
ow->touchBytes(cmd, DS2431_ROW_SIZE + 5);
if (ow->checkInvCrc16(cmd, DS2431_ROW_SIZE + 3, ow->getLSB_u16(crc16)) !=
OneWireNg::EC_SUCCESS)
{
printf("WRITE SCRATCHPAD: CRC error\n");
return false;
}
/* STEP 2: verify scratchpad integrity
*/
cmd[0] = CMD_READ_SCRATCHPAD;
/* TA1, TA2, E/S, row data, CRC16 will be placed here */
memset(&cmd[1], 0xff, DS2431_ROW_SIZE + 5);
ow->resume();
ow->touchBytes(cmd, DS2431_ROW_SIZE + 6);
crc16 = &cmd[DS2431_ROW_SIZE + 4];
if (ow->checkInvCrc16(cmd, DS2431_ROW_SIZE + 4, ow->getLSB_u16(crc16)) !=
OneWireNg::EC_SUCCESS)
{
printf("READ SCRATCHPAD: CRC error\n");
return false;
}
/* PF and AA flags must be cleared TA1, TA2 must match */
uint8_t es = cmd[3];
if (ta1 != cmd[1] || ta2 != cmd[2] || (es & 0x20) || (es & 0x80))
{
printf("READ SCRATCHPAD: command status error\n");
return false;
}
/* check if data was set in scratchpad */
if (checkDataIntegr && memcmp(&cmd[4], rowData, DS2431_ROW_SIZE))
{
printf("READ SCRATCHPAD: row is write protected\n");
return false;
}
/* STEP 3: copy scratchpad into EEPROM
*/
cmd[0] = CMD_COPY_SCRATCHPAD;
/* TA1, TA2, E/S already set as required */
ow->resume();
ow->touchBytes(cmd, 4);
/* wait for completion (10 ms) */
delayMs(10);
return true;
}
/**
* @ref writeRow() analogous to write EEPROM page. Page specified by
* @c pageAddr (0-3).
*/
static bool writePage(const OneWireNg::Id *id,
unsigned pageAddr, const uint8_t pageData[DS2431_PAGE_SIZE],
bool checkDataIntegr)
{
if (pageAddr > 3) return false;
int i;
uint8_t row = pageAddr * (DS2431_PAGE_SIZE / DS2431_ROW_SIZE);
for (i = 0; i < (DS2431_PAGE_SIZE / DS2431_ROW_SIZE); i++, row++) {
if (!writeRow((!i ? id : NULL),
row, &pageData[i * DS2431_ROW_SIZE], checkDataIntegr)) break;
}
if (i < (DS2431_PAGE_SIZE / DS2431_ROW_SIZE))
{
printf("Error writing row %u in page %u\n", (unsigned)row, pageAddr);
return false;
}
return true;
}
#endif /* WRITE_DEMO */
void setup()
{
OneWireNg::Id id;
/* id of a DS2431 device for write demo;
if not set 1st available DS2431 device will be chosen */
OneWireNg::Id dev = {};
ow = new OneWireNg_CurrentPlatform(OW_PIN, false);
stdio_init_all();
#ifdef USE_OD_MODE
ow->overdriveAll();
#endif
/* search for DS2431 devices connected to the bus
*/
ow->searchFilterAdd(DS2431);
printf("Connected DS2431 devices:\n");
ow->searchReset();
while (ow->search(id) == OneWireNg::EC_MORE) {
if (dev[0] != DS2431)
memcpy(&dev, &id[0], sizeof(OneWireNg::Id));
printId(id);
printMem(NULL);
printf("----------\n");
}
#ifdef WRITE_DEMO
/* if no DS2431 found finish the demo */
if (dev[0] != DS2431) return;
uint8_t pageData[DS2431_PAGE_SIZE] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
};
if (writePage(&dev, 1, pageData, true)) {
printf("Page successfully written to EEPROM\n");
}
#endif
}
void loop()
{
delayMs(1000);
}
int main()
{
setup();
for (;;)
loop();
return 0;
}