Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
traxanos committed Mar 16, 2024
1 parent 409fa59 commit 1e27d0c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/knx/tpuart_data_link_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
43 changes: 23 additions & 20 deletions src/rp2040_arduino_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,24 @@ For usage of KNX-IP you have to define either
#include <hardware/watchdog.h> // from Pico SDK
#include <pico/unique_id.h> // from Pico SDK

#ifdef KNX_USE_DMA_UART0
#ifdef USE_KNX_DMA_UART
#include <hardware/dma.h>
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
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -222,7 +224,7 @@ void RP2040ArduinoPlatform::setupUart()
#endif
}

#ifdef KNX_USE_DMA_UART0
#ifdef USE_KNX_DMA_UART
int RP2040ArduinoPlatform::uartAvailable()
{
if (uartDmaChannel == -1)
Expand All @@ -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");
Expand Down
12 changes: 11 additions & 1 deletion src/rp2040_arduino_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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 {};
Expand Down

0 comments on commit 1e27d0c

Please sign in to comment.