[En-Nut-Discussion] Found a bug in MP3 device

Michel unreal at home.nl
Fri Aug 22 11:33:01 CEST 2003


Hi,

I am trying to feed an UROM file into the MP3 driver, vs1001k.c
When I tried to repeat the same file over and over again, just to see
How it was doing I found a bug in the vs1001k.c file.

- VsBufferRequest() and VsBufferAvailable() do not return the same amount of
  free space. More than that, the calculation used was wrong.

After fixing that I expected my problems to be over but unfortunatly this
wasn't the case.
So I looked at my code and I saw that there was no way for me to detect
whether a wrap around occurred in the circular buffer that is used in the
vs1001k driver.

So I figured there are two ways to fix this:
- either make the buffer pointers public so I can detect the wrap around in
my application code
- or add a special write() routine to the vs1001k.c file

I did the last because I thought it looked the best :-) (and because it
seems to work ;) )

See below for my fixes.

BTW. If I create the appropriate directories in the 'mod' dir the files I
put there do not get linked (or even compiler) into the standard libraries.
What am I doing wrong?
BTW2. Just executing 'make clean' and 'make install' in the nut directories
somehow does not update the libraries that my application uses (standard
makefile in app directory)

Vs1001k.h:
extern u_char *VsBufferWrite(u_char *data, u_short nbytes);

Vs1001k.c:

The VsBufferAcknowledge() routine is no longer needed, removed it.

/*!
 * \brief Request MP3 data buffer space.
 *
 * \param sizep Pointer to an unsigned short, which receives the
 *              number of bytes available in the buffer.
 *
 * \return Pointer to the next write position.
 */
u_char *VsBufferRequest(u_short * sizep)
{
    cbi(EIMSK, VS_DREQ_BIT);
    if (vs_writeptr >= vs_readptr)
        *sizep = (vs_dataend - vs_databuff) - (vs_writeptr - vs_readptr);
    else
        *sizep = (vs_readptr - vs_writeptr);
    sbi(EIMSK, VS_DREQ_BIT);

    return vs_writeptr;
}

/*!
 * \brief Returns total free buffer space.
 *
 * \return Total number of free bytes in the buffer.
 */
u_short VsBufferAvailable(void)
{
    u_short avail;

    cbi(EIMSK, VS_DREQ_BIT);
    if (vs_writeptr >= vs_readptr)
        avail = (vs_dataend - vs_databuff) - (vs_writeptr - vs_readptr);
    else
        avail = (vs_readptr - vs_writeptr);
    sbi(EIMSK, VS_DREQ_BIT);

    return avail;
}

/*!
 * \brief Write data to the buffer.
 *
 * \note Make sure nbytes is not larger than
 *       is returned by VsBufferAvailable() or
 *       VsBufferRequest()
 *
 * \param data Pointer to the data to write to the buffer
 * \param nbytes Number of bytes to write
 *
 * \return Pointer to the next write position.
 */
u_char *VsBufferWrite(u_char *data, u_short nbytes)
{
    cbi(EIMSK, VS_DREQ_BIT);
    
    if (vs_writeptr && nbytes)
    {
        if ( (u_short)(vs_dataend - vs_writeptr) < nbytes )
        {
            /*
             * We need to wrap around
             */
            memcpy(vs_writeptr, data, vs_dataend - vs_writeptr);
            memcpy(vs_databuff, data + (vs_dataend - vs_writeptr), nbytes -
(vs_dataend - vs_writeptr));
            vs_writeptr = vs_databuff + nbytes - (vs_dataend - vs_writeptr);
        }
        else
        {
            memcpy(vs_writeptr, data, nbytes);
            vs_writeptr += nbytes;
        }
    }
    if (vs_writeptr >= vs_dataend)
        vs_writeptr = vs_databuff;
    /* Cancel flushing in progress. */
    vs_flush = 0;
    sbi(EIMSK, VS_DREQ_BIT);

    return vs_writeptr;
}




More information about the En-Nut-Discussion mailing list