⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xdma_multi_sg.c

📁 linux嵌入式系统的dma方式的实现代码
💻 C
📖 第 1 页 / 共 3 页
字号:
     * If scatter gather is busy for the DMA channel, return a status because     * restarting it could lose data.     */    Register = XIo_In32(CHANNEL_REGS + XDM_SWCR_REG_OFFSET);    if ((Register & XDM_SWCR_SGE_MASK) != 0)    {        return XST_DMA_SG_IS_STARTED;    }    /*     * Get the address of the last buffer descriptor which the DMA hardware     * finished processing.     */    LastDescriptorPtr =        (XBufDescriptor *)XIo_In32(CHANNEL_REGS + XDM_BDA_REG_OFFSET);    /*     * Start the scatter gather operation by clearing the DMACR.     * This register will be loaded when the BDA is loaded from     * the Buffer Descriptor area     */    DMACRegister = XIo_In32(CHANNEL_REGS + XDM_DMACR_REG_OFFSET);    XIo_Out32(CHANNEL_REGS + XDM_DMACR_REG_OFFSET,0x00000000);    /*     * Setup the first buffer descriptor that will be sent when the scatter     * gather channel is enabled, this is only necessary initially since     * the BDA register of the channel maintains the last buffer descriptor     * processed or when the previous buffer descriptor was the last in the     * chain.     */    if ((LastDescriptorPtr == XNULL))    {        XIo_Out32(CHANNEL_REGS + XDM_BDA_REG_OFFSET,(Xuint32)CHANNEL_DATA.GetPtr);        Register = XIo_In32(CHANNEL_REGS + XDM_SWCR_REG_OFFSET);        /*         * Clear the Disable in the Control Register         */        Register &=~XDM_SWCR_SGD_MASK;        /*         * Set the Enable and the BDAEL bits in the Control Register         */        XIo_Out32(CHANNEL_REGS + XDM_SWCR_REG_OFFSET,                  Register | XDM_SWCR_BDAEL_MASK | XDM_SWCR_SGE_MASK);    }    else    {        XBufDescriptor *NextDescriptorPtr;        /*         * Get the next descriptor to be started, if the status indicates it         * hasn't already been used by the hw, then it's OK to start it,         * sw sets the status of each descriptor to busy and then hw clears         * the busy when it is complete.         */        NextDescriptorPtr = XBufDescriptor_GetNextPtr(LastDescriptorPtr);        if ((XBufDescriptor_GetStatus(NextDescriptorPtr) &             XDM_DMASR_BUSY_MASK) == 0)        {            return XST_DMA_SG_NO_DATA;        }        /*         * Don't start the DMA SG channel if the descriptor to be processed         * by hw is to be committed by the sw, this function can be called         * such that it interrupts a thread that was putting into the list.         */        if (NextDescriptorPtr == CHANNEL_DATA.CommitPtr)        {            return XST_DMA_SG_BD_NOT_COMMITTED;        }        Register = XIo_In32(CHANNEL_REGS + XDM_SWCR_REG_OFFSET);        /*         * Clear the Disable in the Control Register         */        Register &=~XDM_SWCR_SGD_MASK;        /*         * Set the Enable and bit in the Control Register         */        XIo_Out32(CHANNEL_REGS + XDM_SWCR_REG_OFFSET,                  Register | XDM_SWCR_SGE_MASK);    }    /*     * Indicate the DMA channel scatter gather operation was started     * successfully.     */    return XST_SUCCESS;}/*****************************************************************************//**** This function stops a scatter gather operation for a scatter gather* DMA channel. This function starts the process of stopping a scatter* gather operation that is in progress and waits for the stop to be completed.* Since it waits for the operation to stopped before returning, this function* could take an amount of time relative to the size of the DMA scatter gather* operation which is in progress.  The scatter gather list of the DMA channel* is not modified by this function such that starting the scatter gather* channel after stopping it will cause it to resume.  This operation is* considered to be a graceful stop in that the scatter gather operation* completes the current buffer descriptor before stopping.** If the interrupt is enabled, an interrupt will be generated when the* operation is stopped and the caller is responsible for handling the* interrupt.** @param InstancePtr contains a pointer to the multichannel DMA to operate on.*        The DMA channel should be configured to use scatter gather in order*        for this function to be called.** @param Channel is the particular channel of interest.** @param BufDescriptorPtr is also a return value which contains a pointer to the*        buffer descriptor which the scatter gather operation completed when it*        was stopped.** @return* - XST_SUCCESS if scatter gather was stopped successfully*   <br><br>* - XST_DMA_SG_IS_STOPPED indicates scatter gather was not stopped*   because the scatter gather is not started, but was already stopped.*   <br><br>* - BufDescriptorPtr contains a pointer to the buffer descriptor which was*   completed when the operation was stopped.** @note** This function implements a loop which polls the hardware for the Stop* indication. If the hardware is hung or malfunctioning, this function will* loop for an infinite amount of time and this function may never return.*******************************************************************************/XStatus XDmaMulti_SgStop(XDmaMulti *InstancePtr, unsigned Channel,                         XBufDescriptor **BufDescriptorPtr){    Xuint32 Register;    /* Assert to verify input arguments. */    XASSERT_NONVOID(InstancePtr != XNULL);    XASSERT_NONVOID(InstancePtr->ChannelCount > Channel);    XASSERT_NONVOID(BufDescriptorPtr != XNULL);    XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);    /*     * Get the contents of the software control register, if scatter gather is     * not enabled (started), then return a status because the disable     * acknowledge would not be generated.     */    Register = XIo_In32(CHANNEL_REGS + XDM_SWCR_REG_OFFSET);    if ((Register & XDM_SWCR_SGE_MASK) == 0)    {        return XST_DMA_SG_IS_STOPPED;    }    Register = XIo_In32(CHANNEL_REGS + XDM_DMACR_REG_OFFSET);    /*     * Disable scatter gather by writing to the software control register     * without modifying any other bits of the register.     */    XIo_Out32(CHANNEL_REGS + XDM_DMACR_REG_OFFSET,              Register | XDM_DMACR_SG_STOP_MASK);    /*     * Scatter gather does not stop immediately, but after the current     * buffer descriptor is complete, so wait for the DMA channel to indicate     * the disable is complete     */    do    {        Register = XIo_In32(CHANNEL_REGS + XDM_SWCR_REG_OFFSET);    }    while ((Register & XDM_SWCR_SGE_MASK) != 0);    /*     * Ensure the enable for the scatter gather is cleared,     * writing a 1 to only that bit in the register will clear only it.     */    XIo_Out32(CHANNEL_REGS + XDM_SWCR_REG_OFFSET,              (Register | XDM_SWCR_SGD_MASK));    /*     * Set the specified buffer descriptor pointer to point to the buffer     * descriptor that the scatter gather DMA channel was processing.     */    *BufDescriptorPtr =        (XBufDescriptor *)XIo_In32(CHANNEL_REGS + XDM_BDA_REG_OFFSET);    return XST_SUCCESS;}/*****************************************************************************//**** This function creates a scatter gather list for a channel of DMA.  A scatter* gather list consists of a list of buffer descriptors that are available to* be used for scatter gather operations.  Buffer descriptors are put into the* list to request a scatter gather operation to be performed.** A number of buffer descriptors are created from the specified memory and put* into a buffer descriptor list as empty buffer descriptors. This function must* be called before non-empty buffer descriptors may be put into the DMA channel* to request scatter gather operations.** @param InstancePtr contains a pointer to the multichannel DMA to operate on.*        The DMA channel should be configured to use scatter gather in order*        for this function to be called.** @param Channel is the particular channel of interest.** @param BdMemoryPtr contains a pointer to the memory which is to be used for*        buffer descriptors and must not be cached.** @param ByteCount contains the number of bytes for the specified memory to be*        used for buffer descriptors.** @return* - XST_SUCCESS if the scatter gather list was successfully*   created.*   <br><br>* - XST_DMA_SG_LIST_EXISTS indicates that the scatter gather list*   was not created because the list has already been created.** @note** None.*******************************************************************************/XStatus XDmaMulti_CreateSgList(XDmaMulti *InstancePtr,                               unsigned Channel,                               Xuint32 *BdMemoryPtr,                               Xuint32 ByteCount){    XBufDescriptor *BufferDescriptorPtr = (XBufDescriptor *)BdMemoryPtr;    XBufDescriptor *PreviousDescriptorPtr = XNULL;    XBufDescriptor *StartOfListPtr = BufferDescriptorPtr;    Xuint32 UsedByteCount;    /*     * Assert to verify valid input arguments, alignment for those     * arguments that have alignment restrictions, and at least enough     * memory for one buffer descriptor.     */    XASSERT_NONVOID(InstancePtr != XNULL);    XASSERT_NONVOID(InstancePtr->ChannelCount > Channel);    XASSERT_NONVOID(BdMemoryPtr != XNULL);    XASSERT_NONVOID(ByteCount >= sizeof(XBufDescriptor));    XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);    /*     * If the scatter gather list has already been created, then return     * with a status.     */    if (CHANNEL_DATA.TotalDescriptorCount != 0)    {        return XST_DMA_SG_LIST_EXISTS;    }    /*     * Loop thru the specified memory block and create as many buffer     * descriptors as possible putting each into the list which is     * implemented as a ring buffer, make sure not to use any memory which     * is not large enough for a complete buffer descriptor.     */    UsedByteCount = 0;    while ((UsedByteCount + sizeof(XBufDescriptor)) <= ByteCount)    {        /*         * Setup a pointer to the next buffer descriptor in the memory and         * update # of used bytes to know when all of memory is used.         */        BufferDescriptorPtr = (XBufDescriptor *)((Xuint32)BdMemoryPtr +                                                 UsedByteCount);        /*         * Initialize the new buffer descriptor such that it doesn't contain         * garbage which could be used by the DMA hardware.         */        XBufDescriptor_Initialize(BufferDescriptorPtr);        /*         * If this is not the first buffer descriptor to be created,         * then link it to the last created buffer descriptor.         */        if (PreviousDescriptorPtr != XNULL)        {            XBufDescriptor_SetNextPtr(PreviousDescriptorPtr,                                      BufferDescriptorPtr);        }        /*         * Always keep a pointer to the last created buffer descriptor such         * that they can be linked together in the ring buffer.         */        PreviousDescriptorPtr = BufferDescriptorPtr;        /*         * Keep a count of the number of descriptors in the list to allow         * error processing to be performed.         */        CHANNEL_DATA.TotalDescriptorCount++;        UsedByteCount += sizeof(XBufDescriptor);    }    /*     * Connect the last buffer descriptor created and inserted in the list     * to the first such that a ring buffer is created.     */    XBufDescriptor_SetNextPtr(BufferDescriptorPtr, StartOfListPtr);    /*     * Initialize the ring buffer to indicate that there are no     * buffer descriptors in the list which point to valid data buffers.     */    CHANNEL_DATA.PutPtr = BufferDescriptorPtr;    CHANNEL_DATA.GetPtr = BufferDescriptorPtr;    CHANNEL_DATA.CommitPtr = XNULL;    CHANNEL_DATA.LastPtr = BufferDescriptorPtr;    CHANNEL_DATA.ActiveDescriptorCount = 0;    /* Indicate the scatter gather list was successfully created. */    return XST_SUCCESS;}/*****************************************************************************//**** This function determines if the scatter gather list of a  DMA channel is* empty with regard to buffer descriptors which are pointing to buffers to be* used for scatter gather operations.** Channel is the particular channel of interest.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -