Skip to content

can mcp2515: spi can controller driver#6276

Closed
vincent-d wants to merge 2 commits intoRIOT-OS:masterfrom
OTAkeys:pr/mcp2515
Closed

can mcp2515: spi can controller driver#6276
vincent-d wants to merge 2 commits intoRIOT-OS:masterfrom
OTAkeys:pr/mcp2515

Conversation

@vincent-d
Copy link
Copy Markdown
Member

This driver is compliant with the candev interface and this PR is based on #5793 .

It has been tested on our own hardware.

The mcp2515 needs an external clock which can be provided wither by a crystal or by the host MCU.

A can_conf_mcp2515.h file is needed and must contains at least the following:

#include "candev_mcp2515.h"

const static candev_mcp2515_conf_t can_config_candev_mcp2515[] = {
    {
        .spi = SPI_1,
        .cs_pin = xxx,
        .rst_pin = xxx,
        .int_pin = xxx,
    }
};

#define CANDEV_MCP2515_NUMOF (sizeof(can_config_candev_mcp2515) / sizeof(candev_mcp2515_conf_t))

#define CANDEV_MCP2515_CLOCK 20000000U

@LudwigKnuepfer LudwigKnuepfer added Area: drivers Area: Device drivers Type: new feature The issue requests / The PR implemements a new feature for RIOT labels Dec 30, 2016
@LudwigKnuepfer
Copy link
Copy Markdown
Member

If someone from Berlin has time to review and test but needs hardware: feel free to contact me.

@OlegHahm OlegHahm added CI: needs squashing Commits in this PR need to be squashed; If set, CI systems will mark this PR as unmergable State: waiting for other PR State: The PR requires another PR to be merged first labels Jan 3, 2017
@vincent-d vincent-d force-pushed the pr/mcp2515 branch 2 times, most recently from 5f0c589 to b6f79e2 Compare February 6, 2017 13:02
@vincent-d
Copy link
Copy Markdown
Member Author

Rebased and adapted with the new autoinit process from #5793, and the new SPI interface!

@LudwigKnuepfer
Copy link
Copy Markdown
Member

Since no review has happened so far, why don't you squash commits before anyone starts to review?

@vincent-d
Copy link
Copy Markdown
Member Author

mcp2515 part is squashed

@vincent-d vincent-d force-pushed the pr/mcp2515 branch 3 times, most recently from c02f87c to fe54ff0 Compare March 1, 2017 12:33
@vincent-d vincent-d force-pushed the pr/mcp2515 branch 2 times, most recently from d5a8273 to ce1892a Compare March 23, 2017 14:45
@vincent-d vincent-d force-pushed the pr/mcp2515 branch 2 times, most recently from 1a9d198 to 6b7dc4a Compare April 13, 2017 08:15
@aabadie aabadie modified the milestone: Release 2017.07 Jun 26, 2017
Vincent Dupont added 2 commits June 26, 2017 16:19
@vincent-d vincent-d removed this from the Release 2018.04 milestone Apr 11, 2018
@tcschmidt
Copy link
Copy Markdown
Member

@LudwigKnuepfer if you have the hardware and mind testing yourself, why don't you ask @mfrey for assistance?

@tcschmidt
Copy link
Copy Markdown
Member

@LudwigKnuepfer @A-Paul ping?

@A-Paul
Copy link
Copy Markdown
Member

A-Paul commented Jun 15, 2018

@aabadie , @lebrush and @haukepetersen. Is there a chance to review this PR?
I can do some reviewing based on code conventions but I have no insight into CAN. Since you have already been involved in #5793 you might be more effective here.

Copy link
Copy Markdown
Member

@lebrush lebrush left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't judge if it works but codewise looks neat and clean. I added some comments and questions regarding code and style (so no approve or refuse from my side).

/** masks list */
uint32_t masks[MCP2515_RX_MAILBOXES];
/** filters list */
canid_t filter_ids[MCP2515_RX_MAILBOXES][MCP2515_FILTERS_MB1];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two filter_ids too much, which will never be used. The construction MCP2515_FILTERS_MB0 * mailbox_index + filter_pos is used to interface with the mcp2515, I think the same construction could be used to access memory and two positions could be saved.


