/* get random data from sram powerup bits */
unsigned int puf_sram(unsigned char cmode) // look at random SRAM data for PRNG seed
{ // uses a udata section of memeory PUF_SIZE and eeprom from
unsigned int e; // 0 to 2xPUF_SIZE to store key, diff data
static unsigned int seed=0; // if cmode is TRUE the stored key will be zeroed and will return 0
unsigned char entr_s,entr_r,entr_d;
if (seed !=0 ) return seed;
for (e=0; e<PUF_SIZE; e++) {
seed = seed + sram_key[e];
entr_s=sram_key[e];
Busy_eep();
entr_r = Read_b_eep ( e ); // read eeprom seed data
entr_d = entr_s^entr_r; // XOR to look for diff bits
if (!cmode) { // cmode will zero key data history
Busy_eep();
Write_b_eep ( e, entr_s ); // write eeprom key data
Busy_eep();
Write_b_eep ( e+PUF_SIZE, entr_d ); // write eeprom key change data
} else {
Busy_eep();
Write_b_eep ( e, 0 ); // write zeros to eeprom key data
}
ClrWdt(); // reset the WDT timer
}
seed=Make_Crc16(sram_key,PUF_SIZE); // make seed from crc16 of sram
if (cmode) seed=0; // cmode return 0
return (seed);
}