From e3a50e0e1ef7903edb5a10da044e12195b186323 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Sat, 25 Jun 2022 14:56:24 +0300 Subject: [PATCH] configuring fpga and dma --- armsrc/appmain.c | 2 +- armsrc/hfops.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++- armsrc/hfops.h | 2 +- 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index c28357108..5254368ae 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1389,7 +1389,7 @@ static void PacketReceived(PacketCommandNG *packet) { case CMD_HF_ACQ_RAW_ADC: { uint32_t samplesCount = 0; memcpy(&samplesCount, packet->data.asBytes, 4); - HfReadADC(samplesCount); + HfReadADC(samplesCount, true); break; } #endif diff --git a/armsrc/hfops.c b/armsrc/hfops.c index 180cca409..a1bc7b80d 100644 --- a/armsrc/hfops.c +++ b/armsrc/hfops.c @@ -28,11 +28,87 @@ #include "commonutil.h" -int HfReadADC(uint32_t samplesCount) { +int HfReadADC(uint32_t samplesCount, bool ledcontrol) { + if (ledcontrol) LEDsoff(); + + BigBuf_Clear_ext(false); + + // And put the FPGA in the appropriate mode + FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_MODE_RECEIVE_AMPLITUDE); + + // Setup and start DMA. + FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER); + + // The DMA buffer, used to stream samples from the FPGA + dmabuf16_t *dma = get_dma16(); + + // Setup and start DMA. + if (FpgaSetupSscDma((uint8_t *) dma->buf, DMA_BUFFER_SIZE) == false) { + if (g_dbglevel > DBG_ERROR) Dbprintf("FpgaSetupSscDma failed. Exiting"); + + FpgaDisableSscDma(); + FpgaSetupSsc(FPGA_MAJOR_MODE_OFF); + FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); + return PM3_EINIT; + } + + if (ledcontrol) LED_A_ON(); + + uint32_t samples = 0; + //uint32_t dma_start_time = 0; + uint16_t *upTo = dma->buf; + + for (;;) { + if (BUTTON_PRESS()) { + break; + } + + volatile uint16_t behindBy = ((uint16_t *)AT91C_BASE_PDC_SSC->PDC_RPR - upTo) & (DMA_BUFFER_SIZE - 1); + if (behindBy == 0) + continue; + + samples++; + if (samples == 1) { + // DMA has transferred the very first data + //dma_start_time = GetCountSspClk() & 0xfffffff0; + } + + if (upTo >= dma->buf + DMA_BUFFER_SIZE) { // we have read all of the DMA buffer content. + upTo = dma->buf; // start reading the circular buffer from the beginning + + // DMA Counter Register had reached 0, already rotated. + if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_ENDRX)) { + + // primary buffer was stopped + if (AT91C_BASE_PDC_SSC->PDC_RCR == false) { + AT91C_BASE_PDC_SSC->PDC_RPR = (uint32_t) dma->buf; + AT91C_BASE_PDC_SSC->PDC_RCR = DMA_BUFFER_SIZE; + } + // secondary buffer sets as primary, secondary buffer was stopped + if (AT91C_BASE_PDC_SSC->PDC_RNCR == false) { + AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) dma->buf; + AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE; + } + + WDT_HIT(); + } + } + } + + FpgaDisableSscDma(); + FpgaDisableTracing(); + + FpgaSetupSsc(FPGA_MAJOR_MODE_OFF); + // Turn the field off + FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); + + reply_ng(CMD_HF_ACQ_RAW_ADC, PM3_SUCCESS, NULL, 0); + if (ledcontrol) LEDsoff(); + DbpString("HfReadADC " _GREEN_("success")); return 0; diff --git a/armsrc/hfops.h b/armsrc/hfops.h index 76ed4b8bc..2a804ccf1 100644 --- a/armsrc/hfops.h +++ b/armsrc/hfops.h @@ -21,7 +21,7 @@ #include "common.h" -int HfReadADC(uint32_t samplesCount); +int HfReadADC(uint32_t samplesCount, bool ledcontrol); #endif \ No newline at end of file