4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief STM32 backend implementation entropy pulling.
34 * \author Daniele Basile <asterix@develer.com>
38 #include <sec/random_p.h>
40 #include <cpu/power.h>
42 #include <drv/clock_cm3.h>
46 struct stm32_adc *adc = (struct stm32_adc *)ADC1_BASE;
49 * Return the cpu core temperature in raw format
51 INLINE uint16_t hw_readRawTemp(void)
53 /* We sample only from one channel */
54 adc->SQR1 |= BV(SQR1_SQ_LEN_SHIFT);
55 adc->SQR3 = (ADC_TEMP_CH & SQR3_SQ_MASK);
57 /* Start convertion */
58 adc->CR2 |= CR2_EXTTRIG_SWSTRT_SET;
60 /* Wait in polling until conversion is done */
61 while (!(adc->SR & BV(SR_EOC)))
64 /* Return the last converted data */
65 return (uint16_t)adc->DR;
68 INLINE void hw_initIntTemp(void)
70 RCC->APB2ENR |= RCC_APB2_ADC1;
83 * - Wake up temperature and Vrefint
85 adc->CR2 |= BV(CR2_ADON) | ADC_EXTERNALTRIGCONV_NONE | BV(CR2_TSVREFE);
87 /* Set 17.1usec sampling time*/
88 adc->SMPR1 |= ((ADC_SAMPLETIME_239CYCLES5 << SMPR1_CH17) | (ADC_SAMPLETIME_239CYCLES5 << SMPR1_CH16));
92 void random_pull_entropy(uint8_t *entropy, size_t len)
94 // We use the internal temperature sensor of LM3S as a source of entropy.
95 // The last bit of the acquisition is very variable and with a decent distribution
96 // to consider it "entropic". It does not really matter because it will
97 // go through a randomness extractor anyway.
100 for (size_t j=0; j<len; j++)
103 for (int b=0; b<8; ++b)
104 if (hw_readRawTemp() & 1)