CAN-BUS Shield

Hi Everybody,

I am using the CAN-BUS Shield with extended addresses. I found a little problem in function “mcp2515_read_id”. The CAN id is calculated incorrectly (caused by the casting of the unsigned chars). I solved this issue in this way. I hope this can help other users and will be updated in the library.

regards

void MCP_CAN::mcp2515_read_id( const INT8U mcp_addr,                
                                     INT8U* ext,
                                     INT32U* id )
{
    INT8U tbufdata[4];

    *ext = 0;
    *id = 0;

    mcp2515_readRegisterS( mcp_addr, tbufdata, 4 );

    *id = (INT32U) (tbufdata[MCP_SIDH]<<3) + (tbufdata[MCP_SIDL]>>5);

    if ( (tbufdata[MCP_SIDL] & MCP_TXB_EXIDE_M) ==  MCP_TXB_EXIDE_M ) {
                                                                        /* extended id                  */
        *id = (*id<<2) + ((INT32U) (tbufdata[MCP_SIDL] & 0x03));
        *id <<= 16;
        *id = *id + ((INT32U) tbufdata[MCP_EID8]<<8) + (INT32U) tbufdata[MCP_EID0];
        *ext = 1;        
    }        
}

hi~ thanks for share! yes, this is a bug! I’ll update the firmware asap.

Hi,

I made one function that also can be useful for someone. maybe it also can be added to library if you think it is ok.

regards,

/*********************************************************************************************************
** Function name:           waitingForMsg
** Descriptions:            Waiting for X time the message with the id
** input parameters:        id      :   can id
                            timeout :   time to wait                            
** Output parameters:       *len    :   buf length
                            *buf    :   data buf
** Returned value:          the status of the function
*********************************************************************************************************/
INT8U MCP_CAN::waitingForMsg(INT32U id, INT16U timeout, INT8U *len, INT8U *buffer)
{
	INT32U start = millis();
	
	while (millis() - start < timeout) {
		
		if (checkReceive()==CAN_MSGAVAIL) {

    	readMsgBuf(len, buffer);

      if ( id == m_nID ) {
      	return 1;
      }
    }  	
	}
	
#if DEBUG_MODE
  Serial.print("waitingForMsg TIMEOUT: ");
  Serial.println(id,HEX);
#endif

  *len = 0;
  return 0;
}

hey, I really think it’s very nice!! :laughing: Of curse, it should be added to the library~
By the way~ thanks for share!