static inline int _max_filters(int mailbox)
{
return mailbox == 0 ? MCP2515_FILTERS_MB0 : MCP2515_FILTERS_MB1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parenthesis. I've been quite a bit off-line, maybe this is accepted now :-)


void candev_mcp2515_init(candev_mcp2515_t *dev, const candev_mcp2515_conf_t *conf)
{
memset(dev, 0, sizeof(*dev));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dev not checked against NULL

i guess it's assumed that it will never be NULL. I remember in some pieces of RIOT's code the doxygen claims something like @pre (dev != NULL)` to make the user aware (and then point to a usage bug and not a code issue)

memcpy(&dev->candev.bittiming, &timing, sizeof(timing));
/* configure filters to be closed */
for (int mailbox = 0; mailbox < MCP2515_RX_MAILBOXES; mailbox++) {
dev->masks[mailbox] = 0;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these loops are not necessary. You already set all dev struct to 0 on the first line of the method.

}
}
res = mcp2515_set_mode(dev, MODE_NORMAL);
return res;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you can remove res and simply return mcp2515_set_mode(...)

int res = mcp2515_spi_read(dev, MCP2515_CANSTAT, &mode, 1);
if (res == 0) {
//TODO: error handling of extra information in mode result
result = (enum mcp2515_mode) mode & MCP2515_CANSTAT_OPMOD_MASK;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add parenthesis


mcp2515_spi_bitmod(dev, MCP2515_CANCTRL, MCP2515_CANCTRL_REQOP_MASK, (uint8_t) mode);
int cnt = 0;
while (cur_mode != mode && cnt++ < 2) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic number 2

#define CAN_IRQ_PORTREN P1REN
#define CAN_IRQ_PORTIES P1IES
#define CAN_IRQ_PORTIE P1IE
#define CAN_IRQ_PORTIFG P1IFG
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these defines board/cpu specific?

FEATURES_REQUIRED += periph_gpio
endif

ifneq (,$(filter ltc4150,$(USEMODULE)))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this belong here?


DEBUG("Inside mcp2515 set opt=%d\n", opt);
switch (opt) {
case CANOPT_BITTIMING: /**< bit timing parameter */
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why doxygen comment?

@A-Paul
Copy link
Copy Markdown
Member

A-Paul commented Jun 21, 2018

@vincent-d, I apologize for the delay. Hopefully you're still interested to get this driver into RIOT.

I made an approach to come as close as possible to run the test with a real device (although I havn't one yet). Both, with a checked out pure branch and rebased onto master. But build failed.

I also have no clue what to do when the shell will be running. And "tests/driver_mcp2515/README.md" is a litlle bit terse.

Copy link
Copy Markdown
Member

@A-Paul A-Paul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compiling the test application faiies.
In the initial comment you mention creating a "can_conf_mcp2515.h". Where should this be placed?
There seems to be not location in the code where this file is referenced.
It defines a variable of type candev_mcp2515_conf_t, same as in the "main.c" where the unrsolved sysbol are. Why is this defined twice?


static const candev_mcp2515_conf_t conf = {
.spi = SPI_1,
.cs_pin = CAN2_SS_PIN,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Causes a compile error as undeclared. Should it be replaced because names have changed or defined by the user?

static const candev_mcp2515_conf_t conf = {
.spi = SPI_1,
.cs_pin = CAN2_SS_PIN,
.rst_pin = CAN2_RST_PIN,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as in line 46.

.spi = SPI_1,
.cs_pin = CAN2_SS_PIN,
.rst_pin = CAN2_RST_PIN,
.int_pin = CAN2_INT_PIN,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as in line 46.



static const shell_command_t shell_commands[] = {
{ "send", "send some data", _send },
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_send causes a compile error as undeclared.


static const shell_command_t shell_commands[] = {
{ "send", "send some data", _send },
{ "receive", "receive some data", _receive },
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_receive causes a compile error as undeclared.

@vincent-d
Copy link
Copy Markdown
Member Author

I'm gonna need some time to get back into this, it's been quite a long time ;) Thanks for taking a look at it anyway.

We actually don't use it in our product anymore, but I still think this driver is useful as this device is widely used.

If I remember correctly we wanted to rework or remove this test app, as it actually should be tested through the stack.

I can't promise I'll work on this soon as it's not anymore on top of my priority list.

@vincent-d vincent-d removed the State: waiting for other PR State: The PR requires another PR to be merged first label Jun 21, 2018
@smlng
Copy link
Copy Markdown
Member

smlng commented Aug 8, 2018

@vincent-d I have the hardware and tried to test it but couldn't make it work. I connected it to a CAN bus where I'm certain that CAN messages are send on it from another (non-RIOT) device, but the receive IRQ and its handler on the MCP2515 never fired.

Could be SPI related though, but the SPI is working - I tested with another device driver which worked fine.

@A-Paul
Copy link
Copy Markdown
Member

A-Paul commented Aug 14, 2018

@smlng what board are you using and which code changes have you made?

@smlng
Copy link
Copy Markdown
Member

smlng commented Aug 14, 2018

I'm using a Bluepill, I just made a quick test no in-depth debugging, my changes are here https://github.com/smlng/RIOT/tree/testing/mcp2515

@tcschmidt
Copy link
Copy Markdown
Member

@smlng @A-Paul could you document the state of the review with corresponding labels?

@A-Paul A-Paul added the State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet label Nov 5, 2018
@stale
Copy link
Copy Markdown

stale bot commented Aug 10, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions.

@stale stale bot added the State: stale State: The issue / PR has no activity for >185 days label Aug 10, 2019
@smlng smlng added State: don't stale State: Tell state-bot to ignore this issue and removed State: stale State: The issue / PR has no activity for >185 days labels Aug 12, 2019
@wosym wosym mentioned this pull request Jan 31, 2020
@miri64
Copy link
Copy Markdown
Member

miri64 commented Jun 23, 2020

Closing this PR in favor of #13624.

@miri64 miri64 closed this Jun 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: drivers Area: Device drivers CI: needs squashing Commits in this PR need to be squashed; If set, CI systems will mark this PR as unmergable State: don't stale State: Tell state-bot to ignore this issue State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet Type: new feature The issue requests / The PR implemements a new feature for RIOT

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants