BeRTOS
random_lm3s.c
Go to the documentation of this file.
00001 
00037 #include "random_p.h"
00038 
00039 #include <cpu/power.h>
00040 
00041 #include <io/cm3.h>
00042 
00043 #include <drv/clock_cm3.h>
00044 
00045 /*
00046  * Return the cpu core temperature in raw format
00047  */
00048 INLINE uint16_t hw_readRawTemp(void)
00049 {
00050     /* Trig the temperature sampling */
00051     HWREG(ADC0_BASE + ADC_O_PSSI) |= ADC_PSSI_SS3;
00052 
00053     while (!(HWREG(ADC0_BASE + ADC_O_SSFSTAT3) & ADC_SSFSTAT3_FULL))
00054         cpu_relax();
00055 
00056     return (uint16_t)HWREG(ADC0_BASE + ADC_O_SSFIFO3);
00057 }
00058 
00059 INLINE void hw_initIntTemp(void)
00060 {
00061     SYSCTL_RCGC0_R |= SYSCTL_RCGC0_ADC0;
00062 
00063     lm3s_busyWait(10);
00064 
00065     /* Disable all sequence */
00066     HWREG(ADC0_BASE + ADC_O_ACTSS) = 0;
00067     /* Set trigger event to programmed (for all sequence) */
00068     HWREG(ADC0_BASE + ADC_O_EMUX) = 0;
00069     /* Enalbe read of temperature sensor */
00070     HWREG(ADC0_BASE + ADC_O_SSCTL3) |= ADC_SSCTL3_TS0;
00071     /* Enable sequence S03 (single sample on select channel) */
00072     HWREG(ADC0_BASE + ADC_O_ACTSS) |= ADC_ACTSS_ASEN3;
00073 }
00074 
00075 
00076 void random_pull_entropy(uint8_t *entropy, size_t len)
00077 {
00078     // We use the internal temperature sensor of LM3S as a source of entropy.
00079     // The last bit of the acquisition is very variable and with a decent distribution
00080     // to consider it "entropic". It does not really matter because it will
00081     // go through a randomness extractor anyway.
00082     hw_initIntTemp();
00083 
00084     for (size_t j=0; j<len; j++)
00085     {
00086         uint8_t accum = 0;
00087         for (int b=0; b<8; ++b)
00088             if (hw_readRawTemp() & 1)
00089                 accum |= 1<<b;
00090 
00091         *entropy++ = accum;
00092     }
00093 }