PE- SDHC Example Project

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

PE- SDHC Example Project

939 Views
Carvic
Contributor I

I am running CW10.1 and the example SDHC project.  It seems to work fine as-is Reading/Writing single blocks.  I am trying to modify the project to read/write multiple blocks and made the following changes...

 

1.  ReadBuffer[1200UL];

2.  Change...

                            Error = SD_ReadBlockPart(&SD, 0x00000000U, 0, Length, ReadBuffer);

to...

                            Error = SD_Read2Blocks(&SD, 0x00000000U, 0, sizeof(ReadBuffer), ReadBuffer);

3.   Where the functions are declared...

 

bool SD_Read2Blocks(TSDData *SD, uint32_t Address, uint32_t Offset, uint32_t Size, uint8_t *Data)
{
  bool Error = FALSE;
  const uint16_t BlockCount = 2;
  const uint16_t BlockSize = 512;
  static uint8_t Buffer[BlockSize*BlockCount + 4]; /* Data buffer */
  uint8_t *AlignedBuffer = (uint8_t*)(((uint32_t)Buffer + 4) & ~0x03U); /* Align buffer to 4-byte boundary */

  /* Read block */
  Error = SD_TransferBlocks(SD, TRUE, Address, AlignedBuffer, BlockCount);
  /* Cut out block part */
  memcpy(Data, &AlignedBuffer[Offset], Size);
  return Error;
}

 

bool SD_TransferBlocks(TSDData *SD, bool Read, uint32_t Address, uint8_t *Buffer,  uint8_t BlockCount)
{
  bool Error = FALSE;
  const uint16_t BlockSize = 512;
//  const uint16_t BlockCount = 1;
  uint32_t CardAddr = SD_ByteToCardAddress(&SD->CardInfo, Address); /* Read block address */
  LDD_SDHC_TBufferDesc BufferDesc;
  uint32_t Index;

  /* Init read buffer descriptor */
  BufferDesc.DataPtr = Buffer;
  BufferDesc.Size = BlockSize;
  /* Read card data block */
  SDHC_TransferBlocks(SD->SDHCPtr, (Read ? LDD_SDHC_READ : LDD_SDHC_WRITE), CardAddr, &BufferDesc, BlockCount);
  SD_Wait(SD, &Error);
  return Error;
}

 

It is not working correctly, what am I missing??

0 Kudos
Reply
1 Reply

512 Views
LadislavVadkerti
NXP Employee
NXP Employee

Hi,

 

here is the declaration of SDHC_TransferBlocks:

 

LDD_TError TransferBlocks(

  LDD_TDeviceData*            DeviceDataPtr,

  LDD_SDHC_TTransferOperation Operation,

  uint32_t                    Address,

  LDD_SDHC_TBufferDesc*       BufferDescListPtr,

  uint16_t                    BufferDescCount

)

 

where BufferDescListPtr is a pointer to an array and BufferDescCount is the number of items of the array.

The best way how to transfer several blocks is to allocate buffers for the blocks and pass them to the function, for example:

 

uint8_t Buffers[BLOCK_COUNT][BLOCK_SIZE]; // Individual buffers must be properly aligned to a memory boundary!

LDD_SDHC_TBufferDesc BufferDesc[BLOCK_COUNT];

 

for (Index = 0; Index < BLOCK_COUNT; Index++) {

  BufferDesc[Index].DataPtr = Buffers[Index];

  BufferDesc[Index].Size = BLOCK_SIZE;

}

SDHC_TransferBlocks(..., ..., ..., BufferDesc, BLOCK_COUNT);

 

0 Kudos
Reply