motor.c (5061B)
1 #include "PR/os_message.h" 2 #include "PR/os_pi.h" 3 #include "libultra_internal.h" 4 #include "controller.h" 5 #include "macros.h" 6 7 #ifdef VERSION_CN 8 9 s32 __osMotorAccess(UNUSED OSPfs *pfs, UNUSED s32 action) { 10 return PFS_ERR_INVALID; 11 } 12 13 s32 osMotorInit(UNUSED OSMesgQueue *mq, UNUSED OSPfs *pfs, UNUSED int channel) { 14 return PFS_ERR_DEVICE; 15 } 16 17 #else 18 19 void _MakeMotorData(int channel, u16 address, u8 *buffer, OSPifRam *mdata); 20 u32 __osMotorinitialized[MAXCONTROLLERS] = { 0, 0, 0, 0 }; 21 OSPifRam _MotorStopData[MAXCONTROLLERS]; 22 OSPifRam _MotorStartData[MAXCONTROLLERS]; 23 u8 _motorstopbuf[32]; 24 u8 _motorstartbuf[32]; 25 26 s32 osMotorStop(OSPfs *pfs) { 27 int i; 28 s32 ret; 29 u8 *ptr; 30 __OSContRamReadFormat ramreadformat; 31 ptr = (u8 *) &__osPfsPifRam; 32 33 if (!__osMotorinitialized[pfs->channel]) { 34 return PFS_ERR_INVALID; 35 } 36 __osSiGetAccess(); 37 38 __osContLastCmd = CONT_CMD_WRITE_MEMPACK; 39 __osSiRawStartDma(OS_WRITE, &_MotorStopData[pfs->channel]); 40 osRecvMesg(pfs->queue, NULL, OS_MESG_BLOCK); 41 ret = __osSiRawStartDma(OS_READ, &__osPfsPifRam); 42 osRecvMesg(pfs->queue, NULL, OS_MESG_BLOCK); 43 ptr = (u8 *) &__osPfsPifRam; 44 45 if (pfs->channel != 0) { 46 for (i = 0; i < pfs->channel; i++) { 47 ptr++; 48 } 49 } 50 51 ramreadformat = *(__OSContRamReadFormat *) ptr; 52 ret = CHNL_ERR(ramreadformat); 53 if (ret == 0 && ramreadformat.datacrc != __osContDataCrc((u8 *) &_motorstopbuf)) { 54 ret = PFS_ERR_CONTRFAIL; 55 } 56 __osSiRelAccess(); 57 return ret; 58 } 59 60 s32 osMotorStart(OSPfs *pfs) { 61 int i; 62 s32 ret; 63 u8 *ptr; 64 __OSContRamReadFormat ramreadformat; 65 66 ptr = (u8 *) &__osPfsPifRam; 67 68 if (!__osMotorinitialized[pfs->channel]) { 69 return PFS_ERR_INVALID; 70 } 71 72 __osSiGetAccess(); 73 74 __osContLastCmd = CONT_CMD_WRITE_MEMPACK; 75 __osSiRawStartDma(OS_WRITE, &_MotorStartData[pfs->channel]); 76 osRecvMesg(pfs->queue, NULL, OS_MESG_BLOCK); 77 ret = __osSiRawStartDma(OS_READ, &__osPfsPifRam); 78 osRecvMesg(pfs->queue, NULL, OS_MESG_BLOCK); 79 ptr = (u8 *) &__osPfsPifRam; 80 81 if (pfs->channel != 0) { 82 for (i = 0; i < pfs->channel; i++) { 83 ptr++; 84 } 85 } 86 87 ramreadformat = *(__OSContRamReadFormat *) ptr; 88 ret = CHNL_ERR(ramreadformat); 89 if (ret == 0 && ramreadformat.datacrc != __osContDataCrc((u8 *) &_motorstartbuf)) { 90 ret = PFS_ERR_CONTRFAIL; 91 } 92 __osSiRelAccess(); 93 return ret; 94 } 95 96 void _MakeMotorData(int channel, u16 address, u8 *buffer, OSPifRam *mdata) { 97 u8 *ptr; 98 __OSContRamReadFormat ramreadformat; 99 int i; 100 101 ptr = (u8 *) mdata->ramarray; 102 for (i = 0; i < ARRAY_COUNT(mdata->ramarray); i++) { 103 mdata->ramarray[i] = 0; 104 } 105 mdata->pifstatus = CONT_CMD_EXE; 106 ramreadformat.dummy = CONT_CMD_NOP; 107 ramreadformat.txsize = CONT_CMD_WRITE_MEMPACK_TX; 108 ramreadformat.rxsize = CONT_CMD_WRITE_MEMPACK_RX; 109 ramreadformat.cmd = CONT_CMD_WRITE_MEMPACK; 110 111 ramreadformat.address = (address << 0x5) | __osContAddressCrc(address); 112 ramreadformat.datacrc = CONT_CMD_NOP; 113 for (i = 0; i < ARRAY_COUNT(ramreadformat.data); i++) { 114 ramreadformat.data[i] = *buffer++; 115 } 116 if (channel != 0) { 117 for (i = 0; i < channel; i++) { 118 *ptr++ = 0; 119 } 120 } 121 *(__OSContRamReadFormat *) ptr = ramreadformat; 122 ptr += sizeof(__OSContRamReadFormat); 123 ptr[0] = CONT_CMD_END; 124 } 125 126 s32 osMotorInit(OSMesgQueue *mq, OSPfs *pfs, int channel) { 127 int i; 128 s32 ret; 129 u8 temp[32]; 130 pfs->queue = mq; 131 pfs->channel = channel; 132 pfs->status = 0; 133 pfs->activebank = 128; 134 135 for (i = 0; i < ARRAY_COUNT(temp); i++) { 136 temp[i] = 254; 137 } 138 139 ret = __osContRamWrite(mq, channel, 1024, temp, FALSE); 140 if (ret == 2) { // TODO: remove magic constant 141 ret = __osContRamWrite(mq, channel, 1024, temp, FALSE); 142 } 143 if (ret != 0) { 144 return ret; 145 } 146 147 ret = __osContRamRead(mq, channel, 1024, temp); 148 if (ret == 2) { 149 ret = PFS_ERR_CONTRFAIL; // is this right? 150 } 151 if (ret != 0) { 152 return ret; 153 } 154 if (temp[31] == 254) { 155 return PFS_ERR_DEVICE; 156 } 157 158 for (i = 0; i < ARRAY_COUNT(temp); i++) { 159 temp[i] = 128; 160 } 161 162 ret = __osContRamWrite(mq, channel, 1024, temp, FALSE); 163 if (ret == 2) { 164 ret = __osContRamWrite(mq, channel, 1024, temp, FALSE); 165 } 166 if (ret != 0) { 167 return ret; 168 } 169 170 ret = __osContRamRead(mq, channel, 1024, temp); 171 if (ret == 2) { 172 ret = PFS_ERR_CONTRFAIL; 173 } 174 if (ret != 0) { 175 return ret; 176 } 177 if (temp[31] != 128) { 178 return PFS_ERR_DEVICE; 179 } 180 181 if (!__osMotorinitialized[channel]) { 182 for (i = 0; i < ARRAY_COUNT(_motorstartbuf); i++) { 183 _motorstartbuf[i] = 1; 184 _motorstopbuf[i] = 0; 185 } 186 _MakeMotorData(channel, 1536, _motorstartbuf, &_MotorStartData[channel]); 187 _MakeMotorData(channel, 1536, _motorstopbuf, &_MotorStopData[channel]); 188 __osMotorinitialized[channel] = 1; 189 } 190 return 0; 191 } 192 193 #endif