Blame view

RIOT/cpu/stm32_common/periph/hwrng.c 1.81 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  /*
   * Copyright (C) 2014-2017 Freie Universitรคt Berlin
   *               2016 OTA keys S.A.
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     cpu_stm32_common
   * @ingroup     drivers_periph_hwrng
   * @{
   *
   * @file
   * @brief       Low-level random number generator driver implementation
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   * @author      Aurelien Gonce <aurelien.gonce@altran.fr>
   *
   * @}
   */
  
  #include "cpu.h"
  #include "periph_conf.h"
  #include "periph/hwrng.h"
  
  /* only build if the CPU actually provides a RNG peripheral */
  #ifdef RNG
  
  void hwrng_init(void)
  {
      /* no need for initialization */
  }
  
  void hwrng_read(void *buf, unsigned int num)
  {
      unsigned int count = 0;
      uint8_t *b = (uint8_t *)buf;
  
      /* power on and enable the device */
  #if defined(CPU_MODEL_STM32F410RB)
      periph_clk_en(AHB1, RCC_AHB1ENR_RNGEN);
  #elif defined(CPU_FAM_STM32L0)
      periph_clk_en(AHB, RCC_AHBENR_RNGEN);
  #else
      periph_clk_en(AHB2, RCC_AHB2ENR_RNGEN);
  #endif
      RNG->CR = RNG_CR_RNGEN;
  
      /* get random data */
      while (count < num) {
          /* wait for random data to be ready to read */
          while (!(RNG->SR & RNG_SR_DRDY)) {}
          /* read next 4 bytes */
          uint32_t tmp = RNG->DR;
          /* copy data into result vector */
          for (int i = 0; i < 4 && count < num; i++) {
              b[count++] = (uint8_t)tmp;
              tmp = tmp >> 8;
          }
      }
  
      /* finally disable the device again */
      RNG->CR = 0;
  #if defined(CPU_MODEL_STM32F410RB)
      periph_clk_dis(AHB1, RCC_AHB1ENR_RNGEN);
  #elif defined(CPU_FAM_STM32L0)
      periph_clk_dis(AHB, RCC_AHBENR_RNGEN);
  #else
      periph_clk_dis(AHB2, RCC_AHB2ENR_RNGEN);
  #endif
  }
  
  #endif /* RNG */