source: rtems/bsps/arm/atsam/start/getentropy-trng.c @ c344e58

5
Last change on this file since c344e58 was c344e58, checked in by Sebastian Huber <sebastian.huber@…>, on 02/02/20 at 10:00:54

Use RTEMS_SYSINIT_ORDER_LAST_BUT_5

Use RTEMS_SYSINIT_ORDER_LAST_BUT_5 instead of RTEMS_SYSINIT_ORDER_LAST
to allow applications and support functions to place system
initialization handlers behind the standard handlers.

Update #3838.

  • Property mode set to 100644
File size: 1.2 KB
Line 
1/*
2 * Copyright (c) 2017 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#include <libchip/chip.h>
16#include <unistd.h>
17#include <string.h>
18#include <rtems/sysinit.h>
19
20static void atsam_trng_enable(void)
21{
22        PMC_EnablePeripheral(ID_TRNG);
23        TRNG_Enable();
24}
25
26int getentropy(void *ptr, size_t n)
27{
28        while (n > 0) {
29                uint32_t random;
30                size_t copy;
31
32                while ((TRNG_GetStatus() & TRNG_ISR_DATRDY) == 0) {
33                        /* wait */
34                }
35                random = TRNG_GetRandData();
36
37                /*
38                 * Read TRNG status one more time to avoid race condition.
39                 * Otherwise we can read (and clear) an old ready status but get
40                 * a new value. The ready status for this value wouldn't be
41                 * reset.
42                 */
43                TRNG_GetStatus();
44
45                copy = sizeof(random);
46                if (n < copy ) {
47                        copy = n;
48                }
49                memcpy(ptr, &random, copy);
50                n -= copy;
51                ptr += copy;
52        }
53
54        return 0;
55}
56
57RTEMS_SYSINIT_ITEM(
58  atsam_trng_enable,
59  RTEMS_SYSINIT_DEVICE_DRIVERS,
60  RTEMS_SYSINIT_ORDER_LAST_BUT_5
61);
Note: See TracBrowser for help on using the repository browser.