Show Posts
Pages: 1 ... 44 45 [46] 47 48
676  Using Arduino / Programming Questions / Re: While loop preventing ISR from working on: May 31, 2012, 05:23:52 pm
Quote
If I am wrong about these assumptions... At least one assumption you made is wrong. ... Not a particularly helpful answer.

The answer was meant to force you to consider the assumptions you made.  If you could not determine which one is wrong then you would be (theoretically) encouraged to include critical sections.  Obviously, from what you later wrote, the attempt failed.

Quote
Like the suggestions that people asking questions provide complete questions, the same principal applies to answers.

I gave you the complete answer: add critical sections.

Quote
I fully understand the lack of interest in spending time answering what you perceive to be a stupid question;

It's not a stupid question.  But it is a question that has been asked and answered hundreds of times before by people much smarter than you and I.  Except in extremely rare cases the answer is always the same.

Quote
As I said I will read the links you provided; however, a quick perusal indicated that they were describing a general set of circumstances, which may (or may not) affect the specific conditions here.  As I said, if this sketch runs for several days, without incidence, I am less than convinced that the cost you propose is needed...  

After a "quick perusal", you are convinced that you are right and I am wrong.  If that's the amount of effort you are going to exert before reaching a conclusion I give up.  Do what you want @wanderson.  This conversation is over for me.


No I am not convinced you are wrong and I am right.  I just don't believe that you are undoubtedly right, which you seem to believe.  I have been involved in one other thread with you where you made a similar blanket claim, which further research revealed to be unfounded--or at least without the certainty which you claimed... Further, many of your answers are at best terse, and at worst, not particularly salient.  I fully understand how irritating some of the questions asked on a board like this can be.  Indeed, I should have been able to find the need for the volatile keyword, not sure why I didn't find it when I searched--probably because I was looking for the wrong key phrases. But, terse answers, while justifiable, are no more helpful that short incomplete questions.  For instance, instead of stating that one of my assumptions is wrong an equally terse "search for <some keyphrase>" would actually have been helpful.

I have now read all of the links you provided and am still of the opinion that your "solution" may not be necessary in this case.  Specifically, all but one of the accesses are atomic (though I will need to look at the generated asm to confirm this), and the one that isn't simply doesn't matter if portions of the value change in the middle of access. Further, even if you are correct, my interest is not in simply producing a working library that uses an interrupt, but rather in learning the specifics of interrupt handling on AVR's which is an entirely different kettle of fish.  In other words the WHY something needs to be done, not simply the HOW.

677  Using Arduino / Programming Questions / Re: While loop preventing ISR from working on: May 31, 2012, 03:30:34 pm
however, what you didn't answer is how the lack of "critical sections" would affect THIS library
And I will not be.  Failure analysis on interrupt service routines can be very time consuming.  It is also unnecessary.  The solution is trivial: add the critical sections.

Your proposed solution may well be trivial, but I was asking because I was curious about WHY it was necessary for this situation...  If you don't wish to answer that, fine.

Quote
If I am wrong about these assumptions...
At least one assumption you made is wrong.

Not a particularly helpful answer.  Like the suggestions that people asking questions provide complete questions, the same principal applies to answers.  I fully understand the lack of interest in spending time answering what you perceive to be a stupid question; however, this particular answer would have been better left unmade (and that certainly would have been more polite)

Quote
please provide either an explanation...
No.  You have the solution.  It's trivial to add and the cost is negligible.

As I said I will read the links you provided; however, a quick perusal indicated that they were describing a general set of circumstances, which may (or may not) affect the specific conditions here.  As I said, if this sketch runs for several days, without incidence, I am less than convinced that the cost you propose is needed...  At the very least I am interested in learning what circumstances trigger the need for the solution you proposed.
678  Using Arduino / Programming Questions / Re: While loop preventing ISR from working on: May 31, 2012, 02:49:25 pm

Thank you I will read these links ASAP.

Are you claiming critical sections are not necessary?

No, I said exactly what I meant to say, which is that the code seems to be running correctly without "critical sections". 

That kind of thinking will get you: corrupt memory, lock-ups, "random" behaviour, and a long list of other undesirable "features".

Since I will need to run this code for continuously for several days, if I get any "random" behavior, I will add the "critical sections" you mention; however, what you didn't answer is how
the lack of "critical sections" would affect THIS library, which has a ISR that isn't really using pointers (mostly) and where it doesn't matter if in reading a multi-byte value, one or more of the bytes
being read change between reads.  The individual bytes are not pointers to anything, they are simply random... therefore they don't matter if they change mid-read.

