Skip to content

Allow override for SoftwareSerial timer ISR priority #751

Closed
@sjasonsmith

Description

@sjasonsmith

Is your feature request/improvement related to a problem? Please describe.

The recently added SoftwareSerial library uses the default timer priority for software bit timing. For many processors, this defaults to preemption level 14, which will cause bit timing issues when integrating with systems using other interrupts on the system.

This became apparent when integrating SoftwareSerial with Marlin when testing the following Marlin PR:
MarlinFirmware/Marlin#15655

Describe the solution you'd like

I suggest the following changes to the SoftwareSerial library. I am willing to contribute these changes, but wanted input on whether these changes were likely to be accepted before creating a PR.

  1. Replace TIMER_SERIAL constant with SWSERIAL_TIMER_NUM.

    • Prefix clearly communicates that this constant impacts SoftwareSerial.
    • Providing timer number instead of name simplifies construction of interrupt name at build time.
      • This change could be skipped if there is a good way to determine an interrupt number from a timer base address at runtime (Since TIMx is actually a macro to the base address).
    • #error can be used to communicate change if TIMER_SERIAL is defined
  2. Add SWSERIAL_TIMER_INT_PRIO to override interrupt priority

    • Only change the priority if the constant is defined, else use default.
    • Alternatively, default to highest interrupt priority for best bit-timing performance, and allow users to downgrade priority with constant.
  3. Expose additional macros with SWSERIAL prefix, allowing users to customize behavior if needed:

    • SWSERIAL_FORCE_BAUD_RATE (Currently override-able as FORCE_BAUD_RATE)
    • SWSERIAL_OVERSAMPLE
    • SWSERIAL_HALFDUPLEX_DELAY

Describe alternatives you've considered
Working around this in Marlin was quite cumbersome. We were able to get this to communicate by arranging other timer interrupts around the default timer interrupt level, but that solution is not ideal. It likely causes other timing issues due to changes to prioritization among hardware ISRs, etc.

I attempted to set the timer ISR priority from within Marlin, but the change did not persist. I believe that when the timer is reset during a baud rate switch (setSpeed) the timer is being restored to its default priority level.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions