Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include "pico.h" |
| 8 | #include "hardware/structs/rosc.h" |
| 9 | |
| 10 | static uint8_t pico_lwip_random_byte(int cycles) { |
| 11 | static uint8_t byte; |
| 12 | assert(cycles >= 8); |
| 13 | assert(rosc_hw->status & ROSC_STATUS_ENABLED_BITS); |
| 14 | for(int i=0;i<cycles;i++) { |
| 15 | // picked a fairly arbitrary polynomial of 0x35u - this doesn't have to be crazily uniform. |
| 16 | byte = ((byte << 1) | rosc_hw->randombit) ^ (byte & 0x80u ? 0x35u : 0); |
| 17 | // delay a little because the random bit is a little slow |
| 18 | busy_wait_at_least_cycles(30); |
| 19 | } |
| 20 | return byte; |
| 21 | } |
| 22 | |
| 23 | unsigned int pico_lwip_rand(void) { |
| 24 | uint32_t value = 0; |
| 25 | for (int i = 0; i < 4; i++) { |
| 26 | value = (value << 8u) | pico_lwip_random_byte(32); |
| 27 | } |
| 28 | return value; |
| 29 | } |