News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_PulkoMandy

Albireo - USB/SD/Serial interface for CPC

Started by PulkoMandy, 15:54, 18 October 15

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

PulkoMandy

I still don't understand what you mean by "the bits". There are command numbers, which are just, well, arbitrary numbers decided by the chip manufacturer. The page lists the ones used.


There is a status register with a single bit to check, which is documented in the page (it is bit 7).


There are commands which take parameters, the parameters are documented.


And there is the report from the mouse, which is documented in that page: bit 0 is button 0, bit 1 is button 1, and so on, and the next two bytes are the signed offset for X and Y coordinates since the last report was read.


I still don't see what else I could add to this page. And translating the code from BASIC to assembler wouldn't be very hard? I can surely do it, but I don't think assembler is a better way to document these things?

GUNHED

#426
Example: The status port has 8 bits. What is the meaning of every single bit? Not only the status bit. Stuff like that.  :)


Quote from: PulkoMandy on 14:27, 18 February 20
... And translating the code from BASIC to assembler wouldn't be very hard? I can surely do it, but I don't think assembler is a better way to document these things?
Then please do provide an working Z80 example code to read the mouse! If you can. And yes, I know it works for SymbOS. But that's not the point. It doesn't work for me, because at some point I have to wait for something and I don't know what.
http://futureos.de --> Get the revolutionary FutureOS (Update: 2023.11.30)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2021.12.26)

pelrun

If PulkoMandy's documentation is insufficient, then one can always go direct to the CH376 datasheet which is readily available and explains every register and return value in detail.

PulkoMandy

There is only ever one thing to wait for: bit 7 of the status register to be cleared after sending a command with its parameters. The code for this is in the documentation:



WAIT:
v = INP(CMD)
IF v > 127 THEN GOTO WAIT



Do I really need to convert it to ASM for you? Looks simple enough to me.
When do you need to wait? Right after any command where the result is documented as "interrupt". For a mouse driver these commands will be documented here:
https://pulkomandy.github.io/shinra.github.io/CH376_ext.html (translated from part 2 of the datasheet which is only available in chinese).


In the BASIC code this is at "GOSUB 510" and very easy to see after each command that needs it.


I am not aware of any specific use for the other bits in the status register. They are not used in any of my examples and not referenced in the documentation of the chip as far as I can tell. So I assume there is nothing useful there.

GUNHED

Quote from: pelrun on 02:41, 20 February 20
If PulkoMandy's documentation is insufficient, then one can always go direct to the CH376 datasheet which is readily available and explains every register and return value in detail.

See, his docs are better of course. The CH376 docs are minimal though. So nice idea, but bad luck.
http://futureos.de --> Get the revolutionary FutureOS (Update: 2023.11.30)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2021.12.26)

GUNHED

Quote from: PulkoMandy on 09:02, 20 February 20
There is only ever one thing to wait for: bit 7 of the status register to be cleared after sending a command with its parameters. The code for this is in the documentation:



WAIT:
v = INP(CMD)
IF v > 127 THEN GOTO WAIT



Do I really need to convert it to ASM for you? Looks simple enough to me.
When do you need to wait? Right after any command where the result is documented as "interrupt". For a mouse driver these commands will be documented here:
https://pulkomandy.github.io/shinra.github.io/CH376_ext.html (translated from part 2 of the datasheet which is only available in chinese).


In the BASIC code this is at "GOSUB 510" and very easy to see after each command that needs it.


I am not aware of any specific use for the other bits in the status register. They are not used in any of my examples and not referenced in the documentation of the chip as far as I can tell. So I assume there is nothing useful there.


You don't have that running. Because it doesn't work. Proove me wrong.
http://futureos.de --> Get the revolutionary FutureOS (Update: 2023.11.30)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2021.12.26)

PulkoMandy

I can prove you wrong easily: the driver in SymbOS exists and work for several owners of Albireo. I helped Prodatron debug it and I wrote the page while doing that. But he asked me precise questions about my BASIC code (which indeed was incorrect at the time, it was missing the TOKEN variable to generate alternating DATA0 and DATA1 frames), so I could help him and investigate the precise problems he was getting.


So, what do you think? That the SymbOS driver uses magic fairy dust and secret undocumented registers?

SOS

Quote from: PulkoMandy on 16:43, 16 February 20
If you need more than that, I can improve it, but I need specific questions, because it seems quite complete to me.
An "Read-An-LBA-Sector" Function in Basic, which works on every try (100%-Success-Rate)  :D

