Issue #20: Implement driver for RNG

This commit is contained in:
Mario Hüttel 2020-09-07 20:47:56 +02:00
parent ab8228f712
commit cbbd97e1bd
3 changed files with 113 additions and 0 deletions

View File

@ -36,6 +36,7 @@ DEFINES += -DSHELLMATTA_HELP_ALIAS=\"?\"
# RCC Manager
CFILES += stm-periph/clock-enable-manager.c
CFILES += stm-periph/uart.c stm-periph/dma-ring-buffer.c stm-periph/backup-ram.c
CFILES += stm-periph/rng.c
CFILES += digio.c
CFILES += stm-periph/unique-id.c
CFILES += calibration.c

View File

@ -0,0 +1,42 @@
/* Reflow Oven Controller
*
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of the Reflow Oven Controller Project.
*
* The reflow oven controller is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* GDSII-Converter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the reflow oven controller project.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __STM_RNG_H__
#define __STM_RNG_H__
#include <stdint.h>
#include <stdbool.h>
enum random_number_error {
RNG_ERROR_OK = 0,
RNG_ERROR_INACT,
RNG_ERROR_INTERNAL_ERROR,
RNG_ERROR_NOT_READY
};
void random_number_gen_init(bool int_enable);
void random_number_gen_deinit();
void random_number_gen_reset(bool int_en);
enum random_number_error random_number_gen_get_number(uint32_t *random_number, bool wait_for_valid_value);
#endif /* __STM_RNG_H__ */

View File

@ -0,0 +1,70 @@
/* Reflow Oven Controller
*
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of the Reflow Oven Controller Project.
*
* The reflow oven controller is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the reflow oven controller project.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stm-periph/rng.h>
#include <stm-periph/clock-enable-manager.h>
#include <stm32/stm32f4xx.h>
void random_number_gen_init(bool int_enable)
{
rcc_manager_enable_clock(&RCC->AHB2ENR, BITMASK_TO_BITNO(RCC_AHB2ENR_RNGEN));
__DSB();
random_number_gen_reset(int_enable);
}
void random_number_gen_deinit()
{
RNG->CR = 0;
__DSB();
rcc_manager_disable_clock(&RCC->AHB2ENR, BITMASK_TO_BITNO(RCC_AHB2ENR_RNGEN));
}
void random_number_gen_reset(bool int_en)
{
RNG->CR = 0;
__DSB();
RNG->CR = RNG_CR_RNGEN | (int_en ? RNG_CR_IE : 0U);
}
enum random_number_error random_number_gen_get_number(uint32_t *random_number, bool wait_for_valid_value)
{
bool value_ready;
if (!(RNG->CR & RNG_CR_RNGEN))
return RNG_ERROR_INACT;
if (RNG->SR & RNG_SR_SEIS || RNG->SR & RNG_SR_CEIS) {
/* Error detected */
return RNG_ERROR_INTERNAL_ERROR;
}
/* Check if the value is ready. Wait if wait_for_valid_value is true */
do {
value_ready = !!(RNG->SR & RNG_SR_DRDY);
} while (!value_ready && wait_for_valid_value);
/* If the value is valid, return it */
if (value_ready && random_number)
*random_number = RNG->DR;
/* Return from function with proper status */
return (value_ready ? RNG_ERROR_OK : RNG_ERROR_NOT_READY);
}