From 8b54f922fe7c7c037dae2b349baf2e05b2b16c57 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Sun, 2 Jun 2024 09:34:32 +0200 Subject: [PATCH 1/9] First step towards getting NRF52 codegen working again --- low_level_platform/impl/CMakeLists.txt | 2 +- low_level_platform/impl/src/lf_nrf52_support.c | 5 ++--- platform/impl/CMakeLists.txt | 10 ++++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/low_level_platform/impl/CMakeLists.txt b/low_level_platform/impl/CMakeLists.txt index 88961d5c0..aa1fe701d 100644 --- a/low_level_platform/impl/CMakeLists.txt +++ b/low_level_platform/impl/CMakeLists.txt @@ -109,7 +109,7 @@ target_link_libraries(lf-low-level-platform-impl PRIVATE lf::low-level-platform- target_link_libraries(lf-low-level-platform-impl PUBLIC lf-logging-api) target_compile_definitions(lf-low-level-platform-impl PUBLIC PLATFORM_${CMAKE_SYSTEM_NAME}) -message(STATUS "Applying preprocessor definitions to platform...") +message(STATUS "Applying preprocessor definitions to low-level-platform...") macro(low_level_platform_define X) if(DEFINED ${X}) message(STATUS ${X}=${${X}}) diff --git a/low_level_platform/impl/src/lf_nrf52_support.c b/low_level_platform/impl/src/lf_nrf52_support.c index f0147a7d3..3d187c99f 100644 --- a/low_level_platform/impl/src/lf_nrf52_support.c +++ b/low_level_platform/impl/src/lf_nrf52_support.c @@ -39,9 +39,8 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include "platform/lf_nrf52_support.h" -#include "../platform.h" -#include "../utils/util.h" -#include "../tag.h" +#include "low_level_platform.h" +#include "tag.h" #include "nrf.h" #include "nrfx_timer.h" diff --git a/platform/impl/CMakeLists.txt b/platform/impl/CMakeLists.txt index bc12ff11c..32753e7eb 100644 --- a/platform/impl/CMakeLists.txt +++ b/platform/impl/CMakeLists.txt @@ -17,3 +17,13 @@ endif() add_library(lf::platform-impl ALIAS lf-platform-impl) target_link_libraries(lf-platform-impl PRIVATE lf::low-level-platform-api) target_link_libraries(lf-platform-impl PRIVATE lf::platform-api) +message(STATUS "Applying preprocessor definitions to platform...") +macro(platform_define X) + if(DEFINED ${X}) + message(STATUS ${X}=${${X}}) + target_compile_definitions(lf-platform-impl PUBLIC ${X}=${${X}}) + endif(DEFINED ${X}) +endmacro() +platform_define(LF_SINGLE_THREADED) +platform_define(LOG_LEVEL) +platform_define(MODAL_REACTORS) From dcb0889ddadcb3d67115373a7c7ef8c1bb6a0d12 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Mon, 3 Jun 2024 08:50:41 +0200 Subject: [PATCH 2/9] Do not declare variables within switch --- lib/schedule.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/schedule.c b/lib/schedule.c index 5aa9fd528..168352373 100644 --- a/lib/schedule.c +++ b/lib/schedule.c @@ -234,6 +234,7 @@ trigger_handle_t lf_schedule_trigger(environment_t* env, trigger_t* trigger, int // If the event is early, see which policy applies. if (earliest_time > intended_tag.time) { LF_PRINT_DEBUG("Event is early."); + event_t *dummy, *found; switch (trigger->policy) { case drop: LF_PRINT_DEBUG("Policy is drop. Dropping the event."); @@ -247,10 +248,10 @@ trigger_handle_t lf_schedule_trigger(environment_t* env, trigger_t* trigger, int // If the event with the previous tag is still on the event // queue, then replace the token. To find this event, we have // to construct a dummy event_t struct. - event_t* dummy = lf_get_new_event(env); + dummy = lf_get_new_event(env); dummy->trigger = trigger; dummy->base.tag = trigger->last_tag; - event_t* found = (event_t*)pqueue_tag_find_equal_same_tag(env->event_q, (pqueue_tag_element_t*)dummy); + found = (event_t*)pqueue_tag_find_equal_same_tag(env->event_q, (pqueue_tag_element_t*)dummy); if (found != NULL) { // Recycle the existing token and the new event From 93fab3523fc603e17ae4599cea64115b59142736 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Mon, 3 Jun 2024 08:51:04 +0200 Subject: [PATCH 3/9] Do not use util functions in low_level_platform code --- low_level_platform/impl/src/lf_nrf52_support.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/low_level_platform/impl/src/lf_nrf52_support.c b/low_level_platform/impl/src/lf_nrf52_support.c index 3d187c99f..d48ba1383 100644 --- a/low_level_platform/impl/src/lf_nrf52_support.c +++ b/low_level_platform/impl/src/lf_nrf52_support.c @@ -257,7 +257,7 @@ int _lf_interruptable_sleep_until_locked(environment_t* env, instant_t wakeup_ti if (!_lf_async_event) { return 0; } else { - LF_PRINT_DEBUG("Sleep got interrupted...\n"); + // LF_PRINT_DEBUG("Sleep got interrupted...\n"); return -1; } } From 6e8ecd8b062f42a02e220e66f2caea976abd7b55 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Fri, 7 Jun 2024 11:31:13 +0200 Subject: [PATCH 4/9] Enable and disable interrupts without soft device --- .../impl/src/lf_nrf52_support.c | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/low_level_platform/impl/src/lf_nrf52_support.c b/low_level_platform/impl/src/lf_nrf52_support.c index d48ba1383..b50180141 100644 --- a/low_level_platform/impl/src/lf_nrf52_support.c +++ b/low_level_platform/impl/src/lf_nrf52_support.c @@ -73,9 +73,9 @@ static const nrfx_timer_t g_lf_timer_inst = NRFX_TIMER_INSTANCE(3); static volatile uint32_t _lf_time_us_high = 0; /** - * Flag passed to sd_nvic_critical_region_* + * Flag used to count nested interrupt disables. */ -uint8_t _lf_nested_region = 0; +static volatile uint8_t _lf_nested_count = 0; /** * @brief Handle LF timer interrupts @@ -266,14 +266,27 @@ int _lf_interruptable_sleep_until_locked(environment_t* env, instant_t wakeup_ti * @brief Enter critical section. Let NRF Softdevice handle nesting * @return int */ -int lf_enable_interrupts_nested() { return sd_nvic_critical_region_enter(&_lf_nested_region); } +int lf_enable_interrupts_nested() { + if (_lf_nested_count == 0) return 1; // Error. Interrupts have not been disabled. + _lf_nested_count--; + __enable_irq(); + // FIXME: If softdevice is enabled, do the following: + // return sd_nvic_critical_region_exit(&_lf_nested_count); + return 0; +} /** * @brief Exit citical section. Let NRF SoftDevice handle nesting * * @return int */ -int lf_disable_interrupts_nested() { return sd_nvic_critical_region_exit(_lf_nested_region); } +int lf_disable_interrupts_nested() { + _lf_nested_count++; + __disable_irq(); + // FIXME: If softdevice is enabled, do the following: + // return sd_nvic_critical_region_enter(_lf_nested_count); + return 0; +} /** * @brief Set global flag to true so that sleep will return when woken From f817d4b8842f0d230c9e7807bb74ed6a71580b0f Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Tue, 11 Jun 2024 08:32:07 +0200 Subject: [PATCH 5/9] Correct error in incrementing high-order time word --- low_level_platform/impl/src/lf_nrf52_support.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/low_level_platform/impl/src/lf_nrf52_support.c b/low_level_platform/impl/src/lf_nrf52_support.c index b50180141..beee69b0f 100644 --- a/low_level_platform/impl/src/lf_nrf52_support.c +++ b/low_level_platform/impl/src/lf_nrf52_support.c @@ -94,7 +94,7 @@ void lf_timer_event_handler(nrf_timer_event_t event_type, void* p_context) { if (event_type == NRF_TIMER_EVENT_COMPARE2) { _lf_sleep_interrupted = false; } else if (event_type == NRF_TIMER_EVENT_COMPARE3) { - _lf_time_us_high = +1; + _lf_time_us_high += 1; } } @@ -192,13 +192,19 @@ static void lf_busy_wait_until(instant_t wakeup_time) { } /** - * @brief Sleep until the given wakeup time. There are a couple of edge cases to consider + * @brief Sleep until the given wakeup time. + * + * There are a couple of edge cases to consider: * 1. Wakeup time is already past * 2. Implied sleep duration is below `LF_MAX_SLEEP_NS` threshold * 3. Implied sleep duration is above `LF_MAX_SLEEP_NS` limit + * + * This function assumes the caller is in a critical section, so interrupts are disabled. + * It may exit the critical section while waiting for an event, but it will re-enter the + * critical section before returning. * * @param wakeup_time The time instant at which to wake up. - * @return int 0 if sleep completed, or -1 if it was interrupted. + * @return 0 if sleep completed, or -1 if it was interrupted. */ int _lf_interruptable_sleep_until_locked(environment_t* env, instant_t wakeup_time) { instant_t now; @@ -264,7 +270,7 @@ int _lf_interruptable_sleep_until_locked(environment_t* env, instant_t wakeup_ti /** * @brief Enter critical section. Let NRF Softdevice handle nesting - * @return int + * @return 0 */ int lf_enable_interrupts_nested() { if (_lf_nested_count == 0) return 1; // Error. Interrupts have not been disabled. From 169f6ee59eec3996baf36690b41fa545f4d5ddc4 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Tue, 11 Jun 2024 08:32:22 +0200 Subject: [PATCH 6/9] Standardize capitalization of nRF52. --- low_level_platform/api/CMakeLists.txt | 2 +- low_level_platform/impl/CMakeLists.txt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/low_level_platform/api/CMakeLists.txt b/low_level_platform/api/CMakeLists.txt index 9803476d6..9f2172bce 100644 --- a/low_level_platform/api/CMakeLists.txt +++ b/low_level_platform/api/CMakeLists.txt @@ -3,7 +3,7 @@ target_include_directories(lf-low-level-platform-api INTERFACE ${CMAKE_CURRENT_L add_library(lf::low-level-platform-api ALIAS lf-low-level-platform-api) target_link_libraries(lf-low-level-platform-api INTERFACE lf::tag-api) -if(${CMAKE_SYSTEM_NAME} STREQUAL "Nrf52") +if(${CMAKE_SYSTEM_NAME} STREQUAL "nRF52") target_compile_definitions(lf-low-level-platform-api INTERFACE PLATFORM_NRF52) elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Zephyr") target_compile_definitions(lf-low-level-platform-api INTERFACE PLATFORM_ZEPHYR) diff --git a/low_level_platform/impl/CMakeLists.txt b/low_level_platform/impl/CMakeLists.txt index aa1fe701d..c0f2d8bb5 100644 --- a/low_level_platform/impl/CMakeLists.txt +++ b/low_level_platform/impl/CMakeLists.txt @@ -22,7 +22,7 @@ elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") ${CMAKE_CURRENT_LIST_DIR}/src/lf_macos_support.c ${CMAKE_CURRENT_LIST_DIR}/src/lf_atomic_gcc_clang.c ) -elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Nrf52") +elseif(${CMAKE_SYSTEM_NAME} STREQUAL "nRF52") set(LF_LOW_LEVEL_PLATFORM_FILES ${CMAKE_CURRENT_LIST_DIR}/src/lf_nrf52_support.c ${CMAKE_CURRENT_LIST_DIR}/src/lf_atomic_irq.c @@ -45,7 +45,7 @@ elseif(${CMAKE_SYSTEM_NAME} STREQUAL "FlexPRET") ${CMAKE_CURRENT_LIST_DIR}/src/lf_atomic_irq.c ) else() - message(FATAL_ERROR "Your platform is not supported! The C target supports FlexPRET, Linux, MacOS, Nrf52, RP2040, Windows, and Zephyr.") + message(FATAL_ERROR "Your platform is not supported! The C target supports FlexPRET, Linux, MacOS, nRF52, RP2040, Windows, and Zephyr.") endif() list(APPEND LF_LOW_LEVEL_PLATFORM_FILES ${CMAKE_CURRENT_LIST_DIR}/src/lf_platform_util.c) From aa128fb3eede9d7582497f21180f4e73d3bf4e12 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Tue, 11 Jun 2024 09:06:12 +0200 Subject: [PATCH 7/9] Format --- low_level_platform/impl/src/lf_nrf52_support.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/low_level_platform/impl/src/lf_nrf52_support.c b/low_level_platform/impl/src/lf_nrf52_support.c index beee69b0f..a38f094da 100644 --- a/low_level_platform/impl/src/lf_nrf52_support.c +++ b/low_level_platform/impl/src/lf_nrf52_support.c @@ -193,12 +193,12 @@ static void lf_busy_wait_until(instant_t wakeup_time) { /** * @brief Sleep until the given wakeup time. - * + * * There are a couple of edge cases to consider: * 1. Wakeup time is already past * 2. Implied sleep duration is below `LF_MAX_SLEEP_NS` threshold * 3. Implied sleep duration is above `LF_MAX_SLEEP_NS` limit - * + * * This function assumes the caller is in a critical section, so interrupts are disabled. * It may exit the critical section while waiting for an event, but it will re-enter the * critical section before returning. @@ -273,7 +273,8 @@ int _lf_interruptable_sleep_until_locked(environment_t* env, instant_t wakeup_ti * @return 0 */ int lf_enable_interrupts_nested() { - if (_lf_nested_count == 0) return 1; // Error. Interrupts have not been disabled. + if (_lf_nested_count == 0) + return 1; // Error. Interrupts have not been disabled. _lf_nested_count--; __enable_irq(); // FIXME: If softdevice is enabled, do the following: From 1e0d00a5d86ed40a1f1458cabecbd5edbf8126f0 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Sun, 23 Jun 2024 10:53:17 -0700 Subject: [PATCH 8/9] Update to use soft device by default --- .../api/platform/lf_nrf52_support.h | 11 +++++----- .../impl/src/lf_nrf52_support.c | 20 +++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/low_level_platform/api/platform/lf_nrf52_support.h b/low_level_platform/api/platform/lf_nrf52_support.h index 8f9b46620..b695a0543 100644 --- a/low_level_platform/api/platform/lf_nrf52_support.h +++ b/low_level_platform/api/platform/lf_nrf52_support.h @@ -1,4 +1,4 @@ -/* nRF52832 API support for the C target of Lingua Franca. */ +/* nRF52 API support for the C target of Lingua Franca. */ /************* Copyright (c) 2021, The University of California at Berkeley. @@ -24,11 +24,12 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***************/ -/** nrf52 API support for the C target of Lingua Franca. +/** + * nRF52 API support for the C target of Lingua Franca. * - * @author{Soroush Bateni } - * @author{Abhi Gundrala } - * @author{Erling Rennemo Jellum } + * @author{Soroush Bateni } + * @author{Abhi Gundrala } + * @author{Erling Rennemo Jellum } */ #ifndef LF_NRF52_SUPPORT_H diff --git a/low_level_platform/impl/src/lf_nrf52_support.c b/low_level_platform/impl/src/lf_nrf52_support.c index a38f094da..a5765e859 100644 --- a/low_level_platform/impl/src/lf_nrf52_support.c +++ b/low_level_platform/impl/src/lf_nrf52_support.c @@ -268,6 +268,9 @@ int _lf_interruptable_sleep_until_locked(environment_t* env, instant_t wakeup_ti } } +// Definition required by sd_nvic_critical_region_enter() and exit() below. +nrf_nvic_state_t nrf_nvic_state = {0}; + /** * @brief Enter critical section. Let NRF Softdevice handle nesting * @return 0 @@ -276,10 +279,10 @@ int lf_enable_interrupts_nested() { if (_lf_nested_count == 0) return 1; // Error. Interrupts have not been disabled. _lf_nested_count--; - __enable_irq(); - // FIXME: If softdevice is enabled, do the following: - // return sd_nvic_critical_region_exit(&_lf_nested_count); - return 0; + return sd_nvic_critical_region_exit(0); + // FIXME: If softdevice is not enabled, do the following instead of above: + // __enable_irq(); + // return 0; } /** @@ -289,10 +292,11 @@ int lf_enable_interrupts_nested() { */ int lf_disable_interrupts_nested() { _lf_nested_count++; - __disable_irq(); - // FIXME: If softdevice is enabled, do the following: - // return sd_nvic_critical_region_enter(_lf_nested_count); - return 0; + uint8_t success = 0; + return sd_nvic_critical_region_enter(&success); + // FIXME: If softdevice is not enabled, do the following instead of the above: + // __disable_irq(); + // return 0; } /** From 5a19d6f57fe4a9bd1985201f5e5056c42e98c142 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Sun, 23 Jun 2024 11:43:33 -0700 Subject: [PATCH 9/9] Format --- low_level_platform/api/platform/lf_nrf52_support.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/low_level_platform/api/platform/lf_nrf52_support.h b/low_level_platform/api/platform/lf_nrf52_support.h index b695a0543..bd657d224 100644 --- a/low_level_platform/api/platform/lf_nrf52_support.h +++ b/low_level_platform/api/platform/lf_nrf52_support.h @@ -24,7 +24,7 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ***************/ -/** +/** * nRF52 API support for the C target of Lingua Franca. * * @author{Soroush Bateni }