SCI_WRITE .plg plugin to VS1053b takes too long

Writing software for systems that use VLSI Solution's devices as slave codecs to a host microcontroller.
Post Reply
nomnom-ray
User
Posts: 3
Joined: Sat 2022-01-08 8:33

SCI_WRITE .plg plugin to VS1053b takes too long

Post by nomnom-ray »

Hi,

Related hardware (from Adafruit):
  • VS1053 Codec + MicroSD Breakout - MP3/WAV/MIDI/OGG Play + Record - v4
  • Adafruit Feather M0 (a SAMD21 microcontroller like the Arduino zero)
Goal:
  • record voice in ogg format using VS1053; controlled by feather M0.
What's working:
  • clear audio recorded and stored to the SD card (supported by the VS1053 shield from adafruit)
What's the problem:
  • the plugin for the VS1053 to record in an ogg format takes 3 seconds to load into VS1053b's RAM
  • Context: Using the micros() function, I'm able to calculate that the SCI_WRITE() function provided by Adafruit takes 160us. The SCI_WRITE() is invoked ~21000 times by a function similar to an VLSI's example code for loading the plugin (from "VS1053b / VS1063a WAV PCM Recorder Application"). 160us*21000=3.3seconds
Questions:
1. Is there anyway to reduce the load time to less than 0.5s?
2. If not, does the VS1063 need to load a plugin for recording ogg? I think it has ogg format in ROM, but I'm not sure.


Thank you,
Ray
User avatar
pasi
VLSI Staff
Posts: 1848
Joined: Thu 2010-07-15 16:04

Re: SCI_WRITE .plg plugin to VS1053b takes too long

Post by pasi »

Sounds like there are extra and unnecessary waits in their implementation of SCI_WRITE(), or the SPI speed is just very slow (200kHz on average). Or maybe it doesn't use SPI hardware?

The vs1053b doesn't include Ogg Vorbis encoding in ROM, so the encoding application needs to be completely loaded.

1. You would need to dig into the SCI_WRITE implementation and see why it's so slow. If it's using SPI, what SPI clock speed have you configured?

2. Yes, vs1053a could manage without a patch for Ogg Vorbis encoding (if you hardware reset between encodings), but I would still recommend the vs1063a-encpatches patch version to fix a few bugs and give a few extra features for the encoding mode.
Visit https://www.facebook.com/VLSISolution VLSI Solution on Facebook
nomnom-ray
User
Posts: 3
Joined: Sat 2022-01-08 8:33

Re: SCI_WRITE .plg plugin to VS1053b takes too long

Post by nomnom-ray »

1. You would need to dig into the SCI_WRITE implementation and see why it's so slow. If it's using SPI, what SPI clock speed have you configured?
I haven't done oscilloscope analysis on the signal yet, but at least Adafruit's code for sciWrite() looks to match the requirement from VS1053b's datasheet. Adafruit's code is below (unmodified):

Code: Select all

void Adafruit_VS1053::sciWrite(uint8_t addr, uint16_t data) {
#ifdef SPI_HAS_TRANSACTION
  if (useHardwareSPI)
    SPI.beginTransaction(VS1053_CONTROL_SPI_SETTING);
#endif
  digitalWrite(_cs, LOW);
  
  spiwrite(VS1053_SCI_WRITE);
  spiwrite(addr);
  spiwrite(data >> 8);
  spiwrite(data & 0xFF);
  digitalWrite(_cs, HIGH);
#ifdef SPI_HAS_TRANSACTION
  if (useHardwareSPI)
    SPI.endTransaction();
#endif
}
Using arduino's micro() command, each spiwrite() call takes 35us, which is ~30KHz. Below is Adafruit's spiwrite() code unmodified. It uses Arduino's SPI.transfer() function. I've checked that useHardwareSPI is true.

Code: Select all

void Adafruit_VS1053::spiwrite(uint8_t *c, uint16_t num) {
  // MSB first, clock low when inactive (CPOL 0), data valid on leading edge
  // (CPHA 0) Make sure clock starts low

  if (useHardwareSPI) {

    //#if defined(ESP32)  // optimized
    //  SPI.writeBytes(c, num);
    //  return;
    //#endif

    while (num--) {
      SPI.transfer(c[0]);
      c++;
    }

  } else {
    while (num--) {
      for (int8_t i = 7; i >= 0; i--) {
        *clkportreg &= ~clkpin;
        if (c[0] & (1 << i)) {
          *mosiportreg |= mosipin;
        } else {
          *mosiportreg &= ~mosipin;
        }
        *clkportreg |= clkpin;
      }
      *clkportreg &= ~clkpin; // Make sure clock ends low

      c++;
    }
  }
}
Adafruit's VS1053 library uses 8MHz for data and 250KHz for control on the SPI. Code below:

Code: Select all

#define VS1053_CONTROL_SPI_SETTING                                             \
  SPISettings(250000, MSBFIRST, SPI_MODE0) //!< VS1053 SPI control settings
#define VS1053_DATA_SPI_SETTING                                                \
  SPISettings(8000000, MSBFIRST, SPI_MODE0) //!< VS1053 SPI data settings
I've tried to play around with the SPIsettings(). it turns out that increasing the 250KHz to 1MHz reduces the loading time from 3.3s to 1.3s.

What is VS1053b's recommended SPIsetting? The Feather M0 microcontroller uses a 32MHz crystal.

Does you see any problem using SPI.transfer()? Would DMA help? I would rather stay with VS1053b (migrating to another chip would be the last resort).

Thank you,
Ray
User avatar
pasi
VLSI Staff
Posts: 1848
Joined: Thu 2010-07-15 16:04

Re: SCI_WRITE .plg plugin to VS1053b takes too long

Post by pasi »

After reset vs1053b SCI reads can be perform at 12.288MHz/7 = 1.755MHz and writes to SCI or SDI at 12.288MHz/4 = 3.072MHz .

After you've written SCI_CLOCKF, the max speed will depend on the clock multiplier (CLKI/7 and CLKI/4), so if you can increase the SPI speed further, it should shorten the upload time considerably. After that, maybe you can get a little extra by trying to streamline the routines to have less overhead.
Visit https://www.facebook.com/VLSISolution VLSI Solution on Facebook
nomnom-ray
User
Posts: 3
Joined: Sat 2022-01-08 8:33

Re: SCI_WRITE .plg plugin to VS1053b takes too long

Post by nomnom-ray »

...so if you can increase the SPI speed further, it should shorten the upload time considerably.
There is a problem with blindly increasing the SPI speed: the recording becomes distorted with all noise (maybe it's clipping due to some kind of gain state accidentally increased too high). In any case, only uploading the .plg with SPI speed of 250KHz leads to acceptable recording.

is there any significance to the 250KHz number or is it coincidence?
User avatar
pasi
VLSI Staff
Posts: 1848
Joined: Thu 2010-07-15 16:04

Re: SCI_WRITE .plg plugin to VS1053b takes too long

Post by pasi »

Are you writing SCI_CLOCKF before increasing the clock and uploading the patch? (You may need to wait a bit for the clock actually to be changed, then change the SPI speed.)

Is the SPI used for your storage as well?

Have you tried increasing the SPI speed for the duration of the upload and restoring it to the 250kHz afterwards?
Visit https://www.facebook.com/VLSISolution VLSI Solution on Facebook
Post Reply