GUNHED

Quote from: PulkoMandy on 11:11, 20 February 20
I can prove you wrong easily: the driver in SymbOS exists and work for several owners of Albireo. I helped Prodatron debug it and I wrote the page while doing that. But he asked me precise questions about my BASIC code (which indeed was incorrect at the time, it was missing the TOKEN variable to generate alternating DATA0 and DATA1 frames), so I could help him and investigate the precise problems he was getting.


So, what do you think? That the SymbOS driver uses magic fairy dust and secret undocumented registers?

Just provide example code if you can.
http://futureos.de --> Get the revolutionary FutureOS (Update: 2023.11.30)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2021.12.26)

pelrun

#434
Quote from: GUNHED on 09:11, 20 February 20
See, his docs are better of course. The CH376 docs are minimal though. So nice idea, but bad luck.
Documenting every register and value is "minimal"? While WCH has produced some woefully abbreviated datasheets for other chips, the one for the CH376 is fairly decent.

So basically the only answer you'll accept is "somebody else should do all the work for me". Since I'm certainly not going to, I'll bow out here.

GUNHED

#435
Sure.  :)
http://futureos.de --> Get the revolutionary FutureOS (Update: 2023.11.30)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2021.12.26)

Targhan

Ok, I've fiddled a bit with the mouse support, and managed to make it work in assembler. @GUNHED You must have made a small mistake somewhere because I had no real trouble translating the Basic source to ASM.
I'll show the sources when they are more tidy, but if anyone wants them now, I can upload them without problem.

Now I have to work on two things. If @PulkoMandy can give his inputs, it may save me some time.

  • If my cursor is in mode 0, the speed is quite fast. I wonder what kind of calibration would be the most relevant. Simply give a ratio the user (/4, /2, 0, *2, *4 is maybe enough?)?
  • The program waits endlessly while the mouse doesn't move. This is of course not viable for real-time. In the Basic source, there is reference to "the HID set idle command". So I should search about that. I naively attempted to use a simple timeout at the loop, but it doesn't work well (small motions are skipped), plus it each more CPU that it should.

Anyway, this is quite promising :).
Targhan/Arkos

Arkos Tracker 2.0.1 now released! - Follow the news on Twitter!
Disark - A cross-platform Z80 disassembler/source converter
FDC Tool 1.1 - Read Amsdos files without the system

Imperial Mahjong
Orion Prime

Targhan

Well, I'm bit stuck. I'm afraid of that part of the documentation isn't very clear, unless you delve deep into it. If I try to "do something else" if there is no data (INP(CMD) > 127), no data is actually retrieved (missed, probably). If I add a "timeout loop" of 200, it's better but small motions are ignored, and it takes way too CPU to my taste.

@PulkoMandy Could you elaborate more about how to do that efficiently? I can show my sources without problem if you need. Thanks.
Targhan/Arkos

Arkos Tracker 2.0.1 now released! - Follow the news on Twitter!
Disark - A cross-platform Z80 disassembler/source converter
FDC Tool 1.1 - Read Amsdos files without the system

Imperial Mahjong
Orion Prime

PulkoMandy

QuoteIf I try to "do something else" if there is no data (INP(CMD) > 127), no data is actually retrieved (missed, probably).

That's how it should work, normally. The CH376 chip in Albireo will take care of polling the mouse for you and this status bit will change when it's ready to send you the data. And the mouse itself should have no problems with that, it will accumulate data until the next read. I think this is what Prodatron did in SymbOS and it worked fine. There is a problem if you have a very high resolution gamer mouse, because it will report a lot of movement each time and the X and Y deltas are only 8 bit each, so that can overflow quickly if you don't read often enough. But with a standard cheap mouse it should be fine.

Maybe I can look more closely at what may be missing if you show me your code :)

You have 3 other options if that doesn't work:

1) Using HID "Set Idle". This allows to configure the mouse to send a report every now and then even if nothing has changed (no mouse movement, no clicks). I never tried this, so I need to dig a little deeper in USB details to tell you exactly what needs to be done. It will result in some extra processing as you will get an empty report.

2) Disabling the automatic retry on the Albireo side. You can make it so the Albireo doesn't poll the mouse, just asks once and tells you "nothing to read".  For this you use the SET_RETRY command:

