motor.c
4.13 KB
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
/*---------------------------------------------------------------------
Copyright (C) 1997, Nintendo.
File motor.c
Coded by Koji Mitsunari. Oct 11, 1996.
Modified by Koji Mitsunari. Feb 18, 1998.
Comments RUMBLE PAK , Initialize, start & stop
$Id: motor.c,v 1.1.1.2 2002/10/29 08:06:44 blythe Exp $
---------------------------------------------------------------------*/
/**************************************************************************
*
* $Revision: 1.1.1.2 $
* $Date: 2002/10/29 08:06:44 $
* $Source: /root/leakn64/depot/rf/sw/n64os20l/libultra/shared/motor/motor.c,v $
*
**************************************************************************/
#include "osint.h"
#include "controller.h"
#include "siint.h"
#define M_ADDR (0xc000/BLOCKSIZE)
#define BANK_ADDR (0x8000/BLOCKSIZE)
#define MOTOR_ID 0x80
#define CHECK_ID 0xfe
s32 __osMotorAccess(OSPfs *, s32);
static OSPifRam __MotorDataBuf[MAXCONTROLLERS];
#if 0
s32
osMotorStop(OSPfs *pfs)
{
return (__osMotorAccess(pfs, 0));
}
s32
osMotorStart(OSPfs *pfs)
{
return (__osMotorAccess(pfs, 1));
}
#endif
s32
__osMotorAccess(OSPfs *pfs, s32 flag)
{
int i;
s32 ret;
u8 *ptr = (u8 *)(&__MotorDataBuf[pfs->channel]);
if (!(pfs->status & PFS_MOTOR_INITIALIZED))
return(PFS_ERR_INVALID);
/* Block to get resource token */
__osSiGetAccess();
/* Set up request command format for all channels */
__MotorDataBuf[pfs->channel].pifstatus = CONT_FORMAT;
ptr += pfs->channel;
for (i = 0; i < BLOCKSIZE ; i++)
((__OSContRamReadFormat *)ptr)->data[i] = (u8)flag;
__osContLastCmd = CONT_ETC;
__osSiRawStartDma(OS_WRITE, &__MotorDataBuf[pfs->channel]);
(void)osRecvMesg(pfs->queue, (OSMesg *)NULL, OS_MESG_BLOCK);
/* trigger pifmacro */
ret = __osSiRawStartDma(OS_READ, &__MotorDataBuf[pfs->channel]);
(void)osRecvMesg(pfs->queue, (OSMesg *)NULL, OS_MESG_BLOCK);
/* check data CRC */
ret = (s32)(((__OSContRamReadFormat *)ptr)->rxsize & CON_ERR_MASK);
if (ret == 0) {
if (flag == 0) {
if (((__OSContRamReadFormat *)ptr)->datacrc != 0) {
ret = PFS_ERR_CONTRFAIL;
}
} else {
if (((__OSContRamReadFormat *)ptr)->datacrc != 0xeb) {
ret = PFS_ERR_CONTRFAIL;
}
}
}
__osSiRelAccess();
return (ret);
}
static void
__osMakeMotorData(int channel, OSPifRam *mdata)
{
u8 *ptr = (u8 *)(mdata);
__OSContRamReadFormat ramreadformat;
int i;
/* Setup ram read format */
ramreadformat.dummy = 0xff;
ramreadformat.txsize = 35;
ramreadformat.rxsize = 1;
ramreadformat.cmd = CONT_RAM_WRITE;
ramreadformat.addrh = (u8)(M_ADDR >>3);
ramreadformat.addrl =
(u8)( (M_ADDR << 5) | __osContAddressCrc(M_ADDR)) ;
/* setup ram read format */
if (channel)
for (i = 0; i < channel; i++)
*ptr++ = 0;
*((__OSContRamReadFormat *)ptr) = ramreadformat;
ptr += sizeof(ramreadformat);
*((u8 *)ptr) = FORMAT_END;
}
s32
osMotorInit(OSMesgQueue *mq, OSPfs *pfs, int channel)
{
s32 ret;
u8 temp[BLOCKSIZE];
pfs->queue = mq;
pfs->channel = channel;
pfs->activebank = 255; /* Dummy */
pfs->status = 0;
/* Change bank of Dummy ID */
ret = __osPfsSelectBank(pfs, CHECK_ID);
if (ret == PFS_ERR_NEW_PACK) ret = __osPfsSelectBank(pfs, MOTOR_ID);
if (ret != 0) return(ret);
/* Read bank of Dummy ID */
ret = __osContRamRead(mq, channel, BANK_ADDR, temp);
if (ret == PFS_ERR_NEW_PACK) ret = PFS_ERR_CONTRFAIL;
if (ret != 0) {
return(ret);
} else if (temp[BLOCKSIZE-1] == CHECK_ID) {
return(PFS_ERR_DEVICE);
}
/* Change bank of RUMBLE PAK ID */
ret = __osPfsSelectBank(pfs, MOTOR_ID);
if (ret == PFS_ERR_NEW_PACK) ret = PFS_ERR_CONTRFAIL;
if (ret != 0) return(ret);
/* Read bank of RUMBLE PAK ID */
ret = __osContRamRead(mq, channel, BANK_ADDR, temp);
if (ret == PFS_ERR_NEW_PACK) {
ret = PFS_ERR_CONTRFAIL;
}
if (ret != 0) {
return(ret);
} else if (temp[BLOCKSIZE-1] != MOTOR_ID) {
return(PFS_ERR_DEVICE);
}
if (!(pfs->status & PFS_MOTOR_INITIALIZED)) {
__osMakeMotorData(channel, &__MotorDataBuf[channel]);
}
pfs->status = PFS_MOTOR_INITIALIZED;
return(0);
}