📄 wncandevio.c
字号:
devCfg->info.version.major = verInfo->major;
devCfg->info.version.minor = verInfo->minor;
devCfg->info.ctrlType = CAN_GetControllerType (canDev);
devCfg->info.xtalfreq = CAN_GetXtalFreq (canDev);
devCfg->info.numChannels = CAN_GetNumChannels (canDev);
devCfg->info.baudRate = CAN_GetBaudRate (canDev,
&(devCfg->info.samplePoint));
status = OK;
}
if ( (devCfg->flags & WNCAN_CFG_GBLFILTER) == WNCAN_CFG_GBLFILTER)
{
devCfg->filter.mask = CAN_GetGlobalRxFilter (canDev,
devCfg->filter.extended);
status = OK;
}
if ( (devCfg->flags & WNCAN_CFG_BITTIMING) == WNCAN_CFG_BITTIMING)
{
/* WNCAN_CFG_BITTIMING not applicable for GET */
devCfg->bittiming.tseg1=canDev->pCtrl->tseg1;
devCfg->bittiming.tseg2=canDev->pCtrl->tseg2;
devCfg->bittiming.brp=canDev->pCtrl->brp;
devCfg->bittiming.sjw=canDev->pCtrl->sjw;
devCfg->bittiming.oversample=canDev->pCtrl->samples;
status = OK;
}
break;
case WNCAN_REG_SET:
regCfg = (WNCAN_REG *) arg;
status = CAN_WriteReg (canDev,
regCfg->offset,
regCfg->pData,
regCfg->length);
break;
case WNCAN_REG_GET:
regCfg = (WNCAN_REG *) arg;
status = CAN_ReadReg (canDev,
regCfg->offset,
regCfg->pData,
regCfg->length);
break;
}
/* unlock device */
semGive(wncDrv->mutex);
return status;
}
/************************************************************************
*
* wncUtilIoctlChannelCmds - utility routine to process CAN channel ioctl()
* commands
*
* This routine performs the processing for CAN channel commands specified
* via ioctl()
*
* RETURNS: OK or ERROR
*
* ERRNO: N/A
*
*/
LOCAL STATUS wncUtilIoctlChannelCmds
(
WNCAN_DEVIO_FDINFO *fdInfo, /* pointer to DevIO file descriptor */
int command, /* command function code */
int arg /* arbitrary argument */
)
{
WNCAN_DEVIO_DRVINFO* wncDrv = NULL;
WNCAN_DEVICE* canDev = NULL;
STATUS status = ERROR; /* pessimistic */
WNCAN_CHNCONFIG* chnCfg = NULL;
if (fdInfo == NULL)
{
#if DEVIO_DEBUG
logMsg("wncUtilIoctlChannelCmds() ERROR: pointer to DevIO file descriptor is null\n",
0,0,0,0,0,0);
#endif
return ERROR;
}
wncDrv = fdInfo->wnDevIODrv;
if (wncDrv == NULL)
{
#if DEVIO_DEBUG
logMsg("wncUtilIoctlChannelCmds() ERROR: pointer to DevIO driver is null\n",
0,0,0,0,0,0);
#endif
return ERROR;
}
canDev = wncDrv->wncDevice;
if (canDev == NULL)
{
#if DEVIO_DEBUG
logMsg("wncUtilIoctlChannelCmds() ERROR: pointer to DevIO device is null\n",
0,0,0,0,0,0);
#endif
return ERROR;
}
/* lock device */
semTake(wncDrv->mutex, WAIT_FOREVER);
/* Process the specified command */
switch (command)
{
/* CAN channel commands */
case WNCAN_CHNCONFIG_SET:
chnCfg = (WNCAN_CHNCONFIG *) arg;
/*
Determine which configuration options are being set;
User will 'OR' selected items together into flags, so
we need to parse flags here
*/
if ( (chnCfg->flags & WNCAN_CHNCFG_CHANNEL) == WNCAN_CHNCFG_CHANNEL)
{
status = CAN_WriteID(canDev, fdInfo->fdtype.channel.channel,
chnCfg->channel.id, chnCfg->channel.extId);
if(status == ERROR)
{
#if DEVIO_DEBUG
logMsg("wncUtilIoctlChannelCmds() Error: Cannot write id",0,0,0,0,0,0);
#endif
break;
}
}
if ( (chnCfg->flags & WNCAN_CHNCFG_LCLFILTER) == WNCAN_CHNCFG_LCLFILTER)
{
status = CAN_SetLocalMsgFilter (canDev, fdInfo->fdtype.channel.channel,
chnCfg->filter.mask,
chnCfg->filter.extended);
if(status == ERROR)
{
#if DEVIO_DEBUG
logMsg("wncUtilIoctlChannelCmds() Error: Cannot set local"
"local msg filter \n",0,0,0,0,0,0);
#endif
break;
}
}
if ( (chnCfg->flags & WNCAN_CHNCFG_RTR) == WNCAN_CHNCFG_RTR)
{
status = CAN_SetRTR (canDev, fdInfo->fdtype.channel.channel,
chnCfg->rtr);
if(status == ERROR)
{
#if DEVIO_DEBUG
logMsg("wncUtilIoctlChannelCmds() Error: Cannot set rtr\n",0,0,0,0,0,0);
#endif
break;
}
}
if ( (chnCfg->flags & WNCAN_CHNCFG_MODE) == WNCAN_CHNCFG_MODE)
{
status = CAN_SetMode (canDev, fdInfo->fdtype.channel.channel,
chnCfg->mode);
if(status == ERROR)
{
#if DEVIO_DEBUG
logMsg("wncUtilIoctlChannelCmds() Error: Cannot set mode \n",0,0,0,0,0,0);
#endif
break;
}
}
break;
case WNCAN_CHNCONFIG_GET:
chnCfg = (WNCAN_CHNCONFIG *) arg;
/*
Determine which configuration options are to be retrieved;
User will 'OR' selected items together into flags, so
we need to parse flags here
*/
if ( (chnCfg->flags & WNCAN_CHNCFG_CHANNEL) == WNCAN_CHNCFG_CHANNEL)
{
BOOL newdata; /* unused, needed by api */
chnCfg->channel.id = CAN_ReadID(canDev, fdInfo->fdtype.channel.channel,
&chnCfg->channel.extId);
chnCfg->channel.len = WNCAN_MAX_DATA_LEN; /* load the whole message */
CAN_ReadData(canDev, fdInfo->fdtype.channel.channel,
chnCfg->channel.data, &chnCfg->channel.len, &newdata);
}
if ( (chnCfg->flags & WNCAN_CHNCFG_LCLFILTER) == WNCAN_CHNCFG_LCLFILTER)
{
chnCfg->filter.mask = CAN_GetLocalMsgFilter(canDev,
fdInfo->fdtype.channel.channel,
chnCfg->filter.extended);
if(chnCfg->filter.mask == -1)
{
#if DEVIO_DEBUG
logMsg("wncUtilIoctlChannelCmds() Error: Cannot determine local"
"messsage filter",0,0,0,0,0,0);
#endif
status=ERROR;
break;
}
}
if ( (chnCfg->flags & WNCAN_CHNCFG_RTR) == WNCAN_CHNCFG_RTR)
{
chnCfg->rtr = CAN_IsRTR (canDev, fdInfo->fdtype.channel.channel);
if(chnCfg->rtr == -1)
{
#if DEVIO_DEBUG
logMsg("wncUtilIoctlChannelCmds() Error: Cannot determine RTR\n",
0,0,0,0,0,0);
#endif
status=ERROR;
break;
}
}
if ( (chnCfg->flags & WNCAN_CHNCFG_MODE) == WNCAN_CHNCFG_MODE)
{
chnCfg->mode = CAN_GetMode (canDev, fdInfo->fdtype.channel.channel);
}
break;
case WNCAN_CHN_ENABLE:
{
BOOL enable = (BOOL) arg;
if (enable == TRUE)
{
status = CAN_EnableChannel (canDev, fdInfo->fdtype.channel.channel,
fdInfo->fdtype.channel.intType);
if (status == OK)
fdInfo->fdtype.channel.enabled = TRUE;
#if DEVIO_DEBUG
else
logMsg("wncUtilIoctlChannelCmds() Error: Channel cannot be enabled\n",
0,0,0,0,0,0);
#endif
}
else
{
status = CAN_DisableChannel (canDev, fdInfo->fdtype.channel.channel);
if (status == OK)
fdInfo->fdtype.channel.enabled = FALSE;
#if DEVIO_DEBUG
else
logMsg("wncUtilIoctlChannelCmds() Error: Channel cannot be disabled\n",
0,0,0,0,0,0);
#endif
}
}
break;
case WNCAN_CHN_TX:
status = CAN_Tx (canDev, fdInfo->fdtype.channel.channel);
break;
case WNCAN_CHNMSGLOST_GET:
*((int *)arg) = CAN_IsMessageLost (canDev, fdInfo->fdtype.channel.channel);
if(*((int *)arg) == -1)
status=ERROR;
else
status = OK;
break;
case WNCAN_CHNMSGLOST_CLEAR:
status = CAN_ClearMessageLost (canDev, fdInfo->fdtype.channel.channel);
break;
}
/* unlock device */
semGive(wncDrv->mutex);
return status;
}
/************************************************************************
*
* wncUtilIoctlCtlrCmds - utility routine to process CAN controller-specific
* ioctl() commands
*
* This routine performs the processing for CAN controller-specific commands
* specified via ioctl()
*
* RETURNS: OK or ERROR
*
* ERRNO: N/A
*
*/
LOCAL STATUS wncUtilIoctlCtlrCmds
(
WNCAN_DEVIO_FDINFO *fdInfo, /* pointer to DevIO file descriptor */
int command, /* command function code */
int arg /* arbitrary argument */
)
{
WNCAN_DEVIO_DRVINFO* wncDrv = NULL;
WNCAN_DEVICE* canDev = NULL;
WNCAN_CTLRCONFIG* ctlrCfg = NULL;
STATUS status = ERROR; /* pessimistic */
if (fdInfo == NULL)
{
#if DEVIO_DEBUG
logMsg("wncUtilIoctlCtlrCmds() ERROR: pointer to DevIO file descriptor is null\n",
0,0,0,0,0,0);
#endif
return ERROR;
}
wncDrv = fdInfo->wnDevIODrv;
if (wncDrv == NULL)
{
#if DEVIO_DEBUG
logMsg("wncUtilIoctlCtlrCmds() ERROR: pointer to DevIO driver is null\n",
0,0,0,0,0,0);
#endif
return ERROR;
}
canDev = wncDrv->wncDevice;
if (canDev == NULL)
{
#if DEVIO_DEBUG
logMsg("wncUtilIoctlCtlrCmds() ERROR: pointer to DevIO device is null\n",
0,0,0,0,0,0);
#endif
return ERROR;
}
ctlrCfg = (WNCAN_CTLRCONFIG *) arg;
if (ctlrCfg == NULL)
{
#if DEVIO_DEBUG
logMsg("wncUtilIoctlCtlrCmds() ERROR: pointer to controller config is null\n",
0,0,0,0,0,0);
#endif
return ERROR;
}
/* lock device */
semTake(wncDrv->mutex, WAIT_FOREVER);
/* Process the specified command */
switch (command)
{
/* Controller-specific commands */
case WNCAN_CTLRCONFIG_SET:
/* Process TouCAN controller command */
if (wncDrv->ctrlSetConfig)
(*wncDrv->ctrlSetConfig)(wncDrv, (void*)arg);
break;
case WNCAN_CTLRCONFIG_GET:
/* Process TouCAN controller command */
if (wncDrv->ctrlGetConfig)
(*wncDrv->ctrlGetConfig)(wncDrv, (void*)arg);
break;
}
/* unlock device */
semGive(wncDrv->mutex);
return status;
}
/************************************************************************
*
* wncUtilCalcRingBufSize - utility routine to calculate ring buffer size
*
* This routine calculates the size of the ring buffer in bytes when the
* user specifies the number of CAN data messages to be queued
*
* RETURNS: size in bytes
*
* ERRNO: N/A
*
*/
LOCAL int wncUtilCalcRingBufSize
(
int numCanMsgs /* #CAN msgs to queue in ring buffer */
)
{
/* User will specify internal input and output data buffer size in #CAN msgs;
Calculate #bytes here */
int numBytes = 0;
numBytes = numCanMsgs * sizeof(WNCAN_CHNMSG);
return numBytes;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -