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

Refactor logic analyzer #167

Merged
merged 16 commits into from
Apr 7, 2025
Merged

Conversation

bessman
Copy link
Collaborator

@bessman bessman commented Mar 7, 2024

This is the first part of the major refactorization I'm currently working on. It refactors the logic analyzer instrument, as well as the underlying hardware drivers it depends on, including:

  • Input Capture (IC)
  • Direct Memory Access (DMA)
  • Input Change Notification (CN)
  • Timers (TMR)

This pull request breaks any instrument which depend on an interrupt service routine from any of the above drivers. This is because I'm refactoring the interrupts to be more modular by using callbacks. The broken instruments will be fixed as part of the ongoing refactorization.

The new low-level hardware drivers currently reside in src/registers_ng, to be renamed to src/registers once the refactorization is complete. These drivers are designed to be self-contained, i.e. they have no dependencies on any resources other than themselves. This design aims to solve the current spaghetti-ness of the firmware code, where every module depends on several other modules, often in a transitive or circular manner.

Depends on #165 and #166.

Summary by Sourcery

Refactor the logic analyzer instrument and its underlying hardware drivers to improve modularity and reduce code complexity

New Features:

  • Introduced a new modular design for logic analyzer hardware drivers in src/registers_ng

Enhancements:

  • Redesigned low-level hardware drivers to be self-contained
  • Simplified interrupt handling for logic analyzer
  • Improved code organization by separating concerns

Chores:

  • Removed multiple instrument implementations
  • Consolidated hardware driver code
  • Updated build system configuration

@bessman bessman changed the title Refector logic analyzer Refactor logic analyzer Mar 7, 2024
@CloudyPadmal
Copy link
Collaborator

I was thinking about having a separate development branch. That way, we have a protected master or main branch and once development is tested, we can merge the two. What do you think?

@bessman
Copy link
Collaborator Author

bessman commented Mar 7, 2024

In general, I don't think it's necessary for small projects with few committers to have parallel main and development branches. I prefer to have all development go into main, while tagging periodic releases.

On the other hand, merging known broken code (which this is) into main is nasty. So in this case, it makes sense to keep this work in a separate branch until the refactorization is complete.

@bessman bessman closed this Mar 7, 2024
@bessman bessman deleted the refector/logic_analyzer branch March 7, 2024 18:49
@bessman bessman restored the refector/logic_analyzer branch March 7, 2024 18:50
@bessman bessman reopened this Mar 7, 2024
@bessman bessman force-pushed the refector/logic_analyzer branch 3 times, most recently from 8d93290 to 466d79a Compare March 10, 2024 20:00
@bessman bessman changed the base branch from main to develop March 10, 2024 20:02
@bessman bessman force-pushed the refector/logic_analyzer branch 2 times, most recently from eef9251 to 33448ed Compare March 10, 2024 20:18
@bessman bessman force-pushed the refector/logic_analyzer branch from 33448ed to f20242b Compare March 26, 2024 22:49
@bessman bessman marked this pull request as ready for review March 26, 2024 22:52
@bessman
Copy link
Collaborator Author

bessman commented Mar 26, 2024

Going to merge this soon (into the new and probably temporary 'develop' branch).

Some notes on this change:

  • Some functionality has been removed from the logic analyzer, specifically:
    • The ability to cascade and prescale timers. This feature is useful to look at signals with long time periods between edges. I see this as a niche use case, but the feature could be re-added in the future.
    • The ability to capture such events as 'every fours rising edge' and 'every sixteenth rising edge' and the like. I don't see a use case for this. Available capture events are now 'any edge', 'falling edge', and 'rising edge'.
    • The ability to configure multiple pins as trigger source simultaneously. I don't see a use case for this. At most one pin at a time can now be configured as trigger source.
  • A lot of now-obsolete code hasn't yet been removed. It will be removed at a later stage of the refactorization (in a separate pull request).

@bessman bessman deleted the branch fossasia:develop January 12, 2025 15:27
@bessman bessman closed this Jan 12, 2025
@bessman bessman reopened this Jan 12, 2025
@bessman bessman force-pushed the refector/logic_analyzer branch from f20242b to f51e650 Compare April 4, 2025 12:23
@bessman
Copy link
Collaborator Author

bessman commented Apr 4, 2025

@sourcery-ai review

Copy link
Contributor

sourcery-ai bot commented Apr 4, 2025

Reviewer's Guide by Sourcery

This pull request refactors the logic analyzer instrument and its underlying hardware drivers (IC, DMA, CN, TMR) to improve modularity, reduce code complexity, and prepare for future development. It introduces new self-contained hardware drivers in src/registers_ng and uses callback-based interrupt handling. The old instrument and driver implementations have been removed, and the build system has been updated to reflect the new project structure.

Updated class diagram for Logic Analyzer

classDiagram

  class LogicAnalyzer {
    -uint16_t* g_buffer
    -uint16_t g_buffer_n_items
    -uint8_t g_n_channels
    -uint8_t g_initial_states
    -TMR_Timer g_TIMER
    +enum Status LA_capture(uint8_t n_channels, uint16_t events, Edge edge, Channel trigger)
    +enum Status LA_fetch(uint16_t** buffer, uint16_t* n_items)
    +enum Status LA_stop()
    +enum Status LA_get_initial_states(uint8_t* initial_states)
    +enum Status LA_cmd_capture(uint8_t** args, uint16_t args_size, uint8_t** rets, uint16_t* rets_size)
    +enum Status LA_cmd_fetch(uint8_t** args, uint16_t args_size, uint8_t** rets, uint16_t* rets_size)
    +enum Status LA_cmd_stop(uint8_t** args, uint16_t args_size, uint8_t** rets, uint16_t* rets_size)
    +enum Status LA_cmd_get_initial_states(uint8_t** args, uint16_t args_size, uint8_t** rets, uint16_t* rets_size)
  }

  class CN {
    +void CN_reset()
    +void CN_interrupt_enable(Channel pin, InterruptCallback callback)
  }

  class IC {
    +void IC_reset(Channel channel)
    +void IC_start(Channel channel, Edge edge, IC_Timer timer)
    +void IC_interrupt_enable(Channel channel, InterruptCallback callback)
    +void IC_interrupt_disable(Channel channel)
  }

  class DMA {
    +void DMA_reset(Channel channel)
    +void DMA_setup(Channel channel, uint16_t count, size_t address, DMA_Source source)
    +void DMA_start(Channel channel)
    +void DMA_interrupt_enable(Channel channel, InterruptCallback callback)
  }

  class TMR {
    +void TMR_reset(TMR_Timer timer)
    +void TMR_start(TMR_Timer timer)
    +void TMR_set_period(TMR_Timer timer, uint16_t period)
  }

  LogicAnalyzer -- CN
  LogicAnalyzer -- IC
  LogicAnalyzer -- DMA
  LogicAnalyzer -- TMR

  note for LogicAnalyzer "Refactored Logic Analyzer instrument"
Loading

File-Level Changes

Change Details Files
Introduced new low-level hardware drivers in src/registers_ng with a self-contained, modular design, aiming to reduce dependencies and improve code organization.
  • Created new drivers for DMA, IC, CN, and Timers.
  • Implemented callback-based interrupt handling for modularity.
  • Designed drivers to be self-contained with minimal dependencies.
src/registers_ng/dma.c
src/registers_ng/ic.c
src/registers_ng/cn.c
src/registers_ng/tmr.c
src/registers_ng/pins.c
src/registers_ng/CMakeLists.txt
src/registers_ng/dma.h
src/registers_ng/ic.h
src/registers_ng/cn.h
src/registers_ng/tmr.h
src/registers_ng/pins.h
Refactored the logic analyzer instrument to use the new hardware drivers and callback-based interrupts.
  • Modified the logic analyzer to use the new DMA, IC, CN, and Timer drivers.
  • Implemented new command functions for capture, fetch, stop, and getting initial states.
  • Adjusted interrupt handling using callbacks.
src/instruments/logic_analyzer.c
src/instruments/logic_analyzer.h
Removed old instrument and driver implementations and related files.
  • Removed old ADC, interval, logic analyzer, multimeter, oscilloscope, sensors, and wave generator implementations.
  • Removed old IC drivers.
  • Removed old DMA drivers.
src/commands.c
src/registers/system/interrupt_manager.c
src/registers/system/system.c
src/instruments/CMakeLists.txt
src/registers/comparators/CMakeLists.txt
src/registers/converters/CMakeLists.txt
src/CMakeLists.txt
src/helpers/CMakeLists.txt
src/registers/CMakeLists.txt
src/transport/packet_handler.c
src/helpers/interval.c
src/helpers/interval.h
src/instruments/logicanalyzer.c
src/instruments/logicanalyzer.h
src/instruments/multimeter.c
src/instruments/multimeter.h
src/instruments/oscilloscope.c
src/instruments/oscilloscope.h
src/instruments/sensors.c
src/instruments/sensors.h
src/instruments/wavegenerator.c
src/instruments/wavegenerator.h
src/registers/comparators/ic1.c
src/registers/comparators/ic1.h
src/registers/comparators/ic2.c
src/registers/comparators/ic2.h
src/registers/comparators/ic3.c
src/registers/comparators/ic3.h
src/registers/comparators/ic4.c
src/registers/comparators/ic4.h
src/registers/comparators/ic_params.c
src/registers/comparators/ic_params.h
src/registers/converters/adc1.c
src/registers/converters/adc1.h
src/registers/converters/adc_module_features.h
src/registers/memory/CMakeLists.txt
src/registers/memory/dma.c
src/registers/memory/dma.h
Updated the build system to include the new source files and reflect the project's restructured source directories.
  • Added the new registers_ng subdirectory to the build.
  • Updated include directories to include the new registers_ng directory.
  • Removed old instrument source files.
src/CMakeLists.txt
src/instruments/CMakeLists.txt

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @bessman - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Uninitialized 'input' pointer in LA_cmd_capture. (link)

Overall Comments:

  • The introduction of the registers_ng directory and associated changes looks good, but consider renaming it to registers in this PR if the old directory is no longer needed.
  • The new logic analyzer driver introduces a lot of new types - consider grouping them together in a single header file.
Here's what I looked at during the review
  • 🔴 General issues: 1 blocking issue, 2 other issues
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 2 issues found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

bessman added 4 commits April 4, 2025 14:42
There are transitive and circular dependencies everywhere. This began with
removing src/helpers/interval.*, which is superceded by the new logic analyzer
implementation. But the old IC driver transitivelly depended on interval, so
it had to go. Then a bunch of things that depended on IC had to go, and so on
and so forth.

Basically, better to start fresh at this point.
@bessman
Copy link
Collaborator Author

bessman commented Apr 7, 2025

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @bessman - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a high-level overview of the design to the top of each new .c file.
  • It would be helpful to include a diagram of the module dependencies in the documentation.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 2 issues found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@bessman
Copy link
Collaborator Author

bessman commented Apr 7, 2025

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @bessman - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a brief description of the logic analyzer's functionality to the logic_analyzer.h file.
  • The types.h file is a good place to define common types, but consider if it should be moved to the registers_ng directory.
Here's what I looked at during the review
  • 🟡 General issues: 5 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@bessman bessman merged commit a822ccc into fossasia:develop Apr 7, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants