示例#1
0
/*
 * Main program entry point.
 */
int main (void)
{
	AD1PCFG = 0xFFFF;

	//Initialize all of the LED pins
	LATE |= 0x000F;
	TRISE &= 0xFFF0;

	USBDeviceInit();	//usb_device.c.  Initializes USB module SFRs and firmware
    				//variables to known states.
	PMCON = 0;

	for (;;) {
		// Check bus status and service USB interrupts.
		USBDeviceTasks(); // Interrupt or polling method.  If using polling, must call
        			  // this function periodically.  This function will take care
        			  // of processing and responding to SETUP transactions
        			  // (such as during the enumeration process when you first
        			  // plug in).  USB hosts require that USB devices should accept
        			  // and process SETUP packets in a timely fashion.  Therefore,
        			  // when using polling, this function should be called
        			  // frequently (such as once about every 100 microseconds) at any
        			  // time that a SETUP packet might reasonably be expected to
        			  // be sent by the host to your device.  In most cases, the
        			  // USBDeviceTasks() function does not take very long to
        			  // execute (~50 instruction cycles) before it returns.

		// Application-specific tasks.
		// Blink the LEDs according to the USB device status
		BlinkUSBStatus();

		// User Application USB tasks
		if (USBDeviceState >= CONFIGURED_STATE && ! (U1PWRC & PIC32_U1PWRC_USUSPEND)) {
			unsigned nbytes_read;
			static unsigned char inbuf[64], outbuf[64];
			static unsigned led3_count = 0;

			// Pull in some new data if there is new data to pull in
			nbytes_read = getsUSBUSART ((char*) inbuf, 64);
			if (nbytes_read != 0) {
				snprintf (outbuf, sizeof(outbuf),
					"Received %d bytes: %02x...\r\n",
					nbytes_read, inbuf[0]);
				putUSBUSART ((char*) outbuf, strlen (outbuf));
				mLED_2_Toggle();
				mLED_3_On();
				led3_count = 10000;
			}
			if (led3_count) {
				// Turn off LED3 when timeout expired.
				led3_count--;
				if (led3_count == 0)
					mLED_3_Off();
			}

			CDCTxService();
		}
	}
}
示例#2
0
void FraiseSOF(void)
{
    static BYTE il;

    if((il++)==200) { il=0; mLED_2_Toggle(); }

    if(++PollCount==0) PollCount=255;
}
示例#3
0
void FlashOperation(UINT32 op, void* addr, UINT32 data32)
{
    //UINT32 status;

    // NVMADDR only accept Physical Address
    NVMADDR = ConvertToPhysicalAddress(addr);

    #if 0 && defined(DEBUG)
        SerialPrint("FlashOperation / ");
        if (op == FLASH_WORD_WRITE)
        {
            SerialPrint("Write Address 0x");
            SerialPrintNumber(NVMADDR,16);
            SerialPrint("\r\n");
            mLED_1_Toggle();
        }
        if (op == FLASH_PAGE_ERASE)
        {
            SerialPrint("Erase Address 0x");
            SerialPrintNumber(NVMADDR,16);
            SerialPrint("\r\n");
            mLED_2_Toggle();
        }
    #endif

    // Load data into NVMDATA register
    NVMDATA = data32;

    // Suspend or Disable all Interrupts
    //status = DisableInterrupt();

    // Enable Flash Write/Erase Operations

    // 1-Select Flash operation to perform
    // Enable writes to WR bit and LVD circuit
    NVMCON = _NVMCON_WREN_MASK | op;

    // 2-Wait for LVD to become stable (at least 6us).
    Delayus(7);

    // 3-Write unlock sequence before the WR bit is set
    NVMKEY = 0xAA996655;
    NVMKEY = 0x556699AA;

    // 4-Start the operation (WR=1)
    NVMCONSET = _NVMCON_WR_MASK;

    // 5-Wait for operation to complete (WR=0)
    while (NVMCON & _NVMCON_WR_MASK);

    // 6-Disable Flash Write/Erase operations
    NVMCONCLR = _NVMCON_WREN_MASK;

    // Restore Interrupts if necessary
    #if 0
    if (status & 1)
    {
        EnableInterrupt();
    }
    else
        DisableInterrupt();
    #endif

    #if 0
    // Return NVMERR and LVDERR Error Status Bits
    if (NVMCON & (_NVMCON_WRERR_MASK | _NVMCON_LVDERR_MASK))
    {
        while (1)
        {
            mLED_2_Toggle();
            DelayUs(500);
        }
    }
    #endif
}
示例#4
0
void SATURN3D_main (void) {
	SATURN3D_Init_Pins();
	mLED_1_Off();
	mLED_2_On();

	while(1) {
		while (!SATURN3D_SEL || !SATURN3D_REQ) { } //initial sync- both TR and TH are high 
		while (SATURN3D_SEL) { } //wait for this controller to be selected (active low)
		while (SATURN3D_REQ) { } //wait for data request

		mLED_1_Toggle();

		//first send the peripheral id
		//3D controller in digital mode id = 0000
		SATURN3D_D0 = SATURN3D_D1 = SATURN3D_D2 = SATURN3D_D3 = 0;
		SATURN3D_ACK = 0; //tell saturn we're clear to send

		mLED_2_Toggle();

		while (!SATURN3D_REQ && !SATURN3D_SEL) {} //wait for data request toggle

		//now send the data size
		//only 2 bytes of data for digital, so = 0010
		SATURN3D_D1 = 1;
		SATURN3D_ACK = 1; //tell saturn we're clear to send first nibble

		while (SATURN3D_REQ && !SATURN3D_SEL) {} //wait for data request toggle

		//now send directions, UDLR
		SATURN3D_D0 = Stick_Up;
		SATURN3D_D1 = Stick_Down;
		SATURN3D_D2 = Stick_Left;
		SATURN3D_D3 = Stick_Right;
		SATURN3D_ACK = 0;

		while (!SATURN3D_REQ && !SATURN3D_SEL) {} //wait for data request toggle

		//now send kick buttons, in order B/C/A/Start
		SATURN3D_D0 = Stick_Forward;
		SATURN3D_D1 = Stick_Roundhouse;
		SATURN3D_D2 = Stick_Short;
		SATURN3D_D3 = Stick_Start;
		SATURN3D_ACK = 1;
			
		while (SATURN3D_REQ && !SATURN3D_SEL) {} //wait for data request toggle

		//now send punch buttons, in order ZYXR
		SATURN3D_D0 = Stick_Fierce;
		SATURN3D_D1 = Stick_Strong;
		SATURN3D_D2 = Stick_Jab;
#ifdef EXTRA_BUTTONS
		SATURN3D_D3 = Stick_Extra1; //R
#else
		SATURN3D_D3 = 1;
#endif
		SATURN3D_ACK = 0;
		
		while (!SATURN3D_REQ && !SATURN3D_SEL) {} //wait for data request toggle

		//now send the final nibble of data
		SATURN3D_D0 = SATURN3D_D1 = SATURN3D_D2 = 1;
#ifdef EXTRA_BUTTONS
		SATURN3D_D3 = Stick_Extra0; //L
#else
		SATURN3D_D3 = 1;
#endif
		SATURN3D_ACK = 1;

		while (SATURN3D_REQ && !SATURN3D_SEL) {} //wait for data request toggle

		//we have an 'end byte' to send now
		//first nibble = 0000
		SATURN3D_D0 = SATURN3D_D1 = SATURN3D_D2 = SATURN3D_D3 = 0;
		SATURN3D_ACK = 0; //tell saturn we're clear to send

		while (!SATURN3D_REQ && !SATURN3D_SEL) {} //wait for data request toggle

		//second nibble = 0001
		SATURN3D_D0 = 1;
		SATURN3D_ACK = 1;

		//and we're done.
		FrameUpdate();
	}
}		
示例#5
0
文件: ps3usb.c 项目: fluxcorenz/UPCB
void PS3USB_ServiceUSB(void) {
	BUFDESC *buf_desc_ptr;

	if (UIRbits.UERRIF) { //if there's been a USB error of some sort...
		UEIR = 0x00;   //Clear out all of the error flags 
	} else if (UIRbits.SOFIF) { //if there's a start of frame error
		UIRbits.SOFIF = 0;      //clear it
	} else if (UIRbits.IDLEIF) {  //if there's an idle detect (3ms of silence)
		UIRbits.IDLEIF = 0;    //clear the flag
		UCONbits.SUSPND = 1;  //suspend the USB module
	} else if (UIRbits.ACTVIF) { //activity on the D+/D- lines
		UIRbits.ACTVIF = 0;     //clear the flag
		UCONbits.SUSPND = 0;    //and wakeup from suspend 
			/* what if not suspended? does the ACTIVF not get set for good communication?
				or is the Interrupt enable for the ACTVIF flag only enabled on suspend?
				Because the IDLE doesn't look like it sets it. */
	} else if (UIRbits.STALLIF) { //if a STALL handshake received..
		UIRbits.STALLIF = 0;		//clear it
	} else if (UIRbits.URSTIF) { //if we're told to reset
		/* drop everything. Set address to 0x00, flush every buffer
			enable the EP0 control buffer as normal. */
		USB_curr_config = 0x00;  
		UIRbits.TRNIF = 0;		// clear TRNIF four times to clear out the USTAT FIFO
		UIRbits.TRNIF = 0;
		UIRbits.TRNIF = 0;
		UIRbits.TRNIF = 0;
		UEP0 = 0x00;				// clear all EP control registers to disable all endpoints
		UEP1 = 0x00;
		UEP2 = 0x00;
		UEP3 = 0x00;
		UEP4 = 0x00;
		UEP5 = 0x00;
		UEP6 = 0x00;
		UEP7 = 0x00;
		UEP8 = 0x00;
		UEP9 = 0x00;
		UEP10 = 0x00;
		UEP11 = 0x00;
		UEP12 = 0x00;
		UEP13 = 0x00;
		UEP14 = 0x00;
		UEP15 = 0x00;   		
		BD0O.bytecount = PS3USB_EP0_PACKET_SIZE;
		BD0O.address = &PS3USB_EP0_OUT_buffer;	// EP0 OUT gets a buffer
		BD0O.status = 0x88;				// set UOWN bit (USB can write)
		BD0I.address = &PS3USB_EP0_IN_buffer;	// EP0 IN gets a buffer
		BD0I.status = 0x08;				// clear UOWN bit (MCU can write)
		UADDR = 0x00;				// set USB Address to 0
		UIR = 0x00;				// clear all the USB interrupt flags
		UEP0 = ENDPT_CONTROL;	// EP0 is a control pipe and requires an ACK
		UEIE = 0xFF;			// enable all error interrupts
		USB_USWSTAT = DEFAULT_STATE;
		USB_device_status = 0x01;	// self powered, remote wakeup disabled
	} else if (UIRbits.TRNIF) { //transcation complete flag. Some transaction has completed properly
			//need to check the stats to see what happened. 
	
		buf_desc_ptr = (BUFDESC *)((unsigned char *)(&BD0O)+(USTAT&0x7C));	// mask out bits 0, 1, and 7 of USTAT for offset into the buffer descriptor table
		//the masked buffer descriptor should tell us which endpoint something happened in.
		//Copy the contents of the buffer descriptor to the global variable so we can examine it.
		USB_buffer_desc.status = buf_desc_ptr->status;
		USB_buffer_desc.bytecount = buf_desc_ptr->bytecount;
		USB_buffer_desc.address = buf_desc_ptr->address;
		USB_USTAT = USTAT;		// save the USB status register
		UIRbits.TRNIF = 0;		// clear TRNIF interrupt flag
		USB_error_flags = 0x00;	// clear USB error flags
		
		//now that we have the details from the buffer descriptor, figure out what to do with it.
		
		switch (USB_buffer_desc.status&0x3C) {	// extract PID bits to see what kind of token it is.
				//and then process it.
			case TOKEN_SETUP:
				PS3USB_ProcessSetupToken();
				break;
			case TOKEN_IN:
				PS3USB_ProcessInToken();
				break;
			case TOKEN_OUT:
				PS3USB_ProcessOutToken();
				break;
			case TOKEN_ACK:
				break;	
			default:
				mLED_2_Toggle();
				mLED_2_Toggle();
				mLED_2_Toggle();
				break;
		}
		if (USB_error_flags&0x01) {		// if there was a Request Error...
			BD0O.bytecount = PS3USB_EP0_PACKET_SIZE;	// ...get ready to receive the next Setup token...
			BD0I.status = 0x84;
			BD0O.status = 0x84;					// ...and issue a protocol stall on EP0
		}
	}
}
示例#6
0
/******************************************************************************
 * Function:        void ProcessIO(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user routines.
 *                  It is a mixture of both USB and non-USB tasks.
 *
 * Note:            None
 *****************************************************************************/
void ProcessIO(void)
{   
    //Blink the LEDs according to the USB device status, but only do so if the PC application isn't connected and controlling the LEDs.
    if(blinkStatusValid)
    {
        BlinkUSBStatus();
    }

    //User Application USB tasks below.
    //Note: The user application should not begin attempting to read/write over the USB
    //until after the device has been fully enumerated.  After the device is fully
    //enumerated, the USBDeviceState will be set to "CONFIGURED_STATE".
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
    
    //As the device completes the enumeration process, the USBCBInitEP() function will
    //get called.  In this function, we initialize the user application endpoints (in this
    //example code, the user application makes use of endpoint 1 IN and endpoint 1 OUT).
    //The USBGenRead() function call in the USBCBInitEP() function initializes endpoint 1 OUT
    //and "arms" it so that it can receive a packet of data from the host.  Once the endpoint
    //has been armed, the host can then send data to it (assuming some kind of application software
    //is running on the host, and the application software tries to send data to the USB device).
    
    //If the host sends a packet of data to the endpoint 1 OUT buffer, the hardware of the SIE will
    //automatically receive it and store the data at the memory location pointed to when we called
    //USBGenRead().  Additionally, the endpoint handle (in this case USBGenericOutHandle) will indicate
    //that the endpoint is no longer busy.  At this point, it is safe for this firmware to begin reading
    //from the endpoint buffer, and processing the data.  In this example, we have implemented a few very
    //simple commands.  For example, if the host sends a packet of data to the endpoint 1 OUT buffer, with the
    //first byte = 0x80, this is being used as a command to indicate that the firmware should "Toggle LED(s)".
    if(!USBHandleBusy(USBGenericOutHandle))		//Check if the endpoint has received any data from the host.
    {   
        switch(OUTPacket[0])					//Data arrived, check what kind of command might be in the packet of data.
        {
            case 0x80:  //Toggle LED(s) command from PC application.
		        blinkStatusValid = FALSE;		//Disable the regular LED blink pattern indicating USB state, PC application is controlling the LEDs.
                if(mGetLED_1() == mGetLED_2())
                {
                    mLED_1_Toggle();
                    mLED_2_Toggle();
                }
                else
                {
                    mLED_1_On();
                    mLED_2_On();
                }
                break;
            case 0x81:  //Get push button state command from PC application.
                INPacket[0] = 0x81;				//Echo back to the host PC the command we are fulfilling in the first byte.  In this case, the Get Pushbutton State command.
//				if(sw2 == 1)					//pushbutton not pressed, pull up resistor on circuit board is pulling the PORT pin high
				if (UserSW == 1)
				{
					INPacket[1] = 0x01;			
				}
				else							//sw2 must be == 0, pushbutton is pressed and overpowering the pull up resistor
				{
					INPacket[1] = 0x00;
				}				
				//Now check to make sure no previous attempts to send data to the host are still pending.  If any attemps are still
				//pending, we do not want to write to the endpoint 1 IN buffer again, until the previous transaction is complete.
				//Otherwise the unsent data waiting in the buffer will get overwritten and will result in unexpected behavior.    
                if(!USBHandleBusy(USBGenericInHandle))		
	            {	
		            //The endpoint was not "busy", therefore it is safe to write to the buffer and arm the endpoint.					
	                //The USBGenWrite() function call "arms" the endpoint (and makes the handle indicate the endpoint is busy).
	                //Once armed, the data will be automatically sent to the host (in hardware by the SIE) the next time the 
	                //host polls the endpoint.  Once the data is successfully sent, the handle (in this case USBGenericInHandle) 
	                //will indicate the the endpoint is no longer busy.
					USBGenericInHandle = USBGenWrite(USBGEN_EP_NUM,(BYTE*)&INPacket,USBGEN_EP_SIZE);	
                }
                break;
        }
        
        //Re-arm the OUT endpoint for the next packet:
	    //The USBGenRead() function call "arms" the endpoint (and makes it "busy").  If the endpoint is armed, the SIE will 
	    //automatically accept data from the host, if the host tries to send a packet of data to the endpoint.  Once a data 
	    //packet addressed to this endpoint is received from the host, the endpoint will no longer be busy, and the application
	    //can read the data which will be sitting in the buffer.
        USBGenericOutHandle = USBGenRead(USBGEN_EP_NUM,(BYTE*)&OUTPacket,USBGEN_EP_SIZE);
    }
}//end ProcessIO
示例#7
0
/******************************************************************************
 * Function:        void ProcessIO(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user
 *                  routines. It is a mixture of both USB and
 *                  non-USB tasks.
 *
 * Note:            None
 *****************************************************************************/
