From 21ba1201b2541a697dc98f0bb78feb0b3b8f9885 Mon Sep 17 00:00:00 2001 From: Raphael Lehmann Date: Fri, 25 Feb 2022 01:17:27 +0100 Subject: [PATCH] [example] RTC MCP7941x on Raspberry Pico --- examples/rp_pico/rtc_mcp7941x/main.cpp | 147 ++++++++++++++++++++++ examples/rp_pico/rtc_mcp7941x/project.xml | 17 +++ 2 files changed, 164 insertions(+) create mode 100644 examples/rp_pico/rtc_mcp7941x/main.cpp create mode 100644 examples/rp_pico/rtc_mcp7941x/project.xml diff --git a/examples/rp_pico/rtc_mcp7941x/main.cpp b/examples/rp_pico/rtc_mcp7941x/main.cpp new file mode 100644 index 0000000000..5a2a280f48 --- /dev/null +++ b/examples/rp_pico/rtc_mcp7941x/main.cpp @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2022, Raphael Lehmann + * + * This file is part of the modm project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +// ---------------------------------------------------------------------------- + +#include +#include +#include +#include +#include +#include + + +// Set the log level +#undef MODM_LOG_LEVEL +#define MODM_LOG_LEVEL modm::log::INFO + +// Create an IODeviceWrapper around the Uart Peripheral we want to use +modm::IODeviceWrapper loggerDevice; + +// Set all four logger streams to use the UART +modm::log::Logger modm::log::debug(loggerDevice); +modm::log::Logger modm::log::info(loggerDevice); +modm::log::Logger modm::log::warning(loggerDevice); +modm::log::Logger modm::log::error(loggerDevice); + + +using MyI2cMaster = modm::platform::I2cMaster0; +using I2cScl = modm::platform::Gpio1; +using I2cSda = modm::platform::Gpio0; + + +class RtcThread : public modm::pt::Protothread +{ +public: + bool + update() + { + PT_BEGIN(); + + if(PT_CALL(rtc.oscillatorRunning())) { + MODM_LOG_ERROR << "RTC oscillator is running." << modm::endl; + } + else { + MODM_LOG_ERROR << "RTC oscillator is NOT running." << modm::endl; + } + + MODM_LOG_INFO << "Setting date/time to 01.01.2020 00:00.00h" << modm::endl; + dateTime.days = 1; + dateTime.months = 1; + dateTime.years = 20; + dateTime.hours = 0; + dateTime.minutes = 0; + dateTime.seconds = 0; + while(not PT_CALL(rtc.setDateTime(dateTime))) { + MODM_LOG_ERROR << "Unable to set date/time." << modm::endl; + timeout.restart(500ms); + PT_WAIT_UNTIL(timeout.isExpired()); + } + + timeout.restart(500ms); + PT_WAIT_UNTIL(timeout.isExpired()); + + if(PT_CALL(rtc.oscillatorRunning())) { + MODM_LOG_ERROR << "RTC oscillator is running." << modm::endl; + } + else { + MODM_LOG_ERROR << "RTC oscillator is NOT running." << modm::endl; + } + + while (true) + { + dateTime2 = PT_CALL(rtc.getDateTime()); + if(dateTime2.has_value()) { + MODM_LOG_INFO.printf("%02u.%02u.%02u ", dateTime2->days, dateTime2->months, dateTime2->years); + MODM_LOG_INFO.printf("%02u:%02u.%02uh\n", dateTime2->hours, dateTime2->minutes, dateTime2->seconds); + } + else { + MODM_LOG_ERROR << "Unable to read from RTC." << modm::endl; + } + + timeout.restart(2500ms); + PT_WAIT_UNTIL(timeout.isExpired()); + } + + PT_END(); + } + +private: + modm::Mcp7941x rtc{}; + modm::mcp7941x::DateTime dateTime{}; + std::optional dateTime2{}; + modm::ShortTimeout timeout; +}; + + +using namespace Board; + +int +main() +{ + Board::initialize(); + + // initialize Uart0 for MODM_LOG_* + Uart0::connect(); + Uart0::initialize(); + + Leds::setOutput(); + + MyI2cMaster::connect(); + MyI2cMaster::initialize(); + + MODM_LOG_INFO << "RTC MCP7941x Example on Raspberry Pico" << modm::endl; + + modm::Mcp7941xEeprom eeprom{}; + if (auto data = RF_CALL_BLOCKING(eeprom.getUniqueId())) { + MODM_LOG_INFO << "Unique ID (EUI-48/64): "; + MODM_LOG_INFO << modm::hex << (*data)[0] << modm::ascii << ":"; + MODM_LOG_INFO << modm::hex << (*data)[1] << modm::ascii << ":"; + MODM_LOG_INFO << modm::hex << (*data)[2] << modm::ascii << ":"; + MODM_LOG_INFO << modm::hex << (*data)[3] << modm::ascii << ":"; + MODM_LOG_INFO << modm::hex << (*data)[4] << modm::ascii << ":"; + MODM_LOG_INFO << modm::hex << (*data)[5] << modm::ascii << ":"; + MODM_LOG_INFO << modm::hex << (*data)[6] << modm::ascii << ":"; + MODM_LOG_INFO << modm::hex << (*data)[7] << modm::ascii << modm::endl; + } + else { + MODM_LOG_ERROR << "Unable to read unique ID from RTC." << modm::endl; + } + modm::delay(500ms); + + RtcThread rtcThread; + + while (true) + { + LedGreen::toggle(); + rtcThread.update(); + } + + return 0; +} diff --git a/examples/rp_pico/rtc_mcp7941x/project.xml b/examples/rp_pico/rtc_mcp7941x/project.xml new file mode 100644 index 0000000000..b24d0343e3 --- /dev/null +++ b/examples/rp_pico/rtc_mcp7941x/project.xml @@ -0,0 +1,17 @@ + + modm:rp-pico + + + + + + modm:debug + modm:driver:mcp7941x + modm:io + modm:platform:i2c:0 + modm:platform:uart:0 + modm:processing:protothread + modm:processing:timer + modm:build:scons + +