void nrf_send_frame (uint8_t * frame, int send_mode) { int ret; if (mode != MODE_NOMODE) return; UART2PutStr ("sf\n\r"); nrf_write_reg (R_CONFIG, R_CONFIG_PWR_UP | R_CONFIG_EN_CRC); NRF_CS_LOW (); mLED_2_On (); SPI2_xmit (C_W_TX_PAYLOAD); SPI2_transmit (frame, 32); mLED_2_Off (); NRF_CS_HIGH (); NRF_CE_HIGH (); while (1) { ret = nrf_read_reg (R_FIFO_STATUS); if (send_mode) { if ((ret & R_FIFO_STATUS_TX_EMPTY) == R_FIFO_STATUS_TX_EMPTY) break; } else { if ((ret & R_FIFO_STATUS_TX_FULL) == 0) break; } } NRF_CE_LOW (); }
void BlinkUSBStatus(void) { static WORD led_count = 0; if (led_count == 0) led_count = 10000U; led_count--; #define mLED_Both_Off() \ { \ mLED_1_Off(); \ mLED_2_Off(); \ } #define mLED_Both_On() \ { \ mLED_1_On(); \ mLED_2_On(); \ } #define mLED_Only_1_On() \ { \ mLED_1_On(); \ mLED_2_Off(); \ } #define mLED_Only_2_On() \ { \ mLED_1_Off(); \ mLED_2_On(); \ } if (USBSuspendControl == 1) { if (led_count == 0) { mLED_1_Toggle(); if (mGetLED_1()) { mLED_2_On(); } else { mLED_2_Off(); } } } else { if (USBDeviceState == DETACHED_STATE) { mLED_Both_Off(); } else if (USBDeviceState == ATTACHED_STATE) { mLED_Both_On(); } else if (USBDeviceState == POWERED_STATE) { mLED_Only_1_On(); } else if (USBDeviceState == DEFAULT_STATE) { mLED_Only_2_On(); } else if (USBDeviceState == ADDRESS_STATE) { if (led_count == 0) { mLED_1_Toggle(); mLED_2_Off(); } } else if (USBDeviceState == CONFIGURED_STATE) { if (led_count < 100) { mLED_Both_On(); } else { mLED_Both_Off(); } } } }
void usb_tty_status(void) { static WORD led_count = 0; if (led_count == 0)led_count = 4U; led_count--; // verifica UCONbits.SUSPND if (USBSuspendControl == 1) { if (led_count == 0) { mLED_1_Toggle(); if (mGetLED_1()) { mLED_2_On(); } else { mLED_2_Off(); } } } else { if (USBDeviceState == DETACHED_STATE) { mLED_Both_Off(); } else if (USBDeviceState == ATTACHED_STATE) { mLED_Both_On(); } else if (USBDeviceState == POWERED_STATE) { mLED_Only_1_On(); } else if (USBDeviceState == DEFAULT_STATE) { mLED_Only_2_On(); } else if (USBDeviceState == ADDRESS_STATE) { if (led_count == 0) { mLED_1_Toggle(); mLED_2_Off(); } } else if (USBDeviceState == CONFIGURED_STATE) { if (led_count == 0) { mLED_1_Toggle(); if (mGetLED_1()) { mLED_2_Off(); } else { mLED_2_On(); } } } } }
void USB_blinkStatus(void) { static WORD led_count = 0; if (led_count == 0)led_count = 2000U; // Ajustado para USB low speed led_count--; if (USBSuspendControl == 1) { if (led_count == 0) { mLED_1_Toggle(); if (mGetLED_1()) { mLED_2_On(); } else { mLED_2_Off(); } }//end if } else { if (USBDeviceState == DETACHED_STATE) { mLED_Both_Off(); } else if (USBDeviceState == ATTACHED_STATE) { mLED_Both_On(); } else if (USBDeviceState == POWERED_STATE) { mLED_Only_1_On(); } else if (USBDeviceState == DEFAULT_STATE) { mLED_Only_2_On(); } else if (USBDeviceState == ADDRESS_STATE) { if (led_count == 0) { mLED_1_Toggle(); mLED_2_Off(); }//end if } else if (USBDeviceState == CONFIGURED_STATE) { if (led_count == 0) { mLED_1_Toggle(); if (mGetLED_1()) { mLED_2_Off(); } else { mLED_2_On(); } }//end if }//end if(...) }//end if(UCONbits.SUSPND...) }//end BlinkUSBStatus
//left (clear) shift FIFO char emptyShiftInputCommandBuffer(void){ char iterator = 0; char *dataMoveInputPointer = headInputPointer; char shiftAmount = (char)(headInputPointer - commandInBuffer); char bytesToShift = (char)(tailInputPointer - headInputPointer); if (headInputPointer == tailInputPointer){ flushInputCommandBuffer(); return fifoBufferStatusEmpty; } if (headInputPointer == commandInBuffer){ return fifoBufferStatusGood; } if (shiftAmount == 0) return fifoBufferStatusGood; //iffy statement headInputPointer = headInputPointer - bytesToShift; tailInputPointer = tailInputPointer - bytesToShift; for (iterator = 0; iterator < bytesToShift; iterator++){ *(dataMoveInputPointer - shiftAmount) = *(dataMoveInputPointer); dataMoveInputPointer++; if(iterator > 5){ while (1){ mLED_1_On(); mLED_2_Off(); Delay10KTCYx(1); mLED_2_On(); mLED_1_Off(); Delay10KTCYx(1); mLED_2_On(); mLED_2_On(); Delay10KTCYx(1); } } } return fifoBufferStatusGood; }
/************************************************** * SWARMS Input PRECESSING * * * ****************************************************/ void ProcessSWARMSInput(void){ char commandSuccess; char SWARMSCommandID; if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return; // this is where I should insert the code for specific control if(!USBHandleBusy(USBGenericOutHandle)) //Check if the endpoint has received any data from the host. { if(! canfitCommandInOutputCommandBuffer(5)){ //can't ensure reply -- just drop frame USBGenericOutHandle = USBGenRead(USBGEN_EP_NUM,(BYTE*)&OUTPacket,USBGEN_EP_SIZE); mLED_2_On(); mLED_1_Off() return; } SWARMSCommandID = OUTPacket[0]; if(IS_VALID_COMMAND){ commandSuccess = writeInputCommand(OUTPacket); switch(commandSuccess){ case fifoBufferStatusGood: //report success break; case fifoBufferStatusFull: //abort buffer full // mLED_2_Toggle(); SWARMSMakeCommandSuccess(SWARMSCommandID, FAILURE, NULL); //TODO: ADD FLAGS break; case fifoBufferStatusBadInputOversize: //abort buffer oversize input SWARMSMakeCommandSuccess(SWARMSCommandID, FAILURE, NULL); //TODO: ADD FLAGS break; case fifoBufferStatusBadInput: default: //notify bad command SWARMSMakeCommandSuccess(SWARMSCommandID, FAILURE, NULL); //TODO: ADD FLAGS break; } }else{ SWARMSMakeCommandSuccess(SWARMSCommandID, FAILURE, NULL); //TODO: ADD FLAGS } USBGenericOutHandle = USBGenRead(USBGEN_EP_NUM,(BYTE*)&OUTPacket,USBGEN_EP_SIZE); }
// Secondary callback function that gets called when the above // control transfer completes for the USBHIDCBSetReportHandler() void USBHIDCBSetReportComplete(void) { // 1 byte of LED state data should now be in the CtrlTrfData buffer. // Num Lock LED state is in Bit0. if (CtrlTrfData[0] & 0x01) // Make LED1 and LED2 match Num Lock state. { mLED_1_On(); mLED_2_On(); } else { mLED_1_Off(); mLED_2_Off(); } // Stop toggling the LEDs, so you can temporily see the Num lock LED state // instead. // Once the g_usb_led_timer reaches 0, the LEDs will go back to showing USB // state instead. g_blink_status_valid = FALSE; g_usb_led_timer = 140000; }
int nrf_receive_poll (uint8_t * frame) { uint8_t len; uint8_t status = 0; if (mode != MODE_RECEIVE_POLL) return (-1); status = nrf_cmd_status (C_NOP); if ((status & R_STATUS_RX_P_NO) == R_STATUS_RX_FIFO_EMPTY) { if ((status & R_STATUS_RX_DR) == R_STATUS_RX_DR) { nrf_write_reg (R_STATUS, R_STATUS_RX_DR); }; return 0; } nrf_read_long (C_R_RX_PL_WID, 1, &len); nrf_write_reg (R_STATUS, R_STATUS_RX_DR); if (len > 32) return -2; if (len < 1) return -1; /* UART2PutHex (status); UART2PutStr (" "); UART2PutHex (len); UART2PutStr ("\r\n"); */ mLED_2_On (); nrf_read_pkt (32, frame); mLED_2_Off (); return len; }
/****************************************************************************** * Function: void BlinkUSBStatus(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: BlinkUSBStatus turns on and off LEDs corresponding to * the USB device state. * * Note: mLED macros can be found in io_cfg.h * usb_device_state is declared in usbmmap.c and is modified * in usbdrv.c, usbctrltrf.c, and usb9.c *****************************************************************************/ void BlinkUSBStatus(void) { if(UCONbits.SUSPND != 1 && usb_device_state == CONFIGURED_STATE) mLED_2_On(); }//end BlinkUSBStatus
void led_on(void) {mLED_1_On();mLED_2_On();}
/******************************************************************** * Function: void BlinkUSBStatus(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: BlinkUSBStatus turns on and off LEDs * corresponding to the USB device state. * * Note: mLED macros can be found in HardwareProfile.h * USBDeviceState is declared and updated in * usb_device.c. *******************************************************************/ void BlinkUSBStatus(void) { static WORD led_count=0; char a=0; if(led_count == 0)led_count = 10000U; led_count--; #define mLED_Both_Off() {mLED_1_Off();mLED_2_Off();} #define mLED_Both_On() {mLED_1_On();mLED_2_On();} #define mLED_Only_1_On() {mLED_1_On();mLED_2_Off();} #define mLED_Only_2_On() {mLED_1_Off();mLED_2_On();} a = is_clear(); if(a=1) { mLED_3_On(); } else { mLED_3_Off(); } if(USBSuspendControl == 1) { if(led_count==0) { mLED_1_Toggle(); if(mGetLED_1()) { mLED_2_On(); } else { mLED_2_Off(); } }//end if } else { if(USBDeviceState == DETACHED_STATE) { mLED_Both_Off(); } else if(USBDeviceState == ATTACHED_STATE) { mLED_Both_On(); } else if(USBDeviceState == POWERED_STATE) { mLED_Only_1_On(); } else if(USBDeviceState == DEFAULT_STATE) { mLED_Only_2_On(); } else if(USBDeviceState == ADDRESS_STATE) { if(led_count == 0) { mLED_1_Toggle(); mLED_2_Off(); }//end if } else if(USBDeviceState == CONFIGURED_STATE) { if(led_count==0) { mLED_1_Toggle(); if(mGetLED_1()) { mLED_2_Off(); } else { mLED_2_On(); } }//end if }//end if(...) }//end if(UCONbits.SUSPND...) }//end BlinkUSBStatus
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(); } }
/****************************************************************************** * 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
/****************************************************************************** * 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