Wixel SDK
random.c
1 #include <cc2511_types.h>
2 #include <cc2511_map.h>
3 #include <random.h>
4 
5 /* Returns a new random number. Warning: this is not reentrant. It is possible that this function
6  * could occasionally return the same random number to an ISR caller as it returns to a main loop
7  * caller.
8  */
10 {
11  uint8 rand;
12  while(ADCCON1 & 0x0C); // Wait for the random number to finish.
13  rand = RNDL; // Get the random number.
14  ADCCON1 = (ADCCON1 & 0x30) | 0x07; // Start generating the next random number.
15  return rand;
16 }
17 
18 void randomSeed(uint8 seed_msb, uint8 seed_lsb)
19 {
20  // Rescue the random number from these two bad states: 0x0000 and 0x8003.
21  // Without the code below, the random number generator could get stuck in
22  // either of these states if it was seeded badly.
23  if ((seed_lsb == 0 && seed_msb == 0) || (seed_lsb == 0x03 && seed_msb == 0x80))
24  {
25  seed_lsb = 0xAA;
26  }
27 
28  RNDL = seed_msb;
29  RNDL = seed_lsb;
30  randomNumber();
31  randomNumber();
32  randomNumber();
33 }
uint8 randomNumber(void)
Definition: random.c:9
unsigned char uint8
Definition: cc2511_types.h:9