diff --git a/README.md b/README.md index 9d2b7f6..5d02b98 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,5 @@ -MCP2515 Library for Arduino +Sub-Development branch was copied to Master on June 30th, 2016 and is not being maintained and will be deleted soon. The master is current. ============== -MCP2515 library v1.1 -This library is compatible with any shield or CAN interface that uses the MCP2515 CAN protocol controller. -Baudrates 5k, 10k, 20k, 50k, 100k, 125k, 250k, 500k, & 1000k are confirmed to work using a Peak-System PCAN-USB dongle as a reference. - -Installation -============== -Copy this into your "[...]/MySketches/libraries/" folder and restart the Arduino editor. - -NOTE: If you have an older version of the library (CAN_BUS_Shield) be sure to remove - it from the libraries folder or replace the files with those in this library to avoid conflicts. *Happy Coding!* diff --git a/examples/receive/receive.ino b/examples/CAN_receive/CAN_receive.ino similarity index 60% rename from examples/receive/receive.ino rename to examples/CAN_receive/CAN_receive.ino index ee31b3e..06463de 100644 --- a/examples/receive/receive.ino +++ b/examples/CAN_receive/CAN_receive.ino @@ -1,4 +1,6 @@ -// demo: CAN-BUS Shield, receive data +// CAN Receive Example +// + #include #include @@ -12,8 +14,15 @@ MCP_CAN CAN0(10); // Set CS to pin 10 void setup() { Serial.begin(115200); - CAN0.begin(CAN_500KBPS); // init can bus : baudrate = 500k + + // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled. + if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!"); + else Serial.println("Error Initializing MCP2515..."); + + CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data. + pinMode(2, INPUT); // Setting pin 2 for /INT input + Serial.println("MCP2515 Library Receive Example..."); } @@ -21,20 +30,22 @@ void loop() { if(!digitalRead(2)) // If pin 2 is low, read receive buffer { - CAN0.readMsgBuf(&len, rxBuf); // Read data: len = data length, buf = data byte(s) - rxId = CAN0.getCanId(); // Get message ID - Serial.print("ID: "); + CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s) + + Serial.print("ID: "); // Print the message ID. Serial.print(rxId, HEX); + Serial.print(" Data: "); - for(int i = 0; i +#include + +MCP_CAN CAN0(10); // Set CS to pin 10 + +void setup() +{ + Serial.begin(115200); + + // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled. + if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!"); + else Serial.println("Error Initializing MCP2515..."); + + CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted +} + +byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; + +void loop() +{ + // send data: ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send + byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data); + if(sndStat == CAN_OK){ + Serial.println("Message Sent Successfully!"); + } else { + Serial.println("Error Sending Message..."); + } + delay(100); // send data per 100ms +} + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/ diff --git a/examples/CAN_to_Ethernet/CAN_to_Ethernet.ino b/examples/CAN_to_Ethernet/CAN_to_Ethernet.ino new file mode 100644 index 0000000..157341d --- /dev/null +++ b/examples/CAN_to_Ethernet/CAN_to_Ethernet.ino @@ -0,0 +1,59 @@ +// CAN to Ethernet +// Jan 28th, 2014 +// Written by: Cory J. Fowler + +#include +#include +#include +#include + +// Change these for your network! +byte mac[] = {0x00, 0x55, 0x66, 0xEE, 0xFF, 0xFF}; +IPAddress ip(10, 100, 50, 233); +IPAddress gateway(10, 100, 50, 254); +IPAddress dest(10, 100, 50, 210); + +unsigned int localPort = 8888; +unsigned int remPort = 54321; + +unsigned long rxId; +byte len = 0; +byte rxBuf[8]; +char buffer[50]; + +MCP_CAN CAN0(9); // Set CS to pin 9 + +EthernetUDP UDP; +void setup() +{ + Serial.begin(115200); +// CAN0.begin(CAN_250KBPS); // init CAN Bus with 250kb/s baudrate + CAN0.begin(MCP_ANY, CAN_250KBPS, MCP_16MHZ); // init CAN Bus with 250kb/s baudrate at 16MHz with Mask & Filters Disabled + CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data. + pinMode(2, INPUT); // Setting pin 2, MCP2515 /INT, to input mode + Ethernet.begin(mac,ip); // Initialize Ethernet + UDP.begin(localPort); // Initialize the UDP listen port that is currently unused! + + Serial.println("CAN to Ethernet..."); +} + +void loop() +{ + if(!digitalRead(2)) // If pin 2 is low, read receive buffer + { + CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read Data: rxID = Message ID, len = Data Length, buf = Data Byte(s) +// CAN0.readMsgBuf(&len, rxBuf); // Read Data: len = Data Length, buf = Data Byte(s) +// rxId = CAN0.getCanId(); // Function will be depreciated soon due to readMsgBuf now returning ID + + sprintf(buffer, "ID: %.8lX Data: %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X\n\r", + rxId, rxBuf[0], rxBuf[1], rxBuf[2], rxBuf[3], rxBuf[4], rxBuf[5], rxBuf[6], rxBuf[7]); + + UDP.beginPacket(dest, remPort); + UDP.write(buffer); + UDP.endPacket(); + } +} + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/ diff --git a/examples/CAN_to_Ethernet/udp_listen.pl b/examples/CAN_to_Ethernet/udp_listen.pl new file mode 100644 index 0000000..413c803 --- /dev/null +++ b/examples/CAN_to_Ethernet/udp_listen.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl -w +#This works on Debian Linux +#I have not tested it on a Windows system running Perl, but it should work if all dependencies are met. +#Also, FIREWALL. <--- I gaurantee that will be the issue. + +use IO::Socket; + +### Create UDP Listen Socket +my $udpsocket = new IO::Socket::INET ( + LocalPort => '54321', + Proto => 'udp', + ); + die "Could not create socket: $!\n" unless $udpsocket; + +### Data Manipulation and Display +while(1) { + $udpsocket->recv(my $data,512); + print $data; +} diff --git a/examples/Dual_CAN/Dual_CAN.ino b/examples/Dual_CAN/Dual_CAN.ino new file mode 100644 index 0000000..860feb7 --- /dev/null +++ b/examples/Dual_CAN/Dual_CAN.ino @@ -0,0 +1,55 @@ +// Demo: Dual CAN-BUS Shields, Data Pass-through +// Written by: Cory J. Fowler +// January 31st 2014 +// This examples the ability of this library to support more than one MCP2515 based CAN interface. + + +#include +#include + +unsigned long rxId; +byte len; +byte rxBuf[8]; + +byte txBuf0[] = {AA,55,AA,55,AA,55,AA,55}; +byte txBuf1[] = {55,AA,55,AA,55,AA,55,AA}; + +MCP_CAN CAN0(10); // CAN0 interface usins CS on digital pin 10 +MCP_CAN CAN1(9); // CAN1 interface using CS on digital pin 9 + +void setup() +{ + Serial.begin(115200); + + // init CAN0 bus, baudrate: 250k@16MHz + if(CAN0.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK){ + Serial.print("CAN0: Init OK!\r\n"); + CAN0.setMode(MCP_NORMAL); + } else Serial.print("CAN0: Init Fail!!!\r\n"); + + // init CAN1 bus, baudrate: 250k@16MHz + if(CAN1.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK){ + Serial.print("CAN1: Init OK!\r\n"); + CAN1.setMode(MCP_NORMAL); + } else Serial.print("CAN1: Init Fail!!!\r\n"); + + SPI.setClockDivider(SPI_CLOCK_DIV2); // Set SPI to run at 8MHz (16MHz / 2 = 8 MHz) + + CAN0.sendMsgBuf(0x1000000, 1, 8, tx0Buf); + CAN1.sendMsgBuf(0x1000001, 1, 8, tx1Buf); +} + +void loop(){ + if(!digitalRead(2)){ // If pin 2 is low, read CAN0 receive buffer + CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s) + CAN1.sendMsgBuf(rxId, 1, len, rxBuf); // Immediately send message out CAN1 interface + } + if(!digitalRead(3)){ // If pin 3 is low, read CAN1 receive buffer + CAN1.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s) + CAN0.sendMsgBuf(rxId, 1, len, rxBuf); // Immediately send message out CAN0 interface + } +} + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/ diff --git a/examples/Extended_MaskFilter/Extended_MaskFilter.ino b/examples/Extended_MaskFilter/Extended_MaskFilter.ino new file mode 100644 index 0000000..e9dbd54 --- /dev/null +++ b/examples/Extended_MaskFilter/Extended_MaskFilter.ino @@ -0,0 +1,87 @@ + +// MCP2515 Mask and Filter example for extended CAN message frames. +// Written by Cory J. Fowler (20140717) + +/*********************************************************************************** +If you send the following extended IDs below to an Arduino loaded with this sketch +you will find that 0x00FFCC00 and 0x00FF9900 will not get in. + + ID in Hex is the same as the Filter in Hex. + 0x00FFEE00 + 0x00FFDD00 + 0x00FFCC00 This example will NOT be receiving this ID + 0x00FFBB00 + 0x00FFAA00 + 0x00FF9900 This example will NOT be receiving this ID + 0x00FF8800 + 0x00FF7700 + + This mask will check the filters against ID bits 23 through 8. + (Those familiar with J1939 might see why I used this mask.) + MASK = 0x00FFFF00 + If there is an explicit filter match to those bits, the message will be passed to the + receive buffer and the interrupt pin will be set. + + This example will NOT be exclusive to ONLY the above message IDs, for that a mask such + as the below would be used: + MASK = 0x1FFFFFFF + + At the moment, to disable a filter or mask, copy the value of a used filter or mask. + +***********************************************************************************/ + + +#include +#include + +long unsigned int rxId; +unsigned char len = 0; +unsigned char rxBuf[8]; + +MCP_CAN CAN0(10); // Set CS to pin 10 + +void setup() +{ + Serial.begin(115200); + if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 Init Okay!!\r\n"); + else Serial.print("MCP2515 Init Failed!!\r\n"); + pinMode(2, INPUT); // Setting pin 2 for /INT input + + CAN0.init_Mask(0,1,0x00FFFF00); // Init first mask... + CAN0.init_Filt(0,1,0x00FFEE00); // Init first filter... + CAN0.init_Filt(1,1,0x00FFDD00); // Init second filter... + + CAN0.init_Mask(1,1,0x00FFFF00); // Init second mask... + CAN0.init_Filt(2,1,0x00FFBB00); // Init third filter... + CAN0.init_Filt(3,1,0x00FFAA00); // Init fouth filter... + CAN0.init_Filt(4,1,0x00FF8800); // Init fifth filter... + CAN0.init_Filt(5,1,0x00FF7700); // Init sixth filter... + + Serial.println("MCP2515 Library Mask & Filter Example..."); + CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted +} + +void loop() +{ + if(!digitalRead(2)) // If pin 2 is low, read receive buffer + { + CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s) + Serial.print("ID: "); + Serial.print(rxId, HEX); + Serial.print(" Data: "); + for(int i = 0; i +#include + +long unsigned int rxId; +unsigned char len = 0; +unsigned char rxBuf[8]; + +MCP_CAN CAN0(10); // Set CS to pin 10 + +void setup() +{ + Serial.begin(115200); + if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 Init Okay!!\r\n"); + else Serial.print("MCP2515 Init Failed!!\r\n"); + pinMode(2, INPUT); // Setting pin 2 for /INT input + + + CAN0.init_Mask(0,0,0x010F0000); // Init first mask... + CAN0.init_Filt(0,0,0x01000000); // Init first filter... + CAN0.init_Filt(1,0,0x01010000); // Init second filter... + + CAN0.init_Mask(1,0,0x010F0000); // Init second mask... + CAN0.init_Filt(2,0,0x01030000); // Init third filter... + CAN0.init_Filt(3,0,0x01040000); // Init fouth filter... + CAN0.init_Filt(4,0,0x01060000); // Init fifth filter... + CAN0.init_Filt(5,0,0x01070000); // Init sixth filter... + + Serial.println("MCP2515 Library Mask & Filter Example..."); + CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted +} + +void loop() +{ + if(!digitalRead(2)) // If pin 2 is low, read receive buffer + { + CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s) + Serial.print("ID: "); + Serial.print(rxId, HEX); + Serial.print(" Data: "); + for(int i = 0; i -#include - -MCP_CAN CAN0(10); // Set CS to pin 10 - -void setup() -{ - Serial.begin(115200); - // init can bus, baudrate: 500k - if(CAN0.begin(CAN_500KBPS) == CAN_OK) Serial.print("can init ok!!\r\n"); - else Serial.print("Can init fail!!\r\n"); -} - -unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7}; -void loop() -{ - // send data: id = 0x00, standrad flame, data len = 8, stmp: data buf - CAN0.sendMsgBuf(0x00, 0, 8, stmp); - delay(100); // send data per 100ms -} - -/********************************************************************************************************* - END FILE -*********************************************************************************************************/ diff --git a/keywords.txt b/keywords.txt index e7128e8..2fec82e 100644 --- a/keywords.txt +++ b/keywords.txt @@ -5,43 +5,56 @@ ####################################### # Datatypes (KEYWORD1) ####################################### -MCP_CAN KEYWORD1 -mcp_can_dfs KEYWORD1 -mcp_can KEYWORD1 +MCP_CAN KEYWORD1 +mcp_can_dfs KEYWORD1 +mcp_can KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) ####################################### -begin KEYWORD2 -init_Mask KEYWORD2 -init_Filt KEYWORD2 -sendMsgBuf KEYWORD2 -readMsgBuf KEYWORD2 +begin KEYWORD2 +setMode KEYWORD2 +init_Mask KEYWORD2 +init_Filt KEYWORD2 +sendMsgBuf KEYWORD2 +readMsgBuf KEYWORD2 checkReceive KEYWORD2 -checkError KEYWORD2 -getCanId KEYWORD2 +checkError KEYWORD2 ####################################### # Constants (LITERAL1) ####################################### -CAN_5KBPS LITERAL1 -CAN_10KBPS LITERAL1 -CAN_20KBPS LITERAL1 -CAN_40KBPS LITERAL1 -CAN_50KBPS LITERAL1 -CAN_80KBPS LITERAL1 -CAN_100KBPS LITERAL1 -CAN_125KBPS LITERAL1 -CAN_200KBPS LITERAL1 -CAN_250KBPS LITERAL1 -CAN_500KBPS LITERAL1 -CAN_1000KBPS LITERAL1 -CAN_OK LITERAL1 -CAN_FAILINIT LITERAL1 -CAN_FAILTX LITERAL1 -CAN_MSGAVAIL LITERAL1 -CAN_NOMSG LITERAL1 -CAN_CTRLERROR LITERAL1 -CAN_GETTXBFTIMEOUT LITERAL1 -CAN_SENDMSGTIMEOUT LITERAL1 -CAN_FAIL LITERAL1 +MCP_8MHz LITERAL1 +MCP_16MHZ LITERAL1 +MCP_20MHZ LITERAL1 +CAN_4K096BPS LITERAL1 +CAN_5KBPS LITERAL1 +CAN_10KBPS LITERAL1 +CAN_20KBPS LITERAL1 +CAN_31K25BPS LITERAL1 +CAN_40KBPS LITERAL1 +CAN_50KBPS LITERAL1 +CAN_80KBPS LITERAL1 +CAN_100KBPS LITERAL1 +CAN_125KBPS LITERAL1 +CAN_200KBPS LITERAL1 +CAN_250KBPS LITERAL1 +CAN_500KBPS LITERAL1 +CAN_1000KBPS LITERAL1 +MCP_ANY LITERAL1 +MCP_STD LITERAL1 +MCP_EXT LITERAL1 +MCP_STDEXT LITERAL1 +CAN_OK LITERAL1 +CAN_FAILINIT LITERAL1 +CAN_FAILTX LITERAL1 +CAN_MSGAVAIL LITERAL1 +CAN_NOMSG LITERAL1 +CAN_CTRLERROR LITERAL1 +CAN_GETTXBFTIMEOUT LITERAL1 +CAN_SENDMSGTIMEOUT LITERAL1 +CAN_FAIL LITERAL1 +MCP_NORMAL LITERAL1 +MCP_SLEEP LITERAL1 +MCP_LOOPBACK LITERAL1 +MCP_LISTENONLY LITERAL1 diff --git a/mcp_can.cpp b/mcp_can.cpp index aa8e478..9b97b8c 100644 --- a/mcp_can.cpp +++ b/mcp_can.cpp @@ -1,10 +1,11 @@ /* mcp_can.cpp 2012 Copyright (c) Seeed Technology Inc. All right reserved. + 2014 Copyright (c) Cory J. Fowler All Rights Reserved. - Author:Loovee + Author: Loovee Contributor: Cory J. Fowler - 2014-1-16 + 2014-9-16 This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -27,7 +28,7 @@ /********************************************************************************************************* ** Function name: mcp2515_reset -** Descriptions: reset the device +** Descriptions: Performs a software reset *********************************************************************************************************/ void MCP_CAN::mcp2515_reset(void) { @@ -39,7 +40,7 @@ void MCP_CAN::mcp2515_reset(void) /********************************************************************************************************* ** Function name: mcp2515_readRegister -** Descriptions: read register +** Descriptions: Read data register *********************************************************************************************************/ INT8U MCP_CAN::mcp2515_readRegister(const INT8U address) { @@ -56,7 +57,7 @@ INT8U MCP_CAN::mcp2515_readRegister(const INT8U address) /********************************************************************************************************* ** Function name: mcp2515_readRegisterS -** Descriptions: read registerS +** Descriptions: Reads sucessive data registers *********************************************************************************************************/ void MCP_CAN::mcp2515_readRegisterS(const INT8U address, INT8U values[], const INT8U n) { @@ -66,15 +67,14 @@ void MCP_CAN::mcp2515_readRegisterS(const INT8U address, INT8U values[], const I spi_readwrite(address); // mcp2515 has auto-increment of address-pointer for (i=0; i 0) { #if DEBUG_MODE - Serial.print("Enter setting mode fall\r\n"); + Serial.print("Entering Configuration Mode Failure...\r\n"); #endif return res; } #if DEBUG_MODE - Serial.print("Enter setting mode success \r\n"); + Serial.print("Entering Configuration Mode Successful!!!\r\n"); #endif /* set boadrate */ - if(mcp2515_configRate(canSpeed)) + if(mcp2515_configRate(canSpeed, canClock)) { #if DEBUG_MODE - Serial.print("set rate fall!!\r\n"); + Serial.print("Setting Baudrate Failure...\r\n"); #endif return res; } #if DEBUG_MODE - Serial.print("set rate success!!\r\n"); + Serial.print("Setting Baudrate Successful!!!\r\n"); #endif if ( res == MCP2515_OK ) { @@ -337,42 +509,58 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canSpeed) /* mcp25 /* interrupt mode */ mcp2515_setRegister(MCP_CANINTE, MCP_RX0IF | MCP_RX1IF); -#if (DEBUG_RXANY==1) - /* enable both receive-buffers */ - /* to receive any message */ - /* and enable rollover */ - mcp2515_modifyRegister(MCP_RXB0CTRL, - MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, - MCP_RXB_RX_ANY | MCP_RXB_BUKT_MASK); - mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, - MCP_RXB_RX_ANY); -#else - /* enable both receive-buffers */ - /* to receive messages */ - /* with std. and ext. identifie */ - /* rs */ - /* and enable rollover */ - mcp2515_modifyRegister(MCP_RXB0CTRL, - MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, - MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); - mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, - MCP_RXB_RX_STDEXT); -#endif - /* enter normal mode */ - res = mcp2515_setCANCTRL_Mode(MODE_NORMAL); + switch(canIDMode) + { + case (MCP_ANY): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_ANY | MCP_RXB_BUKT_MASK); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_ANY); + break; +/* The followingn two functions of the MCP2515 do not work, there is a bug in the silicon. + case (MCP_STD): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_STD | MCP_RXB_BUKT_MASK ); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_STD); + break; + + case (MCP_EXT): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_EXT | MCP_RXB_BUKT_MASK ); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_EXT); + break; +*/ + case (MCP_STDEXT): + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_STDEXT); + break; + + default: +#if DEBUG_MODE + Serial.print("`Setting ID Mode Failure...\r\n"); +#endif + return MCP2515_FAIL; + break; +} + + + res = mcp2515_setCANCTRL_Mode(mcpMode); if(res) { #if DEBUG_MODE - Serial.print("Enter Normal Mode Fall!!\r\n"); + Serial.print("Returning to Previous Mode Failure...\r\n"); #endif return res; } - -#if DEBUG_MODE - Serial.print("Enter Normal Mode Success!!\r\n"); -#endif - } return res; @@ -380,7 +568,7 @@ INT8U MCP_CAN::mcp2515_init(const INT8U canSpeed) /* mcp25 /********************************************************************************************************* ** Function name: mcp2515_write_id -** Descriptions: write can id +** Descriptions: Write CAN ID *********************************************************************************************************/ void MCP_CAN::mcp2515_write_id( const INT8U mcp_addr, const INT8U ext, const INT32U id ) { @@ -406,12 +594,46 @@ void MCP_CAN::mcp2515_write_id( const INT8U mcp_addr, const INT8U ext, const INT tbufdata[MCP_EID0] = 0; tbufdata[MCP_EID8] = 0; } + + mcp2515_setRegisterS( mcp_addr, tbufdata, 4 ); +} + +/********************************************************************************************************* +** Function name: mcp2515_write_mf +** Descriptions: Write Masks and Filters +*********************************************************************************************************/ +void MCP_CAN::mcp2515_write_mf( const INT8U mcp_addr, const INT8U ext, const INT32U id ) +{ + uint16_t canid; + INT8U tbufdata[4]; + + canid = (uint16_t)(id & 0x0FFFF); + + if ( ext == 1) + { + tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF); + tbufdata[MCP_EID8] = (INT8U) (canid >> 8); + canid = (uint16_t)(id >> 16); + tbufdata[MCP_SIDL] = (INT8U) (canid & 0x03); + tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x1C) << 3); + tbufdata[MCP_SIDL] |= MCP_TXB_EXIDE_M; + tbufdata[MCP_SIDH] = (INT8U) (canid >> 5 ); + } + else + { + tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF); + tbufdata[MCP_EID8] = (INT8U) (canid >> 8); + canid = (uint16_t)(id >> 16); + tbufdata[MCP_SIDL] = (INT8U) ((canid & 0x07) << 5); + tbufdata[MCP_SIDH] = (INT8U) (canid >> 3 ); + } + mcp2515_setRegisterS( mcp_addr, tbufdata, 4 ); } /********************************************************************************************************* ** Function name: mcp2515_read_id -** Descriptions: read can id +** Descriptions: Read CAN ID *********************************************************************************************************/ void MCP_CAN::mcp2515_read_id( const INT8U mcp_addr, INT8U* ext, INT32U* id ) { @@ -436,25 +658,25 @@ void MCP_CAN::mcp2515_read_id( const INT8U mcp_addr, INT8U* ext, INT32U* id ) /********************************************************************************************************* ** Function name: mcp2515_write_canMsg -** Descriptions: write msg +** Descriptions: Write message *********************************************************************************************************/ void MCP_CAN::mcp2515_write_canMsg( const INT8U buffer_sidh_addr) { INT8U mcp_addr; mcp_addr = buffer_sidh_addr; mcp2515_setRegisterS(mcp_addr+5, m_nDta, m_nDlc ); /* write data bytes */ + if ( m_nRtr == 1) /* if RTR set bit in byte */ - { m_nDlc |= MCP_RTR_MASK; - } - mcp2515_setRegister((mcp_addr+4), m_nDlc ); /* write the RTR and DLC */ - mcp2515_write_id(mcp_addr, m_nExtFlg, m_nID ); /* write CAN id */ + + mcp2515_setRegister((mcp_addr+4), m_nDlc ); /* write the RTR and DLC */ + mcp2515_write_id(mcp_addr, m_nExtFlg, m_nID ); /* write CAN id */ } /********************************************************************************************************* ** Function name: mcp2515_read_canMsg -** Descriptions: read message +** Descriptions: Read message *********************************************************************************************************/ void MCP_CAN::mcp2515_read_canMsg( const INT8U buffer_sidh_addr) /* read can msg */ { @@ -467,29 +689,18 @@ void MCP_CAN::mcp2515_read_canMsg( const INT8U buffer_sidh_addr) /* read ctrl = mcp2515_readRegister( mcp_addr-1 ); m_nDlc = mcp2515_readRegister( mcp_addr+4 ); - if ((ctrl & 0x08)) { + if (ctrl & 0x08) m_nRtr = 1; - } - else { + else m_nRtr = 0; - } m_nDlc &= MCP_DLC_MASK; mcp2515_readRegisterS( mcp_addr+5, &(m_nDta[0]), m_nDlc ); } /********************************************************************************************************* -** Function name: sendMsg -** Descriptions: send message -*********************************************************************************************************/ -void MCP_CAN::mcp2515_start_transmit(const INT8U mcp_addr) /* start transmit */ -{ - mcp2515_modifyRegister( mcp_addr-1 , MCP_TXB_TXREQ_M, MCP_TXB_TXREQ_M ); -} - -/********************************************************************************************************* -** Function name: sendMsg -** Descriptions: send message +** Function name: mcp2515_getNextFreeTXBuf +** Descriptions: Send message *********************************************************************************************************/ INT8U MCP_CAN::mcp2515_getNextFreeTXBuf(INT8U *txbuf_n) /* get Next free txbuf */ { @@ -503,8 +714,8 @@ INT8U MCP_CAN::mcp2515_getNextFreeTXBuf(INT8U *txbuf_n) /* get N for (i=0; i 0){ +#if DEBUG_MODE + Serial.print("Entering Configuration Mode Failure...\r\n"); +#endif + return res; +} + + if (num == 0){ + mcp2515_write_mf(MCP_RXM0SIDH, ext, ulData); + + } + else if(num == 1){ + mcp2515_write_mf(MCP_RXM1SIDH, ext, ulData); + } + else res = MCP2515_FAIL; + + res = mcp2515_setCANCTRL_Mode(mcpMode); + if(res > 0){ +#if DEBUG_MODE + Serial.print("Entering Previous Mode Failure...\r\nSetting Mask Failure...\r\n"); +#endif + return res; + } +#if DEBUG_MODE + Serial.print("Setting Mask Successful!!!\r\n"); +#endif + return res; +} + +/********************************************************************************************************* +** Function name: init_Mask +** Descriptions: Public function to set mask(s). +*********************************************************************************************************/ +INT8U MCP_CAN::init_Mask(INT8U num, INT32U ulData) +{ + INT8U res = MCP2515_OK; + INT8U ext = 0; +#if DEBUG_MODE + Serial.print("Starting to Set Mask!!!\r\n"); #endif res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); if(res > 0){ #if DEBUG_MODE - Serial.print("Enter setting mode fall\r\n"); + Serial.print("Entering Configuration Mode Failure...\r\n"); #endif return res; } + if((num & 0x80000000) == 0x80000000) + ext = 1; + if (num == 0){ - mcp2515_write_id(MCP_RXM0SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXM0SIDH, ext, ulData); } else if(num == 1){ - mcp2515_write_id(MCP_RXM1SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXM1SIDH, ext, ulData); } else res = MCP2515_FAIL; - res = mcp2515_setCANCTRL_Mode(MODE_NORMAL); + res = mcp2515_setCANCTRL_Mode(mcpMode); if(res > 0){ #if DEBUG_MODE - Serial.print("Enter normal mode fall\r\n"); + Serial.print("Entering Previous Mode Failure...\r\nSetting Mask Failure...\r\n"); #endif return res; } #if DEBUG_MODE - Serial.print("set Mask success!!\r\n"); + Serial.print("Setting Mask Successful!!!\r\n"); #endif return res; } /********************************************************************************************************* ** Function name: init_Filt -** Descriptions: init canid filters +** Descriptions: Public function to set filter(s). *********************************************************************************************************/ INT8U MCP_CAN::init_Filt(INT8U num, INT8U ext, INT32U ulData) { INT8U res = MCP2515_OK; #if DEBUG_MODE - Serial.print("Begin to set Filter!!\r\n"); + Serial.print("Starting to Set Filter!!!\r\n"); +#endif + res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); + if(res > 0) + { +#if DEBUG_MODE + Serial.print("Enter Configuration Mode Failure...\r\n"); +#endif + return res; + } + + switch( num ) + { + case 0: + mcp2515_write_mf(MCP_RXF0SIDH, ext, ulData); + break; + + case 1: + mcp2515_write_mf(MCP_RXF1SIDH, ext, ulData); + break; + + case 2: + mcp2515_write_mf(MCP_RXF2SIDH, ext, ulData); + break; + + case 3: + mcp2515_write_mf(MCP_RXF3SIDH, ext, ulData); + break; + + case 4: + mcp2515_write_mf(MCP_RXF4SIDH, ext, ulData); + break; + + case 5: + mcp2515_write_mf(MCP_RXF5SIDH, ext, ulData); + break; + + default: + res = MCP2515_FAIL; + } + + res = mcp2515_setCANCTRL_Mode(mcpMode); + if(res > 0) + { +#if DEBUG_MODE + Serial.print("Entering Previous Mode Failure...\r\nSetting Filter Failure...\r\n"); +#endif + return res; + } +#if DEBUG_MODE + Serial.print("Setting Filter Successfull!!!\r\n"); +#endif + + return res; +} + +/********************************************************************************************************* +** Function name: init_Filt +** Descriptions: Public function to set filter(s). +*********************************************************************************************************/ +INT8U MCP_CAN::init_Filt(INT8U num, INT32U ulData) +{ + INT8U res = MCP2515_OK; + INT8U ext = 0; + +#if DEBUG_MODE + Serial.print("Starting to Set Filter!!!\r\n"); #endif res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); if(res > 0) { #if DEBUG_MODE - Serial.print("Enter setting mode fall\r\n"); + Serial.print("Enter Configuration Mode Failure...\r\n"); #endif return res; } + if((num & 0x80000000) == 0x80000000) + ext = 1; + switch( num ) { case 0: - mcp2515_write_id(MCP_RXF0SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXF0SIDH, ext, ulData); break; case 1: - mcp2515_write_id(MCP_RXF1SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXF1SIDH, ext, ulData); break; case 2: - mcp2515_write_id(MCP_RXF2SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXF2SIDH, ext, ulData); break; case 3: - mcp2515_write_id(MCP_RXF3SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXF3SIDH, ext, ulData); break; case 4: - mcp2515_write_id(MCP_RXF4SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXF4SIDH, ext, ulData); break; case 5: - mcp2515_write_id(MCP_RXF5SIDH, ext, ulData); + mcp2515_write_mf(MCP_RXF5SIDH, ext, ulData); break; default: res = MCP2515_FAIL; } - res = mcp2515_setCANCTRL_Mode(MODE_NORMAL); + res = mcp2515_setCANCTRL_Mode(mcpMode); if(res > 0) { #if DEBUG_MODE - Serial.print("Enter normal mode fall\r\nSet filter fail!!\r\n"); + Serial.print("Entering Previous Mode Failure...\r\nSetting Filter Failure...\r\n"); #endif return res; } #if DEBUG_MODE - Serial.print("set Filter success!!\r\n"); + Serial.print("Setting Filter Successfull!!!\r\n"); #endif return res; @@ -643,7 +969,7 @@ INT8U MCP_CAN::init_Filt(INT8U num, INT8U ext, INT32U ulData) /********************************************************************************************************* ** Function name: setMsg -** Descriptions: set can message, such as dlc, id, dta[] and so on +** Descriptions: Set can message, such as dlc, id, dta[] and so on *********************************************************************************************************/ INT8U MCP_CAN::setMsg(INT32U id, INT8U ext, INT8U len, INT8U *pData) { @@ -653,12 +979,13 @@ INT8U MCP_CAN::setMsg(INT32U id, INT8U ext, INT8U len, INT8U *pData) m_nDlc = len; for(i = 0; i