c5416interface.c

来自「DSP与VXWORKS平台的通信」· C语言 代码 · 共 255 行

C
255
字号
/**************************************************************************
Copyright 2002-2007 TGL Ltd. All rights reserved

c5416Interface.c   -- the source file for c5416Interface

DESCRIPTION


modification history
--------------------
2007-10-24, Lixiaoke, v1.0 initial version
--------------------

**************************************************************************/
#include <semLib.h>
#include "c5416hpi.h"
#include "c5416DataBuff.h"
#include "c5416Interface.h"

static const char           MCU_READ_FROM_DSP_TASK_NAME[ ]       = "RecvTask";
static const char           MCU_WRITE_TO_DSP_TASK_NAME[ ]        = "SendTask";
static       int            giTaskMcuReadDspHandle               = -1; 
static       int            giTaskMcuWriteDspHandle              = -1;
static       unsigned int   giMcuWriteDspAddr                    = 0x00000100;
static       unsigned int   giMcuReadFromDspAddr                 = 0x00000180;
static       SEM_ID         gsemInterruptId                      = NULL; 
static       SEM_ID         gsemMutexId                          = NULL; 

static void InterMcuRecvDataISR( void )
{
    int lockKey = intLock();
    semGive( gsemInterruptId );
    intUnlock( lockKey );
}

static int taskMcuRecvData( void )
{
    unsigned short pusStartCodBuf[ 2 ] = { 0x0001, 0x0101 };
    unsigned short pusRecvDataBuf[ 7 ] = { };
    unsigned short usMcuReadFlag = 0x0000;
    if( semTake( gsemMutexId, WAIT_FOREVER ) == ERROR )
    {
        printf( "[ taskMcuRecvData ] : semTake gsemMutexId error!\n" );
        interTransDInit();
        return( 0 );
    }
    hpiWriteDatasAtAddr( giMcuWriteDspAddr, ( unsigned short * )pusStartCodBuf, 2 );/*send the command of start coding*/
    if( semGive( gsemMutexId ) == ERROR )
    {
        printf( "[ taskMcuRecvData ] : semGive gsemMutexId error!\n" );
        interTransDInit();
        return( 0 );
    }
    while( giTaskMcuReadDspHandle != -1 )
    {
        if( semTake( gsemInterruptId, WAIT_FOREVER ) == ERROR )
        {
            printf( "[ taskMcuRecvData ] : semTake gsemInterruptId error!\n" );
            interTransDInit();
            return( 0 );
        }
        if( semTake( gsemMutexId, WAIT_FOREVER ) == ERROR )
        {
            printf( "[ taskMcuRecvData ] : semTake gsemMutexId error!\n" );
            interTransDInit();
            return( 0 );		
        }
        hpiReadDatasAtAddr( giMcuReadFromDspAddr, ( unsigned short * )pusRecvDataBuf, 7 );
        hpiWriteDatasAtAddr( giMcuReadFromDspAddr, ( unsigned short * )( &usMcuReadFlag ), 1 );      	
        if( semGive( gsemMutexId ) == ERROR )
        {
            printf( "[ taskMcuRecvData ] : semGive gsemMutexId error!\n" );
            interTransDInit();
            return( 0 );
        }      	
        if(  *( unsigned short * )pusRecvDataBuf != 0x0001  )
        {
            continue;
        }
        if( *( ( unsigned short * )pusRecvDataBuf + 1 ) != 0x8401 )
        {
            continue;
        }
        if( *( ( unsigned short * )pusRecvDataBuf + 2 ) != 0x0004 )
        {
            continue;
        }
        printf( "\nRecv : %d_%d_%d_%d_%d_%d_%d\n", pusRecvDataBuf[ 0 ], pusRecvDataBuf[ 1 ], pusRecvDataBuf[ 2 ], pusRecvDataBuf[ 3 ], pusRecvDataBuf[ 4 ], pusRecvDataBuf[ 5 ], pusRecvDataBuf[ 6 ] );                 
        if( c5416WriteDataToRecvBuf( ( unsigned short * )pusRecvDataBuf, sizeof( pusRecvDataBuf ) ) == 0 )
        {
            printf( "[ taskMcuRecvData ] : c5416WriteDataToRecvBuf error!\n" );
            interTransDInit();
            return( 0 );            
        }
    }
}

