Small example for xIFO

Dependencies:   xIFO mbed

Committer:
jeroen3
Date:
Mon Oct 28 19:22:59 2013 +0000
Revision:
1:f8ee57bf1292
Parent:
0:afe80692913e
Fix case senstive include

Who changed what in which revision?

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