Skip to content
Snippets Groups Projects
Commit 23f4f9be authored by neiljay's avatar neiljay Committed by GitHub
Browse files

Merge pull request #7386 from francois-berder/pic32-periph-rng

Wi-Fire: Add hardware RNG peripheral support
parents e1bbf437 d8984a6a
No related branches found
No related tags found
No related merge requests found
# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio
FEATURES_PROVIDED += periph_hwrng
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart
......
......@@ -11,6 +11,7 @@
#include <stdio.h>
#include <stdint.h>
#include "periph/gpio.h"
#include "periph/hwrng.h"
#include "periph/uart.h"
#include "bitarithm.h"
#include "board.h"
......@@ -31,6 +32,8 @@ void board_init(void)
uart_init(DEBUG_VIA_UART, DEBUG_UART_BAUD, NULL, 0);
#endif
hwrng_init();
/* Turn off all LED's */
gpio_init(LED1_PIN, GPIO_OUT);
gpio_init(LED2_PIN, GPIO_OUT);
......
/*
* Copyright(C) 2017 Francois Berder <fberder@outlook.fr>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
*/
#include <string.h>
#include "board.h"
#include "periph/hwrng.h"
#ifdef _RNG
static void wait_plen_cycles(void)
{
unsigned int i;
for (i = 0; i < (RNGCON & _RNGCON_PLEN_MASK); ++i)
__asm volatile ("nop");
}
void hwrng_init(void)
{
RNGCON = _RNGCON_TRNGEN_MASK;
/*
* Wait to have at least 64 bits before setting the 64-bit seed
* of the pseudo random generator.
*/
while (RNGCNT < 64) {}
/* Load seed from the TRNG */
RNGCON |= _RNGCON_LOAD_MASK;
while (RNGCON & _RNGCON_LOAD_MASK) {}
RNGCON &= ~_RNGCON_TRNGEN_MASK;
RNGPOLY1 = 0x00C00003;
RNGPOLY2 = 0x00000000;
RNGCON |= 42; /* Set PLEN to 42 */
RNGCON |= _RNGCON_CONT_MASK;
}
void hwrng_read(void *buf, unsigned int num)
{
unsigned int i = 0;
uint8_t *buffer = (uint8_t *)buf;
RNGCON |= _RNGCON_PRNGEN_MASK;
for (i = 0; i < (num >> 3); ++i) {
uint32_t rng1, rng2;
wait_plen_cycles();
rng1 = RNGNUMGEN1;
rng2 = RNGNUMGEN2;
memcpy(buffer, &rng1, sizeof(rng1));
memcpy(buffer + 4, &rng2, sizeof(rng2));
buffer += 8;
}
num &= 0x7;
if (num) {
uint32_t rng1, n = num & 0x3;
wait_plen_cycles();
rng1 = RNGNUMGEN1;
memcpy(buffer, &rng1, n);
num -= n;
buffer += n;
if (num) {
uint32_t rng2 = RNGNUMGEN2;
memcpy(buffer, &rng2, num);
}
}
RNGCON &= ~_RNGCON_PRNGEN_MASK;
}
#endif /* _RNG */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment