diff --git a/README.md b/README.md index 65cf704..7405926 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,12 @@
@@ -61,20 +61,20 @@ _fff_declare(int8_t, fifo_int8, 16); int main(void) { - // volatile prevents the compiler from optimizing the variable away - volatile int8_t tmp; + // volatile prevents the compiler from optimizing the variable away + volatile int8_t tmp; // initialize fifo _fff_init(fifo_int8); - // write a value to the fifo - _fff_write(fifo_int8, -42); + // write a value to the fifo + _fff_write(fifo_int8, -42); - // read back the value from the fifo - tmp = _fff_read(fifo_int8); + // read back the value from the fifo + tmp = _fff_read(fifo_int8); - // 'tmp' contains now the value '-42' - while(1); + // 'tmp' contains now the value '-42' + while(1); } ``` @@ -229,33 +229,36 @@ To use **fifofast** you don't need to know its inner workings. This chapter is f ### The Typical Implementation To get the best performance most fifos are based on an array for data storage. New elements are always placed at the next free index. When the fifo is read, the element of the earliest written index is returned. Example: - empty array: - ┌───┬───┬───┬───┬───┬───┬───┬───┐ - │ │ │ │ │ │ │ │ │ - └───┴───┴───┴───┴───┴───┴───┴───┘ +``` +empty array: +┌───┬───┬───┬───┬───┬───┬───┬───┐ +│ │ │ │ │ │ │ │ │ +└───┴───┴───┴───┴───┴───┴───┴───┘ - write 4 elements: - ┌───┬───┬───┬───┬───┬───┬───┬───┐ - │ a │ b │ c │ d │ │ │ │ │ - └───┴───┴───┴───┴───┴───┴───┴───┘ - - read 2 elements: - ┌───┬───┬───┬───┬───┬───┬───┬───┐ - │ │ │ c │ d │ │ │ │ │ - └───┴───┴───┴───┴───┴───┴───┴───┘ +write 4 elements: +┌───┬───┬───┬───┬───┬───┬───┬───┐ +│ a │ b │ c │ d │ │ │ │ │ +└───┴───┴───┴───┴───┴───┴───┴───┘ +read 2 elements: +┌───┬───┬───┬───┬───┬───┬───┬───┐ +│ │ │ c │ d │ │ │ │ │ +└───┴───┴───┴───┴───┴───┴───┴───┘ +``` After the array was filled at least once, new data must be added to the very first location. This is called a [circular buffer (wiki)][wiki-circular-buffer] - after some time (example): - ┌───┬───┬───┬───┬───┬───┬───┬───┐ - │ │ │ │ │ │ e │ f │ g │ - └───┴───┴───┴───┴───┴───┴───┴───┘ +``` +after some time (example): +┌───┬───┬───┬───┬───┬───┬───┬───┐ +│ │ │ │ │ │ e │ f │ g │ +└───┴───┴───┴───┴───┴───┴───┴───┘ write 1 element: - ┌───┬───┬───┬───┬───┬───┬───┬───┐ - │ h │ │ │ │ │ e │ f │ g │ - └───┴───┴───┴───┴───┴───┴───┴───┘ +┌───┬───┬───┬───┬───┬───┬───┬───┐ +│ h │ │ │ │ │ e │ f │ g │ +└───┴───┴───┴───┴───┴───┴───┴───┘ +``` To detect this overflow the straight forward approach is to use `if(index > array_size){index = 0;}`. This comparison has to be done for _every_ fifo access. Branches take typically more cycles than arithmetic instruction, especially if a [instruction pipeline (wiki)][wiki-pipelining] is used within the MCU.