forked from mozzwald/HDQLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HDQ.cpp
261 lines (211 loc) · 6.4 KB
/
HDQ.cpp
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
252
253
254
255
256
257
258
259
260
261
//.CPP LIBRARY CODE
/*
*
* Texas Instruments HDQ implementation for the Arduino API
* (cleft) Matthieu Lalonde 2008 ([email protected])
* Creative Commons BY-SA-NC
*
* http://trac.mlalonde.net/cral/browser/HDQ/
*
* Revision 1
*
*
*/
/* *********
* USAGE!
* The only other thing needed is to have a string pull up (4.7K did the job, I saw 10K used in the app notes).
************
#include <HDQ.h>
HDQ HDQ(HDQ_DEFAULT_PIN);
uint8_t DC1;
uint8_t DC2;
void setup() {
Serial.begin(9600);
}
void loop() {
for (uint8_t jj = 0; jj < 0x3F; jj++) {
DC1 = HDQ.read(jj);
DC2 = HDQ.read(jj+1);
int total = word(DC2, DC1);
Serial.print("Register 0x");
Serial.print(jj, HEX);
Serial.print(": ");
Serial.println(total);
jj++;
}
delay(2000);
Serial.println("");
}
*/
extern "C"
{
#include <pins_arduino.h>
}
#include <Arduino.h>
#include "HDQ.h"
#define _HDQ_readPin() (*inputReg & bitmask) >> pin // Change me to inline!*/
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
/*
*
* Constructor
* @param pinArg: pin number to attach to
*
*/
HDQ::HDQ(uint8_t pinArg)
{
pin = pinArg;
port = digitalPinToPort(pin);
bitmask = digitalPinToBitMask(pin);
outputReg = portOutputRegister(port);
inputReg = portInputRegister(port);
modeReg = portModeRegister(port);
#ifdef HDQ_DEBUG
debugPin = HDQ_DEFAULT_DEBUG_PIN;
debugPort = digitalPinToPort(debugPin);
debugBitmask = digitalPinToBitMask(debugPin);
debugOutputReg = portOutputRegister(debugPort);
debugInputReg = portInputRegister(debugPort);
debugModeReg = portModeRegister(debugPort);
sbi(*debugModeReg, debugPin); // Set pin as output
sbi(*debugOutputReg, debugPin); // Bring pin low
#endif
}
/*
*
* sendBreak: writes a break to the HDQ line
*/
void HDQ::doBreak(void)
{
sbi(*modeReg, pin); // Set pin as output
// Singal a break on the line
cbi(*outputReg, pin); // Bring pin low
delayMicroseconds(HDQ_DELAY_TB);
// Make sure we leave enough time for the slave to recover
cbi(*modeReg, pin); // Release pin
delayMicroseconds(HDQ_DELAY_TBR);
}
/*
writeByte: write a raw byte of data to the bus
@param payload: the byte to send
*/
void HDQ::writeByte(uint8_t payload)
{
sbi(*modeReg, pin); // Set pin as output
#ifdef HDQ_DEBUG
sbi(*debugOutputReg, debugPin); // Bring debug pin high
#endif
for (uint8_t ii = 0; ii < 8; ii++)
{
// Start bit
cbi(*outputReg, pin); // Bring pin low
delayMicroseconds(HDQ_DELAY_THW1);
// Toggle the pin for this bit
if (payload >> ii & 0x01)
{ // LSB First
sbi(*outputReg, pin); // High
}
else
{
cbi(*outputReg, pin); // Low
}
// Bit time
delayMicroseconds(HDQ_DELAY_THW0 - HDQ_DELAY_THW1 + 5);
// Stop bit
sbi(*outputReg, pin); // Bring the pin high
delayMicroseconds(HDQ_DELAY_TCYCH - HDQ_DELAY_THW0);
}
#ifdef HDQ_DEBUG
cbi(*debugOutputReg, debugPin); // Bring debug pin low
#endif
// Make sure we leave enough time for the slave to recover
cbi(*modeReg, pin); // Release pin
// no need for this as we released the PIN (it is pulled up) and we are waiting for a falling edge
// this blocks our processing and makes timing very tight
// delayMicroseconds(HDQ_DELAY_TBR);
// delayMicroseconds(HDQ_DELAY_TCYCH - HDQ_DELAY_THW0);
return;
}
/*
*
* write: send a payload to the device
* @param reg: the address of the register to write to
* @param payload: data to be sent
* @return: false, unless if verif is set, then
* it will read back the register and
* return true if it matches the payload
*
*/
bool HDQ::write(uint8_t reg, uint8_t payload)
{
// Singal a break
HDQ::doBreak();
// Write the register to write
HDQ::writeByte((reg |= HDQ_ADDR_MASK_WRITE));
// Wait for the slave to finish reading the register
delayMicroseconds((HDQ_DELAY_TRSPS_MAX - HDQ_DELAY_BIT_TOTAL) / 2);
// Write the payload
HDQ::writeByte(payload);
// Wait for the slave to finish writing the payload
delayMicroseconds((HDQ_DELAY_TRSPS_MAX - HDQ_DELAY_BIT_TOTAL) / 2);
cbi(*modeReg, pin); // Release pin
return true;
}
/*
*
* Write with verification
*
*/
bool HDQ::write(uint8_t reg, uint8_t payload, bool verif)
{ // Write the payload
HDQ::write(reg, payload);
// Verify the write
if (payload == HDQ::read(reg))
return true;
return false;
}
/*
*
* read: read from the device
* @param register: address of the register to read
* @return a uint8_t integer
*
*/
uint8_t HDQ::read(uint8_t reg)
{
uint8_t result = 0;
uint8_t maxTries = HDQ_DELAY_FAIL_TRIES; // ~128uS at 8Mhz with 8 instructions per loop
// Singal a break
HDQ::doBreak();
// Write the register to read
HDQ::writeByte((reg |= HDQ_ADDR_MASK_READ));
for (uint8_t ii = 0; ii < 8; ii++)
{
// Wait for the slave to toggle a low, or fail
// TODO: this should really be a falling edge detect but the timing is quite tight :/
maxTries = HDQ_DELAY_FAIL_TRIES;
while (_HDQ_readPin() != 0 && maxTries-- > 0)
if (maxTries == 1)
return 0x55; // make this a more obvious "fail" value (0xFF is too common), still kinda sucks
// Wait until Tdsub and half or one bit has passed
delayMicroseconds(((HDQ_DELAY_TDW0 - HDQ_DELAY_TDW1) / 2) + HDQ_DELAY_TDW1);
#ifdef HDQ_DEBUG
sbi(*debugOutputReg, debugPin); // Bring debug pin high
#endif
// Read the bit
result |= _HDQ_readPin() << ii;
#ifdef HDQ_DEBUG
cbi(*debugOutputReg, debugPin); // Bring debug pin low
#endif
// Wait until Tssub has passed
// delayMicroseconds(HDQ_DELAY_TCYCD - HDQ_DELAY_TDW0); // this seems too long, replaced with other half of TDW0/TDW1 difference
delayMicroseconds((HDQ_DELAY_TDW0 - HDQ_DELAY_TDW1) / 2);
}
delayMicroseconds(HDQ_DELAY_TB);
return result;
}