Circuit Python mcp2515 libary

Thread Starter

Rynxie

Joined Nov 17, 2022
9
Hey guys i want to use circuitpython mcp2515 libary but i am ultimate begginer and couldn't find any examplesis there any one who can help me? thanks!!
 

Ya’akov

Joined Jan 27, 2019
6,858
Welcome to AAC.

This usage example is from the Adafruit website, and they have full API documentation here. You‘ll have to be more specific for someone to offer help.

Usage Example:
import board
import busio
import digitalio
import adafruit_mcp2515

CS_PIN = 5
cs = digitalio.DigitalInOut(board.D5)
cs.direction = digitalio.Direction.OUTPUT
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
mcp = adafruit_mcp2515.MCP2515(spi, cs)
What are you trying to do?
What have you tried?
What “didn’t work”?
 

Thread Starter

Rynxie

Joined Nov 17, 2022
9
Welcome to AAC.

This usage example is from the Adafruit website, and they have full API documentation here. You‘ll have to be more specific for someone to offer help.

Usage Example:
import board
import busio
import digitalio
import adafruit_mcp2515

CS_PIN = 5
cs = digitalio.DigitalInOut(board.D5)
cs.direction = digitalio.Direction.OUTPUT
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
mcp = adafruit_mcp2515.MCP2515(spi, cs)
What are you trying to do?
What have you tried?
What “didn’t work”?
Hi thank u for repliying

I am trying to create a can bus communcation using mcp2515 between Raspberry pi pico and Arduino Uno i couldn't try something because arduino's mcp2515 libary look like makes job totally diffrent then adafruit's mcp2515 libary
 

Ya’akov

Joined Jan 27, 2019
6,858
Hi thank u for repliying

I am trying to create a can bus communcation using mcp2515 between Raspberry pi pico and Arduino Uno i couldn't try something because arduino's mcp2515 libary look like makes job totally diffrent then adafruit's mcp2515 libary
First, which Arduino board are you using? You can use Circuit Python on more modern Arduino-made boards and reduce the confusion with a common code base.

Second, the libraries may well have different interfaces, but they are for the same hardware, so there has to be a correspondence between them. While one might offer more functions than the other, or name the shared ones differently, they are both talking to the MCP2515 and so necessarily have to do the same thing.

It is very hard to help with such a nebulous problem. If you explain what problem you are trying to solve (not the problem of communicating on CAN bus, which is a solution to some other problem) and what you have tried, and how it “didn’t work”, then helping is possible.

On the other hand you might find a generous soul who has done just what you are asking about and is willing to teach you that narrow thing, or more likely, effectively do it for you. On the other hand, while that is possible but unlikely, presenting the trouble you are having when you try is likely to get you some help.

So I encourage you to ask specific questions instead of generally saying “I can’t figure this out”. That’s not going to get you much, I am afraid.
 

Irving

Joined Jan 30, 2016
3,190
I'm not sure why you can't figure this out, newbie or not. Or is that you have no experience of using libraries associated with communication hardware? As Ya'akov said, its hard to advise you how to get somewhere without knowing where you are starting from!

In the simplest form, irrespective of library used, your code has to:
1. initialise the MCP2515 hardware
2a. prepare the transmit data and send the message; and/or
2b. prepare to receive a message, wait for it to arrive, then decode it

Initialising the hardware
The main difference between the libraries is configuring the hardware. In the Adafruit library there is one call to the constructor to configure the SPI chip select (CS) pin, CAN bus bitrate and a few other items that can be ignored and left to the default values. In the Arduino library only the CS pin is configured in the constructor, the bitrate is set separately in a subsequent call.

Depending on your hardware there may be other necessary calls, not part of the CAN Library, to select which SPI bus is being used for example.

Sending a message is just a case of filling in the packet data and ID and calling the sendmessage procedure. In the Arduino library, you fill in a message structure and pass it to the procedure. In the Adafruit library you pass the data as parameters to a procedure to create the packet. CANBus is broadcast system, the ID relates to the message-type not a device address - any device can listen for and act on a specific message-ID.

Receiving a message in both these examples uses polling to see if a message is waiting; if so, the message is read and processed. A high-performance real-time system would use interrupts for message reception, but that's too complex for here.

MCP2515 setup, Arduino-style:
#include <SPI.h>          //Library for using SPI Communication
#include <mcp2515.h>      //Library for using CAN Communication
#define CS_PIN 10

MCP2515 mcp2515(CS_PIN); //declare the MCP device and its chip select

void setup()
{
  while (!Serial);  //acquire serial interface & initialise
  Serial.begin(115200);

//GPIO setup
//nothing needed. CS_PIN GPIO configured & enabled by MCP2515 library by default

//SPI setup
  //nothing needed, SPI configured & enabled  by MCP2515 library by default

//MCP2515 setup
  mcp2515.reset();
  mcp2515.setBitrate(CAN_250KBPS, MCP_8MHZ); //Sets CAN at speed 250KBPS and Clock 8MHz
  mcp2515.setNormalMode();
}

void loop(){
  //send a standard message
  struct can_frame canMsgToSend;  // declare a message structure

  //prepare the message
  canMsgToSend.id = 0x123; //11-bit ID for standard frame
  canMsgToSend.data[0] = 'H'; // message data
  canMsgToSend.data[1] = 'e'; 
  canMsgToSend.data[2] = 'l';
  canMsgToSend.data[3] = 'l';
  canMsgToSend.data[4] = 'o';
  canMgToSend.dlc = 5;  //data length

  //send it
  mcp2515.sendMessage(&canMsgToSend);

  //receive a message
  struct can_frame canMsgToRecv;  // declare a message structure

  if (mcp2515.readMessage(&canMsgToRecv) == MCP2515::ERROR_OK) { // message waiting
    Serial.print("Message ID: " ); Serial.print(canMsgToRecv.id, HEX);
    Serial.print("Message length: "); Serial.print(canMsgToRecv.dlc, DEC);

    for (int i = 0; i<canMsgToRecv.can_dlc; i++)  {  // print the data
      Serial.print(canMsgToRecv.data[i],HEX);
      Serial.print(" ");
    }
    Serial.println();
  }
  delay(100);//send messages approx 10 per sec.
}
MCP2515 setup, Adafruit circuitpython-style:
import board
import busio
import digitalio
import adafruit_mcp2515
import time

