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

📄 mtel.c

📁 Zigbee2006入门(源代码+文档讲解+系统推荐)
💻 C
📖 第 1 页 / 共 4 页
字号:
/*********************************************************************
    Filename:       MTEL.c
    Revised:        $Date: 2007-05-16 11:21:09 -0700 (Wed, 16 May 2007) $
    Revision:       $Revision: 14313 $

    Description:

        MonitorTest Event Loop functions.  Everything in the
        MonitorTest Task (except the serial driver).

    Notes:

    Copyright (c) 2006 by Texas Instruments, Inc.
    All Rights Reserved.  Permission to use, reproduce, copy, prepare
    derivative works, modify, distribute, perform, display or sell this
    software and/or its documentation for any purpose is prohibited
    without the express written consent of Texas Instruments, Inc.
*********************************************************************/

#if defined( MT_TASK )

/*********************************************************************
 * INCLUDES
 */
#include "ZComDef.h"
#include "OnBoard.h"
#include "OSAL.h"
#include "OSAL_Memory.h"
#include "OSAL_Nv.h"
#include "MTEL.h"
#include "DebugTrace.h"
#include "ZMAC.h"

#if !defined ( NONWK )
  #include "NLMEDE.h"
  #include "nwk_bufs.h"
  #include "ZDObject.h"
  #include "ssp.h"
  #include "nwk_util.h"
#endif

#if defined( MT_MAC_FUNC ) || defined( MT_MAC_CB_FUNC )
  #include "MT_MAC.h"
#endif
#if defined( MT_NWK_FUNC ) || defined( MT_NWK_CB_FUNC )
  #include "MT_NWK.h"
  #include "nwk.h"
  #include "nwk_bufs.h"
#endif
#if defined( MT_AF_FUNC ) || defined( MT_AF_CB_FUNC )
  #include "MT_AF.h"
#endif
#if defined( MT_USER_TEST_FUNC )
  #include "AF.h"
#endif
#if defined( MT_ZDO_FUNC )
  #include "MT_ZDO.h"
#endif
#if defined (MT_SAPI_FUNC)
	#include "MT_SAPI.h"
#endif
#if defined( APP_TP )
 #include "TestProfile.h"
#endif
#if defined( APP_TP2 )
 #include "TestProfile2.h"
#endif

#if defined(APP_TGEN)
  #include "TrafficGenApp.h"
#endif
#if defined(APP_DEBUG)
	#include "DebugApp.h"
#endif
#if defined (NWK_TEST)
	#include "HWTTApp.h"
#endif

/* HAL */
#include "hal_uart.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_mailbox.h"
#include "SPIMgr.h"

/*********************************************************************
 * MACROS
 */
#define MTEL_DEBUG_INFO( nParams, p1, p2, p3 ) DEBUG_INFO( COMPID_MTEL, nParams, p1, p2, p3 )

#if defined( EXTERNAL_RAM )
  #define IS_MEM_VALID( Addr )  \
        /* Check for valid internal RAM address. */\
    ( ( (((Addr) >= MCU_RAM_BEG) && ((Addr) <= MCU_RAM_END)) ||  \
        /* Check for valid external RAM address. */\
        (((Addr) >= EXT_RAM_BEG) && ((Addr) <= EXT_RAM_END)) ) ? TRUE : FALSE )
#else
  #define IS_MEM_VALID( Addr )  \
        /* Check for valid internal RAM address. */\
    ( ( ((Addr) >= MCU_RAM_BEG) && ((Addr) <= MCU_RAM_END) ) ? TRUE : FALSE )
#endif

/*********************************************************************
 * CONSTANTS
 */

#ifdef ZPORT
const char *MTVersionString[] = {"1.00 (F8W1.4.2-ZP)", "1.10 (F8W1.4.2-ZP)"};
#else
const char *MTVersionString[] = {"1.00 (F8W1.4.2)", "1.10 (F8W1.4.2)"};
#endif

/*********************************************************************
 * TYPEDEFS
 */

/*********************************************************************
 * GLOBAL VARIABLES
 */
byte MT_TaskID;
byte debugThreshold;
byte debugCompId;

UINT16 save_cmd;

//DEBUG
uint32 longvar;
uint16 *temp_glob_ptr1;
uint16 *temp_glob_ptr2;

/*********************************************************************
 * EXTERNAL VARIABLES
 */

/*********************************************************************
 * EXTERNAL FUNCTIONS
 */
extern unsigned int mac_sim_eventLoop( void );

#ifdef MACSIM
  // Used to pass Zignet message
  extern void MACSIM_TranslateMsg( byte *buf, byte bLen );
#endif

/*********************************************************************
 * LOCAL VARIABLES
 */

/*********************************************************************
 * LOCAL FUNCTIONS
 */
void MT_MsgQueueInit( void );
void MT_ProcessCommand( mtOSALSerialData_t *msg );
void MT_ProcessSerialCommand( byte *msg );
byte MT_RAMRead( UINT16 addr, byte *pData );
byte MT_RAMWrite( UINT16 addr , byte val );
void MT_ProcessDebugMsg( mtDebugMsg_t *pData );
void MT_ProcessDebugStr( mtDebugStr_t *pData );
byte MT_SetDebugThreshold( byte comp_id, byte threshold );
void MT_SendErrorNotification( byte err );
void MT_ResetMsgQueue( void );
byte MT_QueueMsg( byte *msg , byte len );
void MT_ProcessQueue( void );
void MT_SendSPIRespMsg( byte ret, uint16 cmd_id, byte msgLen, byte respLen);
void MT_Reset(byte typID);
byte MT_ProcessSetNV( byte *pData );
void MT_ProcessGetNV( byte *pData );
void MT_ProcessGetNvInfo( void );
void MT_ProcessGetDeviceInfo( void );
byte MTProcessAppMsg( byte *pData, byte len );
void MTProcessAppRspMsg( byte *pData, byte len );

#if (defined HAL_LED) && (HAL_LED == TRUE)
byte MTProcessLedControl( byte *pData );
#endif

#if defined ( MT_USER_TEST_FUNC )
void MT_ProcessAppUserCmd( byte *pData );
#endif

/*********************************************************************
 * @fn      MT_TaskInit
 *
 * @brief
 *
 *   MonitorTest Task Initialization.  This function is put into the
 *   task table.
 *
 * @param   byte task_id - task ID of the MT Task
 *
 * @return  void
 *
 *********************************************************************/
void MT_TaskInit( byte task_id )
{
  MT_TaskID = task_id;

  debugThreshold = 0;
  debugCompId = 0;

  // Initialize the Serial port
  SPIMgr_Init();

} /* MT_TaskInit() */

#ifdef ZTOOL_PORT
/*********************************************************************
 * @fn      MT_IndReset()
 *
 * @brief   Sends a ZTOOL "reset response" message.
 *
 * @param   None
 *
 * @return  None
 *
 *********************************************************************/
void MT_IndReset( void )
{

  byte rsp = 0;  // Reset type==0 indicates Z-Stack reset

  // Send out Reset Response message
  MT_BuildAndSendZToolResponse( (SPI_0DATA_MSG_LEN + sizeof( rsp )),
                                (SPI_RESPONSE_BIT | SPI_CMD_SYS_RESET),
                                sizeof( rsp ), &rsp );
}
#endif

/*********************************************************************
 * @fn      MT_ProcessEvent
 *
 * @brief
 *
 *   MonitorTest Task Event Processor.  This task is put into the
 *   task table.
 *
 * @param   byte task_id - task ID of the MT Task
 * @param   UINT16 events - event(s) for the MT Task
 *
 * @return  void
 */
UINT16 MT_ProcessEvent( byte task_id, UINT16 events )
{
  uint8 *msg_ptr;

  // Could be multiple events, so switch won't work

  if ( events & SYS_EVENT_MSG )
  {
    while ( (msg_ptr = osal_msg_receive( MT_TaskID )) )
    {
      MT_ProcessCommand( (mtOSALSerialData_t *)msg_ptr );
    }

    // Return unproccessed events
    return (events ^ SYS_EVENT_MSG);
  }

#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
  if ( events & MT_ZTOOL_SERIAL_RCV_BUFFER_FULL )
  {
    // Do sometype of error processing
    MT_SendErrorNotification(RECEIVE_BUFFER_FULL);

    // Return unproccessed events
    return (events ^ MT_ZTOOL_SERIAL_RCV_BUFFER_FULL);
  }
#endif

  // Discard or make more handlers
  return 0;

} /* MT_ProcessEvent() */

#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
/*********************************************************************
 * @fn      MT_BuildSPIMsg
 *
 * @brief
 *
 *   Format an SPI message.
 *
 * @param   UINT16 cmd - command id
 * @param   byte *msg - pointer to message buffer
 * @param   byte dataLen - length of data field
 * @param   byte *pData - pointer to data field
 *
 * @return  void
 */
void MT_BuildSPIMsg( UINT16 cmd, byte *msg, byte dataLen, byte *pData )
{
  byte *msgPtr;

  *msg++ = SOP_VALUE;

  msgPtr = msg;

  *msg++ = (byte)(HI_UINT16( cmd ));
  *msg++ = (byte)(LO_UINT16( cmd ));

  if ( pData )
  {
    *msg++ = dataLen;

    msg = osal_memcpy( msg, pData, dataLen );
  }
  else
    *msg++ = 0;

  *msg = SPIMgr_CalcFCS( msgPtr, (byte)(3 + dataLen) );
}
#endif

