Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imx9/px4io: Cleanups #776

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ class ArchPX4IOSerial : public PX4IO_serial

px4_sem_t _recv_sem;

/** dma error perf counter */

perf_counter_t _pc_dmaerrs;

/**
* DMA completion handler.
*/
Expand Down
53 changes: 33 additions & 20 deletions platforms/nuttx/src/px4/nxp/imx9/px4io_serial/px4io_serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
*/

#include <px4_arch/px4io_serial.h>
#include <perf/perf_counter.h>

#include <syslog.h>

Expand All @@ -63,7 +64,8 @@ static const struct uart_config_s g_px4io_config = {

ArchPX4IOSerial::ArchPX4IOSerial() :
_current_packet(nullptr),
_recv_sem(SEM_INITIALIZER(0))
_recv_sem(SEM_INITIALIZER(0)),
_pc_dmaerrs(perf_alloc(PC_COUNT, "px4io: dmaerrs"))
{
px4_sem_setprotocol(&_recv_sem, SEM_PRIO_NONE);

Expand All @@ -88,6 +90,8 @@ ArchPX4IOSerial::~ArchPX4IOSerial()

imx9_dmach_free(_rx_dma);
imx9_dmach_free(_tx_dma);

perf_free(_pc_dmaerrs);
}

int
Expand Down Expand Up @@ -161,6 +165,7 @@ ArchPX4IOSerial::ioctl(unsigned operation, unsigned &arg)
perf_print_counter(_pc_txns);
perf_print_counter(_pc_retries);
perf_print_counter(_pc_timeouts);
perf_print_counter(_pc_dmaerrs);
perf_print_counter(_pc_crcerrs);
perf_print_counter(_pc_protoerrs);
perf_print_counter(_pc_uerrs);
Expand Down Expand Up @@ -216,9 +221,7 @@ ArchPX4IOSerial::_bus_exchange(IOPacket *_packet)
(uintptr_t)_current_packet + DMA_ALIGN_UP(sizeof(IOPacket)));
#endif

/* Start the RX DMA */

_waiting_for_dma = true;
/* Configure the RX and TX DMA */

struct imx9_edma_xfrconfig_s rx_config;
rx_config.saddr = PX4IO_SERIAL_BASE + IMX9_LPUART_DATA_OFFSET;
Expand All @@ -234,11 +237,6 @@ ArchPX4IOSerial::_bus_exchange(IOPacket *_packet)
rx_config.linkch = NULL;
#endif

imx9_dmach_xfrsetup(_rx_dma, &rx_config);
imx9_dmach_start(_rx_dma, _dma_callback, (void *)this);

/* Start the TX DMA */

struct imx9_edma_xfrconfig_s tx_config;

tx_config.saddr = reinterpret_cast<uintptr_t>(_current_packet);
Expand All @@ -254,21 +252,42 @@ ArchPX4IOSerial::_bus_exchange(IOPacket *_packet)
tx_config.linkch = NULL;
#endif

imx9_dmach_xfrsetup(_tx_dma, &tx_config);
imx9_dmach_start(_tx_dma, nullptr, nullptr);
/* If setup fails, just stop and exit */

if (imx9_dmach_xfrsetup(_rx_dma, &rx_config) != OK ||
imx9_dmach_xfrsetup(_tx_dma, &tx_config) != OK) {
_stop_dma();
perf_count(_pc_dmaerrs);
perf_cancel(_pc_txns);
return -EIO;
}

/* Start DMA. imx9_dmach_start can only return OK at the time of writing this,
* but keep the error checking to look proper
*/

_waiting_for_dma = true;

if (imx9_dmach_start(_rx_dma, _dma_callback, (void *)this) != OK ||
imx9_dmach_start(_tx_dma, nullptr, nullptr) != OK) {
_stop_dma();
perf_count(_pc_dmaerrs);
perf_cancel(_pc_txns);
return -EIO;
}

/* Wait for response, max 10 ms */

ret = nxsem_tickwait_uninterruptible(&_recv_sem, MSEC2TICK(10));

if (ret == -ETIMEDOUT) {
if (ret != OK) {
_stop_dma();

nxsem_reset(&_recv_sem, 0);
perf_count(_pc_timeouts);
perf_cancel(_pc_txns); /* don't count this as a transaction */
}

if (ret == OK) {
} else {
/* Check packet CRC */

uint8_t crc = _current_packet->crc;
Expand Down Expand Up @@ -361,8 +380,6 @@ ArchPX4IOSerial::_do_interrupt()
void
ArchPX4IOSerial::_stop_dma()
{
/* Mark that we don't care about any DMA data any more */

_waiting_for_dma = false;

/* Stop the DMA channels */
Expand All @@ -375,8 +392,4 @@ ArchPX4IOSerial::_stop_dma()
while (getreg32(PX4IO_SERIAL_BASE + IMX9_LPUART_STAT_OFFSET) & LPUART_STAT_RDRF) {
getreg32(PX4IO_SERIAL_BASE + IMX9_LPUART_DATA_OFFSET);
}

/* Clear any error status flags */

modreg32(ERR_FLAGS_MASK, ERR_FLAGS_MASK, PX4IO_SERIAL_BASE + IMX9_LPUART_STAT_OFFSET);
}
Loading