samples: Add random driver sample.

Change-Id: If0552cbd478e49c71bbfa02448113a3a0e3b4463
Signed-off-by: Marcus Shawcroft <marcus.shawcroft@arm.com>
This commit is contained in:
Marcus Shawcroft 2016-12-04 22:32:54 +00:00 committed by Anas Nashif
parent 4127775bcf
commit c66adc646d
6 changed files with 88 additions and 0 deletions

View file

@ -0,0 +1,4 @@
BOARD ?= frdm_k64f
CONF_FILE = prj.conf
include ${ZEPHYR_BASE}/Makefile.inc

View file

@ -0,0 +1,23 @@
Title: Random
Description:
A simple random number generation example
--------------------------------------------------------------------------------
Building and Running Project:
This project writes an infinite series of random numbers to the
console, 10 per second.
It can be built and executed on frdm_k64f as follows:
make
Sample Output:
Random Example! arm
random device is 0x2000008c, name is RANDOM_0
0xd7 0x42 0xb0 0x7b 0x56 0x3b 0xc3 0x43 0x8a 0xa3
0xfa 0xec 0xd8 0xc3 0x36 0xf8 0x7b 0x82 0x2b 0x39

View file

@ -0,0 +1,2 @@
CONFIG_RANDOM_GENERATOR=y
CONFIG_STDOUT_CONSOLE=y

View file

@ -0,0 +1 @@
obj-y = main.o

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2016 ARM Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <zephyr.h>
#include <random.h>
#include <stdio.h>
void main(void)
{
struct device *dev;
printf("Random Example! %s\n", CONFIG_ARCH);
dev = device_get_binding(CONFIG_RANDOM_NAME);
if (!dev) {
printf("error: no random device\n");
return;
}
printf("random device is %p, name is %s\n",
dev, dev->config->name);
while (1) {
#define BUFFER_LENGTH 10
uint8_t buffer[BUFFER_LENGTH];
int r;
r = random_get_entropy(dev, buffer, BUFFER_LENGTH);
if (r) {
printf("random_get_entropy failed: %d\n", r);
break;
};
for (int i = 0; i < BUFFER_LENGTH; i++) {
printf(" 0x%x", buffer[i]);
};
printf("\n");
k_sleep(1000);
}
}

View file

@ -0,0 +1,3 @@
[test]
build_only = true
tags = samples