TelephoneBill
Well-known member
Does anyone have a simple code example to exercise the RANDOM NUMBER GENERATOR (RNGA) in T3.6/T3.5 ?
// T3.6/T3.5 Random Number Generator
//==================================
//Thanks and acknowledgement to "manitou" for original test code
//Modifications by "TelephoneBill" dated 23 FEB 2017
#define RNG_CR_GO_MASK 0x1u
#define RNG_CR_HA_MASK 0x2u
#define RNG_CR_INTM_MASK 0x4u
#define RNG_CR_CLRI_MASK 0x8u
#define RNG_CR_SLP_MASK 0x10u
#define RNG_SR_OREG_LVL_MASK 0xFF00u
#define RNG_SR_OREG_LVL_SHIFT 8
#define RNG_SR_OREG_LVL(x) (((uint32_t)(((uint32_t)(x))<<RNG_SR_OREG_LVL_SHIFT))&RNG_SR_OREG_LVL_MASK)
#define SIM_SCGC6_RNGA ((uint32_t)0x00000200)
uint32_t GenRandNum(){
RNG_CR |= RNG_CR_GO_MASK;
while((RNG_SR & RNG_SR_OREG_LVL(0xF)) == 0); //wait for RN to be generated
return RNG_OR; //return RN
}
void setup() {
Serial.begin(230400);
SIM_SCGC6 |= SIM_SCGC6_RNGA; //enable RNG
RNG_CR &= ~RNG_CR_SLP_MASK;
RNG_CR |= RNG_CR_HA_MASK; //high assurance, not needed
}
void loop() {
uint32_t rn;
String strRN, prtRN;
rn=GenRandNum(); //generate RN
strRN = "00000000" + String(rn, HEX); //string with leading zeroes
prtRN = strRN.substring(strRN.length()-8); //truncate to print 8 characters
Serial.println(prtRN); //output to serial monitor
}