From beae0c1dd0564ce63a94ddc725a2785aba0e350e Mon Sep 17 00:00:00 2001 From: KenjutsuGH Date: Sat, 21 Jul 2018 11:58:11 +0200 Subject: [PATCH] Added support for STM32duino --- src/DS3232RTC.cpp | 17 ++++++++++++++--- src/DS3232RTC.h | 5 ++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/DS3232RTC.cpp b/src/DS3232RTC.cpp index 7b40445..9daee90 100644 --- a/src/DS3232RTC.cpp +++ b/src/DS3232RTC.cpp @@ -30,7 +30,11 @@ #elif ARDUINO >= 100 #include #define i2cBegin Wire.begin -#define i2cBeginTransmission Wire.beginTransmission + #ifdef __STM32F1__ + #define i2cBeginTransmission if (_do_init) { _do_init = false; Wire.begin(); } Wire.beginTransmission + #else + #define i2cBeginTransmission Wire.beginTransmission + #endif #define i2cEndTransmission Wire.endTransmission #define i2cRequestFrom Wire.requestFrom #define i2cRead Wire.read @@ -116,7 +120,10 @@ byte DS3232RTC::errCode; // for debug // the begin() function in the setup code. DS3232RTC::DS3232RTC(bool initI2C) { - if (initI2C) i2cBegin(); + // At the moment, Arduino for STM32 (STM32duino) doesn't support GPIO or SPI etc operations inside constructors. + #ifndef __STM32F1__ + if (initI2C) i2cBegin(); + #endif } // Initialize the I2C bus. @@ -370,6 +377,10 @@ uint8_t __attribute__ ((noinline)) DS3232RTC::bcd2dec(uint8_t n) return n - 6 * (n >> 4); } -#ifdef ARDUINO_ARCH_AVR +#if defined(ARDUINO_ARCH_AVR) || defined(__STM32F1__) DS3232RTC RTC; // instantiate an RTC object #endif + +#ifdef __STM32F1__ + bool DS3232RTC::_do_init = true; +#endif diff --git a/src/DS3232RTC.h b/src/DS3232RTC.h index bd35edf..d1f3d65 100644 --- a/src/DS3232RTC.h +++ b/src/DS3232RTC.h @@ -76,9 +76,12 @@ class DS3232RTC private: uint8_t dec2bcd(uint8_t n); static uint8_t bcd2dec(uint8_t n); + #ifdef __STM32F1__ + static bool _do_init; + #endif }; -#ifdef ARDUINO_ARCH_AVR +#if defined(ARDUINO_ARCH_AVR) || defined(__STM32F1__) extern DS3232RTC RTC; #endif