Controlling USB devices with Python and libusb @siliconlabs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hmm and welcome to the Desolator wow we were back and I'm almost over my plague sickness and now we're going to be doing some more fun desk and Lady ADA broadcasts I'm Lady ADA I'm the engineer here and this is my desk and with me is mr. lay data and some camera control hello we're broadcasting from my bedroom and now today tonight we're going to be talking about how to control USB devices with Python and Lib USB which is one of my most favoritest things in the world I love it more than cookies and having libya's be is one of like that neatest libraries ever made it basically allows you to do USB level transfers in user space so instead of having to read a kernel driver or like I use Windows programming where you're like inside the operating system in doing a device drivers that way some of having to do all of that nonsense you can actually just talk to the devices sort of like you would with with a serial port or parallel port and I just think that's so peachy so we're going to be doing that tonight and it's you know a love you I would be wondering like why you're not working on a tester actually it is it is for a tester but it's good generic stuff to know anyways and I want to give a shout out to Jen Axelsson this book is one of the best books I've ever read it's so good that after I bought this because I give away my older version to somebody and then over who I picked up another copy this and I realized like I should really have this in the Adafruit shop so we're going to carry this as well this really great book you can pick it up from almost anywhere and it really covers a lot of details of us because a little more complicated than most protocols from the low-level all the way to the high level so ok let's go to the hover hover over I can show off the thing I'm working on so this is the break-up board I'm working on it's a workout for this CP 2104 which is a really nice USB serial converter chip we have to use this in the huzzah feather with nice about this is it's a lot less expensive than the FTDI chips like the FT 2 3 2 RL and 2:30 p.m. and 2:30 1x much much less expensive and it can go much much faster this can go up to 2 megabits per second I use it at 2 program ESP ATT sixes and 921 killable and it's like super snappy and it works great and it also do arbitrary baud rates which is really nice because the PL 23 or 3 doesn't for example and it's small it doesn't require an external crystal dental caramels any external passes it doesn't even require inline a USB resistor so you put a couple capacitors on there you're good to go and so here's the the board and this is the chip in the center I put two LEDs oryx and TX on here I'll talk about those in a bit and then I've got GPIO is on all sides here at the g5 Ziya control signals over here like DC are and and DSR and DC d and ring and RS cts and RTS this is kind of your standard USB to serial 0 to 3 volt Bible compliant but 0 to 3 volt logic really good for all sorts of microcontroller interfacing and I kind of wanted to make something that people could use with the esp8266 we also with a nice alternative to the FTDI chip because again it's a lot less expensive like I'll be able to sell this breakout for about $5 nah that's not possible with a genuine FTDI chip so it's got a micro USB connector and let's just show it off with my Windows computer so you want to go to the windows copy ok so this is the datasheet for this device let's just start up X CTU this is a you know a very basic serial console I just kind of like this because it's a it has a control over the modem pins so oh can we go to a quad view yeah so Claude okay so we've got the board down here with the scooby cam yeah okay so I've got my oscilloscope hooked up and so like let's connect to t XD and you know you you type characters and they appear I can type different characters and you get beautiful serial pins here and then you can also see the red LED flashing and then of course if you connect grab this jumper and I connect rx to TX it's a nice little echo test and now get the red and green LEDs blinking and you just believe me there's data on the other side and of course there's those control pins so like this is RTS and you can toggle RTS with this little button here and you can see if I do the RTS ready to send signal the oscilloscope goes up and down so you've got the signal pins there's also things like CTS and CTS is an input so you're not going to see it on the oscilloscope but if I connect it to sorry to ground which ones the ground pin oh here you go you'll see over here on the computer this little green thing it turns on and off so it just tell you hey the signal that signal got input there's also like DSR all these modem lines all nine pin connecter of them CTS are off okay so you got all these single fans and you're like this is said lovely you know I've got every control paint like again you don't usually use them usually use RX TX maybe RTS and CTS for flow control sometimes DPR is used to reset like arduino the DTR pan is toggled to reset in arduino with the esp8266 there's a little bit of like frankly kind of weird transistor noodling to get the gpio and we set pans to go off and to put it to bootloader mode so you can kind of abuse those pins a little bit which is nice and there's also a couple of other pins available this is just the schematic for it and this shows you over here okay go to copy you just uh just copy thanks okay so you got it you know this is the schematic for it you've got the control pins ring a DTR DSLR egg bar xt x over here and then there's also a couple outputs we set and suspend so you can reset the chip if you want to just kind of cute there's the USB section over here couple capacitors needed just for setting it up it's actually a microcontroller inside right there it's not like a special ROM chip I don't believe that it's probably a microcontroller that psy labs adjusted or something it's a pretty common thing to do and this generic and they've used it for a couple different chips and then there is this GPIO zero one two and three so GPIO these are signal for status and control and GPIO pins let's go to the chapter chapter seven okay so there's four GPIO pins and three of them have like alternate uses so GPA at zero and one I'm actually using them to control LEDs and you set it in the ROM and it's like a complicated weird situation with that and maybe I'll do a future video but you can set these up to toggle the LEDs and so the red and green LEDs I have those are connected to these two GPIO s that are from this chip and it basically blinks them which is I think it's really nice I think if I'm going to have something that's an FTDI comparison it has to be as good as the ftdi friend which has those really lovely receiving transmit Blinky's I think for the extra three cents it's worth it okay so we already you know this already on my schematic this LED open drain toggle pen control I'm not using r45 and r45 and then they kind of never mentions it again but there's like those other two GPIO s right GPIO to oh and GPIO three and let's go and look at my breakout USB 0cp and we can look at this board so okay so yeah I've got the amount of control pins and then I've got the LEDs on GPIO zero and GPA one but in there's two more GPIO so you pay you two and three and these are not being used and so question is well like how do you use them like it there are these pins that you could take advantage of maybe do something with let's say you wanted a separate signal that was not one of these you could use maybe four bit bang controlling something or or doing a manual reset where you don't want to use the RTS or cts lines or whatever it uses an inputs and outputs but there is no part of the USB serial interface that gives you access right the the RI pin and the Rx and TX those are controlled by the CDC that whatever data communications default USB standard right there's like get ring set ring get RTS set artist whatever all of those control lines and like send by received byte but there's no like generic GPO control for those there's no way in the driver for us to do that it just doesn't exist there's no these are like kind of extras that were tacked on okay so how do you control them if you want to well there's a app note from psy labs I hope it's pronounced Scilab so that's how I say it gonna be still labs but I think Scilab sounds better and so they talked about the other virtual comport interface and you know they talk about flow control on how they do it and stuff and then what they actually do is kinda interesting is they get into the low-level hardware description of this device so they say okay here's your descriptor and like none of those new descriptors like just read regen axelsson spoke really it she covers all of this stuff kind of awesome but it basically says you know your vendor ID your Product ID and descriptors and all that good stuff and then you're like okay this is great but what I really want to do is control the GP iOS and then they actually get to you know there's like every single and some of these are like standardized requests but deep down the datasheet page 17 you get to vendor specific so your vendor specific commands that you can send that you can use to control those GPIO s and like you know why am I doing this well mostly because in the tester I want to be able to make sure that those pins are connected so I have to toggle them I have to basically turn them on and off somehow and then detect that those pins moved up and down and there's no way for me to do that unless I have a hook into them so we're going to use these vendor specific commands vendor specific oxf F be a request type request value index length so this is the structure for the request and we'll we'll get back to this in a little bit but we want to do is basically send a raw request to the USB device with the control endpoint this is the endpoint that isn't used for data it's used for like telling it what to do like for example if you want to tell the chip I want to open up the port and rIDOT that's a control request or if you want to tell it to like toggle the pins up and down that's also control request when you want to send data like Rx and TX data that's a data request that's probably in a separate endpoint okay so the only thing is it's a little annoying to use live USB under windows because it already is attached to a kernel driver like the Windows driver so I shouldn't do this on a Raspberry Pi so we disconnect and we're going to one raspberry pi because Linux is just like super chill like as long as you want as route like your user your root user but you don't have to be in kernel space so it's lovely sometimes you do have to detach the kernel driver but it turns out in this case you don't so you have to learn how to send these requests okay so how do you do that well you're going to that with Libya with me and the USB is a cross-platform user library to access USB devices this is a platform and it's important to multiple programming languages I tend to use Python because it's the fastest easiest way to sort of script up these testers and I like it but you can also do it and see and probably like you know Ruby and like Perl they have hooks for everything it's in C and then they just wrapper it for whatever your scripting language you want and it's pretty straightforward basically you just tell the live USB core hey give me like a handle for this device and then you can just send it requests and I have a longer tutorial it talks about how to fuzz USB devices with Livius b4 we do this for the connect hack so it's kind of fun except I got to refer to my old my old tutorial but once in a while yeah every year too I end up having to do this because I have some chip and I have to get into some weird mode or want to automate something and Lib USB is just an awesome way to do that so let's go to our Raspberry Pi okay so I let my Raspberry Pi and then I guess I can we can use like this section of the screen and then if you want to do the thing if you want to zoom in huh that's great lovely okay so let's start up a new Python script no sorry hello now we're going to run it yet I got make it okay so uh user actually I'll just run Python on board I don't know what Python lives and I'm going to crib but you guys can't see my crib notes because I don't remember the entire API up top my head but you want to do stuff like okay so start with import time because I want to do some delay later and then import USB core and I'm also an import USB util to do this I believe I did a pip sudo pic install hi you would be over what yeah it's already there but hi USB is what you want to use basically if you're writing code in Python you already know you want to like get pip installed or whatever and then install PI USB Google for that and then you want to install it you're good to go okay so you installed PI USB and we're back okay so that imports your work so next step you need to know the V ID and the PID of the device that you want to grab the each device has unique vendor identifiers and a unique product identifier you can also do it by hubs and ports and stuff I think but the easiest way to do it to be honest is to tell Lib USB hey to grab the item by v ID and PID I mean you can only have one of those devices plugged in because if you have like two CP 2104 they won't know which one but like we're not doing that that's it for another time so one LS USB just a lovely utility and it will tell you all of the items that you have plugged in for example you know I have a Wi-Fi adapter and this Wi-Fi adapter is ID obd a colon seven one seven eight one seven six that's v ID PID but that's actually not the device we want instead we want this device which is the CP 2100 X UART bridge also known as a my AVR smart USB light it still is used in some product so they give you a hint is it what it might be but it's up so that's in hex great go back to our device and then we'll put in V ID okay don't forget to wax because it's next you want to tell the USB device Hey the Libya speak or hey get me that VI D PID let find this device for me wow that is so easy all you have to do is say USB cor find amazing and then you just tell it the vendor ID and you tell it the product ID amazing isn't it so and if it finds it you know it will return true or to return up by proper device but if not print could not find CP 2104 so sad and then you want to exit otherwise you should print yeehaw found a CP 2104 device and then we still want to exit but we'll exit in nice way okay great so this is just like our most basic code so let's save it and then run so it says I found it oh hold on let me actually reach the end let me pull this up okay so it does it found it so now I'm going to do a trick where I unplug the device where you want to make sure and I run it it said it couldn't find it okay so good so if it's plugged in it can find it if it's not plugged in it can't find it I am apparently inside the code okay so we're back to our program okay so we found the device excellent so here's actually what we get to do want so what we want to do is connect to the control endpoint and again general Tsin covers all the stuff about endpoints and interfaces but we're just going to a feud we need to know it no we don't sorry the control you can send the control transfer without knowing the actual endpoint and interface you just you send it just a you just say hey I want to send it control transfer so to send a control transfer you run dev so that's the device the device that we have grabbed dev control transfer and then you put in the request type the be rec the w val the w index and then data and if you google for loops code little USB let's look at the API it'll probably somewhere in here it'll be documented get pork can't handle in it okay maybe I'll search for the PI USB control transfer okay so here it is in Docs okay so yeah this basically kind of was doing the same thing but you tool transfer do they have documentation yeah it's here somewhere all right I don't know where it is but basically some more documents that order of arguments but you'll have to tell can we just zoom into this small text yeah to transcode okay so so you need to do a control transfer with the request type the request itself the value of the request the index of the request and the data and the B and the W just tell you that it's a byte in a word and this is what we're going to go back to the date okay so in that datasheet I'll shrink this down so it fits we want to use right latch to toggle that pan so we want BM request type and then this is like 0 1 0 0 1 so this is a export one the B request is oxf F the W val is 371 and the right latch value is the index W length of 0 and the data is none so you actually are cheating by putting the value of what you want to write to that GPIO in W index okay so we'll go back here and then we'll just say rec type equals o x4 1 because that's the request type if you remember from from right here Oh from this is the request type from me there you go this is the request type over here and then for B requests it's going to be that vendor specific which is F F and then W value is Oh X 3781 and then W int X is the put stuff here that's going to be what we actually change around and then data is actually empty the empty set we're not actually going to send anything ok so next up we figure out like what is that how do we actually write the data so they say here the the right match the right latch value that is supplied in W this is following bits 0 through 7 mask of the latch state 2 right where bit 0 is GPI 0 fit 100 GPA 1 up to GPIO n and then the higher byte is the actual state so there's a mask of what bytes you want to write and then what the value is going to be so I want to toggle like to have W index be Oh X so it's going to be 1 shifted over to x and then or that with one shifting over to x and then shift it over eight times so that the high byte has one has basically well I'll just say X 4 I'll be a bit easier - Oh X 4 for GPIO - and then so just for for so do ko2 is Oh X 0 4 ok ok so we write that index value hmm and then what I want to do afterwards is sleep for like the tenth of a second and then I will make the W index equal so the mask is still going to be oxo for but then I'm not going to write to the high byte I'm going to have that V 0 so it's basically turns the this request turns the gpio on and then this request turns it off that's pretty much it and then when I exit okay so let's try this and okay so let's go to quad view okay so I'm going to scope out GPIO 2 and right now it's high but hopefully we'll be able to get it to toggle so let's watch for the data line over there did it work Oh it said W Val no well hold on w val is actually called w value fix that okay so if you saw that very briefly every time i run it there's a bit of a toggle very fast so I want to actually make this happen mo faster so let's let's just do this on repeat while true and then up another time sleep here okay so now it's going to just repeat forever until I hit control-c this is like exactly okay so you can see we've got our lovely little GPIO toggling that's nice and then if we look at the other pins like GPO three it's not toggling so we're only toggling GPIO two by setting that mask and then you know if we want to toggle GPIO three as well let's have them so they alternate so we'll mask off will also mask off Oh x8 so GPIO 3 which is the 4th bit and sorry that's ok this is that that's the high byte oxo 8 if you're smart like do defines and stuff don't don't have all this bit stuff on your own and then o X o 8 8 so this will alternate it'll it'll have both gpo's on hold on to get my bit mask there you go it'll have both GPIO active but I will alternate which one gets turned on ok so we're back we've got this and then let me get my other scoopy probe always please exactly in the wrong location and let's go about io 3 and you can see that yeah we have them alternating yeah isn't it lovely so now it's like okay we got we got the GPIO twiddling it's very easy I mean once you know what to do so in the control transfer do exactly the datasheet says how hard could be just follow the same sheet so if I'm going to make a spaceship I'm going to call it just read the datasheet it's figuring out what long the day you should is the tough part you know in the in the movie contact they just sent a data sheet and and that's what we had to bill they had to build that machine yeah book had similar plot but a lot of people probably saw the movie but yeah just sent the datasheet she sent a data sheet over her satellite it's like the you know it's like that story it's like that it's like the fellow who you had to fix the machine and he took a hammer and he and he hit it in a certain spot somebody was like okay oh that was great you fix it too easy like that it'll be ten thousand dollars and the factory owner said well how could it cost ten thousand dollar and all he did was like hit the machine in a little spot the fella said yes I'm charging you five cents for hitting the Machine and nine thousand nine hundred ninety five dollars for knowing what hitting it out hard yeah I think that that's a retelling of a picasso quote i heard which is I think it's story's been told yeah like why you know someone someone asked Picasso for a quick drawing and he said that'll be like you know ten thousand dollars and it's like well they'd only took you a second and like well it took me like fifty years to be Picasso though he was camera jerk okay so what I did that I uncounted the time sleep because actually I'm curious like how fast can I toggle the pins because if you want to use this for let's say you want to use this for I don't know you're like I want to implement swd over these two GPIO pins because you're insane um how fast could you toggle these pins okay so now we're running and it's pretty fast let's zoom in and see how fast it is so pretty much exactly a kilohertz which is kind of creepy I wonder if there's something that is delaying a millisecond for transaction or something oh you know what that's probably there's probably a millisecond per transaction so it can't toggle faster than that well what a kilohertz isn't what isn't too bad I mean if you wanted to two-bit bang something you know it's fast enough to like PWM sorry - ow - you know control something over SPI or or if you want to turn on/off the LED very very fast you could you could do that as well a poly fake some the webbing if you really needed to you have to be wanting these or cross constantly but looks like they pretty much just run so that's how you do raw data control transfers so USB devices so now that I have gotten those GPIO is working so I've been able to test every GPIO I of course tested the arcs and TX that was easy I got the 5 volt 3 volt pins are easy to test the reset pen is easy to test the suspend a nun suspend pins I figured out how to test those by unbinding the device driver in Linux and the other control lines are easy so that the only thing that I had to do is figure out how to test these pins so what's really nice is what I'll probably do is I can connect these two to these two GPIOs to Raspberry Pi GPIO pins and then I'll toggle them and then read the pin on the other side that way I'll know that those pins came through and we're connected so that's a good way to do a full functional test of every exposed pin and when I do the write-up for this product I'll also have this little bit of code to be like hey for people who want to twiddle these GPIO pins is how you do it and that's what I have to show tonight ok ok so there any open mic is in chat it says uh music and definitely nice no Micah Micah yeah so USB has a 1 millisecond frame place yeah I just think I've seen like I said why did miss it oh right because it's the total transaction see this is why you should read I should be jealous Wilson's agent because you should read Genesis book USB complete then you would remember about how USB has a one millisecond for frame transfer time slice whatever I'm sure I'll I'm sure it's in like Church on page two it's like dear l'amour did you remember that every millisecond there's a USB transfer anyways alright folks like the way we're doing the transparency with the code thing I know I'm in the code yeah I think it's hard to show code live in a person so this might be asleep so yeah I'm kind of over it is in here talks about the gabay so there's a transit though what's neat about USB there's a little these little pulses that happen every second millisecond so if you're interested in more did y tutorial in my earlier life and it's a learn so but props to psy labs are actually putting that documentation up but the connect hack that we did is has a little bit about this as well and also goes into more detail about installing pi USB and then like how to do the control transfers it's it's all in here as well so I kind of like you know read read this remember like how to do this stuff but basically you know yeah find the device and then you can set configuration if you need to and then you can do control transfers and and have fun sending in transmitting data between you and your connect so basically what we did is we got the connect to we figured out the by just guessing we figured out which was the accelerometer and which one was the motor control so the motor is controlled to a control transfer as well okay how about you enjoyed this answer this question and then we're going to get out of here yeah alright someone wants to know they're wiring up a 5 volt relay and they're out of diodes can they just use an LED instead no because the LED has a limit the LED only has a certain amount of current that it would like to have code through it and that limit is probably 30 milliamps and so if your relay it tries to pulse 30 milliamp look at when we know when it releases it if it tries to put born 30 milliamps through its it might blow up your LED I mean like if it's a very very very small relay you might be able to get away with it but I really wouldn't I would just just get a diode from somewhere you can even use a Zener diode really like if if the voltage is higher than the voltage you're using but I would definitely use them oh use a diode ok well that's it for tonight folks we'll see all of you on some shows coming up we have a live from Tony D's desk we've got some desk of Lady ADA some ask an engineer some show and tell and check out our our Twitter because Twitter comm slash Adafruit we're always retweeting some great videos Michael was in chat tonight yeah so looks like isochronous packets for SuperSpeed you can get as low as 48 microseconds per transfer but it'll take one millisecond for most but yeah this is I'm actually you know it's been so long since I read I read this book in college but I should read it again because there's usb3 and I don't know that much about usb3 so I'd like to read a little bit more about it also you know USB on-the-go was invented in the mean time as well but um yeah I just love this book and I don't know who I left my book to but I hope they enjoyed it because uh I couldn't find it when I wanted to look this stuff up ok ok so by the periscope by everybody by the Facebook log on with your USB control trance twitch and YouTube All Right see you later ah
Info
Channel: Adafruit Industries
Views: 119,956
Rating: undefined out of 5
Keywords:
Id: xH_y05pIDTo
Channel Id: undefined
Length: 37min 18sec (2238 seconds)
Published: Fri Apr 08 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.