From 136fbc78d32d9c26ff769121a27011dcd6dab5c4 Mon Sep 17 00:00:00 2001 From: klassents Date: Fri, 13 Sep 2024 15:35:01 +0700 Subject: [PATCH 01/31] =?UTF-8?q?=D0=B2=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB?= =?UTF-8?q?=D0=B5=20=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=B8=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=D0=B0=20=D1=84=D1=83=D0=BD?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D1=8E=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D1=82=D0=B8=D0=BA=D0=BE=D0=B2.=20=D0=B2?= =?UTF-8?q?=20=D0=BF=D0=B0=D0=BF=D0=BA=D0=B5=20variants=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA?= =?UTF-8?q?=D1=86=D0=B8=D0=B8=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D1=8B=20=D1=81=20=D0=BF=D1=80=D0=B5=D1=80=D1=8B?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=D0=BC=D0=B8.=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=80=D1=8B=D0=B1?= =?UTF-8?q?=D0=B0=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5=D0=BA?= =?UTF-8?q?=D0=B8=20softserial?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/wiring_time.c | 5 + cores/arduino/wiring_time.h | 6 + libraries/SoftwareSerial/keywords.txt | 30 ++ libraries/SoftwareSerial/library.properties | 10 + .../SoftwareSerial/src/SoftwareSerial.cpp | 486 ++++++++++++++++++ libraries/SoftwareSerial/src/SoftwareSerial.h | 110 ++++ variants/standart/pins_arduino.h | 27 +- variants/standart/variant.c | 24 +- 8 files changed, 683 insertions(+), 15 deletions(-) create mode 100644 libraries/SoftwareSerial/keywords.txt create mode 100644 libraries/SoftwareSerial/library.properties create mode 100644 libraries/SoftwareSerial/src/SoftwareSerial.cpp create mode 100644 libraries/SoftwareSerial/src/SoftwareSerial.h diff --git a/cores/arduino/wiring_time.c b/cores/arduino/wiring_time.c index 19dde62..ed23435 100644 --- a/cores/arduino/wiring_time.c +++ b/cores/arduino/wiring_time.c @@ -39,6 +39,11 @@ void SysTick_Init(void) HAL_SCR1_Timer_Init(&hscr1_timer); // Ticks are 32 MHz } +uint64_t SysTick_GetTicks(void) +{ + return TICKS_IN_SYSTIMER; +} + // number of microseconds since start of the program uint32_t micros(void) { diff --git a/cores/arduino/wiring_time.h b/cores/arduino/wiring_time.h index 162fbc3..da9698b 100644 --- a/cores/arduino/wiring_time.h +++ b/cores/arduino/wiring_time.h @@ -30,6 +30,12 @@ extern "C" { */ void SysTick_Init(void); +/** + * \brief Returns the number of ticks since the Arduino board began running the current program. + + * \return Number of ticks since the program started + */ +uint64_t SysTick_GetTicks(void); /** * \brief Returns the number of milliseconds since the Arduino board began running the current program. diff --git a/libraries/SoftwareSerial/keywords.txt b/libraries/SoftwareSerial/keywords.txt new file mode 100644 index 0000000..aaea17c --- /dev/null +++ b/libraries/SoftwareSerial/keywords.txt @@ -0,0 +1,30 @@ +####################################### +# Syntax Coloring Map for SoftwareSerial +# (formerly NewSoftSerial) +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +SoftwareSerial KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +end KEYWORD2 +read KEYWORD2 +write KEYWORD2 +available KEYWORD2 +isListening KEYWORD2 +overflow KEYWORD2 +flush KEYWORD2 +listen KEYWORD2 +peek KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/libraries/SoftwareSerial/library.properties b/libraries/SoftwareSerial/library.properties new file mode 100644 index 0000000..61c635b --- /dev/null +++ b/libraries/SoftwareSerial/library.properties @@ -0,0 +1,10 @@ +name=SoftwareSerial +version=0.0.0 +author=Arduino +maintainer=Arduino +sentence=Enables serial communication on any digital pin. +paragraph=The SoftwareSerial library has been developed to allow serial communication on any digital pin of the board, using software to replicate the functionality of the hardware UART. It is possible to have multiple software serial ports with speeds up to 115200 bps. +category=Communication +url=http://www.arduino.cc/en/Reference/SoftwareSerial +architectures=MIK32_Amur + diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp new file mode 100644 index 0000000..f1c685b --- /dev/null +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -0,0 +1,486 @@ +/* +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +The latest version of this library can always be found at +http://arduiniana.org. +*/ + +// When set, _DEBUG co-opts pins 11 and 13 for debugging with an +// oscilloscope or logic analyzer. Beware: it also slightly modifies +// the bit times, so don't rely on it too much at high baud rates +#define _DEBUG 0 +#define _DEBUG_PIN1 11 +#define _DEBUG_PIN2 13 + +#define MAX_SOFT_SERIAL_SPEED 115200 +#define MIN_SOFT_SERIAL_SPEED 300 +// +// Includes +// +// #include +#include +#include +#include "mik32_hal_irq.h" +#include "mik32_hal_gpio.h" + +// +// Statics +// +SoftwareSerial *SoftwareSerial::active_object = 0; +uint8_t SoftwareSerial::_receive_buffer[_SS_MAX_RX_BUFF]; +volatile uint8_t SoftwareSerial::_receive_buffer_tail = 0; +volatile uint8_t SoftwareSerial::_receive_buffer_head = 0; + +// +// Debugging +// +// This function generates a brief pulse +// for debugging or measuring on an oscilloscope. +// #if _DEBUG +// inline void DebugPulse(uint8_t pin, uint8_t count) +// { +// volatile uint8_t *pport = portOutputRegister(digitalPinToPort(pin)); + +// uint8_t val = *pport; +// while (count--) +// { +// *pport = val | digitalPinToBitMask(pin); +// *pport = val; +// } +// } +// #else +// inline void DebugPulse(uint8_t, uint8_t) {} +// #endif + +// +// Private methods +// + +/* static */ +inline void SoftwareSerial::tunedDelay(uint16_t delayTicks) +{ + uint64_t startTicks = SysTick_GetTicks(); + while ((SysTick_GetTicks() - startTicks) < delayTicks) + ; +} + +// This function sets the current object as the "listening" +// one and returns true if it replaces another +bool SoftwareSerial::listen() +{ + if (!_rx_delay_stopbit) + return false; + + if (active_object != this) + { + if (active_object) + active_object->stopListening(); + + _buffer_overflow = false; + _receive_buffer_head = _receive_buffer_tail = 0; + active_object = this; + + setRxIntMsk(true); + return true; + } + + return false; +} + +// Stop listening. Returns true if we were actually listening. +bool SoftwareSerial::stopListening() +{ + if (active_object == this) + { + setRxIntMsk(false); + active_object = NULL; + return true; + } + return false; +} + +// +// The receive routine called by the interrupt handler +// +void SoftwareSerial::recv() +{ + +// #if GCC_VERSION < 40302 +// // Work-around for avr-gcc 4.3.0 OSX version bug +// // Preserve the registers that the compiler misses +// // (courtesy of Arduino forum user *etracer*) +// asm volatile( +// "push r18 \n\t" +// "push r19 \n\t" +// "push r20 \n\t" +// "push r21 \n\t" +// "push r22 \n\t" +// "push r23 \n\t" +// "push r26 \n\t" +// "push r27 \n\t" +// ::); +// #endif + + // uint8_t data = 0; + + // // If RX line is high, then we don't see any start bit + // // so interrupt is probably not for us + // if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) + // { + // // Disable further interrupts during reception, this prevents + // // triggering another interrupt directly after we return, which can + // // cause problems at higher baudrates. + // setRxIntMsk(false); + + // // Wait approximately 1/2 of a bit width to "center" the sample + // tunedDelay(_rx_delay_centering); + // DebugPulse(_DEBUG_PIN2, 1); + + // // Read each of the 8 bits + // for (uint8_t i=8; i > 0; --i) + // { + // tunedDelay(_rx_delay_intrabit); + // data >>= 1; + // DebugPulse(_DEBUG_PIN2, 1); + // if (rx_pin_read()) + // data |= 0x80; + // } + + // if (_inverse_logic) + // data = ~data; + + // // if buffer full, set the overflow flag and return + // uint8_t next = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF; + // if (next != _receive_buffer_head) + // { + // // save new data in buffer: tail points to where byte goes + // _receive_buffer[_receive_buffer_tail] = data; // save new byte + // _receive_buffer_tail = next; + // } + // else + // { + // DebugPulse(_DEBUG_PIN1, 1); + // _buffer_overflow = true; + // } + + // // skip the stop bit + // tunedDelay(_rx_delay_stopbit); + // DebugPulse(_DEBUG_PIN1, 1); + + // // Re-enable interrupts when we're sure to be inside the stop bit + // setRxIntMsk(true); + + // } + +// #if GCC_VERSION < 40302 +// // Work-around for avr-gcc 4.3.0 OSX version bug +// // Restore the registers that the compiler misses +// asm volatile( +// "pop r27 \n\t" +// "pop r26 \n\t" +// "pop r23 \n\t" +// "pop r22 \n\t" +// "pop r21 \n\t" +// "pop r20 \n\t" +// "pop r19 \n\t" +// "pop r18 \n\t" +// ::); +// #endif +} + +uint8_t SoftwareSerial::rx_pin_read() +{ + return (uint8_t)HAL_GPIO_ReadPin((GPIO_TypeDef *)_receivePortRegister, (HAL_PinsTypeDef)_receivePin); +} + +// +// Interrupt handling +// + +/* static */ +inline void SoftwareSerial::handle_interrupt() // todo унести в обработчик прерываний +{ + if (active_object) + { + active_object->recv(); + } +} + +// #if defined(PCINT0_vect) +// ISR(PCINT0_vect) +// { +// SoftwareSerial::handle_interrupt(); +// } +// #endif + +// +// Constructor +// +SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) : + _rx_delay_centering(0), + _rx_delay_intrabit(0), + _rx_delay_stopbit(0), + _tx_delay(0), + _buffer_overflow(false), + _inverse_logic(inverse_logic) +{ + setTX(transmitPin); + setRX(receivePin); +} + +// +// Destructor +// +SoftwareSerial::~SoftwareSerial() +{ + end(); +} + +void SoftwareSerial::setTX(uint8_t tx) +{ + // First write, then set output. If we do this the other way around, + // the pin would be output low for a short while before switching to + // output high. Now, it is input with pullup for a short while, which + // is fine. With inverse logic, either order is fine. + digitalWrite(tx, _inverse_logic ? LOW : HIGH); // todo проверить, что правда выставляется до конфигурации + pinMode(tx, OUTPUT); + _transmitBitMask = (uint16_t)digitalPinToBitMask(tx); // save tx bit mask + _transmitPortRegister = (uint32_t)digitalPinToPort(tx); // save tx port adress +} + +void SoftwareSerial::setRX(uint8_t rx) +{ + if (!_inverse_logic) + pinMode(rx, INPUT_PULLUP); // pullup for normal logic + else + pinMode(rx, INPUT); + _receivePin = rx; // save rx digital pin number + _receiveBitMask = (uint16_t)digitalPinToBitMask(rx); // save rx bit mask + _receivePortRegister = (uint32_t)digitalPinToPort(rx); // save rx port adress +} + +uint16_t SoftwareSerial::subtract_cap(uint16_t num, uint16_t sub) +{ + if (num > sub) + return num - sub; + else + return 1; +} + +// +// Public methods +// +void SoftwareSerial::begin(long speed) +{ + _rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0; + + // limit speed + if (speed > MAX_SOFT_SERIAL_SPEED) speed = MAX_SOFT_SERIAL_SPEED; + if (speed < MIN_SOFT_SERIAL_SPEED) speed = MIN_SOFT_SERIAL_SPEED; + + // Precalculate the various delays in number of ticks + uint32_t bit_delay = F_CPU / speed; + + // 12 (gcc 4.8.2) or 13 (gcc 4.3.2) cycles from start bit to first bit, + // 15 (gcc 4.8.2) or 16 (gcc 4.3.2) cycles between bits, + // 12 (gcc 4.8.2) or 14 (gcc 4.3.2) cycles from last bit to stop bit + // These are all close enough to just use 15 cycles, since the inter-bit + // timings are the most critical (deviations stack 8 times) + _tx_delay = subtract_cap(bit_delay, 15); + + // Only setup rx when we have a valid INT for this pin + if (digitalPinToInterrupt(_receivePin) != NOT_AN_INTERRUPT) + { + #if GCC_VERSION > 40800 + // Timings counted from gcc 4.8.2 output. This works up to 115200 on + // 16Mhz and 57600 on 8Mhz. + // + // When the start bit occurs, there are 3 or 4 cycles before the + // interrupt flag is set, 4 cycles before the PC is set to the right + // interrupt vector address and the old PC is pushed on the stack, + // and then 75 cycles of instructions (including the RJMP in the + // ISR vector table) until the first delay. After the delay, there + // are 17 more cycles until the pin value is read (excluding the + // delay in the loop). + // We want to have a total delay of 1.5 bit time. Inside the loop, + // we already wait for 1 bit time - 23 cycles, so here we wait for + // 0.5 bit time - (71 + 18 - 22) cycles. + _rx_delay_centering = subtract_cap(bit_delay / 2, (4 + 4 + 75 + 17 - 23)); + + // There are 23 cycles in each loop iteration (excluding the delay) + _rx_delay_intrabit = subtract_cap(bit_delay, 23); + + // There are 37 cycles from the last bit read to the start of + // stopbit delay and 11 cycles from the delay until the interrupt + // mask is enabled again (which _must_ happen during the stopbit). + // 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, (37 + 11)); + #else // Timings counted from gcc 4.3.2 output + // Note that this code is a _lot_ slower, mostly due to bad register + // allocation choices of gcc. This works up to 57600 on 16Mhz and + // 38400 on 8Mhz. + _rx_delay_centering = subtract_cap(bit_delay / 2, (4 + 4 + 97 + 29 - 11)); + _rx_delay_intrabit = subtract_cap(bit_delay, 11); + _rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, (44 + 17)); + #endif + + // Enable the INT for the entire port here, but never disable it + // (others might also need it, so we disable the interrupt by using + // the per-pin PCMSK register). + if (_inverse_logic) + _int_mode = GPIO_INT_MODE_RISING; // start bit: low to high + else + _int_mode = GPIO_INT_MODE_FALLING; // start bit: high to low + // Save gpio_mux and gpio_line for on/off interrupts + _int_maskMux = digitalPinToGpioIntMux(_receivePin); // gpio mux value + _int_maskLine = digitalPinToGpioIntLine(_receivePin); // gpio irq line value + HAL_EPIC_MaskEdgeSet(HAL_EPIC_GPIO_IRQ_MASK); // turn on the mask in EPIC + + // *digitalPinToPCICR((int8_t)_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin)); + // _pcint_maskreg = digitalPinToPCMSK(_receivePin); + // _pcint_maskvalue = _BV(digitalPinToPCMSKbit(_receivePin)); + + tunedDelay(_tx_delay); // if we were low this establishes the end + } + +// #if _DEBUG +// pinMode(_DEBUG_PIN1, OUTPUT); +// pinMode(_DEBUG_PIN2, OUTPUT); +// #endif + + listen(); +} + +void SoftwareSerial::setRxIntMsk(bool enable) +{ + if (enable) + HAL_GPIO_InitInterruptLine((HAL_GPIO_Line_Config)_int_maskMux, (HAL_GPIO_InterruptMode)_int_mode); + // *_pcint_maskreg |= _pcint_maskvalue; + else + HAL_GPIO_DeInitInterruptLine((HAL_GPIO_Line)_int_maskLine); + // *_pcint_maskreg &= ~_pcint_maskvalue; +} + +void SoftwareSerial::end() +{ + stopListening(); +} + + +// Read data from buffer +int SoftwareSerial::read() +{ + if (!isListening()) + return -1; + + // Empty buffer? + if (_receive_buffer_head == _receive_buffer_tail) + return -1; + + // Read from "head" + uint8_t data = _receive_buffer[_receive_buffer_head]; // grab next byte + _receive_buffer_head = (_receive_buffer_head + 1) % _SS_MAX_RX_BUFF; + return data; +} + +int SoftwareSerial::available() +{ + if (!isListening()) + return 0; + + return ((unsigned int)(_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head)) % _SS_MAX_RX_BUFF; +} + +size_t SoftwareSerial::write(uint8_t byte) +{ + if (_tx_delay == 0) + { + setWriteError(); + return 0; + } + + // By declaring these as local variables, the compiler will put them + // in registers _before_ disabling interrupts and entering the + // critical timing sections below, which makes it a lot easier to + // verify the cycle timings + uint8_t bit_mask = _transmitBitMask; // битовая маска в порту + + bool inv = _inverse_logic; + uint16_t delay = _tx_delay; + + if (inv) + byte = ~byte; + + noInterrupts(); // turn off interrupts for a clean txmit + + // Write the start bit + if (inv) + // todo проверить, можно ли так выставлять через адрес порта + HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_HIGH); + // *reg |= reg_mask; // бит tx в 1 + else + HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_LOW); + // *reg &= inv_mask; // бит tx в 0 + + tunedDelay(delay); + + // Write each of the 8 bits + for (uint8_t i = 8; i > 0; --i) + { + if (byte & 1) // choose bit + // *reg |= reg_mask; // send 1 + HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_HIGH); + else + // *reg &= inv_mask; // send 0 + HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_LOW); + + tunedDelay(delay); + byte >>= 1; + } + + // restore pin to natural state + if (inv) + HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_LOW); + // *reg &= inv_mask; // бит tx в 0 + else + HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_HIGH); + // *reg |= reg_mask; // бит tx в 1 + + interrupts(); // enable interrupts + tunedDelay(_tx_delay); + + return 1; +} + +void SoftwareSerial::flush() +{ + // There is no tx buffering, simply return +} + +int SoftwareSerial::peek() +{ + if (!isListening()) + return -1; + + // Empty buffer? + if (_receive_buffer_head == _receive_buffer_tail) + return -1; + + // Read from "head" + return _receive_buffer[_receive_buffer_head]; +} diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.h b/libraries/SoftwareSerial/src/SoftwareSerial.h new file mode 100644 index 0000000..8038028 --- /dev/null +++ b/libraries/SoftwareSerial/src/SoftwareSerial.h @@ -0,0 +1,110 @@ +/* +SoftwareSerial.h +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +The latest version of this library can always be found at +http://arduiniana.org. +*/ + +#ifndef SoftwareSerial_h +#define SoftwareSerial_h + +#include +#include + +/****************************************************************************** +* Definitions +******************************************************************************/ + +#ifndef _SS_MAX_RX_BUFF +#define _SS_MAX_RX_BUFF 64 // RX buffer size +#endif + +#ifndef GCC_VERSION +#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) +#endif + +class SoftwareSerial : public Stream +{ +private: + // per object data + uint8_t _receivePin; + uint16_t _receiveBitMask; + uint32_t _receivePortRegister; + uint16_t _transmitBitMask; + uint32_t _transmitPortRegister; + uint8_t _int_maskMux; + uint8_t _int_maskLine; + uint8_t _int_mode; + + // Expressed as 4-cycle delays (must never be 0!) + uint16_t _rx_delay_centering; + uint16_t _rx_delay_intrabit; + uint16_t _rx_delay_stopbit; + uint16_t _tx_delay; + + uint16_t _buffer_overflow:1; + uint16_t _inverse_logic:1; + + // static data + static uint8_t _receive_buffer[_SS_MAX_RX_BUFF]; + static volatile uint8_t _receive_buffer_tail; + static volatile uint8_t _receive_buffer_head; + static SoftwareSerial *active_object; + + // private methods + inline void recv() __attribute__((__always_inline__)); + uint8_t rx_pin_read(); + void setTX(uint8_t transmitPin); + void setRX(uint8_t receivePin); + inline void setRxIntMsk(bool enable) __attribute__((__always_inline__)); + + // Return num - sub, or 1 if the result would be < 1 + static uint16_t subtract_cap(uint16_t num, uint16_t sub); + + // private static method for timing + static inline void tunedDelay(uint16_t delayTicks); + +public: + // public methods + SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false); + ~SoftwareSerial(); + void begin(long speed); + bool listen(); + void end(); + bool isListening() { return this == active_object; } + bool stopListening(); + bool overflow() + { + bool ret = _buffer_overflow; + if (ret) + _buffer_overflow = false; + return ret; + } + int peek(); + + virtual size_t write(uint8_t byte); + virtual int read(); + virtual int available(); + virtual void flush(); + operator bool() { return true; } + + using Print::write; + + // public only for easy access by interrupt handlers + static inline void handle_interrupt() __attribute__((__always_inline__)); +}; + +#endif diff --git a/variants/standart/pins_arduino.h b/variants/standart/pins_arduino.h index ebfccae..75d1ced 100644 --- a/variants/standart/pins_arduino.h +++ b/variants/standart/pins_arduino.h @@ -109,16 +109,33 @@ static const uint8_t SCL = PIN_WIRE_SCL; #define WIRE_FREQ_1000K 1000000 // interrupts -// determines the board pin number by interrupt number -uint32_t interruptToDigitalPin(uint8_t interruptNum); +extern const uint8_t interruptInfo[EXTERNAL_NUM_INTERRUPTS][3]; // determines interrupt number by the board pin number int8_t digitalPinToInterrupt(uint32_t digPinNumber); -// determines gpio interrupt line by interrupt number -uint32_t interruptToGpioIntLine(uint8_t interruptNum); // determines interrupt number by the gpio interrupt line int8_t gpioIntLineToInterrupt(uint32_t gpioIntLine); +// determines gpio interrupt mux by the board pin number +int8_t digitalPinToGpioIntMux(uint8_t digPinNumber); +// determines gpio interrupt line by the board pin number +int8_t digitalPinToGpioIntLine(uint8_t digPinNumber); + +// determines the board pin number by interrupt number +inline uint8_t interruptToDigitalPin(uint8_t interruptNum) +{ + return interruptInfo[interruptNum][0]; +} + +// determines gpio interrupt line by interrupt number +inline uint8_t interruptToGpioIntLine(uint8_t interruptNum) +{ + return (uint8_t)interruptInfo[interruptNum][1]; +} + // determines gpio interrupt mux by interrupt number -uint32_t interruptToGpioIntMux(uint8_t interruptNum); +inline uint8_t interruptToGpioIntMux(uint8_t interruptNum) +{ + return (uint8_t)interruptInfo[interruptNum][2]; +} #ifdef __cplusplus } diff --git a/variants/standart/variant.c b/variants/standart/variant.c index 62b59fb..9608ab5 100644 --- a/variants/standart/variant.c +++ b/variants/standart/variant.c @@ -229,19 +229,23 @@ const uint8_t interruptInfo[EXTERNAL_NUM_INTERRUPTS][3] = {BTN_BUILTIN, GPIO_LINE_6, GPIO_MUX_LINE_6_PORT2_6}, // INT6 (button) }; -uint32_t interruptToDigitalPin(uint8_t interruptNum) +int8_t digitalPinToGpioIntMux(uint8_t digPinNumber) { - return interruptInfo[interruptNum][0]; + for (uint8_t i = 0; i < EXTERNAL_NUM_INTERRUPTS; i++) + { + if (interruptInfo[i][0] == digPinNumber) + return interruptInfo[i][2]; + } + return NOT_AN_INTERRUPT; } - -uint32_t interruptToGpioIntLine(uint8_t interruptNum) +int8_t digitalPinToGpioIntLine(uint8_t digPinNumber) { - return (uint32_t)interruptInfo[interruptNum][1]; -} - -uint32_t interruptToGpioIntMux(uint8_t interruptNum) -{ - return (uint32_t)interruptInfo[interruptNum][2]; + for (uint8_t i = 0; i < EXTERNAL_NUM_INTERRUPTS; i++) + { + if (interruptInfo[i][0] == digPinNumber) + return interruptInfo[i][1]; + } + return NOT_AN_INTERRUPT; } int8_t gpioIntLineToInterrupt(uint32_t gpioIntLine) -- 2.43.0 From 7f0b704b057b9aad10f943495d191f0f542bd6e8 Mon Sep 17 00:00:00 2001 From: klassents Date: Fri, 13 Sep 2024 16:55:18 +0700 Subject: [PATCH 02/31] =?UTF-8?q?=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=20?= =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=B5=D1=82=D1=81=D1=8F,=20begin?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D1=85=D0=BE=D0=B4=D0=B8=D1=82,=20write=20?= =?UTF-8?q?=D1=81=D1=80=D0=B0=D0=B1=D0=B0=D1=82=D1=8B=D0=B2=D0=B0=D0=B5?= =?UTF-8?q?=D1=82,=20=D0=B1=D0=B8=D1=82=D1=8B=20=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5,=20=D1=81=20=D1=82=D0=B0?= =?UTF-8?q?=D0=B9=D0=BC=D0=B8=D0=BD=D0=B3=D0=B0=D0=BC=D0=B8=20=D0=B1=D0=B5?= =?UTF-8?q?=D0=B4=D0=B0.=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BD=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SoftwareSerial/src/SoftwareSerial.cpp | 27 ++++++++----------- libraries/SoftwareSerial/src/SoftwareSerial.h | 1 + 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index f1c685b..98709da 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -202,7 +202,7 @@ void SoftwareSerial::recv() uint8_t SoftwareSerial::rx_pin_read() { - return (uint8_t)HAL_GPIO_ReadPin((GPIO_TypeDef *)_receivePortRegister, (HAL_PinsTypeDef)_receivePin); + return (uint8_t)HAL_GPIO_ReadPin((GPIO_TypeDef *)_receivePortRegister, (HAL_PinsTypeDef)_receiveBitMask); } // @@ -236,8 +236,8 @@ SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inv _buffer_overflow(false), _inverse_logic(inverse_logic) { - setTX(transmitPin); - setRX(receivePin); + _transmitPin = transmitPin; + _receivePin = receivePin; } // @@ -250,12 +250,9 @@ SoftwareSerial::~SoftwareSerial() void SoftwareSerial::setTX(uint8_t tx) { - // First write, then set output. If we do this the other way around, - // the pin would be output low for a short while before switching to - // output high. Now, it is input with pullup for a short while, which - // is fine. With inverse logic, either order is fine. - digitalWrite(tx, _inverse_logic ? LOW : HIGH); // todo проверить, что правда выставляется до конфигурации + // First configure then write because writing pin didn't work until config is applied pinMode(tx, OUTPUT); + digitalWrite(tx, _inverse_logic ? LOW : HIGH); _transmitBitMask = (uint16_t)digitalPinToBitMask(tx); // save tx bit mask _transmitPortRegister = (uint32_t)digitalPinToPort(tx); // save tx port adress } @@ -266,7 +263,6 @@ void SoftwareSerial::setRX(uint8_t rx) pinMode(rx, INPUT_PULLUP); // pullup for normal logic else pinMode(rx, INPUT); - _receivePin = rx; // save rx digital pin number _receiveBitMask = (uint16_t)digitalPinToBitMask(rx); // save rx bit mask _receivePortRegister = (uint32_t)digitalPinToPort(rx); // save rx port adress } @@ -284,6 +280,10 @@ uint16_t SoftwareSerial::subtract_cap(uint16_t num, uint16_t sub) // void SoftwareSerial::begin(long speed) { + // set pins config + setRX(_receivePin); + setTX(_transmitPin); + _rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0; // limit speed @@ -357,6 +357,8 @@ void SoftwareSerial::begin(long speed) tunedDelay(_tx_delay); // if we were low this establishes the end } + else + ErrorMsgHandler("SoftwareSerial.begin(): Rx pin does not support interrupts, use different pin"); // #if _DEBUG // pinMode(_DEBUG_PIN1, OUTPUT); @@ -430,12 +432,9 @@ size_t SoftwareSerial::write(uint8_t byte) // Write the start bit if (inv) - // todo проверить, можно ли так выставлять через адрес порта HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_HIGH); - // *reg |= reg_mask; // бит tx в 1 else HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_LOW); - // *reg &= inv_mask; // бит tx в 0 tunedDelay(delay); @@ -443,10 +442,8 @@ size_t SoftwareSerial::write(uint8_t byte) for (uint8_t i = 8; i > 0; --i) { if (byte & 1) // choose bit - // *reg |= reg_mask; // send 1 HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_HIGH); else - // *reg &= inv_mask; // send 0 HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_LOW); tunedDelay(delay); @@ -456,10 +453,8 @@ size_t SoftwareSerial::write(uint8_t byte) // restore pin to natural state if (inv) HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_LOW); - // *reg &= inv_mask; // бит tx в 0 else HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_HIGH); - // *reg |= reg_mask; // бит tx в 1 interrupts(); // enable interrupts tunedDelay(_tx_delay); diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.h b/libraries/SoftwareSerial/src/SoftwareSerial.h index 8038028..7d89f40 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.h +++ b/libraries/SoftwareSerial/src/SoftwareSerial.h @@ -43,6 +43,7 @@ private: uint8_t _receivePin; uint16_t _receiveBitMask; uint32_t _receivePortRegister; + uint8_t _transmitPin; uint16_t _transmitBitMask; uint32_t _transmitPortRegister; uint8_t _int_maskMux; -- 2.43.0 From aa1163f8598b7c71dd4a317d7e3aed303122fe41 Mon Sep 17 00:00:00 2001 From: klassents Date: Tue, 17 Sep 2024 17:09:11 +0700 Subject: [PATCH 03/31] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B8=D0=BB=D0=B0?= =?UTF-8?q?=D1=81=D1=8C=20=D0=BD=D0=BE=D1=80=D0=BC=D0=B0=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D1=85=20=D1=82=D0=B0=D0=B9=D0=BC=D0=B8=D0=BD=D0=B3=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=B8=20=D0=BD=D0=BE=D1=80=D0=BC=D0=B0=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=20=D1=81=D1=82=D0=B0=D1=80=D1=82-=D0=B1=D0=B8=D1=82?= =?UTF-8?q?=D0=B0,=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D1=8E=D1=82=20?= =?UTF-8?q?=D0=B1=D0=B8=D1=82=D1=80=D0=B5=D0=B9=D1=82=D1=8B=20=D0=B2=D0=BF?= =?UTF-8?q?=D0=BB=D0=BE=D1=82=D1=8C=20=D0=B4=D0=BE=20115200,=20=D1=82?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BD=D0=B0=20115200=20=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20=D0=B1=D0=B0=D0=B9=D1=82=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8?= =?UTF-8?q?=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20=D1=87=D0=B8?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82=D1=81=D1=8F=20=D0=BA=D1=80=D0=B8=D0=B2?= =?UTF-8?q?=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SoftwareSerial/src/SoftwareSerial.cpp | 336 ++++++++---------- libraries/SoftwareSerial/src/SoftwareSerial.h | 6 +- 2 files changed, 154 insertions(+), 188 deletions(-) diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index 98709da..aa15f0f 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -32,7 +32,7 @@ http://arduiniana.org. // #include #include #include -#include "mik32_hal_irq.h" +// #include "mik32_hal_irq.h" #include "mik32_hal_gpio.h" // @@ -69,10 +69,13 @@ volatile uint8_t SoftwareSerial::_receive_buffer_head = 0; // /* static */ -inline void SoftwareSerial::tunedDelay(uint16_t delayTicks) +#include "mik32_hal_scr1_timer.h" +#define CURRENT_TICKS ((uint64_t)(SCR1_TIMER->MTIMEH)<<32 | SCR1_TIMER->MTIME) + +void SoftwareSerial::tunedDelay(uint32_t delayTicks) { - uint64_t startTicks = SysTick_GetTicks(); - while ((SysTick_GetTicks() - startTicks) < delayTicks) + uint64_t startTicks = CURRENT_TICKS; + while ((CURRENT_TICKS - startTicks) < delayTicks) ; } @@ -116,88 +119,58 @@ bool SoftwareSerial::stopListening() // void SoftwareSerial::recv() { - -// #if GCC_VERSION < 40302 -// // Work-around for avr-gcc 4.3.0 OSX version bug -// // Preserve the registers that the compiler misses -// // (courtesy of Arduino forum user *etracer*) -// asm volatile( -// "push r18 \n\t" -// "push r19 \n\t" -// "push r20 \n\t" -// "push r21 \n\t" -// "push r22 \n\t" -// "push r23 \n\t" -// "push r26 \n\t" -// "push r27 \n\t" -// ::); -// #endif - - // uint8_t data = 0; - - // // If RX line is high, then we don't see any start bit - // // so interrupt is probably not for us - // if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) + // if (active_object) // { - // // Disable further interrupts during reception, this prevents - // // triggering another interrupt directly after we return, which can - // // cause problems at higher baudrates. - // setRxIntMsk(false); + // uint8_t data = 0; - // // Wait approximately 1/2 of a bit width to "center" the sample - // tunedDelay(_rx_delay_centering); - // DebugPulse(_DEBUG_PIN2, 1); - - // // Read each of the 8 bits - // for (uint8_t i=8; i > 0; --i) + // // If RX line is high, then we don't see any start bit + // // so interrupt is probably not for us + // if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) // { - // tunedDelay(_rx_delay_intrabit); - // data >>= 1; - // DebugPulse(_DEBUG_PIN2, 1); - // if (rx_pin_read()) - // data |= 0x80; + // // Disable further interrupts during reception, this prevents + // // triggering another interrupt directly after we return, which can + // // cause problems at higher baudrates. + // setRxIntMsk(false); + + // // Wait approximately 1/2 of a bit width to "center" the sample + // tunedDelay(_rx_delay_centering); + // // DebugPulse(_DEBUG_PIN2, 1); + + // // Read each of the 8 bits + // for (uint8_t i=8; i > 0; --i) + // { + // tunedDelay(_rx_delay_intrabit); + // data >>= 1; + // // DebugPulse(_DEBUG_PIN2, 1); + // if (rx_pin_read()) + // data |= 0x80; + // } + + // if (_inverse_logic) + // data = ~data; + + // // if buffer full, set the overflow flag and return + // uint8_t next = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF; + // if (next != _receive_buffer_head) + // { + // // save new data in buffer: tail points to where byte goes + // _receive_buffer[_receive_buffer_tail] = data; // save new byte + // _receive_buffer_tail = next; + // } + // else + // { + // // DebugPulse(_DEBUG_PIN1, 1); + // _buffer_overflow = true; + // } + + // // skip the stop bit + // tunedDelay(_rx_delay_stopbit); + // // DebugPulse(_DEBUG_PIN1, 1); + + // // Re-enable interrupts when we're sure to be inside the stop bit + // setRxIntMsk(true); // } - - // if (_inverse_logic) - // data = ~data; - - // // if buffer full, set the overflow flag and return - // uint8_t next = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF; - // if (next != _receive_buffer_head) - // { - // // save new data in buffer: tail points to where byte goes - // _receive_buffer[_receive_buffer_tail] = data; // save new byte - // _receive_buffer_tail = next; - // } - // else - // { - // DebugPulse(_DEBUG_PIN1, 1); - // _buffer_overflow = true; - // } - - // // skip the stop bit - // tunedDelay(_rx_delay_stopbit); - // DebugPulse(_DEBUG_PIN1, 1); - - // // Re-enable interrupts when we're sure to be inside the stop bit - // setRxIntMsk(true); - // } - -// #if GCC_VERSION < 40302 -// // Work-around for avr-gcc 4.3.0 OSX version bug -// // Restore the registers that the compiler misses -// asm volatile( -// "pop r27 \n\t" -// "pop r26 \n\t" -// "pop r23 \n\t" -// "pop r22 \n\t" -// "pop r21 \n\t" -// "pop r20 \n\t" -// "pop r19 \n\t" -// "pop r18 \n\t" -// ::); -// #endif } uint8_t SoftwareSerial::rx_pin_read() @@ -259,15 +232,17 @@ void SoftwareSerial::setTX(uint8_t tx) void SoftwareSerial::setRX(uint8_t rx) { - if (!_inverse_logic) - pinMode(rx, INPUT_PULLUP); // pullup for normal logic - else - pinMode(rx, INPUT); _receiveBitMask = (uint16_t)digitalPinToBitMask(rx); // save rx bit mask _receivePortRegister = (uint32_t)digitalPinToPort(rx); // save rx port adress + // attachInterrupt(digitalPinToInterrupt(rx), [this]() { this->recv(); }, _inverse_logic ? RISING : FALLING); + if (!_inverse_logic) + pinMode(rx, INPUT_PULLUP); // pullup for normal logic + // HAL_GPIO_PinConfig((GPIO_TypeDef*)_receivePortRegister, (HAL_PinsTypeDef)_receiveBitMask, HAL_GPIO_MODE_GPIO_INPUT, HAL_GPIO_PULL_UP, HAL_GPIO_DS_2MA); + else + pinMode(rx, INPUT); } -uint16_t SoftwareSerial::subtract_cap(uint16_t num, uint16_t sub) +uint32_t SoftwareSerial::subtract_cap(uint32_t num, uint16_t sub) { if (num > sub) return num - sub; @@ -292,74 +267,75 @@ void SoftwareSerial::begin(long speed) // Precalculate the various delays in number of ticks uint32_t bit_delay = F_CPU / speed; - + // 12 (gcc 4.8.2) or 13 (gcc 4.3.2) cycles from start bit to first bit, // 15 (gcc 4.8.2) or 16 (gcc 4.3.2) cycles between bits, // 12 (gcc 4.8.2) or 14 (gcc 4.3.2) cycles from last bit to stop bit // These are all close enough to just use 15 cycles, since the inter-bit // timings are the most critical (deviations stack 8 times) - _tx_delay = subtract_cap(bit_delay, 15); + _tx_delay = subtract_cap(bit_delay, 35); // 15 // Only setup rx when we have a valid INT for this pin if (digitalPinToInterrupt(_receivePin) != NOT_AN_INTERRUPT) { - #if GCC_VERSION > 40800 - // Timings counted from gcc 4.8.2 output. This works up to 115200 on - // 16Mhz and 57600 on 8Mhz. - // - // When the start bit occurs, there are 3 or 4 cycles before the - // interrupt flag is set, 4 cycles before the PC is set to the right - // interrupt vector address and the old PC is pushed on the stack, - // and then 75 cycles of instructions (including the RJMP in the - // ISR vector table) until the first delay. After the delay, there - // are 17 more cycles until the pin value is read (excluding the - // delay in the loop). - // We want to have a total delay of 1.5 bit time. Inside the loop, - // we already wait for 1 bit time - 23 cycles, so here we wait for - // 0.5 bit time - (71 + 18 - 22) cycles. - _rx_delay_centering = subtract_cap(bit_delay / 2, (4 + 4 + 75 + 17 - 23)); + // #if GCC_VERSION > 40800 + // // Timings counted from gcc 4.8.2 output. This works up to 115200 on + // // 16Mhz and 57600 on 8Mhz. + // // + // // When the start bit occurs, there are 3 or 4 cycles before the + // // interrupt flag is set, 4 cycles before the PC is set to the right + // // interrupt vector address and the old PC is pushed on the stack, + // // and then 75 cycles of instructions (including the RJMP in the + // // ISR vector table) until the first delay. After the delay, there + // // are 17 more cycles until the pin value is read (excluding the + // // delay in the loop). + // // We want to have a total delay of 1.5 bit time. Inside the loop, + // // we already wait for 1 bit time - 23 cycles, so here we wait for + // // 0.5 bit time - (71 + 18 - 22) cycles. - // There are 23 cycles in each loop iteration (excluding the delay) - _rx_delay_intrabit = subtract_cap(bit_delay, 23); + // _rx_delay_centering = subtract_cap(bit_delay / 2, (4 + 4 + 75 + 17 - 23)); - // There are 37 cycles from the last bit read to the start of - // stopbit delay and 11 cycles from the delay until the interrupt - // mask is enabled again (which _must_ happen during the stopbit). - // 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, (37 + 11)); - #else // Timings counted from gcc 4.3.2 output - // Note that this code is a _lot_ slower, mostly due to bad register - // allocation choices of gcc. This works up to 57600 on 16Mhz and - // 38400 on 8Mhz. - _rx_delay_centering = subtract_cap(bit_delay / 2, (4 + 4 + 97 + 29 - 11)); - _rx_delay_intrabit = subtract_cap(bit_delay, 11); - _rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, (44 + 17)); - #endif + // // There are 23 cycles in each loop iteration (excluding the delay) + // _rx_delay_intrabit = subtract_cap(bit_delay, 23); + + // // There are 37 cycles from the last bit read to the start of + // // stopbit delay and 11 cycles from the delay until the interrupt + // // mask is enabled again (which _must_ happen during the stopbit). + // // 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, (37 + 11)); + // #else // Timings counted from gcc 4.3.2 output + // // Note that this code is a _lot_ slower, mostly due to bad register + // // allocation choices of gcc. This works up to 57600 on 16Mhz and + // // 38400 on 8Mhz. + // _rx_delay_centering = subtract_cap(bit_delay / 2, (4 + 4 + 97 + 29 - 11)); + // _rx_delay_intrabit = subtract_cap(bit_delay, 11); + // _rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, (44 + 17)); + // #endif // Enable the INT for the entire port here, but never disable it // (others might also need it, so we disable the interrupt by using // the per-pin PCMSK register). - if (_inverse_logic) - _int_mode = GPIO_INT_MODE_RISING; // start bit: low to high - else - _int_mode = GPIO_INT_MODE_FALLING; // start bit: high to low - // Save gpio_mux and gpio_line for on/off interrupts - _int_maskMux = digitalPinToGpioIntMux(_receivePin); // gpio mux value - _int_maskLine = digitalPinToGpioIntLine(_receivePin); // gpio irq line value - HAL_EPIC_MaskEdgeSet(HAL_EPIC_GPIO_IRQ_MASK); // turn on the mask in EPIC + // if (_inverse_logic) + // _int_mode = GPIO_INT_MODE_RISING; // start bit: low to high + // else + // _int_mode = GPIO_INT_MODE_FALLING; // start bit: high to low + // // Save gpio_mux and gpio_line for on/off interrupts + // _int_maskMux = digitalPinToGpioIntMux(_receivePin); // gpio mux value + // _int_maskLine = digitalPinToGpioIntLine(_receivePin); // gpio irq line value + // HAL_EPIC_MaskEdgeSet(HAL_EPIC_GPIO_IRQ_MASK); // turn on the mask in EPIC // *digitalPinToPCICR((int8_t)_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin)); // _pcint_maskreg = digitalPinToPCMSK(_receivePin); // _pcint_maskvalue = _BV(digitalPinToPCMSKbit(_receivePin)); - - tunedDelay(_tx_delay); // if we were low this establishes the end } else ErrorMsgHandler("SoftwareSerial.begin(): Rx pin does not support interrupts, use different pin"); + tunedDelay(_tx_delay); // if we were low this establishes the end + // #if _DEBUG // pinMode(_DEBUG_PIN1, OUTPUT); // pinMode(_DEBUG_PIN2, OUTPUT); @@ -383,6 +359,50 @@ void SoftwareSerial::end() stopListening(); } +size_t SoftwareSerial::write(uint8_t byte) +{ + if (_tx_delay == 0) + { + setWriteError(); + return 0; + } + + if (_inverse_logic) + byte = ~byte; + + noInterrupts(); // turn off interrupts for a clean txmit + + // Write the start bit + if (_inverse_logic) + HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask, GPIO_PIN_HIGH); + else + HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask, GPIO_PIN_LOW); + + tunedDelay(_tx_delay); + + // Write each of the 8 bits + for (uint8_t i = 8; i > 0; --i) + { + if (byte & 1) // choose bit + HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask, GPIO_PIN_HIGH); + else + HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask, GPIO_PIN_LOW); + + tunedDelay(_tx_delay); + byte >>= 1; + } + + // restore pin to natural state + if (_inverse_logic) + HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask, GPIO_PIN_LOW); + else + HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask, GPIO_PIN_HIGH); + + interrupts(); // enable interrupts + tunedDelay(_tx_delay); + + return 1; +} // Read data from buffer int SoftwareSerial::read() @@ -408,60 +428,6 @@ int SoftwareSerial::available() return ((unsigned int)(_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head)) % _SS_MAX_RX_BUFF; } -size_t SoftwareSerial::write(uint8_t byte) -{ - if (_tx_delay == 0) - { - setWriteError(); - return 0; - } - - // By declaring these as local variables, the compiler will put them - // in registers _before_ disabling interrupts and entering the - // critical timing sections below, which makes it a lot easier to - // verify the cycle timings - uint8_t bit_mask = _transmitBitMask; // битовая маска в порту - - bool inv = _inverse_logic; - uint16_t delay = _tx_delay; - - if (inv) - byte = ~byte; - - noInterrupts(); // turn off interrupts for a clean txmit - - // Write the start bit - if (inv) - HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_HIGH); - else - HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_LOW); - - tunedDelay(delay); - - // Write each of the 8 bits - for (uint8_t i = 8; i > 0; --i) - { - if (byte & 1) // choose bit - HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_HIGH); - else - HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_LOW); - - tunedDelay(delay); - byte >>= 1; - } - - // restore pin to natural state - if (inv) - HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_LOW); - else - HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)bit_mask, GPIO_PIN_HIGH); - - interrupts(); // enable interrupts - tunedDelay(_tx_delay); - - return 1; -} - void SoftwareSerial::flush() { // There is no tx buffering, simply return diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.h b/libraries/SoftwareSerial/src/SoftwareSerial.h index 7d89f40..fa9970f 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.h +++ b/libraries/SoftwareSerial/src/SoftwareSerial.h @@ -54,7 +54,7 @@ private: uint16_t _rx_delay_centering; uint16_t _rx_delay_intrabit; uint16_t _rx_delay_stopbit; - uint16_t _tx_delay; + uint32_t _tx_delay; uint16_t _buffer_overflow:1; uint16_t _inverse_logic:1; @@ -73,10 +73,10 @@ private: inline void setRxIntMsk(bool enable) __attribute__((__always_inline__)); // Return num - sub, or 1 if the result would be < 1 - static uint16_t subtract_cap(uint16_t num, uint16_t sub); + static uint32_t subtract_cap(uint32_t num, uint16_t sub); // private static method for timing - static inline void tunedDelay(uint16_t delayTicks); + static void __attribute__( ( noinline, section(".data") ) ) tunedDelay(uint32_t delayTicks); public: // public methods -- 2.43.0 From 1d5ffdc6f083a5fa58d8540bf8ea1b0ca2933837 Mon Sep 17 00:00:00 2001 From: klassents Date: Wed, 18 Sep 2024 10:27:22 +0700 Subject: [PATCH 04/31] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D0=BB=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B8=20=D0=B2=20ram,=20?= =?UTF-8?q?=D0=B2=D0=BD=D1=83=D1=82=D1=80=D0=B8=20=D1=84=D1=83=D0=BD=D0=BA?= =?UTF-8?q?=D1=86=D0=B8=D0=B8=20=D0=B2=D1=81=D0=B5=20=D0=BD=D0=B0=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80=D0=B0=D1=85.=20=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BD=D0=B0=20=D0=BB=D1=8E?= =?UTF-8?q?=D0=B1=D1=8B=D1=85=20=D1=81=D0=BA=D0=BE=D1=80=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8F=D1=85=20=D0=B2=D1=81=D0=B5=20=D0=BE=D0=BA=20=D0=B8=20?= =?UTF-8?q?=D1=81=20=D0=BF=D0=B5=D1=80=D0=B2=D1=8B=D0=BC=20=D0=B1=D0=B0?= =?UTF-8?q?=D0=B9=D1=82=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SoftwareSerial/src/SoftwareSerial.cpp | 42 +++++++++---------- libraries/SoftwareSerial/src/SoftwareSerial.h | 4 +- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index aa15f0f..b1ab6fb 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -32,8 +32,9 @@ http://arduiniana.org. // #include #include #include -// #include "mik32_hal_irq.h" +#include "mik32_hal_irq.h" #include "mik32_hal_gpio.h" +#include "mik32_hal_scr1_timer.h" // // Statics @@ -68,11 +69,11 @@ volatile uint8_t SoftwareSerial::_receive_buffer_head = 0; // Private methods // -/* static */ -#include "mik32_hal_scr1_timer.h" +// get ticks from systick #define CURRENT_TICKS ((uint64_t)(SCR1_TIMER->MTIMEH)<<32 | SCR1_TIMER->MTIME) -void SoftwareSerial::tunedDelay(uint32_t delayTicks) +// function placed in ram. Should not call other function placed in flash +void SoftwareSerial::tunedDelay(uint32_t delayTicks) { uint64_t startTicks = CURRENT_TICKS; while ((CURRENT_TICKS - startTicks) < delayTicks) @@ -359,47 +360,46 @@ void SoftwareSerial::end() stopListening(); } +// function placed in ram. Should not call other function placed in flash size_t SoftwareSerial::write(uint8_t byte) { if (_tx_delay == 0) - { - setWriteError(); return 0; - } if (_inverse_logic) byte = ~byte; - noInterrupts(); // turn off interrupts for a clean txmit + clear_csr(mie, MIE_MEIE); // turn off interrupts for a clean txmit // Write the start bit if (_inverse_logic) - HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask, GPIO_PIN_HIGH); + ((GPIO_TypeDef *)_transmitPortRegister)->SET = (HAL_PinsTypeDef)_transmitBitMask; else - HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask, GPIO_PIN_LOW); + ((GPIO_TypeDef *)_transmitPortRegister)->CLEAR = (HAL_PinsTypeDef)_transmitBitMask; + tunedDelay(_tx_delay); // wait start bit - tunedDelay(_tx_delay); - - // Write each of the 8 bits + // Write each of the 8 bits in LSB mode for (uint8_t i = 8; i > 0; --i) { if (byte & 1) // choose bit - HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask, GPIO_PIN_HIGH); + ((GPIO_TypeDef *)_transmitPortRegister)->SET = (HAL_PinsTypeDef)_transmitBitMask; else - HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask, GPIO_PIN_LOW); - + ((GPIO_TypeDef *)_transmitPortRegister)->CLEAR = (HAL_PinsTypeDef)_transmitBitMask; tunedDelay(_tx_delay); byte >>= 1; } - // restore pin to natural state + // restore pin to natural state - stop bit if (_inverse_logic) - HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask, GPIO_PIN_LOW); + ((GPIO_TypeDef *)_transmitPortRegister)->CLEAR = (HAL_PinsTypeDef)_transmitBitMask; else - HAL_GPIO_WritePin((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask, GPIO_PIN_HIGH); + ((GPIO_TypeDef *)_transmitPortRegister)->SET = (HAL_PinsTypeDef)_transmitBitMask; - interrupts(); // enable interrupts - tunedDelay(_tx_delay); + // enable interrupts + set_csr(mstatus, MSTATUS_MIE); + set_csr(mie, MIE_MEIE); + + tunedDelay(_tx_delay); // wait stop bit return 1; } diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.h b/libraries/SoftwareSerial/src/SoftwareSerial.h index fa9970f..e3bca24 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.h +++ b/libraries/SoftwareSerial/src/SoftwareSerial.h @@ -76,7 +76,7 @@ private: static uint32_t subtract_cap(uint32_t num, uint16_t sub); // private static method for timing - static void __attribute__( ( noinline, section(".data") ) ) tunedDelay(uint32_t delayTicks); + static void tunedDelay(uint32_t delayTicks) __attribute__((noinline, section(".data"))) ; public: // public methods @@ -96,7 +96,7 @@ public: } int peek(); - virtual size_t write(uint8_t byte); + virtual size_t write(uint8_t byte) __attribute__((noinline, section(".data"))); virtual int read(); virtual int available(); virtual void flush(); -- 2.43.0 From 69e1e10a056883a34ad4005fa4b8a20625e840fd Mon Sep 17 00:00:00 2001 From: klassents Date: Thu, 19 Sep 2024 13:07:16 +0700 Subject: [PATCH 05/31] =?UTF-8?q?=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20write=20tuneDelay=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD?= =?UTF-8?q?=D0=B5=D1=81=D0=B5=D0=BD=D1=8B=20=D0=B2=20=D1=80=D0=B0=D0=BC,?= =?UTF-8?q?=20=D0=B2=20ld=20=D1=81=D0=BA=D1=80=D0=B8=D0=BF=D1=82=D0=B5=20?= =?UTF-8?q?=D0=B8=D0=B7-=D0=B7=D0=B0=20=D1=8D=D1=82=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=B0=D1=8F=20=D1=81=D0=B5=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B2=D0=BD=D1=83=D1=82=D1=80=D0=B8=20data,=20=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=BF=D1=80=D0=B8=20=D1=81=D0=B1=D0=BE=D1=80=D0=BA?= =?UTF-8?q?=D0=B5=20=D0=B5=D1=81=D1=82=D1=8C=20=D0=BF=D1=80=D0=B5=D0=B4?= =?UTF-8?q?=D1=83=D0=BF=D1=80=D0=B5=D0=B6=D0=B4=D0=B5=D0=BD=D0=B8=D1=8F.?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D1=8C=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82=20=D0=BD=D0=B0=20=D1=81=D0=BA?= =?UTF-8?q?=D0=BE=D1=80=D0=BE=D1=81=D1=82=D1=8F=D1=85=20=D0=B2=D0=BF=D0=BB?= =?UTF-8?q?=D0=BE=D1=82=D1=8C=20=D0=B4=D0=BE=20115200,=20=D1=87=D1=82?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B2=20=D0=BF=D1=80=D0=BE=D1=86?= =?UTF-8?q?=D0=B5=D1=81=D1=81=D0=B5,=20=D0=BD=D0=B0=202400=20=D1=87=D0=B8?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82=D1=81=D1=8F=20=D0=BD=D0=BE=D1=80=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mik32/shared/ldscripts/spifi_cpp.ld | 5 +- cores/arduino/trap_handler.c | 10 +- .../SoftwareSerial/src/SoftwareSerial.cpp | 275 +++++++----------- libraries/SoftwareSerial/src/SoftwareSerial.h | 4 +- 4 files changed, 120 insertions(+), 174 deletions(-) diff --git a/cores/arduino/mik32/shared/ldscripts/spifi_cpp.ld b/cores/arduino/mik32/shared/ldscripts/spifi_cpp.ld index 8a15697..18bcb89 100644 --- a/cores/arduino/mik32/shared/ldscripts/spifi_cpp.ld +++ b/cores/arduino/mik32/shared/ldscripts/spifi_cpp.ld @@ -53,9 +53,10 @@ SECTIONS { *(.srodata.cst16) *(.srodata.cst8) *(.srodata.cst4) *(.srodata.cst2) *(.srodata*) *(.sdata .sdata.* .sdata* .sdata*.* .gnu.linkonce.s.*) *(.data .data.*) + *(.ramfunc) . = ALIGN(CL_SIZE); - } >ram - + } >ram + __DATA_IMAGE_START__ = LOADADDR(.data); __DATA_IMAGE_END__ = LOADADDR(.data) + SIZEOF(.data); ASSERT(__DATA_IMAGE_END__ < ORIGIN(rom) + LENGTH(rom), "Data image overflows rom section") diff --git a/cores/arduino/trap_handler.c b/cores/arduino/trap_handler.c index 5d01229..f309c87 100644 --- a/cores/arduino/trap_handler.c +++ b/cores/arduino/trap_handler.c @@ -8,10 +8,16 @@ void __attribute__((weak)) wire_handler_wrapper(void) { // dummy function for case when wire library is not in use } +extern void softSerial_handler_wrapper(void); // ---------------------------------------------- // void trap_handler(void) { + // gpio interrupt + if (EPIC_CHECK_GPIO_IRQ()) + // gpio_interrupts_handler(); + softSerial_handler_wrapper(); + // tone timer interrupt if (EPIC_CHECK_TIMER16_1()) tone_interrupt_handler(); @@ -23,10 +29,6 @@ void trap_handler(void) // uart1 interrupt if (EPIC_CHECK_UART_1()) serial_handler_wrapper(1); - - // gpio interrupt - if (EPIC_CHECK_GPIO_IRQ()) - gpio_interrupts_handler(); // i2c interrupt if (EPIC_CHECK_I2C_1()) diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index b1ab6fb..f3611f9 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -17,66 +17,39 @@ The latest version of this library can always be found at http://arduiniana.org. */ -// When set, _DEBUG co-opts pins 11 and 13 for debugging with an -// oscilloscope or logic analyzer. Beware: it also slightly modifies -// the bit times, so don't rely on it too much at high baud rates -#define _DEBUG 0 -#define _DEBUG_PIN1 11 -#define _DEBUG_PIN2 13 - -#define MAX_SOFT_SERIAL_SPEED 115200 -#define MIN_SOFT_SERIAL_SPEED 300 -// -// Includes -// -// #include #include #include #include "mik32_hal_irq.h" #include "mik32_hal_gpio.h" #include "mik32_hal_scr1_timer.h" -// +#define MAX_SOFT_SERIAL_SPEED 115200 +#define MIN_SOFT_SERIAL_SPEED 300 + +// --------- TO LL --------- +// get ticks from systick +#define SYSTICK_GET_TICKS() ((uint64_t)(SCR1_TIMER->MTIMEH)<<32 | SCR1_TIMER->MTIME) +#define GPIO_SET_PIN(GPIO_x, pin) ((GPIO_x)->SET = (pin)) +#define GPIO_CLEAR_PIN(GPIO_x, pin) ((GPIO_x)->CLEAR = (pin)) +#define GPIO_READ_PIN(GPIO_x, pin) (((GPIO_x)->SET & (pin)) != (uint32_t)GPIO_PIN_LOW ? GPIO_PIN_HIGH : GPIO_PIN_LOW) + +// --------- TO LL --------- + + // Statics -// +// static variable is common to all class instances SoftwareSerial *SoftwareSerial::active_object = 0; uint8_t SoftwareSerial::_receive_buffer[_SS_MAX_RX_BUFF]; volatile uint8_t SoftwareSerial::_receive_buffer_tail = 0; volatile uint8_t SoftwareSerial::_receive_buffer_head = 0; -// -// Debugging -// -// This function generates a brief pulse -// for debugging or measuring on an oscilloscope. -// #if _DEBUG -// inline void DebugPulse(uint8_t pin, uint8_t count) -// { -// volatile uint8_t *pport = portOutputRegister(digitalPinToPort(pin)); - -// uint8_t val = *pport; -// while (count--) -// { -// *pport = val | digitalPinToBitMask(pin); -// *pport = val; -// } -// } -// #else -// inline void DebugPulse(uint8_t, uint8_t) {} -// #endif - -// // Private methods -// - -// get ticks from systick -#define CURRENT_TICKS ((uint64_t)(SCR1_TIMER->MTIMEH)<<32 | SCR1_TIMER->MTIME) // function placed in ram. Should not call other function placed in flash void SoftwareSerial::tunedDelay(uint32_t delayTicks) { - uint64_t startTicks = CURRENT_TICKS; - while ((CURRENT_TICKS - startTicks) < delayTicks) + uint64_t startTicks = SYSTICK_GET_TICKS(); + while ((SYSTICK_GET_TICKS() - startTicks) < delayTicks) ; } @@ -120,71 +93,68 @@ bool SoftwareSerial::stopListening() // void SoftwareSerial::recv() { - // if (active_object) - // { - // uint8_t data = 0; + uint8_t data = 0; + // If RX line is high, then we don't see any start bit + // so interrupt is probably not for us + if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) + { + HAL_GPIO_WritePin(GPIO_0, GPIO_PIN_8, GPIO_PIN_HIGH); + // Disable further interrupts during reception, this prevents + // triggering another interrupt directly after we return, which can + // cause problems at higher baudrates. + setRxIntMsk(false); - // // If RX line is high, then we don't see any start bit - // // so interrupt is probably not for us - // if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) - // { - // // Disable further interrupts during reception, this prevents - // // triggering another interrupt directly after we return, which can - // // cause problems at higher baudrates. - // setRxIntMsk(false); + // Wait approximately 1/2 of a bit width to "center" the sample + tunedDelay(_rx_delay_centering); + // DebugPulse(_DEBUG_PIN2, 1); - // // Wait approximately 1/2 of a bit width to "center" the sample - // tunedDelay(_rx_delay_centering); - // // DebugPulse(_DEBUG_PIN2, 1); + // Read each of the 8 bits + for (uint8_t i=8; i > 0; --i) + { + tunedDelay(_rx_delay_intrabit); + data >>= 1; + // DebugPulse(_DEBUG_PIN2, 1); + if (rx_pin_read()) + data |= 0x80; + } - // // Read each of the 8 bits - // for (uint8_t i=8; i > 0; --i) - // { - // tunedDelay(_rx_delay_intrabit); - // data >>= 1; - // // DebugPulse(_DEBUG_PIN2, 1); - // if (rx_pin_read()) - // data |= 0x80; - // } + if (_inverse_logic) + data = ~data; - // if (_inverse_logic) - // data = ~data; + // if buffer full, set the overflow flag and return + uint8_t next = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF; + if (next != _receive_buffer_head) + { + // save new data in buffer: tail points to where byte goes + _receive_buffer[_receive_buffer_tail] = data; // save new byte + _receive_buffer_tail = next; + } + else + { + // DebugPulse(_DEBUG_PIN1, 1); + _buffer_overflow = true; + } - // // if buffer full, set the overflow flag and return - // uint8_t next = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF; - // if (next != _receive_buffer_head) - // { - // // save new data in buffer: tail points to where byte goes - // _receive_buffer[_receive_buffer_tail] = data; // save new byte - // _receive_buffer_tail = next; - // } - // else - // { - // // DebugPulse(_DEBUG_PIN1, 1); - // _buffer_overflow = true; - // } + // skip the stop bit + tunedDelay(_rx_delay_stopbit); + // DebugPulse(_DEBUG_PIN1, 1); - // // skip the stop bit - // tunedDelay(_rx_delay_stopbit); - // // DebugPulse(_DEBUG_PIN1, 1); - - // // Re-enable interrupts when we're sure to be inside the stop bit - // setRxIntMsk(true); - // } - // } + // Re-enable interrupts when we're sure to be inside the stop bit + setRxIntMsk(true); + HAL_GPIO_WritePin(GPIO_0, GPIO_PIN_8, GPIO_PIN_LOW); + } + HAL_GPIO_ClearInterrupts(); } uint8_t SoftwareSerial::rx_pin_read() { - return (uint8_t)HAL_GPIO_ReadPin((GPIO_TypeDef *)_receivePortRegister, (HAL_PinsTypeDef)_receiveBitMask); + return (uint8_t)GPIO_READ_PIN((GPIO_TypeDef *)_receivePortRegister, (HAL_PinsTypeDef)_receiveBitMask); } -// // Interrupt handling -// /* static */ -inline void SoftwareSerial::handle_interrupt() // todo унести в обработчик прерываний +inline void SoftwareSerial::handle_interrupt() { if (active_object) { @@ -192,12 +162,11 @@ inline void SoftwareSerial::handle_interrupt() // todo унести в обра } } -// #if defined(PCINT0_vect) -// ISR(PCINT0_vect) -// { -// SoftwareSerial::handle_interrupt(); -// } -// #endif +extern "C" void softSerial_handler_wrapper(void) +{ + SoftwareSerial::handle_interrupt(); +} + // // Constructor @@ -235,10 +204,8 @@ void SoftwareSerial::setRX(uint8_t rx) { _receiveBitMask = (uint16_t)digitalPinToBitMask(rx); // save rx bit mask _receivePortRegister = (uint32_t)digitalPinToPort(rx); // save rx port adress - // attachInterrupt(digitalPinToInterrupt(rx), [this]() { this->recv(); }, _inverse_logic ? RISING : FALLING); if (!_inverse_logic) pinMode(rx, INPUT_PULLUP); // pullup for normal logic - // HAL_GPIO_PinConfig((GPIO_TypeDef*)_receivePortRegister, (HAL_PinsTypeDef)_receiveBitMask, HAL_GPIO_MODE_GPIO_INPUT, HAL_GPIO_PULL_UP, HAL_GPIO_DS_2MA); else pinMode(rx, INPUT); } @@ -269,78 +236,54 @@ void SoftwareSerial::begin(long speed) // Precalculate the various delays in number of ticks uint32_t bit_delay = F_CPU / speed; - // 12 (gcc 4.8.2) or 13 (gcc 4.3.2) cycles from start bit to first bit, - // 15 (gcc 4.8.2) or 16 (gcc 4.3.2) cycles between bits, - // 12 (gcc 4.8.2) or 14 (gcc 4.3.2) cycles from last bit to stop bit - // These are all close enough to just use 15 cycles, since the inter-bit - // timings are the most critical (deviations stack 8 times) + // empirical value _tx_delay = subtract_cap(bit_delay, 35); // 15 // Only setup rx when we have a valid INT for this pin if (digitalPinToInterrupt(_receivePin) != NOT_AN_INTERRUPT) { - // #if GCC_VERSION > 40800 - // // Timings counted from gcc 4.8.2 output. This works up to 115200 on - // // 16Mhz and 57600 on 8Mhz. - // // - // // When the start bit occurs, there are 3 or 4 cycles before the - // // interrupt flag is set, 4 cycles before the PC is set to the right - // // interrupt vector address and the old PC is pushed on the stack, - // // and then 75 cycles of instructions (including the RJMP in the - // // ISR vector table) until the first delay. After the delay, there - // // are 17 more cycles until the pin value is read (excluding the - // // delay in the loop). - // // We want to have a total delay of 1.5 bit time. Inside the loop, - // // we already wait for 1 bit time - 23 cycles, so here we wait for - // // 0.5 bit time - (71 + 18 - 22) cycles. - // _rx_delay_centering = subtract_cap(bit_delay / 2, (4 + 4 + 75 + 17 - 23)); + // When the start bit occurs, there are 3 or 4 cycles before the + // interrupt flag is set, 4 cycles before the PC is set to the right + // interrupt vector address and the old PC is pushed on the stack, + // and then 75 cycles of instructions (including the RJMP in the + // ISR vector table) until the first delay. After the delay, there + // are 17 more cycles until the pin value is read (excluding the + // delay in the loop). + // We want to have a total delay of 1.5 bit time. Inside the loop, + // we already wait for 1 bit time - 23 cycles, so here we wait for + // 0.5 bit time - (71 + 18 - 22) cycles. + // _rx_delay_centering = subtract_cap(bit_delay / 2, (77)); + _rx_delay_centering = subtract_cap(bit_delay / 2, (123)); - // // There are 23 cycles in each loop iteration (excluding the delay) + // There are 23 cycles in each loop iteration (excluding the delay) // _rx_delay_intrabit = subtract_cap(bit_delay, 23); + _rx_delay_intrabit = subtract_cap(bit_delay, 11); - // // There are 37 cycles from the last bit read to the start of - // // stopbit delay and 11 cycles from the delay until the interrupt - // // mask is enabled again (which _must_ happen during the stopbit). - // // 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, (37 + 11)); - // #else // Timings counted from gcc 4.3.2 output - // // Note that this code is a _lot_ slower, mostly due to bad register - // // allocation choices of gcc. This works up to 57600 on 16Mhz and - // // 38400 on 8Mhz. - // _rx_delay_centering = subtract_cap(bit_delay / 2, (4 + 4 + 97 + 29 - 11)); - // _rx_delay_intrabit = subtract_cap(bit_delay, 11); - // _rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, (44 + 17)); - // #endif - - // Enable the INT for the entire port here, but never disable it - // (others might also need it, so we disable the interrupt by using - // the per-pin PCMSK register). - // if (_inverse_logic) - // _int_mode = GPIO_INT_MODE_RISING; // start bit: low to high - // else - // _int_mode = GPIO_INT_MODE_FALLING; // start bit: high to low - // // Save gpio_mux and gpio_line for on/off interrupts - // _int_maskMux = digitalPinToGpioIntMux(_receivePin); // gpio mux value - // _int_maskLine = digitalPinToGpioIntLine(_receivePin); // gpio irq line value - // HAL_EPIC_MaskEdgeSet(HAL_EPIC_GPIO_IRQ_MASK); // turn on the mask in EPIC - - // *digitalPinToPCICR((int8_t)_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin)); - // _pcint_maskreg = digitalPinToPCMSK(_receivePin); - // _pcint_maskvalue = _BV(digitalPinToPCMSKbit(_receivePin)); + // There are 37 cycles from the last bit read to the start of + // stopbit delay and 11 cycles from the delay until the interrupt + // mask is enabled again (which _must_ happen during the stopbit). + // 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, (48)); + _rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, (61)); + + // Save start bit polarity - low to high if _inverse_logic + _int_mode = _inverse_logic ? (uint8_t)GPIO_INT_MODE_RISING : (uint8_t)GPIO_INT_MODE_FALLING; + // Save gpio_mux and gpio_line for on/off interrupts + _int_maskMux = digitalPinToGpioIntMux(_receivePin); // gpio mux value + _int_maskLine = digitalPinToGpioIntLine(_receivePin); // gpio irq line value + 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"); tunedDelay(_tx_delay); // if we were low this establishes the end -// #if _DEBUG -// pinMode(_DEBUG_PIN1, OUTPUT); -// pinMode(_DEBUG_PIN2, OUTPUT); -// #endif + HAL_GPIO_PinConfig(GPIO_0, GPIO_PIN_8, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); + listen(); } @@ -348,11 +291,11 @@ void SoftwareSerial::begin(long speed) void SoftwareSerial::setRxIntMsk(bool enable) { if (enable) + // enable line interrupt HAL_GPIO_InitInterruptLine((HAL_GPIO_Line_Config)_int_maskMux, (HAL_GPIO_InterruptMode)_int_mode); - // *_pcint_maskreg |= _pcint_maskvalue; else + // disable line interrupt HAL_GPIO_DeInitInterruptLine((HAL_GPIO_Line)_int_maskLine); - // *_pcint_maskreg &= ~_pcint_maskvalue; } void SoftwareSerial::end() @@ -373,27 +316,27 @@ size_t SoftwareSerial::write(uint8_t byte) // Write the start bit if (_inverse_logic) - ((GPIO_TypeDef *)_transmitPortRegister)->SET = (HAL_PinsTypeDef)_transmitBitMask; + GPIO_SET_PIN((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask); else - ((GPIO_TypeDef *)_transmitPortRegister)->CLEAR = (HAL_PinsTypeDef)_transmitBitMask; + GPIO_CLEAR_PIN((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask); tunedDelay(_tx_delay); // wait start bit // Write each of the 8 bits in LSB mode for (uint8_t i = 8; i > 0; --i) { if (byte & 1) // choose bit - ((GPIO_TypeDef *)_transmitPortRegister)->SET = (HAL_PinsTypeDef)_transmitBitMask; + GPIO_SET_PIN((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask); else - ((GPIO_TypeDef *)_transmitPortRegister)->CLEAR = (HAL_PinsTypeDef)_transmitBitMask; + GPIO_CLEAR_PIN((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask); tunedDelay(_tx_delay); byte >>= 1; } // restore pin to natural state - stop bit if (_inverse_logic) - ((GPIO_TypeDef *)_transmitPortRegister)->CLEAR = (HAL_PinsTypeDef)_transmitBitMask; + GPIO_CLEAR_PIN((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask); else - ((GPIO_TypeDef *)_transmitPortRegister)->SET = (HAL_PinsTypeDef)_transmitBitMask; + GPIO_SET_PIN((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask); // enable interrupts set_csr(mstatus, MSTATUS_MIE); diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.h b/libraries/SoftwareSerial/src/SoftwareSerial.h index e3bca24..cfb706d 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.h +++ b/libraries/SoftwareSerial/src/SoftwareSerial.h @@ -76,7 +76,7 @@ private: static uint32_t subtract_cap(uint32_t num, uint16_t sub); // private static method for timing - static void tunedDelay(uint32_t delayTicks) __attribute__((noinline, section(".data"))) ; + static void tunedDelay(uint32_t delayTicks) __attribute__((noinline, section(".ramfunc"))); public: // public methods @@ -96,7 +96,7 @@ public: } int peek(); - virtual size_t write(uint8_t byte) __attribute__((noinline, section(".data"))); + virtual size_t write(uint8_t byte) __attribute__((noinline, section(".ramfunc"))); virtual int read(); virtual int available(); virtual void flush(); -- 2.43.0 From cb95c51a3fc9d36f6d7052c2c63e0a03f51940e8 Mon Sep 17 00:00:00 2001 From: klassents Date: Thu, 19 Sep 2024 14:32:57 +0700 Subject: [PATCH 06/31] =?UTF-8?q?=D0=BF=D0=BE=D0=BA=D1=80=D1=83=D1=82?= =?UTF-8?q?=D0=B8=D0=BB=D0=B0=20=D1=82=D0=B0=D0=B9=D0=BC=D0=B8=D0=BD=D0=B3?= =?UTF-8?q?=D0=B8=20=D0=BD=D0=B0=20=D0=BF=D1=80=D0=B8=D0=B5=D0=BC,=20?= =?UTF-8?q?=D1=87=D1=82=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B1=D0=B0=D0=B9=D1=82?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82=20?= =?UTF-8?q?=D0=BE=D1=82=20300=20=D0=B4=D0=BE=209600,=20=D0=B4=D0=B0=D0=BB?= =?UTF-8?q?=D1=8C=D1=88=D0=B5=20=D0=BD=D0=B0=D0=B4=D0=BE=20=D1=83=D1=81?= =?UTF-8?q?=D0=BA=D0=BE=D1=80=D1=8F=D1=82=D1=8C=20=D1=84=D1=83=D0=BD=D0=BA?= =?UTF-8?q?=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SoftwareSerial/src/SoftwareSerial.cpp | 50 ++++++++----------- libraries/SoftwareSerial/src/SoftwareSerial.h | 8 +-- 2 files changed, 24 insertions(+), 34 deletions(-) diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index f3611f9..fe3a8ed 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -98,7 +98,8 @@ void SoftwareSerial::recv() // so interrupt is probably not for us if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) { - HAL_GPIO_WritePin(GPIO_0, GPIO_PIN_8, GPIO_PIN_HIGH); + GPIO_SET_PIN(GPIO_0, GPIO_PIN_8); + GPIO_CLEAR_PIN(GPIO_0, GPIO_PIN_1); // Disable further interrupts during reception, this prevents // triggering another interrupt directly after we return, which can // cause problems at higher baudrates. @@ -106,14 +107,16 @@ void SoftwareSerial::recv() // Wait approximately 1/2 of a bit width to "center" the sample tunedDelay(_rx_delay_centering); - // DebugPulse(_DEBUG_PIN2, 1); + GPIO_0->OUTPUT_ ^= GPIO_PIN_1; // Read each of the 8 bits for (uint8_t i=8; i > 0; --i) { tunedDelay(_rx_delay_intrabit); data >>= 1; - // DebugPulse(_DEBUG_PIN2, 1); + + GPIO_0->OUTPUT_ ^= GPIO_PIN_1; + if (rx_pin_read()) data |= 0x80; } @@ -131,17 +134,16 @@ void SoftwareSerial::recv() } else { - // DebugPulse(_DEBUG_PIN1, 1); _buffer_overflow = true; } // skip the stop bit tunedDelay(_rx_delay_stopbit); - // DebugPulse(_DEBUG_PIN1, 1); + GPIO_0->OUTPUT_ ^= GPIO_PIN_1; // Re-enable interrupts when we're sure to be inside the stop bit setRxIntMsk(true); - HAL_GPIO_WritePin(GPIO_0, GPIO_PIN_8, GPIO_PIN_LOW); + GPIO_CLEAR_PIN(GPIO_0, GPIO_PIN_8); } HAL_GPIO_ClearInterrupts(); } @@ -236,39 +238,27 @@ void SoftwareSerial::begin(long speed) // Precalculate the various delays in number of ticks uint32_t bit_delay = F_CPU / speed; - // empirical value - _tx_delay = subtract_cap(bit_delay, 35); // 15 + // delays are empirical values here + + // 1 bit delay while transmitting data + _tx_delay = subtract_cap(bit_delay, 35); // Only setup rx when we have a valid INT for this pin if (digitalPinToInterrupt(_receivePin) != NOT_AN_INTERRUPT) { + // 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, 750); - // When the start bit occurs, there are 3 or 4 cycles before the - // interrupt flag is set, 4 cycles before the PC is set to the right - // interrupt vector address and the old PC is pushed on the stack, - // and then 75 cycles of instructions (including the RJMP in the - // ISR vector table) until the first delay. After the delay, there - // are 17 more cycles until the pin value is read (excluding the - // delay in the loop). - // We want to have a total delay of 1.5 bit time. Inside the loop, - // we already wait for 1 bit time - 23 cycles, so here we wait for - // 0.5 bit time - (71 + 18 - 22) cycles. - // _rx_delay_centering = subtract_cap(bit_delay / 2, (77)); - _rx_delay_centering = subtract_cap(bit_delay / 2, (123)); + // 1 bit time + _rx_delay_intrabit = subtract_cap(bit_delay, 200); - // There are 23 cycles in each loop iteration (excluding the delay) - // _rx_delay_intrabit = subtract_cap(bit_delay, 23); - _rx_delay_intrabit = subtract_cap(bit_delay, 11); - - // There are 37 cycles from the last bit read to the start of - // stopbit delay and 11 cycles from the delay until the interrupt - // mask is enabled again (which _must_ happen during the stopbit). // 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, (48)); - _rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, (61)); + _rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, 950); // Save start bit polarity - low to high if _inverse_logic _int_mode = _inverse_logic ? (uint8_t)GPIO_INT_MODE_RISING : (uint8_t)GPIO_INT_MODE_FALLING; @@ -283,7 +273,7 @@ void SoftwareSerial::begin(long speed) tunedDelay(_tx_delay); // if we were low this establishes the end HAL_GPIO_PinConfig(GPIO_0, GPIO_PIN_8, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); - + HAL_GPIO_PinConfig(GPIO_0, GPIO_PIN_1, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); listen(); } diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.h b/libraries/SoftwareSerial/src/SoftwareSerial.h index cfb706d..4a23ee4 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.h +++ b/libraries/SoftwareSerial/src/SoftwareSerial.h @@ -50,10 +50,10 @@ private: uint8_t _int_maskLine; uint8_t _int_mode; - // Expressed as 4-cycle delays (must never be 0!) - uint16_t _rx_delay_centering; - uint16_t _rx_delay_intrabit; - uint16_t _rx_delay_stopbit; + // must never be 0! + uint32_t _rx_delay_centering; + uint32_t _rx_delay_intrabit; + uint32_t _rx_delay_stopbit; uint32_t _tx_delay; uint16_t _buffer_overflow:1; -- 2.43.0 From fbe6abaa4f3f21091701fa1aa9ec04b3fe5e4eca Mon Sep 17 00:00:00 2001 From: klassents Date: Fri, 20 Sep 2024 08:43:58 +0700 Subject: [PATCH 07/31] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0=20=D0=B4=D0=B5=D1=84=D0=B0=D0=B9=D0=BD=D1=8B,=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=BE=D0=B1=D1=85=D0=BE=D0=B4=D0=B8=D0=BC=D1=8B?= =?UTF-8?q?=D0=B5=20=D0=B4=D0=BB=D1=8F=20=D0=BD=D0=B5=D0=BA=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D1=8B=D1=85=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE?= =?UTF-8?q?=D1=82=D0=B5=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/wiring_time.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cores/arduino/wiring_time.h b/cores/arduino/wiring_time.h index da9698b..9f1e690 100644 --- a/cores/arduino/wiring_time.h +++ b/cores/arduino/wiring_time.h @@ -24,6 +24,10 @@ extern "C" { #endif +#define clockCyclesPerMicrosecond() (F_CPU / 1000000L) +#define clockCyclesToMicroseconds(a) ((a) / clockCyclesPerMicrosecond()) +#define microsecondsToClockCycles(a) ((a) * clockCyclesPerMicrosecond()) + /** * \brief Initialize timer for creating delays * -- 2.43.0 From 5d5ec4de2290e3f85f087ac209e05362dbed9238 Mon Sep 17 00:00:00 2001 From: klassents Date: Wed, 25 Sep 2024 10:29:59 +0700 Subject: [PATCH 08/31] =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D1=81?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B8=D0=B5=D0=BC=D0=BE=D0=BC=20=D0=B4=D0=B0?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D1=85.=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=B5=D0=BC=20=D0=B8=D0=B7=20ram,=20=D0=B2=20=D0=BE?= =?UTF-8?q?=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=87=D0=B8=D0=BA=D0=B5=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B5=D1=80=D1=8B=D0=B2=D0=B0=D0=BD=D0=B8=D0=B9=20?= =?UTF-8?q?=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BD=D1=83=D0=B6=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5=20=D1=81=D0=B5=D0=B9=D1=87=D0=B0=D1=81=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B5=D1=80=D1=8B=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F.=20?= =?UTF-8?q?=D0=B2=D1=81=D0=B5=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20=D0=B2=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D0=B5=20SS=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=BC=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=D1=83=D0=BC=D1=83?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B4=D0=B5=D1=84=D0=B0=D0=B9=D0=BD=D1=8B=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D1=83=D1=81=D0=BA=D0=BE=D1=80=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F.=20=D0=B2=20=D1=82=D0=B0=D0=BA=D0=B8=D1=85=20=D0=BF?= =?UTF-8?q?=D0=B0=D1=80=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2=D1=8B=D1=85=20=D1=83?= =?UTF-8?q?=D1=81=D0=BB=D0=BE=D0=B2=D0=B8=D1=8F=D1=85=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82=20=D0=B8=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=D0=B5=D0=BC,=20=D0=B8=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BD=D0=B5=D1=81=D0=BA=D0=BE=D0=BB=D1=8C=D0=BA?= =?UTF-8?q?=D0=B8=D1=85=20=D0=B1=D0=B0=D0=B9=D1=82=20=D0=B4=D0=BE=20115200?= =?UTF-8?q?=20=D0=B2=D0=BA=D0=BB=D1=8E=D1=87=D0=B8=D1=82=D0=B5=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/mik32/shared/ldscripts/ram.ld | 37 +++++++++--- cores/arduino/trap_handler.c | 23 +++++--- .../SoftwareSerial/src/SoftwareSerial.cpp | 58 ++++++++++++------- platform.txt | 3 +- 4 files changed, 83 insertions(+), 38 deletions(-) diff --git a/cores/arduino/mik32/shared/ldscripts/ram.ld b/cores/arduino/mik32/shared/ldscripts/ram.ld index 29049ce..7f9ed1e 100644 --- a/cores/arduino/mik32/shared/ldscripts/ram.ld +++ b/cores/arduino/mik32/shared/ldscripts/ram.ld @@ -10,36 +10,52 @@ MEMORY { } STACK_SIZE = 1024; +HEAP_SIZE = 1024; CL_SIZE = 16; SECTIONS { .text ORIGIN(ram) : { PROVIDE(__TEXT_START__ = .); - *crt0.o(.text .text.*) + *crt0.S.o(.text .text.*) *(.text.smallsysteminit) *(.text.SmallSystemInit) . = ORIGIN(ram) + 0xC0; - KEEP(*crt0.o(.trap_text)) + KEEP(*crt0.S.o(.trap_text)) *(.text) *(.text.*) *(.rodata) *(.rodata.*) . = ALIGN(CL_SIZE); - PROVIDE(__TEXT_END__ = .); } >ram + .init_array (READONLY) : + { + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) + KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) + PROVIDE_HIDDEN (__init_array_end = .); + + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP(*(SORT_BY_INIT_PRIORITY(.fini_array.*))) + KEEP(*(.fini_array)) + PROVIDE_HIDDEN (__fini_array_end = .); + + PROVIDE(__TEXT_END__ = .); + } >ram AT>ram + .data : AT( __TEXT_END__ ) { PROVIDE(__DATA_START__ = .); _gp = .; *(.srodata.cst16) *(.srodata.cst8) *(.srodata.cst4) *(.srodata.cst2) *(.srodata*) - *(.sdata .sdata.* .gnu.linkonce.s.*) + *(.sdata .sdata.* .sdata* .sdata*.* .gnu.linkonce.s.*) *(.data .data.*) + *(.ramfunc) . = ALIGN(CL_SIZE); - } >ram - + } >ram + __DATA_IMAGE_START__ = LOADADDR(.data); __DATA_IMAGE_END__ = LOADADDR(.data) + SIZEOF(.data); ASSERT(__DATA_IMAGE_END__ < ORIGIN(ram) + LENGTH(ram) - STACK_SIZE, "Data image overflows ram section") @@ -72,12 +88,19 @@ SECTIONS { PROVIDE(__BSS_END__ = .); } >ram + .heap : { + PROVIDE(__heap_start = .); + . += HEAP_SIZE; + . = ALIGN(CL_SIZE); + PROVIDE(__heap_end = .); + } >ram + _end = .; PROVIDE(__end = .); /* End of uninitalized data segement */ - .stack ORIGIN(ram) + LENGTH(ram) - STACK_SIZE : { + .stack ORIGIN(ram) + LENGTH(ram) - STACK_SIZE: { FILL(0); PROVIDE(__STACK_START__ = .); . += STACK_SIZE; diff --git a/cores/arduino/trap_handler.c b/cores/arduino/trap_handler.c index f309c87..f756081 100644 --- a/cores/arduino/trap_handler.c +++ b/cores/arduino/trap_handler.c @@ -1,5 +1,8 @@ #include "mik32_hal_irq.h" +#include "mik32_hal_gpio.h" + + // isr functions extern void serial_handler_wrapper(uint8_t uartNumInt); extern void gpio_interrupts_handler(void); @@ -13,6 +16,7 @@ extern void softSerial_handler_wrapper(void); // ---------------------------------------------- // void trap_handler(void) { + ((GPIO_0)->SET = (GPIO_PIN_8)); // gpio interrupt if (EPIC_CHECK_GPIO_IRQ()) // gpio_interrupts_handler(); @@ -22,18 +26,19 @@ void trap_handler(void) if (EPIC_CHECK_TIMER16_1()) tone_interrupt_handler(); - // uart0 interrupt - if (EPIC_CHECK_UART_0()) - serial_handler_wrapper(0); + // // uart0 interrupt + // if (EPIC_CHECK_UART_0()) + // serial_handler_wrapper(0); - // uart1 interrupt - if (EPIC_CHECK_UART_1()) - serial_handler_wrapper(1); + // // uart1 interrupt + // if (EPIC_CHECK_UART_1()) + // serial_handler_wrapper(1); - // i2c interrupt - if (EPIC_CHECK_I2C_1()) - wire_handler_wrapper(); + // // i2c interrupt + // if (EPIC_CHECK_I2C_1()) + // wire_handler_wrapper(); // reset all interrupts HAL_EPIC_Clear(0xFFFFFFFF); + ((GPIO_0)->CLEAR = (GPIO_PIN_8)); } diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index fe3a8ed..e107e0d 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -32,7 +32,9 @@ http://arduiniana.org. #define GPIO_SET_PIN(GPIO_x, pin) ((GPIO_x)->SET = (pin)) #define GPIO_CLEAR_PIN(GPIO_x, pin) ((GPIO_x)->CLEAR = (pin)) #define GPIO_READ_PIN(GPIO_x, pin) (((GPIO_x)->SET & (pin)) != (uint32_t)GPIO_PIN_LOW ? GPIO_PIN_HIGH : GPIO_PIN_LOW) - +#define GPIO_IRQ_LINE_ENABLE(lineMask) ( GPIO_IRQ->ENABLE_SET = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) +#define GPIO_IRQ_LINE_DISABLE(lineMask) ( GPIO_IRQ->ENABLE_CLEAR = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) +#define GPIO_IRQ_CLEAR_ALL() (GPIO_IRQ->CLEAR = 0b11111111) // --------- TO LL --------- @@ -48,9 +50,12 @@ volatile uint8_t SoftwareSerial::_receive_buffer_head = 0; // function placed in ram. Should not call other function placed in flash void SoftwareSerial::tunedDelay(uint32_t delayTicks) { - uint64_t startTicks = SYSTICK_GET_TICKS(); - while ((SYSTICK_GET_TICKS() - startTicks) < delayTicks) - ; + if (delayTicks > 1) + { + uint64_t startTicks = SYSTICK_GET_TICKS(); + while ((SYSTICK_GET_TICKS() - startTicks) < delayTicks) + ; + } } // This function sets the current object as the "listening" @@ -92,21 +97,21 @@ bool SoftwareSerial::stopListening() // The receive routine called by the interrupt handler // void SoftwareSerial::recv() -{ +{ uint8_t data = 0; // If RX line is high, then we don't see any start bit // so interrupt is probably not for us - if (_inverse_logic ? rx_pin_read() : !rx_pin_read()) + bool rxState = (bool)GPIO_READ_PIN((GPIO_TypeDef *)_receivePortRegister, (HAL_PinsTypeDef)_receiveBitMask); + if (_inverse_logic ? rxState : !rxState) { - GPIO_SET_PIN(GPIO_0, GPIO_PIN_8); - GPIO_CLEAR_PIN(GPIO_0, GPIO_PIN_1); + // Disable further interrupts during reception, this prevents // triggering another interrupt directly after we return, which can // cause problems at higher baudrates. - setRxIntMsk(false); + setRxIntMsk(false); // __always_inline__ // Wait approximately 1/2 of a bit width to "center" the sample - tunedDelay(_rx_delay_centering); + tunedDelay(_rx_delay_centering); // __always_inline__ GPIO_0->OUTPUT_ ^= GPIO_PIN_1; // Read each of the 8 bits @@ -117,7 +122,7 @@ void SoftwareSerial::recv() GPIO_0->OUTPUT_ ^= GPIO_PIN_1; - if (rx_pin_read()) + if (GPIO_READ_PIN((GPIO_TypeDef *)_receivePortRegister, (HAL_PinsTypeDef)_receiveBitMask)) data |= 0x80; } @@ -143,9 +148,9 @@ void SoftwareSerial::recv() // Re-enable interrupts when we're sure to be inside the stop bit setRxIntMsk(true); - GPIO_CLEAR_PIN(GPIO_0, GPIO_PIN_8); + } - HAL_GPIO_ClearInterrupts(); + GPIO_IRQ_CLEAR_ALL(); } uint8_t SoftwareSerial::rx_pin_read() @@ -249,22 +254,33 @@ void SoftwareSerial::begin(long speed) // 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, 750); + // _rx_delay_centering = subtract_cap(bit_delay / 2, 750); // spifi + _rx_delay_centering = subtract_cap(bit_delay / 2, 315); // ram // 1 bit time - _rx_delay_intrabit = subtract_cap(bit_delay, 200); + // _rx_delay_intrabit = subtract_cap(bit_delay, 200); // spifi + _rx_delay_intrabit = subtract_cap(bit_delay, 60); // ram // 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, 950); - + // _rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, 950); // spifi + _rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, 142); // ram + + if (speed==115200) + { + _rx_delay_intrabit = subtract_cap(_rx_delay_intrabit, 10); + _rx_delay_stopbit+=30; + } + // Save start bit polarity - low to high if _inverse_logic _int_mode = _inverse_logic ? (uint8_t)GPIO_INT_MODE_RISING : (uint8_t)GPIO_INT_MODE_FALLING; // Save gpio_mux and gpio_line for on/off interrupts _int_maskMux = digitalPinToGpioIntMux(_receivePin); // gpio mux value _int_maskLine = digitalPinToGpioIntLine(_receivePin); // gpio irq line value + HAL_GPIO_InitInterruptLine((HAL_GPIO_Line_Config) _int_maskMux, (HAL_GPIO_InterruptMode)_int_mode); + GPIO_IRQ_LINE_DISABLE(_int_maskLine); // turn off int HAL_EPIC_MaskEdgeSet(HAL_EPIC_GPIO_IRQ_MASK); // turn on the mask in EPIC } else @@ -281,11 +297,11 @@ void SoftwareSerial::begin(long speed) void SoftwareSerial::setRxIntMsk(bool enable) { if (enable) - // enable line interrupt - HAL_GPIO_InitInterruptLine((HAL_GPIO_Line_Config)_int_maskMux, (HAL_GPIO_InterruptMode)_int_mode); + // enable interrupt line + GPIO_IRQ_LINE_ENABLE(_int_maskLine); else - // disable line interrupt - HAL_GPIO_DeInitInterruptLine((HAL_GPIO_Line)_int_maskLine); + // disable interrupt line + GPIO_IRQ_LINE_DISABLE(_int_maskLine); } void SoftwareSerial::end() diff --git a/platform.txt b/platform.txt index 547e26d..9ee1959 100644 --- a/platform.txt +++ b/platform.txt @@ -18,7 +18,8 @@ compiler.MIK32_Amur.extra_include= "-I{build.core.path}/avr/" "-I{build.core.pat compiler.define="-DMCU_{build.mcu}" "-DF_CPU={build.f_cpu}" "-DARDUINO={runtime.ide.version}" "-DARDUINO_{build.board}" "-DARDUINO_ARCH_{build.arch}" "-DMIK32V2" "-D__ELBEAR_ACE_UNO__" "-D__RISC_V__" "-D__ARDUINO_ARC__" "-D__CPU_ARC__" build.flags.optimize=-Os -build.ldscript=spifi_cpp.ld +# build.ldscript=spifi_cpp.ld +build.ldscript=ram.ld # Compiler flags compiler.warning_flags=-Wunused -Wuninitialized -- 2.43.0 From 04f2e12eccb7020282b5cf16728e0e2e466a2b45 Mon Sep 17 00:00:00 2001 From: klassents Date: Wed, 25 Sep 2024 11:12:01 +0700 Subject: [PATCH 09/31] =?UTF-8?q?=D0=B2=D0=B5=D1=80=D0=BD=D1=83=D0=BB?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B5=D1=80=D1=8B=D0=B2=D0=B0=D0=BD=D0=B8=D0=B9,?= =?UTF-8?q?=20=D0=B2=D1=81=D0=B5=20=D0=B5=D1=89=D0=B5=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/trap_handler.c | 18 +++++++++--------- .../SoftwareSerial/src/SoftwareSerial.cpp | 11 ++++++++--- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/cores/arduino/trap_handler.c b/cores/arduino/trap_handler.c index f756081..5479e39 100644 --- a/cores/arduino/trap_handler.c +++ b/cores/arduino/trap_handler.c @@ -26,17 +26,17 @@ void trap_handler(void) if (EPIC_CHECK_TIMER16_1()) tone_interrupt_handler(); - // // uart0 interrupt - // if (EPIC_CHECK_UART_0()) - // serial_handler_wrapper(0); + // uart0 interrupt + if (EPIC_CHECK_UART_0()) + serial_handler_wrapper(0); - // // uart1 interrupt - // if (EPIC_CHECK_UART_1()) - // serial_handler_wrapper(1); + // uart1 interrupt + if (EPIC_CHECK_UART_1()) + serial_handler_wrapper(1); - // // i2c interrupt - // if (EPIC_CHECK_I2C_1()) - // wire_handler_wrapper(); + // i2c interrupt + if (EPIC_CHECK_I2C_1()) + wire_handler_wrapper(); // reset all interrupts HAL_EPIC_Clear(0xFFFFFFFF); diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index e107e0d..c11f6d9 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -29,12 +29,17 @@ http://arduiniana.org. // --------- TO LL --------- // get ticks from systick #define SYSTICK_GET_TICKS() ((uint64_t)(SCR1_TIMER->MTIMEH)<<32 | SCR1_TIMER->MTIME) + #define GPIO_SET_PIN(GPIO_x, pin) ((GPIO_x)->SET = (pin)) #define GPIO_CLEAR_PIN(GPIO_x, pin) ((GPIO_x)->CLEAR = (pin)) #define GPIO_READ_PIN(GPIO_x, pin) (((GPIO_x)->SET & (pin)) != (uint32_t)GPIO_PIN_LOW ? GPIO_PIN_HIGH : GPIO_PIN_LOW) + #define GPIO_IRQ_LINE_ENABLE(lineMask) ( GPIO_IRQ->ENABLE_SET = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) #define GPIO_IRQ_LINE_DISABLE(lineMask) ( GPIO_IRQ->ENABLE_CLEAR = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) #define GPIO_IRQ_CLEAR_ALL() (GPIO_IRQ->CLEAR = 0b11111111) +#define GLOBAL_IRQ_DISABLE() (clear_csr(mie, MIE_MEIE)) +#define GLOBAL_IRQ_ENABLE() set_csr(mstatus, MSTATUS_MIE); \ + set_csr(mie, MIE_MEIE) // --------- TO LL --------- @@ -288,6 +293,7 @@ void SoftwareSerial::begin(long speed) tunedDelay(_tx_delay); // if we were low this establishes the end + // todo убрать HAL_GPIO_PinConfig(GPIO_0, GPIO_PIN_8, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); HAL_GPIO_PinConfig(GPIO_0, GPIO_PIN_1, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); @@ -318,7 +324,7 @@ size_t SoftwareSerial::write(uint8_t byte) if (_inverse_logic) byte = ~byte; - clear_csr(mie, MIE_MEIE); // turn off interrupts for a clean txmit + GLOBAL_IRQ_DISABLE(); // turn off interrupts for a clean txmit // Write the start bit if (_inverse_logic) @@ -345,8 +351,7 @@ size_t SoftwareSerial::write(uint8_t byte) GPIO_SET_PIN((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask); // enable interrupts - set_csr(mstatus, MSTATUS_MIE); - set_csr(mie, MIE_MEIE); + GLOBAL_IRQ_ENABLE(); tunedDelay(_tx_delay); // wait stop bit -- 2.43.0 From 562c29dbe16d039c78e787f83d2ae41971919884 Mon Sep 17 00:00:00 2001 From: klassents Date: Wed, 25 Sep 2024 11:39:22 +0700 Subject: [PATCH 10/31] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D0=BB=D0=B0=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=83=20?= =?UTF-8?q?=D1=81=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80=D0=B0=D0=BC?= =?UTF-8?q?=D0=B8=20=D0=B2=20LL=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/wiring_LL.h | 29 ++++++++++++ .../SoftwareSerial/src/SoftwareSerial.cpp | 46 +++++-------------- libraries/SoftwareSerial/src/SoftwareSerial.h | 2 - 3 files changed, 41 insertions(+), 36 deletions(-) create mode 100644 cores/arduino/wiring_LL.h diff --git a/cores/arduino/wiring_LL.h b/cores/arduino/wiring_LL.h new file mode 100644 index 0000000..fec4efe --- /dev/null +++ b/cores/arduino/wiring_LL.h @@ -0,0 +1,29 @@ +#ifndef _WIRING_LL_H_ +#define _WIRING_LL_H_ + +#include "mik32_hal_irq.h" +#include "mik32_hal_gpio.h" +#include "mik32_hal_scr1_timer.h" + +// ----------------- Systick ----------------- // +// get ticks from systick +#define SYSTICK_GET_TICKS() ((uint64_t)(SCR1_TIMER->MTIMEH)<<32 | SCR1_TIMER->MTIME) + + +// ----------------- IRQ ----------------- // +#define GLOBAL_IRQ_DISABLE() (clear_csr(mie, MIE_MEIE)) +#define GLOBAL_IRQ_ENABLE() set_csr(mstatus, MSTATUS_MIE); \ + set_csr(mie, MIE_MEIE) + +// ----------------- GPIO ----------------- // +#define GPIO_SET_PIN(GPIO_x, pin) ((GPIO_x)->SET = (pin)) +#define GPIO_CLEAR_PIN(GPIO_x, pin) ((GPIO_x)->CLEAR = (pin)) +#define GPIO_READ_PIN(GPIO_x, pin) (((GPIO_x)->SET & (pin)) != (uint32_t)GPIO_PIN_LOW ? GPIO_PIN_HIGH : GPIO_PIN_LOW) + +// ----------------- GPIO IRQ ----------------- // +#define GPIO_IRQ_LINE_ENABLE(lineMask) ( GPIO_IRQ->ENABLE_SET = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) +#define GPIO_IRQ_LINE_DISABLE(lineMask) ( GPIO_IRQ->ENABLE_CLEAR = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) +#define GPIO_IRQ_CLEAR_ALL() (GPIO_IRQ->CLEAR = 0b11111111) + + +#endif /* _WIRING_LL_H_ */ \ No newline at end of file diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index c11f6d9..455bebb 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -19,30 +19,11 @@ http://arduiniana.org. #include #include -#include "mik32_hal_irq.h" -#include "mik32_hal_gpio.h" -#include "mik32_hal_scr1_timer.h" +#include "wiring_LL.h" #define MAX_SOFT_SERIAL_SPEED 115200 #define MIN_SOFT_SERIAL_SPEED 300 -// --------- TO LL --------- -// get ticks from systick -#define SYSTICK_GET_TICKS() ((uint64_t)(SCR1_TIMER->MTIMEH)<<32 | SCR1_TIMER->MTIME) - -#define GPIO_SET_PIN(GPIO_x, pin) ((GPIO_x)->SET = (pin)) -#define GPIO_CLEAR_PIN(GPIO_x, pin) ((GPIO_x)->CLEAR = (pin)) -#define GPIO_READ_PIN(GPIO_x, pin) (((GPIO_x)->SET & (pin)) != (uint32_t)GPIO_PIN_LOW ? GPIO_PIN_HIGH : GPIO_PIN_LOW) - -#define GPIO_IRQ_LINE_ENABLE(lineMask) ( GPIO_IRQ->ENABLE_SET = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) -#define GPIO_IRQ_LINE_DISABLE(lineMask) ( GPIO_IRQ->ENABLE_CLEAR = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) -#define GPIO_IRQ_CLEAR_ALL() (GPIO_IRQ->CLEAR = 0b11111111) -#define GLOBAL_IRQ_DISABLE() (clear_csr(mie, MIE_MEIE)) -#define GLOBAL_IRQ_ENABLE() set_csr(mstatus, MSTATUS_MIE); \ - set_csr(mie, MIE_MEIE) -// --------- TO LL --------- - - // Statics // static variable is common to all class instances SoftwareSerial *SoftwareSerial::active_object = 0; @@ -279,14 +260,13 @@ void SoftwareSerial::begin(long speed) _rx_delay_stopbit+=30; } - // Save start bit polarity - low to high if _inverse_logic - _int_mode = _inverse_logic ? (uint8_t)GPIO_INT_MODE_RISING : (uint8_t)GPIO_INT_MODE_FALLING; - // Save gpio_mux and gpio_line for on/off interrupts - _int_maskMux = digitalPinToGpioIntMux(_receivePin); // gpio mux value - _int_maskLine = digitalPinToGpioIntLine(_receivePin); // gpio irq line value - HAL_GPIO_InitInterruptLine((HAL_GPIO_Line_Config) _int_maskMux, (HAL_GPIO_InterruptMode)_int_mode); - GPIO_IRQ_LINE_DISABLE(_int_maskLine); // turn off int - HAL_EPIC_MaskEdgeSet(HAL_EPIC_GPIO_IRQ_MASK); // turn on the mask in EPIC + // 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"); @@ -302,12 +282,10 @@ void SoftwareSerial::begin(long speed) void SoftwareSerial::setRxIntMsk(bool enable) { - if (enable) - // enable interrupt line - GPIO_IRQ_LINE_ENABLE(_int_maskLine); - else - // disable interrupt line - GPIO_IRQ_LINE_DISABLE(_int_maskLine); + if (enable) + GPIO_IRQ_LINE_ENABLE(_int_maskLine); + else + GPIO_IRQ_LINE_DISABLE(_int_maskLine); } void SoftwareSerial::end() diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.h b/libraries/SoftwareSerial/src/SoftwareSerial.h index 4a23ee4..66d798a 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.h +++ b/libraries/SoftwareSerial/src/SoftwareSerial.h @@ -46,9 +46,7 @@ private: uint8_t _transmitPin; uint16_t _transmitBitMask; uint32_t _transmitPortRegister; - uint8_t _int_maskMux; uint8_t _int_maskLine; - uint8_t _int_mode; // must never be 0! uint32_t _rx_delay_centering; -- 2.43.0 From 85d81a54b1a5ac5f5a4e5397495b13073ed91abb Mon Sep 17 00:00:00 2001 From: klassents Date: Wed, 25 Sep 2024 16:46:57 +0700 Subject: [PATCH 11/31] =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=8B=D1=82=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81=D1=82=D0=B8?= =?UTF-8?q?=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=83=20=D1=81=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B5=D1=80=D1=8B=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=D0=BC=D0=B8=20?= =?UTF-8?q?=D0=B2=20hal.=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B0=D0=B5?= =?UTF-8?q?=D0=BC=20=D0=B3=D0=B4=D0=B5-=D1=82=D0=BE,=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D0=B4=D0=BE=D1=85=D0=BE=D0=B4=D1=8F=20=D0=B4=D0=BE=20trap=5Fha?= =?UTF-8?q?ndler(),=20=D1=85=D0=BE=D1=82=D1=8F=20=D0=BF=D0=BE=20map=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D1=83=20=D0=B2=D1=81=D0=B5=20=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3=D0=B8=D1=87=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/board.cpp | 6 ++++ .../mik32/shared/ldscripts/spifi_cpp.ld | 29 +++++++++++++++---- cores/arduino/mik32/shared/runtime/crt0.S | 20 ++++++++----- cores/arduino/trap_handler.c | 10 +++---- libraries/SoftwareSerial/src/SoftwareSerial.h | 4 +-- platform.txt | 3 +- 6 files changed, 49 insertions(+), 23 deletions(-) diff --git a/cores/arduino/board.cpp b/cores/arduino/board.cpp index bf1dc01..a8b8703 100644 --- a/cores/arduino/board.cpp +++ b/cores/arduino/board.cpp @@ -1,11 +1,15 @@ #include "board.h" #include "mik32_hal_pcc.h" +#include "mik32_hal_irq.h" #include "Arduino.h" // --------------------- init --------------------- // // called before setup() void pre_init(void) { + // set irq vector to ram region + write_csr(mtvec, 0x020000c0); + HAL_Init(); // gpio clock @@ -23,6 +27,8 @@ void post_init(void) { // enable global interrupts by default interrupts(); + + ((GPIO_0)->SET = (GPIO_PIN_0)); } // --------------------- other --------------------- // diff --git a/cores/arduino/mik32/shared/ldscripts/spifi_cpp.ld b/cores/arduino/mik32/shared/ldscripts/spifi_cpp.ld index 18bcb89..20ded45 100644 --- a/cores/arduino/mik32/shared/ldscripts/spifi_cpp.ld +++ b/cores/arduino/mik32/shared/ldscripts/spifi_cpp.ld @@ -21,8 +21,8 @@ SECTIONS { *crt0.S.o(.text .text.*) *(.text.smallsysteminit) *(.text.SmallSystemInit) - . = ORIGIN(rom) + 0xC0; - KEEP(*crt0.S.o(.trap_text)) + /*. = ORIGIN(rom) + 0xC0;*/ + /*KEEP(*crt0.S.o(.trap_text))*/ *(.text) *(.text.*) @@ -50,16 +50,17 @@ SECTIONS { AT( __TEXT_END__ ) { PROVIDE(__DATA_START__ = .); _gp = .; + . = ORIGIN(ram) + 0xC0; + KEEP(*crt0.S.o(.trap_text)) *(.srodata.cst16) *(.srodata.cst8) *(.srodata.cst4) *(.srodata.cst2) *(.srodata*) *(.sdata .sdata.* .sdata* .sdata*.* .gnu.linkonce.s.*) *(.data .data.*) - *(.ramfunc) . = ALIGN(CL_SIZE); - } >ram - + PROVIDE(__DATA_END__ = .); + } >ram + __DATA_IMAGE_START__ = LOADADDR(.data); __DATA_IMAGE_END__ = LOADADDR(.data) + SIZEOF(.data); - ASSERT(__DATA_IMAGE_END__ < ORIGIN(rom) + LENGTH(rom), "Data image overflows rom section") /* thread-local data segment */ .tdata : { @@ -85,10 +86,26 @@ SECTIONS { .bss : { *(.bss .bss.*) + *(COMMON) . = ALIGN(CL_SIZE); PROVIDE(__BSS_END__ = .); } >ram + /* Code intended to be copied to REGION_RAM before execution */ + .ram_text : + AT( ALIGN(__DATA_IMAGE_END__, CL_SIZE) ) { + PROVIDE(__RAM_TEXT_START__ = .); + *(.ram_text) + . = ALIGN(CL_SIZE); + PROVIDE(__RAM_TEXT_END__ = .); + } > ram + + __RAM_TEXT_IMAGE_START__ = LOADADDR(.ram_text); + __RAM_TEXT_IMAGE_END__ = LOADADDR(.ram_text) + SIZEOF(.ram_text); + ASSERT(__RAM_TEXT_IMAGE_END__ < ORIGIN(rom) + LENGTH(rom), "rom segment overflows") + + ASSERT(__RAM_TEXT_END__ < ORIGIN(ram) + LENGTH(ram) - STACK_SIZE - HEAP_SIZE, "REGION_RAM section overflows") + .heap : { PROVIDE(__heap_start = .); . += HEAP_SIZE; diff --git a/cores/arduino/mik32/shared/runtime/crt0.S b/cores/arduino/mik32/shared/runtime/crt0.S index e058b76..e4185cd 100644 --- a/cores/arduino/mik32/shared/runtime/crt0.S +++ b/cores/arduino/mik32/shared/runtime/crt0.S @@ -62,6 +62,11 @@ memset_2: _start: + li t0, 128000 + start_loop_delay: + nop + addi t0, t0, -1 + bnez t0, start_loop_delay # Init stack and global pointer # la_abs sp, __C_STACK_TOP__ @@ -73,20 +78,19 @@ _start: la_abs a2, __DATA_IMAGE_END__ la_abs a3, __DATA_START__ memcpy a1, a2, a3, t0 + + # Init ramfunc + # + la_abs a1, __RAM_TEXT_IMAGE_START__ + la_abs a2, __RAM_TEXT_IMAGE_END__ + la_abs a3, __RAM_TEXT_START__ + memcpy a1, a2, a3, t0 # Clear bss # la_abs a1, __BSS_START__ la_abs a2, __BSS_END__ memset a1, a2, zero - - #ifdef MIK32V0 - # Enable pad_config clocking - # - li t0, (1 << 3) - li t1, 0x50014 - sw t0, (t1) - #endif jalr_abs ra, SmallSystemInit jalr_abs ra, SystemInit diff --git a/cores/arduino/trap_handler.c b/cores/arduino/trap_handler.c index 5479e39..4495da8 100644 --- a/cores/arduino/trap_handler.c +++ b/cores/arduino/trap_handler.c @@ -14,13 +14,13 @@ void __attribute__((weak)) wire_handler_wrapper(void) extern void softSerial_handler_wrapper(void); // ---------------------------------------------- // -void trap_handler(void) +void __attribute__((noinline, section(".ram_text"))) trap_handler(void) { - ((GPIO_0)->SET = (GPIO_PIN_8)); + ((GPIO_0)->CLEAR = (GPIO_PIN_0)); // gpio interrupt if (EPIC_CHECK_GPIO_IRQ()) - // gpio_interrupts_handler(); - softSerial_handler_wrapper(); + gpio_interrupts_handler(); + // softSerial_handler_wrapper(); // tone timer interrupt if (EPIC_CHECK_TIMER16_1()) @@ -40,5 +40,5 @@ void trap_handler(void) // reset all interrupts HAL_EPIC_Clear(0xFFFFFFFF); - ((GPIO_0)->CLEAR = (GPIO_PIN_8)); + ((GPIO_0)->SET = (GPIO_PIN_0)); } diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.h b/libraries/SoftwareSerial/src/SoftwareSerial.h index 66d798a..a7584de 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.h +++ b/libraries/SoftwareSerial/src/SoftwareSerial.h @@ -74,7 +74,7 @@ private: static uint32_t subtract_cap(uint32_t num, uint16_t sub); // private static method for timing - static void tunedDelay(uint32_t delayTicks) __attribute__((noinline, section(".ramfunc"))); + static void tunedDelay(uint32_t delayTicks) __attribute__((noinline, section(".ram_text"))); public: // public methods @@ -94,7 +94,7 @@ public: } int peek(); - virtual size_t write(uint8_t byte) __attribute__((noinline, section(".ramfunc"))); + virtual size_t write(uint8_t byte) __attribute__((noinline, section(".ram_text"))); virtual int read(); virtual int available(); virtual void flush(); diff --git a/platform.txt b/platform.txt index 9ee1959..547e26d 100644 --- a/platform.txt +++ b/platform.txt @@ -18,8 +18,7 @@ compiler.MIK32_Amur.extra_include= "-I{build.core.path}/avr/" "-I{build.core.pat compiler.define="-DMCU_{build.mcu}" "-DF_CPU={build.f_cpu}" "-DARDUINO={runtime.ide.version}" "-DARDUINO_{build.board}" "-DARDUINO_ARCH_{build.arch}" "-DMIK32V2" "-D__ELBEAR_ACE_UNO__" "-D__RISC_V__" "-D__ARDUINO_ARC__" "-D__CPU_ARC__" build.flags.optimize=-Os -# build.ldscript=spifi_cpp.ld -build.ldscript=ram.ld +build.ldscript=spifi_cpp.ld # Compiler flags compiler.warning_flags=-Wunused -Wuninitialized -- 2.43.0 From d228bc7ff9f56ea4e280f1d6900d1a49ebe28fea Mon Sep 17 00:00:00 2001 From: klassents Date: Thu, 26 Sep 2024 11:24:08 +0700 Subject: [PATCH 12/31] =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D0=B0=D0=BC=D0=BE?= =?UTF-8?q?=D0=BC=20=D0=B4=D0=B5=D0=BB=D0=B5=20=D0=BA=D0=BE=D0=B4=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82,=20=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=BE=20=D0=B1=D1=8B=D0=BB=D0=BE=20=D0=B2=20=D0=B1=D1=83?= =?UTF-8?q?=D1=82=D0=BB=D0=BE=D0=B0=D0=B4=D0=B5=D1=80=D0=B5.=20=D0=B2?= =?UTF-8?q?=D1=81=D0=B5=20=D1=81=D0=B2=D1=8F=D0=B7=D0=B0=D0=BD=D0=BD=D0=BE?= =?UTF-8?q?=D0=B5=20=D1=81=20=D0=BF=D1=80=D0=B5=D1=80=D1=8B=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=D0=BC=D0=B8=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D1=8C=20=D0=B2=20=D1=80=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/board.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cores/arduino/board.cpp b/cores/arduino/board.cpp index a8b8703..4b06378 100644 --- a/cores/arduino/board.cpp +++ b/cores/arduino/board.cpp @@ -8,7 +8,7 @@ void pre_init(void) { // set irq vector to ram region - write_csr(mtvec, 0x020000c0); + write_csr(mtvec, 0x02000000); HAL_Init(); @@ -35,8 +35,10 @@ void post_init(void) // print text if Serial is enabled extern "C" void ErrorMsgHandler(const char * msg) { +#ifdef HardwareSerial_h if(Serial) Serial.println(msg); +#endif } -- 2.43.0 From 8f2f29987b40d938fe7d2c8b9a1c95117f973dc4 Mon Sep 17 00:00:00 2001 From: klassents Date: Tue, 8 Oct 2024 13:15:45 +0700 Subject: [PATCH 13/31] =?UTF-8?q?=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20bootloader,=20=D0=B2=20SS=20=D0=B2=D1=81=D0=B5?= =?UTF-8?q?=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8,=20=D1=81=D0=B2?= =?UTF-8?q?=D1=8F=D0=B7=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=81=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B5=D1=80=D1=8B=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=D0=BC?= =?UTF-8?q?=D0=B8,=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=B2=20ram.=20=D0=BD=D0=B0=20115200=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82,=20=D0=BD=D0=BE=20=D0=B2=20?= =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=87=D0=B8=D0=BA?= =?UTF-8?q?=D0=B5=20=D0=BD=D1=83=D0=B6=D0=BD=D0=B0=D1=8F=20=D1=84=D1=83?= =?UTF-8?q?=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20=D0=B2=D1=8B=D0=B7=D1=8B=D0=B2?= =?UTF-8?q?=D0=B0=D0=B5=D1=82=D1=81=D1=8F=20=D0=BD=D0=B0=D0=BF=D1=80=D1=8F?= =?UTF-8?q?=D0=BC=D1=83=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bootloaders/ace-uno/bootloader.hex | 446 +++++++++--------- cores/arduino/trap_handler.c | 4 +- .../SoftwareSerial/src/SoftwareSerial.cpp | 47 +- libraries/SoftwareSerial/src/SoftwareSerial.h | 4 +- 4 files changed, 252 insertions(+), 249 deletions(-) diff --git a/bootloaders/ace-uno/bootloader.hex b/bootloaders/ace-uno/bootloader.hex index f0462b5..3e826ca 100644 --- a/bootloaders/ace-uno/bootloader.hex +++ b/bootloaders/ace-uno/bootloader.hex @@ -1,15 +1,15 @@ :020000040100F9 :10000000FD62938202400100FD12E39E02FE374131 :10001000000213010100B701000293810100B7152E -:100020000001938505F137160001130606F4B706A3 +:100020000001938505FF3716000113060602B70687 :1000300000029386060039A083A2050023A0560083 -:1000400091059106E3EAC5FEB7150001938505F415 -:1000500037160001130606F4B7060002938606263B +:1000400091059106E3EAC5FEB71500019385050207 +:100050003716000113060602B7060002938606262D :1000600039A083A2050023A0560091059106E3EA7A :10007000C5FEB70500029385050337060002130687 :10008000062621A023A005009105E3EDC5FEB700DB -:100090000001E780C00AB7000001E780C00AB7008E -:1000A0000001E780007273005010F5BF82800000ED +:100090000001E780C00AB7000001E780C00AB7107E +:1000A0000001E780808473005010F5BF828000005B :1000B0000000000000000000000000000000000040 :1000C0006F004000197106C20AC40EC612C816CAD3 :1000D0001ACC1ECE22D026D22AD42ED632D836DA48 @@ -32,216 +32,230 @@ :1001E00027009702000096968682E78086FA96801E :1001F000C1171D8F3E96E374C3F8A5B7B707050076 :100200000947D8CFB7170500938707C0984385667D -:1002100093860640558F98C3B71708009387074009 -:1002200023A0070023A2070023A407001307A008A8 -:10023000D8C77D57D8CF354798C3D84F9356570165 -:100240008D8AE5DE8280B71708009387074023A0D8 -:10025000070023A2070023A4070023A607007D5759 -:10026000D8CF23A40702B7170500938707C0984388 -:10027000F1769386F63F758F98C3B7070500D84F80 -:1002800023AE07008280B71708009387074088D7FE -:10029000D84F137707046DDF828037070002B71548 -:1002A000080037460F0023200704814781469385C5 -:1002B000054013061624C84D137505020DC991CECD -:1002C0002320F70437470F00130707246397E7003D -:1002D000B707000205472383E700B7170800938795 -:1002E0000740C8534205418182808546E9B78507AA -:1002F000E39DC7FEFDD2B7470F0093870724232055 -:10030000F704F9B74111B707000222C413870700A9 -:1003100006C6834667000547138407006390E6021C -:100320001305000F8D37B717080093870740D84F84 -:10033000218B09C7D84F13678700D8CF2303040048 -:10034000B240224441018280411106C622C426C225 -:10035000AA84EF009013E1689388086A01488147F6 -:10036000014781460146B70520C726853794980086 -:10037000EF000077130414687D1419E40D45B240B2 -:10038000224492444101828085452685EF00501128 -:1003900005897DF10145E5B7411122C4370400020A -:1003A000930704008C43B70700804AC0BE95B70787 -:1003B000000223ACB70206C626C293974501130478 -:1003C00004003709000289E713058900EF00701A5D -:1003D0000C40B70400029386440413060010130572 -:1003E0008900EF0050141C40370700028356470372 -:1003F000938707101CC013060010B68763F4C6006D -:1004000093070010138444041305F400938707F046 -:100410001306100F814513040410231AF7021D3B25 -:10042000A285138544043D46EF00F0182320040004 -:1004300023220400232404002316040023070400BD -:10044000B240224492440249410182805D71130608 -:1004500080028145280886C6F539BD47230CF10086 -:100460008947230EF1003ED2E177938707082C08D5 -:100470000A85231AF1021923B640616182805D71F9 -:10048000A2C4370400021305840086C6A6C2652BE9 -:1004900013058400E525894513058400EF0050000D -:1004A0009377250085E3AA84854513058400EF0032 -:1004B000207F13E62400AA851376F60F1305840027 -:1004C000EF007001E1689388086A0148814701479D -:1004D00081460146B7052038130584001125E168DF -:1004E00038009388086A1308000285468147014650 -:1004F000B70599EB1305840023040100D523B71534 -:10050000000151469385C5EF6800EF00D00A9304BF -:1005100084009C406C0051463ED085473ED2681016 -:10052000C1673ED4EF003009B70607009C42370789 -:10053000F1FF7D17F98F08109CC2812BB64026442D -:10054000964461618280411106C6F5390D3FB707B7 -:10055000008073905730B70000808290B240410114 -:100560008280411106C622C426C24AC08347350094 -:1005700005476383E70811472A846389E7008DCF25 -:10058000B24022449244024941018280B7070002EE -:1005900083D7470391C30935834744000347540079 -:1005A000E2074207BA9737070002232EF702370700 -:1005B00000022320F700E9B73709000283544903FA -:1005C0000346050093054500370500021305450461 -:1005D0002695EF00407E83470400BE94C204C1808C -:1005E000231A99029307F00FE3FC97F82244B240D4 -:1005F0009244024941014DB33D457131B7070002B4 -:1006000083D7470391C3493B2244B24092440249F5 -:10061000410115BF3707000293070700B705000225 -:1006200003DE470083A705043716080037450F008F -:1006300037480F00B708000201438146130707003F -:10064000130606401305152413080824938848153B -:1006500063EEC6016304030023A0F504834767002B -:10066000A9E73705000213054515E5BD81470323BA -:10067000C60113730302631F03008507E399A7FEF6 -:10068000B7470F009387072423A0F504854723036A -:10069000F7008280E38607FF832E46023383D8006B -:1006A00085062300D301054365B78280011122CC62 -:1006B0003704000226CA4AC84EC652C406CE1304E6 -:1006C0000400930400061309E00FB7090002130A9F -:1006D0000003E136834764001375F50FA303A400FC -:1006E00099C30D31FDB7630F950063042503E31231 -:1006F00045FF3D45493E553605052312A4003D45BD -:100700005936C1BF3D454136313783476400F1FB5F -:10071000FDB7138589000D397DD11305000FCDB7C5 -:10072000011106CE22CC1D333704000213058400CC -:10073000192E130584009921E1689388086A0148FD -:100740008147780085460146B705D9EB130584003B -:10075000230601007126E168814701478146014671 -:10076000B70538FF9388086A0148130584008D2E69 -:10077000713437070002B715080037460F002320F1 -:100780000704814781469385054013061624C84D0A -:100790001375050211CD85CA2320F70437470F00D2 -:1007A000130707246391E702793B01A08546C5B78B -:1007B0008507E39DC7FE81CAB7470F0093870724CB -:1007C0002320F704D5B7DD350547AA876305E50281 -:1007D00009476300E506054591EBB7060600DC4ACC -:1007E0007D771307F73FF98FDCCA014582807D17BB -:1007F00019EB0D4582809306004037A707001307C9 -:100800000712B7050500905D7D8E75D2370606008C -:100810005C4A7D771307F73FF98FD58F41115CCA8A -:1008200002C613073006B2476359F700014541017C -:100830008280856693860680C9B7B24785073EC623 -:10084000DDB791476307F50263EAA7008547630AAE -:10085000F50489476309F50405458280A147E31D36 -:10086000F5FE0947094501A8FD1781EFC8D20D45DE -:10087000828005470D45B7A7070093870712B70683 -:100880000500905E798E6DD28A05C98D4111CCD25A -:1008900002C613073006B247635AF700014541010B -:1008A00082801147C9BF21470145F1B7B24785078B -:1008B0003EC6D5B70547AA876305E5020947630227 -:1008C000E506054591EBB70606009C4A7D771307C0 -:1008D000F73FF98F9CCA014582807D1719EB0D45C2 -:1008E00082809306004037A7070013070712B70559 -:1008F0000500905D7D8E75D2370706001C4B7D7616 -:100900001306F63FF18FD58F1CCB85471CCF4111C5 -:1009100002C613073006B2476359F700014541018B -:1009200082808566938606807DBFB24785073EC676 -:10093000DDB711C98547630DF50205458280FD17B6 -:1009400091EB0D4582800946B7A7070093870712F0 -:10095000B7060500985E718F7DD34111C8D602C6D7 -:1009600013073006B2476357F70001454101828003 -:100970000546D9BFB24785073EC6EDB7011126CA65 -:10098000B7040600DC4806CE22CC4AC84EC652C484 -:1009900056C2F19BDCC89C482A89C845F19B9CC87B -:1009A00083C7C5012E848A07DCC883C7D5018A079F -:1009B0009CC8193D0C44AA8A03454400593518487F -:1009C000B70705002A8A98C358480850D8C3184C5E -:1009D00098C7CD35AA894850A93F834704002A8784 -:1009E00093F6170089E6D44893E62600D4C893F618 -:1009F000270099E637060600544A93E6160054CAC3 -:100A000093F6470099E637060600144A93E6260057 -:100A100014CAA18B99E7B70606009C4A93E7170012 -:100A20009CCAF24062442320590123224901232415 -:100A300039012326E900D244B249224A924A4A8522 -:100A4000424905618280011106CE22CC02C402C651 -:100A50002147B707050037550800D8C705448D471B -:100A60008A85130505803EC022C2292A3755080011 -:100A70009307C0038A851305058022C222C43EC0A5 -:100A80001122F240624405618280411122C406C6EF -:100A90002A84553F18405C4F93E707015CCF1C4404 -:100AA0001CCB5C4085CB1C43B7061000D58F1CC304 -:100AB000144C5C48B240D606CE07D58F834604015D -:100AC000C206D58F8346C4012244E206D58F1CCFCF -:100AD000410182801C43B706F0FFFD16F58FC1BFB0 -:100AE000032305002A8E0325C30113650502232E67 -:100AF000A3002324C3001396260149824D8E23268A -:100B000003012322C300139605016354060299C210 -:100B10000545B1CB01476346D700639C08020D45EC -:100B200082803386E700034606000507230AC300D8 -:100B3000DDB799C2054505CB8147E3D0D7FE032633 -:100B40000E00034546013306F70085072300A60083 -:100B5000EDB783270E00FD18DC4F93F70702D5DFB2 -:100B600011656D8D11E18280B707070083C74701CA -:100B700013F585001D8D3335A00082801C4141474F -:100B8000D8CF8280B7470800938707402A8863043C -:100B9000F508B7570800938707806304F50A3747BD -:100BA0000800630DE50A05458280331E1F01337678 -:100BB000DE0129C683A345008843139318003396AA -:100BC0006F001346F6FF13F43300718D3314640085 -:100BD000418D88C3638B5302638C0302084303AEC9 -:100BE000C500718D331E6E003365C50108C3884290 -:100BF000698E884533156500498E90C2850833D5C6 -:100C00001E0145F53244410182802326C801F9B70F -:100C10002324C801E1B7B716050037170500B71739 -:100C20000500938646C1130707C19387C7C083AEEB -:100C300005008148054F8D4F914233D51E0105EDCA -:100C40008280B716050037170500B7170500938691 -:100C500006C21307C7C1938787C1D1BFB716050066 -:100C600037170500B7170500938686C0130747C0DE -:100C7000938707C06DBF331E1F013376DE0119E273 -:100C8000850865BF411122C635B7E1689388086AB7 -:100C900001488147014781460146B705200689B5CD -:100CA000011106CEA307010089476393F502B7053A -:100CB0002035E1681307F1009388086A01488147ED -:100CC00085460146313DF2400345F10005618280D1 -:100CD000B7052005F9BF011106CE22CC26CA23068E -:100CE000B100AA84A306C1004D37E1689388086A61 -:100CF00001487C00014789460146B78520012685C9 -:100D00000964F93B130414717D1419E40D45F24094 -:100D10006244D24405618280854526855137058924 -:100D200065F50145EDB7011106CE22CC26CA2E8409 -:100D30004AC8AA84328936C6893FB247E16822860A -:100D40009388086A01480147CA86B78580022685CC -:100D5000616479331304146A7D1411C485452685B2 -:100D60008137058975F9F2406244D24442490561F0 -:100D70008280011106CE22CC26CA2EC6AA84313723 -:100D80003246E1689388086A0148814701478146F5 -:100D9000B705802026856164A1331304146A7D148D -:100DA00011C485452685ED3D058975F9F2406244FB -:100DB000D24405618280B3C7A5008D8BB308C500FE -:100DC000B1E78D4763F4C704937735002A87B9EB01 -:100DD00013F6C8FFB306E6409307000263C8D706C0 -:100DE000AE86BA876371C70203A806009107910611 -:100DF00023AE07FFE3EAC7FE9307F6FF998FF19B47 -:100E000091073E97BE956366170182802A87637EAD -:100E1000150383C7050005078505A30FF7FEE39AB1 -:100E2000E8FE828083C60500050793773700A30F8D -:100E3000D7FE8505D1DF83C6050005079377370008 -:100E4000A30FD7FE8505F9FF61B78280411122C645 -:100E50001304000283A3050083A2450083AF85002D -:100E600003AFC50083AE050103AE450103A38501B1 -:100E700003A8C501945113074702B307E640232E88 -:100E800077FC232057FE2322F7FF2324E7FF2326A6 -:100E9000D7FF2328C7FF232A67FE232C07FF232E13 -:100EA000D7FE93854502E347F4FAAE86BA876371AD -:100EB000C70203A806009107910623AE07FFE3EAE5 -:100EC000C7FE9307F6FF998FF19B91073E97BE955A -:100ED0006365170132444101828083C7050005071D -:100EE0008505A30FF7FEE387E8FE83C70500050726 -:100EF0008505A30FF7FEE392E8FEE9BF200000009E -:100F0000010000000300000006000000EB000000EC -:100F10000000008000010000000007000000000049 -:100F200000000000000000000000000000000000C1 -:100F300000000000000000000000000000000000B1 +:1002100093860640558F98C398471367074098C741 +:10022000B71708009387074023A0070023A2070001 +:1002300023A407001307A008D8C77D57D8CF354798 +:1002400098C3D84F935657018D8AE5DE8280B71741 +:1002500008009387074023A0070023A2070023A4D8 +:10026000070023A607007D57D8CF23A40702B7179E +:100270000500938707C09843F1769386F63F758F04 +:1002800098C398471377F7BF98C7B7070500094782 +:1002900098D38280B71708009387074088D7D84F34 +:1002A000137707046DDF82803707000203234704BA +:1002B000B706000237150800B7450F0023A406044F +:1002C0008147014613050540938515240328C50180 +:1002D000B308F30013780802630C080205C2232256 +:1002E000170537470F0023A4F60413070724639765 +:1002F000E700B707000205472383E700B7170800A8 +:1003000093870740C85342054181828005467DBFDF +:100310008507E39DB7FE7DD2B7470F00938707247B +:1003200023A4F60423221705E9B7411106C622C407 +:1003300026C2AA84EF00B022E1689388086A0148C7 +:100340008147014781460146B70520C72685379476 +:100350009800EF003006130414687D1419E40D456D +:10036000B240224492444101828085452685EF00B7 +:10037000702005897DF10145E5B7411122C437049C +:100380000002930704008C43B70700804AC0BE9563 +:10039000B707000223AEB70206C626C293974501EF +:1003A000130404003709000289E713058900EF00F0 +:1003B00090290C40B70400029386C4041306001071 +:1003C00013058900EF0070231C4037070002835695 +:1003D0008703938707101CC013060010B68763F4C9 +:1003E000C600930700101384C4041305F400938718 +:1003F00007F01306100F814513040410231CF702A5 +:10040000913BA2851385C4043D46EF0010282320AC +:1004100004002322040023240400231604002307DD +:100420000400B240224492440249410182805D713D +:10043000130680028145280886C6293BBD47230C48 +:10044000F1008947230EF1003ED2E1779387070838 +:100450002C080A85231AF102E52BB64061618280DF +:100460005D71A2C4370400021305840086C6A6C2CB +:10047000CAC0652513058400EF00807D85451305FE +:100480008400EF00300F8D47814463E3A700AA8406 +:10049000894513058400EF00F00D937725002A8627 +:1004A00089E7136625001376F60FA68513058400E9 +:1004B000EF00B00FE1689388086A0148814701475F +:1004C00081460146B705203813058400E525E1681B +:1004D00038009388086A1308000285468147014660 +:1004E000B70599EB1305840023040100E125B71536 +:1004F00000015146938505FD6800EF0010191309AE +:100500008400832709006C0051463ED085473ED2C7 +:100510006810C1673ED4EF005017B70607009C4231 +:100520003707F1FF7D17F98F08109CC20D2DB640DB +:1005300026449644064961618280411106C6013B0A +:100540000537B707008073905730B700008082905E +:10055000B2404101828041113707000222C406C621 +:100560009307070083C76700854613040700638E5F +:10057000D70009476388E70023030400B240224400 +:10058000410182801305000F3133B71708009387AC +:100590000740D84F218B09C7D84F13678700D8CFA2 +:1005A000693FD9BF411106C622C426C24AC083474B +:1005B000350005476383E70811472A846389E7000C +:1005C0008DCFB24022449244024941018280B70754 +:1005D000000283D7870391C34D3383474400034709 +:1005E0005400E2074207BA97370700022320F704B6 +:1005F000370700022320F700E9B7370900028354C8 +:1006000089030346050093054500370500021305DD +:10061000C5042695EF00700783470400BE94C2040A +:10062000C180231C99029307F00FE3FC97F8224442 +:10063000B24092440249410189B33D45A139B7070F +:10064000000283D7870391C30D3B2244B2409244FA +:1006500002494101DDB53707000241119306070049 +:1006600003DF460026C4B704000283A2840422C626 +:100670003715080037040002B7480F0037430F0052 +:10068000370E00024AC28147232A04021309FFFFE2 +:10069000814E814681458143014801461307070089 +:1006A000130505409388182413030324130ECE1555 +:1006B000636FE60363850E0009462303C70089C6FE +:1006C000B7060002A388F60299C1232A74026304C4 +:1006D000080023A4540483476700A9E73244A244D6 +:1006E0001249370500021305C515410165BD814258 +:1006F0000328C50113780802631B08028502E399E9 +:1007000012FF89C6B7060002A388F60299C1232A00 +:100710007402B7470F009387072423A4F404854786 +:100720002303F7003244A244124941018280E38A44 +:1007300062FC03284502937FF80F637D260113783E +:10074000F80FC29385453308CE002300F801050653 +:100750000548B9BFB307704093F7F70F6394FF00E4 +:100760008546D5B7854EEDBF011122CC4EC652C489 +:1007700037040002B7490F00371ADCBA26CA4AC844 +:1007800056C25AC006CE13040400370900029389EA +:10079000F923930A0003B7040002391A370B000249 +:1007A000213683274904A303A40063F3F9007133BE +:1007B0008347640099C34533E5B783467400638873 +:1007C0005605930700066381F60603C704038D47A9 +:1007D000998F8E07B357FA0093F7F70F6393F606D6 +:1007E0009307170093F7F70F2388F4021147639DCF +:1007F000E7003D45453413058B00053E29C113052F +:10080000000F493C238804022322090451BF3D45BF +:100810002322090423880402B53C793405052312F8 +:10082000A4003D45853CADBF3D45232209042388F6 +:1008300004028D340D3583476400B5FFDDB73D45B7 +:10084000C9B723880402A9BF011106CE22CCC53640 +:100850003704000213058400C92413058400CD2E3B +:10086000E1689388086A0148814778008546014617 +:10087000B705D9EB1305840023060100252EE16896 +:100880009388086A01488147014781460146B705B8 +:1008900038FF13058400392E9532F9350547AA87AC +:1008A0006305E50209476300E506054591EBB706D8 +:1008B0000600DC4A7D771307F73FF98FDCCA014554 +:1008C00082807D1719EB0D4582809306004037A783 +:1008D000070013070712B7050500905D7D8E75D2DE +:1008E000370606005C4A7D771307F73FF98FD58FEF +:1008F00041115CCA02C613073006B2476359F700BC +:10090000014541018280856693860680C9B7B2475A +:1009100085073EC6DDB791476307F50263EAA70086 +:100920008547630AF50489476309F5040545828014 +:10093000A147E31DF5FE0947094501A8FD1781EF11 +:10094000C8D20D45828005470D45B7A7070093879C +:100950000712B7060500905E798E6DD28A05C98DA3 +:100960004111CCD202C613073006B247635AF700D2 +:100970000145410182801147C9BF21470145F1B7B7 +:10098000B24785073EC6D5B70547AA876305E50286 +:1009900009476302E506054591EBB70606009C4A48 +:1009A0007D771307F73FF98F9CCA014582807D1739 +:1009B00019EB0D4582809306004037A70700130707 +:1009C0000712B7050500905D7D8E75D237070600CA +:1009D0001C4B7D761306F63FF18FD58F1CCB8547D8 +:1009E0001CCF411102C613073006B2476359F70006 +:1009F0000145410182808566938606807DBFB247AE +:100A000085073EC6DDB711C98547630DF50205456B +:100A10008280FD1791EB0D4582800946B7A707003C +:100A200093870712B7060500985E718F7DD3411139 +:100A3000C8D602C613073006B2476357F700014510 +:100A4000410182800546D9BFB24785073EC6EDB752 +:100A5000011126CAB7040600DC4806CE22CC4AC8DB +:100A60004EC652C456C2F19BDCC89C482A89C84570 +:100A7000F19B9CC883C7C5012E848A07DCC883C745 +:100A8000D5018A079CC8193D0C44AA8A0345440035 +:100A900059351848B70705002A8A98C3584808509E +:100AA000D8C3184C98C7CD35AA894850A93F834769 +:100AB00004002A8793F6170089E6D44893E62600B7 +:100AC000D4C893F6270099E637060600544A93E601 +:100AD000160054CA93F6470099E637060600144AF2 +:100AE00093E6260014CAA18B99E7B70606009C4A34 +:100AF00093E717009CCAF240624423205901232245 +:100B00004901232439012326E900D244B249224A6B +:100B1000924A4A85424905618280011106CE22CC63 +:100B200002C402C62147B707050037550800D8C7D9 +:100B300005448D478A85130505803EC022C2292AB7 +:100B4000375508009307C0038A851305058022C224 +:100B500022C43EC01122F2406244056182804111EC +:100B600022C406C62A84553F18405C4F93E707010C +:100B70005CCF1C441CCB5C4085CB1C43B7061000EB +:100B8000D58F1CC3144C5C48B240D606CE07D58F17 +:100B900083460401C206D58F8346C4012244E2067F +:100BA000D58F1CCF410182801C43B706F0FFFD1694 +:100BB000F58FC1BF032305002A8E0325C3011365EA +:100BC0000502232EA3002324C30013962601498285 +:100BD0004D8E232603012322C3001396050163547F +:100BE000060299C20545B1CB01476346D700639C15 +:100BF00008020D4582803386E7000346060005079C +:100C0000230AC300DDB799C2054505CB8147E3D070 +:100C1000D7FE03260E00034546013306F70085077D +:100C20002300A600EDB783270E00FD18DC4F93F7D5 +:100C30000702D5DF11656D8D11E18280B7070700CE +:100C400083C7470113F585001D8D3335A0008280D1 +:100C50001C414147D8CF8280B7470800938707409F +:100C60002A886304F508B757080093870780630450 +:100C7000F50A37470800630DE50A05458280331EF3 +:100C80001F013376DE0129C683A3450088431393F1 +:100C9000180033966F001346F6FF13F43300718D7E +:100CA00033146400418D88C3638B5302638C030249 +:100CB000084303AEC500718D331E6E003365C50158 +:100CC00008C38842698E884533156500498E90C2F5 +:100CD000850833D51E0145F5324441018280232623 +:100CE000C801F9B72324C801E1B7B71605003717C3 +:100CF0000500B7170500938646C1130707C1938700 +:100D0000C7C083AE05008148054F8D4F914233D552 +:100D10001E0105ED8280B716050037170500B717CD +:100D20000500938606C21307C7C1938787C1D1BF49 +:100D3000B716050037170500B7170500938686C05C +:100D4000130747C0938707C06DBF331E1F0133765B +:100D5000DE0119E2850865BF411122C635B7E16899 +:100D60009388086A01488147014781460146B705D3 +:100D7000200689B5011106CEA307010089476393B8 +:100D8000F502B7052035E1681307F1009388086A7A +:100D90000148814785460146313DF2400345F10057 +:100DA00005618280B7052005F9BF011106CE22CC6E +:100DB00026CA2306B100AA84A306C1004D37E16804 +:100DC0009388086A01487C00014789460146B78537 +:100DD000200126850964F93B130414717D1419E47C +:100DE0000D45F2406244D2440561828085452685E6 +:100DF0005137058965F50145EDB7011106CE22CCC5 +:100E000026CA2E844AC8AA84328936C6893FB24788 +:100E1000E16822869388086A01480147CA86B78537 +:100E200080022685616479331304146A7D1411C429 +:100E3000854526858137058975F9F2406244D2449B +:100E4000424905618280011106CE22CC26CA2EC6F7 +:100E5000AA8431373246E1689388086A014881479D +:100E600001478146B705802026856164A1331304BC +:100E7000146A7D1411C485452685ED3D058975F9F3 +:100E8000F2406244D24405618280B3C7A5008D8BD5 +:100E9000B308C500B1E78D4763F4C7049377350005 +:100EA0002A87B9EB13F6C8FFB306E64093070002A2 +:100EB00063C8D706AE86BA876371C70203A8060067 +:100EC0009107910623AE07FFE3EAC7FE9307F6FFFB +:100ED000998FF19B91073E97BE95636617018280BB +:100EE0002A87637E150383C7050005078505A30FC1 +:100EF000F7FEE39AE8FE828083C605000507937734 +:100F00003700A30FD7FE8505D1DF83C6050005078F +:100F100093773700A30FD7FE8505F9FF61B782806D +:100F2000411122C61304000283A3050083A24500D9 +:100F300083AF850003AFC50083AE050103AE450155 +:100F400003A3850103A8C501945113074702B30702 +:100F5000E640232E77FC232057FE2322F7FF23248D +:100F6000E7FF2326D7FF2328C7FF232A67FE232C6A +:100F700007FF232ED7FE93854502E347F4FAAE869A +:100F8000BA876371C70203A806009107910623AED2 +:100F900007FFE3EAC7FE9307F6FF998FF19B9107DE +:100FA0003E97BE956365170132444101828083C735 +:100FB000050005078505A30FF7FEE387E8FE83C755 +:100FC000050005078505A30FF7FEE392E8FEE9BFDC +:100FD00020000000010000000300000006000000E7 +:100FE000EB00000000000000000000000000000016 +:100FF000000000800000000000000700000000006A +:1010000000000000000000000000000000000000E0 +:1010100000000000000000000000000000000000D0 :0400000501000000F6 :00000001FF diff --git a/cores/arduino/trap_handler.c b/cores/arduino/trap_handler.c index 4495da8..dc8ecd2 100644 --- a/cores/arduino/trap_handler.c +++ b/cores/arduino/trap_handler.c @@ -19,8 +19,8 @@ void __attribute__((noinline, section(".ram_text"))) trap_handler(void) ((GPIO_0)->CLEAR = (GPIO_PIN_0)); // gpio interrupt if (EPIC_CHECK_GPIO_IRQ()) - gpio_interrupts_handler(); - // softSerial_handler_wrapper(); + // gpio_interrupts_handler(); + softSerial_handler_wrapper(); // tone timer interrupt if (EPIC_CHECK_TIMER16_1()) diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index 455bebb..eb42f25 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -34,7 +34,7 @@ volatile uint8_t SoftwareSerial::_receive_buffer_head = 0; // Private methods // function placed in ram. Should not call other function placed in flash -void SoftwareSerial::tunedDelay(uint32_t delayTicks) +void SoftwareSerial::tunedDelay(uint32_t delayTicks) // .ram_text { if (delayTicks > 1) { @@ -82,25 +82,25 @@ bool SoftwareSerial::stopListening() // // The receive routine called by the interrupt handler // -void SoftwareSerial::recv() +void SoftwareSerial::recv() // .ram_text { - uint8_t data = 0; // If RX line is high, then we don't see any start bit // so interrupt is probably not for us bool rxState = (bool)GPIO_READ_PIN((GPIO_TypeDef *)_receivePortRegister, (HAL_PinsTypeDef)_receiveBitMask); if (_inverse_logic ? rxState : !rxState) { - // Disable further interrupts during reception, this prevents // triggering another interrupt directly after we return, which can // cause problems at higher baudrates. setRxIntMsk(false); // __always_inline__ // Wait approximately 1/2 of a bit width to "center" the sample - tunedDelay(_rx_delay_centering); // __always_inline__ + if (_rx_delay_centering > 0) + tunedDelay(_rx_delay_centering); // .ram_text GPIO_0->OUTPUT_ ^= GPIO_PIN_1; // Read each of the 8 bits + uint8_t data = 0; for (uint8_t i=8; i > 0; --i) { tunedDelay(_rx_delay_intrabit); @@ -147,7 +147,7 @@ uint8_t SoftwareSerial::rx_pin_read() // Interrupt handling /* static */ -inline void SoftwareSerial::handle_interrupt() +void SoftwareSerial::handle_interrupt() // .ram_text { if (active_object) { @@ -155,7 +155,8 @@ inline void SoftwareSerial::handle_interrupt() } } -extern "C" void softSerial_handler_wrapper(void) +extern "C" void softSerial_handler_wrapper(void) __attribute__((noinline, section(".ram_text"))); +extern "C" void softSerial_handler_wrapper(void) { SoftwareSerial::handle_interrupt(); } @@ -237,28 +238,16 @@ void SoftwareSerial::begin(long speed) // Only setup rx when we have a valid INT for this pin if (digitalPinToInterrupt(_receivePin) != NOT_AN_INTERRUPT) { - // 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, 750); // spifi - _rx_delay_centering = subtract_cap(bit_delay / 2, 315); // ram - - // 1 bit time - // _rx_delay_intrabit = subtract_cap(bit_delay, 200); // spifi - _rx_delay_intrabit = subtract_cap(bit_delay, 60); // ram - - // 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, 950); // spifi - _rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, 142); // ram + // 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); - if (speed==115200) - { - _rx_delay_intrabit = subtract_cap(_rx_delay_intrabit, 10); - _rx_delay_stopbit+=30; - } + // 1 bit time + _rx_delay_intrabit = subtract_cap(bit_delay, 60); + + // 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); @@ -274,7 +263,7 @@ void SoftwareSerial::begin(long speed) tunedDelay(_tx_delay); // if we were low this establishes the end // todo убрать - HAL_GPIO_PinConfig(GPIO_0, GPIO_PIN_8, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); + HAL_GPIO_PinConfig(GPIO_0, GPIO_PIN_0, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); HAL_GPIO_PinConfig(GPIO_0, GPIO_PIN_1, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); listen(); diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.h b/libraries/SoftwareSerial/src/SoftwareSerial.h index a7584de..eea17bc 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.h +++ b/libraries/SoftwareSerial/src/SoftwareSerial.h @@ -64,7 +64,7 @@ private: static SoftwareSerial *active_object; // private methods - inline void recv() __attribute__((__always_inline__)); + void recv() __attribute__((noinline, section(".ram_text"))); uint8_t rx_pin_read(); void setTX(uint8_t transmitPin); void setRX(uint8_t receivePin); @@ -103,7 +103,7 @@ public: using Print::write; // public only for easy access by interrupt handlers - static inline void handle_interrupt() __attribute__((__always_inline__)); + static void handle_interrupt() __attribute__((noinline, section(".ram_text"))); }; #endif -- 2.43.0 From 21df57e980ab28538b80a7a0166c4777a8b2995d Mon Sep 17 00:00:00 2001 From: klassents Date: Tue, 8 Oct 2024 14:10:14 +0700 Subject: [PATCH 14/31] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D0=BB=D0=B0=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9?= =?UTF-8?q?=D0=BA=D1=83=20=D0=BF=D1=80=D0=B5=D1=80=D1=8B=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=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"); -- 2.43.0 From b92071316327a701dea85a917da862317de4b783 Mon Sep 17 00:00:00 2001 From: klassents Date: Tue, 8 Oct 2024 17:02:02 +0700 Subject: [PATCH 15/31] =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20?= =?UTF-8?q?=D1=81=20=D0=BE=D0=B4=D0=BD=D0=B8=D0=BC=20=D0=BF=D0=BE=D1=80?= =?UTF-8?q?=D1=82=D0=BE=D0=BC=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SoftwareSerialExample.ino | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 libraries/SoftwareSerial/examples/SoftwareSerialExample/SoftwareSerialExample.ino diff --git a/libraries/SoftwareSerial/examples/SoftwareSerialExample/SoftwareSerialExample.ino b/libraries/SoftwareSerial/examples/SoftwareSerialExample/SoftwareSerialExample.ino new file mode 100644 index 0000000..ef55efe --- /dev/null +++ b/libraries/SoftwareSerial/examples/SoftwareSerialExample/SoftwareSerialExample.ino @@ -0,0 +1,49 @@ +/* + Software serial multiple serial test + + Receives from the hardware serial, sends to software serial. + Receives from software serial, sends to hardware serial. + + The circuit: + * RX is digital pin 2 (connect to TX of other device) + * TX is digital pin 6 (connect to RX of other device) + + Note: + Not all pins on the Elbear Ace-Uno support interrupts, + so only the following can be used for RX: + 2, 3, 4, 5, 8, 9 + + created back in the mists of time + modified 25 May 2012 + by Tom Igoe + based on Mikal Hart's example + + This example code is in the public domain. + + */ +#include + +SoftwareSerial mySerial(2, 6); // RX, TX + +void setup() { + // Open serial communications and wait for port to open: + Serial.begin(115200); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + Serial.println("Goodnight moon!"); + + // set the data rate for the SoftwareSerial port + mySerial.begin(9600); + mySerial.println("Hello, world?"); +} + +void loop() { // run over and over + if (mySerial.available()) { + Serial.write(mySerial.read()); + } + if (Serial.available()) { + mySerial.write(Serial.read()); + } +} -- 2.43.0 From 47ff1ce4eb37a1298bb609c2a2e9be2b7d494fc7 Mon Sep 17 00:00:00 2001 From: klassents Date: Wed, 9 Oct 2024 11:27:37 +0700 Subject: [PATCH 16/31] =?UTF-8?q?=D1=82=D0=B5=D0=BA=D1=83=D1=87=D0=BA?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/WInterrupts.c | 17 ++-- cores/arduino/trap_handler.c | 9 +- cores/arduino/wiring_LL.h | 16 ++-- .../TwoPortReceive/TwoPortReceive.ino | 84 +++++++++++++++++++ .../SoftwareSerial/src/SoftwareSerial.cpp | 63 ++++++-------- variants/standart/pins_arduino.h | 23 ++--- variants/standart/variant.c | 15 ++-- 7 files changed, 141 insertions(+), 86 deletions(-) create mode 100644 libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino diff --git a/cores/arduino/WInterrupts.c b/cores/arduino/WInterrupts.c index d69071f..64bf587 100644 --- a/cores/arduino/WInterrupts.c +++ b/cores/arduino/WInterrupts.c @@ -4,6 +4,7 @@ #include "pins_arduino.h" #include "wiring_digital.h" #include "WInterrupts.h" +#include "wiring_LL.h" extern void ErrorMsgHandler(const char * msg); @@ -100,33 +101,27 @@ void detachInterrupt(uint8_t interruptNum) void disableInterrupt(uint8_t interruptNum) { if(interruptNum < EXTERNAL_NUM_INTERRUPTS) - { - int irq_line_num = interruptToGpioIntLine(interruptNum) >> GPIO_IRQ_LINE_S; // disable gpio interrupt line - GPIO_IRQ->ENABLE_CLEAR = (1 << irq_line_num); - } + GPIO_IRQ_LINE_DISABLE(interruptToGpioIntLine(interruptNum)); } // enable single interrupt void enableInterrupt(uint8_t interruptNum) { if(interruptNum < EXTERNAL_NUM_INTERRUPTS) - { - int irq_line_num = interruptToGpioIntLine(interruptNum) >> GPIO_IRQ_LINE_S; // enable gpio interrupt line - GPIO_IRQ->ENABLE_SET = (1 << irq_line_num); - } + GPIO_IRQ_LINE_ENABLE(interruptToGpioIntLine(interruptNum)); } // common gpio interrupt handler +void gpio_interrupts_handler(void) __attribute__((noinline, section(".ram_text"))); void gpio_interrupts_handler(void) { // go through all the interrupts and call the handler for the triggered line for (uint8_t i = 0; i < EXTERNAL_NUM_INTERRUPTS; i++) { - if (HAL_GPIO_LineInterruptState(interruptToGpioIntLine(i))) + if (GPIO_IRQ_LINE_STATE(interruptToGpioIntLine(i))) intFunc[i](); } - - HAL_GPIO_ClearInterrupts(); + GPIO_IRQ_CLEAR_ALL(); } \ No newline at end of file diff --git a/cores/arduino/trap_handler.c b/cores/arduino/trap_handler.c index dc8ecd2..712913c 100644 --- a/cores/arduino/trap_handler.c +++ b/cores/arduino/trap_handler.c @@ -1,8 +1,5 @@ #include "mik32_hal_irq.h" -#include "mik32_hal_gpio.h" - - // isr functions extern void serial_handler_wrapper(uint8_t uartNumInt); extern void gpio_interrupts_handler(void); @@ -11,16 +8,13 @@ void __attribute__((weak)) wire_handler_wrapper(void) { // dummy function for case when wire library is not in use } -extern void softSerial_handler_wrapper(void); // ---------------------------------------------- // void __attribute__((noinline, section(".ram_text"))) trap_handler(void) { - ((GPIO_0)->CLEAR = (GPIO_PIN_0)); // gpio interrupt if (EPIC_CHECK_GPIO_IRQ()) - // gpio_interrupts_handler(); - softSerial_handler_wrapper(); + gpio_interrupts_handler(); // tone timer interrupt if (EPIC_CHECK_TIMER16_1()) @@ -40,5 +34,4 @@ void __attribute__((noinline, section(".ram_text"))) trap_handler(void) // reset all interrupts HAL_EPIC_Clear(0xFFFFFFFF); - ((GPIO_0)->SET = (GPIO_PIN_0)); } diff --git a/cores/arduino/wiring_LL.h b/cores/arduino/wiring_LL.h index fec4efe..84ba3c7 100644 --- a/cores/arduino/wiring_LL.h +++ b/cores/arduino/wiring_LL.h @@ -16,14 +16,18 @@ set_csr(mie, MIE_MEIE) // ----------------- GPIO ----------------- // -#define GPIO_SET_PIN(GPIO_x, pin) ((GPIO_x)->SET = (pin)) -#define GPIO_CLEAR_PIN(GPIO_x, pin) ((GPIO_x)->CLEAR = (pin)) -#define GPIO_READ_PIN(GPIO_x, pin) (((GPIO_x)->SET & (pin)) != (uint32_t)GPIO_PIN_LOW ? GPIO_PIN_HIGH : GPIO_PIN_LOW) +#define GPIO_SET_PIN(GPIO_x, pin) ((GPIO_x)->SET = (pin)) +#define GPIO_CLEAR_PIN(GPIO_x, pin) ((GPIO_x)->CLEAR = (pin)) +#define GPIO_READ_PIN(GPIO_x, pin) (((GPIO_x)->SET & (pin)) != (uint32_t)GPIO_PIN_LOW ? GPIO_PIN_HIGH : GPIO_PIN_LOW) // ----------------- GPIO IRQ ----------------- // -#define GPIO_IRQ_LINE_ENABLE(lineMask) ( GPIO_IRQ->ENABLE_SET = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) -#define GPIO_IRQ_LINE_DISABLE(lineMask) ( GPIO_IRQ->ENABLE_CLEAR = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) -#define GPIO_IRQ_CLEAR_ALL() (GPIO_IRQ->CLEAR = 0b11111111) +#define GPIO_IRQ_LINE_ENABLE(lineMask) ( GPIO_IRQ->ENABLE_SET = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) +#define GPIO_IRQ_LINE_DISABLE(lineMask) ( GPIO_IRQ->ENABLE_CLEAR = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) +#define GPIO_IRQ_LINE_STATE(lineMask) ((GPIO_IRQ->INTERRUPT & (1 << (lineMask >> GPIO_IRQ_LINE_S))) != 0) +#define GPIO_IRQ_CLEAR_ALL() ( GPIO_IRQ->CLEAR = 0b11111111) +// ----------------- PIN CONFIG ----------------- // +// return config of pin with pinShift(0...16) in portReg (config, pupd, ds for ports 0...2) +#define PIN_GET_PAD_CONFIG(portReg, pinShift) ((PAD_CONFIG->portReg >> (pinShift<<1)) & 0b11) #endif /* _WIRING_LL_H_ */ \ No newline at end of file diff --git a/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino b/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino new file mode 100644 index 0000000..ad9c143 --- /dev/null +++ b/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino @@ -0,0 +1,84 @@ +/* + Software serial multiple serial test + + Receives from the two software serial ports, + sends to the hardware serial port. + + In order to listen on a software port, you call port.listen(). + When using two software serial ports, you have to switch ports + by listen()ing on each one in turn. Pick a logical time to switch + ports, like the end of an expected transmission, or when the + buffer is empty. This example switches ports when there is nothing + more to read from a port + + The circuit: + Two devices which communicate serially are needed. + * First serial device's TX attached to digital pin 2(RX), RX to pin 6(TX) + * Second serial device's TX attached to digital pin 8(RX), RX to pin 10(TX) + + Note: + Not all pins on the Elbear Ace-Uno support interrupts, + so only the following can be used for RX: + 2, 3, 4, 5, 8, 9 + + created 18 Apr. 2011 + modified 19 March 2016 + by Tom Igoe + based on Mikal Hart's twoPortRXExample + + This example code is in the public domain. + + */ + +#include +// software serial #1: RX = digital pin 2, TX = digital pin 6 +SoftwareSerial portOne(2, 6); + +// software serial #2: RX = digital pin 8, TX = digital pin 10 +SoftwareSerial portTwo(8, 10); + +void setup() { + // Open serial communications and wait for port to open: + Serial.begin(115200); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // Start each software serial port + portOne.begin(57600); + portTwo.begin(57600); + + portTwo.listen(); + delay(2000); +} + +void loop() { + // By default, the last initialized port is listening. + // when you want to listen on a port, explicitly select it: + portOne.listen(); + Serial.println("Data from port one:"); + // while there is data coming in, read it + // and send to the hardware serial port: + while (portOne.available() > 0) { + char inByte = portOne.read(); + Serial.write(inByte); + } + + // blank line to separate data from the two ports: + Serial.println(); + delay(2000); + + // // Now listen on the second port + // portTwo.listen(); + // // while there is data coming in, read it + // // and send to the hardware serial port: + // Serial.println("Data from port two:"); + // while (portTwo.available() > 0) { + // char inByte = portTwo.read(); + // Serial.write(inByte); + // } + + // // blank line to separate data from the two ports: + // Serial.println(); + // delay(2000); +} diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index c2a320f..18d9b41 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -89,15 +89,13 @@ void SoftwareSerial::recv() // .ram_text bool rxState = (bool)GPIO_READ_PIN((GPIO_TypeDef *)_receivePortRegister, (HAL_PinsTypeDef)_receiveBitMask); if (_inverse_logic ? rxState : !rxState) { - // Disable further interrupts during reception, this prevents - // triggering another interrupt directly after we return, which can - // cause problems at higher baudrates. + // Disable further interrupts during reception, this prevents triggering another interrupt + // directly after we return, which can cause problems at higher baudrates. setRxIntMsk(false); // __always_inline__ // Wait approximately 1/2 of a bit width to "center" the sample if (_rx_delay_centering > 0) tunedDelay(_rx_delay_centering); // .ram_text - GPIO_0->OUTPUT_ ^= GPIO_PIN_1; // Read each of the 8 bits uint8_t data = 0; @@ -105,13 +103,9 @@ void SoftwareSerial::recv() // .ram_text { tunedDelay(_rx_delay_intrabit); data >>= 1; - - GPIO_0->OUTPUT_ ^= GPIO_PIN_1; - if (GPIO_READ_PIN((GPIO_TypeDef *)_receivePortRegister, (HAL_PinsTypeDef)_receiveBitMask)) data |= 0x80; } - if (_inverse_logic) data = ~data; @@ -124,17 +118,13 @@ void SoftwareSerial::recv() // .ram_text _receive_buffer_tail = next; } else - { _buffer_overflow = true; - } // skip the stop bit tunedDelay(_rx_delay_stopbit); - GPIO_0->OUTPUT_ ^= GPIO_PIN_1; // Re-enable interrupts when we're sure to be inside the stop bit setRxIntMsk(true); - } GPIO_IRQ_CLEAR_ALL(); } @@ -151,7 +141,7 @@ void SoftwareSerial::handle_interrupt() // .ram_text { if (active_object) { - active_object->recv(); + active_object->recv(); // .ram_text } } @@ -187,29 +177,33 @@ SoftwareSerial::~SoftwareSerial() void SoftwareSerial::setTX(uint8_t tx) { - // First configure then write because writing pin didn't work until config is applied + // save pin and port info + _transmitBitMask = (uint16_t)digitalPinToBitMask(tx); + _transmitPortRegister = (uint32_t)digitalPinToPort(tx); + // set state before tx pin initialization to prevent false start bit + if (_inverse_logic) + GPIO_CLEAR_PIN((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask); + else + GPIO_SET_PIN((GPIO_TypeDef *)_transmitPortRegister, (HAL_PinsTypeDef)_transmitBitMask); + // init pin as output pinMode(tx, OUTPUT); - digitalWrite(tx, _inverse_logic ? LOW : HIGH); - _transmitBitMask = (uint16_t)digitalPinToBitMask(tx); // save tx bit mask - _transmitPortRegister = (uint32_t)digitalPinToPort(tx); // save tx port adress } void SoftwareSerial::setRX(uint8_t rx) { - _receiveBitMask = (uint16_t)digitalPinToBitMask(rx); // save rx bit mask - _receivePortRegister = (uint32_t)digitalPinToPort(rx); // save rx port adress - if (!_inverse_logic) - pinMode(rx, INPUT_PULLUP); // pullup for normal logic - else - pinMode(rx, INPUT); - - // Save gpio_line for turning on/off interrupts + // save pin, port and gpio_line info + _receiveBitMask = (uint16_t)digitalPinToBitMask(rx); + _receivePortRegister = (uint32_t)digitalPinToPort(rx); _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 + + // attach interrupt to rx pin if available + attachInterrupt(digitalPinToInterrupt(_receivePin), softSerial_handler_wrapper, _inverse_logic ? RISING : FALLING); + if (!_inverse_logic) + // turn on pull up for rx if logic is not inverse + HAL_GPIO_PinConfig((GPIO_TypeDef *)_receivePortRegister, (HAL_PinsTypeDef)_receiveBitMask, + HAL_GPIO_MODE_GPIO_INPUT, HAL_GPIO_PULL_UP, HAL_GPIO_DS_2MA); + // turn off int line for while (it turning on in attachInterrupt()) + GPIO_IRQ_LINE_DISABLE(_int_maskLine); } uint32_t SoftwareSerial::subtract_cap(uint32_t num, uint16_t sub) @@ -235,10 +229,11 @@ void SoftwareSerial::begin(long speed) // Precalculate the various delays in number of ticks uint32_t bit_delay = F_CPU / speed; + // init tx 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 + // init rx only when we have a valid INT for this pin if (digitalPinToInterrupt(_receivePin) != NOT_AN_INTERRUPT) { // set pin config @@ -260,10 +255,6 @@ void SoftwareSerial::begin(long speed) tunedDelay(_tx_delay); // if we were low this establishes the end - // todo убрать - HAL_GPIO_PinConfig(GPIO_0, GPIO_PIN_0, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); - HAL_GPIO_PinConfig(GPIO_0, GPIO_PIN_1, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA); - listen(); } @@ -363,4 +354,4 @@ int SoftwareSerial::peek() // Read from "head" return _receive_buffer[_receive_buffer_head]; -} +} \ No newline at end of file diff --git a/variants/standart/pins_arduino.h b/variants/standart/pins_arduino.h index 75d1ced..404f7da 100644 --- a/variants/standart/pins_arduino.h +++ b/variants/standart/pins_arduino.h @@ -109,7 +109,13 @@ static const uint8_t SCL = PIN_WIRE_SCL; #define WIRE_FREQ_1000K 1000000 // interrupts -extern const uint8_t interruptInfo[EXTERNAL_NUM_INTERRUPTS][3]; +extern uint8_t interruptInfo[EXTERNAL_NUM_INTERRUPTS][3]; +// determines the board pin number by interrupt number +#define interruptToDigitalPin(interruptNum) (interruptInfo[interruptNum][0]) +// determines gpio interrupt line by interrupt number +#define interruptToGpioIntLine(interruptNum) ((uint8_t)interruptInfo[interruptNum][1]) +// determines gpio interrupt mux by interrupt number +#define interruptToGpioIntMux(interruptNum) ((uint8_t)interruptInfo[interruptNum][2]) // determines interrupt number by the board pin number int8_t digitalPinToInterrupt(uint32_t digPinNumber); // determines interrupt number by the gpio interrupt line @@ -119,23 +125,8 @@ int8_t digitalPinToGpioIntMux(uint8_t digPinNumber); // determines gpio interrupt line by the board pin number int8_t digitalPinToGpioIntLine(uint8_t digPinNumber); -// determines the board pin number by interrupt number -inline uint8_t interruptToDigitalPin(uint8_t interruptNum) -{ - return interruptInfo[interruptNum][0]; -} -// determines gpio interrupt line by interrupt number -inline uint8_t interruptToGpioIntLine(uint8_t interruptNum) -{ - return (uint8_t)interruptInfo[interruptNum][1]; -} -// determines gpio interrupt mux by interrupt number -inline uint8_t interruptToGpioIntMux(uint8_t interruptNum) -{ - return (uint8_t)interruptInfo[interruptNum][2]; -} #ifdef __cplusplus } diff --git a/variants/standart/variant.c b/variants/standart/variant.c index 9608ab5..f5466c2 100644 --- a/variants/standart/variant.c +++ b/variants/standart/variant.c @@ -14,6 +14,7 @@ #include "pins_arduino.h" #include "mik32_hal_adc.h" #include "wiring_analog.h" +#include "wiring_LL.h" #ifdef __cplusplus extern "C" { @@ -107,10 +108,6 @@ volatile uint32_t* portInputRegister(GPIO_TypeDef* GPIO_x) return &GPIO_x->STATE; } -// return config of pin with pinShift(0...16) in portReg (config, pupd, ds for ports 0...2) -#define PIN_PAD_CONFIG(portReg, pinShift) ((PAD_CONFIG->portReg >> (pinShift<<1)) & 0b11) - - // ---------------------- ADC ---------------------- // // determines the ADC channel number by the board pin number uint32_t analogInputToChannelNumber(uint32_t PinNumber) @@ -168,9 +165,9 @@ bool digitalPinPwmIsOn(uint8_t digitalPin) uint8_t pinShift = pwmPinToGpioPinShift(digitalPin); if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0) - config = PIN_PAD_CONFIG(PORT_0_CFG, pinShift); + config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift); else - config = PIN_PAD_CONFIG(PORT_1_CFG, pinShift); + config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift); if (config == 2) return true; @@ -216,9 +213,9 @@ HAL_TIMER32_CHANNEL_IndexTypeDef pwmPinToTimerChannel(uint32_t digPinNumber) } // ---------------------- interrupts ---------------------- // -// interrupt table +// interrupt table is stored in ram to improve performance // index = interrupt number. In each row {digitalPinNumber, IntLineValue, IntMuxValue} -const uint8_t interruptInfo[EXTERNAL_NUM_INTERRUPTS][3] = +uint8_t interruptInfo[EXTERNAL_NUM_INTERRUPTS][3] = { { 2, GPIO_LINE_2, GPIO_MUX_LINE_2_PORT0_10}, // INT0 { 3, GPIO_LINE_0, GPIO_MUX_LINE_0_PORT0_0}, // INT1 @@ -281,7 +278,7 @@ void spi_onBegin(void) // because spi needs pin 1.3 for correct work // replace config from 1.3 to 1.4 - uint8_t config = PIN_PAD_CONFIG(PORT_1_CFG, PIN_3_SHIFT); + uint8_t config = PIN_GET_PAD_CONFIG(PORT_1_CFG, PIN_3_SHIFT); if (config == 0) // common gpio { // get info from pin gpio1.3 and set config to gpio1.4 -- 2.43.0 From 186b1906ad92ac32c8768fca54f153380b773e4c Mon Sep 17 00:00:00 2001 From: klassents Date: Wed, 9 Oct 2024 21:50:28 +0700 Subject: [PATCH 17/31] =?UTF-8?q?=D1=83=D0=B1=D1=80=D0=B0=D0=BB=D0=B0=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B5=D0=B4=D1=83=D0=BF=D1=80=D0=B5=D0=B6=D0=B4?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=BA=D1=80=D1=8B=D1=82=D0=B8=D0=B5=20=D0=BF=D0=B0?= =?UTF-8?q?=D0=BC=D1=8F=D1=82=D0=B8=20=D0=BF=D1=80=D0=B8=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BF=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B8=20=D1=81?= =?UTF-8?q?=D1=82=D1=80=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/WString.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index eb17895..1f06d2b 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -19,6 +19,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#pragma GCC diagnostic ignored "-Wrestrict" // для GCC и Clang + #include "WString.h" #include "itoa.h" -- 2.43.0 From d1a5896d2d5a9ca8440e21a1857d3c0848df488b Mon Sep 17 00:00:00 2001 From: klassents Date: Thu, 10 Oct 2024 10:49:09 +0700 Subject: [PATCH 18/31] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB-=D0=B7=D0=B0=D0=B3?= =?UTF-8?q?=D0=BB=D1=83=D1=88=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/util/delay.h | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 cores/arduino/util/delay.h diff --git a/cores/arduino/util/delay.h b/cores/arduino/util/delay.h new file mode 100644 index 0000000..c9ccd0d --- /dev/null +++ b/cores/arduino/util/delay.h @@ -0,0 +1,6 @@ +#ifndef __DELAY_H_ +#define __DELAY_H_ + + + +#endif // __DELAY_H_ -- 2.43.0 From d7010fba6ebe77f0ffdc2254673af5fcf13142f5 Mon Sep 17 00:00:00 2001 From: klassents Date: Thu, 10 Oct 2024 19:14:24 +0700 Subject: [PATCH 19/31] =?UTF-8?q?=D0=B7=D0=B0=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0=20=D1=87=D0=B0=D1=81=D1=82=D1=8C=20=D1=84=D1=83?= =?UTF-8?q?=D0=BD=D0=BA=D1=86=D0=B8=D0=B9=20=D0=BD=D0=B0=20=D0=BC=D0=B0?= =?UTF-8?q?=D0=BA=D1=80=D0=BE=D1=81=D1=8B=20=D0=B8=D0=B7=20wiring=5FLL.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/WInterrupts.c | 4 +- cores/arduino/board.cpp | 2 - cores/arduino/trap_handler.c | 3 +- cores/arduino/wiring_LL.h | 55 ++++++++++++++----- .../SoftwareSerial/src/SoftwareSerial.cpp | 11 +++- 5 files changed, 53 insertions(+), 22 deletions(-) diff --git a/cores/arduino/WInterrupts.c b/cores/arduino/WInterrupts.c index 64bf587..78bcd08 100644 --- a/cores/arduino/WInterrupts.c +++ b/cores/arduino/WInterrupts.c @@ -19,13 +19,13 @@ static void nothing(void) // enable global interrupts void interrupts(void) { - HAL_IRQ_EnableInterrupts(); + GLOBAL_IRQ_ENABLE(); } // disable global interrupts void noInterrupts(void) { - HAL_IRQ_DisableInterrupts(); + GLOBAL_IRQ_DISABLE(); } // we can provide no more than 8 interrupts on gpio at the same time diff --git a/cores/arduino/board.cpp b/cores/arduino/board.cpp index 4b06378..04bfa4c 100644 --- a/cores/arduino/board.cpp +++ b/cores/arduino/board.cpp @@ -27,8 +27,6 @@ void post_init(void) { // enable global interrupts by default interrupts(); - - ((GPIO_0)->SET = (GPIO_PIN_0)); } // --------------------- other --------------------- // diff --git a/cores/arduino/trap_handler.c b/cores/arduino/trap_handler.c index 712913c..9e6fc21 100644 --- a/cores/arduino/trap_handler.c +++ b/cores/arduino/trap_handler.c @@ -1,4 +1,5 @@ #include "mik32_hal_irq.h" +#include "wiring_LL.h" // isr functions extern void serial_handler_wrapper(uint8_t uartNumInt); @@ -33,5 +34,5 @@ void __attribute__((noinline, section(".ram_text"))) trap_handler(void) wire_handler_wrapper(); // reset all interrupts - HAL_EPIC_Clear(0xFFFFFFFF); + EPIC_CLEAR_ALL(); } diff --git a/cores/arduino/wiring_LL.h b/cores/arduino/wiring_LL.h index 84ba3c7..a635e5f 100644 --- a/cores/arduino/wiring_LL.h +++ b/cores/arduino/wiring_LL.h @@ -4,30 +4,57 @@ #include "mik32_hal_irq.h" #include "mik32_hal_gpio.h" #include "mik32_hal_scr1_timer.h" +#include "mik32_hal_timer16.h" -// ----------------- Systick ----------------- // +// ----------------- COMMON ----------------- // +// convert pin mask from HAL_PinsTypeDef to pin number +#define PIN_MASK_TO_PIN_NUMBER(mask) \ +({ \ + uint8_t pos; \ + for (pos = 0; pos < 16; pos++) \ + if ((mask >> pos) & 1) \ + break; \ + pos; \ +}) + +// ----------------- SYSTICK ----------------- // // get ticks from systick -#define SYSTICK_GET_TICKS() ((uint64_t)(SCR1_TIMER->MTIMEH)<<32 | SCR1_TIMER->MTIME) +#define SYSTICK_GET_TICKS() ((uint64_t)(SCR1_TIMER->MTIMEH)<<32 | SCR1_TIMER->MTIME) +// ----------------- TIMER16 ----------------- // +#define TIM16_DISABLE(htim16) (htim16.Instance->CR &= ~TIMER16_CR_ENABLE_M) +#define TIM16_CLEAR_INT_MASK(htim16, intMask) (htim16.Instance->ICR = intMask) +#define TIM16_GET_ARRM_INT_STATUS(htim16) ((bool)((htim16.Instance->ISR & htim16.Instance->IER) & TIMER16_ISR_ARR_MATCH_M)) + +// ----------------- EPIC ----------------- // +#define EPIC_LEVEL_CLEAR_BY_MASK(mask) (EPIC->MASK_LEVEL_CLEAR |= mask) +#define EPIC_CLEAR_ALL() (EPIC->CLEAR = 0xFFFFFFFF) // ----------------- IRQ ----------------- // -#define GLOBAL_IRQ_DISABLE() (clear_csr(mie, MIE_MEIE)) -#define GLOBAL_IRQ_ENABLE() set_csr(mstatus, MSTATUS_MIE); \ - set_csr(mie, MIE_MEIE) +#define GLOBAL_IRQ_DISABLE() (clear_csr(mie, MIE_MEIE)) +#define GLOBAL_IRQ_ENABLE() set_csr(mstatus, MSTATUS_MIE); \ + set_csr(mie, MIE_MEIE) // ----------------- GPIO ----------------- // -#define GPIO_SET_PIN(GPIO_x, pin) ((GPIO_x)->SET = (pin)) -#define GPIO_CLEAR_PIN(GPIO_x, pin) ((GPIO_x)->CLEAR = (pin)) -#define GPIO_READ_PIN(GPIO_x, pin) (((GPIO_x)->SET & (pin)) != (uint32_t)GPIO_PIN_LOW ? GPIO_PIN_HIGH : GPIO_PIN_LOW) +#define GPIO_SET_PIN(GPIO_x, pinMask) ((GPIO_x)->SET = (pinMask)) +#define GPIO_CLEAR_PIN(GPIO_x, pinMask) ((GPIO_x)->CLEAR = (pinMask)) +#define GPIO_TOGGLE_PIN(GPIO_x, pinMask) ((GPIO_x)->OUTPUT_ ^= pinMask) +#define GPIO_READ_PIN(GPIO_x, pinMask) (((GPIO_x)->SET & (pinMask)) != (uint32_t)GPIO_PIN_LOW ? GPIO_PIN_HIGH : GPIO_PIN_LOW) +// get pin state by it's number +#define GPIO_GET_PIN_STATE(GPIO_x, pinNumber) (((GPIO_x)->OUTPUT_ >> pinNumber) & 0b1) // ----------------- GPIO IRQ ----------------- // -#define GPIO_IRQ_LINE_ENABLE(lineMask) ( GPIO_IRQ->ENABLE_SET = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) -#define GPIO_IRQ_LINE_DISABLE(lineMask) ( GPIO_IRQ->ENABLE_CLEAR = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) -#define GPIO_IRQ_LINE_STATE(lineMask) ((GPIO_IRQ->INTERRUPT & (1 << (lineMask >> GPIO_IRQ_LINE_S))) != 0) -#define GPIO_IRQ_CLEAR_ALL() ( GPIO_IRQ->CLEAR = 0b11111111) +#define GPIO_IRQ_LINE_ENABLE(lineMask) ( GPIO_IRQ->ENABLE_SET = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) +#define GPIO_IRQ_LINE_DISABLE(lineMask) ( GPIO_IRQ->ENABLE_CLEAR = (1 << (lineMask >> GPIO_IRQ_LINE_S)) ) +#define GPIO_IRQ_LINE_STATE(lineMask) ((GPIO_IRQ->INTERRUPT & (1 << (lineMask >> GPIO_IRQ_LINE_S))) != 0) +#define GPIO_IRQ_CLEAR_ALL() ( GPIO_IRQ->CLEAR = 0b11111111) // ----------------- PIN CONFIG ----------------- // -// return config of pin with pinShift(0...16) in portReg (config, pupd, ds for ports 0...2) -#define PIN_GET_PAD_CONFIG(portReg, pinShift) ((PAD_CONFIG->portReg >> (pinShift<<1)) & 0b11) +// return config of pin with pinNumber(0...16) in portReg (config, pupd, ds for ports 0...2) +#define PIN_GET_PAD_CONFIG(portReg, pinNumber) ((PAD_CONFIG->portReg >> (pinNumber<<1)) & 0b11) +#define PIN_SET_PAD_CONFIG(portReg, pinNumber, value) (PAD_CONFIG->portReg = (PAD_CONFIG->portReg & (~PAD_CONFIG_PIN_M(pinNumber))) \ + | PAD_CONFIG_PIN(pinNumber, value)) + + #endif /* _WIRING_LL_H_ */ \ No newline at end of file diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index 18d9b41..001eb0b 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -198,10 +198,15 @@ void SoftwareSerial::setRX(uint8_t rx) // attach interrupt to rx pin if available attachInterrupt(digitalPinToInterrupt(_receivePin), softSerial_handler_wrapper, _inverse_logic ? RISING : FALLING); + // turn on pull up for rx if logic is not inverse if (!_inverse_logic) - // turn on pull up for rx if logic is not inverse - HAL_GPIO_PinConfig((GPIO_TypeDef *)_receivePortRegister, (HAL_PinsTypeDef)_receiveBitMask, - HAL_GPIO_MODE_GPIO_INPUT, HAL_GPIO_PULL_UP, HAL_GPIO_DS_2MA); + { + uint8_t pinNumber = PIN_MASK_TO_PIN_NUMBER(_receiveBitMask); + if (((GPIO_TypeDef*)_receivePortRegister) == GPIO_0) + PIN_SET_PAD_CONFIG(PORT_0_PUPD, pinNumber, HAL_GPIO_PULL_UP); + else if (((GPIO_TypeDef*)_receivePortRegister) == GPIO_1) + PIN_SET_PAD_CONFIG(PORT_1_PUPD, pinNumber, HAL_GPIO_PULL_UP); + } // turn off int line for while (it turning on in attachInterrupt()) GPIO_IRQ_LINE_DISABLE(_int_maskLine); } -- 2.43.0 From 9e7aed4aa7cfd863d450125ccddab5996229678e Mon Sep 17 00:00:00 2001 From: klassents Date: Thu, 10 Oct 2024 21:53:28 +0700 Subject: [PATCH 20/31] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=B8=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=D0=BB=D0=B0=20=D0=BE=D0=B1=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D1=87=D0=B8=D0=BA=D0=B8=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B5=D1=80=D1=8B=D0=B2=D0=B0=D0=BD=D0=B8=D0=B9=20=D0=B2=D1=81?= =?UTF-8?q?=D0=B5=20=D0=BA=20=D0=BE=D0=B4=D0=BD=D0=BE=D0=BC=D1=83=20=D0=B2?= =?UTF-8?q?=D0=B8=D0=B4=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/HardwareSerial.cpp | 2 +- cores/arduino/WInterrupts.c | 4 ++-- cores/arduino/trap_handler.c | 14 +++++++------- libraries/SoftwareSerial/src/SoftwareSerial.cpp | 6 +++--- libraries/Wire/src/Wire.cpp | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 0ee296f..3a6f656 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -140,7 +140,7 @@ void HardwareSerial::rx_complete_irq(void) } // wrapper for use in С-files -extern "C" void serial_handler_wrapper(uint8_t uartNumInt) +extern "C" void serial_interrupt_handler(uint8_t uartNumInt) { if (uartNumInt == 0) { diff --git a/cores/arduino/WInterrupts.c b/cores/arduino/WInterrupts.c index 78bcd08..c1c92f4 100644 --- a/cores/arduino/WInterrupts.c +++ b/cores/arduino/WInterrupts.c @@ -114,8 +114,8 @@ void enableInterrupt(uint8_t interruptNum) } // common gpio interrupt handler -void gpio_interrupts_handler(void) __attribute__((noinline, section(".ram_text"))); -void gpio_interrupts_handler(void) +void gpio_interrupt_handler(void) __attribute__((noinline, section(".ram_text"))); +void gpio_interrupt_handler(void) { // go through all the interrupts and call the handler for the triggered line for (uint8_t i = 0; i < EXTERNAL_NUM_INTERRUPTS; i++) diff --git a/cores/arduino/trap_handler.c b/cores/arduino/trap_handler.c index 9e6fc21..4b709df 100644 --- a/cores/arduino/trap_handler.c +++ b/cores/arduino/trap_handler.c @@ -2,10 +2,10 @@ #include "wiring_LL.h" // isr functions -extern void serial_handler_wrapper(uint8_t uartNumInt); -extern void gpio_interrupts_handler(void); +extern void serial_interrupt_handler(uint8_t uartNumInt); +extern void gpio_interrupt_handler(void); extern void tone_interrupt_handler(void); -void __attribute__((weak)) wire_handler_wrapper(void) +void __attribute__((weak)) wire_interrupt_handler(void) { // dummy function for case when wire library is not in use } @@ -15,7 +15,7 @@ void __attribute__((noinline, section(".ram_text"))) trap_handler(void) { // gpio interrupt if (EPIC_CHECK_GPIO_IRQ()) - gpio_interrupts_handler(); + gpio_interrupt_handler(); // tone timer interrupt if (EPIC_CHECK_TIMER16_1()) @@ -23,15 +23,15 @@ void __attribute__((noinline, section(".ram_text"))) trap_handler(void) // uart0 interrupt if (EPIC_CHECK_UART_0()) - serial_handler_wrapper(0); + serial_interrupt_handler(0); // uart1 interrupt if (EPIC_CHECK_UART_1()) - serial_handler_wrapper(1); + serial_interrupt_handler(1); // i2c interrupt if (EPIC_CHECK_I2C_1()) - wire_handler_wrapper(); + wire_interrupt_handler(); // reset all interrupts EPIC_CLEAR_ALL(); diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index 001eb0b..3bee87d 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -145,8 +145,8 @@ void SoftwareSerial::handle_interrupt() // .ram_text } } -extern "C" void softSerial_handler_wrapper(void) __attribute__((noinline, section(".ram_text"))); -extern "C" void softSerial_handler_wrapper(void) +extern "C" void softSerial_interrupt_handler(void) __attribute__((noinline, section(".ram_text"))); +extern "C" void softSerial_interrupt_handler(void) { SoftwareSerial::handle_interrupt(); } @@ -197,7 +197,7 @@ void SoftwareSerial::setRX(uint8_t rx) _int_maskLine = digitalPinToGpioIntLine(_receivePin); // attach interrupt to rx pin if available - attachInterrupt(digitalPinToInterrupt(_receivePin), softSerial_handler_wrapper, _inverse_logic ? RISING : FALLING); + attachInterrupt(digitalPinToInterrupt(_receivePin), softSerial_interrupt_handler, _inverse_logic ? RISING : FALLING); // turn on pull up for rx if logic is not inverse if (!_inverse_logic) { diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index f5824d0..1593e47 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -309,7 +309,7 @@ void TwoWire::onRequest( void (*function)(void) ) } // С function for trap handler -extern "C" void wire_handler_wrapper(void) +extern "C" void wire_interrupt_handler(void) { twi_interruptHandler(); } -- 2.43.0 From fa2f3d2b8d4e4174e3e1ca59c5bf433026a5f156 Mon Sep 17 00:00:00 2001 From: klassents Date: Fri, 11 Oct 2024 10:12:17 +0700 Subject: [PATCH 21/31] =?UTF-8?q?=D1=83=D0=B1=D1=80=D0=B0=D0=BB=D0=B0=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B5=D0=B4=D1=83=D0=BF=D1=80=D0=B5=D0=B6=D0=B4?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=D0=B7-=D0=B7=D0=B0=20=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B5=D0=BD=D0=BE=D1=81=D0=B0=20=D1=84=D1=83=D0=BD?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D0=B9=20=D0=B2=20=D1=80=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platform.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platform.txt b/platform.txt index 547e26d..55628a0 100644 --- a/platform.txt +++ b/platform.txt @@ -31,7 +31,7 @@ compiler.extra_flags = -march=rv32imc_zicsr_zifencei -mabi=ilp32 -mcmodel=medlow compiler.S.flags = {compiler.extra_flags} -x assembler-with-cpp {compiler.define} {compiler.warning_flags} {compiler.MIK32_Amur.extra_include} compiler.c.flags = -c -std=gnu11 {compiler.extra_flags} {compiler.define} {compiler.warning_flags} {compiler.MIK32_Amur.extra_include} compiler.cpp.flags = -c -std=gnu++17 -fabi-version=0 -fno-exceptions -fno-rtti -fno-use-cxa-atexit -fno-threadsafe-statics {compiler.extra_flags} {compiler.define} {compiler.warning_flags} {compiler.MIK32_Amur.extra_include} -compiler.c.elf.flags = -march=rv32imc_zicsr_zifencei -mabi=ilp32 -mcmodel=medlow -nostartfiles -Xlinker +compiler.c.elf.flags = -march=rv32imc_zicsr_zifencei -mabi=ilp32 -mcmodel=medlow -nostartfiles -Xlinker compiler.ar.flags=rc compiler.elf2bin.flags=-O binary compiler.elf2hex.flags=-O ihex @@ -46,7 +46,7 @@ archive_file_path={build.path}/{archive_file} recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} "{archive_file_path}" "{object_file}" # Combine gc-sections, archives, and objects -recipe.c.combine.pattern= "{compiler.path}{compiler.c.elf.cmd}" -o "{build.path}/{build.project_name}.elf" -T "{build.core.path}/mik32/shared/ldscripts/{build.ldscript}" {compiler.c.elf.flags} -Map={build.path}/{build.project_name}.map -Wl,--gc-sections "-L{build.path}" -Wl,--start-group {object_files} -Wl,--whole-archive "{archive_file_path}" -Wl,--no-whole-archive -lc -Wl,--end-group +recipe.c.combine.pattern= "{compiler.path}{compiler.c.elf.cmd}" -o "{build.path}/{build.project_name}.elf" -T "{build.core.path}/mik32/shared/ldscripts/{build.ldscript}" {compiler.c.elf.flags} -Map={build.path}/{build.project_name}.map -Wl,--gc-sections -Wl,--no-warn-rwx-segments "-L{build.path}" -Wl,--start-group {object_files} -Wl,--whole-archive "{archive_file_path}" -Wl,--no-whole-archive -lc -Wl,--end-group # Create output (.bin file) recipe.objcopy.bin.pattern="{compiler.path}{compiler.objcopy.cmd}" {compiler.elf2bin.flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.bin" -- 2.43.0 From 067284dd0b088116f389a953093368dd3e31c7ed Mon Sep 17 00:00:00 2001 From: klassents Date: Fri, 11 Oct 2024 15:02:21 +0700 Subject: [PATCH 22/31] =?UTF-8?q?=D1=83=D0=B1=D1=80=D0=B0=D0=BB=D0=B0=20?= =?UTF-8?q?=D0=BB=D0=B8=D1=88=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=B5=D1=84=D0=B0?= =?UTF-8?q?=D0=B9=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platform.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform.txt b/platform.txt index 55628a0..a0a41a8 100644 --- a/platform.txt +++ b/platform.txt @@ -15,7 +15,7 @@ compiler.elf2hex.cmd={compiler.prefix}objcopy # Include directories and defines compiler.MIK32_Amur.extra_include= "-I{build.core.path}/avr/" "-I{build.core.path}/mik32/shared/include/" "-I{build.core.path}/mik32/shared/periphery/" "-I{build.core.path}/mik32/shared/runtime/" "-I{build.core.path}/mik32/shared/libs/" "-I{build.core.path}/mik32/hal/core/Include/" "-I{build.core.path}/mik32/hal/peripherals/Include/" "-I{build.core.path}/mik32/hal/utilities/Include/" -compiler.define="-DMCU_{build.mcu}" "-DF_CPU={build.f_cpu}" "-DARDUINO={runtime.ide.version}" "-DARDUINO_{build.board}" "-DARDUINO_ARCH_{build.arch}" "-DMIK32V2" "-D__ELBEAR_ACE_UNO__" "-D__RISC_V__" "-D__ARDUINO_ARC__" "-D__CPU_ARC__" +compiler.define="-DMCU_{build.mcu}" "-DF_CPU={build.f_cpu}" "-DARDUINO={runtime.ide.version}" "-DARDUINO_{build.board}" "-DARDUINO_ARCH_{build.arch}" "-DMIK32V2" "-D__ARDUINO_ARC__" "-D__CPU_ARC__" build.flags.optimize=-Os build.ldscript=spifi_cpp.ld -- 2.43.0 From 9d4483f9e96bd2cf7d69da220ef2914a6043aed4 Mon Sep 17 00:00:00 2001 From: klassents Date: Fri, 11 Oct 2024 20:07:59 +0700 Subject: [PATCH 23/31] =?UTF-8?q?=D1=83=D0=BA=D0=B0=D0=B7=D0=B0=D0=BB?= =?UTF-8?q?=D0=B0=20=D0=B4=D1=80=D1=83=D0=B3=D0=BE=D0=B9=20=D1=83=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B5=D0=BD=D1=8C=20=D0=BE=D0=BF=D1=82=D0=B8=D0=BC?= =?UTF-8?q?=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=87=D0=B8=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=BF=D1=80=D0=B5=D1=80=D1=8B=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/HardwareSerial.cpp | 2 +- cores/arduino/HardwareSerial.h | 2 +- cores/arduino/WInterrupts.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 3a6f656..87794aa 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -140,7 +140,7 @@ void HardwareSerial::rx_complete_irq(void) } // wrapper for use in С-files -extern "C" void serial_interrupt_handler(uint8_t uartNumInt) +extern "C" void __attribute__((optimize("O3"))) serial_interrupt_handler(uint8_t uartNumInt) { if (uartNumInt == 0) { diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index d763cd6..7ff9700 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -100,7 +100,7 @@ class HardwareSerial : public Stream using Print::write; // pull in write(str) operator bool() { return isInited; } - void rx_complete_irq(void); + void rx_complete_irq(void) __attribute__((optimize("O3"))); }; extern HardwareSerial Serial; diff --git a/cores/arduino/WInterrupts.c b/cores/arduino/WInterrupts.c index c1c92f4..472ab82 100644 --- a/cores/arduino/WInterrupts.c +++ b/cores/arduino/WInterrupts.c @@ -114,7 +114,7 @@ void enableInterrupt(uint8_t interruptNum) } // common gpio interrupt handler -void gpio_interrupt_handler(void) __attribute__((noinline, section(".ram_text"))); +void gpio_interrupt_handler(void) __attribute__((noinline, section(".ram_text"), optimize("O3"))); void gpio_interrupt_handler(void) { // go through all the interrupts and call the handler for the triggered line -- 2.43.0 From fa26d442e569819d855aff18d2b8db17765fa41f Mon Sep 17 00:00:00 2001 From: klassents Date: Sun, 13 Oct 2024 09:59:44 +0700 Subject: [PATCH 24/31] =?UTF-8?q?=D0=B7=D0=B0=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0=20=D0=BC=D0=B0=D0=BA=D1=80=D0=BE=D1=81=D1=8B=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20spi=20=D0=BD=D0=B0=20=D1=82=D0=B5,=20?= =?UTF-8?q?=D1=87=D1=82=D0=BE=20=D0=B2=20LL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- variants/standart/variant.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/variants/standart/variant.c b/variants/standart/variant.c index f5466c2..ed0f3b6 100644 --- a/variants/standart/variant.c +++ b/variants/standart/variant.c @@ -266,25 +266,20 @@ int8_t digitalPinToInterrupt(uint32_t digPinNumber) } // ---------------------- SPI ---------------------- // -// pins shift in registers -#define PIN_3_SHIFT 3 -#define PIN_4_SHIFT 4 -#define PORT1_GET_PAD_PUPD(pinShift) ((PAD_CONFIG->PORT_1_PUPD >> (pinShift<<1)) & 0b11) -#define PORT1_GET_GPIO_STATE(pinShift) ((GPIO_1->OUTPUT_ >> pinShift) & 0b1) - void spi_onBegin(void) { // On Elbear Ace-Uno rev1.1.0 there is a seller on pin 1.6 which replace D10 from spi NSS pin 1.3 to pin 1.4, // because spi needs pin 1.3 for correct work // replace config from 1.3 to 1.4 - uint8_t config = PIN_GET_PAD_CONFIG(PORT_1_CFG, PIN_3_SHIFT); + uint8_t config = PIN_GET_PAD_CONFIG(PORT_1_CFG, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3)); if (config == 0) // common gpio { // get info from pin gpio1.3 and set config to gpio1.4 HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_4, HAL_GPIO_GetPinDirection(GPIO_1, GPIO_PIN_3), - (HAL_GPIO_PullTypeDef)PORT1_GET_PAD_PUPD(PIN_3_SHIFT), HAL_GPIO_DS_2MA); - HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_4, (GPIO_PinState)PORT1_GET_GPIO_STATE(PIN_3_SHIFT)); + (HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3)), + HAL_GPIO_DS_2MA); + HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_4, (GPIO_PinState)GPIO_GET_PIN_STATE(GPIO_1, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3))); // pin D10 was switched to different gpio and can be used further } @@ -305,8 +300,8 @@ void spi_onEnd(void) { // get info from pin gpio1.4 and set config to gpio1.3 HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_3, HAL_GPIO_GetPinDirection(GPIO_1, GPIO_PIN_4), - (HAL_GPIO_PullTypeDef)PORT1_GET_PAD_PUPD(PIN_4_SHIFT), HAL_GPIO_DS_2MA); - HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_3, (GPIO_PinState)PORT1_GET_GPIO_STATE(PIN_4_SHIFT)); + (HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_4)), HAL_GPIO_DS_2MA); + HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_3, (GPIO_PinState)GPIO_GET_PIN_STATE(GPIO_1, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_4))); // switch seller back to pin 1.3 HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_6, GPIO_PIN_LOW); -- 2.43.0 From 85535d3e37f513d2f7819217095dd868dbff7d32 Mon Sep 17 00:00:00 2001 From: klassents Date: Sun, 13 Oct 2024 11:28:11 +0700 Subject: [PATCH 25/31] =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D1=87=D0=B8=D0=BA=20=D0=BF=D1=80=D0=B5=D1=80=D1=8B=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B9=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D0=B5=D0=BD=20=D0=B2=20ram.=20=D0=A4=D1=83=D0=BD=D0=BA?= =?UTF-8?q?=D1=86=D0=B8=D0=B8=20=D0=B7=D0=B0=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BD=D0=B0=20=D0=BC=D0=B0=D0=BA=D1=80=D0=BE=D1=81?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/Tone.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/cores/arduino/Tone.cpp b/cores/arduino/Tone.cpp index 81899f3..8dba4e1 100644 --- a/cores/arduino/Tone.cpp +++ b/cores/arduino/Tone.cpp @@ -3,6 +3,7 @@ #include "mik32_hal_timer16.h" #include "mik32_hal_irq.h" +#include "wiring_LL.h" #define PRESCALERS_QTY 7 typedef struct @@ -100,7 +101,7 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) HAL_EPIC_MaskLevelSet(HAL_EPIC_TIMER16_1_MASK); pinMode(pin, OUTPUT); - HAL_GPIO_WritePin((GPIO_TypeDef *)timer_pin_port, timer_pin_mask, GPIO_PIN_LOW); + GPIO_CLEAR_PIN((GPIO_TypeDef *)timer_pin_port, timer_pin_mask); HAL_Timer16_Counter_Start_IT(&htimer16_1, frequencyParams.period_ticks); timerIsOn = true; @@ -125,9 +126,9 @@ void noTone(uint8_t pin) if (timerIsOn) { // pin to 0 - HAL_GPIO_WritePin((GPIO_TypeDef *)timer_pin_port, timer_pin_mask, GPIO_PIN_LOW); - HAL_Timer16_Disable(&htimer16_1); // disable timer - HAL_EPIC_MaskLevelClear(HAL_EPIC_TIMER16_1_MASK); + GPIO_CLEAR_PIN((GPIO_TypeDef *)timer_pin_port, timer_pin_mask); + TIM16_DISABLE(htimer16_1); + EPIC_LEVEL_CLEAR_BY_MASK(HAL_EPIC_TIMER16_1_MASK); pinMode(pin, INPUT); // deinit pin timer_pin = -1; // reset to default timerIsOn = false; @@ -135,25 +136,26 @@ void noTone(uint8_t pin) } // irq handler -extern "C" void tone_interrupt_handler(void) +extern "C" void __attribute__((noinline, section(".ram_text"), optimize("O3"))) tone_interrupt_handler(void) { - if(HAL_Timer16_GetInterruptStatus_ARRM(&htimer16_1)) + if (TIM16_GET_ARRM_INT_STATUS(htimer16_1)) { // timer period has passed, change the pin state if (timer_toggle_count != 0) { - HAL_GPIO_TogglePin((GPIO_TypeDef*)timer_pin_port, timer_pin_mask); - + GPIO_TOGGLE_PIN((GPIO_TypeDef*)timer_pin_port, timer_pin_mask); if (timer_toggle_count > 0) timer_toggle_count--; } else { // turn off if the specified duration has passed - HAL_GPIO_WritePin((GPIO_TypeDef *)timer_pin_port, timer_pin_mask, GPIO_PIN_LOW); - noTone(timer_pin); + GPIO_CLEAR_PIN((GPIO_TypeDef *)timer_pin_port, timer_pin_mask); + TIM16_DISABLE(htimer16_1); + EPIC_LEVEL_CLEAR_BY_MASK(HAL_EPIC_TIMER16_1_MASK); + timerIsOn = false; } } // reset timer interrupt flags - HAL_Timer16_ClearInterruptMask(&htimer16_1, 0xFFFFFFFF); + TIM16_CLEAR_INT_MASK(htimer16_1, 0xFFFFFFFF); } \ No newline at end of file -- 2.43.0 From f8052ae19dd62923c315c0be67ab761decf449b7 Mon Sep 17 00:00:00 2001 From: klassents Date: Sun, 13 Oct 2024 11:28:36 +0700 Subject: [PATCH 26/31] =?UTF-8?q?=D0=BC=D0=B5=D0=BB=D0=BA=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/WInterrupts.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cores/arduino/WInterrupts.c b/cores/arduino/WInterrupts.c index 472ab82..9c4ad1a 100644 --- a/cores/arduino/WInterrupts.c +++ b/cores/arduino/WInterrupts.c @@ -114,8 +114,7 @@ void enableInterrupt(uint8_t interruptNum) } // common gpio interrupt handler -void gpio_interrupt_handler(void) __attribute__((noinline, section(".ram_text"), optimize("O3"))); -void gpio_interrupt_handler(void) +void __attribute__((noinline, section(".ram_text"), optimize("O3"))) gpio_interrupt_handler(void) { // go through all the interrupts and call the handler for the triggered line for (uint8_t i = 0; i < EXTERNAL_NUM_INTERRUPTS; i++) -- 2.43.0 From 9efc6c5adf2ba76c4ca62ede5fe08cba1c8dedcc Mon Sep 17 00:00:00 2001 From: klassents Date: Sun, 13 Oct 2024 11:46:40 +0700 Subject: [PATCH 27/31] =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D1=84=D1=83=D0=BD?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D0=B9=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D1=87=D0=B8=D0=BA=D0=BE=D0=B2=20=D0=B8=D0=B7=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=20=D1=83=D1=80=D0=BE=D0=B2=D0=B5=D0=BD=D1=8C?= =?UTF-8?q?=20=D0=BE=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libraries/Wire/src/Wire.cpp | 2 +- libraries/Wire/src/utility/twi.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 1593e47..c82dbe4 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -309,7 +309,7 @@ void TwoWire::onRequest( void (*function)(void) ) } // С function for trap handler -extern "C" void wire_interrupt_handler(void) +extern "C" void __attribute__((optimize("O3"))) wire_interrupt_handler(void) { twi_interruptHandler(); } diff --git a/libraries/Wire/src/utility/twi.c b/libraries/Wire/src/utility/twi.c index 0d4c86b..1978655 100644 --- a/libraries/Wire/src/utility/twi.c +++ b/libraries/Wire/src/utility/twi.c @@ -323,7 +323,7 @@ void twi_attachSlaveTxEvent( void (*function)(void) ) * Input none * Output none */ -void twi_interruptHandler(void) +void __attribute__((optimize("O3"))) twi_interruptHandler(void) { uint32_t int_mask = HAL_I2C_Get_CR1_Content(&hi2c) & I2C_INTMASK; // interrupts allowed uint32_t interrupt_status = HAL_I2C_Get_Interrupts_Status(&hi2c); // current flags -- 2.43.0 From 6a67d9fd610a2269a0fbd62fcc1538efb73f22f2 Mon Sep 17 00:00:00 2001 From: klassents Date: Sun, 13 Oct 2024 14:06:16 +0700 Subject: [PATCH 28/31] =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=B4=D0=B2=D1=83=D1=85=20=D0=BF=D0=BE?= =?UTF-8?q?=D1=80=D1=82=D0=BE=D0=B2=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= =?UTF-8?q?=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TwoPortReceive/TwoPortReceive.ino | 95 +++++++++++-------- 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino b/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino index ad9c143..62a57cc 100644 --- a/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino +++ b/libraries/SoftwareSerial/examples/TwoPortReceive/TwoPortReceive.ino @@ -20,65 +20,80 @@ Not all pins on the Elbear Ace-Uno support interrupts, so only the following can be used for RX: 2, 3, 4, 5, 8, 9 - - created 18 Apr. 2011 - modified 19 March 2016 - by Tom Igoe - based on Mikal Hart's twoPortRXExample - - This example code is in the public domain. - */ #include + // software serial #1: RX = digital pin 2, TX = digital pin 6 SoftwareSerial portOne(2, 6); - // software serial #2: RX = digital pin 8, TX = digital pin 10 SoftwareSerial portTwo(8, 10); -void setup() { - // Open serial communications and wait for port to open: +// active port is changed by user button pressing +volatile bool isPortOneActive = true; +void btn_pressed_callback(void) +{ + // change active port + if (isPortOneActive) + { + portTwo.listen(); + Serial.println("Port two is listening"); + isPortOneActive = false; + } + else + { + portOne.listen(); + Serial.println("Port one is listening"); + isPortOneActive = true; + } +} + +void setup() +{ + // Open serial communications and wait for port to open Serial.begin(115200); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } + // attach interrupt to user button + attachInterrupt(digitalPinToInterrupt(BTN_BUILTIN), btn_pressed_callback, FALLING); + // Start each software serial port portOne.begin(57600); portTwo.begin(57600); - portTwo.listen(); - delay(2000); + // First listen to port one + portOne.listen(); + isPortOneActive = true; + Serial.println("Port one is listening"); } -void loop() { - // By default, the last initialized port is listening. - // when you want to listen on a port, explicitly select it: - portOne.listen(); - Serial.println("Data from port one:"); - // while there is data coming in, read it - // and send to the hardware serial port: - while (portOne.available() > 0) { - char inByte = portOne.read(); - Serial.write(inByte); +void loop() +{ + // check data from port one + if (portOne.available()) + { + Serial.println("Data from port one:"); + // while there is data coming in, read it + // and send to the hardware serial port + while (portOne.available() > 0) + Serial.write((char)portOne.read()); + + // blank line to separate data from the two ports + Serial.println(); } - // blank line to separate data from the two ports: - Serial.println(); - delay(2000); - - // // Now listen on the second port - // portTwo.listen(); - // // while there is data coming in, read it - // // and send to the hardware serial port: - // Serial.println("Data from port two:"); - // while (portTwo.available() > 0) { - // char inByte = portTwo.read(); - // Serial.write(inByte); - // } - - // // blank line to separate data from the two ports: - // Serial.println(); - // delay(2000); + // check data from port two + if (portTwo.available()) + { + Serial.println("Data from port two:"); + // while there is data coming in, read it + // and send to the hardware serial port + while (portTwo.available() > 0) + Serial.write((char)portTwo.read()); + + // blank line to separate data from the two ports + Serial.println(); + } } -- 2.43.0 From a02f1f23af297c83a6ca525700381094c81b50bf Mon Sep 17 00:00:00 2001 From: klassents Date: Sun, 13 Oct 2024 15:20:57 +0700 Subject: [PATCH 29/31] =?UTF-8?q?=D0=B7=D0=B0=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BC=D0=B0=D0=BA=D1=80=D0=BE=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/HardwareSerial.cpp | 6 +++--- cores/arduino/HardwareSerial.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 87794aa..284d8fc 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -5,7 +5,7 @@ #include "HardwareSerial.h" #include #include "mik32_hal_irq.h" - +#include "wiring_LL.h" // HardwareSerial class objects for use in Arduino IDE HardwareSerial Serial(0); @@ -127,9 +127,9 @@ void HardwareSerial::rx_complete_irq(void) unsigned char c; // while there is something to receive, put the data into the buffer // and edit the buffer index - while (!UART_IsRxFifoEmpty(uart)) + while(!UART_IS_RX_FIFO_EMPTY(uart)) { - c = UART_ReadByte(uart); + c = UART_READ_BYTE(uart); if (i != _rx_buffer_tail) { // write if there is space in the buffer diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index 7ff9700..d7f60a1 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -100,7 +100,7 @@ class HardwareSerial : public Stream using Print::write; // pull in write(str) operator bool() { return isInited; } - void rx_complete_irq(void) __attribute__((optimize("O3"))); + inline void rx_complete_irq(void) __attribute__((always_inline, optimize("O3"))); }; extern HardwareSerial Serial; -- 2.43.0 From 7c266d652170052be77812a58c15937ece1ed525 Mon Sep 17 00:00:00 2001 From: klassents Date: Sun, 13 Oct 2024 15:22:12 +0700 Subject: [PATCH 30/31] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BC=D0=B0?= =?UTF-8?q?=D0=BA=D1=80=D0=BE=D1=81=D1=8B,=20=D0=B4=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB=D0=B0=20=D1=83=D1=80=D0=BE=D0=B2=D0=B5=D0=BD?= =?UTF-8?q?=D1=8C=20=D0=BE=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/arduino/trap_handler.c | 2 +- cores/arduino/wiring_LL.h | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cores/arduino/trap_handler.c b/cores/arduino/trap_handler.c index 4b709df..bf16dbf 100644 --- a/cores/arduino/trap_handler.c +++ b/cores/arduino/trap_handler.c @@ -11,7 +11,7 @@ void __attribute__((weak)) wire_interrupt_handler(void) } // ---------------------------------------------- // -void __attribute__((noinline, section(".ram_text"))) trap_handler(void) +void __attribute__((noinline, section(".ram_text"), optimize("O3"))) trap_handler (void) { // gpio interrupt if (EPIC_CHECK_GPIO_IRQ()) diff --git a/cores/arduino/wiring_LL.h b/cores/arduino/wiring_LL.h index a635e5f..3649ee9 100644 --- a/cores/arduino/wiring_LL.h +++ b/cores/arduino/wiring_LL.h @@ -21,6 +21,10 @@ // get ticks from systick #define SYSTICK_GET_TICKS() ((uint64_t)(SCR1_TIMER->MTIMEH)<<32 | SCR1_TIMER->MTIME) +// ----------------- UART ----------------- // +#define UART_READ_BYTE(pUart) ((uint16_t)pUart->RXDATA) +#define UART_IS_RX_FIFO_EMPTY(pUart) ((pUart->FLAGS & UART_FLAGS_RXNE_M) == 0) + // ----------------- TIMER16 ----------------- // #define TIM16_DISABLE(htim16) (htim16.Instance->CR &= ~TIMER16_CR_ENABLE_M) #define TIM16_CLEAR_INT_MASK(htim16, intMask) (htim16.Instance->ICR = intMask) -- 2.43.0 From 39b9d95d5287febb798a238c74f52aae0a71d56b Mon Sep 17 00:00:00 2001 From: klassents Date: Sun, 13 Oct 2024 15:22:51 +0700 Subject: [PATCH 31/31] =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SoftwareSerialExample.ino | 14 ++++++-------- libraries/SoftwareSerial/src/SoftwareSerial.cpp | 3 +-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/libraries/SoftwareSerial/examples/SoftwareSerialExample/SoftwareSerialExample.ino b/libraries/SoftwareSerial/examples/SoftwareSerialExample/SoftwareSerialExample.ino index ef55efe..1663767 100644 --- a/libraries/SoftwareSerial/examples/SoftwareSerialExample/SoftwareSerialExample.ino +++ b/libraries/SoftwareSerial/examples/SoftwareSerialExample/SoftwareSerialExample.ino @@ -28,10 +28,7 @@ SoftwareSerial mySerial(2, 6); // RX, TX void setup() { // Open serial communications and wait for port to open: Serial.begin(115200); - while (!Serial) { - ; // wait for serial port to connect. Needed for native USB port only - } - + while (!Serial) ; // wait for serial port to connect. Needed for native USB port only Serial.println("Goodnight moon!"); // set the data rate for the SoftwareSerial port @@ -40,10 +37,11 @@ void setup() { } void loop() { // run over and over - if (mySerial.available()) { + while (mySerial.available()) Serial.write(mySerial.read()); - } - if (Serial.available()) { + + while (Serial.available()) mySerial.write(Serial.read()); - } + + delay(50); } diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.cpp b/libraries/SoftwareSerial/src/SoftwareSerial.cpp index 3bee87d..0f312a2 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/src/SoftwareSerial.cpp @@ -145,8 +145,7 @@ void SoftwareSerial::handle_interrupt() // .ram_text } } -extern "C" void softSerial_interrupt_handler(void) __attribute__((noinline, section(".ram_text"))); -extern "C" void softSerial_interrupt_handler(void) +extern "C" void __attribute__((noinline, section(".ram_text"))) softSerial_interrupt_handler(void) { SoftwareSerial::handle_interrupt(); } -- 2.43.0