Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- VENDOR THROUGHPUT CODE
- THIS CODE IS CALLED FROM MAIN LOOP.
- FIRST THE PRESENCE OF USB DEVICE IS CHECKED IN MAIN AND ONCE DEVICE IS FOUND, ENUMERATION STARTS AND THIS CODE IS CALLED IN LOOP.
- -----------------------------------------------------------------------------------------------------------------------------------
- /*******************************************************************************
- Copyright 2016 Microchip Technology Inc. (www.microchip.com)
- *******************************************************************************/
- #include "usb.h"
- #include "usb_device_generic.h"
- #include <stdbool.h>
- /** VARIABLES ******************************************************/
- //NOTE: The below endpoint buffers need to be located in a section of
- //system SRAM that is accessible by the USB module. The USB module on all
- //currently existing Microchip USB microcontrollers use a dedicated DMA
- //interface for reading/writing USB data into/out of main system SRAM.
- //On some USB PIC microcontrollers, all of the microcontroller SRAM is dual
- //access, and therefore all of it can be accessed by either the USB
- //module or the microcontroller core. On other devices, only a certain
- //portion of the SRAM is accessible by the USB module. Therefore, on some
- //devices, it is important to place USB data buffers in certain sections of
- //SRAM, while on other devices, the buffers can be placed anywhere.
- #if defined(COMPILER_MPLAB_C18) //PIC18 devices
- #pragma udata USB_VARIABLES1 = USB_VARS_1
- USB_VOLATILE uint8_t EP1OUTEvenBuffer[64];
- USB_VOLATILE uint8_t EP1OUTOddBuffer[64];
- #pragma udata USB_VARIABLES2 = USB_VARS_2
- USB_VOLATILE uint8_t EP2OUTEvenBuffer[64];
- USB_VOLATILE uint8_t EP2OUTOddBuffer[64];
- USB_VOLATILE uint8_t EP3OUTEvenBuffer[64];
- USB_VOLATILE uint8_t EP3OUTOddBuffer[64];
- #elif defined(__XC8) && defined(FIXED_ADDRESS_MEMORY)
- USB_VOLATILE uint8_t EP1OUTEvenBuffer[64] @ EP1_EVEN_DATA_BUFFER_ADDRESS;
- USB_VOLATILE uint8_t EP1OUTOddBuffer[64] @ EP1_ODD_DATA_BUFFER_ADDRESS;
- USB_VOLATILE uint8_t EP2OUTEvenBuffer[64] @ EP2_EVEN_DATA_BUFFER_ADDRESS;
- USB_VOLATILE uint8_t EP2OUTOddBuffer[64] @ EP2_ODD_DATA_BUFFER_ADDRESS;
- USB_VOLATILE uint8_t EP3OUTEvenBuffer[64] @ EP3_EVEN_DATA_BUFFER_ADDRESS;
- USB_VOLATILE uint8_t EP3OUTOddBuffer[64] @ EP3_ODD_DATA_BUFFER_ADDRESS;
- #else
- //All PIC24, dsPIC, and PIC32 parts
- USB_VOLATILE uint8_t EP1OUTEvenBuffer[64];
- USB_VOLATILE uint8_t EP1OUTOddBuffer[64];
- USB_VOLATILE uint8_t EP2OUTEvenBuffer[64];
- USB_VOLATILE uint8_t EP2OUTOddBuffer[64];
- USB_VOLATILE uint8_t EP3OUTEvenBuffer[64];
- USB_VOLATILE uint8_t EP3OUTOddBuffer[64];
- USB_VOLATILE uint8_t EP1INEvenBuffer[64];
- USB_VOLATILE uint8_t EP1INOddBuffer[64];
- #endif
- //The below variables are only accessed by the CPU and can be placed anywhere in RAM.
- #if defined(COMPILER_MPLAB_C18)
- #pragma udata
- #endif
- USB_HANDLE EP1OUTEvenHandle;
- USB_HANDLE EP2OUTEvenHandle;
- USB_HANDLE EP3OUTEvenHandle;
- USB_HANDLE EP1OUTOddHandle;
- USB_HANDLE EP2OUTOddHandle;
- USB_HANDLE EP3OUTOddHandle;
- int bufval_in = 0;
- int pot_val = 0;
- USB_HANDLE EP1INEvenHandle;// HANDLE FOR IN
- USB_HANDLE EP1INOddHandle;// HANDLE FOR IN
- bool EP1OUTEvenNeedsServicingNext; //true means even need servicing next, false means odd needs servicing next
- bool EP2OUTEvenNeedsServicingNext; //true means even need servicing next, false means odd needs servicing next
- bool EP3OUTEvenNeedsServicingNext; //true means even need servicing next, false means odd needs servicing next
- bool EP1INEvenNeedsServicingNext;
- /*********************************************************************
- * Function: void APP_DeviceVendorThroughputTestInitialize(void);
- *
- * Overview: Initializes the demo
- *
- * PreCondition: Configuration for this app is already set by the USB host.
- *
- * Input: None
- *
- * Output: None
- *
- ********************************************************************/
- void APP_DeviceVendorThroughputTestInitialize()
- {
- EP1OUTEvenHandle = NULL;
- EP2OUTEvenHandle = NULL;
- EP3OUTEvenHandle = NULL;
- EP1OUTOddHandle = NULL;
- EP2OUTOddHandle = NULL;
- EP3OUTOddHandle = NULL;
- EP1INEvenHandle = NULL;
- EP1INOddHandle = NULL;
- //Now that we are configured, enable the endpoints for use in the demo
- // and start the initial transfers
- USBEnableEndpoint(1,USB_OUT_ENABLED|USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
- USBEnableEndpoint(2,USB_OUT_ENABLED|USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
- USBEnableEndpoint(3,USB_OUT_ENABLED|USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
- USBEnableEndpoint(_EP01_IN,USB_OUT_ENABLED|USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
- //Prepare the OUT endpoints to receive the first packets from the host.
- EP1OUTEvenHandle = USBTransferOnePacket(1, OUT_FROM_HOST,(uint8_t*)&EP1OUTEvenBuffer,64); //First 64-bytes of data sent to EP1 OUT will arrive in the even buffer.
- EP1OUTOddHandle = USBTransferOnePacket(1, OUT_FROM_HOST,(uint8_t*)&EP1OUTOddBuffer,64); //Second 64-bytes of data sent to EP1 OUT will arrive in the odd buffer.
- EP1OUTEvenNeedsServicingNext = true; //Used to keep track of which buffer will contain the next sequential data packet.
- EP2OUTEvenHandle = USBTransferOnePacket(2, OUT_FROM_HOST,(uint8_t*)&EP2OUTEvenBuffer,64);
- EP2OUTOddHandle = USBTransferOnePacket(2, OUT_FROM_HOST,(uint8_t*)&EP2OUTOddBuffer,64);
- EP2OUTEvenNeedsServicingNext = true; //Used to keep track of which buffer will contain the next sequential data packet.
- EP3OUTEvenHandle = USBTransferOnePacket(3, OUT_FROM_HOST,(uint8_t*)&EP3OUTEvenBuffer,64);
- EP3OUTOddHandle = USBTransferOnePacket(3, OUT_FROM_HOST,(uint8_t*)&EP3OUTOddBuffer,64);
- EP3OUTEvenNeedsServicingNext = true; //Used to keep track of which buffer will contain the next sequential data packet.
- EP1INEvenHandle = USBTransferOnePacket(_EP01_IN, IN_TO_HOST,(uint8_t*)&EP1INEvenBuffer,64); //First 64-bytes of data RECIVE to EP3 IN will arrive in the even buffer.
- EP1INOddHandle = USBTransferOnePacket(_EP01_IN, IN_TO_HOST,(uint8_t*)&EP1INOddBuffer,64); //Second 64-bytes of data RECIVE to EP3 IN will arrive in the odd buffer.
- EP1INEvenNeedsServicingNext = true;
- }
- /*********************************************************************
- * Function: void APP_DeviceVendorThroughputTestTasks(void);
- *
- * Overview: keeps the demo running.
- *
- * PreCondition: Demo is initialiized.
- *
- * Input: None
- *
- * Output: None
- *
- ********************************************************************/
- void APP_DeviceVendorThroughputTestTasks()
- {
- /* If the USB device isn't configured yet, we can't really do anything
- * else since we don't have a host to talk to. So jump back to the
- * top of the while loop. */
- if( USBGetDeviceState() < CONFIGURED_STATE )
- {
- return;
- }
- /* If we are currently suspended, then we need to see if we need to
- * issue a remote wakeup. In either case, we shouldn't process any
- * keyboard commands since we aren't currently communicating to the host
- * thus just continue back to the start of the while loop. */
- if( USBIsDeviceSuspended()== true )
- {
- return;
- }
- if(EP1OUTEvenNeedsServicingNext == true) //Check which buffer (even/odd) the next set of data is going to arrive in
- {
- if(!USBHandleBusy(EP1OUTEvenHandle)) //Check if the endpoint has received any data from the host.
- {
- int a = 0;
- for(a=0; a<4;a++)
- {
- switch(EP1OUTEvenBuffer[a]) //Data arrived, check what kind of command might be in the packet of data.
- {
- case 0x81:
- if(EP1INEvenNeedsServicingNext == true)
- {
- if(!USBHandleBusy(EP1INEvenHandle)) //Check if the endpoint has SEND any data from the host.
- { //pot_val = 512;
- // int z= 512;
- senddata_to_Host(1022);
- _LATE0 ^= 1;
- int k;
- for(k=6;k<65;k++)
- {
- EP1INEvenBuffer[k] = 0xFF;
- }
- EP1INEvenHandle = USBTransferOnePacket(_EP01_IN, IN_TO_HOST,(uint8_t*)&EP1INEvenBuffer,64);
- EP1INEvenNeedsServicingNext = false;
- }
- break;
- case 0x82: //Toggle LED(s) command from PC application.
- _LATE2 = 0;
- break;
- case 0x80: //Toggle LED(s) command from PC application.
- _LATE2 = 1;
- break;
- }
- }
- }
- EP1OUTEvenHandle = USBTransferOnePacket(1, OUT_FROM_HOST,(uint8_t*)&EP1OUTEvenBuffer,64);
- EP1OUTEvenNeedsServicingNext = false;
- }
- }
- else //else EP1OUTOdd needs servicing next
- {
- if(!USBHandleBusy(EP1OUTOddHandle)) //Check if the endpoint has received any data from the host.
- {
- //Insert code here that would do something useful with the data, according to the needs of
- //the application.
- //Re-arm the EP1OUTOdd BDT entry so the EP1OUTOddBuffer[] can receive
- //the second to next data packet sent by the host.
- int b = 0;
- {
- for(b=0; b<4;b++)
- {
- switch(EP1OUTOddBuffer[b]) //Data arrived, check what kind of command might be in the packet of data.
- {
- case 0x81:
- _LATE0 ^= 1;
- break;
- case 0x82: //Toggle LED(s) command from PC application.
- _LATE2 = 0;
- break;
- case 0x80: //Toggle LED(s) command from PC application.
- _LATE2 = 1;
- break;
- }
- }
- }
- EP1OUTOddHandle = USBTransferOnePacket(1, OUT_FROM_HOST,(uint8_t*)&EP1OUTOddBuffer,64);
- EP1OUTEvenNeedsServicingNext = true;
- }
- }
- if(EP2OUTEvenNeedsServicingNext == true)
- {
- if(!USBHandleBusy(EP2OUTEvenHandle)) //Check if the endpoint has received any data from the host.
- {
- //Re-arm the OUT endpoint for the next packet:
- EP2OUTEvenHandle = USBTransferOnePacket(2, OUT_FROM_HOST,(uint8_t*)&EP2OUTEvenBuffer,64);
- EP2OUTEvenNeedsServicingNext = false;
- }
- }
- else //else EP2OUTOdd needs servicing next
- {
- if(!USBHandleBusy(EP2OUTOddHandle)) //Check if the endpoint has received any data from the host.
- {
- //Re-arm the OUT endpoint for the next packet:
- EP2OUTOddHandle = USBTransferOnePacket(2, OUT_FROM_HOST,(uint8_t*)&EP2OUTOddBuffer,64);
- EP2OUTEvenNeedsServicingNext = true;
- }
- }
- if(EP3OUTEvenNeedsServicingNext == true)
- {
- if(!USBHandleBusy(EP3OUTEvenHandle)) //Check if the endpoint has received any data from the host.
- {
- //Re-arm the OUT endpoint for the next packet:
- EP3OUTEvenHandle = USBTransferOnePacket(3, OUT_FROM_HOST,(uint8_t*)&EP3OUTEvenBuffer,64);
- EP3OUTEvenNeedsServicingNext = false;
- }
- }
- else //else EP3OUTOdd needs servicing next
- {
- if(!USBHandleBusy(EP3OUTOddHandle)) //Check if the endpoint has received any data from the host.
- {
- //Re-arm the OUT endpoint for the next packet:
- EP3OUTOddHandle = USBTransferOnePacket(3, OUT_FROM_HOST,(uint8_t*)&EP3OUTOddBuffer,64);
- EP3OUTEvenNeedsServicingNext = true;
- }
- }
- }
- senddata_to_Host(int bufval)
- {
- pot_val = bufval;
- // pot = ADC_Read10bit(ADC_CHANNEL_POTENTIOMETER);
- EP1INEvenBuffer[0] = 0x00;
- EP1INEvenBuffer[1] = 0x81;
- EP1INEvenBuffer[2] = 0x82;
- EP1INEvenBuffer[3] = (uint8_t)pot_val; //LSB
- EP1INEvenBuffer[4] = pot_val >> 8; //MSB
- if(PORTDbits.RD1 == 1) //pushbutton not pressed, pull up resistor on circuit board is pulling the PORT pin high
- {
- EP1INEvenBuffer[5] = 0x01;
- // LATDbits.LATD4=1;
- // LATDbits.LATD5=0;
- }
- else //sw3 must be == 0, pushbutton is pressed and overpowering the pull up resistor
- {
- EP1INEvenBuffer[5] = 0x00;
- // LATDbits.LATD4=0;
- // LATDbits.LATD5=1;
- }
- }
Add Comment
Please, Sign In to add comment