Use the VS1053b chip: Difference between revisions

From F256 Foenix
Jump to navigationJump to search
(enabling the real time midi mode and using it)
(Big cleanup, mp3 info. better tables)
Line 5: Line 5:
You may have to enable its audio output via manipulation of the CODEC. Check the [[Use the CODEC]] page to see how.
You may have to enable its audio output via manipulation of the CODEC. Check the [[Use the CODEC]] page to see how.


=== Using the real time midi mode ===
=== Registers in the FPGA that interface to the VS1053b chip ===
 
To enable the real time midi mode (ie playing notes on a keyboard and having the VS1053b react to data sent to its FIFO buffer), it first has to be enable via the SCI bus of the chip.


{| class="wikitable"
{| class="wikitable"
Line 20: Line 18:
| SCI Data 2 || 0xD703 || Second byte of data
| SCI Data 2 || 0xD703 || Second byte of data
|-
|-
| FIFO Stat 1 || 0xD704 || First byte of status
| FIFO Count 1 || 0xD704 || First byte of the remaining byte count in the FIFO
|-
| FIFO Count 2 || 0xD705 || Second byte of the remaining byte count in the FIFO
|}
 
Here are a handful of addresses to use with the SCI_ADDR register. Definitely check out the datasheet for way more information. [https://github.com/Mu0n/F256MiscGoodies/tree/main/datasheets VS1053b Datasheet] on p.37
 
{| class="wikitable"
! Register Addr !! R/W !! Abbreviation !! Description
|-
| 0x0 || RW || MODE || Mode control
|-
| 0x1 || RW || STATUS || Status of VS1053b
|-
| 0x2 || RW || BASS || Built-in bass/treble control
|-
| 0x3 || RW || CLOCKF|| Clock freq + multiplier
|}
 
=== Boosting the Clock speed of the VS1053b ===
 
After extensive perusing of the real time midi mode, as well as the mp3 playback mode, boosting the clock speed away from the default one at power up is definitely needed.
Here is the Clock frequency changing command that needs to be populated
inside the 0xD702 and 0xD703 registers (SCI_DATA).
 
The following 2 tables are from the datasheet that can be gotten from here: from the [https://github.com/Mu0n/F256MiscGoodies/tree/main/datasheets VS1053b Datasheet] on p.42
{| class="wikitable"
! Bit field !! 15 !! 14 !! 13 !! 12 !! 11 !! 10 !! 9 !! 8 !! 7 !! 6 !! 5 !! 4 !! 3 !! 2 !! 1 !! 0
|-
| Purpose || M3 || M2 || M1 || A1 || A0 || F11 || F9 || F8 || F7 || F6 || F5 || F4 || F3 || F2 || F1 || F0
|-
|-
| FIFO Stat 2 || 0xD705 || Second byte of status
|}
|}
{| class="wikitable"
! Value Name !! Bits !! Description
|-
| SC_MULT || 15:13 || Clock Multiplier
|-
| SC_ADD || 12:11 || Allowed multiplier addition (only usable for WMA and AAC files)
|-
| SC_FREQ || 10:0 || Clock frequency (if set to zero, the default value of 12.288 MHz is used)
|-
|}
Here are the choices for the SC_MULT values, these hex values is the mask of the 2-byte word of SCI_DATA to be used.
{| class="wikitable"
! Choice !! Mask !! CLKI
|-
| 0 || 0x0000 || XTALI
|-
| 1 || 0x2000 || XTALIx2.0
|-
| 2 || 0x4000 || XTALIx2.0
|-
| 3 || 0x6000|| XTALIx2.5
|-
| 4 || 0x8000|| XTALIx3.0
|-
| 5 || 0xA000|| XTALIx3.5
|-
| 6 || 0xC000|| XTALIx4.5
|-
| 7 || 0xE000|| XTALIx5.0
|}
Here's a C piece of code that boosts the clock frequency to 4.5 times its default values.
<pre>
//target the clock register
POKE(VS_SCI_ADDR,0x03);
//aim for 2.5X clock multiplier, no frills
POKE(VS_SCI_DATA,0x00);
POKE(VS_SCI_DATA+1,0xc0);
//trigger the command
POKE(VS_SCI_CTRL,1);
POKE(VS_SCI_CTRL,0);
//check to see if it's done
while (PEEK(VS_SCI_CTRL) & 0x80)
;
</pre>
=== Using the real time midi mode ===
To enable the real time midi mode (ie playing notes on a keyboard and having the VS1053b react to data sent to its FIFO buffer), it first has to be enable via the SCI bus of the chip.


Next, you have to send this plugin data from VLSI, the makers of this chip, which contains some RLE compression. Here it is in C array form:
Next, you have to send this plugin data from VLSI, the makers of this chip, which contains some RLE compression. Here it is in C array form:


<code>const uint16_t plugin[28] = { /* Compressed plugin  for the VS1053b to enable real time midi mode */
<pre>
const uint16_t plugin[28] = { /* Compressed plugin  for the VS1053b to enable real time midi mode */
   0x0007, 0x0001, 0x8050, 0x0006, 0x0014, 0x0030, 0x0715, 0xb080, /*    0 */
   0x0007, 0x0001, 0x8050, 0x0006, 0x0014, 0x0030, 0x0715, 0xb080, /*    0 */
   0x3400, 0x0007, 0x9255, 0x3d00, 0x0024, 0x0030, 0x0295, 0x6890, /*    8 */
   0x3400, 0x0007, 0x9255, 0x3d00, 0x0024, 0x0030, 0x0295, 0x6890, /*    8 */
Line 33: Line 113:
   0x0200, 0x000a, 0x0001, 0x0050
   0x0200, 0x000a, 0x0001, 0x0050
};
};
</code>
</pre>


In order to properly prepare the SCI bus of the chip, this piece of code can be used with the plugin array:
In order to properly prepare the SCI bus of the chip, this piece of code can be used with the plugin array:


<code>void initVS1053MIDI(void) {
<pre>
void initVS1053MIDI(void) {
     uint8_t n;
     uint8_t n;
     uint16_t addr, val, i=0;
     uint16_t addr, val, i=0;
Line 68: Line 149:
   }
   }
}
}
</code>
</pre>


To send midi bytes to the VS1053b, use register 0xDDB1 just like you would use 0xDDA1 for the SAM2695.
To send midi bytes to the VS1053b, use register 0xDDB1 just like you would use 0xDDA1 for the SAM2695.


==== huge discord note infodump that has to be cleaned and understood ====
==== Example C code to do a playback ====
normally, let's assume we begin with a playback of a MP3, you read the file and you will need to dump like a 2K in before it tells you to stop. But, the FIFO inside the FPGA is also 2K, so in theory before things get ugly, you can upload in the MP3 playback system 2K+2K bytes, which ought to be enough to get it going. Now, if you monitor the FPGA FIFO info when empty you can put more in. Normally, after the VS1053 queue is full, it only need 32 bytes at the time and the FPGA takes care of that. Now, basically need to make sure the FPGA FIFO is topped off. Now, when come to end, you will need to fill extra bytes with 00 to empty the VS1053 Fifo and then the tune will end.
 
Now, the tricky parts is when one needs to stop the sound, there is also a process of filling the FIFO with 00 to cleanse the MP3 pipe. The specs talks about that.
 
This is as far as I went, now, I believe that if someone wants to play oggs and shit like that the controller need to be updated with new code since there are some issues in the latest tape out of the chip because remember people this is a DSP that actually can be programmed. There is actually an app note on how to use it to come out with Histogramic data (VUE bar app, with small LCD? anybody picking up on that? LOL) for one's pleasure
 
This is in a nutshell the process to playback stuff.
Here is the Register File Details for the VS1053 interface:
 
$D700..$D707
 
    case( CPU_A_i[2:0] )
        3'b000: begin CPU_D_o = {Busy, VS1053B_Registers[0][6:0]}; end
        3'b001: begin CPU_D_o = VS1053B_Registers[1]; end
        3'b010: begin CPU_D_o = VS1053B_Command_Read[7:0]; end
        3'b011: begin CPU_D_o = VS1053B_Command_Read[15:8]; end
        3'b100: begin CPU_D_o = Data_FIFO_Count[7:0]; end
        3'b101: begin CPU_D_o = {VS1053B_FIFO_Empty, VS1053B_FIFO_Full, 3'b000, Data_FIFO_Count[10:8]}; end
        3'b110: begin CPU_D_o = 8'h00; end
        3'b111: begin CPU_D_o = 8'h00; end
    endcase
 
