mcp2515: Initial implementation; C; C++

The mcp2515 is a CAN bus controller.  It was developed using the Seeed
CAN bus shield.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson
2016-11-10 11:55:48 -07:00
parent 5eb8af6d70
commit fcb4d9d36d
23 changed files with 4129 additions and 0 deletions

View File

@ -327,6 +327,7 @@ add_example (ecezo)
add_example (mb704x)
add_example (rf22-server)
add_example (rf22-client)
add_example (mcp2515)
# These are special cases where you specify example binary, source file and module(s)
include_directories (${PROJECT_SOURCE_DIR}/src)
@ -369,3 +370,4 @@ if (OPENZWAVE_FOUND)
add_custom_example (tzemt400-example-cxx tzemt400.cxx ozw)
endif()
add_custom_example (nmea_gps_i2c_example-cxx nmea_gps_i2c.cxx nmea_gps)
add_custom_example (mcp2515-txrx-example-cxx mcp2515-txrx.cxx mcp2515)

View File

@ -0,0 +1,125 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <signal.h>
#include <upm_utilities.h>
#include "mcp2515.hpp"
using namespace std;
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main(int argc, char **argv)
{
signal(SIGINT, sig_handler);
// by default we just receive packets
bool do_tx = false;
// if an argument is given, we will transmit packets
if (argc > 1)
do_tx = true;
//! [Interesting]
// NOTE: This example assumes that only two devices are connected
// to the CAN bus, and that both devices are running this example;
// one in TX (transmit) mode, and one in RX (receive) mode.
// If this is not the case, then in rx mode you may see other
// packets from other devices, or not, depending on whether the
// speed is correct for the network. In tx mode, errors may be
// generated on the bus, especially if the CAN bus speed does not
// match the rest of the network.
// You should start the receiver example first. The transmitter
// example will fail after about 5 seconds (timeout) of not being
// able to transmit a message.
// Instantiate a MCP2515 on SPI bus 0 using a hw CS pin (-1).
upm::MCP2515 *sensor = new upm::MCP2515(0, 9);
// By default, after initialization, the baud rate is set to
// 50Kbps, and the mode is NORMAL, so we don't need to set any of
// that here.
// our TX payload. If transmitting, the first number will be
// incremented (and rollover) on each transmission.
string myPayload = "01234567";
while (shouldRun)
{
if (do_tx)
{
cout << "Loading a packet of 8 numbers (0-7) into a TX buffer..."
<< endl;
sensor->loadTXBuffer(MCP2515_TX_BUFFER0, 0, false, false,
myPayload);
// now lets try to transmit it
cout << "Transmitting packet..." << endl;
sensor->transmitBuffer(MCP2515_TX_BUFFER0, true);
myPayload[0]++;
cout << "Transmit successful" << endl;
cout << endl;
upm_delay_ms(500);
}
else
{
// RX mode
// Look for a packet waiting for us in RXB0
if (sensor->rxStatusMsgs() == MCP2515_RXMSG_RXB0)
{
cout << "Packet received in RXB0, decoding..." << endl;
// now lets retrieve and print it
sensor->getRXMsg(MCP2515_RX_BUFFER0);
sensor->printMsg();
cout << endl;
}
upm_delay_ms(100);
}
}
cout << "Exiting..." << endl;
delete sensor;
//! [Interesting]
return 0;
}

79
examples/c++/mcp2515.cxx Normal file
View File

