Shop OBEX P1 Docs P2 Docs Learn Events
USB toys to play with — Parallax Forums

USB toys to play with

BradCBradC Posts: 2,601
edited 2013-10-21 06:56 in Propeller 1
Ok all,

I've got these into a shape that I'm not incredibly embarrassed to show them to the world. (Some parts are still pretty icky)
You'll probably figure it out yourselves, but the files are

USB_lowlevel - the media driver common to all my USB stuff
USB_california - the "California Dreaming" demo beaten into decent shape. It will type the song any time a keyboard led changes state (caps/num/scroll)
USB_Serial - the FullDuplexSerial compatible serial object
USB_Demo_main - very simple loop to loopback serial data for testing.
avrcdc.inf - the driver description file for windows. Lifted from the AVR-CDC project (I've also lifted his VID/PID so as to make it all fit together easily)

The USB_Serial has passed repeated testing on linux, macos and windows with over 20MB each test (takes a little over an hour)..
[noparse][[/noparse]edit] No data corruption, my testing methodology was flawed and I was losing the last 8 bytes due to a premature pipe flush. [noparse][[/noparse]/edit]

The USB_Serial object is not USB compliant (uses bulk endpoints on a low-speed device) but it works on every windows box I've tried it on.

None of the objects are really USB compliant as they take too long to respond to the host. They do respond within the limit of the specification though and therefore should function on any host
you plug them into.

Have at it.

Brad

Edit: If you install the driver for usb-serial on Windows, you will need to change the VID or PID of USB_california or else windows will stupidly think it's a serial port and not a keyboard.
This is not a problem on sane operating systems.
having said that, I should have changed the vpd/pid anyway.

Post Edited (BradC) : 9/16/2007 12:53:29 PM GMT
«1345

Comments

  • BradCBradC Posts: 2,601
    edited 2007-09-16 07:29
    brad@bklaptop2:/tracks$ stty -F /dev/ttyACM0 raw ; cat /dev/ttyACM0 & sleep 1 ; killall cat ; rm test.1 test.2 ; dd if=/dev/urandom of=test.1 bs=120M count=1 ; sync ; cat /dev/ttyACM0 | pipebench -b 128 > test.2 & sleep 1 ; sleep 1 ; cat test.1 > /dev/ttyACM0 ; sleep 10 ; killall cat ; sleep 5 ; sync ; echo; echo ; echo ; md5sum test.1 test.2
     7392
    rm: cannot remove `test.1': No such file or directory
    rm: cannot remove `test.2': No such file or directory
    +  Terminated              cat /dev/ttyACM0
    1+0 records in
    1+0 records out
    125829120 bytes (126 MB) copied, 64.4303 seconds, 2.0 MB/s
     7645
    Summary:
    Piped  120.00 MB in 04h55m36.98s:    6.92 kB/second
    +  Done                    cat /dev/ttyACM0 | pipebench -b 128 >test.2
    
    
    
    cef991981077266e84300515e6557294  test.1
    cef991981077266e84300515e6557294  test.2
    
    



    So it passed a 120MB loopback test with zero data corruption/loss.

    I've also tried a hack where we bypass all the fifo/queues and just re-assemble the incoming packets and loop them straight back out again in the application cog.
    Maximum sustained transfer rate of 7.6KB/s full-duplex. I "assume" you should get nearly double that in one direction.

    Brad
  • BaggersBaggers Posts: 3,019
    edited 2007-09-16 13:18
    Cheers BradC,
    Not had chance to play yet, but once I get some spare time, I will, as this is interesting stuff.
    Well done matey.

    Baggers.
  • StenSten Posts: 1
    edited 2007-09-16 13:27
    Hello Brad

    Do you think it is possible for you to create a low-speed software USB host using a propeller?

    It would be nice to be able to use generic usb mice/keyboards on a prop... smile.gif

    Nik
  • BradCBradC Posts: 2,601
    edited 2007-09-16 13:38
    I've been thinking about that Sten, in fact I just had my head in the spec looking at the boot protocol requirements for keyboard/mouse.
    It would not be easy from a protocol handler perspective and its way down my list of things to try.. but it is something I've been giving some thought to.
    It would certainly be a single USB object per device though, hub enumeration is _way_ out.
  • hippyhippy Posts: 1,981
    edited 2007-09-16 19:58
    I'm suffering some woes ( understatement of the day ! ) trying to get the drivers installed.

    According to www.recursion.jp/avrcdc : "No dedicated driver necessary.
    CDC loads Windows built-in usbser.sys"

    Unfortunately, it's not taht simple on my system; Windows XP SP2.

    It recognises the device is connected, launches the New Hardware Wizard, I select the
    directory for avrcdc.inf, tell it to continue with the unsigned driver, off it goes to
    update files, "usbser.sys to F:\Windows\System32\drivers" but then fails with "An error
    occured during the installation of the device. The system cannot find the file specified".

    Is it really too much to ask that Bill G could have his software name "the file specified" ?

    I don't think it's usbser.sys because that's in *\drivers, and when I copy it to
    the avrcdc.inf directory, rename it and update avrcdc.inf that renamed
    .sys file is copied over to *\drivers ... then the 'file not found" report
    is given.

    Anyone got any ideas on how to move forward ?
  • BradCBradC Posts: 2,601
    edited 2007-09-16 20:12
    Nah.. I had *major* with a capital "M" problems .. like I never even got it to work on Win2k.. so I installed XP-SP2 in a spare partition and that *did* work..

    All I can say is it "just works" under MacOS and Linux (with a hack)..

    I hate windows.. no ..really..

    Does the California Dreamin' demo work for you? I wonder if you run that first does that bugger the driver installation for the serial port as they both use the same VID/PID pair.. (yes, I'm a twit)

    Sorry for the hassle.. I don't actually _do_ windows, though I "obtained" a copy to test it on a machine here..
  • BradCBradC Posts: 2,601
    edited 2007-09-16 20:13
    You *could* try incrementing the PID in both the INF file and the .spin file and see if it works from scratch...

    [noparse][[/noparse]edit] After another attempt, I just got it to work under Win2k with the avrcdc.inf file.
    Know that does not help you though hippy. Bloody flaky OS. I tried that for a day and it never worked. 4 days later and it works 1st go.
    The difference was the first time I guess I tried to tell it to use a device then selected "have disk". This time I just put the .inf the the root
    and told it to add that to its search path. Different problem than you are having with the file not found error though I guess..
    [noparse][[/noparse]\edit]

    Post Edited (BradC) : 9/16/2007 8:54:51 PM GMT
  • hippyhippy Posts: 1,981
    edited 2007-09-16 21:16
    BradC said...
    Sorry for the hassle.

    No worries; you've written an astounding bit of code, and this is
    fluff which 'the other side' needs. That it gets as far as it does is
    amazing enough.

    No joy on Windows 98 SE either, but I wasn't expecting any miracles
    there. Can get it indicated as 'working okay' under Device Manager
    as a Composite USB Device ( COM & LPT ) or a Lucent Corp USB
    Modem ( that uses a CDC-mode driver ) but cannot get to assign a
    COM port :-(

    USBVIEW.EXE shows your part of the universe working as would be
    expected ( as best I can tell ).

    I'll try the keyboard USB and twiddle with the PID's to see if I can
    get any further. I'm afraid I'm no USB expert at all so I can only
    'poke 'n' hope' and see if something springs to life.

    At least I've confirmed the hardware works and the pinouts for
    the USB connector smile.gif
  • BradCBradC Posts: 2,601
    edited 2007-09-16 21:32
    Win2k has exposed a weird race condition with reset handling where the lowlevel and app code would end up doing something odd..
    Never saw it under XP, linux or MacOS though..

    You can ensure it does not happen by rebooting the prop before you plug it in.. I have to plug/unplug the prop 3 or 4 times under 2k to get it to latch up..
    Chasing it down now..
  • BradCBradC Posts: 2,601
    edited 2007-09-16 22:52
    Only in windows.. sheesh....

    Fixes the lockup on unplug/replug under windows. I never saw it under XP, but it would have done it there.

    The rx routine was not seeing EOP if you unplugged it in the middle of a packet. Linux and MacOS only poll the device while it's open. Windows hammers it from the second it enumerates.
    So in Windows unplugging the device had a very high chance of unplugging mid packet. The receive routine would just keep on going, thinking it was getting lots of continuous data and
    with the indirect addressing was scribbling all over the COG ram until it hit dira and switched itself off. (nice safeguard that!)

    No difference to data behaviour, just made unplug/replug more reliable on Windows.

    Brad
  • Chad GeorgeChad George Posts: 138
    edited 2007-09-17 15:10
    Brad,

    I got a chance to try out the USB serial port. Great job!

    A few questions though... I managed to get it to work on a propeller using a 5Mhz crystal under Windows XP with absolutely no problems.
    But on a propeller that uses a 6Mhz crystal I get an unrecognized device error. I changed all the obvious things (5_000_000 declarations)
    but no luck. Any ideas?

    Also I tried it on my Ubuntu 7.04 and it seems to find the device OK. here's my dmesg
    usb 5-1: new low speed USB device using uhci_hcd and address 2
    [noparse][[/noparse]10288.624000] usb 5-1: device descriptor read/64, error -84
    [noparse][[/noparse]10288.908000] usb 5-1: configuration #1 chosen from 1 choice
    [noparse][[/noparse]10289.344000] cdc_acm 5-1:1.0: ttyACM0: USB ACM device
    [noparse][[/noparse]10289.348000] usbcore: registered new interface driver cdc_acm
    [noparse][[/noparse]10289.348000] drivers/usb/class/cdc-acm.c: v0.25:USB Abstract Control Model driver for USB modems and ISDN adapters
    
    



    After unplugging and replugging I get a little less info

    USB disconnect, address 2
    [noparse][[/noparse]10725.100000] usb 5-1: new low speed USB device using uhci_hcd and address 3
    [noparse][[/noparse]10725.280000] usb 5-1: configuration #1 chosen from 1 choice
    [noparse][[/noparse]10725.284000] cdc_acm 5-1:1.0: ttyACM0: USB ACM device
    
    



    My PC seems to know the port is there but when I try to actually use it (kermit), I get an error message when I send a character.
    Your code mentions something about a slight kernel patch required and maybe this is what I'm running up against. What has to be done to make linux work?

    One more question, you mentioned achieving about 6-7k/sec transfer rate. Do I need to do anything special concerning transfer speed.
    Under windows I was able to open the port at 230400baud and it seemed to work (just echoing characters). Will the USB driver and propeller negotiate the actual
    maximum speed, or is it possible to send data too fast.

    Thanks again. This is a great addition to the propeller's capabilities.

    -Chad
  • BradCBradC Posts: 2,601
    edited 2007-09-17 15:37
    Heya Chad, nice to hear it works for you!

    1st.. it is _absolutely_ hard coded to an 80Mhz cog speed. It will not work at any other clock speed, so I guess the 6MHZ crystal is out. Unfortunately this is just an artefact of the bit bashing used to run the USB MAC.
    I guess if I got hold of a 6MHz crystal I could produce a USB_lowlevel driver that would be tailored to run at that speed. It's only a matter of adding some extra delays.

    2nd. Ubuntu 7.04 will find the device, but will not be able to talk to it as the uhci driver blocks all attempts to send bulk packets.
    If you are interested in patching/compiling your kernel, I'll write a nice detail on what needs to be changed. It's only commenting out 3 lines in the kernel 7.04 uses (I'm using it on this machine here)
    I've never actually recompiled an Ubuntu kernel, but I believe it's pretty easy. I compile all mine from the vanilla kernel.org sources when I need to modify them.

    3rd, Baud rate is completely irrelevant to this device. The commands that the PC sends to the prop to set/get baud rate are received and ignored. The device will send/receive data as fast
    as it's little cogs will carry it.

    There is complete flow control built in, so the whole chain will throttle the transfer speeds as required to suit the data source/sink. The USB protocol has reasonably efficient flow control built in, so if your spin
    program is not reading the data fast enough, the system will just throttle back until there is space in the buffer at each end. No data will be lost.
  • hippyhippy Posts: 1,981
    edited 2007-09-17 16:00
    Chad George said...
    I managed to get it to work on a propeller using a 5Mhz crystal under Windows XP with absolutely no problems.

    Good news in that it means it can work, bad news in that it means
    there's something odd/wrong with my PC. Thanks for the report.

    If only XP reported which file it was it could not find I think I'd be
    a lot closer to getting it working [noparse]:([/noparse]

    On the Win 98SE front, it appears to be working, but without any
    means of serial port enumeration it doesn't get a port and cannot
    be accessed. I cannot find any enumerator ( serenum.sys ) for
    98SE, and I don't know how to access USB devices through VB6
    or anything else. No serious complaint as 98SE is 'dead and buried'
    in most respect but good to know it is fundamentally compatible
    with it.
  • Chad GeorgeChad George Posts: 138
    edited 2007-09-17 16:03
    Brad,

    I kinda figured the cog speed thing was hardcoded. I've got a bunch of spare 6Mhz crystals that I use in my sumobot kit.
    Unfortunately I had the kids all solder in their crystals (now I wish I'd used a socket). I'd be more than willing to send you some.

    I'd also be very interested in instructions on modifying the kernel sources. I've never done before but what better time to learn than now.

    Thanks,
    Chad
  • BradCBradC Posts: 2,601
    edited 2007-09-17 20:27
    @Chad, I'm currently running through the Ubuntu build process to jot down some notes and try and make it easy.
    I've compiled so many kernels I just forget the feeling of it all being new to me..

    I'll see if I can locate a 6Mhz crystal locally.. snail mail takes at least 2 weeks to get to me from anywhere on the planet pretty much.

    @hippy. I seriously feel for you. I fought Win2k for a not insignificant period before extending my middle finger to it and doing a clean XP install to be able to test this thing.
    Win98 will never work, they just did not have a CDC-ACM driver in that OS. Hell, they had only just figured out how to enumerate a mouse..

    Now I have it working in Win2k, for every different port or hub port I stick this thing in, the OS asks me to insert the Windows disk so it can install the driver for it. How freakin' thick is this OS?
    Now, I have 10 different USB serial ports installed. The numbering changes every time I stick it in a different port! ARGH! XP is not much better..

    I still have a spare endpoint pair in this low level stack.. I'm gonna have a try at a dual-port model (and maybe if I can bend the standard a little, a 3 port model!) in the next week or two.
    I just found a couple of old 10K pots lying about.. I can see a USB HID joystick in my future.. (can anyone say steering wheel and throttle? OH YEAH!)

    I really appreciate you guys testing this stuff.. nothing worse than "it works for me, must be your problem" when trying to develop software.

    hippy, did you at least get the California Dreamin' keyboard emulation to work?
  • hippyhippy Posts: 1,981
    edited 2007-09-17 21:31
    BradC said...
    hippy, did you at least get the California Dreamin' keyboard emulation to work?

    Yes, under XP. Got the lyrics typed up into an open copy of Notepad so I'll call that definitely success

    I then blew it by unplugging / resetting which caused XP to think I've got an 'Unknown Device" which
    it won't let me flush. Edited the PID and it's now being recognised but haven't managed to get it
    typing anything wink.gif

    One thought on the serial issue is that I'm dual booting so Windows isn't on C:\ which could be a problem,
    or something within layout.inf isn't as expected or is looking in the wrong place and not finding what
    should be there. I'll keep prodding and seeing if I can come up with anything.

    Don't get distracted or bogged down over my problems but keep tearing ahead with other USB offerings.
    I'm just sorry I'm not able to be giving you or anyone else much help.

    I'll have a look round for Microchip and other CDC drivers / applications and see if I can make them
    sing. I've learned more about USB in the last 24 hours than since it was invented. Thanks for the
    inspiration.
  • BradCBradC Posts: 2,601
    edited 2007-09-17 21:47
    Nah, My Xp install is on E:\ and 2k is on C:\. When I boot into XP it still got it right... next

    The California Dreamin' code should type a copy each time you press Caps/Num/Scroll Lock.. it looks for the LED change state message and triggers.

    USB is kinda fun.. it was a drudge at first.. and I still hate writing descriptors, but overall I'm having a ball now.. Now I've got the low level cog stuff sorted, the app level is a doddle..
    Abstraction rules (especially when it makes things _faster_ rather than the alternative).
  • hippyhippy Posts: 1,981
    edited 2007-09-17 22:14
    BradC said...
    Nah, My Xp install is on E:\ and 2k is on C:\. When I boot into XP it still got it
    right... next

    Bugger. But at least another factor taken out of the equation.
    BradC said...
    The California Dreamin' code should type a copy each time you press
    Caps/Num/Scroll Lock.

    Re-burnt the Eeprom using the original PID, unplugged the PropPlug, plugged in the 'Keyboard'
    USB, hit NumsLock and bingo, worked first time. Press again, and more joy. Think a re-boot
    fixed whatever was wrong on that count, although I also suspect that the PropPlug plugged in
    at boot-up was having some adverse effect.
  • hippyhippy Posts: 1,981
    edited 2007-09-18 00:08
    Feature request time ... you knew it would happen, but should be an easy one ...

    Can you update the USB_LowLevel_XXX.spin so the Start method imports the pin numbers as
    parameters ...
    PUB Start(crctable, buffers,dminuspin,dpluspin,enablepin) : OK | i
      Pcrcmask := crctable
      epb      := buffers
      K        := |< dpluspin        ' D+ ( was Pin 1 )
      J        := |< dminuspin       ' D- ( was Pin 0 )
      Enable   := |< enablepin       ' En ( was Pin 2 )
      OK := cognew(@USB_STACK, 0)
    
    


    And likewise USB_Serial_XXX.spin and future higher level drivers so we can
    choose our own I/O assignments. Thanks in anticipation.
  • hippyhippy Posts: 1,981
    edited 2007-09-18 01:16
    Okay, I'm still bashing away and discovered SetupAPILogger here -
    www.sourcequest.com/download/suplog.htm -
    Ignore the download link in the page, just go up a level to -
    http://www.sourcequest.com/download/suplog.htm

    Attached is the best trace I've managed to get of what's going on or
    not going on as the case may be.

    The failure is in the last few lines, and interesting issues are the fails at
    01:51:12.843 and particularly 01:51:18.953. Maybe someone can hazard
    a guess as to what's going on. I suspect it's Windows re-install time.

    On the plus side, I'm learning lots about Windows which I never knew.
  • hippyhippy Posts: 1,981
    edited 2007-09-18 01:38
    Plus some good news ... California Dreamin' worked fine under Win 98SE.
  • BradCBradC Posts: 2,601
    edited 2007-09-18 14:40
    Ubuntu Kernel re-compile howto. Best of luck Chad.

    I'm running Ubuntu 7.04 on my Mac Mini. It's running a 2.6.20-16-generic stock Ubuntu kernel image.

    This *may* cause any linux-restricted-modules drivers you are using to stop working, Atheros wireless and stuff like that.. you've been warned.

    This process took almost 3G of space on my machine, so it's pretty disk hungry too.

    Ok, here is how I did it.

    grab usb.patch.txt and save it to your home directory. ~/

    sudo apt-get install build-essential kernel-package gcc bin86 linux-source fakeroot

    mkdir kernel
    cd kernel
    tar -xjf /usr/src/linux-source-2.6.20.tar.bz2
    cd linux-source-2.6.20
    cat ~/usb.patch.txt | patch -p1
    cp /boot/config-2.6.20-16-generic .config
    fakeroot make-kpkg -initrd --append-to-version=bkc0.1 linux_image

    <wait, wait, make coffee, drink coffee before it gets cold, wait, go out for a nice Italian meal, wait some more>
    Seriously, this stock kernel compiles world+dog, it takes _forever_... (and I'm on a 1.8G dual core with 2GB of ram!)

    cd ..
    sudo dpkg -i linux-image-2.6.20.3-ubuntu1bkc0.1_2.6.20-16.31_i386.deb

    <reboot into the new kernel> - Make sure you select it in grub, it may not be the default selection (it was here)

    [noparse][[/noparse] 71.640000] usb 3-1: new low speed USB device using uhci_hcd and address 2
    [noparse][[/noparse] 71.820000] usb 3-1: configuration #1 chosen from 1 choice
    [noparse][[/noparse] 71.972000] cdc_acm 3-1:1.0: ttyACM0: USB ACM device
    [noparse][[/noparse] 71.980000] usbcore: registered new interface driver cdc_acm
    [noparse][[/noparse] 71.980000] drivers/usb/class/cdc-acm.c: v0.25:USB Abstract Control Model driver for USB modems and ISDN adapters


    brad@bkmac:~$ stty -F /dev/ttyACM0 -echo ; cat /dev/ttyACM0 & sleep 1 ; echo " Hello There " > /dev/ttyACM0 ; sleep 1 ; killall cat
    + Terminated cat /dev/ttyACM0
    5970
    Hello There

    Cookin' with gas!
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-09-18 19:41
    Kill all cat? that's not a very nice thing to do, but at least you were well rested before you did it tongue.gif .

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • hippyhippy Posts: 1,981
    edited 2007-09-18 20:01
    @ BradC : Finally, some very good and welcome news - After a re-install of XP,
    everything is working as it should have from the off.
  • BradCBradC Posts: 2,601
    edited 2007-09-18 20:43
    YAY! (cheers, waves, fireworks, bunting)

    There is the end all solution to windows problems.. re-install..

    Wish it could have been otherwise, but yes the news is very welcome thanks [noparse]:)[/noparse]

    I'm making the mods you suggested and working on a 96Mhz version of the lowlevel cog also.. should have something in a day or so.
  • hippyhippy Posts: 1,981
    edited 2007-09-19 16:42
    @ BradC : Having some problems under Windows XP. Using my own
    terminal emulator application ( VB6 via MScomm32 ) and the old
    Terminal.exe from Win 95 days, after holding down a character to
    send repeatedly the Propeller simply 'stops responding' and eventually
    Windows plays its 'device gone away' tune.

    This is with a simple Spin echo loop, but also with a simple number
    printing loop ( no receive ) sending every 500mS ...

    repeat
       usb.tx("<")
       usb.tx( usb.rx )
       usb.tx(">")
    



    Printing numbers is a bit hit and miss. Most times it fails, but some
    times seems to keep going. When it does fail, I note that the blue
    Led on the PropPlug is flashed ( not connected to anything ) whether
    Propeller Tool is running or not.
  • BradCBradC Posts: 2,601
    edited 2007-09-19 17:01
    Is it reproducible in hyperterminal?
    If not, can you send me a copy of your terminal program?

    The blue led is the receive led isn't it? How could that possibly be triggered from the prop I wonder?
  • BradCBradC Posts: 2,601
    edited 2007-09-19 17:21
    Just for kicks, comment out the call #crc16check in pid_data0 and pid_data1 in USB_lowlevel and see what happens.
    That routine stretches us right out to the timing limits of the USB controller
  • hippyhippy Posts: 1,981
    edited 2007-09-19 18:14
    Repeatable in HyperTerm, seems to occur when opening other applications and browsing, perhaps something putting a strain on the system ?

    With both #crc16check removed it seemed stable.

    I call PUB Start, delay 20 secs then send every 500mS. That allows the Propeller to be powered up, Windows to go 'Bing-Bong", and the associated COM port to be available for HyperTerm when loaded.

    I noticed another effect; sending from Propeller only, characters sent to it via HyperTerm which the Propeller doesn't even try and receive causes transmission to stall for a while but does often recover, send too much and XP grinds to a halt, 100% CPU utilisation smile.gif
  • BradCBradC Posts: 2,601
    edited 2007-09-19 18:45
    Right.. your system seems to be on the edge of the timing limits.. all the other soft-usb implementations ignore the CRC check and just assume it's all good.. I'll have to look closer and
    see what I can trim down to speed things up. It does work on all my machines here, but I do know I'm stretching the timing right to the ragged edge of the spec.

    I'll try some tests on the other stuff.. I assume you mean you get the prop to send data to XP without hyperterminal open and things get ugly?

    Also nasty things happen when you send data to the prop from hyperterminal and you are not calling usb.rx on the prop?

    Maybe windows has ugly issues with flow control. I don't see this problems on MacOS or Linux.. (there's a surprise)

    Will have a play, thanks for the detailed feedback and testing, I really appreciate it.
Sign In or Register to comment.