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 42 commits into
base: main
Choose a base branch
from
Open

Support for STM32 #302

wants to merge 42 commits into from

Conversation

naichenzhao
Copy link
Collaborator

@naichenzhao naichenzhao commented Nov 7, 2023

Summary by CodeRabbit

  • New Features

    • Added support for the STM32F4 platform, enhancing functionality related to clock management, sleep operations, interrupt handling, and system configuration.
  • Documentation

    • Updated documentation to include the term "single-threaded."

lingua-franca-ref.txt Outdated Show resolved Hide resolved
@lhstrh lhstrh changed the title initial test for support for STM32 Support for STM32 Feb 2, 2024
@lhstrh lhstrh marked this pull request as ready for review February 2, 2024 21:41
@petervdonovan
Copy link
Contributor

@lhstrh marked this as ready for review, but I'm not sure I remember why since IIRC we agreed that we are waiting for a cross-compilation smoke test (just to show STM32 support building successfully on a regular laptop). I will review this when the tests are passing and such a smoke test is available; feel free to ping me if I forget.

@lhstrh
Copy link
Member

lhstrh commented Feb 4, 2024

I was being optimistic. We hit some snags with setting up tests.

lhstrh
lhstrh previously requested changes Mar 16, 2024
Copy link
Member

@lhstrh lhstrh left a comment

Choose a reason for hiding this comment

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

@naichenzhao, based on a cursory review, I think that we should try to get this merged soon, but, please:

  • sanitize your comments :-) (suggestions provided)
  • resolve the conflicts

@elgeeko1 (ex-BlackRock Neurotech) is willing to do a deeper review, which you might appreciate given the emphasis of safety in your project. If you can do the requested clean ups this weekend, he'll complete the review early next week. Let's aim to have this merged by the end of next week.

core/platform/lf_STM32f4_support.c Outdated Show resolved Hide resolved
include/core/platform/lf_STM32f4_support.h Outdated Show resolved Hide resolved
core/platform/lf_STM32f4_support.c Outdated Show resolved Hide resolved
core/platform/lf_STM32f4_support.c Outdated Show resolved Hide resolved
@elgeeko1 elgeeko1 self-requested a review April 12, 2024 05:25
@elgeeko1 elgeeko1 self-assigned this Apr 12, 2024
Copy link
Collaborator

@elgeeko1 elgeeko1 left a comment

Choose a reason for hiding this comment

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

I added comments inline, and my general comments follow. At a high level, the most difficult aspects of this implementation are understanding concurrent access to volatile variables and enabling/disabling interrupts. Without more documentation, it's hard to know if these mechanisms are safe.

  1. Reference counting for enabling / disabling interrupts: It's not clear how reference counting is managed, and whether or not it is handled in a way that is safe. Can you add documentation that indicates when this value changes and what could lead to an error?
  2. _lf_single_threaded_notify_of_event is volatile, but it appears it's accessed only by a single-threaded LF application. Is this variable accessed by an ISR?
  3. Unclear ownership of _lf_num_nested_crit_sec: It's unclear when this value is incremented and decremented. Consequently, it's possible that race conditions exist that can result in data corruption (two processes write to the value at the same time). Can you comment more about the use of this variable, who accesses it when, and how to guarantee that it is accessed in a predictable way?
  4. Consider the names lf_critical_section_enter and lf_critical_section_exit instead of lf_disable_interupts_nested and lf_enable_interrupts_nested. This conveys that purpose of the method more clearly.
  5. Inconsistent return values. Zero is consistently success, in some cases 1 is an error, others -1 is an error. Consider standardizing on 1 as success (as it evaluates to true in boolean expressions), and zero as failure.
  6. Remove references to "I" or the thought process that lead you to the code. Instead, document what happens and why it is implemented this way. This is the most concise way for another developer to understand the code.

I hope this is helpful towards hardening your application. I will be happy to review any new updates.

core/platform/lf_STM32f4_support.c Outdated Show resolved Hide resolved
core/platform/lf_STM32f4_support.c Outdated Show resolved Hide resolved
* and force it to reset once. Its really jank but its the only way I could
* find to make it work
*/
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".

