diff --git a/src/knx/tpuart_data_link_layer.cpp b/src/knx/tpuart_data_link_layer.cpp index 4c5c39b0..c7dffdd3 100644 --- a/src/knx/tpuart_data_link_layer.cpp +++ b/src/knx/tpuart_data_link_layer.cpp @@ -525,6 +525,7 @@ void TpUartDataLinkLayer::processRxFrameComplete(uint8_t debug) print(debug); print(" - "); print(_platform.uartAvailable()); + print(" - "); printHex(" - Invalid frame: ", _rxFrame->data(), _rxFrame->size(), false); print("\x1B["); print(0); diff --git a/src/rp2040_arduino_platform.cpp b/src/rp2040_arduino_platform.cpp index 97d0be01..cb0eda5b 100644 --- a/src/rp2040_arduino_platform.cpp +++ b/src/rp2040_arduino_platform.cpp @@ -37,21 +37,24 @@ For usage of KNX-IP you have to define either #include // from Pico SDK #include // from Pico SDK -#ifdef KNX_USE_DMA_UART0 +#ifdef USE_KNX_DMA_UART #include -constexpr uint32_t uartDmaTransferCount = 0b1111111111; -// constexpr uint32_t uartDmaTransferCount = UINT32_MAX; +// constexpr uint32_t uartDmaTransferCount = 0b1111111111; +constexpr uint32_t uartDmaTransferCount = UINT32_MAX; constexpr uint8_t uartDmaBufferExp = 8u; // 2**BufferExp constexpr uint16_t uartDmaBufferSize = (1u << uartDmaBufferExp); int8_t uartDmaChannel = -1; volatile uint8_t __attribute__((aligned(uartDmaBufferSize))) uartDmaBuffer[uartDmaBufferSize] = {}; volatile uint32_t uartDmaReadCount = 0; volatile uint16_t uartDmaRestartCount = 0; +volatile uint32_t uartDmaWriteCount2 = 0; +volatile uint32_t uartDmaAvail = 0; // Liefert die Zahl der gelesenen Bytes seit dem DMA Transferstart inline uint32_t uartDmaWriteCount() { - return uartDmaTransferCount - dma_channel_hw_addr(uartDmaChannel)->transfer_count; + uartDmaWriteCount2 = uartDmaTransferCount - dma_channel_hw_addr(uartDmaChannel)->transfer_count; + return uartDmaWriteCount2; } // Liefert die aktuelle Schreibposition im DMA Buffer @@ -111,18 +114,16 @@ extern Wiznet5500lwIP KNX_NETIF; #endif RP2040ArduinoPlatform::RP2040ArduinoPlatform() -#if !defined(KNX_NO_DEFAULT_UART) && !defined(KNX_USE_DMA_UART0) +#if !defined(KNX_NO_DEFAULT_UART) && !defined(USE_KNX_DMA_UART) : ArduinoPlatform(&KNX_SERIAL) #endif { -#ifndef KNX_USE_DMA_UART0 #ifdef KNX_UART_RX_PIN _rxPin = KNX_UART_RX_PIN; #endif #ifdef KNX_UART_TX_PIN _txPin = KNX_UART_TX_PIN; #endif -#endif #ifndef USE_RP2040_EEPROM_EMULATION _memoryType = Flash; #endif @@ -138,15 +139,13 @@ RP2040ArduinoPlatform::RP2040ArduinoPlatform(HardwareSerial* s) void RP2040ArduinoPlatform::knxUartPins(pin_size_t rxPin, pin_size_t txPin) { -#ifdef KNX_USE_DMA_UART0 _rxPin = rxPin; _txPin = txPin; -#endif } bool RP2040ArduinoPlatform::overflowUart() { -#ifdef KNX_USE_DMA_UART0 +#ifdef USE_KNX_DMA_UART // during dma restart bool ret; const uint32_t writeCount = uartDmaWriteCount(); @@ -176,16 +175,19 @@ bool RP2040ArduinoPlatform::overflowUart() void RP2040ArduinoPlatform::setupUart() { -#ifdef KNX_USE_DMA_UART0 +#ifdef USE_KNX_DMA_UART if (uartDmaChannel == -1) { // configure uart0 - gpio_set_function(0, GPIO_FUNC_UART); - gpio_set_function(1, GPIO_FUNC_UART); - uart_init(uart0, 19200); - uart_set_hw_flow(uart0, false, false); - uart_set_format(uart0, 8, 1, UART_PARITY_EVEN); - uart_set_fifo_enabled(uart0, false); + println("----------------"); + println(_rxPin); + println(_txPin); + gpio_set_function(_rxPin, GPIO_FUNC_UART); + gpio_set_function(_txPin, GPIO_FUNC_UART); + uart_init(KNX_DMA_UART, 19200); + uart_set_hw_flow(KNX_DMA_UART, false, false); + uart_set_format(KNX_DMA_UART, 8, 1, UART_PARITY_EVEN); + uart_set_fifo_enabled(KNX_DMA_UART, false); // configure uart0 uartDmaChannel = dma_claim_unused_channel(true); // get free channel for dma @@ -195,7 +197,7 @@ void RP2040ArduinoPlatform::setupUart() channel_config_set_write_increment(&dmaConfig, true); channel_config_set_high_priority(&dmaConfig, true); channel_config_set_ring(&dmaConfig, true, uartDmaBufferExp); - channel_config_set_dreq(&dmaConfig, DREQ_UART0_RX); + channel_config_set_dreq(&dmaConfig, KNX_DMA_UART_DREQ); dma_channel_set_read_addr(uartDmaChannel, &uart_get_hw(uart0)->dr, false); dma_channel_set_write_addr(uartDmaChannel, uartDmaBuffer, false); dma_channel_set_trans_count(uartDmaChannel, uartDmaTransferCount, false); @@ -222,7 +224,7 @@ void RP2040ArduinoPlatform::setupUart() #endif } -#ifdef KNX_USE_DMA_UART0 +#ifdef USE_KNX_DMA_UART int RP2040ArduinoPlatform::uartAvailable() { if (uartDmaChannel == -1) @@ -235,7 +237,8 @@ int RP2040ArduinoPlatform::uartAvailable() else { uint32_t tc = dma_channel_hw_addr(uartDmaChannel)->transfer_count; - int test = uartDmaTransferCount - (tc)-uartDmaReadCount; + uartDmaAvail = tc; + int test = uartDmaTransferCount - tc - uartDmaReadCount; if (test < -1) { println("FATAL"); diff --git a/src/rp2040_arduino_platform.h b/src/rp2040_arduino_platform.h index 27b4695a..264d1b80 100644 --- a/src/rp2040_arduino_platform.h +++ b/src/rp2040_arduino_platform.h @@ -58,6 +58,16 @@ #endif +#if USE_KNX_DMA_UART == 0 +#define KNX_DMA_UART uart0 +#define KNX_DMA_UART_IRQ UART0_IRQ +#define KNX_DMA_UART_DREQ DREQ_UART0_RX +#elif USE_KNX_DMA_UART == 1 +#define KNX_DMA_UART uart1 +#define KNX_DMA_UART_IRQ UART1_IRQ +#define KNX_DMA_UART_DREQ DREQ_UART1_RX +#endif + class RP2040ArduinoPlatform : public ArduinoPlatform { @@ -69,7 +79,7 @@ class RP2040ArduinoPlatform : public ArduinoPlatform void knxUartPins(pin_size_t rxPin, pin_size_t txPin); void setupUart() override; bool overflowUart() override; - #ifdef KNX_USE_DMA_UART0 + #ifdef USE_KNX_DMA_UART int uartAvailable() override; void closeUart() override; void knxUart( HardwareSerial* serial) override {};