This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Bug in RingBuf library - nrf_ringbuf_free does not free up bytes in buffer

Hi

I am having an issue with the RinBuf library - I am accepting packets from SPI, buffering them in a ringbuf until I can send onwards via BLE. The specific issue I am having is that calling nrf_ringbuf_free does not actually free bytes in the ringbuf!

NRF_RINGBUF_DEF(m_RingBufBleNormalPriority, 2048);
#define MESSAGE_PLUS_PAYLOAD_BYTES 244

void foo(void)
{
    nrf_ringbuf_init(&m_RingBufBleNormalPriority);
    ret_code_t err_code = NRF_SUCCESS;
    uint8_t * p_buffer;
    uint32_t BufferLength = 0;
    uint32_t a;
    
    //do this 8 times = 8*244 bytes = 1952 bytes used
    for (a=0;  a<8; a++)
    {
        BufferLength = MESSAGE_PLUS_PAYLOAD_BYTES;
    
        err_code = nrf_ringbuf_alloc(&m_RingBufBleNormalPriority, &p_buffer, &BufferLength, true);
    
        if (err_code != NRF_SUCCESS)
        {
            NRF_LOG_INFO("Error - nrf_ringbuf_alloc err: %d",err_code);
            return;
        }
    
        if (BufferLength != MESSAGE_PLUS_PAYLOAD_BYTES)
        {
            NRF_LOG_INFO("Error - Wrong length allocated");
            nrf_ringbuf_put(&m_RingBufBleNormalPriority, 0);
            return;
        }
    
        //would ordinarily fill the buffer with data from SPI here
    
        nrf_ringbuf_put(&m_RingBufBleNormalPriority, MESSAGE_PLUS_PAYLOAD_BYTES);
    }
    
    //At this point, 1952 bytes out of the total 2028 bytes utilised in the ring buffer
    
    
    
    //Get one record == 244 bytes from Ring Buffer
    BufferLength = MESSAGE_PLUS_PAYLOAD_BYTES;
    err_code = nrf_ringbuf_get(&m_RingBufBleNormalPriority, &p_buffer, &BufferLength, true);
    
    if (err_code != NRF_SUCCESS)
    {
        NRF_LOG_INFO("Error - nrf_ringbuf_get err: %d",err_code);
        return;
    }
    
    if (BufferLength != MESSAGE_PLUS_PAYLOAD_BYTES)
    {
        NRF_LOG_INFO("Error - Wrong length read from buffer");
        nrf_ringbuf_free(&m_RingBufBleNormalPriority, 0);
        return;
    }
    
    nrf_ringbuf_free(&m_RingBufBleNormalPriority, MESSAGE_PLUS_PAYLOAD_BYTES);
    
    //At this point, buffer utilisation is 1952-244 bytes = 1708 with 340 bytes remaining
    
    //Add another 244 bytes to the Ring Buffer (should be space)
    
    BufferLength = MESSAGE_PLUS_PAYLOAD_BYTES;
    
    err_code = nrf_ringbuf_alloc(&m_RingBufBleNormalPriority, &p_buffer, &BufferLength, true);
    
    if (err_code != NRF_SUCCESS || BufferLength != MESSAGE_PLUS_PAYLOAD_BYTES)
    {
        NRF_LOG_INFO("Error - nrf_ringbuf_alloc err: %d",err_code);
        NRF_LOG_INFO("Requested: %d | Allocated: %d",MESSAGE_PLUS_PAYLOAD_BYTES,BufferLength);
        nrf_ringbuf_put(&m_RingBufBleNormalPriority, 0);
        return;
        
        
        
    /*************** THIS FAILS HERE ***************/
    //BufferLength != MESSAGE_PLUS_PAYLOAD_BYTES only 60 bytes have been allocated
    //there are 340 available
    //If I do another GET operation then I get the same result, 60 bytes available!
    //The function nrf_ringbuf_free DOES NOT free up the 244 bytes I have asked it to!!
    }
    
    
    nrf_ringbuf_put(&m_RingBufBleNormalPriority, MESSAGE_PLUS_PAYLOAD_BYTES);
}   

    
    
    
    
    
    nrf_ringbuf_put(&m_RingBufBleNormalPriority, 0);

See the comments in the example above. I put and commit 244 bytes into the run buffer 8 times over, I then get and free 244 bytes, leaving 340 available

I then attempt to put another 244 bytes into the ring buffer, but I am only given an allocation of 60 bytes despite there being 340 bytes available. The example is monolithic so I am not checking for NRF_ERROR_BUSY (although I do in my real app), neither am I doing anything with the allocated buffers in the above. Incidentally, the initial 8x244 bytes is 1952, 2048-1952  is 60 . Sixty is what I am being offered in my final request DESPITE having Gotten and Free'd 244 bytes beforehand. The nrf_ringbuf_free returns NRF_SUCCESS. If I entend the size of the ringbuff then it just delays the issue, eventually it fills despite having been freed.

Any idea's - maybe I just cannot use a ringbuff in this manner?

Nigel

Parents Reply Children
No Data
Related