-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsiedleBus.c
191 lines (174 loc) · 3.81 KB
/
siedleBus.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
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
#include <avr/io.h>
#include "siedleBus.h"
#include <avr/interrupt.h>
volatile uint8_t dataOut[] = {0,0,0,0};
volatile uint8_t dataIn[] = {0,0,0,0};
volatile uint8_t bitCount = 0;
volatile uint8_t internalState = 0;
#define RECEIVE 1 //currently receiving, unable to transmit
#define SEND 2 //currently transmitting, unable to start another transmission
#define GAP_WAIT 3 //transmission or receiving finished
#define IDLE 4 //waiting for incoming transmission, able to transmit
#define RECEIVE_FIN 5 //frame has been received, able to transmit
#define WAIT_TM_FIN 6 //transmission has finished, no frame received yet
volatile uint8_t siedleSendState = 0;
#define SEND_FIN 2
volatile uint8_t siedleRecState = 0;
#define REC_FIN 2
void purgeTimer(void) {
stopTimer;
resetTimer;
clearWaveformReg;
enableCompareMatchInt;
}
inline void mblistSetBit(volatile uint8_t *list, uint8_t nr) {
unsigned char ArC=0;
ArC=nr/8;
*(list+ArC)|=(1<<(nr-(ArC*8)));
}
inline unsigned char mblistReadBit(volatile uint8_t *list, uint8_t nr) {
unsigned char ArC=0;
ArC=nr/8;
if (*(list+ArC)&(1<<(nr-(ArC*8))))
{
return 1;
} else return 0;
}
inline void mblistClearBit(volatile uint8_t *list, uint8_t nr) {
unsigned char ArC=0;
ArC=nr/8;
*(list+ArC)&=~(1<<(nr-(ArC*8)));
}
uint8_t siedleGetFrame(void) {
if (siedleRecState==REC_FIN)
{
return 1;
}
return 0;
}
uint8_t siedleRecRestart(void) {
siedleRecState=0;
bitCount=0;
internalState=IDLE;
clearExtIntFlag;
enableExtInt;
return 0;
}
/* @brief: Call this once before using any of the other functions.
*/
void siedleSetup(void) {
bitCount=0;
/* Prepare external Interrupt */
configureExtInt;
/* Prepare timer */
purgeTimer();
/* Wait about 100ms */
internalState=GAP_WAIT;
setCompareMatchMax;
startTimer;
}
/* @brief: sends a frame supplied in the argument.
Returns 0 if the bus is busy,
1 otherwise
*/
uint8_t siedleSendRaw(uint8_t *data) {
if (internalState==IDLE)
{
disableExtInt;
for (uint8_t c=0;c<4;c++)
{
dataOut[c]=data[c];
}
purgeTimer();
internalState=SEND;
setCompareMatch1ms;
bitCount=0;
startTimer;
return 1;
}
return 0;
}
ISR(timerCompIntVect) {
purgeTimer();
switch(internalState) {
case RECEIVE: {
if (!(PIND&(1<<PIND3)))
{
mblistSetBit(dataIn,bitCount);
} else mblistClearBit(dataIn,bitCount);
bitCount++;
if (bitCount==32)
{
if ((dataIn[0]&dataIn[1]&dataIn[2]&dataIn[3])==0xFF) //"crap detector"
{
/*
Sometimes, at least in my house, there are glitches of unknown origin
on the bus. Since these glitches seem to be shorter than 1ms, I can
filter them out like this.
*/
} else {
siedleRecState = REC_FIN;
}
/* Wait ~100ms */
internalState=GAP_WAIT;
bitCount=0;
setCompareMatchMax;
startTimer;
} else {
setCompareMatch2ms;
startTimer;
}
}
break;
case SEND: {
if (bitCount==32)
{
load10OhmOff;
load200OhmOff;
bitCount=0;
/* Wait ~100ms */
internalState=GAP_WAIT;
setCompareMatchMax;
startTimer;
} else {
load200OhmOn;
if (mblistReadBit(dataOut,bitCount))
{
load10OhmOff;
} else {
load10OhmOn;
}
setCompareMatch2ms;
startTimer;
bitCount++;
}
}
break;
case GAP_WAIT: {
if (bitCount==gapSize)
{
if (siedleRecState!=REC_FIN)
{
siedleRecRestart();
} else {
internalState=IDLE;
}
} else {
bitCount++;
setCompareMatchMax;
startTimer;
}
}
break;
}
}
ISR(extIntVect) {
siedleRecState = 0;
bitCount = 0;
internalState=RECEIVE;
clearWaveformReg;
resetTimer;
setCompareMatch1ms;
startTimer;
disableExtInt;
}