Class library for using the true Random Number Generator on STM32F4xxx devices.
Dependents: MCU-Benchmark-Sute Doom_Flame-F429ZI_v02 Wether_Meter
Revision 0:1c605984e361, committed 2016-02-08
- Comitter:
- grantphillips
- Date:
- Mon Feb 08 20:15:12 2016 +0000
- Commit message:
- v1.0
Changed in this revision
STM32F4_RNG.cpp | Show annotated file Show diff for this revision Revisions of this file |
STM32F4_RNG.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 1c605984e361 STM32F4_RNG.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/STM32F4_RNG.cpp Mon Feb 08 20:15:12 2016 +0000 @@ -0,0 +1,15 @@ +#include "STM32F4_RNG.h" +#include "mbed.h" + + +STM32F4_RNG::STM32F4_RNG() { + RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN; /* Enable RNG clock source */ + RNG->CR |= RNG_CR_RNGEN; /* RNG Peripheral enable */ +} + + +unsigned long STM32F4_RNG::Get() { + while (!(RNG->SR & (RNG_SR_DRDY))); /* Wait until one RNG number is ready */ + + return RNG->DR; /* Get a 32-bit Random number */ +} \ No newline at end of file
diff -r 000000000000 -r 1c605984e361 STM32F4_RNG.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/STM32F4_RNG.h Mon Feb 08 20:15:12 2016 +0000 @@ -0,0 +1,71 @@ +/* STM32F4_RNG Library v1.0 + * Copyright (c) 2016 Grant Phillips + * grant.phillips@nmmu.ac.za + * + * This library was adapted from tm_stm32f4_rng written by Tilen Majerle. + * (http://stm32f4-discovery.com/2014/07/library-22-true-random-number-generator-stm32f4xx/) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef STM32F4_RNG_H +#define STM32F4_RNG_H + +#include "mbed.h" + +/** Class library for using the true Random Number Generator on STM32F4xxx devices. + * + * Example: + * @code + * #include "mbed.h" + * #include "STM32F4_RNG.h" + * #include "SWO.h" + * + * STM32F4_RNG rnd; + * + * int main() { + * // create a 32-bit unsigned variable + * unsigned long num; //or uint32_t num + * + * while(1) { + * num = rnd.Get(); + * printf("%u\n", num); + * wait(1.0); + * } + * } + * @endcode + */ + +class STM32F4_RNG { + public: + /** Create a STM32F4_RNG object. + * + */ + STM32F4_RNG(); + + /** Gets a 32-bit random number. + * @param + * None + * @return + * 32-bit random number. + */ + unsigned long Get(); +}; + +#endif \ No newline at end of file