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 STM32 #302

Open
wants to merge 46 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
6de22f6
initial test for support for STM32
naichenzhao Nov 7, 2023
256abcc
Point to lingua-franca stm32 branch
lhstrh Nov 7, 2023
92fcfbe
fixed typo
naichenzhao Nov 11, 2023
dca5be8
please merge
naichenzhao Nov 11, 2023
89986a9
hard coded STM32 support
naichenzhao Nov 11, 2023
436b36b
we dont need STM_main anymore
naichenzhao Nov 19, 2023
bcbbf43
updated files to move stuff out of core cmake
naichenzhao Nov 22, 2023
48be057
fixed printing
naichenzhao Dec 7, 2023
31d62b2
changed naming
naichenzhao Dec 18, 2023
73aea2e
updated sleep functions
naichenzhao Jul 15, 2024
7899e6b
updated delay functions
naichenzhao Jul 15, 2024
d11f3ff
rebased in main
naichenzhao Aug 5, 2024
58d2323
change
naichenzhao Aug 5, 2024
5ae3be6
fixed typo
naichenzhao Nov 11, 2023
4bb9231
bruh
naichenzhao Aug 5, 2024
9527537
we dont need STM_main anymore
naichenzhao Nov 19, 2023
8a97bcd
changes
naichenzhao Aug 5, 2024
c46fbcc
fixed printing
naichenzhao Dec 7, 2023
b3097a8
changed naming
naichenzhao Dec 18, 2023
f92307f
updated sleep functions
naichenzhao Jul 15, 2024
f2554dd
updated delay functions
naichenzhao Jul 15, 2024
03124a5
merge
naichenzhao Aug 5, 2024
6d2964c
Merge branch 'stm32' of github.com:lf-lang/reactor-c into stm32
naichenzhao Aug 5, 2024
13ca0d8
Merge branch 'main' of github.com:lf-lang/reactor-c into stm32
naichenzhao Aug 31, 2024
cd0c9c1
quick commit
naichenzhao Aug 31, 2024
6d8d4db
Suppress unused parameter error
edwardalee Aug 9, 2024
bb75db6
Suppress unused parameter error
edwardalee Aug 9, 2024
6a66f47
deleted artifact from rebase
naichenzhao Aug 31, 2024
a285e52
works with newest version
naichenzhao Aug 31, 2024
6ba0f42
did formatting
naichenzhao Sep 7, 2024
a22a4fd
Update low_level_platform/api/platform/lf_STM32f4_support.h
naichenzhao Sep 16, 2024
42fc271
Update low_level_platform/api/platform/lf_STM32f4_support.h
naichenzhao Sep 16, 2024
895a8fb
Update low_level_platform/impl/src/lf_STM32f4_support.c
naichenzhao Sep 16, 2024
0d3af6d
Update low_level_platform/api/platform/lf_STM32f4_support.h
naichenzhao Sep 16, 2024
50603d7
Update low_level_platform/impl/src/lf_STM32f4_support.c
naichenzhao Sep 16, 2024
cba9193
resolved some more comments
naichenzhao Sep 17, 2024
cf36f59
changed macro
naichenzhao Sep 24, 2024
6e9e3c7
Merge branch 'main' into stm32
naichenzhao Oct 4, 2024
6974d8e
Merge branch 'main' into stm32
naichenzhao Oct 5, 2024
a2e1e4b
Merge branch 'stm32' of https://github.com/lf-lang/reactor-c into stm32
naichenzhao Oct 5, 2024
68ec2d7
Run clang-format
erlingrj Oct 5, 2024
2d23659
Update lf-ref to the new lf branch
erlingrj Oct 5, 2024
128e9a4
Merge branch 'main' into stm32
erlingrj Oct 29, 2024
22b871d
Refactor comments
erlingrj Oct 29, 2024
68bd26a
Dont rely on hardcoded relative path to the "STM_SDK"
erlingrj Oct 29, 2024
335d52b
Remove all the STM32 cmake setup from reactor-c. This happens outside
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
36 changes: 15 additions & 21 deletions core/platform/lf_STM32f4_support.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#if defined(PLATFORM_STM32F4)
/*************
I hope this software works LOL
***************/


#include "lf_STM32f4_support.h"
#include "platform.h"
Expand All @@ -15,15 +13,14 @@
static volatile bool _lf_sleep_interrupted = false;
static volatile bool _lf_async_event = false;

#define LF_MAX_SLEEP_NS USEC(UINT32_MAX)
#define LF_MIN_SLEEP_NS USEC(5)

// nested critical section counter
static uint32_t _lf_num_nested_crit_sec = 0;

// Timer upper half (for overflow)
static uint32_t _lf_time_us_high = 0;

#define LF_MIN_SLEEP_NS 10

// Combine 2 32bit works to a 64 bit word (Takes from nrf52 support)
#define COMBINE_HI_LO(hi, lo) ((((uint64_t)hi) << 32) | ((uint64_t)lo))

Expand All @@ -34,8 +31,7 @@ void Error_Handler();
// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
// | Code for timer functions
// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +

// We use timer 5 for our clock (probably better than fucking with sysTick)
// We use timer 5 for our clock (probably better than dealing with sysTick)
void _lf_initialize_clock(void) {
// Standard initializations from generated code
HAL_Init();
Expand Down Expand Up @@ -63,24 +59,24 @@ void _lf_initialize_clock(void) {
TIM5->CNT = 0xFFFFFFFE;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A timer prescaler is typically loaded without a guard, meaning that it won't take event until the next update event of the clock. This is meant to ensure the prescaler doesn't take effect mid-phase, which could cause strange artifacts in your application. When you enable the peripheral, it will start with whatever was in the prescaler register at the time it was enabled, possibly leading to a large delay before the timer ISR fires. The new prescaler value will be loaded after the preceding update event.

I'm not intimately familiar with this chipset, but generally hardware peripherals are configured first and then enabled. You may consider loading the prescaler before you enable the counter. If that doesn't work, a reset is fine, but I suggest a simpler comment "reset to effect new prescaler".

This is important in safety-critical systems, you need to know when your I/O is stable and safe to use. This is known as "I/O stability".

}

/**
* ISR for handling timer overflow -> We increment the upper timer
*/
// ISR for handling timer overflow -> We increment the upper timer
void TIM5_IRQHandler(void) {
if (TIM5->SR & (1 << 1)) {
TIM5->SR &= ~(1 << 1);
_lf_time_us_high += 1;
}
}


/**
* Write the time since boot into time variable
*/
int _lf_clock_now(instant_t *t)
{
// Timer is cooked
if (!t) {
return -1;
return 1;

}
// Get the current microseconds from TIM5
uint32_t _lf_time_us_low = TIM5->CNT;
Expand All @@ -101,7 +97,6 @@ int lf_sleep(interval_t sleep_duration) {

_lf_clock_now(&current_time);
target_time = current_time + sleep_duration;

// HAL_Delay only supports miliseconds. We try to use that for as long as possible
// before switching to another meothd for finer tuned delay times
long delaytime_ms = sleep_duration / 1000000;
Expand Down Expand Up @@ -158,8 +153,8 @@ int _lf_interruptable_sleep_until_locked(environment_t *env, instant_t wakeup_ti
if (!_lf_async_event) {
return 0;
} else {
LF_PRINT_DEBUG(" *The STM32 rises from sleep* \n");
return -1;
return 1;

}

}
Expand All @@ -182,7 +177,8 @@ int lf_disable_interrupts_nested() {

// enables the IRQ (checks if other programs still want it disabled first)
int lf_enable_interrupts_nested() {
// Somebody fucked up, LOL
// Left the critical section more often than it was entered.

if (_lf_num_nested_crit_sec <= 0) {
return 1;
}
Expand All @@ -202,17 +198,14 @@ int _lf_single_threaded_notify_of_event() {
return 0;
}

int test_func(void) {
return 5;
}

// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
// | Other functions I found -> taken from the generated main.c
// | Other functions -> taken from the generated main.c
// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
void lf_SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};


/** Configure the main internal regulator output voltage
naichenzhao marked this conversation as resolved.
Show resolved Hide resolved
*/
__HAL_RCC_PWR_CLK_ENABLE();
Expand Down Expand Up @@ -249,6 +242,7 @@ void Error_Handler(void) {
while (1) {
naichenzhao marked this conversation as resolved.
Show resolved Hide resolved
}
/* USER CODE END Error_Handler_Debug */

}

#endif
Loading