pokeemerald/src/librfu_rfu.c

2306 lines
73 KiB
C
Raw Normal View History

2020-04-28 01:27:54 +02:00
#include <limits.h>
#include "librfu.h"
2020-01-16 00:21:54 +01:00
struct LLSFStruct
{
2020-02-16 19:49:36 +01:00
u8 frameSize;
u8 recvFirstShift;
u8 connSlotFlagShift;
u8 slotStateShift;
u8 ackShift;
u8 phaseShit;
u8 nShift;
u8 recvFirstMask;
u8 connSlotFlagMask;
u8 slotStateMask;
u8 ackMask;
u8 phaseMask;
u8 nMask;
u16 framesMask;
2020-01-16 00:21:54 +01:00
};
struct RfuLocalStruct
{
2020-02-16 19:49:36 +01:00
u8 recvFirst;
u8 connSlotFlag;
u8 slotState;
u8 ack;
u8 phase;
u8 n;
u16 frame;
2020-01-16 00:21:54 +01:00
};
2020-02-16 19:49:36 +01:00
static void rfu_CB_defaultCallback(u8 reqCommand, u16 reqResult);
static void rfu_CB_reset(u8 reqCommand, u16 reqResult);
static void rfu_CB_configGameData(u8 reqCommand, u16 reqResult);
static void rfu_CB_stopMode(u8 reqCommand, u16 reqResult);
static void rfu_CB_startSearchChild(u8 reqCommand, u16 reqResult);
static void rfu_CB_pollAndEndSearchChild(u8 reqCommand, u16 reqResult);
static void rfu_CB_startSearchParent(u8 reqCommand, u16 reqResult);
static void rfu_CB_pollSearchParent(u8 reqCommand, u16 reqResult);
static void rfu_CB_pollConnectParent(u8 reqCommand, u16 reqResult);
static void rfu_CB_pollConnectParent(u8 reqCommand, u16 reqResult);
static void rfu_CB_disconnect(u8 reqCommand, u16 reqResult);
static void rfu_CB_CHILD_pollConnectRecovery(u8 reqCommand, u16 reqResult);
static void rfu_CB_sendData(UNUSED u8 reqCommand, u16 reqResult);
static void rfu_CB_sendData2(UNUSED u8 reqCommand, u16 reqResult);
static void rfu_CB_sendData3(u8 reqCommand, u16 reqResult);
static void rfu_CB_recvData(u8 reqCommand, u16 reqResult);
static void rfu_enableREQCallback(bool8 enable);
2020-01-16 00:21:54 +01:00
static void rfu_STC_clearAPIVariables(void);
static void rfu_STC_readChildList(void);
static void rfu_STC_readParentCandidateList(void);
2020-02-16 19:49:36 +01:00
static void rfu_STC_REQ_callback(u8 reqCommand, u16 reqResult);
2020-01-16 00:21:54 +01:00
static void rfu_STC_removeLinkData(u8, u8);
static void rfu_STC_fastCopy(const u8 **, u8 **, s32);
static void rfu_STC_clearLinkStatus(u8);
static void rfu_NI_checkCommFailCounter(void);
static u16 rfu_STC_setSendData_org(u8, u8, u8, const void *, u32);
static void rfu_constructSendLLFrame(void);
static u16 rfu_STC_NI_constructLLSF(u8, u8 **, struct NIComm *);
static u16 rfu_STC_UNI_constructLLSF(u8, u8 **);
static void rfu_STC_PARENT_analyzeRecvPacket(void);
static void rfu_STC_CHILD_analyzeRecvPacket(void);
static u16 rfu_STC_analyzeLLSF(u8, const u8 *, u16);
static void rfu_STC_UNI_receive(u8, const struct RfuLocalStruct *, const u8 *);
static void rfu_STC_NI_receive_Receiver(u8, const struct RfuLocalStruct *, const u8 *);
2020-02-16 19:49:36 +01:00
static void rfu_STC_NI_receive_Sender(u8, u8, const struct RfuLocalStruct *, UNUSED const u8 *);
2020-01-16 00:21:54 +01:00
static void rfu_STC_NI_initSlot_asRecvDataEntity(u8, struct NIComm *);
static void rfu_STC_NI_initSlot_asRecvControllData(u8, struct NIComm *);
2020-01-16 00:28:23 +01:00
struct RfuSlotStatusUNI *gRfuSlotStatusUNI[RFU_CHILD_MAX];
struct RfuSlotStatusNI *gRfuSlotStatusNI[RFU_CHILD_MAX];
struct RfuLinkStatus *gRfuLinkStatus;
struct RfuStatic *gRfuStatic;
struct RfuFixed *gRfuFixed;
2020-01-16 00:21:54 +01:00
2020-01-16 00:28:23 +01:00
static const struct LLSFStruct llsf_struct[2] = {
2020-02-16 19:49:36 +01:00
[MODE_CHILD] = {
.frameSize = 2,
.recvFirstShift = 14,
.connSlotFlagShift = 0,
.slotStateShift = 10,
.ackShift = 9,
.phaseShit = 5,
.nShift = 7,
.recvFirstMask = 2,
.connSlotFlagMask = 0,
.slotStateMask = 15,
.ackMask = 1,
.phaseMask = 3,
.nMask = 3,
.framesMask = 0x1f
},
[MODE_PARENT] = {
.frameSize = 3,
.recvFirstShift = 22,
.connSlotFlagShift = 18,
.slotStateShift = 14,
.ackShift = 13,
.phaseShit = 9,
.nShift = 11,
.recvFirstMask = 3,
.connSlotFlagMask = 15,
.slotStateMask = 15,
.ackMask = 1,
.phaseMask = 3,
.nMask = 3,
.framesMask = 0x7f
}
2020-01-16 00:21:54 +01:00
};
2020-02-16 19:49:36 +01:00
#define xstr(s) str(s)
#define str(s) #s
2020-04-28 01:25:55 +02:00
static const char version_string[] = "RFU_V" xstr(LIBRFU_VERSION);
2020-01-16 00:21:54 +01:00
static const char str_checkMbootLL[] = "RFU-MBOOT";
2020-02-16 19:49:36 +01:00
#define COPY(src, dst, iterator, size) do { \
const u16 *_src = (const u16 *)(src); \
u16 *_dst = (u16 *)(dst); \
(iterator) = (size); \
while ((iterator)-- != 0) \
*_dst++ = *_src++; \
} while (0)
u16 rfu_initializeAPI(u32 *APIBuffer, u16 buffByteSize, IntrFunc *sioIntrTable_p, bool8 copyInterruptToRam)
{
u16 i;
2020-01-16 00:21:54 +01:00
u16 *dst;
const u16 *src;
2020-02-16 19:49:36 +01:00
u16 buffByteSizeMax;
2020-02-16 19:49:36 +01:00
// is in EWRAM?
if (((uintptr_t)APIBuffer & 0xF000000) == 0x2000000 && copyInterruptToRam)
2020-01-16 00:21:54 +01:00
return ERR_RFU_API_BUFF_ADR;
2020-02-16 19:49:36 +01:00
// is not 4-byte aligned?
2020-01-16 00:21:54 +01:00
if ((u32)APIBuffer & 3)
return ERR_RFU_API_BUFF_ADR;
if (copyInterruptToRam)
{
// An assert/debug print may have existed before, ie
2020-01-16 00:21:54 +01:00
// printf("%s %u < %u", "somefile.c:12345", buffByteSize, num)
2020-02-16 19:49:36 +01:00
// to push this into buffByteSizeMax?
buffByteSizeMax = RFU_API_BUFF_SIZE_RAM;
if (buffByteSize < buffByteSizeMax)
2020-01-16 00:21:54 +01:00
return ERR_RFU_API_BUFF_SIZE;
}
if (!copyInterruptToRam)
{
2020-02-16 19:49:36 +01:00
buffByteSizeMax = RFU_API_BUFF_SIZE_ROM; // same issue as above
if (buffByteSize < buffByteSizeMax)
2020-01-16 00:21:54 +01:00
return ERR_RFU_API_BUFF_SIZE;
}
2020-02-16 19:49:36 +01:00
gRfuLinkStatus = (void *)APIBuffer + 0;
gRfuStatic = (void *)APIBuffer + 0xb4; // + sizeof(*gRfuLinkStatus)
gRfuFixed = (void *)APIBuffer + 0xdc; // + sizeof(*gRfuStatic)
gRfuSlotStatusNI[0] = (void *)APIBuffer + 0x1bc; // + sizeof(*gRfuFixed)
2020-04-28 01:25:55 +02:00
gRfuSlotStatusUNI[0] = (void *)APIBuffer + 0x37c; // + sizeof(*gRfuSlotStatusNI[0]) * RFU_CHILD_MAX
2020-01-16 00:21:54 +01:00
for (i = 1; i < RFU_CHILD_MAX; ++i)
{
gRfuSlotStatusNI[i] = &gRfuSlotStatusNI[i - 1][1];
gRfuSlotStatusUNI[i] = &gRfuSlotStatusUNI[i - 1][1];
}
2020-04-28 01:25:55 +02:00
// remaining space in API buffer is used for `struct RfuIntrStruct`.
2020-01-16 00:21:54 +01:00
gRfuFixed->STWIBuffer = (struct RfuIntrStruct *)&gRfuSlotStatusUNI[3][1];
STWI_init_all((struct RfuIntrStruct *)&gRfuSlotStatusUNI[3][1], sioIntrTable_p, copyInterruptToRam);
rfu_STC_clearAPIVariables();
for (i = 0; i < RFU_CHILD_MAX; ++i)
{
gRfuSlotStatusNI[i]->recvBuffer = NULL;
gRfuSlotStatusNI[i]->recvBufferSize = 0;
gRfuSlotStatusUNI[i]->recvBuffer = NULL;
gRfuSlotStatusUNI[i]->recvBufferSize = 0;
}
2020-02-16 19:49:36 +01:00
// rfu_REQ_changeMasterSlave is the function next to rfu_STC_fastCopy
#if LIBRFU_VERSION < 1026
src = (const u16 *)((uintptr_t)&rfu_STC_fastCopy & ~1);
dst = gRfuFixed->fastCopyBuffer;
buffByteSizeMax = ((void *)rfu_REQ_changeMasterSlave - (void *)rfu_STC_fastCopy) / sizeof(u16);
while (buffByteSizeMax-- != 0)
*dst++ = *src++;
#else
COPY(
(uintptr_t)&rfu_STC_fastCopy & ~1,
gRfuFixed->fastCopyBuffer,
buffByteSizeMax,
0x60 / sizeof(u16)
);
#endif
gRfuFixed->fastCopyPtr = (void *)gRfuFixed->fastCopyBuffer + 1;
2020-01-16 00:21:54 +01:00
return 0;
}
static void rfu_STC_clearAPIVariables(void)
{
u16 IMEBackup = REG_IME;
2020-02-16 19:49:36 +01:00
u8 i, flags;
2020-01-16 00:21:54 +01:00
REG_IME = 0;
2020-02-16 19:49:36 +01:00
flags = gRfuStatic->flags;
2020-01-16 00:21:54 +01:00
CpuFill16(0, gRfuStatic, sizeof(struct RfuStatic));
2020-02-16 19:49:36 +01:00
gRfuStatic->flags = flags & 8;
2020-01-16 00:21:54 +01:00
CpuFill16(0, gRfuLinkStatus, sizeof(struct RfuLinkStatus));
gRfuLinkStatus->watchInterval = 4;
gRfuStatic->nowWatchInterval = 0;
gRfuLinkStatus->parentChild = MODE_NEUTRAL;
rfu_clearAllSlot();
gRfuStatic->SCStartFlag = 0;
2020-02-16 19:49:36 +01:00
for (i = 0; i < RFU_CHILD_MAX; ++i)
2020-01-16 00:21:54 +01:00
gRfuStatic->cidBak[i] = 0;
REG_IME = IMEBackup;
}
2020-01-16 00:21:54 +01:00
void rfu_REQ_PARENT_resumeRetransmitAndChange(void)
{
STWI_set_Callback_M(rfu_STC_REQ_callback);
STWI_send_ResumeRetransmitAndChangeREQ();
}
u16 rfu_UNI_PARENT_getDRAC_ACK(u8 *ackFlag)
{
2020-02-16 19:49:36 +01:00
u8 *buf;
2020-01-16 00:21:54 +01:00
*ackFlag = 0;
if (gRfuLinkStatus->parentChild != MODE_PARENT)
return ERR_MODE_NOT_PARENT;
buf = rfu_getSTWIRecvBuffer();
2020-02-16 19:49:36 +01:00
switch (*buf)
2020-01-16 00:21:54 +01:00
{
case 40:
case 54:
2020-02-16 19:49:36 +01:00
if (buf[1] == 0)
2020-01-16 00:21:54 +01:00
*ackFlag = gRfuLinkStatus->connSlotFlag;
else
2020-02-16 19:49:36 +01:00
*ackFlag = buf[4];
2020-01-16 00:21:54 +01:00
return 0;
default:
return ERR_REQ_CMD_ID;
}
}
void rfu_setTimerInterrupt(u8 timerNo, IntrFunc *timerIntrTable_p)
{
STWI_init_timer(timerIntrTable_p, timerNo);
}
2020-02-16 19:49:36 +01:00
u8 *rfu_getSTWIRecvBuffer(void)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
return (u8 *)gRfuFixed->STWIBuffer;
2020-01-16 00:21:54 +01:00
}
void rfu_setMSCCallback(void (*callback)(u16 reqCommandId))
{
STWI_set_Callback_S(callback);
}
void rfu_setREQCallback(void (*callback)(u16 reqCommandId, u16 reqResult))
{
gRfuFixed->reqCallback = callback;
rfu_enableREQCallback(callback != NULL);
}
static void rfu_enableREQCallback(bool8 enable)
{
if (enable)
gRfuStatic->flags |= 8;
else
gRfuStatic->flags &= 0xF7;
}
2020-02-16 19:49:36 +01:00
static void rfu_STC_REQ_callback(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
STWI_set_Callback_M(rfu_CB_defaultCallback);
gRfuStatic->reqResult = reqResult;
if (gRfuStatic->flags & 8)
2020-02-16 19:49:36 +01:00
gRfuFixed->reqCallback(reqCommand, reqResult);
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_defaultCallback(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
s32 bmSlotFlags;
2020-01-16 00:21:54 +01:00
u8 i;
2020-02-16 19:49:36 +01:00
if (reqCommand == ID_CLOCK_SLAVE_MS_CHANGE_ERROR_BY_DMA_REQ)
{
2020-01-16 00:21:54 +01:00
if (gRfuStatic->flags & 8)
2020-02-16 19:49:36 +01:00
gRfuFixed->reqCallback(reqCommand, reqResult);
bmSlotFlags = gRfuLinkStatus->connSlotFlag | gRfuLinkStatus->linkLossSlotFlag;
2020-01-16 00:21:54 +01:00
for (i = 0; i < RFU_CHILD_MAX; ++i)
2020-02-16 19:49:36 +01:00
if ((bmSlotFlags >> i) & 1)
2020-01-16 00:21:54 +01:00
rfu_STC_removeLinkData(i, 1);
gRfuLinkStatus->parentChild = MODE_NEUTRAL;
}
}
u16 rfu_waitREQComplete(void)
{
STWI_poll_CommandEnd();
return gRfuStatic->reqResult;
}
void rfu_REQ_RFUStatus(void)
{
STWI_set_Callback_M(rfu_STC_REQ_callback);
STWI_send_SystemStatusREQ();
}
u16 rfu_getRFUStatus(u8 *rfuState)
{
if (gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[0] != 0x93)
return ERR_REQ_CMD_ID;
if (STWI_poll_CommandEnd() == 0)
*rfuState = gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[7];
else
*rfuState = 0xFF;
return 0;
}
2020-02-16 19:49:36 +01:00
/*
* RFU Multiboot images are loaded into IWRAM
* struct RfuMbootLL
* {
* struct RfuLinkStatus status;
* u8 filler_B4[0x3C];
* char name[10];
* u16 checksum;
* }
* Returns 1 if the packet to inherit is malformed.
*/
2020-01-16 00:21:54 +01:00
u16 rfu_MBOOT_CHILD_inheritanceLinkStatus(void)
{
const char *s1 = str_checkMbootLL;
char *s2 = (char *)0x30000F0;
u16 checksum;
2020-02-16 19:49:36 +01:00
u16 *mb_buff_iwram_p;
2020-01-16 00:21:54 +01:00
u8 i;
2020-02-16 19:49:36 +01:00
// if (strcmp(s1, s2) != 0) return 1;
2020-01-16 00:21:54 +01:00
while (*s1 != '\0')
if (*s1++ != *s2++)
return 1;
2020-02-16 19:49:36 +01:00
mb_buff_iwram_p = (u16 *)0x3000000;
// The size of struct RfuLinkStatus is 180
2020-01-16 00:21:54 +01:00
checksum = 0;
2020-02-16 19:49:36 +01:00
for (i = 0; i < 180/2; ++i)
checksum += *mb_buff_iwram_p++;
2020-01-16 00:21:54 +01:00
if (checksum != *(u16 *)0x30000FA)
return 1;
CpuCopy16((u16 *)0x3000000, gRfuLinkStatus, sizeof(struct RfuLinkStatus));
2020-02-16 19:49:36 +01:00
gRfuStatic->flags |= 0x80; // mboot
2020-01-16 00:21:54 +01:00
return 0;
}
void rfu_REQ_stopMode(void)
{
vu32 *timerReg;
if (REG_IME == 0)
{
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(ID_STOP_MODE_REQ, 6);
2020-01-16 00:21:54 +01:00
gSTWIStatus->error = ERR_REQ_CMD_IME_DISABLE;
}
2020-01-16 00:21:54 +01:00
else
{
AgbRFU_SoftReset();
rfu_STC_clearAPIVariables();
2020-02-16 19:49:36 +01:00
if (AgbRFU_checkID(8) == RFU_ID)
2020-01-16 00:21:54 +01:00
{
timerReg = &REG_TMCNT(gSTWIStatus->timerSelect);
*timerReg = 0;
*timerReg = (TIMER_ENABLE | TIMER_1024CLK) << 16;
while (*timerReg << 16 < 262 << 16)
;
*timerReg = 0;
STWI_set_Callback_M(rfu_CB_stopMode);
STWI_send_StopModeREQ();
}
else
{
REG_SIOCNT = SIO_MULTI_MODE;
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(ID_STOP_MODE_REQ, 0);
2020-01-16 00:21:54 +01:00
}
}
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_stopMode(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
if (reqResult == 0)
REG_SIOCNT = SIO_MULTI_MODE;
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(reqCommand, reqResult);
2020-01-16 00:21:54 +01:00
}
u32 rfu_REQBN_softReset_and_checkID(void)
{
2020-02-16 19:49:36 +01:00
u32 id;
2020-01-16 00:21:54 +01:00
if (REG_IME == 0)
return ERR_ID_CHECK_IME_DISABLE;
AgbRFU_SoftReset();
rfu_STC_clearAPIVariables();
2020-02-16 19:49:36 +01:00
if ((id = AgbRFU_checkID(30)) == 0)
2020-01-16 00:21:54 +01:00
REG_SIOCNT = SIO_MULTI_MODE;
2020-02-16 19:49:36 +01:00
return id;
2020-01-16 00:21:54 +01:00
}
void rfu_REQ_reset(void)
{
STWI_set_Callback_M(rfu_CB_reset);
STWI_send_ResetREQ();
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_reset(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
if (reqResult == 0)
rfu_STC_clearAPIVariables();
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(reqCommand, reqResult);
2020-01-16 00:21:54 +01:00
}
void rfu_REQ_configSystem(u16 availSlotFlag, u8 maxMFrame, u8 mcTimer)
{
STWI_set_Callback_M(rfu_STC_REQ_callback);
STWI_send_SystemConfigREQ((availSlotFlag & AVAIL_SLOT1) | 0x3C, maxMFrame, mcTimer);
if (mcTimer == 0)
{
2020-02-16 19:49:36 +01:00
gRfuStatic->linkEmergencyLimit = 1;
2020-01-16 00:21:54 +01:00
}
else
{
u16 IMEBackup = REG_IME;
REG_IME = 0;
2020-02-16 19:49:36 +01:00
gRfuStatic->linkEmergencyLimit = Div(600, mcTimer);
2020-01-16 00:21:54 +01:00
REG_IME = IMEBackup;
}
2020-01-16 00:21:54 +01:00
}
void rfu_REQ_configGameData(u8 mbootFlag, u16 serialNo, const u8 *gname, const u8 *uname)
{
2020-02-16 19:49:36 +01:00
u8 packet[16];
2020-01-16 00:21:54 +01:00
u8 i;
2020-02-16 19:49:36 +01:00
u8 check_sum;
2020-01-16 00:21:54 +01:00
const u8 *gnameBackup = gname;
const u8 *unameBackup;
2020-02-16 19:49:36 +01:00
packet[0] = serialNo;
packet[1] = serialNo >> 8;
2020-01-16 00:21:54 +01:00
if (mbootFlag != 0)
2020-02-16 19:49:36 +01:00
packet[1] = (serialNo >> 8) | 0x80;
2020-01-16 00:21:54 +01:00
for (i = 2; i < 15; ++i)
2020-02-16 19:49:36 +01:00
packet[i] = *gname++;
check_sum = 0;
2020-01-16 00:21:54 +01:00
unameBackup = uname;
for (i = 0; i < 8; ++i)
{
2020-02-16 19:49:36 +01:00
check_sum += *unameBackup++;
check_sum += *gnameBackup++;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
packet[15] = ~check_sum;
2020-01-16 00:21:54 +01:00
if (mbootFlag != 0)
2020-02-16 19:49:36 +01:00
packet[14] = 0;
2020-01-16 00:21:54 +01:00
STWI_set_Callback_M(rfu_CB_configGameData);
2020-02-16 19:49:36 +01:00
STWI_send_GameConfigREQ(packet, uname);
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_configGameData(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
s32 serialNo;
u8 *gname_uname_p;
2020-01-16 00:21:54 +01:00
u8 i;
2020-02-16 19:49:36 +01:00
u8 *packet_p;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if (reqResult == 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
packet_p = gSTWIStatus->txPacket->rfuPacket8.data;
serialNo = gRfuLinkStatus->my.serialNo = packet_p[4];
gRfuLinkStatus->my.serialNo = (packet_p[5] << 8) | serialNo;
gname_uname_p = &packet_p[6];
2020-01-16 00:21:54 +01:00
if (gRfuLinkStatus->my.serialNo & 0x8000)
{
gRfuLinkStatus->my.serialNo = gRfuLinkStatus->my.serialNo ^ 0x8000;
gRfuLinkStatus->my.mbootFlag = 1;
}
else
{
gRfuLinkStatus->my.mbootFlag = 0;
}
for (i = 0; i < RFU_GAME_NAME_LENGTH; ++i)
2020-02-16 19:49:36 +01:00
gRfuLinkStatus->my.gname[i] = *gname_uname_p++;
++gname_uname_p;
2020-06-06 22:46:19 +02:00
for (i = 0; i < PLAYER_NAME_LENGTH + 1; ++i)
2020-02-16 19:49:36 +01:00
gRfuLinkStatus->my.uname[i] = *gname_uname_p++;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(reqCommand, reqResult);
2020-01-16 00:21:54 +01:00
}
void rfu_REQ_startSearchChild(void)
{
2020-02-16 19:49:36 +01:00
u16 result;
#if LIBRFU_VERSION >= 1026
u16 i;
for (i = 0; i < RFU_CHILD_MAX; i++)
{
gRfuStatic->lsFixedCount[i] = 0;
}
#endif
2020-01-16 00:21:54 +01:00
STWI_set_Callback_M(rfu_CB_defaultCallback);
STWI_send_SystemStatusREQ();
2020-02-16 19:49:36 +01:00
result = STWI_poll_CommandEnd();
if (result == 0)
2020-01-16 00:21:54 +01:00
{
if (gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[7] == 0)
2020-02-16 19:49:36 +01:00
rfu_STC_clearLinkStatus(MODE_PARENT);
2020-01-16 00:21:54 +01:00
}
else
{
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(ID_SC_START_REQ, result);
2020-01-16 00:21:54 +01:00
}
STWI_set_Callback_M(rfu_CB_startSearchChild);
STWI_send_SC_StartREQ();
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_startSearchChild(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
if (reqResult == 0)
gRfuStatic->SCStartFlag = 1;
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(reqCommand, reqResult);
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
static void rfu_STC_clearLinkStatus(u8 parentChild)
2020-01-16 00:21:54 +01:00
{
u8 i;
2020-02-16 19:49:36 +01:00
2020-01-16 00:21:54 +01:00
rfu_clearAllSlot();
2020-02-16 19:49:36 +01:00
if (parentChild != MODE_CHILD)
2020-01-16 00:21:54 +01:00
{
CpuFill16(0, gRfuLinkStatus->partner, sizeof(gRfuLinkStatus->partner));
gRfuLinkStatus->findParentCount = 0;
}
for (i = 0; i < RFU_CHILD_MAX; ++i)
gRfuLinkStatus->strength[i] = 0;
gRfuLinkStatus->connCount = 0;
gRfuLinkStatus->connSlotFlag = 0;
gRfuLinkStatus->linkLossSlotFlag = 0;
gRfuLinkStatus->getNameFlag = 0;
}
void rfu_REQ_pollSearchChild(void)
{
STWI_set_Callback_M(rfu_CB_pollAndEndSearchChild);
STWI_send_SC_PollingREQ();
}
2020-01-16 00:21:54 +01:00
void rfu_REQ_endSearchChild(void)
{
STWI_set_Callback_M(rfu_CB_pollAndEndSearchChild);
STWI_send_SC_EndREQ();
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_pollAndEndSearchChild(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
if (reqResult == 0)
rfu_STC_readChildList();
2020-02-16 19:49:36 +01:00
if (reqCommand == ID_SC_POLL_REQ)
{
2020-01-16 00:21:54 +01:00
if (gRfuLinkStatus->my.id == 0)
{
STWI_set_Callback_M(rfu_CB_defaultCallback);
STWI_send_SystemStatusREQ();
if (STWI_poll_CommandEnd() == 0)
gRfuLinkStatus->my.id = *(u16 *)&gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket32.data[0];
}
}
2020-02-16 19:49:36 +01:00
else if (reqCommand == ID_SC_END_REQ)
2020-01-16 00:21:54 +01:00
{
if (gRfuLinkStatus->parentChild == MODE_NEUTRAL)
gRfuLinkStatus->my.id = 0;
gRfuStatic->SCStartFlag = 0;
}
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(reqCommand, reqResult);
2020-01-16 00:21:54 +01:00
}
2020-01-16 00:21:54 +01:00
static void rfu_STC_readChildList(void)
{
2020-02-16 19:49:36 +01:00
u32 stwiParam;
u8 numSlots = gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[1];
u8 *data_p;
2020-01-16 00:21:54 +01:00
u8 i;
2020-02-16 19:49:36 +01:00
u8 bm_slot_id;
#if LIBRFU_VERSION < 1026
u8 true_slots[RFU_CHILD_MAX];
#endif
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
#if LIBRFU_VERSION < 1026
if (numSlots != 0)
{
2020-02-16 19:49:36 +01:00
stwiParam = gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket32.data[0];
STWI_set_Callback_M(rfu_CB_defaultCallback);
STWI_send_LinkStatusREQ();
if (STWI_poll_CommandEnd() == 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
data_p = &gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[4];
for (i = 0; i < RFU_CHILD_MAX; ++i)
true_slots[i] = *data_p++;
}
gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket32.data[0] = stwiParam;
}
#endif
for (data_p = &gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[4];
numSlots != 0;
data_p += 4)
{
bm_slot_id = data_p[2];
if (bm_slot_id < RFU_CHILD_MAX && !((gRfuLinkStatus->connSlotFlag >> bm_slot_id) & 1) && !((gRfuLinkStatus->linkLossSlotFlag >> bm_slot_id) & 1))
{
#if LIBRFU_VERSION < 1026
if (true_slots[bm_slot_id] != 0)
++gRfuStatic->lsFixedCount[bm_slot_id];
if (gRfuStatic->lsFixedCount[bm_slot_id] >= 4)
{
gRfuStatic->lsFixedCount[bm_slot_id] = 0;
gRfuLinkStatus->strength[bm_slot_id] = 255;
gRfuLinkStatus->connSlotFlag |= 1 << bm_slot_id;
++gRfuLinkStatus->connCount;
gRfuLinkStatus->partner[bm_slot_id].id = *(u16 *)data_p;
gRfuLinkStatus->partner[bm_slot_id].slot = bm_slot_id;
gRfuLinkStatus->parentChild = MODE_PARENT;
gRfuStatic->flags &= 0x7F;
gRfuStatic->cidBak[bm_slot_id] = gRfuLinkStatus->partner[bm_slot_id].id;
}
#else
gRfuStatic->lsFixedCount[bm_slot_id] = 0xF0;
gRfuLinkStatus->strength[bm_slot_id] = 16;
gRfuLinkStatus->connSlotFlag |= 1 << bm_slot_id;
2020-01-16 00:21:54 +01:00
++gRfuLinkStatus->connCount;
2020-02-16 19:49:36 +01:00
gRfuLinkStatus->partner[bm_slot_id].id = *(u16 *)data_p;
gRfuLinkStatus->partner[bm_slot_id].slot = bm_slot_id;
2020-01-16 00:21:54 +01:00
gRfuLinkStatus->parentChild = MODE_PARENT;
gRfuStatic->flags &= 0x7F;
2020-02-16 19:49:36 +01:00
gRfuStatic->cidBak[bm_slot_id] = gRfuLinkStatus->partner[bm_slot_id].id;
#endif
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
--numSlots;
}
2020-01-16 00:21:54 +01:00
}
void rfu_REQ_startSearchParent(void)
{
STWI_set_Callback_M(rfu_CB_startSearchParent);
STWI_send_SP_StartREQ();
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_startSearchParent(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
if (reqResult == 0)
2020-02-16 19:49:36 +01:00
rfu_STC_clearLinkStatus(MODE_CHILD);
rfu_STC_REQ_callback(reqCommand, reqResult);
2020-01-16 00:21:54 +01:00
}
void rfu_REQ_pollSearchParent(void)
{
STWI_set_Callback_M(rfu_CB_pollSearchParent);
STWI_send_SP_PollingREQ();
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_pollSearchParent(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
if (reqResult == 0)
rfu_STC_readParentCandidateList();
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(reqCommand, reqResult);
2020-01-16 00:21:54 +01:00
}
void rfu_REQ_endSearchParent(void)
{
STWI_set_Callback_M(rfu_STC_REQ_callback);
STWI_send_SP_EndREQ();
}
static void rfu_STC_readParentCandidateList(void)
{
2020-02-16 19:49:36 +01:00
u8 numSlots, i, check_sum, my_check_sum, j;
u8 *uname_p, *packet_p;
struct RfuTgtData *target;
2020-01-16 00:21:54 +01:00
CpuFill16(0, gRfuLinkStatus->partner, sizeof(gRfuLinkStatus->partner));
2020-02-16 19:49:36 +01:00
packet_p = &gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[0];
numSlots = packet_p[1];
packet_p += 4;
2020-01-16 00:21:54 +01:00
gRfuLinkStatus->findParentCount = 0;
2020-02-16 19:49:36 +01:00
for (i = 0; i < RFU_CHILD_MAX && numSlots != 0; ++i)
{
numSlots -= 7;
uname_p = packet_p + 6;
packet_p += 19;
check_sum = ~*packet_p;
++packet_p;
my_check_sum = 0;
for (j = 0; j < 8; ++j)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
my_check_sum += *packet_p++;
my_check_sum += *uname_p++;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
if (my_check_sum == check_sum)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
packet_p -= 28;
target = &gRfuLinkStatus->partner[gRfuLinkStatus->findParentCount];
target->id = *(u16 *)packet_p;
packet_p += 2;
target->slot = *packet_p;
packet_p += 2;
target->serialNo = *(u16 *)packet_p & 0x7FFF;
if (*(u16 *)packet_p & 0x8000)
target->mbootFlag = 1;
2020-01-16 00:21:54 +01:00
else
2020-02-16 19:49:36 +01:00
target->mbootFlag = 0;
packet_p += 2;
for (j = 0; j < RFU_GAME_NAME_LENGTH; ++j)
target->gname[j] = *packet_p++;
++packet_p;
2020-06-06 22:46:19 +02:00
for (j = 0; j < PLAYER_NAME_LENGTH + 1; ++j)
2020-02-16 19:49:36 +01:00
target->uname[j] = *packet_p++;
2020-01-16 00:21:54 +01:00
++gRfuLinkStatus->findParentCount;
}
}
}
void rfu_REQ_startConnectParent(u16 pid)
{
2020-02-16 19:49:36 +01:00
u16 result = 0;
2020-01-16 00:21:54 +01:00
u8 i;
for (i = 0; i < RFU_CHILD_MAX && gRfuLinkStatus->partner[i].id != pid; ++i)
;
2020-02-16 19:49:36 +01:00
if (i == RFU_CHILD_MAX)
result = ERR_PID_NOT_FOUND;
if (result == 0)
2020-01-16 00:21:54 +01:00
{
gRfuStatic->tryPid = pid;
STWI_set_Callback_M(rfu_STC_REQ_callback);
STWI_send_CP_StartREQ(pid);
}
else
{
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(ID_CP_START_REQ, result);
2020-01-16 00:21:54 +01:00
}
}
void rfu_REQ_pollConnectParent(void)
{
STWI_set_Callback_M(rfu_CB_pollConnectParent);
STWI_send_CP_PollingREQ();
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_pollConnectParent(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
u16 id;
u8 slot;
2020-02-16 19:49:36 +01:00
u8 bm_slot_flag, i;
struct RfuTgtData *target_p;
struct RfuTgtData target_local;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if (reqResult == 0)
2020-01-16 00:21:54 +01:00
{
id = gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket32.data[0];
slot = gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[6];
if (gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[7] == 0)
{
2020-02-16 19:49:36 +01:00
bm_slot_flag = 1 << slot;
if (!(bm_slot_flag & gRfuLinkStatus->connSlotFlag))
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
gRfuLinkStatus->connSlotFlag |= bm_slot_flag;
gRfuLinkStatus->linkLossSlotFlag &= ~bm_slot_flag;
2020-01-16 00:21:54 +01:00
gRfuLinkStatus->my.id = id;
++gRfuLinkStatus->connCount;
gRfuLinkStatus->parentChild = MODE_CHILD;
gRfuStatic->flags |= 0x80;
2020-02-16 19:49:36 +01:00
for (i = 0; i < RFU_CHILD_MAX; ++i)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (gRfuLinkStatus->partner[i].id == gRfuStatic->tryPid)
2020-01-16 00:21:54 +01:00
{
if (gRfuLinkStatus->findParentCount != 0)
{
2020-02-16 19:49:36 +01:00
target_p = &target_local;
CpuCopy16(&gRfuLinkStatus->partner[i], &target_local, sizeof(struct RfuTgtData));
2020-01-16 00:21:54 +01:00
CpuFill16(0, gRfuLinkStatus->partner, sizeof(gRfuLinkStatus->partner));
gRfuLinkStatus->findParentCount = 0;
}
else
{
2020-02-16 19:49:36 +01:00
target_p = &gRfuLinkStatus->partner[i];
2020-01-16 00:21:54 +01:00
}
break;
}
}
2020-02-16 19:49:36 +01:00
if (i < RFU_CHILD_MAX)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
CpuCopy16(target_p, &gRfuLinkStatus->partner[slot], sizeof(struct RfuTgtData));
2020-01-16 00:21:54 +01:00
gRfuLinkStatus->partner[slot].slot = slot;
}
}
}
}
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(reqCommand, reqResult);
2020-01-16 00:21:54 +01:00
}
u16 rfu_getConnectParentStatus(u8 *status, u8 *connectSlotNo)
{
2020-02-16 19:49:36 +01:00
u8 *packet_p;
2020-01-16 00:21:54 +01:00
*status = 0xFF;
2020-02-16 19:49:36 +01:00
packet_p = gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data;
if (packet_p[0] == 0xa0 || packet_p[0] == 0xa1)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
packet_p += 6;
*connectSlotNo = packet_p[0];
*status = packet_p[1];
2020-01-16 00:21:54 +01:00
return 0;
}
return ERR_REQ_CMD_ID;
}
void rfu_REQ_endConnectParent(void)
{
STWI_set_Callback_M(rfu_CB_pollConnectParent);
STWI_send_CP_EndREQ();
if (gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[6] < 4)
gRfuStatic->linkEmergencyFlag[gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[6]] = 0;
}
u16 rfu_syncVBlank(void)
{
2020-02-16 19:49:36 +01:00
u8 masterSlave, i;
s32 bmSlotFlag;
2020-01-16 00:21:54 +01:00
rfu_NI_checkCommFailCounter();
if (gRfuLinkStatus->parentChild == MODE_NEUTRAL)
return 0;
if (gRfuStatic->nowWatchInterval != 0)
--gRfuStatic->nowWatchInterval;
2020-02-16 19:49:36 +01:00
masterSlave = rfu_getMasterSlave();
2020-01-16 00:21:54 +01:00
if (!(gRfuStatic->flags & 2))
{
2020-02-16 19:49:36 +01:00
if (masterSlave == AGB_CLK_SLAVE)
2020-01-16 00:21:54 +01:00
{
gRfuStatic->flags |= 4;
gRfuStatic->watchdogTimer = 360;
}
}
2020-02-16 19:49:36 +01:00
else if (masterSlave != AGB_CLK_SLAVE)
2020-01-16 00:21:54 +01:00
{
gRfuStatic->flags &= 0xFB;
}
2020-02-16 19:49:36 +01:00
if (masterSlave != AGB_CLK_SLAVE)
2020-01-16 00:21:54 +01:00
gRfuStatic->flags &= 0xFD;
else
gRfuStatic->flags |= 2;
if (!(gRfuStatic->flags & 4))
return 0;
if (gRfuStatic->watchdogTimer == 0)
{
gRfuStatic->flags &= 0xFB;
2020-02-16 19:49:36 +01:00
bmSlotFlag = gRfuLinkStatus->connSlotFlag | gRfuLinkStatus->linkLossSlotFlag;
for (i = 0; i < RFU_CHILD_MAX; ++i)
if ((bmSlotFlag >> i) & 1)
rfu_STC_removeLinkData(i, 1);
2020-01-16 00:21:54 +01:00
gRfuLinkStatus->parentChild = MODE_NEUTRAL;
return 1;
}
--gRfuStatic->watchdogTimer;
return 0;
}
u16 rfu_REQBN_watchLink(u16 reqCommandId, u8 *bmLinkLossSlot, u8 *linkLossReason, u8 *parentBmLinkRecoverySlot)
{
2020-02-16 19:49:36 +01:00
u8 reasonMaybe = 0;
u8 reqResult = 0;
2020-01-16 00:21:54 +01:00
u8 i;
2020-02-16 19:49:36 +01:00
s32 stwiCommand, stwiParam;
u8 *packet_p;
u8 to_req_disconnect, newLinkLossFlag, num_packets, connSlotFlag, to_disconnect;
2020-01-16 00:21:54 +01:00
*bmLinkLossSlot = 0;
*linkLossReason = REASON_DISCONNECTED;
*parentBmLinkRecoverySlot = 0;
if (gRfuLinkStatus->parentChild == MODE_NEUTRAL || gSTWIStatus->msMode == 0)
return 0;
if (gRfuStatic->flags & 4)
gRfuStatic->watchdogTimer = 360;
if (gRfuStatic->nowWatchInterval == 0)
{
2020-02-16 19:49:36 +01:00
#if LIBRFU_VERSION < 1026
gRfuStatic->nowWatchInterval = gRfuLinkStatus->watchInterval;
#else
2020-01-16 00:21:54 +01:00
gRfuStatic->nowWatchInterval = 4;
2020-02-16 19:49:36 +01:00
#endif
reasonMaybe = 1;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
if ((u8)reqCommandId == ID_DISCONNECTED_AND_CHANGE_REQ)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u8 *packet_p_2 = gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data;
*bmLinkLossSlot = packet_p_2[4];
*linkLossReason = packet_p_2[5];
2020-01-16 00:21:54 +01:00
if (*linkLossReason == REASON_LINK_LOSS)
*bmLinkLossSlot = gRfuLinkStatus->connSlotFlag;
2020-02-16 19:49:36 +01:00
reasonMaybe = 2;
2020-01-16 00:21:54 +01:00
}
else
{
2020-02-16 19:49:36 +01:00
if (reqCommandId == 0x0136)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
newLinkLossFlag = gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[5];
newLinkLossFlag ^= gRfuLinkStatus->connSlotFlag;
*bmLinkLossSlot = newLinkLossFlag & gRfuLinkStatus->connSlotFlag;
2020-01-16 00:21:54 +01:00
*linkLossReason = REASON_LINK_LOSS;
for (i = 0; i < RFU_CHILD_MAX; ++i)
{
if ((*bmLinkLossSlot >> i) & 1)
{
gRfuLinkStatus->strength[i] = 0;
rfu_STC_removeLinkData(i, 0);
}
}
}
2020-02-16 19:49:36 +01:00
if (reasonMaybe == 0)
2020-01-16 00:21:54 +01:00
return 0;
}
2020-02-16 19:49:36 +01:00
stwiCommand = gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket32.command;
stwiParam = gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket32.data[0];
2020-01-16 00:21:54 +01:00
STWI_set_Callback_M(rfu_CB_defaultCallback);
STWI_send_LinkStatusREQ();
2020-02-16 19:49:36 +01:00
reqResult = STWI_poll_CommandEnd();
if (reqResult == 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
packet_p = &gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[4];
2020-01-16 00:21:54 +01:00
for (i = 0; i < RFU_CHILD_MAX; ++i)
2020-02-16 19:49:36 +01:00
gRfuLinkStatus->strength[i] = *packet_p++;
to_req_disconnect = 0;
2020-01-16 00:21:54 +01:00
i = 0;
}
else
{
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(ID_LINK_STATUS_REQ, reqResult);
return reqResult;
2020-01-16 00:21:54 +01:00
}
for (; i < RFU_CHILD_MAX; ++i)
{
2020-02-16 19:49:36 +01:00
#if LIBRFU_VERSION >= 1026
2020-01-16 00:21:54 +01:00
if (gRfuStatic->lsFixedCount[i] != 0)
{
gRfuStatic->lsFixedCount[i] -= 4;
if (gRfuLinkStatus->strength[i] <= 15)
gRfuLinkStatus->strength[i] = 16;
}
2020-02-16 19:49:36 +01:00
#endif
newLinkLossFlag = 1 << i;
if (reqResult == 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (reasonMaybe == 1 && (gRfuLinkStatus->connSlotFlag & newLinkLossFlag))
2020-01-16 00:21:54 +01:00
{
if (gRfuLinkStatus->strength[i] == 0)
{
if (gRfuLinkStatus->parentChild == MODE_PARENT)
{
++gRfuStatic->linkEmergencyFlag[i];
if (gRfuStatic->linkEmergencyFlag[i] > 3)
{
2020-02-16 19:49:36 +01:00
*bmLinkLossSlot |= newLinkLossFlag;
*linkLossReason = REASON_LINK_LOSS;
2020-01-16 00:21:54 +01:00
}
}
else
{
STWI_send_SystemStatusREQ();
if (STWI_poll_CommandEnd() == 0)
{
if (gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[7] == 0)
{
2020-02-16 19:49:36 +01:00
*bmLinkLossSlot |= newLinkLossFlag;
*linkLossReason = REASON_LINK_LOSS;
2020-01-16 00:21:54 +01:00
}
else
{
2020-02-16 19:49:36 +01:00
if (++gRfuStatic->linkEmergencyFlag[i] > gRfuStatic->linkEmergencyLimit)
2020-01-16 00:21:54 +01:00
{
gRfuStatic->linkEmergencyFlag[i] = 0;
STWI_send_DisconnectREQ(gRfuLinkStatus->connSlotFlag);
STWI_poll_CommandEnd();
2020-02-16 19:49:36 +01:00
*bmLinkLossSlot |= newLinkLossFlag;
*linkLossReason = REASON_LINK_LOSS;
2020-01-16 00:21:54 +01:00
}
}
}
}
}
else
{
2020-02-16 19:49:36 +01:00
gRfuStatic->linkEmergencyFlag[i] = 0;
2020-01-16 00:21:54 +01:00
}
}
if (gRfuLinkStatus->parentChild == MODE_PARENT && gRfuLinkStatus->strength[i] != 0)
{
2020-02-16 19:49:36 +01:00
if (newLinkLossFlag & gRfuLinkStatus->linkLossSlotFlag)
2020-01-16 00:21:54 +01:00
{
if (gRfuLinkStatus->strength[i] > 10)
{
2020-02-16 19:49:36 +01:00
*parentBmLinkRecoverySlot |= newLinkLossFlag;
gRfuLinkStatus->connSlotFlag |= newLinkLossFlag;
gRfuLinkStatus->linkLossSlotFlag &= ~newLinkLossFlag;
2020-01-16 00:21:54 +01:00
++gRfuLinkStatus->connCount;
gRfuStatic->linkEmergencyFlag[i] = 0;
}
else
{
gRfuLinkStatus->strength[i] = 0;
}
}
else
{
2020-02-16 19:49:36 +01:00
if (!((gRfuLinkStatus->connSlotFlag | gRfuLinkStatus->linkLossSlotFlag) & newLinkLossFlag))
2020-01-16 00:21:54 +01:00
{
STWI_send_SlotStatusREQ();
STWI_poll_CommandEnd();
2020-02-16 19:49:36 +01:00
packet_p = gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data;
num_packets = packet_p[1] - 1;
for (packet_p += 8; num_packets != 0; packet_p += 4, --num_packets)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u16 cid = *(u16 *)packet_p;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if (packet_p[2] == i && cid == gRfuStatic->cidBak[i])
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
to_req_disconnect |= 1 << i;
2020-01-16 00:21:54 +01:00
break;
}
}
}
}
}
}
connSlotFlag = gRfuLinkStatus->connSlotFlag;
2020-02-16 19:49:36 +01:00
to_disconnect = *bmLinkLossSlot;
to_disconnect &= connSlotFlag;
if (newLinkLossFlag & to_disconnect)
2020-01-16 00:21:54 +01:00
rfu_STC_removeLinkData(i, 0);
}
2020-02-16 19:49:36 +01:00
if (to_req_disconnect != 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
STWI_send_DisconnectREQ(to_req_disconnect);
2020-01-16 00:21:54 +01:00
STWI_poll_CommandEnd();
}
// equivalent to:
2020-02-16 19:49:36 +01:00
// gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket32.command = stwiCommand;
*(u32 *)gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data = stwiCommand;
gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket32.data[0] = stwiParam;
2020-01-16 00:21:54 +01:00
return 0;
}
2020-02-16 19:49:36 +01:00
static void rfu_STC_removeLinkData(u8 bmConnectedPartnerId, u8 bmDisconnect)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u8 bmLinkLossFlag = 1 << bmConnectedPartnerId;
s32 bmLinkRetainedFlag;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
#if LIBRFU_VERSION >= 1026
gRfuStatic->lsFixedCount[bmConnectedPartnerId] = 0;
#endif
if ((gRfuLinkStatus->connSlotFlag & bmLinkLossFlag) && gRfuLinkStatus->connCount != 0)
2020-01-16 00:21:54 +01:00
--gRfuLinkStatus->connCount;
2020-02-16 19:49:36 +01:00
gRfuLinkStatus->connSlotFlag &= bmLinkRetainedFlag = ~bmLinkLossFlag;
gRfuLinkStatus->linkLossSlotFlag |= bmLinkLossFlag;
if (gRfuLinkStatus->parentChild == MODE_CHILD && gRfuLinkStatus->connSlotFlag == 0)
2020-01-16 00:21:54 +01:00
gRfuLinkStatus->parentChild = MODE_NEUTRAL;
2020-02-16 19:49:36 +01:00
if (bmDisconnect)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
CpuFill16(0, &gRfuLinkStatus->partner[bmConnectedPartnerId], sizeof(struct RfuTgtData));
gRfuLinkStatus->linkLossSlotFlag &= bmLinkRetainedFlag;
gRfuLinkStatus->getNameFlag &= bmLinkRetainedFlag;
gRfuLinkStatus->strength[bmConnectedPartnerId] = 0;
2020-01-16 00:21:54 +01:00
}
}
void rfu_REQ_disconnect(u8 bmDisconnectSlot)
{
2020-02-16 19:49:36 +01:00
u16 result;
2020-01-16 00:21:54 +01:00
if ((gRfuLinkStatus->connSlotFlag | gRfuLinkStatus->linkLossSlotFlag) & bmDisconnectSlot)
{
gRfuStatic->recoveryBmSlot = bmDisconnectSlot;
if (gRfuLinkStatus->parentChild == MODE_NEUTRAL && gRfuStatic->flags & 0x80)
{
if (gRfuLinkStatus->linkLossSlotFlag & bmDisconnectSlot)
rfu_CB_disconnect(48, 0);
}
else if (gRfuStatic->SCStartFlag
&& (STWI_set_Callback_M(rfu_CB_defaultCallback),
STWI_send_SC_EndREQ(),
2020-02-16 19:49:36 +01:00
(result = STWI_poll_CommandEnd()) != 0))
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(ID_SC_END_REQ, result);
2020-01-16 00:21:54 +01:00
}
else
{
STWI_set_Callback_M(rfu_CB_disconnect);
STWI_send_DisconnectREQ(bmDisconnectSlot);
}
}
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_disconnect(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u8 i, bm_slot_flag;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if (reqResult == 3 && gRfuLinkStatus->parentChild == MODE_CHILD)
2020-01-16 00:21:54 +01:00
{
STWI_set_Callback_M(rfu_CB_defaultCallback);
STWI_send_SystemStatusREQ();
if (STWI_poll_CommandEnd() == 0 && gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[7] == 0)
2020-02-16 19:49:36 +01:00
reqResult = 0;
2020-01-16 00:21:54 +01:00
}
gRfuStatic->recoveryBmSlot &= gRfuLinkStatus->connSlotFlag | gRfuLinkStatus->linkLossSlotFlag;
gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[8] = gRfuStatic->recoveryBmSlot;
2020-02-16 19:49:36 +01:00
if (reqResult == 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
for (i = 0; i < RFU_CHILD_MAX; ++i)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
bm_slot_flag = 1 << i;
if (bm_slot_flag & gRfuStatic->recoveryBmSlot)
rfu_STC_removeLinkData(i, 1);
2020-01-16 00:21:54 +01:00
}
}
if ((gRfuLinkStatus->connSlotFlag | gRfuLinkStatus->linkLossSlotFlag) == 0)
gRfuLinkStatus->parentChild = MODE_NEUTRAL;
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(reqCommand, reqResult);
2020-01-16 00:21:54 +01:00
if (gRfuStatic->SCStartFlag)
{
STWI_set_Callback_M(rfu_CB_defaultCallback);
STWI_send_SC_StartREQ();
2020-02-16 19:49:36 +01:00
reqResult = STWI_poll_CommandEnd();
if (reqResult != 0)
rfu_STC_REQ_callback(ID_SC_START_REQ, reqResult);
2020-01-16 00:21:54 +01:00
}
}
void rfu_REQ_CHILD_startConnectRecovery(u8 bmRecoverySlot)
{
u8 i;
gRfuStatic->recoveryBmSlot = bmRecoverySlot;
for (i = 0; i < RFU_CHILD_MAX && !((bmRecoverySlot >> i) & 1); ++i)
;
STWI_set_Callback_M(rfu_STC_REQ_callback);
// if i == 4, gRfuLinkStatus->partner[i].id becomes gRfuLinkStatus->my.id
STWI_send_CPR_StartREQ(gRfuLinkStatus->partner[i].id, gRfuLinkStatus->my.id, bmRecoverySlot);
}
void rfu_REQ_CHILD_pollConnectRecovery(void)
{
STWI_set_Callback_M(rfu_CB_CHILD_pollConnectRecovery);
STWI_send_CPR_PollingREQ();
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_CHILD_pollConnectRecovery(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u8 bm_slot_flag, i;
struct RfuLinkStatus *rfuLinkStatus;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if (reqResult == 0 && gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[4] == 0 && gRfuStatic->recoveryBmSlot)
2020-01-16 00:21:54 +01:00
{
gRfuLinkStatus->parentChild = MODE_CHILD;
2020-02-16 19:49:36 +01:00
for (i = 0; i < RFU_CHILD_MAX; ++i)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
bm_slot_flag = 1 << i;
rfuLinkStatus = gRfuLinkStatus; // ???
if (gRfuStatic->recoveryBmSlot & bm_slot_flag & rfuLinkStatus->linkLossSlotFlag)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
gRfuLinkStatus->connSlotFlag |= bm_slot_flag;
gRfuLinkStatus->linkLossSlotFlag &= ~bm_slot_flag;
2020-01-16 00:21:54 +01:00
++gRfuLinkStatus->connCount;
2020-02-16 19:49:36 +01:00
gRfuStatic->linkEmergencyFlag[i] = 0;
2020-01-16 00:21:54 +01:00
}
}
gRfuStatic->recoveryBmSlot = 0;
}
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(reqCommand, reqResult);
2020-01-16 00:21:54 +01:00
}
u16 rfu_CHILD_getConnectRecoveryStatus(u8 *status)
{
*status = 0xFF;
2020-02-16 19:49:36 +01:00
if (gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[0] == 0xB3 || gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[0] == 0xB4)
2020-01-16 00:21:54 +01:00
{
*status = gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[4];
return 0;
}
return ERR_REQ_CMD_ID;
}
void rfu_REQ_CHILD_endConnectRecovery(void)
{
STWI_set_Callback_M(rfu_CB_CHILD_pollConnectRecovery);
STWI_send_CPR_EndREQ();
}
static void rfu_STC_fastCopy(const u8 **src_p, u8 **dst_p, s32 size)
{
const u8 *src = *src_p;
u8 *dst = *dst_p;
s32 i;
for (i = size - 1; i != -1; --i)
*dst++ = *src++;
*src_p = src;
*dst_p = dst;
}
void rfu_REQ_changeMasterSlave(void)
{
2020-02-16 19:49:36 +01:00
if (STWI_read_status(1) == AGB_CLK_MASTER)
2020-01-16 00:21:54 +01:00
{
STWI_set_Callback_M(rfu_STC_REQ_callback);
STWI_send_MS_ChangeREQ();
}
else
{
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(ID_MS_CHANGE_REQ, 0);
2020-01-16 00:21:54 +01:00
}
}
bool8 rfu_getMasterSlave(void)
{
bool8 masterSlave = STWI_read_status(1);
if (masterSlave == AGB_CLK_MASTER)
{
2020-02-16 19:49:36 +01:00
if (gSTWIStatus->sending)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (gSTWIStatus->reqActiveCommand == ID_MS_CHANGE_REQ
|| gSTWIStatus->reqActiveCommand == ID_DATA_TX_AND_CHANGE_REQ
|| gSTWIStatus->reqActiveCommand == ID_RESUME_RETRANSMIT_AND_CHANGE_REQ)
2020-01-16 00:21:54 +01:00
masterSlave = AGB_CLK_SLAVE;
}
}
return masterSlave;
}
void rfu_clearAllSlot(void)
{
u16 i;
u16 IMEBackup = REG_IME;
2020-02-16 19:49:36 +01:00
2020-01-16 00:21:54 +01:00
REG_IME = 0;
for (i = 0; i < RFU_CHILD_MAX; ++i)
{
CpuFill16(0, gRfuSlotStatusNI[i], 2 * sizeof(struct NIComm));
CpuFill16(0, gRfuSlotStatusUNI[i], sizeof(struct UNISend) + sizeof(struct UNIRecv));
gRfuLinkStatus->remainLLFrameSizeChild[i] = 16;
}
gRfuLinkStatus->remainLLFrameSizeParent = LLF_P_SIZE;
gRfuLinkStatus->sendSlotNIFlag = 0;
gRfuLinkStatus->recvSlotNIFlag = 0;
gRfuLinkStatus->sendSlotUNIFlag = 0;
gRfuStatic->recvRenewalFlag = 0;
REG_IME = IMEBackup;
}
2020-02-16 19:49:36 +01:00
static void rfu_STC_releaseFrame(u8 bm_slot_id, u8 send_recv, struct NIComm *NI_comm)
2020-01-16 00:21:54 +01:00
{
if (!(gRfuStatic->flags & 0x80))
{
2020-02-16 19:49:36 +01:00
if (send_recv == 0)
gRfuLinkStatus->remainLLFrameSizeParent += NI_comm->payloadSize;
2020-01-16 00:21:54 +01:00
gRfuLinkStatus->remainLLFrameSizeParent += 3;
}
else
{
2020-02-16 19:49:36 +01:00
if (send_recv == 0)
gRfuLinkStatus->remainLLFrameSizeChild[bm_slot_id] += NI_comm->payloadSize;
gRfuLinkStatus->remainLLFrameSizeChild[bm_slot_id] += 2;
2020-01-16 00:21:54 +01:00
}
}
u16 rfu_clearSlot(u8 connTypeFlag, u8 slotStatusIndex)
{
2020-02-16 19:49:36 +01:00
u16 imeBak, send_recv, i;
struct NIComm *NI_comm;
2020-01-16 00:21:54 +01:00
if (slotStatusIndex >= RFU_CHILD_MAX)
return ERR_SLOT_NO;
if (!(connTypeFlag & (TYPE_UNI_SEND | TYPE_UNI_RECV | TYPE_NI_SEND | TYPE_NI_RECV)))
return ERR_COMM_TYPE;
2020-02-16 19:49:36 +01:00
imeBak = REG_IME;
2020-01-16 00:21:54 +01:00
REG_IME = 0;
if (connTypeFlag & (TYPE_NI_SEND | TYPE_NI_RECV))
{
2020-02-16 19:49:36 +01:00
for (send_recv = 0; send_recv < 2; ++send_recv)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
NI_comm = NULL;
if (send_recv == 0)
2020-01-16 00:21:54 +01:00
{
if (connTypeFlag & TYPE_NI_SEND)
{
2020-02-16 19:49:36 +01:00
NI_comm = &gRfuSlotStatusNI[slotStatusIndex]->send;
gRfuLinkStatus->sendSlotNIFlag &= ~NI_comm->bmSlotOrg;
2020-01-16 00:21:54 +01:00
}
}
else
{
if (connTypeFlag & TYPE_NI_RECV)
{
2020-02-16 19:49:36 +01:00
NI_comm = &gRfuSlotStatusNI[slotStatusIndex]->recv;
2020-01-16 00:21:54 +01:00
gRfuLinkStatus->recvSlotNIFlag &= ~(1 << slotStatusIndex);
}
}
2020-02-16 19:49:36 +01:00
if (NI_comm != NULL)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (NI_comm->state & SLOT_BUSY_FLAG)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
rfu_STC_releaseFrame(slotStatusIndex, send_recv, NI_comm);
for (i = 0; i < RFU_CHILD_MAX; ++i)
if ((NI_comm->bmSlotOrg >> i) & 1)
NI_comm->failCounter = 0;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
CpuFill16(0, NI_comm, sizeof(struct NIComm));
2020-01-16 00:21:54 +01:00
}
}
}
if (connTypeFlag & TYPE_UNI_SEND)
{
2020-02-16 19:49:36 +01:00
struct RfuSlotStatusUNI *slotStatusUNI = gRfuSlotStatusUNI[slotStatusIndex];
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if (slotStatusUNI->send.state & SLOT_BUSY_FLAG)
2020-01-16 00:21:54 +01:00
{
if (!(gRfuStatic->flags & 0x80))
2020-02-16 19:49:36 +01:00
gRfuLinkStatus->remainLLFrameSizeParent += 3 + (u8)slotStatusUNI->send.payloadSize;
2020-01-16 00:21:54 +01:00
else
2020-02-16 19:49:36 +01:00
gRfuLinkStatus->remainLLFrameSizeChild[slotStatusIndex] += 2 + (u8)slotStatusUNI->send.payloadSize;
gRfuLinkStatus->sendSlotUNIFlag &= ~slotStatusUNI->send.bmSlot;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
CpuFill16(0, &slotStatusUNI->send, sizeof(struct UNISend));
2020-01-16 00:21:54 +01:00
}
if (connTypeFlag & TYPE_UNI_RECV)
{
CpuFill16(0, &gRfuSlotStatusUNI[slotStatusIndex]->recv, sizeof(struct UNIRecv));
}
2020-02-16 19:49:36 +01:00
REG_IME = imeBak;
2020-01-16 00:21:54 +01:00
return 0;
}
u16 rfu_setRecvBuffer(u8 connType, u8 slotNo, void *buffer, u32 buffSize)
{
if (slotNo >= RFU_CHILD_MAX)
return ERR_SLOT_NO;
if (connType & TYPE_NI)
{
gRfuSlotStatusNI[slotNo]->recvBuffer = buffer;
gRfuSlotStatusNI[slotNo]->recvBufferSize = buffSize;
}
else if (!(connType & TYPE_UNI))
{
return ERR_COMM_TYPE;
}
else
{
gRfuSlotStatusUNI[slotNo]->recvBuffer = buffer;
gRfuSlotStatusUNI[slotNo]->recvBufferSize = buffSize;
}
return 0;
}
u16 rfu_NI_setSendData(u8 bmSendSlot, u8 subFrameSize, const void *src, u32 size)
{
return rfu_STC_setSendData_org(32, bmSendSlot, subFrameSize, src, size);
}
u16 rfu_UNI_setSendData(u8 bmSendSlot, const void *src, u8 size)
{
u8 subFrameSize;
if (gRfuLinkStatus->parentChild == MODE_PARENT)
subFrameSize = size + 3;
else
subFrameSize = size + 2;
return rfu_STC_setSendData_org(16, bmSendSlot, subFrameSize, src, 0);
}
u16 rfu_NI_CHILD_setSendGameName(u8 slotNo, u8 subFrameSize)
{
return rfu_STC_setSendData_org(64, 1 << slotNo, subFrameSize, &gRfuLinkStatus->my.serialNo, 26);
}
2020-02-16 19:49:36 +01:00
static u16 rfu_STC_setSendData_org(u8 ni_or_uni, u8 bmSendSlot, u8 subFrameSize, const void *src, u32 dataSize)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u8 bm_slot_id, sendSlotFlag;
u8 frameSize;
u8 *llFrameSize_p;
u8 sending;
2020-01-16 00:21:54 +01:00
u8 i;
2020-02-16 19:49:36 +01:00
u16 imeBak;
struct RfuSlotStatusUNI *slotStatus_UNI;
struct RfuSlotStatusNI *slotStatus_NI;
2020-01-16 00:21:54 +01:00
if (gRfuLinkStatus->parentChild == MODE_NEUTRAL)
return ERR_MODE_NOT_CONNECTED;
if (!(bmSendSlot & 0xF))
return ERR_SLOT_NO;
if (((gRfuLinkStatus->connSlotFlag | gRfuLinkStatus->linkLossSlotFlag) & bmSendSlot) != bmSendSlot)
return ERR_SLOT_NOT_CONNECTED;
2020-02-16 19:49:36 +01:00
if (ni_or_uni & 0x10)
sendSlotFlag = gRfuLinkStatus->sendSlotUNIFlag;
2020-01-16 00:21:54 +01:00
else
2020-02-16 19:49:36 +01:00
sendSlotFlag = gRfuLinkStatus->sendSlotNIFlag;
if (sendSlotFlag & bmSendSlot)
2020-01-16 00:21:54 +01:00
return ERR_SLOT_BUSY;
2020-02-16 19:49:36 +01:00
for (bm_slot_id = 0; bm_slot_id < RFU_CHILD_MAX && !((bmSendSlot >> bm_slot_id) & 1); ++bm_slot_id)
2020-01-16 00:21:54 +01:00
;
if (gRfuLinkStatus->parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
llFrameSize_p = &gRfuLinkStatus->remainLLFrameSizeParent;
2020-01-16 00:21:54 +01:00
else if (gRfuLinkStatus->parentChild == MODE_CHILD)
2020-02-16 19:49:36 +01:00
llFrameSize_p = &gRfuLinkStatus->remainLLFrameSizeChild[bm_slot_id];
frameSize = llsf_struct[gRfuLinkStatus->parentChild].frameSize;
if (subFrameSize > *llFrameSize_p || subFrameSize <= frameSize)
2020-01-16 00:21:54 +01:00
return ERR_SUBFRAME_SIZE;
2020-02-16 19:49:36 +01:00
imeBak = REG_IME;
2020-01-16 00:21:54 +01:00
REG_IME = 0;
2020-02-16 19:49:36 +01:00
sending = ni_or_uni & 0x20;
if (sending || ni_or_uni == 0x40)
{
u8 *dataType_p; // a hack to swap instructions
slotStatus_NI = gRfuSlotStatusNI[bm_slot_id];
slotStatus_UNI = NULL;
slotStatus_NI->send.errorCode = 0;
*slotStatus_NI->send.now_p = dataType_p = &slotStatus_NI->send.dataType;
slotStatus_NI->send.remainSize = 7;
slotStatus_NI->send.bmSlotOrg = bmSendSlot;
slotStatus_NI->send.bmSlot = bmSendSlot;
slotStatus_NI->send.payloadSize = subFrameSize - frameSize;
if (sending != 0)
*dataType_p = 0;
2020-01-16 00:21:54 +01:00
else
2020-02-16 19:49:36 +01:00
*dataType_p = 1;
slotStatus_NI->send.dataSize = dataSize;
slotStatus_NI->send.src = src;
slotStatus_NI->send.ack = 0;
slotStatus_NI->send.phase = 0;
2020-01-16 00:21:54 +01:00
#ifndef NONMATCHING // to fix r2, r3, r4, r5 register roulette
asm("":::"r2");
#endif
for (i = 0; i < WINDOW_COUNT; ++i)
{
2020-02-16 19:49:36 +01:00
slotStatus_NI->send.recvAckFlag[i] = 0;
slotStatus_NI->send.n[i] = 1;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
for (bm_slot_id = 0; bm_slot_id < RFU_CHILD_MAX; ++bm_slot_id)
if ((bmSendSlot >> bm_slot_id) & 1)
gRfuSlotStatusNI[bm_slot_id]->send.failCounter = 0;
2020-01-16 00:21:54 +01:00
gRfuLinkStatus->sendSlotNIFlag |= bmSendSlot;
2020-02-16 19:49:36 +01:00
*llFrameSize_p -= subFrameSize;
slotStatus_NI->send.state = SLOT_STATE_SEND_START;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
else if (ni_or_uni & 0x10)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
slotStatus_UNI = gRfuSlotStatusUNI[bm_slot_id];
slotStatus_UNI->send.bmSlot = bmSendSlot;
slotStatus_UNI->send.src = src;
slotStatus_UNI->send.payloadSize = subFrameSize - frameSize;
*llFrameSize_p -= subFrameSize;
slotStatus_UNI->send.state = SLOT_STATE_SEND_UNI;
2020-01-16 00:21:54 +01:00
gRfuLinkStatus->sendSlotUNIFlag |= bmSendSlot;
}
2020-02-16 19:49:36 +01:00
REG_IME = imeBak;
2020-01-16 00:21:54 +01:00
return 0;
}
u16 rfu_changeSendTarget(u8 connType, u8 slotStatusIndex, u8 bmNewTgtSlot)
{
2020-02-16 19:49:36 +01:00
struct RfuSlotStatusNI *slotStatusNI;
u16 imeBak;
u8 i;
2020-01-16 00:21:54 +01:00
if (slotStatusIndex >= RFU_CHILD_MAX)
return ERR_SLOT_NO;
if (connType == 0x20)
{
2020-02-16 19:49:36 +01:00
slotStatusNI = gRfuSlotStatusNI[slotStatusIndex];
if ((slotStatusNI->send.state & SLOT_BUSY_FLAG)
&& (slotStatusNI->send.state & SLOT_SEND_FLAG))
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
connType = bmNewTgtSlot ^ slotStatusNI->send.bmSlot;
2020-01-16 00:21:54 +01:00
if (!(connType & bmNewTgtSlot))
{
if (connType)
{
2020-02-16 19:49:36 +01:00
imeBak = REG_IME;
2020-01-16 00:21:54 +01:00
REG_IME = 0;
2020-02-16 19:49:36 +01:00
for (i = 0; i < RFU_CHILD_MAX; ++i)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if ((connType >> i) & 1)
gRfuSlotStatusNI[i]->send.failCounter = 0;
2020-01-16 00:21:54 +01:00
}
gRfuLinkStatus->sendSlotNIFlag &= ~connType;
2020-02-16 19:49:36 +01:00
slotStatusNI->send.bmSlot = bmNewTgtSlot;
if (slotStatusNI->send.bmSlot == 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
rfu_STC_releaseFrame(slotStatusIndex, 0, &slotStatusNI->send);
slotStatusNI->send.state = SLOT_STATE_SEND_FAILED;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
REG_IME = imeBak;
2020-01-16 00:21:54 +01:00
}
}
else
{
return ERR_SLOT_TARGET;
}
}
else
{
return ERR_SLOT_NOT_SENDING;
}
}
else
{
if (connType == 16)
{
2020-02-16 19:49:36 +01:00
s32 bmSlot;
2020-01-16 00:21:54 +01:00
if (gRfuSlotStatusUNI[slotStatusIndex]->send.state != SLOT_STATE_SEND_UNI)
return ERR_SLOT_NOT_SENDING;
2020-02-16 19:49:36 +01:00
for (bmSlot = 0, i = 0; i < RFU_CHILD_MAX; ++i)
if (i != slotStatusIndex)
bmSlot |= gRfuSlotStatusUNI[i]->send.bmSlot;
if (bmNewTgtSlot & bmSlot)
2020-01-16 00:21:54 +01:00
return ERR_SLOT_TARGET;
2020-02-16 19:49:36 +01:00
imeBak = REG_IME;
2020-01-16 00:21:54 +01:00
REG_IME = 0;
gRfuLinkStatus->sendSlotUNIFlag &= ~gRfuSlotStatusUNI[slotStatusIndex]->send.bmSlot;
gRfuLinkStatus->sendSlotUNIFlag |= bmNewTgtSlot;
gRfuSlotStatusUNI[slotStatusIndex]->send.bmSlot = bmNewTgtSlot;
2020-02-16 19:49:36 +01:00
REG_IME = imeBak;
2020-01-16 00:21:54 +01:00
}
else
{
return ERR_COMM_TYPE;
}
}
return 0;
}
u16 rfu_NI_stopReceivingData(u8 slotStatusIndex)
{
2020-02-16 19:49:36 +01:00
struct NIComm *NI_comm;
u16 imeBak;
2020-01-16 00:21:54 +01:00
if (slotStatusIndex >= RFU_CHILD_MAX)
return ERR_SLOT_NO;
2020-02-16 19:49:36 +01:00
NI_comm = &gRfuSlotStatusNI[slotStatusIndex]->recv;
imeBak = REG_IME;
++imeBak; --imeBak; // fix imeBak, NI_comm register swap
2020-01-16 00:21:54 +01:00
REG_IME = 0;
if (gRfuSlotStatusNI[slotStatusIndex]->recv.state & SLOT_BUSY_FLAG)
{
if (gRfuSlotStatusNI[slotStatusIndex]->recv.state == SLOT_STATE_RECV_LAST)
gRfuSlotStatusNI[slotStatusIndex]->recv.state = SLOT_STATE_RECV_SUCCESS_AND_SENDSIDE_UNKNOWN;
else
gRfuSlotStatusNI[slotStatusIndex]->recv.state = SLOT_STATE_RECV_FAILED;
gRfuLinkStatus->recvSlotNIFlag &= ~(1 << slotStatusIndex);
2020-02-16 19:49:36 +01:00
rfu_STC_releaseFrame(slotStatusIndex, 1, NI_comm);
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
REG_IME = imeBak;
return 0;
2020-01-16 00:21:54 +01:00
}
u16 rfu_UNI_changeAndReadySendData(u8 slotStatusIndex, const void *src, u8 size)
{
2020-02-16 19:49:36 +01:00
struct UNISend *UNI_send;
u8 *frame_p;
u16 imeBak;
u8 frameEnd;
2020-01-16 00:21:54 +01:00
if (slotStatusIndex >= RFU_CHILD_MAX)
return ERR_SLOT_NO;
2020-02-16 19:49:36 +01:00
UNI_send = &gRfuSlotStatusUNI[slotStatusIndex]->send;
if (UNI_send->state != SLOT_STATE_SEND_UNI)
2020-01-16 00:21:54 +01:00
return ERR_SLOT_NOT_SENDING;
if (gRfuLinkStatus->parentChild == MODE_PARENT)
{
2020-02-16 19:49:36 +01:00
frame_p = &gRfuLinkStatus->remainLLFrameSizeParent;
frameEnd = gRfuLinkStatus->remainLLFrameSizeParent + (u8)UNI_send->payloadSize;
2020-01-16 00:21:54 +01:00
}
else
{
2020-02-16 19:49:36 +01:00
frame_p = &gRfuLinkStatus->remainLLFrameSizeChild[slotStatusIndex];
frameEnd = gRfuLinkStatus->remainLLFrameSizeChild[slotStatusIndex] + (u8)UNI_send->payloadSize;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
if (frameEnd < size)
2020-01-16 00:21:54 +01:00
return ERR_SUBFRAME_SIZE;
2020-02-16 19:49:36 +01:00
imeBak = REG_IME;
2020-01-16 00:21:54 +01:00
REG_IME = 0;
2020-02-16 19:49:36 +01:00
UNI_send->src = src;
*frame_p = frameEnd - size;
UNI_send->payloadSize = size;
UNI_send->dataReadyFlag = 1;
REG_IME = imeBak;
2020-01-16 00:21:54 +01:00
return 0;
}
void rfu_UNI_readySendData(u8 slotStatusIndex)
{
if (slotStatusIndex < RFU_CHILD_MAX)
{
if (gRfuSlotStatusUNI[slotStatusIndex]->send.state == SLOT_STATE_SEND_UNI)
gRfuSlotStatusUNI[slotStatusIndex]->send.dataReadyFlag = 1;
}
}
void rfu_UNI_clearRecvNewDataFlag(u8 slotStatusIndex)
{
if (slotStatusIndex < RFU_CHILD_MAX)
gRfuSlotStatusUNI[slotStatusIndex]->recv.newDataFlag = 0;
}
void rfu_REQ_sendData(bool8 clockChangeFlag)
{
if (gRfuLinkStatus->parentChild != MODE_NEUTRAL)
{
if (gRfuLinkStatus->parentChild == MODE_PARENT
&& !(gRfuLinkStatus->sendSlotNIFlag | gRfuLinkStatus->recvSlotNIFlag | gRfuLinkStatus->sendSlotUNIFlag))
{
if (gRfuStatic->commExistFlag)
{
gRfuStatic->emberCount = 16;
gRfuStatic->nullFrameCount = 0;
}
if (gRfuStatic->emberCount)
--gRfuStatic->emberCount;
else
++gRfuStatic->nullFrameCount;
if (gRfuStatic->emberCount
|| !(gRfuStatic->nullFrameCount & 0xF))
{
gRfuFixed->LLFBuffer[0] = 1;
gRfuFixed->LLFBuffer[4] = 0xFF;
STWI_set_Callback_M(rfu_CB_sendData3);
if (!clockChangeFlag)
STWI_send_DataTxREQ(gRfuFixed->LLFBuffer, 1);
else
STWI_send_DataTxAndChangeREQ(gRfuFixed->LLFBuffer, 1);
return;
}
}
else
{
if (!gRfuLinkStatus->LLFReadyFlag)
rfu_constructSendLLFrame();
if (gRfuLinkStatus->LLFReadyFlag)
{
STWI_set_Callback_M(rfu_CB_sendData);
if (clockChangeFlag)
{
STWI_send_DataTxAndChangeREQ(gRfuFixed->LLFBuffer, gRfuStatic->totalPacketSize + 4);
return;
}
STWI_send_DataTxREQ(gRfuFixed->LLFBuffer, gRfuStatic->totalPacketSize + 4);
}
}
if (clockChangeFlag)
{
if (gRfuLinkStatus->parentChild == MODE_PARENT)
{
if (gSTWIStatus->callbackS != NULL)
gSTWIStatus->callbackS(39);
}
else
{
STWI_set_Callback_M(rfu_CB_sendData2);
STWI_send_MS_ChangeREQ();
}
}
}
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_sendData(UNUSED u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u8 i;
struct NIComm *NI_comm;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if (reqResult == 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
for (i = 0; i < RFU_CHILD_MAX; ++i)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (gRfuSlotStatusUNI[i]->send.dataReadyFlag)
gRfuSlotStatusUNI[i]->send.dataReadyFlag = 0;
NI_comm = &gRfuSlotStatusNI[i]->send;
if (NI_comm->state == SLOT_STATE_SEND_NULL)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
rfu_STC_releaseFrame(i, 0, NI_comm);
gRfuLinkStatus->sendSlotNIFlag &= ~NI_comm->bmSlot;
if (NI_comm->dataType == 1)
gRfuLinkStatus->getNameFlag |= 1 << i;
NI_comm->state = SLOT_STATE_SEND_SUCCESS;
2020-01-16 00:21:54 +01:00
}
}
}
gRfuLinkStatus->LLFReadyFlag = 0;
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(ID_DATA_TX_REQ, reqResult);
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_sendData2(UNUSED u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(ID_DATA_TX_REQ, reqResult);
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_sendData3(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (reqResult != 0)
rfu_STC_REQ_callback(ID_DATA_TX_REQ, reqResult);
else if (reqCommand == ID_CLOCK_SLAVE_MS_CHANGE_ERROR_BY_DMA_REQ)
rfu_STC_REQ_callback(ID_CLOCK_SLAVE_MS_CHANGE_ERROR_BY_DMA_REQ, 0);
2020-01-16 00:21:54 +01:00
}
static void rfu_constructSendLLFrame(void)
{
2020-02-16 19:49:36 +01:00
u32 pakcketSize, currSize;
u8 i;
u8 *llf_p;
struct RfuSlotStatusNI *slotStatusNI;
2020-01-16 00:21:54 +01:00
if (gRfuLinkStatus->parentChild != MODE_NEUTRAL
&& gRfuLinkStatus->sendSlotNIFlag | gRfuLinkStatus->recvSlotNIFlag | gRfuLinkStatus->sendSlotUNIFlag)
{
gRfuLinkStatus->LLFReadyFlag = 0;
2020-02-16 19:49:36 +01:00
pakcketSize = 0;
llf_p = (u8 *)&gRfuFixed->LLFBuffer[1];
for (i = 0; i < RFU_CHILD_MAX; ++i)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
currSize = 0;
if (gRfuSlotStatusNI[i]->send.state & SLOT_BUSY_FLAG)
currSize = rfu_STC_NI_constructLLSF(i, &llf_p, &gRfuSlotStatusNI[i]->send);
if (gRfuSlotStatusNI[i]->recv.state & SLOT_BUSY_FLAG)
currSize += rfu_STC_NI_constructLLSF(i, &llf_p, &gRfuSlotStatusNI[i]->recv);
if (gRfuSlotStatusUNI[i]->send.state == SLOT_STATE_SEND_UNI)
currSize += rfu_STC_UNI_constructLLSF(i, &llf_p);
if (currSize != 0)
2020-01-16 00:21:54 +01:00
{
if (gRfuLinkStatus->parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
pakcketSize += currSize;
2020-01-16 00:21:54 +01:00
else
2020-02-16 19:49:36 +01:00
pakcketSize |= currSize << (5 * i + 8);
2020-01-16 00:21:54 +01:00
}
}
2020-02-16 19:49:36 +01:00
if (pakcketSize != 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
while ((u32)llf_p & 3)
*llf_p++ = 0;
gRfuFixed->LLFBuffer[0] = pakcketSize;
2020-01-16 00:21:54 +01:00
if (gRfuLinkStatus->parentChild == MODE_CHILD)
{
2020-02-16 19:49:36 +01:00
u8 *maxSize = llf_p - offsetof(struct RfuFixed, LLFBuffer[1]);
// Does the volatile qualifier make sense?
// It's the same as:
2020-01-16 00:21:54 +01:00
// asm("":::"memory");
2020-02-16 19:49:36 +01:00
pakcketSize = maxSize - *(u8 *volatile *)&gRfuFixed;
2020-01-16 00:21:54 +01:00
}
}
2020-02-16 19:49:36 +01:00
gRfuStatic->totalPacketSize = pakcketSize;
2020-01-16 00:21:54 +01:00
}
}
2020-02-16 19:49:36 +01:00
static u16 rfu_STC_NI_constructLLSF(u8 bm_slot_id, u8 **dest_pp, struct NIComm *NI_comm)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u16 size;
u32 frame;
2020-01-16 00:21:54 +01:00
u8 i;
2020-02-16 19:49:36 +01:00
u8 *frame8_p;
const struct LLSFStruct *llsf = &llsf_struct[gRfuLinkStatus->parentChild];
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if (NI_comm->state == SLOT_STATE_SENDING)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
while (NI_comm->now_p[NI_comm->phase] >= (const u8 *)NI_comm->src + NI_comm->dataSize)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
++NI_comm->phase;
if (NI_comm->phase == 4)
NI_comm->phase = 0;
2020-01-16 00:21:54 +01:00
}
}
2020-02-16 19:49:36 +01:00
if (NI_comm->state & SLOT_RECV_FLAG)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
size = 0;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
else if (NI_comm->state == SLOT_STATE_SENDING)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (NI_comm->now_p[NI_comm->phase] + NI_comm->payloadSize > (const u8 *)NI_comm->src + NI_comm->dataSize)
size = (const u8 *)NI_comm->src + NI_comm->dataSize - NI_comm->now_p[NI_comm->phase];
2020-01-16 00:21:54 +01:00
else
2020-02-16 19:49:36 +01:00
size = NI_comm->payloadSize;
2020-01-16 00:21:54 +01:00
}
else
{
2020-04-28 01:27:54 +02:00
if (NI_comm->remainSize >= NI_comm->payloadSize)
2020-02-16 19:49:36 +01:00
size = NI_comm->payloadSize;
2020-01-16 00:21:54 +01:00
else
2020-02-16 19:49:36 +01:00
size = NI_comm->remainSize;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
frame = (NI_comm->state & 0xF) << llsf->slotStateShift
| NI_comm->ack << llsf->ackShift
| NI_comm->phase << llsf->phaseShit
| NI_comm->n[NI_comm->phase] << llsf->nShift
| size;
2020-01-16 00:21:54 +01:00
if (gRfuLinkStatus->parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
frame |= NI_comm->bmSlot << 18;
frame8_p = (u8 *)&frame;
for (i = 0; i < llsf->frameSize; ++i)
*(*dest_pp)++ = *frame8_p++;
if (size != 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
const u8 *src = NI_comm->now_p[NI_comm->phase];
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
gRfuFixed->fastCopyPtr(&src, dest_pp, size);
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
if (NI_comm->state == SLOT_STATE_SENDING)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
++NI_comm->phase;
if (NI_comm->phase == 4)
NI_comm->phase = 0;
2020-01-16 00:21:54 +01:00
}
if (gRfuLinkStatus->parentChild == MODE_PARENT)
gRfuLinkStatus->LLFReadyFlag = 1;
else
2020-02-16 19:49:36 +01:00
gRfuLinkStatus->LLFReadyFlag |= 1 << bm_slot_id;
return size + llsf->frameSize;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
static u16 rfu_STC_UNI_constructLLSF(u8 bm_slot_id, u8 **dest_p)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
const struct LLSFStruct *llsf;
const u8 *src_p;
u32 frame;
u8 *frame8_p;
2020-01-16 00:21:54 +01:00
u8 i;
2020-02-16 19:49:36 +01:00
struct UNISend *UNI_send = &gRfuSlotStatusUNI[bm_slot_id]->send;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if (!UNI_send->dataReadyFlag || !UNI_send->bmSlot)
2020-01-16 00:21:54 +01:00
return 0;
2020-02-16 19:49:36 +01:00
llsf = &llsf_struct[gRfuLinkStatus->parentChild];
frame = (UNI_send->state & 0xF) << llsf->slotStateShift
| UNI_send->payloadSize;
2020-01-16 00:21:54 +01:00
if (gRfuLinkStatus->parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
frame |= UNI_send->bmSlot << 18;
frame8_p = (u8 *)&frame;
for (i = 0; i < llsf->frameSize; ++i)
*(*dest_p)++ = *frame8_p++;
src_p = UNI_send->src;
gRfuFixed->fastCopyPtr(&src_p, dest_p, UNI_send->payloadSize);
2020-01-16 00:21:54 +01:00
if (gRfuLinkStatus->parentChild == MODE_PARENT)
gRfuLinkStatus->LLFReadyFlag = 16;
else
2020-02-16 19:49:36 +01:00
gRfuLinkStatus->LLFReadyFlag |= 16 << bm_slot_id;
return llsf->frameSize + UNI_send->payloadSize;
2020-01-16 00:21:54 +01:00
}
void rfu_REQ_recvData(void)
{
if (gRfuLinkStatus->parentChild != MODE_NEUTRAL)
{
gRfuStatic->commExistFlag = gRfuLinkStatus->sendSlotNIFlag | gRfuLinkStatus->recvSlotNIFlag | gRfuLinkStatus->sendSlotUNIFlag;
gRfuStatic->recvErrorFlag = 0;
STWI_set_Callback_M(rfu_CB_recvData);
STWI_send_DataRxREQ();
}
}
2020-02-16 19:49:36 +01:00
static void rfu_CB_recvData(u8 reqCommand, u16 reqResult)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u8 i;
struct RfuSlotStatusNI *slotStatusNI;
struct NIComm *NI_comm;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if (reqResult == 0 && gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[1])
2020-01-16 00:21:54 +01:00
{
gRfuStatic->NIEndRecvFlag = 0;
if (gRfuLinkStatus->parentChild == MODE_PARENT)
rfu_STC_PARENT_analyzeRecvPacket();
else
rfu_STC_CHILD_analyzeRecvPacket();
2020-02-16 19:49:36 +01:00
for (i = 0; i < RFU_CHILD_MAX; ++i)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
slotStatusNI = gRfuSlotStatusNI[i];
if (slotStatusNI->recv.state == SLOT_STATE_RECV_LAST && !((gRfuStatic->NIEndRecvFlag >> i) & 1))
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
NI_comm = &slotStatusNI->recv;
if (NI_comm->dataType == 1)
gRfuLinkStatus->getNameFlag |= 1 << i;
rfu_STC_releaseFrame(i, 1, NI_comm);
gRfuLinkStatus->recvSlotNIFlag &= ~NI_comm->bmSlot;
slotStatusNI->recv.state = SLOT_STATE_RECV_SUCCESS;
2020-01-16 00:21:54 +01:00
}
}
if (gRfuStatic->recvErrorFlag)
2020-02-16 19:49:36 +01:00
reqResult = gRfuStatic->recvErrorFlag | ERR_DATA_RECV;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
rfu_STC_REQ_callback(reqCommand, reqResult);
2020-01-16 00:21:54 +01:00
}
static void rfu_STC_PARENT_analyzeRecvPacket(void)
{
2020-02-16 19:49:36 +01:00
u32 frames32;
u8 bm_slot_id;
u8 frame_counts[RFU_CHILD_MAX];
u8 *packet_p;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
frames32 = gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket32.data[0] >> 8;
for (bm_slot_id = 0; bm_slot_id < RFU_CHILD_MAX; ++bm_slot_id)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
frame_counts[bm_slot_id] = frames32 & 0x1F;
frames32 >>= 5;
if (frame_counts[bm_slot_id] == 0)
gRfuStatic->NIEndRecvFlag |= 1 << bm_slot_id;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
packet_p = &gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[8];
for (bm_slot_id = 0; bm_slot_id < RFU_CHILD_MAX; ++bm_slot_id)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (frame_counts[bm_slot_id])
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u8 *frames_p = &frame_counts[bm_slot_id];
2020-01-16 00:21:54 +01:00
do
{
2020-02-16 19:49:36 +01:00
u8 analyzed_frames = rfu_STC_analyzeLLSF(bm_slot_id, packet_p, *frames_p);
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
packet_p += analyzed_frames;
*frames_p -= analyzed_frames;
} while (!(*frames_p & 0x80) && (*frames_p));
2020-01-16 00:21:54 +01:00
}
}
}
static void rfu_STC_CHILD_analyzeRecvPacket(void)
{
2020-02-16 19:49:36 +01:00
u16 frames_remaining;
u8 *packet_p;
u16 analyzed_frames;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
frames_remaining = *(u16 *)&gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[4] & 0x7F;
packet_p = &gRfuFixed->STWIBuffer->rxPacketAlloc.rfuPacket8.data[8];
if (frames_remaining == 0)
2020-01-16 00:21:54 +01:00
gRfuStatic->NIEndRecvFlag = 15;
do
{
2020-02-16 19:49:36 +01:00
if (frames_remaining == 0)
2020-01-16 00:21:54 +01:00
break;
2020-02-16 19:49:36 +01:00
analyzed_frames = rfu_STC_analyzeLLSF(0, packet_p, frames_remaining);
packet_p += analyzed_frames;
frames_remaining -= analyzed_frames;
} while (!(frames_remaining & 0x8000));
}
static u16 rfu_STC_analyzeLLSF(u8 slot_id, const u8 *src, u16 last_frame)
{
struct RfuLocalStruct llsf_NI;
const struct LLSFStruct *llsf_p;
u32 frames;
u8 i;
u16 retVal;
llsf_p = &llsf_struct[~gRfuLinkStatus->parentChild & (MODE_NEUTRAL & MODE_PARENT)];
if (last_frame < llsf_p->frameSize)
return last_frame;
frames = 0;
for (i = 0; i < llsf_p->frameSize; ++i)
frames |= *src++ << 8 * i;
llsf_NI.recvFirst = (frames >> llsf_p->recvFirstShift) & llsf_p->recvFirstMask;
llsf_NI.connSlotFlag = (frames >> llsf_p->connSlotFlagShift) & llsf_p->connSlotFlagMask;
llsf_NI.slotState = (frames >> llsf_p->slotStateShift) & llsf_p->slotStateMask;
llsf_NI.ack = (frames >> llsf_p->ackShift) & llsf_p->ackMask;
llsf_NI.phase = (frames >> llsf_p->phaseShit) & llsf_p->phaseMask;
llsf_NI.n = (frames >> llsf_p->nShift) & llsf_p->nMask;
llsf_NI.frame = (frames & llsf_p->framesMask) & frames;
retVal = llsf_NI.frame + llsf_p->frameSize;
if (llsf_NI.recvFirst == 0)
2020-01-16 00:21:54 +01:00
{
if (gRfuLinkStatus->parentChild == MODE_PARENT)
{
2020-02-16 19:49:36 +01:00
if ((gRfuLinkStatus->connSlotFlag >> slot_id) & 1)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (llsf_NI.slotState == LCOM_UNI)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
rfu_STC_UNI_receive(slot_id, &llsf_NI, src);
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
else if (llsf_NI.ack == 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
rfu_STC_NI_receive_Receiver(slot_id, &llsf_NI, src);
2020-01-16 00:21:54 +01:00
}
else
{
2020-02-16 19:49:36 +01:00
for (i = 0; i < RFU_CHILD_MAX; ++i)
if (((gRfuSlotStatusNI[i]->send.bmSlot >> slot_id) & 1)
&& ((gRfuLinkStatus->sendSlotNIFlag >> slot_id) & 1))
2020-01-16 00:21:54 +01:00
break;
2020-02-16 19:49:36 +01:00
if (i < RFU_CHILD_MAX)
rfu_STC_NI_receive_Sender(i, slot_id, &llsf_NI, src);
2020-01-16 00:21:54 +01:00
}
}
}
else
{
2020-02-16 19:49:36 +01:00
s32 conSlots = gRfuLinkStatus->connSlotFlag & llsf_NI.connSlotFlag;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if (conSlots)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
for (i = 0; i < RFU_CHILD_MAX; ++i)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if ((conSlots >> i) & 1)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (llsf_NI.slotState == LCOM_UNI)
rfu_STC_UNI_receive(i, &llsf_NI, src);
else if (llsf_NI.ack == 0)
rfu_STC_NI_receive_Receiver(i, &llsf_NI, src);
else if ((gRfuLinkStatus->sendSlotNIFlag >> i) & 1)
rfu_STC_NI_receive_Sender(i, i, &llsf_NI, src);
2020-01-16 00:21:54 +01:00
}
}
}
}
}
2020-02-16 19:49:36 +01:00
return retVal;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
static void rfu_STC_UNI_receive(u8 bm_slot_id, const struct RfuLocalStruct *llsf_NI, const u8 *src)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u8 *dest;
u32 size;
struct RfuSlotStatusUNI *slotStatusUNI = gRfuSlotStatusUNI[bm_slot_id];
struct UNIRecv *UNI_recv = &slotStatusUNI->recv;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
UNI_recv->errorCode = 0;
if (gRfuSlotStatusUNI[bm_slot_id]->recvBufferSize < llsf_NI->frame)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
slotStatusUNI->recv.state = SLOT_STATE_RECV_IGNORE;
UNI_recv->errorCode = ERR_RECV_BUFF_OVER;
2020-01-16 00:21:54 +01:00
}
else
2020-02-16 19:49:36 +01:00
{
if (UNI_recv->dataBlockFlag)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (UNI_recv->newDataFlag)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
UNI_recv->errorCode = ERR_RECV_UNK;
goto force_tail_merge;
2020-01-16 00:21:54 +01:00
}
}
else
{
2020-02-16 19:49:36 +01:00
if (UNI_recv->newDataFlag)
UNI_recv->errorCode = ERR_RECV_DATA_OVERWRITED;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
UNI_recv->state = SLOT_STATE_RECEIVING;
size = UNI_recv->dataSize = llsf_NI->frame;
dest = gRfuSlotStatusUNI[bm_slot_id]->recvBuffer;
gRfuFixed->fastCopyPtr(&src, &dest, size);
UNI_recv->newDataFlag = 1;
UNI_recv->state = 0;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
force_tail_merge:
if (UNI_recv->errorCode)
gRfuStatic->recvErrorFlag |= 16 << bm_slot_id;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
static void rfu_STC_NI_receive_Sender(u8 NI_slot, u8 bm_flag, const struct RfuLocalStruct *llsf_NI, const u8 *data_p)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
struct NIComm *NI_comm = &gRfuSlotStatusNI[NI_slot]->send;
u16 state = NI_comm->state;
u8 n = NI_comm->n[llsf_NI->phase];
u8 i;
u16 imeBak;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if ((llsf_NI->slotState == LCOM_NI && state == SLOT_STATE_SENDING)
|| (llsf_NI->slotState == LCOM_NI_START && state == SLOT_STATE_SEND_START)
|| (llsf_NI->slotState == LCOM_NI_END && state == SLOT_STATE_SEND_LAST))
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (NI_comm->n[llsf_NI->phase] == llsf_NI->n)
NI_comm->recvAckFlag[llsf_NI->phase] |= 1 << bm_flag;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
if ((NI_comm->recvAckFlag[llsf_NI->phase] & NI_comm->bmSlot) == NI_comm->bmSlot)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
NI_comm->n[llsf_NI->phase] = (NI_comm->n[llsf_NI->phase] + 1) & 3;
NI_comm->recvAckFlag[llsf_NI->phase] = 0;
if ((u16)(NI_comm->state + ~SLOT_STATE_SEND_NULL) <= 1)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (NI_comm->state == SLOT_STATE_SEND_START)
NI_comm->now_p[llsf_NI->phase] += NI_comm->payloadSize;
2020-01-16 00:21:54 +01:00
else
2020-02-16 19:49:36 +01:00
NI_comm->now_p[llsf_NI->phase] += NI_comm->payloadSize << 2;
NI_comm->remainSize -= NI_comm->payloadSize;
2020-04-28 01:27:54 +02:00
switch (NI_comm->remainSize)
{
default:
case 0:
NI_comm->phase = 0;
if (NI_comm->state == SLOT_STATE_SEND_START)
2020-01-16 00:21:54 +01:00
{
2020-04-28 01:27:54 +02:00
for (i = 0; i < WINDOW_COUNT; ++i)
2020-01-16 00:21:54 +01:00
{
2020-04-28 01:27:54 +02:00
NI_comm->n[i] = 1;
NI_comm->now_p[i] = NI_comm->src + NI_comm->payloadSize * i;
2020-01-16 00:21:54 +01:00
}
2020-04-28 01:27:54 +02:00
NI_comm->remainSize = NI_comm->dataSize;
NI_comm->state = SLOT_STATE_SENDING;
2020-01-16 00:21:54 +01:00
}
2020-04-28 01:27:54 +02:00
else
{
NI_comm->n[0] = 0;
NI_comm->remainSize = 0;
NI_comm->state = SLOT_STATE_SEND_LAST;
}
break;
case 1 ... INT_MAX:
break;
}
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
else if (NI_comm->state == SLOT_STATE_SEND_LAST)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
NI_comm->state = SLOT_STATE_SEND_NULL;
2020-01-16 00:21:54 +01:00
}
}
2020-02-16 19:49:36 +01:00
if (NI_comm->state != state
|| NI_comm->n[llsf_NI->phase] != n
|| (NI_comm->recvAckFlag[llsf_NI->phase] >> bm_flag) & 1)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
imeBak = REG_IME;
2020-01-16 00:21:54 +01:00
REG_IME = 0;
2020-02-16 19:49:36 +01:00
gRfuStatic->recvRenewalFlag |= 16 << bm_flag;
gRfuSlotStatusNI[bm_flag]->send.failCounter = 0;
REG_IME = imeBak;
2020-01-16 00:21:54 +01:00
}
}
2020-02-16 19:49:36 +01:00
static void rfu_STC_NI_receive_Receiver(u8 bm_slot_id, const struct RfuLocalStruct *llsf_NI, const u8 *data_p)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u16 imeBak;
u32 state_check = 0;
struct RfuSlotStatusNI *slotStatus_NI = gRfuSlotStatusNI[bm_slot_id];
struct NIComm *recvSlot = &slotStatus_NI->recv;
u16 state = slotStatus_NI->recv.state;
u8 n = slotStatus_NI->recv.n[llsf_NI->phase];
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if (llsf_NI->slotState == LCOM_NI_END)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
gRfuStatic->NIEndRecvFlag |= 1 << bm_slot_id;
if (slotStatus_NI->recv.state == SLOT_STATE_RECEIVING)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
slotStatus_NI->recv.phase = 0;
slotStatus_NI->recv.n[0] = 0;
slotStatus_NI->recv.state = SLOT_STATE_RECV_LAST;
2020-01-16 00:21:54 +01:00
}
}
2020-02-16 19:49:36 +01:00
else if (llsf_NI->slotState == LCOM_NI)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (state == SLOT_STATE_RECV_START && !recvSlot->remainSize)
rfu_STC_NI_initSlot_asRecvDataEntity(bm_slot_id, recvSlot);
if (recvSlot->state == SLOT_STATE_RECEIVING)
state_check = 1;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
else if (llsf_NI->slotState == LCOM_NI_START)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (state == SLOT_STATE_RECV_START)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
state_check = 1;
2020-01-16 00:21:54 +01:00
}
else
{
2020-02-16 19:49:36 +01:00
rfu_STC_NI_initSlot_asRecvControllData(bm_slot_id, recvSlot);
if (slotStatus_NI->recv.state != SLOT_STATE_RECV_START)
2020-01-16 00:21:54 +01:00
return;
2020-02-16 19:49:36 +01:00
state_check = 1;
2020-01-16 00:21:54 +01:00
}
}
2020-02-16 19:49:36 +01:00
if (state_check != 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (llsf_NI->n == ((recvSlot->n[llsf_NI->phase] + 1) & 3))
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
gRfuFixed->fastCopyPtr(&data_p, (u8 **)&recvSlot->now_p[llsf_NI->phase], llsf_NI->frame);
if (recvSlot->state == SLOT_STATE_RECEIVING)
recvSlot->now_p[llsf_NI->phase] += 3 * recvSlot->payloadSize;
recvSlot->remainSize -= llsf_NI->frame;
recvSlot->n[llsf_NI->phase] = llsf_NI->n;
2020-01-16 00:21:54 +01:00
}
}
2020-02-16 19:49:36 +01:00
if (recvSlot->errorCode == 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
recvSlot->phase = llsf_NI->phase;
if (recvSlot->state != state || recvSlot->n[llsf_NI->phase] != n || recvSlot->n[llsf_NI->phase] == llsf_NI->n)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
imeBak = REG_IME;
2020-01-16 00:21:54 +01:00
REG_IME = 0;
2020-02-16 19:49:36 +01:00
gRfuStatic->recvRenewalFlag |= 1 << bm_slot_id;
recvSlot->failCounter = 0;
REG_IME = imeBak;
2020-01-16 00:21:54 +01:00
}
}
}
2020-02-16 19:49:36 +01:00
static void rfu_STC_NI_initSlot_asRecvControllData(u8 bm_slot_id, struct NIComm *NI_comm)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u8 *llFrameSize_p;
u32 llFrameSize;
u8 bm_slot_flag;
2020-01-16 00:21:54 +01:00
if (gRfuLinkStatus->parentChild == MODE_PARENT)
{
2020-02-16 19:49:36 +01:00
llFrameSize = 3;
llFrameSize_p = &gRfuLinkStatus->remainLLFrameSizeParent;
2020-01-16 00:21:54 +01:00
}
else
{
2020-02-16 19:49:36 +01:00
llFrameSize = 2;
llFrameSize_p = &gRfuLinkStatus->remainLLFrameSizeChild[bm_slot_id];
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
bm_slot_flag = 1 << bm_slot_id;
if (NI_comm->state == 0)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
if (*llFrameSize_p < llFrameSize)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
NI_comm->state = SLOT_STATE_RECV_IGNORE;
NI_comm->errorCode = ERR_RECV_REPLY_SUBFRAME_SIZE;
gRfuStatic->recvErrorFlag |= bm_slot_flag;
2020-01-16 00:21:54 +01:00
}
else
{
2020-02-16 19:49:36 +01:00
NI_comm->errorCode = 0;
*llFrameSize_p -= llFrameSize;
NI_comm->now_p[0] = &NI_comm->dataType;
NI_comm->remainSize = 7;
NI_comm->ack = 1;
NI_comm->payloadSize = 0;
NI_comm->bmSlot = bm_slot_flag;
NI_comm->state = SLOT_STATE_RECV_START;
gRfuLinkStatus->recvSlotNIFlag |= bm_slot_flag;
2020-01-16 00:21:54 +01:00
}
}
}
2020-02-16 19:49:36 +01:00
static void rfu_STC_NI_initSlot_asRecvDataEntity(u8 bm_slot_id, struct NIComm *NI_comm)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
u8 bm_slot_flag, win_id;
2020-01-16 00:21:54 +01:00
2020-02-16 19:49:36 +01:00
if (NI_comm->dataType == 1)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
NI_comm->now_p[0] = (void *)&gRfuLinkStatus->partner[bm_slot_id].serialNo;
2020-01-16 00:21:54 +01:00
}
else
{
2020-02-16 19:49:36 +01:00
if (NI_comm->dataSize > gRfuSlotStatusNI[bm_slot_id]->recvBufferSize)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
bm_slot_flag = 1 << bm_slot_id;
gRfuStatic->recvErrorFlag |= bm_slot_flag;
gRfuLinkStatus->recvSlotNIFlag &= ~bm_slot_flag;
NI_comm->errorCode = ERR_RECV_BUFF_OVER;
NI_comm->state = SLOT_STATE_RECV_FAILED;
rfu_STC_releaseFrame(bm_slot_id, 1, NI_comm);
2020-01-16 00:21:54 +01:00
return;
}
2020-02-16 19:49:36 +01:00
NI_comm->now_p[0] = gRfuSlotStatusNI[bm_slot_id]->recvBuffer;
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
for (win_id = 0; win_id < WINDOW_COUNT; ++win_id)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
NI_comm->n[win_id] = 0;
NI_comm->now_p[win_id] = &NI_comm->now_p[0][NI_comm->payloadSize * win_id];
2020-01-16 00:21:54 +01:00
}
2020-02-16 19:49:36 +01:00
NI_comm->remainSize = NI_comm->dataSize;
NI_comm->state = SLOT_STATE_RECEIVING;
2020-01-16 00:21:54 +01:00
}
static void rfu_NI_checkCommFailCounter(void)
{
2020-02-16 19:49:36 +01:00
u16 imeBak;
u32 recvRenewalFlag;
u8 bm_slot_flag, bm_slot_id;
2020-01-16 00:21:54 +01:00
if (gRfuLinkStatus->sendSlotNIFlag | gRfuLinkStatus->recvSlotNIFlag)
{
2020-02-16 19:49:36 +01:00
imeBak = REG_IME;
2020-01-16 00:21:54 +01:00
REG_IME = 0;
2020-02-16 19:49:36 +01:00
recvRenewalFlag = gRfuStatic->recvRenewalFlag >> 4;
for (bm_slot_id = 0; bm_slot_id < RFU_CHILD_MAX; ++bm_slot_id)
2020-01-16 00:21:54 +01:00
{
2020-02-16 19:49:36 +01:00
bm_slot_flag = 1 << bm_slot_id;
if (gRfuLinkStatus->sendSlotNIFlag & bm_slot_flag
&& !(gRfuStatic->recvRenewalFlag & bm_slot_flag))
++gRfuSlotStatusNI[bm_slot_id]->send.failCounter;
if (gRfuLinkStatus->recvSlotNIFlag & bm_slot_flag
&& !(recvRenewalFlag & bm_slot_flag))
++gRfuSlotStatusNI[bm_slot_id]->recv.failCounter;
2020-01-16 00:21:54 +01:00
}
gRfuStatic->recvRenewalFlag = 0;
2020-02-16 19:49:36 +01:00
REG_IME = imeBak;
2020-01-16 00:21:54 +01:00
}
}
void rfu_REQ_noise(void)
{
STWI_set_Callback_M(rfu_STC_REQ_callback);
STWI_send_TestModeREQ(1, 0);
}