Fast GPIO using C++ templates. Now with port I/O.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include "FastIO.h"
00004 
00005 #define LED_MASK 0x07800000
00006 
00007 DigitalOut led1(LED1);
00008 FastOut<LED2> led2;
00009 PortOut ledport(Port0, LED_MASK);
00010 FastPortOut<Port0, LED_MASK> ledport2;
00011 MaskedPortOut<Port0, LED_MASK> ledport3;
00012 
00013 Timer t;
00014 #define LOOPS 100000000
00015 int main() {
00016     int value = 0;
00017     int count = LOOPS;
00018     t.start();
00019     while ( count -- )
00020     {
00021         led1.write(value);
00022         value = 1-value;
00023     }
00024     t.stop();
00025     printf("DigitalOut: %f seconds (%d ns per iteration).\n", t.read(), t.read_us()/(LOOPS/1000));
00026     
00027     count = LOOPS;
00028     t.reset();
00029     t.start();
00030     while ( count -- )
00031     {
00032         led2 = value;
00033         value = 1-value;
00034     }
00035     t.stop();
00036     printf("FastOut: %f seconds (%d ns per iteration).\n", t.read(), t.read_us()/(LOOPS/1000));
00037 
00038     count = LOOPS;
00039     t.reset();
00040     t.start();
00041     value = LED_MASK;
00042     while ( count -- )
00043     {
00044         ledport.write(value);
00045         value ^= LED_MASK;
00046     }
00047     t.stop();
00048     printf("PortOut: %f seconds (%d ns per iteration).\n", t.read(), t.read_us()/(LOOPS/1000));
00049 
00050     count = LOOPS;
00051     t.reset();
00052     t.start();
00053     value = LED_MASK;
00054     while ( count -- )
00055     {
00056         ledport2 = value;
00057         value ^= LED_MASK;
00058     }
00059     t.stop();
00060     printf("FastPortOut: %f seconds (%d ns per iteration).\n", t.read(), t.read_us()/(LOOPS/1000));
00061 
00062     count = LOOPS;
00063     t.reset();
00064     t.start();
00065     value = LED_MASK;
00066     while ( count -- )
00067     {
00068         ledport3 = value;
00069         value ^= LED_MASK;
00070     }
00071     t.stop();
00072     printf("MaskedPortOut: %f seconds (%d ns per iteration).\n", t.read(), t.read_us()/(LOOPS/1000));
00073 }