From 21df57e980ab28538b80a7a0166c4777a8b2995d Mon Sep 17 00:00:00 2001 From: klassents Date: Tue, 8 Oct 2024 14:10:14 +0700 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=BB=D0=B0=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9=D0=BA?= =?UTF-8?q?=D1=83=20=D0=BF=D1=80=D0=B5=D1=80=D1=8B=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=B2=20setRx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SoftwareSerial/src/SoftwareSerial.cpp | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index eb42f25..c2a320f 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -202,6 +202,14 @@ void SoftwareSerial::setRX(uint8_t rx) pinMode(rx, INPUT_PULLUP); // pullup for normal logic else pinMode(rx, INPUT); + + // Save gpio_line for turning on/off interrupts + _int_maskLine = digitalPinToGpioIntLine(_receivePin); + // Init interrupt line + HAL_GPIO_InitInterruptLine((HAL_GPIO_Line_Config)digitalPinToGpioIntMux(_receivePin), + _inverse_logic ? GPIO_INT_MODE_RISING : GPIO_INT_MODE_FALLING); + GPIO_IRQ_LINE_DISABLE(_int_maskLine); // turn off int line (it turning on while line init) + HAL_EPIC_MaskEdgeSet(HAL_EPIC_GPIO_IRQ_MASK); // turn on the mask in EPIC } uint32_t SoftwareSerial::subtract_cap(uint32_t num, uint16_t sub) @@ -216,11 +224,8 @@ uint32_t SoftwareSerial::subtract_cap(uint32_t num, uint16_t sub) // Public methods // void SoftwareSerial::begin(long speed) -{ - // set pins config - setRX(_receivePin); - setTX(_transmitPin); - +{ + // delays are empirical values here _rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0; // limit speed @@ -230,14 +235,15 @@ void SoftwareSerial::begin(long speed) // Precalculate the various delays in number of ticks uint32_t bit_delay = F_CPU / speed; - // delays are empirical values here - - // 1 bit delay while transmitting data - _tx_delay = subtract_cap(bit_delay, 35); + setTX(_transmitPin); + _tx_delay = subtract_cap(bit_delay, 35); // 1 bit delay while transmitting data // Only setup rx when we have a valid INT for this pin if (digitalPinToInterrupt(_receivePin) != NOT_AN_INTERRUPT) { + // set pin config + setRX(_receivePin); + // We want to have a total delay of 1.5 bit time from start bit. Inside the loop, we already // wait for 1 bit time, so here we wait for 0.5 bit time _rx_delay_centering = subtract_cap(bit_delay / 2, 315); @@ -248,14 +254,6 @@ void SoftwareSerial::begin(long speed) // This delay aims at 3/4 of a bit time, meaning the end of the delay will be at 1/4th of the stopbit. // This allows some extra time for ISR cleanup, which makes 115200 baud at 16Mhz work more reliably _rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, 142); - - // Save gpio_line for turning on/off interrupts - _int_maskLine = digitalPinToGpioIntLine(_receivePin); - // Init interrupt line - HAL_GPIO_InitInterruptLine((HAL_GPIO_Line_Config)digitalPinToGpioIntMux(_receivePin), - _inverse_logic ? GPIO_INT_MODE_RISING : GPIO_INT_MODE_FALLING); - GPIO_IRQ_LINE_DISABLE(_int_maskLine); // turn off int line (it turning on while line init) - HAL_EPIC_MaskEdgeSet(HAL_EPIC_GPIO_IRQ_MASK); // turn on the mask in EPIC } else ErrorMsgHandler("SoftwareSerial.begin(): Rx pin does not support interrupts, use different pin");