Skip to content

Commit c66adc6

Browse files
mshawcroftAnas Nashif
authored andcommitted
samples: Add random driver sample.
Change-Id: If0552cbd478e49c71bbfa02448113a3a0e3b4463 Signed-off-by: Marcus Shawcroft <[email protected]>
1 parent 4127775 commit c66adc6

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

samples/drivers/random/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
BOARD ?= frdm_k64f
2+
CONF_FILE = prj.conf
3+
4+
include ${ZEPHYR_BASE}/Makefile.inc

samples/drivers/random/README.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Title: Random
2+
3+
Description:
4+
5+
A simple random number generation example
6+
7+
--------------------------------------------------------------------------------
8+
9+
Building and Running Project:
10+
11+
This project writes an infinite series of random numbers to the
12+
console, 10 per second.
13+
14+
It can be built and executed on frdm_k64f as follows:
15+
16+
make
17+
18+
Sample Output:
19+
20+
Random Example! arm
21+
random device is 0x2000008c, name is RANDOM_0
22+
0xd7 0x42 0xb0 0x7b 0x56 0x3b 0xc3 0x43 0x8a 0xa3
23+
0xfa 0xec 0xd8 0xc3 0x36 0xf8 0x7b 0x82 0x2b 0x39

samples/drivers/random/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_RANDOM_GENERATOR=y
2+
CONFIG_STDOUT_CONSOLE=y

samples/drivers/random/src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
obj-y = main.o

samples/drivers/random/src/main.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2016 ARM Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <zephyr.h>
18+
#include <random.h>
19+
#include <stdio.h>
20+
21+
void main(void)
22+
{
23+
struct device *dev;
24+
25+
printf("Random Example! %s\n", CONFIG_ARCH);
26+
27+
dev = device_get_binding(CONFIG_RANDOM_NAME);
28+
if (!dev) {
29+
printf("error: no random device\n");
30+
return;
31+
}
32+
33+
printf("random device is %p, name is %s\n",
34+
dev, dev->config->name);
35+
36+
while (1) {
37+
#define BUFFER_LENGTH 10
38+
uint8_t buffer[BUFFER_LENGTH];
39+
int r;
40+
41+
r = random_get_entropy(dev, buffer, BUFFER_LENGTH);
42+
if (r) {
43+
printf("random_get_entropy failed: %d\n", r);
44+
break;
45+
};
46+
47+
for (int i = 0; i < BUFFER_LENGTH; i++) {
48+
printf(" 0x%x", buffer[i]);
49+
};
50+
51+
printf("\n");
52+
53+
k_sleep(1000);
54+
}
55+
}

samples/drivers/random/testcase.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[test]
2+
build_only = true
3+
tags = samples

0 commit comments

Comments
 (0)