Further if a different ISR is called, it shouldn't be accessing the same globals, since they are not declared outside of the c++ file of the library.

If I am wrong about these assumptions, please provide either an explanation or some direction as to what I can read to obtain that explanation (other than the links you already provided).
679  Using Arduino / Programming Questions / Re: While loop preventing ISR from working on: May 31, 2012, 02:15:02 pm
Any global variable referenced within an ISR must be declared "volatile":

...and access must be protected with a critical section.


Can you expound on what you mean with this?  When I made the appropriate global variables volatile, my code seems to function as expected.  Given the nature of the library (produces random numbers) it doesn't
seem to matter if any of the global variables (or portions thereof) get altered while the library is accessing them.
680  Using Arduino / Programming Questions / While loop preventing ISR from working on: May 31, 2012, 11:35:55 am
I am having some difficulty identifying the problem with this library I am developing...  It appears to me that when this code in the library gets executed
Code:
while (WDT_POOL_COUNT < 1)
    waiting += 1; // Just a dummy until ISR creates entropy pool

it never exits, which it should when the ISR puts some data back into the entropy pool.

Using this sketch, which triggers the above while loop immediately, it never sends any data to the PC,

Code:
#include <Entropy.h>

void setup()
{
  Serial.begin(115200);
  Entropy.Initialize();
}

void loop()
{
    Serial.println(Entropy.random());
}

However, the following sketch, which ensures that the entropy pool contains data before calling the random function and therefore never enters the while loop works fine, reporting a series of random values...

Code:
#include <Entropy.h>

void setup()
{
  Serial.begin(115200);
  Entropy.Initialize();
}

void loop()
{
   if(Entropy.avalable()>1)
    Serial.println(Entropy.random());
}

I have created a standalone c++ file that includes the library and replicates the function of the first sketch and run it with the simulator in AVR studio, and get the same result...  For some reason when the while loop is executed, the ISR never gets triggered...  Any ideas?

Here is the code for the library...

Entropy.h
Code:
#ifndef Entropy_h
#define Entropy_h

#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>

const uint32_t WDT_RETURN_BYTE=256;
const uint32_t WDT_RETURN_WORD=65536;

union ENTROPY_LONG_WORD
{
  uint32_t int32;
  uint16_t int16[2];
  uint8_t int8[4];
};

class EntropyClass
{
public:
  void Initialize(void);
  uint32_t random(void);
  uint8_t available(void);
 private:
  ENTROPY_LONG_WORD share_entropy;
  uint32_t retVal;
};
extern EntropyClass Entropy;
#endif

Entropy.cpp
Code:

#include <Entropy.h>

const uint8_t WDT_BUFFER_SIZE=32;
const uint8_t WDT_POOL_SIZE=8;
uint8_t WDT_BUFFER[WDT_BUFFER_SIZE];
uint8_t WDT_BUFFER_POSITION;
uint8_t WDT_POOL_START;
uint8_t WDT_POOL_END;
uint8_t WDT_POOL_COUNT;
uint8_t WDT_LOOP_COUNTER;
uint32_t WDT_ENT_POOL[WDT_POOL_SIZE];

// This function initializes the global variables needed to implement the circular entropy pool and
// the buffer that holds the raw Timer 1 values that are used to create the entropy pool.  It then
// Initializes the Watch Dog Timer (WDT) to perform an interrupt every 2048 clock cycles, (about
// 16 ms) which is as fast as it can be set.
void EntropyClass::Initialize(void)
{
  WDT_BUFFER_POSITION=0;
  WDT_POOL_START = 0;
  WDT_POOL_END = 0;
  WDT_POOL_COUNT = 0;
  cli();                         // Temporarily turn off interrupts, until WDT configured
  MCUSR = 0;                     // Use the MCU status register to reset flags for WDR, BOR, EXTR, and POWR
  WDTCSR |= _BV(WDCE) | _BV(WDE);// WDT control register, This sets the Watchdog Change Enable (WDCE) flag, which is  needed to set the
  WDTCSR = _BV(WDIE);            // Watchdog system reset (WDE) enable and the Watchdog interrupt enable (WDIE)
  sei();                         // Turn interupts on
}

// This function returns a uniformly distributed random integer in the range
// of [0,0xFFFFFFFF] as long as some entropy exists in the pool and a 0
// otherwise.  To ensure a proper random return the available() function
// should be called first to ensure that entropy exists.
//
// The pool is implemented as an 8 value circular buffer
uint32_t EntropyClass::random(void)
{
  uint8_t waiting;
  while (WDT_POOL_COUNT < 1)
    waiting += 1; // Just a dummy until ISR creates entropy pool
  retVal = WDT_ENT_POOL[WDT_POOL_START];
  WDT_POOL_START = (WDT_POOL_START + 1) % WDT_POOL_SIZE;
  --WDT_POOL_COUNT;
  return(retVal);
}


// This function returns a unsigned char (8-bit) with the number of unsigned long values
// in the entropy pool
uint8_t EntropyClass::available(void)
{
  return(WDT_POOL_COUNT);
}

// This interrupt service routine is called every time the WDT interrupt is triggered.
// With the default configuration that is approximately once every 16ms, producing
// approximately two 32-bit integer values every second.
//
// The pool is implemented as an 8 value circular buffer
ISR(WDT_vect)
{
  WDT_BUFFER[WDT_BUFFER_POSITION] = TCNT1L; // Record the Timer 1 low byte (only one needed)
  WDT_BUFFER_POSITION++;                    // every time the WDT interrupt is triggered
  if (WDT_BUFFER_POSITION >= WDT_BUFFER_SIZE)
  {
    WDT_POOL_END = (WDT_POOL_START + WDT_POOL_COUNT) % WDT_POOL_SIZE;
    // The following code is an implementation of Jenkin's one at a time hash
    // This hash function has had preliminary testing to verify that it
    // produces reasonably uniform random results when using WDT jitter
    // on a variety of Arduino platforms
    for(WDT_LOOP_COUNTER = 0; WDT_LOOP_COUNTER < WDT_BUFFER_SIZE; ++WDT_LOOP_COUNTER)
      {
WDT_ENT_POOL[WDT_POOL_END] += WDT_BUFFER[WDT_LOOP_COUNTER];
WDT_ENT_POOL[WDT_POOL_END] += (WDT_ENT_POOL[WDT_POOL_END] << 10);
WDT_ENT_POOL[WDT_POOL_END] ^= (WDT_ENT_POOL[WDT_POOL_END] >> 6);
      }
    WDT_ENT_POOL[WDT_POOL_END] += (WDT_ENT_POOL[WDT_POOL_END] << 3);
    WDT_ENT_POOL[WDT_POOL_END] ^= (WDT_ENT_POOL[WDT_POOL_END] >> 11);
    WDT_ENT_POOL[WDT_POOL_END] += (WDT_ENT_POOL[WDT_POOL_END] << 15);
    WDT_ENT_POOL[WDT_POOL_END] = WDT_ENT_POOL[WDT_POOL_END];
    WDT_BUFFER_POSITION = 0; // Start collecting the next 32 bytes of Timer 1 counts
    if (WDT_POOL_COUNT == WDT_POOL_SIZE) // The entropy pool is full
      WDT_POOL_START = (WDT_POOL_START + 1) % WDT_POOL_SIZE;  
    else // Add another unsigned long (32 bits) to the entropy pool
      ++WDT_POOL_COUNT;
  }
}

EntropyClass Entropy;
681  Using Arduino / Programming Questions / Re: reading text from an sd card on: May 29, 2012, 03:19:19 pm
I'm playing with the sd card read/write tutorial.  The example sketch works fine.

When I create a text file in notepad, copy it to an sd card and open it using the arduino, the formatting is different.  Ie -

Original text - this is a test 1 2 34
Text printed in serial monitor - tisisa es 12 4

I've tried asni, utf8 etc

any ideas?

Original text - this is a test 1 2 34
Serial text    -  t  is is a  es  1 2    4

As previously mentioned code would help; however, the serial text is a subset of the original which would indicate data being dropped, either in the serial transmission to the computer or possibly in your code reading the SD card.
682  Using Arduino / Programming Questions / Re: timer1 code help on: May 24, 2012, 01:55:01 pm
Hello!
I found a useful tutorial about using timers on arduino, and I tried to hack an example to make some tests.
This sketch, should work this way:
1- receive an external signal and trigger an external interrupt
2- the external interrupt function (tachSignal) sets timer1 to wait for a certain amount of time
3- when the ISR runs, it turns on an led and then sets timer1 for another time interval
4- when the ISR runs again, it turns off the led and reset timer1

here is the sketch:

So have you tried to run the code?  How does it work?  What errors do you see?
683  Using Arduino / Programming Questions / Re: Random Seeds and Random Numbers on: May 24, 2012, 01:46:42 pm
Also, after looking at the code you provided, it appears that you used TC0 for your tests. 

Yup.

Quote
As I mentioned, TC0 exhibited less entropy than the other two counters and a greater degree of serial correlation.

Are you certain that is because of hardware differences in the timers?  Is it possible that the difference you observed is because of the way the timers are configured?


I haven't looked into what could be causing the differences, but I suspect that it is predominantly being caused by configuration differences rather than hardware differences.  For instance, when I performed my initial tests using the sketches someone posted earlier in the thread, the sketch that made use of the TimerOne library performed worse than the test that used the default Arduino configuration for T1

For now my basic concern is how the functions perform with a standard Arduino configuration, since that will be the most generally useful.  Also, in terms of generating a truly random seed value, that is all that is required, since configuration changes to the timers can be postponed until after a 32 bit seed can be generated. 
684  Using Arduino / Programming Questions / Re: Random Seeds and Random Numbers on: May 24, 2012, 12:00:13 pm
Quote
however, it also shows a significant amount of entropy in the data stream

By what measure?


Several, but the easiest to reference at the moment is your own reference to the ent results on the raw WDT values I provided-- > 7.9 bits of entropy per 8 bits.  Also the low serial correlation is important when selecting whitening techniques.  There also is no debating that all of the samples, when run through a simple deterministic whitening filter, produce uniform random numbers that easily pass the ent tests.

Now your ent tests on the wdt data I provided, were somewhat flawed, since it appears you combined the WDT values for all three counters TC0,TC1, and TC2 into a single stream for ent to analyze.  I only examined the counters separately, but the similarity in results for TC1 & TC2 made be suspect that their output could be correlated.

Also, after looking at the code you provided, it appears that you used TC0 for your tests.  As I mentioned, TC0 exhibited less entropy than the other two counters and a greater degree of serial correlation.
685  Using Arduino / Programming Questions / Re: JTAG on: May 24, 2012, 08:03:07 am
Need a larger chip for JTAG, '328 does not do it.

True but you can use DebugWire to perform the many of the same functions.
686  Using Arduino / Programming Questions / Re: Random Seeds and Random Numbers on: May 24, 2012, 07:09:39 am
The attached code contains the core function.  Data was collected through Serial to a file or directly to an SD card.  Approximately every 24 hours, the files were sent to me.  I would dig through them looking for reasons the technique would not be reliable.  The goal was to create a reliable method to seed the Arduino pseudo random number generator.

Thank you for the code, I will see if it produces different results with my hardware.

wdt.2012-05-10.UNO.SMD.csv
...is not uniform (there is a trough at 0 and 255 and a spike at 1) nor does it pass ent.

No you are correct, the raw data shows a bias at values 0,1, and 255; however, it also shows a significant amount of entropy in the data stream.   Since all sources of real entropy appear to also have biases, this is not really a major concern.  That is why whitening techniques are needed to produce uniform random numbers from entropy streams such as radioactive decay, or avalanche noise.  Indeed, if you look at the Hotbits site and code (The creator of the ent tool you are using) you will see that even with a source universally acknowledged as random (radioactive decay) he had to implement a couple of whitening techniques; the von Neumann algorithm and a flipping algorithm that alternates output.  Since both algorithms are deterministic they are not producing randomness in and of themselves, they are simply removing biases from an existing random stream and producing more uniform results.

ENT  : Count   :         : 30029646
ENT  : Entropy :         : 7.998433
ENT  : ChiSqr  : Sum     : 54680.07
ENT  : ChiSqr  : p-value : 0.0000 : FAIL


wdt.2012-05-17.MEGA.SMD.csv
...is not uniform and fails ent.

ENT  : Count   :         : 30391944
ENT  : Entropy :         : 7.998461
ENT  : ChiSqr  : Sum     : 54276.31
ENT  : ChiSqr  : p-value : 0.0000 : FAIL


wdt.2012-05-14.UNO.DIP.csv
...ditto.

ENT  : Count   :         : 30282824
ENT  : Entropy :         : 7.998423
ENT  : ChiSqr  : Sum     : 55414.57
ENT  : ChiSqr  : p-value : 0.0000 : FAIL


All true, however, when the same raw data is filtered through a whitening algorithm it produces a nicely uniform random stream.

