-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.c
155 lines (130 loc) · 2.58 KB
/
main.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
#include <avr/io.h>
#define PIN_CLK (1 << PINB0)
#define PIN_DAT (1 << PINB1)
uint8_t readByte()
{
uint8_t i;
uint8_t pins = PINB;
uint8_t prevPins = 0;
uint8_t byte = 0;
for(i = 0; i < 8; ++i)
{
// wait for clk low
prevPins = pins;
while((pins = PINB) & PIN_CLK)
{
// if data changes while clock is high then bail out
// as it's a stop or a start
if((pins & PIN_DAT) != (prevPins & PIN_DAT))
return 0;
prevPins = pins;
}
// wait for clk high
while(!((pins = PINB) & PIN_CLK))
;
byte = (byte << 1) | ((pins & PIN_DAT) > 0);
}
// wait for clk low
prevPins = pins;
while((pins = PINB) & PIN_CLK)
{
if((pins & PIN_DAT) != (prevPins & PIN_DAT))
return 0;
prevPins = pins;
}
// wait for clk high
while(!((pins = PINB) & PIN_CLK))
;
return byte;
}
// force the specified bit (1-8) to be zero
uint8_t overrideByte(uint8_t bit)
{
uint8_t pins = PINB;
uint8_t prevPins = 0;
while(--bit)
{
// wait for clk low
prevPins = pins;
while((pins = PINB) & PIN_CLK)
{
// if dat changes then bail out, it's a start or stop
if((pins & PIN_DAT) != (prevPins & PIN_DAT))
return 0;
}
// wait for clk high
while(!((pins = PINB) & PIN_CLK))
;
}
// wait for clk low
while((pins = PINB) & PIN_CLK)
;
// force dat low
DDRB = (1 << DDB2) | (1 << DDB1);
PORTB = 1 << PORTB2;
// wait for clk high
while(!((pins = PINB) & PIN_CLK))
;
// wait for clk low
while(PINB & PIN_CLK)
;
// release dat (back to high-z)
DDRB = (1 << DDB2);
PORTB = 0;
return 1;
}
void main()
{
uint8_t currentTransmitter = 0;
uint8_t pins = 0;
uint8_t prevPins = 0;
uint8_t addr = 0;
uint8_t lastRegAddr = 0;
CCP = 0xD8;
CLKPSR = 0;
PORTB = 0;
DDRB = 1 << DDB2;
for(;;)
{
PORTB = 0;
DDRB = (1 << DDB2);
prevPins = pins;
pins = PINB;
// if it's a START condition
if(!(pins & PIN_DAT) && (prevPins & PIN_DAT) && (pins & PIN_CLK))
{
RESTART:
// read device address
if((addr = readByte()) == 0)
continue;
// if it's a write to the EP9132
if(addr == 0x70)
{
// read the register address
if((lastRegAddr = readByte()) == 0)
continue;
if(lastRegAddr == 7)
{
if((currentTransmitter = readByte()) == 0)
goto RESTART;
currentTransmitter &= 1;
}
}
// if it's a read from the EP9132
else if(addr == 0x71)
{
if(lastRegAddr == 0x07)
{
// force the 4th bit to zero (bit 4, RX_ENC_ON)
overrideByte(4);
}
else if(lastRegAddr == 0x40)
{
// force the 8th bit to zero (bit 0, RX_M0_RDY)
overrideByte(8);
}
lastRegAddr = 0;
}
}
}
}