Author Topic: Albireo - USB/SD/Serial interface for CPC  (Read 67401 times)

0 Members and 1 Guest are viewing this topic.

Offline PulkoMandy

  • 464 Plus
  • *****
  • Posts: 372
  • Country: fr
  • Liked: 294
  • Likes Given: 1
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #425 on: 15:27, 18 February 20 »
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?

Offline GUNHED

  • 6128 Plus
  • ******
  • Posts: 1.865
  • Country: de
  • Reincarnation of TFM
    • FutureOS - The quickest OS for the CPC and Plus
  • Liked: 842
  • Likes Given: 2096
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #426 on: 22:47, 19 February 20 »
Example: The status port has 8 bits. What is the meaning of every single bit? Not only the status bit. Stuff like that.  :)


... 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.
« Last Edit: 22:51, 19 February 20 by GUNHED »
http://futureos.de --> Get the revolutionary FutureOS (Recent update: 2019.08.07)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2019.08.14)

Offline pelrun

  • Supporter
  • 6128 Plus
  • *
  • Posts: 614
  • Country: au
    • index.php?action=treasury
  • Liked: 307
  • Likes Given: 187
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #427 on: 03: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.

Offline PulkoMandy

  • 464 Plus
  • *****
  • Posts: 372
  • Country: fr
  • Liked: 294
  • Likes Given: 1
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #428 on: 10: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:


Code: [Select]
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.

Offline GUNHED

  • 6128 Plus
  • ******
  • Posts: 1.865
  • Country: de
  • Reincarnation of TFM
    • FutureOS - The quickest OS for the CPC and Plus
  • Liked: 842
  • Likes Given: 2096
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #429 on: 10:11, 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 (Recent update: 2019.08.07)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2019.08.14)

Offline GUNHED

  • 6128 Plus
  • ******
  • Posts: 1.865
  • Country: de
  • Reincarnation of TFM
    • FutureOS - The quickest OS for the CPC and Plus
  • Liked: 842
  • Likes Given: 2096
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #430 on: 10:13, 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:


Code: [Select]
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 (Recent update: 2019.08.07)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2019.08.14)

Offline PulkoMandy

  • 464 Plus
  • *****
  • Posts: 372
  • Country: fr
  • Liked: 294
  • Likes Given: 1
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #431 on: 12: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?

Offline SOS

  • Supporter
  • 464 Plus
  • *
  • Posts: 361
  • Country: de
  • Identity lost
    • index.php?action=treasury
  • Liked: 233
  • Likes Given: 256
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #432 on: 12:31, 20 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

Offline GUNHED

  • 6128 Plus
  • ******
  • Posts: 1.865
  • Country: de
  • Reincarnation of TFM
    • FutureOS - The quickest OS for the CPC and Plus
  • Liked: 842
  • Likes Given: 2096
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #433 on: 13:45, 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 (Recent update: 2019.08.07)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2019.08.14)

Offline pelrun

  • Supporter
  • 6128 Plus
  • *
  • Posts: 614
  • Country: au
    • index.php?action=treasury
  • Liked: 307
  • Likes Given: 187
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #434 on: 01:52, 21 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.
« Last Edit: 01:56, 21 February 20 by pelrun »

Offline GUNHED

  • 6128 Plus
  • ******
  • Posts: 1.865
  • Country: de
  • Reincarnation of TFM
    • FutureOS - The quickest OS for the CPC and Plus
  • Liked: 842
  • Likes Given: 2096
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #435 on: 20:41, 21 February 20 »
Sure.  :)
« Last Edit: 02:07, 22 February 20 by GUNHED »
http://futureos.de --> Get the revolutionary FutureOS (Recent update: 2019.08.07)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2019.08.14)

Offline Targhan

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.119
  • Country: fr
  • Liked: 1051
  • Likes Given: 152
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #436 on: 01:43, 15 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.
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 - alpha 9 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

Offline Targhan

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.119
  • Country: fr
  • Liked: 1051
  • Likes Given: 152
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #437 on: 02:02, 15 April 20 »
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 - alpha 9 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

Offline PulkoMandy

  • 464 Plus
  • *****
  • Posts: 372
  • Country: fr
  • Liked: 294
  • Likes Given: 1
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #438 on: 09:50, 15 April 20 »
Quote
If 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.

Offline GUNHED

  • 6128 Plus
  • ******
  • Posts: 1.865
  • Country: de
  • Reincarnation of TFM
    • FutureOS - The quickest OS for the CPC and Plus
  • Liked: 842
  • Likes Given: 2096
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #439 on: 14:22, 15 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!  :) :) :)
« Last Edit: 14:27, 15 April 20 by GUNHED »
http://futureos.de --> Get the revolutionary FutureOS (Recent update: 2019.08.07)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2019.08.14)

Offline Targhan

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.119
  • Country: fr
  • Liked: 1051
  • Likes Given: 152
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #440 on: 14:42, 15 April 20 »
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 - alpha 9 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

Offline PulkoMandy

  • 464 Plus
  • *****
  • Posts: 372
  • Country: fr
  • Liked: 294
  • Likes Given: 1
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #441 on: 15:17, 15 April 20 »


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
« Last Edit: 15:45, 15 April 20 by PulkoMandy »

Offline PulkoMandy

  • 464 Plus
  • *****
  • Posts: 372
  • Country: fr
  • Liked: 294
  • Likes Given: 1
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #442 on: 22:22, 15 April 20 »
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.

Offline Targhan

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.119
  • Country: fr
  • Liked: 1051
  • Likes Given: 152
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #443 on: 23:38, 15 April 20 »
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.

Quote
So, 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 - alpha 9 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

Offline PulkoMandy

  • 464 Plus
  • *****
  • Posts: 372
  • Country: fr
  • Liked: 294
  • Likes Given: 1
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #444 on: 00:15, 16 April 20 »
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!

Offline Targhan

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.119
  • Country: fr
  • Liked: 1051
  • Likes Given: 152
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #445 on: 01:33, 16 April 20 »
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 - alpha 9 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

Offline PulkoMandy

  • 464 Plus
  • *****
  • Posts: 372
  • Country: fr
  • Liked: 294
  • Likes Given: 1
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #446 on: 14:01, 16 April 20 »
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

« Last Edit: 14:02, 16 April 20 by PulkoMandy »

Offline Targhan

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.119
  • Country: fr
  • Liked: 1051
  • Likes Given: 152
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #447 on: 14:16, 16 April 20 »
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 - alpha 9 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

Offline PulkoMandy

  • 464 Plus
  • *****
  • Posts: 372
  • Country: fr
  • Liked: 294
  • Likes Given: 1
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #448 on: 14:27, 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 ;)

Offline Targhan

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.119
  • Country: fr
  • Liked: 1051
  • Likes Given: 152
Re: Albireo - USB/SD/Serial interface for CPC
« Reply #449 on: 15:19, 16 April 20 »
Very true...
Targhan/Arkos

Arkos Tracker 2 - alpha 9 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