Small example for xIFO

Dependencies:   xIFO mbed

Committer:
jeroen3
Date:
Mon Oct 28 19:10:54 2013 +0000
Revision:
0:afe80692913e
Child:
1:f8ee57bf1292
Initial example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jeroen3 0:afe80692913e 1 #include "mbed.h"
jeroen3 0:afe80692913e 2
jeroen3 0:afe80692913e 3 DigitalOut myled(LED1);
jeroen3 0:afe80692913e 4
jeroen3 0:afe80692913e 5 // Create a buffer (note that this is a C module, not an object)
jeroen3 0:afe80692913e 6 xifo_t buffer;
jeroen3 0:afe80692913e 7 // Allocate memory for buffer
jeroen3 0:afe80692913e 8 xifo_pool_t buffer_memory[16];
jeroen3 0:afe80692913e 9
jeroen3 0:afe80692913e 10 int main() {
jeroen3 0:afe80692913e 11 uint32_t data, succes;
jeroen3 0:afe80692913e 12
jeroen3 0:afe80692913e 13 // Initialise the created buffer
jeroen3 0:afe80692913e 14 xifo_init(&buffer, 128, (uint32_t *)&buffer_memory);
jeroen3 0:afe80692913e 15
jeroen3 0:afe80692913e 16 // Erase buffer contents
jeroen3 0:afe80692913e 17 xifo_clear(&buffer);
jeroen3 0:afe80692913e 18
jeroen3 0:afe80692913e 19 // Write some data
jeroen3 0:afe80692913e 20 succes = xifo_write(&buffer, 0x1);
jeroen3 0:afe80692913e 21 succes = xifo_write(&buffer, 0x2);
jeroen3 0:afe80692913e 22 succes = xifo_write(&buffer, 0x3);
jeroen3 0:afe80692913e 23 succes = xifo_write(&buffer, 0x4);
jeroen3 0:afe80692913e 24 succes = xifo_write(&buffer, 0x5);
jeroen3 0:afe80692913e 25 // Are we full yet?
jeroen3 0:afe80692913e 26 if( xifo_get_free(&buffer) > 0 )
jeroen3 0:afe80692913e 27 {
jeroen3 0:afe80692913e 28 succes = xifo_write(&buffer, 0x6);
jeroen3 0:afe80692913e 29 }
jeroen3 0:afe80692913e 30
jeroen3 0:afe80692913e 31 /* Read from the buffer, read-only! */
jeroen3 0:afe80692913e 32 // Read the least recent element : 0x1
jeroen3 0:afe80692913e 33 data = xifo_read_lr(&buffer, 0);
jeroen3 0:afe80692913e 34
jeroen3 0:afe80692913e 35 // Read the 2nd least recent element : 0x2
jeroen3 0:afe80692913e 36 data = xifo_read_lr(&buffer, 1);
jeroen3 0:afe80692913e 37
jeroen3 0:afe80692913e 38 // Read the most recent element : 0x6
jeroen3 0:afe80692913e 39 data = xifo_read_mr(&buffer, 0);
jeroen3 0:afe80692913e 40
jeroen3 0:afe80692913e 41 // Read the 2nd most recent element : 0x5
jeroen3 0:afe80692913e 42 data = xifo_read_mr(&buffer, 1);
jeroen3 0:afe80692913e 43
jeroen3 0:afe80692913e 44 /* Pop from the buffer, removes elements */
jeroen3 0:afe80692913e 45 // Remove the least recent element : 0x1
jeroen3 0:afe80692913e 46 data = xifo_pop_lr(&buffer);
jeroen3 0:afe80692913e 47
jeroen3 0:afe80692913e 48 // Remove the most recent element : 0x6
jeroen3 0:afe80692913e 49 data = xifo_pop_mr(&buffer);
jeroen3 0:afe80692913e 50
jeroen3 0:afe80692913e 51 /* Done
jeroen3 0:afe80692913e 52 * Buffer should have this: 0x2, 0x3, 0x4, 0x5
jeroen3 0:afe80692913e 53 */
jeroen3 0:afe80692913e 54
jeroen3 0:afe80692913e 55 while(1) {
jeroen3 0:afe80692913e 56 myled = 1;
jeroen3 0:afe80692913e 57 wait(0.2);
jeroen3 0:afe80692913e 58 myled = 0;
jeroen3 0:afe80692913e 59 wait(0.2);
jeroen3 0:afe80692913e 60 }
jeroen3 0:afe80692913e 61 }