Skip to content

Commit

Permalink
add support of stm32duino/Arduino_Core_STM32
Browse files Browse the repository at this point in the history
  • Loading branch information
arcayi committed Aug 20, 2020
1 parent d51de8f commit 4f7a96b
Show file tree
Hide file tree
Showing 14 changed files with 552 additions and 2 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion MySensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
#elif defined(ARDUINO_ARCH_STM32F1)
#include "hal/architecture/STM32F1/MyHwSTM32F1.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_NRF5) || defined(ARDUINO_ARCH_NRF52)
#include "hal/architecture/NRF5/MyHwNRF5.cpp"
#include "hal/crypto/generic/MyCryptoGeneric.cpp"
Expand Down Expand Up @@ -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__)
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions README.stm32duino.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# MySensors Library readme for [stm32duino](https://github.com/stm32duino/Arduino_Core_STM32)

## Why start this since there is STM32 support of [rogerclarkmelbourne/Arduino_STM32](https://github.com/rogerclarkmelbourne/Arduino_STM32)
- It lacks low power support, which is important for battery powered nodes.
- Rich features of STM32 are limited, such as STM32 has more interrupt than AVR.

## Task status
- [x] STM32F1 basic support
- [ ] Low Power support
- [ ] Extra Interrupt support

## Development Memo
Tested on:
- Hardware: STM32F103C8T6 Bluepill 128KB
- configuration

<img src="./Documentation/img/configuration-stm32duino-stm32f1c8t6.png" alt="configuration of stm32f1c8t6 bluepill" width="500"/>

- STM32 Cores by STMicroelectronics Version 1.9.0

# Thanks
- [#Arduino_Core_STM32](https://github.com/stm32duino/Arduino_Core_STM32) for the rich support of STM32 series.
- [Arduino_STM32](https://github.com/rogerclarkmelbourne/Arduino_STM32) for the first STM32 support on STM32.
- [NodeManager](https://github.com/mysensors/NodeManager) for the fast development on mysensors gateways/nodes.
- [MySensors](https://github.com/mysensors/MySensors) for building the base.
4 changes: 3 additions & 1 deletion core/MyCapabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@
#elif defined(ARDUINO_ARCH_AVR)
#define MY_CAP_ARCH "A"
#elif defined(ARDUINO_ARCH_STM32F1)
#define MY_CAP_ARCH "M"
#define MY_CAP_ARCH "F"
#elif defined(ARDUINO_ARCH_STM32)
#define MY_CAP_ARCH "f"
#elif defined(__arm__) && defined(TEENSYDUINO)
#define MY_CAP_ARCH "T"
#elif defined(__linux__)
Expand Down
2 changes: 2 additions & 0 deletions examples/SecurityPersonalizer/SecurityPersonalizer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,8 @@ static void probe_and_print_peripherals(void)
Serial.print(F("| SAMD | DETECTED | N/A | "));
#elif defined(ARDUINO_ARCH_STM32F1)
Serial.print(F("| STM32F1 | DETECTED | N/A | "));
#elif defined(ARDUINO_ARCH_STM32)
Serial.print(F("| STM32 | DETECTED | N/A | "));
#elif defined(__linux__)
Serial.print(F("| Linux | DETECTED | N/A | "));
#else
Expand Down
184 changes: 184 additions & 0 deletions hal/architecture/STM32/MyHwSTM32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* 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 <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2019 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"
#include "drivers/hal_conf_custom.h"
#include "drivers/stm32_ADC_internal_channels.h"

/*
* Pinout STM32F103C8 dev board:
* http://wiki.stm32duino.com/images/a/ae/Bluepillpinout.gif
*
* Wiring RFM69 radio / SPI1
* --------------------------------------------------
* CLK PA5
* MISO PA6
* MOSI PA7
* CSN PA4
* CE NA
* IRQ PA3 (default)
*
* Wiring RF24 radio / SPI1
* --------------------------------------------------
* CLK PA5
* MISO PA6
* MOSI PA7
* CSN PA4
* CE PB0 (default)
* IRQ 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;
// return EEPROM.begin();

// 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<uint8_t *>(buf);
int pos = reinterpret_cast<int>(addr);
eeprom_buffer_fill();
// return eeprom_buffered_read_byte(pos);

// while (length-- > 0) {
// *dst++ = EEPROM.read(pos++);
// }
while (length-- > 0) {
*dst++ = eeprom_buffered_read_byte(pos++);
}
}

void hwWriteConfigBlock(void *buf, void *addr, size_t length) {
uint8_t *src = static_cast<uint8_t *>(buf);
int pos = reinterpret_cast<int>(addr);
// while (length-- > 0) {
// EEPROM.write(pos++, *src++);
// }
while (length-- > 0) {
// EEPROM.write(pos++, *src++);
eeprom_buffered_write_byte(pos++, *src++);
}
eeprom_buffer_flush();
}

// void hwReadConfigBlock(void *buf, void *addr, size_t length) {
// uint8_t *dst = static_cast<uint8_t *>(buf);
// int pos = reinterpret_cast<int>(addr);
// while (length-- > 0) {
// *dst++ = EEPROM.read(pos++);
// }
// }

// void hwWriteConfigBlock(void *buf, void *addr, size_t length) {
// uint8_t *src = static_cast<uint8_t *>(buf);
// int pos = reinterpret_cast<int>(addr);
// while (length-- > 0) {
// EEPROM.write(pos++, *src++);
// }
// }

// uint8_t hwReadConfig(const int addr) {
// uint8_t value;
// hwReadConfigBlock(&value, reinterpret_cast<void *>(addr), 1);
// return value;
// }

// void hwWriteConfig(const int addr, uint8_t value) {
// hwWriteConfigBlock(&value, reinterpret_cast<void *>(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) {
// use internal temperature sensor as noise source
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 = analogRead(ATEMP);
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); // FlashID + ChipID
return true;
}

uint16_t hwCPUVoltage(void) { return (uint16_t)readVref(); }

uint16_t hwCPUFrequency(void) { return F_CPU / 100000UL; }

int8_t hwCPUTemperature(void) { return (int8_t)readTempSensor(readVref()); }

uint16_t hwFreeMem(void) {
// Not yet implemented
return FUNCTION_NOT_SUPPORTED;
}
98 changes: 98 additions & 0 deletions hal/architecture/STM32/MyHwSTM32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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 <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2019 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 <libmaple/iwdg.h>
#include <IWatchdog.h>
// #include <stm32yyxx_ll_iwdg.h>
#include <itoa.h>
#include <EEPROM.h>
#include <SPI.h>

#ifdef __cplusplus
#include <Arduino.h>
#endif

#define CRYPTO_LITTLE_ENDIAN

#ifndef MY_SERIALDEVICE
#define MY_SERIALDEVICE Serial
#endif

#ifndef MY_DEBUGDEVICE
#define MY_DEBUGDEVICE MY_SERIALDEVICE
#endif

#ifndef MY_STM32F1_TEMPERATURE_OFFSET
#define MY_STM32F1_TEMPERATURE_OFFSET (0.0f)
#endif

#ifndef MY_STM32F1_TEMPERATURE_GAIN
#define MY_STM32F1_TEMPERATURE_GAIN (1.0f)
#endif

// SS default
#ifndef SS
#define SS PA4
#endif

// mapping
#define snprintf_P snprintf
#define vsnprintf_P vsnprintf
#define strncpy_P strncpy
#define printf_P printf
#define yield() // not defined

#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() IWatchdog.reload() // iwdg_feed()
// #define hwReboot() nvic_sys_reset()
#define hwReboot() HAL_NVIC_SystemReset()
#define hwMillis() millis()
#define hwGetSleepRemaining() (0ul)

extern void serialEventRun(void) __attribute__((weak));
bool hwInit(void);
void hwRandomNumberInit(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);
#define hwReadConfig(__pos) eeprom_read_byte((const uint32_t)__pos)
#define hwWriteConfig(__pos, __val) eeprom_write_byte((const uint32_t)__pos, (uint8_t)__val)

// 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
Loading

0 comments on commit 4f7a96b

Please sign in to comment.