<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
	<link rel="self" type="application/atom+xml" href="https://forums.obdev.at/app.php/feed/topic/6465" />

	<title>Objective Development Forums</title>
	
	<link href="https://forums.obdev.at/index.php" />
	<updated>2012-02-24T13:43:52+02:00</updated>

	<author><name><![CDATA[Objective Development Forums]]></name></author>
	<id>https://forums.obdev.at/app.php/feed/topic/6465</id>

		<entry>
		<author><name><![CDATA[jokkebk]]></name></author>
		<updated>2012-02-24T13:43:52+02:00</updated>

		<published>2012-02-24T13:43:52+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=6465&amp;p=21022#p21022</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=6465&amp;p=21022#p21022"/>
		<title type="html"><![CDATA[BUG or feature in using usbFunctionRead?]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=6465&amp;p=21022#p21022"><![CDATA[
I tried to use usbFunctionRead today to transfer longer strings to PC. In a nutshell, the firmware code looked like this (nothing special):<br /><br /><div class="codebox"><p>Code: </p><pre><code>static int dataSent; // for USB_DATA_LONGOUT<br /><br />USB_PUBLIC usbMsgLen_t usbFunctionSetup(uchar data&#91;8&#93;) {<br />    usbRequest_t *rq = (void *)data; // cast data to correct type<br />   <br />    if(rq-&gt;bRequest == USB_DATA_LONGOUT) {<br />        dataSent = 0;<br />        return USB_NO_MSG;<br />    }<br />}<br /><br />USB_PUBLIC uchar usbFunctionRead(uchar *data, uchar len) {<br />    uchar i;<br /><br />    for(i = 0; dataSent &lt; 128 &amp;&amp; i &lt; len; i++, dataSent++)<br />        data&#91;i&#93; = '0'+i;<br />    <br />    if(i &amp;&amp; dataSent == 128) <br />        data&#91;i-1&#93; = '\0';<br />      <br />    return i; // equals the amount of bytes written<br />}<br /></code></pre></div><br /><br />Then I defined USB_CFG_IMPLEMENT_FN_READ and flashed. On PC side, I used libusb with essentially the following code (nothing special here either):<br /><br /><div class="codebox"><p>Code: </p><pre><code>char buffer&#91;256&#93; = &quot;&quot;;<br /><br />nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, <br />    USB_DATA_LONGOUT, 0, 0, (char *)buffer, sizeof(buffer), 5000);<br /><br />printf(&quot;Received %d bytes: %s\n&quot;, nBytes, buffer);<br />   <br />if(nBytes &lt; 0)<br />    fprintf(stderr, &quot;USB error: %s\n&quot;, usb_strerror());<br />else<br />    printf(&quot;Received %d bytes: %s\n&quot;, nBytes, buffer);      <br /></code></pre></div><br /><br /><ul><li>With buffer size &gt;= 256, I'm able to receive size&amp;255 bytes. It seems while wLength is word-sized, V-USB is only using the lower byte</li><li>With buffer size == 255, I get &quot;USB error: libusb0-dll:err [control_msg] sending control message failed, win error: The I/O operation has been aborted because of either a thread exit or an application request.&quot;</li><li>With buffer size &lt;= 254, everything works as it should</li></ul><br />It seems like a bug, or a <span style="text-decoration: underline">very</span> strange feature. Especially as the &quot;standard&quot; V-USB method of returning stuff with usbMsgPtr instead of usbFunctionRead completely works with any buffer size. Defining USB_CFG_LONG_TRANSFERS immediately fixes this problem.<br /><br />Joonas<p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=5127">jokkebk</a> — Fri Feb 24, 2012 1:43 pm</p><hr />
]]></content>
	</entry>
	</feed>