Code:
wandrson@delphi:~/Development/hardware-rng/WatchDogTimer$ ent jenkins.32.2012-05-08.MEGA.SMD.tc1.bin
Entropy = 7.999839 bits per byte.

Optimum compression would reduce the size
of this 1250000 byte file by 0 percent.

Chi square distribution for 1250000 samples is 278.64, and randomly
would exceed this value 14.79 percent of the times.

Arithmetic mean value of data bytes is 127.4387 (127.5 = random).
Monte Carlo value for Pi is 3.140088224 (error 0.05 percent).
Serial correlation coefficient is -0.000642 (totally uncorrelated = 0.0).

wandrson@delphi:~/Development/hardware-rng/WatchDogTimer$ ent jenkins.32.2012-05-08.UNO.DIP.tc1.bin
Entropy = 7.999854 bits per byte.

Optimum compression would reduce the size
of this 1250000 byte file by 0 percent.

Chi square distribution for 1250000 samples is 253.21, and randomly
would exceed this value 51.99 percent of the times.

Arithmetic mean value of data bytes is 127.5543 (127.5 = random).
Monte Carlo value for Pi is 3.132907413 (error 0.28 percent).
Serial correlation coefficient is -0.000510 (totally uncorrelated = 0.0).

wandrson@delphi:~/Development/hardware-rng/WatchDogTimer$ ent jenkins.32.2012-05-10.UNO.SMD.tc1.bin
Entropy = 7.999835 bits per byte.

Optimum compression would reduce the size
of this 1250000 byte file by 0 percent.

Chi square distribution for 1250000 samples is 286.47, and randomly
would exceed this value 8.55 percent of the times.

Arithmetic mean value of data bytes is 127.4333 (127.5 = random).
Monte Carlo value for Pi is 3.139665823 (error 0.06 percent).
Serial correlation coefficient is -0.000646 (totally uncorrelated = 0.0).


The bias isn't really bias.  It's the millis interrupt service routine interfering.

I suspect that you misunderstand what the term bias, means in the context.  Something like the millis interrupt service routine interfering is bias, it is affecting the probability of certain outcomes, thereby altering the distribution and leading to the bias, mentioned.  Which is why whitening techniques are needed.  Indeed, that is why I generated and provided the raw data for all three counters on a variety of platforms--to test a variety of whitening filters against the raw data.  TC0 is clearly not showing enough entropy to be useful with this technique; however, both TC1 and TC2 clearly demonstrate sufficient entropy to be useful.  Further, from my currently limited sample of 4 smd chips and 2 dip chips, there doesn't appear to be the difference in behavior you alluded to earlier.

It will take a lot more research and data to determine if this technique is cryptographically useful; however, it is clearly the best method currently available to provide a truly random seed for the internal PRNG--certainly far better than the more typically used (randomSeed(analogRead(0))
687  Using Arduino / Microcontrollers / Re: Standalone proects on: May 23, 2012, 08:33:49 pm
On the Atmel reference designs (search AVR042) they suggest LC filters on both Vcc and AVcc.  May not be nescessary, but for a less than a dollar in parts is not a bad idea.  I ended up using a 20-pin dip header that I placed the power LC filter, a reset circuit, power led, and a signal led.  For less than $10 is parts I was able to make 10 or so...  They make wiring up any AVR pretty easy.  

Also have a 14 pin headers with xtal circuits and AVcc LC filters
688  Using Arduino / Programming Questions / Re: AVRISP MKII Problem on: May 23, 2012, 08:20:07 pm
In that case I would expect the problem to be related to either a security issue with Windows 7 (try running arduino ide as administrator and see if the problem persists), I get the same message on my Ubuntu machine unless I run the IDE with superuser privileges...

If admin works, a google search should turn up instructions on how to fix the permissions on your OS... 
689  Using Arduino / Programming Questions / Re: AVRISP MKII Problem on: May 23, 2012, 05:27:14 pm
when I install those drivers I get the message

"avrdude: usbdev_open(): did not find any USB device "usb" "

Don't install the "drivers" install AVR Studio (without first plugging in the AVRISP).  Plug the AVRISP into a usb port.  Wait until Windows recognizes the device. 
690  Using Arduino / Programming Questions / Re: AVRISP MKII Problem on: May 23, 2012, 03:54:27 pm
Once you have the drivers installed from AVR Studio, the Arduino IDE should work for programming with the AVR ISP MKII
Pages: 1 ... 44 45 [46] 47 48