core/platform/lf_STM32f4_support.c Outdated Show resolved Hide resolved
core/platform/lf_STM32f4_support.c Outdated Show resolved Hide resolved
core/platform/lf_STM32f4_support.c Outdated Show resolved Hide resolved
#define PRINTF_MICROSTEP "%d"

#define LF_TIME_BUFFER_LENGTH 80
#define _LF_TIMEOUT 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

What units is this #def? Should it be a const instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I kinda just copied this over when I compared it against the rp2040 platform - I wasnt sure what this was used for so I kept it just incase. If its not used in LF proper, ill just delete it

include/core/platform/lf_STM32f4_support.h Outdated Show resolved Hide resolved
include/core/platform.h Outdated Show resolved Hide resolved
core/platform/lf_STM32f4_support.c Outdated Show resolved Hide resolved
Copy link
Contributor

coderabbitai bot commented Jul 15, 2024

Walkthrough

The changes introduce support for the STM32F4 platform in the build system and source code. This includes new configuration files, conditional compilation directives, and specific implementations for clock management, sleep operations, and interrupt handling. The enhancements allow the Lingua Franca runtime to manage hardware-specific functions for STM32F4-based systems, improving its versatility and hardware compatibility.

Changes

File Change Summary
core/platform/CMakeLists.txt Added lf_STM32f4_support.c and support for STM32F4 platform in conditional compilation directives.
core/platform/Platform.cmake Added condition for ${CMAKE_SYSTEM_NAME} being "Stm32" and set LF_PLATFORM_FILE to lf_STM32f4_support.c
core/platform/lf_STM32f4_support.c Introduced support functions for the STM32F4 platform, including clock management and interrupt handling.
include/core/platform.h Added #elif block for PLATFORM_STM32F4 to include lf_STM32f4_support.h.
include/core/platform/lf_STM32f4_support.h Defined macros and included necessary headers for STM32F4 platform support.
lingua-franca-ref.txt Added the term "single-threaded".

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@lhstrh lhstrh added the feature New feature label Aug 5, 2024
@lhstrh lhstrh requested a review from erlingrj September 6, 2024 21:39
@lhstrh lhstrh dismissed stale reviews from elgeeko1 and themself September 6, 2024 21:40

Thanks for you help with this, Jeff!

@lhstrh
Copy link
Member

lhstrh commented Sep 6, 2024

Note that the only test failing is due to code formatting issues. Please run make format to fix.

Copy link
Collaborator

@erlingrj erlingrj left a comment

Choose a reason for hiding this comment

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

Hi @naichenzhao, this looks promising. I have a few requests for changes. I think the most important is to support low-power sleep_until.

Do you have any docs for how to install SDK and the right compiler so I can test it out at my end?

low_level_platform/api/platform/lf_STM32f4_support.h Outdated Show resolved Hide resolved
low_level_platform/api/platform/lf_STM32f4_support.h Outdated Show resolved Hide resolved
low_level_platform/api/platform/lf_STM32f4_support.h Outdated Show resolved Hide resolved
low_level_platform/impl/src/lf_STM32f4_support.c Outdated Show resolved Hide resolved
low_level_platform/impl/src/lf_STM32f4_support.c Outdated Show resolved Hide resolved
Comment on lines +139 to +142
do {
_lf_clock_gettime(&now);
// Exit when the timer is up or there is an exception
} while (!_lf_async_event && (now < wakeup_time));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be low-power sleep, not busy-waiting. Is there anything in the HAL for doing low-power sleep?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There seems to be a low power sleep mode, but its only interrupt based. It seems most timed delay implementations just use a while loop. Im not sure it itll be worth it trying to create some workaround now? or just merge now and maybe improve the implementation later.

low_level_platform/impl/src/lf_STM32f4_support.c Outdated Show resolved Hide resolved
low_level_platform/impl/src/lf_STM32f4_support.c Outdated Show resolved Hide resolved
low_level_platform/impl/src/lf_STM32f4_support.c Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants