Shop OBEX P1 Docs P2 Docs Learn Events
P2 USB Host driver (re-)development thread - Page 6 — Parallax Forums

P2 USB Host driver (re-)development thread

13468915

Comments

  • Wuerfel_21Wuerfel_21 Posts: 4,442
    edited 2023-05-09 09:39

    7 port hubs are not neccessarily chained 4-ports. I read somewhere that the hub status report format also just happens to be limited to 7 ports.

  • @rogloh said:

    @Wuerfel_21 said:
    A better question: why?
    What's the point of yoinking the cursor position?

    In general you don't need to, but maybe you want to center the mouse cursor at startup so it's immediately visible, or if you had two COGs sharing the display and each had their own cursor position state you could hit some key to flip the window and the mouse position could be retained from prior session/window etc. There are a few use cases, but admittedly not that many.

    Region mice are other things possible in my driver - each region can have its own mouse sprite not just a single global one. Maybe you want to multiplex a single physical device in different window regions with different co-ordinates (but not simulataneously - that'd be crazy).

    This is all very niche/adcanced.
    I should perhaps clarify that the mouse limits and pointer write are intended as "bonus features" to help with simple applications that might not have a poll loop running at all times. If you do have a poll loop, do not set the mouse limits and use the X/Y as running accumulators, somewhat like:

    VAR
    long prev_x,prev_y
    PUB main() | cur_x,cur_y,delta_x,delta_y
      repeat
        video.wait_vblank()
        cur_x,cur_y,_ := usb.mouse_xyz()
        delta_x := cur_x - (prev_x\cur_x) ' <- this is post-set, not divide
        delta_y := cur_y - (prev_y\cur_y)
    
        ' Do whatever with the deltas now
    

    Setting start position should work, but currently doesn't because the mouse data gets reset upon root port connection. There's really no reason for that, so I'll maybe change that.

  • roglohrogloh Posts: 5,147
    edited 2023-05-09 09:49

    I came up with one possible method for USB mouse position change. Mostly seems to work, but something resets the co-ordinates to 0,0 after I change them if I try to initialise them before the mouse is detected. If I wait for the mouse to be found first before setting the new position it seems okay, plus it doesn't reset to 0,0 if I hot swap the mouse which is interesting. Still looking.

  • @Wuerfel_21 said:
    I should perhaps clarify that the mouse limits and pointer write are intended as "bonus features" to help with simple applications that might not have a poll loop running at all times. If you do have a poll loop, do not set the mouse limits and use the X/Y as running accumulators, somewhat like:

    VAR
    long prev_x,prev_y
    PUB main() | cur_x,cur_y,delta_x,delta_y
      repeat
        video.wait_vblank()
        cur_x,cur_y,_ := usb.mouse_xyz()
        delta_x := cur_x - (prev_x\cur_x) ' <- this is post-set, not divide
        delta_y := cur_y - (prev_y\cur_y)
    
        ' Do whatever with the deltas now
    

    Setting start position should work, but currently doesn't because the mouse data gets reset upon root port connection. There's really no reason for that, so I'll maybe change that.

    Yeah if you burn a COG you can do this, but it can be heavy on the CPU if you want a smooth mouse response on screen.

  • pik33pik33 Posts: 2,350
    edited 2023-05-09 09:58

    I have already managed to replace the mouse in Prop2Play and there is another function to implement: a double click (that my RPi contraption reports as an independent event)

    I will implement the double click detection directly in the player. Until I do this, I used the right button instead

  • @rogloh said:
    Yeah if you burn a COG you can do this, but it can be heavy on the CPU if you want a smooth mouse response on screen.

    No? You just do this once in your vblank loop/interrupt.

  • @rogloh said:
    Still looking.

    Move the mouse data out of the cache region.

  • @Wuerfel_21 said:
    Setting start position should work, but currently doesn't because the mouse data gets reset upon root port connection. There's really no reason for that, so I'll maybe change that.

    Ok that must have been what I was running into. If that is changed this may work and I'll look into what you mean by the cache region. My waitus(2) could probably be replaced with a small delay in actual clocks with inline PASM. The critical section was reduced to a very small amount of code by polling the mouse_update state much later in the sequence just before the write back. It's probably something around 50 clocks now.

                    rdlong  htmp, ptrb[4]                   ' read update flag
                    cmp     htmp, #0 wz
            if_nz   ret                                     ' don't write to HUB if being updated
    
                    ' write back accumulators
                    setq    #3-1
                    wrlong  hr1,ptrb
    
  • Wuerfel_21Wuerfel_21 Posts: 4,442
    edited 2023-05-09 10:16

    I guess your method here is low-enough overhead, so I think I'll add it. (the flag should be a byte though :) )

    Have to rest a bit first though.

  • roglohrogloh Posts: 5,147
    edited 2023-05-09 10:24

    @Wuerfel_21 said:
    I guess your method here is low-enough overhead, so I think I'll add it. (the flag should be a byte though :) )

    Have to rest a bit first though.

    No problem, thanks for doing that. I'm really not trying to make things too difficult, I promise. I know about the byte vs long thing and was thinking the same but just got worried it was out of range of PTR indexing without resorting to further changes of data structure order etc. Can we access ptrb[16] or does it top out at ptrb[15]? Can't recall offhand, needed to double check that. Also we may be able to set Z flag in the new rdlong too, to remove an instruction following it.

    PS. Moving that mouse state outside of cached area did fix the initial state issue I had. Thanks. :smile:

  • Also, no, you do need a slightly longer wait, because at any time the USB cog might get it's 1000 Hz frame IRQ.

  • @rogloh said:
    Can we access ptrb[16] or does it top out at ptrb[15]? Can't recall offhand, needed to double check that.

    Non-modify PTR expression goes from -32 to 31: https://p2docs.github.io/hubmem.html#pointer-expressions

  • roglohrogloh Posts: 5,147
    edited 2023-05-09 10:31

    @Wuerfel_21 said:
    Also, no, you do need a slightly longer wait, because at any time the USB cog might get it's 1000 Hz frame IRQ.

    It can, but the new position will just override it. Any previous increments to the update will be ignored. You just need to ensure that the driver doesn't clobber the newly updated values in HUB RAM before they are applied.

    EDIT: Sorry I see what you mean now, there is an ISR inside the USB code that could extend the time in the critical section.

  • @pik33 said:
    7-port hub strange behavior: 4 ports works of 7 available. I'd be less surprised if only 3 were working: that would mean the second hub in the chain is not supported.

    A second hub in not supported (yet), but also the current implementation is limited to 4 ports. Should be a fairly easy fix to add the remaining 3 ports.
    Can you post the debug output with only the hub connected to be sure that it is not chained ?

  • Wuerfel_21Wuerfel_21 Posts: 4,442
    edited 2023-05-09 10:32

    That's what I mean though. If the interrupt hits between reading the update flag and writing the accumulators, it will spend some(tm)(????) amount of time in start-of-frame lala land and then overwrite the accumulators. One could block the IRQ for this sequence but I think(???) that's asking for trouble.

  • roglohrogloh Posts: 5,147
    edited 2023-05-09 10:35

    @Wuerfel_21 said:
    That's what I mean though. If the interrupt hits between reading the update flag and writing the accumulators, it will spend some(tm)(????) amount of time in start-of-frame lala land and then overwrite the accumulators. One could block the IRQ for this sequence but I think(???) that's asking for trouble.

    Gotcha. Yeah increase the delay slightly in the SPIN2 code to compensate. Not a big deal, this mouse_set_xyz thing is an uncommon action anyway and doesn't slow down the driver side in any significant way.

  • pik33pik33 Posts: 2,350
    edited 2023-05-09 11:12

    @macca said:

    @pik33 said:
    7-port hub strange behavior: 4 ports works of 7 available. I'd be less surprised if only 3 were working: that would mean the second hub in the chain is not supported.

    A second hub in not supported (yet), but also the current implementation is limited to 4 ports. Should be a fairly easy fix to add the remaining 3 ports.
    Can you post the debug output with only the hub connected to be sure that it is not chained ?

    Cog2  hdev_port = 0, #hdev_id = $1A40_0201, $0000_0000, $0000_0000, $0000_0000, 
    $0000_0000, hdev_bcd = $0100                                                    
    Cog2  hconfig_base = $09, $02, $19, $00, $01, $01, $00, $E0, $32, $09, $04, $00,
     $00, $01, $09, $00, $00, $00, $07, $05, $81, $03, $01, $00, $FF                
    Cog2  hdev_intf_idx = 0, hdev_class = $09, hdev_subclass = $00, hdev_protocol = 
    $00                                                                             
    Cog2  hhub_ctrl_ep = $0060_08E1, hhub_ep_addr = $00D0_8800                      
    Cog2  hub_descr_p = $09, $29, $07, $88, $00, $32, $64, $00, $FF                 
    Cog2  hdev_type = $0000_0009, (hdev_type + 1) = $0000_0000   
    
  • By the way, I just found a good application for controlling the mouse position in software when I ran a test of this feature.

    Consider a case where you have some on screen menuing system and you have a USB mouse and/or gamepad present. It's easy to achieve parallel control of the mouse cursor position on screen by causing the mouse co-ordinates to increment/decrement slightly whenever the gamepad up/down/left right buttons are held. This lets both the mouse and a gamepad control the current mouse position on screen and is actually quite useful in real life when you try it out. Example code snippet/patch for hidpad_to_vga.spin2 with this feature is added below. No bounds checking is being done in this test, but that would obviously be included in a real application.

    PRI moveMouse(incx,incy) | x,y,z
      x,y,z:=usb.mouse_xyz() ' read mouse position
      usb.mouse_set_xyz(x+incx,y+incy,z) ' shift it and update position
    
    PRI print_hidpad(num) | id, caps, i
      id := usb.hidpad_id(num)
      caps := usb.hidpad_getcaps(num)
      if caps.[16]
        i := usb.hidpad_axis(num, 0)
        if abs(i) > 400 ' threshold
           moveMouse(i>0 ? 2 : -2, 0)
      if caps.[17]
        i := usb.hidpad_axis(num, 1)
        if abs(i) > 400 ' threshold
           moveMouse(0, i>0 ? 2 : -2)
      text.setTextColours(id ? $F : $8,$1)
      text.printStr(@"HIDpad ")
      text.dec(num)
      ...
    
  • I guess that's one way to implement that :p

  • Okay, upstream version now has a mouse_move method. I've left off the wheel (Z) value because I don't think you'd want to update it together with the position (actually, reading it together with the position is slightly weird, isn't it? Should I make it a separate function?)

  • roglohrogloh Posts: 5,147
    edited 2023-05-09 13:49

    @Wuerfel_21 said:
    Okay, upstream version now has a mouse_move method. I've left off the wheel (Z) value because I don't think you'd want to update it together with the position (actually, reading it together with the position is slightly weird, isn't it? Should I make it a separate function?)

    I think the wheel is independent. I also realized this when I was trying to update the position. You needed to read Z even if you just want to change X and Y onscreen. Separate function for accessing Z should be okay IMO.

  • pik33pik33 Posts: 2,350
    edited 2023-05-09 13:51

    Z needs a separate function. May be useful in a GUI with several windows that can scroll vertically (Prop2play!), while switching between them. Now I simply compute deltas for z restricting it to +-1

  • Wuerfel_21Wuerfel_21 Posts: 4,442
    edited 2023-05-09 13:58

    You're supposed to calculate the delta. Storing your app state into the real-time updating driver buffer is mildly awful.

  • There could be a couple of other things that may be helpful in the driver layer for a mouse controller to use with slower application polling but I don't want to push my luck too much right now...maybe it could be added later one day if considered very useful to clients and there is room etc.

    • optional COG ATN notification on mouse changes to trigger some actions in other COGs
    • latching current mouse co-ordinates, sysclk ticks, on mouse button up/down events

    You can probably still achieve this with continuous rapid polling from another COG but sometimes could miss certain things if you are not fast enough to keep up during fast movement and clicking.

  • There's currently some bytes trying to hold whether there's a pending click, but I think it doesn't work and I might remove it again.

    If you can't get your application to actually react at 60 Hz... it probably has no business having mouse input. The main issue here is that none of the high level environments support multiple threads / interrupts within a single cog.

  • pik33pik33 Posts: 2,350

    The good old retrocomputing way is to check all UI things in vblanks> @Wuerfel_21 said:

    You're supposed to calculate the delta. Storing your app state into the real-time updating driver buffer is mildly awful.

    And you can still directly update the variable from the Spin code. There is a small risk that the variable will not update when the driver updates it at the same time than the Spin method. The workaround is very simple, the mouse update rate is no more than 1000 Hz. Set the variable, read the variable, if the same, ok, if not, set again, as the procedure will be much faster than 1 ms

  • roglohrogloh Posts: 5,147
    edited 2023-05-09 14:27

    @Wuerfel_21 said:
    There's currently some bytes trying to hold whether there's a pending click, but I think it doesn't work and I might remove it again.

    If you can't get your application to actually react at 60 Hz... it probably has no business having mouse input. The main issue here is that none of the high level environments support multiple threads / interrupts within a single cog.

    Yeah an ISR triggered off some event such as mouse change would be really helpful, it could then go update its local states and populate an application event queue to be processed at will by the slower application. No need for continuous 60Hz operation then by the application and it can go process things at leisure. But as you say we don't have such things (yet). Until then it might be a case where we would have a USB driver COG, an input poller COG just to keep up with the driver changes and fill an event queue, and an application COG. Sucks a bit that way but whatever. I'm thinking GUI not 60Hz gaming in this case where some graphics refresh/redraw activities by the application will be slow.

  • If the redraw takes that long, you might just want to set an hourglass cursor and ignore clicks while it's doing that (since the user doesn't really see what they're clicking on).

    If you really wanted a mouse event queue still, it could be made similar to the key queue. Though I wouldn't implement it now. I still want to make full HID mouse a thing, wherein a lot of the current code (as little as it is) might be canned.

  • pik33pik33 Posts: 2,350

    I still want to make full HID mouse a thing,

    What does that mean? A mouse already works. A full report mode?

    where some graphics refresh/redraw activities by the application will be slow.

    The PSRAM driver with command list is fast enough to composite several windows in the real time (= no redraw needed) - the window manager is still on my todo P2 list.

  • roglohrogloh Posts: 5,147
    edited 2023-05-11 06:58

    I've got one of these USB HID encoder devices (Griffin Powermate) here and it would be interesting to see if it could be made to work with the P2 now we have some working USB code.

    Apparently according to someone's Java USB driver I found for it on GitHub it just uses interrupt transfers on endpoint $81 to poll a single axis delta value and the button state in a similar looking format to the mouse boot protocol so I'm now sorta wondering if it could be used like a mouse or gamepad with some mods in the driver code I could add if required. Any thoughts or hints on this?
    https://github.com/EsotericSoftware/powermate/blob/master/src/powermate/PowerMate.java

    It can also set its LED brightness with an interrupt write transfer of a single byte on endpoint 2, although that doesn't map into typical mouse functionality but looks similar to writing LED states for a keyboard perhaps.

    Update: Also when I plugged it into the USB hub on the P2 and ran hidpad_to_vga.spin2 to see what happens, it does show up as a HID gamepad entry with ID 077D0410 but doesn't report an axis or button changes (it does print RX=0 while all others are N/A). It also seems to show up detected on screen only after I actually spin the encoder wheel, not immediately after I plug it in which is strange. Any useful debug code to help figure out what this thing actually reports itself as?
    Here's what I got when I enabled debug and had (just) this device fitted to the USB hub...if it makes sense to anyone.

    roger@RLs-Mac-mini usbnew % l -t -b 2000000 hidpad_to_vga.binary
    ( Entering terminal mode.  Press Ctrl-] or Ctrl-Z to exit. )
    Cog0  INIT $0000_0000 $0000_0000 load
    Cog0  INIT $0000_0404 $0000_0000 load
    Cog1  INIT $0000_3B0C $0000_74C0 load
    Cog2  INIT $0000_4C7C $0000_0000 load
    Cog2  hdev_port = 0, #hdev_id = $058F_9254, $0000_0000, $0000_0000, $0000_0000, $0000_0000, hdev_bcd = $0312
    Cog2  hconfig_base = $09, $02, $19, $00, $01, $01, $00, $E0, $32, $09, $04, $00, $00, $01, $09, $00, $00, $00, $07, $05, $81, $03, $01, $00, $FF
    Cog2  hdev_intf_idx = 0, hdev_class = $09, hdev_subclass = $00, hdev_protocol = $00
    Cog2  hhub_ctrl_ep = $0060_08E1, hhub_ep_addr = $00D0_8800
    Cog2  hub_descr_p = $09, $29, $04, $09, $00, $16, $64, $00, $FF
    Cog2  hdev_type = $0000_0009, (hdev_type + 1) = $0000_0000
    Cog2  hub_status_chg = %00001001_01110011_01000001_00010000
    Cog2  hdev_port = 4, hub_port_status = %00000000_00000001_00000011_00000001
    Cog2  hdev_port = 4, #hdev_id = $058F_9254, $0000_0000, $0000_0000, $0000_0000, $077D_0410, hdev_bcd = $0400
    Cog2  hconfig_base = $09, $02, $29, $00, $01, $01, $04, $A0, $32, $09, $04, $00, $00, $02, $03, $00, $00, $05, $09, $21, $00, $01, $00, $01, $22, $4A, $00, $07, $05, $81, $03, $06, $00, $0A, $07, $05, $02, $03, $01, $00, $0A
    Cog2  hdev_intf_idx = 0, hdev_class = $03, hdev_subclass = $00, hdev_protocol = $00
    Cog2  hdev_ep_addr = $0000_0000, (hdev_ep_addr + 1) = $0000_0000, (hdev_ep_addr + 2) = $0000_0000, (hdev_ep_addr + 3) = $0000_0000, (hdev_ep_addr + 4) = $0040_8400, gp_interval = 10, hdev_out_addr = $0001_0400
    Cog2  pb = $711C, pb = $05, $0C, $09, $01, $A1, $01, $05, $09, $19, $01, $29, $01, $15, $00, $25, $01, $75, $01, $95, $01, $81, $02, $75, $07, $95, $01, $81, $01, $05, $01, $09, $33, $15, $81, $25, $7F, $75, $08, $95, $01, $81, $02, $05, $0C, $09, $01, $15, $00, $26, $FF, $00, $75, $08, $95, $04, $81, $02, $09, $01, $75, $08, $95, $01, $91, $02, $09, $01, $75, $08, $95, $08, $B1, $02, $C0
    Cog2  hdev_type = $0000_0009, (hdev_type + 1) = $0000_0006
    Cog2  hdev_port = 4, hub_port_status = %00000000_00000000_00000000_10100011
    
Sign In or Register to comment.