{"id":20910532,"url":"https://github.com/ziteh/mcp2515-driver","last_synced_at":"2025-08-08T04:13:51.731Z","repository":{"id":130511275,"uuid":"545218437","full_name":"ziteh/mcp2515-driver","owner":"ziteh","description":"Platform independent MCP2515 CAN interface library","archived":false,"fork":false,"pushed_at":"2023-10-31T12:15:16.000Z","size":303,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-02T19:46:21.932Z","etag":null,"topics":["can-bus","library","mcp2515","spi","stm32"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ziteh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-04T01:36:12.000Z","updated_at":"2025-05-09T18:54:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"05055ae5-df4c-4d7e-9485-288f4f61f8fd","html_url":"https://github.com/ziteh/mcp2515-driver","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/ziteh/mcp2515-driver","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziteh%2Fmcp2515-driver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziteh%2Fmcp2515-driver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziteh%2Fmcp2515-driver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziteh%2Fmcp2515-driver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ziteh","download_url":"https://codeload.github.com/ziteh/mcp2515-driver/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziteh%2Fmcp2515-driver/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269361856,"owners_count":24404433,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-08T02:00:09.200Z","response_time":72,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["can-bus","library","mcp2515","spi","stm32"],"created_at":"2024-11-18T14:15:44.173Z","updated_at":"2025-08-08T04:13:51.710Z","avatar_url":"https://github.com/ziteh.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MCP2515 Driver\n\nThis is a platform independent MCP2515 CAN interface library.\n\n\u003e This library was modified under the MIT license from [autowp/arduino-mcp2515](https://github.com/autowp/arduino-mcp2515).\n\n## SPI Config\n\n- SPI mode 0 (CPOL=0, CPHA=0) or/and 1 (CPOL=1, CPHA=1).\n- Data frame format: 8-bit.\n- Data order: MSB first.\n- Max speed: 10 MHz.\n\n## Initialization\n\nTo create connection with MCP2515 provide SPI control functions, baudrate and mode.\n\nThe available modes are listed as follows:\n```C++\nmcp2515.setNormalMode();\nmcp2515.setLoopbackMode();\nmcp2515.setListenOnlyMode();\n```\n\nThe available baudrates are listed as follows:\n```C++\nenum CAN_SPEED {\n    CAN_5KBPS,\n    CAN_10KBPS,\n    CAN_20KBPS,\n    CAN_31K25BPS,\n    CAN_33KBPS,\n    CAN_40KBPS,\n    CAN_50KBPS,\n    CAN_80KBPS,\n    CAN_83K3BPS,\n    CAN_95KBPS,\n    CAN_100KBPS,\n    CAN_125KBPS,\n    CAN_200KBPS,\n    CAN_250KBPS,\n    CAN_500KBPS,\n    CAN_1000KBPS\n};\n```\n\nExample of initialization:\n```C++\nvoid spiSelect(void)\n{\n  gpio_clear(GPIO_CS_PORT, GPIO_CS_PIN); /* CS pin low. */\n}\n\nvoid spiDeselect(void)\n{\n  while (!(SPI_SR(SPI1) \u0026 SPI_SR_TXE));\n  while ((SPI_SR(SPI1) \u0026 SPI_SR_BSY));\n  gpio_set(GPIO_CS_PORT, GPIO_CS_PIN); /* CS pin high. */\n}\n\nuint8_t spiTransfer(uint8_t data)\n{\n  uint16_t rec = spi_xfer(SPI1, data); /* SPI data write and read. */\n  return rec \u0026 0xFF;\n}\n\nvoid mcp2515Init(void)\n{\n  MCP2515 mcp2515(\u0026spiSelect, \u0026spiDeselect, \u0026spiTransfer, \u0026delay);\n\n  mcp2515.reset();\n  mcp2515.setBitrate(CAN_125KBPS);\n  mcp2515.setNormalMode();\n}\n```\n\nYou can also set oscillator frequency for module when setting bitrate:\n```C++\nmcp2515.setBitrate(CAN_125KBPS, MCP_16MHZ);\n```\n\nThe available clock speeds are listed as follows:\n```C++\nenum CAN_CLOCK {\n    MCP_20MHZ,\n    MCP_16MHZ,\n    MCP_8MHZ\n};\n```\n\nDefault value is `MCP_8MHZ`\n\n*Note*: To transfer data on high speed of CAN interface via UART dont forget to update UART baudrate as necessary.\n\n## Frame data format\n\nLibrary uses Linux-like structure to store can frames;\n\n```C++\nstruct can_frame {\n    uint32_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */\n    uint8_t  can_dlc;\n    uint8_t  data[8];\n};\n```\n\nFor additional information see [SocketCAN](https://www.kernel.org/doc/Documentation/networking/can.txt)\n\n## Send Data\n\n```C++\nMCP2515::ERROR sendMessage(const MCP2515::TXBn txbn, const struct can_frame *frame);\nMCP2515::ERROR sendMessage(const struct can_frame *frame);\n```\n\nThis is a function to send data onto the bus.\n\nFor example, In the 'send' example, we have:\n\n```C++\nstruct can_frame frame;\nframe.can_id = 0x000;\nframe.can_dlc = 4;\nframe.data[0] = 0xFF;\nframe.data[1] = 0xFF;\nframe.data[2] = 0xFF;\nframe.data[3] = 0xFF;\n\n/* send out the message to the bus and\ntell other devices this is a standard frame from 0x00. */\nmcp2515.sendMessage(\u0026frame);\n```\n\n```C++\nstruct can_frame frame;\nframe.can_id = 0x12345678 | CAN_EFF_FLAG;\nframe.can_dlc = 2;\nframe.data[0] = 0xFF;\nframe.data[1] = 0xFF;\n\n/* send out the message to the bus using second TX buffer and\ntell other devices this is a extended frame from 0x12345678. */\nmcp2515.sendMessage(MCP2515::TXB1, \u0026frame);\n```\n\n## Receive Data\n\nThe following function is used to receive data on the 'receive' node:\n\n```C++\nMCP2515::ERROR readMessage(const MCP2515::RXBn rxbn, struct can_frame *frame);\nMCP2515::ERROR readMessage(struct can_frame *frame);\n```\n\nIn conditions that masks and filters have been set. This function can only get frames that meet the requirements of masks and filters.\n\nYou can choise one of two method to receive: interrupt-based and polling\n\nExample of poll read\n\n```C++\nstruct can_frame frame;\n\nvoid loop() {\n    if (mcp2515.readMessage(\u0026frame) == MCP2515::ERROR_OK) {\n        // frame contains received message\n    }\n}\n```\n\nExample of interrupt based read\n\n```C++\nvolatile bool interrupt = false;\nstruct can_frame frame;\n\nvoid irqHandler() {\n    interrupt = true;\n}\n\nvoid setup() {\n    ...\n    attachInterrupt(0, irqHandler, FALLING);\n}\n\nvoid loop() {\n    if (interrupt) {\n        interrupt = false;\n\n        uint8_t irq = mcp2515.getInterrupts();\n\n        if (irq \u0026 MCP2515::CANINTF_RX0IF) {\n            if (mcp2515.readMessage(MCP2515::RXB0, \u0026frame) == MCP2515::ERROR_OK) {\n                // frame contains received from RXB0 message\n            }\n        }\n\n        if (irq \u0026 MCP2515::CANINTF_RX1IF) {\n            if (mcp2515.readMessage(MCP2515::RXB1, \u0026frame) == MCP2515::ERROR_OK) {\n                // frame contains received from RXB1 message\n            }\n        }\n    }\n}\n```\n\n\n## Set Receive Mask and Filter\n\nThere are 2 receive mask registers and 5 filter registers on the controller chip that guarantee you get data from the target device. They are useful especially in a large network consisting of numerous nodes.\n\nWe provide two functions for you to utilize these mask and filter registers. They are:\n\n```C++\nMCP2515::ERROR setFilterMask(const MASK mask, const bool ext, const uint32_t ulData)\nMCP2515::ERROR setFilter(const RXF num, const bool ext, const uint32_t ulData)\n```\n\n**MASK mask** represents one of two mask `MCP2515::MASK0` or `MCP2515::MASK1`\n\n**RXF num** represents one of six acceptance filters registers from `MCP2515::RXF0` to `MCP2515::RXF5`\n\n**ext** represents the status of the frame. `false` means it's a mask or filter for a standard frame. `true` means it's for a extended frame.\n\n**ulData** represents the content of the mask of filter.\n\n----\n\nThis software is written by loovee ([luweicong@seeed.cc](luweicong@seeed.cc \"luweicong@seeed.cc\")) for seeed studio,  \nUpdated by Dmitry ([https://github.com/autowp](https://github.com/autowp \"https://github.com/autowp\")),  \nModified by ZiTe ([https://github.com/ziteh](https://github.com/ziteh)),  \nand is licensed under [The MIT License](http://opensource.org/licenses/mit-license.php). Check [`LICENSE`](LICENSE) for more information.  \n\nContributing to this software is warmly welcomed. You can do this basically by  \n[forking](https://help.github.com/articles/fork-a-repo), committing modifications and then [pulling requests](https://help.github.com/articles/using-pull-requests) (follow the links above  \nfor operating guide). Adding change log and your contact into file header is encouraged.  \nThanks for your contribution.\n\nSeeed Studio is an open hardware facilitation company based in Shenzhen, China.  \nBenefiting from local manufacture power and convenient global logistic system,  \nwe integrate resources to serve new era of innovation. Seeed also works with  \nglobal distributors and partners to push open hardware movement.  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziteh%2Fmcp2515-driver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fziteh%2Fmcp2515-driver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziteh%2Fmcp2515-driver/lists"}