<?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/8395" />

	<title>Objective Development Forums</title>
	
	<link href="https://forums.obdev.at/index.php" />
	<updated>2013-05-23T15:21:14+02:00</updated>

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

		<entry>
		<author><name><![CDATA[Talib]]></name></author>
		<updated>2013-05-23T15:21:14+02:00</updated>

		<published>2013-05-23T15:21:14+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25312#p25312</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25312#p25312"/>
		<title type="html"><![CDATA[Re: Noob Confusion....]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25312#p25312"><![CDATA[
Hi<br /><br />I followed this tutorial: <!-- m --><a class="postlink" href="http://codeandlife.com/2012/01/22/avr-attiny-usb-tutorial-part-1/">http://codeandlife.com/2012/01/22/avr-a ... al-part-1/</a><!-- m --><br />It is not C# but it gave me good start at understanding how the basic USB works (Control Messages). - See my (Roelf) comment on the third part of this tutorial if you do not understand the direction thing<br /><br />I have started writing my own custom c# class for my own use. I will share it, but keep in mind I do not code for a living and all judgments on my coding style and methods should be educational and not just criticism.<br /><br />USBClass.cs<br /><div class="codebox"><p>Code: </p><pre><code>using System;<br />using System.Collections.Generic;<br />using LibUsbDotNet;<br />using LibUsbDotNet.Info;<br />using LibUsbDotNet.Main;<br /><br />namespace USBTest<br />{<br />    /// &lt;summary&gt;<br />    /// The general class to obtain the TinyCon devices.<br />    /// &lt;/summary&gt;<br />    static class TinyConUSBClass<br />    {<br />        const int VENDORID = 0x16C0;<br />        const int PRODUCTID = 0x05DC;<br /><br />        public static List&lt;TinyConDeviceClass&gt; TinyConDevices = new List&lt;TinyConDeviceClass&gt;();<br /><br />        /// &lt;summary&gt;<br />        /// Gets all the connected TinyCon devices and store them in TinyConDevices<br />        /// &lt;/summary&gt;<br />        /// &lt;returns&gt;void&lt;/returns&gt;<br />        public static void GetUSBDevices()<br />        {<br />            //Find devices matching the vendor and product ID<br />            UsbDeviceFinder USBFinder = new UsbDeviceFinder(VENDORID, PRODUCTID);<br />            //Using the registry create a list of those devices<br />            <br />            UsbRegDeviceList USBRegistryDevices = UsbDevice.AllDevices.FindAll(USBFinder);<br /><br />            foreach (UsbRegistry USBReg in USBRegistryDevices)<br />            {<br />               TinyConDevices.Add(new TinyConDeviceClass(USBReg));<br />            }<br />        }<br /><br />        /// &lt;summary&gt;<br />        /// Closes all the TinyCon devices and exits the USB driver<br />        /// &lt;/summary&gt;<br />        public static void ExitUSB()<br />        {<br />            foreach (TinyConDeviceClass TinyCon in TinyConDevices)<br />                TinyCon.Close();<br /><br />            UsbDevice.Exit();<br />        }<br />    }<br /><br />    /// &lt;summary&gt;<br />    /// Using my own USB Request type as libusbdotnet's version seems to be incomplete<br />    /// &lt;/summary&gt;<br />    public enum USBRequestType{<br />        RECIPIENT_DEVICE = 0,<br />        RECIPIENT_INTERFACE = 1,<br />        RECIPIENT_ENDPOINT = 2,<br />        RECIPIENT_OTHER = 3,<br />        TYPE_STANDARD = 0,<br />        TYPE_CLASS = 32,<br />        TYPE_VENDOR = 64,<br />        TRANSFER_DIRECTION_OUT = 0,<br />        TRANSFER_DIRECTION_IN = 128<br />    }<br /><br />    /// &lt;summary&gt;<br />    /// The class for individual TinyCon devices.<br />    /// &lt;/summary&gt;<br />    class TinyConDeviceClass<br />    {<br />        private UsbRegistry _USBRegistry;<br />        private UsbDevice _USBDevice = null;<br />        private char&#91;&#93; _USBBuffer = new char&#91;64&#93;;<br /><br />        private enum TinyConRequest<br />        {<br />            LED_STATE = 1<br />        }<br /><br />        /// &lt;summary&gt;<br />        /// Is the TinyCon device open. (Read-only)<br />        /// &lt;/summary&gt;<br />        public bool IsOpen<br />        {<br />            get<br />            {<br />                if (_USBDevice == null)<br />                    return false;<br />                return _USBDevice.IsOpen;<br />            }<br />        }<br /><br />        /// &lt;summary&gt;<br />        /// Read TinyCon serial number. (Read-only)<br />        /// &lt;/summary&gt;<br />        public string GetSerialNumber<br />        {<br />            get<br />            {<br />                if (_USBDevice == null)<br />                    return &quot;Device not open.&quot;;<br />                return _USBDevice.Info.SerialString;<br />            }<br />        }<br /><br />        public TinyConDeviceClass(UsbRegistry USBRegistry)<br />        {<br />            _USBRegistry = USBRegistry;<br />        }<br /><br />        /// &lt;summary&gt;<br />        /// Open the TinyCon device.<br />        /// &lt;/summary&gt;<br />        /// &lt;returns&gt;bool&lt;/returns&gt;<br />        public bool Open()<br />        {<br />            if (!_USBRegistry.Open(out _USBDevice))<br />                return false;<br /><br />            if (_USBDevice == null)<br />                return false;<br /><br />            IUsbDevice wholeUsbDevice = _USBDevice as IUsbDevice;<br />            if (!ReferenceEquals(wholeUsbDevice, null))<br />            {<br />                // This is a &quot;whole&quot; USB device. Before it can be used, <br />                // the desired configuration and interface must be selected.<br /><br />                // Select config #1<br />                wholeUsbDevice.SetConfiguration(1);<br /><br />                // Claim interface #0.<br />                wholeUsbDevice.ClaimInterface(0);<br />            }<br /><br />            return true;<br />        }<br /><br />        /// &lt;summary&gt;<br />        /// Set the LED1 State.<br />        /// &lt;/summary&gt;<br />        /// &lt;param name=&quot;State&quot;&gt;bool&lt;/param&gt;<br />        /// &lt;returns&gt;bool&lt;/returns&gt;<br />        public bool LEDState(bool State)<br />        {<br />            //object and length can be null/0 if no data packet is involved.<br />            UsbSetupPacket SetupPacket = new UsbSetupPacket((byte)(USBRequestType.RECIPIENT_DEVICE | USBRequestType.TYPE_VENDOR), (byte)TinyConRequest.LED_STATE, (short)(State ? 1 : 0), 0, 0);<br />            int LengthTransfered = 0;<br /><br />            if (_USBDevice == null)<br />                return false;<br /><br />            return _USBDevice.ControlTransfer(ref SetupPacket, null, 0, out LengthTransfered);<br />        }<br /><br />        /// &lt;summary&gt;<br />        /// Closes the TinyCon device.<br />        /// &lt;/summary&gt;<br />        public void Close()<br />        {<br />            if (_USBDevice != null)<br />            {<br />                _USBDevice.Close();<br />                _USBDevice = null;<br />            }<br />        }<br />    }<br />}<br /><br /></code></pre></div><br /><br />Please note the class is not complete, but it will give you an idea of what to to.<br /><br />As for implementation:<br /><br /><div class="codebox"><p>Code: </p><pre><code>TinyConUSBClass.GetUSBDevices();<br /><br />if (TinyConUSBClass.TinyConDevices.Count &gt; 0)<br />{<br />    TinyConUSBClass.TinyConDevices&#91;0&#93;.Open();<br />     Console.WriteLine(&quot;Serial Number: &quot; + TinyConUSBClass.TinyConDevices&#91;0&#93;.GetSerialNumber);<br />     TinyConUSBClass.TinyConDevices&#91;0&#93;.LEDState(true);<br />}<br />//No need to call  TinyConUSBClass.TinyConDevices&#91;0&#93;.Close(); as ExitUSB closes all devices<br />TinyConUSBClass.ExitUSB();<br /></code></pre></div><br /><br />As you will note the class is set up so that if you have multiple same devices plugged into one host, they are easily enumerated.<br /><br />PS Thinking of making the TinyConUSBClass not static and singleton, then I could implement a destructor that would call USBExit() automatically.<p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=4488">Talib</a> — Thu May 23, 2013 3:21 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[Anonymous]]></name></author>
		<updated>2013-04-13T19:01:27+02:00</updated>

		<published>2013-04-13T19:01:27+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25131#p25131</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25131#p25131"/>
		<title type="html"><![CDATA[Re: Noob Confusion....]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25131#p25131"><![CDATA[
Use the basic linux subdirectory. There is no GTK in it, it is pure libusb code.<br />(Therefor it is a console only application, but it focuses on the essential.)<br />If you use Windows install a GNU compatible compiler, like MinGW.<br /><br />Best regards<p>Statistics: Posted by Guest — Sat Apr 13, 2013 7:01 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[ebsy]]></name></author>
		<updated>2013-04-13T18:34:03+02:00</updated>

		<published>2013-04-13T18:34:03+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25130#p25130</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25130#p25130"/>
		<title type="html"><![CDATA[Re: Noob Confusion....]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25130#p25130"><![CDATA[
Thanks for that.<br /><br />Unfortunately I'm not sure if this particular approach is going to help.<br />From what I see, your avenue is requiring me to install and us GTK ?<br /><br />Problem is that there are already so many compenents involved, v_usb, linusb.net, c, c#, and serval IDEs (ATmelStudio, VS) .... and GTK would be yet another layer adding to the confusion ?<br />(assumed from an initial look)<br /><br />Having read the above back to myself, I may even now step further back and try and work out the c side of things, cutting out the c#.<br />1 less thing to clutter the horizon.<br /><br />Of course, I'm open to being pursuaded ....<p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=19668">ebsy</a> — Sat Apr 13, 2013 6:34 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[Anonymous]]></name></author>
		<updated>2013-04-13T18:06:53+02:00</updated>

		<published>2013-04-13T18:06:53+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25129#p25129</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25129#p25129"/>
		<title type="html"><![CDATA[Re: Noob Confusion....]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25129#p25129"><![CDATA[
Hi.<br />Maybe you want to look into the VUSB template/example of tinyUSBboard.<br />There you will be provided with firmware and compatible host-side applications (linux, windows, both).<br />If someone wants to have a GUI template for/with GDK, he/she can also use this template.<br /><br /><!-- m --><a class="postlink" href="http://matrixstorm.com/avr/tinyusbboard/#examples">http://matrixstorm.com/avr/tinyusbboard/#examples</a><!-- m --> <br /><br />(&quot;VUSB skeleton/example with corresponding linux, windows (delphi) and platform independent GTK libusb program&quot;)<br /><br />Have fun!<p>Statistics: Posted by Guest — Sat Apr 13, 2013 6:06 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[ebsy]]></name></author>
		<updated>2013-04-13T16:59:45+02:00</updated>

		<published>2013-04-13T16:59:45+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25127#p25127</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25127#p25127"/>
		<title type="html"><![CDATA[Noob Confusion....]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=8395&amp;p=25127#p25127"><![CDATA[
So let me begin by confessing, that I seem to be waaaay out of my depth !<br />I moved from Arduino to AVR, and the whole USB business has left me spinning with information overload and far too many new doors to cope with.<br /><br />Still, I am hoping that if I can get a simple example up and running, it'll be a good basis to start some proper learning.<br /><br />Here's where I am at....<br /><br />I started with this article for v-usb : <!-- m --><a class="postlink" href="http://codeandlife.com/2012/01/22/avr-attiny-usb-tutorial-part-1/">http://codeandlife.com/2012/01/22/avr-a ... al-part-1/</a><!-- m --><br />and was doing well setting up an ATTiny45.<br />Included in the code I programmed it with is :<br /><div class="codebox"><p>Code: </p><pre><code>USB_PUBLIC uchar usbFunctionSetup(uchar data&#91;8&#93;) {<br />   usbRequest_t *rq = (void *)data; // cast data to correct type<br />   switch(rq-&gt;bRequest) { // custom command is in the bRequest field<br />      case USB_LED_ON:<br />      PORTB |= (1&lt;&lt;3);         // turn LED on (ATTiny45 PB3)<br />      return 0;<br />      case USB_LED_OFF:<br />      PORTB &amp;= ~(1&lt;&lt;3);                    // turn LED off (ATTiny45 PB3)<br />      return 0;<br />   }<br />   return 0; // should not get here<br />}<br /></code></pre></div><br /><br /><br />My problems start when it comes to the host-side coding.<br />The example on codeandlife works with C, and I am not too familar with that.<br />Hence after some googling, I found libusbdotnet, and thought I'd be more comofrtable working in c# which I have a 'little' experience of.<br /><br />I've installed the libusbdotnet downloads and examples.<br />What I cannot understand, is how/when the above function (usbFunctionSetup) gets triggered by my c# code.<br />(my LED is not lighting up)<br />I can see that in the AVR code,  usbFunctionSetup is called by  usbProcessRx, when certain conditions are met ... but I really dont' understand all the code as yet.<br /><br />I thought the LED would light even just on the Show.Info example, but I have tried a few other examples as well.<br />Like I said, I am now rather lost, dazed and confused as to what is happening on the host &amp; AVR side sof things and how they all tie together! <br /><br />I could give up and admit I am in too deep .... but I have to get USB working in my ATTiny's.<br />Hope someone will take pity and maybe point me in the right direction ?<br /><br />Eternal thanks to that person !<br /><br /><div class="codebox"><p>Code: </p><pre><code>static inline void usbProcessRx(uchar *data, uchar len)<br />{<br />usbRequest_t    *rq = (void *)data;<br /><br />/* usbRxToken can be:<br /> * 0x2d 00101101 (USBPID_SETUP for setup data)<br /> * 0xe1 11100001 (USBPID_OUT: data phase of setup transfer)<br /> * 0...0x0f for OUT on endpoint X<br /> */<br />    DBG2(0x10 + (usbRxToken &amp; 0xf), data, len + 2); /* SETUP=1d, SETUP-DATA=11, OUTx=1x */<br />    USB_RX_USER_HOOK(data, len)<br />#if USB_CFG_IMPLEMENT_FN_WRITEOUT<br />    if(usbRxToken &lt; 0x10){  /* OUT to endpoint != 0: endpoint number in usbRxToken */<br />        usbFunctionWriteOut(data, len);<br />        return;<br />    }<br />#endif<br />    if(usbRxToken == (uchar)USBPID_SETUP){<br />        if(len != 8)    /* Setup size must be always 8 bytes. Ignore otherwise. */<br />            return;<br />        usbMsgLen_t replyLen;<br />        usbTxBuf&#91;0&#93; = USBPID_DATA0;         /* initialize data toggling */<br />        usbTxLen = USBPID_NAK;              /* abort pending transmit */<br />        usbMsgFlags = 0;<br />        uchar type = rq-&gt;bmRequestType &amp; USBRQ_TYPE_MASK;<br />        if(type != USBRQ_TYPE_STANDARD){    /* standard requests are handled by driver */<br />            replyLen = usbFunctionSetup(data);<br />        }else{<br />            replyLen = usbDriverSetup(rq);<br />        }<br /></code></pre></div><p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=19668">ebsy</a> — Sat Apr 13, 2013 4:59 pm</p><hr />
]]></content>
	</entry>
	</feed>
