Use your mbed and it\'s noisy analog inputs as a hardware random number generator!

Dependencies:   mbed SHA256

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EntropyPool.h"
00003 #include "PowerControl.h"
00004 #include "EthernetPowerControl.h"
00005 
00006 DigitalOut led1(LED1);
00007 DigitalOut led2(LED2);
00008 DigitalOut led3(LED3);
00009 DigitalOut led4(LED4);
00010 Serial pc(USBTX, USBRX);
00011 
00012 EntropyPool pool;
00013 AnalogIn noise1(p15);
00014 AnalogIn noise2(p16);
00015 AnalogIn noise3(p17);
00016 AnalogIn noise4(p18);
00017 AnalogIn noise5(p19);
00018 AnalogIn noise6(p20);
00019 
00020 void gatherEntropy()
00021 {
00022     pool.gatherAnalogNoise(noise1);
00023     pool.gatherAnalogNoise(noise2);
00024     pool.gatherAnalogNoise(noise3);
00025     pool.gatherAnalogNoise(noise4);
00026     pool.gatherAnalogNoise(noise5);
00027     pool.gatherAnalogNoise(noise6);
00028 }
00029 
00030 void write_block()
00031 {
00032     led1 = 0; led2 = 1;
00033     while(pc.readable()) pc.getc();
00034     led2 = 0; led3 = 1;
00035     char* random = pool.produce(1024);
00036     led3 = 0; led4 = 1;
00037     for(int i = 0; i < 1024; ++i)
00038         pc.putc(random[i]);
00039     delete random;
00040     led4 = 0; led1 = 1;
00041 }
00042 
00043 int main() {
00044     pool.gatherer.attach(&gatherEntropy);
00045     
00046     // Disable ethernet to save power
00047     PHY_PowerDown();
00048     
00049     // Write at maximum warp
00050     pc.baud(115200);
00051     pc.attach(&write_block, Serial::RxIrq);
00052     
00053     // Sleep untill called
00054     while(1) Sleep();
00055 }