PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Entropy.h 1.9KB

3 vuotta sitten
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Entropy - A entropy (random number) generator for the Arduino
  2. //
  3. // Copyright 2014 by Walter Anderson
  4. //
  5. // This file is part of Entropy, an Arduino library.
  6. // Entropy 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 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // Entropy 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.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with Entropy. If not, see <http://www.gnu.org/licenses/>.
  18. #ifndef Entropy_h
  19. #define Entropy_h
  20. #include <stdint.h>
  21. // Separate the ARM Due headers we use
  22. #ifdef ARDUINO_SAM_DUE
  23. #include <sam.h>
  24. #include <sam3xa/include/component/component_trng.h>
  25. #endif
  26. // Teensy required headers
  27. #ifdef TEENSYDUINO
  28. #include <util/atomic.h>
  29. #endif
  30. // Separate AVR headers from ARM headers
  31. #ifdef __AVR__
  32. #include <avr/interrupt.h>
  33. #include <avr/wdt.h>
  34. #include <util/atomic.h>
  35. #endif
  36. const uint32_t WDT_RETURN_BYTE=256;
  37. const uint32_t WDT_RETURN_WORD=65536;
  38. union ENTROPY_LONG_WORD
  39. {
  40. uint32_t int32;
  41. uint16_t int16[2];
  42. uint8_t int8[4];
  43. };
  44. class EntropyClass
  45. {
  46. public:
  47. void Initialize(void);
  48. uint32_t random(void);
  49. uint32_t random(uint32_t max);
  50. uint32_t random(uint32_t min, uint32_t max);
  51. uint8_t randomByte(void);
  52. uint16_t randomWord(void);
  53. float randomf(void);
  54. float randomf(float max);
  55. float randomf(float min, float max);
  56. float rnorm(float mean, float stdDev);
  57. uint8_t available(void);
  58. private:
  59. ENTROPY_LONG_WORD share_entropy;
  60. uint32_t retVal;
  61. uint8_t random8(void);
  62. uint16_t random16(void);
  63. };
  64. extern EntropyClass Entropy;
  65. #endif