CAN2 on STM32F4-Discovery

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

grant
Posts: 14
Joined: Wed Jan 16, 2013 1:59 am

CAN2 on STM32F4-Discovery

Post by grant »

Has anyone else been successful in getting the second CAN channel working on the STM32F4-Discovery board?

I have tried so much as modifying Giovanni's source code (/os/hal/platforms/STM32/can_lld.[h|c]) to add specialized handler code for CAN2 and still no luck. My test also included modifying the /testhal/STM32F4xx/CAN example to make pairs of the four lights blink whenever messages are sent/received in loopback mode. Sadly only three of the lights work since the messages are not coming back on CAN2.

This is the only thing standing in the way from me moving from projects written using the STM32 libraries. Once I get this fixed, I'll be proudly adopting ChibiStudio/SPC5Studio for future development.

Suggestions?

Also, I modified /boards/ST_STM32F4_DISCOVERY/cfg/board.chcfg as shown below:

Code: Select all

diff --git a/boards/ST_STM32F4_DISCOVERY/cfg/board.chcfg b/boards/ST_STM32F4_DISCOVERY/cfg/board.chcfg
index 68d30e2..18c985f 100644
--- a/boards/ST_STM32F4_DISCOVERY/cfg/board.chcfg
+++ b/boards/ST_STM32F4_DISCOVERY/cfg/board.chcfg
@@ -240,21 +240,21 @@
         Mode="Input"
         Alternate="0" />
       <pin12
-        ID=""
+        ID="CAN2_RX"
         Type="PushPull"
         Level="High"
         Speed="Maximum"
         Resistor="PullUp"
-        Mode="Input"
-        Alternate="0" />
+        Mode="Alternate"
+        Alternate="9" />
       <pin13
-        ID=""
+        ID="CAN2_TX"
         Type="PushPull"
         Level="High"
         Speed="Maximum"
         Resistor="PullUp"
-        Mode="Input"
-        Alternate="0" />
+        Mode="Alternate"
+        Alternate="9" />
       <pin14
         ID=""
         Type="PushPull"
@@ -404,21 +404,21 @@
     </GPIOC>
     <GPIOD>
       <pin0
-        ID=""
+        ID="CAN1_RX"
         Type="PushPull"
         Level="High"
         Speed="Maximum"
         Resistor="PullUp"
-        Mode="Input"
-        Alternate="0" />
+        Mode="Alternate"
+        Alternate="9" ></pin0>
       <pin1
-        ID=""
+        ID="CAN1_TX"
         Type="PushPull"
         Level="High"
         Speed="Maximum"
         Resistor="PullUp"
-        Mode="Input"
-        Alternate="0" />
+        Mode="Alternate"
+        Alternate="9" ></pin1>
       <pin2
         ID=""
         Type="PushPull"
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: CAN2 on STM32F4-Discovery

Post by Giovanni »

Hi,

I can give it a try in the weekend, thanks for reporting.

Giovanni
grant
Posts: 14
Joined: Wed Jan 16, 2013 1:59 am

Re: CAN2 on STM32F4-Discovery

Post by grant »

My board.chcfg may be wrong. I am porting over a project from an Olimex STM32-H107 and the pin descriptions are slightly different between the STM32 library versions.

Thanks for the support Giovanni! I'm be happy to collaborate to get this thing working. I can see about sending my efforts thus far if it matters to you.

Cheers!
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: CAN2 on STM32F4-Discovery

Post by Giovanni »

Hi,

If you can send me the modified file I'll try to integrate it next week. I committed changes to the STM32F4 CAN demo to use two CANs if you want to give it a try, just uncomment the lines.

Giovanni
grant
Posts: 14
Joined: Wed Jan 16, 2013 1:59 am

Re: CAN2 on STM32F4-Discovery

Post by grant »

Using the mabl git repo (thanks Matthias), I did a sync and pulled your changes.

The problem is that there is no "CAND2" in the system. That's what I had to add previously, but it didn't seem to work.

I've attached my progress so far as a series of patches: 4 of them, applied in reverse order. I'm sure the problem is in can_ll.c somewhere...

Thoughts?
Attachments
STM32F4-DiscoveryCAN2_ChibiOS_patches.zip
(12.26 KiB) Downloaded 718 times
grant
Posts: 14
Joined: Wed Jan 16, 2013 1:59 am

Re: CAN2 on STM32F4-Discovery

Post by grant »

Any news on figuring out what is keeping these messages from being received on CAN2?
Do my patches help or hinder?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: CAN2 on STM32F4-Discovery

Post by Giovanni »

Hi,

Not tested yet, stay tuned :)

Giovanni
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: CAN2 on STM32F4-Discovery

Post by Giovanni »

Hi,

I am unable to use those patches, Tortoise SVN does not seem to recognize the file format, also it is not clear on which revisions the patches should be applied, for example the patch 0001-One-more-attempt-plus-removing-a-compiler-warning.patch modifies code that is not even on the repository.

Giovanni
grant
Posts: 14
Joined: Wed Jan 16, 2013 1:59 am

Re: CAN2 on STM32F4-Discovery

Post by grant »

Not a problem. What patch format do you prefer?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: CAN2 on STM32F4-Discovery

Post by Giovanni »

I use Tortoise SVN, not sure how those patches are named.

Anyway, I studied the STM32 RM section regarding CANs and I have some doubts (and remembered why I didn't support CAN2 in first place).

1) CAN2 is a slave of CAN1, the two units share filters. This does not fit well with ChibiOS because drivers configuration structures are meant to not affect other unrelated units. With shared filters configuring CAN1 would affect CAN2.

2) Filter registers belong to the CAN1 registers block, does this mean that I have to activate CAN1 clock in order to use CAN2? can CAN2 be used independently from CAN1 at all?

About point 1, a specific function could be added to handle filters, nuch like the CAN_FilterInit() in the library, the canStart() configuration structure would no more handle filters. The function would be STM32-specific and added to the LLD, no changes to the HLD would be required. The function would be called before calling canStart().

About point 2 I am not sure, is CAN1 clock required during CAN2 operations? this would create interesting corner cases while doing canStart()/canStop() on both CANs.

Probably the problems you met are caused by one of the above issues.
Post Reply