RNG/PRNG using a MSP430

Vladha

I am currently trying to make a Random Number Generator using a MSP430 (MSP43 G2553) and then create an algorithm for a Pseudo Random Number Generator. I have a code for these two processes and also a code used to test the programs with LEDs. For some reason, I encounter some errors that I cannot seem to resolve. I will attach the codes and the errors to get a second opinion on the syntax.

 #include <msp430g2553.h>

 #include "rand.h"

    /**
     * Random number generator.
     *
     * NOTE: This affects Timer A.
     *
     * Algorithm from TI SLAA338:
     * http://www.ti.com/sc/docs/psheets/abstract/apps/slaa338.htm
     *
     * @return 16 random bits generated from a hardware source.
     */





        unsigned int rand(); {
        int i, j;
        unsigned int result = 0;

        /* Save state */
        unsigned int TACCTL0_old = TACCTL0;
        unsigned int TACTL_old = TACTL;

        /* Set up timer */
        TACCTL0 = CAP | CM_1 | CCIS_1;            // Capture mode, positive edge
        TACTL = TASSEL_2 | MC_2;                  // SMCLK, continuous up

        /* Generate bits */
        for (i = 0; i < 16; i++) {
            unsigned int ones = 0;

            for (j = 0; j < 5; j++) {
                while (!(CCIFG & TACCTL0));       // Wait for interrupt

                TACCTL0 &= ~CCIFG;                // Clear interrupt

                if (1 & TACCR0)                   // If LSb set, count it
                    ones++;
            }

            result >>= 1;                         // Save previous bits

            if (ones >= 3)                        // Best out of 5
                result |= 0x8000;                 // Set MSb
        }

        /* Restore state */


        TACCTL0 = TACCTL0_old;
            TACTL = TACTL_old;

            return result;
        }



    /**
     * Pseudo-random number generator.
     *
     * Implemented by a 16-bit linear congruential generator.
     * NOTE: Only treat the MSB of the return value as random.
     *
     * @param state Previous state of the generator.
     * @return Next state of the generator.
     */

        unsigned int prand(unsigned int state) {
            return (M * state + I);                   // Generate the next state of the LCG
        }

This is the code for the RNG and PRNG. The list of errors:

