Skip to content

[PXCT-17] add RTC RPC example #2298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,94 @@ int servoMove(int angle) {
}
```

### RTC RPC

The Real-time Clock (RTC) can be accessed from the M4 core, and using RPC, we can read the values.

In this example, we set the RTC to a specific date (can be adjusted in the example), send it to the M7 via RPC, and finally prints it out in the Serial Monitor using the M7 core.

**M4 sketch:**
```arduino
/**
* Initial author: Henatu (https://forum.arduino.cc/u/henatu/summary)
* modified 03 December 2024
* by Hannes Siebeneicher
*/

#include "mbed.h"
#include <mbed_mktime.h>
#include "RPC.h"

constexpr unsigned long printInterval{ 1000 };
unsigned long printNow{};

void setup() {
if (RPC.begin()) {
RPC.println("M4: Reading the RTC.");
RTCset(); //sets the RTC to start from a specific time & date
}
}

void loop() {
if (millis() > printNow) {
RPC.print("M4 System Clock: ");
RPC.println(getLocaltime());
printNow = millis() + printInterval;
}
}

String getLocaltime() {
char buffer[32];
tm t;
_rtc_localtime(time(NULL), &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT);
strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
return String(buffer);
}

void RTCset() // Set cpu RTC
{
tm t;
t.tm_sec = (0); // 0-59
t.tm_min = (58); // 0-59
t.tm_hour = (11); // 0-23
t.tm_mday = (1); // 1-31
t.tm_mon = (9); // 0-11 "0" = Jan, -1
t.tm_year = ((24) + 100); // year since 1900, current year + 100 + 1900 = correct year
set_time(mktime(&t)); // set RTC clock
}
```

The M7 sketch is found below, and uses RPC to print out the incoming data from the M4.

**M7 sketch:**
```arduino
/**
* Initial author: Henatu (https://forum.arduino.cc/u/henatu/summary)
* modified 03 December 2024
* by Hannes Siebeneicher
*/

#include "mbed.h"
#include "RPC.h"

void setup() {
RPC.begin();
Serial.begin(9600);
while (!Serial) {
; // Wait for Serial (USB) connection
}
Serial.println("M7: Serial connection initiated");
}

void loop() {
if (RPC.available()) {
char incomingByte = RPC.read(); // Read byte from RPC
Serial.write(incomingByte); // Forward the byte to Serial (USB)
}
}

```

### MicroPython RPC LED

This example demonstrates how to use MicroPython (running on the M7 core) to remotely control an LED from the M4 core.
Expand Down
Loading