@@ -562,130 +562,84 @@ int servoMove(int angle) {
562562
563563This example demonstrates how the RTC can be accessed from the M4:
564564
565- Each example is written as a ** single sketch** intended to be uploaded to ** both cores** .
566-
567565** M4 sketch:**
568566``` arduino
567+ /**
568+ * Initial author: Henatu (https://forum.arduino.cc/u/henatu/summary)
569+ * modified 03 December 2024
570+ * by Hannes Siebeneicher
571+ */
572+
569573#include "mbed.h"
570574#include <mbed_mktime.h>
571575#include "RPC.h"
572576
573- constexpr unsigned long printInterval { 1000 };
574- unsigned long printNow {};
577+ constexpr unsigned long printInterval{ 1000 };
578+ unsigned long printNow{};
575579
576580void 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- }
581+ if (RPC.begin()) {
582+ RPC.println("M4: Reading the RTC.");
583+ //RTCset() //Uncomment if you need to set the RTC for the first time.
584+ }
588585}
589586
590587void 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- }
588+ if (millis() > printNow) {
589+ RPC.print("M4 System Clock: ");
590+ RPC.println(getLocaltime());
591+ printNow = millis() + printInterval;
592+ }
605593}
606594
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);
595+ String getLocaltime() {
596+ char buffer[32];
597+ tm t;
598+ _rtc_localtime(time(NULL), &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT);
599+ strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
600+ return String(buffer);
614601}
615602
616603void RTCset() // Set cpu RTC
617- {
604+ {
618605 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
606+ t.tm_sec = (0); // 0-59
607+ t.tm_min = (58); // 0-59
608+ t.tm_hour = (11); // 0-23
609+ t.tm_mday = (1); // 1-31
610+ t.tm_mon = (9); // 0-11 "0" = Jan, -1
611+ t.tm_year = ((24) + 100); // year since 1900, current year + 100 + 1900 = correct year
612+ set_time(mktime(&t)); // set RTC clock
626613}
627614```
628615
629616** M7 sketch:**
630617``` arduino
618+ /**
619+ * Initial author: Henatu (https://forum.arduino.cc/u/henatu/summary)
620+ * modified 03 December 2024
621+ * by Hannes Siebeneicher
622+ */
623+
631624#include "mbed.h"
632- #include <mbed_mktime.h>
633625#include "RPC.h"
634626
635- constexpr unsigned long printInterval { 1000 };
636- unsigned long printNow {};
637-
638627void 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- }
628+ RPC.begin();
629+ Serial.begin(9600);
630+ while (!Serial) {
631+ ; // Wait for Serial (USB) connection
632+ }
633+ Serial.println("M7: Serial connection initiated");
650634}
651635
652636void 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);
637+ if (RPC.available()) {
638+ char incomingByte = RPC.read(); // Read byte from RPC
639+ Serial.write(incomingByte); // Forward the byte to Serial (USB)
640+ }
676641}
677642
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- }
689643```
690644
691645### MicroPython RPC LED
0 commit comments