Пример #1
0
/**
* \brief         This function will connect the tool with hardware. This will
*                establish the data link between the application and hardware.
* \param[in]     bConnect TRUE to Connect, FALSE to Disconnect
* \return        Returns defERR_OK if successful otherwise corresponding Error code.
* \authors       Arunkumar Karri
* \date          07.10.2011 Created
*/
static int nConnect(BOOL bConnect)
{
    int nReturn = -1;
    XLstatus xlStatus;

    if (!sg_bIsConnected && bConnect) // Disconnected and to be connected
    {
        /* Set the permission mask for all channel access */
        g_xlPermissionMask = g_xlChannelMask;


        xlStatus = xlOpenPort(&g_xlPortHandle[0], g_AppName, g_xlChannelMask, &g_xlPermissionMask,
                              RX_QUEUE_SIZE, XL_INTERFACE_VERSION, XL_BUS_TYPE_LIN);

        if (XL_SUCCESS == xlStatus)
        {
            //Calculate connected Timestamp
            QueryPerformanceCounter(&sg_QueryTickCount);
            // Get frequency of the performance counter
            QueryPerformanceFrequency(&sg_lnFrequency);
            // Convert it to time stamp with the granularity of hundreds of microsecond
            //if (sg_QueryTickCount.QuadPart * 10000 > sg_QueryTickCount.QuadPart)
            if ((sg_QueryTickCount.QuadPart * 10000) > sg_lnFrequency.QuadPart)
            {
                sg_TimeStamp = (sg_QueryTickCount.QuadPart * 10000) / sg_lnFrequency.QuadPart;
            }
            else
            {
                sg_TimeStamp = (sg_QueryTickCount.QuadPart / sg_lnFrequency.QuadPart) * 10000;
            }

            /* Transit into 'CREATE TIME MAP' state */
            sg_byCurrState = CREATE_MAP_TIMESTAMP;
            vMapDeviceChannelIndex();
            sg_bIsConnected = bConnect;

            xlFlushReceiveQueue(g_xlPortHandle[0]);
            /* Set LIN channel Params */
            nReturn = nSetBaudRate();
        }
        else
        {
            return S_FALSE;
        }

    }
    else if (sg_bIsConnected && !bConnect) // Connected & to be disconnected
    {
        sg_bIsConnected = bConnect;
        Sleep(0); // Let other threads run for once
        nReturn = nDisconnectFromDriver();
    }
    else
    {
        nReturn = defERR_OK;
    }
    if ( sg_bIsConnected )
    {
        InitializeCriticalSection(&sg_CritSectForWrite);

    }
    else
    {
        DeleteCriticalSection(&sg_CritSectForWrite);
    }

    return nReturn;
}
Пример #2
0
////////////////////////////////////////////////////////////////////////////
// demoInitDriver
// initializes the driver with one port and all founded channels which
// have a connected CAN cab/piggy.
//
////////////////////////////////////////////////////////////////////////////
XLstatus demoInitDriver(XLaccess *pxlChannelMaskTx, unsigned char *pxlChannelIndex)
{
   XLstatus          xlStatus;
   XLaccess          xlChannelMaskTx = 0;
   unsigned int      i;
  
  
   // ------------------------------------
   // open the driver
   // ------------------------------------
   xlStatus = xlOpenDriver ();
  
   // ------------------------------------
   // get/print the hardware configuration
   // ------------------------------------
   if (XL_SUCCESS == xlStatus)
   {
      xlStatus = xlGetDriverConfig(&g_xlDrvConfig);
   }
  
   if (XL_SUCCESS == xlStatus)
   {
      demoPrintConfig();
    
      printf("Usage: J1939_stack_test <BaudRate> <ApplicationName> <Identifier>\n\n");
    
      // ------------------------------------
      // select the wanted channels
      // ------------------------------------
      g_xlChannelMask = 0;

      for (i=0; i < g_xlDrvConfig.channelCount; i++)
      {
         // we take all hardware we found and
         // check that we have only CAN cabs/piggy's
         // at the moment there is no VN8910 XLAPI support!
         if (g_xlDrvConfig.channel[i].channelBusCapabilities & XL_BUS_ACTIVE_CAP_CAN)
         { 
            if (!*pxlChannelMaskTx)
            {
               *pxlChannelMaskTx = g_xlDrvConfig.channel[i].channelMask;
               *pxlChannelIndex  = g_xlDrvConfig.channel[i].channelIndex;
            }

            g_xlChannelMask |= g_xlDrvConfig.channel[i].channelMask;
         }
      }
    
      if (!g_xlChannelMask)
      {
         printf("ERROR: no available channels found! (e.g. no CANcabs...)\n\n");
         xlStatus = XL_ERROR;
      }
   }

   g_xlPermissionMask = g_xlChannelMask;
  
   // ------------------------------------
   // open ONE port including all channels
   // ------------------------------------
   if (XL_SUCCESS == xlStatus)
   {
      xlStatus = xlOpenPort(&g_xlPortHandle, g_AppName, g_xlChannelMask, &g_xlPermissionMask, RX_QUEUE_SIZE, XL_INTERFACE_VERSION, XL_BUS_TYPE_CAN);
      printf("- OpenPort         : CM=0x%I64x, PH=0x%02X, PM=0x%I64x, %s\n", 
      g_xlChannelMask, g_xlPortHandle, g_xlPermissionMask, xlGetErrorString(xlStatus));
   }

   if ((XL_SUCCESS == xlStatus) && (XL_INVALID_PORTHANDLE != g_xlPortHandle))
   { 
      // ------------------------------------
      // if we have permission we set the
      // bus parameters (baudrate)
      // ------------------------------------
      if (g_xlChannelMask == g_xlPermissionMask)
      {
         xlStatus = xlCanSetChannelBitrate(g_xlPortHandle, g_xlChannelMask, g_BaudRate);
         printf("- SetChannelBitrate: baudr.=%u, %s\n",g_BaudRate, xlGetErrorString(xlStatus));
      } 
      else
      {
         printf("-                  : we have NO init access!\n");
      } 
   }
   else
   {
      xlClosePort(g_xlPortHandle);
      g_xlPortHandle = XL_INVALID_PORTHANDLE;
      xlStatus = XL_ERROR;
   }
  
   return xlStatus;
}