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

📄 c6x1x_edma_mcasp.c

📁 在DSP(DEC6713)上实现了音频的采样与播放!
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
 *  Copyright 2003 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *  
 */
/* "@(#) DDK 1.11.00.00 11-04-03 (ddk-b13)" */
/* 
 *  ======== c6x1x_edma_mcasp.c ========
 * 
 *  Generic McASP driver for the TMS320C6x1x series. Uses the EDMA.
 */

#include <std.h>
#include <atm.h>
#include <hwi.h>
#include <que.h>

#include <iom.h>

#include <csl.h>
#include <csl_mcasp.h>
#include <csl_irq.h>
#include <csl_edma.h>
#include <csl_cache.h>
#include <csl_chip.h>

#include <c6x1x_edma_mcasp.h>

/*
 *  Macro to start serializer & state machine
 *  if turned on, driver will start both transmit and receive serializer 
 *  and state machine synchronously  
 */
#define STARTSERIALIZERSYNC     0

/*
 *  Macro to enable edma loop job interrupt
 *  if turned on, each time edma loop job gets running, an ineterrupt will
 *  be generated. This is only used for debug purpose.
 */
#define ENABLELOOPINTR          0 

#if ENABLELOOPINTR
#include <log.h>
extern far LOG_Obj trace;
#endif

#define IRQEVTINPUT             5
#define IRQEVTOUTPUT            6
#define IRQEDMA                 8


/* Maximum number of EDMA jobs linked at a time (Must be 2). */
#define MAXLINKCNT 2

/* Used as index since IOM mode is a bit mask and not an index */
#define INPUT   0
#define OUTPUT  1

/* States for chanCleanUp() */
#define SETFALSE 1
#define FREETCC 2
#define FREETABLE 3
#define FREETABLEEX 4
#define DELCHAN 5

/* Macro to increment and return the indice that ranges over MAXLINKCNT */
#define nextIndex(index) ((index) ^ 1)

/* Number of ports available */
#define NUMPORTS _MCASP_PORT_CNT 

/* Number of channels per port (one input and one output channel) */
#define NUMCHANS 2

/* Structure containing channel specific variables */
typedef struct ChanObj {
    Uns inUse;                /* True if the channel is in use */
    Int mode;                 /* Input or output channel */
    struct PortObj *port;     /* Pointer to the port which owns this chan */
    EDMA_Handle xferPram;     /* Handle to transfer PaRAM */
    EDMA_Handle pramTbl[MAXLINKCNT]; /* Handles to link PaRAMs */
    EDMA_Handle prevPramPtr;  /* Points to the PaRAM last used */
    EDMA_Handle loophEdma;    /* Handle to the Loop job PaRAM */
    IOM_Packet *flushPacket;  /* Holds the flushpacket (if any) */
    IOM_Packet *abortPacket;  /* Holds the abortpacket (if any) */
    IOM_Packet *packetList[MAXLINKCNT]; /* Holds linked  packets */
    QUE_Obj packetQueue;      /* Holds submitted but not linked packets */
    Int submitCount;          /* Number of submit calls pending */
    Int writeIndex;           /* Index of next PaRAM to write to */
    Int readIndex;            /* Index of next PaRAM to read from */
    Int tcc;                  /* Channel transfer complete code */
    IOM_TiomCallback cbFxn;   /* Called when I/O complete */
    Ptr cbArg;                /* Argument to callback function */
} ChanObj, *ChanHandle;

/* Structure containing port specific variables */
typedef struct PortObj {
    Uns inUse;                /* True if the port is in use */
    Int devId;                /* The device id passed to mdBindDev() */
    Bool cacheCalls;          /* Submitted buffers are in cacheable memory */
    Uint32 enableHclkg;       /* Holds enable Hclk variable */
    Uint32 enableClkg;        /* Holds enable Clk variable */
    Uint32 enableFsyncg;      /* Holds enable Fsync variable */
    MCASP_Handle hMcasp;      /* CSL Device handle */
    ChanObj chans[NUMCHANS];  /* The channels associated with the port */
    Uns chanCreated;          /* One channel in this port has been Created */
    C6X1X_EDMA_MCASP_TevtCallback evtCallback; /* event callback */
    Uns evtMask;              /* registered events */
} PortObj, *PortHandle;

/* Declare the port and channel structures */
static PortObj ports[NUMPORTS];

/* Define EDMA Event Id's Array */
static Uns eventIds[NUMPORTS][2] = {
    { EDMA_CHA_AREVT0, EDMA_CHA_AXEVT0 },
#if NUMPORTS >= 2
    { EDMA_CHA_AREVT1, EDMA_CHA_AXEVT1 },
#endif
#if NUMPORTS == 3
    { EDMA_CHA_AREVT2, EDMA_CHA_AXEVT2 }
#endif
};

