RingBuffer Component with Put/Get/Clear Events

Sometimes I have a good idea how to extend one of my Processor Expert components with an extra feature, but then I step back because why implementing more than I need at the moment? Until another user of the component simply asks for the same thing, and here we go: if one or more can take advantage of a feature, that’s definitely a strong argument to add it :-). This happened with the RingBuffer Processor Expert component I’m using in many projects. And a reader of this blog asked to add some extra event methods: when an item is added or removed to the buffer.

RingBuffer used in USB Component with Extra Events

RingBuffer used in USB Component with Extra Events

Beside of the OnItemPut() and OnItemGet() events, I added an extra OnClear() event which gets called when the ring buffer Clear() method gets called. The events are disabled  by default not to add any overhead, and they can be enabled individually.

Using the CDE (Component Development Environment) of Processor Expert makes it very easy to such additional events: define the interface for the event, and then add the event code to the driver. Below shows it for the Put() method:

byte %'ModuleName'%.%Put(%'ModuleName'_ElementType elem)
{
  byte res = ERR_OK;
  %'ModuleName'%.DEFINE_CRITICAL();

  %'ModuleName'%.ENTER_CRITICAL();
  if (%'ModuleName'%.inSize==%'ModuleName'%.BUF_SIZE) {
    res = ERR_TXFULL;
  } else {
    %'ModuleName'%.buffer[%'ModuleName'%.inIdx] = elem;
    %'ModuleName'%.inSize++;
    %'ModuleName'%.inIdx++;
    if (%'ModuleName'%.inIdx==%'ModuleName'%.BUF_SIZE) {
      %'ModuleName'%.inIdx = 0;
    }
  }
  %'ModuleName'%.EXIT_CRITICAL();
%if defined(OnBufferFull)
  if (res==ERR_TXFULL) {
    %OnBufferFull(); /* call user event */
  }
%endif
%if defined(OnItemPut)
  %OnItemPut(elem); /* call optional user event */
%endif
  return res;
}

To check the details of the change, see the commit on GitHub.

So with using the new RingBuffer events in my USB stack, I can get application notifications for every byte received or sent, which is very useful.

The updated component will be available with the next *.PEupd release.

Happy Eventing 🙂

Advertisements

8 thoughts on “RingBuffer Component with Put/Get/Clear Events

  1. Is it possible to increase the size of Ring buffer ? I am using CodeWarrior version 10.3 and there the limit on ring buffer size is 256bytes.

    Like

  2. Hi Erick,
    Thanks for keeping us informed with new features.
    I was using an old version version of this ring buffer component, just updated and seems the new one doesn’t have the “Clear:” Method. is it correct?
    Thanks
    Marco

    Like

    • Hi Marco,
      No, that would not be correct. The ‘RingBuffer’ component has a Clear() method. Are you sure that you use the ‘RingBuffer’ component? I ask because in the past I had a RingBufferUInt8 which is obsolete new.
      Can you check again?
      Thanks,
      Erich

      Like

      • Hi Erick,
        This happened in a Home copy of the project where I need to do some code walk through, Compilation failed due the lack of the “Clear” method. After updating the components from Sourceforge, compilation failed again, but If I add a new Ringbuffer then I get the Clear method along with the events mentioned in this post, what I need is erase and replace with the new.
        One thing I notice is that this new ring buffer component generates a Critical Section. I have worked with the component that lacks this referenced component for months. is there any advice on this? so far I haven’t notice any critical section problem as just move the ring buffer to another buffer where I I do any data handling. My application requires 8 UARTs and I am multiplexing the serial port 2:1 with each one having its own working buffer, where I do the data processing.
        Thanks again for valuable work, it is very useful and practical. It has saved me tons of hours of work!
        Marco

        Like

      • Hi Marco,
        The problem is described in https://mcuoneclipse.com/2014/01/26/entercritical-and-exitcritical-why-things-are-failing-badly/. The latest version of Processor Expert has a different fix for it. So it very much depends which version of Processor Expert you are using. Even if you are using the faulty one, it requires a certain sequence explained in the article to make it fail. So you might have seen failure in your system, but not noticed it.
        And good to hear that my stuff is useful for you. That way I know that I can continue doing this :-).
        Erich

        Like

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s