forked from afska/gba-link-connection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkSPI.hpp
263 lines (216 loc) · 7.24 KB
/
LinkSPI.hpp
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
262
263
#ifndef LINK_SPI_H
#define LINK_SPI_H
// --------------------------------------------------------------------------
// An SPI handler for the Link Port (Normal Mode, 32bits).
// --------------------------------------------------------------------------
// Usage:
// - 1) Include this header in your main.cpp file and add:
// LinkSPI* linkSPI = new LinkSPI();
// - 2) (Optional) Add the interrupt service routines: (*)
// irq_init(NULL);
// irq_add(II_SERIAL, LINK_SPI_ISR_SERIAL);
// // (this is only required for `transferAsync`)
// - 3) Initialize the library with:
// linkSPI->activate(LinkSPI::Mode::MASTER_256KBPS);
// // (use LinkSPI::Mode::SLAVE on the other end)
// - 4) Exchange 32-bit data with the other end:
// u32 data = linkSPI->transfer(0x12345678);
// // (this blocks the console indefinitely)
// - 5) Exchange data with a cancellation callback:
// u32 data = linkSPI->transfer(0x12345678, []() {
// u16 keys = ~REG_KEYS & KEY_ANY;
// return keys & KEY_START;
// });
// - 6) Exchange data asynchronously:
// linkSPI->transferAsync(0x12345678);
// // ...
// if (linkSPI->getAsyncState() == LinkSPI::AsyncState::READY) {
// u32 data = linkSPI->getAsyncData();
// // ...
// }
// --------------------------------------------------------------------------
// (*) libtonc's interrupt handler sometimes ignores interrupts due to a bug.
// That causes packet loss. You REALLY want to use libugba's instead.
// (see examples)
// --------------------------------------------------------------------------
// considerations:
// - when using Normal Mode between two GBAs, use a GBC Link Cable!
// - only use the 2Mbps mode with custom hardware (very short wires)!
// - don't send 0xFFFFFFFF, it's reserved for errors!
// --------------------------------------------------------------------------
#include <tonc_core.h>
// 8-bit mode (uncomment to enable)
// #define LINK_SPI_8BIT_MODE
#ifdef LINK_SPI_8BIT_MODE
#define LINK_SPI_DATA_TYPE u8
#endif
#ifndef LINK_SPI_8BIT_MODE
#define LINK_SPI_DATA_TYPE u32
#endif
#ifdef LINK_SPI_8BIT_MODE
#define LINK_SPI_DATA_REG REG_SIODATA8
#endif
#ifndef LINK_SPI_8BIT_MODE
#define LINK_SPI_DATA_REG REG_SIODATA32
#endif
#ifdef LINK_SPI_8BIT_MODE
#define LINK_SPI_NO_DATA 0xff
#endif
#ifndef LINK_SPI_8BIT_MODE
#define LINK_SPI_NO_DATA 0xffffffff
#endif
#define LINK_SPI_BIT_CLOCK 0
#define LINK_SPI_BIT_CLOCK_SPEED 1
#define LINK_SPI_BIT_SI 2
#define LINK_SPI_BIT_SO 3
#define LINK_SPI_BIT_START 7
#define LINK_SPI_BIT_LENGTH 12
#define LINK_SPI_BIT_IRQ 14
#define LINK_SPI_BIT_GENERAL_PURPOSE_LOW 14
#define LINK_SPI_BIT_GENERAL_PURPOSE_HIGH 15
static volatile char LINK_SPI_VERSION[] = "LinkSPI/v6.3.0";
const u32 LINK_SPI_MASK_CLEAR_SO_BIT = ~(1 << LINK_SPI_BIT_SO);
const u32 LINK_SPI_MASK_SET_START_BIT = (1 << LINK_SPI_BIT_START);
class LinkSPI {
public:
enum Mode { SLAVE, MASTER_256KBPS, MASTER_2MBPS };
enum AsyncState { IDLE, WAITING, READY };
bool isActive() { return isEnabled; }
void activate(Mode mode) {
this->mode = mode;
this->waitMode = false;
this->asyncState = IDLE;
this->asyncData = 0;
setNormalMode32Bit();
disableTransfer();
if (mode == SLAVE)
setSlaveMode();
else {
setMasterMode();
if (mode == MASTER_256KBPS)
set256KbpsSpeed();
else if (mode == MASTER_2MBPS)
set2MbpsSpeed();
}
isEnabled = true;
}
void deactivate() {
isEnabled = false;
setGeneralPurposeMode();
mode = SLAVE;
waitMode = false;
asyncState = IDLE;
asyncData = 0;
}
LINK_SPI_DATA_TYPE transfer(LINK_SPI_DATA_TYPE data) {
return transfer(data, []() { return false; });
}
template <typename F>
LINK_SPI_DATA_TYPE transfer(LINK_SPI_DATA_TYPE data,
F cancel,
bool _async = false,
bool _customAck = false) {
if (asyncState != IDLE)
return LINK_SPI_NO_DATA;
setData(data);
if (_async) {
asyncState = WAITING;
setInterruptsOn();
} else {
setInterruptsOff();
}
while (isMaster() && waitMode && !isSlaveReady())
if (cancel()) {
disableTransfer();
setInterruptsOff();
asyncState = IDLE;
return LINK_SPI_NO_DATA;
}
enableTransfer();
startTransfer();
if (_async)
return LINK_SPI_NO_DATA;
while (!isReady())
if (cancel()) {
stopTransfer();
disableTransfer();
return LINK_SPI_NO_DATA;
}
if (!_customAck)
disableTransfer();
return getData();
}
void transferAsync(LINK_SPI_DATA_TYPE data) {
transfer(
data, []() { return false; }, true);
}
template <typename F>
void transferAsync(LINK_SPI_DATA_TYPE data, F cancel) {
transfer(data, cancel, true);
}
LINK_SPI_DATA_TYPE getAsyncData() {
if (asyncState != READY)
return LINK_SPI_NO_DATA;
LINK_SPI_DATA_TYPE data = asyncData;
asyncState = IDLE;
return data;
}
Mode getMode() { return mode; }
void setWaitModeActive(bool isActive) { waitMode = isActive; }
bool isWaitModeActive() { return waitMode; }
AsyncState getAsyncState() { return asyncState; }
void _onSerial(bool _customAck = false) {
if (!isEnabled || asyncState != WAITING)
return;
if (!_customAck)
disableTransfer();
setInterruptsOff();
asyncState = READY;
asyncData = getData();
}
void _setSOHigh() { setBitHigh(LINK_SPI_BIT_SO); }
void _setSOLow() { setBitLow(LINK_SPI_BIT_SO); }
bool _isSIHigh() { return isBitHigh(LINK_SPI_BIT_SI); }
private:
Mode mode = Mode::SLAVE;
bool waitMode = false;
AsyncState asyncState = IDLE;
LINK_SPI_DATA_TYPE asyncData = 0;
volatile bool isEnabled = false;
void setNormalMode32Bit() {
REG_RCNT = REG_RCNT & ~(1 << LINK_SPI_BIT_GENERAL_PURPOSE_HIGH);
#ifdef LINK_SPI_8BIT_MODE
REG_SIOCNT = 0;
#endif
#ifndef LINK_SPI_8BIT_MODE
REG_SIOCNT = 1 << LINK_SPI_BIT_LENGTH;
#endif
}
void setGeneralPurposeMode() {
REG_RCNT = (REG_RCNT & ~(1 << LINK_SPI_BIT_GENERAL_PURPOSE_LOW)) |
(1 << LINK_SPI_BIT_GENERAL_PURPOSE_HIGH);
}
void setData(LINK_SPI_DATA_TYPE data) { LINK_SPI_DATA_REG = data; }
LINK_SPI_DATA_TYPE getData() { return LINK_SPI_DATA_REG; }
void enableTransfer() { _setSOLow(); }
void disableTransfer() { _setSOHigh(); }
void startTransfer() { setBitHigh(LINK_SPI_BIT_START); }
void stopTransfer() { setBitLow(LINK_SPI_BIT_START); }
bool isReady() { return !isBitHigh(LINK_SPI_BIT_START); }
bool isSlaveReady() { return !_isSIHigh(); }
void setMasterMode() { setBitHigh(LINK_SPI_BIT_CLOCK); }
void setSlaveMode() { setBitLow(LINK_SPI_BIT_CLOCK); }
void set256KbpsSpeed() { setBitLow(LINK_SPI_BIT_CLOCK_SPEED); }
void set2MbpsSpeed() { setBitHigh(LINK_SPI_BIT_CLOCK_SPEED); }
void setInterruptsOn() { setBitHigh(LINK_SPI_BIT_IRQ); }
void setInterruptsOff() { setBitLow(LINK_SPI_BIT_IRQ); }
bool isMaster() { return mode != SLAVE; }
bool isBitHigh(u8 bit) { return (REG_SIOCNT >> bit) & 1; }
void setBitHigh(u8 bit) { REG_SIOCNT |= 1 << bit; }
void setBitLow(u8 bit) { REG_SIOCNT &= ~(1 << bit); }
};
extern LinkSPI* linkSPI;
inline void LINK_SPI_ISR_SERIAL() {
linkSPI->_onSerial();
}
#endif // LINK_SPI_H