|
| 1 | +#include <stdint.h> |
| 2 | +#include <wolfHAL/rng/stm32wb_rng.h> |
| 3 | +#include <wolfHAL/rng/rng.h> |
| 4 | +#include <wolfHAL/clock/clock.h> |
| 5 | +#include <wolfHAL/error.h> |
| 6 | +#include <wolfHAL/regmap.h> |
| 7 | +#include <wolfHAL/bitops.h> |
| 8 | + |
| 9 | +/* |
| 10 | + * STM32WB RNG Register Definitions |
| 11 | + * |
| 12 | + * The RNG peripheral uses an analog noise source to produce 32-bit |
| 13 | + * random values. One value is available at a time in DR, signaled |
| 14 | + * by the DRDY flag in SR. |
| 15 | + */ |
| 16 | + |
| 17 | +/* Control Register */ |
| 18 | +#define SRNG_CR_REG 0x00 |
| 19 | +#define SRNG_CR_RNGEN WHAL_MASK(2) /* RNG enable */ |
| 20 | +#define SRNG_CR_CED WHAL_MASK(5) /* Clock error detection disable */ |
| 21 | + |
| 22 | +/* Status Register */ |
| 23 | +#define SRNG_SR_REG 0x04 |
| 24 | +#define SRNG_SR_DRDY WHAL_MASK(0) /* Data ready */ |
| 25 | +#define SRNG_SR_CECS WHAL_MASK(1) /* Clock error current status */ |
| 26 | +#define SRNG_SR_SECS WHAL_MASK(2) /* Seed error current status */ |
| 27 | +#define SRNG_SR_CEIS WHAL_MASK(5) /* Clock error interrupt status */ |
| 28 | +#define SRNG_SR_SEIS WHAL_MASK(6) /* Seed error interrupt status */ |
| 29 | + |
| 30 | +/* Data Register - 32-bit random value */ |
| 31 | +#define SRNG_DR_REG 0x08 |
| 32 | + |
| 33 | +whal_Error whal_Stm32wbRng_Init(whal_Rng *rngDev) |
| 34 | +{ |
| 35 | + whal_Error err; |
| 36 | + whal_Stm32wbRng_Cfg *cfg; |
| 37 | + |
| 38 | + cfg = (whal_Stm32wbRng_Cfg *)rngDev->cfg; |
| 39 | + |
| 40 | + err = whal_Clock_Enable(cfg->clkCtrl, cfg->clk); |
| 41 | + if (err != WHAL_SUCCESS) { |
| 42 | + return err; |
| 43 | + } |
| 44 | + |
| 45 | + return WHAL_SUCCESS; |
| 46 | +} |
| 47 | + |
| 48 | +whal_Error whal_Stm32wbRng_Deinit(whal_Rng *rngDev) |
| 49 | +{ |
| 50 | + whal_Error err; |
| 51 | + whal_Stm32wbRng_Cfg *cfg = (whal_Stm32wbRng_Cfg *)rngDev->cfg; |
| 52 | + |
| 53 | + err = whal_Clock_Disable(cfg->clkCtrl, cfg->clk); |
| 54 | + if (err) { |
| 55 | + return err; |
| 56 | + } |
| 57 | + |
| 58 | + return WHAL_SUCCESS; |
| 59 | +} |
| 60 | + |
| 61 | +whal_Error whal_Stm32wbRng_Generate(whal_Rng *rngDev, uint8_t *rngData, size_t rngDataSz) |
| 62 | +{ |
| 63 | + whal_Error err = WHAL_SUCCESS; |
| 64 | + const whal_Regmap *reg = &rngDev->regmap; |
| 65 | + size_t status; |
| 66 | + size_t offset = 0; |
| 67 | + |
| 68 | + /* Enable the RNG peripheral */ |
| 69 | + whal_Reg_Update(reg->base, SRNG_CR_REG, SRNG_CR_RNGEN, |
| 70 | + whal_SetBits(SRNG_CR_RNGEN, 1)); |
| 71 | + |
| 72 | + while (offset < rngDataSz) { |
| 73 | + /* Wait for a random value to be ready */ |
| 74 | + do { |
| 75 | + /* Check for seed or clock error */ |
| 76 | + whal_Reg_Get(reg->base, SRNG_SR_REG, SRNG_SR_SECS, &status); |
| 77 | + if (status) { |
| 78 | + err = WHAL_EINVAL; |
| 79 | + goto exit; |
| 80 | + } |
| 81 | + whal_Reg_Get(reg->base, SRNG_SR_REG, SRNG_SR_CECS, &status); |
| 82 | + if (status) { |
| 83 | + err = WHAL_EINVAL; |
| 84 | + goto exit; |
| 85 | + } |
| 86 | + |
| 87 | + whal_Reg_Get(reg->base, SRNG_SR_REG, SRNG_SR_DRDY, &status); |
| 88 | + } while (!status); |
| 89 | + |
| 90 | + /* Read 32-bit random value */ |
| 91 | + uint32_t rnd = *(volatile uint32_t *)(reg->base + SRNG_DR_REG); |
| 92 | + |
| 93 | + /* Copy bytes into output buffer */ |
| 94 | + for (size_t i = 0; i < 4 && offset < rngDataSz; i++, offset++) { |
| 95 | + rngData[offset] = (uint8_t)(rnd >> (i * 8)); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | +exit: |
| 100 | + /* Disable the RNG peripheral */ |
| 101 | + whal_Reg_Update(reg->base, SRNG_CR_REG, SRNG_CR_RNGEN, |
| 102 | + whal_SetBits(SRNG_CR_RNGEN, 0)); |
| 103 | + |
| 104 | + return err; |
| 105 | +} |
| 106 | + |
| 107 | +const whal_RngDriver whal_Stm32wbRng_Driver = { |
| 108 | + .Init = whal_Stm32wbRng_Init, |
| 109 | + .Deinit = whal_Stm32wbRng_Deinit, |
| 110 | + .Generate = whal_Stm32wbRng_Generate, |
| 111 | +}; |
0 commit comments