|
ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
|
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. | |
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.
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.
| #define FRAM_BUFFER_END 1769 |
| #define FRAM_BUFFER_START 0 |
| 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().
| uint16_t fifo_buffer_len | ( | void | ) |
Get the current number of measurements stored in the buffer.
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().
| fram_status fifo_drop | ( | void | ) |
Drops the next measurement in the buffer.
Alternative to fifo_get when the data is not needed.
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().
| fram_status fifo_get | ( | uint8_t * | data, |
| uint8_t * | len | ||
| ) |
Reads a measurement from the queue.
| data | Array to be read into |
| len | Length of data |
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().
| 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.
Definition at line 326 of file fifo.c.
References fifo_buffer_clear(), fifo_load_buffer_state(), FRAM_BUFFER_START, and FRAM_OK.
| fram_status fifo_load_buffer_state | ( | void | ) |
Loads the buffer state (read address, write address, and buffer length) from FRAM.
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().
| 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.
| idx | Index of the measurement to peek |
| data | Array to be read into |
| len | Length of data |
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().
| fram_status fifo_put | ( | const uint8_t * | data, |
| uint8_t | num_bytes | ||
| ) |
Puts a measurement into the circular buffer.
| data | An array of data bytes. |
| num_bytes | The number of bytes to be written. |
Definition at line 55 of file fifo.c.
References data, fifo_save_buffer_state(), FRAM_BUFFER_END, FRAM_BUFFER_FULL, FRAM_OK, and fram_write().
Referenced by main(), test_fifo_buffer_clear(), test_fifo_buffer_len(), test_fifo_drop(), test_fifo_load_buffer_state(), test_fifo_peek(), test_fifo_put_Sequential(), test_fifo_put_Sequential_BufferFull(), test_fifo_put_ValidData(), test_fifo_Sequential(), test_fifo_Sequential_BufferFull(), test_fifo_ValidData(), and test_fifo_wraparound().
| fram_status fifo_save_buffer_state | ( | void | ) |
Saves the buffer state (read address, write address, and buffer length) to FRAM.
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().
| int fifo_size | ( | void | ) |
Gets the total number of bytes in the buffer.
Definition at line 346 of file fifo.c.
Referenced by test_fifo_put_Sequential_BufferFull(), test_fifo_Sequential_BufferFull(), and test_fifo_wraparound().