rahultheengineer

vendor Thoughput Main Code

Apr 12th, 2017
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.59 KB | None | 0 0
  1. VENDOR THROUGHPUT CODE
  2. THIS CODE IS CALLED FROM MAIN LOOP.
  3. FIRST THE PRESENCE OF USB DEVICE IS CHECKED IN MAIN AND ONCE DEVICE IS FOUND, ENUMERATION STARTS AND THIS CODE IS CALLED IN LOOP.
  4. -----------------------------------------------------------------------------------------------------------------------------------
  5.  
  6. /*******************************************************************************
  7. Copyright 2016 Microchip Technology Inc. (www.microchip.com)
  8. *******************************************************************************/
  9. #include "usb.h"
  10. #include "usb_device_generic.h"
  11. #include <stdbool.h>
  12.  
  13. /** VARIABLES ******************************************************/
  14. //NOTE:  The below endpoint buffers need to be located in a section of
  15. //system SRAM that is accessible by the USB module.  The USB module on all
  16. //currently existing Microchip USB microcontrollers use a dedicated DMA
  17. //interface for reading/writing USB data into/out of main system SRAM.
  18.  
  19. //On some USB PIC microcontrollers, all of the microcontroller SRAM is dual
  20. //access, and therefore all of it can be accessed by either the USB
  21. //module or the microcontroller core.  On other devices, only a certain
  22. //portion of the SRAM is accessible by the USB module. Therefore, on some
  23. //devices, it is important to place USB data buffers in certain sections of
  24. //SRAM, while on other devices, the buffers can be placed anywhere.
  25.  
  26. #if defined(COMPILER_MPLAB_C18) //PIC18 devices
  27.     #pragma udata USB_VARIABLES1 = USB_VARS_1
  28.     USB_VOLATILE uint8_t EP1OUTEvenBuffer[64];
  29.     USB_VOLATILE uint8_t EP1OUTOddBuffer[64];
  30.     #pragma udata USB_VARIABLES2 = USB_VARS_2
  31.     USB_VOLATILE uint8_t EP2OUTEvenBuffer[64];
  32.     USB_VOLATILE uint8_t EP2OUTOddBuffer[64];
  33.     USB_VOLATILE uint8_t EP3OUTEvenBuffer[64];
  34.     USB_VOLATILE uint8_t EP3OUTOddBuffer[64];
  35. #elif defined(__XC8) && defined(FIXED_ADDRESS_MEMORY)
  36.     USB_VOLATILE uint8_t EP1OUTEvenBuffer[64] @ EP1_EVEN_DATA_BUFFER_ADDRESS;
  37.     USB_VOLATILE uint8_t EP1OUTOddBuffer[64]  @ EP1_ODD_DATA_BUFFER_ADDRESS;
  38.     USB_VOLATILE uint8_t EP2OUTEvenBuffer[64] @ EP2_EVEN_DATA_BUFFER_ADDRESS;
  39.     USB_VOLATILE uint8_t EP2OUTOddBuffer[64]  @ EP2_ODD_DATA_BUFFER_ADDRESS;
  40.     USB_VOLATILE uint8_t EP3OUTEvenBuffer[64] @ EP3_EVEN_DATA_BUFFER_ADDRESS;
  41.     USB_VOLATILE uint8_t EP3OUTOddBuffer[64]  @ EP3_ODD_DATA_BUFFER_ADDRESS;
  42. #else
  43.     //All PIC24, dsPIC, and PIC32 parts
  44.     USB_VOLATILE uint8_t EP1OUTEvenBuffer[64];
  45.     USB_VOLATILE uint8_t EP1OUTOddBuffer[64];
  46.     USB_VOLATILE uint8_t EP2OUTEvenBuffer[64];
  47.     USB_VOLATILE uint8_t EP2OUTOddBuffer[64];
  48.     USB_VOLATILE uint8_t EP3OUTEvenBuffer[64];
  49.     USB_VOLATILE uint8_t EP3OUTOddBuffer[64];
  50.    
  51.     USB_VOLATILE uint8_t EP1INEvenBuffer[64];
  52.     USB_VOLATILE uint8_t EP1INOddBuffer[64];
  53.    
  54. #endif
  55.  
  56. //The below variables are only accessed by the CPU and can be placed anywhere in RAM.
  57. #if defined(COMPILER_MPLAB_C18)
  58.     #pragma udata
  59. #endif
  60.  
  61. USB_HANDLE EP1OUTEvenHandle;
  62. USB_HANDLE EP2OUTEvenHandle;
  63. USB_HANDLE EP3OUTEvenHandle;
  64. USB_HANDLE EP1OUTOddHandle;
  65. USB_HANDLE EP2OUTOddHandle;
  66. USB_HANDLE EP3OUTOddHandle;
  67.  
  68. int bufval_in = 0;
  69. int pot_val = 0;
  70.  
  71. USB_HANDLE EP1INEvenHandle;// HANDLE FOR IN
  72. USB_HANDLE EP1INOddHandle;// HANDLE FOR IN
  73.  
  74. bool EP1OUTEvenNeedsServicingNext;  //true means even need servicing next, false means odd needs servicing next
  75. bool EP2OUTEvenNeedsServicingNext;  //true means even need servicing next, false means odd needs servicing next
  76. bool EP3OUTEvenNeedsServicingNext;  //true means even need servicing next, false means odd needs servicing next
  77.  
  78. bool EP1INEvenNeedsServicingNext;
  79.  
  80.  
  81. /*********************************************************************
  82. * Function: void APP_DeviceVendorThroughputTestInitialize(void);
  83. *
  84. * Overview: Initializes the demo
  85. *
  86. * PreCondition: Configuration for this app is already set by the USB host.
  87. *
  88. * Input: None
  89. *
  90. * Output: None
  91. *
  92. ********************************************************************/
  93. void APP_DeviceVendorThroughputTestInitialize()
  94. {
  95.     EP1OUTEvenHandle = NULL;
  96.     EP2OUTEvenHandle = NULL;
  97.     EP3OUTEvenHandle = NULL;
  98.  
  99.     EP1OUTOddHandle = NULL;
  100.     EP2OUTOddHandle = NULL;
  101.     EP3OUTOddHandle = NULL;
  102.      
  103.     EP1INEvenHandle = NULL;
  104.     EP1INOddHandle = NULL;
  105.  
  106.    
  107.     //Now that we are configured, enable the endpoints for use in the demo
  108.     //  and start the initial transfers
  109.     USBEnableEndpoint(1,USB_OUT_ENABLED|USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
  110.     USBEnableEndpoint(2,USB_OUT_ENABLED|USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
  111.     USBEnableEndpoint(3,USB_OUT_ENABLED|USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
  112.  
  113.     USBEnableEndpoint(_EP01_IN,USB_OUT_ENABLED|USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
  114.     //Prepare the OUT endpoints to receive the first packets from the host.
  115.     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.
  116.     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.
  117.     EP1OUTEvenNeedsServicingNext = true;    //Used to keep track of which buffer will contain the next sequential data packet.
  118.  
  119.     EP2OUTEvenHandle = USBTransferOnePacket(2, OUT_FROM_HOST,(uint8_t*)&EP2OUTEvenBuffer,64);
  120.     EP2OUTOddHandle = USBTransferOnePacket(2, OUT_FROM_HOST,(uint8_t*)&EP2OUTOddBuffer,64);
  121.     EP2OUTEvenNeedsServicingNext = true;    //Used to keep track of which buffer will contain the next sequential data packet.
  122.  
  123.     EP3OUTEvenHandle = USBTransferOnePacket(3, OUT_FROM_HOST,(uint8_t*)&EP3OUTEvenBuffer,64);
  124.     EP3OUTOddHandle = USBTransferOnePacket(3, OUT_FROM_HOST,(uint8_t*)&EP3OUTOddBuffer,64);
  125.     EP3OUTEvenNeedsServicingNext = true;    //Used to keep track of which buffer will contain the next sequential data packet.
  126.    
  127.     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.
  128.     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.
  129.     EP1INEvenNeedsServicingNext = true;
  130. }
  131.  
  132.  
  133. /*********************************************************************
  134. * Function: void APP_DeviceVendorThroughputTestTasks(void);
  135. *
  136. * Overview: keeps the demo running.
  137. *
  138. * PreCondition: Demo is initialiized.
  139. *
  140. * Input: None
  141. *
  142. * Output: None
  143. *
  144. ********************************************************************/
  145. void APP_DeviceVendorThroughputTestTasks()
  146. {
  147.     /* If the USB device isn't configured yet, we can't really do anything
  148.      * else since we don't have a host to talk to.  So jump back to the
  149.      * top of the while loop. */
  150.     if( USBGetDeviceState() < CONFIGURED_STATE )
  151.     {
  152.         return;
  153.     }
  154.  
  155.     /* If we are currently suspended, then we need to see if we need to
  156.      * issue a remote wakeup.  In either case, we shouldn't process any
  157.      * keyboard commands since we aren't currently communicating to the host
  158.      * thus just continue back to the start of the while loop. */
  159.     if( USBIsDeviceSuspended()== true )
  160.     {
  161.         return;
  162.     }
  163.  
  164.    
  165.     if(EP1OUTEvenNeedsServicingNext == true)    //Check which buffer (even/odd) the next set of data is going to arrive in
  166.     {
  167.         if(!USBHandleBusy(EP1OUTEvenHandle))    //Check if the endpoint has received any data from the host.
  168.         {
  169.             int a = 0;
  170.                 for(a=0; a<4;a++)
  171.                 {
  172.                     switch(EP1OUTEvenBuffer[a])                 //Data arrived, check what kind of command might be in the packet of data.
  173.                         {                  
  174.                            case 0x81:
  175.                                        
  176.                             if(EP1INEvenNeedsServicingNext == true)
  177.                             {
  178.                               if(!USBHandleBusy(EP1INEvenHandle))       //Check if the endpoint has SEND any data from the host.
  179.                               { //pot_val = 512;
  180. //                                  int z= 512;
  181.                                   senddata_to_Host(1022);
  182.                                   _LATE0 ^= 1;
  183.                                   int k;
  184.                                   for(k=6;k<65;k++)
  185.                                     {
  186.                                   EP1INEvenBuffer[k] = 0xFF;
  187.                                     }
  188.                             EP1INEvenHandle = USBTransferOnePacket(_EP01_IN, IN_TO_HOST,(uint8_t*)&EP1INEvenBuffer,64);
  189.                             EP1INEvenNeedsServicingNext = false;
  190.                    
  191.                                 }
  192.                                              
  193.                                          break;
  194.                                                          
  195.                            case 0x82:  //Toggle LED(s) command from PC application.
  196.                                           _LATE2 = 0;
  197.                                          break;
  198.  
  199.                            case 0x80:  //Toggle LED(s) command from PC application.
  200.                                          _LATE2 = 1;
  201.                                           break;
  202.                         }
  203.                 }
  204.             }
  205.             EP1OUTEvenHandle = USBTransferOnePacket(1, OUT_FROM_HOST,(uint8_t*)&EP1OUTEvenBuffer,64);
  206.             EP1OUTEvenNeedsServicingNext = false;
  207.     }
  208. }
  209.     else //else EP1OUTOdd needs servicing next
  210.     {
  211.         if(!USBHandleBusy(EP1OUTOddHandle))     //Check if the endpoint has received any data from the host.
  212.         {
  213.                 //Insert code here that would do something useful with the data, according to the needs of
  214.                 //the application.
  215.  
  216.             //Re-arm the EP1OUTOdd BDT entry so the EP1OUTOddBuffer[] can receive
  217.             //the second to next data packet sent by the host.
  218.            
  219.             int b = 0;
  220.             {
  221.                 for(b=0; b<4;b++)
  222.                 {
  223.                     switch(EP1OUTOddBuffer[b])                  //Data arrived, check what kind of command might be in the packet of data.
  224.                         {
  225.                  
  226.                            case 0x81:
  227.                                         _LATE0 ^= 1;
  228.                                         break;
  229.                                                          
  230.                            case 0x82:  //Toggle LED(s) command from PC application.
  231.                                          _LATE2 = 0;
  232.                                          break;
  233.  
  234.                            case 0x80:  //Toggle LED(s) command from PC application.
  235.                                          _LATE2 = 1;
  236.                                          break;
  237.                         }
  238.                 }
  239.             }    
  240.             EP1OUTOddHandle = USBTransferOnePacket(1, OUT_FROM_HOST,(uint8_t*)&EP1OUTOddBuffer,64);
  241.             EP1OUTEvenNeedsServicingNext = true;
  242.         }
  243.     }
  244.  
  245.  
  246.     if(EP2OUTEvenNeedsServicingNext == true)
  247.     {
  248.         if(!USBHandleBusy(EP2OUTEvenHandle))        //Check if the endpoint has received any data from the host.
  249.         {
  250.             //Re-arm the OUT endpoint for the next packet:
  251.             EP2OUTEvenHandle = USBTransferOnePacket(2, OUT_FROM_HOST,(uint8_t*)&EP2OUTEvenBuffer,64);
  252.             EP2OUTEvenNeedsServicingNext = false;
  253.         }
  254.     }
  255.     else //else EP2OUTOdd needs servicing next
  256.     {
  257.         if(!USBHandleBusy(EP2OUTOddHandle))     //Check if the endpoint has received any data from the host.
  258.         {
  259.             //Re-arm the OUT endpoint for the next packet:
  260.             EP2OUTOddHandle = USBTransferOnePacket(2, OUT_FROM_HOST,(uint8_t*)&EP2OUTOddBuffer,64);
  261.             EP2OUTEvenNeedsServicingNext = true;
  262.         }
  263.     }
  264.  
  265.     if(EP3OUTEvenNeedsServicingNext == true)
  266.     {
  267.         if(!USBHandleBusy(EP3OUTEvenHandle))        //Check if the endpoint has received any data from the host.
  268.         {
  269.             //Re-arm the OUT endpoint for the next packet:
  270.             EP3OUTEvenHandle = USBTransferOnePacket(3, OUT_FROM_HOST,(uint8_t*)&EP3OUTEvenBuffer,64);
  271.             EP3OUTEvenNeedsServicingNext = false;
  272.         }
  273.     }
  274.     else //else EP3OUTOdd needs servicing next
  275.     {
  276.         if(!USBHandleBusy(EP3OUTOddHandle))     //Check if the endpoint has received any data from the host.
  277.         {
  278.             //Re-arm the OUT endpoint for the next packet:
  279.             EP3OUTOddHandle = USBTransferOnePacket(3, OUT_FROM_HOST,(uint8_t*)&EP3OUTOddBuffer,64);
  280.             EP3OUTEvenNeedsServicingNext = true;
  281.         }
  282.     }
  283. }
  284.  
  285.  
  286. senddata_to_Host(int bufval)
  287. {
  288.     pot_val = bufval;
  289. //      pot = ADC_Read10bit(ADC_CHANNEL_POTENTIOMETER);
  290.  
  291.                 EP1INEvenBuffer[0] = 0x00;
  292.                 EP1INEvenBuffer[1] = 0x81;
  293.                 EP1INEvenBuffer[2] = 0x82;
  294.  
  295.                 EP1INEvenBuffer[3] = (uint8_t)pot_val; //LSB
  296.                 EP1INEvenBuffer[4] = pot_val >> 8;     //MSB
  297.  
  298.                    if(PORTDbits.RD1 == 1)   //pushbutton not pressed, pull up resistor on circuit board is pulling the PORT pin high
  299.                     {
  300.                             EP1INEvenBuffer[5] = 0x01;
  301. //                            LATDbits.LATD4=1;
  302. //                            LATDbits.LATD5=0;
  303.                     }
  304.                     else                                    //sw3 must be == 0, pushbutton is pressed and overpowering the pull up resistor
  305.                     {
  306.                             EP1INEvenBuffer[5] = 0x00;
  307. //                            LATDbits.LATD4=0;
  308. //                            LATDbits.LATD5=1;
  309.                     }
  310.  
  311.                    
  312. }
Add Comment
Please, Sign In to add comment