-
Notifications
You must be signed in to change notification settings - Fork 7
/
ds2408.c
111 lines (99 loc) · 2.6 KB
/
ds2408.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
/*
* Copyright © 2010-2015, Matthias Urlichs <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License (included; see the file LICENSE)
* for more details.
*/
/* This code implements (some of) the DS2408 8-bit I/O (obsolete).
*/
#include "onewire.h"
#include "debug.h"
#define C_READ_PIO 0xF0 // TODO
#define C_READ_CHANNEL 0xF5 // TODO
#define C_WRITE_CHANNEL 0x5A // TODO
#define C_WRITE_CONDSEARCH 0xCC // TODO
#define C_RESET_LATCHES 0xC3 // TODO
static uint8_t
pio_logic = 0xFF,
pio_output_latch = 0xFF,
pio_activity_latch = 0x00,
cond_search_mask = 0x00,
cond_polarity_mask = 0x00,
control_status = 0x88;
void do_read_pio(void)
{
uint16_t crc = 0;
uint16_t adr;
uint8_t bcrc = 1;
uint8_t b;
/*
The following code does:
* receive address (2 bytes), add them to CRC
* send 1..32 bytes (0xFF), add them to CRC
* send counter (4 bytes, CRC)
* send 4 zero bytes
* send inverted CRC
This is all very straightforward, except that the CRC calculation
is delayed somewhat: the time between recv_byte_in() and xmit_byte()
is only a bit wide, which may not be enough time to add a CRC byte.
*/
recv_byte();
crc = crc16(crc,0xF0);
b = recv_byte_in();
recv_byte();
crc = crc16(crc,b);
adr = b;
b = recv_byte_in();
adr |= b<<8;
#define XMIT(val) do { \
xmit_byte(val); \
if(bcrc) { crc = crc16(crc,b); bcrc = 0; } \
crc = crc16(crc,val); \
} while(0)
while(adr < 0x88) { XMIT(0); adr++; }
switch(adr) {
case 0x88: XMIT(pio_logic);
case 0x89: XMIT(pio_output_latch);
case 0x8A: XMIT(pio_activity_latch);
case 0x8B: XMIT(cond_search_mask);
case 0x8C: XMIT(cond_polarity_mask);
case 0x8D: XMIT(control_status);
case 0x8E: XMIT(0xFF);
case 0x8F: XMIT(0xFF);
crc = ~crc;
xmit_byte(crc);
xmit_byte(crc >> 8);
default:
while(1)
XMIT(0xFF);
}
}
void do_command(uint8_t cmd)
{
if(cmd == C_READ_PIO) {
DBG_P(":I");
do_read_pio();
} else {
DBG_P("?CI ");
DBG_X(cmd);
set_idle();
}
}
void update_idle(uint8_t bits)
{
//DBG_C('\\');
}
void init_state(void)
{
}
void mainloop(void)
{
}