added random number generator driver and stuff

This commit is contained in:
Thomas Klaehn 2016-08-30 17:04:22 +02:00
parent b777ab9e6f
commit d7e6f8b0ee
11 changed files with 233 additions and 6 deletions

View File

@ -146,6 +146,7 @@
<buildTargets> <buildTargets>
<target name="all" path="software/source/test/firmware/kernel/list" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="all" path="software/source/test/firmware/kernel/list" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>all</buildTarget> <buildTarget>all</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -153,6 +154,7 @@
</target> </target>
<target name="clean" path="software/source/test/firmware/kernel/list" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="clean" path="software/source/test/firmware/kernel/list" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>clean</buildTarget> <buildTarget>clean</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -160,7 +162,6 @@
</target> </target>
<target name="distclean" path="software/source" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="distclean" path="software/source" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>distclean</buildTarget> <buildTarget>distclean</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>false</useDefaultCommand> <useDefaultCommand>false</useDefaultCommand>
@ -216,6 +217,7 @@
</target> </target>
<target name="all" path="software/test/firmware/kernel/list" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="all" path="software/test/firmware/kernel/list" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>all</buildTarget> <buildTarget>all</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -247,6 +249,7 @@
</target> </target>
<target name="distclean" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="distclean" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>distclean</buildTarget> <buildTarget>distclean</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -292,6 +295,14 @@
<useDefaultCommand>false</useDefaultCommand> <useDefaultCommand>false</useDefaultCommand>
<runAllBuilders>true</runAllBuilders> <runAllBuilders>true</runAllBuilders>
</target> </target>
<target name="rng test" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments>TEST_APP=rng BOARD=stm32f4-discovery DEBUG=y</buildArguments>
<buildTarget>test</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>false</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
<target name="msp430-ccrf example_radio_rx all" path="software/source/test" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="msp430-ccrf example_radio_rx all" path="software/source/test" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments>APP=example_radio_rx BOARD=msp430-ccrf DEBUG=y</buildArguments> <buildArguments>APP=example_radio_rx BOARD=msp430-ccrf DEBUG=y</buildArguments>
@ -318,6 +329,7 @@
</target> </target>
<target name="all" path="software/source/test/firmware/kernel/ringbuffer" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="all" path="software/source/test/firmware/kernel/ringbuffer" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>all</buildTarget> <buildTarget>all</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -325,6 +337,7 @@
</target> </target>
<target name="clean" path="software/source/test/firmware/kernel/ringbuffer" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="clean" path="software/source/test/firmware/kernel/ringbuffer" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>clean</buildTarget> <buildTarget>clean</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>

View File