'''VS1053B_Registers[0] - Bit Fields: ($D700..$D703)'''
 
[0] 1: Start Transfer
 
[1] 1: Read Register, 0: Write Register
 
[7] 1: SPI Transfer in Progress [Busy]
 
'''VS1053B_Registers[1]'''
 
[3:0]  Register to Address
 
'''VS1053B_Registers[2] Data Access Low'''
 
'''VS1053B_Registers[3] Data Access Hi'''
 
There are 2 Accessible Area for the VS1053
The Control/Register Section with 16bits wide Data and 16 Addresses (See specs)
The Other is the Data(Stream) Port, there is no Address and it is 8bits
 
When one wants to control the chip, one need to setup the address and data (for write) and address for read, one needs to setup the direction and when everything is ready, one needs to trigger the transaction.
When one needs to write file data for playback, one needs only to write data to FIFO port @ $D704 and monitor FIFO @ $D705/$D706
Here is a piece of code, not the best, prolly not the most kosher either, but that ought to get you started.
I am simply playing back a very tiny sound effect (small enough to fit in the memory so not much to play back)
 
MP3_Playing:
 
                lda #$42
                sta $d702  ; Set the Stream mode
                lda #$48
                sta $d703
                lda #$21    ; Start Transaction
                sta $d700
                lda #$20    ; Return to Zero
                sta $d700
 
;                lda #$00
;                sta $d702
;                sta $d703
;                lda #$23    ; Go read the Command Register
;                sta $d700
;                lda $d702
;                lda $d703
 
                ldx #$00
VS_Pause:               
                inx
                cpx #$80
                bne VS_Pause
 
                lda #$80
                sta $00    ; Let's enable the MMU Edit, keep the MMU 00 in play
                lda #$08    ; Bring about the first 8K
                sta $0D    ; It starts @ $08 == page 0
 
                setaxl
 
                ldx #$0000
                ldy #$0000
; Go Fill the FIFO
                setas
MP3_Fill_FIFO:  ;lda @l MP3_Music,x
                lda $A000,x
                sta VS1053_STREAM_DATA      ; THere is 2K FIFO
                inx
                iny
                cpy #$0800                  ; Fill 2K
                bne MP3_Fill_FIFO
 
MP3_Fill_FIFO_Wait:
                ldy #$0000
                lda VS1053_FIFO_COUNT_HI    ; Load High Portion is monitor for
                and #$80
                cmp #$80
                bne MP3_Fill_FIFO_Wait
 
                cpx #$2000
                bne MP3_Fill_FIFO
                ldx #$0000


                lda $0D
Check out this source code on Mu0n's F256KSimpleCdoodles repo:
                inc A
                sta $0D
                cmp #$0c
                bne MP3_Fill_FIFO


                lda #$05
https://github.com/Mu0n/F256KsimpleCdoodles/blob/main/mp3/src/mp3.c
                sta $0D
MP3_We_are_Done:
                setaxs
                rts

Revision as of 11:09, 2 March 2025

How to use the VS1053b chip

This chip is only present in the F256K2 or the F256Jr2. It isn't available on the earlier F256K nor the F256Jr.

You may have to enable its audio output via manipulation of the CODEC. Check the Use the CODEC page to see how.

Registers in the FPGA that interface to the VS1053b chip

Register Name Address Purpose
SCI Control 0xD700 Control the flow of information
SCI Address 0xD701 Select to which VS1053b address to write to
SCI Data 1 0xD702 First byte of data
SCI Data 2 0xD703 Second byte of data
FIFO Count 1 0xD704 First byte of the remaining byte count in the FIFO
FIFO Count 2 0xD705 Second byte of the remaining byte count in the FIFO

Here are a handful of addresses to use with the SCI_ADDR register. Definitely check out the datasheet for way more information. VS1053b Datasheet on p.37

Register Addr R/W Abbreviation Description
0x0 RW MODE Mode control
0x1 RW STATUS Status of VS1053b
0x2 RW BASS Built-in bass/treble control
0x3 RW CLOCKF Clock freq + multiplier

