nRF5 SDK  v13.1.0
Choose documentation:
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Queue library

The queue library provides interrupt secure implementation of the circular buffer to store predefined objects.

Key features include:

  • Protected enqueuing
  • Protected dequeuing
  • Two modes of overflow handling

Creating a queue

To create a queue instance, use the NRF_QUEUE_DEF macro and provide the stored type, the identifier, the queue size, and the working mode. The macro allocates memory for the queue and the internals of the instance.

The following example code allocates memory for a queue instance and a queue that can store up to 10 bytes:

NRF_QUEUE_DEF(uint8_t, m_byte_queue, 10, NRF_QUEUE_MODE_NO_OVERFLOW);

After the queue is defined, you can define its interface (this step is optional):

NRF_QUEUE_INTERFACE_DEC(uint8_t, byte_queue);
NRF_QUEUE_INTERFACE_DEF(uint8_t, byte_queue, &m_byte_queue)

Using the queue

This section contains sample code that lets you perform actions on the queue instance:

  • Putting an item into the queue:
    uint8_t data = 0x01;
    ret_code_t err_code = byte_queue_push(&data);
    APP_ERROR_CHECK(err_code);
  • Getting an item without removing it from the queue:
    uint8_t data;
    ret_code_t err_code = byte_queue_peek(&data);
    APP_ERROR_CHECK(err_code);
    If the queue is empty, NRF_ERROR_NOT_FOUND will be returned.
  • Getting more items from the queue:
    uint8_t data[5];
    ret_code_t err_code = byte_queue_read(data, sizeof(data));
    APP_ERROR_CHECK(err_code);