From 790beccb80d1868dd0a647b8b22ea18e788e5c2c Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Sat, 10 Sep 2016 18:18:47 +0300 Subject: [PATCH] Add examples for Arduino Framework --- examples/arduino-blink/.gitignore | 3 + examples/arduino-blink/.travis.yml | 65 +++++++ examples/arduino-blink/README.rst | 38 ++++ examples/arduino-blink/lib/readme.txt | 38 ++++ examples/arduino-blink/platformio.ini | 18 ++ examples/arduino-blink/src/Blink.cpp | 25 +++ examples/arduino-external-libs/.gitignore | 4 + examples/arduino-external-libs/.travis.yml | 65 +++++++ examples/arduino-external-libs/README.rst | 38 ++++ examples/arduino-external-libs/lib/readme.txt | 38 ++++ examples/arduino-external-libs/platformio.ini | 20 +++ .../src/BMP180_altitude_example.ino | 169 ++++++++++++++++++ examples/arduino-internal-libs/.gitignore | 3 + examples/arduino-internal-libs/.travis.yml | 65 +++++++ examples/arduino-internal-libs/README.rst | 38 ++++ examples/arduino-internal-libs/lib/readme.txt | 38 ++++ examples/arduino-internal-libs/platformio.ini | 18 ++ .../src/Gilchrist_RTC.ino | 93 ++++++++++ 18 files changed, 776 insertions(+) create mode 100644 examples/arduino-blink/.gitignore create mode 100644 examples/arduino-blink/.travis.yml create mode 100644 examples/arduino-blink/README.rst create mode 100644 examples/arduino-blink/lib/readme.txt create mode 100644 examples/arduino-blink/platformio.ini create mode 100644 examples/arduino-blink/src/Blink.cpp create mode 100644 examples/arduino-external-libs/.gitignore create mode 100644 examples/arduino-external-libs/.travis.yml create mode 100644 examples/arduino-external-libs/README.rst create mode 100644 examples/arduino-external-libs/lib/readme.txt create mode 100644 examples/arduino-external-libs/platformio.ini create mode 100644 examples/arduino-external-libs/src/BMP180_altitude_example.ino create mode 100644 examples/arduino-internal-libs/.gitignore create mode 100644 examples/arduino-internal-libs/.travis.yml create mode 100644 examples/arduino-internal-libs/README.rst create mode 100644 examples/arduino-internal-libs/lib/readme.txt create mode 100644 examples/arduino-internal-libs/platformio.ini create mode 100644 examples/arduino-internal-libs/src/Gilchrist_RTC.ino diff --git a/examples/arduino-blink/.gitignore b/examples/arduino-blink/.gitignore new file mode 100644 index 0000000..5402c18 --- /dev/null +++ b/examples/arduino-blink/.gitignore @@ -0,0 +1,3 @@ +.pioenvs +.clang_complete +.gcc-flags.json diff --git a/examples/arduino-blink/.travis.yml b/examples/arduino-blink/.travis.yml new file mode 100644 index 0000000..b636184 --- /dev/null +++ b/examples/arduino-blink/.travis.yml @@ -0,0 +1,65 @@ +# Continuous Integration (CI) is the practice, in software +# engineering, of merging all developer working copies with a shared mainline +# several times a day < http://docs.platformio.org/en/latest/ci/index.html > +# +# Documentation: +# +# * Travis CI Embedded Builds with PlatformIO +# < https://docs.travis-ci.com/user/integration/platformio/ > +# +# * PlatformIO integration with Travis CI +# < http://docs.platformio.org/en/latest/ci/travis.html > +# +# * User Guide for `platformio ci` command +# < http://docs.platformio.org/en/latest/userguide/cmd_ci.html > +# +# +# Please choose one of the following templates (proposed below) and uncomment +# it (remove "# " before each line) or use own configuration according to the +# Travis CI documentation (see above). +# + + +# +# Template #1: General project. Test it using existing `platformio.ini`. +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# install: +# - pip install -U platformio +# +# script: +# - platformio run + + +# +# Template #2: The project is intended to by used as a library with examples +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# env: +# - PLATFORMIO_CI_SRC=path/to/test/file.c +# - PLATFORMIO_CI_SRC=examples/file.ino +# - PLATFORMIO_CI_SRC=path/to/test/directory +# +# install: +# - pip install -U platformio +# +# script: +# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N diff --git a/examples/arduino-blink/README.rst b/examples/arduino-blink/README.rst new file mode 100644 index 0000000..b445869 --- /dev/null +++ b/examples/arduino-blink/README.rst @@ -0,0 +1,38 @@ +.. Copyright 2014-present PlatformIO + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +How to build PlatformIO based project +===================================== + +1. `Install PlatformIO `_ +2. Download `development platform with examples `_ +3. Extract ZIP archive +4. Run these commands: + +.. code-block:: bash + + # Change directory to example + > cd platform-ststm32/examples/arduino-blink + + # Build project + > platformio run + + # Upload firmware + > platformio run --target upload + + # Build specific environment + > platformio run -e maple + + # Upload firmware for the specific environment + > platformio run -e maple --target upload + + # Clean build files + > platformio run --target clean diff --git a/examples/arduino-blink/lib/readme.txt b/examples/arduino-blink/lib/readme.txt new file mode 100644 index 0000000..607a92a --- /dev/null +++ b/examples/arduino-blink/lib/readme.txt @@ -0,0 +1,38 @@ + +This directory is intended for the project specific (private) libraries. +PlatformIO will compile them to static libraries and link to executable file. + +The source code of each library should be placed in separate directory, like +"lib/private_lib/[here are source files]". + +For example, see how can be organized `Foo` and `Bar` libraries: + +|--lib +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| |--Foo +| | |- Foo.c +| | |- Foo.h +| |- readme.txt --> THIS FILE +|- platformio.ini +|--src + |- main.c + +Then in `src/main.c` you should use: + +#include +#include + +// rest H/C/CPP code + +PlatformIO will find your libraries automatically, configure preprocessor's +include paths and build them. + +See additional options for PlatformIO Library Dependency Finder `lib_*`: + +http://docs.platformio.org/en/latest/projectconf.html#lib-install + diff --git a/examples/arduino-blink/platformio.ini b/examples/arduino-blink/platformio.ini new file mode 100644 index 0000000..d36e0c2 --- /dev/null +++ b/examples/arduino-blink/platformio.ini @@ -0,0 +1,18 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter, extra scripting +; Upload options: custom port, speed and extra flags +; Library options: dependencies, extra library storages +; +; Please visit documentation for the other options and examples +; http://docs.platformio.org/en/stable/projectconf.html + +[env:maple] +platform = ststm32 +framework = arduino +board = maple + +[env:genericSTM32F103RB] +platform = ststm32 +framework = arduino +board = genericSTM32F103RB diff --git a/examples/arduino-blink/src/Blink.cpp b/examples/arduino-blink/src/Blink.cpp new file mode 100644 index 0000000..c9f0e6a --- /dev/null +++ b/examples/arduino-blink/src/Blink.cpp @@ -0,0 +1,25 @@ +/* + * Blink + * Turns on an LED on for one second, + * then off for one second, repeatedly. + */ + +#include + +void setup() +{ + // initialize LED digital pin as an output. + pinMode(PC13, OUTPUT); +} + +void loop() +{ + // turn the LED on (HIGH is the voltage level) + digitalWrite(PC13, HIGH); + // wait for a second + delay(1000); + // turn the LED off by making the voltage LOW + digitalWrite(PC13, LOW); + // wait for a second + delay(1000); +} diff --git a/examples/arduino-external-libs/.gitignore b/examples/arduino-external-libs/.gitignore new file mode 100644 index 0000000..5dac9f5 --- /dev/null +++ b/examples/arduino-external-libs/.gitignore @@ -0,0 +1,4 @@ +.pioenvs +.piolibdeps +.clang_complete +.gcc-flags.json diff --git a/examples/arduino-external-libs/.travis.yml b/examples/arduino-external-libs/.travis.yml new file mode 100644 index 0000000..b636184 --- /dev/null +++ b/examples/arduino-external-libs/.travis.yml @@ -0,0 +1,65 @@ +# Continuous Integration (CI) is the practice, in software +# engineering, of merging all developer working copies with a shared mainline +# several times a day < http://docs.platformio.org/en/latest/ci/index.html > +# +# Documentation: +# +# * Travis CI Embedded Builds with PlatformIO +# < https://docs.travis-ci.com/user/integration/platformio/ > +# +# * PlatformIO integration with Travis CI +# < http://docs.platformio.org/en/latest/ci/travis.html > +# +# * User Guide for `platformio ci` command +# < http://docs.platformio.org/en/latest/userguide/cmd_ci.html > +# +# +# Please choose one of the following templates (proposed below) and uncomment +# it (remove "# " before each line) or use own configuration according to the +# Travis CI documentation (see above). +# + + +# +# Template #1: General project. Test it using existing `platformio.ini`. +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# install: +# - pip install -U platformio +# +# script: +# - platformio run + + +# +# Template #2: The project is intended to by used as a library with examples +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# env: +# - PLATFORMIO_CI_SRC=path/to/test/file.c +# - PLATFORMIO_CI_SRC=examples/file.ino +# - PLATFORMIO_CI_SRC=path/to/test/directory +# +# install: +# - pip install -U platformio +# +# script: +# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N diff --git a/examples/arduino-external-libs/README.rst b/examples/arduino-external-libs/README.rst new file mode 100644 index 0000000..199cbd4 --- /dev/null +++ b/examples/arduino-external-libs/README.rst @@ -0,0 +1,38 @@ +.. Copyright 2014-present PlatformIO + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +How to build PlatformIO based project +===================================== + +1. `Install PlatformIO `_ +2. Download `development platform with examples `_ +3. Extract ZIP archive +4. Run these commands: + +.. code-block:: bash + + # Change directory to example + > cd platform-ststm32/examples/arduino-external-libs + + # Build project + > platformio run + + # Upload firmware + > platformio run --target upload + + # Build specific environment + > platformio run -e maple + + # Upload firmware for the specific environment + > platformio run -e maple --target upload + + # Clean build files + > platformio run --target clean diff --git a/examples/arduino-external-libs/lib/readme.txt b/examples/arduino-external-libs/lib/readme.txt new file mode 100644 index 0000000..607a92a --- /dev/null +++ b/examples/arduino-external-libs/lib/readme.txt @@ -0,0 +1,38 @@ + +This directory is intended for the project specific (private) libraries. +PlatformIO will compile them to static libraries and link to executable file. + +The source code of each library should be placed in separate directory, like +"lib/private_lib/[here are source files]". + +For example, see how can be organized `Foo` and `Bar` libraries: + +|--lib +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| |--Foo +| | |- Foo.c +| | |- Foo.h +| |- readme.txt --> THIS FILE +|- platformio.ini +|--src + |- main.c + +Then in `src/main.c` you should use: + +#include +#include + +// rest H/C/CPP code + +PlatformIO will find your libraries automatically, configure preprocessor's +include paths and build them. + +See additional options for PlatformIO Library Dependency Finder `lib_*`: + +http://docs.platformio.org/en/latest/projectconf.html#lib-install + diff --git a/examples/arduino-external-libs/platformio.ini b/examples/arduino-external-libs/platformio.ini new file mode 100644 index 0000000..3427c11 --- /dev/null +++ b/examples/arduino-external-libs/platformio.ini @@ -0,0 +1,20 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter, extra scripting +; Upload options: custom port, speed and extra flags +; Library options: dependencies, extra library storages +; +; Please visit documentation for the other options and examples +; http://docs.platformio.org/en/stable/projectconf.html + +[env:maple] +platform = ststm32 +framework = arduino +board = maple +lib_deps = BMP180 + +[env:genericSTM32F103RB] +platform = ststm32 +framework = arduino +board = genericSTM32F103RB +lib_deps = BMP180 diff --git a/examples/arduino-external-libs/src/BMP180_altitude_example.ino b/examples/arduino-external-libs/src/BMP180_altitude_example.ino new file mode 100644 index 0000000..c06ce1b --- /dev/null +++ b/examples/arduino-external-libs/src/BMP180_altitude_example.ino @@ -0,0 +1,169 @@ +/* SFE_BMP180 altitude example sketch + +This sketch shows how to use the Bosch BMP180 pressure sensor +as an altimiter. +https://www.sparkfun.com/products/11824 + +Like most pressure sensors, the BMP180 measures absolute pressure. +Since absolute pressure varies with altitude, you can use the pressure +to determine your altitude. + +Because pressure also varies with weather, you must first take a pressure +reading at a known baseline altitude. Then you can measure variations +from that pressure + +Hardware connections: + +- (GND) to GND ++ (VDD) to 3.3V + +(WARNING: do not connect + to 5V or the sensor will be damaged!) + +You will also need to connect the I2C pins (SCL and SDA) to your +Arduino. The pins are different on different Arduinos: + +Any Arduino pins labeled: SDA SCL +Uno, Redboard, Pro: A4 A5 +Mega2560, Due: 20 21 +Leonardo: 2 3 + +Leave the IO (VDDIO) pin unconnected. This pin is for connecting +the BMP180 to systems with lower logic levels such as 1.8V + +Have fun! -Your friends at SparkFun. + +The SFE_BMP180 library uses floating-point equations developed by the +Weather Station Data Logger project: http://wmrx00.sourceforge.net/ + +Our example code uses the "beerware" license. You can do anything +you like with this code. No really, anything. If you find it useful, +buy me a beer someday. + +V10 Mike Grusin, SparkFun Electronics 10/24/2013 +*/ + +// Your sketch must #include this library, and the Wire library. +// (Wire is a standard library included with Arduino.): + +#include +#include +#include + +// You will need to create an SFE_BMP180 object, here called "pressure": + +SFE_BMP180 pressure; + +double baseline; // baseline pressure + +void setup() +{ + Serial.begin(9600); + Serial.println("REBOOT"); + + // Initialize the sensor (it is important to get calibration values stored on the device). + + if (pressure.begin()) + Serial.println("BMP180 init success"); + else + { + // Oops, something went wrong, this is usually a connection problem, + // see the comments at the top of this sketch for the proper connections. + + Serial.println("BMP180 init fail (disconnected?)\n\n"); + while(1); // Pause forever. + } + + // Get the baseline pressure: + + baseline = getPressure(); + + Serial.print("baseline pressure: "); + Serial.print(baseline); + Serial.println(" mb"); +} + +void loop() +{ + double a,P; + + // Get a new pressure reading: + + P = getPressure(); + + // Show the relative altitude difference between + // the new reading and the baseline reading: + + a = pressure.altitude(P,baseline); + + Serial.print("relative altitude: "); + if (a >= 0.0) Serial.print(" "); // add a space for positive numbers + Serial.print(a,1); + Serial.print(" meters, "); + if (a >= 0.0) Serial.print(" "); // add a space for positive numbers + Serial.print(a*3.28084,0); + Serial.println(" feet"); + + delay(500); +} + + +double getPressure() +{ + char status; + double T,P,p0,a; + + // You must first get a temperature measurement to perform a pressure reading. + + // Start a temperature measurement: + // If request is successful, the number of ms to wait is returned. + // If request is unsuccessful, 0 is returned. + + status = pressure.startTemperature(); + if (status != 0) + { + // Wait for the measurement to complete: + + delay(status); + + // Retrieve the completed temperature measurement: + // Note that the measurement is stored in the variable T. + // Use '&T' to provide the address of T to the function. + // Function returns 1 if successful, 0 if failure. + + status = pressure.getTemperature(T); + if (status != 0) + { + // Start a pressure measurement: + // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait). + // If request is successful, the number of ms to wait is returned. + // If request is unsuccessful, 0 is returned. + + status = pressure.startPressure(3); + if (status != 0) + { + // Wait for the measurement to complete: + delay(status); + + // Retrieve the completed pressure measurement: + // Note that the measurement is stored in the variable P. + // Use '&P' to provide the address of P. + // Note also that the function requires the previous temperature measurement (T). + // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.) + // Function returns 1 if successful, 0 if failure. + + status = pressure.getPressure(P,T); + if (status != 0) + { + return(P); + } + else Serial.println("error retrieving pressure measurement\n"); + } + else Serial.println("error starting pressure measurement\n"); + } + else Serial.println("error retrieving temperature measurement\n"); + } + else Serial.println("error starting temperature measurement\n"); +} + + + diff --git a/examples/arduino-internal-libs/.gitignore b/examples/arduino-internal-libs/.gitignore new file mode 100644 index 0000000..5402c18 --- /dev/null +++ b/examples/arduino-internal-libs/.gitignore @@ -0,0 +1,3 @@ +.pioenvs +.clang_complete +.gcc-flags.json diff --git a/examples/arduino-internal-libs/.travis.yml b/examples/arduino-internal-libs/.travis.yml new file mode 100644 index 0000000..b636184 --- /dev/null +++ b/examples/arduino-internal-libs/.travis.yml @@ -0,0 +1,65 @@ +# Continuous Integration (CI) is the practice, in software +# engineering, of merging all developer working copies with a shared mainline +# several times a day < http://docs.platformio.org/en/latest/ci/index.html > +# +# Documentation: +# +# * Travis CI Embedded Builds with PlatformIO +# < https://docs.travis-ci.com/user/integration/platformio/ > +# +# * PlatformIO integration with Travis CI +# < http://docs.platformio.org/en/latest/ci/travis.html > +# +# * User Guide for `platformio ci` command +# < http://docs.platformio.org/en/latest/userguide/cmd_ci.html > +# +# +# Please choose one of the following templates (proposed below) and uncomment +# it (remove "# " before each line) or use own configuration according to the +# Travis CI documentation (see above). +# + + +# +# Template #1: General project. Test it using existing `platformio.ini`. +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# install: +# - pip install -U platformio +# +# script: +# - platformio run + + +# +# Template #2: The project is intended to by used as a library with examples +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# env: +# - PLATFORMIO_CI_SRC=path/to/test/file.c +# - PLATFORMIO_CI_SRC=examples/file.ino +# - PLATFORMIO_CI_SRC=path/to/test/directory +# +# install: +# - pip install -U platformio +# +# script: +# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N diff --git a/examples/arduino-internal-libs/README.rst b/examples/arduino-internal-libs/README.rst new file mode 100644 index 0000000..0fac78d --- /dev/null +++ b/examples/arduino-internal-libs/README.rst @@ -0,0 +1,38 @@ +.. Copyright 2014-present PlatformIO + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +How to build PlatformIO based project +===================================== + +1. `Install PlatformIO `_ +2. Download `development platform with examples `_ +3. Extract ZIP archive +4. Run these commands: + +.. code-block:: bash + + # Change directory to example + > cd platform-ststm32/examples/arduino-internal-libs + + # Build project + > platformio run + + # Upload firmware + > platformio run --target upload + + # Build specific environment + > platformio run -e maple + + # Upload firmware for the specific environment + > platformio run -e maple --target upload + + # Clean build files + > platformio run --target clean diff --git a/examples/arduino-internal-libs/lib/readme.txt b/examples/arduino-internal-libs/lib/readme.txt new file mode 100644 index 0000000..607a92a --- /dev/null +++ b/examples/arduino-internal-libs/lib/readme.txt @@ -0,0 +1,38 @@ + +This directory is intended for the project specific (private) libraries. +PlatformIO will compile them to static libraries and link to executable file. + +The source code of each library should be placed in separate directory, like +"lib/private_lib/[here are source files]". + +For example, see how can be organized `Foo` and `Bar` libraries: + +|--lib +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| |--Foo +| | |- Foo.c +| | |- Foo.h +| |- readme.txt --> THIS FILE +|- platformio.ini +|--src + |- main.c + +Then in `src/main.c` you should use: + +#include +#include + +// rest H/C/CPP code + +PlatformIO will find your libraries automatically, configure preprocessor's +include paths and build them. + +See additional options for PlatformIO Library Dependency Finder `lib_*`: + +http://docs.platformio.org/en/latest/projectconf.html#lib-install + diff --git a/examples/arduino-internal-libs/platformio.ini b/examples/arduino-internal-libs/platformio.ini new file mode 100644 index 0000000..d36e0c2 --- /dev/null +++ b/examples/arduino-internal-libs/platformio.ini @@ -0,0 +1,18 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter, extra scripting +; Upload options: custom port, speed and extra flags +; Library options: dependencies, extra library storages +; +; Please visit documentation for the other options and examples +; http://docs.platformio.org/en/stable/projectconf.html + +[env:maple] +platform = ststm32 +framework = arduino +board = maple + +[env:genericSTM32F103RB] +platform = ststm32 +framework = arduino +board = genericSTM32F103RB diff --git a/examples/arduino-internal-libs/src/Gilchrist_RTC.ino b/examples/arduino-internal-libs/src/Gilchrist_RTC.ino new file mode 100644 index 0000000..a131127 --- /dev/null +++ b/examples/arduino-internal-libs/src/Gilchrist_RTC.ino @@ -0,0 +1,93 @@ +/* + * test-rtc.c + * + * Example program that sets up the Real Time Clock and then blinks the + * LED in patterns for seconds and alarm interrupts. + * + * Created by Rod Gilchrist on 11-12-24. + Ray Burnette: 20150521: + Edited: \Documents\Arduino\hardware\STM32\STM32F1\system\libmaple\stm32f1\include\series\rcc.h to include: + #include + #define RCC_BDCR_RTCSEL_LSI (0x2 << 8) + static inline void rcc_start_lsi(void) + static inline void rcc_start_lse(void) + static inline void rcc_start_hse(void) + + Arduino GUI 1.7.3 from Arduino.org + Sketch uses 20,268 bytes (18%) of program storage space. Maximum is 110,592 bytes. + Global variables use 4,552 bytes of dynamic memory. + */ + +#include "RTClock.h" + +#define BOARD_LED_PIN PB1 + +int globAlmCnt = 0; +int globOvCnt = 0; +int globSecCnt = 0; +int specAlmCnt = 0; +int lastGlobAlmCnt = -1; +int lastSpecAlmCnt = -1; + +void rtc_sec_intr() { if (rtc_is_second()) globSecCnt++; } +void rtc_ovf_intr() { if (rtc_is_overflow()) globOvCnt++; } +void rtc_glob_alm_intr() { if (rtc_is_alarm()) globAlmCnt++; } +void rtc_spec_alm_intr() { if (rtc_is_alarm()) specAlmCnt++; } + +void setup() { + // http://forums.leaflabs.com/topic.php?id=1437 + // slow! div speed. NOTE! 512 is stop/hang when USB not connected! + // rcc_set_prescaler(RCC_PRESCALER_AHB, RCC_AHB_SYSCLK_DIV_256); + // Normal speed: + // rcc_set_prescaler(RCC_PRESCALER_AHB, RCC_AHB_SYSCLK_DIV_1); + + pinMode(BOARD_LED_PIN, OUTPUT); + delay(5000); + Serial.println("begin RTC blink"); + delay(1000); + + rtc_init(RTCSEL_LSI); + rtc_set_prescaler_load(0x7fff); + rtc_set_count(0); + + rtc_attach_interrupt(RTC_SECONDS_INTERRUPT, rtc_sec_intr); + rtc_attach_interrupt(RTC_OVERFLOW_INTERRUPT, rtc_ovf_intr); // expected every 128000 seconds + rtc_attach_interrupt(RTC_ALARM_GLOBAL_INTERRUPT, rtc_glob_alm_intr); + rtc_attach_interrupt(RTC_ALARM_SPECIFIC_INTERRUPT, rtc_spec_alm_intr); +} + +void loop() { + int i,n; + + Serial.print("Time + interrupt counts: "); + Serial.print(rtc_get_count()); + Serial.print("."); + Serial.print(rtc_get_divider()); + Serial.print(" ("); + Serial.print(globSecCnt); + Serial.print(", "); + Serial.print(globOvCnt); + Serial.print(", "); + Serial.print(globAlmCnt); + Serial.print(", "); + Serial.print(specAlmCnt); + Serial.println(")"); + + delay(1000); + + digitalWrite(BOARD_LED_PIN, 1); + if ((lastSpecAlmCnt != specAlmCnt) || (lastGlobAlmCnt != globAlmCnt)){ + lastGlobAlmCnt = globAlmCnt; + lastSpecAlmCnt = specAlmCnt; + + Serial.println(" -- alarm -- "); + for (i=0;i<3;i++) { digitalWrite(BOARD_LED_PIN, 0); delay(100); digitalWrite(BOARD_LED_PIN, 1); delay(100);} + n = rtc_get_count() + 5; + rtc_set_alarm(n); + } + + delay(1000); + digitalWrite(BOARD_LED_PIN, 0); +} + +