Random Number Generator Library

This Library provides a set of routines for easier handling of the Random Number Generator module available on CEC1x02.

Library Routines

rng_power

Prototype // for CEC1302 MCU :

void rng_power(uint16_t pwr_on);

// for CEC1702 MCU :

void rng_power(uint8_t pwr_on);

Description

This routine is used for power control of the RNG block.

Parameters
  • pwr_on: power parameter. Valid values :
    Value Description
    1 Puts the module to sleep (1 gate off clocks to block).
    0 Enables the block (gate on clocks to block).
  • Returns

    Nothing.

    Requires

    Nothing.

    Notes

    None.

    rng_reset

    Prototype

    void rng_reset();

    Description

    This routine resets the RNG block.

    Parameters

    None.

    Returns

    Nothing.

    Requires

    Nothing.

    Notes

    None.

    rng_mode

    Prototype

    void rng_mode(uint8_t mode);

    Description

    The function controls the mode of RNG.

    Parameters
    • mode: Select mode. Valid values :
      Value Description
      1 Enables pseudo random mode.
      0 Enables asynchronous (true random) mode.
    Returns

    Nothing.

    Requires

    Nothing.

    Notes

    None.

    rng_is_on

    Prototype // for CEC1302 MCU :

    uint16_t rng_is_on();

    // for CEC1702 MCU :

    uint8_t rng_is_on();

    Description

    This function is used to check if the NDRNG block is powered on.

    Parameters tmode_pseudo
    Returns
    • 1 - block is on,
    • 0 - otherwise.
    Requires

    Nothing.

    Notes

    None.

    rng_start

    Prototype

    void rng_start();

    Description

    This routine is used to start the NDRNG engine. Once started, the NDRNG will fill its internal 1Kbit internal FIFO with random bits.
    The NDRNG block will hang if its FIFO is read while empty. Firmware must poll the NDRNG’s FIFO level and only read data from the 32-bit FIFO data register when NOT empty.

    Parameters tmode_pseudo
    Returns

    Nothing.

    Requires

    Nothing.

    Notes

    None.

    rng_stop

    Prototype

    void rng_stop();

    Description

    This routine stops the NDRNG engine. When the engine is stopped, the NDRNG will not re-fill its FIFO when data is removed.

    Parameters

    None.

    Returns

    Nothing.

    Requires

    Nothing.

    Notes

    None.

    rng_get_fifo_level

    Prototype

    uint32_t rng_get_fifo_level();

    Description

    This routine reads the NDRNG FIFO level register and returns the number of 32-bit words of random data currently in FIFO.
    This call must be issued before reading the FIFO and only read FIFO if this call returns a non-zero number.

    Parameters

    None.

    Returns

    Returns the number of 32-bit words in the NDRNG FIFO. Maximum value is 32 (32x32 = 1024 bits).

    Requires

    Nothing.

    Notes

    None.

    rng_get_bytes

    Prototype

    uint32_t rng_get_bytes(uint8_t * pbuff, uint32_t nbytes);

    Description

    This routine fills a buffer with random bytes.

    Parameters
    • pbuff: pointer to a buffer where the data will be stored.
    • nbytes: number of random bytes to be retrieved.
    Returns

    The number of bytes retrieved is returned.

    Requires

    Nothing.

    Notes

    The API reads 32 bits at a time from FIFO. If bytes are requested, a 32 bit word is read and 4 bytes are retrieved. However, only the number of bytes requested is returned.

    rng_get_words

    Prototype

    uint32_t rng_get_words(uint32_t *pwords, uint32_t nwords);

    Description

    This function reads a specified number of words (32-bit data) into the buffer specified by the caller. This function is an all-in-one routine.
    Powers on the NDRNG, starts the NDRNG, polls the FIFO level and reads words from the FIFO only if it is not empty.
    It loops until the specified number of words is read.

    Parameters
    • pwords: pointer to word (32-bit) aligned SRAM buffer.
    • nwords: Number of 32-bit words of random data to read.
    Returns

    Returns the actual number of bytes read.

    Requires

    Nothing.

    Notes

    No time out is implemented, if the NDRNG FIFO hardware stops filling the FIFO, this routine will loop forever.

    Copyright (c) 2002-2019 mikroElektronika. All rights reserved.
    What do you think about this topic ? Send us feedback!
    Want more examples and libraries? 
    Find them on LibStock - A place for the code