From abba1ec3771948a485c5a58cf3c3d6558090079e Mon Sep 17 00:00:00 2001 From: Olivier Date: Sun, 21 May 2017 14:20:07 +0200 Subject: [PATCH] Minor updates STM32F1 support (#851) --- hal/architecture/MyHwSTM32F1.cpp | 32 +++++++++++++++++++++++++----- hal/architecture/MyHwSTM32F1.h | 18 ++++++++--------- hal/architecture/MyMainSTM32F1.cpp | 12 +++++------ 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/hal/architecture/MyHwSTM32F1.cpp b/hal/architecture/MyHwSTM32F1.cpp index 455f834..ef34318 100644 --- a/hal/architecture/MyHwSTM32F1.cpp +++ b/hal/architecture/MyHwSTM32F1.cpp @@ -1,4 +1,4 @@ -/** +/** * The MySensors Arduino library handles the wireless radio link and protocol * between your home built sensors/actuators and HA controller of choice. * The sensors forms a self healing radio network with optional repeaters. Each @@ -18,8 +18,6 @@ */ #include "MyHwSTM32F1.h" -#include - /* * Pinout STM32F103C8 dev board: @@ -50,7 +48,6 @@ bool hwInit(void) MY_SERIALDEVICE.begin(MY_BAUD_RATE); #endif if (EEPROM.init() == EEPROM_OK) { - uint16 cnt; EEPROM.count(&cnt); if(cnt>=EEPROM.maxcount()) { @@ -120,6 +117,31 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo return MY_SLEEP_NOT_POSSIBLE; } + +void hwRandomNumberInit(void) +{ + // use internal temperature sensor as noise source + adc_reg_map *regs = ADC1->regs; + regs->CR2 |= ADC_CR2_TSVREFE; + regs->SMPR1 |= ADC_SMPR1_SMP16; + + uint32_t seed = 0; + uint16_t currentValue, newValue = 0; + + for (uint8_t i = 0; i<32; i++) { + const uint32_t timeout = hwMillis() + 20; + while (timeout >= hwMillis()) { + newValue = adc_read(ADC1, 16); + if (newValue != currentValue) { + currentValue = newValue; + break; + } + } + seed ^= ( (newValue + hwMillis()) & 7) << i; + } + randomSeed(seed); +} + bool hwUniqueID(unique_id_t* uniqueID) { (void)memcpy((uint8_t*)uniqueID, (uint32_t*)0x1FFFF7E0, 16); @@ -142,7 +164,7 @@ uint16_t hwCPUFrequency() uint16_t hwFreeMem() { //Not yet implemented - return 0; + return FUNCTION_NOT_SUPPORTED; } void hwDebugPrint(const char *fmt, ...) diff --git a/hal/architecture/MyHwSTM32F1.h b/hal/architecture/MyHwSTM32F1.h index 3478741..d63bfa1 100644 --- a/hal/architecture/MyHwSTM32F1.h +++ b/hal/architecture/MyHwSTM32F1.h @@ -24,7 +24,7 @@ #include #include - +#include #ifdef __cplusplus #include #endif @@ -33,37 +33,37 @@ #define snprintf_P snprintf #define vsnprintf_P vsnprintf - // emulated EEPROM (experimental) #define EEPROM_PAGE_SIZE (uint16_t)1024 #define EEPROM_SIZE (uint16_t)2048 #define EEPROM_START_ADDRESS ((uint32_t)(0x8000000 + 128 * EEPROM_PAGE_SIZE - 2 * EEPROM_PAGE_SIZE)) - #ifndef MY_SERIALDEVICE #define MY_SERIALDEVICE Serial #endif -// Define these as macros to save valuable space +#ifndef digitalPinToInterrupt +#define digitalPinToInterrupt(__pin) (__pin) +#endif + #define hwDigitalWrite(__pin, __value) digitalWrite(__pin, __value) #define hwDigitalRead(__pin) digitalRead(__pin) #define hwPinMode(__pin, __value) pinMode(__pin, __value) #define hwWatchdogReset() iwdg_feed() #define hwReboot() nvic_sys_reset() #define hwMillis() millis() -void (*serialEventRun)() = NULL; +extern void serialEventRun(void) __attribute__((weak)); +//void (*serialEventRun)() = NULL; bool hwInit(void); +void hwRandomNumberInit(void); void hwReadConfigBlock(void* buf, void* adr, size_t length); void hwWriteConfigBlock(void* buf, void* adr, size_t length); void hwWriteConfig(const int addr, uint8_t value); uint8_t hwReadConfig(const int addr); - -#define hwRandomNumberInit() randomSeed(analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN)) - #ifndef DOXYGEN #define MY_CRITICAL_SECTION #endif /* DOXYGEN */ -#endif +#endif \ No newline at end of file diff --git a/hal/architecture/MyMainSTM32F1.cpp b/hal/architecture/MyMainSTM32F1.cpp index c529f64..83f90ce 100644 --- a/hal/architecture/MyMainSTM32F1.cpp +++ b/hal/architecture/MyMainSTM32F1.cpp @@ -1,4 +1,4 @@ -/** +/** * The MySensors Arduino library handles the wireless radio link and protocol * between your home built sensors/actuators and HA controller of choice. * The sensors forms a self healing radio network with optional repeaters. Each @@ -17,8 +17,6 @@ * version 2 as published by the Free Software Foundation. */ -// Initialize library and handle sketch functions like we want to - extern "C" void __libc_init_array(void); // Force init to be called *first*, i.e. before static object allocation. @@ -28,13 +26,13 @@ __attribute__(( constructor (101))) void premain() init(); } +// Initialize library and handle sketch functions like we want to int main(void) { - _begin(); - + _begin(); // Startup MySensors library for(;;) { - _process(); - if (loop) { + _process(); // Process incoming data + if (loop) { // Call sketch loop loop(); } if (serialEventRun) {