Warning[Pa050]: non-native end of line sequence detected (this diagnostic is only issued once) E:\Downloads\msp430-rng-master\rand.h 1 
Error[Pe169]: expected a declaration E:\Downloads\msp430-rng-master\rand.c 18 
Error[Pe169]: expected a declaration E:\Downloads\msp430-rng-master\rand.c 39 
Error[Pe169]: expected a declaration E:\Downloads\msp430-rng-master\rand.c 41 
Error[Pe169]: expected a declaration E:\Downloads\msp430-rng-master\rand.c 45 
Error[Pe169]: expected a declaration E:\Downloads\msp430-rng-master\rand.c 47 
Warning[Pe012]: parsing restarts here after previous syntax error E:\Downloads\msp430-rng-master\rand.c 50 
Error[Pe077]: this declaration has no storage class or type specifier E:\Downloads\msp430-rng-master\rand.c 51 
Error[Pe147]: declaration is incompatible with "unsigned short volatile TA0CTL @ 0x160" (declared at line 527 of "C:\Program Files (x86)\IAR Systems\Embedded  E:\Downloads\msp430-rng-master\rand.c 51 
Workbench 6.5\430\inc\msp430g2553.h") 
Error[Be022]: location address not allowed for initialized variables (writable variables without the __no_init attribute) E:\Downloads\msp430-rng-master\rand.c 51 
Error[Pe020]: identifier "TACTL_old" is undefined E:\Downloads\msp430-rng-master\rand.c 51 
Error[Pe169]: expected a declaration E:\Downloads\msp430-rng-master\rand.c 53 
Error[Pe169]: expected a declaration E:\Downloads\msp430-rng-master\rand.c 54 
Warning[Pe012]: parsing restarts here after previous syntax error E:\Downloads\msp430-rng-master\rand.c 68 

The code for testing is:

 #include <msp430g2553.h>
    #include <signal.h>
    #include <isr_compat.h>

    #include "rand.h"

    #define LED_OUT P1OUT
    #define LED_DIR P1DIR

    #define LED_RED   BIT0
    #define LED_GREEN BIT6

    #define BLINK_DELAY 1200                      // 200 ms at 6 KHz

    #define BITS_RAND  16
    #define BITS_PRAND 8                          // Using only the MSB of the prand state

    int failure = 0;

    /**
     * Set up the timers and blink!
     */
    void prepare_to_blink() {
        BCSCTL3 |= LFXT1S_2;                      // Set LF to VLO = 12 KHz
        BCSCTL1 |= DIVA_1;                        // ACLK = LF / 2 = 6 KHz

        TACCR0 = BLINK_DELAY;                     // Set the timer
        TACTL = TASSEL_1 | MC_1;                  // TACLK = ACLK; up to CCR0
        TACCTL1 = CCIE | OUTMOD_3;                // TA1 interrupt enable; PWM set/reset

        __bis_SR_register(LPM3_bits | GIE);       // LPM3 w/ interrupt
    }

    int interrupt(TIMERA1_VECTOR) blink_LED (void) {
        TACCTL1 &= ~CCIFG;                        // Unset interrupt flag

        if (failure)                              // Toggle LEDs
            LED_OUT ^= LED_RED;
        else
            LED_OUT ^= LED_GREEN;
    }

    /******************************************************************************
     * Monobit
     *
     * SP 800-22 Rev. 1a
     * http://csrc.nist.gov/publications/nistpubs/800-22-rev1a/SP800-22rev1a.pdf
     ******************************************************************************/

    /* The hardware RNG is slow, so limit test to 400 bits. */
    #define MONOBIT_TIMES_RAND 25                 // 400 / BITS_RAND

    /* Each 8-bit number tested with monobit contributes 8 bits, so in the worst
     * case, the signed 16-bit bucket can store information about this many
     * numbers: */
    #define MONOBIT_TIMES_PRAND 4095              // (2 ^ 15 - 1) / BITS_PRAND

    /* The maximum absolute value of the sum bucket after a monobit test, where
     * 0.01 is the minimum P-value and inverfc is the inverse of the complementary
     * error function. */
    #define MONOBIT_MAX_VAL_RAND  51              // inverfc(0.01) * sqrt(2) * sqrt(400)
    #define MONOBIT_MAX_VAL_PRAND 466             // inverfc(0.01) * sqrt(2) * sqrt(2 ^ 15 - 1)

    /**
     * Monobit test for rand().
     *
     * Returns 0 on success; otherwise otherwise.
     */
    int monobit_rand() {
        int sum = 0;

        int i, j;

        for (i = 0; i < MONOBIT_TIMES_RAND; i++) {
            int r = rand();

            /* Add up all the bits, taking 0 to mean -1. */
            for (j = 0; j < BITS_RAND; j++) {
                sum += r & 0x1 ? 1 : -1;
                r >>= 1;
            }
        }

        if (sum < 0)
            sum = 0 - sum;                        // Absolute value

        return sum > MONOBIT_MAX_VAL_RAND;
    }

    /**
     * Monobit test for prand().
     *
     * Returns 0 on success; otherwise otherwise.
     */
    int monobit_prand() {
        int state = rand();
        int sum = 0;

        int i, j;

        for (i = 0; i < MONOBIT_TIMES_PRAND; i++) {
            int r = state >> (16 - BITS_PRAND);   // Ignore the least significant bits

            /* Add up all the bits, taking 0 to mean -1. */
            for (j = 0; j < BITS_PRAND; j++) {
                sum += r & 0x1 ? 1 : -1;
                r >>= 1;
            }

            state = prand(state);
        }

        if (sum < 0)
            sum = 0 - sum;                        // Absolute value

        return sum > MONOBIT_MAX_VAL_PRAND;
    }

    /**
     * Store the failure code on the top of the stack and alternate flashing the
     * LEDs to signify failure.
     *
     * Never returns!
     */
    void fail(int code) {
        asm("push %0" : : "r" (code));

        failure = 1;

        LED_OUT &= ~LED_GREEN;
        prepare_to_blink();
    }

    /**
     * Run though all the tests.
     *
     * Both LEDs are lit up while testing, and one will blink once the tests are
     * done, depending on the outcome.
     */
    void main() {
        LED_DIR |= LED_RED | LED_GREEN;
        LED_OUT |= LED_RED | LED_GREEN;

        if (monobit_rand())
            fail(0xdead);

        if (monobit_prand())
            fail(0xbeef);

        LED_OUT &= ~LED_RED;
        prepare_to_blink();
    }

And the list of errors when compiling:

Warning[Pa050]: non-native end of line sequence detected (this diagnostic is only issued once) E:\Downloads\msp430-rng-master\rand.h 1 

Error[Pe169]: expected a declaration E:\Downloads\msp430-rng-master\test.c 34 

Warning[Pe012]: parsing restarts here after previous syntax error E:\Downloads\msp430-rng-master\test.c 116 

Warning[Pe1051]: standard requires that parameter "TIMERA1_VECTOR" be given a type by a subsequent declaration ("int" assumed) E:\Downloads\msp430-rng-master\test.c 34 

Error[Pe130]: expected a "{" E:\Downloads\msp430-rng-master\test.c 117 

Warning[Pe940]: missing return statement at end of non-void function "interrupt" E:\Downloads\msp430-rng-master\test.c 117 

Error[Pe018]: expected a ")" E:\Downloads\msp430-rng-master\test.c 126 

Warning[Pe223]: function "monobit_rand" declared implicitly E:\Downloads\msp430-rng-master\test.c 144 

Warning[Pe223]: function "monobit_prand" declared implicitly E:\Downloads\msp430-rng-master\test.c 147 
bblincoe

You have incorrect syntax. Please see the following line:

unsigned int rand(); {

This line should read:

unsigned int rand() {

Edit

In your test code, you have the following:

int interrupt(TIMERA1_VECTOR) blink_LED (void) {

I have never seen an interrupt service routine defined in this way. Instead it should be defined (in IAR EW430) like this:

#pragma vector=TIMERA1_VECTOR
__interrupt void blink_LED(void) {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related