random.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Daniel Campora
  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 <stdint.h>
  27. #include <stdbool.h>
  28. #include "py/obj.h"
  29. #include "inc/hw_types.h"
  30. #include "inc/hw_ints.h"
  31. #include "inc/hw_memmap.h"
  32. #include "rom_map.h"
  33. #include "pybrtc.h"
  34. #include "simplelink.h"
  35. #include "modnetwork.h"
  36. #include "modwlan.h"
  37. #include "random.h"
  38. #include "debug.h"
  39. /******************************************************************************
  40. * LOCAL TYPES
  41. ******************************************************************************/
  42. typedef union _rng_id_t {
  43. uint32_t id32;
  44. uint16_t id16[3];
  45. uint8_t id8[6];
  46. } rng_id_t;
  47. /******************************************************************************
  48. * LOCAL VARIABLES
  49. ******************************************************************************/
  50. static uint32_t s_seed;
  51. /******************************************************************************
  52. * LOCAL FUNCTION DECLARATIONS
  53. ******************************************************************************/
  54. STATIC uint32_t lfsr (uint32_t input);
  55. /******************************************************************************
  56. * PRIVATE FUNCTIONS
  57. ******************************************************************************/
  58. STATIC uint32_t lfsr (uint32_t input) {
  59. assert( input != 0 );
  60. return (input >> 1) ^ (-(input & 0x01) & 0x00E10000);
  61. }
  62. /******************************************************************************/
  63. // MicroPython bindings;
  64. STATIC mp_obj_t machine_rng_get(void) {
  65. return mp_obj_new_int(rng_get());
  66. }
  67. MP_DEFINE_CONST_FUN_OBJ_0(machine_rng_get_obj, machine_rng_get);
  68. /******************************************************************************
  69. * PUBLIC FUNCTIONS
  70. ******************************************************************************/
  71. void rng_init0 (void) {
  72. rng_id_t juggler;
  73. uint32_t seconds;
  74. uint16_t mseconds;
  75. // get the seconds and the milliseconds from the RTC
  76. pyb_rtc_get_time(&seconds, &mseconds);
  77. wlan_get_mac (juggler.id8);
  78. // flatten the 48-bit board identification to 24 bits
  79. juggler.id16[0] ^= juggler.id16[2];
  80. juggler.id8[0] ^= juggler.id8[3];
  81. juggler.id8[1] ^= juggler.id8[4];
  82. juggler.id8[2] ^= juggler.id8[5];
  83. s_seed = juggler.id32 & 0x00FFFFFF;
  84. s_seed += (seconds & 0x000FFFFF) + mseconds;
  85. // the seed must not be zero
  86. if (s_seed == 0) {
  87. s_seed = 1;
  88. }
  89. }
  90. uint32_t rng_get (void) {
  91. s_seed = lfsr( s_seed );
  92. return s_seed;
  93. }