static int taskMcuSendData( void )
{
    unsigned short pusSendDataBuf[ 7 ] = { };
    unsigned short pusDspReadFlag; 
    unsigned short iCount;
    while( giTaskMcuWriteDspHandle != -1 )
    {
        if( c5416ReadDataFromSendBuf( ( unsigned short * )pusSendDataBuf, sizeof( pusSendDataBuf ) ) == 0 )
        {
            printf( "[ taskMcuSendData ] : c5416ReadDataFromSendBuf error!\n" );
            interTransDInit();
            return( 0 );    		
        }
        if( *( unsigned short * )pusSendDataBuf != 0x0001 )
        {
            continue;
        }
        if( *( ( unsigned short * )pusSendDataBuf + 1 ) != 0x8401 )
        {
            continue;
        }
        if( *( ( unsigned short * )pusSendDataBuf + 2 ) != 0x0004 )
        {
            continue;
        }   
        printf( "\nSend : %d_%d_%d_%d_%d_%d_%d\n", pusSendDataBuf[ 0 ], pusSendDataBuf[ 1 ], pusSendDataBuf[ 2 ], pusSendDataBuf[ 3 ], pusSendDataBuf[ 4 ], pusSendDataBuf[ 5 ], pusSendDataBuf[ 6 ] );
        *( unsigned short * )pusSendDataBuf = 0x0001;
        *( ( unsigned short * )pusSendDataBuf + 1 ) = 0x0801;
        *( ( unsigned short * )pusSendDataBuf + 2 ) = 0x0004;
        if( semTake( gsemMutexId, WAIT_FOREVER ) == ERROR )
        {
            printf( "[ taskMcuSendData ] : semTake gsemMutexBuf error!\n" );
            interTransDInit();
            return( 0 );
        }
        for( iCount = 0; iCount < 4; iCount ++ )
        {
            hpiReadDatasAtAddr( giMcuWriteDspAddr, ( unsigned short * )( &pusDspReadFlag ), 1 );
            if( pusDspReadFlag == 0x00 )
            {
                break;
            }
            if( iCount < 3 )
            {
                taskDelay( 10 );
            }
        }
        if( pusDspReadFlag != 0x00 )
        {
            printf( "[ taskMcuSendData ] : Dsp not read data caused error!\n" );
            interTransDInit(); 
            return( 0 );        	
        }      
        hpiWriteDatasAtAddr( giMcuWriteDspAddr, ( unsigned short * )pusSendDataBuf, 7 ); 
        if( semGive( gsemMutexId ) == ERROR )
        {
            printf( "[ taskMcuSendData ] : semGive gsemMutexId error!\n" );
            interTransDInit();
            return( 0 );
        }        
    }
}

int interTransInit( void )
{
    hpiResetDsp();
    if( hpiLoadDsp( "codec.out" ) == 0 )
    {
        printf("[ interTransInit ] : load DSP error!\n");
        interTransDInit();
        return( 0 );
    }
    if( gsemInterruptId == NULL )
    {
        if( ( gsemInterruptId = semBCreate( SEM_EMPTY, SEM_Q_FIFO ) ) == NULL )
        {
            printf( "[ interTransInit ] : create binary semphore gsemInterruptId error!\n" );
            interTransDInit();
            return( 0 );
        }   
    } 
    if( gsemMutexId == NULL )
    {
        if( ( gsemMutexId = semMCreate( SEM_Q_FIFO ) ) == NULL )
        {
            printf( "[ interTransInit ] : create mutex semaphore gsemMutexId error!\n" );
            interTransDInit();
            return( 0 );
        }
    }
    if( giTaskMcuReadDspHandle == -1 )
    {
        if( ( giTaskMcuReadDspHandle =taskSpawn( ( char * )MCU_READ_FROM_DSP_TASK_NAME, 120, 0, 10240, ( FUNCPTR )taskMcuRecvData,
                                            0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ) ) == ERROR )
        {
            printf( "[ interTransInit ] : initialize task \"%s\" error!\n", MCU_READ_FROM_DSP_TASK_NAME );
            interTransDInit();
            return( 0 );
        }
    }
    if( giTaskMcuWriteDspHandle == -1 )
    {
        if( ( giTaskMcuWriteDspHandle =taskSpawn( ( char * )MCU_WRITE_TO_DSP_TASK_NAME, 121, 0, 10240, ( FUNCPTR )taskMcuSendData,
                                            0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ) ) == ERROR )
        {
            printf( "[ interTransInit ] : initialize task \"%s\" error!\n", MCU_WRITE_TO_DSP_TASK_NAME );
            interTransDInit();
            return( 0 );
        }                                            
    }
    if( hpiInterruptConnect( InterMcuRecvDataISR ) == 0 )
    {
        printf("[ interTransInit ] : connect to ISR error!\n");
        interTransDInit();        
        return( 0 );
    }        
    return( 1 );
}

int interTransDInit( void )
{
    unsigned short pusEndCodBuf[ 2 ] = { 0x0001, 0x0102 };
    hpiWriteDatasAtAddr( giMcuWriteDspAddr, ( unsigned short * )pusEndCodBuf, 2 );	
    if( giTaskMcuWriteDspHandle != -1 )
    {
        if( taskDelete( giTaskMcuWriteDspHandle ) == ERROR )
        {
            printf( "[ interTransDInit ] : delete giTaskMcuWriteDspHandle error!\n" );
        }
        giTaskMcuWriteDspHandle = -1;    	
    }
    if( giTaskMcuReadDspHandle != -1 )
    {
        if( taskDelete( giTaskMcuReadDspHandle ) == ERROR )
        {
            printf( "[ interTransDInit ] : delete giTaskMcuReadDspHandle error!\n" );    
        }
        giTaskMcuReadDspHandle = -1;
    }
    if( gsemMutexId != NULL )
    {
        if( semDelete( gsemMutexId ) == ERROR )
        {
            printf("[ interTransDInit ] : delete gsemMutexId error!\n" );
        }
        gsemMutexId = NULL;
    }
    if( gsemInterruptId != NULL )
    {
        if( semDelete( gsemInterruptId ) == ERROR )
        {
            printf( "[ interTransDInit ] : delete gsemInterruptId error!\n" );
        }
        gsemInterruptId = NULL;
    }
    return( 1 );
}

⌨️ 快捷键说明

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