Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
14028fb
first changes for using 2nd core
elral Mar 19, 2024
e1a7744
missing include file added
elral Mar 19, 2024
f7b6803
missing include file added
elral Mar 19, 2024
0b70d57
correct order of commit eeprom for 2nd core
elral Mar 19, 2024
df79e5e
fixed eeprom write for Atmels
elral Mar 19, 2024
f0eb027
added 2nd core
elral Mar 19, 2024
814fa49
fixed missing defines
elral Mar 19, 2024
159b2dd
reformatting
elral Mar 20, 2024
be963a0
better structure for communicating with 2nd core
elral Mar 20, 2024
1499d46
this framework needs own function to stop core
elral Mar 20, 2024
773c72f
running version
elral Mar 21, 2024
62148f7
hints added to transfer a function to 2nd core
elral Mar 21, 2024
8b1d903
not required include file deleted
elral Mar 21, 2024
bfe32b5
reformatting
elral Mar 21, 2024
47c6179
reformatting
elral Mar 21, 2024
4729004
add include file for running 2nd core
elral Mar 21, 2024
efac257
2nd core functions only if defined
elral Mar 22, 2024
b7cdff0
reformatting
elral Mar 22, 2024
b5932b7
add comments
elral Mar 22, 2024
6342be1
wait for 2nd core to be ready
elral Mar 23, 2024
216e010
copy payload after core 1 is ready
elral Mar 23, 2024
dcd7af3
custom device update() must also run on 2nd core
elral Apr 8, 2024
5baba16
don't call update() function if required from 1st core, always within…
elral Apr 8, 2024
87732a5
use stepper on second core if defined
elral Apr 9, 2024
c841e94
fixed wrong types of variables in loop1()
elral Apr 9, 2024
7dd6d09
better structure within loop1()
elral Apr 9, 2024
65d45ca
better structure for loop1()
elral Apr 10, 2024
cd2ba2a
better structure in update()
elral Apr 10, 2024
5fdc197
fixed wrong size of variables
elral Apr 10, 2024
ac5d86b
add a small delay in loop1() to enable stop mode for writing to the E…
elral Apr 19, 2024
c05624b
Merge branch 'main' into use_2nd_core
elral May 5, 2024
7befa9a
Merge branch 'main' into use_2nd_core
elral May 6, 2024
2777ddb
Merge branch 'use_2nd_core' of https://github.com/elral/MobiFlight-Fi…
elral May 6, 2024
4832867
Merge branch 'MobiFlight:main' into use_2nd_core
elral May 24, 2024
6e7ba0e
not required define deleted
elral May 27, 2024
ed9b3ec
Merge branch 'main' into use_2nd_core
DocMoebiuz May 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 80 additions & 2 deletions src/MF_CustomDevice/CustomDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "mobiflight.h"
#include "CustomDevice.h"
#include "MFCustomDevice.h"
#if defined(USE_2ND_CORE)
#include <FreeRTOS.h>
#endif

/* **********************************************************************************
Normally nothing has to be changed in this file
Expand All @@ -14,6 +17,9 @@ namespace CustomDevice
MFCustomDevice *customDevice;
uint8_t customDeviceRegistered = 0;
uint8_t maxCustomDevices = 0;
#if defined(USE_2ND_CORE)
char payload[SERIAL_RX_BUFFER_SIZE];
#endif

bool setupArray(uint16_t count)
{
Expand Down Expand Up @@ -62,9 +68,11 @@ namespace CustomDevice
********************************************************************************** */
void update()
{
#if !defined(USE_2ND_CORE)
for (int i = 0; i != customDeviceRegistered; i++) {
customDevice[i].update();
}
#endif
}

/* **********************************************************************************
Expand All @@ -84,7 +92,19 @@ namespace CustomDevice
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
customDevice[device].set(messageID, output); // send the string to your custom device
#if defined(USE_2ND_CORE)
// 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();
// Hmhm, how to get the function pointer to a function from class??
// 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
#endif
}

/* **********************************************************************************
Expand All @@ -102,5 +122,63 @@ namespace CustomDevice
customDevice[i].set(MESSAGEID_POWERSAVINGMODE, (char*)"0");
}
}

} // 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()
{
// send "ready" message to 1st core
rp2040.fifo.push(true);
}

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
for (int i = 0; i != CustomDevice::customDeviceRegistered; i++) {
#if defined(MF_CUSTOMDEVICE_HAS_UPDATE)
CustomDevice::customDevice[i].update();
#endif
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();
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();
}
#endif
}
}
#endif
79 changes: 79 additions & 0 deletions src/MF_Stepper/Stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@
#include "mobiflight.h"
#include "MFStepper.h"
#include "Stepper.h"
#if defined(STEPPER_ON_2ND_CORE)
#include <FreeRTOS.h>
#endif

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)
{
Expand Down Expand Up @@ -61,7 +71,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()
Expand All @@ -79,7 +98,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()
Expand All @@ -90,15 +118,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)
Expand All @@ -110,4 +148,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, stepper;
int32_t param1, param2;

while (1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while (true) would be prettier.

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
}
}
}
}
#endif

// Stepper.cpp