added random number generator driver and stuff
This commit is contained in:
@@ -24,8 +24,35 @@
|
||||
|
||||
#include "gpio.h"
|
||||
#include "stm32f4_gpio.h"
|
||||
|
||||
#include "rng.h"
|
||||
#include "stm32f4_rng.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
|
||||
static const GPIO_InitTypeDef port_cfg_c0 = {
|
||||
.Pin = GPIO_PIN_0,
|
||||
|
25
source/firmware/arch/stm32f4xx/driver/include/stm32f4_rng.h
Normal file
25
source/firmware/arch/stm32f4xx/driver/include/stm32f4_rng.h
Normal 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_ */
|
43
source/firmware/arch/stm32f4xx/driver/stm32f4_rng.c
Normal file
43
source/firmware/arch/stm32f4xx/driver/stm32f4_rng.c
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user