#define LED_qBlink LED_BUILTIN #define qBlink() (digitalWriteFast(LED_qBlink, !digitalReadFast(LED_qBlink) )) elapsedMillis Pause; elapsedMillis DotPrint; int Complete; int resetReasonHw; uint32_t bat; void setup() { resetReasonHw = RCM_SRS0; // Get reason for Rest value resetReasonHw |= (RCM_SRS1 << 8); Serial.begin(38400); Serial1.begin(2000000); digitalWrite(LED_qBlink, HIGH); pinMode(LED_qBlink, OUTPUT); Pause = 0; qBlink(); while (!Serial && (millis() <= 4000)) ; while (!Serial && (Pause <= 4000)) // give some time for USB Serial to connect qBlink(); Serial.println("OK"); Serial1.println("OK_Serial Online"); Pause = 0; DotPrint = 0; Complete = 0; resetReason(); read_myID(); print_myID(); analogReference(EXTERNAL); analogReadResolution(12); analogReadAveraging(32); // this one is optional. bat = getInputVoltage(); Serial.print("\nTeensy Voltage:" ); Serial.println(bat); } void loop() { uint32_t tbat; if ( Pause > 300) { qBlink(); Pause = 0; // blink every 300 millis if ( DotPrint > 2000 ) { DotPrint = 0; Complete++; Serial.print(":"); } if ( Complete > 50 ) { Complete = 0; tbat = getInputVoltage(); if ( tbat != bat ) { Serial.print("\nTeensy Voltage:" ); Serial.println(bat); bat = tbat; } Serial.println(); // break line of dots } } } void serialEvent() { while (Serial.available()) { Serial1.write(Serial.read()); } } void serialEvent1() { while (Serial1.available() ) { Serial.write(Serial1.read()); } } void resetReason() { uint16_t mask = 1; Serial.print(">>> Reason for 'reset': "); Serial.print(resetReasonHw, HEX); do { switch (mask & resetReasonHw) { //RCM_SRS0 case 0x0001: Serial.print(F(" wakeup")); break; case 0x0002: Serial.print(F(" LowVoltage")); break; case 0x0004: Serial.print(F(" LossOfClock")); break; case 0x0008: Serial.print(F(" LossOfLock")); break; //case 0x0010 reserved case 0x0020: Serial.print(F(" wdog")); break; case 0x0040: Serial.print(F(" ExtResetPin")); break; case 0x0080: Serial.print(F(" PwrOn")); break; //RCM_SRS1 case 0x0100: Serial.print(F(" JTAG")); break; case 0x0200: Serial.print(F(" CoreLockup")); break; case 0x0400: Serial.print(F(" SoftWare")); break; case 0x0800: Serial.print(F(" MDM_AP")); break; case 0x1000: Serial.print(F(" EZPT")); break; case 0x2000: Serial.print(F(" SACKERR")); break; //default: break; } } while (mask <<= 1); Serial.println(" :: done Reason"); } static uint8_t myID[8]; void read_EE(uint8_t word, uint8_t *buf, uint8_t offset) { noInterrupts(); FTFL_FCCOB0 = 0x41; // Selects the READONCE command FTFL_FCCOB1 = word; // read the given word of read once area // launch command and wait until complete FTFL_FSTAT = FTFL_FSTAT_CCIF; while (!(FTFL_FSTAT & FTFL_FSTAT_CCIF)) ; *(buf + offset + 0) = FTFL_FCCOB4; *(buf + offset + 1) = FTFL_FCCOB5; *(buf + offset + 2) = FTFL_FCCOB6; *(buf + offset + 3) = FTFL_FCCOB7; interrupts(); } void read_myID() { read_EE(0xe, myID, 0); // should be 04 E9 E5 xx, this being PJRC's registered OUI read_EE(0xf, myID, 4); // xx xx xx xx } unsigned long serialNum = 0; void print_myID() { uint32_t ii; Serial.printf(" ID byte = %d decimal, from ", myID[7]); for (ii = 0; ii < sizeof(myID); ii++) { if ( ii ) Serial.printf(" :"); Serial.printf("%02X", myID[ii]); if ( ii > 3) serialNum = (serialNum << 8) + myID[ii]; } Serial.printf(" Serial# %lu0\n", serialNum); } uint32_t getInputVoltage() { // for Teensy 3.1, only valid between 2.0V and 3.5V. Returns in millivolts. uint32_t x = analogRead(39); return (178 * x * x + 2688743864 - 1182047 * x) / 371794; }