-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathps3-charger.c
199 lines (175 loc) · 4.62 KB
/
ps3-charger.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
192
193
194
195
196
197
198
199
/* ATMEGA328PB code to emulate enough of a USB host to get a PS3
controller to charge.
Copyright (c) 2010 Jim Paris <[email protected]>
Copyright (c) 2023 Alexandre Ignatiev <[email protected]>
BSD license
*/
#define F_CPU 12000000UL
#include <avr/io.h>
#include <util/delay.h>
#define MASK_DP0 (1 << 7)
#define MASK_DP1 (1 << 2)
#define MASK_DP2 (1 << 3)
#define MASK_DP3 (1 << 6)
#define MASK_DM0 (1 << 1)
#define MASK_DM1 (1 << 4)
#define MASK_DM2 (1 << 5)
#define MASK_DM3 (1 << 0)
#define MASK_D0 (MASK_DP0 | MASK_DM0)
#define MASK_D1 (MASK_DP1 | MASK_DM1)
#define MASK_D2 (MASK_DP2 | MASK_DM2)
#define MASK_D3 (MASK_DP3 | MASK_DM3)
#define MASK_DP (MASK_DP0 | MASK_DP1 | MASK_DP2 | MASK_DP3)
#define MASK_DM (MASK_DM0 | MASK_DM1 | MASK_DM2 | MASK_DM3)
#define MASK_BOTH (MASK_DP | MASK_DM)
#define MASK_NONE 0
#define MASK_OUT MASK_BOTH
#define MASK_IN MASK_NONE
#define FS_J MASK_DP
#define FS_K MASK_DM
#define SE0 0
#define J "out %[_portb], %[_j]\n"
#define K "out %[_portb], %[_k]\n"
#define X "out %[_portb], %[_x]\n"
#define IN "out %[_ddrb], %[_in]\n"
#define OUT "out %[_ddrb], %[_out]\n"
#define D "nop\n" // delay 1 bit time
#define asmblock(str) asm volatile (str \
: \
: [_portb] "I" (_SFR_IO_ADDR(PORTB)), \
[_ddrb] "I" (_SFR_IO_ADDR(DDRB)), \
[_j] "r" (FS_J), \
[_k] "r" (FS_K), \
[_x] "r" (SE0), \
[_in] "r" (MASK_IN), \
[_out] "r" (MASK_OUT) \
: "memory")
/* Send a bus reset */
static void send_reset(void)
{
/* SE0 for >= 10 ms */
PORTB = SE0;
DDRB = MASK_OUT;
_delay_ms(10);
DDRB = MASK_IN;
}
/* Wait for next frame and send SOF.
Returns 0 if the device is not present. */
static int send_SOF(int count)
{
uint8_t status = (PINB & MASK_BOTH);
while (count--) {
_delay_ms(1);
if ((PINB & MASK_BOTH) != status)
return 0;
asmblock(
OUT
// send SOF 1337
K J K J K J K K K J J K J J K K K J K K K K J K K J J J J J J K X X J
IN
X
);
}
return 1;
}
/* Send SET_ADDRESS */
static void send_SET_ADDRESS(void)
{
asmblock(
OUT
// send SETUP
K J K J K J K K K J J J K K J K J K J K J K J K J K J K K J K J X X J
// delay a little
D D D D D D D D
// send DATA0
K J K J K J K K K K J K J K K K J K J K J K J K K J J K J K J K K J K
J K J K J K J K J K J K J K J K J K J K J K J K J K J K J K J K J K J
K J K J K J K J K J J J K K J J J J J K K J K K J K X X J
IN
X
// device will send ACK now (within 16 bit times)
);
// wait until new frame; an immediate IN will just get NAKed
send_SOF(1);
asmblock(
OUT
// send IN
K J K J K J K K K J K K J J J K J K J K J K J K J K J K K J K J X X J
// device will send DATA1 now.
IN
// delay for the expected 35 bits of data
X D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D
// wait 12 more bit times (16 is max turnaround)
D D D D D D D D D D D D
OUT
// send ACK
K J K J K J K K J J K J J K K K X X J
IN
X
);
}
/* Send SET_CONFIGURATION */
static void send_SET_CONFIGURATION(void)
{
asmblock(
OUT
// send SETUP
K J K J K J K K K J J J K K J K K J K J K J K J K J K K J J J J X X J
// delay a little
D D D D D D D D
// send DATA0
K J K J K J K K K K J K J K K K J K J K J K J K K J K K J K J K K J K
J K J K J K J K J K J K J K J K J K J K J K J K J K J K J K J K J K J
K J K J K J K J K J J J J K J J K J J K K J K K J K X X J
IN
X
// device will send ACK now (within 16 bit times)
);
// wait until new frame; an immediate IN will just get NAKed
send_SOF(1);
asmblock(
OUT
// send IN
K J K J K J K K K J K K J J J K K J K J K J K J K J K K J J J J X X J
// device will send DATA1 now.
IN
// delay for the expected 35 bits of data
X D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D
// wait 12 more bit times (16 is max turnaround)
D D D D D D D D D D D D
OUT
// send ACK
K J K J K J K K J J K J J K K K X X J
IN
X
);
}
int main(void) {
// Clear prescaler (no need to adjust the fuses)
CLKPR = (1 << CLKPCE);
CLKPR = 0;
// Calibrate internal RC osc to 12MHz
OSCCAL = 0xcd; // 12.02 MHz measured
// Tristate all D+ and D-
DDRB = MASK_IN;
PORTB = SE0;
DDRD = 1; // Status LED output
_delay_ms(100);
for (;;) {
// if the device doesn't appear present, do nothing
while ((PINB & MASK_D0) != MASK_DP0 && (PINB & MASK_D1) != MASK_DP1 && (PINB & MASK_D2) != MASK_DP2 && (PINB & MASK_D3) != MASK_DP3)
_delay_ms(1);
// perform reset
_delay_ms(100);
send_reset();
PIND = 1; // Toggle status LED
// perform basic enumeration
send_SOF(10);
send_SET_ADDRESS();
send_SOF(3);
send_SET_CONFIGURATION();
// now send empty frames as long as the device is present.
while (send_SOF(1))
continue;
}
}