📄 ew_can.c
字号:
ii += 8;
pFilter += 2;
}
/*--- Save current table ID ---*/
id0 = *pFilter;
/*--- Add new Filter ID lower bound ---*/
*pFilter = FilterID;
/*--- Prepare new extend ID, higher bound ---*/
FilterID = pNewFilter->ID2; /* ID */
FilterID |= ( pNewFilter->Controller2 << 29); /* Controller */
pFilter++;
/*--- Save current table ID ---*/
id1 = *pFilter;
/*--- Add new Filter ID higher bound ---*/
*pFilter = FilterID;
/*--- Move all existing filter bounds two steps forward ---*/
pFilter++;
while ( ii < ENDofTable )
{
/*--- lower bound ---*/
id3 = *pFilter; /* get filter */
*pFilter = id0; /* restore filter */
id0 = id3; /* move filter */
pFilter++;
/*--- higher bound ---*/
id4 = *pFilter; /* get filter */
*pFilter = id1; /* restore filter */
id1 = id4; /* move filter */
pFilter++;
ii += 8;
}
/*--- Update end of table register ---*/
ENDofTable += 8;
break;
}
if ( pNewFilter->FilterType != EW_CAN_EXT_GRP )
{
/*--- Add new Filter ID ---*/
*pFilter = FilterID;
/*--- Move all existing filters one step forward ---*/
pFilter++;
while ( ii < ENDofTable )
{
id1 = *pFilter; /* get filter */
*pFilter = id0; /* restore filter */
id0 = id1; /* move filter */
pFilter++;
ii += 4;
}
/*--- Update end of table register ---*/
ENDofTable += 4;
}
/*--- Acceptance filter mode ----*/
AFMR = EW_AFMR_CAN;
/*--- Release reset! ---*/
C1MOD = ( C1MOD & ~0x01 );
C2MOD = ( C2MOD & ~0x01 );
/*--- List has been full? ---*/
Address = ENDofTable;
Address += ( SFF_sa * 6 );
if( Address > 0x800 )
{
/*--- Acceptance filter table full after new entries ---*/
return EW_FULL;
}
/*--- Handle user code on function exit ---*/
EXIT_EW_ADDFILTER_CAN;
return EW_OK;
} /* EW_AddFilter_CAN */
S16 EW_Manage_CAN( U8 Controller )
/*
**---------------------------------------------------------------------------
**
** Abstract:
** Manage status information from the selected module.
**
** This function can be polled by the application to get status
** information from the CAN controller.
** If interrupts not are used it shall be called (polled) as
** often as possible.
**
** The function scans the global controller status register.
**
** Parameters:
** Controller The Controller number
**
** Returns:
** EW_CAN_RECEIVED A message is available
** EW_CAN_OVERRUN Data overrun
** EW_CAN_TRANSMITTED Requested transmissions completed
** EW_CAN_ERROR Error status
** EW_CAN_BUS_ERROR Bus error status
** EW_CAN_FILTER_ERROR Acceptance Filter error
**
**---------------------------------------------------------------------------
*/
{
S16 RetCode;
U32 status;
/*--- Handle user code on function entry ---*/
ENTER_EW_MANAGE_CAN;
RetCode = 0;
/*--- New message received? ---*/
if( Controller == 1)
{
status = C1GSR;
}
else if( Controller == 2)
{
status = C2GSR;
}
if( status & 0x01 )
{
/*--- A message is available ---*/
RetCode |= EW_CAN_RECEIVED;
}
/*--- Data overrun? ---*/
if( status & 0x02 )
{
/*--- A data overrun has occurred ---*/
RetCode |= EW_CAN_OVERRUN;
}
/*--- Transmit complete status? ---*/
if( status & 0x08 )
{
/*--- All requested transmissions has been successfully completed ---*/
RetCode |= EW_CAN_TRANSMITTED;
}
/*--- Error status? ---*/
if( status & 0x40 )
{
/*
**------------------------------------------------------
** Error status
** RXERR or TXERR bitfields contain error counter value
**------------------------------------------------------
*/
RetCode |= EW_CAN_ERROR;
}
/*--- Bus error status? ---*/
if( status & 0x80 )
{
/*--- Bus error status ---*/
RetCode |= EW_CAN_BUS_ERROR;
}
/*--- Acceptance Filter error? ---*/
if( LUTerr > 0 )
{
/*--- YES some problem detected ---*/
RetCode |= EW_CAN_FILTER_ERROR;
}
/*--- Handle user code on function exit ---*/
EXIT_EW_MANAGE_CAN;
return RetCode;
} /* EW_Manage_CAN */
S16 EW_ReceiveData_CAN( U8 Controller, CAN_MSG *pRxMsg )
/*
**---------------------------------------------------------------------------
**
** Abstract:
** Reads data from full CAN message or the selected CAN controller.
** The function EW_Manage_CAN can be called to see if a message are
** available and if errors has occured.
**
** Parameters:
** Controller Full CAN message or the CAN controller number
** 0 reading Full CAN message
** 1-2 reading CAN controller #1 or #2
**
** pRxMsg Pointer to the CAN_MSG struct were received information
** shall be stored.
**
** Returns:
** EW_DATA if it was a data frame
** EW_RTR if it was a remote request frame
** EW_EMPTY if the receive buffer is empty
** EW_BUSY if message is receiving
** EW_ERROR if data overrun mail flag is set during read
**
**---------------------------------------------------------------------------
*/
{
S16 ErrorCode;
U32 ii, jj;
U32 FilterID;
U32 volatile *pFilter;
/*--- Handle user code on function entry ---*/
ENTER_EW_RECEIVEDATA_CAN;
switch( Controller )
{
case 0:
/*
**------------------
** Full CAN message
**------------------
*/
/*--- First verify that CAN controller not is busy receiving a message ---*/
/* if(( CANRxSR & 0xf ) > 0 ) test with 0xf if Four CAN controllers are available in the microcontroller */
if(( CANRxSR & 0x3 ) > 0 )
{
/*--- Receiving CAN message ---*/
ErrorCode = EW_BUSY;
}
else
{
/*--- Error code initialized ---*/
ErrorCode = EW_ERROR;
/*--- Test if new Full CAN message received ---*/
/*--- Set pointer to first location of filter ---*/
pFilter = (U32 *) ( ACCEPTANCE_FILTER_RAM_BASE+ ENDofTable );
/*--- Number of items in table ---*/
jj = (SFF_sa >> 1);
for( ii = 0; ii < jj; ii++ )
{
if(( *pFilter & 0x3000000L ) == 0x1000000L )
{
/*--- SEM == 1 ---*/
/*
**-------------------------------------------------
** Increment variable used only during driver test
** SEM1count++;
**-------------------------------------------------
*/
}
if(( *pFilter & 0x3000000L ) == 0x3000000L )
{
/*--- SEM == 3, Message updated, read information ---*/
/*--- Read FF, RTR, DLC info and ID ---*/
pRxMsg->CTRL = (*pFilter & 0xC00F0000L);
pRxMsg->ID = (*pFilter & 0x000007FFL);
/*--- Clear SEM ---*/
*pFilter &= ~0x3000000L;
pFilter++;
/*--- Read Data 2 ---*/
pRxMsg->Data1= *pFilter;
pFilter++;
/*--- Read Data 3 ---*/
pRxMsg->Data2= *pFilter;
/*--- Message still OK? ---*/
pFilter -= 2;
if(( *pFilter & 0x3000000L ) == 0)
{
/*--- Yes OK ---*/
/*--- Get CAN controller # info - contained in filter table ---*/
pFilter = (U32 *) ACCEPTANCE_FILTER_RAM_BASE;
pFilter += (ii>>1);
FilterID = *pFilter;
if( ii&1 )
{
/*--- Get info from position 13-15 ---*/
FilterID &= 0xE000;
/*--- Store info in bit position 24-26 ---*/
FilterID <<= 11;
}
else
{
/*--- Get info from position 29-31 ---*/
FilterID &= 0xE0000000L;
/*--- Store info in bit position 24-26 ---*/
FilterID >>= 5;
}
/*--- Add CAN controller # info to bit position 24-26 ---*/
pRxMsg->CTRL |= FilterID;
if(( pRxMsg->CTRL & EW_CAN_CTRL_RTR ) != 0)
{
/*--- RTR message received ---*/
ErrorCode = EW_RTR;
}
else
{
/*--- Data message received ---*/
ErrorCode = EW_DATA;
}
break;
}
else
{
/*--- Message has been updated during read ---*/
/*
**-------------------------------------------------
** Increment variable used only during driver test
** SEM2count++;
**-------------------------------------------------
*/
return EW_EMPTY;
}
}
else
{
/*--- Next message ---*/
pFilter += 3;
}
}
// Neee this test ????
/*--- Any new message found? ---*/
if( ii == jj )
{
/*--- Receive buffer is empty ---*/
ErrorCode = EW_EMPTY;
}
}
break;
case 1:
/*
**--------------------------
** CAN controller 1 message
**--------------------------
*/
if( C1GSR & C1GSR_RBS )
{
/*--- A received message is available ---*/
/*--- Read FF, RTR, DLC info ---*/
pRxMsg->CTRL = (C1RFS & 0xC00F0000L);
/*--- Read ID ---*/
pRxMsg->ID = C1RID;
/*--- Read Data ---*/
pRxMsg->Data1= C1RDA;
pRxMsg->Data2= C1RDB;
/*--- Bit 2: RRB (Release receiver buffer) ---*/
C1CMR |= C1CMR_RRB;
if(( pRxMsg->CTRL & EW_CAN_CTRL_RTR ) != 0)
{
/*--- RTR message received ---*/
ErrorCode = EW_RTR;
}
else
{
/*--- Data message received ---*/
ErrorCode = EW_DATA;
}
/*--- Data overrun? ---*/
if( C1GSR & 0x02 )
{
/*--- A data overrun has occurred ---*/
ErrorCode = EW_ERROR;
}
}
else
{
/*--- Receive buffer is empty ---*/
ErrorCode = EW_EMPTY;
}
break;
case 2:
/*
**--------------------------
** CAN controller 2 message
**--------------------------
*/
if( C2GSR & C1GSR_RBS )
{
/*--- A received message is available ---*/
pRxMsg->CTRL = (C2RFS & 0xC00F0000L);
/*--- Read ID ---*/
pRxMsg->ID = C2RID;
/*--- Read Data ---*/
pRxMsg->Data1= C2RDA;
pRxMsg->Data2= C2RDB;
/*--- Bit 2: RRB (Release receiver buffer) ---*/
C2CMR |= C1CMR_RRB;
if(( pRxMsg->CTRL & EW_CAN_CTRL_RTR ) != 0)
{
/*--- RTR message received ---*/
ErrorCode = EW_RTR;
}
else
{
/*--- Data message received ---*/
ErrorCode = EW_DATA;
}
/*--- Data overrun? ---*/
if( C1GSR & 0x02 )
{
/*--- A data overrun has occurred ---*/
ErrorCode = EW_ERROR;
}
}
else
{
/*--- Receive buffer is empty ---*/
ErrorCode = EW_EMPTY;
}
break;
default:
/*--- wrong controller parameter ---*/
ErrorCode = EW_ERROR;
break;
}
/*--- Handle user code on function exit ---*/
EXIT_EW_RECEIVEDATA_CAN;
return ErrorCode;
} /* EW_ReceiveData_CAN */
S16 EW_SendData_CAN( U8 Controller, CAN_MSG *pTxMsg )
/*
**---------------------------------------------------------------------------
**
** Abstract:
** Writes data to the selected CAN controller.
**
** The CAN controller contains three transmission buffers for each CAN
** controller. The function trys to find an empty Tx buffer to start
** transmission from any empty buffer. Please note that Philips has
** described a problem with the the transmit buffers and recommend that
** only one of the buffers shall be used.
**
** See Philips ERRATA SHEET
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -