From d8b0da566be04c1b6c3c4a621ef410f9f37bbddb Mon Sep 17 00:00:00 2001 From: Hannes Siebeneicher Date: Mon, 25 Nov 2024 11:08:19 +0100 Subject: [PATCH 1/3] add RTC RPC example --- .../giga-dual-core/giga-dual-core.md | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md index 65ca66b3c9..9186b28de2 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md @@ -558,6 +558,136 @@ int servoMove(int angle) { } ``` +### RTC RPC + +This example demonstrates how the RTC can be accessed from the M4: + +Each example is written as a **single sketch** intended to be uploaded to **both cores**. + +**M4 sketch:** +```arduino +#include "mbed.h" +#include +#include "RPC.h" + +constexpr unsigned long printInterval { 1000 }; +unsigned long printNow {}; + +void setup() { + RPC.begin(); + if (RPC.cpu_id() == CM7_CPUID) { + Serial.begin(19200); + while (!Serial) { + ; // Wait for Serial (USB) connection + } + Serial.println("M7: Serial connection initiated"); + } else { + //RTCset() //Uncomment if you need to set the RTC for the first time. + RPC.println("M4: Reading the RTC."); + } +} + +void loop() { + if (RPC.cpu_id() == CM7_CPUID) { + if (RPC.available()) { + char incomingByte = RPC.read(); // Read byte from RPC + Serial.write(incomingByte); // Forward the byte to Serial (USB) + } + } + else + { + 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 +} +``` + +**M7 sketch:** +```arduino +#include "mbed.h" +#include +#include "RPC.h" + +constexpr unsigned long printInterval { 1000 }; +unsigned long printNow {}; + +void setup() { + RPC.begin(); + if (RPC.cpu_id() == CM7_CPUID) { + Serial.begin(19200); + while (!Serial) { + ; // Wait for Serial (USB) connection + } + Serial.println("M7: Serial connection initiated"); + } else { + //RTCset() //Uncomment if you need to set the RTC for the first time. + RPC.println("M4: Reading the RTC."); + } +} + +void loop() { + if (RPC.cpu_id() == CM7_CPUID) { + if (RPC.available()) { + char incomingByte = RPC.read(); // Read byte from RPC + Serial.write(incomingByte); // Forward the byte to Serial (USB) + } + } + else + { + 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 +} +``` + ### MicroPython RPC LED This example demonstrates how to use MicroPython (running on the M7 core) to remotely control an LED from the M4 core. From 09271392b3607510b45245375ed70f4d737d822a Mon Sep 17 00:00:00 2001 From: Hannes Siebeneicher Date: Tue, 3 Dec 2024 14:17:26 +0100 Subject: [PATCH 2/3] split RTC RPC example --- .../giga-dual-core/giga-dual-core.md | 140 ++++++------------ 1 file changed, 47 insertions(+), 93 deletions(-) diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md index 9186b28de2..926eae8802 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md @@ -562,130 +562,84 @@ int servoMove(int angle) { This example demonstrates how the RTC can be accessed from the M4: -Each example is written as a **single sketch** intended to be uploaded to **both cores**. - **M4 sketch:** ```arduino +/** +* Initial author: Henatu (https://forum.arduino.cc/u/henatu/summary) +* modified 03 December 2024 +* by Hannes Siebeneicher +*/ + #include "mbed.h" #include #include "RPC.h" -constexpr unsigned long printInterval { 1000 }; -unsigned long printNow {}; +constexpr unsigned long printInterval{ 1000 }; +unsigned long printNow{}; void setup() { - RPC.begin(); - if (RPC.cpu_id() == CM7_CPUID) { - Serial.begin(19200); - while (!Serial) { - ; // Wait for Serial (USB) connection - } - Serial.println("M7: Serial connection initiated"); - } else { - //RTCset() //Uncomment if you need to set the RTC for the first time. - RPC.println("M4: Reading the RTC."); - } + if (RPC.begin()) { + RPC.println("M4: Reading the RTC."); + //RTCset() //Uncomment if you need to set the RTC for the first time. + } } void loop() { - if (RPC.cpu_id() == CM7_CPUID) { - if (RPC.available()) { - char incomingByte = RPC.read(); // Read byte from RPC - Serial.write(incomingByte); // Forward the byte to Serial (USB) - } - } - else - { - if (millis() > printNow) { - RPC.print("M4 System Clock: "); - RPC.println(getLocaltime()); - printNow = millis() + printInterval; - } - } + 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); +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 + 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 } ``` **M7 sketch:** ```arduino +/** +* Initial author: Henatu (https://forum.arduino.cc/u/henatu/summary) +* modified 03 December 2024 +* by Hannes Siebeneicher +*/ + #include "mbed.h" -#include #include "RPC.h" -constexpr unsigned long printInterval { 1000 }; -unsigned long printNow {}; - void setup() { - RPC.begin(); - if (RPC.cpu_id() == CM7_CPUID) { - Serial.begin(19200); - while (!Serial) { - ; // Wait for Serial (USB) connection - } - Serial.println("M7: Serial connection initiated"); - } else { - //RTCset() //Uncomment if you need to set the RTC for the first time. - RPC.println("M4: Reading the RTC."); - } + RPC.begin(); + Serial.begin(9600); + while (!Serial) { + ; // Wait for Serial (USB) connection + } + Serial.println("M7: Serial connection initiated"); } void loop() { - if (RPC.cpu_id() == CM7_CPUID) { - if (RPC.available()) { - char incomingByte = RPC.read(); // Read byte from RPC - Serial.write(incomingByte); // Forward the byte to Serial (USB) - } - } - else - { - 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); + if (RPC.available()) { + char incomingByte = RPC.read(); // Read byte from RPC + Serial.write(incomingByte); // Forward the byte to Serial (USB) + } } -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 -} ``` ### MicroPython RPC LED From ea7db1b6790be0a04a6b63ae989875a5f51ba793 Mon Sep 17 00:00:00 2001 From: Hannes Siebeneicher <60609597+Hannes7eicher@users.noreply.github.com> Date: Wed, 4 Dec 2024 10:29:55 +0100 Subject: [PATCH 3/3] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Karl Söderby <35461661+karlsoderby@users.noreply.github.com> --- .../tutorials/giga-dual-core/giga-dual-core.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md index 926eae8802..f707566916 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md @@ -560,7 +560,9 @@ int servoMove(int angle) { ### RTC RPC -This example demonstrates how the RTC can be accessed from the M4: +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 @@ -580,7 +582,7 @@ unsigned long printNow{}; void setup() { if (RPC.begin()) { RPC.println("M4: Reading the RTC."); - //RTCset() //Uncomment if you need to set the RTC for the first time. + RTCset(); //sets the RTC to start from a specific time & date } } @@ -613,6 +615,8 @@ void RTCset() // Set cpu RTC } ``` +The M7 sketch is found below, and uses RPC to print out the incoming data from the M4. + **M7 sketch:** ```arduino /**