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

📄 canpie_hal.c.svn-base

📁 canpie 一个can bus的协议栈 - CAN interface for embedded control - CAN interface for PC (without local
💻 SVN-BASE
📖 第 1 页 / 共 4 页
字号:
        //----------------------------------------------------------------   // check if the logical interface number is in range   //      if( (slLogIfV > CP_CHANNEL_MAX - 1) || (slLogIfV < 0) )   {      return;   }      //----------------------------------------------------------------   // clear the complete HAL structure for that logic interface   //   memset(&atsCpHalS[slLogIfV], 0x00, sizeof(_TsCpHal));      //----------------------------------------------------------------   // interface not assigned yet   //   atsCpHalS[slLogIfV].tsCanPort.slLogIf = -1;   atsCpHalS[slLogIfV].tsCanPort.slPhyIf = -1;      atsCpHalS[slLogIfV].slStatus = CP_HAL_DRV_NONE;      //----------------------------------------------------------------   // mark all message queues as not assigned   //   for(slQueueCntT = 0; slQueueCntT < CP_QUEUE_MAX; slQueueCntT++)   {      atsCpHalS[slLogIfV].atsMsgQueue[slQueueCntT].tsCanPort.slQueue = -1;   }}//----------------------------------------------------------------------------//// can_print_frame()                                                          //// print CAN frame to buffer                                                  ////----------------------------------------------------------------------------//#ifdef  CP_DEBUGstatic void can_print_frame(_TsCpCanMsg * ptsCanMsgV, char * szBufferV){   int   slPosT;   _U08  ubDataCntT;   _U08  ubDlcT;          //----------------------------------------------------------------   // Format is:   // ID(e): ----- DLC: - Data: -- -- .. -- (data frame,  Ext. ID)   // ID(s):   --- DLC: - Data: -- -- .. -- (data frame,  Std. ID)   // ID(s):   --- DLC: - RTR               (remote frame,  Std. ID)   // DLC is set to x if value > 8   //   if(CpMsgIsExtended(ptsCanMsgV))   {      slPosT = sprintf( &szBufferV[0], "ID(e): 0x%08X ",                        CpMsgGetExtId(ptsCanMsgV));   }   else   {      slPosT = sprintf( &szBufferV[0], "ID(s):      0x%03X ",                        CpMsgGetStdId(ptsCanMsgV));   }   //----------------------------------------------------------------   //  print DLC   //     ubDlcT = CpMsgGetDlc(ptsCanMsgV);   if(ubDlcT > 8)   {      slPosT += sprintf( &szBufferV[slPosT], "DLC: x ");      ubDlcT = 8; // limit to highest possible value   }   else   {      slPosT += sprintf( &szBufferV[slPosT], "DLC: %d ", ubDlcT);   }      //----------------------------------------------------------------   // check for remote frame   //      if(CpMsgIsRemote(ptsCanMsgV))   {      sprintf( &szBufferV[slPosT], "RTR");   }   else   {      slPosT += sprintf( &szBufferV[slPosT], "Data: ");      for(ubDataCntT = 0; ubDataCntT < ubDlcT; ubDataCntT++)      {         slPosT += sprintf( &szBufferV[slPosT], "%02X ",                            CpMsgGetData(ptsCanMsgV,ubDataCntT));      }   }}#endif//----------------------------------------------------------------------------//// can_register_driver()                                                      ////                                                                            ////----------------------------------------------------------------------------//int can_register_driver(int slLogIfV, int slPhyIfV, _TsCpHal * ptsCpHalV){   int         slCanIfIdxT;   // iterate over all CAN interface numbers   int         slCanIfSelT;   // assigned logic CAN interface number   int         slQueueCntT;   // counter for message queues   _TsCpHal *  ptsHalT;       // pointer to HAL entry            //----------------------------------------------------------------   // test the logic interface number   //   if( slLogIfV >= CP_CHANNEL_MAX)    {      PK_INF("Log. CAN-I/F number out of range (MAX=%d)", (CP_CHANNEL_MAX -1));      return(-CpErr_CHANNEL);   }   //----------------------------------------------------------------   // test the physical interface number   //   if( slPhyIfV >= CP_CHANNEL_MAX)    {      PK_INF("Phy. CAN-I/F number out of range (MAX=%d)", (CP_CHANNEL_MAX -1));      return(-CpErr_CHANNEL);   }      PK_DBG(eDBG_PORT, "register log. CAN-I/F %d", slLogIfV);   //----------------------------------------------------------------   // test the _TsCpHal structure   //   if( ptsCpHalV == 0L) return(-CpErr_GENERIC);      //-----------------------------------------------------------------   // does the driver request the next free port?   //   if(slLogIfV < 0)   {      ptsHalT = 0L;                 // no free slot yet      slCanIfIdxT = CP_CHANNEL_1;   // start at first port            //---------------------------------------------------------      // search in all possible entries for a free slot      //      while(slCanIfIdxT < CP_CHANNEL_MAX)      {         //-------------------------------------------------         // the port is not assigned if the structure         // element 'slStatus' is CP_HAL_DRV_NONE         //         if(atsCpHalS[slCanIfIdxT].slStatus == CP_HAL_DRV_NONE)         {            //-----------------------------------------            // we found a free slot here            //            ptsHalT = &(atsCpHalS[slCanIfIdxT]);            break;         }         slCanIfIdxT++;      }         //---------------------------------------------------------      // If 'ptsHalT' is still set to 0L, we could not find      // a free slot for the driver      //      if(ptsHalT == 0L)       {         PK_INF("No free log. CAN-I/F available");         return(-CpErr_CHANNEL);      }      slCanIfSelT = slCanIfIdxT;   }   else   {      //----------------------------------------------------      // At this position we try to assign the driver at a       // specific slot, given by the index 'slLogIfV'. We       // have to check if the requested port is free for       // allocation.      //      slCanIfSelT = slLogIfV;      if(atsCpHalS[slCanIfSelT].slStatus != CP_HAL_DRV_NONE)      {         PK_INF("Log. CAN-/IF %d already assigned", slCanIfSelT);         return(-CpErr_CHANNEL);      }      ptsHalT = &(atsCpHalS[slCanIfSelT]);   }      //----------------------------------------------------------------   // copy the HAL structure from the driver and store   // the assigned port number   //   memcpy(ptsHalT, ptsCpHalV, sizeof(_TsCpHal));   ptsHalT->slStatus = CP_HAL_DRV_OK;   ptsHalT->tsCanPort.slLogIf = slCanIfSelT;   ptsHalT->tsCanPort.slPhyIf = slPhyIfV;         //----------------------------------------------------------------   // mark all message queues as not assigned   //   for(slQueueCntT = 0; slQueueCntT < CP_QUEUE_MAX; slQueueCntT++)   {      ptsHalT->atsMsgQueue[slQueueCntT].tsCanPort.slQueue = -1;   }      //----------------------------------------------------------------   // add this device to the /proc interface   //   can_proc_dev_add(slCanIfSelT);      PK_INF("Log. CAN-I/F %d has been assigned", slCanIfSelT);     return(slCanIfSelT);}//----------------------------------------------------------------------------//// can_unregister_driver()                                                    ////                                                                            ////----------------------------------------------------------------------------//void can_unregister_driver(int slLogIfV){   //----------------------------------------------------------------   // test the port number   //   if( slLogIfV >= CP_CHANNEL_MAX) return;   if( slLogIfV <  0) return;   PK_DBG(eDBG_PORT, "unregister log. CAN-I/F %d", slLogIfV);   can_proc_dev_remove(slLogIfV);   can_clear_hal_struct(slLogIfV);}//----------------------------------------------------------------------------//// can_get_hal()                                                              ////                                                                            ////----------------------------------------------------------------------------//_TsCpHal * can_get_hal(int slLogIfV){   //----------------------------------------------------------------   // test the port number   //   if( slLogIfV >= CP_CHANNEL_MAX)    {      return(0L);   }   return( &(atsCpHalS[slLogIfV]) );}//----------------------------------------------------------------------------//// can_get_port()                                                             ////                                                                            ////----------------------------------------------------------------------------//_TsCpPort * can_get_port(int slLogIfV){   //----------------------------------------------------------------   // test the port number   //   if( slLogIfV >= CP_CHANNEL_MAX)    {      return(0L);   }   return( &(atsCpHalS[slLogIfV].tsCanPort) );}//----------------------------------------------------------------------------//// init_module()                                                              //////----------------------------------------------------------------------------//static int __init ModInit(void){   _U08  ubCanIfNumT;   //----------------------------------------------------------------   // print some info who we are   //          PK_INF("CANpie version %s", CP_MODULE_VERSION);   PK_INF("Starting kernel driver");   #ifdef  CP_DEBUG   PK_INF("Debug support enabled, debug level=%d", uwCpDebugG);   #endif      //----------------------------------------------------------------   // clear the structure for the CAN Hardware Abstraction layer   //   for(ubCanIfNumT = 0; ubCanIfNumT < CP_CHANNEL_MAX; ubCanIfNumT++)   {      can_clear_hal_struct(ubCanIfNumT);   }   //-----------------------------------------------------------------   // socket support (AF_CAN)   //   #if CP_CONFIG_SOCKET == 1   can_socket_init();   #endif         //-----------------------------------------------------------------   // support for /dev/can character device driver   //   #if CP_CONFIG_DEV == 1   cp_dev_init();   #endif      //-----------------------------------------------------------------   // support for /proc filesystem   //   can_proc_init();      return(0);}//----------------------------------------------------------------------------//// cleanup_module()                                                           //////----------------------------------------------------------------------------//static void __exit ModExit(void){   //-----------------------------------------------------------------   // socket support (AF_CAN)   //   #if CP_CONFIG_SOCKET == 1   can_socket_cleanup();   #endif         //-----------------------------------------------------------------   // support for /dev/can character device driver   //   #if CP_CONFIG_DEV == 1   cp_dev_cleanup();   #endif      //-----------------------------------------------------------------   // remove /proc/net/can filesystem   //   can_proc_cleanup();               PK_INF("CANpie Kernel driver removed");}module_init( ModInit );module_exit( ModExit );

⌨️ 快捷键说明

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