-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSPIMaster.h
59 lines (44 loc) · 1.35 KB
/
SPIMaster.h
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
#ifndef SPI_MASTER_H
#define SPI_MASTER_H
#include <tonc.h>
#include "SPISlave.h"
class SPIMaster {
public:
u32 transfer(u32 value) {
retry:
setNormalMode();
set32BitPackets();
set2MhzSpeed();
setMasterMode();
// (NO$GBA outputs 0xffffffff when the other GBA is busy running the audio)
setData(value == 0xffffffff ? ZERO_FLAG : value);
enableTransfer();
while (!isSlaveReady())
;
startTransfer();
while (!isReady())
;
disableTransfer();
u32 data = getData();
if (data == 0xffffffff)
goto retry;
return data;
}
private:
void setNormalMode() {
REG_RCNT = 0;
REG_SIOCNT = 0;
}
void setData(u32 data) { REG_SIODATA32 = data; }
u32 getData() { return REG_SIODATA32; }
void enableTransfer() { BIT_SET_LOW(REG_SIOCNT, SPI_BIT_SO); }
void disableTransfer() { BIT_SET_HIGH(REG_SIOCNT, SPI_BIT_SO); }
void startTransfer() { BIT_SET_HIGH(REG_SIOCNT, SPI_BIT_START); }
bool isSlaveReady() { return !BIT_IS_HIGH(REG_SIOCNT, SPI_BIT_SI); }
bool isReady() { return !BIT_IS_HIGH(REG_SIOCNT, SPI_BIT_START); }
void set32BitPackets() { BIT_SET_HIGH(REG_SIOCNT, SPI_BIT_LENGTH); }
void set2MhzSpeed() { BIT_SET_HIGH(REG_SIOCNT, SPI_BIT_CLOCK_SPEED); }
void setMasterMode() { BIT_SET_HIGH(REG_SIOCNT, SPI_BIT_CLOCK); }
};
extern SPIMaster* spiMaster;
#endif // SPI_MASTER_H