8 years, 8 months ago.

CAN library example code fails to compile


Beginner on mbed, I tried to test the example code given in the library mbed (mbed > Classes > CAN).

Working on NUCLEO F334R8, I : 1) imported the "Nucleo_printf" example program (and the program worked) 2) replaced the code within main.cpp by the following code 3) compiled and got some strange errors

I would really appreciate help of any kind.

Code :

#include "mbed.h"

Ticker ticker;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
CAN can1(p9, p10) ;  // CAN can1( PA_11 , PA_12 );
CAN can1(p9, p10) ;  // CAN can2( PB_12 , PB_13) ;

char counter = 0 ;

void send () 
if (can1.write(CANMessage (1337, &cpt, 1 )))
        printf ("Message sent: %d\n", counter ) ;
        counter++ ;
    led1 = ! led1 ;

int main() 
    ticker.attach (&send, 1) ; 
    CANMessage msg ;
            printf("Message received : %d\n\n", msg.data[0]);
            led2 = !led2 ;
        wait (0.2) ; 

Errors : /media/uploads/Nio/error_report.png

Best regards, Nio

4 Answers

7 years, 9 months ago.

Hello, I think you didn't declare CAN2 port, you just comment can1 and can 2, can1 is declared twice.... :-) CAN can1(p9, p10) ; CAN can1( PA_11 , PA_12 ); CAN can1(p9, p10) ; CAN can2( PB_12 , PB_13) ;

8 years, 6 months ago.

So, I got around this problem only to find more.

So some how there is some pre-compilation or the compiler or something programmitcally is using the capital "C" as a keyword. If u rename to lower case "c" the issue goes away but you open a six pack each with its own can of worms.

Keep in mind this library is for the micro-controller LPC1768 so if you are using say a ARMSTM32F303 than you will need to write your own api_can.c functions to call the correct HAL functions. There is a good example, in mbed editor go to find programs and look for "CAN_Nucleo_Hello" to get an idea, but you will problaly need to download the mbed source and import or create the nessary files to overwrite what you need, I am still in process with this whole thing.

Suerte! Kiko

8 years, 6 months ago.

Hi William,

In case NUCLEO-F103RB or compatible boards are available to you then you might give CAN_Nucleo_Hello a try. For more details also have a look at the CANnucleo library.


8 years, 6 months ago.

The CANNucleo Library doesn't works with NUCLEO F334R8. I think there is a problem in the mbed lib because in STM32f334x8.h there is:

/#define CAN ((CAN_TypeDef *) CAN_BASE)

yeah bruno is right, all you need to do is re-name CAN to can in the nessary files, except above, and it will compile, ive done this and it works.

posted by frank madero 24 Nov 2015

Ok, I stopped being lazy and looked into this. While other problems exists, this is the most destructive.

Remember that #define CAN will replace anything the compiler sees with that matches the string "CAN" and since the can class we use is named "CAN" then kaboom!.

So rename the file and the class name and methods, all those that have "CAN" alone should suffice, I took the easy way out and renamed everything including the file to "CANBus".

The line #if DEVICE_CAN for stm32f303 hasnt been defined, so I dropped it into to device.h.

I have more but this is all I can remember for now, hopefully it can get someone started.


posted by frank madero 25 Nov 2015

NUCLEO-F334R8 board has been added to the CANnucleo library.

posted by Zoltan Hudak 30 Dec 2015