@ -0,0 +1,79 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include "mcp2515.hpp"
using namespace std;
int main(int argc, char **argv)
{
//! [Interesting]
// Instantiate a MCP2515 on SPI bus 0 using a hw CS pin (-1).
upm::MCP2515 *sensor = new upm::MCP2515(0, -1);
cout << "Setting loopback mode..." << endl;
// set the mode to loopback mode. In loopback mode, transmitted
// packets are sent directly to an appropriate receive buffer
// without actually going out onto the CAN bus.
sensor->setOpmode(MCP2515_OPMODE_LOOPBACK);
// lets build up a packet and try loading it (8 bytes max).
string myPayload = "01234567";
cout << "Loading a packet of 8 numbers (0-7) into a TX buffer..." << endl;
sensor->loadTXBuffer(MCP2515_TX_BUFFER0, 0, false, false, myPayload);
// now lets try to transmit it
cout << "Transmitting packet..." << endl;
sensor->transmitBuffer(MCP2515_TX_BUFFER0, true);
cout << "Transmit successful" << endl;
// There should now be a packet waiting for us in RXB0
if (sensor->rxStatusMsgs() == MCP2515_RXMSG_RXB0)
{
cout << "Packet received in RXB0, decoding..." << endl;
// now lets retrieve and print it
sensor->getRXMsg(MCP2515_RX_BUFFER0);
sensor->printMsg();
}
else
{
cout << "No packet found, how strange." << endl;
}
cout << "Exiting..." << endl;
delete sensor;
//! [Interesting]
return 0;
}

View File

@ -141,6 +141,7 @@ add_example (ms5803)
add_example (ims)
add_example (ecezo)
add_example (mb704x)
add_example (mcp2515)
# Custom examples
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
@ -149,3 +150,4 @@ add_custom_example (lcm1602-parallel-example-c lcm1602-parallel.c lcm1602)
add_custom_example (rpr220-intr-example-c rpr220-intr.c rpr220)
add_custom_example (md-stepper-example-c md-stepper.c md)
add_custom_example (button_intr-example-c button_intr.c button)
add_custom_example (mcp2515-txrx-example-c mcp2515-txrx.c mcp2515)

144
examples/c/mcp2515-txrx.c Normal file
View File

@ -0,0 +1,144 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <upm_utilities.h>
#include "mcp2515.h"
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main(int argc, char **argv)
{
signal(SIGINT, sig_handler);
// by default we just receive packets
bool do_tx = false;
// if an argument is given, we will transmit packets
if (argc > 1)
do_tx = true;
//! [Interesting]
// NOTE: This example assumes that only two devices are connected
// to the CAN bus, and that both devices are running this example;
// one in TX (transmit) mode, and one in RX (receive) mode.
// If this is not the case, then in rx mode you may see other
// packets from other devices, or not, depending on whether the
// speed is correct for the network. In tx mode, errors may be
// generated on the bus, especially if the CAN bus speed does not
// match the rest of the network.
// You should start the receiver example first. The transmitter
// example will fail after about 5 seconds (timeout) of not being
// able to transmit a message.
// Instantiate a MCP2515 on SPI bus 0 using a hw CS pin (-1).
mcp2515_context sensor = mcp2515_init(0, -1);
if (!sensor)
{
printf("mcp2515_init() failed\n");
return 1;
}
// By default, after initialization, the baud rate is set to
// 50Kbps, and the mode is NORMAL, so we don't need to set any of
// that here.
if (!do_tx)
printf("RX mode, waiting for packets...\n");
// our TX payload. If transmitting, the first number will be
// incremented (and rollover) on each transmission.
uint8_t myPayload[8] = {0, 1, 2, 3, 4, 5, 6, 7};
while (shouldRun)
{
if (do_tx)
{
// lets build up a packet and try loading it.
printf("Loading a packet of 8 numbers into a tx buffer...\n");
if (mcp2515_load_tx_buffer(sensor, MCP2515_TX_BUFFER0, 0,
false, false, myPayload, 8))
{
printf("mcp2515_load_tx_buffer() failed\n");
return 1;
}
// now lets try to transmit it
printf("Transmitting packet...\n");
upm_result_t rv = 0;
if ((rv = mcp2515_transmit_buffer(sensor, MCP2515_TX_BUFFER0,
true)))
{
printf("mcp2515_transmit_buffer() failed with code %d\n", rv);
return 1;
}
// increment the first number for each transmission
myPayload[0]++;
printf("Transmit successful\n\n");
upm_delay_ms(500);
}
else
{
// RX mode
// Look for a packet waiting for us in RXB0
if (mcp2515_rx_status_msgs(sensor) == MCP2515_RXMSG_RXB0)
{
printf("Packet received in RXB0, decoding...\n");
// now lets retrieve it
MCP2515_MSG_T msg;
if (mcp2515_get_rx_msg(sensor, MCP2515_RX_BUFFER0, &msg))
{
printf("mcp2515_get_rx_msg() failed\n");
return 1;
}
mcp2515_print_msg(sensor, &msg);
printf("\n");
}
upm_delay_ms(100);
}
}
printf("Exiting...\n");
mcp2515_close(sensor);
//! [Interesting]
return 0;
}

