Skip to content

Add mechanism to set SoftwareSerial interrupt priority #753

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions libraries/SoftwareSerial/src/SoftwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
// Includes
//
#include "SoftwareSerial.h"
#include <timer.h>

#define OVERSAMPLE 3 // in RX, Timer will generate interruption OVERSAMPLE time during a bit. Thus OVERSAMPLE ticks in a bit. (interrupt not synchonized with edge).

Expand Down Expand Up @@ -96,6 +97,8 @@
// Statics
//
HardwareTimer SoftwareSerial::timer(TIMER_SERIAL);
const IRQn_Type SoftwareSerial::timer_interrupt_number = static_cast<IRQn_Type>(getTimerUpIrq(TIMER_SERIAL));
uint32_t SoftwareSerial::timer_interrupt_priority = NVIC_EncodePriority(NVIC_GetPriorityGrouping(), TIM_IRQ_PRIO, TIM_IRQ_SUBPRIO);
SoftwareSerial *SoftwareSerial::active_listener = nullptr;
SoftwareSerial *volatile SoftwareSerial::active_out = nullptr;
SoftwareSerial *volatile SoftwareSerial::active_in = nullptr;
Expand All @@ -107,6 +110,12 @@ uint32_t SoftwareSerial::rx_buffer = 0;
int32_t SoftwareSerial::rx_bit_cnt = -1; // rx_bit_cnt = -1 : waiting for start bit
uint32_t SoftwareSerial::cur_speed = 0;

void SoftwareSerial::setInterruptPriority(uint32_t preemptPriority, uint32_t subPriority)
{
timer_interrupt_priority = NVIC_EncodePriority(NVIC_GetPriorityGrouping(), preemptPriority, subPriority);
NVIC_SetPriority(timer_interrupt_number, timer_interrupt_priority);
}

//
// Private methods
//
Expand Down Expand Up @@ -134,6 +143,7 @@ void SoftwareSerial::setSpeed(uint32_t speed)
timer.setCount(0);
timer.attachInterrupt(&handleInterrupt);
timer.resume();
NVIC_SetPriority(timer_interrupt_number, timer_interrupt_priority);
} else {
timer.detachInterrupt();
}
Expand Down
4 changes: 4 additions & 0 deletions libraries/SoftwareSerial/src/SoftwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class SoftwareSerial : public Stream {
// static data
static bool initialised;
static HardwareTimer timer;
static const IRQn_Type timer_interrupt_number;
static uint32_t timer_interrupt_priority;
static SoftwareSerial *active_listener;
static SoftwareSerial *volatile active_out;
static SoftwareSerial *volatile active_in;
Expand Down Expand Up @@ -118,6 +120,8 @@ class SoftwareSerial : public Stream {
return true;
}

static void setInterruptPriority(uint32_t preemptPriority, uint32_t subPriority);

using Print::write;
};

Expand Down