gpt4 book ai didi

c - 使用 MSP430 的 RNG/PRNG

转载 作者:行者123 更新时间:2023-11-30 17:32:14 24 4
gpt4 key购买 nike

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

 #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

最佳答案

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

无符号 int rand(); {

这一行应为:

无符号 int rand() {

编辑

在您的测试代码中,您有以下内容:

int 中断(TIMERA1_VECTOR)blink_LED(void){

我从未见过以这种方式定义的中断服务例程。相反,它应该像这样定义(在 IAR EW430 中):

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

关于c - 使用 MSP430 的 RNG/PRNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24302937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com