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 36aee1702..d1eb77947 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" @@ -321,7 +324,7 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs #define MY_RAM_ROUTING_TABLE_ENABLED #elif 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__) @@ -446,6 +449,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..7bce587b9 --- /dev/null +++ b/hal/architecture/STM32/MyHwSTM32.cpp @@ -0,0 +1,156 @@ +/* + * 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 + + return true; +} + +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++); + } +} + +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++); + } + eeprom_buffer_flush(); +} + +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; +} + + + +bool hwUniqueID(unique_id_t *uniqueID) +{ + // 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; +} + +uint16_t hwCPUVoltage(void) +{ + //Not yet implemented + return FUNCTION_NOT_SUPPORTED; +} + +uint16_t hwCPUFrequency(void) +{ + return HAL_RCC_GetSysClockFreq()/1000000UL; +} + +int8_t hwCPUTemperature(void) +{ + return FUNCTION_NOT_SUPPORTED; +} + +uint16_t hwFreeMem(void) +{ + //Not yet implemented + return FUNCTION_NOT_SUPPORTED; +} + + +void hwWatchdogReset(void) +{ + IWatchdog.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..eae6bd820 --- /dev/null +++ b/hal/architecture/STM32/MyHwSTM32.h @@ -0,0 +1,128 @@ +/* + * 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 + +#ifdef __cplusplus +#include +#endif + +#define CRYPTO_LITTLE_ENDIAN + +#ifndef MY_SERIALDEVICE +// On Nucleo board UART 2 connect to ST-LINK +#if SERIAL_UART_INSTANCE == 2 +#define MY_SERIALDEVICE Serial2 +#else +#define MY_SERIALDEVICE Serial +#endif +#endif + +#ifndef MY_DEBUGDEVICE +#define MY_DEBUGDEVICE MY_SERIALDEVICE +#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..4b87dee7e --- /dev/null +++ b/hal/architecture/STM32/MyMainSTM32.cpp @@ -0,0 +1,66 @@ +/* + * 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; +} 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