Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples_v30x/rng_rand_test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
all : flash

TARGET:=rng_rand_test
TARGET_MCU:=CH32V303
TARGET_MCU_PACKAGE:=CH32V303

include ../../ch32fun/ch32fun.mk

flash : cv_flash
clean : cv_clean


25 changes: 25 additions & 0 deletions examples_v30x/rng_rand_test/ch32v30x_rng.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "ch32fun.h"

static void RNG_init(void) {
// Enable RNG clock
RCC->AHBPCENR |= RCC_AHBPeriph_RNG;

// Enable the RNG
RNG->CR |= RNG_CR_RNGEN;
}

// Get a random number (blocking)
static u32 RNG_rand(void) {
// Wait until data is ready
while(!(RNG->SR & RNG_SR_DRDY)) {
// Handle seed error
if(!(RNG->SR & RNG_SR_SECS)) continue;

RNG->CR &= ~RNG_CR_RNGEN; // Disable RNG
RNG->SR = 0; // Clear error flags
RNG->CR |= RNG_CR_RNGEN; // Re-enable RNG
}

// Read and return random number
return RNG->DR;
}
13 changes: 13 additions & 0 deletions examples_v30x/rng_rand_test/funconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _FUNCONFIG_H
#define _FUNCONFIG_H

#define FUNCONF_SYSTICK_USE_HCLK 1
#define FUNCONF_USE_HSI 1
#define FUNCONF_USE_PLL 0
#define FUNCONF_PLL_MULTIPLIER 2

#define FUNCONF_USE_DEBUGPRINTF 1
#define FUNCONF_SYSTEM_CORE_CLOCK 8 * 1000 * 1000

#endif

19 changes: 19 additions & 0 deletions examples_v30x/rng_rand_test/rng_rand_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Simple example that shows how to use the Random Number Generator functions

#include "ch32fun.h"
#include <stdio.h>
#include "ch32v30x_rng.h"

int main() {
SystemInit();
Delay_Ms(100);

printf("\n~ Random Number Test ~\n");
RNG_init();

while(1) {
u32 rand = RNG_rand();
printf("Dec=%10u, Hex=0x%08X\n", rand, rand);
Delay_Ms(1000);
}
}