ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches

Circular buffer for measurements. More...

Macros

#define FRAM_BUFFER_START   0
 
#define FRAM_BUFFER_END   1769
 

Functions

fram_status fifo_put (const uint8_t *data, uint8_t num_bytes)
 Puts a measurement into the circular buffer.
 
fram_status fifo_get (uint8_t *data, uint8_t *len)
 Reads a measurement from the queue.
 
fram_status fifo_peek (size_t idx, uint8_t *data, uint8_t *len)
 Peeks at the next measurement in the buffer without removing it.
 
fram_status fifo_drop (void)
 Drops the next measurement in the buffer.
 
uint16_t fifo_buffer_len (void)
 Get the current number of measurements stored in the buffer.
 
fram_status fifo_buffer_clear (void)
 Clears the buffer.
 
fram_status fifo_save_buffer_state (void)
 Saves the buffer state (read address, write address, and buffer length) to FRAM.
 
fram_status fifo_load_buffer_state (void)
 Loads the buffer state (read address, write address, and buffer length) from FRAM.
 
fram_status fifo_init (void)
 Initializes the buffer.
 
int fifo_size (void)
 Gets the total number of bytes in the buffer.
 

Detailed Description

Circular buffer for measurements.

A circular buffer is implemented on part of the memory space of the fram chip. The address space used can be modified with FRAM_BUFFER_START and FRAM_BUFFER_END depending on user needs.

Measurements are stored with a single uint8_t of their length followed by the serialized protobuf message. On reads, the length is first read, then the length number of bytes are read into RAM. Ensure the read buffer used is of sufficient size.

The buffer's implementation does not allow for overwriting of data. Once the buffer is full, indicated by FRAM_BUFFER_FULL, data needs to be removed by getting the next measurement or clearing the buffer entirely.

Todo:
Implement a clear pointer in the following

The buffer operates as a circular queue with three pointers:

Unlike traditional circular buffers that maintain only read and write pointers with overflow protection, the ENTS design adds a clear pointer to track backend uplink confirmations. This pointer ensures that unacknowledged data is not overwritten by subsequent writes. The write pointer is allowed to wrap around but is restricted from advancing past the clear pointer. This constraint protects data integrity in field deployments where network failures may delay acknowledgment indefinitely.

Macro Definition Documentation

◆ FRAM_BUFFER_END

#define FRAM_BUFFER_END   1769

Ending address of buffer, which is INCLUSIVE

Definition at line 68 of file fifo.h.

◆ FRAM_BUFFER_START

#define FRAM_BUFFER_START   0

Starting address of buffer, which is INCLUSIVE

Definition at line 63 of file fifo.h.

Function Documentation

◆ fifo_buffer_clear()

fram_status fifo_buffer_clear ( void  )

Clears the buffer.

Read and write addresses are set to their default values allowing for the buffer to be overwritten.

Definition at line 235 of file fifo.c.

References fifo_save_buffer_state(), FRAM_BUFFER_START, and FRAM_OK.

Referenced by fifo_init(), setUp(), test_fifo_buffer_clear(), test_fifo_drop(), test_fifo_load_buffer_state(), and test_fifo_peek().

◆ fifo_buffer_len()

uint16_t fifo_buffer_len ( void  )

Get the current number of measurements stored in the buffer.

Returns
Number of measurements

Definition at line 233 of file fifo.c.

Referenced by main(), test_fifo_buffer_clear(), test_fifo_buffer_len(), test_fifo_drop(), test_fifo_peek(), test_fifo_put_ValidData(), and test_fifo_wraparound().

◆ fifo_drop()

fram_status fifo_drop ( void  )

Drops the next measurement in the buffer.

Alternative to fifo_get when the data is not needed.

Returns
See fram_status

Definition at line 210 of file fifo.c.

References fifo_save_buffer_state(), FRAM_BUFFER_EMPTY, FRAM_OK, and fram_read().

Referenced by test_fifo_drop().

◆ fifo_get()

fram_status fifo_get ( uint8_t *  data,
uint8_t *  len 
)

Reads a measurement from the queue.

Parameters
dataArray to be read into
lenLength of data
Returns
See fram_status

Definition at line 102 of file fifo.c.

References data, fifo_save_buffer_state(), FRAM_BUFFER_EMPTY, FRAM_BUFFER_END, FRAM_OK, and fram_read().

Referenced by test_fifo_BufferEmpty(), test_fifo_drop(), test_fifo_load_buffer_state(), test_fifo_Sequential(), test_fifo_Sequential_BufferFull(), test_fifo_ValidData(), and test_fifo_wraparound().

◆ fifo_init()

fram_status fifo_init ( void  )

Initializes the buffer.

Initializes the FIFO buffer by loading the buffer state (read address, write address, and buffer length) from FRAM. If the state cannot be loaded or is invalid, it initializes the buffer with default values.

Returns
fram_status, status of the FRAM operation.

Definition at line 326 of file fifo.c.

References fifo_buffer_clear(), fifo_load_buffer_state(), FRAM_BUFFER_START, and FRAM_OK.

◆ fifo_load_buffer_state()

fram_status fifo_load_buffer_state ( void  )

Loads the buffer state (read address, write address, and buffer length) from FRAM.

Returns
fram_status, status of the FRAM operation.

Definition at line 290 of file fifo.c.

References buffer, FRAM_OK, and fram_read().

Referenced by fifo_init(), and test_fifo_load_buffer_state().

◆ fifo_peek()

fram_status fifo_peek ( size_t  idx,
uint8_t *  data,
uint8_t *  len 
)

Peeks at the next measurement in the buffer without removing it.

Alternative to fifo_get when the data is needed but should remain in the buffer. Index starts at the read pointer.

Parameters
idxIndex of the measurement to peek
dataArray to be read into
lenLength of data
Returns
See fram_status

Definition at line 147 of file fifo.c.

References data, FRAM_BUFFER_EMPTY, FRAM_BUFFER_END, FRAM_OK, FRAM_OUT_OF_RANGE, fram_read(), and i.

Referenced by test_fifo_peek().

◆ fifo_put()

fram_status fifo_put ( const uint8_t *  data,
uint8_t  num_bytes 
)

◆ fifo_save_buffer_state()

fram_status fifo_save_buffer_state ( void  )

Saves the buffer state (read address, write address, and buffer length) to FRAM.

Returns
fram_status, status of the FRAM operation.

Definition at line 247 of file fifo.c.

References buffer, FRAM_OK, and fram_write().

Referenced by fifo_buffer_clear(), fifo_drop(), fifo_get(), and fifo_put().

◆ fifo_size()

int fifo_size ( void  )

Gets the total number of bytes in the buffer.

Returns
Number of bytes, fram_status otherwise.

Definition at line 346 of file fifo.c.

Referenced by test_fifo_put_Sequential_BufferFull(), test_fifo_Sequential_BufferFull(), and test_fifo_wraparound().