/*
 * Forward declaration of the IOM interface functions. They are only
 * exposed via the IOM function table to avoid namespace pollution.
 */
static Int mdBindDev(Ptr *devp, Int devid, Ptr devParams);
static Int mdCreateChan(Ptr *chanp, Ptr devp, String name, Int mode,
                        Ptr chanParams, IOM_TiomCallback cbFxn, Ptr cbArg);
static Int mdDeleteChan(Ptr chanp);
static Int mdSubmitChan(Ptr chanp, IOM_Packet *packet);
static Int mdUnBindDev(Ptr devp);

#if ENABLELOOPINTR
static Void isrLoop(Int tcc);
#endif

/* Public IOM interface table */
IOM_Fxns C6X1X_EDMA_MCASP_FXNS = {
    &mdBindDev,
    &mdUnBindDev,
    IOM_CONTROLCHANNOTIMPL,
    &mdCreateChan,
    &mdDeleteChan,
    &mdSubmitChan
};

/* Local function prototypes */
static Void chanCleanUp(ChanHandle chan, Uns state);
static Void isrCommon(ChanHandle chan);
static Void isrInput(Int tcc);
static Void isrOutput(Int tcc);
static Void isrEvent(Int mode);
static Void linkPacket(ChanHandle chan, IOM_Packet *packet);

/* Local driver variables. */
static Uint32 loopDstBuf;
static Uint32 loopSrcBuf;

#if ENABLELOOPINTR
static Void isrLoop(Int tcc)
{
    LOG_printf(&trace, "LOOP=%d", tcc);
}
#endif

/*
 * ======== chanCleanUp ========
 * Cleans up the channel resources.
 */
static Void chanCleanUp(ChanHandle chan, Uns state)
{
    switch(state) {
    case DELCHAN:
        /* Close the EDMA channel */
        EDMA_close(chan->xferPram);

        /* Disable transfer interrupts from the EDMA */
        EDMA_intDisable(chan->tcc);
        /* will fall through the next case */
    case FREETABLEEX:
        /* Free the EDMA link PaRAM tables */
        EDMA_freeTableEx(MAXLINKCNT, chan->pramTbl);
        /* will fall through the next case */
    case FREETABLE:
        /* Free the loop EDMA PaRAM table */
        EDMA_freeTable(chan->loophEdma);
        /* will fall through the next case */
    case FREETCC:
        /* Free the transfer complete code */
        EDMA_intFree(chan->tcc);
        /* will fall through the next case */
    case SETFALSE:
        /* Mark the channel as closed */
        chan->inUse = FALSE;
        break;
    }
}
/*
 * ======== isrCommon ========
 * Shared ISR code between input and output. Processes a normal EDMA job.
 */
static Void isrCommon(ChanHandle chan)
{
    IOM_Packet *packet;
    Int cnt;

    /* Check to see if this is a completed async abort call */
    if (chan->abortPacket) {
        /* Discard all packets in transmission or queued up */
        if (chan->submitCount > MAXLINKCNT) {
            cnt = MAXLINKCNT;
        }
        else {
            cnt = chan->submitCount;
        }

        while (cnt > 0) {
            packet = chan->packetList[chan->readIndex];
            packet->status = IOM_ABORTED;
            (*chan->cbFxn)(chan->cbArg, packet);

            chan->readIndex = nextIndex(chan->readIndex);
            cnt--;
        }

        while (!QUE_empty(&chan->packetQueue)) {
            packet = QUE_dequeue(&chan->packetQueue);
            packet->status = IOM_ABORTED;
            (*chan->cbFxn)(chan->cbArg, packet);
        }

        /* Reset the driver channel state */
        chan->writeIndex = 0;
        chan->readIndex = 0;
        chan->submitCount = 0;

        chan->abortPacket->status = IOM_COMPLETED;
        (*chan->cbFxn)(chan->cbArg, chan->abortPacket);
        chan->abortPacket = NULL;
        return;
    }

    /* Fetch the completed packet */
    packet = chan->packetList[chan->readIndex];

    chan->readIndex = nextIndex(chan->readIndex);

    /* Mark the packet as completed */
    packet->status = IOM_COMPLETED;

    /* Call the callback function */
    (*chan->cbFxn)(chan->cbArg, packet);

    chan->submitCount--;

    /*
     * See if there are any unlinked packets in the packetQueue
     * and if so link them.
     */
    if (chan->submitCount >= MAXLINKCNT) {
        packet = QUE_dequeue(&chan->packetQueue);
        linkPacket(chan, packet);
    }
}

/*
 * ======== isrInput ========
 * The input isr called from the EDMA dispatcher every time an input
 * EDMA job completes.
 */
