Circular Buffer Libraries inside the Nordic NRF5 SDK

What are the difference between the usage of critical sections and nrf_atomic operations?

Using critical sections disable all interrupts temporarily and re-enables them again. This is no problem as long at it is for short times, but it has a performance penalty and you block potentially unrelated high priority interrupts.

The atomic operations module use the LDREX/STREX instructions that are present in the Cortex-M4 core. This is used to make a form of mutex to make sure that buffer is not updated by one “user” while it is being manipulated by another user without the need for critical sections.

Frequency Asked Question (FAQ)

Question 1)

How does it work for example if I want to get 5 elements and all of them are of different sizes? As far as I can tell the library doesn’t support stuff like this.


You need to use your own scheme on top if you need it to handle different objects, for instance a TLV (type-length-value) variant.

Question 2)

When is thread safety important? When protecting against interrupts? Or is this an RTOS thing?


It is important if you are using the library from several interrupt priorities, regardless of it is using an RTOS or not. If it is possible that a fifo operation can be preempted by another fifo operation, then that is a concern.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.