getting HRNG to work

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
kiwi
Posts: 13
Joined: Mon Feb 23, 2015 3:47 pm

getting HRNG to work

Post by kiwi »

Hello guys,

I'm using a STM32F4 and I'm currently trying to get the build in RNG to work. Did any of you ever tried this before?
I used stm32f4xx.c and stm32f4xx.h and also added the corresponding stm32f4xx_rcc.c and stm32f4xx_rcc.h to get it to compile. (I also had to delete all the asset_param statements since they are undefined.)
But so far RNG_GetFlagStatus only returns RESET.
Do you have any ideas?

thanks,
kiwi

Code: Select all

#include "ch.h"
#include "hal.h"
/* RNGDriver */
#include "stm32f4xx_rng.h"

/* RNG Thread */
static WORKING_AREA(rngThreadWorkingArea, 128);

/* TODO: handle with interrupt */
static msg_t rngThread(void* arg) {
  (void) arg;
  while (TRUE) {
    uint32_t random;
    if(RNG_GetFlagStatus(RNG_FLAG_DRDY) == SET) {
      palSetPad(GPIOG, GPIOG_LED4_RED);
      random = RNG_GetRandomNumber();
    } else if(RNG_GetFlagStatus(RNG_FLAG_CECS) == SET) {
      RNG_ClearITPendingBit(RNG_IT_CEI);
    } else if(RNG_GetFlagStatus(RNG_FLAG_SECS) == SET) {
      RNG_ClearITPendingBit(RNG_IT_SEI);
    }
    // wait.
    chThdSleepMilliseconds(500);
  }
}


int main(void)
{
  chSysInit();
  halInit();

  /* enable TRNG */
  RNG_DeInit();
  RNG_Cmd(ENABLE);
  /* Start thread to read random numbers */
  (void)chThdCreateStatic(rngThreadWorkingArea, sizeof(rngThreadWorkingArea),
                          NORMALPRIO, rngThread, NULL);

  while(TRUE) {

  }

  /* Return success */
  return 1;
}
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: getting HRNG to work

Post by Giovanni »

Hi,

Maybe you have to enable the clock for RNG.

Giovanni
kiwi
Posts: 13
Joined: Mon Feb 23, 2015 3:47 pm

Re: getting HRNG to work

Post by kiwi »

Hi Giovanni,

can you tell me how to do that or point me to an example?

EDIT:
You probably mean this, right?

Code: Select all

RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE)


EDIT2:
I have a follow up question, how do I register an interrupt on the RNG?

best,
kiwi
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: getting HRNG to work

Post by Giovanni »

Hi,

Right for the clock.

About ISRs, see here: http://www.chibios.org/dokuwiki/doku.ph ... interrupts

The ISR name is always "VectorXXX", XXX in your case is 180 (hex offset in the vectors table).

In order to enable the IRQ on the NVIC:

Code: Select all

    nvicEnableVector(80, CORTEX_PRIORITY_MASK(<priority>));


Valid priorities go from 2 to 15, 0 and 1 are reserved.

Giovanni
kiwi
Posts: 13
Joined: Mon Feb 23, 2015 3:47 pm

Re: getting HRNG to work

Post by kiwi »

Hi,

I got the ISR running and it is working as expected, but I still do not understand some things.
can you explain them to me?
How do you know you have to use Vector180 as the ISR?
And also why do you use

Code: Select all

    nvicEnableVector(80, CORTEX_PRIORITY_MASK(<priority>));

insted of

Code: Select all

    nvicEnableVector(180, CORTEX_PRIORITY_MASK(<priority>));


EDIT:

One more question:
I'am want to save the received random number to disk and am trying to write it to a file inside of the ISR. Is this generally a good idea or should it be
avoided? Also the interrupt stack overflows as soon as the code enters f_write(). Can I increase the interrupt stack size? Or should I just put everything in a seperate thread an let the interrupt wake up the thread?

thanks,
kiwi
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: getting HRNG to work

Post by Giovanni »

80 is the number of the vector, 180 is he hexadecimal offset of the vector in the table: 0x180. Both are in the RM table listing interrupt sources.

Writing files from an ISR is definitely a no-go. You should send a message to a thread that would make the writing (a mailbox is suggested for this).

Giovanni
Post Reply