@ -24,8 +24,35 @@
#include "gpio.h" #include "gpio.h"
#include "stm32f4_gpio.h" #include "stm32f4_gpio.h"
#include "rng.h"
#include "stm32f4_rng.h"
#include "driver.h" #include "driver.h"
// Random number generator
static RNG_HandleTypeDef stm32f4_rng_handle = {
.Instance = RNG,
};
static struct stm32f4_rng stm32f4_rng = {
.rng_handle = &stm32f4_rng_handle,
};
static struct rng __rng = {
.arch_dep_device = &stm32f4_rng,
.fp = &rng_fp,
};
#ifdef TEST_APP
static const struct driver rng = {
#else
const struct driver rng = {
#endif
.driver_type = DRIVER_TYPE_RNG,
.device_driver = &__rng,
};
// GPIO_C0 // GPIO_C0
static const GPIO_InitTypeDef port_cfg_c0 = { static const GPIO_InitTypeDef port_cfg_c0 = {
.Pin = GPIO_PIN_0, .Pin = GPIO_PIN_0,

View File

@ -0,0 +1,25 @@
/*
* stm32f4_rng.h
*
* Created on: Aug 30, 2016
* Author: tkl
*/
#ifndef SOURCE_FIRMWARE_ARCH_STM32F4XX_INCLUDE_STM32F4_RNG_H_
#define SOURCE_FIRMWARE_ARCH_STM32F4XX_INCLUDE_STM32F4_RNG_H_
struct stm32f4_rng {
RNG_HandleTypeDef *rng_handle;
};
int stm32f4_rng_open(const void *this);
int stm32f4_rng_close(const void *this);
unsigned int stm32f4_rng_read(const void *this);
static const struct rng_fp rng_fp = {
.open = stm32f4_rng_open,
.close = stm32f4_rng_close,
.read = stm32f4_rng_read,
};
#endif /* SOURCE_FIRMWARE_ARCH_STM32F4XX_INCLUDE_STM32F4_RNG_H_ */

View File

@ -0,0 +1,43 @@
/*
* stm32f4_rng.c
*
* Created on: Aug 30, 2016
* Author: tkl
*/
#include <stddef.h>
#include "board.h"
int stm32f4_rng_open(const void *this)
{
if(NULL == this)
return -1;
struct stm32f4_rng *dev = (struct stm32f4_rng *)this;
__HAL_RCC_RNG_CLK_ENABLE();
HAL_RNG_DeInit(dev->rng_handle);
HAL_RNG_Init(dev->rng_handle);
return 0;
}
int stm32f4_rng_close(const void *this)
{
if(NULL == this)
return -1;
struct stm32f4_rng *dev = (struct stm32f4_rng *)this;
__HAL_RCC_RNG_CLK_DISABLE();
HAL_RNG_DeInit(dev->rng_handle);
return 0;
}
unsigned int stm32f4_rng_read(const void *this)
{
if(NULL == this)
return -1;
struct stm32f4_rng *dev = (struct stm32f4_rng *)this;
uint32_t random = 0;
HAL_RNG_GenerateRandomNumber(dev->rng_handle, &random);
return random;
}

View File

@ -12,7 +12,7 @@
#include "gpio.h" #include "gpio.h"
#include "i2c.h" #include "i2c.h"
#include "pwm.h" #include "pwm.h"
//#include "rtc.h" #include "rng.h"
#include "spi.h" #include "spi.h"
#include "uart.h" #include "uart.h"
@ -34,8 +34,10 @@ int drv_open(const struct driver *driver)
case DRIVER_TYPE_PWM: case DRIVER_TYPE_PWM:
ret = pwm_open((const struct pwm *)(driver->device_driver)); ret = pwm_open((const struct pwm *)(driver->device_driver));
break; break;
case DRIVER_TYPE_RNG:
ret = rng_open((const struct rng *)(driver->device_driver));
break;
case DRIVER_TYPE_RTC: case DRIVER_TYPE_RTC:
// ret = rtc_open((const struct rtc *)(driver->device_driver));
break; break;
case DRIVER_TYPE_SPI: case DRIVER_TYPE_SPI:
ret = spi_open((const struct spi *)(driver->device_driver)); ret = spi_open((const struct spi *)(driver->device_driver));
@ -65,8 +67,10 @@ int drv_close(const struct driver *driver)
case DRIVER_TYPE_PWM: case DRIVER_TYPE_PWM:
ret = pwm_close((const struct pwm *)(driver->device_driver)); ret = pwm_close((const struct pwm *)(driver->device_driver));
break; break;
case DRIVER_TYPE_RNG:
ret = rng_close((const struct rng *)(driver->device_driver));
break;
case DRIVER_TYPE_RTC: case DRIVER_TYPE_RTC:
// ret = rtc_close((const struct rtc *)(driver->device_driver));
break; break;
case DRIVER_TYPE_SPI: case DRIVER_TYPE_SPI:
ret = spi_close((const struct spi *)(driver->device_driver)); ret = spi_close((const struct spi *)(driver->device_driver));
@ -96,7 +100,9 @@ int drv_read(const struct driver *driver, char *buffer, int len)
case DRIVER_TYPE_I2C: case DRIVER_TYPE_I2C:
break; break;
case DRIVER_TYPE_PWM: case DRIVER_TYPE_PWM:
ret = -1; break;
case DRIVER_TYPE_RNG:
ret = rng_read((const struct rng *)(driver->device_driver));
break; break;
case DRIVER_TYPE_RTC: case DRIVER_TYPE_RTC:
break; break;
@ -129,7 +135,8 @@ int drv_write(const struct driver *driver, const char *buffer, int len)
case DRIVER_TYPE_I2C: case DRIVER_TYPE_I2C:
break; break;
case DRIVER_TYPE_PWM: case DRIVER_TYPE_PWM:
ret = -1; break;
case DRIVER_TYPE_RNG:
break; break;
case DRIVER_TYPE_RTC: case DRIVER_TYPE_RTC:
break; break;
@ -166,6 +173,8 @@ int drv_ioctl(const struct driver *driver, unsigned int cmd, const void *data)
return pwm_get_pulse_width_ns((const struct pwm *)(driver->device_driver)); return pwm_get_pulse_width_ns((const struct pwm *)(driver->device_driver));
} }
break; break;
case DRIVER_TYPE_RNG:
break;
case DRIVER_TYPE_RTC: case DRIVER_TYPE_RTC:
break; break;
case DRIVER_TYPE_SPI: case DRIVER_TYPE_SPI:

View File

@ -0,0 +1,30 @@
/*
* rng.h
*
* Created on: Aug 30, 2016
* Author: tkl
*/
#ifndef SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_RNG_H_
#define SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_RNG_H_
typedef int (*rng_fp_open_t)(const void*);
typedef int (*rng_fp_close_t)(const void*);
typedef unsigned int (*rng_fp_read_t)(const void*);
struct rng_fp {
const rng_fp_open_t open;
const rng_fp_close_t close;
const rng_fp_read_t read;
};
struct rng {
const void *arch_dep_device;
const struct rng_fp *fp;
};
int rng_open(const struct rng *device);
int rng_close(const struct rng *device);
unsigned int rng_read(const struct rng *device);
#endif /* SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_RNG_H_ */

View File

@ -0,0 +1,36 @@
/*
* rng.c
*
* Created on: Aug 30, 2016
* Author: tkl
*/
#include <stddef.h>
#include "rng.h"
int rng_open(const struct rng *device)
{
if(device == NULL)
return -1;
rng_fp_open_t open = device->fp->open;
return open(device->arch_dep_device);
}
int rng_close(const struct rng *device)
{
if(device == NULL)
return -1;
rng_fp_close_t close = device->fp->close;
return close(device->arch_dep_device);
}
unsigned int rng_read(const struct rng *device)
{
if(device == NULL)
return 0;
rng_fp_read_t read = device->fp->read;
return read(device->arch_dep_device);
}

View File

@ -17,6 +17,7 @@ enum driver_type {
DRIVER_TYPE_GPIO, DRIVER_TYPE_GPIO,
DRIVER_TYPE_I2C, DRIVER_TYPE_I2C,
DRIVER_TYPE_PWM, DRIVER_TYPE_PWM,
DRIVER_TYPE_RNG,
DRIVER_TYPE_RTC, DRIVER_TYPE_RTC,
DRIVER_TYPE_SPI, DRIVER_TYPE_SPI,
DRIVER_TYPE_UART DRIVER_TYPE_UART

38
source/test/rng/main.c Normal file
View File

@ -0,0 +1,38 @@
/*
* main.c
*
* Created on: Aug 2, 2016
* Author: tkl
*/
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include "driver.h"
#include "board.h"
#include "stack.h"
#include "queue.h"
#include "kernel.h"
#include "driver.h"
#include "list.h"
#include "shell.h"
int main(void)
{
char print_buffer[50];
shell_init(&uart_1);
drv_open(&rng);
while(1) {
int random = drv_read(&rng, NULL, 0);
sprintf(print_buffer, "random: %d\r\n", random);
shell_write(print_buffer, strlen(print_buffer));
sleep_ms(1000);
}
return 0;
}

1
source/test/rng/rng.mk Normal file
View File

@ -0,0 +1 @@
SRC_DIR += source/test/rng

View File

@ -6,6 +6,10 @@ ifeq ($(TEST_APP), pwm)
include source/test/pwm/pwm.mk include source/test/pwm/pwm.mk
endif endif
ifeq ($(TEST_APP), rng)
include source/test/rng/rng.mk
endif
ifeq ($(TEST_APP), shell) ifeq ($(TEST_APP), shell)
include source/test/shell/shell.mk include source/test/shell/shell.mk
endif endif