6 years, 5 months ago.

USE of FIFO in STM-Nucleo board

I am using a piece of code that reads some data from sensors, i want to write the data into the internal memory of the micro controller"Nucleo-board" using FIFO technique, i searched online but could not get a useful example to work on. Do i need to use some extra libraries for that? could someone kindly help me on that please?

2 Answers

6 years, 5 months ago.

You can try use part of RTOS, Mail and queues API. https://os.mbed.com/docs/v5.6/reference/mail.html Not sure if it will work in interrupt context. Works fine with threads and ISR.

Basic Mail and Queue example. https://docs.mbed.com/docs/mbed-os-api-ref/en/stable/APIs/tasks/rtos/

6 years, 5 months ago.

Do you have a fixed and not excessive maximum amount of data you need to queue up? If so then it's normally most efficient to just define an array of the appropriate data type and have two counters telling you where in the array to read and write next.

e.g.

#define _bufferSize_ 1024
float dataBuffer[_bufferSize_];
int readIndex =0;
int writeIndex =0;
int dataCount = 0;

bool fifoWrite(float value) {
  if (dataCount < _bufferSize_) {
    dataBuffer[writeIndex] = value;
    dataCount++;
    writeIndex++;
    if (writeIndex == _bufferSize_)
      writeIndex =0;
    return true;
  }
  return false;
}

bool fifoRead(float *value) {
  if (dataCount > 0) {
    *value = dataBuffer[readIndex];
    dataCount--;
    readIndex++;
    if (readIndex== _bufferSize_)
      readIndex=0;
    return true;
  }
  return false;
}