-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathw1.c
136 lines (114 loc) · 2.44 KB
/
w1.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
/*
* Basic routines to bit-bang standard 1-Wire via a GPIO pin
*
* Copyright 2018 Jonathan McDowell <[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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <avr/io.h>
#include <util/atomic.h>
#include <util/delay.h>
#include "w1.h"
uint8_t w1_crc(uint8_t *buf, uint8_t len)
{
uint8_t i, j, crc;
crc = 0;
for (i = 0; i < len; i++)
{
crc ^= buf[i];
for (j = 0; j < 8; j++)
{
crc = crc >> 1 ^ ((crc & 1) ? 0x8c : 0);
}
}
return crc;
}
void w1_write(uint8_t val)
{
uint8_t i;
for (i = 0; i < 8; i++) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
/* Pull low for 6µs for 1, 60µs for 0 */
DDRB |= 1 << W1_PIN;
if (val & 1)
_delay_us(6);
else
_delay_us(60);
/* Release to make up to 70µs total */
DDRB &= ~(1 << W1_PIN);
if (val & 1)
_delay_us(64);
else
_delay_us(10);
}
val >>= 1;
}
}
bool w1_read_bit()
{
bool val;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
/* Pull low for 6µs */
DDRB |= 1 << W1_PIN;
_delay_us(6);
/* Release for 9µs */
DDRB &= ~(1 << W1_PIN);
_delay_us(9);
/* Read the line state */
val = ((PINB >> W1_PIN) & 1);
}
_delay_us(55);
return val;
}
uint8_t w1_read_byte()
{
uint8_t i, val;
val = 0;
for (i = 0; i < 8; i++) {
if (w1_read_bit())
val |= (1 << i);
}
return val;
}
void w1_read(uint8_t *buf, uint8_t len)
{
uint8_t i;
for (i = 0; i < len; i++) {
buf[i] = w1_read_byte();
}
}
bool w1_reset(bool nowait)
{
bool present;
/* Pull low for 480µs */
DDRB |= 1 << W1_PIN;
_delay_us(480);
/* Release for 70µs */
DDRB &= ~(1 << W1_PIN);
_delay_us(70);
/* If there's a device present it'll have pulled the line low */
present = !((PINB >> W1_PIN) & 1);
/* Wait for reset to complete */
if (!nowait)
_delay_us(410);
return present;
}
void w1_init(void)
{
/* Set 1w pin to low */
PORTB &= ~(1 << W1_PIN);
}