Wilk96
Posts: 6
Joined: Mon Jun 04, 2018 12:23 pm

Interrupt from PICAN2 does not work

Mon Jun 04, 2018 12:53 pm

Hello, everybody,

this is my first post in this forum. I have a Raspberry Pi 3b with the kernel version 4.9.35 v7 (Raspbian Jessie). For this I bought a PICAN2 board to receive CAN messages. I set it up according to the manufacturer (/boot/config.txt and initialized the CAN interface).
Using the socket can drivers I can receive messages (./candump can0).

Now I want to write a program in C/C++, which evaluates an incoming CAN message depending on the ID. This should happen within an interrupt. According to the manufacturer, the PICAN2 should have an Rx interrupt on GPIO 25. This is not sent by me.

I tried the ISR with wiringPi and a hand switch, which supplies GPIO25 from pin 17 with 3.3 volts, and pulls when pressing PIN 9 (ground). The ISR is called here.

Is there a step I forgot to take to get the interrupt triggered? Does the PICAN2 board also have to be adapted?

Thank you for your help.

Friendly greetings
Wilk96

Pi Flyer
Posts: 48
Joined: Sat Jun 04, 2016 1:57 pm

Re: Interrupt from PICAN2 does not work

Mon Jun 04, 2018 3:58 pm

Are you sure you want to use an interrupt?

I've had quite a bit of success with the PICAN2 hat, but was interfacing it with Python via the python-can module. The buffered reader in that class works great...you'll just have to filter out the messages with the ID you want.

https://python-can.readthedocs.io/en/2.1.0/

Wilk96
Posts: 6
Joined: Mon Jun 04, 2018 12:23 pm

Re: Interrupt from PICAN2 does not work

Tue Jun 05, 2018 6:26 am

Thank you for your answer.

Yes, this should be done using an Rx interrupt, as each message should be evaluated as soon as possible. Furthermore, the whole thing should be realized in C/C++, because I want to build a GUI with CodeBlocks (wxSmith) later.

User avatar
mark3112
Posts: 46
Joined: Mon Apr 09, 2018 10:39 am

Re: Interrupt from PICAN2 does not work

Tue Jun 05, 2018 9:21 am

Have you set the RXnIE bit in the CANINTE register?
I have a pi connected to 4 MCP2515 and initially used interrupts and wiringPI. A problem I hit straight away was the restriction of a maximum of two interrupts and the fact the interrupt handling routine was itself interruptable, which caused havoc with the chip select lines to the CAN controllers.
Now using pigpio and am using callbacks instead and that works far better.
You can make a real CPU in a FPGA, but you can’t make a real FPGA in a CPU.

Wilk96
Posts: 6
Joined: Mon Jun 04, 2018 12:23 pm

Re: Interrupt from PICAN2 does not work

Tue Jun 05, 2018 10:48 am

I think I found the Problem:
The Interrupt on GPIO25 works fine from PICAN2, but it might be too fast for wiringPi to find it. I just measured the GPIO25 with an oscilloscope while receiving some CAN-Messages.

Now I am searching for an alternative library to wiringPi, that can find Interrupts much faster.

Wilk96
Posts: 6
Joined: Mon Jun 04, 2018 12:23 pm

Re: Interrupt from PICAN2 does not work

Tue Jun 05, 2018 12:36 pm

I tried to solve the problem with the pigpio library. It has worked, but the function "isr", in which a counter is increased by 1. Based on this, I see that the counter for each message increases twice (because of the falling and rising edge?).

Is there a possibility to consider only the falling edge with pigpio?

User avatar
mark3112
Posts: 46
Joined: Mon Apr 09, 2018 10:39 am

Re: Interrupt from PICAN2 does not work

Tue Jun 05, 2018 6:51 pm

“The Interrupt on GPIO25 works fine from PICAN2, but it might be too fast for wiringPi to find it.”
I don’t understand that, how can the signal be too fast when it’s software that resets the INT Line on MCP2515 - GPIO25?
It could be a driver is reading the data and resetting the INT line?

I drive 4 CAN Controllers on 1 SPI Bus with 2 chip selects and can drive each bus at 100% with TX and RX interrupts and haven’t been able to fault the response of wiringPi or pigpio using Raspbian Lite. This is used in a commercial lighting controller that is in production, if there were problems I would know.

“because of the falling and rising edge?”
I use gpioSetAlertFunc and set it for Change To Low/Falling Edge
You can make a real CPU in a FPGA, but you can’t make a real FPGA in a CPU.

Pi Flyer
Posts: 48
Joined: Sat Jun 04, 2016 1:57 pm

Re: Interrupt from PICAN2 does not work

Tue Jun 05, 2018 7:01 pm

Wilk96 wrote:
Tue Jun 05, 2018 6:26 am
Thank you for your answer.

Yes, this should be done using an Rx interrupt, as each message should be evaluated as soon as possible. Furthermore, the whole thing should be realized in C/C++, because I want to build a GUI with CodeBlocks (wxSmith) later.
How fast do you need to process the messages? I don't believe CAN in general to be deterministic.

Point is, I've never utilized an interrupt because the bus had all kinds of data on it, and it needed to be queued to handle it all.

Wilk96
Posts: 6
Joined: Mon Jun 04, 2018 12:23 pm

Re: Interrupt from PICAN2 does not work

Wed Jun 06, 2018 11:54 am

As fast as I can, I would like to interpret the data from the messages to the main Programm, that this can work with the latest data.

The pigpio should also fit to my Options.

I am trying to work with it, to solve my Problems.

Wilk96
Posts: 6
Joined: Mon Jun 04, 2018 12:23 pm

Re: Interrupt from PICAN2 does not work

Wed Jun 06, 2018 1:18 pm

I think, I got something, that solves my problem: pigpio

But I have another question now...
Could u explain me the difference between the pigpio-functions:

-gpioSetAlertFunc
-gpioSetISRFunc

User avatar
mark3112
Posts: 46
Joined: Mon Apr 09, 2018 10:39 am

Re: Interrupt from PICAN2 does not work

Wed Jun 06, 2018 6:19 pm

Looking at the pigpio documentation:

“gpioSetAlertFunc - [Polled]
The GPIO are sampled at a rate set when the library is started.

gpioSetISRFunc – [Event Driven]
The GPIO is exported, set to be an input, and set to interrupt on the given edge and timeout.”

Although I’m sure Joan could supply a better explanation.

To me, that seems way too simplistic in the Linux/ARM/Multi-Core environment of the Pi (am already considering performance tests of the two methods).

I’m from the very, old school, embedded, background. To me, you don’t put complex code in the IRQ Handler. But on the Pi it doesn’t seem to matter as the IRQ thread starts up on a separate core! (I had an argument with the Application Software Engineer, challenged to test the impact of complex IRQ handlers over setting a flag and handling the event in main(), I lost.

As the sampling rate is 5ns and even the IRQ response can’t be relied upon, does it really matter whether the detection method uses (very fast) polling or an IRQ generated response?
For CAN packets, I doubt it, unless you have an insane baud rate.

As I mentioned earlier, I had problems implementing multiple IRQ sources using wiringPi and turned to pigpio only when I needed to use the second SPI bus. That’s when I started using gpioSetAlertFunc. If you are only using 1 interrupt I can’t see there would be any significant difference between the two commands.
Even then, I don’t know if the problem lies in Raspbian’s implementation of IRQ’s or wiringPi. But as Raspbian isn’t designed to be a real-time operating system, some delays/restrictions are to be expected unless you go bare metal, and then I would suggest there are MCU’s better configured to your requirements (but the HMDI/Stereo/WiFi/Ethernet/Serial/Bluetooth/USB access, just makes the Pi a simple ‘black box’ solution to so many problems).
You can make a real CPU in a FPGA, but you can’t make a real FPGA in a CPU.

Return to “Interfacing (DSI, CSI, I2C, etc.)”