#GPIO ssetup
CS_PIN = 5
cs = digitalio.DigitalInOut(board.D5)
cs.direction = digitalio.Direction.OUTPUT

#SPI setup
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)

#MCP2515 setup
mcp2515 = adafruit_mcp2515.MCP2515(spi, cs) #defaults to 250kps & NormalMode

while true:
#send a standard message

#prepare the message
  msgToSend = Message(id=0x123, data=b"Hello", extended=False)

#send it
  mcp2515.send(msgToSend)

#receive a message
  msgToRecv = mcp2515.read_message()
  if msgToRecv != None:
    print("Message ID ", hex(msgToRecv.id))
    if isinstance(msgToRecv , Message):
      print("message data:", msgToRecv.data)

time.sleep(0.1) #100mS
 

Thread Starter

Rynxie

Joined Nov 17, 2022
9
I'm not sure why you can't figure this out, newbie or not. Or is that you have no experience of using libraries associated with communication hardware? As Ya'akov said, its hard to advise you how to get somewhere without knowing where you are starting from!

In the simplest form, irrespective of library used, your code has to:
1. initialise the MCP2515 hardware
2a. prepare the transmit data and send the message; and/or
2b. prepare to receive a message, wait for it to arrive, then decode it

Initialising the hardware
The main difference between the libraries is configuring the hardware. In the Adafruit library there is one call to the constructor to configure the SPI chip select (CS) pin, CAN bus bitrate and a few other items that can be ignored and left to the default values. In the Arduino library only the CS pin is configured in the constructor, the bitrate is set separately in a subsequent call.

Depending on your hardware there may be other necessary calls, not part of the CAN Library, to select which SPI bus is being used for example.

Sending a message is just a case of filling in the packet data and ID and calling the sendmessage procedure. In the Arduino library, you fill in a message structure and pass it to the procedure. In the Adafruit library you pass the data as parameters to a procedure to create the packet. CANBus is broadcast system, the ID relates to the message-type not a device address - any device can listen for and act on a specific message-ID.

Receiving a message in both these examples uses polling to see if a message is waiting; if so, the message is read and processed. A high-performance real-time system would use interrupts for message reception, but that's too complex for here.

MCP2515 setup, Arduino-style:
#include <SPI.h>          //Library for using SPI Communication
#include <mcp2515.h>      //Library for using CAN Communication
#define CS_PIN 10

MCP2515 mcp2515(CS_PIN); //declare the MCP device and its chip select

void setup()
{
  while (!Serial);  //acquire serial interface & initialise
  Serial.begin(115200);

//GPIO setup
//nothing needed. CS_PIN GPIO configured & enabled by MCP2515 library by default

//SPI setup
  //nothing needed, SPI configured & enabled  by MCP2515 library by default

//MCP2515 setup
  mcp2515.reset();
  mcp2515.setBitrate(CAN_250KBPS, MCP_8MHZ); //Sets CAN at speed 250KBPS and Clock 8MHz
  mcp2515.setNormalMode();
}

void loop(){
  //send a standard message
  struct can_frame canMsgToSend;  // declare a message structure

  //prepare the message
  canMsgToSend.id = 0x123; //11-bit ID for standard frame
  canMsgToSend.data[0] = 'H'; // message data
  canMsgToSend.data[1] = 'e';
  canMsgToSend.data[2] = 'l';
  canMsgToSend.data[3] = 'l';
  canMsgToSend.data[4] = 'o';
  canMgToSend.dlc = 5;  //data length

  //send it
  mcp2515.sendMessage(&canMsgToSend);

  //receive a message
  struct can_frame canMsgToRecv;  // declare a message structure

  if (mcp2515.readMessage(&canMsgToRecv) == MCP2515::ERROR_OK) { // message waiting
    Serial.print("Message ID: " ); Serial.print(canMsgToRecv.id, HEX);
    Serial.print("Message length: "); Serial.print(canMsgToRecv.dlc, DEC);

    for (int i = 0; i<canMsgToRecv.can_dlc; i++)  {  // print the data
      Serial.print(canMsgToRecv.data[i],HEX);
      Serial.print(" ");
    }
    Serial.println();
  }
  delay(100);//send messages approx 10 per sec.
}
MCP2515 setup, Adafruit circuitpython-style:
import board
import busio
import digitalio
import adafruit_mcp2515
import time

#GPIO ssetup
CS_PIN = 5
cs = digitalio.DigitalInOut(board.D5)
cs.direction = digitalio.Direction.OUTPUT

#SPI setup
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)

#MCP2515 setup
mcp2515 = adafruit_mcp2515.MCP2515(spi, cs) #defaults to 250kps & NormalMode

while true:
#send a standard message

#prepare the message
  msgToSend = Message(id=0x123, data=b"Hello", extended=False)

#send it
  mcp2515.send(msgToSend)

#receive a message
  msgToRecv = mcp2515.read_message()
  if msgToRecv != None:
    print("Message ID ", hex(msgToRecv.id))
    if isinstance(msgToRecv , Message):
      print("message data:", msgToRecv.data)

time.sleep(0.1) #100mS
thank u sir i am a newbie and i am stugling a lot
 
Top