Search code examples
carmxilinx

Why xilinx use a pointer to store data as array?


I take a look at the AXI-DMA example from xilinx and I get stuck at this point in XAxiDma_SimplePollExample

for(Index = 0; Index < MAX_PKT_LEN; Index ++) {
        TxBufferPtr[Index] = Value;

        Value = (Value + 1) & 0xFF;
}

The variable TxBufferPtr is a pointer, pointing on the memory address TX_BUFFER_BASE

u8 *TxBufferPtr;
TxBufferPtr = (u8 *)TX_BUFFER_BASE ;

Why do they use a pointer to store data with a loop? I have learned, that I should use an array to initialize the memory and use the start address of this array. So my example will look like this

u8 TxBufferPtr[MAX_PKT_LEN];
for(Index = 0; Index < MAX_PKT_LEN; Index ++) {
        TxBufferPtr[Index] = Value;

        Value = (Value + 1) & 0xFF;

How do xilinx avoid that other data may be placed in range of the pointer address + the loop range, so that the loop will overwrite this data? Is there any trick when I use this or is it just "bad" code and use of pointer?


Solution

  • There is a huge difference between the two code examples.

    Your code, i.e.

    u8 TxBufferPtr[MAX_PKT_LEN];
    

    will allocate memory for the array. This will be MAX_PKT_LEN u8 typically reserved on a stack.

    This code:

    u8 *TxBufferPtr;
    TxBufferPtr = (u8 *)TX_BUFFER_BASE ;
    

    does not allocate memory for the array. The code assumes that TX_BUFFER_BASE is the address of some memory that has already been allocated/reserved for the program.

    If you are on some embedded system without MMU it can be a direct memory mapped region to some HW component inside the FPGA. In small simple embedded systems it's normal to map HW components using pointers to fixed addresses. The FPGA designer will give you the value of TX_BUFFER_BASE as it is the FPGA designer that decides how the memory space will be used.