can_loop.c

来自「canpie 一个can bus的协议栈 - CAN interface fo」· C语言 代码 · 共 555 行 · 第 1/2 页

C
555
字号
   //----------------------------------------------------------------
   // test the channel number
   //



   *pubStatusV = ubStatusT;
           
   return(ubReturnT);
}


//----------------------------------------------------------------------------//
// CpLoopDriverInit()                                                         //
// Initialize the CAN driver                                                  //
//----------------------------------------------------------------------------//
_TvCpStatus CpLoopDriverInit(_U08 ubPhysPortV, _TsCpPort * ptvPortV)
{
   _U08  ubReturnT = CpErr_OK;



   //----------------------------------------------------------------
   // test the channel number
   //
   if( ubPhysPortV >= CP_CHANNEL_MAX) return (CpErr_CHANNEL);




   return(ubReturnT);
}


//----------------------------------------------------------------------------//
// CpLoopDriverRelease()                                                      //
// De-Initialize the CAN driver                                               //
//----------------------------------------------------------------------------//
_TvCpStatus CpLoopDriverRelease(_TsCpPort * ptvPortV)
{
   _U08  ubReturnT = CpErr_OK;




   return(ubReturnT);
}



//----------------------------------------------------------------------------//
// CpLoopHDI()                                                                //
// Fill Hardware Description Interface (HDI) structure                        //
//----------------------------------------------------------------------------//
_U08 CpLoopHDI(_U08 ubChannelV, _TsCpHdi * pHdiV)

{
   //----------------------------------------------------------------
   // test the channel number
   //
   if( ubChannelV >= CP_CHANNEL_MAX) return (CpErr_CHANNEL);

   if(pHdiV == 0L) return(CpErr_GENERIC);
   
   pHdiV->ubSupportFlags   = 0;
   pHdiV->ubControllerType = 0;
   pHdiV->ubIRQNumber      = 0;
   pHdiV->szDriverName     = szDriverNameS;
   pHdiV->szVersionNumber  = szDriverVersS;
   
   return (CpErr_OK);
}


//----------------------------------------------------------------------------//
// CpLoopFilterAll()                                                          //
// Enable / Disable filter for all messages                                   //
//----------------------------------------------------------------------------//
_U08 CpLoopFilterAll(_U08 ubChannelV, _BIT btEnableV)
{
   //----------------------------------------------------------------
   // test the channel number
   //
   if( ubChannelV >= CP_CHANNEL_MAX) return (CpErr_CHANNEL);


   return (CpErr_OK);
}


//----------------------------------------------------------------------------//
// CpLoopFilterMsg()                                                          //
// Enable / Disable filter for a single messages                              //
//----------------------------------------------------------------------------//
_U08 CpLoopFilterMsg(_U08 ubChannelV, _U16 uwIdV, _BIT btEnableV)
{
   //----------------------------------------------------------------
   // test the channel number
   //
   if( ubChannelV >= CP_CHANNEL_MAX) return (CpErr_CHANNEL);


   return (CpErr_OK);
}


//----------------------------------------------------------------------------//
// CpLoopMsgRead()                                                            //
// Read CAN message from controller                                           //
//----------------------------------------------------------------------------//
_TvCpStatus CpLoopMsgRead( _TsCpPort * ptsPortV, _TsCpCanMsg * ptsCanMsgV,
                           _U16 * puwMsgCntV)
{
   _U08  ubReturnT = CpErr_OK;
   _U16  uwMsgCntT = 0;
   

   //----------------------------------------------------------------
   // read data from the receive queue
   //
   uwMsgCntT = can_queue_rcv_pop(ptsPortV, ptsCanMsgV, *puwMsgCntV);
   *puwMsgCntV = uwMsgCntT;

   return(ubReturnT);
}


//----------------------------------------------------------------------------//
// CpLoopMsgTransmit()                                                        //
// Write CAN message to controller                                            //
//----------------------------------------------------------------------------//
_TvCpStatus CpLoopMsgWrite(_TsCpPort * ptsPortV, _TsCpCanMsg * ptsCanMsgV,
                           _U16 * puwMsgCntV)
{
   _U08        ubReturnT = CpErr_OK;
   _TsCpCanMsg tsCanMsgT;

   PK_DBG(  "copy data to log. I/F=%d , phy. I/F=%d", 
            ptsPortV->slLogIf, ptsPortV->slPhyIf);
            
   //----------------------------------------------------------------
   // copy data from the transmit queue into the receive queue
   //
   can_queue_trm_pop(ptsPortV, &tsCanMsgT, 1);
   
   PK_DBG(  "Message: ID=%04X DLC=%1d Data=%02X %02X", 
            tsCanMsgT.tuMsgId.ulExt, tsCanMsgT.ubMsgDLC, 
            tsCanMsgT.tuMsgData.aubByte[0],
            tsCanMsgT.tuMsgData.aubByte[1]);
   
   can_queue_rcv_push(ptsPortV, &tsCanMsgT, 1);

   return(ubReturnT);
}


//----------------------------------------------------------------------------//
// CpLoopRegRead()                                                            //
// Read one byte from specified CAN controller register                       //
//----------------------------------------------------------------------------//
_U08 CpLoopRegRead(_U08 ubChannelV, _U16 uwRegAdrV, _U08 * pubValueV)
{
   //----------------------------------------------------------------
   // test the channel number
   //
   if( ubChannelV >= CP_CHANNEL_MAX) return (CpErr_CHANNEL);


   return (CpErr_OK);
}


//----------------------------------------------------------------------------//
// CpLoopRegWrite()                                                           //
// Write one byte to specified CAN controller register                        //
//----------------------------------------------------------------------------//
_U08 CpLoopRegWrite(_U08 ubChannelV, _U16 uwRegAdrV, _U08 ubValueV)
{
   //----------------------------------------------------------------
   // test the channel number
   //
   if( ubChannelV >= CP_CHANNEL_MAX) return (CpErr_CHANNEL);


   return (CpErr_OK);
}


/*----------------------------------------------------------------------------*\
** module init and exit code                                                  **
** function implementation                                                    **
\*----------------------------------------------------------------------------*/


//----------------------------------------------------------------
// Setup the CpHal_s structure
//
static _TsCpHal tsMcLoopHalS = {
   pfnBitrate        : CpLoopBitrate,
   pfnBufferGetData  : 0L,
   pfnBufferGetDlc   : 0L,
   pfnBufferInit     : 0L,
   pfnBufferSetData  : 0L,
   pfnBufferSetDlc   : 0L,
   pfnBufferSend     : 0L,
   pfnBufferRelease  : 0L,
   pfnCanMode        : CpLoopCanMode,
   pfnCanStatus      : CpLoopCanStatus,
   pfnDriverInit     : CpLoopDriverInit,
   pfnDriverRelease  : CpLoopDriverRelease,
   pfnFilterAll      : 0L,
   pfnFilterMsg      : 0L,
   pfnHwDescription  : CpLoopHDI,

   pfnMsgRead        : CpLoopMsgRead,
   pfnMsgWrite       : CpLoopMsgWrite,
   pfnRegRead        : 0L,
   pfnRegWrite       : 0L

};





//----------------------------------------------------------------------------//
// init_module()                                                              //
//                                                                            //
//----------------------------------------------------------------------------//
static int __init CpLoopInit(void)
{
   int   slRegisterT;
   int   slCanIfIdxT;
   
      
   //----------------------------------------------------------------
   // print some info who we are
   //       
   PK_INF("CANpie loop driver version %s", CP_MODULE_VERSION);

   
   //----------------------------------------------------------------
   // clear the physical-to-logical CAN IF table
   //       
   for(slCanIfIdxT = 0; slCanIfIdxT < MAX_PHY_IF; slCanIfIdxT++)
   {
      aslCanLogIf[slCanIfIdxT] = -1;
   }
   
   slRegisterT = can_register_driver(port, 0, &tsMcLoopHalS);
   if(slRegisterT < 0)
   {
      return(-ENODEV);
   }
   
   aslCanLogIf[0] = slRegisterT;
   
   port = slRegisterT;
   PK_INF("Logic port number: %d", port);
   return(0);
}


//----------------------------------------------------------------------------//
// cleanup_module()                                                           //
//                                                                            //
//----------------------------------------------------------------------------//
static void __exit CpLoopExit(void)
{
   can_unregister_driver(0);
   PK_INF("CANpie Kernel driver removed");
}


module_init(CpLoopInit);
module_exit(CpLoopExit);

⌨️ 快捷键说明

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