static Void isrInput(Int tcc)
{
    ChanHandle chan;
    Int portNbr;

    /* Check which port was responsible for the interrupt */
    for (portNbr = 0; portNbr < NUMPORTS; portNbr++) {
        chan = &ports[portNbr].chans[INPUT];

        if (chan->tcc == tcc && chan->inUse) {

            if (EDMA_RGETH(chan->xferPram, DST) == (Uint32) &loopDstBuf &&
                chan->submitCount > 1 && !chan->abortPacket) {
                /*
                 * An emulation halt has occured with more than 1 job
                 * submitted. Link the currently executing job (the Loop job)
                 * to the first of the linked jobs which hadn't been called
                 * back. This way we still have the same number of submitted
                 * jobs after the execution continues as we had before the
                 * emulation halt (breakpoint) occured (this preserves double
                 * buffering if used).
                 */
                EDMA_disableChannel(chan->xferPram);
                EDMA_link(chan->xferPram, chan->pramTbl[chan->readIndex]);
                EDMA_enableChannel(chan->xferPram);
            }
            else {
                /* Call the common ISR code for a finished normal EDMA job */
                isrCommon(chan);
            }
        }
    }
}

/*
 * ======== isrOutput ========
 * The output isr called from the EDMA dispatcher every time an output
 * EDMA job completes.
 */
static Void isrOutput(Int tcc)
{
    ChanHandle chan;
    Int portNbr;

    /* Check which port was responsible for the interrupt */
    for (portNbr = 0; portNbr < NUMPORTS; portNbr++) {
        chan = &ports[portNbr].chans[OUTPUT];

        if (chan->tcc == tcc && chan->inUse) {

            if (EDMA_RGETH(chan->xferPram, SRC) == (Uint32)&loopSrcBuf &&
                chan->submitCount > 1 && !chan->abortPacket) {
                /*
                 * An emulation halt has occured with more than 1 job
                 * submitted. Link the currently executing job (the Loop job)
                 * to the first of the linked jobs which hadn't been called
                 * back. This way we still have the same number of submitted
                 * jobs after the execution continues as we had before the
                 * emulation halt (breakpoint) occured (this preserves double
                 * buffering if used).
                 */
                EDMA_disableChannel(chan->xferPram);
                EDMA_link(chan->xferPram, chan->pramTbl[chan->readIndex]);
                EDMA_enableChannel(chan->xferPram);
            }
            else {
                /* Call the common ISR code for a finished normal EDMA job */
                isrCommon(chan);

                /* Check to see if an async flush has completed */
                if (chan->submitCount == 0 && chan->flushPacket) {
                    chan->flushPacket->status = IOM_COMPLETED;
                    (*chan->cbFxn)(chan->cbArg,chan->flushPacket);
                    chan->flushPacket = NULL;
                }
            }
        }
    }
}

/*
 * ======== isrEvent ========
 * The event isr called when an McASP input/output event happens
 * This is used for exception event handling asserted by McASP. Normal data
 * hanlding is done by EDMA. 
 */
static Void isrEvent(Int mode)
{
    MCASP_Handle hMcasp;
    Uns portNbr;
    Uns events;
    Uns eventReturn;
    
    /* Check which port was responsible for the interrupt */
    for (portNbr = 0; portNbr < NUMPORTS; portNbr++) {
        if (ports[portNbr].inUse) {
            hMcasp = ports[portNbr].hMcasp;
            events = 0;
            if (mode == INPUT) {          
                if (MCASP_FGETH(hMcasp, RSTAT, ROVRN)) {
                    MCASP_FSETSH(hMcasp, RSTAT, ROVRN, YES);
                    events |= C6X1X_EDMA_MCASP_EVT_ROVRN;
                } 
                if (MCASP_FGETH(hMcasp, RSTAT, RCKFAIL)) {
                    MCASP_FSETSH(hMcasp, RSTAT, RCKFAIL, YES);
                    events |= C6X1X_EDMA_MCASP_EVT_RCKFAIL;
                }
                if (MCASP_FGETH(hMcasp, RSTAT, RSYNCERR)) {
                    MCASP_FSETSH(hMcasp, RSTAT, RSYNCERR, YES);
                    events |= C6X1X_EDMA_MCASP_EVT_RSYNCERR;
                }   
                if (MCASP_FGETH(hMcasp, RSTAT, RDMAERR)) {
                    MCASP_FSETH(hMcasp, RSTAT, RDMAERR, 1);
                    events |= C6X1X_EDMA_MCASP_EVT_RDMAERR;
                }   
                /* Clear not registered events here */
                MCASP_RSETH(hMcasp, RSTAT, 0xFFFFFFFF);
            } 
            else {
                if (MCASP_FGETH(hMcasp, XSTAT, XUNDRN)) {
                    MCASP_FSETSH(hMcasp, XSTAT, XUNDRN, YES);
                    events |= C6X1X_EDMA_MCASP_EVT_XUNDRN;
                } 

⌨️ 快捷键说明

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