-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathds2450.c
137 lines (116 loc) · 2.57 KB
/
ds2450.c
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
#include "ows.h"
#include "ow_crc16.h"
#include <avr/io.h>
#include <string.h>
struct {
uint16_t conversion_readout[4]; // page 0
struct {
unsigned int rc:4; // number of bits, 0000 = 16
unsigned int : 2;
unsigned int oc:1; // output value
unsigned int oe:1; // output enable
unsigned int ir:1; // 0 - half max voltage, 1 - full max voltage
unsigned int : 1;
unsigned int ael:1; // alarm enable low
unsigned int aeh:1; // alarm enable high
unsigned int afl:1; // alarm flag low
unsigned int afh:1; // alarm flag high
unsigned int : 1;
unsigned int por:1; // just powered on
} control_status[4]; // page 1
struct {
uint8_t low;
uint8_t high;
} alarm_settings[4]; // page 2
uint8_t calibration[8]; // page3
} memory;
void init_memory()
{
memset(&memory, 0, sizeof(memory));
uint16_t* p = (uint16_t*)&memory.control_status;
*p++ = 0x8C08;
*p++ = 0x8C08;
*p++ = 0x8C08;
*p++ = 0x8C08;
*p++ = 0xFF00;
*p++ = 0xFF00;
*p++ = 0xFF00;
*p++ = 0xFF00;
memory.calibration[4] = 0x40;
}
char myrom[8] = {0x20, 0xBB, 0xAD, 0xCD, 0x0A, 0x00, 0x00, 0x00};
int main()
{
init_memory();
ows_setup(myrom);
for(;;)
ows_wait_request();
}
void ows_process_cmds()
{
uint16_t memory_address;
uint8_t b;
ow_crc16_reset();
switch(ows_recv())
{
case 0xAA: /* READ MEMORY */
ow_crc16_update(0xAA);
b = ows_recv();
((uint8_t*)&memory_address)[0] = b;
ow_crc16_update(b);
b = ows_recv();
((uint8_t*)&memory_address)[1] = b;
ow_crc16_update(b);
for(;;)
{
uint8_t b = ((uint8_t*)&memory)[memory_address];
ows_send(b);
ow_crc16_update(b);
if(errno)
break;
if((memory_address & 0x0F) == 0x0F) /* end of page */
{
uint16_t crc = ow_crc16_get();
ows_send(((uint8_t*)&crc)[0]);
ows_send(((uint8_t*)&crc)[1]);
ow_crc16_reset();
}
++memory_address;
if(memory_address >= sizeof(memory))
while(! errno)
ows_send(0xFF);
}
break;
case 0x55: /* WRITE MEMORY */
ow_crc16_update(0x55);
b = ows_recv();
((uint8_t*)&memory_address)[0] = b;
ow_crc16_update(b);
b = ows_recv();
((uint8_t*)&memory_address)[1] = b;
ow_crc16_update(b);
for(;;)
{
b = ows_recv();
if(errno)
break;
ow_crc16_update(b);
uint16_t crc = ow_crc16_get();
ows_send(((uint8_t*)&crc)[0]);
ows_send(((uint8_t*)&crc)[1]);
((uint8_t*)&memory)[memory_address] = b;
ows_send(b);
if(errno)
break;
++memory_address;
ow_crc16_reset();
ow_crc16_update(((uint8_t*)&memory_address)[0]);
ow_crc16_update(((uint8_t*)&memory_address)[1]);
}
break;
case 0x3C: /* CONVERT */
break;
default:
break;
}
}