<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">Hi Alex,<br>
      <br>
      I haven't gotten round to writing up the protocol yet, but it is
      reasonably simple. Here is a fairly long and detailed email, which
      will probably be copied to the wiki at some point.<br>
      <br>
      <br>
      The device appears as a custom USB device, and all of the commands
      are sent as control transfers. There is almost a one-to-one
      mapping between the methods in the <tt>pyenergy.EnergyMonitor</tt>
      class and the control transfers performed to the device. The only
      slight complication is there are 3 ADCs and 4 measurement points,
      the mapping of which ADC measures which measurement point is done
      in the <tt>enableMeasurementPoint</tt> method (with request
      number 7).<br>
      <br>
      For the exact details, see: <a
href="https://github.com/jpallister/stm32f4-energy-monitor/blob/pyusb/src/python/pyenergy.py">https://github.com/jpallister/stm32f4-energy-monitor/blob/pyusb/src/python/pyenergy.py</a><br>
      <br>
      The actual control transfers themselves use the <tt>bRequest</tt>
      parameter of the control transfer to differentiate between types
      of action. The <tt>wValue</tt> and <tt>wIndex</tt> parameters
      both specify data for that particular request. There are many
      possible requests:<br>
      <br>
      <table width="100%" border="1" cellpadding="2" cellspacing="2">
        <tbody>
          <tr>
            <td valign="top">Request
              <meta http-equiv="content-type" content="text/html;
                charset=ISO-8859-1">
              <br>
            </td>
            <td valign="top">bRequest</td>
            <td valign="top">wValue</td>
            <td valign="top">wIndex</td>
            <td valign="top">Description</td>
          </tr>
          <tr>
            <td valign="top">Toggle LEDS<br>
            </td>
            <td valign="top">0<br>
            </td>
            <td valign="top">N/A<br>
            </td>
            <td valign="top">N/A<br>
            </td>
            <td valign="top">Toggle one of the LEDs on the board<br>
            </td>
          </tr>
          <tr>
            <td valign="top">Start Measurement<br>
            </td>
            <td valign="top">1<br>
            </td>
            <td valign="top">Measurement point<br>
            </td>
            <td valign="top">N/A<br>
            </td>
            <td valign="top">Start measuring the measurement point
              specified by wValue<br>
            </td>
          </tr>
          <tr>
            <td valign="top">Stop Measurement<br>
            </td>
            <td valign="top">2<br>
            </td>
            <td valign="top">Measurement point<br>
            </td>
            <td valign="top">N/A<br>
            </td>
            <td valign="top">Stop measurement the measurement point
              specified by wValue<br>
            </td>
          </tr>
          <tr>
            <td valign="top">Set Serial<br>
            </td>
            <td valign="top">3<br>
            </td>
            <td valign="top">Low chars<br>
            </td>
            <td valign="top">High chars<br>
            </td>
            <td valign="top">Set the 4 character serial of the device. <br>
              <br>
              Bottom 8 bits of wValue is ASCII, character 0. Top 8 bits
              is character 1. <br>
              Bottom 8 bits of wIndex is character 2 and top is
              character 3<br>
            </td>
          </tr>
          <tr>
            <td valign="top">Set Trigger<br>
            </td>
            <td valign="top">4<br>
            </td>
            <td valign="top">Measurement point and pin<br>
            </td>
            <td valign="top">Port<br>
            </td>
            <td valign="top">Set a trigger on a pin, so that measurement
              is automatically triggered on a rising edge, and stopped
              on a falling edge.<br>
              <br>
              Bottom 8 bits is the pin number (wValue).<br>
              Top 8 bits is the measurement point number.<br>
              wIndex is the ASCII representation of the port (A, B,
              C...)<br>
              Currently I have only tested this with PA0.<br>
            </td>
          </tr>
          <tr>
            <td valign="top">Get Energy<br>
            </td>
            <td valign="top">6<br>
            </td>
            <td valign="top">Measurement point<br>
            </td>
            <td valign="top">N/A<br>
            </td>
            <td valign="top">Get the accumulated energy figures.<br>
              <br>
              Returns a 48 byte structure.<br>
              <br>
              <meta http-equiv="content-type" content="text/html;
                charset=ISO-8859-1">
              <pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="line" id="LC107" style="box-sizing: border-box; padding-left: 10px;"><span class="k" style="box-sizing: border-box; font-weight: bold;">typedef</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">struct</span> <span class="p" style="box-sizing: border-box;">{</span></div><div class="line" id="LC108" style="box-sizing: border-box; padding-left: 10px;">    <span class="kt" style="box-sizing: border-box; color: rgb(68, 85, 136); font-weight: bold;">uint64_t</span> <span class="n" style="box-sizing: border-b
