Skip to content

Commit d8b0da5

Browse files
committed
add RTC RPC example
1 parent 7a3bfda commit d8b0da5

File tree

1 file changed

+130
-0
lines changed
  • content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core

1 file changed

+130
-0
lines changed

content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,136 @@ int servoMove(int angle) {
558558
}
559559
```
560560

561+
### RTC RPC
562+
563+
This example demonstrates how the RTC can be accessed from the M4:
564+
565+
Each example is written as a **single sketch** intended to be uploaded to **both cores**.
566+
567+
**M4 sketch:**
568+
```arduino
569+
#include "mbed.h"
570+
#include <mbed_mktime.h>
571+
#include "RPC.h"
572+
573+
constexpr unsigned long printInterval { 1000 };
574+
unsigned long printNow {};
575+
576+
void setup() {
577+
RPC.begin();
578+
if (RPC.cpu_id() == CM7_CPUID) {
579+
Serial.begin(19200);
580+
while (!Serial) {
581+
; // Wait for Serial (USB) connection
582+
}
583+
Serial.println("M7: Serial connection initiated");
584+
} else {
585+
//RTCset() //Uncomment if you need to set the RTC for the first time.
586+
RPC.println("M4: Reading the RTC.");
587+
}
588+
}
589+
590+
void loop() {
591+
if (RPC.cpu_id() == CM7_CPUID) {
592+
if (RPC.available()) {
593+
char incomingByte = RPC.read(); // Read byte from RPC
594+
Serial.write(incomingByte); // Forward the byte to Serial (USB)
595+
}
596+
}
597+
else
598+
{
599+
if (millis() > printNow) {
600+
RPC.print("M4 System Clock: ");
601+
RPC.println(getLocaltime());
602+
printNow = millis() + printInterval;
603+
}
604+
}
605+
}
606+
607+
String getLocaltime()
608+
{
609+
char buffer[32];
610+
tm t;
611+
_rtc_localtime(time(NULL), &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT);
612+
strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
613+
return String(buffer);
614+
}
615+
616+
void RTCset() // Set cpu RTC
617+
{
618+
tm t;
619+
t.tm_sec = (0); // 0-59
620+
t.tm_min = (58); // 0-59
621+
t.tm_hour = (11); // 0-23
622+
t.tm_mday = (1); // 1-31
623+
t.tm_mon = (9); // 0-11 "0" = Jan, -1
624+
t.tm_year = ((24)+100); // year since 1900, current year + 100 + 1900 = correct year
625+
set_time(mktime(&t)); // set RTC clock
626+
}
627+
```
628+
629+
**M7 sketch:**
630+
```arduino
631+
#include "mbed.h"
632+
#include <mbed_mktime.h>
633+
#include "RPC.h"
634+
635+
constexpr unsigned long printInterval { 1000 };
636+
unsigned long printNow {};
637+
638+
void setup() {
639+
RPC.begin();
640+
if (RPC.cpu_id() == CM7_CPUID) {
641+
Serial.begin(19200);
642+
while (!Serial) {
643+
; // Wait for Serial (USB) connection
644+
}
645+
Serial.println("M7: Serial connection initiated");
646+
} else {
647+
//RTCset() //Uncomment if you need to set the RTC for the first time.
648+
RPC.println("M4: Reading the RTC.");
649+
}
650+
}
651+
652+
void loop() {
653+
if (RPC.cpu_id() == CM7_CPUID) {
654+
if (RPC.available()) {
655+
char incomingByte = RPC.read(); // Read byte from RPC
656+
Serial.write(incomingByte); // Forward the byte to Serial (USB)
657+
}
658+
}
659+
else
660+
{
661+
if (millis() > printNow) {
662+
RPC.print("M4 System Clock: ");
663+
RPC.println(getLocaltime());
664+
printNow = millis() + printInterval;
665+
}
666+
}
667+
}
668+
669+
String getLocaltime()
670+
{
671+
char buffer[32];
672+
tm t;
673+
_rtc_localtime(time(NULL), &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT);
674+
strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
675+
return String(buffer);
676+
}
677+
678+
void RTCset() // Set cpu RTC
679+
{
680+
tm t;
681+
t.tm_sec = (0); // 0-59
682+
t.tm_min = (58); // 0-59
683+
t.tm_hour = (11); // 0-23
684+
t.tm_mday = (1); // 1-31
685+
t.tm_mon = (9); // 0-11 "0" = Jan, -1
686+
t.tm_year = ((24)+100); // year since 1900, current year + 100 + 1900 = correct year
687+
set_time(mktime(&t)); // set RTC clock
688+
}
689+
```
690+
561691
### MicroPython RPC LED
562692

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

0 commit comments

Comments
 (0)