Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for the stm32 platform #2185

Draft
wants to merge 25 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1ef67b0
Added Stm32 platform
lhstrh Nov 7, 2023
56ed5f6
initial STM32 support works but very hard-coded
naichenzhao Nov 11, 2023
9728279
we dont need STM_main anymore
naichenzhao Nov 19, 2023
e87f1b2
should be able to copy most of generated libraaries
naichenzhao Nov 19, 2023
522db67
updated to move code out of core cmake
naichenzhao Nov 22, 2023
127d62a
fixed print
naichenzhao Dec 7, 2023
6423ee1
maybe this works?
naichenzhao Dec 18, 2023
2de5aa4
removed sdk
naichenzhao Feb 2, 2024
972470c
bump reactor
naichenzhao Jul 15, 2024
58a444c
Merge branch 'master' of github.com:lf-lang/lingua-franca into stm32_2
naichenzhao Aug 31, 2024
b9f4770
small test commit
naichenzhao Aug 31, 2024
368ffe3
updating pointers:
naichenzhao Aug 31, 2024
8876b6b
bump reactor
naichenzhao Aug 31, 2024
0be631e
slight update to code
naichenzhao Aug 31, 2024
38b2313
bump reactor again
naichenzhao Aug 31, 2024
e89b9e7
formatting
naichenzhao Sep 5, 2024
35c7437
bimp and format
naichenzhao Sep 7, 2024
4ea0ab3
bump reactor-c again
naichenzhao Sep 24, 2024
5c3939d
bump reactor
naichenzhao Oct 5, 2024
1192af1
bump reactor again
naichenzhao Oct 5, 2024
60d9eac
bump reactor and clean
naichenzhao Oct 5, 2024
9f0dd8e
Add cmake-init-include target property
erlingrj Nov 5, 2024
b8b75e2
Move STM32F4 cmake out of lfc and require the user to provide it inst…
erlingrj Nov 5, 2024
a34676f
Merge remote-tracking branch 'origin/master' into stm32_2
erlingrj Nov 5, 2024
05b449c
Merge with master and bump reactor-c
erlingrj Nov 5, 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
148 changes: 148 additions & 0 deletions core/src/main/java/org/lflang/generator/c/CCmakeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ CodeBuilder generateCMakeCode(
// remove warnings for rp2040 only to make debug easier
cMakeCode.pr("set(CMAKE_C_FLAGS \"${CMAKE_C_FLAGS} -w\")");
break;
case STM32:
cMakeCode.pr("set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/arm-none-eabi-gcc.cmake)");
cMakeCode.pr("project(" + executableName + " LANGUAGES C)");
cMakeCode.newLine();
break;
case FLEXPRET:
if (System.getenv("FP_PATH") == null) {
messageReporter.nowhere().warning("No FP_PATH environment variable found");
Expand Down Expand Up @@ -252,6 +257,11 @@ CodeBuilder generateCMakeCode(
cMakeCode.pr("endif()");

cMakeCode.pr("# Require C11");
switch (platformOptions.platform()) {
erlingrj marked this conversation as resolved.
Show resolved Hide resolved
case STM32:
cMakeCode.pr("enable_language(C CXX ASM)");
break;
}
cMakeCode.pr("set(CMAKE_C_STANDARD 11)");
cMakeCode.pr("set(CMAKE_C_STANDARD_REQUIRED ON)");
cMakeCode.newLine();
Expand Down Expand Up @@ -321,6 +331,14 @@ CodeBuilder generateCMakeCode(
executableName,
Stream.concat(additionalSources.stream(), sources.stream())));
break;
case STM32:
cMakeCode.pr(
setUpMainTargetStm32(
hasMain,
executableName,
Stream.concat(additionalSources.stream(), sources.stream()),
boardProperties));
break;
case FLEXPRET:
cMakeCode.pr(
setUpMainTargetFlexPRET(
Expand Down Expand Up @@ -616,4 +634,134 @@ private static String setUpMainTargetFlexPRET(

return code.toString();
}

private static String setUpMainTargetStm32(
boolean hasMain, String executableName, Stream<String> cSources, String[] boardProperties) {
var code = new CodeBuilder();
code.newLine();
code.newLine();

code.pr("add_subdirectory(core)");
code.newLine();
code.pr("set(LF_MAIN_TARGET " + executableName + ")");

// Declaration preamble
code.pr("set(PROJECT_DIR ${CMAKE_CURRENT_SOURCE_DIR})");
code.pr("set(STM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../STM_sdk)");
erlingrj marked this conversation as resolved.
Show resolved Hide resolved
code.newLine();

// Configure board settings
if (boardProperties.length < 1 || boardProperties[0].equals("")) {
// By default, we set it to STM32f446RE
code.pr("set(MCU_FAMILY STM32F4xx)");
code.pr("set(MCU_MODEL STM32F446xx)");
} else {
code.pr("set(MCU_FAMILY STM32" + boardProperties[0].substring(0, 2) + "xx)");
code.pr("set(MCU_MODEL STM32" + boardProperties[0].substring(0, 4) + "xx)");
}

code.pr("set(CPU_PARAMETERS -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=softfp)");
naichenzhao marked this conversation as resolved.
Show resolved Hide resolved
code.newLine();

// Define linker and startup scropts
if (boardProperties.length < 1 || boardProperties[0].equals("")) {
// By default, we set it to STM32f446RE
code.pr("set(STARTUP_SCRIPT ${STM_DIR}/startup_stm32f446xx.s)");
naichenzhao marked this conversation as resolved.
Show resolved Hide resolved
code.pr("set(MCU_LINKER_SCRIPT ${STM_DIR}/STM32F446RETx_FLASH.ld)");
} else {
code.pr(
"set(STARTUP_SCRIPT ${STM_DIR}/startup_stm32f"
+ boardProperties[0].substring(1, 4)
+ "xx.s)");
code.pr(
"set(MCU_LINKER_SCRIPT ${STM_DIR}/STM32"
+ boardProperties[0].substring(0, 4)
+ "RETx_FLASH.ld)");
}
code.newLine();

// Glob together directories and sources
code.pr(
"file(GLOB_RECURSE STM32CUBEMX_SOURCES ${STM_DIR}/Core/*.c"
+ " ${STM_DIR}/Drivers/${MCU_FAMILY}_HAL_Driver/*.c)");
code.pr(
"set(CUBEMX_INCLUDE_DIRECTORIES\n"
+ " ${PROJECT_DIR}\n"
+ " ${PROJECT_DIR}/include/Main\n"
+ " ${STM_DIR}/Core/Inc\n"
+ " ${STM_DIR}/Drivers/${MCU_FAMILY}_HAL_Driver/Inc\n"
+ " ${STM_DIR}/Drivers/${MCU_FAMILY}_HAL_Driver/Inc/Legacy\n"
+ " ${STM_DIR}/Drivers/CMSIS/Device/ST/${MCU_FAMILY}/Include\n"
+ " ${STM_DIR}/Drivers/CMSIS/Include\n"
+ ")");
code.newLine();

// Add needed executables
if (hasMain) {
code.pr("# Declare a new executable target and list all its sources");
code.pr("add_executable(");
} else {
code.pr("# Declare a new library target and list all its sources");
code.pr("add_library(");
}
code.indent();
code.pr("${LF_MAIN_TARGET}");
code.pr("${STM32CUBEMX_SOURCES} ");
code.pr("${STARTUP_SCRIPT} ");
cSources.forEach(code::pr);
code.unindent();
code.pr(")");
code.newLine();

code.pr("target_include_directories(${LF_MAIN_TARGET} PUBLIC ${CUBEMX_INCLUDE_DIRECTORIES})");
code.pr("target_compile_definitions(${LF_MAIN_TARGET} PUBLIC ${MCU_MODEL} USE_HAL_DRIVER)");
code.newLine();
code.newLine();
code.newLine();

// setup compiler and linker options
code.pr("# Compiler definitions for the STM32");
code.pr(
"target_compile_options(${LF_MAIN_TARGET} PUBLIC\n"
+ " ${CPU_PARAMETERS}\n"
+ " -Wall\n"
+ " -Wextra\n"
+ " -Wpedantic\n"
+ " -Wno-unused-parameter\n"
+ " $<$<COMPILE_LANGUAGE:CXX>:\n"
+ " -Wno-volatile\n"
+ " -Wold-style-cast\n"
+ " -Wuseless-cast\n"
+ " -Wsuggest-override>\n"
+ " $<$<CONFIG:Debug>:-Og -g3 -ggdb>\n"
+ " $<$<CONFIG:Release>:-Og -g0>)\n");
code.newLine();
code.pr(
"target_link_options(${LF_MAIN_TARGET} PRIVATE\n"
+ " -T${MCU_LINKER_SCRIPT}\n"
+ " ${CPU_PARAMETERS}\n"
+ " -Wl,-Map=${CMAKE_PROJECT_NAME}.map\n"
+ " --specs=nosys.specs\n"
+ " -Wl,--start-group\n"
+ " -lc\n"
+ " -lm\n"
+ " -lstdc++\n"
+ " -Wl,--end-group\n"
+ " -Wl,--print-memory-usage)");
code.newLine();

// define post-build
code.pr(
"add_custom_command(TARGET ${LF_MAIN_TARGET} POST_BUILD\n"
+ " COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${LF_MAIN_TARGET}>)");
code.pr(
"add_custom_command(TARGET ${LF_MAIN_TARGET} POST_BUILD\n"
+ " COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${LF_MAIN_TARGET}>\n"
+ " ${LF_MAIN_TARGET}.hex\n"
+ " COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${LF_MAIN_TARGET}>\n"
+ " ${LF_MAIN_TARGET}.bin)");
code.newLine();

return code.toString();
}
}
5 changes: 5 additions & 0 deletions core/src/main/java/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,11 @@ protected void copyTargetFiles() throws IOException {
FileUtil.copyFileFromClassPath(
"/lib/platform/zephyr/Kconfig", fileConfig.getSrcGenPath(), true);
}
case STM32 -> {
// Copy over STM32 library (Currently hard-coded)
FileUtil.copyFileFromClassPath(
"/lib/platform/stm32/arm-none-eabi-gcc.cmake", fileConfig.getSrcGenPath(), true);
}
case RP2040 -> {
// For the pico src-gen, copy over vscode configurations for debugging
Path vscodePath = fileConfig.getSrcGenPath().resolve(".vscode");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ private String generateMainFunction() {
case RP2040 -> {
return String.join("\n", "int main(void) {", " return lf_reactor_c_main(0, NULL);", "}");
}

case STM32 -> {
return String.join("\n", "int main(void) {", " return lf_reactor_c_main(0, NULL);", "}");
}
erlingrj marked this conversation as resolved.
Show resolved Hide resolved
default -> {
return String.join(
"\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum Platform {
LINUX("Linux"),
MAC("Darwin"),
ZEPHYR("Zephyr"),
STM32("Stm32"),
FLEXPRET("FlexPRET"),
WINDOWS("Windows");

Expand Down
23 changes: 23 additions & 0 deletions core/src/main/resources/lib/platform/stm32/arm-none-eabi-gcc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)

# Some default GCC settings
set(TOOLCHAIN_PREFIX arm-none-eabi-)
set(FLAGS
erlingrj marked this conversation as resolved.
Show resolved Hide resolved
"-fdata-sections -ffunction-sections \
--specs=nano.specs -Wl,--gc-sections")
set(CPP_FLAGS
"-fno-rtti -fno-exceptions \
-fno-threadsafe-statics")

set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc ${FLAGS})
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++ ${FLAGS} ${CPP_FLAGS})
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy)
set(CMAKE_SIZE ${TOOLCHAIN_PREFIX}size)

set(CMAKE_EXECUTABLE_SUFFIX_ASM ".elf")
set(CMAKE_EXECUTABLE_SUFFIX_C ".elf")
set(CMAKE_EXECUTABLE_SUFFIX_CXX ".elf")

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
Loading