<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>
<blockquote type="cite">Am I right in thinking that the device is
a HID class device, or is it more custom than this? (thinking
about Windows driver support here)<br>
</blockquote>
Unfortunately I didn't think about Windows at all when I designed
the firmware. The device just exposes a vendor specific class. It
also has a bulk in and out endpoint, and an interrupt in endpoint,
all of which are currently unused in the latest firmware (they
were previously used, and I've forgotten to remove them from the
descriptor).<br>
<br>
In the first version of the firmware I streamed back every sample
taken by the ADC to the PC, using a standard serial class, however
I ended up having problems with this (I can't remember exactly
what). The newest version of the firmware dropped the tracing
ability in favour of accumulating onboard and sending back the
final energy figures, which turned out to be much more robust.<br>
<br>
I don't think there is any reason the serial link couldn't be
added back in. There is an example using libopencm3 here:<br>
<a
href="https://github.com/libopencm3/libopencm3-examples/blob/master/examples/stm32/f4/stm32f4-discovery/usb_cdcacm/cdcacm.c">https://github.com/libopencm3/libopencm3-examples/blob/master/examples/stm32/f4/stm32f4-discovery/usb_cdcacm/cdcacm.c</a><br>
<br>
James<br>
<br>
<br>
On 06/02/14 14:59, Alex J Lennon wrote:<br>
</div>
<blockquote cite="mid:52F3A353.6030104@dynamicdevices.co.uk"
type="cite">
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
Hi James,<br>
<br>
That's really useful thanks. I have some background in USB
development so can see where you are coming from with this.<br>
<br>
Am I right in thinking that the device is a HID class device, or
is it more custom than this? (thinking about Windows driver
support here)<br>
<br>
I had assumed that it would be enumerating as a standard serial
class device and you would have a protocol running over the <br>
virtualised serial link, this being easier in terms of driver
support on host OS platforms.<br>
<br>
Was there a reason for not going down the USB serial route? I am
wondering how difficult that would be for me to put in place?<br>
<br>
Thanks!<br>
<br>
Alex<br>
<br>
<div class="moz-cite-prefix">On 06/02/2014 12:46, James Pallister
wrote:<br>
</div>
<blockquote cite="mid:52F3843C.6000305@bristol.ac.uk" type="cite">
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<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 moz-do-not-send="true"
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: bord
er-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; co
lo
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-b
ox
; 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: bord
er-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-sizin
g: 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>&
lt;
span class="o" style="box-sizing: border-box; font-weight: bold;">.<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: borde
r-
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 moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:mageec@mageec.org">mageec@mageec.org</a>
<a moz-do-not-send="true" 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>
</blockquote>
<br>
<div class="moz-signature">-- <br>
<div class="preview">
<p style="font-family: Helvetica, Arial, sans-serif;
font-size: 10px; line-height: 12px;"><a
moz-do-not-send="true"
href="http://www.dynamicdevices.co.uk/" class="clink"><img
src="cid:part5.07050603.06010205@bristol.ac.uk"
alt="Dynamic Devices Ltd" id="sig-logo" border="0"></a></p>
<p style="font-family: Helvetica, Arial, sans-serif;
font-size: 10px; line-height: 12px; color: rgb(153, 153,
153);"><span id="name-input" style="font-weight: bold;"
class="txt">Alex J Lennon</span> <span id="title-sep">/</span>
<span id="title-input" style="color: #999;" class="txt">Director</span><br>
<span id="address-input" style="color: #999;" class="txt">1
Queensway, Liverpool L22 4RA</span> </p>
<p style="font-family: Helvetica, Arial, sans-serif;
font-size: 10px; line-height: 12px;"> <span
id="mobile-input" style="color: #999;" class="txt">mobile:
+44 (0)7956 668178</span> <br>
<br>
</p>
<p style="font-size: 10px; line-height: 12px; font-family:
Helvetica, Arial, sans-serif"> <a moz-do-not-send="true"
id="linkedin-input" class="social"
href="http://www.linkedin.com/in/alexjlennon"><img
src="cid:part7.04010200.08070401@bristol.ac.uk"
alt="Linkedin"></a> <a moz-do-not-send="true"
id="skype-input" class="social"
href="skype:alexjlennon?add"><img
src="cid:part9.03030504.03040703@bristol.ac.uk"
alt="Skype"></a></p>
<p id="disclaimer-input" style="font-family: Helvetica, Arial,
sans-serif; color: rgb(153, 153, 153); font-size: 9px;
line-height: 12px;width: 25%" class="txt">This e-mail
message may contain confidential or legally privileged
information and is intended only for the use of the intended
recipient(s). Any unauthorized disclosure, dissemination,
distribution, copying or the taking of any action in
reliance on the information herein is prohibited. E-mails
are not secure and cannot be guaranteed to be error free as
they can be intercepted, amended, or contain viruses. Anyone
who communicates with us by e-mail is deemed to have
accepted these risks. Company Name is not responsible for
errors or omissions in this message and denies any
responsibility for any damage arising from the use of
e-mail. Any opinion and other statement contained in this
message and any attachment are solely those of the author
and do not necessarily represent those of the company.</p>
</div>
</div>
</blockquote>
<br>
</body>
</html>