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

📄 rfccontrol.c

📁 IT projecotr reference design.
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************/
/*             TEXAS INSTRUMENTS PROPRIETARY INFORMATION                    */
/*                                                                          */
/*  (c) Copyright, Texas Instruments Incorporated, 2006.                    */
/*      All Rights Reserved.                                                */
/*                                                                          */
/*  Property of Texas Instruments Incorporated. Restricted Rights -         */
/*  Use, duplication, or disclosure is subject to restrictions set          */
/*  forth in TI's program license agreement and associated documentation.   */
/****************************************************************************/

/****************************************************************************/
/* rfcControl.c                                                             */
/*                                                                          */
/* Remote function call controller.                                         */
/****************************************************************************/

#include <string.h>

#include "common.h"
#include "ddp2230_rtos_include.h"
#include "gpio.h"
#include "urt.h"
#include "usb.h"

#include "app_Cfg.h"
#include "global.h"
#include "gpioFunction.h"
#include "taskParm.h"
#include "dbmessage.h"
#include "sysmon.h"
#include "rfcControl.h"
#include "rfcServer.h"
#include "usbIO.h"



/****************************************************************************/
/* Local declarations.                                                      */
/****************************************************************************/

#define TBUFFERSIZE  256                            /* transfer buffer size */



/****************************************************************************/
/* Local data.                                                              */
/****************************************************************************/

static const char rfcTaskName[] = "rfcCtlTask";

static const URTINIT urtTemplate =         /* UART initialization template. */
{
    TRUE,                                                    /* PortEnable  */
    URT_115200,                                              /* BaudRate    */
    URT_8DBITS,                                              /* DataBits    */
    URT_1SBITS,                                              /* StopBits    */
    URT_NONE,                                                /* Parity      */
    URT_OFF,                                                 /* FlowControl */
    URT_RX_ONE_HALF_FULL,                                    /* RXTrigLevel */
    URT_TX_ONE_HALF_FULL,                                    /* TXTrigLevel */
    URT_NONINV_RXDPOL                                        /* RXDPolarity */
};

static URTPORT uartPortRFC;                                /* RFC UART port */

static uint16  dlwcount;                      /* data link write byte count */
static uint16  nRemReadRFC;     /* number of bytes remaining in RFC request */
static uint16  nRemWriteRFC;   /* number of bytes available in RFC response */
static uint16  nWrittenRFC;                      /* number of bytes written */

static uint32  rfcTaskID = ID_INVALID;                       /* RFC task id */

static RFCMESSAGE *pRFCMsg;                     /* request/response message */
static uint08     *pTBuffer;                             /* transfer buffer */

static int  (*readFunc) ( void *pBuffer );                    /* &read func */
static BOOL (*writeFunc)( void *pBuffer, uint32 count );     /* &write func */

static uint08 *pReq;                            /* pointer into RFC request */
static uint08 *pRsp;                           /* pointer into RFC response */


static int  rfcReadSerial ( void *pBuffer );
static BOOL rfcWriteSerial( void *pBuffer, uint32 count );

static int  rfcReadUSB ( void *pBuffer );
static BOOL rfcWriteUSB( void *pBuffer, uint32 count );

static int32 rfcDictFind( uint16 funcID );
static void  rfcExec( void );
static void  rfcMsgReceive( void );
static void  rfcMsgTransmit( void );



/****************************************************************************/
/* Return task information.                                                 */
/****************************************************************************/

void rfc_info( TASKINFO_STRUCT *info )
{
    info -> taskID    = rfcTaskID;
    info -> stackSize = STACK_RFC;
    info -> taskName  = rfcTaskName;
}



/****************************************************************************/
/* Reset and open.                                                          */
/****************************************************************************/

EXEC_CC_ENUM rfc_init( void )
{
    URTINIT uInitStruct;                   /* UART initialization structure */
    BOOL    isFactoryMode;                  /* USB protocol is factory mode */
    BOOL    rfcIsEnabled = FALSE;                          /* assume no RFC */
    uint08  rfcConfig;                                 /* RFC configuration */

                        /****************************************************/
                        /* Examine the port configuration:                  */
                        /* - If a serial port, configure the serial port    */
                        /*   and start the serial interface task.           */
                        /* - If the USB port, start the USB interface task. */
                        /****************************************************/

    rfcConfig     = gpConfiguration -> Peripherals.RemoteFunctionCall;
    isFactoryMode = ( USBMAIN_FACTORYMODE == gpConfiguration -> Peripherals.UsbMode );

    uartPortRFC = rfcConfig >> 5;                        /* port identifier */

                        /****************************************************/
                        /* UART.                                            */
                        /****************************************************/

    if( uartPortRFC < 0x02 )       /* if using a UART for projector control */
    {
        uInitStruct = urtTemplate;                         /* copy template */

        if( rfcConfig & 0x10 )                        /* if HW flow control */
          uInitStruct.FlowControl = URT_HW;

        uInitStruct.BaudRate = (URTBAUD)( rfcConfig & 0x07 );   /* BIT rate */

        if( uartPortRFC == URT_PORT1 )                          /* if PORT1 */
        {
            GPIO_EnableAlternativeFunction( GPIO_UART1_TXD_RXD, TRUE );
            GPIO_SetPinConfig( GIO_UART1TXD, GIOCFG_OUTPUT, TRUE, GIOCFG_ACTIVE );
        }

        URT_EnableFIFOs( uartPortRFC, TRUE );               /* enable FIFOs */

        if( PASS != URT_SetConfig( uartPortRFC, &uInitStruct ))
            dbmsg_trace( DBM_ALWAYS, "Remote function call UART init error\r\n" );

        dbmsg_trace( DBM_ALWAYS, "Opening remote function call on UART\r\n" );
        rfcIsEnabled = TRUE;

        readFunc  = rfcReadSerial;
        writeFunc = rfcWriteSerial;
    }

                        /****************************************************/
                        /* USB.                                             */
                        /****************************************************/

    else if(( uartPortRFC < 0x03 ) && isFactoryMode )       /* if using USB */
    {
        dbmsg_trace( DBM_ALWAYS, "Opening remote function call on USB\r\n" );
        rfcIsEnabled = TRUE;

        readFunc  = rfcReadUSB;
        writeFunc = rfcWriteUSB;
    }

                        /****************************************************/
                        /* Allocate data buffers and start receiver task if */
                        /* RFC is enabled.                                  */
                        /****************************************************/

    if( rfcIsEnabled )
    {
        if( RTA_SUCCESS != RTA_MemRequest( gID_MemPool, TBUFFERSIZE, (void**)&pTBuffer ))
        {
            dbmsg_trace( DBM_ALWAYS, "Can't allocate RFC transfer buffer\r\n" );
            return EXEC_CC_RTOSERR;
        }

        if( RTA_SUCCESS != RTA_MemRequest( gID_MemPool, sizeof( RFCMESSAGE ), (void**)&pRFCMsg ))
        {
            dbmsg_trace( DBM_ALWAYS, "Can't allocate RFC message buffer\r\n" );
            return EXEC_CC_RTOSERR;
        }


        if( RTA_SUCCESS != RTA_TaskCreate( rfcMsgReceive, &rfcTaskID,
            PRIORITY_RFC, STACK_RFC, "rfct", 0 ))
        {
            dbmsg_trace( DBM_ALWAYS, "Can't start remote function call task\r\n" );
            return EXEC_CC_RTOSERR;
        }
    }

    return EXEC_CC_PASS;
}



/****************************************************************************/
/* Serial port Rx function.                                                 */
/*  pBuffer: Pointer to buffer receiving characters.                        */
/*  returns: Number of bytes read.                                          */
/****************************************************************************/

static int rfcReadSerial( void *pBuffer )
{
    int08  cc;                                       /* API completion code */
    uint32 rxCount = 0;                             /* number of bytes read */

    cc = URT_Read( uartPortRFC, TBUFFERSIZE, pBuffer, 1, &rxCount );

    if(( PASS != cc ) && ( URT_TIMEOUT != cc ))
    {
        dbmsg_ftrace( DBM_ALWAYS, "RFC UART read API cc = %d\r\n", cc );
        return 0;
    }

    return (int)rxCount;
}



/****************************************************************************/
/* Serial port Tx function.                                                 */
/*  pBuffer: Pointer to transmit buffer.                                    */
/*    count: Transmit byte count.                                           */
/*  returns: FALSE=Fail; TRUE=Pass.                                         */
/****************************************************************************/

static BOOL rfcWriteSerial( void *pBuffer, uint32 count )
{
    int08  cc;                                       /* API completion code */
    uint32 txCount;                              /* number of bytes written */

    cc = URT_Write( uartPortRFC, count, pBuffer, 50, &txCount );

    if( PASS != cc )
    {
        dbmsg_ftrace( DBM_ALWAYS, "RFC UART write API cc = %d\r\n", cc );
        return FALSE;
    }

    return TRUE;
}



/****************************************************************************/
/* USB Rx function.                                                         */
/*  pBuffer: Pointer to buffer receiving characters.                        */
/*  returns: Number of bytes read.                                          */
/****************************************************************************/

static int rfcReadUSB( void *pBuffer )
{
    return (int)usbIO_GetPeerPacket( pBuffer, 0 );
}



/****************************************************************************/
/* USB Tx function.                                                         */
/*  pBuffer: Pointer to transmit buffer.                                    */
/*    count: Transmit byte count.                                           */
/*  returns: FALSE=Fail; TRUE=Pass.                                         */
/****************************************************************************/

static BOOL rfcWriteUSB( void *pBuffer, uint32 count )
{
    usbIO_PutPeerResponse( (uint16)count, pBuffer );

    return TRUE;
}



/*****************************************************************************/

static void startSend( void )
{
    dlwcount = 0;                               /* no bytes buffered to send */
}

/*****************************************************************************/

static void endSend( void )
{
    (*writeFunc)( pTBuffer, dlwcount );
}

/*****************************************************************************/

static void sendByte( char k )
{
    pTBuffer[ dlwcount++ ] = k;                           /* buffer the byte */

    if( dlwcount == TBUFFERSIZE )                          /* if buffer full */
    {
        endSend();                                            /* send buffer */
        dlwcount = 0;                                      /* reset to start */
    }
}

/*****************************************************************************/

static void translateByte( char k )
{

    if( DLE != k )
    {
        sendByte( k );
    }
    else
    {
        sendByte( DLE );
        sendByte( DLC );
    }
}

/****************************************************************************/
/* Send an RFC response message.                                            */
/****************************************************************************/

static void rfcMsgTransmit( void )
{
    uint32  msgSize = sizeof( pRFCMsg -> h ) + MBUFFERSIZE - nRemWriteRFC;

    uint16  crc;                                                  /* CRC-16 */
    uint08 *p = (uint08*)pRFCMsg;              /* message character pointer */

                        /****************************************************/
                        /* Set response size.                               */
                        /****************************************************/

    pRFCMsg -> h.szRsp = nWrittenRFC;

                        /****************************************************/
                        /* Calculate message CRC.                           */
                        /****************************************************/

    crc = crc16( pRFCMsg, msgSize, CRC_SEED );

                        /****************************************************/
                        /* Start text.                                      */

⌨️ 快捷键说明

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