rng.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2018 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "rng.h"
  27. #if MICROPY_HW_ENABLE_RNG
  28. #define RNG_TIMEOUT_MS (10)
  29. uint32_t rng_get(void) {
  30. // Enable the RNG peripheral if it's not already enabled
  31. if (!(RNG->CR & RNG_CR_RNGEN)) {
  32. #if defined(STM32H7)
  33. // Set RNG Clock source
  34. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVQ);
  35. __HAL_RCC_RNG_CONFIG(RCC_RNGCLKSOURCE_PLL);
  36. #endif
  37. __HAL_RCC_RNG_CLK_ENABLE();
  38. RNG->CR |= RNG_CR_RNGEN;
  39. }
  40. // Wait for a new random number to be ready, takes on the order of 10us
  41. uint32_t start = HAL_GetTick();
  42. while (!(RNG->SR & RNG_SR_DRDY)) {
  43. if (HAL_GetTick() - start >= RNG_TIMEOUT_MS) {
  44. return 0;
  45. }
  46. }
  47. // Get and return the new random number
  48. return RNG->DR;
  49. }
  50. // Return a 30-bit hardware generated random number.
  51. STATIC mp_obj_t pyb_rng_get(void) {
  52. return mp_obj_new_int(rng_get() >> 2);
  53. }
  54. MP_DEFINE_CONST_FUN_OBJ_0(pyb_rng_get_obj, pyb_rng_get);
  55. #else // MICROPY_HW_ENABLE_RNG
  56. // For MCUs that don't have an RNG we still need to provide a rng_get() function,
  57. // eg for lwIP. A pseudo-RNG is not really ideal but we go with it for now. We
  58. // don't want to use urandom's pRNG because then the user won't see a reproducible
  59. // random stream.
  60. // Yasmarang random number generator by Ilya Levin
  61. // http://www.literatecode.com/yasmarang
  62. STATIC uint32_t pyb_rng_yasmarang(void) {
  63. static uint32_t pad = 0xeda4baba, n = 69, d = 233;
  64. static uint8_t dat = 0;
  65. pad += dat + d * n;
  66. pad = (pad << 3) + (pad >> 29);
  67. n = pad | 2;
  68. d ^= (pad << 31) + (pad >> 1);
  69. dat ^= (char)pad ^ (d >> 8) ^ 1;
  70. return pad ^ (d << 5) ^ (pad >> 18) ^ (dat << 1);
  71. }
  72. uint32_t rng_get(void) {
  73. return pyb_rng_yasmarang();
  74. }
  75. #endif // MICROPY_HW_ENABLE_RNG