/*
 * stm32f4_rng.c
 *
 *  Created on: Aug 30, 2016
 *      Author: tkl
 */

#include <stddef.h>

#include "rng.h"
#include "stm32f4xx.h"
#include "stm32f4_rng.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;
}