void ProcessIO(void)
{   
    //Blink the LEDs according to the USB device status, but only do so if the PC application isn't connected and controlling the LEDs.
    if(blinkStatusValid)
    {
        BlinkUSBStatus();
    }

    // Check if the device is enumerated and is ready to accept commands.
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;

    if(!USBHandleBusy(USBGenericOutHandle))		//Check if the endpoint has received any data from the host.
    {   
        switch(OUTPacket[0])					//Data arrived, check what kind of command might be in the packet of data.
        {
			case 'A':
			if(!USBHandleBusy(USBGenericInHandle))		
	            {
					if(OUTPacket[1] == '0')
					{
						ADCON0bits.ADON = 0;	// Switch off ADC.
						ADCON0 &= 0xF0;			// Select channel 0
						ADCON0bits.ADON = 1;
						ADCON0bits.GO = 1;
						// Wait if conversion is happening
						while(ADCON0bits.DONE);
						INPacket[0] = ADRESL;
						INPacket[1] = ADRESH;
					}
					else if(OUTPacket[1] == '1')
					{
						ADCON0bits.ADON = 0;	// Switch off ADC.
						ADCON0 &= 0xF0;			// Select channel 
						ADCON0bits.CHS0 = 1;
						ADCON0bits.ADON = 1;
						ADCON0bits.GO = 1;
						// Wait if conversion is happening
						while(ADCON0bits.DONE);
						INPacket[0] = ADRESL;
						INPacket[1] = ADRESH;
					}
					USBGenericInHandle = USBGenWrite(USBGEN_EP_NUM,(BYTE*)&INPacket,USBGEN_EP_SIZE);	
                }	
				break;
            case 0x80:  //Toggle LED(s) command from PC application.
		        blinkStatusValid = FALSE;		//Disable the regular LED blink pattern indicating USB state, PC application is controlling the LEDs.
                if(mGetLED_1() == mGetLED_2())
                {
                    mLED_1_Toggle();
                    mLED_2_Toggle();
                }
                else
                {
                    mLED_1_On();
                    mLED_2_On();
                }
                break;
            case 0x81:  //Get push button state command from PC application.
                if(!USBHandleBusy(USBGenericInHandle))		
	            {	
		            //The endpoint was not "busy", therefore it is safe to write to the buffer and arm the endpoint.					
                    INPacket[0] = 0x81;				//Echo back to the host PC the command we are fulfilling in the first byte.  In this case, the Get Pushbutton State command.
    				if(sw2 == 1)					//pushbutton not pressed, pull up resistor on circuit board is pulling the PORT pin high
    				{
    					INPacket[1] = 0x01;			
    				}
    				else							//sw2 must be == 0, pushbutton is pressed and overpowering the pull up resistor
    				{
    					INPacket[1] = 0x00;
    				}				
	                // Arm back the handle.
					USBGenericInHandle = USBGenWrite(USBGEN_EP_NUM,(BYTE*)&INPacket,USBGEN_EP_SIZE);	
                }
                break;
        }
        
        USBGenericOutHandle = USBGenRead(USBGEN_EP_NUM,(BYTE*)&OUTPacket,USBGEN_EP_SIZE);
    }
}//end ProcessIO