OUT CMD,&0B
OUT DAT,&25
OUT DAT,&0F ' or &00, I'm not sure which works best

Badly translated from chinese docs about this command here: https://pulkomandy.github.io/shinra.github.io/CH376_ext.html

In that case, if the mouse has nothing to send you, Albireo will just fail the read operation instead of waiting. And you can try again later.

3) Use interrupts. It's possible to configure the Albireo to trigger an interrupt when data is ready to read. Let me know if that's suitable for you, I didn't use it a lot yet because usually, there are other things going on and you don't want interrupts happening in the midle of your time-critical code.

GUNHED

#439
Quote from: Targhan on 23:43, 14 April 20
Ok, I've fiddled a bit with the mouse support, and managed to make it work in assembler. @GUNHED You must have made a small mistake somewhere because I had no real trouble translating the Basic source to ASM.

Thanks for to information. Maybe my board is too old or the docs changed in a some detail. I may come back tho this project some day and take a look at my code again.

Supporting the Albireo is for sure a good idea since a decent number of units was shipped.

Thanks for the update again!  :) :) :)
http://futureos.de --> Get the revolutionary FutureOS (Update: 2023.11.30)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2021.12.26)

Targhan

Ok, I've uploaded my (raw) sources here:
https://bitbucket.org/JulienNevo/cpcmouse/src/master/

Simply plug your mouse in the Albireo. Move the mouse at first to make the cursor appear.

This is working, but like I said, waiting for the data takes all the CPU. @PulkoMandy : In the albireo.asm/AlbireoWaitForCommandEndWithTimeOut method, if I set a timeout for the loop (uncomment it), you'll see that the mouse does not work fine anymore, and sometimes gets absolutely stuck.

Thanks for your help and advice!
Targhan/Arkos

Arkos Tracker 2.0.1 now released! - Follow the news on Twitter!
Disark - A cross-platform Z80 disassembler/source converter
FDC Tool 1.1 - Read Amsdos files without the system

Imperial Mahjong
Orion Prime

PulkoMandy

#441


Here's how I would try to do this:

At the end of the initialization (line 56 in your code):



        ; Submit our first request to the mouse to get things started


        ;Basic 230
        ld a,#4e : call AlbiCmd
       
        ;Basic 250
  ld a,0
        call AlbiDat
        ld a,(Token + 1)
        xor #80
        ld (Token + 1),a
       
        ;Basic 300
        ld a,#19 : call AlbiDat


And the mouse read function:


ManageMouse:
        ;Basic 330
        call Border26
        ; Don't wait here, just test the bit to see if data is ready


        ld bc,ACE_CMD
        in a,(c)
        rla
        ; Nothing to do? cool!
        jp c,Border0 ; Restore border and return


        ; If we get here, there is data waiting for us, so we need to read it
        ;Basic 340
        ld a,#27 : call AlbiCmd
       
        ;Basic 370
        call AliReadDat
        or a
        jp z,Schedule_transfer ; In case we got no data, ask again (this is unlikely, I think this check can be removed)
       
        ;Reads Buttons.
        call AliReadDat
        ;Reads deltaX.
        call AliReadDat
        call ByteToWord
        call CursorMoveHorizontally
        ;Reads deltaY.
        call AliReadDat
        call ByteToWord
        call CursorMoveVertically
        ;Reads Wheel.
        call AliReadDat
       
        ; And schedule a new data transfer for the next VBL
Schedule_transfer:
        ;Basic 230
        ld a,#4e : call AlbiCmd
       
        ;Basic 250
Tokenld a,0
        call AlbiDat
        ld a,(Token + 1)
        xor #80
        ld (Token + 1),a
       
        ;Basic 300
        ld a,#19 : call AlbiDat
       
        ; That's all for this time!
        jp Border0

PulkoMandy

I forgot to say something about the mouse speed. In my test, I worked with BASIC graphics coordinate (640x400 "virtual" screen) and the speed seemed fine with a standard mouse. So, one ASR for Y and two for X and you should be fine for mode 0 pixels (maybe keep the coordinates in 640x400 and downconvert only when displaying the cursor, this way fine movements will be easier). But maybe extra division is needed for high-DPI gamer mouses.

Targhan

THIS IS WORKING!! Thanks a lot. I've pushed your modifications. You can link it from your webpage if you want, if people want to have a working example (I wouldn't have guessed it would be so complicated to use the mouse with the Albireo, I have to be honest about it), this repo will be kept and will evolve with other interfaces.

