From a286002d270dd64fd9dea9c0108c1cc91a317d46 Mon Sep 17 00:00:00 2001 From: dxl <64101226@qq.com> Date: Thu, 13 Aug 2026 12:07:45 +0800 Subject: [PATCH] Refactor ISO14443A DMA RX to use cross-platform buffer helpers Replace the hard-coded AT91 PDC register access in SniffIso14443a() with cross-platform helpers for the RX double-buffer status/refresh, so the sniffer no longer depends on AT91-specific registers. - Add FPGA_SSC_DMA_RX_{Primary,Secondary}_Done and FPGA_SSC_DMA_RX_Refresh_{Both,Secondary} to fpga_apis.h. - AT91: primary maps to PDC RPR/RCR, secondary ("next") to RNPR/RNCR. - AT32: no double buffer, so the primary path is a no-op and all re-arming is done through the secondary (single-shot DMA re-arm). --- armsrc/iso14443a.c | 24 ++++-------------------- common_arm/fpga/fpga_apis.h | 21 +++++++++++++++++++++ common_arm/fpga/fpga_hw_at32.h | 28 ++++++++++++++++++++++++++++ common_arm/fpga/fpga_hw_at91.h | 20 ++++++++++++++++++++ 4 files changed, 73 insertions(+), 20 deletions(-) diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index 9f784c0b5..8e7f0afe2 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -881,18 +881,10 @@ void RAMFUNC SniffIso14443a(uint8_t param) { dataLen = DMA_BUFFER_SIZE - readBufDataP + dmaBufDataP; } - // TODO DXL This cross platform issue needs to be addressed - // When the current buffer address is zeroed, it usually means the next buffer hasn't been assigned a value in time, - // indicating that the MCU's processing speed is slow. - // The logic here is to reset the state machine to receive data again. - // DMA fully stalled: both buffers exhausted. Re-arm primary + secondary, // resync the read pointer, and drop the in-flight frame. - if (AT91C_BASE_PDC_SSC->PDC_RCR == 0) { - AT91C_BASE_PDC_SSC->PDC_RPR = (uint32_t) dma->buf; - AT91C_BASE_PDC_SSC->PDC_RCR = DMA_BUFFER_SIZE; - AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) dma->buf; - AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE; + if (FPGA_SSC_DMA_RX_Primary_Done()) { + FPGA_SSC_DMA_RX_Refresh_Both(dma->buf, DMA_BUFFER_SIZE); data = dma->buf; rx_samples += DMA_BUFFER_SIZE; Uart14aReset(); @@ -919,17 +911,9 @@ void RAMFUNC SniffIso14443a(uint8_t param) { continue; } - /** - * TODO DXL This problem needs to be addressed. - * if (FPGA_SSC_DMA_RX_Done()) { - * FPGA_SSC_DMA_RX_Refresh_Repeat(dma->buf, DMA_BUFFER_SIZE); - * } - */ - // secondary buffer exhausted, primary still running — refill secondary - if (AT91C_BASE_PDC_SSC->PDC_RNCR == 0) { - AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) dma->buf; - AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE; + if (FPGA_SSC_DMA_RX_Secondary_Done()) { + FPGA_SSC_DMA_RX_Refresh_Secondary(dma->buf, DMA_BUFFER_SIZE); } LED_A_OFF(); diff --git a/common_arm/fpga/fpga_apis.h b/common_arm/fpga/fpga_apis.h index 26b96ac22..48255bd65 100644 --- a/common_arm/fpga/fpga_apis.h +++ b/common_arm/fpga/fpga_apis.h @@ -219,6 +219,27 @@ STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Repeat(void *buf, uint16_t len) // It can be used when data processing speed is slow or when asynchronous reception processing with multiple buffers is required. STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Single(void *buf, uint16_t len); +//----------------------------------------------------------------------------- +// DMA RX double-buffer status & refresh (primary + "next"). +// The AT91 PDC exposes a primary buffer (RPR/RCR) and a "next" buffer +// (RNPR/RNCR); when the primary drains, the "next" buffer is swapped in +// automatically. Platforms without a "next" buffer (AT32) implement these +// so the primary operations degrade to no-ops and all (re)arming is done +// through the secondary buffer. +//----------------------------------------------------------------------------- + +// Has the primary RX buffer fully drained (its remaining count reached 0)? +STATIC_FORCE_INLINE bool FPGA_SSC_DMA_RX_Primary_Done(void); + +// Has the "next" RX buffer also been consumed (nothing left to swap in)? +STATIC_FORCE_INLINE bool FPGA_SSC_DMA_RX_Secondary_Done(void); + +// Re-arm both the primary and the "next" RX buffer (restart continuous RX). +STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Both(void *buf, uint16_t len); + +// Re-arm only the "next" RX buffer. +STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Secondary(void *buf, uint16_t len); + //----------------------------------------------------------------------------- // Provide a 24MHz clock from ARM to FPGA // This is the most important main clock for FPGA, so it must be implemented! diff --git a/common_arm/fpga/fpga_hw_at32.h b/common_arm/fpga/fpga_hw_at32.h index 78a83fec0..7f542161b 100644 --- a/common_arm/fpga/fpga_hw_at32.h +++ b/common_arm/fpga/fpga_hw_at32.h @@ -138,4 +138,32 @@ STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Single(void *buf, uint16_t len) FPGA_SSC_DMA_RX_Enable(); } +STATIC_FORCE_INLINE bool FPGA_SSC_DMA_RX_Primary_Done(void) { + // AT32 has no "primary + next" double buffer. The single-shot DMA simply + // stops when the buffer is full, and that is the normal end of one + // reception cycle (the data is still valid), not the "both buffers + // exhausted" hard stall this hook describes. So it never applies here. + return false; +} + +STATIC_FORCE_INLINE bool FPGA_SSC_DMA_RX_Secondary_Done(void) { + // The single-shot DMA counter reaching 0 means the buffer is full and the + // next reception must be re-armed — this is the "secondary buffer" role on + // AT32 (the buffer that comes after the current one finishes). + return FPGA_SSC_DMA_RX_Done(); +} + +STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Both(void *buf, uint16_t len) { + // AT32 does not support a primary-buffer refresh (there is no double + // buffer), so this is intentionally a no-op. Primary_Done() is always + // false, so the caller never reaches this path anyway. + (void)buf; + (void)len; +} + +STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Secondary(void *buf, uint16_t len) { + // Re-arm the single buffer to start the next reception. + FPGA_SSC_DMA_RX_Refresh_Single(buf, len); +} + #endif \ No newline at end of file diff --git a/common_arm/fpga/fpga_hw_at91.h b/common_arm/fpga/fpga_hw_at91.h index 1b9dba1a0..f93e338f8 100644 --- a/common_arm/fpga/fpga_hw_at91.h +++ b/common_arm/fpga/fpga_hw_at91.h @@ -83,4 +83,24 @@ STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Single(void *buf, uint16_t len) AT91C_BASE_PDC_SSC->PDC_RCR = len; // transfer this many samples } +STATIC_FORCE_INLINE bool FPGA_SSC_DMA_RX_Primary_Done(void) { + return AT91C_BASE_PDC_SSC->PDC_RCR == 0; +} + +STATIC_FORCE_INLINE bool FPGA_SSC_DMA_RX_Secondary_Done(void) { + return AT91C_BASE_PDC_SSC->PDC_RNCR == 0; +} + +STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Both(void *buf, uint16_t len) { + AT91C_BASE_PDC_SSC->PDC_RPR = (uint32_t) buf; // primary buffer address + AT91C_BASE_PDC_SSC->PDC_RCR = len; // primary buffer count + AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) buf; // next buffer address + AT91C_BASE_PDC_SSC->PDC_RNCR = len; // next buffer count +} + +STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Secondary(void *buf, uint16_t len) { + AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) buf; // next buffer address + AT91C_BASE_PDC_SSC->PDC_RNCR = len; // next buffer count +} + #endif \ No newline at end of file