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

📄 dmablocks.c

📁 基于ADSP-BF535 USB驱动应用程序
💻 C
字号:
/*----------------------------------------------------------------------------------
*
* COPYRIGHT (c) 2001 by Singing Electrons, Inc. All rights reserved.
*
*
* Module Name			: C:\se\adi\audioclass\dmablocks.c
*
* Description			: A simple Heap Manager for DMA memory.
*
*
* Revision History	: At bottom of the file.
*
*---------------------------------------------------------------------------------*/

/** include files **/
#include <sys/exception.h>
#include <sys/excause.h>
#include <defBF535.h>
#include "dtype.h"
#include "dmablocks.h"
#include "dprintf.h"

/** local definitions **/
#define DMA_BUFFER_SIZE 2048
#define DMA_BLOCK_GRANULARITY 16 
#define DMA_MAX_BUFFER_ALLOCATIONS (DMA_BUFFER_SIZE / DMA_BLOCK_GRANULARITY)

/* default settings */

/** external functions **/

/** external data **/

/** internal functions **/

/** public data **/

/** private data **/
#pragma align 128
section("data1") UCHAR gDMABuffer[DMA_BUFFER_SIZE];
int gDMAAllocations[DMA_MAX_BUFFER_ALLOCATIONS];


/** public functions **/

/** private functions **/


/*------------------------------------------------------------
*	InitDMA
*
*	Parameters:  
*
*	Globals Used:
*		gDMAAllocations
*
*	Description:
*		This routine initializes the simple DMA block manager!
*
*	Returns:
*		Nothing
*
*------------------------------------------------------------*/
void InitDMA (void)
	{
		memset (&gDMAAllocations, 0, sizeof (gDMAAllocations));
	}


/*------------------------------------------------------------
*	AllocDMABlock
*
*	Parameters:  
*		numBytesToAllocate - number of bytes to allocat.
*
*	Globals Used:
*		gDMAAllocations
*
*	Description:
*		Allocates a block out of the DMA buffer space. This routine
*		allocates blocks in increments of 16 bytes.
*
*	Returns:
*		On success address of the allocated block, On failure, nil.
*
*------------------------------------------------------------*/
void *AllocDMABlock (int numBytesToAllocate)
	{
		int bufferIndex, actualBytesToAllocate, newBase, lastBuffer, totalRemaining;
		void *retVal = nil;

		//we need to allocate on a 16 byte boundary!
		if ((ULONG)numBytesToAllocate & 0x000f)
			actualBytesToAllocate = (int)(numBytesToAllocate & 0xfff0) + 16;
		else
			actualBytesToAllocate = numBytesToAllocate;

		/* This is not good as it uses a divide!
		actualBytesToAllocate = (numBytesToAllocate / 16) * 16;
		actualBytesToAllocate += (numBytesToAllocate % 16) ? 16 : 0;
		*/


		totalRemaining = actualBytesToAllocate;
		bufferIndex = 0;
		newBase = bufferIndex;
		lastBuffer = bufferIndex;
		while (bufferIndex < DMA_MAX_BUFFER_ALLOCATIONS)
			{
				if (gDMAAllocations[bufferIndex] == 0)
					{
						totalRemaining -= DMA_BLOCK_GRANULARITY;
						if (totalRemaining == 0)
							{
								lastBuffer = bufferIndex;

								//Bingo we've got what we need!
								retVal = &gDMABuffer[newBase * DMA_BLOCK_GRANULARITY];

								//Mark the allocations as used!
								for (bufferIndex = newBase; bufferIndex <= lastBuffer; bufferIndex++)
									gDMAAllocations[bufferIndex] = actualBytesToAllocate;

								//break out of the while loop!
								break;

							}
					}
				else
					{
						totalRemaining = actualBytesToAllocate;
						newBase = bufferIndex + 1;
					}
				bufferIndex++;
			}

		//Dprintf1 ("AllocDMABlock: RetVal: %08x", retVal);

		return (retVal);
	}


/*------------------------------------------------------------
*	FreeDMABlock
*
*	Parameters:  
*		blockToDealloc - address of the block to deallocate.
*
*	Globals Used:
*		gDMAAllocations
*
*	Description:
*		This routine deallocates a previously allocated DMA block. Please
*		note that this is currently a very simple heap manager, which
*		does not do any heap compaction etc..
*
*	Returns:
*		Nothing
*
*------------------------------------------------------------*/
void FreeDMABlock (void *blockToDealloc)
	{
		UINT offset;
		int startingBufferIndex;
		int numBytesToFree;
		
		if (blockToDealloc == nil)
			return;

		offset = (UCHAR *)blockToDealloc - gDMABuffer;
		startingBufferIndex = offset >> 4; //This assumes 16byte DMA block granularity
		numBytesToFree = gDMAAllocations[startingBufferIndex];

		while (numBytesToFree > 0)
			{
				gDMAAllocations[startingBufferIndex] = 0;
				startingBufferIndex++;
				numBytesToFree -= DMA_BLOCK_GRANULARITY;
			}
	}



/*----------------------------------------------------------------------------------
* $Log: dmablocks.c,v $
* Revision 1.1  2003/03/03 18:13:45  Devendra
* First Rev, the device enumerates as Mass Storage Class
*
* Revision 1.4  2003/01/13 19:49:14  Devendra
* Removed dma buffer initialization.
*
* Revision 1.2  2003/01/10 01:41:21  Devendra
* Added checking to make sure we don't try to free "null" blocks.
*
* Revision 1.1  2003/01/09 21:46:15  Devendra
* Added a better DMA block heap manager (dmablocks) so that it can be used with isoch. streaming.
*
*
*---------------------------------------------------------------------------------*/

⌨️ 快捷键说明

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