Shop OBEX P1 Docs P2 Docs Learn Events
California Dreamin' — Parallax Forums

California Dreamin'

BradCBradC Posts: 2,601
edited 2012-05-04 06:04 in Propeller 1
Ok mobsters, as a bit of a teaser I've attached a _very_ preliminary version of a low-speed USB stack.

I've had to break it into 2 cogs unfortunately (I'm a bit upset about that), but doing it that way just makes more sense when you don't have an interrupt.

This code enumerates as a USB keyboard and types out the lyrics from "California Dreaming" as translated by Michael Hetherington here ...
http://www.sxlist.com/techref/scenix/lib/io/dev/keys/usbdemo-mh.htm

The stack has not been validated, it has bugs and the code is absolutely woeful. I've had my head stuck in 650 pages USB specs all week and not even thought about tidying / optimising any of the code yet.

The advantage of using 2 cogs here is we can properly validate all our inbound CRC's (I've skipped the 5 bit ones at the moment while I build a better checker), and get a response out plus manage *all* possible endpoints.

Most of the embedded stacks face limitations on endpoint management, speed and proper validation. As we run 2 processors for the stack we don't have most of those limitations.

Ignore the :
- bad code (yes I know)
- debug code (it's very hacky)
- typos
- comments written at 3am after half a bottle of Jamesons
- fatal logic errors
- horrid descriptor strings (again, half a bottle of Jamesons at 3am)



Tested on linux and win2k.

I've got improvements and other things in the works. I've learned a hell of a lot about USB in the last couple of days.. but the main thing I learned is that a prop is *just not fast enough* to do Full-Speed.

If, perchance you wanted to run this code up you need 2 68Ohm resistors and a 3.3k.

Plug it into a PC *before* you fire it up as it will enumerate, then about 5 seconds later it will happily type out the lyrics.. then it's game over until you reset the device.

Brad (unicode blows goats)
Why won't the forums allow me to upload a .spin file? I had to add .txt to the end to get it to go! Bah!

[noparse][[/noparse]edit - one of the uploaded files was wrong]
[noparse][[/noparse]edit - IO starts at pin 0 and not pin 1, so 0-2 not 1-3]

Post Edited (BradC) : 9/6/2007 3:21:24 AM GMT

Comments

  • Harrison.Harrison. Posts: 484
    edited 2007-09-05 20:07
    This is very impressive, even if it doesn't do more then enumerate as a HID keyboard and type out letters. I'll have to find a worthless usb cable to cut up and use for testing this.
  • BaggersBaggers Posts: 3,019
    edited 2007-09-05 20:21
    Well done BradC, it's one thing I didn't think Prop1 would be up to, but you've done a cracker of a job getting the prop to handle USB [noparse]:)[/noparse]

    Is there anything a prop can't do! I'm beginning to doubt it. well, maybe make a cuppa tea, although some roboteer will probably put that to the test now. lol

    Like Harrison, I'm gonna have to get a spare USB cable, to hack up [noparse]:)[/noparse]

    Jim.
  • BeanBean Posts: 8,129
    edited 2007-09-05 21:31
    BradC,
    Well done ! I was wondering if a Propeller could do this.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The first rule to being successful is "Learn from your mistakes",
    The second rule is "Be willing to make mistakes"
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • BergamotBergamot Posts: 185
    edited 2007-09-05 21:34
    Well no wonder your code has bugs; you're drinking irish whiskey... [noparse]:)[/noparse]
  • QuattroRS4QuattroRS4 Posts: 916
    edited 2007-09-05 22:28
    Yeah and thats the best of it !

    John Twomey
    Ireland

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Necessity is the mother of invention'
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-09-05 23:53
    Brad,
    Please check my grasp of your program:

    A prop,
    connected by a generic usb cord [noparse][[/noparse]not a prop plug!]
    to a win pc or linux box [noparse][[/noparse]running a program that expects keyboard input],
    pretends to be a usb keyboard that is typing California Dreaming.

    My real question is this: what references to usb have you found are most useful? And can you share links to them?

    Fred
  • BradCBradC Posts: 2,601
    edited 2007-09-06 03:47
    @Bergamot : I usually drink Scotch, but I had a lovely bottle of Jamesons special reserve that was looking lonely

    @Fred : Absolutely correct. No additional chips, just the propeller, 3 IO pins and 3 resistors

    As for useful docs, these three are all you would need (or all I needed anyway). What made my life easier is I wrote a basic USB analyser based on USB_lowlevel_003.spin (attached above) and used that to watch the enumeration process of a cheapo USB mouse I had lying around. That helped. I have worked with USB HID descriptors before too when playing with Qemu, so I guess that gave me a leg-up.

    HID Class specification
    http://www.usb.org/developers/devclass_docs/HID1_11.pdf

    USB 2.0 specs
    http://www.usb.org/developers/docs/usb_20_071607.zip

    USB in a nutshell
    http://www.beyondlogic.org/usbnutshell/usb-in-a-nutshell.pdf

    Despite the somewhat excessive size, the USB 2.0 spec is absolutely the only document you really need. It answers all possible questions.

    Plenty of people (well, at least 4 given a cursory search) had done this before. 3 on AVR and one on SX.
    I did have a look at Dick Streefland's USBTiny code bit the majority of it is in C and chock full of macros. Made my head hurt. Worth a look if you do AVR though.
    http://www.xs4all.nl/~dicks/avr/usbtiny/
    He does a nice little 4 bit CRC16 routine in there that would be perfect for a memory starved application.

    Shoulders of giants and all that...

    The propeller needs 3 IO pins instead of the usual 2 as it's too slow to boot up on power-on. The USB bus tries to enumerate with it prior to it actually being in a running state, so we use the 3rd pin to do the presence signal when the stack is up and running.
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-06 06:54
    I like the following articles :www.usbmadesimple.co.uk/

    A lot of references on this site: www.lvr.com/usb.htm
  • BradCBradC Posts: 2,601
    edited 2007-09-06 07:28
    deSilva said...
    I like the following articles :www.usbmadesimple.co.uk/


    That particular site is pretty good. Wish I'd had that last week
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-09-06 07:46
    Thanks Brad.

    Post Edited (Fred Hawkins) : 9/6/2007 9:06:08 AM GMT
  • Gerry KeelyGerry Keely Posts: 75
    edited 2007-09-06 08:16
    Whiskey(whisky !) in Irish is UiSce Beatha. Notice the common connection !



    Gerrytongue.gif·
  • QuattroRS4QuattroRS4 Posts: 916
    edited 2007-09-06 12:21
    Nice one there Gerry ... I take it you are from the emerald Isle too then ?

    John Twomey

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Necessity is the mother of invention'
  • hippyhippy Posts: 1,981
    edited 2007-09-15 17:49
    @ BradC : Just to clarify, is this correct for the hardware configuration ? What does Vusb connect to, if anything ? Am I right in assuming the same connections will be used for your Serial-USB interface ?
                          N/C --<  Vusb
    
                 ___
        P0 <>---|___|----.----<> D-
                 68R     |
                 ___     |
        P1 <>---|___|----|----<> D+
                 68R     |
                 ___     |
        P2 >----|___|----'  .--> 0V
                 1K5       _|_
    
    
       Correct Hardware Configuration
    
    


    Edited : Corrected pin numbers to P0..P2

    Edited Again : Pins now selectable in source, 68R plus 1K5, not 680R plus 3K3 as shown earlier. Also 1K5 from P2 to D-, not 3K3 from P2 to P0 as previously shown.

    Post Edited (hippy) : 10/1/2007 12:58:51 PM GMT
  • BradCBradC Posts: 2,601
    edited 2007-09-15 17:51
    Vusb is not connected anywhere at all. Yep that looks right except they are p0-p2 rather than p1-p3..
    Yeah, the serial-usb uses the same cable and connections.. it's posted up now along with an improved version of the california dreaming code (read with less bugs) [noparse]:)[/noparse]
  • hippyhippy Posts: 1,981
    edited 2007-09-15 17:57
    I posted just before you delivered your Keyboard / serial code update. Many thanks for both.
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-15 19:04
    Just BTW: There is a tendency to use "particular" resistor values to pretend a more precise behaviour... I have never ever in all my hobby electronic life used any value other than "E3":
    47 - 100 - 220 - 470 - 1k - 2k2 - 4k7 - 10k - 22k - 47k- 100k - 220k - 470k - 1M
    And I do not plan to use something like 680 or 3k3, or use anything for the video DAC but 220 + 470 + 1k smile.gif

    P.S.: When I look into my resistor box.... 47, 47k and 470k have been also hardly used
  • SkogsgurraSkogsgurra Posts: 231
    edited 2007-09-15 20:26
    BradC - I had just been told that USB cannot be handled like this. Where did you go wrong? - Or were I wrongly informed?? I hope the latter - thanks for a great contribution!!!

    BTW, I got some Wales whisky liquer when I visited swansea this summer. Is that OK to imbibe? Name is "xxx Jones". What else could one expect?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Chad GeorgeChad George Posts: 138
    edited 2007-09-16 18:12
    Great work. I too had heard it said that USB was too much for the current prop.

    I know this work is in the early stages, but where do to you "think" it might be able to go?
    Is there any chance of USB host capabilities? Or could we have a USB serial port (even if its at a lower baud rate)

    I've been working on designs for cheap educational robot kits and a software only solution for programming a robot using the USB port (since almost no computers come with serial ports anymore) would help a lot.

    I'd be interested in helping make this happen.

    I know its off topic but has anyone been working on a bit-banging ethernet driver?
    I know I've heard of it being done on other micros.

    -Chad
  • BradCBradC Posts: 2,601
    edited 2007-09-16 18:53
    Serial? Try here.

    http://forums.parallax.com/forums/default.aspx?f=25&m=216569

    I can get sustained rates of about 6.8-7K bytes/second in both directions simultaneously. I've not benchmarked it in one direction.
    Gonna have a crack at a bi-directional class compliant MIDI interface next I think.
  • Harrison.Harrison. Posts: 484
    edited 2007-09-16 20:35
    Chad George said...

    I know its off topic but has anyone been working on a bit-banging ethernet driver?
    I know I've heard of it being done on other micros.

    Not to derail this topic, but any links on the other projects that do bit-bang ethernet? I don't know if it's worth it since a spi ethernet chip is about 5 bucks + another 5 bucks for magnetics. The only project I am aware of uses a FPGA and can barely do receive.
  • Chad GeorgeChad George Posts: 138
    edited 2007-09-17 02:06
    Awesome, thanks for all the work.
    I have a feeling I won't get much real work done tomorrow until I get this working on my robot kit.

    This is really exciting stuff.

    -Chad
  • BradCBradC Posts: 2,601
    edited 2007-09-17 20:47
    @Harrison & Chad

    I've done some prelim reading on Ethernet.. it _might_ be possible to do transmit, but I can pretty confidently say receive is well out of the Propellers league. If I could do that I could do Full Speed USB (which would be nice)
    10Mhz is just not gonna happen. Hard enough doing NRZI and Bit stuff at 1.5Mhz for USB
  • tpw_mantpw_man Posts: 276
    edited 2008-02-07 20:55
    Can you make a driver that allows the user to send strings on command?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I am 1010, so be surprised!
  • TracTrac Posts: 7
    edited 2012-04-27 06:32
    Hi Brad, thx for this great code.
    I intend to use your USB generic propeller-keyboard to send makro's and therefore I need for example to send CTRL-C , ALT-F or SHIFT-a to write an 'A'.
    If I now send this line of command $E0,$06 it acts as follows --- press-[CTRL]-release-press-[c]-release --- what results in 'c' instead of 'CTRL-C'.
    Is there a way to include a 'keep key down' code so it can send --- press-[CTRL]-hold-press-[c]-release -- ?

    -Trac
  • pedwardpedward Posts: 1,642
    edited 2012-04-27 11:57
    I would assume that you are sending scan codes as a keyboard, you need to send the proper scan code for the combined CTRL-C.

    Wireshark has a USB sniffing capability which should help you in debugging what the right codes should be.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-04-27 12:38
    I notice in the Keyboard object to use shift or control characters a set value is added to the orignal key value.
    ''      +100    if Shift
    ''      +200    if Ctrl
    ''      +400    if Alt
    ''      +800    if Win
    ''
    ''      eg. Ctrl-Alt-Delete = $6C9
    

    (Delete by itself is $C9.)

    I don't know if USB has a similar type of protocol.
  • TracTrac Posts: 7
    edited 2012-05-02 03:48
    Hi Duane and Pedward,

    I added the keyboard layout for a Generic US keyboard. In Red are the hex scancodes.

    I did 'googled' extended keyboard scancodes but they are not working with this code. Normally it is as Duane mentioned, there are 2 hex codes to be send, but this code is written to send only the 2 last characters and ignore the first.

    This is what I found out.

    ep1send mov out1, #1
    test ep1len, allones wz
    IF_Z mov out1, #0
    IF_Z jmp #ep1send_ret
    call #bufstart
    mov data, #0
    call #pushbuf
    call #pushbuf
    xor ep1toggle, #1 wz
    IF_Z jmp #:keyup 'If you remove this line then the USB keyboard acts is like you hold down a key aaaaaaaaaaaaaaaaaaa
    rdbyte data, ep1buf
    call #pushbuf ' Key down
    mov data, #0
    jmp #:next
    :keyup
    add ep1buf, #1
    sub ep1len, #1
    call #pushbuf ' Key up


    My guess is that there is a bit of code to write to look at the first hex code (ex. $06C9) but my assembler knowledge is almost zero. :innocent:




    1

    2

    3

    4

    5

    6

    7

    8

    9

    0A

    0B




    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11



    -

    err

    err

    err

    A (Q)

    B

    C

    D

    E

    F

    G

    H



    0C

    0D

    0E

    0F

    10

    11

    12

    13

    14

    15

    16

    17



    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23



    I

    J

    K

    L

    M (;)

    N

    O

    P

    Q (A)

    R

    S

    T



    18

    19

    1A

    1B

    1C

    1D

    1E

    1F

    20

    21

    22

    23



    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35



    U

    V

    W (Z)

    X

    Y

    Z (W)

    1

    2

    3

    4

    5

    6



    24

    25

    26

    27

    28

    29

    2A

    2B

    2C

    2D

    2E

    2F



    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47



    7

    8

    9

    0

    Enter

    Esc

    BSp

    Tab

    Space

    - / _

    = / +

    [ / {



    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    3A

    3B



    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59



    ] / }

    \ / |

    ...

    ; / :

    ' / "

    ` / ~

    , / <

    . / >

    / / ?

    Caps Lock

    F1

    F2



    3C

    3D

    3E

    3F

    40

    41

    42

    43

    44

    45

    46

    47



    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71



    F3

    F4

    F5

    F6

    F7

    F8

    F9

    F10

    F11

    F12

    PrtScr

    Scroll Lock



    48

    49

    4A

    4B

    4C

    4D

    4E

    4F

    50

    51

    52

    53



    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83



    Pause

    Insert

    Home

    PgUp

    Delete

    End

    PgDn

    Right

    Left

    Down

    Up

    Num Lock



    54

    55

    56

    57

    58

    59

    5A

    5B

    5C

    5D

    5E

    5F



    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95



    KP /

    KP *

    KP -

    KP +

    KP Enter

    KP 1 / End

    KP 2 / Down

    KP 3 / PgDn

    KP 4 / Left

    KP 5

    KP 6 / Right

    KP 7 / Home



    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    6A

    6B



    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107



    KP 8 / Up

    KP 9 / PgUp

    KP 0 / Ins

    KP . / Del

    ...

    Applic

    Power

    KP =

    F13

    F14

    F15

    F16



    6C

    6D

    6E

    6F

    70

    71

    72

    73

    74

    75

    76

    77



    108

    109

    110

    111

    112

    113

    114

    115

    116

    117

    118

    119



    F17

    F18

    F19

    F20

    F21

    F22

    F23

    F24

    Execute

    Help

    Menu

    Select



    78

    79

    7A

    7B

    7C

    7D

    7E

    7F

    80

    81

    82

    83



    120

    121

    122

    123

    124

    125

    126

    127

    128

    129

    130

    131



    Stop

    Again

    Undo

    Cut

    Copy

    Paste

    Find

    Mute

    Volume Up

    Volume Down

    Locking Caps Lock

    Locking Num Lock



    84

    85

    86

    87

    88

    89

    8A

    8B

    8C

    8D

    8E

    8F



    132

    133

    134

    135

    136

    137

    138

    139

    140

    141

    142

    143



    Locking Scroll Lock

    KP ,

    KP =

    Internat

    Internat

    Internat

    Internat

    Internat

    Internat

    Internat

    Internat

    Internat



    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    9A

    9B



    144

    145

    146

    147

    148

    149

    150

    151

    152

    153

    154

    155



    LANG

    LANG

    LANG

    LANG

    LANG

    LANG

    LANG

    LANG

    LANG

    Alt Erase

    SysRq

    Cancel



    9C

    9D

    9E

    9F

    A0

    A1

    A2

    A3

    A4

    A5

    A6

    A7



    156

    157

    158

    159

    160

    161

    162

    163

    164

    165

    166

    167



    Clear

    Prior

    Return

    Separ

    Out

    Oper

    Clear / Again

    CrSel / Props

    ExSel






    E0

    E1

    E2

    E3

    E4

    E5

    E6

    E7







    224

    225

    226

    227

    228

    229

    230

    231







    LCtrl

    LShift

    LAlt

    LGUI

    RCtrl

    RShift

    RAlt

    RGUI






Sign In or Register to comment.