add rng
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#ifndef _FLASH_H_H
|
||||
#define _FLASH_H_H
|
||||
#ifndef _FLASH_H_
|
||||
#define _FLASH_H_
|
||||
|
||||
void flash_erase_page(uint8_t page);
|
||||
void flash_write_dword(uint32_t addr, uint64_t data);
|
||||
|
@@ -11,6 +11,7 @@
|
||||
#include "stm32l4xx_ll_usart.h"
|
||||
#include "stm32l4xx_ll_bus.h"
|
||||
#include "stm32l4xx_ll_tim.h"
|
||||
#include "stm32l4xx_ll_rng.h"
|
||||
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
@@ -30,6 +31,7 @@ static void MX_GPIO_Init(void);
|
||||
static void MX_USART1_UART_Init(void);
|
||||
static void MX_TIM2_Init(void);
|
||||
static void MX_TIM6_Init(void);
|
||||
static void MX_RNG_Init(void);
|
||||
|
||||
#define Error_Handler() _Error_Handler(__FILE__,__LINE__)
|
||||
void _Error_Handler(char *file, int line);
|
||||
@@ -63,6 +65,7 @@ void hw_init(void)
|
||||
MX_TIM2_Init();
|
||||
|
||||
MX_TIM6_Init();
|
||||
MX_RNG_Init();
|
||||
|
||||
TIM6->SR = 0;
|
||||
__enable_irq();
|
||||
@@ -299,3 +302,14 @@ static void MX_TIM6_Init(void)
|
||||
// Start immediately
|
||||
LL_TIM_EnableCounter(TIM6);
|
||||
}
|
||||
|
||||
/* RNG init function */
|
||||
static void MX_RNG_Init(void)
|
||||
{
|
||||
|
||||
/* Peripheral clock enable */
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_RNG);
|
||||
|
||||
LL_RNG_Enable(RNG);
|
||||
|
||||
}
|
||||
|
@@ -15,13 +15,14 @@
|
||||
|
||||
#include "app.h"
|
||||
#include "flash.h"
|
||||
#include "rng.h"
|
||||
|
||||
#define Error_Handler() _Error_Handler(__FILE__,__LINE__)
|
||||
|
||||
#define LED_PIN_G LL_GPIO_PIN_0
|
||||
#define LED_PIN_B LL_GPIO_PIN_1
|
||||
#define LED_PIN_R LL_GPIO_PIN_2
|
||||
#define LED_PORT GPIOA
|
||||
#define LED_PORT GPIOA
|
||||
|
||||
void hw_init(void);
|
||||
|
||||
@@ -140,11 +141,22 @@ void TIM6_DAC_IRQHandler()
|
||||
__65_seconds += 1;
|
||||
}
|
||||
|
||||
void dump_hex(uint8_t * b, int len)
|
||||
{
|
||||
while(len--)
|
||||
{
|
||||
printf("%02x ", *b++);
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
uint8_t str[] = "YouCompleteMe: a code-completion engine for Vim";
|
||||
uint8_t buf[500];
|
||||
uint8_t buf[5000];
|
||||
uint32_t i = 0;
|
||||
float ent;
|
||||
float test = 1235.889944f;
|
||||
hw_init();
|
||||
printf("hello solo\r\n");
|
||||
|
||||
@@ -169,6 +181,12 @@ int main(void)
|
||||
uint32_t t2 = millis();
|
||||
printf("100 ms delay (%lu)\r\n",t2-t1);
|
||||
|
||||
|
||||
ent = rng_test(64 * 1024);
|
||||
|
||||
printf("entropy of 64KB from RNG: %.6f\r\n", ent);
|
||||
printf("test float: %.2f\r\n", test);
|
||||
|
||||
// Test PWM + weighting of RGB
|
||||
test_colors();
|
||||
|
||||
|
85
targets/stm32l442/src/rng.c
Normal file
85
targets/stm32l442/src/rng.c
Normal file
@@ -0,0 +1,85 @@
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "stm32l4xx_ll_rng.h"
|
||||
|
||||
#include "rng.h"
|
||||
|
||||
int __errno = 0;
|
||||
|
||||
void rng_get_bytes(uint8_t * dst, size_t sz)
|
||||
{
|
||||
uint8_t r[8];
|
||||
unsigned int i,j;
|
||||
for (i = 0; i < sz; i += 4)
|
||||
{
|
||||
while( !LL_RNG_IsActiveFlag_DRDY(RNG) )
|
||||
;
|
||||
*(uint32_t*)&r = LL_RNG_ReadRandData32(RNG);
|
||||
|
||||
if (RNG->SR & 0x66)
|
||||
{
|
||||
printf("Error RNG: %02lx\r\n", RNG->SR);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (j = 0; j < 4; j++)
|
||||
{
|
||||
if ((i + j) > sz)
|
||||
{
|
||||
return;
|
||||
}
|
||||
dst[i + j] = r[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float shannon_entropy(float * p, size_t sz)
|
||||
{
|
||||
|
||||
unsigned int i;
|
||||
float entropy = 0.0f;
|
||||
|
||||
for(i=0; i < sz; i++)
|
||||
{
|
||||
if (p[i] > 0.0)
|
||||
{
|
||||
entropy -= p[i] * (float) log( (double) p[i]);
|
||||
}
|
||||
}
|
||||
|
||||
entropy = entropy / (float) log ((double) 2.0);
|
||||
|
||||
return entropy;
|
||||
}
|
||||
|
||||
// Measure shannon entropy of RNG
|
||||
float rng_test(size_t n)
|
||||
{
|
||||
unsigned int i;
|
||||
int sz = 0;
|
||||
uint8_t buf[4];
|
||||
int counts[256];
|
||||
float p[256];
|
||||
|
||||
memset(counts, 0, sizeof(counts));
|
||||
|
||||
for(i=0; i < n; i+=4)
|
||||
{
|
||||
rng_get_bytes(buf, 4);
|
||||
sz += 4;
|
||||
|
||||
counts[buf[0]]++;
|
||||
counts[buf[1]]++;
|
||||
counts[buf[2]]++;
|
||||
counts[buf[3]]++;
|
||||
}
|
||||
|
||||
for (i = 0; i < 256; i++)
|
||||
{
|
||||
p[i] = ((float)counts[i])/sz;
|
||||
}
|
||||
|
||||
return shannon_entropy(p, 256);
|
||||
}
|
9
targets/stm32l442/src/rng.h
Normal file
9
targets/stm32l442/src/rng.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef _RNG_H_
|
||||
#define _RNG_H_
|
||||
#include <stdint.h>
|
||||
|
||||
void rng_get_bytes(uint8_t * dst, size_t sz);
|
||||
float shannon_entropy(float * p, size_t sz);
|
||||
float rng_test(size_t n);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user