First Release

Dependencies:   USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NiseKabuto.cpp Source File

NiseKabuto.cpp

00001 #include "NiseKabuto.h"
00002 #include "InputDeviceDetector.h"
00003 
00004 // _InputTypeとInputStatus.InputDeviceTypeの違いは????
00005 
00006 /**
00007 --------------------------------------------------
00008 
00009  Constructor
00010 
00011 --------------------------------------------------
00012 **/
00013 NiseKabuto::NiseKabuto(PinName inputPins[], PinName outputPins[], PinName configurePins[])
00014  : 
00015     _ConfigSwitch(
00016         configurePins[0],configurePins[1],configurePins[2],configurePins[3],
00017         configurePins[4],configurePins[5],configurePins[6],configurePins[7]
00018     )
00019 {
00020     InitInterruptPriority();
00021 //  InitTimer0();
00022     
00023     // Pin Setting
00024     _ConfigSwitch.mode(PullUp);
00025     
00026     // Detect Input device
00027     InputDeviceDetector myDetector = InputDeviceDetector(inputPins);
00028     _InputType = myDetector.GetInputType();
00029     //printf("_InputType: %d\n",_InputType & 0xff);
00030     
00031 
00032     // Initialize  input instance
00033     switch(_InputType)
00034     {
00035         case CONFIG_INMODE_CYBERSTICK_ANALOG:
00036         case CONFIG_INMODE_CYBERSTICK_DIGITAL:
00037             // Initialize Input: CyberStick
00038             InitInput_CyberStick(inputPins);
00039             break;
00040             
00041         case CONFIG_INMODE_MD6B:
00042             // Initialize Input: MD6B
00043             InitInput_MD6B(inputPins);
00044             break;
00045 
00046         default:
00047             InitInput_MD6B(inputPins);
00048             break;
00049     }
00050 
00051     /*
00052     wait_ms(5000);
00053     printf("_ConfigSwitch: %d\n",_ConfigSwitch & 0xff);
00054     */
00055     
00056     // Save output mode
00057     //printf("CS:%02x\n\r",~_ConfigSwitch);
00058     _OutputType = (~_ConfigSwitch) & 0x0ff;
00059 
00060     // Initialize output instance
00061     switch(_OutputType)
00062     {
00063         case CONFIGPIN_OUTMODE_SSMULCON:
00064             // Initialize Output: SS Multi Controller
00065             InitOutput_SSMulCon(outputPins);
00066             break;
00067             
00068         case CONFIGPIN_OUTMODE_PCE:
00069             // Initialize Output: PCE Analog/Digital Controller
00070             InitOutput_PCE(outputPins);
00071             break;
00072 
00073         case CONFIGPIN_OUTMODE_FC:
00074             // Initialize Output: Famicom Controller
00075             InitOutput_FC(outputPins);
00076             break;
00077         case CONFIGPIN_OUTMODE_MD:
00078             // Initialize Output: Megadrive analog joystick
00079             InitOutput_MD(outputPins);
00080             break;
00081         default:
00082             // Initialize Output: PlayStation3 USB Joystick
00083             InitOutput_PS3USB();
00084             break;
00085     }
00086 }
00087 
00088 
00089 /**
00090 --------------------------------------------------
00091 
00092  Initialize Input Device: CyberStick
00093 
00094 --------------------------------------------------
00095 **/
00096 void NiseKabuto::InitInput_CyberStick(PinName inputPins[])
00097 {
00098     // Create In_CyberStick instance
00099     _In_CyberStick = new In_CyberStick(
00100                     inputPins[0],inputPins[1],inputPins[2],inputPins[3],
00101                     inputPins[4],inputPins[5],inputPins[6],&_InputStatus);
00102 }
00103 
00104 
00105 
00106 /**
00107 --------------------------------------------------
00108 
00109  Initialize Input Device: Fighting Pad 6B
00110 
00111 --------------------------------------------------
00112 **/
00113 void NiseKabuto::InitInput_MD6B(PinName inputPins[])
00114 {
00115     // Create In_CyberStick instance
00116     _In_MD6B= new In_MD6B(
00117                     inputPins[0],inputPins[1],inputPins[2],inputPins[3],
00118                     inputPins[4],inputPins[5],inputPins[6],&_InputStatus);
00119 }
00120 
00121 
00122 
00123 /**
00124 --------------------------------------------------
00125 
00126  Initialize Output Device: Megadrive analog/digital joystick
00127 
00128 --------------------------------------------------
00129 **/
00130 void NiseKabuto::InitOutput_MD(PinName outputPins[])
00131 {
00132     // Create Out_MD instance
00133     _Out_MD = new Out_MD(
00134                     outputPins[0],outputPins[1],outputPins[2],outputPins[3],
00135                     outputPins[4],outputPins[5],outputPins[6],
00136                     &_InputStatus
00137                 );
00138     
00139     if( _InputType == CONFIG_INMODE_CYBERSTICK_ANALOG || _InputType == CONFIG_INMODE_CYBERSTICK_DIGITAL) 
00140     {
00141         _Out_MD->SetupInputControll(_In_CyberStick, &In_CyberStick::StartReading, &In_CyberStick::StopReading);
00142     }
00143     else if( _InputType == CONFIG_INMODE_MD6B )
00144     {
00145         _Out_MD->SetupInputControll(_In_MD6B, &In_MD6B::StartReading, &In_MD6B::StopReading);
00146     }
00147 }
00148 
00149 /**
00150 --------------------------------------------------
00151 
00152  Initialize Output Device: SS Multi Controller
00153 
00154 --------------------------------------------------
00155 **/
00156 void NiseKabuto::InitOutput_SSMulCon(PinName outputPins[])
00157 {
00158     // Create Out_SSMulCon instance
00159     _Out_SSMulCon = new Out_SSMulCon(
00160                     outputPins[0],outputPins[1],outputPins[2],outputPins[3],
00161                     outputPins[4],outputPins[5],outputPins[6],
00162                     &_InputStatus
00163                 );
00164 
00165     if( _InputType == CONFIG_INMODE_CYBERSTICK_ANALOG || _InputType == CONFIG_INMODE_CYBERSTICK_DIGITAL) 
00166     {
00167         _Out_SSMulCon->SetupInputControll(_In_CyberStick, &In_CyberStick::StartReading, &In_CyberStick::StopReading);
00168     }
00169     else if( _InputType == CONFIG_INMODE_MD6B )
00170     {
00171         _Out_SSMulCon->SetupInputControll(_In_MD6B, &In_MD6B::StartReading, &In_MD6B::StopReading);
00172     }
00173 }
00174 
00175 /**
00176 --------------------------------------------------
00177 
00178  Initialize Output Device: PCE Analog/Digital Controller
00179 
00180 --------------------------------------------------
00181 **/
00182 void NiseKabuto::InitOutput_PCE(PinName outputPins[])
00183 {
00184     // Create Out_PCEAnalog instance
00185     _Out_PCE = new Out_PCE(
00186                     outputPins[0],outputPins[1],outputPins[2],outputPins[3],
00187                     outputPins[4],outputPins[5],outputPins[6],
00188                     &_InputStatus
00189                 );
00190 
00191     if( _InputType == CONFIG_INMODE_CYBERSTICK_ANALOG || _InputType == CONFIG_INMODE_CYBERSTICK_DIGITAL) 
00192     {
00193         _Out_PCE->SetupInputControll(_In_CyberStick, &In_CyberStick::StartReading, &In_CyberStick::StopReading);
00194     }
00195     else if( _InputType == CONFIG_INMODE_MD6B )
00196     {
00197         _Out_PCE->SetupInputControll(_In_MD6B, &In_MD6B::StartReading, &In_MD6B::StopReading);
00198     }
00199 }
00200 
00201 
00202 /**
00203 --------------------------------------------------
00204 
00205  Initialize Output Device: Famicom Controller
00206 
00207 --------------------------------------------------
00208 **/
00209 void NiseKabuto::InitOutput_FC(PinName outputPins[])
00210 {
00211     // Create Out_FC instance
00212     _Out_FC = new Out_FC(
00213                     outputPins[0],outputPins[1],outputPins[2],outputPins[3],
00214                     &_InputStatus
00215                 );
00216 
00217     if( _InputType == CONFIG_INMODE_CYBERSTICK_ANALOG || _InputType == CONFIG_INMODE_CYBERSTICK_DIGITAL) 
00218     {
00219         _Out_FC->SetupInputControll(_In_CyberStick, &In_CyberStick::StartReading, &In_CyberStick::StopReading);
00220     }
00221     else if( _InputType == CONFIG_INMODE_MD6B )
00222     {
00223         _Out_FC->SetupInputControll(_In_MD6B, &In_MD6B::StartReading, &In_MD6B::StopReading);
00224     }
00225 }
00226 
00227 /**
00228 --------------------------------------------------
00229 
00230  Initialize Output Device: PlayStation3 USB Joystick
00231 
00232 --------------------------------------------------
00233 **/
00234 void NiseKabuto::InitOutput_PS3USB(void)
00235 {
00236     // Create Out_PS3USB instance
00237     _Out_PS3USB = new Out_PS3USB(
00238                     &_InputStatus
00239                 );
00240 
00241     if( _InputType == CONFIG_INMODE_CYBERSTICK_ANALOG || _InputType == CONFIG_INMODE_CYBERSTICK_DIGITAL) 
00242     {
00243         _Out_PS3USB->SetupInputControll(_In_CyberStick, &In_CyberStick::StartReading, &In_CyberStick::StopReading);
00244     }
00245     else if( _InputType == CONFIG_INMODE_MD6B )
00246     {
00247         _Out_PS3USB->SetupInputControll(_In_MD6B, &In_MD6B::StartReading, &In_MD6B::StopReading);
00248     }
00249 }
00250 
00251 
00252 /**
00253 --------------------------------------------------
00254 
00255  Show (for test)
00256 
00257 --------------------------------------------------
00258 **/
00259 
00260 void NiseKabuto::Show()
00261 {
00262     char    strBuf[100];
00263 
00264     __disable_irq();
00265     int val = _InputStatus.Buttons;
00266     int ch0 = _InputStatus.Ch0;
00267     int ch1 = _InputStatus.Ch1;
00268     int ch2 = _InputStatus.Ch2;
00269     int ch3 = _InputStatus.Ch3;
00270     char mode = _InputStatus.InputDeviceType;
00271     char temp0 = _InputStatus.Temp[0];
00272     char temp1 = _InputStatus.Temp[1];
00273     char temp2 = _InputStatus.Temp[2];
00274     char temp3 = _InputStatus.Temp[3];
00275     char temp4 = _InputStatus.Temp[4];
00276     char temp5 = _InputStatus.Temp[5];
00277     __enable_irq();
00278     
00279     strBuf[0]=0;
00280 
00281     switch(mode)
00282     {
00283         case CONFIG_INMODE_CYBERSTICK_ANALOG:
00284         case CONFIG_INMODE_CYBERSTICK_DIGITAL:
00285             strcat(strBuf, "CS");
00286             if( mode==CONFIG_INMODE_CYBERSTICK_ANALOG )
00287             {
00288                 strcat(strBuf, "AN");
00289             }
00290             else
00291             {
00292                 strcat(strBuf, "DG");
00293             }
00294             break;
00295             
00296         case CONFIG_INMODE_MD6B:
00297             strcat(strBuf, "6B");
00298             break;
00299     }
00300 
00301     switch(_OutputType)
00302     {
00303         case CONFIGPIN_OUTMODE_SSMULCON:
00304             strcat(strBuf, "->SSMC ");
00305             break;
00306             
00307         case CONFIGPIN_OUTMODE_PCE:
00308             strcat(strBuf, "->PCE ");
00309             break;
00310 
00311         case CONFIGPIN_OUTMODE_FC:
00312             strcat(strBuf, "->Famicom ");
00313             break;
00314 
00315         case CONFIGPIN_OUTMODE_MD:
00316             strcat(strBuf, "->MD ");
00317             break;      
00318 
00319         default:
00320             strcat(strBuf, "->PS3USB ");
00321             break;      
00322     }
00323     
00324     switch(mode)
00325     {
00326         case CONFIG_INMODE_CYBERSTICK_ANALOG:
00327         case CONFIG_INMODE_CYBERSTICK_DIGITAL:
00328             strcat(strBuf, val & 0x8000? "---" : "Tup");
00329             strcat(strBuf, val & 0x4000? "---" : "Tdw");
00330             strcat(strBuf, val & 0x2000? "-" : "U");
00331             strcat(strBuf, val & 0x1000? "-" : "D");
00332             strcat(strBuf, val & 0x0800? "-" : "L");
00333             strcat(strBuf, val & 0x0400? "-" : "R");
00334             strcat(strBuf, val & 0x0200? "-" : "A");
00335             strcat(strBuf, val & 0x0100? "-" : "B");
00336             strcat(strBuf, val & 0x0020? "-" : "C");
00337             strcat(strBuf, val & 0x0010? "-" : "D");
00338             strcat(strBuf, val & 0x0008? "--" : "E1");
00339             strcat(strBuf, val & 0x0004? "--" : "E2");
00340             strcat(strBuf, val & 0x0002? "---" : "Fst");
00341             strcat(strBuf, val & 0x0001? "---" : "Gsl");
00342             strcat(strBuf, val & 0x0080? "--" : "A'");
00343             strcat(strBuf, val & 0x0040? "--" : "B'");
00344             printf("%s ",strBuf);
00345 
00346             printf("CH0:%02X ",   ch0);
00347             printf("CH1:%02X ",   ch1);
00348             printf("CH2:%02X ",   ch2);
00349             printf("CH3:%02X ",   ch3);
00350             /*
00351             printf("\r\ni:%02x o:%02x %02x %02x ", temp0, temp2, temp5, temp4); // debug(in, out functions)
00352             printf("tc(%u) mr0(%u) ", LPC_TIM3->TC, LPC_TIM3->MR0);             // debug(TIMER3)
00353             */
00354             printf("\r\n");
00355 
00356             break;
00357 
00358         case CONFIG_INMODE_MD6B:
00359             strcat(strBuf, val & 0x002000? "-" : "U");
00360             strcat(strBuf, val & 0x001000? "-" : "D");
00361             strcat(strBuf, val & 0x000800? "-" : "L");
00362             strcat(strBuf, val & 0x000400? "-" : "R");
00363             strcat(strBuf, val & 0x000200? "-" : "A");
00364             strcat(strBuf, val & 0x000100? "-" : "B");
00365             strcat(strBuf, val & 0x000020? "-" : "C");
00366             strcat(strBuf, val & 0x040000? "-" : "X");
00367             strcat(strBuf, val & 0x020000? "-" : "Y");
00368             strcat(strBuf, val & 0x010000? "-" : "Z");
00369             strcat(strBuf, val & 0x000002? "-" : "S");
00370             strcat(strBuf, val & 0x000001? "-" : "M");
00371             printf("%s ",strBuf);
00372             //printf("%02x %02x %02x %02x %02x %02x", temp0, temp1, temp2, temp3, temp4, temp5);
00373             /*
00374             printf("i:%02x o:%02x %02x %02x ", temp0, temp2, temp5, temp4);
00375             printf("TIM3 tc(%u) mr0(%u) diff(%d)", 
00376                 LPC_TIM3->TC, 
00377                 LPC_TIM3->MR0, 
00378                 int(LPC_TIM3->MR0) - int(LPC_TIM3->TC) );
00379             */
00380             printf("\r\n");
00381             /*
00382             printf("\r\nIR:%08x,TCR:%08x,PR:%08x,PC:%08x,MCR:%08x,EMR:%08x,CTCR:%08x tc(%u) mr0(%u) diff(%d) pending:%u ", 
00383             //  NVIC_GetActive(TIMER3_IRQn),
00384                 LPC_TIM3->IR, 
00385                 LPC_TIM3->TCR, 
00386                 LPC_TIM3->PR, 
00387                 LPC_TIM3->PC, 
00388                 LPC_TIM3->MCR, 
00389                 LPC_TIM3->EMR, 
00390                 LPC_TIM3->CTCR, 
00391                 LPC_TIM3->TC, 
00392                 LPC_TIM3->MR0, 
00393                 int(LPC_TIM3->MR0) - int(LPC_TIM3->TC), 
00394                 ( NVIC->ISPR[0] & ( 1<<4 ) ) );
00395             printf("\r\n");
00396             */
00397             break;
00398     }
00399 
00400     //
00401     // ここで実行させるのはおかしいが、暫定的に
00402     //
00403     if( (int(LPC_TIM3->MR0) - int(LPC_TIM3->TC)) <0 )
00404     {
00405         printf("=================================================\r\n");
00406         printf("                   Resetting MR0 !!!!            \r\n");
00407         printf("=================================================\r\n");
00408         LPC_TIM3->MR0 = LPC_TIM3->TC + 1000;
00409     }
00410 
00411 }
00412 
00413 void NiseKabuto::InitInterruptPriority(void)
00414 {
00415     // http://mbed.org/users/earlz/code/MbedConsole/file/370b9e559f92/main.cpp
00416     
00417     NVIC_SetPriority(NonMaskableInt_IRQn, 100 ); 
00418     NVIC_SetPriority(MemoryManagement_IRQn, 100);
00419     
00420     NVIC_SetPriority(BusFault_IRQn, 100);
00421     NVIC_SetPriority(UsageFault_IRQn, 100);
00422     NVIC_SetPriority(SVCall_IRQn, 100);
00423     NVIC_SetPriority(DebugMonitor_IRQn, 100);
00424     NVIC_SetPriority(PendSV_IRQn, 100);
00425     NVIC_SetPriority(SysTick_IRQn, 50);
00426     NVIC_SetPriority(WDT_IRQn, 100);
00427     NVIC_SetPriority(TIMER0_IRQn, 85);
00428     NVIC_SetPriority(TIMER1_IRQn, 85);
00429     NVIC_SetPriority(TIMER2_IRQn, 85);
00430     NVIC_SetPriority(TIMER3_IRQn, 85);
00431     NVIC_SetPriority(UART0_IRQn, 75);
00432     NVIC_SetPriority(UART1_IRQn, 100);
00433     NVIC_SetPriority(UART2_IRQn, 100);
00434     NVIC_SetPriority(UART3_IRQn, 100);
00435     
00436     NVIC_SetPriority(PWM1_IRQn, 100);
00437     NVIC_SetPriority(I2C0_IRQn, 100);
00438     NVIC_SetPriority(I2C1_IRQn, 100);
00439     NVIC_SetPriority(I2C2_IRQn, 100);
00440     NVIC_SetPriority(SPI_IRQn, 100);
00441     NVIC_SetPriority(SSP0_IRQn, 100);
00442     NVIC_SetPriority(SSP1_IRQn, 100);
00443     NVIC_SetPriority(PLL0_IRQn, 100);
00444     NVIC_SetPriority(RTC_IRQn, 100);
00445     NVIC_SetPriority(EINT0_IRQn, 100);
00446     NVIC_SetPriority(EINT1_IRQn, 100);
00447     
00448     NVIC_SetPriority(EINT2_IRQn, 100);
00449     NVIC_SetPriority(EINT3_IRQn, 20);    // 最高
00450     NVIC_SetPriority(ADC_IRQn, 100);
00451     NVIC_SetPriority(BOD_IRQn, 100);
00452     NVIC_SetPriority(USB_IRQn, 100);
00453     NVIC_SetPriority(CAN_IRQn, 100);
00454     NVIC_SetPriority(DMA_IRQn, 100);
00455     
00456     NVIC_SetPriority(I2S_IRQn, 100);
00457     NVIC_SetPriority(ENET_IRQn, 100);
00458     NVIC_SetPriority(RIT_IRQn, 100);
00459     NVIC_SetPriority(MCPWM_IRQn, 100);
00460     NVIC_SetPriority(QEI_IRQn, 100);
00461     NVIC_SetPriority(PLL1_IRQn, 100);
00462     
00463 }
00464 
00465 
00466 void NiseKabuto::WaitUs(uint32_t nus)
00467 {
00468     uint32_t countNop, countNopMAX;
00469     countNopMAX = 24;   // 96MHz動作のmbedで、1usになるような値にする
00470     
00471     for(uint32_t i=0; i<nus; i++)
00472     {
00473         countNop = countNopMAX;
00474         while(countNop--)
00475         {
00476             __nop();
00477         }
00478     }
00479 
00480 }
00481