summaryrefslogtreecommitdiff
path: root/src/librfu_stwi.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/librfu_stwi.c')
-rw-r--r--src/librfu_stwi.c81
1 files changed, 42 insertions, 39 deletions
diff --git a/src/librfu_stwi.c b/src/librfu_stwi.c
index b88f21737..0287b358a 100644
--- a/src/librfu_stwi.c
+++ b/src/librfu_stwi.c
@@ -28,8 +28,8 @@ void STWI_init_all(struct RfuIntrStruct *interruptStruct, IntrFunc *interrupt, b
}
gSTWIStatus->rxPacket = &interruptStruct->rxPacketAlloc;
gSTWIStatus->txPacket = &interruptStruct->txPacketAlloc;
- gSTWIStatus->msMode = 1;
- gSTWIStatus->state = 0;
+ gSTWIStatus->msMode = AGB_CLK_MASTER;
+ gSTWIStatus->state = 0; // master send req
gSTWIStatus->reqLength = 0;
gSTWIStatus->reqNext = 0;
gSTWIStatus->ackLength = 0;
@@ -39,7 +39,7 @@ void STWI_init_all(struct RfuIntrStruct *interruptStruct, IntrFunc *interrupt, b
gSTWIStatus->timerActive = 0;
gSTWIStatus->error = 0;
gSTWIStatus->recoveryCount = 0;
- gSTWIStatus->unk_2c = 0;
+ gSTWIStatus->sending = 0;
REG_RCNT = 0x100; // TODO: mystery bit?
REG_SIOCNT = SIO_INTR_ENABLE | SIO_32BIT_MODE | SIO_115200_BPS;
STWI_init_Callback_M();
@@ -71,7 +71,7 @@ void AgbRFU_SoftReset(void)
*timerH = 3;
REG_RCNT = 0x80A0;
REG_SIOCNT = SIO_INTR_ENABLE | SIO_32BIT_MODE | SIO_115200_BPS;
- gSTWIStatus->state = 0;
+ gSTWIStatus->state = 0; // master send req
gSTWIStatus->reqLength = 0;
gSTWIStatus->reqNext = 0;
gSTWIStatus->reqActiveCommand = 0;
@@ -81,9 +81,9 @@ void AgbRFU_SoftReset(void)
gSTWIStatus->timerState = 0;
gSTWIStatus->timerActive = 0;
gSTWIStatus->error = 0;
- gSTWIStatus->msMode = 1;
+ gSTWIStatus->msMode = AGB_CLK_MASTER;
gSTWIStatus->recoveryCount = 0;
- gSTWIStatus->unk_2c = 0;
+ gSTWIStatus->sending = 0;
}
void STWI_set_MS_mode(u8 mode)
@@ -131,12 +131,12 @@ void STWI_set_Callback_S(void (*callbackS)(u16))
void STWI_set_Callback_ID(void (*func)(void)) // name in SDK, but is actually setting a function pointer
{
- gSTWIStatus->unk_20 = func;
+ gSTWIStatus->callbackID = func;
}
u16 STWI_poll_CommandEnd(void)
{
- while (gSTWIStatus->unk_2c == TRUE)
+ while (gSTWIStatus->sending == 1)
;
return gSTWIStatus->error;
}
@@ -195,7 +195,7 @@ void STWI_send_ConfigStatusREQ(void)
}
}
-void STWI_send_GameConfigREQ(const u8 *unk1, const u8 *data)
+void STWI_send_GameConfigREQ(const u8 *serial_gname, const u8 *uname)
{
u8 *packetBytes;
s32 i;
@@ -203,29 +203,28 @@ void STWI_send_GameConfigREQ(const u8 *unk1, const u8 *data)
if (!STWI_init(ID_GAME_CONFIG_REQ))
{
gSTWIStatus->reqLength = 6;
- // TODO: what is unk1
packetBytes = gSTWIStatus->txPacket->rfuPacket8.data;
packetBytes += sizeof(u32);
- *(u16 *)packetBytes = *(u16 *)unk1;
+ *(u16 *)packetBytes = *(u16 *)serial_gname;
packetBytes += sizeof(u16);
- unk1 += sizeof(u16);
+ serial_gname += sizeof(u16);
for (i = 0; i < 14; ++i)
{
- *packetBytes = *unk1;
+ *packetBytes = *serial_gname;
++packetBytes;
- ++unk1;
+ ++serial_gname;
}
for (i = 0; i < 8; ++i)
{
- *packetBytes = *data;
+ *packetBytes = *uname;
++packetBytes;
- ++data;
+ ++uname;
}
STWI_start_Command();
}
}
-void STWI_send_SystemConfigREQ(u16 unk1, u8 unk2, u8 unk3)
+void STWI_send_SystemConfigREQ(u16 availSlotFlag, u8 maxMFrame, u8 mcTimer)
{
if (!STWI_init(ID_SYSTEM_CONFIG_REQ))
{
@@ -234,9 +233,9 @@ void STWI_send_SystemConfigREQ(u16 unk1, u8 unk2, u8 unk3)
gSTWIStatus->reqLength = 1;
packetBytes = gSTWIStatus->txPacket->rfuPacket8.data;
packetBytes += sizeof(u32);
- *packetBytes++ = unk3;
- *packetBytes++ = unk2;
- *(u16*)packetBytes = unk1;
+ *packetBytes++ = mcTimer;
+ *packetBytes++ = maxMFrame;
+ *(u16*)packetBytes = availSlotFlag;
STWI_start_Command();
}
}
@@ -499,20 +498,17 @@ static void STWI_intr_timer(void)
STWI_stop_timer();
STWI_reset_ClockCounter();
if (gSTWIStatus->callbackM != NULL)
- gSTWIStatus->callbackM(255, 0);
+ gSTWIStatus->callbackM(ID_CLOCK_SLAVE_MS_CHANGE_ERROR_BY_DMA_REQ, 0);
break;
}
}
-static void STWI_set_timer(u8 unk)
+static void STWI_set_timer(u8 count)
{
- vu16 *timerL;
- vu16 *timerH;
-
- timerL = &REG_TMCNT_L(gSTWIStatus->timerSelect);
- timerH = &REG_TMCNT_H(gSTWIStatus->timerSelect);
+ vu16 *timerL = &REG_TMCNT_L(gSTWIStatus->timerSelect);
+ vu16 *timerH = &REG_TMCNT_H(gSTWIStatus->timerSelect);
REG_IME = 0;
- switch (unk)
+ switch (count)
{
case 50:
*timerL = 0xFCCB;
@@ -543,25 +539,31 @@ static void STWI_stop_timer(void)
REG_TMCNT_H(gSTWIStatus->timerSelect) = 0;
}
+/*
+ * Set up STWI to send REQ. Returns 1 if error (see below).
+ */
static u16 STWI_init(u8 request)
{
if (!REG_IME)
{
+ // Can't start sending if IME is disabled.
gSTWIStatus->error = ERR_REQ_CMD_IME_DISABLE;
if (gSTWIStatus->callbackM != NULL)
gSTWIStatus->callbackM(request, gSTWIStatus->error);
return TRUE;
}
- else if (gSTWIStatus->unk_2c == TRUE)
+ else if (gSTWIStatus->sending == 1)
{
+ // Already sending something. Cancel and error.
gSTWIStatus->error = ERR_REQ_CMD_SENDING;
- gSTWIStatus->unk_2c = FALSE;
+ gSTWIStatus->sending = 0;
if (gSTWIStatus->callbackM != NULL)
gSTWIStatus->callbackM(request, gSTWIStatus->error);
return TRUE;
}
- else if(!gSTWIStatus->msMode)
+ else if (gSTWIStatus->msMode == AGB_CLK_SLAVE)
{
+ // Can't send if clock slave
gSTWIStatus->error = ERR_REQ_CMD_CLOCK_SLAVE;
if (gSTWIStatus->callbackM != NULL)
gSTWIStatus->callbackM(request, gSTWIStatus->error, gSTWIStatus);
@@ -569,9 +571,10 @@ static u16 STWI_init(u8 request)
}
else
{
- gSTWIStatus->unk_2c = TRUE;
+ // Good to go, start sending
+ gSTWIStatus->sending = 1;
gSTWIStatus->reqActiveCommand = request;
- gSTWIStatus->state = 0;
+ gSTWIStatus->state = 0; // master send req
gSTWIStatus->reqLength = 0;
gSTWIStatus->reqNext = 0;
gSTWIStatus->ackLength = 0;
@@ -595,7 +598,7 @@ static s32 STWI_start_Command(void)
// but the cast here is required to avoid register issue
*(u32 *)gSTWIStatus->txPacket->rfuPacket8.data = 0x99660000 | (gSTWIStatus->reqLength << 8) | gSTWIStatus->reqActiveCommand;
REG_SIODATA32 = gSTWIStatus->txPacket->rfuPacket32.command;
- gSTWIStatus->state = 0;
+ gSTWIStatus->state = 0; // master send req
gSTWIStatus->reqNext = 1;
imeTemp = REG_IME;
REG_IME = 0;
@@ -608,7 +611,7 @@ static s32 STWI_start_Command(void)
static s32 STWI_restart_Command(void)
{
- if (gSTWIStatus->recoveryCount <= 1)
+ if (gSTWIStatus->recoveryCount < 2)
{
++gSTWIStatus->recoveryCount;
STWI_start_Command();
@@ -618,17 +621,17 @@ static s32 STWI_restart_Command(void)
if (gSTWIStatus->reqActiveCommand == ID_MS_CHANGE_REQ || gSTWIStatus->reqActiveCommand == ID_DATA_TX_AND_CHANGE_REQ || gSTWIStatus->reqActiveCommand == ID_UNK35_REQ || gSTWIStatus->reqActiveCommand == ID_RESUME_RETRANSMIT_AND_CHANGE_REQ)
{
gSTWIStatus->error = ERR_REQ_CMD_CLOCK_DRIFT;
- gSTWIStatus->unk_2c = 0;
+ gSTWIStatus->sending = 0;
if (gSTWIStatus->callbackM != NULL)
gSTWIStatus->callbackM(gSTWIStatus->reqActiveCommand, gSTWIStatus->error);
}
else
{
gSTWIStatus->error = ERR_REQ_CMD_CLOCK_DRIFT;
- gSTWIStatus->unk_2c = 0;
+ gSTWIStatus->sending = 0;
if (gSTWIStatus->callbackM != NULL)
gSTWIStatus->callbackM(gSTWIStatus->reqActiveCommand, gSTWIStatus->error);
- gSTWIStatus->state = 4; // TODO: what's 4
+ gSTWIStatus->state = 4; // error
}
}
return 0;
@@ -636,7 +639,7 @@ static s32 STWI_restart_Command(void)
static s32 STWI_reset_ClockCounter(void)
{
- gSTWIStatus->state = 5; // TODO: what is 5
+ gSTWIStatus->state = 5; // slave receive req init
gSTWIStatus->reqLength = 0;
gSTWIStatus->reqNext = 0;
REG_SIODATA32 = (1 << 31);