Skip to content

Commit

Permalink
Merge pull request #1581 from lf-lang/arduino-mbed-support
Browse files Browse the repository at this point in the history
Arduino MBED RTOS Support
  • Loading branch information
lhstrh authored Feb 24, 2023
2 parents 7d52516 + a6a1231 commit c04d8d3
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 23 deletions.
1 change: 1 addition & 0 deletions .github/workflows/c-arduino-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ jobs:
arduino-cli core install arduino:megaavr
arduino-cli core install arduino:samd
arduino-cli core install arduino:sam
arduino-cli core install arduino:mbed
- name: Perform Arduino tests for C target with default scheduler
run: ./gradlew test --tests org.lflang.tests.runtime.CArduinoTest.runArduinoTests
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:

# Run the C Arduino integration tests.
c-arduino-tests:
uses: lf-lang/lingua-franca/.github/workflows/c-arduino-tests.yml@master
uses: lf-lang/lingua-franca/.github/workflows/c-arduino-tests.yml@arduino-mbed-support
needs: cancel

# Run the C Zephyr integration tests.
Expand Down
2 changes: 1 addition & 1 deletion org.lflang/src/lib/c/reactor-c
Submodule reactor-c updated 42 files
+14 −0 .github/workflows/ci.yml
+3 −2 core/CMakeLists.txt
+1 −0 core/federated/RTI/CMakeLists.txt
+2 −2 core/federated/RTI/rti.c
+2 −2 core/federated/federate.c
+11 −12 core/platform/CMakeLists.txt
+73 −0 core/platform/arduino_mbed/ConditionWrapper.cpp
+71 −0 core/platform/arduino_mbed/MutexWrapper.cpp
+70 −0 core/platform/arduino_mbed/ThreadWrapper.cpp
+154 −24 core/platform/lf_arduino_support.c
+2 −0 core/platform/lf_linux_support.c
+2 −0 core/platform/lf_macos_support.c
+2 −0 core/platform/lf_nrf52_support.c
+2 −0 core/platform/lf_os_single_threaded_support.c
+2 −0 core/platform/lf_unix_clock_support.c
+2 −0 core/platform/lf_unix_syscall_support.c
+2 −0 core/platform/lf_windows_support.c
+52 −21 core/platform/lf_zephyr_support.c
+2 −0 core/reactor.c
+7 −5 core/threaded/reactor_threaded.c
+1 −1 core/threaded/scheduler_GEDF_NP.c
+1 −1 core/threaded/scheduler_GEDF_NP_CI.c
+2 −0 core/threaded/scheduler_NP.c
+3 −3 core/threaded/scheduler_PEDF_NP.c
+1 −0 core/threaded/scheduler_adaptive.c
+2 −0 core/threaded/scheduler_sync_tag_advance.c
+4 −4 core/trace.c
+2 −7 core/utils/CMakeLists.txt
+5 −3 core/utils/semaphore.c
+4 −10 include/core/platform.h
+49 −0 include/core/platform/arduino_mbed/ConditionWrapper.h
+47 −0 include/core/platform/arduino_mbed/MutexWrapper.h
+46 −0 include/core/platform/arduino_mbed/ThreadWrapper.h
+23 −19 include/core/platform/lf_C11_threads_support.h
+23 −19 include/core/platform/lf_POSIX_threads_support.h
+13 −4 include/core/platform/lf_arduino_support.h
+1 −1 include/core/platform/lf_nrf52_support.h
+22 −19 include/core/platform/lf_windows_support.h
+8 −6 include/core/platform/lf_zephyr_support.h
+2 −2 include/core/threaded/worker_states.h
+1 −1 lingua-franca-ref.txt
+3 −3 util/sensor_simulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ private String generateCodeToInitializeFederate(FederateInstance federate, Linke
code.pr(String.join("\n",
"// Initialize the socket mutex",
"lf_mutex_init(&outbound_socket_mutex);",
"lf_cond_init(&port_status_changed);"
"lf_cond_init(&port_status_changed, &mutex);"
));

// Find the STA (A.K.A. the global STP offset) for this federate.
Expand Down
11 changes: 5 additions & 6 deletions org.lflang/src/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ public void doGenerate(Resource resource, LFGeneratorContext context) {
}
} else {
try {
FileUtil.arduinoDeleteHelper(fileConfig.getSrcGenPath().resolve("src/"));
FileUtil.arduinoDeleteHelper(fileConfig.getSrcGenPath().resolve("src/"), targetConfig.threading);
FileUtil.relativeIncludeHelper(fileConfig.getSrcGenPath().resolve("src/"));
} catch (IOException e) {
//noinspection ThrowableNotThrown,ResultOfMethodCallIgnored
Expand Down Expand Up @@ -1913,11 +1913,10 @@ protected void setUpGeneralParameters() {
// So that each separate compile knows about modal reactors, do this:
targetConfig.compileDefinitions.put("MODAL_REACTORS", "TRUE");
}
if (targetConfig.threading && targetConfig.platformOptions.platform == Platform.ARDUINO) {
//Add error message when user attempts to set threading=true for Arduino
if (targetConfig.setByUser.contains(TargetProperty.THREADING)) {
System.out.println("Threading is incompatible on Arduino. Setting threading to false.");
}
if (targetConfig.threading && targetConfig.platformOptions.platform == Platform.ARDUINO
&& (targetConfig.platformOptions.board == null || !targetConfig.platformOptions.board.contains("mbed"))) {
//non-MBED boards should not use threading
System.out.println("Threading is incompatible on your current Arduino flavor. Setting threading to false.");
targetConfig.threading = false;
}

Expand Down
6 changes: 0 additions & 6 deletions org.lflang/src/org/lflang/generator/c/CPreambleGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ public static String generateDefineDirectives(
code.pr("#define LOG_LEVEL " + logLevel);
code.pr("#define TARGET_FILES_DIRECTORY " + addDoubleQuotes(srcGenPath.toString()));


if (targetConfig.platformOptions.platform == Platform.ARDUINO) {
code.pr("#define MICROSECOND_TIME");
code.pr("#define BIT_32");
}

if (tracing != null) {
targetConfig.compileDefinitions.put("LF_TRACE", tracing.traceFileName);
}
Expand Down
5 changes: 3 additions & 2 deletions org.lflang/src/org/lflang/util/ArduinoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ private LFCommand arduinoCompileCommand(FileConfig fileConfig, TargetConfig targ
var fileWriter = new FileWriter(testScript.getAbsoluteFile(), true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
String board = targetConfig.platformOptions.board != null ? targetConfig.platformOptions.board : "arduino:avr:leonardo";
String isThreaded = targetConfig.platformOptions.board.contains("mbed") ? "-DLF_THREADED" : "-DLF_UNTHREADED";
bufferedWriter.write("arduino-cli compile -b " + board + " --build-property " +
"compiler.c.extra_flags=\"-DLF_UNTHREADED -DPLATFORM_ARDUINO -DINITIAL_EVENT_QUEUE_SIZE=10 -DINITIAL_REACT_QUEUE_SIZE=10\" " +
"--build-property compiler.cpp.extra_flags=\"-DLF_UNTHREADED -DPLATFORM_ARDUINO -DINITIAL_EVENT_QUEUE_SIZE=10 -DINITIAL_REACT_QUEUE_SIZE=10\" "
"compiler.c.extra_flags=\"" + isThreaded + " -DPLATFORM_ARDUINO -DINITIAL_EVENT_QUEUE_SIZE=10 -DINITIAL_REACT_QUEUE_SIZE=10\" " +
"--build-property compiler.cpp.extra_flags=\"" + isThreaded + " -DPLATFORM_ARDUINO -DINITIAL_EVENT_QUEUE_SIZE=10 -DINITIAL_REACT_QUEUE_SIZE=10\" "
+ srcGenPath.toString());
bufferedWriter.close();
return commandFactory.createCommand(
Expand Down
14 changes: 8 additions & 6 deletions org.lflang/src/org/lflang/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,20 +394,22 @@ private static boolean copyDirectoryFromJar(JarURLConnection connection, final P
* @param dir The folder to search for folders and files to delete.
* @throws IOException If the given folder and unneeded files cannot be deleted.
*/
public static void arduinoDeleteHelper(Path dir) throws IOException {
public static void arduinoDeleteHelper(Path dir, boolean threadingOn) throws IOException {
deleteDirectory(dir.resolve("core/federated")); // TODO: Add Federated Support to Arduino
deleteDirectory(dir.resolve("include/core/federated")); // TODO: Add Federated Support to Arduino

deleteDirectory(dir.resolve("core/threaded")); // No Threaded Support for Arduino
deleteDirectory(dir.resolve("include/core/threaded")); // No Threaded Support for Arduino
if (!threadingOn) {
deleteDirectory(dir.resolve("core/threaded")); // No Threaded Support for Arduino
deleteDirectory(dir.resolve("include/core/threaded")); // No Threaded Support for Arduino
deleteDirectory(dir.resolve("core/platform/arduino_mbed")); // No Threaded Support for Arduino
}

List<Path> allPaths = Files.walk(dir)
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
for (Path path : allPaths) {
String toCheck = path.toString().toLowerCase();
if (toCheck.contains("cmake") || toCheck.contains("semaphore")
|| (toCheck.contains("core/platform/") && !toCheck.contains("lf_arduino"))) {
if (toCheck.contains("cmake")) {
Files.delete(path);
}
}
Expand Down Expand Up @@ -444,7 +446,7 @@ private static String fileNameMatchConverter(String fileName, Path currPath, Map
*/
public static boolean isCFile(Path path) {
String fileName = path.getFileName().toString();
return fileName.endsWith(".c") || fileName.endsWith(".h");
return fileName.endsWith(".c") || fileName.endsWith(".cpp") || fileName.endsWith(".h");
}

/**
Expand Down
22 changes: 22 additions & 0 deletions test/C/src/arduino/BlinkMBED.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* This example demonstrates a very simple blink program that will turn on and
* off an LED on the Arduino Board with a 50% duty cycle switching every
* half-second.
*/
target C {
platform: {
name: "arduino",
board: "arduino:mbed:nano33ble"
}
}

main reactor BlinkMBED {
timer t1(0, 1 sec)
timer t2(500 msec, 1 sec)

reaction(startup) {= pinMode(LED_BUILTIN, OUTPUT); =}

reaction(t1) {= digitalWrite(LED_BUILTIN, HIGH); =}

reaction(t2) {= digitalWrite(LED_BUILTIN, LOW); =}
}

0 comments on commit c04d8d3

Please sign in to comment.