@@ -562,130 +562,84 @@ int servoMove(int angle) {
562
562
563
563
This example demonstrates how the RTC can be accessed from the M4:
564
564
565
- Each example is written as a ** single sketch** intended to be uploaded to ** both cores** .
566
-
567
565
** M4 sketch:**
568
566
``` arduino
567
+ /**
568
+ * Initial author: Henatu (https://forum.arduino.cc/u/henatu/summary)
569
+ * modified 03 December 2024
570
+ * by Hannes Siebeneicher
571
+ */
572
+
569
573
#include "mbed.h"
570
574
#include <mbed_mktime.h>
571
575
#include "RPC.h"
572
576
573
- constexpr unsigned long printInterval { 1000 };
574
- unsigned long printNow {};
577
+ constexpr unsigned long printInterval{ 1000 };
578
+ unsigned long printNow{};
575
579
576
580
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
- }
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
+ }
588
585
}
589
586
590
587
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
- }
588
+ if (millis() > printNow) {
589
+ RPC.print("M4 System Clock: ");
590
+ RPC.println(getLocaltime());
591
+ printNow = millis() + printInterval;
592
+ }
605
593
}
606
594
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);
614
601
}
615
602
616
603
void RTCset() // Set cpu RTC
617
- {
604
+ {
618
605
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
626
613
}
627
614
```
628
615
629
616
** M7 sketch:**
630
617
``` arduino
618
+ /**
619
+ * Initial author: Henatu (https://forum.arduino.cc/u/henatu/summary)
620
+ * modified 03 December 2024
621
+ * by Hannes Siebeneicher
622
+ */
623
+
631
624
#include "mbed.h"
632
- #include <mbed_mktime.h>
633
625
#include "RPC.h"
634
626
635
- constexpr unsigned long printInterval { 1000 };
636
- unsigned long printNow {};
637
-
638
627
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
- }
628
+ RPC.begin();
629
+ Serial.begin(9600);
630
+ while (!Serial) {
631
+ ; // Wait for Serial (USB) connection
632
+ }
633
+ Serial.println("M7: Serial connection initiated");
650
634
}
651
635
652
636
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);
637
+ if (RPC.available()) {
638
+ char incomingByte = RPC.read(); // Read byte from RPC
639
+ Serial.write(incomingByte); // Forward the byte to Serial (USB)
640
+ }
676
641
}
677
642
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
643
```
690
644
691
645
### MicroPython RPC LED
0 commit comments