AnsweredAssumed Answered

Copy data from memory to memory fails

Question asked by zzzmqp on Jul 21, 2016
Latest reply on Aug 17, 2016 by zzzmqp


Dear all,

I scratch my head about this for too long with no progress, so...

 

I work with a LPC4337 with external SDRAM (256Mb / 32 MB) at the EMC. The external SDRAM is located at DYCS0:

/* SDRAM Address Base for DYCS0*/ #define BUFFER_BASE 0x28000000 // Start Memory address for data buffer #define BUFFER_SIZE 0x02000000 // 32 Mb = 33,554,432 byte   (0x2800 0000 --> 0x2A00 0000)

 

I can read- and write- access the buffer with no problems.

I also successfully send the buffer data out through USB (USBD_ROM_LIBUSB):

void TxBuffer(const uint32_t* buf, uint32_t len) { //...   libusbdev_QueueSendReq(buf, len); //... }

 

/* Queue the given buffer for transmision to USB host application. */ ErrorCode_t libusbdev_QueueSendReq(uint8_t *pBuf, uint32_t buf_len) {   LUSB_CTRL_T *pUSB = (LUSB_CTRL_T *) &g_lusb;   ErrorCode_t ret = ERR_FAILED;     /* Check if a read request is pending */   if (pUSB->pTxBuf == 0) {   /* Queue the read request */   pUSB->pTxBuf = pBuf;   pUSB->txBuffLen = buf_len;     USBD_API->hw->WriteEP(pUSB->hUsb, LUSB_IN_EP, pBuf, buf_len);   ret = LPC_OK;   }     return ret; }

 

Now I added a SD card to store the same data in a file. Based on the SDMMC example (including FatFS):

void TxBuffer(const uint32_t* buf, uint32_t len) { //...   SDMMC_AddData(buf, len); //... }

 

void SDMMC_AddData(uint8_t *pBuf, uint32_t buf_len) {   FRESULT rc; /* Result code */   UINT bw;     rc = f_write(&Fil, pBuf, buf_len, &bw); //... }

 

This f_write() call works just fine when the buffer is a byte array (for example writing a text string).

 

However, when i pass on my external memory location pointer as pBuf (just like with the USB variant), f_write() fails. I tracked it down to the "mem_cpy()" lines within f_write(). After a few bytes of copying from 0x28000000+, it just 'stops'. No hard fault - the debugger is just quiet for a few seconds, then disconnects from the target.

 

The FatFS mem_cpy() is a simple byte by byte copy:

/* Copy memory to memory */ static void mem_cpy (void* dst, const void* src, UINT cnt) {   BYTE *d = (BYTE*)dst;   const BYTE *s = (const BYTE*)src;     if (cnt) {   do *d++ = *s++; while (--cnt);   } }

 

So I tried the native memcpy() with the same result (it never returns).

 

My next try was a DMA transfer, which also never completes (stays enabled with transfer size >0):

/* Copy memory to memory */ static void mem_cpy_DMA(uint32_t dstAddr, uint32_t srcAddr, uint32_t bytes) {   while (LPC_GPDMA->CH[1].CONFIG & 0x1); // Wait for previous transfer to complete     /* BUFFER - DMA CONFIGURATION */   LPC_GPDMA->CH[1].SRCADDR = srcAddr;   LPC_GPDMA->CH[1].DESTADDR = dstAddr;   LPC_GPDMA->CH[1].LLI = 0; // Linked List Items not used   LPC_GPDMA->CH[1].CONTROL = (bytes <<  0)| // Transfer size   (0x0 << 12)  | // Source Burst Size = 1 transfer per request   (0x0 << 15)  | // Destination Burst Size = 1 transfer per request   (0x0 << 18)  | // Source width = byte   (0x0 << 21)  | // Destination width = byte   (0x1 << 24)  | // Source AHB master = AHB master 1   (0x1 << 25)  | // Destination AHB master = AHB master 1   (0x1 << 26)  | // Source incremented after each transfer   (0x1 << 27)  | // Destination incremented after each transfer   (0x0 << 28)  | // PROT1: privileged access disabled   (0x1 << 29)  | // PROT2: bufferable access enabled   (0x0 << 30)  | // PROT3: cacheable access disabled   (0x0L << 31); // Terminal count interrupt disabled   LPC_GPDMA->CH[1].CONFIG  =  (0x1 << 0)   | // Channel immediately enabled      (0x0 << 1)   | // Source peripheral = Don't care (transfer from memory)   (0x0 << 6)   | // Destination peripheral = Don't care (transfer to memory)   (0x0 << 11)  | // Flow control = Memory to memory (DMA control)   (0x0 << 14)  | // Interrupt error mask   (0x0L << 15); // Terminal count interrupt mask }

 

Then I tried out of curiosity to change the external SDRAM back to a (smaller) local RAM, and all three memory copy variants work fine:

#define BUFFER_BASE  0x20000000  // Start Memory address buffer #define BUFFER_SIZE  0x00010000  // 64 kB AHB SRAM

 

So my big question is: Why does the memory copy fail?

(...Although the external SDRAM buffer obviously works fine with the USB code?)

(...Why does it work with a local SRAM address, but not with the external SDRAM address?)

All experiments are with single packets of a like 70-90 bytes, so memory size and speed etc.are not a factor at this point.

 

Any thoughts or ideas are much appreciated!

Outcomes