Boosting the Clock speed of the VS1053b

After extensive perusing of the real time midi mode, as well as the mp3 playback mode, boosting the clock speed away from the default one at power up is definitely needed. Here is the Clock frequency changing command that needs to be populated inside the 0xD702 and 0xD703 registers (SCI_DATA).

The following 2 tables are from the datasheet that can be gotten from here: from the VS1053b Datasheet on p.42

Bit field 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Purpose M3 M2 M1 A1 A0 F11 F9 F8 F7 F6 F5 F4 F3 F2 F1 F0
Value Name Bits Description
SC_MULT 15:13 Clock Multiplier
SC_ADD 12:11 Allowed multiplier addition (only usable for WMA and AAC files)
SC_FREQ 10:0 Clock frequency (if set to zero, the default value of 12.288 MHz is used)

Here are the choices for the SC_MULT values, these hex values is the mask of the 2-byte word of SCI_DATA to be used.

Choice Mask CLKI
0 0x0000 XTALI
1 0x2000 XTALIx2.0
2 0x4000 XTALIx2.0
3 0x6000 XTALIx2.5
4 0x8000 XTALIx3.0
5 0xA000 XTALIx3.5
6 0xC000 XTALIx4.5
7 0xE000 XTALIx5.0

Here's a C piece of code that boosts the clock frequency to 4.5 times its default values.

//target the clock register
POKE(VS_SCI_ADDR,0x03);
//aim for 2.5X clock multiplier, no frills
POKE(VS_SCI_DATA,0x00);
POKE(VS_SCI_DATA+1,0xc0);
//trigger the command
POKE(VS_SCI_CTRL,1);
POKE(VS_SCI_CTRL,0);
//check to see if it's done
	while (PEEK(VS_SCI_CTRL) & 0x80)
		;

Using the real time midi mode

To enable the real time midi mode (ie playing notes on a keyboard and having the VS1053b react to data sent to its FIFO buffer), it first has to be enable via the SCI bus of the chip.

Next, you have to send this plugin data from VLSI, the makers of this chip, which contains some RLE compression. Here it is in C array form:

const uint16_t plugin[28] = { /* Compressed plugin  for the VS1053b to enable real time midi mode */
  0x0007, 0x0001, 0x8050, 0x0006, 0x0014, 0x0030, 0x0715, 0xb080, /*    0 */
  0x3400, 0x0007, 0x9255, 0x3d00, 0x0024, 0x0030, 0x0295, 0x6890, /*    8 */
  0x3400, 0x0030, 0x0495, 0x3d00, 0x0024, 0x2908, 0x4d40, 0x0030, /*   10 */
  0x0200, 0x000a, 0x0001, 0x0050
};

In order to properly prepare the SCI bus of the chip, this piece of code can be used with the plugin array:

void initVS1053MIDI(void) {
    uint8_t n;
    uint16_t addr, val, i=0;
  while (i<sizeof(plugin)/sizeof(plugin[0])) {
    addr = plugin[i++];
    n = plugin[i++];
    if (n & 0x8000) { /* RLE run, replicate n samples */
      n &= 0x7FFF;
      val = plugin[i++];
      while (n--) {
        //WriteVS10xxRegister(addr, val);
        POKE(VS_SCI_ADDR,addr);
        POKEW(VS_SCI_DATA,val);
        POKE(VS_SCI_CTRL,1);
        POKE(VS_SCI_CTRL,0);
		while (PEEK(VS_SCI_CTRL) & 0x80);
      }
    } else {           /* Copy run, copy n samples */
      while (n--) {
        val = plugin[i++];
        //WriteVS10xxRegister(addr, val);
        POKE(VS_SCI_ADDR,addr);
        POKEW(VS_SCI_DATA,val);
        POKE(VS_SCI_CTRL,1);
        POKE(VS_SCI_CTRL,0);

		while (PEEK(VS_SCI_CTRL) & 0x80);
      }
    }
  }
}

To send midi bytes to the VS1053b, use register 0xDDB1 just like you would use 0xDDA1 for the SAM2695.

Example C code to do a playback

Check out this source code on Mu0n's F256KSimpleCdoodles repo:

https://github.com/Mu0n/F256KsimpleCdoodles/blob/main/mp3/src/mp3.c