Miac CAN Problem

For MIAC users to discuss projects, applications, and any other issues related to the MIAC unit.

Moderators: Benj, Mods

Post Reply
User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Miac CAN Problem

Post by QMESAR »

Hi Team.
Please help the old man ,I am on the edge of throwing my toys out of the pram :wink:
Not understanding the background workings of the USB slave component I ran in to the following problem with my MIAC and CAN.
Frist is is necessary to say that I can not trigger the CAN send function from the USB Slave firmware(I did and it functions) as from a Windows GUI
the Timing move around and the CAN TX cycle time is not stable enough therefore the Machine sometimes complains CAN Timeout (we see oscillations between 8 and 14mSec with windows controlling the Cycle time ,
Therefore I made a Custom Interrupt with TMR1 (10mSec ) and then CAll a macro to send the CAN message this function spot on cycle time is correct and no issue !
2.jpg
2.jpg (64.5 KiB) Viewed 8454 times
Now the Problem when I now use the the CAN component call inside the macro/Interrupt then the CAN freezes and it seems the USB also

CAN macro called in the TMR1 Interrupt
3.jpg
3.jpg (19.91 KiB) Viewed 8454 times
CAN freezed no transmison and also no USB activity in the labVIEW GUI
1.jpg
1.jpg (24.39 KiB) Viewed 8454 times
Any Ideas why this would happen or comments to achieve my goal would be appreciated?
another concerns is that I only use the CAN component in the TMR1 interrupt however the compiler is complaining that there are multiple calls and it has duplicated the functions the same is for the USBslave component not sure if this is normal or some sort of issue that will byte me later
1.jpg
1.jpg (84.86 KiB) Viewed 8449 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Miac CAN Problem

Post by Benj »

Hi Peter,

Can you post your Flowcode project file. It sounds like there may be a collision happening.

The USB code is driven using an interrupt as is the timer code. If you are inside a CAN function as part of the timer interrupt and then a USB interrupt happens and goes into the CAN code again then this could have unwanted consequences, such as lock ups. Though this is purely a guess.

What about using the timer/usb interrupts to set a flag and then checking that flag in your main loop before calling the CAN code. This way the collision cannot happen.

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Miac CAN Problem

Post by QMESAR »

Benj wrote:What about using the timer/usb interrupts to set a flag and then checking that flag in your main loop before calling the CAN code. This way the collision cannot happen.
Ben Thanks
I agree with this and it is as we always do this however with this USBslave firmware from MATRIX there is no main loop to set a flag in ,if this is allowed to Run a main loop I for sure go this way.
I check there is no code that could cause collisions
Just another concern working with the custom interrupts for the first time in FC, I seem to miss how FC handle the High and low priority because
if I could use USB for Priority High and TMR1 for low it could also help.
Any case here is the project I will be great full if you guys can help me out with all of this confusion
MiacUSB Slave_Firmware.rar
(257.82 KiB) Downloaded 249 times
Here are the MATRIX Source Main macro in my understanding a main loop is not used or will help as the comment says
this is a blocking function once you start the slave service it always sit in the slave service .
big confusion for the old man here :D
5.jpg
5.jpg (64.67 KiB) Viewed 8436 times

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Miac CAN Problem

Post by QMESAR »

Team.
Just checked the UsbSlave Source (Firmware) Matrix project has this multiple call graph problem in both FC7 and FC 8
Not sure if this is an issue or not just saying
5.jpg
5.jpg (55.1 KiB) Viewed 8437 times

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Miac CAN Problem

Post by QMESAR »

Hi Ben.
I have tried to add a while loop and do the Flag in the Interrupt and execute the CAN in the main(while) loop however as mentioned above it seems the USB slave RunSlaveService is a blocking function my code never enters the main loop (a bit expected as the comment from MATRIX in the source code shows
:D I am in big trouble believe :D :D me
1.jpg
1.jpg (56.63 KiB) Viewed 8426 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Miac CAN Problem

Post by Benj »

Hi Peter,

Yes you're right I hadn't thought of that but yes the RunSlaveService macro is blocking so you can't do a while 1 loop.

So instead can you do this. Take any CAN related calls out of the USB Slave service macro. Instead set global variables to flag that CAN activity needs to happen.

Next using your timer interrupt monitor the variables and service them before clearing them ready for the next request.

This way the CAN SPI should only be being used in one interrupt macro and so shouldn't clash.

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Miac CAN Problem

Post by QMESAR »

Instead set global variables to flag that CAN activity needs to happen.
Next using your timer interrupt monitor the variables and service them before clearing them ready for the next request.
I afraid the old man lost you here completely :oops:
Where should I place the variables (I need to read the CAN TX buffers take out the 16 data bytes ) then process them and send them to the PC over the USB link ,I also need to take out 9 bytes from the USB slave (send by the PC) load them into the CAN TX and send them out .
Please do me favor and just look at the attached project :oops:

Edit.
Ben I checked now and all CAN calls are removed from the USB SlaveService once I place a CAN call in the TMR Interrupt the mulitiple call graph warning comes up.
new project
MiacUSB Slave_Firmware.rar
(256 KiB) Downloaded 233 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Miac CAN Problem

Post by Benj »

Hi Peter,

The reentrant warning is because the CAN init is called in the main and the CAN functions are called in the interrupt. Both use the SPI routines but there can be no collision because the init finishes before the interrupt is enabled.

I tested the warnings go away by adding the init to the interrupt on first call.
MIAC_eBooster_Firmware.fcfx
(60.66 KiB) Downloaded 245 times

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Miac CAN Problem

Post by QMESAR »

Hi Ben.

Thank you very much :D however I am still lost for the collision between the TMR1 and the USB interrupts :oops:

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Miac CAN Problem

Post by Benj »

In your timer macro do the check RX and store the return into a variable. If there is a message waiting then also store the ID and data bytes. Access these variables in the USB slave macro.

Similarly in the USB slave macro when you are asked to send a CAN message simply add the ID and data to different variables and check these variables as part of your timer interrupt.

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Miac CAN Problem

Post by QMESAR »

Ben.
Thank you a million ,understood now I am with you :D :D
I will give it all ago tomorrow it is already late with us

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Miac CAN Problem

Post by QMESAR »

Hi Ben.
Slowly we are getting off the hook now :D implemented what we discussed and the USB and CAN are working now and the CAN cycle time is spot on.
Thank you very much once again you helped me a great deal ,Appreciated :D

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Miac CAN Problem

Post by Benj »

Hi Peter,

Brilliant glad you're making progress. Thanks for letting us know.

Post Reply