#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
/*********************************************************************
 * @fn      MT_BuildAndSendZToolResponse
 *
 * @brief
 *
 *   Build and send a ZTOOL msg
 *
 * @param   byte err
 *
 * @return  void
 */
void MT_BuildAndSendZToolResponse( byte msgLen, uint16 cmd,
                                   byte dataLen, byte *pData )
{
  byte *msg_ptr;

  // Get a message buffer to build response message
  msg_ptr = osal_mem_alloc( msgLen );
  if ( msg_ptr )
  {
#ifdef SPI_MGR_DEFAULT_PORT
    MT_BuildSPIMsg( cmd, msg_ptr, dataLen, pData );
    HalUARTWrite ( SPI_MGR_DEFAULT_PORT, msg_ptr, msgLen );
#endif
    osal_mem_free( msg_ptr );
  }
}
#endif

#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
/*********************************************************************
 * @fn      MT_BuildAndSendZToolCB
 *
 * @brief
 *
 *   Build and send a ZTOOL Callback msg
 *
 * @param   len - length of data portion of the message
 *
 * @return  void
 */
void MT_BuildAndSendZToolCB( uint16 callbackID, byte len, byte *pData )
{
  byte msgLen;
  mtOSALSerialData_t *msgPtr;
  byte *msg;

  msgLen = sizeof ( mtOSALSerialData_t ) + SPI_0DATA_MSG_LEN + len;

  msgPtr = (mtOSALSerialData_t *)osal_msg_allocate( msgLen );
  if ( msgPtr )
  {
    msgPtr->hdr.event = CB_FUNC;
    msgPtr->msg = (uint8 *)(msgPtr+1);
    msg = msgPtr->msg;

    //First byte is used as the event type for MT
    *msg++ = SOP_VALUE;
    *msg++ = HI_UINT16( callbackID );
    *msg++ = LO_UINT16( callbackID );
    *msg++ = len;

    //Fill up the data bytes
    osal_memcpy( msg, pData, len );

    osal_msg_send( MT_TaskID, (uint8 *)msgPtr );
  }
}
#endif

/*********************************************************************
 * @fn      MT_ProcessCommand
 *
 * @brief
 *
 *   Process Event Messages.
 *
 * @param   byte *msg - pointer to event message
 *
 * @return
 */
void MT_ProcessCommand( mtOSALSerialData_t *msg )
{
  byte deallocate;
#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
  byte *msg_ptr;
  byte len;

  // A little setup for AF, CB_FUNC and MT_SYS_APP_RSP_MSG
  msg_ptr = msg->msg;
#endif // ZTOOL

  deallocate = true;

  // Use the first byte of the message as the command ID
  switch ( msg->hdr.event )
  {
#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
    case CMD_SERIAL_MSG:
      MT_ProcessSerialCommand( msg->msg );
      break;

    case CMD_DEBUG_MSG:
      MT_ProcessDebugMsg( (mtDebugMsg_t *)msg );
      break;

    case CMD_DEBUG_STR:
      MT_ProcessDebugStr( (mtDebugStr_t *)msg );
      break;

    case CB_FUNC:
      /*
        Build SPI message here instead of redundantly calling MT_BuildSPIMsg
        because we have copied data already in the allocated message
      */

      /* msg_ptr is the beginning of the intended SPI message */
      len = SPI_0DATA_MSG_LEN + msg_ptr[DATALEN_FIELD];

      /*
        FCS goes to the last byte in the message and is calculated over all
        the bytes except FCS and SOP
      */
      msg_ptr[len-1] = SPIMgr_CalcFCS( msg_ptr + 1 , (byte)(len-2) );

#ifdef SPI_MGR_DEFAULT_PORT
      HalUARTWrite ( SPI_MGR_DEFAULT_PORT, msg_ptr, len );
#endif
      break;

#if !defined ( NONWK )
    case MT_SYS_APP_RSP_MSG:
      len = SPI_0DATA_MSG_LEN + msg_ptr[DATALEN_FIELD];
      MTProcessAppRspMsg( msg_ptr, len );
      break;
#endif  // NONWK
#endif  // ZTOOL

    default:
      break;
  }

  if ( deallocate )
  {
    osal_msg_deallocate( (uint8 *)msg );
  }
}

#if defined (ZTOOL_P1) || defined (ZTOOL_P2)
/*********************************************************************
 * @fn      MT_ProcessDebugMsg
 *
 * @brief
 *

⌨️ 快捷键说明

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