101
examples/c/mcp2515.c Normal file
View File

@ -0,0 +1,101 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <stdio.h>
#include "mcp2515.h"
int main(int argc, char **argv)
{
//! [Interesting]
// Instantiate a MCP2515 on SPI bus 0 using a hw CS pin (-1).
mcp2515_context sensor = mcp2515_init(0, -1);
if (!sensor)
{
printf("mcp2515_init() failed\n");
return 1;
}
printf("Setting loopback mode...\n");
// set the mode to loopback mode. In loopback mode, transmitted
// packets are sent directly to an appropriate receive buffer
// without actually going out onto the CAN bus.
if (mcp2515_set_opmode(sensor, MCP2515_OPMODE_LOOPBACK))
{
printf("mcp2515_set_opmode(loopback) failed.\n");
return 1;
}
// lets build up a packet and try loading it.
uint8_t myPayload[8] = {0, 1, 2, 3, 4, 5, 6, 7};
printf("Loading a packet of 8 numbers (0-7) into a TX buffer...\n");
if (mcp2515_load_tx_buffer(sensor, MCP2515_TX_BUFFER0, 0, false, false,
myPayload, 8))
{
printf("mcp2515_load_tx_buffer() failed\n");
return 1;
}
// now lets try to transmit it
printf("Transmitting packet...\n");
upm_result_t rv = 0;
if ((rv = mcp2515_transmit_buffer(sensor, MCP2515_TX_BUFFER0, true)))
{
printf("mcp2515_transmit_buffer() failed with code %d\n", rv);
return 1;
}
printf("Transmit successful\n");
// There should now be a packet waiting for us in RXB0
if (mcp2515_rx_status_msgs(sensor) == MCP2515_RXMSG_RXB0)
{
printf("Packet received in RXB0, decoding...\n");
// now lets retrieve it
MCP2515_MSG_T msg;
if (mcp2515_get_rx_msg(sensor, MCP2515_RX_BUFFER0, &msg))
{
printf("mcp2515_get_rx_msg() failed\n");
return 1;
}
mcp2515_print_msg(sensor, &msg);
}
else
{
printf("No packet found, how strange.\n");
}
printf("Exiting...\n");
mcp2515_close(sensor);
//! [Interesting]
return 0;
}

View File

@ -160,6 +160,7 @@ add_example(MS5803_Example ms5803)
add_example(ECEZO_Example ecezo)
add_example(IMS_Example ims)
add_example(MB704X_Example mb704x)
add_example(MCP2515_Example mcp2515)
add_example_with_path(Jhd1313m1_lcdSample lcd i2clcd)
add_example_with_path(Jhd1313m1Sample lcd i2clcd)
@ -181,3 +182,4 @@ if (OPENZWAVE_FOUND)
add_example_with_path(TZEMT400_Example ozw ozw)
endif()
add_example_with_path(NMEAGPS_I2C_Example nmea_gps nmea_gps)
add_example_with_path(MCP2515_TXRX_Example mcp2515 mcp2515)

View File

@ -0,0 +1,73 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import upm_mcp2515.MCP2515;
public class MCP2515_Example
{
public static void main(String[] args) throws InterruptedException
{
// ! [Interesting]
// Instantiate a MCP2515 on SPI bus 0 using a hw CS pin (-1).
MCP2515 sensor = new MCP2515(0, -1);
// set the mode to loopback mode. In loopback mode, transmitted
// packets are sent directly to an appropriate receive buffer
// without actually going out onto the CAN bus.
sensor.setOpmode(upm_mcp2515.MCP2515_OPMODE_T.MCP2515_OPMODE_LOOPBACK);
// lets build up a packet and try loading it (8 bytes max).
String myPayload = "01234567";
System.out.println("Loading a packet of 8 numbers (0-7) into a TX buffer...");
sensor.loadTXBuffer(upm_mcp2515.MCP2515_TX_BUFFER_T.MCP2515_TX_BUFFER0,
0, false, false, myPayload);
// now lets try to transmit it
System.out.println("Transmitting packet...");
sensor.transmitBuffer(upm_mcp2515.MCP2515_TX_BUFFER_T.MCP2515_TX_BUFFER0,
true);
System.out.println("Transmit successful");
// There should now be a packet waiting for us in RXB0
if (sensor.rxStatusMsgs() == upm_mcp2515.MCP2515_RXMSG_T.MCP2515_RXMSG_RXB0)
{
System.out.println("Packet received in RXB0, decoding...");
// now lets retrieve and print it
sensor.getRXMsg(upm_mcp2515.MCP2515_RX_BUFFER_T.MCP2515_RX_BUFFER0);
sensor.printMsg();
}
else
{
System.out.println("No packet found, how strange.");
}
System.out.println("Exiting...");
// ! [Interesting]
}
}

View File

@ -0,0 +1,94 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import upm_mcp2515.MCP2515;
public class MCP2515_TXRX_Example
{
public static void main(String[] args) throws InterruptedException
{
// ! [Interesting]
// by default we just receive packets
Boolean do_tx = false;
if (args.length > 0)
do_tx = true;
// Instantiate a MCP2515 on SPI bus 0 using a hw CS pin (-1).
MCP2515 sensor = new MCP2515(0, -1);
// NOTE: This example assumes that only two devices are connected
// to the CAN bus, and that both devices are running this example;
// one in TX (transmit) mode, and one in RX (receive) mode.
// If this is not the case, then in rx mode you may see other
// packets from other devices, or not, depending on whether the
// speed is correct for the network. In tx mode, errors may be
// generated on the bus, especially if the CAN bus speed does not
// match the rest of the network.
// You should start the receiver example first. The transmitter
// example will fail after about 5 seconds (timeout) of not being
// able to transmit a message.
while (true)
{
if (do_tx)
{
// lets build up a packet and try loading it (8 bytes max).
String myPayload = "01234567";
System.out.println("Loading a packet of 8 numbers (0-7) into a TX buffer...");
sensor.loadTXBuffer(upm_mcp2515.MCP2515_TX_BUFFER_T.MCP2515_TX_BUFFER0,
0, false, false, myPayload);
// now lets try to transmit it
System.out.println("Transmitting packet...");
sensor.transmitBuffer(upm_mcp2515.MCP2515_TX_BUFFER_T.MCP2515_TX_BUFFER0,
true);
System.out.println("Transmit successful");
System.out.println();
Thread.sleep(500);
}
else
{
// There should now be a packet waiting for us in RXB0
if (sensor.rxStatusMsgs() == upm_mcp2515.MCP2515_RXMSG_T.MCP2515_RXMSG_RXB0)
{
System.out.println("Packet received in RXB0, decoding...");
// now lets retrieve and print it
sensor.getRXMsg(upm_mcp2515.MCP2515_RX_BUFFER_T.MCP2515_RX_BUFFER0);
sensor.printMsg();
System.out.println();
}
Thread.sleep(100);
}
}
// ! [Interesting]
}
}

View File

@ -0,0 +1,94 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var sensorObj = require('jsupm_mcp2515');
// Instantiate a MCP2515 on SPI bus 0 using a hw CS pin (-1).
var sensor = new sensorObj.MCP2515(0, -1);
// NOTE: This example assumes that only two devices are connected
// to the CAN bus, and that both devices are running this example;
// one in TX (transmit) mode, and one in RX (receive) mode.
// If this is not the case, then in rx mode you may see other
// packets from other devices, or not, depending on whether the
// speed is correct for the network. In tx mode, errors may be
// generated on the bus, especially if the CAN bus speed does not
// match the rest of the network.
// You should start the receiver example first. The transmitter
// example will fail after about 5 seconds (timeout) of not being
// able to transmit a message.
function transmit(sensor)
{
// lets build up a packet and try loading it (8 bytes max).
var myPayload = "01234567";
console.log("Loading a packet of 8 numbers (0-7) into a TX buffer...");
sensor.loadTXBuffer(sensorObj.MCP2515_TX_BUFFER0,
0, false, false, myPayload);
// now lets try to transmit it
console.log("Transmitting packet...");
sensor.transmitBuffer(sensorObj.MCP2515_TX_BUFFER0, true);
console.log("Transmit successful");
console.log();
}
function receive(sensor)
{
// There should now be a packet waiting for us in RXB0
if (sensor.rxStatusMsgs() == sensorObj.MCP2515_RXMSG_RXB0)
{
console.log("Packet received in RXB0, decoding...");
// now lets retrieve and print it
sensor.getRXMsg(sensorObj.MCP2515_RX_BUFFER0);
sensor.printMsg();
console.log();
}
}
// by default we just receive packets
if (process.argv.length > 2)
{
// transmit mode
interval = setInterval(function() { transmit(sensor); }, 500);
}
else
{
// receive mode
interval = setInterval(function() { receive(sensor); }, 100);
}
process.on('SIGINT', function()
{
sensor = null;
sensorObj.cleanUp();
sensorObj = null;
console.log("Exiting...");
process.exit(0);
});

View File

@ -0,0 +1,66 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var sensorObj = require('jsupm_mcp2515');
// Instantiate a MCP2515 on SPI bus 0 using a hw CS pin (-1).
var sensor = new sensorObj.MCP2515(0, -1);
// set the mode to loopback mode. In loopback mode, transmitted
// packets are sent directly to an appropriate receive buffer
// without actually going out onto the CAN bus.
sensor.setOpmode(sensorObj.MCP2515_OPMODE_LOOPBACK);
// lets build up a packet and try loading it (8 bytes max).
var myPayload = "01234567";
console.log("Loading a packet of 8 numbers (0-7) into a TX buffer...");
sensor.loadTXBuffer(sensorObj.MCP2515_TX_BUFFER0, 0, false, false, myPayload);
// now lets try to transmit it
console.log("Transmitting packet...");
sensor.transmitBuffer(sensorObj.MCP2515_TX_BUFFER0, true);
console.log("Transmit successful");
// There should now be a packet waiting for us in RXB0
if (sensor.rxStatusMsgs() == sensorObj.MCP2515_RXMSG_RXB0)
{
console.log("Packet received in RXB0, decoding...");
// now lets retrieve and print it
sensor.getRXMsg(sensorObj.MCP2515_RX_BUFFER0);
sensor.printMsg();
}
else
{
console.log("No packet found, how strange.");
}
console.log("Exiting...");
sensor = null;
sensorObj.cleanUp();
sensorObj = null;
process.exit(0);

99
examples/python/mcp2515-txrx.py Executable file
View File

@ -0,0 +1,99 @@
#!/usr/bin/python
# Author: Jon Trulson <jtrulson@ics.com>
# Copyright (c) 2016 Intel Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import print_function
import time, sys, signal, atexit
from upm import pyupm_mcp2515 as MCP2515
def main():
# Instantiate a MCP2515 on SPI bus 0 using a hw CS pin (-1).
sensor = MCP2515.MCP2515(0, -1)
## Exit handlers ##
# This function stops python from printing a stacktrace when you
# hit control-C
def SIGINTHandler(signum, frame):
raise SystemExit
# This function lets you run code on exit, including functions from sensor
def exitHandler():
print("Exiting")
sys.exit(0)
# Register exit handlers
atexit.register(exitHandler)
signal.signal(signal.SIGINT, SIGINTHandler)
# by default we just receive packets
do_tx = False;
if (len(sys.argv) > 1):
do_tx = True
# NOTE: This example assumes that only two devices are connected
# to the CAN bus, and that both devices are running this example;
# one in TX (transmit) mode, and one in RX (receive) mode.
# If this is not the case, then in rx mode you may see other
# packets from other devices, or not, depending on whether the
# speed is correct for the network. In tx mode, errors may be
# generated on the bus, especially if the CAN bus speed does not
# match the rest of the network.
# You should start the receiver example first. The transmitter
# example will fail after about 5 seconds (timeout) of not being
# able to transmit a message.
# By default, after initialization, the baud rate is set to
# 50Kbps, and the mode is NORMAL, so we don't need to set any of
# that here.
while (True):
if (do_tx):
print("Loading a packet of 8 numbers (0-7) into a TX buffer...")
myPayload = "01234567";
sensor.loadTXBuffer(MCP2515.MCP2515_TX_BUFFER0, 0, False,
False, myPayload);
# now lets try to transmit it
print("Transmitting packet...")
sensor.transmitBuffer(MCP2515.MCP2515_TX_BUFFER0, True)
print("Transmit successful")
print()
time.sleep(.5)
else:
# RX mode
# Look for a packet waiting for us in RXB0
if (sensor.rxStatusMsgs() == MCP2515.MCP2515_RXMSG_RXB0):
print("Packet received in RXB0, decoding...")
# now lets retrieve and print it
sensor.getRXMsg(MCP2515.MCP2515_RX_BUFFER0)
sensor.printMsg();
print()
print("Exiting...")
if __name__ == '__main__':
main()

79
examples/python/mcp2515.py Executable file
View File

@ -0,0 +1,79 @@
#!/usr/bin/python
# Author: Jon Trulson <jtrulson@ics.com>
# Copyright (c) 2016 Intel Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import print_function
import time, sys, signal, atexit
from upm import pyupm_mcp2515 as MCP2515
def main():
# Instantiate a MCP2515 on SPI bus 0 using a hw CS pin (-1).
sensor = MCP2515.MCP2515(0, -1)
## Exit handlers ##
# This function stops python from printing a stacktrace when you
# hit control-C
def SIGINTHandler(signum, frame):
raise SystemExit
# This function lets you run code on exit, including functions from sensor
def exitHandler():
print("Exiting")
sys.exit(0)
# Register exit handlers
atexit.register(exitHandler)
signal.signal(signal.SIGINT, SIGINTHandler)
print("Setting loopback mode...")
# set the mode to loopback mode. In loopback mode, transmitted
# packets are sent directly to an appropriate receive buffer
# without actually going out onto the CAN bus.
sensor.setOpmode(MCP2515.MCP2515_OPMODE_LOOPBACK);
# lets build up a packet and try loading it (8 bytes max).
myPayload = "01234567";
print("Loading a packet of 8 numbers (0-7) into a TX buffer...")
sensor.loadTXBuffer(MCP2515.MCP2515_TX_BUFFER0, 0, False, False, myPayload);
# now lets try to transmit it
print("Transmitting packet...")
sensor.transmitBuffer(MCP2515.MCP2515_TX_BUFFER0, True);
print("Transmit successful")
# There should now be a packet waiting for us in RXB0
if (sensor.rxStatusMsgs() == MCP2515.MCP2515_RXMSG_RXB0):
print("Packet received in RXB0, decoding...")
# now lets retrieve and print it
sensor.getRXMsg(MCP2515.MCP2515_RX_BUFFER0);
sensor.printMsg();
else:
print("No packet found, how strange.")
print("Exiting...")
if __name__ == '__main__':
main()