diff --git a/hal/architecture/MyHw.h b/hal/architecture/MyHw.h index fd78f47..3288ff9 100644 --- a/hal/architecture/MyHw.h +++ b/hal/architecture/MyHw.h @@ -51,18 +51,13 @@ uint8_t hwReadConfig(int adr); */ //#define MY_HW_HAS_GETRANDOM -/** - * Sleep for a defined time, using minimum power. - * @param ms Time to sleep, in [ms]. - * @return Nonsense, please ignore. - */ - +/// @brief unique ID typedef uint8_t unique_id_t[16]; /** * Sleep for a defined time, using minimum power. * @param ms Time to sleep, in [ms]. - * @return -1. + * @return MY_WAKE_UP_BY_TIMER. */ int8_t hwSleep(unsigned long ms); @@ -71,7 +66,7 @@ int8_t hwSleep(unsigned long ms); * @param interrupt Interrupt number, which can wake the mcu from sleep. * @param mode Interrupt mode, as passed to attachInterrupt. * @param ms Time to sleep, in [ms]. - * @return -1 when woken by timer, or interrupt number when woken by interrupt. + * @return MY_WAKE_UP_BY_TIMER when woken by timer, or interrupt number when woken by interrupt. */ int8_t hwSleep(uint8_t interrupt, uint8_t mode, unsigned long ms); @@ -82,14 +77,14 @@ int8_t hwSleep(uint8_t interrupt, uint8_t mode, unsigned long ms); * @param interrupt2 Interrupt2 number, which can wake the mcu from sleep. * @param mode2 Interrupt2 mode, as passed to attachInterrupt. * @param ms Time to sleep, in [ms]. - * @return -1 when woken by timer, or interrupt number when woken by interrupt. + * @return MY_WAKE_UP_BY_TIMER when woken by timer, or interrupt number when woken by interrupt. */ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms); /** * Retrieve unique ID -* @param uniqueID uniqueID +* @param uniqueID unique ID * @return True if unique ID successfully retrieved */ bool hwUniqueID(unique_id_t* uniqueID); diff --git a/hal/architecture/MyHwTeensy3.cpp b/hal/architecture/MyHwTeensy3.cpp index 391dc43..e84c65e 100644 --- a/hal/architecture/MyHwTeensy3.cpp +++ b/hal/architecture/MyHwTeensy3.cpp @@ -132,6 +132,24 @@ uint16_t hwFreeMem() return FUNCTION_NOT_SUPPORTED; } +void hwRandomNumberInit(void) +{ +#if defined(__MK64FX512__) || defined(__MK66FX1M0__) + // use HW RNG present on Teensy3.5/3.6 + // init RNG + SIM_SCGC6 |= SIM_SCGC6_RNGA; // enable RNG + RNG_CR &= ~RNG_CR_SLP_MASK; + RNG_CR |= RNG_CR_HA_MASK; // high assurance + // get random number + RNG_CR |= RNG_CR_GO_MASK; + while (!(RNG_SR & RNG_SR_OREG_LVL(0xF))); + const uint32_t seed = RNG_OR; + randomSeed(seed); +#else + randomSeed(analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN)); +#endif +} + void hwDebugPrint(const char *fmt, ...) { char fmtBuffer[MY_SERIAL_OUTPUT_SIZE]; diff --git a/hal/architecture/MyHwTeensy3.h b/hal/architecture/MyHwTeensy3.h index c8bd02f..529f603 100644 --- a/hal/architecture/MyHwTeensy3.h +++ b/hal/architecture/MyHwTeensy3.h @@ -38,15 +38,25 @@ #define _BV(x) (1<<(x)) #endif +#if defined(__MK64FX512__) || defined(__MK66FX1M0__) +#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))<