From bb74c3ad5cf7a189149e0ece206d350e4f76b931 Mon Sep 17 00:00:00 2001 From: tanneberger Date: Fri, 21 Feb 2025 01:33:42 +0100 Subject: [PATCH 1/3] pico uart without encryption_layer --- include/reactor-uc/network_channel.h | 5 +- .../reactor-uc/platform/pico/uart_channel.h | 34 +++ src/network_channel.c | 3 + src/platform/pico/uart_channel.c | 229 ++++++++++++++++++ 4 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 include/reactor-uc/platform/pico/uart_channel.h create mode 100644 src/platform/pico/uart_channel.c diff --git a/include/reactor-uc/network_channel.h b/include/reactor-uc/network_channel.h index 4c42fc72..1e21a319 100644 --- a/include/reactor-uc/network_channel.h +++ b/include/reactor-uc/network_channel.h @@ -121,7 +121,7 @@ struct PolledNetworkChannel { /** * @brief Polls for new data and calls the callback handler if a message is successfully decoded */ - void (*poll)(NetworkChannel *self); + void (*poll)(PolledNetworkChannel *self); }; struct AsyncNetworkChannel { @@ -153,6 +153,9 @@ struct AsyncNetworkChannel { #ifdef NETWORK_CHANNEL_TCP_POSIX #error "NETWORK_POSIX_TCP not supported on PICO" #endif +#ifdef NETWORK_CHANNEL_UART +#include "platform/pico/uart_channel.h" +#endif #elif defined(PLATFORM_FLEXPRET) #ifdef NETWORK_CHANNEL_TCP_POSIX diff --git a/include/reactor-uc/platform/pico/uart_channel.h b/include/reactor-uc/platform/pico/uart_channel.h new file mode 100644 index 00000000..3c13df67 --- /dev/null +++ b/include/reactor-uc/platform/pico/uart_channel.h @@ -0,0 +1,34 @@ +#ifndef REACTOR_UC_PICO_UART_CHANNEL_H +#define REACTOR_UC_PICO_UART_CHANNEL_H + +#include "reactor-uc/network_channel/uart_channel.h" +#include "reactor-uc/network_channel.h" +#include "reactor-uc/environment.h" + +#include "pico/stdlib.h" +#include "hardware/uart.h" +#include "hardware/irq.h" + +typedef struct FederatedConnectionBundle FederatedConnectionBundle; +typedef struct UartPolledChannel UartPolledChannel; +typedef struct UartAsyncChannel UartAsyncChannel; + +#define UART_CHANNEL_BUFFERSIZE 1024 + +struct UartPolledChannel { + PolledNetworkChannel super; + NetworkChannelState state; + FederateMessage output; + unsigned char receive_buffer[UART_CHANNEL_BUFFERSIZE]; + unsigned char send_buffer[UART_CHANNEL_BUFFERSIZE]; + unsigned int receive_buffer_index; + uart_inst_t *uart_device; + + FederatedConnectionBundle *bundle; + void (*receive_callback)(FederatedConnectionBundle *bundle, const FederateMessage *message); +}; + +void UartPolledChannel_ctor(UartPolledChannel *self, uint32_t uart_device, uint32_t baud, UartDataBits data_bits, + UartParityBits parity, UartStopBits stop_bits); + +#endif diff --git a/src/network_channel.c b/src/network_channel.c index 84648141..76381dc7 100644 --- a/src/network_channel.c +++ b/src/network_channel.c @@ -24,6 +24,9 @@ #ifdef NETWORK_CHANNEL_TCP_POSIX #error "NETWORK_POSIC_TCP not supported on PICO" #endif +// #ifdef NETWORK_CHANNEL_UART +#include "platform/pico/uart_channel.c" +// #endif #elif defined(PLATFORM_FLEXPRET) #ifdef NETWORK_CHANNEL_TCP_POSIX diff --git a/src/platform/pico/uart_channel.c b/src/platform/pico/uart_channel.c new file mode 100644 index 00000000..401bed09 --- /dev/null +++ b/src/platform/pico/uart_channel.c @@ -0,0 +1,229 @@ +#include "reactor-uc/platform/pico/uart_channel.h" +#include "reactor-uc/platform/pico/pico.h" +#include "reactor-uc/logging.h" +#include "reactor-uc/serialization.h" +#include "reactor-uc/environment.h" + +#define UART_CHANNEL_ERR(fmt, ...) LF_ERR(NET, "UartPolledChannel: " fmt, ##__VA_ARGS__) +#define UART_CHANNEL_WARN(fmt, ...) LF_WARN(NET, "UartPolledChannel: " fmt, ##__VA_ARGS__) +#define UART_CHANNEL_INFO(fmt, ...) LF_INFO(NET, "UartPolledChannel: " fmt, ##__VA_ARGS__) +#define UART_CHANNEL_DEBUG(fmt, ...) LF_DEBUG(NET, "UartPolledChannel: " fmt, ##__VA_ARGS__) + +#define UART_OPEN_MESSAGE {0xC, 0x1, 0xE, 0x1, 0xC, 0xD}; +#define UART_CLOSE_MESSAGE {0x2, 0xF, 0x6, 0x6, 0xC, 0x2}; +#define MINIMUM_MESSAGE_SIZE 25 +#define UART_CHANNEL_EXPECTED_CONNECT_DURATION SEC(2) + +static UartPolledChannel *uart_channel_0 = NULL; +static UartPolledChannel *uart_channel_1 = NULL; + +static lf_ret_t UartPolledChannel_open_connection(NetworkChannel *untyped_self) { + UartPolledChannel *self = (UartPolledChannel *)untyped_self; + char connect_message[] = UART_OPEN_MESSAGE; + uart_write_blocking(self->uart_device, (const uint8_t *)connect_message, sizeof(connect_message)); + UART_CHANNEL_DEBUG("Open connection"); + return LF_OK; +} + +static void UartPolledChannel_close_connection(NetworkChannel *untyped_self) { + UART_CHANNEL_DEBUG("Close connection"); + (void)untyped_self; +} + +static void UartPolledChannel_free(NetworkChannel *untyped_self) { + UART_CHANNEL_DEBUG("Free"); + (void)untyped_self; +} + +static bool UartPolledChannel_is_connected(NetworkChannel *untyped_self) { + UartPolledChannel *self = (UartPolledChannel *)untyped_self; + UART_CHANNEL_DEBUG("Open connection - Sending Ping"); + char connect_message[] = UART_OPEN_MESSAGE; + uart_write_blocking(self->uart_device, (const uint8_t *)connect_message, sizeof(connect_message)); + return self->state == NETWORK_CHANNEL_STATE_CONNECTED; +} + +static lf_ret_t UartPolledChannel_send_blocking(NetworkChannel *untyped_self, const FederateMessage *message) { + UartPolledChannel *self = (UartPolledChannel *)untyped_self; + int message_size = serialize_to_protobuf(message, self->send_buffer, UART_CHANNEL_BUFFERSIZE); + UART_CHANNEL_DEBUG("Sending Message of Size: %i", message_size); + + if (message_size < 0) { + UART_CHANNEL_ERR("Was unable to serialize messsage!"); + return LF_ERR; + } + + uart_write_blocking(self->uart_device, (const uint8_t *)self->send_buffer, message_size); + return LF_OK; +} + +static void UartPolledChannel_register_receive_callback(NetworkChannel *untyped_self, + void (*receive_callback)(FederatedConnectionBundle *conn, + const FederateMessage *message), + FederatedConnectionBundle *bundle) { + UART_CHANNEL_DEBUG("Register receive callback"); + UartPolledChannel *self = (UartPolledChannel *)untyped_self; + + self->receive_callback = receive_callback; + self->bundle = bundle; +} + +void _UartPolledChannel_interrupt_handler(UartPolledChannel *self) { + while (uart_is_readable(self->uart_device)) { + uint8_t received_byte = uart_getc(self->uart_device); + self->receive_buffer[self->receive_buffer_index] = received_byte; + self->receive_buffer_index++; + + char connect_message[] = UART_OPEN_MESSAGE; + if (received_byte == connect_message[sizeof(connect_message) - 1] && + self->receive_buffer_index >= sizeof(connect_message)) { + if (memcmp(connect_message, &self->receive_buffer[self->receive_buffer_index - sizeof(connect_message)], + sizeof(connect_message)) == 0) { + self->receive_buffer_index -= sizeof(connect_message); + printf("Found Byte Signature\n"); + self->state = NETWORK_CHANNEL_STATE_CONNECTED; + _lf_environment->platform->new_async_event(_lf_environment->platform); + } + } + } + if (self->receive_buffer_index > MINIMUM_MESSAGE_SIZE) { + _lf_environment->platform->new_async_event(_lf_environment->platform); + } +} + +static void _UartPolledChannel_pico_interrupt_handler_0(void) { + if (uart_channel_0 != NULL) { + _UartPolledChannel_interrupt_handler(uart_channel_0); + } +} + +static void _UartPolledChannel_pico_interrupt_handler_1(void) { + if (uart_channel_1 != NULL) { + _UartPolledChannel_interrupt_handler(uart_channel_1); + } +} + +void UartPolledChannel_poll(PolledNetworkChannel *untyped_self) { + UartPolledChannel *self = (UartPolledChannel *)untyped_self; + + while (self->receive_buffer_index > MINIMUM_MESSAGE_SIZE) { + int bytes_left = deserialize_from_protobuf(&self->output, self->receive_buffer, self->receive_buffer_index); + UART_CHANNEL_DEBUG("Bytes Left after attempted to deserialize %d", bytes_left); + + if (bytes_left >= 0) { + _lf_environment->enter_critical_section(_lf_environment); + int receive_buffer_index = self->receive_buffer_index; + self->receive_buffer_index = bytes_left; + memcpy(self->receive_buffer, self->receive_buffer + (receive_buffer_index - bytes_left), bytes_left); + _lf_environment->leave_critical_section(_lf_environment); + + // TODO: we potentially can move this memcpy out of the critical section + + if (self->receive_callback != NULL) { + UART_CHANNEL_DEBUG("calling user callback!"); + self->receive_callback(self->bundle, &self->output); + } + } else { + break; + } + } +} + +static unsigned int from_uc_data_bits(UartDataBits data_bits) { + switch (data_bits) { + case UC_UART_DATA_BITS_5: + return 5; + case UC_UART_DATA_BITS_6: + return 6; + case UC_UART_DATA_BITS_7: + return 7; + case UC_UART_DATA_BITS_8: + return 8; + }; + + return 8; +} + +static uart_parity_t from_uc_parity_bits(UartParityBits parity_bits) { + switch (parity_bits) { + case UC_UART_PARITY_NONE: + return UART_PARITY_NONE; + case UC_UART_PARITY_EVEN: + return UART_PARITY_EVEN; + case UC_UART_PARITY_ODD: + return UART_PARITY_ODD; + case UC_UART_PARITY_MARK: + throw("Not supported by pico SDK"); + case UC_UART_PARITY_SPACE: + throw("Not supported by pico SDK"); + } + + return UART_PARITY_EVEN; +} + +static unsigned int from_uc_stop_bits(UartStopBits stop_bits) { + switch (stop_bits) { + case UC_UART_STOP_BITS_1: + return 1; + case UC_UART_STOP_BITS_2: + return 2; + } + + return 2; +} + +void UartPolledChannel_ctor(UartPolledChannel *self, uint32_t uart_device, uint32_t baud, UartDataBits data_bits, + UartParityBits parity_bits, UartStopBits stop_bits) { + + assert(self != NULL); + + // Concrete fields + self->receive_buffer_index = 0; + self->receive_callback = NULL; + self->bundle = NULL; + + self->state = NETWORK_CHANNEL_STATE_UNINITIALIZED; + self->super.super.mode = NETWORK_CHANNEL_MODE_POLLED; + self->super.super.expected_connect_duration = UART_CHANNEL_EXPECTED_CONNECT_DURATION; + self->super.super.type = NETWORK_CHANNEL_TYPE_UART; + self->super.super.is_connected = UartPolledChannel_is_connected; + self->super.super.open_connection = UartPolledChannel_open_connection; + self->super.super.close_connection = UartPolledChannel_close_connection; + self->super.super.send_blocking = UartPolledChannel_send_blocking; + self->super.super.register_receive_callback = UartPolledChannel_register_receive_callback; + self->super.super.free = UartPolledChannel_free; + self->super.poll = UartPolledChannel_poll; + + if (uart_device == 0) { + self->uart_device = uart0; + uart_channel_0 = self; + } else if (uart_device == 1) { + self->uart_device = uart1; + uart_channel_1 = self; + } else { + throw("The Raspberry Pi pico only supports uart devices 0 and 1."); + } + + uart_init(self->uart_device, 2400); + gpio_set_function(4, UART_FUNCSEL_NUM(self->uart_device, 4)); + gpio_set_function(5, UART_FUNCSEL_NUM(self->uart_device, 5)); + int actual = uart_set_baudrate(self->uart_device, baud); + + if (actual != (int)baud) { + UART_CHANNEL_WARN("Other baudrate then specified got configured requested: %d actual %d", baud, actual); + } + + uart_set_hw_flow(self->uart_device, false, false); + + uart_set_format(self->uart_device, from_uc_data_bits(data_bits), from_uc_stop_bits(stop_bits), + from_uc_parity_bits(parity_bits)); + + uart_set_fifo_enabled(self->uart_device, false); + + irq_set_exclusive_handler(UART0_IRQ, _UartPolledChannel_pico_interrupt_handler_0); + irq_set_exclusive_handler(UART1_IRQ, _UartPolledChannel_pico_interrupt_handler_1); + irq_set_enabled(UART0_IRQ, true); + irq_set_enabled(UART1_IRQ, true); + + uart_set_irq_enables(self->uart_device, true, false); +} \ No newline at end of file From 322d8e30586b236d8bd3f256c98d0b438361ba97 Mon Sep 17 00:00:00 2001 From: tanneberger Date: Sun, 23 Feb 2025 00:23:13 +0100 Subject: [PATCH 2/3] uart-cmake --- benchmarks/src/PingPongUc.lf | 2 +- cmake/lfc.cmake | 9 ++++---- .../lflang/generator/uc/UcCmakeGenerator.kt | 23 ++++++++++--------- runAll.sh | 0 src/platform/pico/uart_channel.c | 5 +++- 5 files changed, 22 insertions(+), 17 deletions(-) create mode 100644 runAll.sh diff --git a/benchmarks/src/PingPongUc.lf b/benchmarks/src/PingPongUc.lf index 7c43acd2..07630fbf 100644 --- a/benchmarks/src/PingPongUc.lf +++ b/benchmarks/src/PingPongUc.lf @@ -79,4 +79,4 @@ main reactor ping.finished -> pong.finish; ping.send -> pong.receive; pong.send -> ping.receive; -} \ No newline at end of file +} diff --git a/cmake/lfc.cmake b/cmake/lfc.cmake index d261c19e..24d0365c 100644 --- a/cmake/lfc.cmake +++ b/cmake/lfc.cmake @@ -61,15 +61,16 @@ function(lf_build_generated_code MAIN_TARGET SOURCE_GEN_DIR) endif() # Check if the Include.cmake file exists in SOURCE_GEN_DIR - if (NOT EXISTS ${SOURCE_GEN_DIR}/Include.cmake) + if (NOT EXISTS ${SOURCE_GEN_DIR}/${FEDERATE}/Include.cmake) message(FATAL_ERROR "Include.cmake does not exist in src-gen directory: ${SOURCE_GEN_DIR}/Include.cmake") endif() - include(${SOURCE_GEN_DIR}/Include.cmake) - add_subdirectory(${REACTOR_UC_PATH}) + include(${SOURCE_GEN_DIR}/${FEDERATE}/Include.cmake) + message(${REACTOR_UC_PATH}) + #add_subdirectory(${REACTOR_UC_PATH}) target_sources(${MAIN_TARGET} PRIVATE ${LFC_GEN_MAIN} ${LFC_GEN_SOURCES}) target_include_directories(${MAIN_TARGET} PRIVATE ${LFC_GEN_INCLUDE_DIRS}) target_link_libraries(${MAIN_TARGET} PUBLIC reactor-uc) target_compile_definitions(reactor-uc PUBLIC LF_LOG_LEVEL_ALL=${LOG_LEVEL}) target_compile_definitions(reactor-uc PUBLIC ${LFC_GEN_COMPILE_DEFS}) -endfunction() \ No newline at end of file +endfunction() diff --git a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcCmakeGenerator.kt b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcCmakeGenerator.kt index ff25d8ea..a45e9ee5 100644 --- a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcCmakeGenerator.kt +++ b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcCmakeGenerator.kt @@ -52,17 +52,18 @@ abstract class UcCmakeGenerator( |set(LF_MAIN_TARGET ${mainTarget}) |set(CMAKE_BUILD_TYPE ${targetConfig.getOrDefault(BuildTypeProperty.INSTANCE)}) |set(PLATFORM POSIX CACHE STRING "Target platform") - |include($S{CMAKE_CURRENT_SOURCE_DIR}/Include.cmake) - |add_executable($S{LF_MAIN_TARGET} $S{LFC_GEN_SOURCES} $S{LFC_GEN_MAIN}) - |install(TARGETS $S{LF_MAIN_TARGET} - | RUNTIME DESTINATION $S{CMAKE_INSTALL_BINDIR} - | OPTIONAL - |) - |add_compile_definitions("LF_LOG_LEVEL_ALL=LF_LOG_LEVEL_${targetConfig.getOrDefault(LoggingProperty.INSTANCE).name.uppercase()}") - |add_compile_definitions($S{LFC_GEN_COMPILE_DEFS}) - |add_subdirectory($S{REACTOR_UC_PATH}) - |target_link_libraries($S{LF_MAIN_TARGET} PRIVATE reactor-uc) - |target_include_directories($S{LF_MAIN_TARGET} PRIVATE $S{LFC_GEN_INCLUDE_DIRS}) + |include($S{CMAKE_CURRENT_SOURCE_DIR}/src-gen/$S{LF_MAIN}/$S{FEDERATE}/Include.cmake) + |#include(./Include.cmake) + |#add_executable($S{LF_MAIN_TARGET} $S{LFC_GEN_SOURCES} $S{LFC_GEN_MAIN}) + |#install(TARGETS $S{LF_MAIN_TARGET} + |# RUNTIME DESTINATION $S{CMAKE_INSTALL_BINDIR} + |# OPTIONAL + |#) + |#add_compile_definitions("LF_LOG_LEVEL_ALL=LF_LOG_LEVEL_${targetConfig.getOrDefault(LoggingProperty.INSTANCE).name.uppercase()}") + |#add_compile_definitions($S{LFC_GEN_COMPILE_DEFS}) + |#add_subdirectory($S{REACTOR_UC_PATH}) + |#target_link_libraries($S{LF_MAIN_TARGET} PRIVATE reactor-uc) + |#target_include_directories($S{LF_MAIN_TARGET} PRIVATE $S{LFC_GEN_INCLUDE_DIRS}) ${" |"..(includeFiles?.joinWithLn { "include(\"$it\")" } ?: "")} """ .trimMargin() diff --git a/runAll.sh b/runAll.sh new file mode 100644 index 00000000..e69de29b diff --git a/src/platform/pico/uart_channel.c b/src/platform/pico/uart_channel.c index 401bed09..f244e6b8 100644 --- a/src/platform/pico/uart_channel.c +++ b/src/platform/pico/uart_channel.c @@ -26,6 +26,9 @@ static lf_ret_t UartPolledChannel_open_connection(NetworkChannel *untyped_self) } static void UartPolledChannel_close_connection(NetworkChannel *untyped_self) { + UartPolledChannel *self = (UartPolledChannel *)untyped_self; + char close_message[] = UART_CLOSE_MESSAGE; + uart_write_blocking(self->uart_device, (const uint8_t *)close_message, sizeof(close_message)); UART_CHANNEL_DEBUG("Close connection"); (void)untyped_self; } @@ -80,7 +83,7 @@ void _UartPolledChannel_interrupt_handler(UartPolledChannel *self) { if (memcmp(connect_message, &self->receive_buffer[self->receive_buffer_index - sizeof(connect_message)], sizeof(connect_message)) == 0) { self->receive_buffer_index -= sizeof(connect_message); - printf("Found Byte Signature\n"); + printf("Found Byte Signature of Open Message\n"); self->state = NETWORK_CHANNEL_STATE_CONNECTED; _lf_environment->platform->new_async_event(_lf_environment->platform); } From 38f274c4e64255982f3dd41d48e3191159e82862 Mon Sep 17 00:00:00 2001 From: tanneberger Date: Tue, 4 Mar 2025 05:01:19 +0100 Subject: [PATCH 3/3] fsw25 prep --- cmake/lfc.cmake | 2 +- include/reactor-uc/macros_internal.h | 7 ++++-- include/reactor-uc/network_channel.h | 6 ++--- .../lflang/generator/uc/UcCmakeGenerator.kt | 24 +++++++++---------- .../lflang/generator/uc/UcNetworkChannel.kt | 4 ++-- src/network_channel.c | 6 ++--- src/platform/pico/pico.c | 2 +- src/platform/pico/uart_channel.c | 10 ++++++-- src/reaction.c | 3 ++- src/reactor.c | 1 - 10 files changed, 37 insertions(+), 28 deletions(-) diff --git a/cmake/lfc.cmake b/cmake/lfc.cmake index 24d0365c..109cdc19 100644 --- a/cmake/lfc.cmake +++ b/cmake/lfc.cmake @@ -67,7 +67,7 @@ function(lf_build_generated_code MAIN_TARGET SOURCE_GEN_DIR) include(${SOURCE_GEN_DIR}/${FEDERATE}/Include.cmake) message(${REACTOR_UC_PATH}) - #add_subdirectory(${REACTOR_UC_PATH}) + add_subdirectory(${REACTOR_UC_PATH}) target_sources(${MAIN_TARGET} PRIVATE ${LFC_GEN_MAIN} ${LFC_GEN_SOURCES}) target_include_directories(${MAIN_TARGET} PRIVATE ${LFC_GEN_INCLUDE_DIRS}) target_link_libraries(${MAIN_TARGET} PUBLIC reactor-uc) diff --git a/include/reactor-uc/macros_internal.h b/include/reactor-uc/macros_internal.h index 300d6554..aee1b4c3 100644 --- a/include/reactor-uc/macros_internal.h +++ b/include/reactor-uc/macros_internal.h @@ -10,7 +10,8 @@ * */ #define LF_TRIGGER_REGISTER_EFFECT(trigger, effect) \ - do { \ + do { \ + assert((effect) != NULL); \ assert((trigger)->effects.num_registered < (trigger)->effects.size); \ (trigger)->effects.reactions[(trigger)->effects.num_registered++] = (effect); \ } while (0) @@ -18,7 +19,8 @@ // Register a reaction as a source of a trigger. `trigger` must be a pointer to // a derived Trigger type. #define LF_TRIGGER_REGISTER_SOURCE(trigger, source) \ - do { \ + do { \ + assert((source) != NULL); \ assert((trigger)->sources.num_registered < (trigger)->sources.size); \ (trigger)->sources.reactions[(trigger)->sources.num_registered++] = (source); \ } while (0) @@ -27,6 +29,7 @@ // a derived Trigger type. #define LF_TRIGGER_REGISTER_OBSERVER(trigger, observer) \ do { \ + assert((observer) != NULL); \ assert((trigger)->observers.num_registered < (trigger)->observers.size); \ (trigger)->observers.reactions[(trigger)->observers.num_registered++] = (observer); \ } while (0) diff --git a/include/reactor-uc/network_channel.h b/include/reactor-uc/network_channel.h index 1e21a319..9745ec68 100644 --- a/include/reactor-uc/network_channel.h +++ b/include/reactor-uc/network_channel.h @@ -150,9 +150,9 @@ struct AsyncNetworkChannel { #endif #elif defined(PLATFORM_PICO) -#ifdef NETWORK_CHANNEL_TCP_POSIX -#error "NETWORK_POSIX_TCP not supported on PICO" -#endif +//#ifdef NETWORK_CHANNEL_TCP_POSIX +//#error "NETWORK_POSIX_TCP not supported on PICO" +//#endif #ifdef NETWORK_CHANNEL_UART #include "platform/pico/uart_channel.h" #endif diff --git a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcCmakeGenerator.kt b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcCmakeGenerator.kt index a45e9ee5..3c7e4b9d 100644 --- a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcCmakeGenerator.kt +++ b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcCmakeGenerator.kt @@ -52,18 +52,18 @@ abstract class UcCmakeGenerator( |set(LF_MAIN_TARGET ${mainTarget}) |set(CMAKE_BUILD_TYPE ${targetConfig.getOrDefault(BuildTypeProperty.INSTANCE)}) |set(PLATFORM POSIX CACHE STRING "Target platform") - |include($S{CMAKE_CURRENT_SOURCE_DIR}/src-gen/$S{LF_MAIN}/$S{FEDERATE}/Include.cmake) - |#include(./Include.cmake) - |#add_executable($S{LF_MAIN_TARGET} $S{LFC_GEN_SOURCES} $S{LFC_GEN_MAIN}) - |#install(TARGETS $S{LF_MAIN_TARGET} - |# RUNTIME DESTINATION $S{CMAKE_INSTALL_BINDIR} - |# OPTIONAL - |#) - |#add_compile_definitions("LF_LOG_LEVEL_ALL=LF_LOG_LEVEL_${targetConfig.getOrDefault(LoggingProperty.INSTANCE).name.uppercase()}") - |#add_compile_definitions($S{LFC_GEN_COMPILE_DEFS}) - |#add_subdirectory($S{REACTOR_UC_PATH}) - |#target_link_libraries($S{LF_MAIN_TARGET} PRIVATE reactor-uc) - |#target_include_directories($S{LF_MAIN_TARGET} PRIVATE $S{LFC_GEN_INCLUDE_DIRS}) + |#include($S{CMAKE_CURRENT_SOURCE_DIR}/src-gen/$S{LF_MAIN}/$S{FEDERATE}/Include.cmake) + |include(./Include.cmake) + |add_executable($S{LF_MAIN_TARGET} $S{LFC_GEN_SOURCES} $S{LFC_GEN_MAIN}) + |install(TARGETS $S{LF_MAIN_TARGET} + | RUNTIME DESTINATION $S{CMAKE_INSTALL_BINDIR} + | OPTIONAL + |) + |add_compile_definitions("LF_LOG_LEVEL_ALL=LF_LOG_LEVEL_${targetConfig.getOrDefault(LoggingProperty.INSTANCE).name.uppercase()}") + |add_compile_definitions($S{LFC_GEN_COMPILE_DEFS}) + |add_subdirectory($S{REACTOR_UC_PATH}) + |target_link_libraries($S{LF_MAIN_TARGET} PRIVATE reactor-uc) + |target_include_directories($S{LF_MAIN_TARGET} PRIVATE $S{LFC_GEN_INCLUDE_DIRS}) ${" |"..(includeFiles?.joinWithLn { "include(\"$it\")" } ?: "")} """ .trimMargin() diff --git a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcNetworkChannel.kt b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcNetworkChannel.kt index 2470c8b7..ec6db3c7 100644 --- a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcNetworkChannel.kt +++ b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcNetworkChannel.kt @@ -279,7 +279,7 @@ abstract class UcNetworkChannel( UART -> { val srcEp = (srcIf as UcUARTInterface).createEndpoint() - val destEp = (srcIf as UcUARTInterface).createEndpoint() + val destEp = (destIf as UcUARTInterface).createEndpoint() channel = UcUARTChannel(srcEp, destEp) } @@ -327,7 +327,7 @@ class UcUARTChannel(private val uart_src: UcUARTEndpoint, private val uart_dest: "Uart${if (uart_src.async) "Async" else "Polled"}Channel_ctor(&self->channel, ${uart_src.uart_device}, ${uart_src.baud_rate}, UC_${uart_src.data_bits}, UC_${uart_src.parity}, UC_${uart_src.stop_bits});" override fun generateChannelCtorDest() = - "Uart${if (uart_src.async) "Async" else "Polled"}Channel_ctor(&self->channel, ${uart_dest.uart_device}, ${uart_dest.baud_rate}, UC_${uart_dest.data_bits}, UC_${uart_dest.parity}, UC_${uart_dest.stop_bits});" + "Uart${if (uart_dest.async) "Async" else "Polled"}Channel_ctor(&self->channel, ${uart_dest.uart_device}, ${uart_dest.baud_rate}, UC_${uart_dest.data_bits}, UC_${uart_dest.parity}, UC_${uart_dest.stop_bits});" override val codeType: String get() = diff --git a/src/network_channel.c b/src/network_channel.c index 76381dc7..67b4b90a 100644 --- a/src/network_channel.c +++ b/src/network_channel.c @@ -21,9 +21,9 @@ #endif #elif defined(PLATFORM_PICO) -#ifdef NETWORK_CHANNEL_TCP_POSIX -#error "NETWORK_POSIC_TCP not supported on PICO" -#endif +//#ifdef NETWORK_CHANNEL_TCP_POSIX +//#error "NETWORK_POSIC_TCP not supported on PICO" +//#endif // #ifdef NETWORK_CHANNEL_UART #include "platform/pico/uart_channel.c" // #endif diff --git a/src/platform/pico/pico.c b/src/platform/pico/pico.c index 6498904e..1ba99c85 100644 --- a/src/platform/pico/pico.c +++ b/src/platform/pico/pico.c @@ -12,7 +12,7 @@ void Platform_vprintf(const char *fmt, va_list args) { vprintf(fmt, args); } lf_ret_t PlatformPico_initialize(Platform *self) { PlatformPico *p = (PlatformPico *)self; - stdio_init_all(); + //stdio_init_all(); // init sync structs critical_section_init(&p->crit_sec); sem_init(&p->sem, 0, 1); diff --git a/src/platform/pico/uart_channel.c b/src/platform/pico/uart_channel.c index f244e6b8..6f94c9bd 100644 --- a/src/platform/pico/uart_channel.c +++ b/src/platform/pico/uart_channel.c @@ -197,19 +197,25 @@ void UartPolledChannel_ctor(UartPolledChannel *self, uint32_t uart_device, uint3 self->super.super.free = UartPolledChannel_free; self->super.poll = UartPolledChannel_poll; + int rx_pin; + int tx_pin; if (uart_device == 0) { self->uart_device = uart0; uart_channel_0 = self; + rx_pin = 1; + tx_pin = 0; } else if (uart_device == 1) { self->uart_device = uart1; uart_channel_1 = self; + rx_pin = 9; + tx_pin = 8; } else { throw("The Raspberry Pi pico only supports uart devices 0 and 1."); } uart_init(self->uart_device, 2400); - gpio_set_function(4, UART_FUNCSEL_NUM(self->uart_device, 4)); - gpio_set_function(5, UART_FUNCSEL_NUM(self->uart_device, 5)); + gpio_set_function(tx_pin, UART_FUNCSEL_NUM(self->uart_device, tx_pin)); + gpio_set_function(rx_pin, UART_FUNCSEL_NUM(self->uart_device, rx_pin)); int actual = uart_set_baudrate(self->uart_device, baud); if (actual != (int)baud) { diff --git a/src/reaction.c b/src/reaction.c index dc08413f..2705e691 100644 --- a/src/reaction.c +++ b/src/reaction.c @@ -25,7 +25,7 @@ int calculate_port_level(Port *port) { } } - for (size_t i = 0; i < port->sources.size; i++) { + for (size_t i = 0; i < port->sources.num_registered; i++) { Reaction *source = port->sources.reactions[i]; validate(source); int source_level = source->get_level(source); @@ -34,6 +34,7 @@ int calculate_port_level(Port *port) { } } + printf("Input port %p has level %d", port, current); LF_DEBUG(ENV, "Input port %p has level %d", port, current); return current; } diff --git a/src/reactor.c b/src/reactor.c index 8982f4aa..3f43a30c 100644 --- a/src/reactor.c +++ b/src/reactor.c @@ -30,7 +30,6 @@ void Reactor_validate(Reactor *self) { if (trigger->type == TRIG_INPUT || trigger->type == TRIG_OUTPUT) { Port *port = (Port *)trigger; validate(port->effects.num_registered == port->effects.size); - validate(port->sources.num_registered == port->sources.size); validate(port->conns_out_size >= port->conns_out_registered); for (size_t i = 0; i < port->conns_out_registered; i++) { Connection *conn = port->conns_out[i];