Use the VS1053b chip

From F256 Foenix
Revision as of 11:09, 2 March 2025 by 1BitFeverDreams (talk | contribs) (Big cleanup, mp3 info. better tables)
Jump to navigationJump to search

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