ox; color: rgb(51, 51, 51);">energy_accum</span><span class="p" style="box-sizing: border-box;">;</span></div><div class="line" id="LC109" style="box-sizing: border-box; padding-left: 10px;">    <span class="kt" style="box-sizing: border-box; color: rgb(68, 85, 136); font-weight: bold;">uint64_t</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">elapsed_time</span><span class="p" style="box-sizing: border-box;">;</span></div><div class="line" id="LC110" style="box-sizing: border-box; padding-left: 10px;">    <span class="kt" style="box-sizing: border-box; color: rgb(68, 85, 136); font-weight: bold;">unsigned</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">peak_power</span><span class="p" style="box-sizing: border-box;">;</span></div><div class="line" id="LC111" style="box-sizing: border-box; padding-left: 10px;">    <span class="kt" style="box-sizing: border-box; colo
r: rgb(68, 85, 136); font-weight: bold;">unsigned</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">peak_voltage</span><span class="p" style="box-sizing: border-box;">;</span></div><div class="line" id="LC112" style="box-sizing: border-box; padding-left: 10px;">    <span class="kt" style="box-sizing: border-box; color: rgb(68, 85, 136); font-weight: bold;">unsigned</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">peak_current</span><span class="p" style="box-sizing: border-box;">;</span></div><div class="line" id="LC113" style="box-sizing: border-box; padding-left: 10px;">    <span class="kt" style="box-sizing: border-box; color: rgb(68, 85, 136); font-weight: bold;">unsigned</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">n_samples</span><span class="p" style="box-sizing: border-box;">;</span></div><div class="line" id="LC114" style="box-sizing: border-box
; padding-left: 10px;">    <span class="kt" style="box-sizing: border-box; color: rgb(68, 85, 136); font-weight: bold;">uint64_t</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">avg_current</span><span class="p" style="box-sizing: border-box;">;</span></div><div class="line" id="LC115" style="box-sizing: border-box; padding-left: 10px;">    <span class="kt" style="box-sizing: border-box; color: rgb(68, 85, 136); font-weight: bold;">uint64_t</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">avg_voltage</span><span class="p" style="box-sizing: border-box;">;</span></div><div class="line" id="LC116" style="box-sizing: border-box; padding-left: 10px;"><span class="p" style="box-sizing: border-box;">}</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">accumulated_data</span><span class="p" style="box-sizing: border-box;">;</span></div></pre>
              <br>
              Have a look at the convertData method in pyenergy.py for
              how to convert these figures to real units.<br>
              <br>
            </td>
          </tr>
          <tr>
            <td valign="top">Map ADC to measurement point<br>
            </td>
            <td valign="top">7<br>
            </td>
            <td valign="top">Measurement point<br>
            </td>
            <td valign="top">ADC<br>
            </td>
            <td valign="top">Map an ADC to a measurement point.<br>
              <br>
              ADC = number 0-2.<br>
              Probably best not to do this while the measurement is
              running<br>
            </td>
          </tr>
          <tr>
            <td valign="top">Is running<br>
            </td>
            <td valign="top">8<br>
            </td>
            <td valign="top">Measurement point<br>
            </td>
            <td valign="top">N/A<br>
            </td>
            <td valign="top">Checks whether the measurement point is
              running or not. Useful for when triggers are being used.<br>
              <br>
              Returns 4 bytes, integer. <br>
              1 = Running<br>
              0 = Not running<br>
            </td>
          </tr>
          <tr>
            <td valign="top">Get number of runs<br>
            </td>
            <td valign="top">9<br>
            </td>
            <td valign="top">Measurement point<br>
            </td>
            <td valign="top">N/A<br>
            </td>
            <td valign="top">Reports back the number of completed runs
              performed. This is mainly a sanity check, so that the host
              will know if it has 'missed' a measurement.<br>
              <br>
              Returns 4 bytes, integer = number of runs.<br>
            </td>
          </tr>
          <tr>
            <td valign="top">Clear number of runs<br>
            </td>
            <td valign="top">10<br>
            </td>
            <td valign="top">Measurement point<br>
            </td>
            <td valign="top">N/A<br>
            </td>
            <td valign="top">Resets the number of runs counter to 0.<br>
            </td>
          </tr>
          <tr>
            <td valign="top">Get instantaneous<br>
            </td>
            <td valign="top">11<br>
            </td>
            <td valign="top">Measurement point<br>
            </td>
            <td valign="top">N/A<br>
            </td>
            <td valign="top">This returns the most recent raw ADC values
              for voltage and current. This is useful for graphs.<br>
              <br>
              24 byte result structure.<br>
              <br>
              <meta http-equiv="content-type" content="text/html;
                charset=ISO-8859-1">
              <pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="line" id="LC118" style="box-sizing: border-box; padding-left: 10px;"><span class="k" style="box-sizing: border-box; font-weight: bold;">typedef</span> <span class="k" style="box-sizing: border-box; font-weight: bold;">struct</span> <span class="p" style="box-sizing: border-box;">{</span></div><div class="line" id="LC119" style="box-sizing: border-box; padding-left: 10px;">    <span class="kt" style="box-sizing: border-box; color: rgb(68, 85, 136); font-weight: bold;">unsigned</span> <span class="n" style="box-sizing: border-b
ox; color: rgb(51, 51, 51);">voltage</span><span class="p" style="box-sizing: border-box;">;</span></div><div class="line" id="LC120" style="box-sizing: border-box; padding-left: 10px;">    <span class="kt" style="box-sizing: border-box; color: rgb(68, 85, 136); font-weight: bold;">unsigned</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">current</span><span class="p" style="box-sizing: border-box;">;</span></div><div class="line" id="LC121" style="box-sizing: border-box; padding-left: 10px;">    <span class="kt" style="box-sizing: border-box; color: rgb(68, 85, 136); font-weight: bold;">unsigned</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">average_voltage</span><span class="p" style="box-sizing: border-box;">;</span></div><div class="line" id="LC122" style="box-sizing: border-box; padding-left: 10px;">    <span class="kt" style="box-sizing: border-box; color: rg
b(68, 85, 136); font-weight: bold;">unsigned</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">average_current</span><span class="p" style="box-sizing: border-box;">;</span></div><div class="line" id="LC123" style="box-sizing: border-box; padding-left: 10px;">    <span class="kt" style="box-sizing: border-box; color: rgb(68, 85, 136); font-weight: bold;">uint64_t</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">current_time</span><span class="p" style="box-sizing: border-box;">;</span></div><div class="line" id="LC124" style="box-sizing: border-box; padding-left: 10px;"><span class="p" style="box-sizing: border-box;">}</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">instant_data</span><span class="p" style="box-sizing: border-box;">;</span></div></pre>
              <br>
              The current and voltage are both fairly noisey, so they
              are averaged over the last 32 samples. This provides much
              smoother results.<br>
              <br>
              Have a look at the convertData method in pyenergy.py for
              how to convert these figures to real units.<br>
            </td>
          </tr>
        </tbody>
      </table>
      <br>
      I'm not sure how this would be implemented in .NET, however if
      there are bindings for libusb, then it boils down to some fairly
      easy control transfers. The USB device appears as ID
      0xf539:0xf539.<br>
      <br>
      For example in python:<br>
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
      <pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="line" id="LC72" style="box-sizing: border-box; padding-left: 10px;"><span class="c" style="box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;"># Return whether the measurement point is currently taking</span></div><div class="line" id="LC73" style="box-sizing: border-box; padding-left: 10px;"><span class="c" style="box-sizing: border-box; color: rgb(153, 153, 136); font-style: italic;"># measurements or not</span></div><div class="line" id="LC74" style="box-sizing: border-box; padding-left: 10px;"><span class="k" style="box-sizing: b
order-box; font-weight: bold;">def</span> <span class="nf" style="box-sizing: border-box; color: rgb(153, 0, 0); font-weight: bold;">isRunning</span><span class="p" style="box-sizing: border-box;">(</span><span class="bp" style="box-sizing: border-box; color: rgb(153, 153, 153);">self</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">m_point</span><span class="o" style="box-sizing: border-box; font-weight: bold;">=</span><span class="mi" style="box-sizing: border-box; color: rgb(0, 153, 153);">1</span><span class="p" style="box-sizing: border-box;">):</span></div><div class="line" id="LC75" style="box-sizing: border-box; padding-left: 10px;">    <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">b</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="bp" style="box-sizing: border-box; color: rgb(153, 153, 153);">self</span><
span class="o" style="box-sizing: border-box; font-weight: bold;">.</span><span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">dev</span><span class="o" style="box-sizing: border-box; font-weight: bold;">.</span><span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">ctrl_transfer</span><span class="p" style="box-sizing: border-box;">(</span><span class="mh" style="box-sizing: border-box; color: rgb(0, 153, 153);">0xc1</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: rgb(0, 153, 153);">8</span><span class="p" style="box-sizing: border-box;">,</span> <span class="nb" style="box-sizing: border-box; color: rgb(0, 134, 179);">int</span><span class="p" style="box-sizing: border-box;">(</span><span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">m_point</span><span class="p" style="box-sizing: border-box;">),</span> <span class="mi" style="box-sizing: border-box; color: 
rgb(0, 153, 153);">0</span><span class="p" style="box-sizing: border-box;">,</span> <span class="mi" style="box-sizing: border-box; color: rgb(0, 153, 153);">4</span><span class="p" style="box-sizing: border-box;">)</span></div><div class="line" id="LC76" style="box-sizing: border-box; padding-left: 10px;">
</div><div class="line" id="LC77" style="box-sizing: border-box; padding-left: 10px;">    <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">running</span> <span class="o" style="box-sizing: border-box; font-weight: bold;">=</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">unpack</span><span class="p" style="box-sizing: border-box;">(</span><span class="s" style="box-sizing: border-box; color: rgb(221, 17, 68);">"=L"</span><span class="p" style="box-sizing: border-box;">,</span> <span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">b</span><span class="p" style="box-sizing: border-box;">)</span></div><div class="line" id="LC78" style="box-sizing: border-box; padding-left: 10px;">    <span class="k" style="box-sizing: border-box; font-weight: bold;">return</span> <span class="nb" style="box-sizing: border-box; color: rgb(0, 134, 179);">bool</span><span class="p" style="box-sizing: border-
box;">(</span><span class="n" style="box-sizing: border-box; color: rgb(51, 51, 51);">running</span><span class="p" style="box-sizing: border-box;">[</span><span class="mi" style="box-sizing: border-box; color: rgb(0, 153, 153);">0</span><span class="p" style="box-sizing: border-box;">])</span></div></pre>
      <br>
      Here the code is performing a control transfer on endpoint 0xC1
      (indicating there is data to receive, endpoint 0x41 is used when
      no data needs to be returned). Request type 8, for "is running",
      then the measurement point as wValue. The final parameter
      indicates 4 bytes to be returned. This is converted to a boolean.
      I suspect it would be similar in .NET.<br>
      <br>
      The board should be fairly forgiving about the sequencing of
      requests sent to it, with the exception that Bad Things will
      happen if different ADCs are mapped to the same measurement point
      in request 7.<br>
      <br>
      If anything is unclear, or I've missed out some vital bit of
      information, just shout.<br>
      <br>
      Cheers,<br>
      James<br>
      <br>
      On 06/02/14 09:14, Alex J Lennon wrote:<br>
    </div>
    <blockquote cite="mid:52F35271.3080806@dynamicdevices.co.uk"
      type="cite">
      <pre wrap="">Hi all,

Could anybody point me to a breakdown of the comms. protocol between
the data gatherer and the host PC, if there is such a thing?

I'm interested in putting together a simple .NET application, perhaps
involving
ZedGraph, having one foot on the dark-side as it were.

Many thanks!

Alex Lennon


_______________________________________________
mageec mailing list
<a class="moz-txt-link-abbreviated" href="mailto:mageec@mageec.org">mageec@mageec.org</a>
<a class="moz-txt-link-freetext" href="http://mageec.org/cgi-bin/mailman/listinfo/mageec">http://mageec.org/cgi-bin/mailman/listinfo/mageec</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>