Skip to content

Commit

Permalink
Implement LFC templates for RIOT
Browse files Browse the repository at this point in the history
  • Loading branch information
LasseRosenow committed Feb 3, 2025
1 parent 719b096 commit 8792ea7
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 41 deletions.
24 changes: 24 additions & 0 deletions examples/riot/coap_federated_lf/CoapFederatedLF/r1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
LF_MAIN ?= CoapFederatedLF
LF_FED ?= r1

# Execute the LF compiler if build target is "all"
ifeq ($(firstword $(MAKECMDGOALS)),all)
_ := $(shell $(REACTOR_UC_PATH)/lfc/bin/lfc-dev $(CURDIR)/../../src/$(LF_MAIN).lf -n -o $(CURDIR))
endif

# ---- RIOT specific configuration ----
# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/RIOT

# If no BOARD is found in the environment, use this default:
BOARD ?= native

# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
DEVELHELP ?= 1

# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

include $(REACTOR_UC_PATH)/make/riot/riot-lfc.mk
24 changes: 24 additions & 0 deletions examples/riot/coap_federated_lf/CoapFederatedLF/r2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
LF_MAIN ?= CoapFederatedLF
LF_FED ?= r2

# Execute the LF compiler if build target is "all"
ifeq ($(firstword $(MAKECMDGOALS)),all)
_ := $(shell $(REACTOR_UC_PATH)/lfc/bin/lfc-dev $(CURDIR)/../../src/$(LF_MAIN).lf -n -o $(CURDIR))
endif

# ---- RIOT specific configuration ----
# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/RIOT

# If no BOARD is found in the environment, use this default:
BOARD ?= native

# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
DEVELHELP ?= 1

# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

include $(REACTOR_UC_PATH)/make/riot/riot-lfc.mk
27 changes: 1 addition & 26 deletions examples/riot/coap_federated_lf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,9 @@ REACTOR_UC_PATH ?= $(CURDIR)/../../../

# The name of the LF application inside "./src" to build/run/flash etc.
LF_MAIN ?= CoapFederatedLF
FEDERATE ?= r1

# Execute the LF compiler if build target is "all"
ifeq ($(firstword $(MAKECMDGOALS)),all)
_ := $(shell $(REACTOR_UC_PATH)/lfc/bin/lfc-dev src/$(LF_MAIN).lf)
_ := $(shell $(REACTOR_UC_PATH)/lfc/bin/lfc-dev --gen-fed-templates src/$(LF_MAIN).lf)
endif

# ---- RIOT specific configuration ----
# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../../../../RIOT

# If no BOARD is found in the environment, use this default:
BOARD ?= native

# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
DEVELHELP ?= 1

# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

# Enable reactor-uc features
CFLAGS += -DNETWORK_CHANNEL_COAP_RIOT

# Configure CoAP retransmission timeout
CFLAGS += -DCONFIG_GCOAP_NO_RETRANS_BACKOFF=1
CFLAGS += -DCONFIG_COAP_ACK_TIMEOUT_MS=400
CFLAGS += -DCONFIG_COAP_MAX_RETRANSMIT=4

include $(REACTOR_UC_PATH)/make/riot/riot-lfc.mk
6 changes: 4 additions & 2 deletions examples/riot/coap_federated_lf/build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash
FEDERATE=r1 PORT=tap0 make all
FEDERATE=r2 PORT=tap1 make all
make all

PORT=tap0 make all -C ./CoapFederatedLF/r1
PORT=tap1 make all -C ./CoapFederatedLF/r2
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.lflang.target.property.type.PlatformType
class UcFederate(val inst: Instantiation, val bankIdx: Int) {
val isBank = inst.isBank
val platform: PlatformType.Platform = AttributeUtils.getFederatePlatform(inst)
private val interfaces = mutableListOf<UcNetworkInterface>()
val interfaces = mutableListOf<UcNetworkInterface>()
val codeType = if (isBank) "${inst.codeTypeFederate}_${bankIdx}" else inst.codeTypeFederate
val name = if (isBank) "${inst.name}_${bankIdx}" else inst.name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,28 @@ class UcFederatedTemplateGenerator(private val mainDef: Instantiation, private v
val make = """
|LF_MAIN ?= ${mainDef.name}
|LF_FED ?= ${federate.name}
|LF_SRC_PKG_DIR ?= ${projectRoot}/../
|
|# Execute the LF compiler if build target is "all"
|ifeq ($S{'$'}(firstword $S{'$'}(MAKECMDGOALS)),all)
| _ := $S{'$'}(shell $S{'$'}(REACTOR_UC_PATH)/lfc/bin/lfc-dev $S{LF_SRC_PKG_DIR}/src/$S{'$'}(LF_MAIN).lf -n -o ${projectRoot})
|ifeq ($S(firstword $S(MAKECMDGOALS)),all)
| _ := $S(shell $S(REACTOR_UC_PATH)/lfc/bin/lfc-dev $S(CURDIR)/../../src/$S(LF_MAIN).lf -n -o $S(CURDIR))
|endif
|
|# ---- RIOT specific configuration ----
|RIOTBASE = $S{'$'}(CURDIR)/RIOT
|# This has to be the absolute path to the RIOT base directory:
|RIOTBASE ?= $S(CURDIR)/RIOT
|
|# If no BOARD is found in the environment, use this default:
|BOARD ?= native
|EVENT_QUEUE_SIZE?=20
|REACTION_QUEUE_SIZE?=20
|
|include $S{'$'}(REACTOR_UC_PATH)/make/riot/riot-lfc.mk
|# Comment this out to disable code in RIOT that does safety checking
|# which is not needed in a production environment but helps in the
|# development process:
|DEVELHELP ?= 1
|
|# Change this to 0 show compiler invocation lines by default:
|QUIET ?= 1
|
|include $S(REACTOR_UC_PATH)/make/riot/riot-lfc.mk
""".trimMargin()
FileUtil.writeToFile(make, projectRoot.resolve("Makefile"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package org.lflang.generator.uc
import org.lflang.FileConfig
import org.lflang.target.TargetConfig
import org.lflang.generator.PrependOperator
import org.lflang.generator.PrependOperator.rangeTo
import org.lflang.joinWithLn
import org.lflang.lf.Reactor
import org.lflang.target.property.NoCompileProperty
import org.lflang.target.property.type.PlatformType
import org.lflang.toUnixString
import java.nio.file.Path
import kotlin.io.path.name
Expand All @@ -16,7 +19,7 @@ abstract class UcMakeGenerator(private val mainTarget: String, private val numEv
fun doGenerateMake(sources: List<Path>, compileDefs: List<String>) = with(PrependOperator) {
val sources = sources.filterNot { it.name == "lf_main.c" }
"""
| # Makefile generated for ${mainTarget}
|# Makefile generated for ${mainTarget}
|LFC_GEN_SOURCES = \
${" | "..sources.joinWithLn { it.toUnixString() + if (it != sources.last()) " \\" else "" }}
|LFC_GEN_MAIN = lf_main.c
Expand All @@ -25,6 +28,7 @@ abstract class UcMakeGenerator(private val mainTarget: String, private val numEv
|REACTION_QUEUE_SIZE = ${max(numReactions, 1)}
|EVENT_QUEUE_SIZE = ${max(numEvents, 2)}
|
|
""".trimMargin()
}
}
Expand All @@ -37,5 +41,17 @@ class UcMakeGeneratorNonFederated(private val main: Reactor, private val targetC

class UcMakeGeneratorFederated(private val federate: UcFederate, targetConfig: TargetConfig, fileConfig: UcFileConfig, numEvents: Int, numReactions: Int)
: UcMakeGenerator(federate.codeType, numEvents, numReactions) {
override fun generateMake(sources: List<Path>) = doGenerateMake(sources, federate.getCompileDefs())
override fun generateMake(sources: List<Path>): String {
val channelTypes = federate.interfaces.map { it.type }.toSet()

return """
${"|"..doGenerateMake(sources, federate.getCompileDefs())}
${channelTypes.map { when (it) {
NetworkChannelType.TCP_IP -> "|CFLAGS += -DNETWORK_CHANNEL_TCP_RIOT"
NetworkChannelType.COAP_UDP_IP -> "|CFLAGS += -DNETWORK_CHANNEL_COAP_RIOT" //TODO: Abstract RIOT away!
NetworkChannelType.NONE -> ""
NetworkChannelType.CUSTOM -> ""
}}.joinWithLn { it }}
""".trimMargin()
}
}
6 changes: 3 additions & 3 deletions make/riot/riot-lfc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ ifndef RIOTBASE
endif

# Check if this is a federated program
ifdef FEDERATE
ifdef LF_FED
# Name of your RIOT application
APPLICATION ?= $(LF_MAIN)-$(FEDERATE)
APPLICATION ?= $(LF_MAIN)-$(LF_FED)

# Path of generated lf c-code
LF_SRC_GEN_PATH ?= $(CURDIR)/src-gen/$(LF_MAIN)/$(FEDERATE)
LF_SRC_GEN_PATH ?= $(CURDIR)/src-gen/$(LF_MAIN)/$(LF_FED)
else
# Name of your RIOT application
APPLICATION ?= $(LF_MAIN)
Expand Down
6 changes: 6 additions & 0 deletions make/riot/riot.mk
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ USEMODULE += reactor-uc
CFLAGS += -DEVENT_QUEUE_SIZE=$(EVENT_QUEUE_SIZE)
CFLAGS += -DREACTION_QUEUE_SIZE=$(REACTION_QUEUE_SIZE)

# Configure CoAP retransmission timeout
# TODO: Specify generic keywords to share this configuration across platforms similar to EVENT_QUEUE_SIZE
CFLAGS += -DCONFIG_GCOAP_NO_RETRANS_BACKOFF=1
CFLAGS += -DCONFIG_COAP_ACK_TIMEOUT_MS=400
CFLAGS += -DCONFIG_COAP_MAX_RETRANSMIT=4

include $(RIOTBASE)/Makefile.include

0 comments on commit 8792ea7

Please sign in to comment.