Skip to content

Commit 21ba120

Browse files
rlehandryblack
authored andcommitted
[example] RTC MCP7941x on Raspberry Pico
1 parent 23b4c06 commit 21ba120

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright (c) 2022, Raphael Lehmann
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <modm/board.hpp>
13+
#include <modm/debug/logger.hpp>
14+
#include <modm/driver/rtc/mcp7941x.hpp>
15+
#include <modm/processing/protothread.hpp>
16+
#include <modm/processing/timer.hpp>
17+
#include <optional>
18+
19+
20+
// Set the log level
21+
#undef MODM_LOG_LEVEL
22+
#define MODM_LOG_LEVEL modm::log::INFO
23+
24+
// Create an IODeviceWrapper around the Uart Peripheral we want to use
25+
modm::IODeviceWrapper<Uart0, modm::IOBuffer::BlockIfFull> loggerDevice;
26+
27+
// Set all four logger streams to use the UART
28+
modm::log::Logger modm::log::debug(loggerDevice);
29+
modm::log::Logger modm::log::info(loggerDevice);
30+
modm::log::Logger modm::log::warning(loggerDevice);
31+
modm::log::Logger modm::log::error(loggerDevice);
32+
33+
34+
using MyI2cMaster = modm::platform::I2cMaster0;
35+
using I2cScl = modm::platform::Gpio1;
36+
using I2cSda = modm::platform::Gpio0;
37+
38+
39+
class RtcThread : public modm::pt::Protothread
40+
{
41+
public:
42+
bool
43+
update()
44+
{
45+
PT_BEGIN();
46+
47+
if(PT_CALL(rtc.oscillatorRunning())) {
48+
MODM_LOG_ERROR << "RTC oscillator is running." << modm::endl;
49+
}
50+
else {
51+
MODM_LOG_ERROR << "RTC oscillator is NOT running." << modm::endl;
52+
}
53+
54+
MODM_LOG_INFO << "Setting date/time to 01.01.2020 00:00.00h" << modm::endl;
55+
dateTime.days = 1;
56+
dateTime.months = 1;
57+
dateTime.years = 20;
58+
dateTime.hours = 0;
59+
dateTime.minutes = 0;
60+
dateTime.seconds = 0;
61+
while(not PT_CALL(rtc.setDateTime(dateTime))) {
62+
MODM_LOG_ERROR << "Unable to set date/time." << modm::endl;
63+
timeout.restart(500ms);
64+
PT_WAIT_UNTIL(timeout.isExpired());
65+
}
66+
67+
timeout.restart(500ms);
68+
PT_WAIT_UNTIL(timeout.isExpired());
69+
70+
if(PT_CALL(rtc.oscillatorRunning())) {
71+
MODM_LOG_ERROR << "RTC oscillator is running." << modm::endl;
72+
}
73+
else {
74+
MODM_LOG_ERROR << "RTC oscillator is NOT running." << modm::endl;
75+
}
76+
77+
while (true)
78+
{
79+
dateTime2 = PT_CALL(rtc.getDateTime());
80+
if(dateTime2.has_value()) {
81+
MODM_LOG_INFO.printf("%02u.%02u.%02u ", dateTime2->days, dateTime2->months, dateTime2->years);
82+
MODM_LOG_INFO.printf("%02u:%02u.%02uh\n", dateTime2->hours, dateTime2->minutes, dateTime2->seconds);
83+
}
84+
else {
85+
MODM_LOG_ERROR << "Unable to read from RTC." << modm::endl;
86+
}
87+
88+
timeout.restart(2500ms);
89+
PT_WAIT_UNTIL(timeout.isExpired());
90+
}
91+
92+
PT_END();
93+
}
94+
95+
private:
96+
modm::Mcp7941x<MyI2cMaster> rtc{};
97+
modm::mcp7941x::DateTime dateTime{};
98+
std::optional<modm::mcp7941x::DateTime> dateTime2{};
99+
modm::ShortTimeout timeout;
100+
};
101+
102+
103+
using namespace Board;
104+
105+
int
106+
main()
107+
{
108+
Board::initialize();
109+
110+
// initialize Uart0 for MODM_LOG_*
111+
Uart0::connect<GpioOutput16::Tx>();
112+
Uart0::initialize<Board::SystemClock, 115200_Bd>();
113+
114+
Leds::setOutput();
115+
116+
MyI2cMaster::connect<I2cScl::Scl, I2cSda::Sda>();
117+
MyI2cMaster::initialize<SystemClock, 100_kHz>();
118+
119+
MODM_LOG_INFO << "RTC MCP7941x Example on Raspberry Pico" << modm::endl;
120+
121+
modm::Mcp7941xEeprom<MyI2cMaster> eeprom{};
122+
if (auto data = RF_CALL_BLOCKING(eeprom.getUniqueId())) {
123+
MODM_LOG_INFO << "Unique ID (EUI-48/64): ";
124+
MODM_LOG_INFO << modm::hex << (*data)[0] << modm::ascii << ":";
125+
MODM_LOG_INFO << modm::hex << (*data)[1] << modm::ascii << ":";
126+
MODM_LOG_INFO << modm::hex << (*data)[2] << modm::ascii << ":";
127+
MODM_LOG_INFO << modm::hex << (*data)[3] << modm::ascii << ":";
128+
MODM_LOG_INFO << modm::hex << (*data)[4] << modm::ascii << ":";
129+
MODM_LOG_INFO << modm::hex << (*data)[5] << modm::ascii << ":";
130+
MODM_LOG_INFO << modm::hex << (*data)[6] << modm::ascii << ":";
131+
MODM_LOG_INFO << modm::hex << (*data)[7] << modm::ascii << modm::endl;
132+
}
133+
else {
134+
MODM_LOG_ERROR << "Unable to read unique ID from RTC." << modm::endl;
135+
}
136+
modm::delay(500ms);
137+
138+
RtcThread rtcThread;
139+
140+
while (true)
141+
{
142+
LedGreen::toggle();
143+
rtcThread.update();
144+
}
145+
146+
return 0;
147+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<library>
2+
<extends>modm:rp-pico</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/rp_pico/rtc_mcp7941x</option>
5+
<option name="modm:platform:cortex-m:vector_table_location">ram</option>
6+
</options>
7+
<modules>
8+
<module>modm:debug</module>
9+
<module>modm:driver:mcp7941x</module>
10+
<module>modm:io</module>
11+
<module>modm:platform:i2c:0</module>
12+
<module>modm:platform:uart:0</module>
13+
<module>modm:processing:protothread</module>
14+
<module>modm:processing:timer</module>
15+
<module>modm:build:scons</module>
16+
</modules>
17+
</library>

0 commit comments

Comments
 (0)