Advertisement
Guest User

cyfxbulklpautomanytoone.c

a guest
May 10th, 2013
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.61 KB | None | 0 0
  1. /*
  2. ## Cypress USB 3.0 Platform source file (cyfxbulklpautomanytoone.c)
  3. ## ===========================
  4. ##
  5. ## Copyright Cypress Semiconductor Corporation, 2010-2011,
  6. ## All Rights Reserved
  7. ## UNPUBLISHED, LICENSED SOFTWARE.
  8. ##
  9. ## CONFIDENTIAL AND PROPRIETARY INFORMATION
  10. ## WHICH IS THE PROPERTY OF CYPRESS.
  11. ##
  12. ## Use of this file is governed
  13. ## by the license agreement included in the file
  14. ##
  15. ## <install>/license/license.txt
  16. ##
  17. ## where <install> is the Cypress software
  18. ## installation root directory path.
  19. ##
  20. ## ===========================
  21. */
  22.  
  23. /* This file illustrates the bulkloop application example using the DMA AUTO_MANY_TO_ONE mode */
  24.  
  25. /*
  26. This example illustrates the use of the FX3 firmware APIs to implement a data loopback
  27. application over three USB bulk endpoints. The device enumerates as a vendor specific USB
  28. device with three bulk endpoints (1-OUT, 2-OUT and 1-IN). The application loops back any
  29. data that it receives on the two bulk OUT endpoints to the bulk IN endpoint in an
  30. interleaved fashion.
  31.  
  32. The loopback is achieved with the help of a multichannel DMA AUTO_MANY_TO_ONE which is created
  33. connecting the three endpoints. There is no FX3 CPU involvement in the data transfer, and the
  34. loopback is performed automatically by the FX3 device hardware.
  35.  
  36. The DMA buffer size is defined based on the USB speed. 64 for full speed, 512 for high speed and 1024
  37. for super speed. CY_FX_BULKLP_DMA_BUF_COUNT in the header file defines the number of DMA buffers.
  38. */
  39.  
  40. #include "cyu3system.h"
  41. #include "cyu3os.h"
  42. #include "cyu3dma.h"
  43. #include "cyu3error.h"
  44. #include "cyfxbulklpautomanytoone.h"
  45. #include "cyu3usb.h"
  46. #include "cyu3uart.h"
  47. #include "cyfxusbdebug.h"
  48.  
  49. CyU3PThread BulkLpAppThread; /* Bulk loop application thread structure */
  50. CyU3PThread applnThread;
  51. CyU3PDmaMultiChannel glChHandleBulkLp; /* DMA Channel handle */
  52.  
  53. CyBool_t glIsApplnActive = CyFalse; /* Whether the application is active or not. */
  54.  
  55. /* Application Error Handler */
  56. void
  57. CyFxAppErrorHandler (
  58. CyU3PReturnStatus_t apiRetStatus /* API return status */
  59. )
  60. {
  61. /* Application failed with the error code apiRetStatus */
  62.  
  63. /* Add custom debug or recovery actions here */
  64.  
  65. /* Loop Indefinitely */
  66. for (;;)
  67. {
  68. /* Thread sleep : 100 ms */
  69. CyU3PThreadSleep (100);
  70. }
  71. }
  72.  
  73. /* This function initializes the debug module. The debug prints
  74. * are routed to the UART and can be seen using a UART console
  75. * running at 115200 baud rate. */
  76. void
  77. CyFxBulkLpApplnDebugInit (void)
  78. {
  79. CyU3PUartConfig_t uartConfig;
  80. CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
  81.  
  82. /* Initialize the UART for printing debug messages */
  83. apiRetStatus = CyU3PUartInit();
  84. if (apiRetStatus != CY_U3P_SUCCESS)
  85. {
  86. /* Error handling */
  87. CyFxAppErrorHandler(apiRetStatus);
  88. }
  89.  
  90. /* Set UART configuration */
  91. CyU3PMemSet ((uint8_t *)&uartConfig, 0, sizeof (uartConfig));
  92. uartConfig.baudRate = CY_U3P_UART_BAUDRATE_115200;
  93. uartConfig.stopBit = CY_U3P_UART_ONE_STOP_BIT;
  94. uartConfig.parity = CY_U3P_UART_NO_PARITY;
  95. uartConfig.txEnable = CyTrue;
  96. uartConfig.rxEnable = CyFalse;
  97. uartConfig.flowCtrl = CyFalse;
  98. uartConfig.isDma = CyTrue;
  99.  
  100. apiRetStatus = CyU3PUartSetConfig (&uartConfig, NULL);
  101. if (apiRetStatus != CY_U3P_SUCCESS)
  102. {
  103. CyFxAppErrorHandler(apiRetStatus);
  104. }
  105.  
  106. /* Set the UART transfer to a really large value. */
  107. apiRetStatus = CyU3PUartTxSetBlockXfer (0xFFFFFFFF);
  108. if (apiRetStatus != CY_U3P_SUCCESS)
  109. {
  110. CyFxAppErrorHandler(apiRetStatus);
  111. }
  112.  
  113. /* Initialize the debug module. */
  114. apiRetStatus = CyU3PDebugInit (CY_U3P_LPP_SOCKET_UART_CONS, 8);
  115. if (apiRetStatus != CY_U3P_SUCCESS)
  116. {
  117. CyFxAppErrorHandler(apiRetStatus);
  118. }
  119. }
  120.  
  121. /* This function starts the bulk loop application. This is called
  122. * when a SET_CONF event is received from the USB host. The endpoints
  123. * are configured and the DMA pipe is setup in this function. */
  124. void
  125. CyFxBulkLpApplnStart (
  126. void)
  127. {
  128. uint16_t size = 0;
  129. CyU3PEpConfig_t epCfg;
  130. CyU3PDmaMultiChannelConfig_t dmaCfg;
  131. CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
  132. CyU3PUSBSpeed_t usbSpeed = CyU3PUsbGetSpeed();
  133.  
  134. /* First identify the usb speed. Once that is identified,
  135. * create a DMA channel and start the transfer on this. */
  136.  
  137. /* Based on the Bus Speed configure the endpoint packet size */
  138. switch (usbSpeed)
  139. {
  140. case CY_U3P_FULL_SPEED:
  141. size = 64;
  142. break;
  143.  
  144. case CY_U3P_HIGH_SPEED:
  145. size = 512;
  146. break;
  147.  
  148. case CY_U3P_SUPER_SPEED:
  149. size = 1024;
  150. break;
  151.  
  152. default:
  153. CyU3PDebugPrint (4, "Error! Invalid USB speed.\n");
  154. CyFxAppErrorHandler (CY_U3P_ERROR_FAILURE);
  155. break;
  156. }
  157.  
  158. CyU3PMemSet ((uint8_t *)&epCfg, 0, sizeof (epCfg));
  159. epCfg.enable = CyTrue;
  160. epCfg.epType = CY_U3P_USB_EP_BULK;
  161. epCfg.burstLen = 1;
  162. epCfg.streams = 0;
  163. epCfg.pcktSize = size;
  164.  
  165. /* Producer 1 endpoint configuration */
  166. apiRetStatus = CyU3PSetEpConfig(CY_FX_EP_PRODUCER_1, &epCfg);
  167. if (apiRetStatus != CY_U3P_SUCCESS)
  168. {
  169. CyU3PDebugPrint (4, "CyU3PSetEpConfig failed, Error code = %d\n", apiRetStatus);
  170. CyFxAppErrorHandler (apiRetStatus);
  171. }
  172.  
  173. /* Producer 2 endpoint configuration */
  174. apiRetStatus = CyU3PSetEpConfig(CY_FX_EP_PRODUCER_2, &epCfg);
  175. if (apiRetStatus != CY_U3P_SUCCESS)
  176. {
  177. CyU3PDebugPrint (4, "CyU3PSetEpConfig failed, Error code = %d\n", apiRetStatus);
  178. CyFxAppErrorHandler (apiRetStatus);
  179. }
  180.  
  181. /* Producer 3 endpoint configuration */
  182. apiRetStatus = CyU3PSetEpConfig(CY_FX_EP_PRODUCER_3, &epCfg);
  183. if (apiRetStatus != CY_U3P_SUCCESS)
  184. {
  185. CyU3PDebugPrint (4, "CyU3PSetEpConfig failed, Error code = %d\n", apiRetStatus);
  186. CyFxAppErrorHandler (apiRetStatus);
  187. }
  188.  
  189. /* Consumer endpoint configuration */
  190. apiRetStatus = CyU3PSetEpConfig(CY_FX_EP_CONSUMER, &epCfg);
  191. if (apiRetStatus != CY_U3P_SUCCESS)
  192. {
  193. CyU3PDebugPrint (4, "CyU3PSetEpConfig failed, Error code = %d\n", apiRetStatus);
  194. CyFxAppErrorHandler (apiRetStatus);
  195. }
  196.  
  197. apiRetStatus = CyU3PSetEpConfig(CY_FX_EP_DEBUG, &epCfg);
  198. if (apiRetStatus != CY_U3P_SUCCESS)
  199. {
  200. CyU3PDebugPrint (4, "CyU3PSetEpConfig failed, Error code = %d\n", apiRetStatus);
  201. CyFxAppErrorHandler (apiRetStatus);
  202. }
  203.  
  204. /* Create a DMA AUTO_MANY_TO_ONE channel between
  205. * the producer and consumer sockets of the U port.
  206. * DMA size is set based on the USB speed. */
  207. dmaCfg.size = size;
  208. dmaCfg.count = CY_FX_BULKLP_DMA_BUF_COUNT;
  209. dmaCfg.validSckCount = 5;
  210. dmaCfg.prodSckId[0] = CY_FX_EP_PRODUCER_1_SOCKET;
  211. dmaCfg.prodSckId[1] = CY_FX_EP_PRODUCER_2_SOCKET;
  212. dmaCfg.prodSckId[2] = CY_FX_EP_PRODUCER_3_SOCKET;
  213. dmaCfg.consSckId[0] = CY_FX_EP_CONSUMER_SOCKET;
  214. dmaCfg.consSckId[1] = CY_FX_EP_DEBUG_SOCKET;
  215. dmaCfg.dmaMode = CY_U3P_DMA_MODE_BYTE;
  216. dmaCfg.notification = 0;
  217. dmaCfg.cb = NULL;
  218. dmaCfg.prodHeader = 0;
  219. dmaCfg.prodFooter = 0;
  220. dmaCfg.consHeader = 0;
  221. dmaCfg.prodAvailCount = 0;
  222.  
  223. apiRetStatus = CyU3PDmaMultiChannelCreate (&glChHandleBulkLp,
  224. CY_U3P_DMA_TYPE_AUTO_MANY_TO_ONE, &dmaCfg);
  225. if (apiRetStatus != CY_U3P_SUCCESS)
  226. {
  227. CyU3PDebugPrint (4, "CyU3PDmaMultiChannelCreate failed, Error code = %d\n", apiRetStatus);
  228. CyFxAppErrorHandler(apiRetStatus);
  229. }
  230.  
  231. /* Flush the Endpoint memory */
  232. CyU3PUsbFlushEp(CY_FX_EP_PRODUCER_1);
  233. CyU3PUsbFlushEp(CY_FX_EP_PRODUCER_2);
  234. CyU3PUsbFlushEp(CY_FX_EP_PRODUCER_3);
  235. CyU3PUsbFlushEp(CY_FX_EP_CONSUMER);
  236. CyU3PUsbFlushEp(CY_FX_EP_DEBUG);
  237.  
  238. /* Set DMA Channel transfer size. */
  239. apiRetStatus = CyU3PDmaMultiChannelSetXfer (&glChHandleBulkLp, CY_FX_BULKLP_DMA_TX_SIZE, 0);
  240. if (apiRetStatus != CY_U3P_SUCCESS)
  241. {
  242. CyU3PDebugPrint (4, "CyU3PDmaMultiChannelSetXfer Failed, Error code = %d\n", apiRetStatus);
  243. CyFxAppErrorHandler(apiRetStatus);
  244. }
  245.  
  246. apiRetStatus = CyU3PDebugInit (CY_FX_EP_DEBUG_SOCKET, 8);
  247. if (apiRetStatus != CY_U3P_SUCCESS)
  248. {
  249. CyU3PDebugPrint (4, "CyU3PDmaMultiChannelSetXfer Failed, Error code = %d\n", apiRetStatus);
  250. CyFxAppErrorHandler (apiRetStatus);
  251. }
  252.  
  253. /* Update the status flag. */
  254. glIsApplnActive = CyTrue;
  255. }
  256.  
  257. /* This function stops the bulk loop application. This shall be called whenever
  258. * a RESET or DISCONNECT event is received from the USB host. The endpoints are
  259. * disabled and the DMA pipe is destroyed by this function. */
  260. void
  261. CyFxBulkLpApplnStop (
  262. void)
  263. {
  264. CyU3PEpConfig_t epCfg;
  265. CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
  266.  
  267. /* Update the flag. */
  268. glIsApplnActive = CyFalse;
  269.  
  270. CyU3PDebugDeInit ();
  271.  
  272. /* Flush the endpoint memory */
  273. CyU3PUsbFlushEp(CY_FX_EP_PRODUCER_1);
  274. CyU3PUsbFlushEp(CY_FX_EP_PRODUCER_2);
  275. CyU3PUsbFlushEp(CY_FX_EP_PRODUCER_3);
  276. CyU3PUsbFlushEp(CY_FX_EP_CONSUMER);
  277. CyU3PUsbFlushEp(CY_FX_EP_DEBUG);
  278.  
  279. /* Destroy the channel */
  280. CyU3PDmaMultiChannelDestroy (&glChHandleBulkLp);
  281.  
  282. /* Disable endpoints. */
  283. CyU3PMemSet ((uint8_t *)&epCfg, 0, sizeof (epCfg));
  284. epCfg.enable = CyFalse;
  285.  
  286. /* Producer 1 endpoint configuration. */
  287. apiRetStatus = CyU3PSetEpConfig(CY_FX_EP_PRODUCER_1, &epCfg);
  288. if (apiRetStatus != CY_U3P_SUCCESS)
  289. {
  290. CyU3PDebugPrint (4, "CyU3PSetEpConfig failed, Error code = %d\n", apiRetStatus);
  291. CyFxAppErrorHandler (apiRetStatus);
  292. }
  293.  
  294. /* Producer 2 endpoint configuration. */
  295. apiRetStatus = CyU3PSetEpConfig(CY_FX_EP_PRODUCER_2, &epCfg);
  296. if (apiRetStatus != CY_U3P_SUCCESS)
  297. {
  298. CyU3PDebugPrint (4, "CyU3PSetEpConfig failed, Error code = %d\n", apiRetStatus);
  299. CyFxAppErrorHandler (apiRetStatus);
  300. }
  301.  
  302. /* Producer 3 endpoint configuration. */
  303. apiRetStatus = CyU3PSetEpConfig(CY_FX_EP_PRODUCER_3, &epCfg);
  304. if (apiRetStatus != CY_U3P_SUCCESS)
  305. {
  306. CyU3PDebugPrint (4, "CyU3PSetEpConfig failed, Error code = %d\n", apiRetStatus);
  307. CyFxAppErrorHandler (apiRetStatus);
  308. }
  309.  
  310. /* Consumer endpoint configuration. */
  311. apiRetStatus = CyU3PSetEpConfig(CY_FX_EP_CONSUMER, &epCfg);
  312. if (apiRetStatus != CY_U3P_SUCCESS)
  313. {
  314. CyU3PDebugPrint (4, "CyU3PSetEpConfig failed, Error code = %d\n", apiRetStatus);
  315. CyFxAppErrorHandler (apiRetStatus);
  316. }
  317.  
  318. apiRetStatus = CyU3PSetEpConfig(CY_FX_EP_DEBUG, &epCfg);
  319. if (apiRetStatus != CY_U3P_SUCCESS)
  320. {
  321. CyU3PDebugPrint (4, "CyU3PSetEpConfig failed, Error code = %d\n", apiRetStatus);
  322. CyFxAppErrorHandler (apiRetStatus);
  323. }
  324. }
  325.  
  326. /* Callback to handle the USB setup requests. */
  327. CyBool_t
  328. CyFxBulkLpApplnUSBSetupCB (
  329. uint32_t setupdat0, /* SETUP Data 0 */
  330. uint32_t setupdat1 /* SETUP Data 1 */
  331. )
  332. {
  333. /* Fast enumeration is used. Only requests addressed to the interface, class,
  334. * vendor and unknown control requests are received by this function.
  335. * This application does not support any class or vendor requests. */
  336.  
  337. uint8_t bRequest, bReqType;
  338. uint8_t bType, bTarget;
  339. uint16_t wValue, wIndex;
  340. CyBool_t isHandled = CyFalse;
  341.  
  342. /* Decode the fields from the setup request. */
  343. bReqType = (setupdat0 & CY_U3P_USB_REQUEST_TYPE_MASK);
  344. bType = (bReqType & CY_U3P_USB_TYPE_MASK);
  345. bTarget = (bReqType & CY_U3P_USB_TARGET_MASK);
  346. bRequest = ((setupdat0 & CY_U3P_USB_REQUEST_MASK) >> CY_U3P_USB_REQUEST_POS);
  347. wValue = ((setupdat0 & CY_U3P_USB_VALUE_MASK) >> CY_U3P_USB_VALUE_POS);
  348. wIndex = ((setupdat1 & CY_U3P_USB_INDEX_MASK) >> CY_U3P_USB_INDEX_POS);
  349.  
  350. if (bType == CY_U3P_USB_STANDARD_RQT)
  351. {
  352. /* Handle SET_FEATURE(FUNCTION_SUSPEND) and CLEAR_FEATURE(FUNCTION_SUSPEND)
  353. * requests here. It should be allowed to pass if the device is in configured
  354. * state and failed otherwise. */
  355. if ((bTarget == CY_U3P_USB_TARGET_INTF) && ((bRequest == CY_U3P_USB_SC_SET_FEATURE)
  356. || (bRequest == CY_U3P_USB_SC_CLEAR_FEATURE)) && (wValue == 0))
  357. {
  358. if (glIsApplnActive)
  359. CyU3PUsbAckSetup ();
  360. else
  361. CyU3PUsbStall (0, CyTrue, CyFalse);
  362.  
  363. isHandled = CyTrue;
  364. }
  365.  
  366. /* CLEAR_FEATURE request for endpoint is always passed to the setup callback
  367. * regardless of the enumeration model used. When a clear feature is received,
  368. * the previous transfer has to be flushed and cleaned up. This is done at the
  369. * protocol level. Since this is just a loopback operation, there is no higher
  370. * level protocol. So flush the EP memory and reset the DMA channel associated
  371. * with it. If there are more than one EP associated with the channel reset both
  372. * the EPs. The endpoint stall and toggle / sequence number is also expected to be
  373. * reset. Return CyFalse to make the library clear the stall and reset the endpoint
  374. * toggle. Or invoke the CyU3PUsbStall (ep, CyFalse, CyTrue) and return CyTrue.
  375. * Here we are clearing the stall. */
  376. if ((bTarget == CY_U3P_USB_TARGET_ENDPT) && (bRequest == CY_U3P_USB_SC_CLEAR_FEATURE)
  377. && (wValue == CY_U3P_USBX_FS_EP_HALT))
  378. {
  379. if ((wIndex == CY_FX_EP_PRODUCER_1) || (wIndex == CY_FX_EP_PRODUCER_2) || (wIndex == CY_FX_EP_PRODUCER_3) || (wIndex == CY_FX_EP_CONSUMER) || (wIndex == CY_FX_EP_DEBUG))
  380. {
  381. if (glIsApplnActive)
  382. {
  383. CyU3PDmaMultiChannelReset (&glChHandleBulkLp);
  384. CyU3PUsbFlushEp(CY_FX_EP_PRODUCER_1);
  385. CyU3PUsbFlushEp(CY_FX_EP_PRODUCER_2);
  386. CyU3PUsbFlushEp(CY_FX_EP_PRODUCER_3);
  387. CyU3PUsbFlushEp(CY_FX_EP_CONSUMER);
  388. CyU3PUsbFlushEp(CY_FX_EP_DEBUG);
  389.  
  390. CyU3PUsbResetEp (CY_FX_EP_PRODUCER_1);
  391. CyU3PUsbResetEp (CY_FX_EP_PRODUCER_2);
  392. CyU3PUsbResetEp (CY_FX_EP_PRODUCER_3);
  393. CyU3PUsbResetEp (CY_FX_EP_CONSUMER);
  394. CyU3PUsbResetEp (CY_FX_EP_DEBUG);
  395.  
  396. CyU3PDmaMultiChannelSetXfer (&glChHandleBulkLp, CY_FX_BULKLP_DMA_TX_SIZE, 0);
  397. CyU3PUsbStall (wIndex, CyFalse, CyTrue);
  398. isHandled = CyTrue;
  399. }
  400. }
  401. }
  402. }
  403.  
  404. return isHandled;
  405. }
  406.  
  407. /* This is the callback function to handle the USB events. */
  408. void
  409. CyFxBulkLpApplnUSBEventCB (
  410. CyU3PUsbEventType_t evtype, /* Event type */
  411. uint16_t evdata /* Event data */
  412. )
  413. {
  414. switch (evtype)
  415. {
  416. case CY_U3P_USB_EVENT_SETCONF:
  417. /* Stop the application before restarting. */
  418. if (glIsApplnActive)
  419. {
  420. CyFxBulkLpApplnStop ();
  421. }
  422. /* Start the loop back function. */
  423. CyFxBulkLpApplnStart ();
  424. break;
  425.  
  426. case CY_U3P_USB_EVENT_RESET:
  427. case CY_U3P_USB_EVENT_DISCONNECT:
  428. /* Stop the loop back function. */
  429. if (glIsApplnActive)
  430. {
  431. CyFxBulkLpApplnStop ();
  432. }
  433. break;
  434.  
  435. default:
  436. break;
  437. }
  438. }
  439.  
  440. /* Callback function to handle LPM requests from the USB 3.0 host. This function is invoked by the API
  441. whenever a state change from U0 -> U1 or U0 -> U2 happens. If we return CyTrue from this function, the
  442. FX3 device is retained in the low power state. If we return CyFalse, the FX3 device immediately tries
  443. to trigger an exit back to U0.
  444.  
  445. This application does not have any state in which we should not allow U1/U2 transitions; and therefore
  446. the function always return CyTrue.
  447. */
  448. CyBool_t
  449. CyFxBulkLpApplnLPMRqtCB (
  450. CyU3PUsbLinkPowerMode link_mode)
  451. {
  452. return CyTrue;
  453. }
  454.  
  455. /* This function initializes the USB Module, sets the enumeration descriptors.
  456. * This function does not start the bulk streaming and this is done only when
  457. * SET_CONF event is received. */
  458. void
  459. CyFxBulkLpApplnInit (void)
  460. {
  461. CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
  462.  
  463. /* Start the USB functionality. */
  464. apiRetStatus = CyU3PUsbStart();
  465. if (apiRetStatus != CY_U3P_SUCCESS)
  466. {
  467. CyU3PDebugPrint (4, "CyU3PUsbStart failed to Start, Error code = %d\n", apiRetStatus);
  468. CyFxAppErrorHandler(apiRetStatus);
  469. }
  470.  
  471. /* The fast enumeration is the easiest way to setup a USB connection,
  472. * where all enumeration phase is handled by the library. Only the
  473. * class / vendor requests need to be handled by the application. */
  474. CyU3PUsbRegisterSetupCallback(CyFxBulkLpApplnUSBSetupCB, CyTrue);
  475.  
  476. /* Setup the callback to handle the USB events. */
  477. CyU3PUsbRegisterEventCallback(CyFxBulkLpApplnUSBEventCB);
  478.  
  479. /* Register a callback to handle LPM requests from the USB 3.0 host. */
  480. CyU3PUsbRegisterLPMRequestCallback(CyFxBulkLpApplnLPMRqtCB);
  481.  
  482. /* Set the USB Enumeration descriptors */
  483.  
  484. /* Super speed device descriptor. */
  485. apiRetStatus = CyU3PUsbSetDesc(CY_U3P_USB_SET_SS_DEVICE_DESCR, NULL, (uint8_t *)CyFxUSB30DeviceDscr);
  486. if (apiRetStatus != CY_U3P_SUCCESS)
  487. {
  488. CyU3PDebugPrint (4, "USB set device descriptor failed, Error code = %d\n", apiRetStatus);
  489. CyFxAppErrorHandler(apiRetStatus);
  490. }
  491.  
  492. /* High speed device descriptor. */
  493. apiRetStatus = CyU3PUsbSetDesc(CY_U3P_USB_SET_HS_DEVICE_DESCR, NULL, (uint8_t *)CyFxUSB20DeviceDscr);
  494. if (apiRetStatus != CY_U3P_SUCCESS)
  495. {
  496. CyU3PDebugPrint (4, "USB set device descriptor failed, Error code = %d\n", apiRetStatus);
  497. CyFxAppErrorHandler(apiRetStatus);
  498. }
  499.  
  500. /* BOS descriptor */
  501. apiRetStatus = CyU3PUsbSetDesc(CY_U3P_USB_SET_SS_BOS_DESCR, NULL, (uint8_t *)CyFxUSBBOSDscr);
  502. if (apiRetStatus != CY_U3P_SUCCESS)
  503. {
  504. CyU3PDebugPrint (4, "USB set configuration descriptor failed, Error code = %d\n", apiRetStatus);
  505. CyFxAppErrorHandler(apiRetStatus);
  506. }
  507.  
  508. /* Device qualifier descriptor */
  509. apiRetStatus = CyU3PUsbSetDesc(CY_U3P_USB_SET_DEVQUAL_DESCR, NULL, (uint8_t *)CyFxUSBDeviceQualDscr);
  510. if (apiRetStatus != CY_U3P_SUCCESS)
  511. {
  512. CyU3PDebugPrint (4, "USB set device qualifier descriptor failed, Error code = %d\n", apiRetStatus);
  513. CyFxAppErrorHandler(apiRetStatus);
  514. }
  515.  
  516. /* Super speed configuration descriptor */
  517. apiRetStatus = CyU3PUsbSetDesc(CY_U3P_USB_SET_SS_CONFIG_DESCR, NULL, (uint8_t *)CyFxUSBSSConfigDscr);
  518. if (apiRetStatus != CY_U3P_SUCCESS)
  519. {
  520. CyU3PDebugPrint (4, "USB set configuration descriptor failed, Error code = %d\n", apiRetStatus);
  521. CyFxAppErrorHandler(apiRetStatus);
  522. }
  523.  
  524. /* High speed configuration descriptor */
  525. apiRetStatus = CyU3PUsbSetDesc(CY_U3P_USB_SET_HS_CONFIG_DESCR, NULL, (uint8_t *)CyFxUSBHSConfigDscr);
  526. if (apiRetStatus != CY_U3P_SUCCESS)
  527. {
  528. CyU3PDebugPrint (4, "USB Set Other Speed Descriptor failed, Error Code = %d\n", apiRetStatus);
  529. CyFxAppErrorHandler(apiRetStatus);
  530. }
  531.  
  532. /* Full speed configuration descriptor */
  533. apiRetStatus = CyU3PUsbSetDesc(CY_U3P_USB_SET_FS_CONFIG_DESCR, NULL, (uint8_t *)CyFxUSBFSConfigDscr);
  534. if (apiRetStatus != CY_U3P_SUCCESS)
  535. {
  536. CyU3PDebugPrint (4, "USB Set Configuration Descriptor failed, Error Code = %d\n", apiRetStatus);
  537. CyFxAppErrorHandler(apiRetStatus);
  538. }
  539.  
  540. /* String descriptor 0 */
  541. apiRetStatus = CyU3PUsbSetDesc(CY_U3P_USB_SET_STRING_DESCR, 0, (uint8_t *)CyFxUSBStringLangIDDscr);
  542. if (apiRetStatus != CY_U3P_SUCCESS)
  543. {
  544. CyU3PDebugPrint (4, "USB set string descriptor failed, Error code = %d\n", apiRetStatus);
  545. CyFxAppErrorHandler(apiRetStatus);
  546. }
  547.  
  548. /* String descriptor 1 */
  549. apiRetStatus = CyU3PUsbSetDesc(CY_U3P_USB_SET_STRING_DESCR, 1, (uint8_t *)CyFxUSBManufactureDscr);
  550. if (apiRetStatus != CY_U3P_SUCCESS)
  551. {
  552. CyU3PDebugPrint (4, "USB set string descriptor failed, Error code = %d\n", apiRetStatus);
  553. CyFxAppErrorHandler(apiRetStatus);
  554. }
  555.  
  556. /* String descriptor 2 */
  557. apiRetStatus = CyU3PUsbSetDesc(CY_U3P_USB_SET_STRING_DESCR, 2, (uint8_t *)CyFxUSBProductDscr);
  558. if (apiRetStatus != CY_U3P_SUCCESS)
  559. {
  560. CyU3PDebugPrint (4, "USB set string descriptor failed, Error code = %d\n", apiRetStatus);
  561. CyFxAppErrorHandler(apiRetStatus);
  562. }
  563.  
  564. /* Connect the USB Pins with super speed operation enabled. */
  565. apiRetStatus = CyU3PConnectState(CyTrue, CyTrue);
  566. if (apiRetStatus != CY_U3P_SUCCESS)
  567. {
  568. CyU3PDebugPrint (4, "USB Connect failed, Error code = %d\n", apiRetStatus);
  569. CyFxAppErrorHandler(apiRetStatus);
  570. }
  571. }
  572.  
  573. /* Entry function for the BulkLpAppThread. */
  574. void
  575. BulkLpAppThread_Entry (
  576. uint32_t input)
  577. {
  578. /* Initialize the debug module */
  579. CyFxBulkLpApplnDebugInit();
  580.  
  581. /* Initialize the bulk loop application */
  582. CyFxBulkLpApplnInit();
  583.  
  584. for (;;)
  585. {
  586. CyU3PThreadSleep (1000);
  587. if (glIsApplnActive)
  588. {
  589. CyU3PDebugPrint (2, "USB Debug logger: time from start in ticks: %d\n", CyU3PGetTime ());
  590. }
  591. }
  592. }
  593.  
  594. /* Application define function which creates the threads. */
  595. void
  596. CyFxApplicationDefine (
  597. void)
  598. {
  599. void *ptr;
  600. uint32_t retThrdCreate = CY_U3P_SUCCESS;
  601.  
  602. /* Allocate the memory for the threads */
  603. ptr = CyU3PMemAlloc (CY_FX_BULKLP_THREAD_STACK);
  604.  
  605. /* Create the thread for the application */
  606. retThrdCreate = CyU3PThreadCreate (&BulkLpAppThread, /* Bulk loop App Thread structure */
  607. "21:Bulk_loop_AUTO_MANY_TO_ONE", /* Thread ID and Thread name */
  608. BulkLpAppThread_Entry, /* Bulk loop App Thread Entry function */
  609. 0, /* No input parameter to thread */
  610. ptr, /* Pointer to the allocated thread stack */
  611. CY_FX_BULKLP_THREAD_STACK, /* Bulk loop App Thread stack size */
  612. CY_FX_BULKLP_THREAD_PRIORITY, /* Bulk loop App Thread priority */
  613. CY_FX_BULKLP_THREAD_PRIORITY, /* Bulk loop App Thread priority */
  614. CYU3P_NO_TIME_SLICE, /* No time slice for the application thread */
  615. CYU3P_AUTO_START /* Start the Thread immediately */
  616. );
  617.  
  618. /* Check the return code */
  619. if (retThrdCreate != 0)
  620. {
  621. /* Thread Creation failed with the error code retThrdCreate */
  622.  
  623. /* Add custom recovery or debug actions here */
  624.  
  625. /* Application cannot continue */
  626. /* Loop indefinitely */
  627. while(1);
  628. }
  629. }
  630.  
  631. /*
  632. * Main function
  633. */
  634. int
  635. main (void)
  636. {
  637. CyU3PIoMatrixConfig_t io_cfg;
  638. CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
  639.  
  640. /* Initialize the device */
  641. status = CyU3PDeviceInit (NULL);
  642. if (status != CY_U3P_SUCCESS)
  643. {
  644. goto handle_fatal_error;
  645. }
  646.  
  647. /* Initialize the caches. Enable both Instruction and Data Caches. */
  648. status = CyU3PDeviceCacheControl (CyTrue, CyTrue, CyTrue);
  649. if (status != CY_U3P_SUCCESS)
  650. {
  651. goto handle_fatal_error;
  652. }
  653.  
  654. /* Configure the IO matrix for the device. On the FX3 DVK board, the COM port
  655. * is connected to the IO(53:56). This means that either DQ32 mode should be
  656. * selected or lppMode should be set to UART_ONLY. Here we are choosing
  657. * UART_ONLY configuration. */
  658. io_cfg.isDQ32Bit = CyFalse;
  659. io_cfg.useUart = CyTrue;
  660. io_cfg.useI2C = CyFalse;
  661. io_cfg.useI2S = CyFalse;
  662. io_cfg.useSpi = CyFalse;
  663. io_cfg.lppMode = CY_U3P_IO_MATRIX_LPP_UART_ONLY;
  664. /* No GPIOs are enabled. */
  665. io_cfg.gpioSimpleEn[0] = 0;
  666. io_cfg.gpioSimpleEn[1] = 0;
  667. io_cfg.gpioComplexEn[0] = 0;
  668. io_cfg.gpioComplexEn[1] = 0;
  669. status = CyU3PDeviceConfigureIOMatrix (&io_cfg);
  670. if (status != CY_U3P_SUCCESS)
  671. {
  672. goto handle_fatal_error;
  673. }
  674.  
  675. /* This is a non returnable call for initializing the RTOS kernel */
  676. CyU3PKernelEntry ();
  677.  
  678. /* Dummy return to make the compiler happy */
  679. return 0;
  680.  
  681. handle_fatal_error:
  682.  
  683. /* Cannot recover from this error. */
  684. while (1);
  685. }
  686.  
  687. /* [ ] */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement