Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dual CAN #1

Open
milesfrankland opened this issue Jun 4, 2018 · 16 comments
Open

Dual CAN #1

milesfrankland opened this issue Jun 4, 2018 · 16 comments

Comments

@milesfrankland
Copy link

Hi, the SAMC21 has 2 CAN ports - will this work with both at the same time?

@prices
Copy link
Member

prices commented Jun 4, 2018

Theoretically if you make a different object for each one. That being said, I have not ever tried it.

@mfvmx
Copy link

mfvmx commented May 29, 2019

I tried pointing samc21_can.cpp to the new pins for the second CANbus (PB10 & PB11 on a C21G) and group with no response. Also tried the same in Atmel Studio with no luck. I can't find any example from Atmel or otherwise of the second CANbus channel working.

@prices
Copy link
Member

prices commented May 29, 2019

Yeah, looking at this again it is going to take some work to even make it work easily with CAN1, let alone with both at the same time. I can start looking into it, but I am really busy right now. I don't know how long it might take me.

prices added a commit that referenced this issue May 29, 2019
…different pins. It might allow both CAN to be used.
prices added a commit that referenced this issue May 29, 2019
… hard coded it that way. This way only the TX pin needs to be specified. The software expects a pair to be used like that and currently doesn't support a split port.
@prices
Copy link
Member

prices commented May 29, 2019

I modified it so that you should be able to specify CAN0 or CAN1, and set the pins to use. Only the TX pin needs to be specified because the software currently can only deal with the pins as a pair. I don't have an easy way to test that this works with CAN1. I can only tell you that it works as it used to with CAN0. Please let me know if it works for you.

@mfvmx
Copy link

mfvmx commented May 30, 2019

Thanks, I will try this tomorrow. For my CAN0 I am using PB22 & PB23 so in the old code I had to change:
PORT->Group[0].DIRSET.reg = PORT_PA24; to
PORT->Group[1].DIRSET.reg = PORT_PB22;
so it might be worth passing in a Group variable as well as the pin number.

@prices
Copy link
Member

prices commented May 30, 2019

That's what I get for doing it so quickly. Thanks for pointing it out. It now has a group argument for the constructor.

@naprave
Copy link

naprave commented Oct 15, 2020

Hello, firstly thank you for your work, but CAN1 does not work out of the box and I need it for my project:) I would love to help get the lib tested with CAN1, can you provide me with a theoretical working example for CAN1 use?
I'm using SAMC21_CAN can(0, ID_CAN1, PORT_PB10, 1); (is this the correct pin/port input format, i've tried them all)
and
ret = can.begin(MCP_ANY, CAN_100KBPS, MCP_8MHZ);
which fails with CAN_FAIL in the end of begin ("Something went wrong!!!!!!!!").
I'm using a SAMC21G18A.
I'm suspicious about this line:
PORT->Group[_group].PMUX[_cantx / 2].reg = PORT_PMUX_PMUXE(6 /* CAN0 G /) | PORT_PMUX_PMUXO(6 / CAN0 G */);

Thank you!

@prices
Copy link
Member

prices commented Oct 15, 2020

Can you try changing line 101 in samc21_can.cpp to:

GCLK->PCHCTRL[CAN1_GCLK_ID].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK0;

@prices
Copy link
Member

prices commented Oct 15, 2020

Also, the cantx parameter would be 10 for PB10, with a group of 1. PORT_PB10 is a bit mask, not a pin number.

@naprave
Copy link

naprave commented Oct 15, 2020

Thank you for your quick reply! The initialization now works, but there is nothing happening on pin PB10 (it's just high). I used SAMC21_CAN can(0, ID_CAN1, PIN_PB10, 1);
I'm using arduino, board xeno mini. 32khz crystal, usb disabled, two uart one wire one spi, no bootloader, timer 732hz.
PORT_PB10 is 1024 (2^10), PIN_PB10 is 42, is this line OK?
PORT->Group[_group].DIRSET.reg = (1 << _cantx);
Shouldn't it be group 1, pin 10?

@naprave
Copy link

naprave commented Oct 15, 2020

Yes setting the cantx parameter to 10 is now switching the value and CAN1 seems to be working!
if (group == 1) cantx -= something (PB02)?

@prices
Copy link
Member

prices commented Oct 15, 2020

Yes setting the cantx parameter to 10 is now switching the value and CAN1 seems to be working!

Glad to hear it!

if (group == 1) cantx -= something (PB02)?

I am not sure what you are trying to get at here.

@naprave
Copy link

naprave commented Oct 15, 2020

Well each group starts with 0 for pin numbers, the PORT_PB10 is 1024 meaning the pin is numbered as 10 in group B.

But the PIN_PB10 constant is absolute across ports and is 42 and should be converted to the per port value in its group, which is 10, when accessing specific group registers.

So the working init is now:
SAMC21_CAN can(0, ID_CAN1, 10, 1);
The cantx pin parameter should be the group specific number of the port, not the absolute pin number, as the code currently works.

And if you wanted to use PIN_PB10 (for any group>0) you would have to convert to group specific numbering in the library.

@prices
Copy link
Member

prices commented Oct 15, 2020

I could do that, but I will have to make sure I don't break the current API.

@naprave
Copy link

naprave commented Oct 15, 2020

Well to keep the api oblivious to ports you could get the pins group from the wiring:
./wiring_digital.c: uint8_t pinPort = GetPort(ulPin);

Or keep the API the same, but either way you would have to convert the pin's absolute number to its group specific number, like digitalWrite does:
void digitalWrite( uint32_t ulPin, uint32_t ulVal )
{
uint8_t pinPort = GetPort(ulPin);
uint8_t pinNum = GetPin(ulPin);

Thanks again for your efforts.

@prices
Copy link
Member

prices commented Oct 15, 2020

I moved the API change to bug #4 as it is not within the scope of this bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants