<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>LPC MicrocontrollersのトピックUSB ROM API: generic HID example bug?</title>
    <link>https://community.nxp.com/t5/LPC-Microcontrollers/USB-ROM-API-generic-HID-example-bug/m-p/523947#M6583</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by mch0 on Mon Sep 01 01:11:27 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm working on a HID interface using the ROM API (LPC4370).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am using the LPCOpen generic HID example as a starting point and the code is working so far.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;However, since I'll have to add more functionality I must also understand at least some implications of the ROM API. For example, where and when to allocate memory for what purpose.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;During that process I found out that during the various init()-calls the parameter blocks are also used to keep track of the "top of memory" and the remaining size for usage by the ROM stack.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would have liked to read about that in some documentation but I did not find it mentioned yet in the docs I have read or got so far.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, my current problem now is one of understanding the example code, actually I think it's buggy.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The bug does NOT trigger and will not in the example, but I'd really like to see if my understanding of the memory allocation strategy is correct or not.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Details:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The code snippet is from the LPCOpen 2.12 example &lt;/SPAN&gt;&lt;I&gt;usb_rom_hid_generic&lt;/I&gt;&lt;SPAN&gt;, file &lt;/SPAN&gt;&lt;I&gt;hid_generic.c&lt;/I&gt;&lt;SPAN&gt;, function &lt;/SPAN&gt;&lt;BR /&gt;&lt;I&gt;usb_hid_init(...)&lt;/I&gt;&lt;SPAN&gt;:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ret = USBD_API-&amp;gt;hid-&amp;gt;init(hUsb, &amp;amp;hid_param);
/* allocate USB accessable memory space for report data */
loopback_report =&amp;nbsp; (uint8_t *) hid_param.mem_base;
hid_param.mem_base += 4;
hid_param.mem_size += 4;
/* update memory variables */
*mem_base = hid_param.mem_base;
*mem_size = hid_param.mem_size;
return ret;&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As I understand it the first line (API init call) takes the current memory base and available size as given in the &lt;/SPAN&gt;&lt;I&gt;hid_param&lt;/I&gt;&lt;SPAN&gt;-structure and updates both the base and the size entries. So after the call we know how many bytes are still available and where that area starts.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now this example uses a 1 byte report and somewhere the buffer for it must be allocated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For that the example also uses the ROM API stack area and updates the structure entries manually.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If so, I do understand the first calculation (&lt;/SPAN&gt;&lt;I&gt;hid_param.mem_base += 4;&lt;/I&gt;&lt;SPAN&gt;) to some extent, since maybe the programmer wanted to keep a word alignment. In my opinipn +1 would have been sufficient.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;But I do not understand the second calculation &lt;/SPAN&gt;&lt;I&gt;hid_param.mem_size += 4;&lt;/I&gt;&lt;SPAN&gt; since that shows an increases the remaining space! Shouldn't it decrease by the allocated amount, i.e. 4?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Also the function reports the new memory data back to the caller, so that the size information is also corrupted in the more general &lt;/SPAN&gt;&lt;I&gt;usb_param&lt;/I&gt;&lt;SPAN&gt;-structure.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As said, with the example it does not matter, but for understanding what happens it's important for me.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;After all I'll use much longer reports and maybe more of them.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Best regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;BR /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 15 Jun 2016 16:46:18 GMT</pubDate>
    <dc:creator>lpcware</dc:creator>
    <dc:date>2016-06-15T16:46:18Z</dc:date>
    <item>
      <title>USB ROM API: generic HID example bug?</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/USB-ROM-API-generic-HID-example-bug/m-p/523947#M6583</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by mch0 on Mon Sep 01 01:11:27 MST 2014&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm working on a HID interface using the ROM API (LPC4370).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am using the LPCOpen generic HID example as a starting point and the code is working so far.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;However, since I'll have to add more functionality I must also understand at least some implications of the ROM API. For example, where and when to allocate memory for what purpose.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;During that process I found out that during the various init()-calls the parameter blocks are also used to keep track of the "top of memory" and the remaining size for usage by the ROM stack.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would have liked to read about that in some documentation but I did not find it mentioned yet in the docs I have read or got so far.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, my current problem now is one of understanding the example code, actually I think it's buggy.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The bug does NOT trigger and will not in the example, but I'd really like to see if my understanding of the memory allocation strategy is correct or not.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Details:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The code snippet is from the LPCOpen 2.12 example &lt;/SPAN&gt;&lt;I&gt;usb_rom_hid_generic&lt;/I&gt;&lt;SPAN&gt;, file &lt;/SPAN&gt;&lt;I&gt;hid_generic.c&lt;/I&gt;&lt;SPAN&gt;, function &lt;/SPAN&gt;&lt;BR /&gt;&lt;I&gt;usb_hid_init(...)&lt;/I&gt;&lt;SPAN&gt;:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ret = USBD_API-&amp;gt;hid-&amp;gt;init(hUsb, &amp;amp;hid_param);
/* allocate USB accessable memory space for report data */
loopback_report =&amp;nbsp; (uint8_t *) hid_param.mem_base;
hid_param.mem_base += 4;
hid_param.mem_size += 4;
/* update memory variables */
*mem_base = hid_param.mem_base;
*mem_size = hid_param.mem_size;
return ret;&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As I understand it the first line (API init call) takes the current memory base and available size as given in the &lt;/SPAN&gt;&lt;I&gt;hid_param&lt;/I&gt;&lt;SPAN&gt;-structure and updates both the base and the size entries. So after the call we know how many bytes are still available and where that area starts.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now this example uses a 1 byte report and somewhere the buffer for it must be allocated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For that the example also uses the ROM API stack area and updates the structure entries manually.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If so, I do understand the first calculation (&lt;/SPAN&gt;&lt;I&gt;hid_param.mem_base += 4;&lt;/I&gt;&lt;SPAN&gt;) to some extent, since maybe the programmer wanted to keep a word alignment. In my opinipn +1 would have been sufficient.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;But I do not understand the second calculation &lt;/SPAN&gt;&lt;I&gt;hid_param.mem_size += 4;&lt;/I&gt;&lt;SPAN&gt; since that shows an increases the remaining space! Shouldn't it decrease by the allocated amount, i.e. 4?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Also the function reports the new memory data back to the caller, so that the size information is also corrupted in the more general &lt;/SPAN&gt;&lt;I&gt;usb_param&lt;/I&gt;&lt;SPAN&gt;-structure.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As said, with the example it does not matter, but for understanding what happens it's important for me.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;After all I'll use much longer reports and maybe more of them.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Best regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;BR /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 16:46:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/USB-ROM-API-generic-HID-example-bug/m-p/523947#M6583</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T16:46:18Z</dc:date>
    </item>
  </channel>
</rss>

