2014-06-19 45 views
0

我目前正在尝试使用MSP430(MSP43 G2553)制作一个随机数生成器,然后为伪随机数生成器创建一个算法。我有这两个进程的代码,也有一个用于测试LED程序的代码。 由于某些原因,我遇到了一些我无法解决的错误。我将附上代码和错误以获得关于语法的第二意见。使用MSP430的RNG/PRNG

#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 
     } 

这是RNG和PRNG的代码。错误列表:

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 

的代码进行测试是:

#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(); 
    } 

和错误的编译时列表:

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 

回答

0

你有不正确的语法。请参阅以下行:

unsigned int rand(); {

此行应为:

unsigned int rand() {

编辑

在你的测试代码,您具备以下条件:

int interrupt(TIMERA1_VECTOR) blink_LED (void) {

我从来没有见过以这种方式定义的中断服务程序。相反,它应该是这样定义的(在IAR EW430中):

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

是的,它确实清除了我的错误日志,但我仍然有3个更多的是在扰乱我。错误[P​​e020]:标识符“TIMERA1_VECTOR”未定义E:\ Downloads \ msp430-rng-master \ test.c 32 错误[Pe661]:预期整数常量E:\ Downloads \ msp430-rng-master \ test.c 32 Error [Pe018]:expected a“)”E:\ Downloads \ msp430-rng-master \ test.c 125 – Vladha

+0

msp430g2553.h文件中命名的向量是什么? – bblincoe

+0

#define TIMER1_A1_VECTOR – Vladha