Cover image
Try Now
2020-09-12

Micropython MCP2515 Pilote, portant de la bibliothèque Arduino MCP2515 peut interface

3 years

Works with Finder

4

Github Watches

14

Github Forks

28

Github Stars

micropython-mcp2515

Micropython MCP2515 driver, porting from Arduino MCP2515 CAN interface library

Getting Started

This MCP2515 driver could be used for any MCU or platform supported by Micropython, such as Pyboard, ESP32, ESP8286, etc.

"""
Following SPI drivers are supported, please adjust by your hardware
from .src import SPIESP8286 as SPI
from .src import SPIESP32 as SPI


The CAN driver can be initialized with default baudrate 10MHz
CAN(SPI(cs=YOUR_SPI_CS_PIN)) or
CAN(SPI(cs=YOUR_SPI_CS_PIN, baudrate=YOUR_DESIRED_BAUDRATE))

And here is an example to set filter for extended frame with ID 0x12345678
can.setFilter(RXF.RXF0, True, 0x12345678 | CAN_EFF_FLAG)
can.setFilterMask(MASK.MASK0, True, 0x1FFFFFFF | CAN_EFF_FLAG)
can.setFilterMask(MASK.MASK1, True, 0x1FFFFFFF | CAN_EFF_FLAG)
"""
import time

from src import (
    CAN,
    CAN_CLOCK,
    CAN_EFF_FLAG,
    CAN_ERR_FLAG,
    CAN_RTR_FLAG,
    CAN_SPEED,
    ERROR,
)
from src import SPIESP32 as SPI
from src import CANFrame


def main():
    # Initialization
    can = CAN(SPI(cs=23))

    # Configuration
    if can.reset() != ERROR.ERROR_OK:
        print("Can not reset for MCP2515")
        return
    if can.setBitrate(CAN_SPEED.CAN_500KBPS, CAN_CLOCK.MCP_8MHZ) != ERROR.ERROR_OK:
        print("Can not set bitrate for MCP2515")
        return
    if can.setNormalMode() != ERROR.ERROR_OK:
        print("Can not set normal mode for MCP2515")
        return

    # Prepare frames
    data = b"\x12\x34\x56\x78\x9A\xBC\xDE\xF0"
    sff_frame = CANFrame(can_id=0x7FF, data=data)
    sff_none_data_frame = CANFrame(can_id=0x7FF)
    err_frame = CANFrame(can_id=0x7FF | CAN_ERR_FLAG, data=data)
    eff_frame = CANFrame(can_id=0x12345678 | CAN_EFF_FLAG, data=data)
    eff_none_data_frame = CANFrame(can_id=0x12345678 | CAN_EFF_FLAG)
    rtr_frame = CANFrame(can_id=0x7FF | CAN_RTR_FLAG)
    rtr_with_eid_frame = CANFrame(can_id=0x12345678 | CAN_RTR_FLAG | CAN_EFF_FLAG)
    rtr_with_data_frame = CANFrame(can_id=0x7FF | CAN_RTR_FLAG, data=data)
    frames = [
        sff_frame,
        sff_none_data_frame,
        err_frame,
        eff_frame,
        eff_none_data_frame,
        rtr_frame,
        rtr_with_eid_frame,
        rtr_with_data_frame,
    ]

    # Read all the time and send message in each second
    end_time, n = time.ticks_add(time.ticks_ms(), 1000), -1
    while True:
        error, iframe = can.readMessage()
        if error == ERROR.ERROR_OK:
            print("RX  {}".format(iframe))

        if time.ticks_diff(time.ticks_ms(), end_time) >= 0:
            end_time = time.ticks_add(time.ticks_ms(), 1000)
            n += 1
            n %= len(frames)

            error = can.sendMessage(frames[n])
            if error == ERROR.ERROR_OK:
                print("TX  {}".format(frames[n]))
            else:
                print("TX failed with error code {}".format(error))


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        pass

Limitation

  • Since Micropython is not fast enough, MCP2515 might send duplicated messages when CAN speed is too fast or SPI speed is too low. This is because previous message will still be transmitted repeatedly when MCP2515's SPI CS is not disabled timely.

相关推荐

  • Joshua Armstrong
  • Confidential guide on numerology and astrology, based of GG33 Public information

  • https://suefel.com
  • Latest advice and best practices for custom GPT development.

  • Emmet Halm
  • Converts Figma frames into front-end code for various mobile frameworks.

  • Elijah Ng Shi Yi
  • Advanced software engineer GPT that excels through nailing the basics.

  • lumpenspace
  • Take an adjectivised noun, and create images making it progressively more adjective!

  • https://maiplestudio.com
  • Find Exhibitors, Speakers and more

  • Yusuf Emre Yeşilyurt
  • I find academic articles and books for research and literature reviews.

  • Carlos Ferrin
  • Encuentra películas y series en plataformas de streaming.

  • tomoyoshi hirata
  • Sony α7IIIマニュアルアシスタント

  • https://zenepic.net
  • Embark on a thrilling diplomatic quest across a galaxy on the brink of war. Navigate complex politics and alien cultures to forge peace and avert catastrophe in this immersive interstellar adventure.

  • apappascs
  • Découvrez la collection la plus complète et la plus à jour de serveurs MCP sur le marché. Ce référentiel sert de centre centralisé, offrant un vaste catalogue de serveurs MCP open-source et propriétaires, avec des fonctionnalités, des liens de documentation et des contributeurs.

  • ShrimpingIt
  • Manipulation basée sur Micropython I2C de l'exposition GPIO de la série MCP, dérivée d'Adafruit_MCP230XX

  • jae-jae
  • MCP Server pour récupérer le contenu de la page Web à l'aide du navigateur sans tête du dramwright.

  • ravitemer
  • Un puissant plugin Neovim pour gérer les serveurs MCP (Protocole de contexte modèle)

  • patruff
  • Pont entre les serveurs Olllama et MCP, permettant aux LLM locaux d'utiliser des outils de protocole de contexte de modèle

  • pontusab
  • La communauté du curseur et de la planche à voile, recherchez des règles et des MCP

  • av
  • Exécutez sans effort LLM Backends, API, Frontends et Services avec une seule commande.

  • appcypher
  • Serveurs MCP géniaux - une liste organisée de serveurs de protocole de contexte de modèle

  • Mintplex-Labs
  • L'application tout-en-un desktop et Docker AI avec chiffon intégré, agents AI, constructeur d'agent sans code, compatibilité MCP, etc.

  • chongdashu
  • Activer les clients adjoints AI comme Cursor, Windsurf et Claude Desktop pour contrôler le moteur Unreal à travers le langage naturel à l'aide du Protocole de contexte modèle (MCP).

    Reviews

    1 (1)
    Avatar
    user_hiEL9zqL
    2025-04-17

    The micropython-mcp2515 by jxltom is an excellent tool for integrating MCP2515 CAN controllers using MicroPython. It's well-documented and easy to use, making it perfect for hobbyists and professionals alike. The repository at https://github.com/jxltom/micropython-mcp2515 offers comprehensive information and examples, which are greatly helpful. Highly recommended for anyone working with CAN protocol and MicroPython!