From 257c600edcc131aa3f2c0a175424c826f916e106 Mon Sep 17 00:00:00 2001 From: KooLru Date: Sat, 23 May 2020 18:47:35 +0300 Subject: [PATCH 01/10] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index bec8ade2e..b0c25487d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ doxygen.log TAGS tags .DS_Store +.vs/* From 58e1f0bf199227643cf9d1ebde60b9b94e102384 Mon Sep 17 00:00:00 2001 From: KooLru Date: Sat, 23 May 2020 22:43:13 +0300 Subject: [PATCH 02/10] STM32 Cores start implementation --- .gitignore | 1 + MySensors.h | 7 +- hal/architecture/STM32/MyHwSTM32.cpp | 255 +++++++++++++++++++++++++ hal/architecture/STM32/MyHwSTM32.h | 132 +++++++++++++ hal/architecture/STM32/MyMainSTM32.cpp | 67 +++++++ 5 files changed, 461 insertions(+), 1 deletion(-) create mode 100644 hal/architecture/STM32/MyHwSTM32.cpp create mode 100644 hal/architecture/STM32/MyHwSTM32.h create mode 100644 hal/architecture/STM32/MyMainSTM32.cpp diff --git a/.gitignore b/.gitignore index bec8ade2e..b0c25487d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ doxygen.log TAGS tags .DS_Store +.vs/* diff --git a/MySensors.h b/MySensors.h index de78f3688..5f3093489 100644 --- a/MySensors.h +++ b/MySensors.h @@ -67,6 +67,9 @@ #include "drivers/extEEPROM/extEEPROM.cpp" #include "hal/architecture/SAMD/MyHwSAMD.cpp" #include "hal/crypto/generic/MyCryptoGeneric.cpp" +#elif defined(ARDUINO_ARCH_STM32) +#include "hal/architecture/STM32/MyHwSTM32.cpp" +#include "hal/crypto/generic/MyCryptoGeneric.cpp" #elif defined(ARDUINO_ARCH_STM32F1) #include "hal/architecture/STM32F1/MyHwSTM32F1.cpp" #include "hal/crypto/generic/MyCryptoGeneric.cpp" @@ -313,7 +316,7 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs // RAM ROUTING TABLE #if defined(MY_RAM_ROUTING_TABLE_FEATURE) && defined(MY_REPEATER_FEATURE) // activate feature based on architecture -#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_NRF5) || defined(ARDUINO_ARCH_STM32F1) || defined(TEENSYDUINO) || defined(__linux__) +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_NRF5) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32) || defined(TEENSYDUINO) || defined(__linux__) #define MY_RAM_ROUTING_TABLE_ENABLED #elif defined(ARDUINO_ARCH_AVR) #if defined(__avr_atmega1280__) || defined(__avr_atmega1284__) || defined(__avr_atmega2560__) @@ -447,6 +450,8 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs #include "hal/architecture/Linux/MyMainLinuxGeneric.cpp" #elif defined(ARDUINO_ARCH_STM32F1) #include "hal/architecture/STM32F1/MyMainSTM32F1.cpp" +#elif defined(ARDUINO_ARCH_STM32) +#include "hal/architecture/STM32/MyMainSTM32.cpp" #elif defined(__arm__) && defined(TEENSYDUINO) #include "hal/architecture/Teensy3/MyMainTeensy3.cpp" #endif diff --git a/hal/architecture/STM32/MyHwSTM32.cpp b/hal/architecture/STM32/MyHwSTM32.cpp new file mode 100644 index 000000000..a30d320cc --- /dev/null +++ b/hal/architecture/STM32/MyHwSTM32.cpp @@ -0,0 +1,255 @@ +/* + * 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 + * repeater and gateway builds a routing tables in EEPROM which keeps track of the + * network topology allowing messages to be routed to nodes. + * + * Created by Henrik Ekblad + * Copyright (C) 2013-2020 Sensnology AB + * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors + * + * Documentation: http://www.mysensors.org + * Support Forum: http://forum.mysensors.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + */ + +#include "MyHwSTM32.h" + +/* +* Pinout STM32F103C8 dev board: +* http://wiki.stm32duino.com/images/a/ae/Bluepillpinout.gif +* +* Wiring +* -------------------------------------------------- +RFM69 CLK MISO MOSI CSN CE IRQ +SPI1 PA5 PA6 PA7 PA4 NA PA3 (default) + +RF24 CLK MISO MOSI CSN CE IRQ +SPI1 PA5 PA6 PA7 PA4 PB0 NA + +*/ + +bool hwInit(void) +{ +#if !defined(MY_DISABLED_SERIAL) + MY_SERIALDEVICE.begin(MY_BAUD_RATE); + #if defined(MY_GATEWAY_SERIAL) + while (!MY_SERIALDEVICE) {} +#endif +#endif + //todo EEPROM initialisation???? + Serial1.print("EEPROM.length "); + Serial1.println(EEPROM.length()); + + //if (EEPROM.init() == EEPROM_OK) { + // uint16 cnt; + // EEPROM.count(&cnt); + // if(cnt>=EEPROM.maxcount()) { + // // tmp, WIP: format eeprom if full + // EEPROM.format(); + // } + return true; + //} + //return false; +} + +void hwReadConfigBlock(void *buf, void *addr, size_t length) +{ + uint8_t *dst = static_cast(buf); + int offs = reinterpret_cast(addr); + while (length-- > 0) { + *dst++ = EEPROM.read(offs++); + } +} + +void hwWriteConfigBlock(void *buf, void *addr, size_t length) +{ + uint8_t *src = static_cast(buf); + int offs = reinterpret_cast(addr); + while (length-- > 0) { + EEPROM.write(offs++, *src++); + } +// EEPROM.commit(); +} + +uint8_t hwReadConfig(const int addr) +{ + uint8_t value; + hwReadConfigBlock(&value, reinterpret_cast(addr), 1); + return value; +} + +void hwWriteConfig(const int addr, uint8_t value) +{ + if (hwReadConfig(addr) != value) { + hwWriteConfigBlock(&value, reinterpret_cast(addr), 1); + } +} + +int8_t hwSleep(uint32_t ms) +{ + // TODO: Not supported! + (void)ms; + return MY_SLEEP_NOT_POSSIBLE; +} + +int8_t hwSleep(const uint8_t interrupt, const uint8_t mode, uint32_t ms) +{ + // TODO: Not supported! + (void)interrupt; + (void)mode; + (void)ms; + return MY_SLEEP_NOT_POSSIBLE; +} + +int8_t hwSleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2, + const uint8_t mode2, + uint32_t ms) +{ + // TODO: Not supported! + (void)interrupt1; + (void)mode1; + (void)interrupt2; + (void)mode2; + (void)ms; + return MY_SLEEP_NOT_POSSIBLE; +} + + +//void hwRandomNumberInit(void) +//{ +// // This function initializes the random number generator with a seed +// // of 32 bits. This method is good enough to earn FIPS 140-2 conform +// // random data. This should reach to generate 32 Bit for randomSeed(). +// uint32_t seed = 0; +// uint32_t timeout = millis() + 20; +// +// // Trigger floating effect of an unconnected pin +// pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT_PULLUP); +// pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT); +// delay(10); +// +// // Generate 32 bits of datas +// for (uint8_t i=0; i<32; i++) { +// const int pinValue = analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN); +// // Wait until the analog value has changed +// while ((pinValue == analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN)) && (timeout>=millis())) { +// seed ^= (millis() << i); +// // Check of data generation is slow +// if (timeout<=millis()) { +// // Trigger pin again +// pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT_PULLUP); +// pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT); +// // Pause a short while +// delay(seed % 10); +// timeout = millis() + 20; +// } +// } +// } +// randomSeed(seed); +// +//// // 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 = 0; +//// uint16_t 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); +//// regs->CR2 &= ~ADC_CR2_TSVREFE; // disable VREFINT and temp sensor +//} + +bool hwUniqueID(unique_id_t *uniqueID) +{ + (void)memcpy((uint8_t *)uniqueID, (uint32_t *)0x1FFFF7E0, 16); // FlashID + ChipID + return true; +} + +uint16_t hwCPUVoltage(void) +{ + //Not yet implemented + return FUNCTION_NOT_SUPPORTED; + + //adc_reg_map *regs = ADC1->regs; + //regs->CR2 |= ADC_CR2_TSVREFE; // enable VREFINT and temp sensor + //regs->SMPR1 = ADC_SMPR1_SMP17; // sample rate for VREFINT ADC channel + //adc_calibrate(ADC1); +// + //const uint16_t vdd = adc_read(ADC1, 17); + //regs->CR2 &= ~ADC_CR2_TSVREFE; // disable VREFINT and temp sensor + //return (uint16_t)(1200u * 4096u / vdd); +} + +uint16_t hwCPUFrequency(void) +{ + return F_CPU/100000UL; +} +//https://github.com/stm32duino/Arduino_Core_STM32/issues/5#issuecomment-411868042 +int8_t hwCPUTemperature(void) +{ + //STM32ADC adc(ADC1); + + //adc.begin(PB1); + + // adc.startConversion(); + + //uint32_t v = adc.read(); + //uint32_t vcc = adc.measureVcc(); + //int16_t temp = adc.measureTemp(); + //printf("ADC: %lu, vcc: %lumV temp: %dC\n", v, vcc, temp); + //delay(1000); + +// return adc.measureTemp(); + + //adc_reg_map *regs = ADC1->regs; + //regs->CR2 |= ADC_CR2_TSVREFE; // enable VREFINT and Temperature sensor + //regs->SMPR1 |= ADC_SMPR1_SMP16 | ADC_SMPR1_SMP17; + //adc_calibrate(ADC1); +// + ////const uint16_t adc_temp = adc_read(ADC1, 16); + ////const uint16_t vref = 1200 * 4096 / adc_read(ADC1, 17); + //// calibrated at 25°C, ADC output = 1430mV, avg slope = 4.3mV / °C, increasing temp ~ lower voltage + //const int8_t temp = static_cast((1430.0 - (adc_read(ADC1, 16) * 1200 / adc_read(ADC1, + // 17))) / 4.3 + 25.0); + //regs->CR2 &= ~ADC_CR2_TSVREFE; // disable VREFINT and temp sensor + //return (temp - MY_STM32F1_TEMPERATURE_OFFSET) / MY_STM32F1_TEMPERATURE_GAIN; + return FUNCTION_NOT_SUPPORTED; + +} + +uint16_t hwFreeMem(void) +{ + //Not yet implemented + return FUNCTION_NOT_SUPPORTED; +} + + +void hwWatchdogReset(void) +{ + IWatchdog.reload(); + //NRF_WDT->RR[0] = WDT_RR_RR_Reload; +} + +void hwReboot(void) +{ + NVIC_SystemReset(); + while (true) + ; +} diff --git a/hal/architecture/STM32/MyHwSTM32.h b/hal/architecture/STM32/MyHwSTM32.h new file mode 100644 index 000000000..bf3d64ba2 --- /dev/null +++ b/hal/architecture/STM32/MyHwSTM32.h @@ -0,0 +1,132 @@ +/* + * 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 + * repeater and gateway builds a routing tables in EEPROM which keeps track of the + * network topology allowing messages to be routed to nodes. + * + * Created by Henrik Ekblad + * Copyright (C) 2013-2020 Sensnology AB + * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors + * + * Documentation: http://www.mysensors.org + * Support Forum: http://forum.mysensors.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + */ + +#ifndef MyHwSTM32_h +#define MyHwSTM32_h + +#include +#include +#include +#include + +#ifdef __cplusplus +#include +#endif + +#define CRYPTO_LITTLE_ENDIAN + +#ifndef MY_SERIALDEVICE +#define MY_SERIALDEVICE Serial +#endif + +#ifndef MY_DEBUGDEVICE +#define MY_DEBUGDEVICE MY_SERIALDEVICE +#endif + +#ifndef MY_STM32_TEMPERATURE_OFFSET +#define MY_STM32_TEMPERATURE_OFFSET (0.0f) +#endif + +#ifndef MY_STM32_TEMPERATURE_GAIN +#define MY_STM32_TEMPERATURE_GAIN (1.0f) +#endif + +// SS default +#ifndef SS +#define SS PA4 +#endif + +// mapping +//#define yield() // not defined + +//#ifndef digitalPinToInterrupt +//#define digitalPinToInterrupt(__pin) (__pin) +//#endif +// redefine 8 bit types of inttypes.h (as of SAMD board defs 1.8.1) +#undef PRId8 +#undef PRIi8 +#undef PRIo8 +#undef PRIu8 +#undef PRIx8 +#undef PRIX8 +#undef PRIdLEAST8 +#undef PRIiLEAST8 +#undef PRIoLEAST8 +#undef PRIuLEAST8 +#undef PRIxLEAST8 +#undef PRIXLEAST8 +#undef PRIdFAST8 +#undef PRIiFAST8 +#undef PRIoFAST8 +#undef PRIuFAST8 +#undef PRIxFAST8 +#undef PRIXFAST8 +#define PRId8 "d" +#define PRIi8 "i" +#define PRIo8 "o" +#define PRIu8 "u" +#define PRIx8 "x" +#define PRIX8 "X" +#define PRIdLEAST8 "d" +#define PRIiLEAST8 "i" +#define PRIoLEAST8 "o" +#define PRIuLEAST8 "u" +#define PRIxLEAST8 "x" +#define PRIXLEAST8 "X" +#define PRIdFAST8 "d" +#define PRIiFAST8 "i" +#define PRIoFAST8 "o" +#define PRIuFAST8 "u" +#define PRIxFAST8 "x" +#define PRIXFAST8 "X" + +#define hwDigitalWrite(__pin, __value) digitalWrite(__pin, __value) +#define hwDigitalRead(__pin) digitalRead(__pin) +#define hwPinMode(__pin, __value) pinMode(__pin, __value) +#define hwMillis() millis() + +//void hwRandomNumberInit(void); +#define hwRandomNumberInit() randomSeed(analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN)) +#define hwGetSleepRemaining() (0ul) + +//extern void serialEventRun(void) __attribute__((weak)); +bool hwInit(void); + +void hwWatchdogReset(void); +void hwReboot(void); + +void hwReadConfigBlock(void *buf, void *addr, size_t length); +void hwWriteConfigBlock(void *buf, void *addr, size_t length); +void hwWriteConfig(const int addr, uint8_t value); +uint8_t hwReadConfig(const int addr); + + + +// SOFTSPI +#ifdef MY_SOFTSPI +#error Soft SPI is not available on this architecture! +#endif +#define hwSPI SPI //!< hwSPI + + +#ifndef DOXYGEN +#define MY_CRITICAL_SECTION +#endif /* DOXYGEN */ + +#endif diff --git a/hal/architecture/STM32/MyMainSTM32.cpp b/hal/architecture/STM32/MyMainSTM32.cpp new file mode 100644 index 000000000..05fe5486f --- /dev/null +++ b/hal/architecture/STM32/MyMainSTM32.cpp @@ -0,0 +1,67 @@ +/* + * 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 + * repeater and gateway builds a routing tables in EEPROM which keeps track of the + * network topology allowing messages to be routed to nodes. + * + * Created by Henrik Ekblad + * Copyright (C) 2013-2020 Sensnology AB + * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors + * + * Documentation: http://www.mysensors.org + * Support Forum: http://forum.mysensors.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + */ + +#define ARDUINO_MAIN +#include "Arduino.h" + +// Force init to be called *first*, i.e. before static object allocation. +// Otherwise, statically allocated objects that need HAL may fail. +__attribute__((constructor(101))) void premain() +{ + + // Required by FreeRTOS, see http://www.freertos.org/RTOS-Cortex-M3-M4.html +#ifdef NVIC_PRIORITYGROUP_4 + HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); +#endif +#if (__CORTEX_M == 0x07U) + // Defined in CMSIS core_cm7.h +#ifndef I_CACHE_DISABLED + SCB_EnableICache(); +#endif +#ifndef D_CACHE_DISABLED + SCB_EnableDCache(); +#endif +#endif + + init(); +} + +/* + * \brief Main entry point of Arduino application + */ +int main(void) +{ + + initVariant(); + + _begin(); // Startup MySensors library + for(;;) { + _process(); // Process incoming data +#if defined(CORE_CALLBACK) + CoreCallback(); +#endif + if (loop) { + loop(); // Call sketch loop + } + if (serialEventRun) { + serialEventRun(); + } + } + return 0; +} From 23adb6f00462e63cb8515fb7f50c0afa584386eb Mon Sep 17 00:00:00 2001 From: KooLru Date: Sun, 24 May 2020 00:38:01 +0300 Subject: [PATCH 03/10] Cleanup code --- hal/architecture/STM32/MyHwSTM32.cpp | 109 +-------------------------- 1 file changed, 2 insertions(+), 107 deletions(-) diff --git a/hal/architecture/STM32/MyHwSTM32.cpp b/hal/architecture/STM32/MyHwSTM32.cpp index a30d320cc..cd758cd6d 100644 --- a/hal/architecture/STM32/MyHwSTM32.cpp +++ b/hal/architecture/STM32/MyHwSTM32.cpp @@ -41,20 +41,8 @@ bool hwInit(void) while (!MY_SERIALDEVICE) {} #endif #endif - //todo EEPROM initialisation???? - Serial1.print("EEPROM.length "); - Serial1.println(EEPROM.length()); - - //if (EEPROM.init() == EEPROM_OK) { - // uint16 cnt; - // EEPROM.count(&cnt); - // if(cnt>=EEPROM.maxcount()) { - // // tmp, WIP: format eeprom if full - // EEPROM.format(); - // } + return true; - //} - //return false; } void hwReadConfigBlock(void *buf, void *addr, size_t length) @@ -73,7 +61,6 @@ void hwWriteConfigBlock(void *buf, void *addr, size_t length) while (length-- > 0) { EEPROM.write(offs++, *src++); } -// EEPROM.commit(); } uint8_t hwReadConfig(const int addr) @@ -120,61 +107,6 @@ int8_t hwSleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t inte } -//void hwRandomNumberInit(void) -//{ -// // This function initializes the random number generator with a seed -// // of 32 bits. This method is good enough to earn FIPS 140-2 conform -// // random data. This should reach to generate 32 Bit for randomSeed(). -// uint32_t seed = 0; -// uint32_t timeout = millis() + 20; -// -// // Trigger floating effect of an unconnected pin -// pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT_PULLUP); -// pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT); -// delay(10); -// -// // Generate 32 bits of datas -// for (uint8_t i=0; i<32; i++) { -// const int pinValue = analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN); -// // Wait until the analog value has changed -// while ((pinValue == analogRead(MY_SIGNING_SOFT_RANDOMSEED_PIN)) && (timeout>=millis())) { -// seed ^= (millis() << i); -// // Check of data generation is slow -// if (timeout<=millis()) { -// // Trigger pin again -// pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT_PULLUP); -// pinMode(MY_SIGNING_SOFT_RANDOMSEED_PIN, INPUT); -// // Pause a short while -// delay(seed % 10); -// timeout = millis() + 20; -// } -// } -// } -// randomSeed(seed); -// -//// // 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 = 0; -//// uint16_t 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); -//// regs->CR2 &= ~ADC_CR2_TSVREFE; // disable VREFINT and temp sensor -//} bool hwUniqueID(unique_id_t *uniqueID) { @@ -186,52 +118,16 @@ uint16_t hwCPUVoltage(void) { //Not yet implemented return FUNCTION_NOT_SUPPORTED; - - //adc_reg_map *regs = ADC1->regs; - //regs->CR2 |= ADC_CR2_TSVREFE; // enable VREFINT and temp sensor - //regs->SMPR1 = ADC_SMPR1_SMP17; // sample rate for VREFINT ADC channel - //adc_calibrate(ADC1); -// - //const uint16_t vdd = adc_read(ADC1, 17); - //regs->CR2 &= ~ADC_CR2_TSVREFE; // disable VREFINT and temp sensor - //return (uint16_t)(1200u * 4096u / vdd); } uint16_t hwCPUFrequency(void) { return F_CPU/100000UL; } -//https://github.com/stm32duino/Arduino_Core_STM32/issues/5#issuecomment-411868042 + int8_t hwCPUTemperature(void) { - //STM32ADC adc(ADC1); - - //adc.begin(PB1); - - // adc.startConversion(); - - //uint32_t v = adc.read(); - //uint32_t vcc = adc.measureVcc(); - //int16_t temp = adc.measureTemp(); - //printf("ADC: %lu, vcc: %lumV temp: %dC\n", v, vcc, temp); - //delay(1000); - -// return adc.measureTemp(); - - //adc_reg_map *regs = ADC1->regs; - //regs->CR2 |= ADC_CR2_TSVREFE; // enable VREFINT and Temperature sensor - //regs->SMPR1 |= ADC_SMPR1_SMP16 | ADC_SMPR1_SMP17; - //adc_calibrate(ADC1); -// - ////const uint16_t adc_temp = adc_read(ADC1, 16); - ////const uint16_t vref = 1200 * 4096 / adc_read(ADC1, 17); - //// calibrated at 25°C, ADC output = 1430mV, avg slope = 4.3mV / °C, increasing temp ~ lower voltage - //const int8_t temp = static_cast((1430.0 - (adc_read(ADC1, 16) * 1200 / adc_read(ADC1, - // 17))) / 4.3 + 25.0); - //regs->CR2 &= ~ADC_CR2_TSVREFE; // disable VREFINT and temp sensor - //return (temp - MY_STM32F1_TEMPERATURE_OFFSET) / MY_STM32F1_TEMPERATURE_GAIN; return FUNCTION_NOT_SUPPORTED; - } uint16_t hwFreeMem(void) @@ -244,7 +140,6 @@ uint16_t hwFreeMem(void) void hwWatchdogReset(void) { IWatchdog.reload(); - //NRF_WDT->RR[0] = WDT_RR_RR_Reload; } void hwReboot(void) From 7249c449ed18f0a210d25da7516bedfec7ee9ca0 Mon Sep 17 00:00:00 2001 From: KooLru Date: Sun, 24 May 2020 22:55:12 +0300 Subject: [PATCH 04/10] Fix coding style mistakes --- hal/architecture/STM32/MyHwSTM32.cpp | 6 +++--- hal/architecture/STM32/MyHwSTM32.h | 8 -------- hal/architecture/STM32/MyMainSTM32.cpp | 17 ++++++++--------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/hal/architecture/STM32/MyHwSTM32.cpp b/hal/architecture/STM32/MyHwSTM32.cpp index cd758cd6d..91e6cf4eb 100644 --- a/hal/architecture/STM32/MyHwSTM32.cpp +++ b/hal/architecture/STM32/MyHwSTM32.cpp @@ -23,7 +23,7 @@ * Pinout STM32F103C8 dev board: * http://wiki.stm32duino.com/images/a/ae/Bluepillpinout.gif * -* Wiring +* Wiring * -------------------------------------------------- RFM69 CLK MISO MOSI CSN CE IRQ SPI1 PA5 PA6 PA7 PA4 NA PA3 (default) @@ -37,12 +37,12 @@ bool hwInit(void) { #if !defined(MY_DISABLED_SERIAL) MY_SERIALDEVICE.begin(MY_BAUD_RATE); - #if defined(MY_GATEWAY_SERIAL) +#if defined(MY_GATEWAY_SERIAL) while (!MY_SERIALDEVICE) {} #endif #endif - return true; + return true; } void hwReadConfigBlock(void *buf, void *addr, size_t length) diff --git a/hal/architecture/STM32/MyHwSTM32.h b/hal/architecture/STM32/MyHwSTM32.h index bf3d64ba2..dd107605a 100644 --- a/hal/architecture/STM32/MyHwSTM32.h +++ b/hal/architecture/STM32/MyHwSTM32.h @@ -39,14 +39,6 @@ #define MY_DEBUGDEVICE MY_SERIALDEVICE #endif -#ifndef MY_STM32_TEMPERATURE_OFFSET -#define MY_STM32_TEMPERATURE_OFFSET (0.0f) -#endif - -#ifndef MY_STM32_TEMPERATURE_GAIN -#define MY_STM32_TEMPERATURE_GAIN (1.0f) -#endif - // SS default #ifndef SS #define SS PA4 diff --git a/hal/architecture/STM32/MyMainSTM32.cpp b/hal/architecture/STM32/MyMainSTM32.cpp index 05fe5486f..4b87dee7e 100644 --- a/hal/architecture/STM32/MyMainSTM32.cpp +++ b/hal/architecture/STM32/MyMainSTM32.cpp @@ -25,21 +25,21 @@ __attribute__((constructor(101))) void premain() { - // Required by FreeRTOS, see http://www.freertos.org/RTOS-Cortex-M3-M4.html + // Required by FreeRTOS, see http://www.freertos.org/RTOS-Cortex-M3-M4.html #ifdef NVIC_PRIORITYGROUP_4 - HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); + HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); #endif #if (__CORTEX_M == 0x07U) - // Defined in CMSIS core_cm7.h + // Defined in CMSIS core_cm7.h #ifndef I_CACHE_DISABLED - SCB_EnableICache(); + SCB_EnableICache(); #endif #ifndef D_CACHE_DISABLED - SCB_EnableDCache(); + SCB_EnableDCache(); #endif #endif - init(); + init(); } /* @@ -47,14 +47,13 @@ __attribute__((constructor(101))) void premain() */ int main(void) { - - initVariant(); + initVariant(); _begin(); // Startup MySensors library for(;;) { _process(); // Process incoming data #if defined(CORE_CALLBACK) - CoreCallback(); + CoreCallback(); #endif if (loop) { loop(); // Call sketch loop From ca6d806915035680887381b13d293367c5f7a249 Mon Sep 17 00:00:00 2001 From: KooLru Date: Tue, 26 May 2020 20:43:24 +0300 Subject: [PATCH 05/10] CPUFrequency via HAL_RCC_GetSysClockFreq --- hal/architecture/STM32/MyHwSTM32.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hal/architecture/STM32/MyHwSTM32.cpp b/hal/architecture/STM32/MyHwSTM32.cpp index 91e6cf4eb..2db50a60b 100644 --- a/hal/architecture/STM32/MyHwSTM32.cpp +++ b/hal/architecture/STM32/MyHwSTM32.cpp @@ -122,7 +122,7 @@ uint16_t hwCPUVoltage(void) uint16_t hwCPUFrequency(void) { - return F_CPU/100000UL; + return HAL_RCC_GetSysClockFreq()/1000000UL; } int8_t hwCPUTemperature(void) From 7f470b922e43724ac6c264a886ef92430b89e933 Mon Sep 17 00:00:00 2001 From: KooLru Date: Tue, 30 Jun 2020 11:18:02 +0300 Subject: [PATCH 06/10] Fix hwUniqueID and EEPROM Fix hwUniqueID on STMF4. Speedup EEPROM write. --- hal/architecture/STM32/MyHwSTM32.cpp | 19 ++++++++++++++++--- hal/architecture/STM32/MyHwSTM32.h | 7 +++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/hal/architecture/STM32/MyHwSTM32.cpp b/hal/architecture/STM32/MyHwSTM32.cpp index 2db50a60b..3561aa3d5 100644 --- a/hal/architecture/STM32/MyHwSTM32.cpp +++ b/hal/architecture/STM32/MyHwSTM32.cpp @@ -47,20 +47,29 @@ bool hwInit(void) void hwReadConfigBlock(void *buf, void *addr, size_t length) { + //MY_SERIALDEVICE.println(F("START hwReadConfigBlock")); uint8_t *dst = static_cast(buf); int offs = reinterpret_cast(addr); + + eeprom_buffer_fill(); while (length-- > 0) { - *dst++ = EEPROM.read(offs++); + *dst++ = eeprom_buffered_read_byte(offs++); } + //MY_SERIALDEVICE.println(F("END hwReadConfigBlock")); } void hwWriteConfigBlock(void *buf, void *addr, size_t length) { + //MY_SERIALDEVICE.println(F("START hwWriteConfigBlock")); uint8_t *src = static_cast(buf); int offs = reinterpret_cast(addr); + while (length-- > 0) { - EEPROM.write(offs++, *src++); + eeprom_buffered_write_byte(offs++, *src++); } + //MY_SERIALDEVICE.println(F("START FLUSH")); + eeprom_buffer_flush(); + //MY_SERIALDEVICE.println(F("END hwWriteConfigBlock")); } uint8_t hwReadConfig(const int addr) @@ -110,7 +119,11 @@ int8_t hwSleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t inte bool hwUniqueID(unique_id_t *uniqueID) { - (void)memcpy((uint8_t *)uniqueID, (uint32_t *)0x1FFFF7E0, 16); // FlashID + ChipID + //Fill ID with FF + (void)memset((uint8_t *)uniqueID, 0xFF, 16); + //Read device ID + (void)memcpy((uint8_t *)uniqueID, (uint32_t *)UID_BASE, 12); + return true; } diff --git a/hal/architecture/STM32/MyHwSTM32.h b/hal/architecture/STM32/MyHwSTM32.h index dd107605a..2c3dee304 100644 --- a/hal/architecture/STM32/MyHwSTM32.h +++ b/hal/architecture/STM32/MyHwSTM32.h @@ -22,7 +22,6 @@ #include #include -#include #include #ifdef __cplusplus @@ -32,7 +31,11 @@ #define CRYPTO_LITTLE_ENDIAN #ifndef MY_SERIALDEVICE -#define MY_SERIALDEVICE Serial +#if SERIAL_UART_INSTANCE == 2 //On Nucleo board UART 2 connect to ST-LINK +#define MY_SERIALDEVICE Serial2 +#else +#define MY_SERIALDEVICE Serial +#endif #endif #ifndef MY_DEBUGDEVICE From bef2c938d2a6eef4ce2083aa85dfe079a3f6fe5b Mon Sep 17 00:00:00 2001 From: KooLru Date: Tue, 30 Jun 2020 11:23:06 +0300 Subject: [PATCH 07/10] Code cleanup --- hal/architecture/STM32/MyHwSTM32.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/hal/architecture/STM32/MyHwSTM32.cpp b/hal/architecture/STM32/MyHwSTM32.cpp index 3561aa3d5..1a1c8bc55 100644 --- a/hal/architecture/STM32/MyHwSTM32.cpp +++ b/hal/architecture/STM32/MyHwSTM32.cpp @@ -47,7 +47,6 @@ bool hwInit(void) void hwReadConfigBlock(void *buf, void *addr, size_t length) { - //MY_SERIALDEVICE.println(F("START hwReadConfigBlock")); uint8_t *dst = static_cast(buf); int offs = reinterpret_cast(addr); @@ -55,21 +54,17 @@ void hwReadConfigBlock(void *buf, void *addr, size_t length) while (length-- > 0) { *dst++ = eeprom_buffered_read_byte(offs++); } - //MY_SERIALDEVICE.println(F("END hwReadConfigBlock")); } void hwWriteConfigBlock(void *buf, void *addr, size_t length) { - //MY_SERIALDEVICE.println(F("START hwWriteConfigBlock")); uint8_t *src = static_cast(buf); int offs = reinterpret_cast(addr); while (length-- > 0) { eeprom_buffered_write_byte(offs++, *src++); } - //MY_SERIALDEVICE.println(F("START FLUSH")); eeprom_buffer_flush(); - //MY_SERIALDEVICE.println(F("END hwWriteConfigBlock")); } uint8_t hwReadConfig(const int addr) @@ -119,9 +114,9 @@ int8_t hwSleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t inte bool hwUniqueID(unique_id_t *uniqueID) { - //Fill ID with FF + // Fill ID with FF (void)memset((uint8_t *)uniqueID, 0xFF, 16); - //Read device ID + // Read device ID (void)memcpy((uint8_t *)uniqueID, (uint32_t *)UID_BASE, 12); return true; From 4c3ac5512a1bad7bff58291351a75f41849f97e4 Mon Sep 17 00:00:00 2001 From: KooLru Date: Tue, 30 Jun 2020 11:39:24 +0300 Subject: [PATCH 08/10] Code cleanup --- hal/architecture/STM32/MyHwSTM32.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/hal/architecture/STM32/MyHwSTM32.cpp b/hal/architecture/STM32/MyHwSTM32.cpp index 1a1c8bc55..7bce587b9 100644 --- a/hal/architecture/STM32/MyHwSTM32.cpp +++ b/hal/architecture/STM32/MyHwSTM32.cpp @@ -49,7 +49,6 @@ void hwReadConfigBlock(void *buf, void *addr, size_t length) { uint8_t *dst = static_cast(buf); int offs = reinterpret_cast(addr); - eeprom_buffer_fill(); while (length-- > 0) { *dst++ = eeprom_buffered_read_byte(offs++); @@ -60,7 +59,6 @@ void hwWriteConfigBlock(void *buf, void *addr, size_t length) { uint8_t *src = static_cast(buf); int offs = reinterpret_cast(addr); - while (length-- > 0) { eeprom_buffered_write_byte(offs++, *src++); } From 13c42ca4a706812536ca99b68f33172c64b7090e Mon Sep 17 00:00:00 2001 From: KooLru Date: Tue, 30 Jun 2020 11:46:19 +0300 Subject: [PATCH 09/10] Reformat the code --- hal/architecture/STM32/MyHwSTM32.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/hal/architecture/STM32/MyHwSTM32.h b/hal/architecture/STM32/MyHwSTM32.h index 2c3dee304..eae6bd820 100644 --- a/hal/architecture/STM32/MyHwSTM32.h +++ b/hal/architecture/STM32/MyHwSTM32.h @@ -31,10 +31,11 @@ #define CRYPTO_LITTLE_ENDIAN #ifndef MY_SERIALDEVICE -#if SERIAL_UART_INSTANCE == 2 //On Nucleo board UART 2 connect to ST-LINK +// On Nucleo board UART 2 connect to ST-LINK +#if SERIAL_UART_INSTANCE == 2 #define MY_SERIALDEVICE Serial2 -#else -#define MY_SERIALDEVICE Serial +#else +#define MY_SERIALDEVICE Serial #endif #endif From df9c00a717ff074d6e46ed2394c00c84d75378bf Mon Sep 17 00:00:00 2001 From: KooLru Date: Tue, 25 Aug 2020 01:31:36 +0300 Subject: [PATCH 10/10] Add IRQ pin definition for RFM69 radio --- hal/transport/RFM69/driver/new/RFM69_new.h | 2 ++ hal/transport/RFM69/driver/old/RFM69_old.h | 3 +++ 2 files changed, 5 insertions(+) diff --git a/hal/transport/RFM69/driver/new/RFM69_new.h b/hal/transport/RFM69/driver/new/RFM69_new.h index 8e2613466..0a64c55a9 100644 --- a/hal/transport/RFM69/driver/new/RFM69_new.h +++ b/hal/transport/RFM69/driver/new/RFM69_new.h @@ -90,6 +90,8 @@ #define DEFAULT_RFM69_IRQ_PIN (2) //!< DEFAULT_RFM69_IRQ_PIN #elif defined(LINUX_ARCH_RASPBERRYPI) #define DEFAULT_RFM69_IRQ_PIN (22) //!< DEFAULT_RFM69_IRQ_PIN +#elif defined(ARDUINO_ARCH_STM32) +#define DEFAULT_RFM69_IRQ_PIN (PA3) //!< DEFAULT_RFM69_IRQ_PIN #elif defined(ARDUINO_ARCH_STM32F1) #define DEFAULT_RFM69_IRQ_PIN (PA3) //!< DEFAULT_RFM69_IRQ_PIN #elif defined(TEENSYDUINO) diff --git a/hal/transport/RFM69/driver/old/RFM69_old.h b/hal/transport/RFM69/driver/old/RFM69_old.h index 4b3f95245..1009f3893 100644 --- a/hal/transport/RFM69/driver/old/RFM69_old.h +++ b/hal/transport/RFM69/driver/old/RFM69_old.h @@ -56,6 +56,9 @@ #elif defined(LINUX_ARCH_RASPBERRYPI) #define DEFAULT_RFM69_IRQ_PIN (22) //!< DEFAULT_RFM69_IRQ_PIN #define DEFAULT_RFM69_IRQ_NUM DEFAULT_RFM69_IRQ_PIN //!< DEFAULT_RFM69_IRQ_NUM +#elif defined(ARDUINO_ARCH_STM32) +#define DEFAULT_RFM69_IRQ_PIN (PA3) //!< DEFAULT_RFM69_IRQ_PIN +#define DEFAULT_RFM69_IRQ_NUM DEFAULT_RFM69_IRQ_PIN //!< DEFAULT_RFM69_IRQ_NUM #elif defined(ARDUINO_ARCH_STM32F1) #define DEFAULT_RFM69_IRQ_PIN (PA3) //!< DEFAULT_RFM69_IRQ_PIN #define DEFAULT_RFM69_IRQ_NUM DEFAULT_RFM69_IRQ_PIN //!< DEFAULT_RFM69_IRQ_NUM