Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 8 months ago.
NUCLEO F302R8 CAN network example
Could someone give me CAN bus example for NUCLEO F302R8 ?
2 Answers
7 years, 8 months ago.
Have you tried setting the CAN module into loopback mode for testing? As Mark says the module rx pin needs to see the bus or initialization fails. If you monitor your printf debug messages it yells at you when this happens. Also did you find the reference manual for that micro? It has helpful details on the CAN peripheral - section 31.
By the way, if you don't have a logic analyzer, the Saleae logic analyzers are really nice little devices (start at $100) for working with serial data like this. Here is a short program that sends and receives dava via loopback. Works on my F091RC board.
/*** Thread: CAN *** * Test CAN Messages */ #include "mbed.h" #include "CAN.h" /* CAN (RD TD) */ CAN can(PA_11, PA_12); /* Heartbeat */ DigitalOut led(LED1); /* Structures to help pack and unpack the 8 CAN data bytes */ typedef struct bytes_64 { uint8_t b0; uint8_t b1; uint8_t b2; uint8_t b3; uint8_t b4; uint8_t b5; uint8_t b6; uint8_t b7; } bytes_64; typedef union { uint64_t data; bytes_64 bytes; } data_packed; /* main */ int main() { printf("\r\n---Start---\r\n"); data_packed send_data; //struct for send message data_packed receive_data; //struct for rcv message CANMessage msg; //message object for rcv int send_id = 10; //send message id int send_status = 0; /* Config Loopback and Frequency */ send_data.data = 0; can.frequency(1000000); can.mode(CAN::LocalTest); /* Loop */ while (true) { /* Send */ send_status = can.write(CANMessage(send_id, (char*) &send_data, 8)); if (send_status == true) { send_data.data = send_data.data + 1; } /* Receive */ if (can.read(msg)) { //move message bytes to receive, so we can access as a uint64_t (u long long) receive_data.bytes.b7 = msg.data[7]; receive_data.bytes.b6 = msg.data[6]; receive_data.bytes.b5 = msg.data[5]; receive_data.bytes.b4 = msg.data[4]; receive_data.bytes.b3 = msg.data[3]; receive_data.bytes.b2 = msg.data[2]; receive_data.bytes.b1 = msg.data[1]; receive_data.bytes.b0 = msg.data[0]; printf("Message received ID,Msg: %d, %llu\r\n", msg.id, receive_data.data); } /* Heartbeat */ led = !led; wait(0.5); } //while }
7 years, 8 months ago.
Have you tried the official CAN example? https://developer.mbed.org/handbook/CAN You should remove the second CAN because the F302 has only one and change CAN pin names to PA_12, PA_11. I have tried and it worked.
Yes, I had tried this example as you said, and I connected TJA1041 as CAN transceiver with the board. But I can't get any message from the transceiver when I transfer CAN message from the board. I want to know the problem is coming from code or hardware, so I think I need another program from different API to make sure where the problem is.
BTW, I tried official program with PB_8 and PB_9 few weeks ago, and it's strange that program can't run when I just used PB_8 and PB_9 to declare a CAN object, but PA_12 and PA_11 worked. And I still don't know why.
posted by 17 Mar 2017Make sure there are at least two CAN nodes connected to the same CAN bus. You won't see any data to be transmitted to the CAN bus from a solitary node since it evaluates the situation as faulty CAN bus. Nevertheless, if you would like to test another CAN API then you can give the CANnucleo library a try. Also have a look at the demo and Wiki page.
posted by 17 Mar 2017Yes, minimum two nodes, same baud rate and bus termination. Can you write more about your CAN bus setup?
About PB_8, PB_9 problem: It is a bug. You have to remove comment from line 229 and 235 in file PeripheralPins.c https://github.com/ARMmbed/mbed-os/blob/master/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F302x8/TARGET_NUCLEO_F302R8/PeripheralPins.c
posted by 17 Mar 2017(For Zoltan Hudak) Thanks for you information. I had tried CANnucleo already, but it still have problems that I can't find device.h in the mebed-dev to setup this API. I checked the connection of CAN nodes, and it seems fine. (For Mark Peter Vargha) my setup of CAN bus test like this
I-7565-H2 is a equipment can show CAN message on my PC, and I'm sure it works fine. I set CAN baud rate at 1000K.
The connection between board and CAN transceiver are
Since this connection was made by my friend, I have no idea about this connection. I supposed that CAN transceiver IC only use two pins(CAN_TX and CAN_RX), so I'm not sure about the connection of other pins like ERR.
posted by 18 Mar 2017I tried CANnucleo and worked on F103 and on F302. You should share a photo of your setup.
Check these: 1. CAN transceiver supply voltage must be 5V. Will not work with 3.3V. 2. CAN Tx from MCU goes to CAN Tx on transceiver. Do not cross connect like serial. 3. If you have a logic analyser, check Rx, TX and bus traffic.
posted by 18 Mar 2017I appreciate your help and I really want to show my CAN setup to you, but it could be a problem because I put all these stuff (Board, CAN transceiver, etc...) in other place. The photo of my setup will be shared after two to three days. And I'll check these points you said when I take my stuff back, thanks.
posted by 18 Mar 2017Hello,
In order to use the CANnucleo library with the NUCLEO-F302R8 board you should modify the
your_project_name/mbed-dev/targets/TARGET_STM/TARGET_STM32F3/device.h
header file as described on the CANnucleo wiki page.
You may also have a look at the CAN_Hello demo about using the mbed built-in API (without the CANnucleo library).