When should I call CyU3PUsbSendEP0Data in fx3

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Ready
Level 2
Level 2
10 sign-ins 10 replies posted 5 sign-ins

Hello, recently I'm developing on the fx3 chip, and I'm wondering when should I call CyU3PUsbSendEP0Data to get the data from a control transfer. Must I call it in side the callback that is register in CyU3PUsbRegisterSetupCallback? Or could I also only check there is a new control transfer in that callback and call the CyU3PUsbSendEP0Data later in the main thread (AppThread_Entry)? 

Thanks in advance!

0 Likes
1 Solution

Hello,

As I mentioned before, the host cannot initiate another request until the data phase of the ongoing request is completed. So, there wont be a case as you mentioned.

Best Regards,
Jayakrishna

View solution in original post

11 Replies
JayakrishnaT_76
Moderator
Moderator
Moderator
First question asked 1000 replies posted 750 replies posted

Hello,

Can you please let me know what exactly is your end application?

If you want to respond to a USB request from host, then you should be using the API CyU3PUsbSendEP0Data () inside the callback. You can parse the request inside the callback function and respond accordingly. Please refer to the following SDK example project to understand it better:

C:\Program Files (x86)\Cypress\EZ-USB FX3 SDK\1.3\firmware\basic_examples\cyfxbulksrcsink

Best Regards,
Jayakrishna
0 Likes

Thank you, I checked this example, but I have another question:

In the example, the CyU3PUsbSendEP0Data is called in the main thread, which is BulkSrcSinkAppThread_Entry. In this case, what if a new control transfer arrives and the firmware is still process the previous control write transfer? Let's say that the firmware in BulkSrcSinkAppThread_Entry haven't processed the gl_setupdat0 and gl_setupdat1 of the previous control write transfer, and in this time a new control transfer is sent by the host, then what would happen? Would the  gl_setupdat0 and gl_setupdat1 be set to new value in the CyFxBulkSrcSinkApplnUSBSetupCB callback?

0 Likes

Hello,

Actually, the host will wait until the data stage of a control request is complete. So,  the host will wait until the request is completely handled (i.e the data is sent back to the host). Only after that it will send a new command. So, it is very unlikely that the gl_setupdat0 and gl_setupdat1 can change in this implementation.

You can also choose to handle the request within the callback function as done in the below example:

C:\Program Files (x86)\Cypress\EZ-USB FX3 SDK\1.3\firmware\basic_examples\cyfxflashprog

Best Regards,
Jayakrishna
0 Likes

Sorry I made a mistake in the last reply, the  CyU3PUsbGetEP0Data is called in the main thread instead of in the callback, so it is still the same in this case? What would happen if CyU3PUsbGetEP0Data is not called but a new control transfer arrives? 

0 Likes

Hello,

Yes, it is the same. The API CyU3PUsbGetEP0Data () in USBBulkSrcSink project is called when the vendor command 0x80 is issued to the device. As part of this command, the host is expected to send data to the device. If the API CyU3PUsbGetEP0Data () is used in firmware without the host sending data, then it will fail with a timeout error.

Best Regards,
Jayakrishna
0 Likes

Thanks, so for a control write transfer (host sends data to fx3), it's impossible to receive another control transfer if CyU3PUsbGetEP0Data is not called by fx3?

0 Likes

Hello,

It is not impossible. But, the first control transfer will fail with timeout on the host side if the data is not received by FX3.

Best Regards,
Jayakrishna
0 Likes

But then there would be a dangerous case . We process the CyU3PUsbGetEP0Data in the main thread, so we will parse the gl_setupdat0 and gl_setupdat1 in the main thread to get the vendor command. But then if the host sends another control transfer before we finished process the current control write transfer, the callback will be called, and gl_setupdat0 and gl_setupdat1 will be updated in the callback. Then we can't guarantee we can process the right variables, and when we call CyU3PUsbGetEP0Data in the main thread, we can't know at that time this is a call to to get the data from the previous control write transfer or the new arrived one. 

0 Likes

Hello,

Apologies for not clarity of explanation in my previous response. 

1. If the host sends a vendor command. Then the device should read it by using the API CyU3PUsbGetEP0Data  (). 

2. If the device does not read the data using the API CyU3PUsbGetEP0Data (), then the control request will fail on the host side. You can initiate another control request after the previous one failed.

3. If the device uses the API CyU3PUsbGetEP0Data  () to read the incoming data from the host, then the transfer is ongoing. The host can send another transfer only if the existing transfer is completed.

I hope this answers your queries. Please let me know if you have any doubts on this.

Best Regards,
Jayakrishna
0 Likes

Thank you for your explanation, but let's think about a case:

The host send a control write transfer to fx3, and the fx3 received it and is going to read the data through  CyU3PUsbGetEP0Data in the main thread, but we know it still would take some time to process from the callback to the place where we call CyU3PUsbGetEP0Data in the main thread. What if during this small period, the host send another control transfer? Would the callback be called again?

  • if the callback is called again, then would there be a chance that the global variables gl_setupdat0 and gl_setupdat1 be updated first before the fx3 process them in the main thread (because there is a time window between the callback to the place where we call CyU3PUsbGetEP0Data in the main thread for the first control transfer)
  • if the callback is called again, before we call the CyU3PUsbGetEP0Data in the main thread, then fx3 runs into the place where we call CyU3PUsbGetEP0Data in the main thread, is the call to CyU3PUsbGetEP0Data to read the data from the first transfer or the second? 
0 Likes

Hello,

As I mentioned before, the host cannot initiate another request until the data phase of the ongoing request is completed. So, there wont be a case as you mentioned.

Best Regards,
Jayakrishna