From 14028fb4854016c95f5ad859193c7543f3a71a63 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 19 Mar 2024 09:17:22 +0100 Subject: [PATCH 01/31] first changes for using 2nd core --- src/MF_CustomDevice/CustomDevice.cpp | 6 ++++++ src/MF_CustomDevice/CustomDevice.h | 3 +++ src/MF_Modules/MFEEPROM.cpp | 10 ++++++++++ src/MF_Modules/MFEEPROM.h | 20 ++++++++++++++++++++ src/mobiflight.cpp | 3 +++ 5 files changed, 42 insertions(+) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index fc80876c..90a41d7e 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -102,5 +102,11 @@ namespace CustomDevice customDevice[i].set(MESSAGEID_POWERSAVINGMODE, "0"); } } +#if defined(USE_2ND_CORE) + void init_2ndCore() + { + MFCustomDevice_init_2ndCore(); + } +#endif } // end of namespace diff --git a/src/MF_CustomDevice/CustomDevice.h b/src/MF_CustomDevice/CustomDevice.h index 1fcb5fdf..469b7a5b 100644 --- a/src/MF_CustomDevice/CustomDevice.h +++ b/src/MF_CustomDevice/CustomDevice.h @@ -8,4 +8,7 @@ namespace CustomDevice void update(); void OnSet(); void PowerSave(bool state); +#if defined(USE_2ND_CORE) + void init_2ndCore(); +#endif } \ No newline at end of file diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index 7d63bfd5..22aee9db 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -35,6 +35,16 @@ bool MFEEPROM::write_byte(uint16_t adr, const uint8_t data) EEPROM.write(adr, data); #if defined(ARDUINO_ARCH_RP2040) EEPROM.commit(); +#endif +#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) + // ######################################################################### + // Communication with Core1 + // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html + // ######################################################################### + multicore_fifo_push_blocking(CORE1_CMD_STOP); + multicore_lockout_start_blocking(); + EEPROM.commit(); + multicore_lockout_end_blocking(); #endif return true; } diff --git a/src/MF_Modules/MFEEPROM.h b/src/MF_Modules/MFEEPROM.h index 92fe7328..f2b4113c 100644 --- a/src/MF_Modules/MFEEPROM.h +++ b/src/MF_Modules/MFEEPROM.h @@ -46,6 +46,16 @@ class MFEEPROM EEPROM.put(adr, t); #if defined(ARDUINO_ARCH_RP2040) EEPROM.commit(); +#endif +#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) + // ######################################################################### + // Communication with Core1 + // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html + // ######################################################################### + multicore_fifo_push_blocking(CORE1_CMD_STOP); + multicore_lockout_start_blocking(); + EEPROM.commit(); + multicore_lockout_end_blocking(); #endif return true; } @@ -59,6 +69,16 @@ class MFEEPROM } #if defined(ARDUINO_ARCH_RP2040) EEPROM.commit(); +#endif +#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) + // ######################################################################### + // Communication with Core1 + // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html + // ######################################################################### + multicore_fifo_push_blocking(CORE1_CMD_STOP); + multicore_lockout_start_blocking(); + EEPROM.commit(); + multicore_lockout_end_blocking(); #endif return true; } diff --git a/src/mobiflight.cpp b/src/mobiflight.cpp index 45ebae74..690a5301 100644 --- a/src/mobiflight.cpp +++ b/src/mobiflight.cpp @@ -163,6 +163,9 @@ void setup() cmdMessenger.printLfCr(); ResetBoard(); initPollIntervals(); +#if defined(USE_2ND_CORE) + CustomDevice::init_2ndCore(); +#endif } // ************************************************************ From e1a77443655cec037fe118bfcc798c7558fd7428 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 19 Mar 2024 09:28:11 +0100 Subject: [PATCH 02/31] missing include file added --- src/MF_Modules/MFEEPROM.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index 22aee9db..757316ac 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -7,6 +7,9 @@ #include #include "MFEEPROM.h" #include "MFBoards.h" +#if defined(USE_2ND_CORE) +#include "MFCustomDevice.h" +#endif MFEEPROM::MFEEPROM() {} From f7b6803c320d48de879964956905128a0d7db13f Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 19 Mar 2024 09:29:41 +0100 Subject: [PATCH 03/31] missing include file added --- src/MF_Modules/MFEEPROM.cpp | 3 --- src/MF_Modules/MFEEPROM.h | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index 757316ac..22aee9db 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -7,9 +7,6 @@ #include #include "MFEEPROM.h" #include "MFBoards.h" -#if defined(USE_2ND_CORE) -#include "MFCustomDevice.h" -#endif MFEEPROM::MFEEPROM() {} diff --git a/src/MF_Modules/MFEEPROM.h b/src/MF_Modules/MFEEPROM.h index f2b4113c..3d075920 100644 --- a/src/MF_Modules/MFEEPROM.h +++ b/src/MF_Modules/MFEEPROM.h @@ -7,6 +7,9 @@ #pragma once #include +#if defined(USE_2ND_CORE) +#include "MFCustomDevice.h" +#endif class MFEEPROM { From 0b70d576ada3c7076cfe8ed2ad8edd6afcfbf009 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 19 Mar 2024 10:45:13 +0100 Subject: [PATCH 04/31] correct order of commit eeprom for 2nd core --- src/MF_Modules/MFEEPROM.cpp | 5 ++--- src/MF_Modules/MFEEPROM.h | 10 ++++------ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index 22aee9db..3393ed08 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -33,9 +33,6 @@ bool MFEEPROM::write_byte(uint16_t adr, const uint8_t data) { if (adr >= _eepromLength) return false; EEPROM.write(adr, data); -#if defined(ARDUINO_ARCH_RP2040) - EEPROM.commit(); -#endif #if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) // ######################################################################### // Communication with Core1 @@ -43,7 +40,9 @@ bool MFEEPROM::write_byte(uint16_t adr, const uint8_t data) // ######################################################################### multicore_fifo_push_blocking(CORE1_CMD_STOP); multicore_lockout_start_blocking(); +#endif EEPROM.commit(); +#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) multicore_lockout_end_blocking(); #endif return true; diff --git a/src/MF_Modules/MFEEPROM.h b/src/MF_Modules/MFEEPROM.h index 3d075920..9bda4b00 100644 --- a/src/MF_Modules/MFEEPROM.h +++ b/src/MF_Modules/MFEEPROM.h @@ -47,9 +47,6 @@ class MFEEPROM { if (adr + sizeof(T) > _eepromLength) return false; EEPROM.put(adr, t); -#if defined(ARDUINO_ARCH_RP2040) - EEPROM.commit(); -#endif #if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) // ######################################################################### // Communication with Core1 @@ -57,7 +54,9 @@ class MFEEPROM // ######################################################################### multicore_fifo_push_blocking(CORE1_CMD_STOP); multicore_lockout_start_blocking(); +#endif EEPROM.commit(); +#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) multicore_lockout_end_blocking(); #endif return true; @@ -70,9 +69,6 @@ class MFEEPROM for (uint16_t i = 0; i < len; i++) { EEPROM.put(adr + i, t[i]); } -#if defined(ARDUINO_ARCH_RP2040) - EEPROM.commit(); -#endif #if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) // ######################################################################### // Communication with Core1 @@ -80,7 +76,9 @@ class MFEEPROM // ######################################################################### multicore_fifo_push_blocking(CORE1_CMD_STOP); multicore_lockout_start_blocking(); +#endif EEPROM.commit(); +#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) multicore_lockout_end_blocking(); #endif return true; From df79e5e3dd3d02cb98ffa3a81abe05daa3f993c1 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 19 Mar 2024 13:55:32 +0100 Subject: [PATCH 05/31] fixed eeprom write for Atmels --- src/MF_Modules/MFEEPROM.cpp | 4 +++- src/MF_Modules/MFEEPROM.h | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index 3393ed08..05f8d66c 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -41,7 +41,9 @@ bool MFEEPROM::write_byte(uint16_t adr, const uint8_t data) multicore_fifo_push_blocking(CORE1_CMD_STOP); multicore_lockout_start_blocking(); #endif - EEPROM.commit(); +#if defined(ARDUINO_ARCH_RP2040) + EEPROM.commit(); +#endif #if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) multicore_lockout_end_blocking(); #endif diff --git a/src/MF_Modules/MFEEPROM.h b/src/MF_Modules/MFEEPROM.h index 9bda4b00..0188a608 100644 --- a/src/MF_Modules/MFEEPROM.h +++ b/src/MF_Modules/MFEEPROM.h @@ -55,7 +55,9 @@ class MFEEPROM multicore_fifo_push_blocking(CORE1_CMD_STOP); multicore_lockout_start_blocking(); #endif +#if defined(ARDUINO_ARCH_RP2040) EEPROM.commit(); +#endif #if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) multicore_lockout_end_blocking(); #endif @@ -77,7 +79,9 @@ class MFEEPROM multicore_fifo_push_blocking(CORE1_CMD_STOP); multicore_lockout_start_blocking(); #endif +#if defined(ARDUINO_ARCH_RP2040) EEPROM.commit(); +#endif #if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) multicore_lockout_end_blocking(); #endif From f0eb0279a856e8c164e3de7b6171c1e62a64b9ae Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 19 Mar 2024 14:01:14 +0100 Subject: [PATCH 06/31] added 2nd core --- src/MF_CustomDevice/CustomDevice.cpp | 55 ++++++++++++++++++++++++++-- src/MF_CustomDevice/CustomDevice.h | 17 +++++++++ src/MF_Modules/MFEEPROM.cpp | 2 +- src/MF_Modules/MFEEPROM.h | 4 +- 4 files changed, 72 insertions(+), 6 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 90a41d7e..7e16f507 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -14,6 +14,7 @@ namespace CustomDevice MFCustomDevice *customDevice; uint8_t customDeviceRegistered = 0; uint8_t maxCustomDevices = 0; + char *output; bool setupArray(uint16_t count) { @@ -82,9 +83,21 @@ namespace CustomDevice if (device >= customDeviceRegistered) // and do nothing if this device is not registered return; int16_t messageID = cmdMessenger.readInt16Arg(); // get the messageID number - char *output = cmdMessenger.readStringArg(); // get the pointer to the new raw string + output = cmdMessenger.readStringArg(); // get the pointer to the new raw string cmdMessenger.unescape(output); // and unescape the string if escape characters are used - customDevice[device].set(messageID, output); // send the string to your custom device + +#if defined(USE_2ND_CORE) + // ######################################################################### + // Communication with Core1 + // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html + // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore + // ######################################################################### + multicore_fifo_push_blocking(CORE1_DATA | CORE1_DATA_DEVICE | (device & 0x00FFFFFF)); + multicore_fifo_push_blocking(CORE1_DATA | CORE1_DATA_MESSAGE_ID | (messageID & 0x00FFFFFF)); + multicore_fifo_push_blocking(CORE1_CMD | CORE1_CMD_SEND); +#else + customDevice[device].set(messageID, output); // send the string to your custom device +#endif } /* ********************************************************************************** @@ -102,11 +115,47 @@ namespace CustomDevice customDevice[i].set(MESSAGEID_POWERSAVINGMODE, "0"); } } + #if defined(USE_2ND_CORE) + void checkDataFromCore0() + { + static uint32_t receivedDevice, receivedMessageID, receivedPayload; + // ######################################################################### + // Communication with Core0 + // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html + // see https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore + // ######################################################################### + if (multicore_fifo_rvalid()) { + uint32_t dataCore0 = multicore_fifo_pop_blocking(); + if (dataCore0 & CORE1_CMD) { // check if bit 32 is set to 1 + if (dataCore0 & CORE1_CMD_STOP) + multicore_lockout_victim_init(); + if (dataCore0 & CORE1_CMD_SEND) + customDevice[receivedDevice].set(receivedMessageID, output); + } else { // bit 32 is set to 0 + if (dataCore0 & CORE1_DATA_DEVICE) + receivedDevice = dataCore0 & 0x00FFFFFF; + if (dataCore0 & CORE1_DATA_MESSAGE_ID) + receivedMessageID = dataCore0 & 0x00FFFFFF; + if (dataCore0 & CORE1_DATA_PAYLOAD) + receivedPayload = dataCore0 & 0x00FFFFFF; + } + } + } + + void loop_2ndCore() + { + while(1) + { + checkDataFromCore0(); + } + } + void init_2ndCore() { - MFCustomDevice_init_2ndCore(); + multicore_launch_core1(loop_2ndCore); } + #endif } // end of namespace diff --git a/src/MF_CustomDevice/CustomDevice.h b/src/MF_CustomDevice/CustomDevice.h index 469b7a5b..c75b38ed 100644 --- a/src/MF_CustomDevice/CustomDevice.h +++ b/src/MF_CustomDevice/CustomDevice.h @@ -1,5 +1,22 @@ #pragma once +#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) +// ######################################################################### +// For Communication with Core1 +// see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html +// https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore +// ######################################################################### +#define CORE1_CMD (1 << 31) +#define CORE1_DATA 0 + +#define CORE1_CMD_STOP (1 << 0) +#define CORE1_CMD_SEND (1 << 1) + +#define CORE1_DATA_DEVICE (1 << 30) +#define CORE1_DATA_MESSAGE_ID (1 << 29) +#define CORE1_DATA_PAYLOAD (1 << 28) +#endif + namespace CustomDevice { bool setupArray(uint16_t count); diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index 05f8d66c..97f6a8bb 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -38,7 +38,7 @@ bool MFEEPROM::write_byte(uint16_t adr, const uint8_t data) // Communication with Core1 // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html // ######################################################################### - multicore_fifo_push_blocking(CORE1_CMD_STOP); + multicore_fifo_push_blocking(CORE1_CMD | CORE1_CMD_STOP); multicore_lockout_start_blocking(); #endif #if defined(ARDUINO_ARCH_RP2040) diff --git a/src/MF_Modules/MFEEPROM.h b/src/MF_Modules/MFEEPROM.h index 0188a608..31bef223 100644 --- a/src/MF_Modules/MFEEPROM.h +++ b/src/MF_Modules/MFEEPROM.h @@ -52,7 +52,7 @@ class MFEEPROM // Communication with Core1 // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html // ######################################################################### - multicore_fifo_push_blocking(CORE1_CMD_STOP); + multicore_fifo_push_blocking(CORE1_CMD | CORE1_CMD_STOP); multicore_lockout_start_blocking(); #endif #if defined(ARDUINO_ARCH_RP2040) @@ -76,7 +76,7 @@ class MFEEPROM // Communication with Core1 // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html // ######################################################################### - multicore_fifo_push_blocking(CORE1_CMD_STOP); + multicore_fifo_push_blocking(CORE1_CMD | CORE1_CMD_STOP); multicore_lockout_start_blocking(); #endif #if defined(ARDUINO_ARCH_RP2040) From 814fa496cacdcfb3711ac9ac8327c2ec46eebfd8 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 19 Mar 2024 14:04:30 +0100 Subject: [PATCH 07/31] fixed missing defines --- src/MF_Modules/MFEEPROM.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MF_Modules/MFEEPROM.h b/src/MF_Modules/MFEEPROM.h index 31bef223..2d346359 100644 --- a/src/MF_Modules/MFEEPROM.h +++ b/src/MF_Modules/MFEEPROM.h @@ -8,7 +8,7 @@ #include #if defined(USE_2ND_CORE) -#include "MFCustomDevice.h" +#include "CustomDevice.h" #endif class MFEEPROM From 159b2dd217f0334ec367dc761c498feff8fe6ac7 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Wed, 20 Mar 2024 06:36:29 +0100 Subject: [PATCH 08/31] reformatting --- src/MF_CustomDevice/CustomDevice.cpp | 2 -- src/MF_CustomDevice/CustomDevice.h | 3 +-- src/MF_Modules/MFEEPROM.cpp | 20 ++++++++++---------- src/MF_Modules/MFEEPROM.h | 20 ++++++++++---------- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 7e16f507..6b2fd1cb 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -89,7 +89,6 @@ namespace CustomDevice #if defined(USE_2ND_CORE) // ######################################################################### // Communication with Core1 - // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### multicore_fifo_push_blocking(CORE1_DATA | CORE1_DATA_DEVICE | (device & 0x00FFFFFF)); @@ -122,7 +121,6 @@ namespace CustomDevice static uint32_t receivedDevice, receivedMessageID, receivedPayload; // ######################################################################### // Communication with Core0 - // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html // see https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### if (multicore_fifo_rvalid()) { diff --git a/src/MF_CustomDevice/CustomDevice.h b/src/MF_CustomDevice/CustomDevice.h index c75b38ed..29492657 100644 --- a/src/MF_CustomDevice/CustomDevice.h +++ b/src/MF_CustomDevice/CustomDevice.h @@ -1,9 +1,8 @@ #pragma once -#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) +#if defined(USE_2ND_CORE) // ######################################################################### // For Communication with Core1 -// see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### #define CORE1_CMD (1 << 31) diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index 97f6a8bb..efd38230 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -33,19 +33,19 @@ bool MFEEPROM::write_byte(uint16_t adr, const uint8_t data) { if (adr >= _eepromLength) return false; EEPROM.write(adr, data); -#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) - // ######################################################################### - // Communication with Core1 - // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html - // ######################################################################### - multicore_fifo_push_blocking(CORE1_CMD | CORE1_CMD_STOP); - multicore_lockout_start_blocking(); -#endif #if defined(ARDUINO_ARCH_RP2040) +#if defined(USE_2ND_CORE) + // ######################################################################### + // Communication with Core1 + // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore + // ######################################################################### + multicore_fifo_push_blocking(CORE1_CMD | CORE1_CMD_STOP); + multicore_lockout_start_blocking(); +#endif EEPROM.commit(); +#if defined(USE_2ND_CORE) + multicore_lockout_end_blocking(); #endif -#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) - multicore_lockout_end_blocking(); #endif return true; } diff --git a/src/MF_Modules/MFEEPROM.h b/src/MF_Modules/MFEEPROM.h index 2d346359..6bbdb7fe 100644 --- a/src/MF_Modules/MFEEPROM.h +++ b/src/MF_Modules/MFEEPROM.h @@ -47,19 +47,19 @@ class MFEEPROM { if (adr + sizeof(T) > _eepromLength) return false; EEPROM.put(adr, t); -#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) +#if defined(ARDUINO_ARCH_RP2040) +#if defined(USE_2ND_CORE) // ######################################################################### // Communication with Core1 - // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html + // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### multicore_fifo_push_blocking(CORE1_CMD | CORE1_CMD_STOP); multicore_lockout_start_blocking(); #endif -#if defined(ARDUINO_ARCH_RP2040) EEPROM.commit(); -#endif -#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) +#if defined(USE_2ND_CORE) multicore_lockout_end_blocking(); +#endif #endif return true; } @@ -71,19 +71,19 @@ class MFEEPROM for (uint16_t i = 0; i < len; i++) { EEPROM.put(adr + i, t[i]); } -#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) +#if defined(ARDUINO_ARCH_RP2040) +#if defined(USE_2ND_CORE) // ######################################################################### // Communication with Core1 - // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html + // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### multicore_fifo_push_blocking(CORE1_CMD | CORE1_CMD_STOP); multicore_lockout_start_blocking(); #endif -#if defined(ARDUINO_ARCH_RP2040) EEPROM.commit(); -#endif -#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) +#if defined(USE_2ND_CORE) multicore_lockout_end_blocking(); +#endif #endif return true; } From be963a0e3c750681f321e9e4a8cc146049e56cf9 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Wed, 20 Mar 2024 08:51:29 +0100 Subject: [PATCH 09/31] better structure for communicating with 2nd core --- src/MF_CustomDevice/CustomDevice.cpp | 37 +++++++++++++++++----------- src/MF_CustomDevice/CustomDevice.h | 10 ++------ src/MF_Modules/MFEEPROM.cpp | 2 +- src/MF_Modules/MFEEPROM.h | 4 +-- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 6b2fd1cb..78d87fbb 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -87,13 +87,16 @@ namespace CustomDevice cmdMessenger.unescape(output); // and unescape the string if escape characters are used #if defined(USE_2ND_CORE) + strncpy(payload, output, DSERIAL_RX_BUFFER_SIZE); // ######################################################################### // Communication with Core1 + // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### - multicore_fifo_push_blocking(CORE1_DATA | CORE1_DATA_DEVICE | (device & 0x00FFFFFF)); - multicore_fifo_push_blocking(CORE1_DATA | CORE1_DATA_MESSAGE_ID | (messageID & 0x00FFFFFF)); - multicore_fifo_push_blocking(CORE1_CMD | CORE1_CMD_SEND); + multicore_fifo_push_blocking(CORE1_CMD_SEND); + multicore_fifo_push_blocking(device); + multicore_fifo_push_blocking(messageID); + //multicore_fifo_push_blocking(payload); #else customDevice[device].set(messageID, output); // send the string to your custom device #endif @@ -121,22 +124,26 @@ namespace CustomDevice static uint32_t receivedDevice, receivedMessageID, receivedPayload; // ######################################################################### // Communication with Core0 + // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html // see https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### if (multicore_fifo_rvalid()) { uint32_t dataCore0 = multicore_fifo_pop_blocking(); - if (dataCore0 & CORE1_CMD) { // check if bit 32 is set to 1 - if (dataCore0 & CORE1_CMD_STOP) - multicore_lockout_victim_init(); - if (dataCore0 & CORE1_CMD_SEND) - customDevice[receivedDevice].set(receivedMessageID, output); - } else { // bit 32 is set to 0 - if (dataCore0 & CORE1_DATA_DEVICE) - receivedDevice = dataCore0 & 0x00FFFFFF; - if (dataCore0 & CORE1_DATA_MESSAGE_ID) - receivedMessageID = dataCore0 & 0x00FFFFFF; - if (dataCore0 & CORE1_DATA_PAYLOAD) - receivedPayload = dataCore0 & 0x00FFFFFF; + switch (dataCore0) + { + case CORE1_CMD_STOP: + multicore_lockout_victim_init(); + break; + + case CORE1_CMD_SEND: + receivedDevice = multicore_fifo_pop_blocking(); + receivedMessageID = multicore_fifo_pop_blocking(); + //receivedPayload = multicore_fifo_pop_blocking(); + customDevice[receivedDevice].set(receivedMessageID, payload); + break; + + default: + break; } } } diff --git a/src/MF_CustomDevice/CustomDevice.h b/src/MF_CustomDevice/CustomDevice.h index 29492657..ecc5c8f1 100644 --- a/src/MF_CustomDevice/CustomDevice.h +++ b/src/MF_CustomDevice/CustomDevice.h @@ -1,19 +1,12 @@ #pragma once -#if defined(USE_2ND_CORE) +#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) // ######################################################################### // For Communication with Core1 // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### -#define CORE1_CMD (1 << 31) -#define CORE1_DATA 0 - #define CORE1_CMD_STOP (1 << 0) #define CORE1_CMD_SEND (1 << 1) - -#define CORE1_DATA_DEVICE (1 << 30) -#define CORE1_DATA_MESSAGE_ID (1 << 29) -#define CORE1_DATA_PAYLOAD (1 << 28) #endif namespace CustomDevice @@ -26,5 +19,6 @@ namespace CustomDevice void PowerSave(bool state); #if defined(USE_2ND_CORE) void init_2ndCore(); + char payload[SERIAL_RX_BUFFER_SIZE]; #endif } \ No newline at end of file diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index efd38230..29401ba6 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -39,7 +39,7 @@ bool MFEEPROM::write_byte(uint16_t adr, const uint8_t data) // Communication with Core1 // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### - multicore_fifo_push_blocking(CORE1_CMD | CORE1_CMD_STOP); + multicore_fifo_push_blocking(CORE1_CMD_STOP); multicore_lockout_start_blocking(); #endif EEPROM.commit(); diff --git a/src/MF_Modules/MFEEPROM.h b/src/MF_Modules/MFEEPROM.h index 6bbdb7fe..ca1316cb 100644 --- a/src/MF_Modules/MFEEPROM.h +++ b/src/MF_Modules/MFEEPROM.h @@ -53,7 +53,7 @@ class MFEEPROM // Communication with Core1 // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### - multicore_fifo_push_blocking(CORE1_CMD | CORE1_CMD_STOP); + multicore_fifo_push_blocking(CORE1_CMD_STOP); multicore_lockout_start_blocking(); #endif EEPROM.commit(); @@ -77,7 +77,7 @@ class MFEEPROM // Communication with Core1 // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### - multicore_fifo_push_blocking(CORE1_CMD | CORE1_CMD_STOP); + multicore_fifo_push_blocking(CORE1_CMD_STOP); multicore_lockout_start_blocking(); #endif EEPROM.commit(); From 1499d46cc367485da11e20a0f98b4ab34d203cba Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Wed, 20 Mar 2024 10:37:48 +0100 Subject: [PATCH 10/31] this framework needs own function to stop core --- src/MF_CustomDevice/CustomDevice.cpp | 29 +++++++++++++--------------- src/MF_CustomDevice/CustomDevice.h | 4 +--- src/MF_Modules/MFEEPROM.cpp | 9 ++------- src/MF_Modules/MFEEPROM.h | 21 ++++---------------- 4 files changed, 20 insertions(+), 43 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 78d87fbb..98a194cd 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -14,7 +14,9 @@ namespace CustomDevice MFCustomDevice *customDevice; uint8_t customDeviceRegistered = 0; uint8_t maxCustomDevices = 0; - char *output; +#if defined(USE_2ND_CORE) + char payload[SERIAL_RX_BUFFER_SIZE]; +#endif bool setupArray(uint16_t count) { @@ -82,21 +84,20 @@ namespace CustomDevice int16_t device = cmdMessenger.readInt16Arg(); // get the device number if (device >= customDeviceRegistered) // and do nothing if this device is not registered return; - int16_t messageID = cmdMessenger.readInt16Arg(); // get the messageID number - output = cmdMessenger.readStringArg(); // get the pointer to the new raw string - cmdMessenger.unescape(output); // and unescape the string if escape characters are used - + int16_t messageID = cmdMessenger.readInt16Arg(); // get the messageID number + char* output = cmdMessenger.readStringArg(); // get the pointer to the new raw string + cmdMessenger.unescape(output); // and unescape the string if escape characters are used #if defined(USE_2ND_CORE) - strncpy(payload, output, DSERIAL_RX_BUFFER_SIZE); + strncpy(payload, output, SERIAL_RX_BUFFER_SIZE); // ######################################################################### // Communication with Core1 - // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### multicore_fifo_push_blocking(CORE1_CMD_SEND); multicore_fifo_push_blocking(device); multicore_fifo_push_blocking(messageID); - //multicore_fifo_push_blocking(payload); + multicore_fifo_push_blocking((uint32_t)&payload); + #else customDevice[device].set(messageID, output); // send the string to your custom device #endif @@ -121,25 +122,21 @@ namespace CustomDevice #if defined(USE_2ND_CORE) void checkDataFromCore0() { - static uint32_t receivedDevice, receivedMessageID, receivedPayload; + static uint32_t receivedDevice, receivedMessageID; + char *receivedPayload; // ######################################################################### // Communication with Core0 - // see https://raspberrypi.github.io/pico-sdk-doxygen/group__multicore__fifo.html // see https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### if (multicore_fifo_rvalid()) { uint32_t dataCore0 = multicore_fifo_pop_blocking(); switch (dataCore0) { - case CORE1_CMD_STOP: - multicore_lockout_victim_init(); - break; - case CORE1_CMD_SEND: receivedDevice = multicore_fifo_pop_blocking(); receivedMessageID = multicore_fifo_pop_blocking(); - //receivedPayload = multicore_fifo_pop_blocking(); - customDevice[receivedDevice].set(receivedMessageID, payload); + receivedPayload = (char*)multicore_fifo_pop_blocking(); + customDevice[receivedDevice].set(receivedMessageID, receivedPayload); break; default: diff --git a/src/MF_CustomDevice/CustomDevice.h b/src/MF_CustomDevice/CustomDevice.h index ecc5c8f1..1821222b 100644 --- a/src/MF_CustomDevice/CustomDevice.h +++ b/src/MF_CustomDevice/CustomDevice.h @@ -5,8 +5,7 @@ // For Communication with Core1 // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore // ######################################################################### -#define CORE1_CMD_STOP (1 << 0) -#define CORE1_CMD_SEND (1 << 1) +#define CORE1_CMD_SEND (1 << 0) #endif namespace CustomDevice @@ -19,6 +18,5 @@ namespace CustomDevice void PowerSave(bool state); #if defined(USE_2ND_CORE) void init_2ndCore(); - char payload[SERIAL_RX_BUFFER_SIZE]; #endif } \ No newline at end of file diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index 29401ba6..83d968d0 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -35,16 +35,11 @@ bool MFEEPROM::write_byte(uint16_t adr, const uint8_t data) EEPROM.write(adr, data); #if defined(ARDUINO_ARCH_RP2040) #if defined(USE_2ND_CORE) - // ######################################################################### - // Communication with Core1 - // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore - // ######################################################################### - multicore_fifo_push_blocking(CORE1_CMD_STOP); - multicore_lockout_start_blocking(); + rp2040.idleOtherCore(); #endif EEPROM.commit(); #if defined(USE_2ND_CORE) - multicore_lockout_end_blocking(); + rp2040.resumeOtherCore(); #endif #endif return true; diff --git a/src/MF_Modules/MFEEPROM.h b/src/MF_Modules/MFEEPROM.h index ca1316cb..c15dec0e 100644 --- a/src/MF_Modules/MFEEPROM.h +++ b/src/MF_Modules/MFEEPROM.h @@ -7,9 +7,6 @@ #pragma once #include -#if defined(USE_2ND_CORE) -#include "CustomDevice.h" -#endif class MFEEPROM { @@ -49,16 +46,11 @@ class MFEEPROM EEPROM.put(adr, t); #if defined(ARDUINO_ARCH_RP2040) #if defined(USE_2ND_CORE) - // ######################################################################### - // Communication with Core1 - // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore - // ######################################################################### - multicore_fifo_push_blocking(CORE1_CMD_STOP); - multicore_lockout_start_blocking(); + rp2040.idleOtherCore(); #endif EEPROM.commit(); #if defined(USE_2ND_CORE) - multicore_lockout_end_blocking(); + rp2040.resumeOtherCore(); #endif #endif return true; @@ -73,16 +65,11 @@ class MFEEPROM } #if defined(ARDUINO_ARCH_RP2040) #if defined(USE_2ND_CORE) - // ######################################################################### - // Communication with Core1 - // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore - // ######################################################################### - multicore_fifo_push_blocking(CORE1_CMD_STOP); - multicore_lockout_start_blocking(); + rp2040.idleOtherCore(); #endif EEPROM.commit(); #if defined(USE_2ND_CORE) - multicore_lockout_end_blocking(); + rp2040.resumeOtherCore(); #endif #endif return true; From 773c72f831781d0d6f543fe8bd85a09de211d7bd Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Thu, 21 Mar 2024 11:02:56 +0100 Subject: [PATCH 11/31] running version --- src/MF_CustomDevice/CustomDevice.cpp | 67 +++++++++------------------- src/MF_CustomDevice/CustomDevice.h | 11 ----- src/MF_Modules/MFEEPROM.cpp | 7 --- src/MF_Modules/MFEEPROM.h | 12 ----- src/mobiflight.cpp | 3 -- 5 files changed, 20 insertions(+), 80 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 98a194cd..8d4eceec 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -1,6 +1,9 @@ #include "mobiflight.h" #include "CustomDevice.h" #include "MFCustomDevice.h" +#if defined(USE_2ND_CORE) +#include "pico/util/queue.h" +#endif /* ********************************************************************************** Normally nothing has to be changed in this file @@ -89,14 +92,9 @@ namespace CustomDevice cmdMessenger.unescape(output); // and unescape the string if escape characters are used #if defined(USE_2ND_CORE) strncpy(payload, output, SERIAL_RX_BUFFER_SIZE); - // ######################################################################### - // Communication with Core1 - // https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore - // ######################################################################### - multicore_fifo_push_blocking(CORE1_CMD_SEND); - multicore_fifo_push_blocking(device); - multicore_fifo_push_blocking(messageID); - multicore_fifo_push_blocking((uint32_t)&payload); + rp2040.fifo.push(device); + rp2040.fifo.push(messageID); + rp2040.fifo.push((uint32_t)&payload); #else customDevice[device].set(messageID, output); // send the string to your custom device @@ -118,46 +116,21 @@ namespace CustomDevice customDevice[i].set(MESSAGEID_POWERSAVINGMODE, "0"); } } +} // end of namespace -#if defined(USE_2ND_CORE) - void checkDataFromCore0() - { - static uint32_t receivedDevice, receivedMessageID; - char *receivedPayload; - // ######################################################################### - // Communication with Core0 - // see https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore - // ######################################################################### - if (multicore_fifo_rvalid()) { - uint32_t dataCore0 = multicore_fifo_pop_blocking(); - switch (dataCore0) - { - case CORE1_CMD_SEND: - receivedDevice = multicore_fifo_pop_blocking(); - receivedMessageID = multicore_fifo_pop_blocking(); - receivedPayload = (char*)multicore_fifo_pop_blocking(); - customDevice[receivedDevice].set(receivedMessageID, receivedPayload); - break; - - default: - break; - } - } - } +void setup1() { + // Nothing ToDo +} - void loop_2ndCore() - { - while(1) - { - checkDataFromCore0(); +void loop1() { + int32_t device, messageID; + char *payload; + while (1) { + if (rp2040.fifo.available() == 3) { + device = rp2040.fifo.pop(); + messageID = rp2040.fifo.pop(); + payload = (char*)rp2040.fifo.pop(); + CustomDevice::customDevice[device].set(messageID, payload); } } - - void init_2ndCore() - { - multicore_launch_core1(loop_2ndCore); - } - -#endif - -} // end of namespace +} diff --git a/src/MF_CustomDevice/CustomDevice.h b/src/MF_CustomDevice/CustomDevice.h index 1821222b..1fcb5fdf 100644 --- a/src/MF_CustomDevice/CustomDevice.h +++ b/src/MF_CustomDevice/CustomDevice.h @@ -1,13 +1,5 @@ #pragma once -#if defined(ARDUINO_ARCH_RP2040) && defined(USE_2ND_CORE) -// ######################################################################### -// For Communication with Core1 -// https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#pico_multicore -// ######################################################################### -#define CORE1_CMD_SEND (1 << 0) -#endif - namespace CustomDevice { bool setupArray(uint16_t count); @@ -16,7 +8,4 @@ namespace CustomDevice void update(); void OnSet(); void PowerSave(bool state); -#if defined(USE_2ND_CORE) - void init_2ndCore(); -#endif } \ No newline at end of file diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index 83d968d0..ab485b88 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -34,15 +34,8 @@ bool MFEEPROM::write_byte(uint16_t adr, const uint8_t data) if (adr >= _eepromLength) return false; EEPROM.write(adr, data); #if defined(ARDUINO_ARCH_RP2040) -#if defined(USE_2ND_CORE) - rp2040.idleOtherCore(); -#endif EEPROM.commit(); -#if defined(USE_2ND_CORE) - rp2040.resumeOtherCore(); -#endif #endif return true; } - // MFEEPROM.cpp \ No newline at end of file diff --git a/src/MF_Modules/MFEEPROM.h b/src/MF_Modules/MFEEPROM.h index c15dec0e..92fe7328 100644 --- a/src/MF_Modules/MFEEPROM.h +++ b/src/MF_Modules/MFEEPROM.h @@ -45,13 +45,7 @@ class MFEEPROM if (adr + sizeof(T) > _eepromLength) return false; EEPROM.put(adr, t); #if defined(ARDUINO_ARCH_RP2040) -#if defined(USE_2ND_CORE) - rp2040.idleOtherCore(); -#endif EEPROM.commit(); -#if defined(USE_2ND_CORE) - rp2040.resumeOtherCore(); -#endif #endif return true; } @@ -64,13 +58,7 @@ class MFEEPROM EEPROM.put(adr + i, t[i]); } #if defined(ARDUINO_ARCH_RP2040) -#if defined(USE_2ND_CORE) - rp2040.idleOtherCore(); -#endif EEPROM.commit(); -#if defined(USE_2ND_CORE) - rp2040.resumeOtherCore(); -#endif #endif return true; } diff --git a/src/mobiflight.cpp b/src/mobiflight.cpp index 690a5301..45ebae74 100644 --- a/src/mobiflight.cpp +++ b/src/mobiflight.cpp @@ -163,9 +163,6 @@ void setup() cmdMessenger.printLfCr(); ResetBoard(); initPollIntervals(); -#if defined(USE_2ND_CORE) - CustomDevice::init_2ndCore(); -#endif } // ************************************************************ From 62148f72a8e505bb76113d391fe40fe622b4fa82 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Thu, 21 Mar 2024 11:17:23 +0100 Subject: [PATCH 12/31] hints added to transfer a function to 2nd core --- src/MF_CustomDevice/CustomDevice.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 8d4eceec..c5748055 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -92,6 +92,7 @@ namespace CustomDevice cmdMessenger.unescape(output); // and unescape the string if escape characters are used #if defined(USE_2ND_CORE) strncpy(payload, output, SERIAL_RX_BUFFER_SIZE); + //rp2040.fifo.push((uintptr_t) &customDevice[device].set); rp2040.fifo.push(device); rp2040.fifo.push(messageID); rp2040.fifo.push((uint32_t)&payload); @@ -127,9 +128,11 @@ void loop1() { char *payload; while (1) { if (rp2040.fifo.available() == 3) { + //int32_t (*func)() = (int32_t(*)()) rp2040.fifo.pop(); device = rp2040.fifo.pop(); messageID = rp2040.fifo.pop(); payload = (char*)rp2040.fifo.pop(); + //(*func)(messageID, payload); CustomDevice::customDevice[device].set(messageID, payload); } } From 8b1d9034e5e7ff09c32860fd93bd3244aa088007 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Thu, 21 Mar 2024 11:21:53 +0100 Subject: [PATCH 13/31] not required include file deleted --- src/MF_CustomDevice/CustomDevice.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index c5748055..0514b0fc 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -1,9 +1,6 @@ #include "mobiflight.h" #include "CustomDevice.h" #include "MFCustomDevice.h" -#if defined(USE_2ND_CORE) -#include "pico/util/queue.h" -#endif /* ********************************************************************************** Normally nothing has to be changed in this file From bfe32b538f7a0ae1d850b1a8c063c25f19b1ee65 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Thu, 21 Mar 2024 11:23:49 +0100 Subject: [PATCH 14/31] reformatting --- src/MF_Modules/MFEEPROM.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index ab485b88..ee8644d7 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -34,7 +34,7 @@ bool MFEEPROM::write_byte(uint16_t adr, const uint8_t data) if (adr >= _eepromLength) return false; EEPROM.write(adr, data); #if defined(ARDUINO_ARCH_RP2040) - EEPROM.commit(); + EEPROM.commit(); #endif return true; } From 47c6179f663824eb3300a30d1818fc2d066bf42d Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Thu, 21 Mar 2024 11:24:29 +0100 Subject: [PATCH 15/31] reformatting --- src/MF_Modules/MFEEPROM.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/MF_Modules/MFEEPROM.cpp b/src/MF_Modules/MFEEPROM.cpp index ee8644d7..7d63bfd5 100644 --- a/src/MF_Modules/MFEEPROM.cpp +++ b/src/MF_Modules/MFEEPROM.cpp @@ -38,4 +38,5 @@ bool MFEEPROM::write_byte(uint16_t adr, const uint8_t data) #endif return true; } + // MFEEPROM.cpp \ No newline at end of file From 4729004d3e7605412c516bd6491f375d42b1dbb8 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Thu, 21 Mar 2024 15:25:04 +0100 Subject: [PATCH 16/31] add include file for running 2nd core --- src/MF_CustomDevice/CustomDevice.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 0514b0fc..0d083e58 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -1,6 +1,9 @@ #include "mobiflight.h" #include "CustomDevice.h" #include "MFCustomDevice.h" +#if defined(USE_2ND_CORE) +#include +#endif /* ********************************************************************************** Normally nothing has to be changed in this file @@ -93,7 +96,6 @@ namespace CustomDevice rp2040.fifo.push(device); rp2040.fifo.push(messageID); rp2040.fifo.push((uint32_t)&payload); - #else customDevice[device].set(messageID, output); // send the string to your custom device #endif From efac257a315c9d37379f1c7e86be3b06a37584d9 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Fri, 22 Mar 2024 06:26:30 +0100 Subject: [PATCH 17/31] 2nd core functions only if defined --- src/MF_CustomDevice/CustomDevice.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 0d083e58..cb15a6a3 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -118,6 +118,7 @@ namespace CustomDevice } } // end of namespace +#if defined(USE_2ND_CORE) void setup1() { // Nothing ToDo } @@ -126,7 +127,7 @@ void loop1() { int32_t device, messageID; char *payload; while (1) { - if (rp2040.fifo.available() == 3) { + if (rp2040.fifo.available() > 2) { //int32_t (*func)() = (int32_t(*)()) rp2040.fifo.pop(); device = rp2040.fifo.pop(); messageID = rp2040.fifo.pop(); @@ -136,3 +137,4 @@ void loop1() { } } } +#endif From b7cdff0a4e39e349460539d6199aee5a8a550f15 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Fri, 22 Mar 2024 08:36:09 +0100 Subject: [PATCH 18/31] reformatting --- src/MF_CustomDevice/CustomDevice.cpp | 34 +++++++++++++++------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index cb15a6a3..38c21527 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -87,17 +87,17 @@ namespace CustomDevice int16_t device = cmdMessenger.readInt16Arg(); // get the device number if (device >= customDeviceRegistered) // and do nothing if this device is not registered return; - int16_t messageID = cmdMessenger.readInt16Arg(); // get the messageID number - char* output = cmdMessenger.readStringArg(); // get the pointer to the new raw string - cmdMessenger.unescape(output); // and unescape the string if escape characters are used + int16_t messageID = cmdMessenger.readInt16Arg(); // get the messageID number + char *output = cmdMessenger.readStringArg(); // get the pointer to the new raw string + cmdMessenger.unescape(output); // and unescape the string if escape characters are used #if defined(USE_2ND_CORE) - strncpy(payload, output, SERIAL_RX_BUFFER_SIZE); - //rp2040.fifo.push((uintptr_t) &customDevice[device].set); - rp2040.fifo.push(device); - rp2040.fifo.push(messageID); - rp2040.fifo.push((uint32_t)&payload); + strncpy(payload, output, SERIAL_RX_BUFFER_SIZE); + // rp2040.fifo.push((uintptr_t) &customDevice[device].set); + rp2040.fifo.push(device); + rp2040.fifo.push(messageID); + rp2040.fifo.push((uint32_t)&payload); #else - customDevice[device].set(messageID, output); // send the string to your custom device + customDevice[device].set(messageID, output); // send the string to your custom device #endif } @@ -119,19 +119,21 @@ namespace CustomDevice } // end of namespace #if defined(USE_2ND_CORE) -void setup1() { +void setup1() +{ // Nothing ToDo } -void loop1() { +void loop1() +{ int32_t device, messageID; - char *payload; + char *payload; while (1) { if (rp2040.fifo.available() > 2) { - //int32_t (*func)() = (int32_t(*)()) rp2040.fifo.pop(); - device = rp2040.fifo.pop(); - messageID = rp2040.fifo.pop(); - payload = (char*)rp2040.fifo.pop(); + // int32_t (*func)() = (int32_t(*)()) rp2040.fifo.pop(); + device = (int16_t)rp2040.fifo.pop(); + messageID = (int16_t)rp2040.fifo.pop(); + payload = (char *)rp2040.fifo.pop(); //(*func)(messageID, payload); CustomDevice::customDevice[device].set(messageID, payload); } From b5932b7a2d08f81d10aa842dad92ef6bd2e90a42 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Fri, 22 Mar 2024 08:50:34 +0100 Subject: [PATCH 19/31] add comments --- src/MF_CustomDevice/CustomDevice.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 38c21527..3f505392 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -92,7 +92,7 @@ namespace CustomDevice cmdMessenger.unescape(output); // and unescape the string if escape characters are used #if defined(USE_2ND_CORE) strncpy(payload, output, SERIAL_RX_BUFFER_SIZE); - // rp2040.fifo.push((uintptr_t) &customDevice[device].set); + // rp2040.fifo.push((uintptr_t) &customDevice[device].set); // Hmhm, how to get the function pointer to a function from class?? rp2040.fifo.push(device); rp2040.fifo.push(messageID); rp2040.fifo.push((uint32_t)&payload); @@ -119,6 +119,13 @@ namespace CustomDevice } // end of namespace #if defined(USE_2ND_CORE) +/* ********************************************************************************** + This will run the set() function from the custom device on the 2nd core + Be aware NOT to use the function calls from the Pico SDK! + Only use the functions from the used framework from EarlePhilHower + If you mix them up it will give undefined behaviour and strange effects + see https://arduino-pico.readthedocs.io/en/latest/multicore.html +********************************************************************************** */ void setup1() { // Nothing ToDo @@ -130,11 +137,11 @@ void loop1() char *payload; while (1) { if (rp2040.fifo.available() > 2) { - // int32_t (*func)() = (int32_t(*)()) rp2040.fifo.pop(); + // int32_t (*func)(int16_t, char*) = (int32_t(*)(int16_t, char*)) rp2040.fifo.pop(); device = (int16_t)rp2040.fifo.pop(); messageID = (int16_t)rp2040.fifo.pop(); payload = (char *)rp2040.fifo.pop(); - //(*func)(messageID, payload); + // (*func)(messageID, payload); CustomDevice::customDevice[device].set(messageID, payload); } } From 6342be14bacbb648f257af5f2fd8943aeed4bc3b Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Sat, 23 Mar 2024 06:56:46 +0100 Subject: [PATCH 20/31] wait for 2nd core to be ready --- src/MF_CustomDevice/CustomDevice.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 3f505392..24b91e5a 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -92,6 +92,10 @@ namespace CustomDevice cmdMessenger.unescape(output); // and unescape the string if escape characters are used #if defined(USE_2ND_CORE) strncpy(payload, output, SERIAL_RX_BUFFER_SIZE); + while (!rp2040.fifo.available()) { + // Just wait for core 1 to be ready + } + rp2040.fifo.pop(); // rp2040.fifo.push((uintptr_t) &customDevice[device].set); // Hmhm, how to get the function pointer to a function from class?? rp2040.fifo.push(device); rp2040.fifo.push(messageID); @@ -135,6 +139,8 @@ void loop1() { int32_t device, messageID; char *payload; + // send ready "ready" message to core 0 + rp2040.fifo.push(true); while (1) { if (rp2040.fifo.available() > 2) { // int32_t (*func)(int16_t, char*) = (int32_t(*)(int16_t, char*)) rp2040.fifo.pop(); @@ -143,6 +149,7 @@ void loop1() payload = (char *)rp2040.fifo.pop(); // (*func)(messageID, payload); CustomDevice::customDevice[device].set(messageID, payload); + rp2040.fifo.push(true); } } } From 216e010ad01af83899b475883725ed25df037a28 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Sat, 23 Mar 2024 07:50:37 +0100 Subject: [PATCH 21/31] copy payload after core 1 is ready --- src/MF_CustomDevice/CustomDevice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 24b91e5a..d97a3f50 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -91,10 +91,10 @@ namespace CustomDevice char *output = cmdMessenger.readStringArg(); // get the pointer to the new raw string cmdMessenger.unescape(output); // and unescape the string if escape characters are used #if defined(USE_2ND_CORE) - strncpy(payload, output, SERIAL_RX_BUFFER_SIZE); while (!rp2040.fifo.available()) { // Just wait for core 1 to be ready } + strncpy(payload, output, SERIAL_RX_BUFFER_SIZE); rp2040.fifo.pop(); // rp2040.fifo.push((uintptr_t) &customDevice[device].set); // Hmhm, how to get the function pointer to a function from class?? rp2040.fifo.push(device); @@ -139,7 +139,7 @@ void loop1() { int32_t device, messageID; char *payload; - // send ready "ready" message to core 0 + // send "ready" message to core 0 rp2040.fifo.push(true); while (1) { if (rp2040.fifo.available() > 2) { From dcd7af37b365eba9f3a65d5cef9c6e8bb47257c3 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Mon, 8 Apr 2024 09:11:44 +0200 Subject: [PATCH 22/31] custom device update() must also run on 2nd core --- src/MF_CustomDevice/CustomDevice.cpp | 49 +++++++++++++++++++--------- src/MF_CustomDevice/CustomDevice.h | 3 ++ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index d97a3f50..94937f17 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -69,7 +69,14 @@ namespace CustomDevice void update() { for (int i = 0; i != customDeviceRegistered; i++) { +#if defined(USE_2ND_CORE) + // wait for 2nd core + rp2040.fifo.pop(); + rp2040.fifo.push(FUNCTION_UPDATE); + rp2040.fifo.push(i); +#else customDevice[i].update(); +#endif } } @@ -91,12 +98,13 @@ namespace CustomDevice char *output = cmdMessenger.readStringArg(); // get the pointer to the new raw string cmdMessenger.unescape(output); // and unescape the string if escape characters are used #if defined(USE_2ND_CORE) - while (!rp2040.fifo.available()) { - // Just wait for core 1 to be ready - } + // copy the message, could get be overwritten from the next message while processing on 2nd core strncpy(payload, output, SERIAL_RX_BUFFER_SIZE); + // wait for 2nd core rp2040.fifo.pop(); - // rp2040.fifo.push((uintptr_t) &customDevice[device].set); // Hmhm, how to get the function pointer to a function from class?? + // Hmhm, how to get the function pointer to a function from class?? + // rp2040.fifo.push((uintptr_t) &customDevice[device].set); + rp2040.fifo.push(FUNCTION_ONSET); rp2040.fifo.push(device); rp2040.fifo.push(messageID); rp2040.fifo.push((uint32_t)&payload); @@ -132,24 +140,35 @@ namespace CustomDevice ********************************************************************************** */ void setup1() { - // Nothing ToDo + // send "ready" message to 1st core + rp2040.fifo.push(true); } void loop1() { int32_t device, messageID; char *payload; - // send "ready" message to core 0 - rp2040.fifo.push(true); + uint8_t function = 0; + while (1) { - if (rp2040.fifo.available() > 2) { - // int32_t (*func)(int16_t, char*) = (int32_t(*)(int16_t, char*)) rp2040.fifo.pop(); - device = (int16_t)rp2040.fifo.pop(); - messageID = (int16_t)rp2040.fifo.pop(); - payload = (char *)rp2040.fifo.pop(); - // (*func)(messageID, payload); - CustomDevice::customDevice[device].set(messageID, payload); - rp2040.fifo.push(true); + if (rp2040.fifo.available()) { + function = (uint8_t)rp2040.fifo.pop(); + if (function == FUNCTION_ONSET) { + // Hmhm, how to get the function pointer to a function from class?? + // int32_t (*func)(int16_t, char*) = (int32_t(*)(int16_t, char*)) rp2040.fifo.pop(); + device = (int16_t)rp2040.fifo.pop(); + messageID = (int16_t)rp2040.fifo.pop(); + payload = (char *)rp2040.fifo.pop(); + // (*func)(messageID, payload); + CustomDevice::customDevice[device].set(messageID, payload); + // send ready for next message to 1st core + rp2040.fifo.push(true); + } else if (function == FUNCTION_UPDATE) { + device = (int16_t)rp2040.fifo.pop(); + CustomDevice::customDevice[device].update(); + // send ready for next message to 1st core + rp2040.fifo.push(true); + } } } } diff --git a/src/MF_CustomDevice/CustomDevice.h b/src/MF_CustomDevice/CustomDevice.h index 1fcb5fdf..cce962cd 100644 --- a/src/MF_CustomDevice/CustomDevice.h +++ b/src/MF_CustomDevice/CustomDevice.h @@ -2,6 +2,9 @@ namespace CustomDevice { +#define FUNCTION_ONSET 1 +#define FUNCTION_UPDATE 2 + bool setupArray(uint16_t count); void Add(uint16_t adrPin, uint16_t adrType, uint16_t adrConfig); void Clear(); From 5baba1642803f54ac552f6a8b96dc900ce0c9cdd Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Mon, 8 Apr 2024 10:58:00 +0200 Subject: [PATCH 23/31] don't call update() function if required from 1st core, always within 2nd core --- src/MF_CustomDevice/CustomDevice.cpp | 48 ++++++++++++++-------------- src/MF_CustomDevice/CustomDevice.h | 3 -- src/mobiflight.cpp | 2 +- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 94937f17..3944e95b 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -69,12 +69,7 @@ namespace CustomDevice void update() { for (int i = 0; i != customDeviceRegistered; i++) { -#if defined(USE_2ND_CORE) - // wait for 2nd core - rp2040.fifo.pop(); - rp2040.fifo.push(FUNCTION_UPDATE); - rp2040.fifo.push(i); -#else +#if !defined(USE_2ND_CORE) customDevice[i].update(); #endif } @@ -104,7 +99,6 @@ namespace CustomDevice rp2040.fifo.pop(); // Hmhm, how to get the function pointer to a function from class?? // rp2040.fifo.push((uintptr_t) &customDevice[device].set); - rp2040.fifo.push(FUNCTION_ONSET); rp2040.fifo.push(device); rp2040.fifo.push(messageID); rp2040.fifo.push((uint32_t)&payload); @@ -148,28 +142,34 @@ void loop1() { int32_t device, messageID; char *payload; - uint8_t function = 0; + uint32_t lastMillis = 0; while (1) { if (rp2040.fifo.available()) { - function = (uint8_t)rp2040.fifo.pop(); - if (function == FUNCTION_ONSET) { - // Hmhm, how to get the function pointer to a function from class?? - // int32_t (*func)(int16_t, char*) = (int32_t(*)(int16_t, char*)) rp2040.fifo.pop(); - device = (int16_t)rp2040.fifo.pop(); - messageID = (int16_t)rp2040.fifo.pop(); - payload = (char *)rp2040.fifo.pop(); - // (*func)(messageID, payload); - CustomDevice::customDevice[device].set(messageID, payload); - // send ready for next message to 1st core - rp2040.fifo.push(true); - } else if (function == FUNCTION_UPDATE) { - device = (int16_t)rp2040.fifo.pop(); - CustomDevice::customDevice[device].update(); - // send ready for next message to 1st core - rp2040.fifo.push(true); + // Hmhm, how to get the function pointer to a function from class?? + // int32_t (*func)(int16_t, char*) = (int32_t(*)(int16_t, char*)) rp2040.fifo.pop(); + device = (int16_t)rp2040.fifo.pop(); + messageID = (int16_t)rp2040.fifo.pop(); + payload = (char *)rp2040.fifo.pop(); + // (*func)(messageID, payload); + CustomDevice::customDevice[device].set(messageID, payload); + // send ready for next message to 1st core + rp2040.fifo.push(true); + } +#if defined(MF_CUSTOMDEVICE_HAS_UPDATE) +#ifdef MF_CUSTOMDEVICE_POLL_MS + if (millis() - lastMillis >= MF_CUSTOMDEVICE_POLL_MS) { + for (int i = 0; i != CustomDevice::customDeviceRegistered; i++) { + CustomDevice::customDevice[i].update(); } + lastMillis = millis(); } +#else + for (int i = 0; i != CustomDevice::customDeviceRegistered; i++) { + CustomDevice::customDevice[i].update(); + } +#endif +#endif } } #endif diff --git a/src/MF_CustomDevice/CustomDevice.h b/src/MF_CustomDevice/CustomDevice.h index cce962cd..1fcb5fdf 100644 --- a/src/MF_CustomDevice/CustomDevice.h +++ b/src/MF_CustomDevice/CustomDevice.h @@ -2,9 +2,6 @@ namespace CustomDevice { -#define FUNCTION_ONSET 1 -#define FUNCTION_UPDATE 2 - bool setupArray(uint16_t count); void Add(uint16_t adrPin, uint16_t adrType, uint16_t adrConfig); void Clear(); diff --git a/src/mobiflight.cpp b/src/mobiflight.cpp index 45ebae74..da58a4c6 100644 --- a/src/mobiflight.cpp +++ b/src/mobiflight.cpp @@ -204,7 +204,7 @@ void loop() timedUpdate(DigInMux::read, &lastUpdate.DigInMux, MF_INMUX_POLL_MS); #endif -#if MF_CUSTOMDEVICE_SUPPORT == 1 && defined(MF_CUSTOMDEVICE_HAS_UPDATE) +#if MF_CUSTOMDEVICE_SUPPORT == 1 && defined(MF_CUSTOMDEVICE_HAS_UPDATE) && !defined(USE_2ND_CORE) #ifdef MF_CUSTOMDEVICE_POLL_MS timedUpdate(CustomDevice::update, &lastUpdate.CustomDevice, MF_CUSTOMDEVICE_POLL_MS); #else From 87732a5670415305fc3006adc8bcae1ba5777063 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 9 Apr 2024 15:26:22 +0200 Subject: [PATCH 24/31] use stepper on second core if defined --- src/MF_Stepper/Stepper.cpp | 78 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/MF_Stepper/Stepper.cpp b/src/MF_Stepper/Stepper.cpp index c751a3b9..de29c1e2 100644 --- a/src/MF_Stepper/Stepper.cpp +++ b/src/MF_Stepper/Stepper.cpp @@ -7,6 +7,9 @@ #include "mobiflight.h" #include "MFStepper.h" #include "Stepper.h" +#if defined(STEPPER_ON_2ND_CORE) +#include +#endif namespace Stepper { @@ -14,6 +17,12 @@ namespace Stepper uint8_t steppersRegistered = 0; uint8_t maxSteppers = 0; + enum { + FUNC_MOVETO = 1, + FUNC_ZETZERO, + FUNC_SPEEDACCEL + }; + bool setupArray(uint16_t count) { if (!FitInMemory(sizeof(MFStepper) * count)) @@ -61,7 +70,16 @@ namespace Stepper if (stepper >= steppersRegistered) return; +#if defined(STEPPER_ON_2ND_CORE) + // wait for 2nd core + rp2040.fifo.pop(); + rp2040.fifo.push(FUNC_MOVETO); + rp2040.fifo.push(stepper); + rp2040.fifo.push(newPos); + rp2040.fifo.push(false); +#else steppers[stepper].moveTo(newPos); +#endif } void OnReset() @@ -79,7 +97,16 @@ namespace Stepper if (stepper >= steppersRegistered) return; +#if defined(STEPPER_ON_2ND_CORE) + // wait for 2nd core + rp2040.fifo.pop(); + rp2040.fifo.push(FUNC_ZETZERO); + rp2040.fifo.push(stepper); + rp2040.fifo.push(false); + rp2040.fifo.push(false); +#else steppers[stepper].setZero(); +#endif } void OnSetSpeedAccel() @@ -90,15 +117,25 @@ namespace Stepper if (stepper >= steppersRegistered) return; +#if defined(STEPPER_ON_2ND_CORE) + rp2040.fifo.pop(); // wait for 2nd core + rp2040.fifo.push(FUNC_SPEEDACCEL); + rp2040.fifo.push(stepper); + rp2040.fifo.push(maxSpeed); + rp2040.fifo.push(maxAccel); +#else steppers[stepper].setMaxSpeed(maxSpeed); steppers[stepper].setAcceleration(maxAccel); +#endif } void update() { +#if !defined(STEPPER_ON_2ND_CORE) for (uint8_t i = 0; i < steppersRegistered; i++) { steppers[i].update(); } +#endif } void PowerSave(bool state) @@ -110,4 +147,45 @@ namespace Stepper } // namespace +#if defined(STEPPER_ON_2ND_CORE) +/* ********************************************************************************** + This will run the set() function from the custom device on the 2nd core + Be aware NOT to use the function calls from the Pico SDK! + Only use the functions from the used framework from EarlePhilHower + If you mix them up it will give undefined behaviour and strange effects + see https://arduino-pico.readthedocs.io/en/latest/multicore.html +********************************************************************************** */ +void setup1() +{ + rp2040.fifo.push(true); // inform core 1 to be ready +} + +void loop1() +{ + uint8_t command = 0; + uint32_t stepper, param1, param2; + + while (1) { + if (rp2040.fifo.available()) { + command = (int16_t)rp2040.fifo.pop(); + stepper = (int16_t)rp2040.fifo.pop(); + param1 = (int16_t)rp2040.fifo.pop(); + param2 = (int16_t)rp2040.fifo.pop(); + if (command == Stepper::FUNC_MOVETO) { + Stepper::steppers[stepper].moveTo(param1); + } else if (command == Stepper::FUNC_ZETZERO) { + Stepper::steppers[stepper].setZero(); + } else if (command == Stepper::FUNC_SPEEDACCEL) { + Stepper::steppers[stepper].setMaxSpeed(param1); + Stepper::steppers[stepper].setAcceleration(param2); + } + rp2040.fifo.push(true); // inform core 1 to be ready for next command + } + for (uint8_t i = 0; i < Stepper::steppersRegistered; ++i) { + Stepper::steppers[i].update(); + } + } +} +#endif + // Stepper.cpp From c841e94c952d605273119d16f978a16abf92bba2 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 9 Apr 2024 17:50:00 +0200 Subject: [PATCH 25/31] fixed wrong types of variables in loop1() --- src/MF_Stepper/Stepper.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/MF_Stepper/Stepper.cpp b/src/MF_Stepper/Stepper.cpp index de29c1e2..47960800 100644 --- a/src/MF_Stepper/Stepper.cpp +++ b/src/MF_Stepper/Stepper.cpp @@ -162,15 +162,15 @@ void setup1() void loop1() { - uint8_t command = 0; - uint32_t stepper, param1, param2; + uint8_t command, stepper; + int32_t param1, param2; while (1) { if (rp2040.fifo.available()) { - command = (int16_t)rp2040.fifo.pop(); - stepper = (int16_t)rp2040.fifo.pop(); - param1 = (int16_t)rp2040.fifo.pop(); - param2 = (int16_t)rp2040.fifo.pop(); + command = (uint8_t)rp2040.fifo.pop(); + stepper = (uint8_t)rp2040.fifo.pop(); + param1 = (int32_t)rp2040.fifo.pop(); + param2 = (int32_t)rp2040.fifo.pop(); if (command == Stepper::FUNC_MOVETO) { Stepper::steppers[stepper].moveTo(param1); } else if (command == Stepper::FUNC_ZETZERO) { From 7dd6d098b432e3339aba494c237a9d95de19f6b1 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Tue, 9 Apr 2024 17:58:28 +0200 Subject: [PATCH 26/31] better structure within loop1() --- src/MF_Stepper/Stepper.cpp | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/MF_Stepper/Stepper.cpp b/src/MF_Stepper/Stepper.cpp index 47960800..ed2a5c4d 100644 --- a/src/MF_Stepper/Stepper.cpp +++ b/src/MF_Stepper/Stepper.cpp @@ -16,12 +16,13 @@ namespace Stepper MFStepper *steppers; uint8_t steppersRegistered = 0; uint8_t maxSteppers = 0; - +#if defined(STEPPER_ON_2ND_CORE) enum { FUNC_MOVETO = 1, FUNC_ZETZERO, FUNC_SPEEDACCEL }; +#endif bool setupArray(uint16_t count) { @@ -166,23 +167,23 @@ void loop1() int32_t param1, param2; while (1) { - if (rp2040.fifo.available()) { - command = (uint8_t)rp2040.fifo.pop(); - stepper = (uint8_t)rp2040.fifo.pop(); - param1 = (int32_t)rp2040.fifo.pop(); - param2 = (int32_t)rp2040.fifo.pop(); - if (command == Stepper::FUNC_MOVETO) { - Stepper::steppers[stepper].moveTo(param1); - } else if (command == Stepper::FUNC_ZETZERO) { - Stepper::steppers[stepper].setZero(); - } else if (command == Stepper::FUNC_SPEEDACCEL) { - Stepper::steppers[stepper].setMaxSpeed(param1); - Stepper::steppers[stepper].setAcceleration(param2); - } - rp2040.fifo.push(true); // inform core 1 to be ready for next command - } for (uint8_t i = 0; i < Stepper::steppersRegistered; ++i) { Stepper::steppers[i].update(); + if (rp2040.fifo.available()) { + command = (uint8_t)rp2040.fifo.pop(); + stepper = (uint8_t)rp2040.fifo.pop(); + param1 = (int32_t)rp2040.fifo.pop(); + param2 = (int32_t)rp2040.fifo.pop(); + if (command == Stepper::FUNC_MOVETO) { + Stepper::steppers[stepper].moveTo(param1); + } else if (command == Stepper::FUNC_ZETZERO) { + Stepper::steppers[stepper].setZero(); + } else if (command == Stepper::FUNC_SPEEDACCEL) { + Stepper::steppers[stepper].setMaxSpeed(param1); + Stepper::steppers[stepper].setAcceleration(param2); + } + rp2040.fifo.push(true); // inform core 1 to be ready for next command + } } } } From 65d45cad0fcf4415ab63c61218607c7acc438a96 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Wed, 10 Apr 2024 06:35:06 +0200 Subject: [PATCH 27/31] better structure for loop1() --- src/MF_CustomDevice/CustomDevice.cpp | 32 +++++++++++++--------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 3944e95b..a2391e88 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -145,30 +145,28 @@ void loop1() uint32_t lastMillis = 0; while (1) { - if (rp2040.fifo.available()) { - // Hmhm, how to get the function pointer to a function from class?? - // int32_t (*func)(int16_t, char*) = (int32_t(*)(int16_t, char*)) rp2040.fifo.pop(); - device = (int16_t)rp2040.fifo.pop(); - messageID = (int16_t)rp2040.fifo.pop(); - payload = (char *)rp2040.fifo.pop(); - // (*func)(messageID, payload); - CustomDevice::customDevice[device].set(messageID, payload); - // send ready for next message to 1st core - rp2040.fifo.push(true); - } -#if defined(MF_CUSTOMDEVICE_HAS_UPDATE) #ifdef MF_CUSTOMDEVICE_POLL_MS if (millis() - lastMillis >= MF_CUSTOMDEVICE_POLL_MS) { +#endif for (int i = 0; i != CustomDevice::customDeviceRegistered; i++) { +#if defined(MF_CUSTOMDEVICE_HAS_UPDATE) CustomDevice::customDevice[i].update(); +#endif + if (rp2040.fifo.available()) { + // Hmhm, how to get the function pointer to a function from class?? + // int32_t (*func)(int16_t, char*) = (int32_t(*)(int16_t, char*)) rp2040.fifo.pop(); + device = (int16_t)rp2040.fifo.pop(); + messageID = (int16_t)rp2040.fifo.pop(); + payload = (char *)rp2040.fifo.pop(); + // (*func)(messageID, payload); + CustomDevice::customDevice[device].set(messageID, payload); + // send ready for next message to 1st core + rp2040.fifo.push(true); + } } +#ifdef MF_CUSTOMDEVICE_POLL_MS lastMillis = millis(); } -#else - for (int i = 0; i != CustomDevice::customDeviceRegistered; i++) { - CustomDevice::customDevice[i].update(); - } -#endif #endif } } From cd2ba2a2b202404ad0e32c3f3156ca45d1944a4f Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Wed, 10 Apr 2024 06:41:48 +0200 Subject: [PATCH 28/31] better structure in update() --- src/MF_CustomDevice/CustomDevice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index a2391e88..670f385a 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -68,11 +68,11 @@ namespace CustomDevice ********************************************************************************** */ void update() { - for (int i = 0; i != customDeviceRegistered; i++) { #if !defined(USE_2ND_CORE) + for (int i = 0; i != customDeviceRegistered; i++) { customDevice[i].update(); -#endif } +#endif } /* ********************************************************************************** From 5fdc197741d6b65d5fa53fa7189d6f8450c625e3 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Wed, 10 Apr 2024 08:39:01 +0200 Subject: [PATCH 29/31] fixed wrong size of variables --- src/MF_CustomDevice/CustomDevice.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 670f385a..9e1b33f8 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -140,7 +140,8 @@ void setup1() void loop1() { - int32_t device, messageID; + uint8_t device; + int16_t messageID; char *payload; uint32_t lastMillis = 0; @@ -155,7 +156,7 @@ void loop1() if (rp2040.fifo.available()) { // Hmhm, how to get the function pointer to a function from class?? // int32_t (*func)(int16_t, char*) = (int32_t(*)(int16_t, char*)) rp2040.fifo.pop(); - device = (int16_t)rp2040.fifo.pop(); + device = (uint8_t)rp2040.fifo.pop(); messageID = (int16_t)rp2040.fifo.pop(); payload = (char *)rp2040.fifo.pop(); // (*func)(messageID, payload); From ac5d86b6c5a86eb3cafe16f995ec6cd0a561eb11 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Fri, 19 Apr 2024 14:07:49 +0200 Subject: [PATCH 30/31] add a small delay in loop1() to enable stop mode for writing to the EEPROM --- src/MF_CustomDevice/CustomDevice.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/MF_CustomDevice/CustomDevice.cpp b/src/MF_CustomDevice/CustomDevice.cpp index 9e1b33f8..480a69bf 100644 --- a/src/MF_CustomDevice/CustomDevice.cpp +++ b/src/MF_CustomDevice/CustomDevice.cpp @@ -143,9 +143,18 @@ void loop1() uint8_t device; int16_t messageID; char *payload; +#ifdef MF_CUSTOMDEVICE_POLL_MS uint32_t lastMillis = 0; +#endif while (1) { +#ifndef MF_CUSTOMDEVICE_POLL_MS + // For now I don't know the reason why this is required. + // It might be that getting the stop command from the 1st core + // needs some idle time. If it is not used the 1st core stops when + // writing to the EEPROM which stops the 2nd core + delayMicroseconds(1); +#endif #ifdef MF_CUSTOMDEVICE_POLL_MS if (millis() - lastMillis >= MF_CUSTOMDEVICE_POLL_MS) { #endif @@ -153,7 +162,7 @@ void loop1() #if defined(MF_CUSTOMDEVICE_HAS_UPDATE) CustomDevice::customDevice[i].update(); #endif - if (rp2040.fifo.available()) { + if (rp2040.fifo.available() == 3) { // Hmhm, how to get the function pointer to a function from class?? // int32_t (*func)(int16_t, char*) = (int32_t(*)(int16_t, char*)) rp2040.fifo.pop(); device = (uint8_t)rp2040.fifo.pop(); @@ -164,6 +173,7 @@ void loop1() // send ready for next message to 1st core rp2040.fifo.push(true); } + } #ifdef MF_CUSTOMDEVICE_POLL_MS lastMillis = millis(); From 6e7ba0ed48e8b5a7160e99ee3507d495916a4a18 Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Mon, 27 May 2024 21:24:53 +0200 Subject: [PATCH 31/31] not required define deleted --- src/mobiflight.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mobiflight.cpp b/src/mobiflight.cpp index da58a4c6..45ebae74 100644 --- a/src/mobiflight.cpp +++ b/src/mobiflight.cpp @@ -204,7 +204,7 @@ void loop() timedUpdate(DigInMux::read, &lastUpdate.DigInMux, MF_INMUX_POLL_MS); #endif -#if MF_CUSTOMDEVICE_SUPPORT == 1 && defined(MF_CUSTOMDEVICE_HAS_UPDATE) && !defined(USE_2ND_CORE) +#if MF_CUSTOMDEVICE_SUPPORT == 1 && defined(MF_CUSTOMDEVICE_HAS_UPDATE) #ifdef MF_CUSTOMDEVICE_POLL_MS timedUpdate(CustomDevice::update, &lastUpdate.CustomDevice, MF_CUSTOMDEVICE_POLL_MS); #else