-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathOWI.h
151 lines (140 loc) · 3.7 KB
/
OWI.h
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
/**
* @file Hardware/OWI.h
* @version 1.0
*
* @section License
* Copyright (C) 2017, Mikael Patel
*
* This library is free hardware; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Hardware Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*/
#ifndef HARDWARE_OWI_H
#define HARDWARE_OWI_H
#include "OWI.h"
#include "Driver/DS2482.h"
/**
* One Wire Interface (OWI) Bus Manager class using DS2482,
* Single-Channel 1-Wire Master, TWI to OWI Bridge Device.
*/
namespace Hardware {
class OWI : public ::OWI {
public:
/**
* Construct one wire bus manager for DS2482.
* @param[in] twi bus manager.
* @param[in] subaddr sub-address for device.
*/
OWI(TWI& twi, uint8_t subaddr = 0) :
m_bridge(twi, 0x18 | (subaddr & 0x03))
{
}
/**
* @override{OWI}
* Reset the one wire bus and check that at least one device is
* presence.
* @return true(1) if successful otherwise false(0).
*/
virtual bool reset()
{
return (m_bridge.one_wire_reset());
}
/**
* @override{OWI}
* Read the given number of bits from the one wire bus. Default
* number of bits is 8. Calculate partial check-sum.
* @param[in] bits to be read.
* @return value read.
*/
virtual uint8_t read(uint8_t bits = CHARBITS)
{
uint8_t res = 0;
if (bits == CHARBITS) {
m_bridge.one_wire_read_byte(res);
}
else {
uint8_t adjust = CHARBITS - bits;
bool value = 0;
while (bits--) {
res >>= 1;
m_bridge.one_wire_read_bit(value);
res |= (value ? 0x80 : 0x00);
}
res >>= adjust;
}
return (res);
}
/**
* @override{OWI}
* Write the given value to the one wire bus. The bits are written
* from LSB to MSB.
* @param[in] value to write.
* @param[in] bits to be written.
*/
virtual void write(uint8_t value, uint8_t bits = CHARBITS)
{
if (bits == CHARBITS) {
m_bridge.one_wire_write_byte(value);
}
else {
while (bits--) {
m_bridge.one_wire_write_bit(value & 0x01);
value >>= 1;
}
}
}
/**
* @override{OWI}
* Search (rom and alarm) support function. Reads 2-bits and writes
* given direction 1-bit value when discrepancy 0b00 read. Writes
* one(1) when 0b01 read, zero(0) on 0b10. Reading 0b11 is an error
* state.
* @param[in,out] dir bit to write when discrepancy read.
* @return 2-bits read and bit written.
*/
virtual int8_t triplet(uint8_t& dir)
{
return (m_bridge.one_wire_triplet(dir));
}
/**
* Global reset of device state machine logic. Returns true if
* successful otherwise false.
* @return bool.
*/
bool device_reset()
{
return (m_bridge.device_reset());
}
/**
* Configure one wire bus master with given parameters. Returns true
* if successful otherwise false.
* @param[in] apu active pull-up (default true).
* @param[in] spu strong pull-up (default false).
* @param[in] iws one wire speed (default false).
* @return bool.
*/
bool device_configuration(bool apu = true, bool spu = false, bool iws = false)
{
return (m_bridge.write_configuration(apu, spu, iws));
}
/**
* Select given channel (DS2482-800). Return true if successful
* otherwise false.
* @param[in] chan channel number (0..7).
* @return bool.
*/
bool channel_select(uint8_t chan)
{
return (m_bridge.channel_select(chan));
}
protected:
DS2482 m_bridge;
};
};
#endif