summaryrefslogtreecommitdiff
path: root/avr/1wire.c
blob: 5b6a09df99badf074733648ec54d334cdf456d70 (plain)
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
/************************************************************************/
/*									*/
/*			Access Dallas 1-Wire Devices			*/
/*                                                                      */
/*              Author: Peter Dannegger                                 */
/*                      danni@specs.de                                  */
/*                                                                      */
/************************************************************************/

/* modified by Florian Jung (flo@windfisch.org).
 * Changelog:
 *     - try to drive parasite-powered devices via avr-builtin pullup
 *     - DS199X access functions
 */

/* WARNING WARNING WARNING:
 * this code VIOLATES THE 1-WIRE SPECIFICATION! Instead of "letting the
 * line float" (which implies that it's gently pulled up to 5V by the
 * pullup resistor), we're hard connecting the line against +5V, without
 * any resistor in between. After some time, we let the line float, only
 * held up by the (weak) AVR-internal pullup.
 *
 * THIS COULD DESTROY YOUR HARDWARE! you have been warned.
 *
 * (We do this because the internal pullup isn't strong enough to serve
 * the DS1992 with power. The line is only hard-connected to +5V, when
 * we're (sort of) sure that the DS1992 won't pull down the line to GND
 * (since that would cause a short-circuit). Buuuut there's no guarantee,
 * it just works[tm])
 */


#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "1wire.h"

#ifndef W1_PIN
#define W1_PIN	PB4
#define W1_IN	PINB
#define W1_OUT	PORTB
#define W1_DDR	DDRB
#endif

uint8_t buff[2];


#define hard_gnd do { W1_OUT &= ~(1<<W1_PIN); W1_DDR |= 1<<W1_PIN; } while(0)
#define hard_vcc do { W1_OUT |= 1<<W1_PIN; W1_DDR |= 1<<W1_PIN; } while(0)
#define soft_vcc do { W1_DDR &= ~(1<<W1_PIN); W1_OUT |= 1<<W1_PIN; } while(0)
#define tristate do { W1_DDR &= ~(1<<W1_PIN); W1_OUT &= ~(1<<W1_PIN); } while(0)

uint8_t w1_reset(void)
{
	uint8_t err = 0;

	hard_gnd;

	_delay_us(480 );			// 480 us
	cli();
	
	hard_vcc;
	_delay_us(33);
	soft_vcc;
	
	_delay_us( 66-33 );
	if (W1_IN & (1<<W1_PIN))			// no presence detect
		err = 1;
	sei();
	_delay_us( 480 - 66 );
	if( (W1_IN & (1<<W1_PIN)) == 0 )		// short circuit
		err = 2;
	return err;
}


uint8_t w1_bit_io( uint8_t b )
{
	cli();
	hard_gnd;

	_delay_us( 1 );
	if( b )
	{
		hard_vcc;
		_delay_us(7 );
		soft_vcc;
		_delay_us( 15 - 1 - 7 );
	}
	else
	{
		_delay_us( 15 - 1 );
	}
	if( (W1_IN & (1<<W1_PIN)) == 0 )
		b = 0;
	_delay_us( 60 - 15 );
	//soft_vcc;
	hard_vcc;
	sei();
	return b;
}


uint8_t w1_byte_wr( uint8_t b )
{
	uint8_t i = 8, j;
	do
	{
		j = w1_bit_io( b & 1 );
		b >>= 1;
		if( j )
			b |= 0x80;
	}
	while( --i );
	soft_vcc;
	return b;
}


uint8_t w1_byte_rd( void )
{
	return w1_byte_wr( 0xFF );
}

/*
uint8_t w1_rom_search( uint8_t diff, uint8_t *id )
{
	uint8_t i, j, next_diff;
	uint8_t b;

	if( w1_reset() )
		return PRESENCE_ERR;			// error, no device found
	w1_byte_wr( SEARCH_ROM );			// ROM search command
	next_diff = LAST_DEVICE;			// unchanged on last device
	i = 8 * 8;					// 8 bytes
	do
	{
		j = 8;					// 8 bits
		do
		{
			b = w1_bit_io( 1 );			// read bit
			if( w1_bit_io( 1 ) ) 			// read complement bit
			{
				if( b )					// 11
					return DATA_ERR;			// data error
			}
			else
			{
				if( !b ) 				// 00 = 2 devices
				{
					if( diff > i ||
					        ((*id & 1) && diff != i) )
					{
						b = 1;				// now 1
						next_diff = i;			// next pass 0
					}
				}
			}
			w1_bit_io( b );     			// write bit
			*id >>= 1;
			if( b )					// store bit
				*id |= 0x80;
			i--;
		}
		while( --j );
		id++;					// next byte
	}
	while( i );
	return next_diff;				// to continue search
}*/

void ds1992_read(uint16_t addr, uint8_t* buf, uint8_t len)
{
	w1_byte_wr(SKIP_ROM);
	w1_byte_wr(0xF0); // read
	w1_byte_wr((addr & 0x00FF));
	w1_byte_wr((addr & 0xFF00) >> 8);
	for (int i=0; i < len; i++)
		buf[i] = w1_byte_rd();
}

void ds1992_scratch_write(uint16_t addr, const uint8_t* buf, uint8_t len)
{
	w1_byte_wr(SKIP_ROM);
	w1_byte_wr(0x0F); // write scratchpad
	w1_byte_wr((addr & 0x00FF));
	w1_byte_wr((addr & 0xFF00) >> 8);
	for (int i=0; i < len; i++)
		w1_byte_wr(buf[i]);
}

uint8_t ds1992_scratch_verify(uint16_t addr, const uint8_t* buf, uint8_t len, uint8_t* es_reg_ptr)
{
	uint8_t status = 0;

	w1_byte_wr(SKIP_ROM);
	w1_byte_wr(0xAA); // read scratchpad
	
	// check address bytes
	if (w1_byte_rd() != (addr & 0x00FF)) status |= 0x01;
	if (w1_byte_rd() != (addr & 0xFF00) >> 8) status |= 0x02;
	
	uint8_t es_reg = w1_byte_rd();
	*es_reg_ptr = es_reg;
	// check E/S register
	if (es_reg & 0x20) // PF: partial byte flag
		status |= 0x04;
	if (es_reg & 0x40) // OF: overflow flag
		status |= 0x08;
	if ((es_reg & 0x1F) != (addr & 0x1F) + (len-1))
		status |= 0x10;

	for (int i=0; i < len; i++)
		if (w1_byte_rd() != buf[i])
			status |= 0xF0;
	
	return status;
}

void ds1992_scratch_copy(uint16_t addr, uint8_t es_reg)
{
	w1_byte_wr(SKIP_ROM);
	w1_byte_wr(0x55); // copy scratchpad to memory
	w1_byte_wr((addr & 0x00FF));
	w1_byte_wr((addr & 0xFF00) >> 8);
	w1_byte_wr(es_reg);

	while(w1_byte_rd() != 0);
}

void w1_command( uint8_t command, uint8_t *id )
{
	uint8_t i;
	w1_reset();
	if( id )
	{
		w1_byte_wr( MATCH_ROM );			// to a single device
		i = 8;
		do
		{
			w1_byte_wr( *id );
			id++;
		}
		while( --i );
	}
	else
	{
		w1_byte_wr( SKIP_ROM );			// to all devices
	}
	w1_byte_wr( command );
}