📄 tmai.c.bak
字号:
/*
* Copyright (c) 1998, 1999 by TriMedia Technologies.
*
* +------------------------------------------------------------------+
* | This software is furnished under a license and may only be used |
* | and copied in accordance with the terms and conditions of such |
* | a license and with the inclusion of this copyright notice. This |
* | software or any other copies of this software may not be provided|
* | or otherwise made available to any other person. The ownership |
* | and title of this software is not transferred. |
* | |
* | The information in this software is subject to change without |
* | any prior notice and should not be construed as a commitment by |
* | TriMedia Technologies. |
* | |
* | this code and information is provided "as is" without any |
* | warranty of any kind, either expressed or implied, including but |
* | not limited to the implied warranties of merchantability and/or |
* | fitness for any particular purpose. |
* +------------------------------------------------------------------+
*
* Module name : tmAI.c 1.73
*
* Last update : 18:21:58 - 00/11/09
*
* Description :
*
* TriMedia audio input library.
* Supports control and use of TriMedia audio input hardware
*
* This is part of the TriMedia device libraries.
*
* Detailed function prototypes are found in tmAI.h.
* The AI driver relies on the board support package.
*
*
* Revision :
* Built for the TCS 2.0 release
*
*
*/
/* normally, these come from the Makefile */
#ifndef MAJOR_VERSION
#define MAJOR_VERSION 3
#endif
#ifndef MINOR_VERSION
#define MINOR_VERSION 0
#endif
#ifndef BUILD_VERSION
#define BUILD_VERSION 0
#endif
#include <tm1/tmBoard.h>
#include <tmlib/tmtypes.h>
#include <tm1/tmProcessor.h>
#include <tm1/tmInterrupts.h>
#include <tm1/tmAI.h>
#include <tm1/tmAImmio.h>
#include <tm1/tmAssert.h>
#include <tmlib/dprintf.h>
#include <tmlib/AppModel.h>
#include <tm1/tsaReg.h>
#include <string.h>
#include <tm1/tmGPIO.h>
/*-------------------------------defines-------------------------------------*/
#define AI_MAGIC 0x75839347
/*--------------------------------types--------------------------------------*/
typedef struct aiInstanceInfo_t {
UInt32 magic;
aiInstanceSetup_t setup;
Bool initialized;
unitSelect_t unitName;
Int unitIndex;
boardAIConfig_t *boardAIConfig;
Int gpioInstance;
} aiInstVars_t, *paiInstVars_t;
typedef struct
{
UInt32 numberOfUnits;
unitSelect_t *unitNumbers;
aiCapabilities_t *unitCapabilities;
} aiCapabilitiesList_t;
/*-------------------------------statics-------------------------------------*/
static aiCapabilitiesList_t aiCaps =
{
0,
Null,
Null
};
static tmLibdevErr_t prepareCapabilities(void);
static tmLibdevErr_t getUnitIndex(unitSelect_t unitName, Int *unitIndex);
/*------------------------------functions------------------------------------*/
static tmLibdevErr_t getUnitIndex(unitSelect_t unitName, Int *unitIndex)
{
Int i;
for (i = 0; i < aiCaps.numberOfUnits; i++)
{
if (aiCaps.unitNumbers[i] == unitName)
{
*unitIndex = i;
return TMLIBDEV_OK;
}
}
return TMLIBDEV_ERR_NOT_AVAILABLE_IN_HW;
}
/**************************************************************************************/
extern tmLibdevErr_t aiGetNumberOfUnits(UInt32 *pNumberOfUnits)
{
UInt32 i = 0;
Char *searchString = "/bsp/AI/*";
tsaRegFind_t findInfo;
tmLibdevErr_t err;
tmAssert(pNumberOfUnits, TMLIBDEV_ERR_NULL_PARAMETER);
err = tsaRegFindFirstEntry(searchString, &findInfo);
if (err)
{
*pNumberOfUnits = 0;
return TMLIBDEV_OK;
}
i++;
while (tsaRegFindNextEntry(searchString, &findInfo) == TMLIBDEV_OK)
i++;
*pNumberOfUnits = i;
return TMLIBDEV_OK;
}
extern tmLibdevErr_t aiGetCapabilities(paiCapabilities_t *pCap)
{
return aiGetCapabilitiesM(pCap, unit0);
}
extern tmLibdevErr_t aiGetCapabilitiesM(paiCapabilities_t *pCap, unitSelect_t unitName)
{
tmLibdevErr_t err;
boardAIConfig_t *AIConfig;
Int unitIndex;
tmAssert(pCap, TMLIBDEV_ERR_NULL_PARAMETER);
err = prepareCapabilities();
if (err != TMLIBDEV_OK)
return err;
if (getUnitIndex(unitName, &unitIndex))
return TMLIBDEV_ERR_NOT_AVAILABLE_IN_HW;
if (TMLIBDEV_OK != tsaBoardGetAI(unitName, &AIConfig))
{
/* this should never happen, since we have just checked the */
/* number of units */
/* FIXME : anyway, the error code should not be not supported */
return TMLIBDEV_ERR_NOT_AVAILABLE_IN_HW;
}
/* Update capabilities, prepareCapabilities() only writes initial values into this struct.
Some of the values can change (depending on the selected input). */
aiCaps.unitCapabilities[unitIndex].maxSRate = AIConfig->maxSRate;
aiCaps.unitCapabilities[unitIndex].minSRate = AIConfig->minSRate;
if ((AIConfig->codecName != Null))
{
strncpy(aiCaps.unitCapabilities[unitIndex].codecName, AIConfig->codecName, DEVICE_NAME_LENGTH);
}
*pCap = &aiCaps.unitCapabilities[unitIndex];
return TMLIBDEV_OK;
}
/* This code is only executed once. */
static tmLibdevErr_t prepareCapabilities(void)
{
UInt32 numberOfUnits, i;
boardAIConfig_t *AIConfig;
tmLibdevErr_t err = TMLIBDEV_OK;
unitSelect_t unitNumber = unit0;
AppModel_suspend_scheduling();
if (aiCaps.unitCapabilities == Null)
{
/* the number of units should not be contained in the board */
/* structure. Indeed, a developer might want to add a new unit */
/* without being able to modify the board config struct, since */
/* (s)he does not have access to the source */
if ((err = aiGetNumberOfUnits(&numberOfUnits)) != TMLIBDEV_OK)
goto prepareCapabilitiesExit;
if (numberOfUnits == 0)
{
err = TMLIBDEV_ERR_NOT_AVAILABLE_IN_HW;
goto prepareCapabilitiesExit;
}
aiCaps.numberOfUnits = numberOfUnits;
if (aiCaps.unitCapabilities == Null)
{
aiCaps.unitNumbers = (unitSelect_t *) malloc(aiCaps.numberOfUnits * sizeof(unitSelect_t));
if (aiCaps.unitNumbers == Null)
{
err = TMLIBDEV_ERR_MEMALLOC_FAILED;
goto prepareCapabilitiesExit;
}
aiCaps.unitCapabilities = (paiCapabilities_t) malloc(aiCaps.numberOfUnits * sizeof(aiCapabilities_t));
if (aiCaps.unitCapabilities == Null)
{
free(aiCaps.unitNumbers);
aiCaps.unitNumbers = Null;
err = TMLIBDEV_ERR_MEMALLOC_FAILED;
goto prepareCapabilitiesExit;
}
}
i = 0;
while (i < aiCaps.numberOfUnits)
{
if (TMLIBDEV_OK != tsaBoardGetAI(unitNumber, &AIConfig))
{
/* this unit number is not supported, check next */
unitNumber++;
continue;
}
aiCaps.unitNumbers[i] = unitNumber;
aiCaps.unitCapabilities[i].version.majorVersion = MAJOR_VERSION;
aiCaps.unitCapabilities[i].version.minorVersion = MINOR_VERSION;
aiCaps.unitCapabilities[i].version.buildVersion = BUILD_VERSION;
aiCaps.unitCapabilities[i].numSupportedInstances = 1;
aiCaps.unitCapabilities[i].numCurrentInstances = 0;
aiCaps.unitCapabilities[i].audioTypeFormats = AIConfig->audioTypeFormats;
aiCaps.unitCapabilities[i].audioSubtypeFormats = AIConfig->audioSubtypeFormats;
aiCaps.unitCapabilities[i].audioAdapters = AIConfig->audioAdapters;
aiCaps.unitCapabilities[i].maxSRate = AIConfig->maxSRate;
aiCaps.unitCapabilities[i].minSRate = AIConfig->minSRate;
aiCaps.unitCapabilities[i].mmioBase = AIConfig->mmioBase;
aiCaps.unitCapabilities[i].intNumber = AIConfig->intNumber;
if ((AIConfig->codecName != Null))
{
strncpy(aiCaps.unitCapabilities[i].codecName, AIConfig->codecName, DEVICE_NAME_LENGTH);
}
unitNumber++;
i++;
}
}
prepareCapabilitiesExit:
AppModel_resume_scheduling();
return err;
}
/**************************************************************************************/
extern tmLibdevErr_t aiOpen(Int * instance)
{
return aiOpenM(instance, unit0);
}
extern tmLibdevErr_t aiOpenM(Int * instance, unitSelect_t unitName)
{
,boardAIConfig_t *boardAIConfig;
tmLibdevErr_t rVal;
paiInstVars_t instVars;
gpioInstanceSetup_t gpioSetup;
UInt32 i;
Int unitIndex;
printf("tmAI.c aiOpenM\n");
tmAssert(instance != Null, TMLIBDEV_ERR_NULL_PARAMETER);
rVal = tsaBoardGetAI(unitName, &boardAIConfig);
if (rVal != TMLIBDEV_OK)
return TMLIBDEV_ERR_NOT_AVAILABLE_IN_HW;
AppModel_suspend_scheduling();
rVal = prepareCapabilities();
if (rVal != TMLIBDEV_OK)
goto aiOpenExit;
if (getUnitIndex(unitName, &unitIndex))
{
rVal = TMLIBDEV_ERR_NOT_AVAILABLE_IN_HW;
goto aiOpenExit;
}
if (aiCaps.unitCapabilities[unitIndex].numCurrentInstances >=
aiCaps.unitCapabilities[unitIndex].numSupportedInstances)
{
rVal = TMLIBDEV_ERR_NO_MORE_INSTANCES;
goto aiOpenExit;
}
rVal = intOpen(boardAIConfig->intNumber);
if (rVal != TMLIBDEV_OK)
{
goto aiOpenExit;
}
/* allocate and initialize instVars struct */
instVars = (paiInstVars_t) malloc(sizeof(aiInstVars_t));
if (instVars == Null)
{
rVal = TMLIBDEV_ERR_MEMALLOC_FAILED;
intClose(boardAIConfig->intNumber);
goto aiOpenExit;
}
instVars->magic = AI_MAGIC;
instVars->initialized = False;
instVars->unitName = unitName;
instVars->unitIndex = unitIndex;
instVars->boardAIConfig = boardAIConfig;
instVars->gpioInstance = 0;
/* this call will only fail if there is no GPIO capabilities */
if (gpioOpen(&(instVars->gpioInstance)) == TMLIBDEV_OK)
{
/* Get the default instance setup */
gpioGetInstanceSetup(instVars->gpioInstance, &gpioSetup);
for (i = boardAIConfig->gpioFirstPin; i <= boardAIConfig->gpioLastPin; i++)
{
gpioInsertMode(gpioSetup.pinMode[i >> 4],
i - (boardAIConfig->gpioFirstPin & 0xfffffff0),
gpioRegularMode);
gpioInsertPin(gpioSetup.pinMask[i >> 5],
i - (boardAIConfig->gpioFirstPin & 0xffffffe0),
1);
}
rVal = gpioInstanceSetup(instVars->gpioInstance, &gpioSetup);
if (rVal != TMLIBDEV_OK)
{
gpioClose(instVars->gpioInstance);
intClose(boardAIConfig->intNumber);
free(instVars);
goto aiOpenExit;
}
}
aiCaps.unitCapabilities[unitIndex].numCurrentInstances++;
*instance = (Int) instVars;
aiOpenExit:
AppModel_resume_scheduling();
return rVal;
}
/**************************************************************************************/
extern tmLibdevErr_t aiInstanceSetup(Int instance, paiInstanceSetup_t pSetup)
{
paiInstVars_t instVars = (paiInstVars_t) instance;
tmLibdevErr_t rval = TMLIBDEV_OK;
boardAIParam_t param;
intInstanceSetup_t setup;
boardAIConfig_t *boardAIConfig = instVars->boardAIConfig;
UInt32 mmioBase = boardAIConfig->mmioBase;
printf("1 tmAI.c : aiInstanceSetup\n");
tmAssert(instance, TMLIBDEV_ERR_NOT_OWNER);
tmAssert(pSetup, TMLIBDEV_ERR_NULL_PARAMETER);
tmAssert((pSetup->size % AI_SIZE_GRANULARITY) == 0, AIO_ERR_INVALID_SIZE);
tmAssert(((UInt) pSetup->base1 % AI_BASE_GRANULARITY) == 0, AIO_ERR_INVALID_BASE);
tmAssert(((UInt) pSetup->base2 % AI_BASE_GRANULARITY) == 0, AIO_ERR_INVALID_BASE);
printf("2 tmAI.c : aiInstanceSetup\n");
if (instVars->magic != AI_MAGIC)
return TMLIBDEV_ERR_NOT_OWNER;
AppModel_suspend_scheduling();
param.audioTypeFormat = pSetup->audioTypeFormat;
param.audioSubtypeFormat = pSetup->audioSubtypeFormat;
param.sRate = pSetup->sRate;
param.size = pSetup->size;
param.input = pSetup->input;
if (!(param.audioTypeFormat & boardAIConfig->audioTypeFormats))
{
rval = AIO_ERR_UNSUPPORTED_DATA_TYPE;
goto aiInstanceSetupExit;
}
if (!(param.audioSubtypeFormat & boardAIConfig->audioSubtypeFormats))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -