📄 can.c
字号:
*
* PreCondition:
*
* Input: none
*
* Output: none
*
* Side Effects:
*
* Overview: Indicates to the module that the data has been read.
*
* Note: This effectively frees the active hardware buffer
* to continue receiving.
********************************************************************/
void CANRead(void)
{
if (_msgRxBuf == 0)
{
RXB0CONbits.RXFUL = 0;
}
else
if (_msgRxBuf == 1)
{
RXB1CONbits.RXFUL = 0;
}
}
/*********************************************************************
* Function: NEAR unsigned char CANIsTxRdy(void)
*
* PreCondition:
*
* Input: none
*
* Output: unsigned char status of the request
*
* Side Effects:
*
* Overview: Returns TRUE if the transmit engine is ready to
* accept new data to transmit.
*
* Note: This function is as simple as it gets, i.e. direct
* access to only one TX buffer.
********************************************************************/
NEAR unsigned char CANIsTxRdy(void)
{
return (!(TXB0CONbits.TXREQ));
}
/*********************************************************************
* Function: NEAR unsigned char CANIsMsgSent(void)
*
* PreCondition:
*
* Input: none
*
* Output: unsigned char status of the request
*
* Side Effects:
*
* Overview: Returns the tag of the data that was last transmitted.
* Any additional calls without additional transmissions
* will return NULL.
*
* Note: This is useful to determine when and who's data was
* placed on the bus. This may seem redundent since
* only one hardware buffer is used; however, this
* architecture allows for expansion to deep software/
* hardware buffer designs without major changes to
* the upper level firmware.
********************************************************************/
NEAR unsigned char CANIsMsgSent(void)
{
NEAR unsigned char temp;
if (!(TXB0CONbits.TXREQ))
{
temp = _msgTxTag;
_msgTxTag = 0;
return (temp);
}
else
return (0);
}
/*********************************************************************
* Function: void CANSend(NEAR unsigned char txTag)
*
* PreCondition:
*
* Input: none
*
* Output: none
*
* Side Effects:
*
* Overview: Tell the transmit engine that the loaded data is
* is ready to send.
*
* Note: Data may not be sent immediately. The tag is used
* to trace the transmission since other data may
* also be queued to transmit.
********************************************************************/
void CANSend(NEAR unsigned char txTag)
{
// Remember the message that was sent
_msgTxTag = txTag;
TXB0CONbits.TXREQ = 1;
}
/*********************************************************************
* Function: NEAR unsigned int CANGetRxCID(void)
*
* PreCondition:
*
* Input: none
*
* Output: unsigned int connection ID (11-bit CAN ID)
*
* Side Effects:
*
* Overview: Get the connection ID from the current receive
* message.
*
* Note: The CAN ID is assumed to be left justified within
* the 16-bit word.
********************************************************************/
NEAR unsigned int CANGetRxCID(void)
{
NEAR _INT cid;
// Only return the lower byte
if (_msgRxBuf == 0)
{
cid.bytes.LSB = RXB0SIDL;
cid.bytes.MSB = RXB0SIDH;
}
else //if (_msgRxBuf == 1)
{
cid.bytes.LSB = RXB1SIDL;
cid.bytes.MSB = RXB1SIDH;
}
return (cid.word);
}
/*********************************************************************
* Function: NEAR unsigned char CANGetRxCnt(void)
*
* PreCondition: Data should have been received prior to calling
* this function; otherwise, it is likely the returned
* count is either not valid or associated to the
* previous message. Use CANIsRxRdy() to determine if
* data is available.
*
* Input: none
*
* Output: unsigned char count of the data
*
* Side Effects:
*
* Overview: This function returns the number of bytes waiting
* in the receive buffer.
*
* Note:
********************************************************************/
NEAR unsigned char CANGetRxCnt(void)
{
NEAR unsigned char count;
// Read out of the selected message buffer
if (_msgRxBuf == 0)
{
count = RXB0DLC;
}
else //if (_msgRxBuf == 1)
{
count = RXB1DLC;
}
return (count);
}
/*********************************************************************
* Function: unsigned char * NEAR CANGetRxDataPtr(void)
*
* PreCondition: Data should have been received prior to calling
* this function; otherwise, it is likely the returned
* pointer is either not valid or associated to the
* previous message. Use CANIsRxRdy() to determine if
* data is available.
*
* Input: none
*
* Output: unsigned char *
*
* Side Effects:
*
* Overview: This function returns the pointer to the RX buffer.
*
* Note: Use the direct block copy methods for faster data
* movement.
********************************************************************/
unsigned char * NEAR CANGetRxDataPtr(void)
{
// Read out of the selected message buffer
if (_msgRxBuf == 0)
{
return(&RXB0D0);
}
else //if (_msgRxBuf == 1)
{
return(&RXB1D0);
}
}
/*********************************************************************
* Function: void CANGetRxDataTyp0(unsigned char * NEAR usrBuf)
* void CANGetRxDataTyp1(unsigned char * NEAR usrBuf)
* void CANGetRxDataTyp2(unsigned char * NEAR usrBuf)
*
* PreCondition: Data should have been received prior to calling
* this function; otherwise, it is likely the returned
* count is either not valid or associated to the
* previous message. Use CANIsRxRdy() to determine if
* data is available.
*
* Input: pointer to user supplied buffer
*
* Output: void
*
* Side Effects:
*
* Overview: Get block data from the buffer.
*
* Note: These functions copy blocks of data rather than
* single bytes. This method is very fast; however,
* data limits are not checked. It is up to the
* caller to verify the count to toss data that is
* not valid.
********************************************************************/
void CANGetRxDataTyp0(unsigned char * NEAR usrBuf)
{
// Read out of the selected message buffer
if (_msgRxBuf == 0)
{
*((_CAN_DATA_TYP0 *)usrBuf) = *((_CAN_DATA_TYP0 *)(&RXB0D0));
}
else //if (_msgRxBuf == 1)
{
*((_CAN_DATA_TYP0 *)usrBuf) = *((_CAN_DATA_TYP0 *)(&RXB1D0));
}
}
void CANGetRxDataTyp1(unsigned char * NEAR usrBuf)
{
// Read out of the selected message buffer
if (_msgRxBuf == 0)
{
*((_CAN_DATA_TYP1 *)usrBuf) = *((_CAN_DATA_TYP1 *)((&RXB0D0)+1));
}
else //if (_msgRxBuf == 1)
{
*((_CAN_DATA_TYP1 *)usrBuf) = *((_CAN_DATA_TYP1 *)((&RXB1D0)+1));
}
}
void CANGetRxDataTyp2(unsigned char * NEAR usrBuf)
{
// Read out of the selected message buffer
if (_msgRxBuf == 0)
{
*((_CAN_DATA_TYP2 *)usrBuf) = *((_CAN_DATA_TYP2 *)((&RXB0D0)+2));
}
else //if (_msgRxBuf == 1)
{
*((_CAN_DATA_TYP2 *)usrBuf) = *((_CAN_DATA_TYP2 *)((&RXB1D0)+2));
}
}
/*********************************************************************
* Function: void CANPutTxCID(NEAR unsigned int txCID)
*
* PreCondition: The function CANIsTxRdy() must be called prior
* to using this function; otherwise, the previous
* transmission request may be corrupted.
*
* Input: none
*
* Output: unsigned char *
*
* Side Effects:
*
* Overview: Write the CID to the TX buffer.
*
* Note:
********************************************************************/
void CANPutTxCID(NEAR unsigned int txCID)
{
NEAR _INT temp;
temp.word = txCID;
// Write the CID to the buffer
TXB0SIDL = temp.bytes.LSB;
TXB0SIDH = temp.bytes.MSB;
}
/*********************************************************************
* Function: void CANPutTxCnt(NEAR unsigned char txCount)
*
* PreCondition: The function CANIsTxRdy() must be called prior
* to using this function; otherwise, the previous
* transmission request may be corrupted.
*
* Input: none
*
* Output: unsigned char *
*
* Side Effects:
*
* Overview: Write the count to the TX buffer.
*
*
* Note:
********************************************************************/
void CANPutTxCnt(NEAR unsigned char txCount)
{
TXB0DLC = txCount & 0x0F;
}
/*********************************************************************
* Function: unsigned char * NEAR CANGetTxDataPtr(void)
*
* PreCondition: The function CANIsTxRdy() must be called prior
* to using this function; otherwise, the pointer
* may be invalid.
*
* Input: none
*
* Output: unsigned char *
*
* Side Effects:
*
* Overview: Return a pointer to the buffer.
*
*
* Note:
********************************************************************/
unsigned char * NEAR CANGetTxDataPtr(void)
{
return (&TXB0D0);
}
/*********************************************************************
* Function: void CANPutTxDataTyp0(unsigned char * NEAR usrBuf)
* void CANPutTxDataTyp1(unsigned char * NEAR usrBuf)
* void CANPutTxDataTyp2(unsigned char * NEAR usrBuf)
*
* PreCondition: The function CANIsTxRdy() must be called prior
* to using this function; otherwise, the previous
* transmission request may be corrupted.
*
* Input: none
*
* Output: unsigned char *
*
* Side Effects:
*
* Overview: Put a block of data in the TX buffer.
*
* Note: Copying a block into the buffer does not imply
* that the entire block must be transmitted. This
* function mearly loads the data into the buffer
* following the specified format TYP0, TYP1, or TYP2.
********************************************************************/
void CANPutTxDataTyp0(unsigned char * NEAR usrBuf)
{
*((_CAN_DATA_TYP0 *)((&TXB0D0))) = *((_CAN_DATA_TYP0 *)usrBuf);
}
void CANPutTxDataTyp1(unsigned char * NEAR usrBuf)
{
*((_CAN_DATA_TYP1 *)((&TXB0D0)+1)) = *((_CAN_DATA_TYP1 *)usrBuf);
}
void CANPutTxDataTyp2(unsigned char * NEAR usrBuf)
{
*((_CAN_DATA_TYP2 *)((&TXB0D0)+2)) = *((_CAN_DATA_TYP2 *)usrBuf);
}
/*********************************************************************
* Function: void CANInit(void)
*
* PreCondition:
*
* Input: none
*
* Output: unsigned char *
*
* Side Effects: Filters are removed.
*
* Overview: Initialize the module.
*
* Note:
********************************************************************/
void CANInit(void)
{
CANClose();
// Init some internal control registers
_msgTxTag = 0;
_msgRxBuf = 0;
// Init device specific hardware for CAN
CIOCON = 0x20;
LATBbits.LATB2 = 1;
TRISBbits.TRISB2 = 0;
TRISBbits.TRISB3 = 1;
RXM0SIDH = 0xFF;
RXM0SIDL = 0xFF;
RXM1SIDH = 0xFF;
RXM1SIDL = 0xFF;
RXF0SIDL = 0xFF;
RXF1SIDL = 0xFF;
RXF2SIDL = 0xFF;
RXF3SIDL = 0xFF;
RXF4SIDL = 0xFF;
RXF5SIDL = 0xFF;
RXB0CON = 0x20;
RXB1CON = 0x20;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -