USB_Utilities.c
上传用户:gxz1972
上传日期:2019-09-13
资源大小:323k
文件大小:10k
源码类别:

传真(Fax)编程

开发平台:

C/C++

  1. //-----------------------------------------------------------------------------
  2. // USB_Utilities.c
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2007 Vson Technology, Inc.
  5. // http://www.usbmcu.com
  6. //
  7. // Program Description:
  8. //
  9. //
  10. //
  11. //
  12. //
  13. // MCU:            C8051F347
  14. // Tool chain:     Keil C51 7.50 / Keil EVAL C51
  15. //                 Silicon Laboratories IDE version 2.6
  16. // Command Line:   
  17. // Project Name:   TR1000
  18. //
  19. //
  20. // Release 1.0
  21. //    -All changes by Brin Cai
  22. //    -24 JUL 2007
  23. //
  24. //
  25. #include "Include_H.h"
  26. //-----------------------------------------------------------------------------
  27. // HaltEndpoint
  28. //-----------------------------------------------------------------------------
  29. //
  30. // Return Value : None
  31. // Parameters   : 
  32. // 1) UINT uEp
  33. //
  34. //-----------------------------------------------------------------------------
  35. BYTE HaltEndpoint (UINT uEp)
  36. {
  37.    BYTE bReturnState, bIndex;
  38.    // Save current INDEX value and target selected endpoint
  39.    UREAD_BYTE (INDEX, bIndex);
  40.    UWRITE_BYTE (INDEX, (BYTE)uEp & 0x00EF);
  41.    // Halt selected endpoint and update its status flag
  42.    switch (uEp)
  43.    {
  44.       case EP1_IN:
  45.          UWRITE_BYTE (EINCSRL, rbInSDSTL);
  46.          gEp1InStatus.bEpState = EP_HALTED;
  47.          bReturnState = EP_IDLE;          // Return success flag
  48.          break;
  49.       case EP2_OUT:
  50.          UWRITE_BYTE (EOUTCSRL, rbOutSDSTL);
  51.          gEp2OutStatus.bEpState = EP_HALTED;
  52.          bReturnState = EP_IDLE;          // Return success flag
  53.          break;
  54.       default:
  55.          bReturnState = EP_ERROR;         // Return error flag
  56.                                           // if endpoint not found
  57.          break;
  58.    }
  59.    UWRITE_BYTE (INDEX, bIndex);           // Restore saved INDEX
  60.    return bReturnState;
  61. }
  62. //-----------------------------------------------------------------------------
  63. // EnableEndpoint
  64. //-----------------------------------------------------------------------------
  65. //
  66. // Return Value : None
  67. // Parameters   : 
  68. // 1) UINT uEp
  69. //
  70. //-----------------------------------------------------------------------------
  71. BYTE EnableEndpoint (UINT uEp)
  72. {
  73.    BYTE bReturnState, bIndex;
  74.    // Save current INDEX value and target selected endpoint
  75.    UREAD_BYTE (INDEX, bIndex);
  76.    UWRITE_BYTE (INDEX, (BYTE)uEp & 0x00EF);
  77.    // Flag selected endpoint has HALTED
  78.    switch (uEp)
  79.    {
  80.       case EP1_IN:
  81.          // Disable STALL condition and clear the data toggle
  82.          UWRITE_BYTE (EINCSRL, rbInCLRDT);
  83.          gEp1InStatus.bEpState = EP_IDLE; // Return success
  84.          bReturnState = EP_IDLE;
  85.          break;
  86.       case EP2_OUT:
  87.          // Disable STALL condition and clear the data toggle
  88.          UWRITE_BYTE (EOUTCSRL, rbOutCLRDT);
  89.          gEp2OutStatus.bEpState = EP_IDLE;// Return success
  90.          bReturnState = EP_IDLE;
  91.          break;
  92.       default:
  93.          bReturnState = EP_ERROR;         // Return error
  94.                                           // if no endpoint found
  95.          break;
  96.    }
  97.    UWRITE_BYTE (INDEX, bIndex);           // Restore INDEX
  98.    return bReturnState;
  99. }
  100. //-----------------------------------------------------------------------------
  101. // GetEpStatus
  102. //-----------------------------------------------------------------------------
  103. //
  104. // Return Value : None
  105. // Parameters   : 
  106. // 1) UINT uEp
  107. //
  108. //-----------------------------------------------------------------------------
  109. BYTE GetEpStatus (UINT uEp)
  110. {
  111.    BYTE bReturnState;
  112.    // Get selected endpoint status
  113.    switch (uEp)
  114.    {
  115.       case EP1_IN:
  116.          bReturnState = gEp1InStatus.bEpState;
  117.          break;
  118.       case EP2_OUT:
  119.          bReturnState = gEp2OutStatus.bEpState;
  120.          break;
  121.       default:
  122.          bReturnState = EP_ERROR;
  123.          break;
  124.    }
  125.    return bReturnState;
  126. }
  127. //-----------------------------------------------------------------------------
  128. // SetConfiguration
  129. //-----------------------------------------------------------------------------
  130. //
  131. // Return Value : None
  132. // Parameters   : 
  133. // 1) BYTE SelectConfig
  134. //
  135. //-----------------------------------------------------------------------------
  136. BYTE SetConfiguration(BYTE SelectConfig)
  137. {
  138.    BYTE bReturnState = EP_IDLE;           // Endpoint state return value
  139.    PIF_STATUS pIfStatus;                  // Pointer to interface status
  140.                                           // structure
  141.    // Store address of selected config desc
  142.    gDeviceStatus.pConfig = &gDescriptorMap.bCfg1;
  143.    // Confirm that this configuration descriptor matches the requested
  144.    // configuration value
  145.    if (gDeviceStatus.pConfig[cfg_bConfigurationValue] != SelectConfig)
  146.    {
  147.       bReturnState = EP_ERROR;
  148.    }
  149.    else
  150.    {
  151.       // Store number of interfaces for this configuration
  152.       gDeviceStatus.bNumInterf = gDeviceStatus.pConfig[cfg_bNumInterfaces];
  153.       // Store total number of interface descriptors for this configuration
  154.       gDeviceStatus.bTotalInterfDsc = MAX_IF;
  155.       // Get pointer to the interface status structure
  156.       pIfStatus = (PIF_STATUS)&gDeviceStatus.IfStatus[0];
  157.       // Build Interface status structure for Interface0
  158.       pIfStatus->bIfNumber = 0;           // Set interface number
  159.       pIfStatus->bCurrentAlt = 0;         // Select alternate number zero
  160.       pIfStatus->bNumAlts = 0;            // No other alternates
  161.       SetInterface(pIfStatus);            // Configure Interface0, Alternate0
  162.       gDeviceStatus.bDevState = DEV_CONFIG;// Set device state to configured
  163.       gDeviceStatus.bCurrentConfig = SelectConfig;// Store current config
  164.    }
  165.    return bReturnState;
  166. }
  167. //-----------------------------------------------------------------------------
  168. // SetInterface
  169. //-----------------------------------------------------------------------------
  170. //
  171. // Return Value : None
  172. // Parameters   : 
  173. // 1) PIF_STATUS pIfStatus
  174. //
  175. //-----------------------------------------------------------------------------
  176. BYTE SetInterface(PIF_STATUS pIfStatus)
  177. {
  178.    BYTE bReturnState = EP_IDLE;
  179.    BYTE bIndex;
  180.    // Save current INDEX value
  181.    UREAD_BYTE (INDEX, bIndex);
  182.    // Add actions for each possible interface alternate selections
  183.    switch(pIfStatus->bIfNumber)
  184.    {
  185.       // Configure endpoints for interface0
  186.       case 0:
  187.          // Configure Endpoint1 IN
  188.          UWRITE_BYTE(INDEX, 1);           // Index to Endpoint1 registers
  189.          // direction = IN ; Double-buffering enabled
  190.          UWRITE_BYTE(EINCSRH, (rbInDIRSEL | rbInDBIEN));      
  191.          gEp1InStatus.uNumBytes = 0;      // Reset byte counter
  192.          gEp1InStatus.uMaxP = EP1_IN_MAXP;// Set maximum packet size
  193.          gEp1InStatus.bEp = EP1_IN;       // Set endpoint address
  194.          gEp1InStatus.bEpState = EP_IDLE; // Set endpoint state
  195.          // Endpoint2 OUT
  196.          UWRITE_BYTE(INDEX, 2);           // Index to Endpoint2 registers
  197.          // Double-buffering enabled ; direction = OUT
  198.          UWRITE_BYTE(EOUTCSRH, rbOutDBOEN);      
  199.          gEp2OutStatus.uNumBytes = 0;     // Reset byte counter
  200.          gEp2OutStatus.uMaxP = EP2_OUT_MAXP;// Set maximum packet size
  201.          gEp2OutStatus.bEp = EP2_OUT;     // Set endpoint number
  202.          gEp2OutStatus.bEpState = EP_IDLE;// Set endpoint state
  203.          UWRITE_BYTE(INDEX, 0);           // Return to index 0
  204.          break;
  205.       // Configure endpoints for interface1
  206.       case 1:
  207.       // Configure endpoints for interface2
  208.       case 2:
  209.       // Default (error)
  210.       default:
  211.          bReturnState = EP_ERROR;
  212.    }
  213.    UWRITE_BYTE (INDEX, bIndex);           // Restore INDEX
  214.    return bReturnState;
  215. }
  216. //-----------------------------------------------------------------------------
  217. // SetInterface
  218. //-----------------------------------------------------------------------------
  219. //
  220. // Return Value : None
  221. // Parameters   : 
  222. // 1) BYTE bEp
  223. // 2) UINT uNumBytes
  224. // 3) BYTE * pData
  225. //
  226. // Read from the selected endpoint FIFO
  227. //
  228. //-----------------------------------------------------------------------------
  229. void FIFORead (BYTE bEp, UINT uNumBytes, BYTE * pData)
  230. {
  231.    BYTE TargetReg;
  232.    UINT i;
  233.    // If >0 bytes requested,
  234.    if (uNumBytes)
  235.    {
  236.       TargetReg = FIFO_EP0 + bEp;         // Find address for target
  237.                                           // endpoint FIFO
  238.       USB0ADR = (TargetReg & 0x3F);       // Set address (mask out bits7-6)
  239.       USB0ADR |= 0xC0;                    // Set auto-read and initiate
  240.                                           // first read
  241.       // Unload <NumBytes> from the selected FIFO
  242.   //Read NumBytes-1
  243.       for(i=0;i<uNumBytes-1;i++)
  244.       {
  245.          while(USB0ADR & 0x80);           // Wait for BUSY->'0' (data ready)
  246.          pData[i] = USB0DAT;              // Copy data byte
  247.       }
  248.       //Read the last byte
  249.       while(USB0ADR & 0x80);              // Wait for BUSY->'0' (data ready)
  250.       pData[i] = USB0DAT;                 // Copy data byte
  251.       USB0ADR = 0;                        // Clear auto-read
  252.    }
  253. }
  254. //-----------------------------------------------------------------------------
  255. // FIFOWrite
  256. //-----------------------------------------------------------------------------
  257. //
  258. // Return Value : None
  259. // Parameters   : 
  260. // 1) BYTE bEp
  261. // 2) UINT uNumBytes
  262. // 3) BYTE * pData
  263. //
  264. // Write to the selected endpoint FIFO
  265. //
  266. //-----------------------------------------------------------------------------
  267. void FIFOWrite (BYTE bEp, UINT uNumBytes, BYTE * pData)
  268. {
  269.    BYTE TargetReg;
  270.    UINT i;
  271.    // If >0 bytes requested,
  272.    if (uNumBytes)
  273.    {
  274.       TargetReg = FIFO_EP0 + bEp;         // Find address for target //FIO_EP0==0X20
  275.                                           // endpoint FIFO
  276.       while(USB0ADR & 0x80);              // Wait for BUSY->'0'
  277.                                           // (register available)
  278.       USB0ADR = (TargetReg & 0x3F);       // Set address (mask out bits7-6)
  279.       // Write <NumBytes> to the selected FIFO
  280.       for(i=0;i<uNumBytes;i++)
  281.       {
  282.          USB0DAT = pData[i];
  283.          while(USB0ADR & 0x80);           // Wait for BUSY->'0' (data ready)
  284.       }
  285.    }
  286. }
  287. //-----------------------------------------------------------------------------
  288. // End Of File
  289. //-----------------------------------------------------------------------------