QuoteSo, one ASR for Y and two for X
What do you mean by ASR? I don't know the term.
Targhan/Arkos

Arkos Tracker 2.0.1 now released! - Follow the news on Twitter!
Disark - A cross-platform Z80 disassembler/source converter
FDC Tool 1.1 - Read Amsdos files without the system

Imperial Mahjong
Orion Prime

PulkoMandy

Oops, I don't do enough z80 coding these days, it seems. I mean SRA, of course (to divide by two).


Well, the code is not that complex once written, but yes, it's a bit difficult to gather all the infos from my docs, the ch376 hardware docs, and usb/hid specs...
Albireo is just an usb interface and anything above that (except for mass storage/usb sticks) is all done by the z80. This gives a lot of flexibility (want to connect a printer? An usb midi adapter?) but it means some extra work to set things up.


I'll update the docs to link to this (maybe an annotated version of the listing to try to more clearly explain what is done at each step).


Thanks for your efforts investigating this!

Targhan

I've added Symbiface 2 now :).
As for Albireo, I've added the buttons management, and it seems there's something wrong by your doc. Bit 0 does NOT seem to be left button, but bit 1. Bit 2 is right button.
Targhan/Arkos

Arkos Tracker 2.0.1 now released! - Follow the news on Twitter!
Disark - A cross-platform Z80 disassembler/source converter
FDC Tool 1.1 - Read Amsdos files without the system

Imperial Mahjong
Orion Prime

PulkoMandy

#446
Annotated version of the asm source: https://github.com/pulkomandy/shinra.github.io/blob/master/albireomouse.asm


As for the buttons, this is strange, but it depends on your mouse. USB mouses can have more buttons and wheels so the report format can change. If you don't plan to handle the mousewheel, you could configure the mouse to "boot mode" where you're sure it has only 2 buttons and X/Y movement. This is compatible with all mouses, but the loss of mousewheel can be annoying (Prodatron did not use this mode in SymbOS because he wanted the mouse wheel, for example).


Setting up boot mode is documented in the Albireo webpage (things to do at init before sending the first "read" command):


Quote
So, let's first tell the CH376 we want to perform a data transfer:

OUT CMD,&2C ' For WRITE commands, you first send the data to Albireo, then trigger the transfer. It's symetrical to READ. 


And let's tell how many bytes there is:

OUT DAT,8 


The CH376 is now waiting for our 8 data bytes. We will now write the HID command to enter boot mode:


OUT DAT,&21 ' (because all HID commands start with &21)
OUT DAT,&0B ' (this is the HID SET PROTOCOL command)
OUT DAT,&00:OUT DAT,&00 ' (protocol number 0 - on 16 bits - is the BOOT protocol)
OUT DAT,&00:OUT DAT,&00:OUT DAT,&00:OUT DAT,&00


All USB commands must also include an index and a data length, 16 bits each. Both are set to 0 for the SET PROTOCOL command.
The bytes are now loaded in the CH376. We can request it to perform the USB transaction:

OUT CMD,&4E:OUT DAT,&80:OUT DAT,&0D ' The &80 here must match the "token" sequence as for the read commands


Targhan

Nice doc!

As for the buttons, it's a very simple USB mouse with wheel. However, Symbiface 2 (emulated with Winape) seems to handle it well (first button = left, second = right). So for now I'll stick with my implementation. Don't tell me I also have to code a configuration screen to define which button is which... Though I should probably have to, because there is still the mouse X/Y ratio to handle... Arg.
Targhan/Arkos

Arkos Tracker 2.0.1 now released! - Follow the news on Twitter!
Disark - A cross-platform Z80 disassembler/source converter
FDC Tool 1.1 - Read Amsdos files without the system

Imperial Mahjong
Orion Prime

PulkoMandy

Quote from: Targhan on 12:16, 16 April 20
Don't tell me I also have to code a configuration screen to define which button is which...


Your left-handed users will thank you ;)

Targhan

Targhan/Arkos

Arkos Tracker 2.0.1 now released! - Follow the news on Twitter!
Disark - A cross-platform Z80 disassembler/source converter
FDC Tool 1.1 - Read Amsdos files without the system

Imperial Mahjong
Orion Prime

Powered by SMFPacks Menu Editor Mod